1 /* x3ldap.c - LDAP functionality for x3, by Rubin
2 * Copyright 2002-2007 x3 Development Team
4 * This file is part of x3.
6 * x3 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * * get queries working in static existance, so i understand how it works
23 * * get ldap enabled in ./configure
24 * * x3.conf settings to enable/configure its use
25 * * generic functions to enable ldap
26 * * nickserv.c work to use said functions.
42 extern struct nickserv_config nickserv_conf
;
46 int admin_bind
= false;
50 if(!nickserv_conf
.ldap_enable
)
52 /* TODO: check here for all required config options and exit() out if not present */
53 //ld = ldap_init(nickserv_conf.ldap_host, nickserv_conf.ldap_port);
56 if(ldap_initialize(&ld
, nickserv_conf
.ldap_uri
)) {
57 log_module(MAIN_LOG
, LOG_ERROR
, "LDAP initilization failed!\n");
60 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &nickserv_conf
.ldap_version
);
61 log_module(MAIN_LOG
, LOG_INFO
, "Success! ldap_init() was successfull in connecting to %s\n", nickserv_conf
.ldap_uri
);
66 /* Try to auth someone. If theres problems, try reconnecting
67 * once every 10 seconds for 1 minute.
68 * TODO: move this stuff to config file
70 unsigned int ldap_do_bind( const char *dn
, const char *pass
)
76 q
= ldap_simple_bind_s(ld
, dn
, pass
);
77 if(q
== LDAP_SUCCESS
) {
78 log_module(MAIN_LOG
, LOG_DEBUG
, "bind() successfull! You are bound as %s", dn
);
82 else if(q
== LDAP_INVALID_CREDENTIALS
) {
86 log_module(MAIN_LOG
, LOG_ERROR
, "Bind failed: %s/****** (%s)", dn
, ldap_err2string(q
));
87 /* ldap_perror(ld, "ldap"); */
91 /* TODO: return to the user that this is a connection error and not a problem
94 log_module(MAIN_LOG
, LOG_ERROR
, "Failing to reconnect to ldap server. Auth failing.");
98 log_module(MAIN_LOG
, LOG_ERROR
, "ldap_do_bind falling off the end. this shouldnt happen");
101 int ldap_do_admin_bind()
104 if(!(nickserv_conf
.ldap_admin_dn
&& *nickserv_conf
.ldap_admin_dn
&&
105 nickserv_conf
.ldap_admin_pass
&& *nickserv_conf
.ldap_admin_pass
)) {
106 log_module(MAIN_LOG
, LOG_ERROR
, "Tried to admin bind, but no admin credentials configured in config file. ldap_admin_dn/ldap_admin_pass");
107 return LDAP_OTHER
; /* not configured to do this */
109 rc
= ldap_do_bind(nickserv_conf
.ldap_admin_dn
, nickserv_conf
.ldap_admin_pass
);
110 if(rc
== LDAP_SUCCESS
)
116 unsigned int ldap_check_auth( const char *account
, const char *pass
)
120 if(!nickserv_conf
.ldap_enable
)
123 memset(buff
, 0, MAXLEN
);
124 snprintf(buff
, sizeof(buff
)-1, nickserv_conf
.ldap_dn_fmt
/*"uid=%s,ou=Users,dc=afternet,dc=org"*/, account
);
126 return ldap_do_bind(buff
, pass
);
130 int ldap_search_user(const char *account
, LDAPMessage
**entry
)
133 char filter
[MAXLEN
+1];
137 struct timeval timeout
;
139 memset(filter
, 0, MAXLEN
+1);
140 snprintf(filter
, MAXLEN
, "%s=%s", nickserv_conf
.ldap_field_account
, account
);
145 timeout
.tv_sec
= nickserv_conf
.ldap_timeout
;
146 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
147 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
150 if( (rc
= ldap_search_st(ld
, nickserv_conf
.ldap_base
, LDAP_SCOPE_ONELEVEL
, filter
, NULL
, 0, &timeout
, &res
)) != LDAP_SUCCESS
) {
151 log_module(MAIN_LOG
, LOG_ERROR
, "search failed: %s %s: %s", nickserv_conf
.ldap_base
, filter
, ldap_err2string(rc
));
154 log_module(MAIN_LOG
, LOG_DEBUG
, "Search successfull! %s %s\n", nickserv_conf
.ldap_base
, filter
);
155 if(ldap_count_entries(ld
, res
) != 1) {
156 log_module(MAIN_LOG
, LOG_DEBUG
, "LDAP search got %d entries when looking for %s", ldap_count_entries(ld
, res
), account
);
157 return(LDAP_OTHER
); /* Search was a success, but user not found.. */
159 log_module(MAIN_LOG
, LOG_DEBUG
, "LDAP search got %d entries", ldap_count_entries(ld
, res
));
160 *entry
= ldap_first_entry(ld
, res
);
164 /* queries the ldap server for account..
165 * if a single account match is found,
166 * email is allocated and set to the email address
167 * and returns LDAP_SUCCESS. returns LDAP_OTHER if
168 * 0 or 2+ entries are matched, or the proper ldap error
169 * code for other errors.
171 int ldap_get_user_info(const char *account
, char **email
)
174 struct berval
**value
;
175 LDAPMessage
*entry
, *res
;
178 if( (rc
= ldap_search_user(account
, &res
)) == LDAP_SUCCESS
) {
179 entry
= ldap_first_entry(ld
, res
);
180 value
= ldap_get_values_len(ld
, entry
, nickserv_conf
.ldap_field_email
);
185 *email
= strdup(value
[0]->bv_val
);
186 log_module(MAIN_LOG
, LOG_DEBUG
, "%s: %s\n", nickserv_conf
.ldap_field_email
, value
[0]->bv_val
);
187 ldap_value_free_len(value
);
189 value = ldap_get_values(ld, entry, "description");
190 log_module(MAIN_LOG, LOG_DEBUG, "Description: %s\n", value[0]);
191 value = ldap_get_values(ld, entry, "userPassword");
192 log_module(MAIN_LOG, LOG_DEBUG, "pass: %s\n", value ? value[0] : "error");
201 ldap_first_attribute();
204 ldap_next_attribute();
211 /* get errors with ldap_err2string(); */
214 /********* base64 stuff ***********/
216 unsigned char *pack(const char *str
, unsigned int *len
)
221 static unsigned char buf
[MAXLEN
+1];
224 memset(buf
, 0, MAXLEN
+1);
229 if((n
>= '0') && (n
<= '9')) {
231 } else if ((n
>= 'A') && (n
<= 'F')) {
233 } else if ((n
>= 'a') && (n
<= 'f')) {
236 printf("pack type H: illegal hex digit %c", n
);
241 buf
[++outputpos
] = 0;
246 buf
[outputpos
] |= (n
<< nibbleshift
);
247 nibbleshift
= (nibbleshift
+ 4) & 7;
253 char **make_object_vals()
256 static char **object_vals
= NULL
;
261 object_vals
= malloc(sizeof( *object_vals
) * (nickserv_conf
.ldap_object_classes
->used
+1));
263 for(y
= 0; y
< nickserv_conf
.ldap_object_classes
->used
; y
++) {
264 object_vals
[y
] = nickserv_conf
.ldap_object_classes
->list
[y
];
266 object_vals
[y
] = NULL
;
268 /* NOTE: The return value of this is only good until the next call to it. */
271 char *make_password(const char *crypted
)
274 unsigned char *packed
;
278 packed
= pack(crypted
, &len
);
279 base64_encode_alloc(packed
, len
, &base64pass
);
280 passbuf
= malloc(strlen(base64pass
) + 1 + 5);
281 strcpy(passbuf
, "{MD5}");
282 strcat(passbuf
, base64pass
);
283 //log_module(MAIN_LOG, LOG_DEBUG, "Encoded password is: '%s'", passbuf);
289 LDAPMod
**make_mods_add(const char *account
, const char *password
, const char *email
, int *num_mods_ret
)
291 static char *account_vals
[] = { NULL
, NULL
};
292 static char *password_vals
[] = { NULL
, NULL
};
293 static char *email_vals
[] = { NULL
, NULL
};
297 /* TODO: take this from nickserv_conf.ldap_add_objects */
299 static char **object_vals
;
301 account_vals
[0] = NULL
;
302 account_vals
[1] = NULL
;
303 password_vals
[0] = NULL
;
304 password_vals
[1] = NULL
;
305 email_vals
[0] = NULL
;
306 email_vals
[1] = NULL
;
307 object_vals
= make_object_vals();
309 account_vals
[0] = (char *) account
;
310 if (password
!= NULL
) {
311 password_vals
[0] = (char *) password
;
314 email_vals
[0] = (char *) email
;
316 if(!(nickserv_conf
.ldap_field_account
&& *nickserv_conf
.ldap_field_account
))
317 return 0; /* account required */
318 if(!(nickserv_conf
.ldap_field_password
&& *nickserv_conf
.ldap_field_password
))
319 return 0; /* password required */
320 if(email
&& *email
&& nickserv_conf
.ldap_field_email
&& *nickserv_conf
.ldap_field_email
)
323 mods
= ( LDAPMod
** ) malloc(( num_mods
+ 1 ) * sizeof( LDAPMod
* ));
324 for( i
= 0; i
< num_mods
; i
++) {
325 mods
[i
] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
326 memset(mods
[i
], 0, sizeof(LDAPMod
));
329 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
330 mods
[mod
]->mod_type
= strdup("objectclass");
331 mods
[mod
]->mod_values
= object_vals
;
334 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
335 mods
[mod
]->mod_type
= strdup(nickserv_conf
.ldap_field_account
);
336 mods
[mod
]->mod_values
= account_vals
;
339 if (password
!= NULL
) {
340 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
341 mods
[mod
]->mod_type
= strdup(nickserv_conf
.ldap_field_password
);
342 mods
[mod
]->mod_values
= password_vals
;
346 if(nickserv_conf
.ldap_field_email
&& *nickserv_conf
.ldap_field_email
&& email
&& *email
) {
347 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
348 mods
[mod
]->mod_type
= strdup(nickserv_conf
.ldap_field_email
);
349 mods
[mod
]->mod_values
= email_vals
;
353 *num_mods_ret
= num_mods
;
357 int ldap_do_add(const char *account
, const char *crypted
, const char *email
)
365 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
366 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
371 passbuf
= make_password(crypted
);
372 snprintf(newdn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
373 mods
= make_mods_add(account
, (crypted
!= NULL
? passbuf
: crypted
), email
, &num_mods
);
375 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for ldap_add");
378 rc
= ldap_add_ext_s(ld
, newdn
, mods
, NULL
, NULL
);
379 if(rc
!= LDAP_SUCCESS
&& rc
!= LDAP_ALREADY_EXISTS
) {
380 log_module(MAIN_LOG
, LOG_ERROR
, "Error adding ldap account: %s -- %s", account
, ldap_err2string(rc
));
384 for(i
= 0; i
< num_mods
; i
++) {
385 free(mods
[i
]->mod_type
);
394 int ldap_delete_account(char *account
)
399 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
400 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
404 memset(dn
, 0, MAXLEN
);
405 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
406 return(ldap_delete_s(ld
, dn
));
409 int ldap_rename_account(char *oldaccount
, char *newaccount
)
411 char dn
[MAXLEN
], newdn
[MAXLEN
];
414 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
415 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
419 memset(dn
, 0, MAXLEN
);
420 memset(newdn
, 0, MAXLEN
);
421 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, oldaccount
);
422 strcat(newdn
, nickserv_conf
.ldap_field_account
);
424 strcat(newdn
, newaccount
);
425 rc
= ldap_modrdn2_s(ld
, dn
, newdn
, true);
426 if(rc
!= LDAP_SUCCESS
) {
427 log_module(MAIN_LOG
, LOG_ERROR
, "Error modifying ldap account: %s -- %s", oldaccount
, ldap_err2string(rc
));
434 LDAPMod
**make_mods_modify(const char *password
, const char *email
, int *num_mods_ret
)
436 static char *password_vals
[] = { NULL
, NULL
};
437 static char *email_vals
[] = { NULL
, NULL
};
440 /* TODO: take this from nickserv_conf.ldap_add_objects */
443 password_vals
[0] = (char *) password
;
444 email_vals
[0] = (char *) email
;
446 if(!(nickserv_conf
.ldap_field_password
&& *nickserv_conf
.ldap_field_password
))
447 return 0; /* password required */
449 if(email && *email && nickserv_conf.ldap_field_email && *nickserv_conf.ldap_field_email)
457 mods
= ( LDAPMod
** ) malloc(( num_mods
+ 1 ) * sizeof( LDAPMod
* ));
458 for( i
= 0; i
< num_mods
; i
++) {
459 mods
[i
] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
460 memset(mods
[i
], 0, sizeof(LDAPMod
));
464 if(nickserv_conf
.ldap_field_password
&& *nickserv_conf
.ldap_field_password
&&
466 mods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
467 mods
[i
]->mod_type
= strdup(nickserv_conf
.ldap_field_password
);
468 mods
[i
]->mod_values
= password_vals
;
472 if(nickserv_conf
.ldap_field_email
&& *nickserv_conf
.ldap_field_email
&& email
) {
473 mods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
474 mods
[i
]->mod_type
= strdup(nickserv_conf
.ldap_field_email
);
475 mods
[i
]->mod_values
= email_vals
;
479 *num_mods_ret
= num_mods
;
483 /* Save OpServ level to LDAP
485 * level - OpServ level
487 * A level of <0 will be treated as 0
489 int ldap_do_oslevel(const char *account
, int level
, int oldlevel
)
492 static char *oslevel_vals
[] = { NULL
, NULL
};
493 char dn
[MAXLEN
], temp
[MAXLEN
];
496 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
497 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
505 snprintf(temp
, MAXLEN
-1, "%d", (level
? level
: oldlevel
));
506 oslevel_vals
[0] = (char *) temp
;
508 if(!(nickserv_conf
.ldap_field_oslevel
&& *nickserv_conf
.ldap_field_oslevel
))
511 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
513 mods
= ( LDAPMod
** ) malloc(( 1 ) * sizeof( LDAPMod
* ));
514 mods
[0] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
515 memset(mods
[0], 0, sizeof(LDAPMod
));
517 mods
[0]->mod_op
= (level
? LDAP_MOD_REPLACE
: LDAP_MOD_DELETE
);
518 mods
[0]->mod_type
= strdup(nickserv_conf
.ldap_field_oslevel
);
519 mods
[0]->mod_values
= oslevel_vals
;
522 rc
= ldap_modify_s(ld
, dn
, mods
);
523 if(rc
!= LDAP_SUCCESS
) {
524 log_module(MAIN_LOG
, LOG_ERROR
, "Error modifying ldap OpServ level: %s -- %s", account
, ldap_err2string(rc
));
527 free(mods
[0]->mod_type
);
534 /* Save email or password to server
536 * password - UNENCRYPTED password. This function encrypts if libs are available
537 * email - email address
539 * NULL to make no change
541 int ldap_do_modify(const char *account
, const char *password
, const char *email
)
547 char *passbuf
= NULL
;
549 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
550 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
555 passbuf
= make_password(password
);
558 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
559 mods
= make_mods_modify(passbuf
, email
, &num_mods
);
561 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for ldap_do_modify");
564 rc
= ldap_modify_s(ld
, dn
, mods
);
565 if(rc
!= LDAP_SUCCESS
) {
566 log_module(MAIN_LOG
, LOG_ERROR
, "Error modifying ldap account: %s -- %s", account
, ldap_err2string(rc
));
569 for(i
= 0; i
< num_mods
; i
++) {
570 free(mods
[i
]->mod_type
);
579 LDAPMod
**make_mods_group(const char *account
, int operation
, int *num_mods_ret
)
581 static char *uid_vals
[] = { NULL
, NULL
};
584 /* TODO: take this from nickserv_conf.ldap_add_objects */
587 uid_vals
[0] = (char *) account
;
589 if(!(nickserv_conf
.ldap_field_group_member
&& *nickserv_conf
.ldap_field_group_member
))
590 return 0; /* password required */
592 mods
= ( LDAPMod
** ) malloc(( num_mods
+ 1 ) * sizeof( LDAPMod
* ));
593 for( i
= 0; i
< num_mods
; i
++) {
594 mods
[i
] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
595 memset(mods
[i
], 0, sizeof(LDAPMod
));
599 mods
[i
]->mod_op
= operation
;
600 mods
[i
]->mod_type
= strdup(nickserv_conf
.ldap_field_group_member
);
601 mods
[i
]->mod_values
= uid_vals
;
604 *num_mods_ret
= num_mods
;
609 int ldap_add2group(char *account
, const char *group
)
615 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
616 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
619 mods
= make_mods_group(account
, LDAP_MOD_ADD
, &num_mods
);
621 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for add2group");
624 rc
= ldap_modify_s(ld
, group
, mods
);
625 if(rc
!= LDAP_SUCCESS
&& rc
!= LDAP_TYPE_OR_VALUE_EXISTS
) {
626 log_module(MAIN_LOG
, LOG_ERROR
, "Error adding %s to group %s: %s", account
, group
, ldap_err2string(rc
));
629 for(i
= 0; i
< num_mods
; i
++) {
630 free(mods
[i
]->mod_type
);
637 int ldap_delfromgroup(char *account
, const char *group
)
643 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
644 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
647 mods
= make_mods_group(account
, LDAP_MOD_DELETE
, &num_mods
);
649 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for delfromgroup");
652 rc
= ldap_modify_s(ld
, group
, mods
);
653 if(rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_ATTRIBUTE
) {
654 log_module(MAIN_LOG
, LOG_ERROR
, "Error removing %s from group %s: %s", account
, group
, ldap_err2string(rc
));
657 for(i
= 0; i
< num_mods
; i
++) {
658 free(mods
[i
]->mod_type
);
669 ldap_unbind_ext(ld
, NULL
, NULL
);
672 /* queries the ldap server for account..
673 * returns LDAP_SUCCESS if a match is found
674 * returns LDAP_OTHER if no match is found
675 * on error returns the proper ldap error
677 int ldap_user_exists(const char *account
)
682 rc
= ldap_search_user(account
, &res
);