smbk5pwd.c 25 KB
Newer Older
1
2
3
/* smbk5pwd.c - Overlay for managing Samba and Heimdal passwords */
/* $OpenLDAP$ */
/*
4
 * Copyright 2004-2005 by Howard Chu, Symas Corp.
5
6
7
8
9
10
11
12
13
14
 * 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>.
 */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
15
16
/*
 * Support for table-driven configuration added by Pierangelo Masarati.
17
 * Support for sambaPwdMustChange and sambaPwdCanChange added by Marco D'Ettorre.
Pierangelo Masarati's avatar
Pierangelo Masarati committed
18
19
20
 *
 * The conditions of the OpenLDAP Public License apply.
 */
21
22
23
24
25
26
27
28
29
30
31

#include <portable.h>

#ifndef SLAPD_OVER_SMBK5PWD
#define SLAPD_OVER_SMBK5PWD SLAPD_MOD_DYNAMIC
#endif

#ifdef SLAPD_OVER_SMBK5PWD

#include <slap.h>
#include <ac/errno.h>
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
32
#include <ac/string.h>
33

34
35
#include "config.h"

36
#ifdef DO_KRB5
37
38
39
40
#include <lber.h>
#include <lber_pvt.h>
#include <lutil.h>

41
42
43
44
45
46
47
/* make ASN1_MALLOC_ENCODE use our allocator */
#define malloc	ch_malloc

#include <krb5.h>
#include <kadm5/admin.h>
#include <hdb.h>

48
49
50
51
52
53
#ifndef HDB_INTERFACE_VERSION
#define	HDB_MASTER_KEY_SET	master_key_set
#else
#define	HDB_MASTER_KEY_SET	hdb_master_key_set
#endif

54
55
56
57
58
59
60
61
62
63
64
65
66
67
static krb5_context context;
static void *kadm_context;
static kadm5_config_params conf;
static HDB *db;

static AttributeDescription *ad_krb5Key;
static AttributeDescription *ad_krb5KeyVersionNumber;
static AttributeDescription *ad_krb5PrincipalName;
static ObjectClass *oc_krb5KDCEntry;
#endif

#ifdef DO_SAMBA
#include <openssl/des.h>
#include <openssl/md4.h>
68
#include "ldap_utf8.h"
69
70
71
72

static AttributeDescription *ad_sambaLMPassword;
static AttributeDescription *ad_sambaNTPassword;
static AttributeDescription *ad_sambaPwdLastSet;
73
static AttributeDescription *ad_sambaPwdMustChange;
74
static AttributeDescription *ad_sambaPwdCanChange;
75
76
77
static ObjectClass *oc_sambaSamAccount;
#endif

78
79
80
81
82
83
84
85
86
87
88
/* Per-instance configuration information */
typedef struct smbk5pwd_t {
	unsigned	mode;
#define	SMBK5PWD_F_KRB5		(0x1U)
#define	SMBK5PWD_F_SAMBA	(0x2U)

#define SMBK5PWD_DO_KRB5(pi)	((pi)->mode & SMBK5PWD_F_KRB5)
#define SMBK5PWD_DO_SAMBA(pi)	((pi)->mode & SMBK5PWD_F_SAMBA)

#ifdef DO_KRB5
	/* nothing yet */
89
90
#endif

91
92
93
#ifdef DO_SAMBA
	/* How many seconds before forcing a password change? */
	time_t	smb_must_change;
94
95
        /* How many seconds after allowing a password change? */
        time_t  smb_can_change;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#endif
} smbk5pwd_t;

static const unsigned SMBK5PWD_F_ALL	=
	0
#ifdef DO_KRB5
	| SMBK5PWD_F_KRB5
#endif
#ifdef DO_SAMBA
	| SMBK5PWD_F_SAMBA
#endif
;

static int smbk5pwd_modules_init( smbk5pwd_t *pi );

111
#ifdef DO_SAMBA
112
static const char hex[] = "0123456789abcdef";
113
114
115

/* From liblutil/passwd.c... */
static void lmPasswd_to_key(
116
	const char *lmPasswd,
117
	des_cblock *key)
118
{
119
120
121
	const unsigned char *lpw = (const unsigned char *)lmPasswd;
	unsigned char *k = (unsigned char *)key;

122
	/* make room for parity bits */
123
124
125
126
127
128
129
130
	k[0] = lpw[0];
	k[1] = ((lpw[0]&0x01)<<7) | (lpw[1]>>1);
	k[2] = ((lpw[1]&0x03)<<6) | (lpw[2]>>2);
	k[3] = ((lpw[2]&0x07)<<5) | (lpw[3]>>3);
	k[4] = ((lpw[3]&0x0F)<<4) | (lpw[4]>>4);
	k[5] = ((lpw[4]&0x1F)<<3) | (lpw[5]>>5);
	k[6] = ((lpw[5]&0x3F)<<2) | (lpw[6]>>6);
	k[7] = ((lpw[6]&0x7F)<<1);
131
132

	des_set_odd_parity( key );
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
}

#define MAX_PWLEN 256
#define	HASHLEN	16

static void hexify(
	const char in[HASHLEN],
	struct berval *out
)
{
	int i;
	char *a;
	unsigned char *b;

	out->bv_val = ch_malloc(HASHLEN*2 + 1);
	out->bv_len = HASHLEN*2;

	a = out->bv_val;
	b = (unsigned char *)in;
	for (i=0; i<HASHLEN; i++) {
		*a++ = hex[*b >> 4];
		*a++ = hex[*b++ & 0x0f];
	}
	*a++ = '\0';
}

static void lmhash(
	struct berval *passwd,
	struct berval *hash
)
{
164
165
166
167
	char UcasePassword[15];
	des_cblock key;
	des_key_schedule schedule;
	des_cblock StdText = "KGS!@#$%";
168
169
	des_cblock hbuf[2];

170
171
172
	strncpy( UcasePassword, passwd->bv_val, 14 );
	UcasePassword[14] = '\0';
	ldap_pvt_str2upper( UcasePassword );
173

174
175
176
	lmPasswd_to_key( UcasePassword, &key );
	des_set_key_unchecked( &key, schedule );
	des_ecb_encrypt( &StdText, &hbuf[0], schedule , DES_ENCRYPT );
177

178
179
180
	lmPasswd_to_key( &UcasePassword[7], &key );
	des_set_key_unchecked( &key, schedule );
	des_ecb_encrypt( &StdText, &hbuf[1], schedule , DES_ENCRYPT );
181
182
183
184
185
186
187
188
189

	hexify( (char *)hbuf, hash );
}

static void nthash(
	struct berval *passwd,
	struct berval *hash
)
{
190
191
	/* Windows currently only allows 14 character passwords, but
	 * may support up to 256 in the future. We assume this means
192
	 * 256 UCS2 characters, not 256 bytes...
193
	 */
194
	char hbuf[HASHLEN];
195
	MD4_CTX ctx;
196
197
198
199

	if (passwd->bv_len > MAX_PWLEN*2)
		passwd->bv_len = MAX_PWLEN*2;
		
200
201
	MD4_Init( &ctx );
	MD4_Update( &ctx, passwd->bv_val, passwd->bv_len );
202
	MD4_Final( (unsigned char *)hbuf, &ctx );
203
204
205
206
207

	hexify( hbuf, hash );
}
#endif /* DO_SAMBA */

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
#ifdef DO_KRB5

static int smbk5pwd_op_cleanup(
	Operation *op,
	SlapReply *rs )
{
	slap_callback *cb;

	/* clear out the current key */
	ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup,
		NULL, NULL );

	/* free the callback */
	cb = op->o_callback;
	op->o_callback = cb->sc_next;
	op->o_tmpfree( cb, op->o_tmpmemctx );
	return 0;
}

static int smbk5pwd_op_bind(
	Operation *op,
	SlapReply *rs )
{
	/* If this is a simple Bind, stash the Op pointer so our chk
	 * function can find it. Set a cleanup callback to clear it
	 * out when the Bind completes.
	 */
	if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE ) {
		slap_callback *cb;
		ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup, op,
			NULL );
		cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
		cb->sc_cleanup = smbk5pwd_op_cleanup;
		cb->sc_next = op->o_callback;
		op->o_callback = cb;
	}
	return SLAP_CB_CONTINUE;
}

247
248
249
static LUTIL_PASSWD_CHK_FUNC k5key_chk;
static LUTIL_PASSWD_HASH_FUNC k5key_hash;
static const struct berval k5key_scheme = BER_BVC("{K5KEY}");
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

/* This password scheme stores no data in the userPassword attribute
 * other than the scheme name. It assumes the invoking entry is a
 * krb5KDCentry and compares the passed-in credentials against the
 * krb5Key attribute. The krb5Key may be multi-valued, but they are
 * simply multiple keytypes generated from the same input string, so
 * only the first value needs to be compared here.
 *
 * Since the lutil_passwd API doesn't pass the Entry object in, we
 * have to fetch it ourselves in order to get access to the other
 * attributes. We accomplish this with the help of the overlay's Bind
 * function, which stores the current Operation pointer in thread-specific
 * storage so we can retrieve it here. The Operation provides all
 * the necessary context for us to get Entry from the database.
 */
265
static int k5key_chk(
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
	const struct berval *sc,
	const struct berval *passwd,
	const struct berval *cred,
	const char **text )
{
	void *ctx;
	Operation *op;
	int rc;
	Entry *e;
	Attribute *a;
    krb5_error_code ret;
    krb5_keyblock key;
    krb5_salt salt;
	hdb_entry ent;

	/* Find our thread context, find our Operation */
	ctx = ldap_pvt_thread_pool_context();

	if ( ldap_pvt_thread_pool_getkey( ctx, smbk5pwd_op_cleanup, (void **)&op, NULL ) ||
		!op )
		return LUTIL_PASSWD_ERR;

	rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
	if ( rc != LDAP_SUCCESS ) return LUTIL_PASSWD_ERR;

	rc = LUTIL_PASSWD_ERR;
	do {
		size_t l;
		Key ekey = {0};

		a = attr_find( e->e_attrs, ad_krb5PrincipalName );
		if (!a ) break;

		memset( &ent, 0, sizeof(ent) );
		ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
		if ( ret ) break;
		krb5_get_pw_salt( context, ent.principal, &salt );
		krb5_free_principal( context, ent.principal );

		a = attr_find( e->e_attrs, ad_krb5Key );
		if ( !a ) break;

		ent.keys.len = 1;
		ent.keys.val = &ekey;
		decode_Key((unsigned char *) a->a_vals[0].bv_val,
			(size_t) a->a_vals[0].bv_len, &ent.keys.val[0], &l);
312
		if ( db->HDB_MASTER_KEY_SET )
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
			hdb_unseal_keys( context, db, &ent );

		krb5_string_to_key_salt( context, ekey.key.keytype, cred->bv_val,
			salt, &key );

		krb5_free_salt( context, salt );

		if ( memcmp( ekey.key.keyvalue.data, key.keyvalue.data,
			key.keyvalue.length ) == 0 ) rc = LUTIL_PASSWD_OK;

		krb5_free_keyblock_contents( context, &key );
		krb5_free_keyblock_contents( context, &ekey.key );

	} while(0);
	be_entry_release_r( op, e );
	return rc;
}
330
331
332
333
334
335
336
337
338
339

static int k5key_hash(
	const struct berval *scheme,
	const struct berval *passwd,
	struct berval *hash,
	const char **text )
{
	ber_dupbv( hash, (struct berval *)&k5key_scheme );
	return LUTIL_PASSWD_OK;
}
340
341
342
#endif /* DO_KRB5 */

static int smbk5pwd_exop_passwd(
343
344
345
	Operation *op,
	SlapReply *rs )
{
346
	int rc;
347
348
349
350
	req_pwdexop_s *qpw = &op->oq_pwdexop;
	Entry *e;
	Modifications *ml;
	slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
351
	smbk5pwd_t *pi = on->on_bi.bi_private;
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367

	/* Not the operation we expected, pass it on... */
	if ( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) ) {
		return SLAP_CB_CONTINUE;
	}

	op->o_bd->bd_info = (BackendInfo *)on->on_info;
	rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
	if ( rc != LDAP_SUCCESS ) return rc;

#ifdef DO_KRB5
	/* Kerberos stuff */
	do {
		krb5_error_code ret;
		hdb_entry ent;
		struct berval *keys;
368
369
		int kvno, i;
		Attribute *a;
370

371
372
		if ( !SMBK5PWD_DO_KRB5( pi ) ) break;

373
374
375
376
377
378
379
380
381
382
		if ( !is_entry_objectclass(e, oc_krb5KDCEntry, 0 ) ) break;

		a = attr_find( e->e_attrs, ad_krb5PrincipalName );
		if ( !a ) break;

		memset( &ent, 0, sizeof(ent) );
		ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
		if ( ret ) break;

		a = attr_find( e->e_attrs, ad_krb5KeyVersionNumber );
383
		kvno = 0;
384
		if ( a ) {
385
386
387
			if ( lutil_atoi( &kvno, a->a_vals[0].bv_val ) != 0 ) {
				Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
					"dn=\"%s\" unable to parse krb5KeyVersionNumber=\"%s\"\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
388
					op->o_log_prefix, e->e_name.bv_val, a->a_vals[0].bv_val );
389
390
			}

391
392
		} else {
			/* shouldn't happen, this is a required attr */
393
394
			Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
				"dn=\"%s\" missing krb5KeyVersionNumber\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
395
				op->o_log_prefix, e->e_name.bv_val, 0 );
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
		}

		ret = _kadm5_set_keys(kadm_context, &ent, qpw->rs_new.bv_val);
		hdb_seal_keys(context, db, &ent);
		krb5_free_principal( context, ent.principal );

		keys = ch_malloc( (ent.keys.len + 1) * sizeof(struct berval));

		for (i = 0; i < ent.keys.len; i++) {
			unsigned char *buf;
			size_t len;

			ASN1_MALLOC_ENCODE(Key, buf, len, &ent.keys.val[i], &len, ret);
			if (ret != 0)
				break;
			
412
			keys[i].bv_val = (char *)buf;
413
414
			keys[i].bv_len = len;
		}
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
415
		BER_BVZERO( &keys[i] );
416

417
418
		_kadm5_free_keys(kadm_context, ent.keys.len, ent.keys.val);

419
420
421
422
423
424
425
426
427
428
429
430
		if ( i != ent.keys.len ) {
			ber_bvarray_free( keys );
			break;
		}

		ml = ch_malloc(sizeof(Modifications));
		if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
		ml->sml_next = qpw->rs_mods;
		qpw->rs_mods = ml;

		ml->sml_desc = ad_krb5Key;
		ml->sml_op = LDAP_MOD_REPLACE;
431
432
433
#ifdef SLAP_MOD_INTERNAL
		ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
434
435
436
437
438
439
440
441
442
		ml->sml_values = keys;
		ml->sml_nvalues = NULL;
		
		ml = ch_malloc(sizeof(Modifications));
		ml->sml_next = qpw->rs_mods;
		qpw->rs_mods = ml;
		
		ml->sml_desc = ad_krb5KeyVersionNumber;
		ml->sml_op = LDAP_MOD_REPLACE;
443
444
445
#ifdef SLAP_MOD_INTERNAL
		ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
446
447
448
449
		ml->sml_values = ch_malloc( 2 * sizeof(struct berval));
		ml->sml_values[0].bv_val = ch_malloc( 64 );
		ml->sml_values[0].bv_len = sprintf(ml->sml_values[0].bv_val,
			"%d", kvno+1 );
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
450
		BER_BVZERO( &ml->sml_values[1] );
451
		ml->sml_nvalues = NULL;
452
	} while ( 0 );
453
454
455
456
#endif /* DO_KRB5 */

#ifdef DO_SAMBA
	/* Samba stuff */
457
	if ( SMBK5PWD_DO_SAMBA( pi ) && is_entry_objectclass(e, oc_sambaSamAccount, 0 ) ) {
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
		struct berval *keys;
		ber_len_t j,l;
		wchar_t *wcs, wc;
		char *c, *d;
		struct berval pwd;
		
		/* Expand incoming UTF8 string to UCS4 */
		l = ldap_utf8_chars(qpw->rs_new.bv_val);
		wcs = ch_malloc((l+1) * sizeof(wchar_t));

		ldap_x_utf8s_to_wcs( wcs, qpw->rs_new.bv_val, l );
		
		/* Truncate UCS4 to UCS2 */
		c = (char *)wcs;
		for (j=0; j<l; j++) {
			wc = wcs[j];
			*c++ = wc & 0xff;
			*c++ = (wc >> 8) & 0xff;
		}
		*c++ = 0;
		pwd.bv_val = (char *)wcs;
		pwd.bv_len = l * 2;

		ml = ch_malloc(sizeof(Modifications));
		if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
		ml->sml_next = qpw->rs_mods;
		qpw->rs_mods = ml;

		keys = ch_malloc( 2 * sizeof(struct berval) );
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
487
		BER_BVZERO( &keys[1] );
488
489
490
491
		nthash( &pwd, keys );
		
		ml->sml_desc = ad_sambaNTPassword;
		ml->sml_op = LDAP_MOD_REPLACE;
492
493
494
#ifdef SLAP_MOD_INTERNAL
		ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
495
496
497
498
499
500
501
502
503
504
		ml->sml_values = keys;
		ml->sml_nvalues = NULL;

		/* Truncate UCS2 to 8-bit ASCII */
		c = pwd.bv_val+1;
		d = pwd.bv_val+2;
		for (j=1; j<l; j++) {
			*c++ = *d++;
			d++;
		}
Howard Chu's avatar
Howard Chu committed
505
506
		pwd.bv_len /= 2;
		pwd.bv_val[pwd.bv_len] = '\0';
507
508
509
510
511
512

		ml = ch_malloc(sizeof(Modifications));
		ml->sml_next = qpw->rs_mods;
		qpw->rs_mods = ml;

		keys = ch_malloc( 2 * sizeof(struct berval) );
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
513
		BER_BVZERO( &keys[1] );
514
515
516
517
		lmhash( &pwd, keys );
		
		ml->sml_desc = ad_sambaLMPassword;
		ml->sml_op = LDAP_MOD_REPLACE;
518
519
520
#ifdef SLAP_MOD_INTERNAL
		ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
521
522
523
524
525
526
527
528
529
530
		ml->sml_values = keys;
		ml->sml_nvalues = NULL;

		ch_free(wcs);

		ml = ch_malloc(sizeof(Modifications));
		ml->sml_next = qpw->rs_mods;
		qpw->rs_mods = ml;

		keys = ch_malloc( 2 * sizeof(struct berval) );
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
531
532
533
534
535
		keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
		keys[0].bv_len = snprintf(keys[0].bv_val,
			STRLENOF( "9223372036854775807L" ) + 1,
			"%ld", slap_get_time());
		BER_BVZERO( &keys[1] );
536
537
538
		
		ml->sml_desc = ad_sambaPwdLastSet;
		ml->sml_op = LDAP_MOD_REPLACE;
539
540
541
#ifdef SLAP_MOD_INTERNAL
		ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
542
543
		ml->sml_values = keys;
		ml->sml_nvalues = NULL;
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

		if (pi->smb_must_change)
		{
			ml = ch_malloc(sizeof(Modifications));
			ml->sml_next = qpw->rs_mods;
			qpw->rs_mods = ml;

			keys = ch_malloc( 2 * sizeof(struct berval) );
			keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
			keys[0].bv_len = snprintf(keys[0].bv_val,
					STRLENOF( "9223372036854775807L" ) + 1,
					"%ld", slap_get_time() + pi->smb_must_change);
			BER_BVZERO( &keys[1] );

			ml->sml_desc = ad_sambaPwdMustChange;
			ml->sml_op = LDAP_MOD_REPLACE;
#ifdef SLAP_MOD_INTERNAL
			ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
			ml->sml_values = keys;
			ml->sml_nvalues = NULL;
		}
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

                if (pi->smb_can_change)
                {
                        ml = ch_malloc(sizeof(Modifications));
                        ml->sml_next = qpw->rs_mods;
                        qpw->rs_mods = ml;

                        keys = ch_malloc( 2 * sizeof(struct berval) );
                        keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
                        keys[0].bv_len = snprintf(keys[0].bv_val,
                                        STRLENOF( "9223372036854775807L" ) + 1,
                                        "%ld", slap_get_time() + pi->smb_can_change);
                        BER_BVZERO( &keys[1] );

                        ml->sml_desc = ad_sambaPwdCanChange;
                        ml->sml_op = LDAP_MOD_REPLACE;
#ifdef SLAP_MOD_INTERNAL
                        ml->sml_flags = SLAP_MOD_INTERNAL;
#endif
                        ml->sml_values = keys;
                        ml->sml_nvalues = NULL;
                }
588
589
590
591
592
593
594
595
596
	}
#endif /* DO_SAMBA */
	be_entry_release_r( op, e );

	return SLAP_CB_CONTINUE;
}

static slap_overinst smbk5pwd;

597
598
599
/* back-config stuff */
enum {
	PC_SMB_MUST_CHANGE = 1,
600
	PC_SMB_CAN_CHANGE,
601
602
	PC_SMB_ENABLE
};
603

604
605
606
static ConfigDriver smbk5pwd_cf_func;

/*
607
 * NOTE: uses OID arcs OLcfgCtAt:1 and OLcfgCtOc:1
608
609
610
611
612
 */

static ConfigTable smbk5pwd_cfats[] = {
	{ "smbk5pwd-enable", "arg",
		2, 0, 0, ARG_MAGIC|PC_SMB_ENABLE, smbk5pwd_cf_func,
613
		"( OLcfgCtAt:1.1 NAME 'olcSmbK5PwdEnable' "
614
615
616
617
		"DESC 'Modules to be enabled' "
		"SYNTAX OMsDirectoryString )", NULL, NULL },
	{ "smbk5pwd-must-change", "time",
		2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_MUST_CHANGE, smbk5pwd_cf_func,
618
		"( OLcfgCtAt:1.2 NAME 'olcSmbK5PwdMustChange' "
619
620
		"DESC 'Credentials validity interval' "
		"SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
621
622
        { "smbk5pwd-can-change", "time",
                2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_CAN_CHANGE, smbk5pwd_cf_func,
623
                "( OLcfgCtAt:1.3 NAME 'olcSmbK5PwdCanChange' "
624
625
                "DESC 'Credentials minimum validity interval' "
                "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
626
627
628
629
630

	{ NULL, NULL, 0, 0, 0, ARG_IGNORED }
};

static ConfigOCs smbk5pwd_cfocs[] = {
631
	{ "( OLcfgCtOc:1.1 "
632
633
634
635
636
637
		"NAME 'olcSmbK5PwdConfig' "
		"DESC 'smbk5pwd overlay configuration' "
		"SUP olcOverlayConfig "
		"MAY ( "
			"olcSmbK5PwdEnable "
			"$ olcSmbK5PwdMustChange "
638
			"$ olcSmbK5PwdCanChange "
639
640
641
642
643
644
645
		") )", Cft_Overlay, smbk5pwd_cfats },

	{ NULL, 0, NULL }
};

/*
 * add here other functionalities; handle their initialization
646
 * as appropriate in smbk5pwd_modules_init().
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
 */
static slap_verbmasks smbk5pwd_modules[] = {
	{ BER_BVC( "krb5" ),		SMBK5PWD_F_KRB5	},
	{ BER_BVC( "samba" ),		SMBK5PWD_F_SAMBA },
	{ BER_BVNULL,			-1 }
};

static int
smbk5pwd_cf_func( ConfigArgs *c )
{
	slap_overinst	*on = (slap_overinst *)c->bi;

	int		rc = 0;
	smbk5pwd_t	*pi = on->on_bi.bi_private;

	if ( c->op == SLAP_CONFIG_EMIT ) {
		switch( c->type ) {
		case PC_SMB_MUST_CHANGE:
#ifdef DO_SAMBA
			c->value_int = pi->smb_must_change;
#else /* ! DO_SAMBA */
			c->value_int = 0;
#endif /* ! DO_SAMBA */
			break;

672
673
674
675
676
677
678
679
                case PC_SMB_CAN_CHANGE:
#ifdef DO_SAMBA
                        c->value_int = pi->smb_can_change;
#else /* ! DO_SAMBA */
                        c->value_int = 0;
#endif /* ! DO_SAMBA */
                        break;

680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
		case PC_SMB_ENABLE:
			c->rvalue_vals = NULL;
			if ( pi->mode ) {
				mask_to_verbs( smbk5pwd_modules, pi->mode, &c->rvalue_vals );
				if ( c->rvalue_vals == NULL ) {
					rc = 1;
				}
			}
			break;

		default:
			assert( 0 );
			rc = 1;
		}
		return rc;

	} else if ( c->op == LDAP_MOD_DELETE ) {
		switch( c->type ) {
		case PC_SMB_MUST_CHANGE:
			break;

701
702
703
                case PC_SMB_CAN_CHANGE:
                        break;

704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
		case PC_SMB_ENABLE:
			if ( !c->line ) {
				pi->mode = 0;

			} else {
				slap_mask_t	m;

				m = verb_to_mask( c->line, smbk5pwd_modules );
				pi->mode &= ~m;
			}
			break;

		default:
			assert( 0 );
			rc = 1;
		}
		return rc;
	}

	switch( c->type ) {
	case PC_SMB_MUST_CHANGE:
#ifdef DO_SAMBA
Pierangelo Masarati's avatar
Pierangelo Masarati committed
726
727
728
729
730
731
		if ( c->value_int < 0 ) {
			Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
				"<%s> invalid negative value \"%d\".",
				c->log, c->argv[ 0 ], 0 );
			return 1;
		}
732
733
734
735
736
737
738
739
740
741
		pi->smb_must_change = c->value_int;
#else /* ! DO_SAMBA */
		Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
			"<%s> only meaningful "
			"when compiled with -DDO_SAMBA.\n",
			c->log, c->argv[ 0 ], 0 );
		return 1;
#endif /* ! DO_SAMBA */
		break;

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
        case PC_SMB_CAN_CHANGE:
#ifdef DO_SAMBA
                if ( c->value_int < 0 ) {
                        Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
                                "<%s> invalid negative value \"%d\".",
                                c->log, c->argv[ 0 ], 0 );
                        return 1;
                }
                pi->smb_can_change = c->value_int;
#else /* ! DO_SAMBA */
                Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
                        "<%s> only meaningful "
                        "when compiled with -DDO_SAMBA.\n",
                        c->log, c->argv[ 0 ], 0 );
                return 1;
#endif /* ! DO_SAMBA */
                break;

760
	case PC_SMB_ENABLE: {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
761
		slap_mask_t	mode = pi->mode, m;
762
763

		rc = verbs_to_mask( c->argc, c->argv, smbk5pwd_modules, &m );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
764
		if ( rc > 0 ) {
765
766
767
768
769
			Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
				"<%s> unknown module \"%s\".\n",
				c->log, c->argv[ 0 ], c->argv[ rc ] );
			return 1;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
770
771
772
773

		/* we can hijack the smbk5pwd_t structure because
		 * from within the configuration, this is the only
		 * active thread. */
774
775
776
777
778
779
		pi->mode |= m;

#ifndef DO_KRB5
		if ( SMBK5PWD_DO_KRB5( pi ) ) {
			Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
				"<%s> module \"%s\" only allowed when compiled with -DDO_KRB5.\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
780
781
				c->log, c->argv[ 0 ], c->argv[ rc ] );
			pi->mode = mode;
782
783
784
785
786
787
788
789
			return 1;
		}
#endif /* ! DO_KRB5 */

#ifndef DO_SAMBA
		if ( SMBK5PWD_DO_SAMBA( pi ) ) {
			Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
				"<%s> module \"%s\" only allowed when compiled with -DDO_SAMBA.\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
790
791
				c->log, c->argv[ 0 ], c->argv[ rc ] );
			pi->mode = mode;
792
793
794
795
796
797
798
			return 1;
		}
#endif /* ! DO_SAMBA */

		{
			BackendDB	db = *c->be;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
799
800
			/* Re-initialize the module, because
			 * the configuration might have changed */
801
802
803
			db.bd_info = (BackendInfo *)on;
			rc = smbk5pwd_modules_init( pi );
			if ( rc ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
804
				pi->mode = mode;
805
806
807
808
809
810
811
812
813
				return 1;
			}
		}

		} break;

	default:
		assert( 0 );
		return 1;
814
	}
815
816
817
818
819
820
	return rc;
}

static int
smbk5pwd_modules_init( smbk5pwd_t *pi )
{
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
	static struct {
		const char		*name;
		AttributeDescription	**adp;
	}
#ifdef DO_KRB5
	krb5_ad[] = {
		{ "krb5Key",			&ad_krb5Key },
		{ "krb5KeyVersionNumber",	&ad_krb5KeyVersionNumber },
		{ "krb5PrincipalName",		&ad_krb5PrincipalName },
		{ NULL }
	},
#endif /* DO_KRB5 */
#ifdef DO_SAMBA
	samba_ad[] = {
		{ "sambaLMPassword",		&ad_sambaLMPassword },
		{ "sambaNTPassword",		&ad_sambaNTPassword },
		{ "sambaPwdLastSet",		&ad_sambaPwdLastSet },
		{ "sambaPwdMustChange",		&ad_sambaPwdMustChange },
839
                { "sambaPwdCanChange",          &ad_sambaPwdCanChange },
840
841
842
843
844
845
846
		{ NULL }
	},
#endif /* DO_SAMBA */
	dummy_ad;

	/* this is to silence the unused var warning */
	dummy_ad.name = NULL;
847
848
849
850
851
852

#ifdef DO_KRB5
	if ( SMBK5PWD_DO_KRB5( pi ) && oc_krb5KDCEntry == NULL ) {
		krb5_error_code	ret;
		extern HDB 	*_kadm5_s_get_db(void *);

853
854
		int		i, rc;

855
		/* Make sure all of our necessary schema items are loaded */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
856
		oc_krb5KDCEntry = oc_find( "krb5KDCEntry" );
857
858
859
860
		if ( !oc_krb5KDCEntry ) {
			Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
				"unable to find \"krb5KDCEntry\" objectClass.\n",
				0, 0, 0 );
861
			return -1;
862
863
		}

864
865
		for ( i = 0; krb5_ad[ i ].name != NULL; i++ ) {
			const char	*text;
866

867
			*(krb5_ad[ i ].adp) = NULL;
868

869
870
871
872
873
874
875
876
			rc = slap_str2ad( krb5_ad[ i ].name, krb5_ad[ i ].adp, &text );
			if ( rc != LDAP_SUCCESS ) {
				Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
					"unable to find \"%s\" attributeType: %s (%d).\n",
					krb5_ad[ i ].name, text, rc );
				oc_krb5KDCEntry = NULL;
				return rc;
			}
877
878
879
880
881
882
		}

		/* Initialize Kerberos context */
		ret = krb5_init_context(&context);
		if (ret) {
			Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
Howard Chu's avatar
Howard Chu committed
883
884
				"unable to initialize krb5 context (%d).\n",
				ret, 0, 0 );
885
886
			oc_krb5KDCEntry = NULL;
			return -1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
887
		}
888

889
890
891
892
893
		ret = kadm5_s_init_with_password_ctx( context,
			KADM5_ADMIN_SERVICE,
			NULL,
			KADM5_ADMIN_SERVICE,
			&conf, 0, 0, &kadm_context );
Howard Chu's avatar
Howard Chu committed
894
895
896
897
898
899
900
901
902
903
904
905
906
907
		if (ret) {
			char *err_str, *err_msg = "<unknown error>";
			err_str = krb5_get_error_string( context );
			if (!err_str)
				err_msg = krb5_get_err_text( context, ret );
			Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
				"unable to initialize krb5 admin context: %s (%d).\n",
				err_str ? err_str : err_msg, ret, 0 );
			if (err_str)
				krb5_free_error_string( context, err_str );
			krb5_free_context( context );
			oc_krb5KDCEntry = NULL;
			return -1;
		}
908

909
		db = _kadm5_s_get_db( kadm_context );
910
	}
911
912
913
#endif /* DO_KRB5 */

#ifdef DO_SAMBA
914
	if ( SMBK5PWD_DO_SAMBA( pi ) && oc_sambaSamAccount == NULL ) {
915
916
		int		i, rc;

917
918
919
920
921
		oc_sambaSamAccount = oc_find( "sambaSamAccount" );
		if ( !oc_sambaSamAccount ) {
			Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
				"unable to find \"sambaSamAccount\" objectClass.\n",
				0, 0, 0 );
922
			return -1;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
923
924
		}

925
926
		for ( i = 0; samba_ad[ i ].name != NULL; i++ ) {
			const char	*text;
927

928
			*(samba_ad[ i ].adp) = NULL;
929

930
931
932
933
934
935
936
937
			rc = slap_str2ad( samba_ad[ i ].name, samba_ad[ i ].adp, &text );
			if ( rc != LDAP_SUCCESS ) {
				Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
					"unable to find \"%s\" attributeType: %s (%d).\n",
					samba_ad[ i ].name, text, rc );
				oc_sambaSamAccount = NULL;
				return rc;
			}
938
939
		}
	}
940
941
#endif /* DO_SAMBA */

942
943
944
945
	return 0;
}

static int
Ralf Haferkamp's avatar
Ralf Haferkamp committed
946
smbk5pwd_db_init(BackendDB *be, ConfigReply *cr)
947
948
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
Pierangelo Masarati's avatar
cleanup    
Pierangelo Masarati committed
949
	smbk5pwd_t	*pi;
950
951
952
953
954
955
956
957
958
959
960

	pi = ch_calloc( 1, sizeof( smbk5pwd_t ) );
	if ( pi == NULL ) {
		return 1;
	}
	on->on_bi.bi_private = (void *)pi;

	return 0;
}

static int
Ralf Haferkamp's avatar
Ralf Haferkamp committed
961
smbk5pwd_db_open(BackendDB *be, ConfigReply *cr)
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	smbk5pwd_t	*pi = (smbk5pwd_t *)on->on_bi.bi_private;

	int	rc;

	if ( pi->mode == 0 ) {
		pi->mode = SMBK5PWD_F_ALL;
	}

	rc = smbk5pwd_modules_init( pi );
	if ( rc ) {
		return rc;
	}

	return 0;
}

static int
Ralf Haferkamp's avatar
Ralf Haferkamp committed
981
smbk5pwd_db_destroy(BackendDB *be, ConfigReply *cr)
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	smbk5pwd_t	*pi = (smbk5pwd_t *)on->on_bi.bi_private;

	if ( pi ) {
		ch_free( pi );
	}

	return 0;
}

int
smbk5pwd_initialize(void)
{
	int		rc;

998
999
	smbk5pwd.on_bi.bi_type = "smbk5pwd";

1000
	smbk5pwd.on_bi.bi_db_init = smbk5pwd_db_init;