]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/smbk5pwd/smbk5pwd.c
fc881db5522d2e81c318e8d213c7140035e49b73
[openldap] / contrib / slapd-modules / smbk5pwd / smbk5pwd.c
1 /* smbk5pwd.c - Overlay for managing Samba and Heimdal passwords */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 2004-2005 by Howard Chu, Symas Corp.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15
16 #include <portable.h>
17
18 #ifndef SLAPD_OVER_SMBK5PWD
19 #define SLAPD_OVER_SMBK5PWD SLAPD_MOD_DYNAMIC
20 #endif
21
22 #ifdef SLAPD_OVER_SMBK5PWD
23
24 #include <slap.h>
25 #include <ac/errno.h>
26 #include <ac/string.h>
27
28 #include "config.h"
29
30 #ifdef DO_KRB5
31 #include <lber.h>
32 #include <lber_pvt.h>
33 #include <lutil.h>
34
35 /* make ASN1_MALLOC_ENCODE use our allocator */
36 #define malloc  ch_malloc
37
38 #include <krb5.h>
39 #include <kadm5/admin.h>
40 #include <hdb.h>
41
42 #ifndef HDB_INTERFACE_VERSION
43 #define HDB_MASTER_KEY_SET      master_key_set
44 #else
45 #define HDB_MASTER_KEY_SET      hdb_master_key_set
46 #endif
47
48 static krb5_context context;
49 static void *kadm_context;
50 static kadm5_config_params conf;
51 static HDB *db;
52
53 static AttributeDescription *ad_krb5Key;
54 static AttributeDescription *ad_krb5KeyVersionNumber;
55 static AttributeDescription *ad_krb5PrincipalName;
56 static ObjectClass *oc_krb5KDCEntry;
57 #endif
58
59 #ifdef DO_SAMBA
60 #include <openssl/des.h>
61 #include <openssl/md4.h>
62 #include "ldap_utf8.h"
63
64 static AttributeDescription *ad_sambaLMPassword;
65 static AttributeDescription *ad_sambaNTPassword;
66 static AttributeDescription *ad_sambaPwdLastSet;
67 static AttributeDescription *ad_sambaPwdMustChange;
68 static ObjectClass *oc_sambaSamAccount;
69 #endif
70
71 /* Per-instance configuration information */
72 typedef struct smbk5pwd_t {
73         unsigned        mode;
74 #define SMBK5PWD_F_KRB5         (0x1U)
75 #define SMBK5PWD_F_SAMBA        (0x2U)
76
77 #define SMBK5PWD_DO_KRB5(pi)    ((pi)->mode & SMBK5PWD_F_KRB5)
78 #define SMBK5PWD_DO_SAMBA(pi)   ((pi)->mode & SMBK5PWD_F_SAMBA)
79
80 #ifdef DO_KRB5
81         /* nothing yet */
82 #endif
83
84 #ifdef DO_SAMBA
85         /* How many seconds before forcing a password change? */
86         time_t  smb_must_change;
87 #endif
88 } smbk5pwd_t;
89
90 static const unsigned SMBK5PWD_F_ALL    =
91         0
92 #ifdef DO_KRB5
93         | SMBK5PWD_F_KRB5
94 #endif
95 #ifdef DO_SAMBA
96         | SMBK5PWD_F_SAMBA
97 #endif
98 ;
99
100 static int smbk5pwd_modules_init( smbk5pwd_t *pi );
101
102 #ifdef DO_SAMBA
103 static const char hex[] = "0123456789abcdef";
104
105 /* From liblutil/passwd.c... */
106 static void lmPasswd_to_key(
107         const char *lmPasswd,
108         des_cblock *key)
109 {
110         const unsigned char *lpw = (const unsigned char *)lmPasswd;
111         unsigned char *k = (unsigned char *)key;
112
113         /* make room for parity bits */
114         k[0] = lpw[0];
115         k[1] = ((lpw[0]&0x01)<<7) | (lpw[1]>>1);
116         k[2] = ((lpw[1]&0x03)<<6) | (lpw[2]>>2);
117         k[3] = ((lpw[2]&0x07)<<5) | (lpw[3]>>3);
118         k[4] = ((lpw[3]&0x0F)<<4) | (lpw[4]>>4);
119         k[5] = ((lpw[4]&0x1F)<<3) | (lpw[5]>>5);
120         k[6] = ((lpw[5]&0x3F)<<2) | (lpw[6]>>6);
121         k[7] = ((lpw[6]&0x7F)<<1);
122
123         des_set_odd_parity( key );
124 }
125
126 #define MAX_PWLEN 256
127 #define HASHLEN 16
128
129 static void hexify(
130         const char in[HASHLEN],
131         struct berval *out
132 )
133 {
134         int i;
135         char *a;
136         unsigned char *b;
137
138         out->bv_val = ch_malloc(HASHLEN*2 + 1);
139         out->bv_len = HASHLEN*2;
140
141         a = out->bv_val;
142         b = (unsigned char *)in;
143         for (i=0; i<HASHLEN; i++) {
144                 *a++ = hex[*b >> 4];
145                 *a++ = hex[*b++ & 0x0f];
146         }
147         *a++ = '\0';
148 }
149
150 static void lmhash(
151         struct berval *passwd,
152         struct berval *hash
153 )
154 {
155         char UcasePassword[15];
156         des_cblock key;
157         des_key_schedule schedule;
158         des_cblock StdText = "KGS!@#$%";
159         des_cblock hbuf[2];
160
161         strncpy( UcasePassword, passwd->bv_val, 14 );
162         UcasePassword[14] = '\0';
163         ldap_pvt_str2upper( UcasePassword );
164
165         lmPasswd_to_key( UcasePassword, &key );
166         des_set_key_unchecked( &key, schedule );
167         des_ecb_encrypt( &StdText, &hbuf[0], schedule , DES_ENCRYPT );
168
169         lmPasswd_to_key( &UcasePassword[7], &key );
170         des_set_key_unchecked( &key, schedule );
171         des_ecb_encrypt( &StdText, &hbuf[1], schedule , DES_ENCRYPT );
172
173         hexify( (char *)hbuf, hash );
174 }
175
176 static void nthash(
177         struct berval *passwd,
178         struct berval *hash
179 )
180 {
181         /* Windows currently only allows 14 character passwords, but
182          * may support up to 256 in the future. We assume this means
183          * 256 UCS2 characters, not 256 bytes...
184          */
185         char hbuf[HASHLEN];
186         MD4_CTX ctx;
187
188         if (passwd->bv_len > MAX_PWLEN*2)
189                 passwd->bv_len = MAX_PWLEN*2;
190                 
191         MD4_Init( &ctx );
192         MD4_Update( &ctx, passwd->bv_val, passwd->bv_len );
193         MD4_Final( (unsigned char *)hbuf, &ctx );
194
195         hexify( hbuf, hash );
196 }
197 #endif /* DO_SAMBA */
198
199 #ifdef DO_KRB5
200
201 static int smbk5pwd_op_cleanup(
202         Operation *op,
203         SlapReply *rs )
204 {
205         slap_callback *cb;
206
207         /* clear out the current key */
208         ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup,
209                 NULL, NULL );
210
211         /* free the callback */
212         cb = op->o_callback;
213         op->o_callback = cb->sc_next;
214         op->o_tmpfree( cb, op->o_tmpmemctx );
215         return 0;
216 }
217
218 static int smbk5pwd_op_bind(
219         Operation *op,
220         SlapReply *rs )
221 {
222         /* If this is a simple Bind, stash the Op pointer so our chk
223          * function can find it. Set a cleanup callback to clear it
224          * out when the Bind completes.
225          */
226         if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE ) {
227                 slap_callback *cb;
228                 ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup, op,
229                         NULL );
230                 cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
231                 cb->sc_cleanup = smbk5pwd_op_cleanup;
232                 cb->sc_next = op->o_callback;
233                 op->o_callback = cb;
234         }
235         return SLAP_CB_CONTINUE;
236 }
237
238 static LUTIL_PASSWD_CHK_FUNC k5key_chk;
239 static LUTIL_PASSWD_HASH_FUNC k5key_hash;
240 static const struct berval k5key_scheme = BER_BVC("{K5KEY}");
241
242 /* This password scheme stores no data in the userPassword attribute
243  * other than the scheme name. It assumes the invoking entry is a
244  * krb5KDCentry and compares the passed-in credentials against the
245  * krb5Key attribute. The krb5Key may be multi-valued, but they are
246  * simply multiple keytypes generated from the same input string, so
247  * only the first value needs to be compared here.
248  *
249  * Since the lutil_passwd API doesn't pass the Entry object in, we
250  * have to fetch it ourselves in order to get access to the other
251  * attributes. We accomplish this with the help of the overlay's Bind
252  * function, which stores the current Operation pointer in thread-specific
253  * storage so we can retrieve it here. The Operation provides all
254  * the necessary context for us to get Entry from the database.
255  */
256 static int k5key_chk(
257         const struct berval *sc,
258         const struct berval *passwd,
259         const struct berval *cred,
260         const char **text )
261 {
262         void *ctx;
263         Operation *op;
264         int rc;
265         Entry *e;
266         Attribute *a;
267     krb5_error_code ret;
268     krb5_keyblock key;
269     krb5_salt salt;
270         hdb_entry ent;
271
272         /* Find our thread context, find our Operation */
273         ctx = ldap_pvt_thread_pool_context();
274
275         if ( ldap_pvt_thread_pool_getkey( ctx, smbk5pwd_op_cleanup, (void **)&op, NULL ) ||
276                 !op )
277                 return LUTIL_PASSWD_ERR;
278
279         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
280         if ( rc != LDAP_SUCCESS ) return LUTIL_PASSWD_ERR;
281
282         rc = LUTIL_PASSWD_ERR;
283         do {
284                 size_t l;
285                 Key ekey = {0};
286
287                 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
288                 if (!a ) break;
289
290                 memset( &ent, 0, sizeof(ent) );
291                 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
292                 if ( ret ) break;
293                 krb5_get_pw_salt( context, ent.principal, &salt );
294                 krb5_free_principal( context, ent.principal );
295
296                 a = attr_find( e->e_attrs, ad_krb5Key );
297                 if ( !a ) break;
298
299                 ent.keys.len = 1;
300                 ent.keys.val = &ekey;
301                 decode_Key((unsigned char *) a->a_vals[0].bv_val,
302                         (size_t) a->a_vals[0].bv_len, &ent.keys.val[0], &l);
303                 if ( db->HDB_MASTER_KEY_SET )
304                         hdb_unseal_keys( context, db, &ent );
305
306                 krb5_string_to_key_salt( context, ekey.key.keytype, cred->bv_val,
307                         salt, &key );
308
309                 krb5_free_salt( context, salt );
310
311                 if ( memcmp( ekey.key.keyvalue.data, key.keyvalue.data,
312                         key.keyvalue.length ) == 0 ) rc = LUTIL_PASSWD_OK;
313
314                 krb5_free_keyblock_contents( context, &key );
315                 krb5_free_keyblock_contents( context, &ekey.key );
316
317         } while(0);
318         be_entry_release_r( op, e );
319         return rc;
320 }
321
322 static int k5key_hash(
323         const struct berval *scheme,
324         const struct berval *passwd,
325         struct berval *hash,
326         const char **text )
327 {
328         ber_dupbv( hash, (struct berval *)&k5key_scheme );
329         return LUTIL_PASSWD_OK;
330 }
331 #endif /* DO_KRB5 */
332
333 static int smbk5pwd_exop_passwd(
334         Operation *op,
335         SlapReply *rs )
336 {
337         int rc;
338         req_pwdexop_s *qpw = &op->oq_pwdexop;
339         Entry *e;
340         Modifications *ml;
341         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
342         smbk5pwd_t *pi = on->on_bi.bi_private;
343
344         /* Not the operation we expected, pass it on... */
345         if ( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) ) {
346                 return SLAP_CB_CONTINUE;
347         }
348
349         op->o_bd->bd_info = (BackendInfo *)on->on_info;
350         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
351         if ( rc != LDAP_SUCCESS ) return rc;
352
353 #ifdef DO_KRB5
354         /* Kerberos stuff */
355         do {
356                 krb5_error_code ret;
357                 hdb_entry ent;
358                 struct berval *keys;
359                 int kvno, i;
360                 Attribute *a;
361
362                 if ( !SMBK5PWD_DO_KRB5( pi ) ) break;
363
364                 if ( !is_entry_objectclass(e, oc_krb5KDCEntry, 0 ) ) break;
365
366                 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
367                 if ( !a ) break;
368
369                 memset( &ent, 0, sizeof(ent) );
370                 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
371                 if ( ret ) break;
372
373                 a = attr_find( e->e_attrs, ad_krb5KeyVersionNumber );
374                 if ( a ) {
375                         kvno = atoi(a->a_vals[0].bv_val);
376                 } else {
377                         /* shouldn't happen, this is a required attr */
378                         kvno = 0;
379                 }
380
381                 ret = _kadm5_set_keys(kadm_context, &ent, qpw->rs_new.bv_val);
382                 hdb_seal_keys(context, db, &ent);
383                 krb5_free_principal( context, ent.principal );
384
385                 keys = ch_malloc( (ent.keys.len + 1) * sizeof(struct berval));
386
387                 for (i = 0; i < ent.keys.len; i++) {
388                         unsigned char *buf;
389                         size_t len;
390
391                         ASN1_MALLOC_ENCODE(Key, buf, len, &ent.keys.val[i], &len, ret);
392                         if (ret != 0)
393                                 break;
394                         
395                         keys[i].bv_val = (char *)buf;
396                         keys[i].bv_len = len;
397                 }
398                 BER_BVZERO( &keys[i] );
399
400                 _kadm5_free_keys(kadm_context, ent.keys.len, ent.keys.val);
401
402                 if ( i != ent.keys.len ) {
403                         ber_bvarray_free( keys );
404                         break;
405                 }
406
407                 ml = ch_malloc(sizeof(Modifications));
408                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
409                 ml->sml_next = qpw->rs_mods;
410                 qpw->rs_mods = ml;
411
412                 ml->sml_desc = ad_krb5Key;
413                 ml->sml_op = LDAP_MOD_REPLACE;
414 #ifdef SLAP_MOD_INTERNAL
415                 ml->sml_flags = SLAP_MOD_INTERNAL;
416 #endif
417                 ml->sml_values = keys;
418                 ml->sml_nvalues = NULL;
419                 
420                 ml = ch_malloc(sizeof(Modifications));
421                 ml->sml_next = qpw->rs_mods;
422                 qpw->rs_mods = ml;
423                 
424                 ml->sml_desc = ad_krb5KeyVersionNumber;
425                 ml->sml_op = LDAP_MOD_REPLACE;
426 #ifdef SLAP_MOD_INTERNAL
427                 ml->sml_flags = SLAP_MOD_INTERNAL;
428 #endif
429                 ml->sml_values = ch_malloc( 2 * sizeof(struct berval));
430                 ml->sml_values[0].bv_val = ch_malloc( 64 );
431                 ml->sml_values[0].bv_len = sprintf(ml->sml_values[0].bv_val,
432                         "%d", kvno+1 );
433                 BER_BVZERO( &ml->sml_values[1] );
434                 ml->sml_nvalues = NULL;
435         } while ( 0 );
436 #endif /* DO_KRB5 */
437
438 #ifdef DO_SAMBA
439         /* Samba stuff */
440         if ( SMBK5PWD_DO_SAMBA( pi ) && is_entry_objectclass(e, oc_sambaSamAccount, 0 ) ) {
441                 struct berval *keys;
442                 ber_len_t j,l;
443                 wchar_t *wcs, wc;
444                 char *c, *d;
445                 struct berval pwd;
446                 
447                 /* Expand incoming UTF8 string to UCS4 */
448                 l = ldap_utf8_chars(qpw->rs_new.bv_val);
449                 wcs = ch_malloc((l+1) * sizeof(wchar_t));
450
451                 ldap_x_utf8s_to_wcs( wcs, qpw->rs_new.bv_val, l );
452                 
453                 /* Truncate UCS4 to UCS2 */
454                 c = (char *)wcs;
455                 for (j=0; j<l; j++) {
456                         wc = wcs[j];
457                         *c++ = wc & 0xff;
458                         *c++ = (wc >> 8) & 0xff;
459                 }
460                 *c++ = 0;
461                 pwd.bv_val = (char *)wcs;
462                 pwd.bv_len = l * 2;
463
464                 ml = ch_malloc(sizeof(Modifications));
465                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
466                 ml->sml_next = qpw->rs_mods;
467                 qpw->rs_mods = ml;
468
469                 keys = ch_malloc( 2 * sizeof(struct berval) );
470                 BER_BVZERO( &keys[1] );
471                 nthash( &pwd, keys );
472                 
473                 ml->sml_desc = ad_sambaNTPassword;
474                 ml->sml_op = LDAP_MOD_REPLACE;
475 #ifdef SLAP_MOD_INTERNAL
476                 ml->sml_flags = SLAP_MOD_INTERNAL;
477 #endif
478                 ml->sml_values = keys;
479                 ml->sml_nvalues = NULL;
480
481                 /* Truncate UCS2 to 8-bit ASCII */
482                 c = pwd.bv_val+1;
483                 d = pwd.bv_val+2;
484                 for (j=1; j<l; j++) {
485                         *c++ = *d++;
486                         d++;
487                 }
488                 pwd.bv_len /= 2;
489                 pwd.bv_val[pwd.bv_len] = '\0';
490
491                 ml = ch_malloc(sizeof(Modifications));
492                 ml->sml_next = qpw->rs_mods;
493                 qpw->rs_mods = ml;
494
495                 keys = ch_malloc( 2 * sizeof(struct berval) );
496                 BER_BVZERO( &keys[1] );
497                 lmhash( &pwd, keys );
498                 
499                 ml->sml_desc = ad_sambaLMPassword;
500                 ml->sml_op = LDAP_MOD_REPLACE;
501 #ifdef SLAP_MOD_INTERNAL
502                 ml->sml_flags = SLAP_MOD_INTERNAL;
503 #endif
504                 ml->sml_values = keys;
505                 ml->sml_nvalues = NULL;
506
507                 ch_free(wcs);
508
509                 ml = ch_malloc(sizeof(Modifications));
510                 ml->sml_next = qpw->rs_mods;
511                 qpw->rs_mods = ml;
512
513                 keys = ch_malloc( 2 * sizeof(struct berval) );
514                 keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
515                 keys[0].bv_len = snprintf(keys[0].bv_val,
516                         STRLENOF( "9223372036854775807L" ) + 1,
517                         "%ld", slap_get_time());
518                 BER_BVZERO( &keys[1] );
519                 
520                 ml->sml_desc = ad_sambaPwdLastSet;
521                 ml->sml_op = LDAP_MOD_REPLACE;
522 #ifdef SLAP_MOD_INTERNAL
523                 ml->sml_flags = SLAP_MOD_INTERNAL;
524 #endif
525                 ml->sml_values = keys;
526                 ml->sml_nvalues = NULL;
527
528                 if (pi->smb_must_change)
529                 {
530                         ml = ch_malloc(sizeof(Modifications));
531                         ml->sml_next = qpw->rs_mods;
532                         qpw->rs_mods = ml;
533
534                         keys = ch_malloc( 2 * sizeof(struct berval) );
535                         keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
536                         keys[0].bv_len = snprintf(keys[0].bv_val,
537                                         STRLENOF( "9223372036854775807L" ) + 1,
538                                         "%ld", slap_get_time() + pi->smb_must_change);
539                         BER_BVZERO( &keys[1] );
540
541                         ml->sml_desc = ad_sambaPwdMustChange;
542                         ml->sml_op = LDAP_MOD_REPLACE;
543 #ifdef SLAP_MOD_INTERNAL
544                         ml->sml_flags = SLAP_MOD_INTERNAL;
545 #endif
546                         ml->sml_values = keys;
547                         ml->sml_nvalues = NULL;
548                 }
549         }
550 #endif /* DO_SAMBA */
551         be_entry_release_r( op, e );
552
553         return SLAP_CB_CONTINUE;
554 }
555
556 static slap_overinst smbk5pwd;
557
558 /* back-config stuff */
559 enum {
560         PC_SMB_MUST_CHANGE = 1,
561         PC_SMB_ENABLE
562 };
563
564 static ConfigDriver smbk5pwd_cf_func;
565
566 /*
567  * NOTE: uses OID arcs OLcfgOvAt:6 and OLcfgOvOc:6
568  */
569
570 static ConfigTable smbk5pwd_cfats[] = {
571         { "smbk5pwd-enable", "arg",
572                 2, 0, 0, ARG_MAGIC|PC_SMB_ENABLE, smbk5pwd_cf_func,
573                 "( OLcfgOvAt:6.1 NAME 'olcSmbK5PwdEnable' "
574                 "DESC 'Modules to be enabled' "
575                 "SYNTAX OMsDirectoryString )", NULL, NULL },
576         { "smbk5pwd-must-change", "time",
577                 2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_MUST_CHANGE, smbk5pwd_cf_func,
578                 "( OLcfgOvAt:6.2 NAME 'olcSmbK5PwdMustChange' "
579                 "DESC 'Credentials validity interval' "
580                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
581
582         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
583 };
584
585 static ConfigOCs smbk5pwd_cfocs[] = {
586         { "( OLcfgOvOc:6.1 "
587                 "NAME 'olcSmbK5PwdConfig' "
588                 "DESC 'smbk5pwd overlay configuration' "
589                 "SUP olcOverlayConfig "
590                 "MAY ( "
591                         "olcSmbK5PwdEnable "
592                         "$ olcSmbK5PwdMustChange "
593                 ") )", Cft_Overlay, smbk5pwd_cfats },
594
595         { NULL, 0, NULL }
596 };
597
598 /*
599  * add here other functionalities; handle their initialization
600  * as appropriate in smbk5pwd_modules_init().
601  */
602 static slap_verbmasks smbk5pwd_modules[] = {
603         { BER_BVC( "krb5" ),            SMBK5PWD_F_KRB5 },
604         { BER_BVC( "samba" ),           SMBK5PWD_F_SAMBA },
605         { BER_BVNULL,                   -1 }
606 };
607
608 static int
609 smbk5pwd_cf_func( ConfigArgs *c )
610 {
611         slap_overinst   *on = (slap_overinst *)c->bi;
612
613         int             rc = 0;
614         smbk5pwd_t      *pi = on->on_bi.bi_private;
615
616         if ( c->op == SLAP_CONFIG_EMIT ) {
617                 switch( c->type ) {
618                 case PC_SMB_MUST_CHANGE:
619 #ifdef DO_SAMBA
620                         c->value_int = pi->smb_must_change;
621 #else /* ! DO_SAMBA */
622                         c->value_int = 0;
623 #endif /* ! DO_SAMBA */
624                         break;
625
626                 case PC_SMB_ENABLE:
627                         c->rvalue_vals = NULL;
628                         if ( pi->mode ) {
629                                 mask_to_verbs( smbk5pwd_modules, pi->mode, &c->rvalue_vals );
630                                 if ( c->rvalue_vals == NULL ) {
631                                         rc = 1;
632                                 }
633                         }
634                         break;
635
636                 default:
637                         assert( 0 );
638                         rc = 1;
639                 }
640                 return rc;
641
642         } else if ( c->op == LDAP_MOD_DELETE ) {
643                 switch( c->type ) {
644                 case PC_SMB_MUST_CHANGE:
645                         break;
646
647                 case PC_SMB_ENABLE:
648                         if ( !c->line ) {
649                                 pi->mode = 0;
650
651                         } else {
652                                 slap_mask_t     m;
653
654                                 m = verb_to_mask( c->line, smbk5pwd_modules );
655                                 pi->mode &= ~m;
656                         }
657                         break;
658
659                 default:
660                         assert( 0 );
661                         rc = 1;
662                 }
663                 return rc;
664         }
665
666         switch( c->type ) {
667         case PC_SMB_MUST_CHANGE:
668 #ifdef DO_SAMBA
669                 pi->smb_must_change = c->value_int;
670 #else /* ! DO_SAMBA */
671                 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
672                         "<%s> only meaningful "
673                         "when compiled with -DDO_SAMBA.\n",
674                         c->log, c->argv[ 0 ], 0 );
675                 return 1;
676 #endif /* ! DO_SAMBA */
677                 break;
678
679         case PC_SMB_ENABLE: {
680                 slap_mask_t     m;
681
682                 rc = verbs_to_mask( c->argc, c->argv, smbk5pwd_modules, &m );
683                 if ( rc ) {
684                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
685                                 "<%s> unknown module \"%s\".\n",
686                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
687                         return 1;
688                 }
689                 pi->mode |= m;
690
691 #ifndef DO_KRB5
692                 if ( SMBK5PWD_DO_KRB5( pi ) ) {
693                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
694                                 "<%s> module \"%s\" only allowed when compiled with -DDO_KRB5.\n",
695                                 c->log, c->argv[ 0 ], c->argv[ i ] );
696                         return 1;
697                 }
698 #endif /* ! DO_KRB5 */
699
700 #ifndef DO_SAMBA
701                 if ( SMBK5PWD_DO_SAMBA( pi ) ) {
702                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
703                                 "<%s> module \"%s\" only allowed when compiled with -DDO_SAMBA.\n",
704                                 c->log, c->argv[ 0 ], c->argv[ i ] );
705                         return 1;
706                 }
707 #endif /* ! DO_SAMBA */
708
709                 {
710                         BackendDB       db = *c->be;
711
712                         db.bd_info = (BackendInfo *)on;
713                         rc = smbk5pwd_modules_init( pi );
714                         if ( rc ) {
715                                 return 1;
716                         }
717                 }
718
719                 } break;
720
721         default:
722                 assert( 0 );
723                 return 1;
724         }
725         return rc;
726 }
727
728 static int
729 smbk5pwd_modules_init( smbk5pwd_t *pi )
730 {
731         int             rc;
732         const char      *text;
733
734 #ifdef DO_KRB5
735         if ( SMBK5PWD_DO_KRB5( pi ) && oc_krb5KDCEntry == NULL ) {
736                 krb5_error_code ret;
737                 extern HDB      *_kadm5_s_get_db(void *);
738
739                 /* Make sure all of our necessary schema items are loaded */
740                 oc_krb5KDCEntry = oc_find("krb5KDCEntry");
741                 if ( !oc_krb5KDCEntry ) {
742                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
743                                 "unable to find \"krb5KDCEntry\" objectClass.\n",
744                                 0, 0, 0 );
745                         rc = -1;
746                         goto cleanup_krb5;
747                 }
748
749                 rc = slap_str2ad( "krb5Key", &ad_krb5Key, &text );
750                 if ( rc != LDAP_SUCCESS ) {
751                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
752                                 "unable to find \"krb5Key\" attributeType: %s (%d).\n",
753                                 text, rc, 0 );
754                         goto cleanup_krb5;
755                 }
756
757                 rc = slap_str2ad( "krb5KeyVersionNumber", &ad_krb5KeyVersionNumber, &text );
758                 if ( rc != LDAP_SUCCESS ) {
759                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
760                                 "unable to find \"krb5KeyVersionNumber\" attributeType: %s (%d).\n",
761                                 text, rc, 0 );
762                         goto cleanup_krb5;
763                 }
764
765                 rc = slap_str2ad( "krb5PrincipalName", &ad_krb5PrincipalName, &text );
766                 if ( rc != LDAP_SUCCESS ) {
767                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
768                                 "unable to find \"krb5PrincipalName\" attributeType: %s (%d).\n",
769                                 text, rc, 0 );
770                         goto cleanup_krb5;
771                 }
772
773                 /* Initialize Kerberos context */
774                 ret = krb5_init_context(&context);
775                 if (ret) {
776                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
777                                 "unable to initialize krb5 context.\n",
778                                 0, 0, 0 );
779                         rc = -1;
780                         goto cleanup_krb5;
781                 }
782
783                 ret = kadm5_s_init_with_password_ctx( context,
784                         KADM5_ADMIN_SERVICE,
785                         NULL,
786                         KADM5_ADMIN_SERVICE,
787                         &conf, 0, 0, &kadm_context );
788         
789                 db = _kadm5_s_get_db(kadm_context);
790
791                 if ( 0 ) {
792 cleanup_krb5:;
793                         oc_krb5KDCEntry = NULL;
794
795                         return rc;
796                 }
797         }
798 #endif /* DO_KRB5 */
799
800 #ifdef DO_SAMBA
801         if ( SMBK5PWD_DO_SAMBA( pi ) && oc_sambaSamAccount == NULL ) {
802                 oc_sambaSamAccount = oc_find( "sambaSamAccount" );
803                 if ( !oc_sambaSamAccount ) {
804                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
805                                 "unable to find \"sambaSamAccount\" objectClass.\n",
806                                 0, 0, 0 );
807                         rc = -1;
808                         goto cleanup_samba;
809                 }
810
811                 rc = slap_str2ad( "sambaLMPassword", &ad_sambaLMPassword, &text );
812                 if ( rc != LDAP_SUCCESS ) {
813                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
814                                 "unable to find \"sambaLMPassword\" attributeType: %s (%d).\n",
815                                 text, rc, 0 );
816                         goto cleanup_samba;
817                 }
818
819                 rc = slap_str2ad( "sambaNTPassword", &ad_sambaNTPassword, &text );
820                 if ( rc != LDAP_SUCCESS ) {
821                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
822                                 "unable to find \"sambaLMPassword\" attributeType: %s (%d).\n",
823                                 text, rc, 0 );
824                         goto cleanup_samba;
825                 }
826
827                 rc = slap_str2ad( "sambaPwdLastSet", &ad_sambaPwdLastSet, &text );
828                 if ( rc != LDAP_SUCCESS ) {
829                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
830                                 "unable to find \"sambaLMPassword\" attributeType: %s (%d).\n",
831                                 text, rc, 0 );
832                         goto cleanup_samba;
833                 }
834
835                 if ( 0 ) {
836 cleanup_samba:;
837                         oc_sambaSamAccount = NULL;
838
839                         return rc;
840                 }
841         }
842 #endif /* DO_SAMBA */
843
844         return 0;
845 }
846
847 static int
848 smbk5pwd_db_init(BackendDB *be)
849 {
850         slap_overinst   *on = (slap_overinst *)be->bd_info;
851         smbk5pwd_t *pi;
852
853         pi = ch_calloc( 1, sizeof( smbk5pwd_t ) );
854         if ( pi == NULL ) {
855                 return 1;
856         }
857         on->on_bi.bi_private = (void *)pi;
858
859         return 0;
860 }
861
862 static int
863 smbk5pwd_db_open(BackendDB *be)
864 {
865         slap_overinst   *on = (slap_overinst *)be->bd_info;
866         smbk5pwd_t      *pi = (smbk5pwd_t *)on->on_bi.bi_private;
867
868         int     rc;
869
870         if ( pi->mode == 0 ) {
871                 pi->mode = SMBK5PWD_F_ALL;
872         }
873
874         rc = smbk5pwd_modules_init( pi );
875         if ( rc ) {
876                 return rc;
877         }
878
879         return 0;
880 }
881
882 static int
883 smbk5pwd_db_destroy(BackendDB *be)
884 {
885         slap_overinst   *on = (slap_overinst *)be->bd_info;
886         smbk5pwd_t      *pi = (smbk5pwd_t *)on->on_bi.bi_private;
887
888         if ( pi ) {
889                 ch_free( pi );
890         }
891
892         return 0;
893 }
894
895 int
896 smbk5pwd_initialize(void)
897 {
898         int             rc;
899
900         smbk5pwd.on_bi.bi_type = "smbk5pwd";
901
902         smbk5pwd.on_bi.bi_db_init = smbk5pwd_db_init;
903         smbk5pwd.on_bi.bi_db_open = smbk5pwd_db_open;
904         smbk5pwd.on_bi.bi_db_destroy = smbk5pwd_db_destroy;
905
906         smbk5pwd.on_bi.bi_extended = smbk5pwd_exop_passwd;
907     
908 #ifdef DO_KRB5
909         smbk5pwd.on_bi.bi_op_bind = smbk5pwd_op_bind;
910
911         lutil_passwd_add( (struct berval *)&k5key_scheme, k5key_chk, k5key_hash );
912 #endif
913
914         smbk5pwd.on_bi.bi_cf_ocs = smbk5pwd_cfocs;
915
916         rc = config_register_schema( smbk5pwd_cfats, smbk5pwd_cfocs );
917         if ( rc ) {
918                 return rc;
919         }
920
921         return overlay_register( &smbk5pwd );
922 }
923
924 #if SLAPD_OVER_SMBK5PWD == SLAPD_MOD_DYNAMIC
925 int init_module(int argc, char *argv[]) {
926         return smbk5pwd_initialize();
927 }
928 #endif
929
930 #endif /* defined(SLAPD_OVER_SMBK5PWD) */