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 2 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.
35 //#include <sys/select.h>
45 #ifdef HAVE_SYS_SELECT_H
46 #include <sys/select.h>
48 #ifdef HAVE_SYS_SOCKET_H
49 #include <sys/socket.h>
53 /* char dn[] = "uid=%s,ou=Users,dc=afternet,dc=org";
54 char password[] = "xxxxxxx";
55 char base[] = "ou=Users,dc=afternet,dc=org";
58 extern struct nickserv_config nickserv_conf
;
65 if(!nickserv_conf
.ldap_enable
)
67 /* TODO: check here for all required config options and exit() out if not present */
68 ld
= ldap_init(nickserv_conf
.ldap_host
, nickserv_conf
.ldap_port
);
70 log_module(MAIN_LOG
, LOG_ERROR
, "LDAP initilization failed!\n");
73 ldap_set_option(ld
, LDAP_OPT_PROTOCOL_VERSION
, &nickserv_conf
.ldap_version
);
74 log_module(MAIN_LOG
, LOG_INFO
, "Success! ldap_init() was successfull in connecting to %s port %d\n", nickserv_conf
.ldap_host
, nickserv_conf
.ldap_port
);
78 /* Try to auth someone. If theres problems, try reconnecting
79 * once every 10 seconds for 1 minute.
80 * TODO: move this stuff to config file
82 unsigned int ldap_check_auth( char *account
, char *pass
)
87 if(!nickserv_conf
.ldap_enable
)
90 memset(buff
, 0, MAXLEN
);
91 snprintf(buff
, sizeof(buff
)-1, nickserv_conf
.ldap_dn_fmt
/*"uid=%s,ou=Users,dc=afternet,dc=org"*/, account
);
94 q
= ldap_simple_bind_s(ld
, buff
, pass
);
95 if(q
== LDAP_SUCCESS
) {
98 else if(q
== LDAP_INVALID_CREDENTIALS
) {
102 log_module(MAIN_LOG
, LOG_ERROR
, "Bind failed: %s/****** (%d)\n", buff
, q
);
103 ldap_perror(ld
, "ldap");
104 /* Re-init to re-connect to ldap server if thats the problem */
106 ldap_do_init(nickserv_conf
);
109 /* TODO: return to the user that this is a connection error and not a problem
110 * with their password
112 log_module(MAIN_LOG
, LOG_ERROR
, "Failing to reconnect to ldap server. Auth failing.");
116 log_module(MAIN_LOG
, LOG_DEBUG
, "bind() successfull! You are bound as %s\n", buff
);
121 #ifdef notdef /* not used yet - will be used to pull email etc out of ldap */
122 LDAPMessage
ldap_search_user(char uid
)
125 char filter
[] = "cn=admin";
127 struct timeval timeout
;
133 if( ldap_search_st(ld
, base
, LDAP_SCOPE_ONELEVEL
, filter
, NULL
, 0, &timeout
, &res
) != LDAP_SUCCESS
) {
134 log_module(MAIN_LOG
, LOG_ERROR
, "search failed: %s %s\n", base
, filter
);
137 log_module(MAIN_LOG
, LOG_DEBUG
, "Search successfull! %s %s\n", base
, filter
);
138 log_module(MAIN_LOG
, LOG_DEBUG
, "Got %d entries\n", ldap_count_entries(ld
, res
));
142 entry
= ldap_first_entry(ld
, res
);
143 value
= ldap_get_values(ld
, entry
, "cn");
144 log_module(MAIN_LOG
, LOG_DEBUG
, "cn: %s\n", value
[0]);
145 value
= ldap_get_values(ld
, entry
, "description");
146 log_module(MAIN_LOG
, LOG_DEBUG
, "Description: %s\n", value
[0]);
147 value
= ldap_get_values(ld
, entry
, "userPassword");
148 log_module(MAIN_LOG
, LOG_DEBUG
, "pass: %s\n", value
? value
[0] : "error");
153 ldap_first_attribute();
156 ldap_next_attribute();
164 /* get errors with ldap_err2string(); */