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