]> git.sur5r.net Git - openldap/blob - contrib/slapd-modules/smbk5pwd/smbk5pwd.c
ITS#6024 Don't send cookies without csn.
[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 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                 int kvno, i;
425                 Attribute *a;
426
427                 if ( !SMBK5PWD_DO_KRB5( pi ) ) break;
428
429                 if ( !is_entry_objectclass(e, oc_krb5KDCEntry, 0 ) ) break;
430
431                 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
432                 if ( !a ) break;
433
434                 memset( &ent, 0, sizeof(ent) );
435                 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
436                 if ( ret ) break;
437
438                 a = attr_find( e->e_attrs, ad_krb5KeyVersionNumber );
439                 kvno = 0;
440                 if ( a ) {
441                         if ( lutil_atoi( &kvno, a->a_vals[0].bv_val ) != 0 ) {
442                                 Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
443                                         "dn=\"%s\" unable to parse krb5KeyVersionNumber=\"%s\"\n",
444                                         op->o_log_prefix, e->e_name.bv_val, a->a_vals[0].bv_val );
445                         }
446
447                 } else {
448                         /* shouldn't happen, this is a required attr */
449                         Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
450                                 "dn=\"%s\" missing krb5KeyVersionNumber\n",
451                                 op->o_log_prefix, e->e_name.bv_val, 0 );
452                 }
453
454                 ret = _kadm5_set_keys(kadm_context, &ent, qpw->rs_new.bv_val);
455                 hdb_seal_keys(context, db, &ent);
456                 krb5_free_principal( context, ent.principal );
457
458                 keys = ch_malloc( (ent.keys.len + 1) * sizeof(struct berval));
459
460                 for (i = 0; i < ent.keys.len; i++) {
461                         unsigned char *buf;
462                         size_t len;
463
464                         ASN1_MALLOC_ENCODE(Key, buf, len, &ent.keys.val[i], &len, ret);
465                         if (ret != 0)
466                                 break;
467                         
468                         keys[i].bv_val = (char *)buf;
469                         keys[i].bv_len = len;
470                 }
471                 BER_BVZERO( &keys[i] );
472
473                 _kadm5_free_keys(kadm_context, ent.keys.len, ent.keys.val);
474
475                 if ( i != ent.keys.len ) {
476                         ber_bvarray_free( keys );
477                         break;
478                 }
479
480                 ml = ch_malloc(sizeof(Modifications));
481                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
482                 ml->sml_next = qpw->rs_mods;
483                 qpw->rs_mods = ml;
484
485                 ml->sml_desc = ad_krb5Key;
486                 ml->sml_op = LDAP_MOD_REPLACE;
487 #ifdef SLAP_MOD_INTERNAL
488                 ml->sml_flags = SLAP_MOD_INTERNAL;
489 #endif
490                 ml->sml_numvals = i;
491                 ml->sml_values = keys;
492                 ml->sml_nvalues = NULL;
493                 
494                 ml = ch_malloc(sizeof(Modifications));
495                 ml->sml_next = qpw->rs_mods;
496                 qpw->rs_mods = ml;
497                 
498                 ml->sml_desc = ad_krb5KeyVersionNumber;
499                 ml->sml_op = LDAP_MOD_REPLACE;
500 #ifdef SLAP_MOD_INTERNAL
501                 ml->sml_flags = SLAP_MOD_INTERNAL;
502 #endif
503                 ml->sml_numvals = 1;
504                 ml->sml_values = ch_malloc( 2 * sizeof(struct berval));
505                 ml->sml_values[0].bv_val = ch_malloc( 64 );
506                 ml->sml_values[0].bv_len = sprintf(ml->sml_values[0].bv_val,
507                         "%d", kvno+1 );
508                 BER_BVZERO( &ml->sml_values[1] );
509                 ml->sml_nvalues = NULL;
510         } while ( 0 );
511 #endif /* DO_KRB5 */
512
513 #ifdef DO_SAMBA
514         /* Samba stuff */
515         if ( SMBK5PWD_DO_SAMBA( pi ) && is_entry_objectclass(e, oc_sambaSamAccount, 0 ) ) {
516                 struct berval *keys;
517                 ber_len_t j,l;
518                 wchar_t *wcs, wc;
519                 char *c, *d;
520                 struct berval pwd;
521                 
522                 /* Expand incoming UTF8 string to UCS4 */
523                 l = ldap_utf8_chars(qpw->rs_new.bv_val);
524                 wcs = ch_malloc((l+1) * sizeof(wchar_t));
525
526                 ldap_x_utf8s_to_wcs( wcs, qpw->rs_new.bv_val, l );
527                 
528                 /* Truncate UCS4 to UCS2 */
529                 c = (char *)wcs;
530                 for (j=0; j<l; j++) {
531                         wc = wcs[j];
532                         *c++ = wc & 0xff;
533                         *c++ = (wc >> 8) & 0xff;
534                 }
535                 *c++ = 0;
536                 pwd.bv_val = (char *)wcs;
537                 pwd.bv_len = l * 2;
538
539                 ml = ch_malloc(sizeof(Modifications));
540                 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
541                 ml->sml_next = qpw->rs_mods;
542                 qpw->rs_mods = ml;
543
544                 keys = ch_malloc( 2 * sizeof(struct berval) );
545                 BER_BVZERO( &keys[1] );
546                 nthash( &pwd, keys );
547                 
548                 ml->sml_desc = ad_sambaNTPassword;
549                 ml->sml_op = LDAP_MOD_REPLACE;
550 #ifdef SLAP_MOD_INTERNAL
551                 ml->sml_flags = SLAP_MOD_INTERNAL;
552 #endif
553                 ml->sml_numvals = 1;
554                 ml->sml_values = keys;
555                 ml->sml_nvalues = NULL;
556
557                 /* Truncate UCS2 to 8-bit ASCII */
558                 c = pwd.bv_val+1;
559                 d = pwd.bv_val+2;
560                 for (j=1; j<l; j++) {
561                         *c++ = *d++;
562                         d++;
563                 }
564                 pwd.bv_len /= 2;
565                 pwd.bv_val[pwd.bv_len] = '\0';
566
567                 ml = ch_malloc(sizeof(Modifications));
568                 ml->sml_next = qpw->rs_mods;
569                 qpw->rs_mods = ml;
570
571                 keys = ch_malloc( 2 * sizeof(struct berval) );
572                 BER_BVZERO( &keys[1] );
573                 lmhash( &pwd, keys );
574                 
575                 ml->sml_desc = ad_sambaLMPassword;
576                 ml->sml_op = LDAP_MOD_REPLACE;
577 #ifdef SLAP_MOD_INTERNAL
578                 ml->sml_flags = SLAP_MOD_INTERNAL;
579 #endif
580                 ml->sml_numvals = 1;
581                 ml->sml_values = keys;
582                 ml->sml_nvalues = NULL;
583
584                 ch_free(wcs);
585
586                 ml = ch_malloc(sizeof(Modifications));
587                 ml->sml_next = qpw->rs_mods;
588                 qpw->rs_mods = ml;
589
590                 keys = ch_malloc( 2 * sizeof(struct berval) );
591                 keys[0].bv_val = ch_malloc( LDAP_PVT_INTTYPE_CHARS(long) );
592                 keys[0].bv_len = snprintf(keys[0].bv_val,
593                         LDAP_PVT_INTTYPE_CHARS(long),
594                         "%ld", slap_get_time());
595                 BER_BVZERO( &keys[1] );
596                 
597                 ml->sml_desc = ad_sambaPwdLastSet;
598                 ml->sml_op = LDAP_MOD_REPLACE;
599 #ifdef SLAP_MOD_INTERNAL
600                 ml->sml_flags = SLAP_MOD_INTERNAL;
601 #endif
602                 ml->sml_numvals = 1;
603                 ml->sml_values = keys;
604                 ml->sml_nvalues = NULL;
605
606                 if (pi->smb_must_change)
607                 {
608                         ml = ch_malloc(sizeof(Modifications));
609                         ml->sml_next = qpw->rs_mods;
610                         qpw->rs_mods = ml;
611
612                         keys = ch_malloc( 2 * sizeof(struct berval) );
613                         keys[0].bv_val = ch_malloc( LDAP_PVT_INTTYPE_CHARS(long) );
614                         keys[0].bv_len = snprintf(keys[0].bv_val,
615                                         LDAP_PVT_INTTYPE_CHARS(long),
616                                         "%ld", slap_get_time() + pi->smb_must_change);
617                         BER_BVZERO( &keys[1] );
618
619                         ml->sml_desc = ad_sambaPwdMustChange;
620                         ml->sml_op = LDAP_MOD_REPLACE;
621 #ifdef SLAP_MOD_INTERNAL
622                         ml->sml_flags = SLAP_MOD_INTERNAL;
623 #endif
624                         ml->sml_numvals = 1;
625                         ml->sml_values = keys;
626                         ml->sml_nvalues = NULL;
627                 }
628
629                 if (pi->smb_can_change)
630                 {
631                         ml = ch_malloc(sizeof(Modifications));
632                         ml->sml_next = qpw->rs_mods;
633                         qpw->rs_mods = ml;
634
635                         keys = ch_malloc( 2 * sizeof(struct berval) );
636                         keys[0].bv_val = ch_malloc( LDAP_PVT_INTTYPE_CHARS(long) );
637                         keys[0].bv_len = snprintf(keys[0].bv_val,
638                                         LDAP_PVT_INTTYPE_CHARS(long),
639                                         "%ld", slap_get_time() + pi->smb_can_change);
640                         BER_BVZERO( &keys[1] );
641
642                         ml->sml_desc = ad_sambaPwdCanChange;
643                         ml->sml_op = LDAP_MOD_REPLACE;
644 #ifdef SLAP_MOD_INTERNAL
645                         ml->sml_flags = SLAP_MOD_INTERNAL;
646 #endif
647                         ml->sml_numvals = 1;
648                         ml->sml_values = keys;
649                         ml->sml_nvalues = NULL;
650                 }
651         }
652 #endif /* DO_SAMBA */
653         be_entry_release_r( op, e );
654         qpw->rs_new.bv_val[qpw->rs_new.bv_len] = term;
655
656         return SLAP_CB_CONTINUE;
657 }
658
659 static slap_overinst smbk5pwd;
660
661 /* back-config stuff */
662 enum {
663         PC_SMB_MUST_CHANGE = 1,
664         PC_SMB_CAN_CHANGE,
665         PC_SMB_ENABLE
666 };
667
668 static ConfigDriver smbk5pwd_cf_func;
669
670 /*
671  * NOTE: uses OID arcs OLcfgCtAt:1 and OLcfgCtOc:1
672  */
673
674 static ConfigTable smbk5pwd_cfats[] = {
675         { "smbk5pwd-enable", "arg",
676                 2, 0, 0, ARG_MAGIC|PC_SMB_ENABLE, smbk5pwd_cf_func,
677                 "( OLcfgCtAt:1.1 NAME 'olcSmbK5PwdEnable' "
678                 "DESC 'Modules to be enabled' "
679                 "SYNTAX OMsDirectoryString )", NULL, NULL },
680         { "smbk5pwd-must-change", "time",
681                 2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_MUST_CHANGE, smbk5pwd_cf_func,
682                 "( OLcfgCtAt:1.2 NAME 'olcSmbK5PwdMustChange' "
683                 "DESC 'Credentials validity interval' "
684                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
685         { "smbk5pwd-can-change", "time",
686                 2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_CAN_CHANGE, smbk5pwd_cf_func,
687                 "( OLcfgCtAt:1.3 NAME 'olcSmbK5PwdCanChange' "
688                 "DESC 'Credentials minimum validity interval' "
689                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
690
691         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
692 };
693
694 static ConfigOCs smbk5pwd_cfocs[] = {
695         { "( OLcfgCtOc:1.1 "
696                 "NAME 'olcSmbK5PwdConfig' "
697                 "DESC 'smbk5pwd overlay configuration' "
698                 "SUP olcOverlayConfig "
699                 "MAY ( "
700                         "olcSmbK5PwdEnable "
701                         "$ olcSmbK5PwdMustChange "
702                         "$ olcSmbK5PwdCanChange "
703                 ") )", Cft_Overlay, smbk5pwd_cfats },
704
705         { NULL, 0, NULL }
706 };
707
708 /*
709  * add here other functionalities; handle their initialization
710  * as appropriate in smbk5pwd_modules_init().
711  */
712 static slap_verbmasks smbk5pwd_modules[] = {
713         { BER_BVC( "krb5" ),            SMBK5PWD_F_KRB5 },
714         { BER_BVC( "samba" ),           SMBK5PWD_F_SAMBA },
715         { BER_BVNULL,                   -1 }
716 };
717
718 static int
719 smbk5pwd_cf_func( ConfigArgs *c )
720 {
721         slap_overinst   *on = (slap_overinst *)c->bi;
722
723         int             rc = 0;
724         smbk5pwd_t      *pi = on->on_bi.bi_private;
725
726         if ( c->op == SLAP_CONFIG_EMIT ) {
727                 switch( c->type ) {
728                 case PC_SMB_MUST_CHANGE:
729 #ifdef DO_SAMBA
730                         c->value_int = pi->smb_must_change;
731 #else /* ! DO_SAMBA */
732                         c->value_int = 0;
733 #endif /* ! DO_SAMBA */
734                         break;
735
736                 case PC_SMB_CAN_CHANGE:
737 #ifdef DO_SAMBA
738                         c->value_int = pi->smb_can_change;
739 #else /* ! DO_SAMBA */
740                         c->value_int = 0;
741 #endif /* ! DO_SAMBA */
742                         break;
743
744                 case PC_SMB_ENABLE:
745                         c->rvalue_vals = NULL;
746                         if ( pi->mode ) {
747                                 mask_to_verbs( smbk5pwd_modules, pi->mode, &c->rvalue_vals );
748                                 if ( c->rvalue_vals == NULL ) {
749                                         rc = 1;
750                                 }
751                         }
752                         break;
753
754                 default:
755                         assert( 0 );
756                         rc = 1;
757                 }
758                 return rc;
759
760         } else if ( c->op == LDAP_MOD_DELETE ) {
761                 switch( c->type ) {
762                 case PC_SMB_MUST_CHANGE:
763                         break;
764
765                 case PC_SMB_CAN_CHANGE:
766                         break;
767
768                 case PC_SMB_ENABLE:
769                         if ( !c->line ) {
770                                 pi->mode = 0;
771
772                         } else {
773                                 slap_mask_t     m;
774
775                                 m = verb_to_mask( c->line, smbk5pwd_modules );
776                                 pi->mode &= ~m;
777                         }
778                         break;
779
780                 default:
781                         assert( 0 );
782                         rc = 1;
783                 }
784                 return rc;
785         }
786
787         switch( c->type ) {
788         case PC_SMB_MUST_CHANGE:
789 #ifdef DO_SAMBA
790                 if ( c->value_int < 0 ) {
791                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
792                                 "<%s> invalid negative value \"%d\".",
793                                 c->log, c->argv[ 0 ], 0 );
794                         return 1;
795                 }
796                 pi->smb_must_change = c->value_int;
797 #else /* ! DO_SAMBA */
798                 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
799                         "<%s> only meaningful "
800                         "when compiled with -DDO_SAMBA.\n",
801                         c->log, c->argv[ 0 ], 0 );
802                 return 1;
803 #endif /* ! DO_SAMBA */
804                 break;
805
806         case PC_SMB_CAN_CHANGE:
807 #ifdef DO_SAMBA
808                 if ( c->value_int < 0 ) {
809                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
810                                 "<%s> invalid negative value \"%d\".",
811                                 c->log, c->argv[ 0 ], 0 );
812                         return 1;
813                 }
814                 pi->smb_can_change = c->value_int;
815 #else /* ! DO_SAMBA */
816                 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
817                         "<%s> only meaningful "
818                         "when compiled with -DDO_SAMBA.\n",
819                         c->log, c->argv[ 0 ], 0 );
820                 return 1;
821 #endif /* ! DO_SAMBA */
822                 break;
823
824         case PC_SMB_ENABLE: {
825                 slap_mask_t     mode = pi->mode, m;
826
827                 rc = verbs_to_mask( c->argc, c->argv, smbk5pwd_modules, &m );
828                 if ( rc > 0 ) {
829                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
830                                 "<%s> unknown module \"%s\".\n",
831                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
832                         return 1;
833                 }
834
835                 /* we can hijack the smbk5pwd_t structure because
836                  * from within the configuration, this is the only
837                  * active thread. */
838                 pi->mode |= m;
839
840 #ifndef DO_KRB5
841                 if ( SMBK5PWD_DO_KRB5( pi ) ) {
842                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
843                                 "<%s> module \"%s\" only allowed when compiled with -DDO_KRB5.\n",
844                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
845                         pi->mode = mode;
846                         return 1;
847                 }
848 #endif /* ! DO_KRB5 */
849
850 #ifndef DO_SAMBA
851                 if ( SMBK5PWD_DO_SAMBA( pi ) ) {
852                         Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
853                                 "<%s> module \"%s\" only allowed when compiled with -DDO_SAMBA.\n",
854                                 c->log, c->argv[ 0 ], c->argv[ rc ] );
855                         pi->mode = mode;
856                         return 1;
857                 }
858 #endif /* ! DO_SAMBA */
859
860                 {
861                         BackendDB       db = *c->be;
862
863                         /* Re-initialize the module, because
864                          * the configuration might have changed */
865                         db.bd_info = (BackendInfo *)on;
866                         rc = smbk5pwd_modules_init( pi );
867                         if ( rc ) {
868                                 pi->mode = mode;
869                                 return 1;
870                         }
871                 }
872
873                 } break;
874
875         default:
876                 assert( 0 );
877                 return 1;
878         }
879         return rc;
880 }
881
882 static int
883 smbk5pwd_modules_init( smbk5pwd_t *pi )
884 {
885         static struct {
886                 const char              *name;
887                 AttributeDescription    **adp;
888         }
889 #ifdef DO_KRB5
890         krb5_ad[] = {
891                 { "krb5Key",                    &ad_krb5Key },
892                 { "krb5KeyVersionNumber",       &ad_krb5KeyVersionNumber },
893                 { "krb5PrincipalName",          &ad_krb5PrincipalName },
894                 { "krb5ValidEnd",               &ad_krb5ValidEnd },
895                 { NULL }
896         },
897 #endif /* DO_KRB5 */
898 #ifdef DO_SAMBA
899         samba_ad[] = {
900                 { "sambaLMPassword",            &ad_sambaLMPassword },
901                 { "sambaNTPassword",            &ad_sambaNTPassword },
902                 { "sambaPwdLastSet",            &ad_sambaPwdLastSet },
903                 { "sambaPwdMustChange",         &ad_sambaPwdMustChange },
904                 { "sambaPwdCanChange",          &ad_sambaPwdCanChange },
905                 { NULL }
906         },
907 #endif /* DO_SAMBA */
908         dummy_ad;
909
910         /* this is to silence the unused var warning */
911         dummy_ad.name = NULL;
912
913 #ifdef DO_KRB5
914         if ( SMBK5PWD_DO_KRB5( pi ) && oc_krb5KDCEntry == NULL ) {
915                 krb5_error_code ret;
916                 extern HDB      *_kadm5_s_get_db(void *);
917
918                 int             i, rc;
919
920                 /* Make sure all of our necessary schema items are loaded */
921                 oc_krb5KDCEntry = oc_find( "krb5KDCEntry" );
922                 if ( !oc_krb5KDCEntry ) {
923                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
924                                 "unable to find \"krb5KDCEntry\" objectClass.\n",
925                                 0, 0, 0 );
926                         return -1;
927                 }
928
929                 for ( i = 0; krb5_ad[ i ].name != NULL; i++ ) {
930                         const char      *text;
931
932                         *(krb5_ad[ i ].adp) = NULL;
933
934                         rc = slap_str2ad( krb5_ad[ i ].name, krb5_ad[ i ].adp, &text );
935                         if ( rc != LDAP_SUCCESS ) {
936                                 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
937                                         "unable to find \"%s\" attributeType: %s (%d).\n",
938                                         krb5_ad[ i ].name, text, rc );
939                                 oc_krb5KDCEntry = NULL;
940                                 return rc;
941                         }
942                 }
943
944                 /* Initialize Kerberos context */
945                 ret = krb5_init_context(&context);
946                 if (ret) {
947                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
948                                 "unable to initialize krb5 context (%d).\n",
949                                 ret, 0, 0 );
950                         oc_krb5KDCEntry = NULL;
951                         return -1;
952                 }
953
954                 ret = kadm5_s_init_with_password_ctx( context,
955                         KADM5_ADMIN_SERVICE,
956                         NULL,
957                         KADM5_ADMIN_SERVICE,
958                         &conf, 0, 0, &kadm_context );
959                 if (ret) {
960                         char *err_str, *err_msg = "<unknown error>";
961                         err_str = krb5_get_error_string( context );
962                         if (!err_str)
963                                 err_msg = (char *)krb5_get_err_text( context, ret );
964                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
965                                 "unable to initialize krb5 admin context: %s (%d).\n",
966                                 err_str ? err_str : err_msg, ret, 0 );
967                         if (err_str)
968                                 krb5_free_error_string( context, err_str );
969                         krb5_free_context( context );
970                         oc_krb5KDCEntry = NULL;
971                         return -1;
972                 }
973
974                 db = _kadm5_s_get_db( kadm_context );
975         }
976 #endif /* DO_KRB5 */
977
978 #ifdef DO_SAMBA
979         if ( SMBK5PWD_DO_SAMBA( pi ) && oc_sambaSamAccount == NULL ) {
980                 int             i, rc;
981
982                 oc_sambaSamAccount = oc_find( "sambaSamAccount" );
983                 if ( !oc_sambaSamAccount ) {
984                         Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
985                                 "unable to find \"sambaSamAccount\" objectClass.\n",
986                                 0, 0, 0 );
987                         return -1;
988                 }
989
990                 for ( i = 0; samba_ad[ i ].name != NULL; i++ ) {
991                         const char      *text;
992
993                         *(samba_ad[ i ].adp) = NULL;
994
995                         rc = slap_str2ad( samba_ad[ i ].name, samba_ad[ i ].adp, &text );
996                         if ( rc != LDAP_SUCCESS ) {
997                                 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
998                                         "unable to find \"%s\" attributeType: %s (%d).\n",
999                                         samba_ad[ i ].name, text, rc );
1000                                 oc_sambaSamAccount = NULL;
1001                                 return rc;
1002                         }
1003                 }
1004         }
1005 #endif /* DO_SAMBA */
1006
1007         return 0;
1008 }
1009
1010 static int
1011 smbk5pwd_db_init(BackendDB *be, ConfigReply *cr)
1012 {
1013         slap_overinst   *on = (slap_overinst *)be->bd_info;
1014         smbk5pwd_t      *pi;
1015
1016         pi = ch_calloc( 1, sizeof( smbk5pwd_t ) );
1017         if ( pi == NULL ) {
1018                 return 1;
1019         }
1020         on->on_bi.bi_private = (void *)pi;
1021
1022         return 0;
1023 }
1024
1025 static int
1026 smbk5pwd_db_open(BackendDB *be, ConfigReply *cr)
1027 {
1028         slap_overinst   *on = (slap_overinst *)be->bd_info;
1029         smbk5pwd_t      *pi = (smbk5pwd_t *)on->on_bi.bi_private;
1030
1031         int     rc;
1032
1033         if ( pi->mode == 0 ) {
1034                 pi->mode = SMBK5PWD_F_ALL;
1035         }
1036
1037         rc = smbk5pwd_modules_init( pi );
1038         if ( rc ) {
1039                 return rc;
1040         }
1041
1042         return 0;
1043 }
1044
1045 static int
1046 smbk5pwd_db_destroy(BackendDB *be, ConfigReply *cr)
1047 {
1048         slap_overinst   *on = (slap_overinst *)be->bd_info;
1049         smbk5pwd_t      *pi = (smbk5pwd_t *)on->on_bi.bi_private;
1050
1051         if ( pi ) {
1052                 ch_free( pi );
1053         }
1054
1055         return 0;
1056 }
1057
1058 int
1059 smbk5pwd_initialize(void)
1060 {
1061         int             rc;
1062
1063         smbk5pwd.on_bi.bi_type = "smbk5pwd";
1064
1065         smbk5pwd.on_bi.bi_db_init = smbk5pwd_db_init;
1066         smbk5pwd.on_bi.bi_db_open = smbk5pwd_db_open;
1067         smbk5pwd.on_bi.bi_db_destroy = smbk5pwd_db_destroy;
1068
1069         smbk5pwd.on_bi.bi_extended = smbk5pwd_exop_passwd;
1070     
1071 #ifdef DO_KRB5
1072         smbk5pwd.on_bi.bi_op_bind = smbk5pwd_op_bind;
1073
1074         lutil_passwd_add( (struct berval *)&k5key_scheme, k5key_chk, k5key_hash );
1075 #endif
1076
1077         smbk5pwd.on_bi.bi_cf_ocs = smbk5pwd_cfocs;
1078
1079         rc = config_register_schema( smbk5pwd_cfats, smbk5pwd_cfocs );
1080         if ( rc ) {
1081                 return rc;
1082         }
1083
1084         return overlay_register( &smbk5pwd );
1085 }
1086
1087 #if SLAPD_OVER_SMBK5PWD == SLAPD_MOD_DYNAMIC
1088 int init_module(int argc, char *argv[]) {
1089         return smbk5pwd_initialize();
1090 }
1091 #endif
1092
1093 #endif /* defined(SLAPD_OVER_SMBK5PWD) */