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