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