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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
/*
SSSD
LDAP Helper routines
Copyright (C) Simo Sorce <ssorce@redhat.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define LDAP_DEPRECATED 1
#include <ldap.h>
#include "util/util.h"
#include "confdb/confdb.h"
#include "providers/ldap/sdap.h"
struct sdap_gen_opts default_basic_opts[] = {
{ "ldapUri", "ldap://localhost", NULL },
{ "defaultBindDn", NULL, NULL },
{ "defaultAuthtokType", NULL, NULL },
{ "defaultAuthtok", NULL, NULL },
{ "network_timeout", "5", NULL },
{ "opt_timeout", "5", NULL },
{ "tls_reqcert", "hard", NULL },
{ "userSearchBase", "dc=example,dc=com", NULL },
{ "userSearchScope", "sub", NULL },
{ "userSearchFilter", NULL, NULL },
{ "groupSearchBase", "dc=example,dc=com", NULL },
{ "groupSearchScope", "sub", NULL },
{ "groupSearchFilter", NULL, NULL },
{ "ldapSchema", "rfc2307", NULL },
{ "offline_timeout", "60", NULL },
{ "force_upper_case_realm", "0", NULL },
{ "enumeration_refresh_timeout", "300", NULL }
};
struct sdap_id_map rfc2307_user_map[] = {
{ "userObjectClass", "posixAccount", SYSDB_USER_CLASS, NULL },
{ "userName", "uid", SYSDB_NAME, NULL },
{ "userPwd", "userPassword", SYSDB_PWD, NULL },
{ "userUidNumber", "uidNumber", SYSDB_UIDNUM, NULL },
{ "userGidNumber", "gidNumber", SYSDB_GIDNUM, NULL },
{ "userGecos", "gecos", SYSDB_GECOS, NULL },
{ "userHomeDirectory", "homeDirectory", SYSDB_HOMEDIR, NULL },
{ "userShell", "loginShell", SYSDB_SHELL, NULL },
{ "userPrincipal", "krbPrincipalName", SYSDB_UPN, NULL },
{ "userFullname", "cn", SYSDB_FULLNAME, NULL },
{ "userMemberOf", NULL, SYSDB_MEMBEROF, NULL },
{ "userUUID", NULL, SYSDB_UUID, NULL },
{ "userModifyTimestamp", "modifyTimestamp", SYSDB_ORIG_MODSTAMP, NULL }
};
struct sdap_id_map rfc2307_group_map[] = {
{ "groupObjectClass", "posixGroup", SYSDB_GROUP_CLASS, NULL },
{ "groupName", "cn", SYSDB_NAME, NULL },
{ "groupPwd", "userPassword", SYSDB_PWD, NULL },
{ "groupGidNumber", "gidNumber", SYSDB_GIDNUM, NULL },
{ "groupMember", "memberuid", SYSDB_MEMBER, NULL },
{ "groupUUID", NULL, SYSDB_UUID, NULL },
{ "groupModifyTimestamp", "modifyTimestamp", SYSDB_ORIG_MODSTAMP, NULL }
};
struct sdap_id_map rfc2307bis_user_map[] = {
{ "userObjectClass", "posixAccount", SYSDB_USER_CLASS, NULL },
{ "userName", "uid", SYSDB_NAME, NULL },
{ "userPwd", "userPassword", SYSDB_PWD, NULL },
{ "userUidNumber", "uidNumber", SYSDB_UIDNUM, NULL },
{ "userGidNumber", "gidNumber", SYSDB_GIDNUM, NULL },
{ "userGecos", "gecos", SYSDB_GECOS, NULL },
{ "userHomeDirectory", "homeDirectory", SYSDB_HOMEDIR, NULL },
{ "userShell", "loginShell", SYSDB_SHELL, NULL },
{ "userPrincipal", "krbPrincipalName", SYSDB_UPN, NULL },
{ "userFullname", "cn", SYSDB_FULLNAME, NULL },
{ "userMemberOf", "memberOf", SYSDB_MEMBEROF, NULL },
/* FIXME: this is 389ds specific */
{ "userUUID", "nsUniqueId", SYSDB_UUID, NULL },
{ "userModifyTimestamp", "modifyTimestamp", SYSDB_ORIG_MODSTAMP, NULL }
};
struct sdap_id_map rfc2307bis_group_map[] = {
{ "groupObjectClass", "posixGroup", SYSDB_GROUP_CLASS, NULL },
{ "groupName", "cn", SYSDB_NAME, NULL },
{ "groupPwd", "userPassword", SYSDB_PWD, NULL },
{ "groupGidNumber", "gidNumber", SYSDB_GIDNUM, NULL },
{ "groupMember", "member", SYSDB_MEMBER, NULL },
/* FIXME: this is 389ds specific */
{ "groupUUID", "nsUniqueId", SYSDB_UUID, NULL },
{ "groupModifyTimestamp", "modifyTimestamp", SYSDB_ORIG_MODSTAMP, NULL }
};
/* =Retrieve-Options====================================================== */
int sdap_get_options(TALLOC_CTX *memctx,
struct confdb_ctx *cdb,
const char *conf_path,
struct sdap_options **_opts)
{
struct sdap_id_map *default_user_map;
struct sdap_id_map *default_group_map;
struct sdap_options *opts;
int i, ret;
opts = talloc(memctx, struct sdap_options);
if (!opts) return ENOMEM;
opts->basic = talloc_array(opts, struct sdap_gen_opts, SDAP_OPTS_BASIC);
if (!opts) return ENOMEM;
opts->user_map = talloc_array(opts, struct sdap_id_map, SDAP_OPTS_USER);
if (!opts) return ENOMEM;
opts->group_map = talloc_array(opts, struct sdap_id_map, SDAP_OPTS_GROUP);
if (!opts) return ENOMEM;
for (i = 0; i < SDAP_OPTS_BASIC; i++) {
opts->basic[i].opt_name = default_basic_opts[i].opt_name;
opts->basic[i].def_value = default_basic_opts[i].def_value;
ret = confdb_get_string(cdb, opts, conf_path,
opts->basic[i].opt_name,
opts->basic[i].def_value,
&opts->basic[i].value);
if (ret != EOK ||
(opts->basic[i].def_value && !opts->basic[i].value)) {
DEBUG(0, ("Failed to retrieve a value (%s)\n",
opts->basic[i].opt_name));
if (ret != EOK) ret = EINVAL;
goto done;
}
DEBUG(5, ("Option %s has value %s\n",
opts->basic[i].opt_name, opts->basic[i].value));
}
/* re-read special options that are easier to be consumed after they are
* transformed */
/* TODO: better to have a blob object than a string here */
ret = confdb_get_string(cdb, opts, conf_path,
"defaultAuthtok", NULL,
&opts->default_authtok);
if (ret != EOK) goto done;
if (opts->default_authtok) {
opts->default_authtok_size = strlen(opts->default_authtok);
}
ret = confdb_get_int(cdb, opts, conf_path,
"network_timeout", 5,
&opts->network_timeout);
if (ret != EOK) goto done;
ret = confdb_get_int(cdb, opts, conf_path,
"opt_timeout", 5,
&opts->opt_timeout);
if (ret != EOK) goto done;
ret = confdb_get_int(cdb, opts, conf_path,
"offline_timeout", 60,
&opts->offline_timeout);
if (ret != EOK) goto done;
ret = confdb_get_bool(cdb, opts, conf_path,
"force_upper_case_realm", false,
&opts->force_upper_case_realm);
if (ret != EOK) goto done;
ret = confdb_get_int(cdb, opts, conf_path,
"enumeration_refresh_timeout", 300,
&opts->enum_refresh_timeout);
if (ret != EOK) goto done;
/* schema type */
if (strcasecmp(opts->basic[SDAP_SCHEMA].value, "rfc2307") == 0) {
opts->schema_type = SDAP_SCHEMA_RFC2307;
default_user_map = rfc2307_user_map;
default_group_map = rfc2307_group_map;
} else
if (strcasecmp(opts->basic[SDAP_SCHEMA].value, "rfc2307bis") == 0) {
opts->schema_type = SDAP_SCHEMA_RFC2307BIS;
default_user_map = rfc2307bis_user_map;
default_group_map = rfc2307bis_group_map;
} else {
DEBUG(0, ("Unrecognized schema type: %s\n",
opts->basic[SDAP_SCHEMA].value));
ret = EINVAL;
goto done;
}
for (i = 0; i < SDAP_OPTS_USER; i++) {
opts->user_map[i].opt_name = default_user_map[i].opt_name;
opts->user_map[i].def_name = default_user_map[i].def_name;
opts->user_map[i].sys_name = default_user_map[i].sys_name;
ret = confdb_get_string(cdb, opts, conf_path,
opts->user_map[i].opt_name,
opts->user_map[i].def_name,
&opts->user_map[i].name);
if (ret != EOK ||
(opts->user_map[i].def_name && !opts->user_map[i].name)) {
DEBUG(0, ("Failed to retrieve a value (%s)\n",
opts->user_map[i].opt_name));
if (ret != EOK) ret = EINVAL;
goto done;
}
DEBUG(5, ("Option %s has value %s\n",
opts->user_map[i].opt_name, opts->user_map[i].name));
}
for (i = 0; i < SDAP_OPTS_GROUP; i++) {
opts->group_map[i].opt_name = default_group_map[i].opt_name;
opts->group_map[i].def_name = default_group_map[i].def_name;
opts->group_map[i].sys_name = default_group_map[i].sys_name;
ret = confdb_get_string(cdb, opts, conf_path,
opts->group_map[i].opt_name,
opts->group_map[i].def_name,
&opts->group_map[i].name);
if (ret != EOK ||
(opts->group_map[i].def_name && !opts->group_map[i].name)) {
DEBUG(0, ("Failed to retrieve a value (%s)\n",
opts->group_map[i].opt_name));
if (ret != EOK) ret = EINVAL;
goto done;
}
DEBUG(5, ("Option %s has value %s\n",
opts->group_map[i].opt_name, opts->group_map[i].name));
}
ret = EOK;
*_opts = opts;
done:
if (ret != EOK) talloc_zfree(opts);
return ret;
}
/* =Parse-msg============================================================= */
static int sdap_parse_entry(TALLOC_CTX *memctx,
struct sdap_handle *sh, struct sdap_msg *sm,
struct sdap_id_map *map, int attrs_num,
struct sysdb_attrs **_attrs, char **_dn)
{
struct sysdb_attrs *attrs;
BerElement *ber = NULL;
char **vals;
char *str;
int lerrno;
int a, i, ret;
lerrno = 0;
ldap_set_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
attrs = sysdb_new_attrs(memctx);
if (!attrs) return ENOMEM;
str = ldap_get_dn(sh->ldap, sm->msg);
if (!str) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
DEBUG(1, ("ldap_get_dn failed: %d(%s)\n",
lerrno, ldap_err2string(lerrno)));
ret = EIO;
goto fail;
}
DEBUG(9, ("OriginalDN: [%s].\n", str));
ret = sysdb_attrs_add_string(attrs, SYSDB_ORIG_DN, str);
if (ret) goto fail;
if (_dn) {
*_dn = talloc_strdup(memctx, str);
if (!*_dn) {
ret = ENOMEM;
ldap_memfree(str);
goto fail;
}
}
ldap_memfree(str);
vals = ldap_get_values(sh->ldap, sm->msg, "objectClass");
if (!vals) {
DEBUG(1, ("Unknown entry type, no objectClasses found!\n"));
ret = EINVAL;
goto fail;
}
for (i = 0; vals[i]; i++) {
/* the objectclass is always the first name in the map */
if (strcasecmp(vals[i], map[0].name) == 0) {
/* ok it's a user */
break;
}
}
if (!vals[i]) {
DEBUG(1, ("Not a user entry, objectClass not matching: %s\n",
map[0].name));
ldap_value_free(vals);
ret = EINVAL;
goto fail;
}
ldap_value_free(vals);
str = ldap_first_attribute(sh->ldap, sm->msg, &ber);
if (!str) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
DEBUG(1, ("Entry has no attributes [%d(%s)]!?\n",
lerrno, ldap_err2string(lerrno)));
ret = EINVAL;
goto fail;
}
while (str) {
for (a = 1; a < attrs_num; a++) {
/* check if this attr is valid with the chosen schema */
if (!map[a].name) continue;
/* check if it is an attr we are interested in */
if (strcasecmp(str, map[a].name) == 0) break;
}
if (a < attrs_num) {
/* interesting attr */
vals = ldap_get_values(sh->ldap, sm->msg, str);
if (!vals) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
DEBUG(1, ("LDAP Library error: %d(%s)",
lerrno, ldap_err2string(lerrno)));
ret = EIO;
goto fail;
}
if (!vals[0]) {
DEBUG(1, ("Missing value after ldap_get_values() ??\n"));
ret = EINVAL;
goto fail;
}
for (i = 0; vals[i]; i++) {
ret = sysdb_attrs_add_string(attrs, map[a].sys_name, vals[i]);
if (ret) goto fail;
}
ldap_value_free(vals);
}
ldap_memfree(str);
str = ldap_next_attribute(sh->ldap, sm->msg, ber);
}
ber_free(ber, 0);
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
if (lerrno) {
DEBUG(1, ("LDAP Library error: %d(%s)",
lerrno, ldap_err2string(lerrno)));
ret = EIO;
goto fail;
}
*_attrs = attrs;
return EOK;
fail:
if (ber) ber_free(ber, 0);
talloc_free(attrs);
return ret;
}
/* This function converts an ldap message into a sysdb_attrs structure.
* It converts only known user attributes, the rest are ignored.
* If the entry is not that of an user an error is returned.
* The original DN is stored as an attribute named originalDN */
int sdap_parse_user(TALLOC_CTX *memctx, struct sdap_options *opts,
struct sdap_handle *sh, struct sdap_msg *sm,
struct sysdb_attrs **_attrs, char **_dn)
{
return sdap_parse_entry(memctx, sh, sm, opts->user_map,
SDAP_OPTS_USER, _attrs, _dn);
}
/* This function converts an ldap message into a sysdb_attrs structure.
* It converts only known group attributes, the rest are ignored.
* If the entry is not that of an user an error is returned.
* The original DN is stored as an attribute named originalDN */
int sdap_parse_group(TALLOC_CTX *memctx, struct sdap_options *opts,
struct sdap_handle *sh, struct sdap_msg *sm,
struct sysdb_attrs **_attrs, char **_dn)
{
return sdap_parse_entry(memctx, sh, sm, opts->group_map,
SDAP_OPTS_GROUP, _attrs, _dn);
}
/* =Get-DN-from-message=================================================== */
int sdap_get_msg_dn(TALLOC_CTX *memctx, struct sdap_handle *sh,
struct sdap_msg *sm, char **_dn)
{
char *str;
int lerrno;
lerrno = 0;
ldap_set_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
str = ldap_get_dn(sh->ldap, sm->msg);
if (!str) {
ldap_get_option(sh->ldap, LDAP_OPT_RESULT_CODE, &lerrno);
DEBUG(1, ("ldap_get_dn failed: %d(%s)\n",
lerrno, ldap_err2string(lerrno)));
return EIO;
}
*_dn = talloc_strdup(memctx, str);
ldap_memfree(str);
if (!*_dn) return ENOMEM;
return EOK;
}
|