]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/smbk5pwd/smbk5pwd.c
b96d4856c4e27cb6c424bc25060f27617965377c
[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                 kvno = 0;
381                 if ( a ) {
382                         if ( lutil_atoi( &kvno, a->a_vals[0].bv_val ) != 0 ) {
383                                 Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
384                                         "dn=\"%s\" unable to parse krb5KeyVersionNumber=\"%s\"\n",
385                                         op->o_log_prefix, e->e_name.bv_val, a->a_vals[0].bv_val );
386                         }
387
388                 } else {
389                         /* shouldn't happen, this is a required attr */
390                         Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
391                                 "dn=\"%s\" missing krb5KeyVersionNumber\n",
392                                 op->o_log_prefix, e->e_name.bv_val, 0 );
393                 }
394
395                 ret = _kadm5_set_keys(kadm_context, &ent, qpw->rs_new.bv_val);
396                 hdb_seal_keys(context, db, &ent);
397                 krb5_free_principal( context, ent.principal );
398
399                 keys = ch_malloc( (ent.keys.len + 1) * sizeof(struct berval));
400
401                 for (i = 0; i < ent.keys.len; i++) {
402                         unsigned char *buf;
403                         size_t len;
404
405                         ASN1_MALLOC_ENCODE(Key, buf, len, &ent.keys.val[i], &len, ret);
406                         if (ret != 0)
407                                 break;
408                         
409                         keys[i].bv_val = (char *)buf;
410                         keys[i].bv_len = len;
411                 }
412                 BER_BVZERO( &keys[i] );
413
414                 _kadm5_free_keys(kadm_context, ent.keys.len, ent.keys.val);
415
416                 if ( i != ent.keys.len ) {
417                         ber_bvarray_free( keys );
418                         break;
419                 }
420
421                 ml = ch_malloc(sizeof(Modifications));
422                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
423                 ml->sml_next = qpw->rs_mods;
424                 qpw->rs_mods = ml;
425
426                 ml->sml_desc = ad_krb5Key;
427                 ml->sml_op = LDAP_MOD_REPLACE;
428 #ifdef SLAP_MOD_INTERNAL
429                 ml->sml_flags = SLAP_MOD_INTERNAL;
430 #endif
431                 ml->sml_values = keys;
432                 ml->sml_nvalues = NULL;
433                 
434                 ml = ch_malloc(sizeof(Modifications));
435                 ml->sml_next = qpw->rs_mods;
436                 qpw->rs_mods = ml;
437                 
438                 ml->sml_desc = ad_krb5KeyVersionNumber;
439                 ml->sml_op = LDAP_MOD_REPLACE;
440 #ifdef SLAP_MOD_INTERNAL
441                 ml->sml_flags = SLAP_MOD_INTERNAL;
442 #endif
443                 ml->sml_values = ch_malloc( 2 * sizeof(struct berval));
444                 ml->sml_values[0].bv_val = ch_malloc( 64 );
445                 ml->sml_values[0].bv_len = sprintf(ml->sml_values[0].bv_val,
446                         "%d", kvno+1 );
447                 BER_BVZERO( &ml->sml_values[1] );
448                 ml->sml_nvalues = NULL;
449         } while ( 0 );
450 #endif /* DO_KRB5 */
451
452 #ifdef DO_SAMBA
453         /* Samba stuff */
454         if ( SMBK5PWD_DO_SAMBA( pi ) && is_entry_objectclass(e, oc_sambaSamAccount, 0 ) ) {
455                 struct berval *keys;
456                 ber_len_t j,l;
457                 wchar_t *wcs, wc;
458                 char *c, *d;
459                 struct berval pwd;
460                 
461                 /* Expand incoming UTF8 string to UCS4 */
462                 l = ldap_utf8_chars(qpw->rs_new.bv_val);
463                 wcs = ch_malloc((l+1) * sizeof(wchar_t));
464
465                 ldap_x_utf8s_to_wcs( wcs, qpw->rs_new.bv_val, l );
466                 
467                 /* Truncate UCS4 to UCS2 */
468                 c = (char *)wcs;
469                 for (j=0; j<l; j++) {
470                         wc = wcs[j];
471                         *c++ = wc & 0xff;
472                         *c++ = (wc >> 8) & 0xff;
473                 }
474                 *c++ = 0;
475                 pwd.bv_val = (char *)wcs;
476                 pwd.bv_len = l * 2;
477
478                 ml = ch_malloc(sizeof(Modifications));
479                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
480                 ml->sml_next = qpw->rs_mods;
481                 qpw->rs_mods = ml;
482
483                 keys = ch_malloc( 2 * sizeof(struct berval) );
484                 BER_BVZERO( &keys[1] );
485                 nthash( &pwd, keys );
486                 
487                 ml->sml_desc = ad_sambaNTPassword;
488                 ml->sml_op = LDAP_MOD_REPLACE;
489 #ifdef SLAP_MOD_INTERNAL
490                 ml->sml_flags = SLAP_MOD_INTERNAL;
491 #endif
492                 ml->sml_values = keys;
493                 ml->sml_nvalues = NULL;
494
495                 /* Truncate UCS2 to 8-bit ASCII */
496                 c = pwd.bv_val+1;
497                 d = pwd.bv_val+2;
498                 for (j=1; j<l; j++) {
499                         *c++ = *d++;
500                         d++;
501                 }
502                 pwd.bv_len /= 2;
503                 pwd.bv_val[pwd.bv_len] = '\0';
504
505                 ml = ch_malloc(sizeof(Modifications));
506                 ml->sml_next = qpw->rs_mods;
507                 qpw->rs_mods = ml;
508
509                 keys = ch_malloc( 2 * sizeof(struct berval) );
510                 BER_BVZERO( &keys[1] );
511                 lmhash( &pwd, keys );
512                 
513                 ml->sml_desc = ad_sambaLMPassword;
514                 ml->sml_op = LDAP_MOD_REPLACE;
515 #ifdef SLAP_MOD_INTERNAL
516                 ml->sml_flags = SLAP_MOD_INTERNAL;
517 #endif
518                 ml->sml_values = keys;
519                 ml->sml_nvalues = NULL;
520
521                 ch_free(wcs);
522
523                 ml = ch_malloc(sizeof(Modifications));
524                 ml->sml_next = qpw->rs_mods;
525                 qpw->rs_mods = ml;
526
527                 keys = ch_malloc( 2 * sizeof(struct berval) );
528                 keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
529                 keys[0].bv_len = snprintf(keys[0].bv_val,
530                         STRLENOF( "9223372036854775807L" ) + 1,
531                         "%ld", slap_get_time());
532                 BER_BVZERO( &keys[1] );
533                 
534                 ml->sml_desc = ad_sambaPwdLastSet;
535                 ml->sml_op = LDAP_MOD_REPLACE;
536 #ifdef SLAP_MOD_INTERNAL
537                 ml->sml_flags = SLAP_MOD_INTERNAL;
538 #endif
539                 ml->sml_values = keys;
540                 ml->sml_nvalues = NULL;
541
542                 if (pi->smb_must_change)
543                 {
544                         ml = ch_malloc(sizeof(Modifications));
545                         ml->sml_next = qpw->rs_mods;
546                         qpw->rs_mods = ml;
547
548                         keys = ch_malloc( 2 * sizeof(struct berval) );
549                         keys[0].bv_val = ch_malloc( STRLENOF( "9223372036854775807L" ) + 1 );
550                         keys[0].bv_len = snprintf(keys[0].bv_val,
551                                         STRLENOF( "9223372036854775807L" ) + 1,
552                                         "%ld", slap_get_time() + pi->smb_must_change);
553                         BER_BVZERO( &keys[1] );
554
555                         ml->sml_desc = ad_sambaPwdMustChange;
556                         ml->sml_op = LDAP_MOD_REPLACE;
557 #ifdef SLAP_MOD_INTERNAL
558                         ml->sml_flags = SLAP_MOD_INTERNAL;
559 #endif
560                         ml->sml_values = keys;
561                         ml->sml_nvalues = NULL;
562                 }
563         }
564 #endif /* DO_SAMBA */
565         be_entry_release_r( op, e );
566
567         return SLAP_CB_CONTINUE;
568 }
569
570 static slap_overinst smbk5pwd;
571
572 /* back-config stuff */
573 enum {
574         PC_SMB_MUST_CHANGE = 1,
575         PC_SMB_ENABLE
576 };
577
578 static ConfigDriver smbk5pwd_cf_func;
579
580 /*
581  * NOTE: uses OID arcs OLcfgOvAt:6 and OLcfgOvOc:6
582  */
583
584 static ConfigTable smbk5pwd_cfats[] = {
585         { "smbk5pwd-enable", "arg",
586                 2, 0, 0, ARG_MAGIC|PC_SMB_ENABLE, smbk5pwd_cf_func,
587                 "( OLcfgOvAt:6.1 NAME 'olcSmbK5PwdEnable' "
588                 "DESC 'Modules to be enabled' "
589                 "SYNTAX OMsDirectoryString )", NULL, NULL },
590         { "smbk5pwd-must-change", "time",
591                 2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_MUST_CHANGE, smbk5pwd_cf_func,
592                 "( OLcfgOvAt:6.2 NAME 'olcSmbK5PwdMustChange' "
593                 "DESC 'Credentials validity interval' "
594                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
595
596         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
597 };
598
599 static ConfigOCs smbk5pwd_cfocs[] = {
600         { "( OLcfgOvOc:6.1 "
601                 "NAME 'olcSmbK5PwdConfig' "
602                 "DESC 'smbk5pwd overlay configuration' "
603                 "SUP olcOverlayConfig "
604                 "MAY ( "
605                         "olcSmbK5PwdEnable "
606                         "$ olcSmbK5PwdMustChange "
607                 ") )", Cft_Overlay, smbk5pwd_cfats },
608
609         { NULL, 0, NULL }
610 };
611
612 /*
613  * add here other functionalities; handle their initialization
614  * as appropriate in smbk5pwd_modules_init().
615  */
616 static slap_verbmasks smbk5pwd_modules[] = {
617         { BER_BVC( "krb5" ),            SMBK5PWD_F_KRB5 },
618         { BER_BVC( "samba" ),           SMBK5PWD_F_SAMBA },
619         { BER_BVNULL,                   -1 }
620 };
621
622 static int
623 smbk5pwd_cf_func( ConfigArgs *c )
624 {
625         slap_overinst   *on = (slap_overinst *)c->bi;
626
627         int             rc = 0;
628         smbk5pwd_t      *pi = on->on_bi.bi_private;
629
630         if ( c->op == SLAP_CONFIG_EMIT ) {
631                 switch( c->type ) {
632                 case PC_SMB_MUST_CHANGE:
633 #ifdef DO_SAMBA
634                         c->value_int = pi->smb_must_change;
635 #else /* ! DO_SAMBA */
636                         c->value_int = 0;
637 #endif /* ! DO_SAMBA */
638                         break;
639
640                 case PC_SMB_ENABLE:
641                         c->rvalue_vals = NULL;
642                         if ( pi->mode ) {
643                                 mask_to_verbs( smbk5pwd_modules, pi->mode, &c->rvalue_vals );
644                                 if ( c->rvalue_vals == NULL ) {
645                                         rc = 1;
646                                 }
647                         }
648                         break;
649
650                 default:
651                         assert( 0 );
652                         rc = 1;
653                 }
654                 return rc;
655
656         } else if ( c->op == LDAP_MOD_DELETE ) {
657                 switch( c->type ) {
658                 case PC_SMB_MUST_CHANGE:
659                         break;
660
661                 case PC_SMB_ENABLE:
662                         if ( !c->line ) {
663                                 pi->mode = 0;
664
665                         } else {
666                                 slap_mask_t     m;
667
668                                 m = verb_to_mask( c->line, smbk5pwd_modules );
669                                 pi->mode &= ~m;
670                         }
671                         break;
672
673                 default:
674                         assert( 0 );
675                         rc = 1;
676                 }
677                 return rc;
678         }
679
680         switch( c->type ) {
681         case PC_SMB_MUST_CHANGE:
682 #ifdef DO_SAMBA
683                 if ( c->value_int < 0 ) {
684                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
685                                 "<%s> invalid negative value \"%d\".",
686                                 c->log, c->argv[ 0 ], 0 );
687                         return 1;
688                 }
689                 pi->smb_must_change = c->value_int;
690 #else /* ! DO_SAMBA */
691                 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
692                         "<%s> only meaningful "
693                         "when compiled with -DDO_SAMBA.\n",
694                         c->log, c->argv[ 0 ], 0 );
695                 return 1;
696 #endif /* ! DO_SAMBA */
697                 break;
698
699         case PC_SMB_ENABLE: {
700                 slap_mask_t     mode = pi->mode, m;
701
702                 rc = verbs_to_mask( c->argc, c->argv, smbk5pwd_modules, &m );
703                 if ( rc > 0 ) {
704                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
705                                 "<%s> unknown module \"%s\".\n",
706                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
707                         return 1;
708                 }
709
710                 /* we can hijack the smbk5pwd_t structure because
711                  * from within the configuration, this is the only
712                  * active thread. */
713                 pi->mode |= m;
714
715 #ifndef DO_KRB5
716                 if ( SMBK5PWD_DO_KRB5( pi ) ) {
717                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
718                                 "<%s> module \"%s\" only allowed when compiled with -DDO_KRB5.\n",
719                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
720                         pi->mode = mode;
721                         return 1;
722                 }
723 #endif /* ! DO_KRB5 */
724
725 #ifndef DO_SAMBA
726                 if ( SMBK5PWD_DO_SAMBA( pi ) ) {
727                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
728                                 "<%s> module \"%s\" only allowed when compiled with -DDO_SAMBA.\n",
729                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
730                         pi->mode = mode;
731                         return 1;
732                 }
733 #endif /* ! DO_SAMBA */
734
735                 {
736                         BackendDB       db = *c->be;
737
738                         /* Re-initialize the module, because
739                          * the configuration might have changed */
740                         db.bd_info = (BackendInfo *)on;
741                         rc = smbk5pwd_modules_init( pi );
742                         if ( rc ) {
743                                 pi->mode = mode;
744                                 return 1;
745                         }
746                 }
747
748                 } break;
749
750         default:
751                 assert( 0 );
752                 return 1;
753         }
754         return rc;
755 }
756
757 static int
758 smbk5pwd_modules_init( smbk5pwd_t *pi )
759 {
760         static struct {
761                 const char              *name;
762                 AttributeDescription    **adp;
763         }
764 #ifdef DO_KRB5
765         krb5_ad[] = {
766                 { "krb5Key",                    &ad_krb5Key },
767                 { "krb5KeyVersionNumber",       &ad_krb5KeyVersionNumber },
768                 { "krb5PrincipalName",          &ad_krb5PrincipalName },
769                 { NULL }
770         },
771 #endif /* DO_KRB5 */
772 #ifdef DO_SAMBA
773         samba_ad[] = {
774                 { "sambaLMPassword",            &ad_sambaLMPassword },
775                 { "sambaNTPassword",            &ad_sambaNTPassword },
776                 { "sambaPwdLastSet",            &ad_sambaPwdLastSet },
777                 { "sambaPwdMustChange",         &ad_sambaPwdMustChange },
778                 { NULL }
779         },
780 #endif /* DO_SAMBA */
781         dummy_ad;
782
783         /* this is to silence the unused var warning */
784         dummy_ad.name = NULL;
785
786 #ifdef DO_KRB5
787         if ( SMBK5PWD_DO_KRB5( pi ) && oc_krb5KDCEntry == NULL ) {
788                 krb5_error_code ret;
789                 extern HDB      *_kadm5_s_get_db(void *);
790
791                 int             i, rc;
792
793                 /* Make sure all of our necessary schema items are loaded */
794                 oc_krb5KDCEntry = oc_find( "krb5KDCEntry" );
795                 if ( !oc_krb5KDCEntry ) {
796                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
797                                 "unable to find \"krb5KDCEntry\" objectClass.\n",
798                                 0, 0, 0 );
799                         return -1;
800                 }
801
802                 for ( i = 0; krb5_ad[ i ].name != NULL; i++ ) {
803                         const char      *text;
804
805                         *(krb5_ad[ i ].adp) = NULL;
806
807                         rc = slap_str2ad( krb5_ad[ i ].name, krb5_ad[ i ].adp, &text );
808                         if ( rc != LDAP_SUCCESS ) {
809                                 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
810                                         "unable to find \"%s\" attributeType: %s (%d).\n",
811                                         krb5_ad[ i ].name, text, rc );
812                                 oc_krb5KDCEntry = NULL;
813                                 return rc;
814                         }
815                 }
816
817                 /* Initialize Kerberos context */
818                 ret = krb5_init_context(&context);
819                 if (ret) {
820                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
821                                 "unable to initialize krb5 context.\n",
822                                 0, 0, 0 );
823                         oc_krb5KDCEntry = NULL;
824                         return -1;
825                 }
826
827                 /* FIXME: check return code? */
828                 ret = kadm5_s_init_with_password_ctx( context,
829                         KADM5_ADMIN_SERVICE,
830                         NULL,
831                         KADM5_ADMIN_SERVICE,
832                         &conf, 0, 0, &kadm_context );
833
834                 /* FIXME: check return code? */
835                 db = _kadm5_s_get_db( kadm_context );
836         }
837 #endif /* DO_KRB5 */
838
839 #ifdef DO_SAMBA
840         if ( SMBK5PWD_DO_SAMBA( pi ) && oc_sambaSamAccount == NULL ) {
841                 int             i, rc;
842
843                 oc_sambaSamAccount = oc_find( "sambaSamAccount" );
844                 if ( !oc_sambaSamAccount ) {
845                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
846                                 "unable to find \"sambaSamAccount\" objectClass.\n",
847                                 0, 0, 0 );
848                         return -1;
849                 }
850
851                 for ( i = 0; samba_ad[ i ].name != NULL; i++ ) {
852                         const char      *text;
853
854                         *(samba_ad[ i ].adp) = NULL;
855
856                         rc = slap_str2ad( samba_ad[ i ].name, samba_ad[ i ].adp, &text );
857                         if ( rc != LDAP_SUCCESS ) {
858                                 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
859                                         "unable to find \"%s\" attributeType: %s (%d).\n",
860                                         samba_ad[ i ].name, text, rc );
861                                 oc_sambaSamAccount = NULL;
862                                 return rc;
863                         }
864                 }
865         }
866 #endif /* DO_SAMBA */
867
868         return 0;
869 }
870
871 static int
872 smbk5pwd_db_init(BackendDB *be)
873 {
874         slap_overinst   *on = (slap_overinst *)be->bd_info;
875         smbk5pwd_t      *pi;
876
877         pi = ch_calloc( 1, sizeof( smbk5pwd_t ) );
878         if ( pi == NULL ) {
879                 return 1;
880         }
881         on->on_bi.bi_private = (void *)pi;
882
883         return 0;
884 }
885
886 static int
887 smbk5pwd_db_open(BackendDB *be)
888 {
889         slap_overinst   *on = (slap_overinst *)be->bd_info;
890         smbk5pwd_t      *pi = (smbk5pwd_t *)on->on_bi.bi_private;
891
892         int     rc;
893
894         if ( pi->mode == 0 ) {
895                 pi->mode = SMBK5PWD_F_ALL;
896         }
897
898         rc = smbk5pwd_modules_init( pi );
899         if ( rc ) {
900                 return rc;
901         }
902
903         return 0;
904 }
905
906 static int
907 smbk5pwd_db_destroy(BackendDB *be)
908 {
909         slap_overinst   *on = (slap_overinst *)be->bd_info;
910         smbk5pwd_t      *pi = (smbk5pwd_t *)on->on_bi.bi_private;
911
912         if ( pi ) {
913                 ch_free( pi );
914         }
915
916         return 0;
917 }
918
919 int
920 smbk5pwd_initialize(void)
921 {
922         int             rc;
923
924         smbk5pwd.on_bi.bi_type = "smbk5pwd";
925
926         smbk5pwd.on_bi.bi_db_init = smbk5pwd_db_init;
927         smbk5pwd.on_bi.bi_db_open = smbk5pwd_db_open;
928         smbk5pwd.on_bi.bi_db_destroy = smbk5pwd_db_destroy;
929
930         smbk5pwd.on_bi.bi_extended = smbk5pwd_exop_passwd;
931     
932 #ifdef DO_KRB5
933         smbk5pwd.on_bi.bi_op_bind = smbk5pwd_op_bind;
934
935         lutil_passwd_add( (struct berval *)&k5key_scheme, k5key_chk, k5key_hash );
936 #endif
937
938         smbk5pwd.on_bi.bi_cf_ocs = smbk5pwd_cfocs;
939
940         rc = config_register_schema( smbk5pwd_cfats, smbk5pwd_cfocs );
941         if ( rc ) {
942                 return rc;
943         }
944
945         return overlay_register( &smbk5pwd );
946 }
947
948 #if SLAPD_OVER_SMBK5PWD == SLAPD_MOD_DYNAMIC
949 int init_module(int argc, char *argv[]) {
950         return smbk5pwd_initialize();
951 }
952 #endif
953
954 #endif /* defined(SLAPD_OVER_SMBK5PWD) */