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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
/* rbacuser.c - RBAC users */
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
*/
#include "portable.h"
#include <stdio.h>
#include <ac/string.h>
#include "slap.h"
#include "slap-config.h"
#include "lutil.h"
#include "rbac.h"
static int ppolicy_cid = -1;
static rbac_user_t *
rbac_alloc_user()
{
rbac_user_t *userp = ch_calloc( 1, sizeof(rbac_user_t) );
BER_BVZERO( &userp->tenantid );
BER_BVZERO( &userp->uid );
BER_BVZERO( &userp->dn );
BER_BVZERO( &userp->password );
BER_BVZERO( &userp->constraints );
BER_BVZERO( &userp->msg );
userp->roles = NULL;
userp->role_constraints = NULL;
return userp;
}
static int
rbac_read_user_cb( Operation *op, SlapReply *rs )
{
rbac_callback_info_t *cbp = op->o_callback->sc_private;
rbac_ad_t *user_ads;
rbac_user_t *userp = NULL;
int rc = 0, i;
Debug( LDAP_DEBUG_ANY, "rbac_read_user_cb\n" );
if ( rs->sr_type != REP_SEARCH ) {
Debug( LDAP_DEBUG_ANY, "rbac_read_user_cb: "
"sr_type != REP_SEARCH\n" );
return 0;
}
assert( cbp );
user_ads = cbp->tenantp->schema->user_ads;
userp = rbac_alloc_user();
if ( !userp ) {
Debug( LDAP_DEBUG_ANY, "rbac_read_user_cb: "
"rbac_alloc_user failed\n" );
goto done;
}
ber_dupbv( &userp->dn, &rs->sr_entry->e_name );
Debug( LDAP_DEBUG_ANY, "DEBUG rbac_read_user_cb (%s): "
"rc (%d)\n",
userp->dn.bv_val, rc );
for ( i = 0; !BER_BVISNULL( &user_ads[i].attr ); i++ ) {
Attribute *attr = NULL;
attr = attr_find( rs->sr_entry->e_attrs, *user_ads[i].ad );
if ( attr != NULL ) {
switch ( user_ads[i].type ) {
case RBAC_ROLE_ASSIGNMENT:
ber_bvarray_dup_x( &userp->roles, attr->a_nvals, NULL );
break;
case RBAC_ROLE_CONSTRAINTS:
ber_bvarray_dup_x(
&userp->role_constraints, attr->a_nvals, NULL );
break;
case RBAC_USER_CONSTRAINTS:
ber_dupbv_x( &userp->constraints, &attr->a_nvals[0], NULL );
break;
case RBAC_UID:
ber_dupbv_x( &userp->uid, &attr->a_nvals[0], NULL );
break;
default:
break;
}
}
}
done:;
cbp->private = userp;
return 0;
}
static int
rbac_bind_cb( Operation *op, SlapReply *rs )
{
rbac_user_t *ui = op->o_callback->sc_private;
LDAPControl *ctrl = ldap_control_find(
LDAP_CONTROL_PASSWORDPOLICYRESPONSE, rs->sr_ctrls, NULL );
if ( ctrl ) {
LDAP *ld;
ber_int_t expire, grace;
LDAPPasswordPolicyError error;
ldap_create( &ld );
if ( ld ) {
int rc = ldap_parse_passwordpolicy_control(
ld, ctrl, &expire, &grace, &error );
if ( rc == LDAP_SUCCESS ) {
ui->authz = RBAC_PASSWORD_GOOD;
if ( grace > 0 ) {
//ui->msg.bv_len = sprintf(ui->msg.bv_val,
// "Password expired; %d grace logins remaining",
// grace);
ui->authz = RBAC_BIND_NEW_AUTHTOK_REQD;
} else if ( error != PP_noError ) {
ber_str2bv( ldap_passwordpolicy_err2txt( error ), 0, 0,
&ui->msg );
switch ( error ) {
case PP_passwordExpired:
ui->authz = RBAC_PASSWORD_EXPIRATION_WARNING;
if ( expire >= 0 ) {
char *unit = "seconds";
if ( expire > 60 ) {
expire /= 60;
unit = "minutes";
}
if ( expire > 60 ) {
expire /= 60;
unit = "hours";
}
if ( expire > 24 ) {
expire /= 24;
unit = "days";
}
#if 0 /* Who warns about expiration so far in advance? */
if (expire > 7) {
expire /= 7;
unit = "weeks";
}
if (expire > 4) {
expire /= 4;
unit = "months";
}
if (expire > 12) {
expire /= 12;
unit = "years";
}
#endif
}
//rs->sr_err = ;
break;
case PP_accountLocked:
ui->authz = RBAC_ACCOUNT_LOCKED;
//rs->sr_err = ;
break;
case PP_changeAfterReset:
ui->authz = RBAC_CHANGE_AFTER_RESET;
rs->sr_err = LDAP_SUCCESS;
break;
case PP_passwordModNotAllowed:
ui->authz = RBAC_NO_MODIFICATIONS;
//rs->sr_err = ;
break;
case PP_mustSupplyOldPassword:
ui->authz = RBAC_MUST_SUPPLY_OLD;
//rs->sr_err = ;
break;
case PP_insufficientPasswordQuality:
ui->authz = RBAC_INSUFFICIENT_QUALITY;
//rs->sr_err = ;
break;
case PP_passwordTooShort:
ui->authz = RBAC_PASSWORD_TOO_SHORT;
//rs->sr_err = ;
break;
case PP_passwordTooYoung:
ui->authz = RBAC_PASSWORD_TOO_YOUNG;
//rs->sr_err = ;
break;
case PP_passwordInHistory:
ui->authz = RBAC_HISTORY_VIOLATION;
//rs->sr_err = ;
break;
case PP_noError:
default:
// do nothing
//ui->authz = RBAC_PASSWORD_GOOD;
rs->sr_err = LDAP_SUCCESS;
break;
}
// switch (error) {
// case PP_passwordExpired:
/* report this during authz */
// rs->sr_err = LDAP_SUCCESS;
/* fallthru */
// case PP_changeAfterReset:
// ui->authz = RBAC_BIND_NEW_AUTHTOK_REQD;
// }
}
}
ldap_unbind_ext( ld, NULL, NULL );
}
}
return 0;
}
/* exported user functions */
int
rbac_authenticate_user( Operation *op, rbac_user_t *userp )
{
int rc = LDAP_SUCCESS;
slap_callback cb = { 0 };
SlapReply rs2 = { REP_RESULT };
Operation op2 = *op;
LDAPControl *sctrls[4];
LDAPControl sctrl[3];
int nsctrls = 0;
LDAPControl c;
struct berval ber_bvnull = BER_BVNULL;
struct berval dn, ndn;
rc = dnPrettyNormal( 0, &userp->dn, &dn, &ndn, NULL );
if ( rc != LDAP_SUCCESS ) {
goto done;
}
cb.sc_response = rbac_bind_cb;
cb.sc_private = userp;
op2.o_callback = &cb;
op2.o_dn = ber_bvnull;
op2.o_ndn = ber_bvnull;
op2.o_tag = LDAP_REQ_BIND;
op2.o_protocol = LDAP_VERSION3;
op2.orb_method = LDAP_AUTH_SIMPLE;
op2.orb_cred = userp->password;
op2.o_req_dn = dn;
op2.o_req_ndn = ndn;
// loading the ldap pw policy controls loaded into here, added by smm:
c.ldctl_oid = LDAP_CONTROL_PASSWORDPOLICYREQUEST;
c.ldctl_value.bv_val = NULL;
c.ldctl_value.bv_len = 0;
c.ldctl_iscritical = 0;
sctrl[nsctrls] = c;
sctrls[nsctrls] = &sctrl[nsctrls];
sctrls[++nsctrls] = NULL;
op2.o_ctrls = sctrls;
if ( ppolicy_cid < 0 ) {
rc = slap_find_control_id( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
&ppolicy_cid );
if ( rc != LDAP_SUCCESS ) {
goto done;
}
}
// smm - need to set the control flag too:
op2.o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_CRITICAL;
slap_op_time( &op2.o_time, &op2.o_tincr );
op2.o_bd = frontendDB;
rc = op2.o_bd->be_bind( &op2, &rs2 );
if ( userp->authz > 0 ) {
Debug( LDAP_DEBUG_ANY, "rbac_authenticate_user (%s): "
"password policy violation (%d)\n",
userp->dn.bv_val ? userp->dn.bv_val : "NULL", userp->authz );
}
done:;
ch_free( dn.bv_val );
ch_free( ndn.bv_val );
Debug( LDAP_DEBUG_ANY, "rbac_authenticate_user (%s): "
"rc (%d)\n",
userp->dn.bv_val ? userp->dn.bv_val : "NULL", rc );
return rc;
}
/*
isvalidusername(): from OpenLDAP ~/contrib/slapd-modules/nssov/passwd.c
Checks to see if the specified name is a valid user name.
This test is based on the definition from POSIX (IEEE Std 1003.1, 2004, 3.426 User Name
and 3.276 Portable Filename Character Set):
http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_426
http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_276
The standard defines user names valid if they contain characters from
the set [A-Za-z0-9._-] where the hyphen should not be used as first
character. As an extension this test allows the dolar '$' sign as the last
character to support Samba special accounts.
*/
static int
isvalidusername( struct berval *bv )
{
int i;
char *name = bv->bv_val;
if ( (name == NULL) || ( name[0] == '\0' ) ) return 0;
/* check first character */
if ( !( ( name[0] >= 'A' && name[0] <= 'Z' ) ||
( name[0] >= 'a' && name[0] <= 'z' ) ||
( name[0] >= '0' && name[0] <= '9' ) || name[0] == '.' ||
name[0] == '_' ) )
return 0;
/* check other characters */
for ( i = 1; i < bv->bv_len; i++ ) {
if ( name[i] == '$' ) {
/* if the char is $ we require it to be the last char */
if ( name[i + 1] != '\0' ) return 0;
} else if ( !( ( name[i] >= 'A' && name[i] <= 'Z' ) ||
( name[i] >= 'a' && name[i] <= 'z' ) ||
( name[i] >= '0' && name[i] <= '9' ) ||
name[i] == '.' || name[i] == '_' ||
name[i] == '-' ) )
return 0;
}
/* no test failed so it must be good */
return -1;
}
rbac_user_t *
rbac_read_user( Operation *op, rbac_req_t *reqp )
{
int rc = LDAP_SUCCESS;
tenant_info_t *tenantp = rbac_tid2tenant( &reqp->tenantid );
rbac_user_t *userp = NULL;
char fbuf[RBAC_BUFLEN];
struct berval filter = { sizeof(fbuf), fbuf };
SlapReply rs2 = { REP_RESULT };
Operation op2 = *op;
slap_callback cb = { 0 };
rbac_callback_info_t rbac_cb;
if ( !tenantp ) {
Debug( LDAP_DEBUG_ANY, "rbac_read_user: "
"missing tenant information\n" );
rc = LDAP_UNWILLING_TO_PERFORM;
goto done;
}
/* uid is a pre-requisite for reading the user information */
if ( BER_BVISNULL( &reqp->uid ) ) {
Debug( LDAP_DEBUG_ANY, "rbac_read_user: "
"missing uid, unable to read user entry\n" );
rc = LDAP_UNWILLING_TO_PERFORM;
goto done;
}
if ( !isvalidusername( &reqp->uid ) ) {
Debug( LDAP_DEBUG_ANY, "rbac_read_user: "
"invalid user id\n" );
rc = LDAP_NO_SUCH_OBJECT;
goto done;
}
rbac_cb.tenantp = tenantp;
rbac_cb.private = NULL;
memset( fbuf, 0, sizeof(fbuf) );
strcpy( fbuf, "uid=" );
strncat( fbuf, reqp->uid.bv_val, reqp->uid.bv_len );
filter.bv_val = fbuf;
filter.bv_len = strlen( fbuf );
if ( rc != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY, "rbac_create_session: "
"invalid DN syntax\n" );
goto done;
}
cb.sc_private = &rbac_cb;
cb.sc_response = rbac_read_user_cb;
op2.o_callback = &cb;
op2.o_tag = LDAP_REQ_SEARCH;
op2.o_dn = tenantp->admin;
op2.o_ndn = tenantp->admin;
op2.o_req_dn = tenantp->users_basedn;
op2.o_req_ndn = tenantp->users_basedn;
op2.ors_filterstr = filter;
op2.ors_filter = str2filter_x( &op2, filter.bv_val );
op2.ors_scope = LDAP_SCOPE_SUBTREE;
op2.ors_attrs = tenantp->schema->user_attrs;
op2.ors_tlimit = SLAP_NO_LIMIT;
op2.ors_slimit = SLAP_NO_LIMIT;
op2.ors_attrsonly = 0;
op2.o_bd = frontendDB;
op2.ors_limit = NULL;
rc = op2.o_bd->be_search( &op2, &rs2 );
filter_free_x( &op2, op2.ors_filter, 1 );
done:;
if ( rc == LDAP_SUCCESS && rbac_cb.private ) {
userp = (rbac_user_t *)rbac_cb.private;
if ( !BER_BVISNULL( &reqp->authtok ) )
ber_dupbv( &userp->password, &reqp->authtok );
rbac_cb.private = NULL;
return userp;
} else {
userp = (rbac_user_t *)rbac_cb.private;
rbac_free_user( userp );
return NULL;
}
}
/* evaluate temporal constraints for the user */
int
rbac_user_temporal_constraint( rbac_user_t *userp )
{
int rc = LDAP_SUCCESS;
rbac_constraint_t *cp = NULL;
if ( BER_BVISNULL( &userp->constraints ) ) {
/* no temporal constraint */
goto done;
}
cp = rbac_bv2constraint( &userp->constraints );
if ( !cp ) {
Debug( LDAP_DEBUG_ANY, "rbac_user_temporal_constraint: "
"invalid user constraint \n" );
rc = LDAP_OTHER;
goto done;
}
rc = rbac_check_time_constraint( cp );
done:;
rbac_free_constraint( cp );
return rc;
}
/*
rbac_constraint_t *
rbac_user_role_constraintsx(rbac_user_t *userp)
{
rbac_constraint_t *tmp, *cp = NULL;
int i = 0;
if (!userp || !userp->role_constraints)
goto done;
while (!BER_BVISNULL(&userp->role_constraints[i])) {
tmp = rbac_bv2constraint(&userp->role_constraints[i++]);
if (tmp) {
if (!cp) {
cp = tmp;
} else {
tmp->next = cp;
cp = tmp;
}
}
}
done:;
return cp;
}
*/
rbac_constraint_t *
rbac_user_role_constraints( BerVarray values )
{
rbac_constraint_t *curr, *head = NULL;
int i = 0;
if ( values ) {
while ( !BER_BVISNULL( &values[i] ) ) {
curr = rbac_bv2constraint( &values[i++] );
if ( curr ) {
curr->next = head;
head = curr;
}
}
}
return head;
}
/*
void main() {
item * curr, * head;
int i;
head = NULL;
for(i=1;i<=10;i++) {
curr = (item *)malloc(sizeof(item));
curr->val = i;
curr->next = head;
head = curr;
}
curr = head;
while(curr) {
printf("%d\n", curr->val);
curr = curr->next ;
}
}
*/
/*
*
rbac_user_role_constraints2(BerVarray values)
{
rbac_constraint_t *tmp, *cp = NULL;
int i = 0;
if (!values)
goto done;
while (!BER_BVISNULL(&values[i])) {
tmp = rbac_bv2constraint(&values[i++]);
if (tmp) {
if (!cp) {
cp = tmp;
} else {
tmp->next = cp;
cp = tmp;
//cp->next = tmp;
//cp = tmp->next;
}
}
}
done:;
return cp;
}
rbac_user_role_constraints3(rbac_constraint_t *values)
{
rbac_constraint_t *tmp, *cp = NULL;
int i = 0;
if (!values)
goto done;
while (!BER_BVISNULL(values[i])) {
tmp = rbac_bv2constraint(&values[i++]);
if (tmp) {
if (!cp) {
cp = tmp;
} else {
tmp->next = cp;
cp = tmp;
}
}
}
done:;
return cp;
}
*/
void
rbac_free_user( rbac_user_t *userp )
{
if ( !userp ) return;
if ( !BER_BVISNULL( &userp->tenantid ) ) {
ber_memfree( userp->tenantid.bv_val );
}
if ( !BER_BVISNULL( &userp->uid ) ) {
ber_memfree( userp->uid.bv_val );
}
if ( !BER_BVISNULL( &userp->dn ) ) {
ber_memfree( userp->dn.bv_val );
}
if ( !BER_BVISNULL( &userp->constraints ) ) {
ber_memfree( userp->constraints.bv_val );
}
if ( !BER_BVISNULL( &userp->password ) ) {
ber_memfree( userp->password.bv_val );
}
if ( !BER_BVISNULL( &userp->msg ) ) {
ber_memfree( userp->msg.bv_val );
}
if ( userp->roles ) ber_bvarray_free( userp->roles );
if ( userp->role_constraints ) ber_bvarray_free( userp->role_constraints );
ch_free( userp );
}
|