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