summaryrefslogtreecommitdiffstats
path: root/contrib/slapd-modules/rbac/util.c
blob: 11a5e54a2418994a2217d16bf23fb3e90c22ddf0 (plain)
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
/* util.c - RBAC utility */
/* $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/ctype.h>
#include <ac/string.h>

#include "slap.h"
#include "slap-config.h"
#include "lutil.h"

#include "rbac.h"

#define DELIMITER '$'

#define SUNDAY 0x01
#define MONDAY 0x02
#define TUESDAY 0x04
#define WEDNESDAY 0x08
#define THURSDAY 0x10
#define FRIDAY 0x20
#define SATURDAY 0x40

#define ALL_WEEK "all"

void
rbac_free_constraint( rbac_constraint_t *cp )
{
	if ( !cp ) return;

	if ( !BER_BVISNULL( &cp->name ) ) {
		ch_free( cp->name.bv_val );
	}

	ch_free( cp );
}

void
rbac_free_constraints( rbac_constraint_t *constraints )
{
	rbac_constraint_t *cp, *tmp;

	if ( !constraints ) return;

	tmp = constraints;
	while ( tmp ) {
		cp = tmp->next;
		rbac_free_constraint( tmp );
		tmp = cp;
	}

	return;
}

rbac_constraint_t *
rbac_alloc_constraint()
{
	rbac_constraint_t *cp = NULL;

	cp = ch_calloc( 1, sizeof(rbac_constraint_t) );
	return cp;
}

static int
is_well_formed_constraint( struct berval *bv )
{
	int rc = LDAP_SUCCESS;

	/* assume well-formed role/user-constraints, for the moment */

	if ( rc != LDAP_SUCCESS ) {
		Debug( LDAP_DEBUG_ANY, "is_well_formed_constraint: "
				"rbac role/user constraint not well-formed: %s\n",
				bv->bv_val );
	}

	return rc;
}

/* input contains 4 digits, representing time */
/* in hhmm format */
static int
constraint_parse_time( char *input )
{
	int btime;
	char *ptr = input;

	btime = ( *ptr++ - '0' ) * 12;
	btime += ( *ptr++ - '0' );
	btime *= 60; /* turning into mins */
	btime += ( *ptr++ - '0' ) * 10;
	btime += ( *ptr++ - '0' );
	btime *= 60; /* turning into secs */

	return btime;
}

/* input contains 4 digits, representing year */
/* in yyyy format */
static int
constraint_parse_year( char *input )
{
	int i;
	int year = 0;
	char *ptr = input;

	for ( i = 0; i <= 3; i++, ptr++ ) {
		year = year * 10 + *ptr - '0';
	}

	return year;
}

/* input contains 2 digits, representing month */
/* in mm format */
static int
constraint_parse_month( char *input )
{
	int i;
	int month = 0;
	char *ptr = input;

	for ( i = 0; i < 2; i++, ptr++ ) {
		month = month * 10 + *ptr - '0';
	}

	return month;
}

/* input contains 2 digits, representing day in month */
/* in dd format */
static int
constraint_parse_day_in_month( char *input )
{
	int i;
	int day_in_month = 0;
	char *ptr = input;

	for ( i = 0; i < 2; i++, ptr++ ) {
		day_in_month = day_in_month * 10 + *ptr - '0';
	}

	return day_in_month;
}

rbac_constraint_t *
rbac_bv2constraint( struct berval *bv )
{
	rbac_constraint_t *cp = NULL;
	int rc = LDAP_SUCCESS;
	char *ptr, *endp = NULL;
	int len = 0;
	int year, month, mday;

	if ( !bv || BER_BVISNULL( bv ) ) goto done;

	rc = is_well_formed_constraint( bv );
	if ( rc != LDAP_SUCCESS ) {
		goto done;
	}

	cp = rbac_alloc_constraint();
	if ( !cp ) {
		rc = LDAP_UNWILLING_TO_PERFORM;
		goto done;
	}

	/* constraint name */
	ptr = bv->bv_val;
	endp = ptr;
	while ( *endp != DELIMITER ) {
		endp++;
		len++;
	}

	if ( len > 0 ) {
		cp->name.bv_val = ch_malloc( len + 1 );
		strncpy( cp->name.bv_val, ptr, len );
		cp->name.bv_val[len] = '\0';
		cp->name.bv_len = len;
	} else {
		rc = LDAP_OTHER;
		goto done;
	}

	/* allowed inactivity period */
	ptr = endp;
	endp++;
	if ( isdigit( *endp ) ) {
		int secs = 0;
		while ( isdigit( *endp ) ) {
			secs = secs * 10 + *endp - '0';
			endp++;
		}
		cp->allowed_inactivity = secs;
	} else if ( *endp != DELIMITER ) {
		rc = LDAP_OTHER;
		goto done;
	}

	ptr = endp;
	endp = ptr + 1;

	/* begin time */
	if ( isdigit( *endp ) ) {
		cp->begin_time = constraint_parse_time( endp );
		while ( isdigit( *endp ) )
			endp++;
	}

	ptr = endp;
	while ( *ptr != DELIMITER )
		ptr++;
	endp = ptr + 1;

	/* end time */
	if ( isdigit( *endp ) ) {
		cp->end_time = constraint_parse_time( endp );
		while ( isdigit( *endp ) )
			endp++;
	}

	ptr = endp;
	while ( *ptr != DELIMITER )
		ptr++;
	endp = ptr + 1;

	/* begin year/month/day_in_month */
	if ( isdigit( *endp ) ) {
		lutil_tm tm;
		year = constraint_parse_year( endp );
		endp += 4;
		month = constraint_parse_month( endp );
		endp += 2;
		mday = constraint_parse_day_in_month( endp );
		endp += 2;

		tm.tm_year = year - 1900;
		tm.tm_mon = month - 1;
		tm.tm_mday = mday;
		tm.tm_sec = 0;
		tm.tm_min = 0;
		tm.tm_hour = 0;

		lutil_tm2time( &tm, &cp->begin_date );
	}

	ptr = endp;
	while ( *ptr != DELIMITER )
		ptr++;
	endp = ptr + 1;

	/* end year/month/day_in_month */
	if ( isdigit( *endp ) ) {
		lutil_tm tm;
		year = constraint_parse_year( endp );
		endp += 4;
		month = constraint_parse_month( endp );
		endp += 2;
		mday = constraint_parse_day_in_month( endp );
		endp += 2;

		tm.tm_year = year - 1900;
		tm.tm_mon = month - 1;
		tm.tm_mday = mday;
		tm.tm_sec = 0;
		tm.tm_min = 0;
		tm.tm_hour = 0;

		lutil_tm2time( &tm, &cp->end_date );
	}

	ptr = endp;
	while ( *ptr != DELIMITER )
		ptr++;
	endp = ptr + 1;

	/* begin lock year/month/day_in_month */
	if ( isdigit( *endp ) ) {
		lutil_tm tm;
		year = constraint_parse_year( endp );
		endp += 4;
		month = constraint_parse_month( endp );
		endp += 2;
		mday = constraint_parse_day_in_month( endp );
		endp += 2;

		tm.tm_year = year - 1900;
		tm.tm_mon = month - 1;
		tm.tm_mday = mday;
		tm.tm_sec = 0;
		tm.tm_min = 0;
		tm.tm_hour = 0;

		lutil_tm2time( &tm, &cp->begin_lock_date );
	}

	ptr = endp;
	while ( *ptr != DELIMITER )
		ptr++;
	endp = ptr + 1;

	/* end lock year/month/day_in_month */
	if ( isdigit( *endp ) ) {
		lutil_tm tm;

		year = constraint_parse_year( endp );
		endp += 4;
		month = constraint_parse_month( endp );
		endp += 2;
		mday = constraint_parse_day_in_month( endp );
		endp += 2;

		tm.tm_year = year - 1900;
		tm.tm_mon = month - 1;
		tm.tm_mday = mday;
		tm.tm_sec = 0;
		tm.tm_min = 0;
		tm.tm_hour = 0;

		lutil_tm2time( &tm, &cp->end_lock_date );
	}

	ptr = endp;
	while ( *ptr != DELIMITER )
		ptr++;
	endp = ptr + 1;

	/* dayMask */

	/* allow "all" to mean the entire week */
	if ( strncasecmp( endp, ALL_WEEK, strlen( ALL_WEEK ) ) == 0 ) {
		cp->day_mask = SUNDAY | MONDAY | TUESDAY | WEDNESDAY | THURSDAY |
				FRIDAY | SATURDAY;
	}

	while ( *endp && isdigit( *endp ) ) {
		switch ( *endp - '0' ) {
			case 1:
				cp->day_mask |= SUNDAY;
				break;
			case 2:
				cp->day_mask |= MONDAY;
				break;
			case 3:
				cp->day_mask |= TUESDAY;
				break;
			case 4:
				cp->day_mask |= WEDNESDAY;
				break;
			case 5:
				cp->day_mask |= THURSDAY;
				break;
			case 6:
				cp->day_mask |= FRIDAY;
				break;
			case 7:
				cp->day_mask |= SATURDAY;
				break;
			default:
				/* should not be here */
				rc = LDAP_OTHER;
				goto done;
		}
		endp++;
	}

done:;
	if ( rc != LDAP_SUCCESS ) {
		rbac_free_constraint( cp );
		cp = NULL;
	}

	return cp;
}

static int
constraint_day_of_week( rbac_constraint_t *cp, int wday )
{
	int rc = LDAP_UNWILLING_TO_PERFORM;

	/* assumption: Monday is 1st day of a week */
	switch ( wday ) {
		case 1:
			if ( !(cp->day_mask & MONDAY) ) goto done;
			break;
		case 2:
			if ( !(cp->day_mask & TUESDAY) ) goto done;
			break;
		case 3:
			if ( !(cp->day_mask & WEDNESDAY) ) goto done;
			break;
		case 4:
			if ( !(cp->day_mask & THURSDAY) ) goto done;
			break;
		case 5:
			if ( !(cp->day_mask & FRIDAY) ) goto done;
			break;
		case 6:
			if ( !(cp->day_mask & SATURDAY) ) goto done;
			break;
		case 0:
		case 7:
			if ( !(cp->day_mask & SUNDAY) ) goto done;
			break;
		default:
			/* should not be here */
			goto done;
	}

	rc = LDAP_SUCCESS;

done:;
	return rc;
}

int
rbac_check_time_constraint( rbac_constraint_t *cp )
{
	int rc = LDAP_UNWILLING_TO_PERFORM;
	time_t now;
	struct tm result, *resultp;

	now = slap_get_time();

	/*
	 * does slapd support day-of-week (wday)?
	 * using native routine for now.
	 * Win32's gmtime call is already thread-safe, to the _r
	 * decorator is unneeded.
	 */
#ifdef _WIN32
	resultp = gmtime( &now );
#else
	resultp = gmtime_r( &now, &result );
#endif
	if ( !resultp ) goto done;
#if 0
	timestamp.bv_val = timebuf;
	timestamp.bv_len = sizeof(timebuf);
	slap_timestamp(&now, &timestamp);
	lutil_parsetime(timestamp.bv_val, &now_tm);
	lutil_tm2time(&now_tm, &now_tt);
#endif

	if ( ( cp->begin_date.tt_sec > 0 && cp->begin_date.tt_sec > now ) ||
			( cp->end_date.tt_sec > 0 && cp->end_date.tt_sec < now ) ) {
		/* not within allowed time period */
		goto done;
	}

	/* allowed time period during a day */
	if ( cp->begin_time > 0 && cp->end_time > 0 ) {
		int timeofday = ( resultp->tm_hour * 60 + resultp->tm_min ) * 60 +
				resultp->tm_sec;
		if ( timeofday < cp->begin_time || timeofday > cp->end_time ) {
			/* not within allowed time period in a day */
			goto done;
		}
	}

	/* allowed day in a week */
	if ( cp->day_mask > 0 ) {
		rc = constraint_day_of_week( cp, resultp->tm_wday );
		if ( rc != LDAP_SUCCESS ) goto done;
	}

	/* during lock-out period? */
	if ( ( cp->begin_lock_date.tt_sec > 0 &&
				 cp->begin_lock_date.tt_sec < now ) &&
			( cp->end_lock_date.tt_sec > 0 &&
					cp->end_lock_date.tt_sec > now ) ) {
		/* within locked out period */
		rc = LDAP_UNWILLING_TO_PERFORM;
		goto done;
	}

	/* passed all tests */
	rc = LDAP_SUCCESS;

done:;
	return rc;
}

rbac_constraint_t *
rbac_role2constraint( struct berval *role, rbac_constraint_t *role_constraints )
{
	rbac_constraint_t *cp = NULL;

	if ( !role_constraints || !role ) goto done;

	cp = role_constraints;
	while ( cp ) {
		if ( ber_bvstrcasecmp( role, &cp->name ) == 0 ) {
			/* found the role constraint */
			goto done;
		}
		cp = cp->next;
	}

done:;
	return cp;
}

void
rbac_to_lower( struct berval *bv )
{
	// convert the berval to lower case:
	int i;
	for ( i = 0; i < bv->bv_len; i++ ) {
		bv->bv_val[i] = tolower( bv->bv_val[i] );
	}
}