Debian Codegraph @

modules/auth/ldap.c

raw
1 /*
2 * Copyright (c) 2009 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * LDAP authentication.
6 */
7
8 /*
9 Supports the following options:
10
11 url -- required ldap URL (e.g. ldap://host.domain.com/)
12
13 then either:
14
15 dnformat -- basedn to authenticate against. Use %s to specify where
16 to insert the nick
17
18 or
19
20 base -- basedn to begin the search for the matching dn of the user
21 attribute -- the attribute to search against to find the nick
22 binddn -- distinguished name to bind to for searching (optional)
23 bindauth -- password for the distinguished name (optional, must specify if binddn given)
24
25 */
26
27 #include "atheme.h"
28
29 #include <ldap.h>
30
31 DECLARE_MODULE_V1("auth/ldap", false, _modinit, _moddeinit, PACKAGE_STRING, VENDOR_STRING);
32
33 mowgli_list_t conf_ldap_table;
34 struct
35 {
36 char *url;
37 char *dnformat;
38 char *attribute;
39 char *base;
40 char *binddn;
41 char *bindauth;
42 bool useDN;
43 } ldap_config;
44 LDAP *ldap_conn;
45
46 static void ldap_config_ready(void *unused)
47 {
48 int res;
49 char *p;
50 static time_t lastwarning;
51
52 if (ldap_conn != NULL)
53 ldap_unbind_ext_s(ldap_conn, NULL, NULL);
54 ldap_conn = NULL;
55 if (ldap_config.url == NULL)
56 {
57 slog(LG_ERROR, "ldap_config_ready(): ldap {} missing url definition");
58 return;
59 }
60 if ((ldap_config.dnformat == NULL) && ((ldap_config.base == NULL) || (ldap_config.attribute == NULL)))
61 {
62 slog(LG_ERROR, "ldap_config_ready(): ldap {} block requires dnformat or base & attribute definition");
63 return;
64 }
65 if (ldap_config.binddn != NULL && ldap_config.bindauth == NULL)
66 {
67 slog(LG_ERROR, "ldap_config_ready(): ldap{} block requires bindauth to be defined if binddn is defined");
68 return;
69 }
70
71 if (ldap_config.dnformat != NULL)
72 {
73 ldap_config.useDN = true;
74 p = strchr(ldap_config.dnformat, '%');
75 if (p == NULL || p[1] != 's' || strchr(p + 1, '%'))
76 {
77 slog(LG_ERROR, "ldap_config_ready(): dnformat must contain exactly one %%s and no other %%");
78 return;
79 }
80 }
81 else
82 ldap_config.useDN = false;
83
84 ldap_set_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &(const int)
85 {
86 3});
87 res = ldap_initialize(&ldap_conn, ldap_config.url);
88 if (res != LDAP_SUCCESS)
89 {
90 slog(LG_ERROR, "ldap_config_ready(): ldap_initialize(%s) failed: %s", ldap_config.url, ldap_err2string(res));
91 if (CURRTIME > lastwarning + 300)
92 {
93 slog(LG_INFO, "LDAP:ERROR: \2%s\2", ldap_err2string(res));
94 wallops("Problem with LDAP server: %s", ldap_err2string(res));
95 lastwarning = CURRTIME;
96 }
97 return;
98 }
99
100 /* short timeouts, because this blocks atheme as a whole */
101 ldap_set_option(ldap_conn, LDAP_OPT_TIMEOUT, &(const struct timeval){1, 0});
102 ldap_set_option(ldap_conn, LDAP_OPT_NETWORK_TIMEOUT, &(const struct timeval){1, 0});
103 ldap_set_option(ldap_conn, LDAP_OPT_DEREF, &(const int){false});
104 ldap_set_option(ldap_conn, LDAP_OPT_REFERRALS, &(const int){false});
105 }
106
107 static bool ldap_auth_user(myuser_t *mu, const char *password)
108 {
109 int res;
110 struct berval cred;
111 LDAPMessage *message, *entry;
112
113 if (ldap_conn == NULL)
114 {
115 ldap_config_ready(NULL);
116 }
117 if (ldap_conn == NULL)
118 {
119 slog(LG_INFO, "ldap_auth_user(): no connection");
120 return false;
121 }
122
123 if (strchr(entity(mu)->name, ' '))
124 {
125 slog(LG_INFO, "ldap_auth_user(%s): bad name: found space", entity(mu)->name);
126 return false;
127 }
128 if (strchr(entity(mu)->name, ','))
129 {
130 slog(LG_INFO, "ldap_auth_user(%s): bad name: found comma", entity(mu)->name);
131 return false;
132 }
133 if (strchr(entity(mu)->name, '/'))
134 {
135 slog(LG_INFO, "ldap_auth_user(%s): bad name: found /", entity(mu)->name);
136 return false;
137 }
138
139 /* Use DN to find exact match */
140 if (ldap_config.useDN)
141 {
142 char dn[512];
143 cred.bv_len = strlen(password);
144 /* sstrdup it to remove the const */
145 cred.bv_val = sstrdup(password);
146
147 snprintf(dn, sizeof dn, ldap_config.dnformat, entity(mu)->name);
148 res = ldap_sasl_bind_s(ldap_conn, dn, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
149 if (res == LDAP_SERVER_DOWN)
150 {
151 ldap_config_ready(NULL);
152 res = ldap_sasl_bind_s(ldap_conn, dn, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
153 }
154 if (res == LDAP_SUCCESS)
155 return true;
156 else if (res == LDAP_INVALID_CREDENTIALS)
157 {
158 slog(LG_INFO, "ldap_auth_user(%s): ldap auth bind failed: %s", entity(mu)->name, ldap_err2string(res));
159 return false;
160 }
161 slog(LG_INFO, "ldap_auth_user(): ldap_bind_s failed: %s", ldap_err2string(res));
162 return false;
163
164
165 /* Use base + attribute search for Auth */
166 }
167 else
168 {
169 char what[512];
170 char *binddn = NULL;
171
172 cred.bv_len = 0;
173
174 if (ldap_config.binddn != NULL && ldap_config.bindauth != NULL)
175 {
176 binddn = ldap_config.binddn;
177 cred.bv_val = ldap_config.bindauth;
178 cred.bv_len = strlen(ldap_config.bindauth);
179 }
180
181 res = ldap_sasl_bind_s(ldap_conn, binddn, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
182 if (res == LDAP_SERVER_DOWN)
183 {
184 ldap_config_ready(NULL);
185 res = ldap_sasl_bind_s(ldap_conn, binddn, LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
186 }
187 if (res != LDAP_SUCCESS)
188 {
189 slog(LG_INFO, "ldap_auth_user(): ldap_bind_s failed: %s", ldap_err2string(res));
190 return false;
191 }
192
193 sprintf(what, "%s=%s", ldap_config.attribute, entity(mu)->name);
194 if ((res = ldap_search_ext_s(ldap_conn, ldap_config.base, LDAP_SCOPE_SUBTREE, what, NULL, 0, NULL, NULL, NULL, 0, &message)) != LDAP_SUCCESS)
195 {
196 slog(LG_INFO, "ldap_auth_user(%s): ldap search failed: %s", entity(mu)->name, ldap_err2string(res));
197 return false;
198 }
199
200 cred.bv_len = strlen(password);
201 /* sstrdup it to remove the const */
202 cred.bv_val = sstrdup(password);
203
204 for (entry = ldap_first_message(ldap_conn, message); entry && ldap_msgtype(entry) == LDAP_RES_SEARCH_ENTRY; entry = ldap_next_message(ldap_conn, entry))
205 {
206
207 res = ldap_sasl_bind_s(ldap_conn, ldap_get_dn(ldap_conn, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL);
208 if (res == LDAP_SUCCESS)
209 {
210 ldap_msgfree(message);
211 return true;
212 }
213 }
214
215 ldap_msgfree(message);
216 }
217
218 slog(LG_INFO, "ldap_auth_user(%s): ldap auth bind failed: %s", entity(mu)->name, ldap_err2string(res));
219 return false;
220 }
221
222 void _modinit(module_t * m)
223 {
224 hook_add_event("config_ready");
225 hook_add_config_ready(ldap_config_ready);
226
227 add_subblock_top_conf("LDAP", &conf_ldap_table);
228 add_dupstr_conf_item("URL", &conf_ldap_table, 0, &ldap_config.url, NULL);
229 add_dupstr_conf_item("DNFORMAT", &conf_ldap_table, 0, &ldap_config.dnformat, NULL);
230 add_dupstr_conf_item("BASE", &conf_ldap_table, 0, &ldap_config.base, NULL);
231 add_dupstr_conf_item("ATTRIBUTE", &conf_ldap_table, 0, &ldap_config.attribute, NULL);
232 add_dupstr_conf_item("BINDDN", &conf_ldap_table, 0, &ldap_config.binddn, NULL);
233 add_dupstr_conf_item("BINDAUTH", &conf_ldap_table, 0, &ldap_config.bindauth, NULL);
234
235 auth_user_custom = &ldap_auth_user;
236
237 auth_module_loaded = true;
238 }
239
240 void _moddeinit(module_unload_intent_t intent)
241 {
242 auth_user_custom = NULL;
243
244 auth_module_loaded = false;
245
246 if (ldap_conn != NULL)
247 ldap_unbind_ext_s(ldap_conn, NULL, NULL);
248
249 hook_del_config_ready(ldap_config_ready);
250 del_conf_item("URL", &conf_ldap_table);
251 del_conf_item("DNFORMAT", &conf_ldap_table);
252 del_conf_item("BASE", &conf_ldap_table);
253 del_conf_item("ATTRIBUTE", &conf_ldap_table);
254 del_conf_item("BINDDN", &conf_ldap_table);
255 del_conf_item("BINDAUTH", &conf_ldap_table);
256 del_top_conf("LDAP");
257 }
258
259 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
260 * vim:ts=8
261 * vim:sw=8
262 * vim:noexpandtab
263 */