| 1 |
/* This file is part of GNU Dico. |
| 2 |
Copyright (C) 2008-2024 Sergey Poznyakoff |
| 3 |
|
| 4 |
GNU Dico is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
GNU Dico is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
#include <dico.h> |
| 19 |
#include <appi18n.h> |
| 20 |
#include <wordsplit.h> |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <ldap.h> |
| 24 |
#include <lber.h> |
| 25 |
|
| 26 |
struct _dico_ldap_handle { |
| 27 |
LDAP *ldap; |
| 28 |
const char *url; |
| 29 |
char *base; |
| 30 |
char *binddn; |
| 31 |
char *passwd; |
| 32 |
int tls; |
| 33 |
/* FIXME: sasl */ |
| 34 |
int debug; |
| 35 |
char *user_filter; |
| 36 |
char *group_filter; |
| 37 |
}; |
| 38 |
|
| 39 |
void |
| 40 |
free_ldap_handle(struct _dico_ldap_handle *lp) |
| 41 |
{ |
| 42 |
free(lp->base); |
| 43 |
free(lp->binddn); |
| 44 |
free(lp->passwd); |
| 45 |
free(lp->user_filter); |
| 46 |
free(lp->group_filter); |
| 47 |
free(lp); |
| 48 |
} |
| 49 |
|
| 50 |
static int |
| 51 |
_dico_conn_setup(struct _dico_ldap_handle *lp) |
| 52 |
{ |
| 53 |
int rc; |
| 54 |
LDAP *ld = NULL; |
| 55 |
int protocol = LDAP_VERSION3; /* FIXME: must be configurable */ |
| 56 |
|
| 57 |
if (lp->debug) { |
| 58 |
if (ber_set_option(NULL, LBER_OPT_DEBUG_LEVEL, &lp->debug) |
| 59 |
!= LBER_OPT_SUCCESS ) |
| 60 |
dico_log(L_ERR, 0, _("cannot set LBER_OPT_DEBUG_LEVEL %d"), |
| 61 |
lp->debug); |
| 62 |
|
| 63 |
if (ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, &lp->debug) |
| 64 |
!= LDAP_OPT_SUCCESS ) |
| 65 |
dico_log(L_ERR, 0, _("could not set LDAP_OPT_DEBUG_LEVEL %d"), |
| 66 |
lp->debug); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
rc = ldap_initialize(&ld, lp->url); |
| 71 |
if (rc != LDAP_SUCCESS) { |
| 72 |
dico_log(L_ERR, 0, |
| 73 |
_("cannot create LDAP session handle for URI=%s (%d): %s"), |
| 74 |
lp->url, rc, ldap_err2string(rc)); |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
if (lp->tls) { |
| 79 |
rc = ldap_start_tls_s(ld, NULL, NULL); |
| 80 |
if (rc != LDAP_SUCCESS) { |
| 81 |
dico_log(L_ERR, 0, _("ldap_start_tls failed: %s"), |
| 82 |
ldap_err2string(rc)); |
| 83 |
return 1; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &protocol); |
| 88 |
|
| 89 |
/* FIXME: Timeouts, SASL, etc. */ |
| 90 |
lp->ldap = ld; |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
static int |
| 95 |
_dico_ldap_bind(struct _dico_ldap_handle *lp) |
| 96 |
{ |
| 97 |
int msgid, err, rc; |
| 98 |
LDAPMessage *result; |
| 99 |
LDAPControl **ctrls; |
| 100 |
char msgbuf[256]; |
| 101 |
char *matched = NULL; |
| 102 |
char *info = NULL; |
| 103 |
char **refs = NULL; |
| 104 |
static struct berval passwd; |
| 105 |
|
| 106 |
passwd.bv_val = lp->passwd; |
| 107 |
passwd.bv_len = passwd.bv_val ? strlen (passwd.bv_val) : 0; |
| 108 |
|
| 109 |
msgbuf[0] = 0; |
| 110 |
|
| 111 |
rc = ldap_sasl_bind(lp->ldap, lp->binddn, LDAP_SASL_SIMPLE, &passwd, |
| 112 |
NULL, NULL, &msgid); |
| 113 |
if (msgid == -1) { |
| 114 |
dico_log(L_ERR, 0, |
| 115 |
"ldap_sasl_bind(SIMPLE) failed: %s", ldap_err2string(rc)); |
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
if (ldap_result(lp->ldap, msgid, LDAP_MSG_ALL, NULL, &result) == -1) { |
| 120 |
dico_log(L_ERR, 0, "ldap_result failed"); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
rc = ldap_parse_result(lp->ldap, result, &err, &matched, &info, &refs, |
| 125 |
&ctrls, 1); |
| 126 |
if (rc != LDAP_SUCCESS) { |
| 127 |
dico_log(L_ERR, 0, "ldap_parse_result failed: %s", |
| 128 |
ldap_err2string(rc)); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
|
| 132 |
if (ctrls) |
| 133 |
ldap_controls_free(ctrls); |
| 134 |
|
| 135 |
if (err != LDAP_SUCCESS |
| 136 |
|| msgbuf[0] |
| 137 |
|| (matched && matched[0]) |
| 138 |
|| (info && info[0]) |
| 139 |
|| refs) { |
| 140 |
dico_log(L_ERR, 0, "ldap_bind: %s (%d)%s", |
| 141 |
ldap_err2string(err), err, msgbuf); |
| 142 |
|
| 143 |
if (matched && *matched) |
| 144 |
dico_log(L_ERR, 0, "matched DN: %s", matched); |
| 145 |
|
| 146 |
if (info && *info) |
| 147 |
dico_log(L_ERR, 0, "additional info: %s", info); |
| 148 |
|
| 149 |
if (refs && *refs) { |
| 150 |
int i; |
| 151 |
dico_log(L_ERR, 0, "referrals:"); |
| 152 |
for (i = 0; refs[i]; i++) |
| 153 |
dico_log(L_ERR, 0, "%s", refs[i]); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (matched) |
| 158 |
ber_memfree(matched); |
| 159 |
if (info) |
| 160 |
ber_memfree(info); |
| 161 |
if (refs) |
| 162 |
ber_memvfree((void **)refs); |
| 163 |
|
| 164 |
return err != LDAP_SUCCESS; |
| 165 |
} |
| 166 |
|
| 167 |
static void |
| 168 |
_dico_ldap_unbind(struct _dico_ldap_handle *lp) |
| 169 |
{ |
| 170 |
if (lp->ldap) { |
| 171 |
ldap_set_option(lp->ldap, LDAP_OPT_SERVER_CONTROLS, NULL); |
| 172 |
ldap_unbind_ext(lp->ldap, NULL, NULL); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
static int |
| 177 |
db_open(void **phandle, dico_url_t url, const char *options) |
| 178 |
{ |
| 179 |
struct _dico_ldap_handle hstr, *handle; |
| 180 |
int rc; |
| 181 |
long debug = 0; |
| 182 |
|
| 183 |
struct dico_option option[] = { |
| 184 |
{ DICO_OPTSTR(base), dico_opt_string, &hstr.base }, |
| 185 |
{ DICO_OPTSTR(binddn), dico_opt_string, &hstr.binddn }, |
| 186 |
{ DICO_OPTSTR(passwd), dico_opt_string, &hstr.passwd }, |
| 187 |
{ DICO_OPTSTR(tls), dico_opt_bool, &hstr.tls }, |
| 188 |
{ DICO_OPTSTR(debug), dico_opt_long, &debug }, |
| 189 |
{ DICO_OPTSTR(user-filter), dico_opt_string, &hstr.user_filter }, |
| 190 |
{ DICO_OPTSTR(group-filter), dico_opt_string, &hstr.group_filter }, |
| 191 |
{ NULL } |
| 192 |
}; |
| 193 |
|
| 194 |
memset(&hstr, 0, sizeof(hstr)); |
| 195 |
if (options) { |
| 196 |
struct wordsplit ws; |
| 197 |
|
| 198 |
if (wordsplit(options, &ws, WRDSF_DEFFLAGS)) { |
| 199 |
dico_log(L_ERR, 0, _("cannot parse options `%s': %s"), options, |
| 200 |
wordsplit_strerror(&ws)); |
| 201 |
return 1; |
| 202 |
} |
| 203 |
|
| 204 |
rc = dico_parseopt(option, ws.ws_wordc, ws.ws_wordv, |
| 205 |
DICO_PARSEOPT_PARSE_ARGV0, NULL); |
| 206 |
wordsplit_free(&ws); |
| 207 |
if (rc) |
| 208 |
return 1; |
| 209 |
hstr.debug = debug; |
| 210 |
} |
| 211 |
hstr.url = url->string; |
| 212 |
|
| 213 |
handle = malloc(sizeof(*handle)); |
| 214 |
if (!handle) { |
| 215 |
dico_log(L_ERR, errno, _("cannot allocate handle")); |
| 216 |
return 1; |
| 217 |
} |
| 218 |
*handle = hstr; |
| 219 |
|
| 220 |
rc = _dico_conn_setup(handle) || |
| 221 |
_dico_ldap_bind(handle); |
| 222 |
if (rc) { |
| 223 |
free_ldap_handle(handle); |
| 224 |
} else { |
| 225 |
*phandle = handle; |
| 226 |
} |
| 227 |
return rc; |
| 228 |
} |
| 229 |
|
| 230 |
int |
| 231 |
db_close(void *handle) |
| 232 |
{ |
| 233 |
struct _dico_ldap_handle *lp = handle; |
| 234 |
_dico_ldap_unbind(lp); |
| 235 |
free_ldap_handle(lp); |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
char * |
| 241 |
_dico_ldap_expand_user(const char *query, const char *user) |
| 242 |
{ |
| 243 |
struct wordsplit ws; |
| 244 |
const char *env[3]; |
| 245 |
char *res; |
| 246 |
|
| 247 |
env[0] = "user"; |
| 248 |
env[1] = user; |
| 249 |
env[2] = NULL; |
| 250 |
|
| 251 |
ws.ws_env = env; |
| 252 |
if (wordsplit(query, &ws, |
| 253 |
WRDSF_NOSPLIT | WRDSF_NOCMD | |
| 254 |
WRDSF_ENV | WRDSF_ENV_KV)) { |
| 255 |
dico_log(L_ERR, 0, _("cannot expand query `%s': %s"), query, |
| 256 |
wordsplit_strerror(&ws)); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
res = ws.ws_wordv[0]; |
| 261 |
ws.ws_wordv[0] = NULL; |
| 262 |
wordsplit_free(&ws); |
| 263 |
return res; |
| 264 |
} |
| 265 |
|
| 266 |
static LDAPMessage * |
| 267 |
_dico_ldap_search(struct _dico_ldap_handle *lp, const char *filter_pat, |
| 268 |
const char *attr, const char *user) |
| 269 |
{ |
| 270 |
int rc; |
| 271 |
char *filter; |
| 272 |
LDAPMessage *res; |
| 273 |
ber_int_t msgid; |
| 274 |
char *attrs[2]; |
| 275 |
|
| 276 |
attrs[0] = (char*) attr; |
| 277 |
attrs[1] = NULL; |
| 278 |
|
| 279 |
if (filter_pat) { |
| 280 |
filter = _dico_ldap_expand_user(filter_pat, user); |
| 281 |
if (!filter) { |
| 282 |
DICO_LOG_MEMERR(); |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
} else |
| 286 |
filter = NULL; |
| 287 |
rc = ldap_search_ext(lp->ldap, lp->base, LDAP_SCOPE_SUBTREE, |
| 288 |
filter, attrs, 0, |
| 289 |
NULL, NULL, NULL, -1, &msgid); |
| 290 |
if (filter) |
| 291 |
free(filter); |
| 292 |
|
| 293 |
if (rc != LDAP_SUCCESS) { |
| 294 |
dico_log(L_ERR, 0, "ldap_search_ext: %s", ldap_err2string(rc)); |
| 295 |
/*if (rc == LDAP_NO_SUCH_OBJECT)*/ |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
|
| 299 |
rc = ldap_result(lp->ldap, msgid, LDAP_MSG_ALL, NULL, &res); |
| 300 |
if (rc < 0) { |
| 301 |
dico_log(L_ERR, 0, "ldap_result failed"); |
| 302 |
return NULL; |
| 303 |
} |
| 304 |
return res; |
| 305 |
} |
| 306 |
|
| 307 |
static int |
| 308 |
db_get_pass(void *handle, const char *qpw, const char *key, char **ppass) |
| 309 |
{ |
| 310 |
struct _dico_ldap_handle *lp = handle; |
| 311 |
LDAPMessage *res, *msg; |
| 312 |
int rc; |
| 313 |
struct berval **values; |
| 314 |
|
| 315 |
res = _dico_ldap_search(lp, lp->user_filter, qpw, key); |
| 316 |
if (!res) |
| 317 |
return 1; |
| 318 |
|
| 319 |
rc = ldap_count_entries(lp->ldap, res); |
| 320 |
if (rc == 0) { |
| 321 |
dico_log(L_ERR, 0, "not enough entires"); |
| 322 |
ldap_msgfree(res); |
| 323 |
return 1; |
| 324 |
} |
| 325 |
|
| 326 |
msg = ldap_first_entry(lp->ldap, res); |
| 327 |
|
| 328 |
values = ldap_get_values_len(lp->ldap, msg, qpw); |
| 329 |
if (ldap_count_values_len(values) == 0) { |
| 330 |
dico_log(L_ERR, 0, "not enough entires"); |
| 331 |
ldap_msgfree(res); |
| 332 |
return 1; |
| 333 |
} |
| 334 |
*ppass = strdup(values[0]->bv_val); |
| 335 |
rc = *ppass == NULL; |
| 336 |
if (rc) |
| 337 |
dico_log(L_ERR, 0, "not enough memory"); |
| 338 |
ldap_value_free_len(values); |
| 339 |
ldap_msgfree(res); |
| 340 |
return rc; |
| 341 |
} |
| 342 |
|
| 343 |
static int |
| 344 |
_free_group(void *item, void *data) |
| 345 |
{ |
| 346 |
free(item); |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
static int |
| 351 |
db_get_groups(void *handle, const char *qgr, const char *key, |
| 352 |
dico_list_t *pgroups) |
| 353 |
{ |
| 354 |
struct _dico_ldap_handle *lp = handle; |
| 355 |
LDAPMessage *res, *msg; |
| 356 |
int rc; |
| 357 |
dico_list_t groups = NULL; |
| 358 |
|
| 359 |
res = _dico_ldap_search(lp, lp->group_filter, qgr, key); |
| 360 |
if (!res) |
| 361 |
return 1; |
| 362 |
|
| 363 |
rc = ldap_count_entries(lp->ldap, res); |
| 364 |
if (rc == 0) { |
| 365 |
dico_log(L_INFO, 0, "no groups containing %s", key); |
| 366 |
ldap_msgfree(res); |
| 367 |
*pgroups = NULL; |
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
groups = dico_list_create(); |
| 372 |
dico_list_set_free_item(groups, _free_group, NULL); |
| 373 |
*pgroups = groups; |
| 374 |
|
| 375 |
rc = 0; |
| 376 |
for (msg = ldap_first_entry(lp->ldap, res); msg; |
| 377 |
msg = ldap_next_entry(lp->ldap, msg)) { |
| 378 |
struct berval **values; |
| 379 |
size_t i, count; |
| 380 |
|
| 381 |
values = ldap_get_values_len(lp->ldap, msg, qgr); |
| 382 |
count = ldap_count_values_len(values); |
| 383 |
for (i = 0; i < count; i++) { |
| 384 |
char *s = strdup(values[i]->bv_val); |
| 385 |
dico_list_append(groups, s); |
| 386 |
} |
| 387 |
ldap_value_free_len(values); |
| 388 |
} |
| 389 |
ldap_msgfree(res); |
| 390 |
return rc; |
| 391 |
} |
| 392 |
|
| 393 |
static struct dico_udb_def ldap_udb_def = { |
| 394 |
"ldap", |
| 395 |
db_open, |
| 396 |
db_close, |
| 397 |
db_get_pass, |
| 398 |
db_get_groups |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
static int |
| 403 |
dico_ldap_init(int argc, char **argv) |
| 404 |
{ |
| 405 |
return dico_udb_define(&ldap_udb_def); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
struct dico_database_module DICO_EXPORT(ldap, module) = { |
| 410 |
DICO_MODULE_VERSION, |
| 411 |
DICO_CAPA_NODB, |
| 412 |
dico_ldap_init, |
| 413 |
}; |