1 /* smbk5pwd.c - Overlay for managing Samba and Heimdal passwords */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2011 The OpenLDAP Foundation.
6 * Portions Copyright 2004-2005 by Howard Chu, Symas Corp.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * Support for table-driven configuration added by Pierangelo Masarati.
19 * Support for sambaPwdMustChange and sambaPwdCanChange added by Marco D'Ettorre.
24 #ifndef SLAPD_OVER_SMBK5PWD
25 #define SLAPD_OVER_SMBK5PWD SLAPD_MOD_DYNAMIC
28 #ifdef SLAPD_OVER_SMBK5PWD
32 #include <ac/string.h>
41 /* make ASN1_MALLOC_ENCODE use our allocator */
42 #define malloc ch_malloc
45 #include <kadm5/admin.h>
48 #ifndef HDB_INTERFACE_VERSION
49 #define HDB_MASTER_KEY_SET master_key_set
51 #define HDB_MASTER_KEY_SET hdb_master_key_set
54 static krb5_context context;
55 static void *kadm_context;
56 static kadm5_config_params conf;
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;
69 typedef unsigned char DES_cblock[8];
71 #include <openssl/des.h>
72 #include <openssl/md4.h>
74 #include "ldap_utf8.h"
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;
84 /* Per-instance configuration information */
85 typedef struct smbk5pwd_t {
87 #define SMBK5PWD_F_KRB5 (0x1U)
88 #define SMBK5PWD_F_SAMBA (0x2U)
90 #define SMBK5PWD_DO_KRB5(pi) ((pi)->mode & SMBK5PWD_F_KRB5)
91 #define SMBK5PWD_DO_SAMBA(pi) ((pi)->mode & SMBK5PWD_F_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;
105 static const unsigned SMBK5PWD_F_ALL =
115 static int smbk5pwd_modules_init( smbk5pwd_t *pi );
118 static const char hex[] = "0123456789abcdef";
120 /* From liblutil/passwd.c... */
121 static void lmPasswd_to_key(
122 const char *lmPasswd,
125 const unsigned char *lpw = (const unsigned char *)lmPasswd;
126 unsigned char *k = (unsigned char *)key;
128 /* make room for parity bits */
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);
139 des_set_odd_parity( key );
143 #define MAX_PWLEN 256
147 const char in[HASHLEN],
155 out->bv_val = ch_malloc(HASHLEN*2 + 1);
156 out->bv_len = HASHLEN*2;
159 b = (unsigned char *)in;
160 for (i=0; i<HASHLEN; i++) {
162 *a++ = hex[*b++ & 0x0f];
168 struct berval *passwd,
172 char UcasePassword[15];
174 DES_cblock StdText = "KGS!@#$%";
177 DES_key_schedule schedule;
178 #elif defined(HAVE_GNUTLS)
179 gcry_cipher_hd_t h = NULL;
182 err = gcry_cipher_open( &h, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_CBC, 0 );
186 strncpy( UcasePassword, passwd->bv_val, 14 );
187 UcasePassword[14] = '\0';
188 ldap_pvt_str2upper( UcasePassword );
190 lmPasswd_to_key( UcasePassword, &key );
192 err = gcry_cipher_setkey( h, &key, sizeof(key) );
194 err = gcry_cipher_encrypt( h, &hbuf[0], sizeof(key), &StdText, sizeof(key) );
196 gcry_cipher_reset( h );
197 lmPasswd_to_key( &UcasePassword[7], &key );
198 err = gcry_cipher_setkey( h, &key, sizeof(key) );
200 err = gcry_cipher_encrypt( h, &hbuf[1], sizeof(key), &StdText, sizeof(key) );
203 gcry_cipher_close( h );
205 #elif defined(HAVE_OPENSSL)
206 des_set_key_unchecked( &key, schedule );
207 des_ecb_encrypt( &StdText, &hbuf[0], schedule , DES_ENCRYPT );
209 lmPasswd_to_key( &UcasePassword[7], &key );
210 des_set_key_unchecked( &key, schedule );
211 des_ecb_encrypt( &StdText, &hbuf[1], schedule , DES_ENCRYPT );
214 hexify( (char *)hbuf, hash );
218 struct berval *passwd,
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...
231 if (passwd->bv_len > MAX_PWLEN*2)
232 passwd->bv_len = MAX_PWLEN*2;
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 );
242 hexify( hbuf, hash );
244 #endif /* DO_SAMBA */
248 static int smbk5pwd_op_cleanup(
254 /* clear out the current key */
255 ldap_pvt_thread_pool_setkey( op->o_threadctx, smbk5pwd_op_cleanup,
256 NULL, 0, NULL, NULL );
258 /* free the callback */
260 op->o_callback = cb->sc_next;
261 op->o_tmpfree( cb, op->o_tmpmemctx );
265 static int smbk5pwd_op_bind(
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.
273 if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE ) {
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;
282 return SLAP_CB_CONTINUE;
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}");
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.
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.
303 static int k5key_chk(
304 const struct berval *sc,
305 const struct berval *passwd,
306 const struct berval *cred,
319 /* Find our thread context, find our Operation */
320 ctx = ldap_pvt_thread_pool_context();
322 if ( ldap_pvt_thread_pool_getkey( ctx, smbk5pwd_op_cleanup, &op_tmp, NULL )
324 return LUTIL_PASSWD_ERR;
327 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
328 if ( rc != LDAP_SUCCESS ) return LUTIL_PASSWD_ERR;
330 rc = LUTIL_PASSWD_ERR;
335 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
338 memset( &ent, 0, sizeof(ent) );
339 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
342 a = attr_find( e->e_attrs, ad_krb5ValidEnd );
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;
354 krb5_get_pw_salt( context, ent.principal, &salt );
355 krb5_free_principal( context, ent.principal );
357 a = attr_find( e->e_attrs, ad_krb5Key );
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 );
367 krb5_string_to_key_salt( context, ekey.key.keytype, cred->bv_val,
370 krb5_free_salt( context, salt );
372 if ( memcmp( ekey.key.keyvalue.data, key.keyvalue.data,
373 key.keyvalue.length ) == 0 ) rc = LUTIL_PASSWD_OK;
375 krb5_free_keyblock_contents( context, &key );
376 krb5_free_keyblock_contents( context, &ekey.key );
379 be_entry_release_r( op, e );
383 static int k5key_hash(
384 const struct berval *scheme,
385 const struct berval *passwd,
389 ber_dupbv( hash, (struct berval *)&k5key_scheme );
390 return LUTIL_PASSWD_OK;
394 static int smbk5pwd_exop_passwd(
399 req_pwdexop_s *qpw = &op->oq_pwdexop;
402 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
403 smbk5pwd_t *pi = on->on_bi.bi_private;
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;
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;
415 term = qpw->rs_new.bv_val[qpw->rs_new.bv_len];
416 qpw->rs_new.bv_val[qpw->rs_new.bv_len] = '\0';
428 if ( !SMBK5PWD_DO_KRB5( pi ) ) break;
430 if ( !is_entry_objectclass(e, oc_krb5KDCEntry, 0 ) ) break;
432 a = attr_find( e->e_attrs, ad_krb5PrincipalName );
435 memset( &ent, 0, sizeof(ent) );
436 ret = krb5_parse_name(context, a->a_vals[0].bv_val, &ent.principal);
439 a = attr_find( e->e_attrs, ad_krb5KeyVersionNumber );
442 if ( lutil_atoi( &kvno, a->a_vals[0].bv_val ) != 0 ) {
443 Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
444 "dn=\"%s\" unable to parse krb5KeyVersionNumber=\"%s\"\n",
445 op->o_log_prefix, e->e_name.bv_val, a->a_vals[0].bv_val );
449 /* shouldn't happen, this is a required attr */
450 Debug( LDAP_DEBUG_ANY, "%s smbk5pwd EXOP: "
451 "dn=\"%s\" missing krb5KeyVersionNumber\n",
452 op->o_log_prefix, e->e_name.bv_val, 0 );
455 ret = hdb_generate_key_set_password(context, ent.principal,
456 qpw->rs_new.bv_val, &ent.keys.val, &nkeys);
457 ent.keys.len = nkeys;
458 hdb_seal_keys(context, db, &ent);
459 krb5_free_principal( context, ent.principal );
461 keys = ch_malloc( (ent.keys.len + 1) * sizeof(struct berval));
463 for (i = 0; i < ent.keys.len; i++) {
467 ASN1_MALLOC_ENCODE(Key, buf, len, &ent.keys.val[i], &len, ret);
471 keys[i].bv_val = (char *)buf;
472 keys[i].bv_len = len;
474 BER_BVZERO( &keys[i] );
476 hdb_free_keys(context, ent.keys.len, ent.keys.val);
478 if ( i != ent.keys.len ) {
479 ber_bvarray_free( keys );
483 ml = ch_malloc(sizeof(Modifications));
484 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
485 ml->sml_next = qpw->rs_mods;
488 ml->sml_desc = ad_krb5Key;
489 ml->sml_op = LDAP_MOD_REPLACE;
490 #ifdef SLAP_MOD_INTERNAL
491 ml->sml_flags = SLAP_MOD_INTERNAL;
494 ml->sml_values = keys;
495 ml->sml_nvalues = NULL;
497 ml = ch_malloc(sizeof(Modifications));
498 ml->sml_next = qpw->rs_mods;
501 ml->sml_desc = ad_krb5KeyVersionNumber;
502 ml->sml_op = LDAP_MOD_REPLACE;
503 #ifdef SLAP_MOD_INTERNAL
504 ml->sml_flags = SLAP_MOD_INTERNAL;
507 ml->sml_values = ch_malloc( 2 * sizeof(struct berval));
508 ml->sml_values[0].bv_val = ch_malloc( 64 );
509 ml->sml_values[0].bv_len = sprintf(ml->sml_values[0].bv_val,
511 BER_BVZERO( &ml->sml_values[1] );
512 ml->sml_nvalues = NULL;
518 if ( SMBK5PWD_DO_SAMBA( pi ) && is_entry_objectclass(e, oc_sambaSamAccount, 0 ) ) {
525 /* Expand incoming UTF8 string to UCS4 */
526 l = ldap_utf8_chars(qpw->rs_new.bv_val);
527 wcs = ch_malloc((l+1) * sizeof(wchar_t));
529 ldap_x_utf8s_to_wcs( wcs, qpw->rs_new.bv_val, l );
531 /* Truncate UCS4 to UCS2 */
533 for (j=0; j<l; j++) {
536 *c++ = (wc >> 8) & 0xff;
539 pwd.bv_val = (char *)wcs;
542 ml = ch_malloc(sizeof(Modifications));
543 if (!qpw->rs_modtail) qpw->rs_modtail = &ml->sml_next;
544 ml->sml_next = qpw->rs_mods;
547 keys = ch_malloc( 2 * sizeof(struct berval) );
548 BER_BVZERO( &keys[1] );
549 nthash( &pwd, keys );
551 ml->sml_desc = ad_sambaNTPassword;
552 ml->sml_op = LDAP_MOD_REPLACE;
553 #ifdef SLAP_MOD_INTERNAL
554 ml->sml_flags = SLAP_MOD_INTERNAL;
557 ml->sml_values = keys;
558 ml->sml_nvalues = NULL;
560 /* Truncate UCS2 to 8-bit ASCII */
563 for (j=1; j<l; j++) {
568 pwd.bv_val[pwd.bv_len] = '\0';
570 ml = ch_malloc(sizeof(Modifications));
571 ml->sml_next = qpw->rs_mods;
574 keys = ch_malloc( 2 * sizeof(struct berval) );
575 BER_BVZERO( &keys[1] );
576 lmhash( &pwd, keys );
578 ml->sml_desc = ad_sambaLMPassword;
579 ml->sml_op = LDAP_MOD_REPLACE;
580 #ifdef SLAP_MOD_INTERNAL
581 ml->sml_flags = SLAP_MOD_INTERNAL;
584 ml->sml_values = keys;
585 ml->sml_nvalues = NULL;
589 ml = ch_malloc(sizeof(Modifications));
590 ml->sml_next = qpw->rs_mods;
593 keys = ch_malloc( 2 * sizeof(struct berval) );
594 keys[0].bv_val = ch_malloc( LDAP_PVT_INTTYPE_CHARS(long) );
595 keys[0].bv_len = snprintf(keys[0].bv_val,
596 LDAP_PVT_INTTYPE_CHARS(long),
597 "%ld", slap_get_time());
598 BER_BVZERO( &keys[1] );
600 ml->sml_desc = ad_sambaPwdLastSet;
601 ml->sml_op = LDAP_MOD_REPLACE;
602 #ifdef SLAP_MOD_INTERNAL
603 ml->sml_flags = SLAP_MOD_INTERNAL;
606 ml->sml_values = keys;
607 ml->sml_nvalues = NULL;
609 if (pi->smb_must_change)
611 ml = ch_malloc(sizeof(Modifications));
612 ml->sml_next = qpw->rs_mods;
615 keys = ch_malloc( 2 * sizeof(struct berval) );
616 keys[0].bv_val = ch_malloc( LDAP_PVT_INTTYPE_CHARS(long) );
617 keys[0].bv_len = snprintf(keys[0].bv_val,
618 LDAP_PVT_INTTYPE_CHARS(long),
619 "%ld", slap_get_time() + pi->smb_must_change);
620 BER_BVZERO( &keys[1] );
622 ml->sml_desc = ad_sambaPwdMustChange;
623 ml->sml_op = LDAP_MOD_REPLACE;
624 #ifdef SLAP_MOD_INTERNAL
625 ml->sml_flags = SLAP_MOD_INTERNAL;
628 ml->sml_values = keys;
629 ml->sml_nvalues = NULL;
632 if (pi->smb_can_change)
634 ml = ch_malloc(sizeof(Modifications));
635 ml->sml_next = qpw->rs_mods;
638 keys = ch_malloc( 2 * sizeof(struct berval) );
639 keys[0].bv_val = ch_malloc( LDAP_PVT_INTTYPE_CHARS(long) );
640 keys[0].bv_len = snprintf(keys[0].bv_val,
641 LDAP_PVT_INTTYPE_CHARS(long),
642 "%ld", slap_get_time() + pi->smb_can_change);
643 BER_BVZERO( &keys[1] );
645 ml->sml_desc = ad_sambaPwdCanChange;
646 ml->sml_op = LDAP_MOD_REPLACE;
647 #ifdef SLAP_MOD_INTERNAL
648 ml->sml_flags = SLAP_MOD_INTERNAL;
651 ml->sml_values = keys;
652 ml->sml_nvalues = NULL;
655 #endif /* DO_SAMBA */
656 be_entry_release_r( op, e );
657 qpw->rs_new.bv_val[qpw->rs_new.bv_len] = term;
659 return SLAP_CB_CONTINUE;
662 static slap_overinst smbk5pwd;
664 /* back-config stuff */
666 PC_SMB_MUST_CHANGE = 1,
671 static ConfigDriver smbk5pwd_cf_func;
674 * NOTE: uses OID arcs OLcfgCtAt:1 and OLcfgCtOc:1
677 static ConfigTable smbk5pwd_cfats[] = {
678 { "smbk5pwd-enable", "arg",
679 2, 0, 0, ARG_MAGIC|PC_SMB_ENABLE, smbk5pwd_cf_func,
680 "( OLcfgCtAt:1.1 NAME 'olcSmbK5PwdEnable' "
681 "DESC 'Modules to be enabled' "
682 "SYNTAX OMsDirectoryString )", NULL, NULL },
683 { "smbk5pwd-must-change", "time",
684 2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_MUST_CHANGE, smbk5pwd_cf_func,
685 "( OLcfgCtAt:1.2 NAME 'olcSmbK5PwdMustChange' "
686 "DESC 'Credentials validity interval' "
687 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
688 { "smbk5pwd-can-change", "time",
689 2, 2, 0, ARG_MAGIC|ARG_INT|PC_SMB_CAN_CHANGE, smbk5pwd_cf_func,
690 "( OLcfgCtAt:1.3 NAME 'olcSmbK5PwdCanChange' "
691 "DESC 'Credentials minimum validity interval' "
692 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
694 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
697 static ConfigOCs smbk5pwd_cfocs[] = {
699 "NAME 'olcSmbK5PwdConfig' "
700 "DESC 'smbk5pwd overlay configuration' "
701 "SUP olcOverlayConfig "
704 "$ olcSmbK5PwdMustChange "
705 "$ olcSmbK5PwdCanChange "
706 ") )", Cft_Overlay, smbk5pwd_cfats },
712 * add here other functionalities; handle their initialization
713 * as appropriate in smbk5pwd_modules_init().
715 static slap_verbmasks smbk5pwd_modules[] = {
716 { BER_BVC( "krb5" ), SMBK5PWD_F_KRB5 },
717 { BER_BVC( "samba" ), SMBK5PWD_F_SAMBA },
722 smbk5pwd_cf_func( ConfigArgs *c )
724 slap_overinst *on = (slap_overinst *)c->bi;
727 smbk5pwd_t *pi = on->on_bi.bi_private;
729 if ( c->op == SLAP_CONFIG_EMIT ) {
731 case PC_SMB_MUST_CHANGE:
733 c->value_int = pi->smb_must_change;
734 #else /* ! DO_SAMBA */
736 #endif /* ! DO_SAMBA */
739 case PC_SMB_CAN_CHANGE:
741 c->value_int = pi->smb_can_change;
742 #else /* ! DO_SAMBA */
744 #endif /* ! DO_SAMBA */
748 c->rvalue_vals = NULL;
750 mask_to_verbs( smbk5pwd_modules, pi->mode, &c->rvalue_vals );
751 if ( c->rvalue_vals == NULL ) {
763 } else if ( c->op == LDAP_MOD_DELETE ) {
765 case PC_SMB_MUST_CHANGE:
768 case PC_SMB_CAN_CHANGE:
778 m = verb_to_mask( c->line, smbk5pwd_modules );
791 case PC_SMB_MUST_CHANGE:
793 if ( c->value_int < 0 ) {
794 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
795 "<%s> invalid negative value \"%d\".",
796 c->log, c->argv[ 0 ], 0 );
799 pi->smb_must_change = c->value_int;
800 #else /* ! DO_SAMBA */
801 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
802 "<%s> only meaningful "
803 "when compiled with -DDO_SAMBA.\n",
804 c->log, c->argv[ 0 ], 0 );
806 #endif /* ! DO_SAMBA */
809 case PC_SMB_CAN_CHANGE:
811 if ( c->value_int < 0 ) {
812 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
813 "<%s> invalid negative value \"%d\".",
814 c->log, c->argv[ 0 ], 0 );
817 pi->smb_can_change = c->value_int;
818 #else /* ! DO_SAMBA */
819 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
820 "<%s> only meaningful "
821 "when compiled with -DDO_SAMBA.\n",
822 c->log, c->argv[ 0 ], 0 );
824 #endif /* ! DO_SAMBA */
827 case PC_SMB_ENABLE: {
828 slap_mask_t mode = pi->mode, m;
830 rc = verbs_to_mask( c->argc, c->argv, smbk5pwd_modules, &m );
832 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
833 "<%s> unknown module \"%s\".\n",
834 c->log, c->argv[ 0 ], c->argv[ rc ] );
838 /* we can hijack the smbk5pwd_t structure because
839 * from within the configuration, this is the only
844 if ( SMBK5PWD_DO_KRB5( pi ) ) {
845 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
846 "<%s> module \"%s\" only allowed when compiled with -DDO_KRB5.\n",
847 c->log, c->argv[ 0 ], c->argv[ rc ] );
851 #endif /* ! DO_KRB5 */
854 if ( SMBK5PWD_DO_SAMBA( pi ) ) {
855 Debug( LDAP_DEBUG_ANY, "%s: smbk5pwd: "
856 "<%s> module \"%s\" only allowed when compiled with -DDO_SAMBA.\n",
857 c->log, c->argv[ 0 ], c->argv[ rc ] );
861 #endif /* ! DO_SAMBA */
864 BackendDB db = *c->be;
866 /* Re-initialize the module, because
867 * the configuration might have changed */
868 db.bd_info = (BackendInfo *)on;
869 rc = smbk5pwd_modules_init( pi );
886 smbk5pwd_modules_init( smbk5pwd_t *pi )
890 AttributeDescription **adp;
894 { "krb5Key", &ad_krb5Key },
895 { "krb5KeyVersionNumber", &ad_krb5KeyVersionNumber },
896 { "krb5PrincipalName", &ad_krb5PrincipalName },
897 { "krb5ValidEnd", &ad_krb5ValidEnd },
903 { "sambaLMPassword", &ad_sambaLMPassword },
904 { "sambaNTPassword", &ad_sambaNTPassword },
905 { "sambaPwdLastSet", &ad_sambaPwdLastSet },
906 { "sambaPwdMustChange", &ad_sambaPwdMustChange },
907 { "sambaPwdCanChange", &ad_sambaPwdCanChange },
910 #endif /* DO_SAMBA */
913 /* this is to silence the unused var warning */
914 dummy_ad.name = NULL;
917 if ( SMBK5PWD_DO_KRB5( pi ) && oc_krb5KDCEntry == NULL ) {
919 extern HDB *_kadm5_s_get_db(void *);
923 /* Make sure all of our necessary schema items are loaded */
924 oc_krb5KDCEntry = oc_find( "krb5KDCEntry" );
925 if ( !oc_krb5KDCEntry ) {
926 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
927 "unable to find \"krb5KDCEntry\" objectClass.\n",
932 for ( i = 0; krb5_ad[ i ].name != NULL; i++ ) {
935 *(krb5_ad[ i ].adp) = NULL;
937 rc = slap_str2ad( krb5_ad[ i ].name, krb5_ad[ i ].adp, &text );
938 if ( rc != LDAP_SUCCESS ) {
939 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
940 "unable to find \"%s\" attributeType: %s (%d).\n",
941 krb5_ad[ i ].name, text, rc );
942 oc_krb5KDCEntry = NULL;
947 /* Initialize Kerberos context */
948 ret = krb5_init_context(&context);
950 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
951 "unable to initialize krb5 context (%d).\n",
953 oc_krb5KDCEntry = NULL;
957 ret = kadm5_s_init_with_password_ctx( context,
961 &conf, 0, 0, &kadm_context );
963 char *err_str, *err_msg = "<unknown error>";
964 err_str = krb5_get_error_string( context );
966 err_msg = (char *)krb5_get_err_text( context, ret );
967 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
968 "unable to initialize krb5 admin context: %s (%d).\n",
969 err_str ? err_str : err_msg, ret, 0 );
971 krb5_free_error_string( context, err_str );
972 krb5_free_context( context );
973 oc_krb5KDCEntry = NULL;
977 db = _kadm5_s_get_db( kadm_context );
982 if ( SMBK5PWD_DO_SAMBA( pi ) && oc_sambaSamAccount == NULL ) {
985 oc_sambaSamAccount = oc_find( "sambaSamAccount" );
986 if ( !oc_sambaSamAccount ) {
987 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
988 "unable to find \"sambaSamAccount\" objectClass.\n",
993 for ( i = 0; samba_ad[ i ].name != NULL; i++ ) {
996 *(samba_ad[ i ].adp) = NULL;
998 rc = slap_str2ad( samba_ad[ i ].name, samba_ad[ i ].adp, &text );
999 if ( rc != LDAP_SUCCESS ) {
1000 Debug( LDAP_DEBUG_ANY, "smbk5pwd: "
1001 "unable to find \"%s\" attributeType: %s (%d).\n",
1002 samba_ad[ i ].name, text, rc );
1003 oc_sambaSamAccount = NULL;
1008 #endif /* DO_SAMBA */
1014 smbk5pwd_db_init(BackendDB *be, ConfigReply *cr)
1016 slap_overinst *on = (slap_overinst *)be->bd_info;
1019 pi = ch_calloc( 1, sizeof( smbk5pwd_t ) );
1023 on->on_bi.bi_private = (void *)pi;
1029 smbk5pwd_db_open(BackendDB *be, ConfigReply *cr)
1031 slap_overinst *on = (slap_overinst *)be->bd_info;
1032 smbk5pwd_t *pi = (smbk5pwd_t *)on->on_bi.bi_private;
1036 if ( pi->mode == 0 ) {
1037 pi->mode = SMBK5PWD_F_ALL;
1040 rc = smbk5pwd_modules_init( pi );
1049 smbk5pwd_db_destroy(BackendDB *be, ConfigReply *cr)
1051 slap_overinst *on = (slap_overinst *)be->bd_info;
1052 smbk5pwd_t *pi = (smbk5pwd_t *)on->on_bi.bi_private;
1062 smbk5pwd_initialize(void)
1066 smbk5pwd.on_bi.bi_type = "smbk5pwd";
1068 smbk5pwd.on_bi.bi_db_init = smbk5pwd_db_init;
1069 smbk5pwd.on_bi.bi_db_open = smbk5pwd_db_open;
1070 smbk5pwd.on_bi.bi_db_destroy = smbk5pwd_db_destroy;
1072 smbk5pwd.on_bi.bi_extended = smbk5pwd_exop_passwd;
1075 smbk5pwd.on_bi.bi_op_bind = smbk5pwd_op_bind;
1077 lutil_passwd_add( (struct berval *)&k5key_scheme, k5key_chk, k5key_hash );
1080 smbk5pwd.on_bi.bi_cf_ocs = smbk5pwd_cfocs;
1082 rc = config_register_schema( smbk5pwd_cfats, smbk5pwd_cfocs );
1087 return overlay_register( &smbk5pwd );
1090 #if SLAPD_OVER_SMBK5PWD == SLAPD_MOD_DYNAMIC
1091 int init_module(int argc, char *argv[]) {
1092 return smbk5pwd_initialize();
1096 #endif /* defined(SLAPD_OVER_SMBK5PWD) */