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