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.
32 #define LDAP_DEPRECATED 1
44 extern struct nickserv_config nickserv_conf
;
48 int admin_bind
= false;
52 if(!nickserv_conf
.ldap_enable
)
54 /* TODO: check here for all required config options and exit() out if not present */
55 //ld = ldap_init(nickserv_conf.ldap_host, nickserv_conf.ldap_port);
58 if(ldap_initialize(&ld
, nickserv_conf
.ldap_uri
)) {
59 log_module(MAIN_LOG
, LOG_ERROR
, "LDAP initilization failed!\n");
62 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &nickserv_conf
.ldap_version
);
63 log_module(MAIN_LOG
, LOG_INFO
, "Success! ldap_init() was successfull in connecting to %s\n", nickserv_conf
.ldap_uri
);
68 /* Try to auth someone. If theres problems, try reconnecting
69 * once every 10 seconds for 1 minute.
70 * TODO: move this stuff to config file
72 unsigned int ldap_do_bind( const char *dn
, const char *pass
)
78 q
= ldap_simple_bind_s(ld
, dn
, pass
);
79 if(q
== LDAP_SUCCESS
) {
80 log_module(MAIN_LOG
, LOG_DEBUG
, "bind() successfull! You are bound as %s", dn
);
84 else if(q
== LDAP_INVALID_CREDENTIALS
) {
88 log_module(MAIN_LOG
, LOG_ERROR
, "Bind failed: %s/****** (%s)", dn
, ldap_err2string(q
));
89 /* ldap_perror(ld, "ldap"); */
93 /* TODO: return to the user that this is a connection error and not a problem
96 log_module(MAIN_LOG
, LOG_ERROR
, "Failing to reconnect to ldap server. Auth failing.");
100 log_module(MAIN_LOG
, LOG_ERROR
, "ldap_do_bind falling off the end. this shouldnt happen");
103 int ldap_do_admin_bind()
106 if(!(nickserv_conf
.ldap_admin_dn
&& *nickserv_conf
.ldap_admin_dn
&&
107 nickserv_conf
.ldap_admin_pass
&& *nickserv_conf
.ldap_admin_pass
)) {
108 log_module(MAIN_LOG
, LOG_ERROR
, "Tried to admin bind, but no admin credentials configured in config file. ldap_admin_dn/ldap_admin_pass");
109 return LDAP_OTHER
; /* not configured to do this */
111 rc
= ldap_do_bind(nickserv_conf
.ldap_admin_dn
, nickserv_conf
.ldap_admin_pass
);
112 if(rc
== LDAP_SUCCESS
)
118 unsigned int ldap_check_auth( const char *account
, const char *pass
)
122 if(!nickserv_conf
.ldap_enable
)
125 memset(buff
, 0, MAXLEN
);
126 snprintf(buff
, sizeof(buff
)-1, nickserv_conf
.ldap_dn_fmt
/*"uid=%s,ou=Users,dc=afternet,dc=org"*/, account
);
128 return ldap_do_bind(buff
, pass
);
132 int ldap_search_user(const char *account
, LDAPMessage
**entry
)
135 char filter
[MAXLEN
+1];
139 struct timeval timeout
;
141 memset(filter
, 0, MAXLEN
+1);
142 snprintf(filter
, MAXLEN
, "%s=%s", nickserv_conf
.ldap_field_account
, account
);
147 timeout
.tv_sec
= nickserv_conf
.ldap_timeout
;
148 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
149 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
152 if( (rc
= ldap_search_st(ld
, nickserv_conf
.ldap_base
, LDAP_SCOPE_ONELEVEL
, filter
, NULL
, 0, &timeout
, &res
)) != LDAP_SUCCESS
) {
153 log_module(MAIN_LOG
, LOG_ERROR
, "search failed: %s %s: %s", nickserv_conf
.ldap_base
, filter
, ldap_err2string(rc
));
156 log_module(MAIN_LOG
, LOG_DEBUG
, "Search successfull! %s %s\n", nickserv_conf
.ldap_base
, filter
);
157 if(ldap_count_entries(ld
, res
) != 1) {
158 log_module(MAIN_LOG
, LOG_DEBUG
, "LDAP search got %d entries when looking for %s", ldap_count_entries(ld
, res
), account
);
159 return(LDAP_OTHER
); /* Search was a success, but user not found.. */
161 log_module(MAIN_LOG
, LOG_DEBUG
, "LDAP search got %d entries", ldap_count_entries(ld
, res
));
162 *entry
= ldap_first_entry(ld
, res
);
166 /* queries the ldap server for account..
167 * if a single account match is found,
168 * email is allocated and set to the email address
169 * and returns LDAP_SUCCESS. returns LDAP_OTHER if
170 * 0 or 2+ entries are matched, or the proper ldap error
171 * code for other errors.
173 int ldap_get_user_info(const char *account
, char **email
)
176 struct berval
**value
;
177 LDAPMessage
*entry
, *res
;
180 if( (rc
= ldap_search_user(account
, &res
)) == LDAP_SUCCESS
) {
181 entry
= ldap_first_entry(ld
, res
);
182 value
= ldap_get_values_len(ld
, entry
, nickserv_conf
.ldap_field_email
);
187 *email
= strdup(value
[0]->bv_val
);
188 log_module(MAIN_LOG
, LOG_DEBUG
, "%s: %s\n", nickserv_conf
.ldap_field_email
, value
[0]->bv_val
);
189 ldap_value_free_len(value
);
191 value = ldap_get_values(ld, entry, "description");
192 log_module(MAIN_LOG, LOG_DEBUG, "Description: %s\n", value[0]);
193 value = ldap_get_values(ld, entry, "userPassword");
194 log_module(MAIN_LOG, LOG_DEBUG, "pass: %s\n", value ? value[0] : "error");
203 ldap_first_attribute();
206 ldap_next_attribute();
213 /* get errors with ldap_err2string(); */
216 /********* base64 stuff ***********/
218 unsigned char *pack(const char *str
, unsigned int *len
)
223 static unsigned char buf
[MAXLEN
+1];
226 memset(buf
, 0, MAXLEN
+1);
231 if((n
>= '0') && (n
<= '9')) {
233 } else if ((n
>= 'A') && (n
<= 'F')) {
235 } else if ((n
>= 'a') && (n
<= 'f')) {
238 printf("pack type H: illegal hex digit %c", n
);
243 buf
[++outputpos
] = 0;
248 buf
[outputpos
] |= (n
<< nibbleshift
);
249 nibbleshift
= (nibbleshift
+ 4) & 7;
255 char **make_object_vals()
258 static char **object_vals
= NULL
;
263 object_vals
= malloc(sizeof( *object_vals
) * (nickserv_conf
.ldap_object_classes
->used
+1));
265 for(y
= 0; y
< nickserv_conf
.ldap_object_classes
->used
; y
++) {
266 object_vals
[y
] = nickserv_conf
.ldap_object_classes
->list
[y
];
268 object_vals
[y
] = NULL
;
270 /* NOTE: The return value of this is only good until the next call to it. */
273 char *make_password(const char *crypted
)
276 unsigned char *packed
;
280 packed
= pack(crypted
, &len
);
281 base64_encode_alloc((char *)packed
, len
, &base64pass
);
282 passbuf
= malloc(strlen(base64pass
) + 1 + 5);
283 strcpy(passbuf
, "{MD5}");
284 strcat(passbuf
, base64pass
);
285 //log_module(MAIN_LOG, LOG_DEBUG, "Encoded password is: '%s'", passbuf);
291 LDAPMod
**make_mods_add(const char *account
, const char *password
, const char *email
, int *num_mods_ret
)
293 static char *account_vals
[] = { NULL
, NULL
};
294 static char *password_vals
[] = { NULL
, NULL
};
295 static char *email_vals
[] = { NULL
, NULL
};
299 /* TODO: take this from nickserv_conf.ldap_add_objects */
301 static char **object_vals
;
303 account_vals
[0] = NULL
;
304 account_vals
[1] = NULL
;
305 password_vals
[0] = NULL
;
306 password_vals
[1] = NULL
;
307 email_vals
[0] = NULL
;
308 email_vals
[1] = NULL
;
309 object_vals
= make_object_vals();
311 account_vals
[0] = (char *) account
;
312 if (password
!= NULL
) {
313 password_vals
[0] = (char *) password
;
316 email_vals
[0] = (char *) email
;
318 if(!(nickserv_conf
.ldap_field_account
&& *nickserv_conf
.ldap_field_account
))
319 return 0; /* account required */
320 if(!(nickserv_conf
.ldap_field_password
&& *nickserv_conf
.ldap_field_password
))
321 return 0; /* password required */
322 if(email
&& *email
&& nickserv_conf
.ldap_field_email
&& *nickserv_conf
.ldap_field_email
)
325 mods
= ( LDAPMod
** ) malloc(( num_mods
+ 1 ) * sizeof( LDAPMod
* ));
326 for( i
= 0; i
< num_mods
; i
++) {
327 mods
[i
] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
328 memset(mods
[i
], 0, sizeof(LDAPMod
));
331 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
332 mods
[mod
]->mod_type
= strdup("objectclass");
333 mods
[mod
]->mod_values
= object_vals
;
336 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
337 mods
[mod
]->mod_type
= strdup(nickserv_conf
.ldap_field_account
);
338 mods
[mod
]->mod_values
= account_vals
;
341 if (password
!= NULL
) {
342 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
343 mods
[mod
]->mod_type
= strdup(nickserv_conf
.ldap_field_password
);
344 mods
[mod
]->mod_values
= password_vals
;
348 if(nickserv_conf
.ldap_field_email
&& *nickserv_conf
.ldap_field_email
&& email
&& *email
) {
349 mods
[mod
]->mod_op
= LDAP_MOD_ADD
;
350 mods
[mod
]->mod_type
= strdup(nickserv_conf
.ldap_field_email
);
351 mods
[mod
]->mod_values
= email_vals
;
355 *num_mods_ret
= num_mods
;
359 int ldap_do_add(const char *account
, const char *crypted
, const char *email
)
365 char *passbuf
= NULL
;
367 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
368 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
373 passbuf
= make_password(crypted
);
374 snprintf(newdn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
375 mods
= make_mods_add(account
, (crypted
!= NULL
? passbuf
: crypted
), email
, &num_mods
);
377 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for ldap_add");
380 rc
= ldap_add_ext_s(ld
, newdn
, mods
, NULL
, NULL
);
381 if(rc
!= LDAP_SUCCESS
&& rc
!= LDAP_ALREADY_EXISTS
) {
382 log_module(MAIN_LOG
, LOG_ERROR
, "Error adding ldap account: %s -- %s", account
, ldap_err2string(rc
));
386 for(i
= 0; i
< num_mods
; i
++) {
387 free(mods
[i
]->mod_type
);
396 int ldap_delete_account(char *account
)
401 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
402 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
406 memset(dn
, 0, MAXLEN
);
407 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
408 return(ldap_delete_s(ld
, dn
));
411 int ldap_rename_account(char *oldaccount
, char *newaccount
)
413 char dn
[MAXLEN
], newdn
[MAXLEN
];
416 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
417 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
421 memset(dn
, 0, MAXLEN
);
422 memset(newdn
, 0, MAXLEN
);
423 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, oldaccount
);
424 strcat(newdn
, nickserv_conf
.ldap_field_account
);
426 strcat(newdn
, newaccount
);
427 rc
= ldap_modrdn2_s(ld
, dn
, newdn
, true);
428 if(rc
!= LDAP_SUCCESS
) {
429 log_module(MAIN_LOG
, LOG_ERROR
, "Error modifying ldap account: %s -- %s", oldaccount
, ldap_err2string(rc
));
436 LDAPMod
**make_mods_modify(const char *password
, const char *email
, int *num_mods_ret
)
438 static char *password_vals
[] = { NULL
, NULL
};
439 static char *email_vals
[] = { NULL
, NULL
};
442 /* TODO: take this from nickserv_conf.ldap_add_objects */
445 password_vals
[0] = (char *) password
;
446 email_vals
[0] = (char *) email
;
448 if(!(nickserv_conf
.ldap_field_password
&& *nickserv_conf
.ldap_field_password
))
449 return 0; /* password required */
451 if(email && *email && nickserv_conf.ldap_field_email && *nickserv_conf.ldap_field_email)
459 mods
= ( LDAPMod
** ) malloc(( num_mods
+ 1 ) * sizeof( LDAPMod
* ));
460 for( i
= 0; i
< num_mods
; i
++) {
461 mods
[i
] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
462 memset(mods
[i
], 0, sizeof(LDAPMod
));
466 if(nickserv_conf
.ldap_field_password
&& *nickserv_conf
.ldap_field_password
&&
468 mods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
469 mods
[i
]->mod_type
= strdup(nickserv_conf
.ldap_field_password
);
470 mods
[i
]->mod_values
= password_vals
;
474 if(nickserv_conf
.ldap_field_email
&& *nickserv_conf
.ldap_field_email
&& email
) {
475 mods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
476 mods
[i
]->mod_type
= strdup(nickserv_conf
.ldap_field_email
);
477 mods
[i
]->mod_values
= email_vals
;
481 *num_mods_ret
= num_mods
;
485 /* Save OpServ level to LDAP
487 * level - OpServ level
489 * A level of <0 will be treated as 0
491 int ldap_do_oslevel(const char *account
, int level
, int oldlevel
)
494 static char *oslevel_vals
[] = { NULL
, NULL
};
495 char dn
[MAXLEN
], temp
[MAXLEN
];
498 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
499 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
507 snprintf(temp
, MAXLEN
-1, "%d", (level
? level
: oldlevel
));
508 oslevel_vals
[0] = (char *) temp
;
510 if(!(nickserv_conf
.ldap_field_oslevel
&& *nickserv_conf
.ldap_field_oslevel
))
513 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
515 mods
= ( LDAPMod
** ) malloc(( 1 ) * sizeof( LDAPMod
* ));
516 mods
[0] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
517 memset(mods
[0], 0, sizeof(LDAPMod
));
519 mods
[0]->mod_op
= (level
? LDAP_MOD_REPLACE
: LDAP_MOD_DELETE
);
520 mods
[0]->mod_type
= strdup(nickserv_conf
.ldap_field_oslevel
);
521 mods
[0]->mod_values
= oslevel_vals
;
524 rc
= ldap_modify_s(ld
, dn
, mods
);
525 if(rc
!= LDAP_SUCCESS
) {
526 log_module(MAIN_LOG
, LOG_ERROR
, "Error modifying ldap OpServ level: %s -- %s", account
, ldap_err2string(rc
));
529 free(mods
[0]->mod_type
);
536 /* Save email or password to server
538 * password - UNENCRYPTED password. This function encrypts if libs are available
539 * email - email address
541 * NULL to make no change
543 int ldap_do_modify(const char *account
, const char *password
, const char *email
)
549 char *passbuf
= NULL
;
551 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
552 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
557 passbuf
= make_password(password
);
560 snprintf(dn
, MAXLEN
-1, nickserv_conf
.ldap_dn_fmt
, account
);
561 mods
= make_mods_modify(passbuf
, email
, &num_mods
);
563 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for ldap_do_modify");
566 rc
= ldap_modify_s(ld
, dn
, mods
);
567 if(rc
!= LDAP_SUCCESS
) {
568 log_module(MAIN_LOG
, LOG_ERROR
, "Error modifying ldap account: %s -- %s", account
, ldap_err2string(rc
));
571 for(i
= 0; i
< num_mods
; i
++) {
572 free(mods
[i
]->mod_type
);
581 LDAPMod
**make_mods_group(const char *account
, int operation
, int *num_mods_ret
)
583 static char *uid_vals
[] = { NULL
, NULL
};
586 /* TODO: take this from nickserv_conf.ldap_add_objects */
589 uid_vals
[0] = (char *) account
;
591 if(!(nickserv_conf
.ldap_field_group_member
&& *nickserv_conf
.ldap_field_group_member
))
592 return 0; /* password required */
594 mods
= ( LDAPMod
** ) malloc(( num_mods
+ 1 ) * sizeof( LDAPMod
* ));
595 for( i
= 0; i
< num_mods
; i
++) {
596 mods
[i
] = (LDAPMod
*) malloc(sizeof(LDAPMod
));
597 memset(mods
[i
], 0, sizeof(LDAPMod
));
601 mods
[i
]->mod_op
= operation
;
602 mods
[i
]->mod_type
= strdup(nickserv_conf
.ldap_field_group_member
);
603 mods
[i
]->mod_values
= uid_vals
;
606 *num_mods_ret
= num_mods
;
611 int ldap_add2group(char *account
, const char *group
)
617 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
618 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
621 mods
= make_mods_group(account
, LDAP_MOD_ADD
, &num_mods
);
623 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for add2group");
626 rc
= ldap_modify_s(ld
, group
, mods
);
627 if(rc
!= LDAP_SUCCESS
&& rc
!= LDAP_TYPE_OR_VALUE_EXISTS
) {
628 log_module(MAIN_LOG
, LOG_ERROR
, "Error adding %s to group %s: %s", account
, group
, ldap_err2string(rc
));
631 for(i
= 0; i
< num_mods
; i
++) {
632 free(mods
[i
]->mod_type
);
639 int ldap_delfromgroup(char *account
, const char *group
)
645 if(!admin_bind
&& LDAP_SUCCESS
!= ( rc
= ldap_do_admin_bind())) {
646 log_module(MAIN_LOG
, LOG_ERROR
, "failed to bind as admin");
649 mods
= make_mods_group(account
, LDAP_MOD_DELETE
, &num_mods
);
651 log_module(MAIN_LOG
, LOG_ERROR
, "Error building mods for delfromgroup");
654 rc
= ldap_modify_s(ld
, group
, mods
);
655 if(rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_ATTRIBUTE
) {
656 log_module(MAIN_LOG
, LOG_ERROR
, "Error removing %s from group %s: %s", account
, group
, ldap_err2string(rc
));
659 for(i
= 0; i
< num_mods
; i
++) {
660 free(mods
[i
]->mod_type
);
671 ldap_unbind_ext(ld
, NULL
, NULL
);
674 /* queries the ldap server for account..
675 * returns LDAP_SUCCESS if a match is found
676 * returns LDAP_OTHER if no match is found
677 * on error returns the proper ldap error
679 int ldap_user_exists(const char *account
)
684 rc
= ldap_search_user(account
, &res
);