2 * Copyright (c) 1999-2001, 2003, PADL Software Pty Ltd.
3 * Copyright (c) 2004, Andrew Bartlett.
4 * Copyright (c) 2003 - 2008, Kungliga Tekniska Högskolan.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static krb5_error_code
LDAP__connect(krb5_context context
, HDB
*);
45 static krb5_error_code
LDAP_close(krb5_context context
, HDB
*);
47 static krb5_error_code
48 LDAP_message2entry(krb5_context context
, HDB
* db
, LDAPMessage
* msg
,
49 int flags
, hdb_entry_ex
* ent
);
51 static const char *default_structural_object
= "account";
52 static char *structural_object
;
53 static const char *default_ldap_url
= "ldapi:///";
54 static krb5_boolean samba_forwardable
;
62 char *h_bind_password
;
63 krb5_boolean h_start_tls
;
67 #define HDB2LDAP(db) (((struct hdbldapdb *)(db)->hdb_db)->h_lp)
68 #define HDB2MSGID(db) (((struct hdbldapdb *)(db)->hdb_db)->h_msgid)
69 #define HDBSETMSGID(db,msgid) \
70 do { ((struct hdbldapdb *)(db)->hdb_db)->h_msgid = msgid; } while(0)
71 #define HDB2BASE(dn) (((struct hdbldapdb *)(db)->hdb_db)->h_base)
72 #define HDB2URL(dn) (((struct hdbldapdb *)(db)->hdb_db)->h_url)
73 #define HDB2BINDDN(db) (((struct hdbldapdb *)(db)->hdb_db)->h_bind_dn)
74 #define HDB2BINDPW(db) (((struct hdbldapdb *)(db)->hdb_db)->h_bind_password)
75 #define HDB2CREATE(db) (((struct hdbldapdb *)(db)->hdb_db)->h_createbase)
81 static char * krb5kdcentry_attrs
[] = {
88 "krb5KeyVersionNumber",
103 "sambaPwdMustChange",
108 static char *krb5principal_attrs
[] = {
113 "krb5PrincipalRealm",
122 LDAP_no_size_limit(krb5_context context
, LDAP
*lp
)
124 int ret
, limit
= LDAP_NO_LIMIT
;
126 ret
= ldap_set_option(lp
, LDAP_OPT_SIZELIMIT
, (const void *)&limit
);
127 if (ret
!= LDAP_SUCCESS
) {
128 krb5_set_error_message(context
, HDB_ERR_BADVERSION
,
129 "ldap_set_option: %s",
130 ldap_err2string(ret
));
131 return HDB_ERR_BADVERSION
;
137 check_ldap(krb5_context context
, HDB
*db
, int ret
)
142 case LDAP_SERVER_DOWN
:
143 LDAP_close(context
, db
);
150 static krb5_error_code
151 LDAP__setmod(LDAPMod
*** modlist
, int modop
, const char *attribute
,
156 if (*modlist
== NULL
) {
157 *modlist
= (LDAPMod
**)ber_memcalloc(1, sizeof(LDAPMod
*));
158 if (*modlist
== NULL
)
162 for (cMods
= 0; (*modlist
)[cMods
] != NULL
; cMods
++) {
163 if ((*modlist
)[cMods
]->mod_op
== modop
&&
164 strcasecmp((*modlist
)[cMods
]->mod_type
, attribute
) == 0) {
171 if ((*modlist
)[cMods
] == NULL
) {
174 *modlist
= (LDAPMod
**)ber_memrealloc(*modlist
,
175 (cMods
+ 2) * sizeof(LDAPMod
*));
176 if (*modlist
== NULL
)
179 (*modlist
)[cMods
] = (LDAPMod
*)ber_memalloc(sizeof(LDAPMod
));
180 if ((*modlist
)[cMods
] == NULL
)
183 mod
= (*modlist
)[cMods
];
185 mod
->mod_type
= ber_strdup(attribute
);
186 if (mod
->mod_type
== NULL
) {
188 (*modlist
)[cMods
] = NULL
;
192 if (modop
& LDAP_MOD_BVALUES
) {
193 mod
->mod_bvalues
= NULL
;
195 mod
->mod_values
= NULL
;
198 (*modlist
)[cMods
+ 1] = NULL
;
204 static krb5_error_code
205 LDAP_addmod_len(LDAPMod
*** modlist
, int modop
, const char *attribute
,
206 unsigned char *value
, size_t len
)
211 ret
= LDAP__setmod(modlist
, modop
| LDAP_MOD_BVALUES
, attribute
, &cMods
);
218 bv
= (*modlist
)[cMods
]->mod_bvalues
;
220 for (i
= 0; bv
[i
] != NULL
; i
++)
222 bv
= ber_memrealloc(bv
, (i
+ 2) * sizeof(*bv
));
224 bv
= ber_memalloc(2 * sizeof(*bv
));
228 (*modlist
)[cMods
]->mod_bvalues
= bv
;
230 bv
[i
] = ber_memalloc(sizeof(**bv
));;
234 bv
[i
]->bv_val
= (void *)value
;
243 static krb5_error_code
244 LDAP_addmod(LDAPMod
*** modlist
, int modop
, const char *attribute
,
250 ret
= LDAP__setmod(modlist
, modop
, attribute
, &cMods
);
257 bv
= (*modlist
)[cMods
]->mod_values
;
259 for (i
= 0; bv
[i
] != NULL
; i
++)
261 bv
= ber_memrealloc(bv
, (i
+ 2) * sizeof(*bv
));
263 bv
= ber_memalloc(2 * sizeof(*bv
));
267 (*modlist
)[cMods
]->mod_values
= bv
;
269 bv
[i
] = ber_strdup(value
);
279 static krb5_error_code
280 LDAP_addmod_generalized_time(LDAPMod
*** mods
, int modop
,
281 const char *attribute
, KerberosTime
* time
)
286 /* XXX not threadsafe */
288 strftime(buf
, sizeof(buf
), "%Y%m%d%H%M%SZ", tm
);
290 return LDAP_addmod(mods
, modop
, attribute
, buf
);
293 static krb5_error_code
294 LDAP_addmod_integer(krb5_context context
,
295 LDAPMod
*** mods
, int modop
,
296 const char *attribute
, unsigned long l
)
301 ret
= asprintf(&buf
, "%ld", l
);
303 krb5_set_error_message(context
, ENOMEM
,
304 "asprintf: out of memory:");
307 ret
= LDAP_addmod(mods
, modop
, attribute
, buf
);
312 static krb5_error_code
313 LDAP_get_string_value(HDB
* db
, LDAPMessage
* entry
,
314 const char *attribute
, char **ptr
)
316 struct berval
**vals
;
318 vals
= ldap_get_values_len(HDB2LDAP(db
), entry
, attribute
);
319 if (vals
== NULL
|| vals
[0] == NULL
) {
321 return HDB_ERR_NOENTRY
;
324 *ptr
= malloc(vals
[0]->bv_len
+ 1);
326 ldap_value_free_len(vals
);
330 memcpy(*ptr
, vals
[0]->bv_val
, vals
[0]->bv_len
);
331 (*ptr
)[vals
[0]->bv_len
] = 0;
333 ldap_value_free_len(vals
);
338 static krb5_error_code
339 LDAP_get_integer_value(HDB
* db
, LDAPMessage
* entry
,
340 const char *attribute
, int *ptr
)
345 ret
= LDAP_get_string_value(db
, entry
, attribute
, &val
);
353 static krb5_error_code
354 LDAP_get_generalized_time_value(HDB
* db
, LDAPMessage
* entry
,
355 const char *attribute
, KerberosTime
* kt
)
363 ret
= LDAP_get_string_value(db
, entry
, attribute
, &gentime
);
367 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
370 return HDB_ERR_NOENTRY
;
381 bervalstrcmp(struct berval
*v
, const char *str
)
383 size_t len
= strlen(str
);
384 return (v
->bv_len
== len
) && strncasecmp(str
, (char *)v
->bv_val
, len
) == 0;
388 static krb5_error_code
389 LDAP_entry2mods(krb5_context context
, HDB
* db
, hdb_entry_ex
* ent
,
390 LDAPMessage
* msg
, LDAPMod
*** pmods
)
393 krb5_boolean is_new_entry
;
395 LDAPMod
**mods
= NULL
;
397 unsigned long oflags
, nflags
;
400 krb5_boolean is_samba_account
= FALSE
;
401 krb5_boolean is_account
= FALSE
;
402 krb5_boolean is_heimdal_entry
= FALSE
;
403 krb5_boolean is_heimdal_principal
= FALSE
;
405 struct berval
**vals
;
411 ret
= LDAP_message2entry(context
, db
, msg
, 0, &orig
);
415 is_new_entry
= FALSE
;
417 vals
= ldap_get_values_len(HDB2LDAP(db
), msg
, "objectClass");
419 int num_objectclasses
= ldap_count_values_len(vals
);
420 for (i
=0; i
< num_objectclasses
; i
++) {
421 if (bervalstrcmp(vals
[i
], "sambaSamAccount"))
422 is_samba_account
= TRUE
;
423 else if (bervalstrcmp(vals
[i
], structural_object
))
425 else if (bervalstrcmp(vals
[i
], "krb5Principal"))
426 is_heimdal_principal
= TRUE
;
427 else if (bervalstrcmp(vals
[i
], "krb5KDCEntry"))
428 is_heimdal_entry
= TRUE
;
430 ldap_value_free_len(vals
);
434 * If this is just a "account" entry and no other objectclass
435 * is hanging on this entry, it's really a new entry.
437 if (is_samba_account
== FALSE
&& is_heimdal_principal
== FALSE
&&
438 is_heimdal_entry
== FALSE
) {
439 if (is_account
== TRUE
) {
442 ret
= HDB_ERR_NOENTRY
;
451 /* to make it perfectly obvious we're depending on
452 * orig being intiialized to zero */
453 memset(&orig
, 0, sizeof(orig
));
455 ret
= LDAP_addmod(&mods
, LDAP_MOD_ADD
, "objectClass", "top");
459 /* account is the structural object class */
460 if (is_account
== FALSE
) {
461 ret
= LDAP_addmod(&mods
, LDAP_MOD_ADD
, "objectClass",
468 ret
= LDAP_addmod(&mods
, LDAP_MOD_ADD
, "objectClass", "krb5Principal");
469 is_heimdal_principal
= TRUE
;
473 ret
= LDAP_addmod(&mods
, LDAP_MOD_ADD
, "objectClass", "krb5KDCEntry");
474 is_heimdal_entry
= TRUE
;
480 krb5_principal_compare(context
, ent
->entry
.principal
, orig
.entry
.principal
)
483 if (is_heimdal_principal
|| is_heimdal_entry
) {
485 ret
= krb5_unparse_name(context
, ent
->entry
.principal
, &tmp
);
489 ret
= LDAP_addmod(&mods
, LDAP_MOD_REPLACE
,
490 "krb5PrincipalName", tmp
);
498 if (is_account
|| is_samba_account
) {
499 ret
= krb5_unparse_name_short(context
, ent
->entry
.principal
, &tmp
);
502 ret
= LDAP_addmod(&mods
, LDAP_MOD_REPLACE
, "uid", tmp
);
511 if (is_heimdal_entry
&& (ent
->entry
.kvno
!= orig
.entry
.kvno
|| is_new_entry
)) {
512 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
513 "krb5KeyVersionNumber",
519 if (is_heimdal_entry
&& ent
->entry
.valid_start
) {
520 if (orig
.entry
.valid_end
== NULL
521 || (*(ent
->entry
.valid_start
) != *(orig
.entry
.valid_start
))) {
522 ret
= LDAP_addmod_generalized_time(&mods
, LDAP_MOD_REPLACE
,
524 ent
->entry
.valid_start
);
530 if (ent
->entry
.valid_end
) {
531 if (orig
.entry
.valid_end
== NULL
|| (*(ent
->entry
.valid_end
) != *(orig
.entry
.valid_end
))) {
532 if (is_heimdal_entry
) {
533 ret
= LDAP_addmod_generalized_time(&mods
, LDAP_MOD_REPLACE
,
535 ent
->entry
.valid_end
);
539 if (is_samba_account
) {
540 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
542 *(ent
->entry
.valid_end
));
549 if (ent
->entry
.pw_end
) {
550 if (orig
.entry
.pw_end
== NULL
|| (*(ent
->entry
.pw_end
) != *(orig
.entry
.pw_end
))) {
551 if (is_heimdal_entry
) {
552 ret
= LDAP_addmod_generalized_time(&mods
, LDAP_MOD_REPLACE
,
559 if (is_samba_account
) {
560 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
561 "sambaPwdMustChange",
562 *(ent
->entry
.pw_end
));
570 #if 0 /* we we have last_pw_change */
571 if (is_samba_account
&& ent
->entry
.last_pw_change
) {
572 if (orig
.entry
.last_pw_change
== NULL
|| (*(ent
->entry
.last_pw_change
) != *(orig
.entry
.last_pw_change
))) {
573 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
575 *(ent
->entry
.last_pw_change
));
582 if (is_heimdal_entry
&& ent
->entry
.max_life
) {
583 if (orig
.entry
.max_life
== NULL
584 || (*(ent
->entry
.max_life
) != *(orig
.entry
.max_life
))) {
586 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
588 *(ent
->entry
.max_life
));
594 if (is_heimdal_entry
&& ent
->entry
.max_renew
) {
595 if (orig
.entry
.max_renew
== NULL
596 || (*(ent
->entry
.max_renew
) != *(orig
.entry
.max_renew
))) {
598 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
600 *(ent
->entry
.max_renew
));
606 oflags
= HDBFlags2int(orig
.entry
.flags
);
607 nflags
= HDBFlags2int(ent
->entry
.flags
);
609 if (is_heimdal_entry
&& oflags
!= nflags
) {
611 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
618 /* Remove keys if they exists, and then replace keys. */
619 if (!is_new_entry
&& orig
.entry
.keys
.len
> 0) {
620 vals
= ldap_get_values_len(HDB2LDAP(db
), msg
, "krb5Key");
622 ldap_value_free_len(vals
);
624 ret
= LDAP_addmod(&mods
, LDAP_MOD_DELETE
, "krb5Key", NULL
);
630 for (i
= 0; i
< ent
->entry
.keys
.len
; i
++) {
633 && ent
->entry
.keys
.val
[i
].key
.keytype
== ETYPE_ARCFOUR_HMAC_MD5
) {
636 time_t now
= time(NULL
);
638 /* the key might have been 'sealed', but samba passwords
639 are clear in the directory */
640 ret
= hdb_unseal_key(context
, db
, &ent
->entry
.keys
.val
[i
]);
644 nt
= ent
->entry
.keys
.val
[i
].key
.keyvalue
.data
;
645 /* store in ntPassword, not krb5key */
646 ret
= hex_encode(nt
, 16, &ntHexPassword
);
649 krb5_set_error_message(context
, ret
, "hdb-ldap: failed to "
653 ret
= LDAP_addmod(&mods
, LDAP_MOD_REPLACE
, "sambaNTPassword",
658 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_REPLACE
,
659 "sambaPwdLastSet", now
);
663 /* have to kill the LM passwod if it exists */
664 vals
= ldap_get_values_len(HDB2LDAP(db
), msg
, "sambaLMPassword");
666 ldap_value_free_len(vals
);
667 ret
= LDAP_addmod(&mods
, LDAP_MOD_DELETE
,
668 "sambaLMPassword", NULL
);
673 } else if (is_heimdal_entry
) {
675 size_t len
, buf_size
;
677 ASN1_MALLOC_ENCODE(Key
, buf
, buf_size
, &ent
->entry
.keys
.val
[i
], &len
, ret
);
681 krb5_abortx(context
, "internal error in ASN.1 encoder");
683 /* addmod_len _owns_ the key, doesn't need to copy it */
684 ret
= LDAP_addmod_len(&mods
, LDAP_MOD_ADD
, "krb5Key", buf
, len
);
690 if (ent
->entry
.etypes
) {
691 int add_krb5EncryptionType
= 0;
694 * Only add/modify krb5EncryptionType if it's a new heimdal
695 * entry or krb5EncryptionType already exists on the entry.
699 vals
= ldap_get_values_len(HDB2LDAP(db
), msg
, "krb5EncryptionType");
701 ldap_value_free_len(vals
);
702 ret
= LDAP_addmod(&mods
, LDAP_MOD_DELETE
, "krb5EncryptionType",
706 add_krb5EncryptionType
= 1;
708 } else if (is_heimdal_entry
)
709 add_krb5EncryptionType
= 1;
711 if (add_krb5EncryptionType
) {
712 for (i
= 0; i
< ent
->entry
.etypes
->len
; i
++) {
713 if (is_samba_account
&&
714 ent
->entry
.keys
.val
[i
].key
.keytype
== ETYPE_ARCFOUR_HMAC_MD5
)
717 } else if (is_heimdal_entry
) {
718 ret
= LDAP_addmod_integer(context
, &mods
, LDAP_MOD_ADD
,
719 "krb5EncryptionType",
720 ent
->entry
.etypes
->val
[i
]);
735 else if (mods
!= NULL
) {
736 ldap_mods_free(mods
, 1);
741 hdb_free_entry(context
, &orig
);
746 static krb5_error_code
747 LDAP_dn2principal(krb5_context context
, HDB
* db
, const char *dn
,
748 krb5_principal
* principal
)
752 const char *filter
= "(objectClass=krb5Principal)";
753 LDAPMessage
*res
= NULL
, *e
;
756 ret
= LDAP_no_size_limit(context
, HDB2LDAP(db
));
760 rc
= ldap_search_ext_s(HDB2LDAP(db
), dn
, LDAP_SCOPE_SUBTREE
,
761 filter
, krb5principal_attrs
, 0,
764 if (check_ldap(context
, db
, rc
)) {
765 ret
= HDB_ERR_NOENTRY
;
766 krb5_set_error_message(context
, ret
, "ldap_search_ext_s: "
767 "filter: %s error: %s",
768 filter
, ldap_err2string(rc
));
772 e
= ldap_first_entry(HDB2LDAP(db
), res
);
774 ret
= HDB_ERR_NOENTRY
;
778 ret
= LDAP_get_string_value(db
, e
, "krb5PrincipalName", &p
);
780 ret
= HDB_ERR_NOENTRY
;
784 ret
= krb5_parse_name(context
, p
, principal
);
795 need_quote(unsigned char c
)
806 const static char hexchar
[] = "0123456789ABCDEF";
808 static krb5_error_code
809 escape_value(krb5_context context
, const unsigned char *unquoted
, char **quoted
)
813 for (i
= 0, len
= 0; unquoted
[i
] != '\0'; i
++, len
++) {
814 if (need_quote((unsigned char)unquoted
[i
]))
818 *quoted
= malloc(len
+ 1);
819 if (*quoted
== NULL
) {
820 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
824 for (i
= 0; unquoted
[0] ; unquoted
++) {
825 if (need_quote((unsigned char *)unquoted
[0])) {
826 (*quoted
)[i
++] = '\\';
827 (*quoted
)[i
++] = hexchar
[(unquoted
[0] >> 4) & 0xf];
828 (*quoted
)[i
++] = hexchar
[(unquoted
[0] ) & 0xf];
830 (*quoted
)[i
++] = (char)unquoted
[0];
837 static krb5_error_code
838 LDAP__lookup_princ(krb5_context context
,
840 const char *princname
,
846 char *quote
, *filter
= NULL
;
848 ret
= LDAP__connect(context
, db
);
853 * Quote searches that contain filter language, this quote
854 * searches for *@REALM, which takes very long time.
857 ret
= escape_value(context
, princname
, "e
);
861 rc
= asprintf(&filter
,
862 "(&(objectClass=krb5Principal)(krb5PrincipalName=%s))",
868 krb5_set_error_message(context
, ret
, "malloc: out of memory");
872 ret
= LDAP_no_size_limit(context
, HDB2LDAP(db
));
876 rc
= ldap_search_ext_s(HDB2LDAP(db
), HDB2BASE(db
),
877 LDAP_SCOPE_SUBTREE
, filter
,
878 krb5kdcentry_attrs
, 0,
881 if (check_ldap(context
, db
, rc
)) {
882 ret
= HDB_ERR_NOENTRY
;
883 krb5_set_error_message(context
, ret
, "ldap_search_ext_s: "
884 "filter: %s - error: %s",
885 filter
, ldap_err2string(rc
));
889 if (userid
&& ldap_count_entries(HDB2LDAP(db
), *msg
) == 0) {
895 ret
= escape_value(context
, userid
, "e
);
899 rc
= asprintf(&filter
,
900 "(&(|(objectClass=sambaSamAccount)(objectClass=%s))(uid=%s))",
901 structural_object
, quote
);
905 krb5_set_error_message(context
, ret
, "asprintf: out of memory");
909 ret
= LDAP_no_size_limit(context
, HDB2LDAP(db
));
913 rc
= ldap_search_ext_s(HDB2LDAP(db
), HDB2BASE(db
), LDAP_SCOPE_SUBTREE
,
914 filter
, krb5kdcentry_attrs
, 0,
917 if (check_ldap(context
, db
, rc
)) {
918 ret
= HDB_ERR_NOENTRY
;
919 krb5_set_error_message(context
, ret
,
920 "ldap_search_ext_s: filter: %s error: %s",
921 filter
, ldap_err2string(rc
));
935 static krb5_error_code
936 LDAP_principal2message(krb5_context context
, HDB
* db
,
937 krb5_const_principal princ
, LDAPMessage
** msg
)
939 char *name
, *name_short
= NULL
;
945 ret
= krb5_unparse_name(context
, princ
, &name
);
949 ret
= krb5_get_default_realms(context
, &r0
);
954 for (r
= r0
; *r
!= NULL
; r
++) {
955 if(strcmp(krb5_principal_get_realm(context
, princ
), *r
) == 0) {
956 ret
= krb5_unparse_name_short(context
, princ
, &name_short
);
958 krb5_free_host_realm(context
, r0
);
965 krb5_free_host_realm(context
, r0
);
967 ret
= LDAP__lookup_princ(context
, db
, name
, name_short
, msg
);
975 * Construct an hdb_entry from a directory entry.
977 static krb5_error_code
978 LDAP_message2entry(krb5_context context
, HDB
* db
, LDAPMessage
* msg
,
979 int flags
, hdb_entry_ex
* ent
)
981 char *unparsed_name
= NULL
, *dn
= NULL
, *ntPasswordIN
= NULL
;
982 char *samba_acct_flags
= NULL
;
983 struct berval
**keys
;
984 struct berval
**vals
;
985 int tmp
, tmp_time
, i
, ret
, have_arcfour
= 0;
987 memset(ent
, 0, sizeof(*ent
));
988 ent
->entry
.flags
= int2HDBFlags(0);
990 ret
= LDAP_get_string_value(db
, msg
, "krb5PrincipalName", &unparsed_name
);
992 ret
= krb5_parse_name(context
, unparsed_name
, &ent
->entry
.principal
);
996 ret
= LDAP_get_string_value(db
, msg
, "uid",
999 ret
= krb5_parse_name(context
, unparsed_name
, &ent
->entry
.principal
);
1003 krb5_set_error_message(context
, HDB_ERR_NOENTRY
,
1004 "hdb-ldap: ldap entry missing"
1006 return HDB_ERR_NOENTRY
;
1012 ret
= LDAP_get_integer_value(db
, msg
, "krb5KeyVersionNumber",
1015 ent
->entry
.kvno
= 0;
1017 ent
->entry
.kvno
= integer
;
1020 keys
= ldap_get_values_len(HDB2LDAP(db
), msg
, "krb5Key");
1025 ent
->entry
.keys
.len
= ldap_count_values_len(keys
);
1026 ent
->entry
.keys
.val
= (Key
*) calloc(ent
->entry
.keys
.len
, sizeof(Key
));
1027 if (ent
->entry
.keys
.val
== NULL
) {
1029 krb5_set_error_message(context
, ret
, "calloc: out of memory");
1032 for (i
= 0; i
< ent
->entry
.keys
.len
; i
++) {
1033 decode_Key((unsigned char *) keys
[i
]->bv_val
,
1034 (size_t) keys
[i
]->bv_len
, &ent
->entry
.keys
.val
[i
], &l
);
1040 * This violates the ASN1 but it allows a principal to
1041 * be related to a general directory entry without creating
1042 * the keys. Hopefully it's OK.
1044 ent
->entry
.keys
.len
= 0;
1045 ent
->entry
.keys
.val
= NULL
;
1047 ret
= HDB_ERR_NOENTRY
;
1052 vals
= ldap_get_values_len(HDB2LDAP(db
), msg
, "krb5EncryptionType");
1056 ent
->entry
.etypes
= malloc(sizeof(*(ent
->entry
.etypes
)));
1057 if (ent
->entry
.etypes
== NULL
) {
1059 krb5_set_error_message(context
, ret
,"malloc: out of memory");
1062 ent
->entry
.etypes
->len
= ldap_count_values_len(vals
);
1063 ent
->entry
.etypes
->val
= calloc(ent
->entry
.etypes
->len
, sizeof(int));
1064 if (ent
->entry
.etypes
->val
== NULL
) {
1066 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1067 ent
->entry
.etypes
->len
= 0;
1070 for (i
= 0; i
< ent
->entry
.etypes
->len
; i
++) {
1073 buf
= malloc(vals
[i
]->bv_len
+ 1);
1076 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1079 memcpy(buf
, vals
[i
]->bv_val
, vals
[i
]->bv_len
);
1080 buf
[vals
[i
]->bv_len
] = '\0';
1081 ent
->entry
.etypes
->val
[i
] = atoi(buf
);
1084 ldap_value_free_len(vals
);
1087 for (i
= 0; i
< ent
->entry
.keys
.len
; i
++) {
1088 if (ent
->entry
.keys
.val
[i
].key
.keytype
== ETYPE_ARCFOUR_HMAC_MD5
) {
1094 /* manually construct the NT (type 23) key */
1095 ret
= LDAP_get_string_value(db
, msg
, "sambaNTPassword", &ntPasswordIN
);
1096 if (ret
== 0 && have_arcfour
== 0) {
1101 keys
= realloc(ent
->entry
.keys
.val
,
1102 (ent
->entry
.keys
.len
+ 1) * sizeof(ent
->entry
.keys
.val
[0]));
1106 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1109 ent
->entry
.keys
.val
= keys
;
1110 memset(&ent
->entry
.keys
.val
[ent
->entry
.keys
.len
], 0, sizeof(Key
));
1111 ent
->entry
.keys
.val
[ent
->entry
.keys
.len
].key
.keytype
= ETYPE_ARCFOUR_HMAC_MD5
;
1112 ret
= krb5_data_alloc (&ent
->entry
.keys
.val
[ent
->entry
.keys
.len
].key
.keyvalue
, 16);
1114 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1119 ret
= hex_decode(ntPasswordIN
,
1120 ent
->entry
.keys
.val
[ent
->entry
.keys
.len
].key
.keyvalue
.data
, 16);
1121 ent
->entry
.keys
.len
++;
1123 if (ent
->entry
.etypes
== NULL
) {
1124 ent
->entry
.etypes
= malloc(sizeof(*(ent
->entry
.etypes
)));
1125 if (ent
->entry
.etypes
== NULL
) {
1127 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1130 ent
->entry
.etypes
->val
= NULL
;
1131 ent
->entry
.etypes
->len
= 0;
1134 for (i
= 0; i
< ent
->entry
.etypes
->len
; i
++)
1135 if (ent
->entry
.etypes
->val
[i
] == ETYPE_ARCFOUR_HMAC_MD5
)
1137 /* If there is no ARCFOUR enctype, add one */
1138 if (i
== ent
->entry
.etypes
->len
) {
1139 etypes
= realloc(ent
->entry
.etypes
->val
,
1140 (ent
->entry
.etypes
->len
+ 1) *
1141 sizeof(ent
->entry
.etypes
->val
[0]));
1142 if (etypes
== NULL
) {
1144 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1147 ent
->entry
.etypes
->val
= etypes
;
1148 ent
->entry
.etypes
->val
[ent
->entry
.etypes
->len
] =
1149 ETYPE_ARCFOUR_HMAC_MD5
;
1150 ent
->entry
.etypes
->len
++;
1154 ret
= LDAP_get_generalized_time_value(db
, msg
, "createTimestamp",
1155 &ent
->entry
.created_by
.time
);
1157 ent
->entry
.created_by
.time
= time(NULL
);
1159 ent
->entry
.created_by
.principal
= NULL
;
1161 if (flags
& HDB_F_ADMIN_DATA
) {
1162 ret
= LDAP_get_string_value(db
, msg
, "creatorsName", &dn
);
1164 LDAP_dn2principal(context
, db
, dn
, &ent
->entry
.created_by
.principal
);
1168 ent
->entry
.modified_by
= calloc(1, sizeof(*ent
->entry
.modified_by
));
1169 if (ent
->entry
.modified_by
== NULL
) {
1171 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1175 ret
= LDAP_get_generalized_time_value(db
, msg
, "modifyTimestamp",
1176 &ent
->entry
.modified_by
->time
);
1178 ret
= LDAP_get_string_value(db
, msg
, "modifiersName", &dn
);
1180 LDAP_dn2principal(context
, db
, dn
, &ent
->entry
.modified_by
->principal
);
1183 free(ent
->entry
.modified_by
);
1184 ent
->entry
.modified_by
= NULL
;
1189 ent
->entry
.valid_start
= malloc(sizeof(*ent
->entry
.valid_start
));
1190 if (ent
->entry
.valid_start
== NULL
) {
1192 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1195 ret
= LDAP_get_generalized_time_value(db
, msg
, "krb5ValidStart",
1196 ent
->entry
.valid_start
);
1199 free(ent
->entry
.valid_start
);
1200 ent
->entry
.valid_start
= NULL
;
1203 ent
->entry
.valid_end
= malloc(sizeof(*ent
->entry
.valid_end
));
1204 if (ent
->entry
.valid_end
== NULL
) {
1206 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1209 ret
= LDAP_get_generalized_time_value(db
, msg
, "krb5ValidEnd",
1210 ent
->entry
.valid_end
);
1213 free(ent
->entry
.valid_end
);
1214 ent
->entry
.valid_end
= NULL
;
1217 ret
= LDAP_get_integer_value(db
, msg
, "sambaKickoffTime", &tmp_time
);
1219 if (ent
->entry
.valid_end
== NULL
) {
1220 ent
->entry
.valid_end
= malloc(sizeof(*ent
->entry
.valid_end
));
1221 if (ent
->entry
.valid_end
== NULL
) {
1223 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1227 *ent
->entry
.valid_end
= tmp_time
;
1230 ent
->entry
.pw_end
= malloc(sizeof(*ent
->entry
.pw_end
));
1231 if (ent
->entry
.pw_end
== NULL
) {
1233 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1236 ret
= LDAP_get_generalized_time_value(db
, msg
, "krb5PasswordEnd",
1240 free(ent
->entry
.pw_end
);
1241 ent
->entry
.pw_end
= NULL
;
1244 ret
= LDAP_get_integer_value(db
, msg
, "sambaPwdLastSet", &tmp_time
);
1248 delta
= krb5_config_get_time_default(context
, NULL
,
1251 "password_lifetime",
1255 if (ent
->entry
.pw_end
== NULL
) {
1256 ent
->entry
.pw_end
= malloc(sizeof(*ent
->entry
.pw_end
));
1257 if (ent
->entry
.pw_end
== NULL
) {
1259 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1264 *ent
->entry
.pw_end
= tmp_time
+ delta
;
1268 ret
= LDAP_get_integer_value(db
, msg
, "sambaPwdMustChange", &tmp_time
);
1270 if (ent
->entry
.pw_end
== NULL
) {
1271 ent
->entry
.pw_end
= malloc(sizeof(*ent
->entry
.pw_end
));
1272 if (ent
->entry
.pw_end
== NULL
) {
1274 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1278 *ent
->entry
.pw_end
= tmp_time
;
1282 ret
= LDAP_get_integer_value(db
, msg
, "sambaPwdLastSet", &tmp_time
);
1284 hdb_entry_set_pw_change_time(context
, &ent
->entry
, tmp_time
);
1289 ent
->entry
.max_life
= malloc(sizeof(*ent
->entry
.max_life
));
1290 if (ent
->entry
.max_life
== NULL
) {
1292 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1295 ret
= LDAP_get_integer_value(db
, msg
, "krb5MaxLife", &max_life
);
1297 free(ent
->entry
.max_life
);
1298 ent
->entry
.max_life
= NULL
;
1300 *ent
->entry
.max_life
= max_life
;
1306 ent
->entry
.max_renew
= malloc(sizeof(*ent
->entry
.max_renew
));
1307 if (ent
->entry
.max_renew
== NULL
) {
1309 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1312 ret
= LDAP_get_integer_value(db
, msg
, "krb5MaxRenew", &max_renew
);
1314 free(ent
->entry
.max_renew
);
1315 ent
->entry
.max_renew
= NULL
;
1317 *ent
->entry
.max_renew
= max_renew
;
1320 ret
= LDAP_get_integer_value(db
, msg
, "krb5KDCFlags", &tmp
);
1324 ent
->entry
.flags
= int2HDBFlags(tmp
);
1326 /* Try and find Samba flags to put into the mix */
1327 ret
= LDAP_get_string_value(db
, msg
, "sambaAcctFlags", &samba_acct_flags
);
1329 /* parse the [UXW...] string:
1333 'H' Homedir required
1335 'U' User account (normal)
1336 'M' MNS logon user account - what is this ?
1337 'W' Workstation account
1340 'X' No Xpiry on password
1341 'I' Interdomain trust account
1346 int flags_len
= strlen(samba_acct_flags
);
1351 if (samba_acct_flags
[0] != '['
1352 || samba_acct_flags
[flags_len
- 1] != ']')
1355 /* Allow forwarding */
1356 if (samba_forwardable
)
1357 ent
->entry
.flags
.forwardable
= TRUE
;
1359 for (i
=0; i
< flags_len
; i
++) {
1360 switch (samba_acct_flags
[i
]) {
1366 /* how to handle no password in kerberos? */
1369 ent
->entry
.flags
.invalid
= TRUE
;
1374 /* temp duplicate */
1375 ent
->entry
.flags
.invalid
= TRUE
;
1378 ent
->entry
.flags
.client
= TRUE
;
1384 ent
->entry
.flags
.server
= TRUE
;
1385 ent
->entry
.flags
.client
= TRUE
;
1388 ent
->entry
.flags
.invalid
= TRUE
;
1391 if (ent
->entry
.pw_end
) {
1392 free(ent
->entry
.pw_end
);
1393 ent
->entry
.pw_end
= NULL
;
1397 ent
->entry
.flags
.server
= TRUE
;
1398 ent
->entry
.flags
.client
= TRUE
;
1403 free(samba_acct_flags
);
1410 free(unparsed_name
);
1413 hdb_free_entry(context
, ent
);
1418 static krb5_error_code
1419 LDAP_close(krb5_context context
, HDB
* db
)
1422 ldap_unbind_ext(HDB2LDAP(db
), NULL
, NULL
);
1423 ((struct hdbldapdb
*)db
->hdb_db
)->h_lp
= NULL
;
1429 static krb5_error_code
1430 LDAP_lock(krb5_context context
, HDB
* db
, int operation
)
1435 static krb5_error_code
1436 LDAP_unlock(krb5_context context
, HDB
* db
)
1441 static krb5_error_code
1442 LDAP_seq(krb5_context context
, HDB
* db
, unsigned flags
, hdb_entry_ex
* entry
)
1444 int msgid
, rc
, parserc
;
1445 krb5_error_code ret
;
1448 msgid
= HDB2MSGID(db
);
1450 return HDB_ERR_NOENTRY
;
1453 rc
= ldap_result(HDB2LDAP(db
), msgid
, LDAP_MSG_ONE
, NULL
, &e
);
1455 case LDAP_RES_SEARCH_REFERENCE
:
1459 case LDAP_RES_SEARCH_ENTRY
:
1460 /* We have an entry. Parse it. */
1461 ret
= LDAP_message2entry(context
, db
, e
, flags
, entry
);
1464 case LDAP_RES_SEARCH_RESULT
:
1465 /* We're probably at the end of the results. If not, abandon. */
1467 ldap_parse_result(HDB2LDAP(db
), e
, NULL
, NULL
, NULL
,
1469 ret
= HDB_ERR_NOENTRY
;
1470 if (parserc
!= LDAP_SUCCESS
1471 && parserc
!= LDAP_MORE_RESULTS_TO_RETURN
) {
1472 krb5_set_error_message(context
, ret
, "ldap_parse_result: %s",
1473 ldap_err2string(parserc
));
1474 ldap_abandon_ext(HDB2LDAP(db
), msgid
, NULL
, NULL
);
1476 HDBSETMSGID(db
, -1);
1478 case LDAP_SERVER_DOWN
:
1480 LDAP_close(context
, db
);
1481 HDBSETMSGID(db
, -1);
1485 /* Some unspecified error (timeout?). Abandon. */
1487 ldap_abandon_ext(HDB2LDAP(db
), msgid
, NULL
, NULL
);
1488 ret
= HDB_ERR_NOENTRY
;
1489 HDBSETMSGID(db
, -1);
1492 } while (rc
== LDAP_RES_SEARCH_REFERENCE
);
1495 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
1496 ret
= hdb_unseal_keys(context
, db
, &entry
->entry
);
1498 hdb_free_entry(context
, entry
);
1505 static krb5_error_code
1506 LDAP_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
1507 hdb_entry_ex
*entry
)
1509 krb5_error_code ret
;
1512 ret
= LDAP__connect(context
, db
);
1516 ret
= LDAP_no_size_limit(context
, HDB2LDAP(db
));
1520 ret
= ldap_search_ext(HDB2LDAP(db
), HDB2BASE(db
),
1522 "(|(objectClass=krb5Principal)(objectClass=sambaSamAccount))",
1523 krb5kdcentry_attrs
, 0,
1524 NULL
, NULL
, NULL
, 0, &msgid
);
1526 return HDB_ERR_NOENTRY
;
1528 HDBSETMSGID(db
, msgid
);
1530 return LDAP_seq(context
, db
, flags
, entry
);
1533 static krb5_error_code
1534 LDAP_nextkey(krb5_context context
, HDB
* db
, unsigned flags
,
1535 hdb_entry_ex
* entry
)
1537 return LDAP_seq(context
, db
, flags
, entry
);
1540 static krb5_error_code
1541 LDAP__connect(krb5_context context
, HDB
* db
)
1543 int rc
, version
= LDAP_VERSION3
;
1545 * Empty credentials to do a SASL bind with LDAP. Note that empty
1546 * different from NULL credentials. If you provide NULL
1547 * credentials instead of empty credentials you will get a SASL
1548 * bind in progress message.
1550 struct berval bv
= { 0, "" };
1551 const char *sasl_method
= "EXTERNAL";
1552 const char *bind_dn
= NULL
;
1554 if (HDB2BINDDN(db
) != NULL
&& HDB2BINDPW(db
) != NULL
) {
1555 /* A bind DN was specified; use SASL SIMPLE */
1556 bind_dn
= HDB2BINDDN(db
);
1557 sasl_method
= LDAP_SASL_SIMPLE
;
1558 bv
.bv_val
= HDB2BINDPW(db
);
1559 bv
.bv_len
= strlen(bv
.bv_val
);
1563 /* connection has been opened. ping server. */
1564 struct sockaddr_un addr
;
1565 socklen_t len
= sizeof(addr
);
1568 if (ldap_get_option(HDB2LDAP(db
), LDAP_OPT_DESC
, &sd
) == 0 &&
1569 getpeername(sd
, (struct sockaddr
*) &addr
, &len
) < 0) {
1570 /* the other end has died. reopen. */
1571 LDAP_close(context
, db
);
1575 if (HDB2LDAP(db
) != NULL
) /* server is UP */
1578 rc
= ldap_initialize(&((struct hdbldapdb
*)db
->hdb_db
)->h_lp
, HDB2URL(db
));
1579 if (rc
!= LDAP_SUCCESS
) {
1580 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "ldap_initialize: %s",
1581 ldap_err2string(rc
));
1582 return HDB_ERR_NOENTRY
;
1585 rc
= ldap_set_option(HDB2LDAP(db
), LDAP_OPT_PROTOCOL_VERSION
,
1586 (const void *)&version
);
1587 if (rc
!= LDAP_SUCCESS
) {
1588 krb5_set_error_message(context
, HDB_ERR_BADVERSION
,
1589 "ldap_set_option: %s", ldap_err2string(rc
));
1590 LDAP_close(context
, db
);
1591 return HDB_ERR_BADVERSION
;
1594 if (((struct hdbldapdb
*)db
->hdb_db
)->h_start_tls
) {
1595 rc
= ldap_start_tls_s(HDB2LDAP(db
), NULL
, NULL
);
1597 if (rc
!= LDAP_SUCCESS
) {
1598 krb5_set_error_message(context
, HDB_ERR_BADVERSION
,
1599 "ldap_start_tls_s: %s", ldap_err2string(rc
));
1600 LDAP_close(context
, db
);
1601 return HDB_ERR_BADVERSION
;
1605 rc
= ldap_sasl_bind_s(HDB2LDAP(db
), bind_dn
, sasl_method
, &bv
,
1607 if (rc
!= LDAP_SUCCESS
) {
1608 krb5_set_error_message(context
, HDB_ERR_BADVERSION
,
1609 "ldap_sasl_bind_s: %s", ldap_err2string(rc
));
1610 LDAP_close(context
, db
);
1611 return HDB_ERR_BADVERSION
;
1617 static krb5_error_code
1618 LDAP_open(krb5_context context
, HDB
* db
, int flags
, mode_t mode
)
1620 /* Not the right place for this. */
1621 #ifdef HAVE_SIGACTION
1622 struct sigaction sa
;
1625 sa
.sa_handler
= SIG_IGN
;
1626 sigemptyset(&sa
.sa_mask
);
1628 sigaction(SIGPIPE
, &sa
, NULL
);
1630 signal(SIGPIPE
, SIG_IGN
);
1631 #endif /* HAVE_SIGACTION */
1633 return LDAP__connect(context
, db
);
1636 static krb5_error_code
1637 LDAP_fetch_kvno(krb5_context context
, HDB
* db
, krb5_const_principal principal
,
1638 unsigned flags
, krb5_kvno kvno
, hdb_entry_ex
* entry
)
1640 LDAPMessage
*msg
, *e
;
1641 krb5_error_code ret
;
1643 ret
= LDAP_principal2message(context
, db
, principal
, &msg
);
1647 e
= ldap_first_entry(HDB2LDAP(db
), msg
);
1649 ret
= HDB_ERR_NOENTRY
;
1653 ret
= LDAP_message2entry(context
, db
, e
, flags
, entry
);
1655 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
1656 ret
= hdb_unseal_keys(context
, db
, &entry
->entry
);
1658 hdb_free_entry(context
, entry
);
1668 static krb5_error_code
1669 LDAP_fetch(krb5_context context
, HDB
* db
, krb5_const_principal principal
,
1670 unsigned flags
, hdb_entry_ex
* entry
)
1672 return LDAP_fetch_kvno(context
, db
, principal
,
1673 flags
& (~HDB_F_KVNO_SPECIFIED
), 0, entry
);
1676 static krb5_error_code
1677 LDAP_store(krb5_context context
, HDB
* db
, unsigned flags
,
1678 hdb_entry_ex
* entry
)
1680 LDAPMod
**mods
= NULL
;
1681 krb5_error_code ret
;
1684 LDAPMessage
*msg
= NULL
, *e
= NULL
;
1685 char *dn
= NULL
, *name
= NULL
;
1687 ret
= LDAP_principal2message(context
, db
, entry
->entry
.principal
, &msg
);
1689 e
= ldap_first_entry(HDB2LDAP(db
), msg
);
1691 ret
= krb5_unparse_name(context
, entry
->entry
.principal
, &name
);
1697 ret
= hdb_seal_keys(context
, db
, &entry
->entry
);
1701 /* turn new entry into LDAPMod array */
1702 ret
= LDAP_entry2mods(context
, db
, entry
, e
, &mods
);
1707 ret
= asprintf(&dn
, "krb5PrincipalName=%s,%s", name
, HDB2CREATE(db
));
1710 krb5_set_error_message(context
, ret
, "asprintf: out of memory");
1713 } else if (flags
& HDB_F_REPLACE
) {
1714 /* Entry exists, and we're allowed to replace it. */
1715 dn
= ldap_get_dn(HDB2LDAP(db
), e
);
1717 /* Entry exists, but we're not allowed to replace it. Bail. */
1718 ret
= HDB_ERR_EXISTS
;
1722 /* write entry into directory */
1724 /* didn't exist before */
1725 rc
= ldap_add_ext_s(HDB2LDAP(db
), dn
, mods
, NULL
, NULL
);
1726 errfn
= "ldap_add_ext_s";
1728 /* already existed, send deltas only */
1729 rc
= ldap_modify_ext_s(HDB2LDAP(db
), dn
, mods
, NULL
, NULL
);
1730 errfn
= "ldap_modify_ext_s";
1733 if (check_ldap(context
, db
, rc
)) {
1734 char *ld_error
= NULL
;
1735 ldap_get_option(HDB2LDAP(db
), LDAP_OPT_ERROR_STRING
,
1737 ret
= HDB_ERR_CANT_LOCK_DB
;
1738 krb5_set_error_message(context
, ret
, "%s: %s (DN=%s) %s: %s",
1739 errfn
, name
, dn
, ldap_err2string(rc
), ld_error
);
1750 ldap_mods_free(mods
, 1);
1757 static krb5_error_code
1758 LDAP_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
1760 krb5_error_code ret
;
1761 LDAPMessage
*msg
, *e
;
1763 int rc
, limit
= LDAP_NO_LIMIT
;
1765 ret
= LDAP_principal2message(context
, db
, principal
, &msg
);
1769 e
= ldap_first_entry(HDB2LDAP(db
), msg
);
1771 ret
= HDB_ERR_NOENTRY
;
1775 dn
= ldap_get_dn(HDB2LDAP(db
), e
);
1777 ret
= HDB_ERR_NOENTRY
;
1781 rc
= ldap_set_option(HDB2LDAP(db
), LDAP_OPT_SIZELIMIT
, (const void *)&limit
);
1782 if (rc
!= LDAP_SUCCESS
) {
1783 ret
= HDB_ERR_BADVERSION
;
1784 krb5_set_error_message(context
, ret
, "ldap_set_option: %s",
1785 ldap_err2string(rc
));
1789 rc
= ldap_delete_ext_s(HDB2LDAP(db
), dn
, NULL
, NULL
);
1790 if (check_ldap(context
, db
, rc
)) {
1791 ret
= HDB_ERR_CANT_LOCK_DB
;
1792 krb5_set_error_message(context
, ret
, "ldap_delete_ext_s: %s",
1793 ldap_err2string(rc
));
1806 static krb5_error_code
1807 LDAP_destroy(krb5_context context
, HDB
* db
)
1809 krb5_error_code ret
;
1811 LDAP_close(context
, db
);
1813 ret
= hdb_clear_master_key(context
, db
);
1817 free(HDB2CREATE(db
));
1828 static krb5_error_code
1829 hdb_ldap_common(krb5_context context
,
1831 const char *search_base
,
1834 struct hdbldapdb
*h
;
1835 const char *create_base
= NULL
;
1836 const char *ldap_secret_file
= NULL
;
1838 if (url
== NULL
|| url
[0] == '\0') {
1840 p
= krb5_config_get_string(context
, NULL
, "kdc",
1841 "hdb-ldap-url", NULL
);
1843 p
= default_ldap_url
;
1848 if (search_base
== NULL
&& search_base
[0] == '\0') {
1849 krb5_set_error_message(context
, ENOMEM
, "ldap search base not configured");
1850 return ENOMEM
; /* XXX */
1853 if (structural_object
== NULL
) {
1856 p
= krb5_config_get_string(context
, NULL
, "kdc",
1857 "hdb-ldap-structural-object", NULL
);
1859 p
= default_structural_object
;
1860 structural_object
= strdup(p
);
1861 if (structural_object
== NULL
) {
1862 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1868 krb5_config_get_bool_default(context
, NULL
, TRUE
,
1869 "kdc", "hdb-samba-forwardable", NULL
);
1871 *db
= calloc(1, sizeof(**db
));
1873 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1876 memset(*db
, 0, sizeof(**db
));
1878 h
= calloc(1, sizeof(*h
));
1882 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
1888 if (asprintf(&(*db
)->hdb_name
, "ldap:%s", search_base
) == -1) {
1889 LDAP_destroy(context
, *db
);
1891 krb5_set_error_message(context
, ENOMEM
, "strdup: out of memory");
1895 h
->h_url
= strdup(url
);
1896 h
->h_base
= strdup(search_base
);
1897 if (h
->h_url
== NULL
|| h
->h_base
== NULL
) {
1898 LDAP_destroy(context
, *db
);
1900 krb5_set_error_message(context
, ENOMEM
, "strdup: out of memory");
1904 ldap_secret_file
= krb5_config_get_string(context
, NULL
, "kdc",
1905 "hdb-ldap-secret-file", NULL
);
1906 if (ldap_secret_file
!= NULL
) {
1907 krb5_config_binding
*tmp
;
1908 krb5_error_code ret
;
1911 ret
= krb5_config_parse_file(context
, ldap_secret_file
, &tmp
);
1915 p
= krb5_config_get_string(context
, tmp
, "kdc",
1916 "hdb-ldap-bind-dn", NULL
);
1918 h
->h_bind_dn
= strdup(p
);
1920 p
= krb5_config_get_string(context
, tmp
, "kdc",
1921 "hdb-ldap-bind-password", NULL
);
1923 h
->h_bind_password
= strdup(p
);
1925 krb5_config_file_free(context
, tmp
);
1929 krb5_config_get_bool_default(context
, NULL
, FALSE
,
1930 "kdc", "hdb-ldap-start-tls", NULL
);
1932 create_base
= krb5_config_get_string(context
, NULL
, "kdc",
1933 "hdb-ldap-create-base", NULL
);
1934 if (create_base
== NULL
)
1935 create_base
= h
->h_base
;
1937 h
->h_createbase
= strdup(create_base
);
1938 if (h
->h_createbase
== NULL
) {
1939 LDAP_destroy(context
, *db
);
1941 krb5_set_error_message(context
, ENOMEM
, "strdup: out of memory");
1945 (*db
)->hdb_master_key_set
= 0;
1946 (*db
)->hdb_openp
= 0;
1947 (*db
)->hdb_capability_flags
= 0;
1948 (*db
)->hdb_open
= LDAP_open
;
1949 (*db
)->hdb_close
= LDAP_close
;
1950 (*db
)->hdb_fetch_kvno
= LDAP_fetch_kvno
;
1951 (*db
)->hdb_store
= LDAP_store
;
1952 (*db
)->hdb_remove
= LDAP_remove
;
1953 (*db
)->hdb_firstkey
= LDAP_firstkey
;
1954 (*db
)->hdb_nextkey
= LDAP_nextkey
;
1955 (*db
)->hdb_lock
= LDAP_lock
;
1956 (*db
)->hdb_unlock
= LDAP_unlock
;
1957 (*db
)->hdb_rename
= NULL
;
1958 (*db
)->hdb__get
= NULL
;
1959 (*db
)->hdb__put
= NULL
;
1960 (*db
)->hdb__del
= NULL
;
1961 (*db
)->hdb_destroy
= LDAP_destroy
;
1967 hdb_ldap_create(krb5_context context
, HDB
** db
, const char *arg
)
1969 return hdb_ldap_common(context
, db
, arg
, NULL
);
1973 hdb_ldapi_create(krb5_context context
, HDB
** db
, const char *arg
)
1975 krb5_error_code ret
;
1976 char *search_base
, *p
;
1978 asprintf(&p
, "ldapi:%s", arg
);
1981 krb5_set_error_message(context
, ENOMEM
, "out of memory");
1984 search_base
= strchr(p
+ strlen("ldapi://"), ':');
1985 if (search_base
== NULL
) {
1987 krb5_set_error_message(context
, HDB_ERR_BADVERSION
,
1988 "search base missing");
1989 return HDB_ERR_BADVERSION
;
1991 *search_base
= '\0';
1994 ret
= hdb_ldap_common(context
, db
, search_base
, p
);
1999 #ifdef OPENLDAP_MODULE
2002 static krb5_error_code KRB5_LIB_CALL
*
2003 init(krb5_context context
, void **ctx
)
2008 static void KRB5_LIB_CALL
*
2013 struct hdb_method hdb_ldap_interface
= {
2014 HDB_INTERFACE_VERSION
,
2021 struct hdb_so_method hdb_ldapi_interface
= {
2022 HDB_INTERFACE_VERSION
,
2031 #endif /* OPENLDAP */