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