2 Unix SMB/CIFS implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Jean François Micouleau 1998
5 Copyright (C) Gerald Carter 2001-2003
6 Copyright (C) Shahms King 2001
7 Copyright (C) Andrew Bartlett 2002-2003
8 Copyright (C) Stefan (metze) Metzmacher 2002-2003
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #ifndef LDAP_OPT_SUCCESS
30 #define LDAP_OPT_SUCCESS 0
33 /* Try not to hit the up or down server forever */
35 #define SMBLDAP_DONT_PING_TIME 10 /* ping only all 10 seconds */
36 #define SMBLDAP_NUM_RETRIES 8 /* retry only 8 times */
38 #define SMBLDAP_IDLE_TIME 150 /* After 2.5 minutes disconnect */
41 /* attributes used by Samba 2.2 */
43 ATTRIB_MAP_ENTRY attrib_map_v22
[] = {
44 { LDAP_ATTR_UID
, "uid" },
45 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
46 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
47 { LDAP_ATTR_UNIX_HOME
, "homeDirectory" },
48 { LDAP_ATTR_PWD_LAST_SET
, "pwdLastSet" },
49 { LDAP_ATTR_PWD_CAN_CHANGE
, "pwdCanChange" },
50 { LDAP_ATTR_PWD_MUST_CHANGE
, "pwdMustChange" },
51 { LDAP_ATTR_LOGON_TIME
, "logonTime" },
52 { LDAP_ATTR_LOGOFF_TIME
, "logoffTime" },
53 { LDAP_ATTR_KICKOFF_TIME
, "kickoffTime" },
54 { LDAP_ATTR_CN
, "cn" },
55 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
56 { LDAP_ATTR_HOME_PATH
, "smbHome" },
57 { LDAP_ATTR_HOME_DRIVE
, "homeDrive" },
58 { LDAP_ATTR_LOGON_SCRIPT
, "scriptPath" },
59 { LDAP_ATTR_PROFILE_PATH
, "profilePath" },
60 { LDAP_ATTR_DESC
, "description" },
61 { LDAP_ATTR_USER_WKS
, "userWorkstations"},
62 { LDAP_ATTR_USER_RID
, "rid" },
63 { LDAP_ATTR_PRIMARY_GROUP_RID
, "primaryGroupID"},
64 { LDAP_ATTR_LMPW
, "lmPassword" },
65 { LDAP_ATTR_NTPW
, "ntPassword" },
66 { LDAP_ATTR_DOMAIN
, "domain" },
67 { LDAP_ATTR_OBJCLASS
, "objectClass" },
68 { LDAP_ATTR_ACB_INFO
, "acctFlags" },
69 { LDAP_ATTR_MOD_TIMESTAMP
, "modifyTimestamp" },
70 { LDAP_ATTR_LIST_END
, NULL
}
73 ATTRIB_MAP_ENTRY attrib_map_to_delete_v22
[] = {
74 { LDAP_ATTR_PWD_LAST_SET
, "pwdLastSet" },
75 { LDAP_ATTR_PWD_CAN_CHANGE
, "pwdCanChange" },
76 { LDAP_ATTR_PWD_MUST_CHANGE
, "pwdMustChange" },
77 { LDAP_ATTR_LOGON_TIME
, "logonTime" },
78 { LDAP_ATTR_LOGOFF_TIME
, "logoffTime" },
79 { LDAP_ATTR_KICKOFF_TIME
, "kickoffTime" },
80 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
81 { LDAP_ATTR_HOME_PATH
, "smbHome" },
82 { LDAP_ATTR_HOME_DRIVE
, "homeDrives" },
83 { LDAP_ATTR_LOGON_SCRIPT
, "scriptPath" },
84 { LDAP_ATTR_PROFILE_PATH
, "profilePath" },
85 { LDAP_ATTR_USER_WKS
, "userWorkstations"},
86 { LDAP_ATTR_USER_RID
, "rid" },
87 { LDAP_ATTR_PRIMARY_GROUP_RID
, "primaryGroupID"},
88 { LDAP_ATTR_LMPW
, "lmPassword" },
89 { LDAP_ATTR_NTPW
, "ntPassword" },
90 { LDAP_ATTR_DOMAIN
, "domain" },
91 { LDAP_ATTR_ACB_INFO
, "acctFlags" },
92 { LDAP_ATTR_LIST_END
, NULL
}
95 /* attributes used by Samba 3.0's sambaSamAccount */
97 ATTRIB_MAP_ENTRY attrib_map_v30
[] = {
98 { LDAP_ATTR_UID
, "uid" },
99 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
100 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
101 { LDAP_ATTR_UNIX_HOME
, "homeDirectory" },
102 { LDAP_ATTR_PWD_LAST_SET
, "sambaPwdLastSet" },
103 { LDAP_ATTR_PWD_CAN_CHANGE
, "sambaPwdCanChange" },
104 { LDAP_ATTR_PWD_MUST_CHANGE
, "sambaPwdMustChange" },
105 { LDAP_ATTR_LOGON_TIME
, "sambaLogonTime" },
106 { LDAP_ATTR_LOGOFF_TIME
, "sambaLogoffTime" },
107 { LDAP_ATTR_KICKOFF_TIME
, "sambaKickoffTime" },
108 { LDAP_ATTR_CN
, "cn" },
109 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
110 { LDAP_ATTR_HOME_DRIVE
, "sambaHomeDrive" },
111 { LDAP_ATTR_HOME_PATH
, "sambaHomePath" },
112 { LDAP_ATTR_LOGON_SCRIPT
, "sambaLogonScript" },
113 { LDAP_ATTR_PROFILE_PATH
, "sambaProfilePath" },
114 { LDAP_ATTR_DESC
, "description" },
115 { LDAP_ATTR_USER_WKS
, "sambaUserWorkstations" },
116 { LDAP_ATTR_USER_SID
, LDAP_ATTRIBUTE_SID
},
117 { LDAP_ATTR_PRIMARY_GROUP_SID
, "sambaPrimaryGroupSID" },
118 { LDAP_ATTR_LMPW
, "sambaLMPassword" },
119 { LDAP_ATTR_NTPW
, "sambaNTPassword" },
120 { LDAP_ATTR_DOMAIN
, "sambaDomainName" },
121 { LDAP_ATTR_OBJCLASS
, "objectClass" },
122 { LDAP_ATTR_ACB_INFO
, "sambaAcctFlags" },
123 { LDAP_ATTR_MUNGED_DIAL
, "sambaMungedDial" },
124 { LDAP_ATTR_BAD_PASSWORD_COUNT
, "sambaBadPasswordCount" },
125 { LDAP_ATTR_BAD_PASSWORD_TIME
, "sambaBadPasswordTime" },
126 { LDAP_ATTR_PWD_HISTORY
, "sambaPasswordHistory" },
127 { LDAP_ATTR_MOD_TIMESTAMP
, "modifyTimestamp" },
128 { LDAP_ATTR_LOGON_HOURS
, "sambaLogonHours" },
129 { LDAP_ATTR_LIST_END
, NULL
}
132 ATTRIB_MAP_ENTRY attrib_map_to_delete_v30
[] = {
133 { LDAP_ATTR_PWD_LAST_SET
, "sambaPwdLastSet" },
134 { LDAP_ATTR_PWD_CAN_CHANGE
, "sambaPwdCanChange" },
135 { LDAP_ATTR_PWD_MUST_CHANGE
, "sambaPwdMustChange" },
136 { LDAP_ATTR_LOGON_TIME
, "sambaLogonTime" },
137 { LDAP_ATTR_LOGOFF_TIME
, "sambaLogoffTime" },
138 { LDAP_ATTR_KICKOFF_TIME
, "sambaKickoffTime" },
139 { LDAP_ATTR_HOME_DRIVE
, "sambaHomeDrive" },
140 { LDAP_ATTR_HOME_PATH
, "sambaHomePath" },
141 { LDAP_ATTR_LOGON_SCRIPT
, "sambaLogonScript" },
142 { LDAP_ATTR_PROFILE_PATH
, "sambaProfilePath" },
143 { LDAP_ATTR_USER_WKS
, "sambaUserWorkstations" },
144 { LDAP_ATTR_USER_SID
, LDAP_ATTRIBUTE_SID
},
145 { LDAP_ATTR_PRIMARY_GROUP_SID
, "sambaPrimaryGroupSID" },
146 { LDAP_ATTR_LMPW
, "sambaLMPassword" },
147 { LDAP_ATTR_NTPW
, "sambaNTPassword" },
148 { LDAP_ATTR_DOMAIN
, "sambaDomainName" },
149 { LDAP_ATTR_ACB_INFO
, "sambaAcctFlags" },
150 { LDAP_ATTR_MUNGED_DIAL
, "sambaMungedDial" },
151 { LDAP_ATTR_BAD_PASSWORD_COUNT
, "sambaBadPasswordCount" },
152 { LDAP_ATTR_BAD_PASSWORD_TIME
, "sambaBadPasswordTime" },
153 { LDAP_ATTR_PWD_HISTORY
, "sambaPasswordHistory" },
154 { LDAP_ATTR_LOGON_HOURS
, "sambaLogonHours" },
155 { LDAP_ATTR_LIST_END
, NULL
}
158 /* attributes used for allocating RIDs */
160 ATTRIB_MAP_ENTRY dominfo_attr_list
[] = {
161 { LDAP_ATTR_DOMAIN
, "sambaDomainName" },
162 { LDAP_ATTR_NEXT_RID
, "sambaNextRid" },
163 { LDAP_ATTR_NEXT_USERRID
, "sambaNextUserRid" },
164 { LDAP_ATTR_NEXT_GROUPRID
, "sambaNextGroupRid" },
165 { LDAP_ATTR_DOM_SID
, LDAP_ATTRIBUTE_SID
},
166 { LDAP_ATTR_ALGORITHMIC_RID_BASE
,"sambaAlgorithmicRidBase"},
167 { LDAP_ATTR_OBJCLASS
, "objectClass" },
168 { LDAP_ATTR_LIST_END
, NULL
},
171 /* Samba 3.0 group mapping attributes */
173 ATTRIB_MAP_ENTRY groupmap_attr_list
[] = {
174 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
175 { LDAP_ATTR_GROUP_SID
, LDAP_ATTRIBUTE_SID
},
176 { LDAP_ATTR_GROUP_TYPE
, "sambaGroupType" },
177 { LDAP_ATTR_SID_LIST
, "sambaSIDList" },
178 { LDAP_ATTR_DESC
, "description" },
179 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
180 { LDAP_ATTR_CN
, "cn" },
181 { LDAP_ATTR_OBJCLASS
, "objectClass" },
182 { LDAP_ATTR_LIST_END
, NULL
}
185 ATTRIB_MAP_ENTRY groupmap_attr_list_to_delete
[] = {
186 { LDAP_ATTR_GROUP_SID
, LDAP_ATTRIBUTE_SID
},
187 { LDAP_ATTR_GROUP_TYPE
, "sambaGroupType" },
188 { LDAP_ATTR_DESC
, "description" },
189 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
190 { LDAP_ATTR_SID_LIST
, "sambaSIDList" },
191 { LDAP_ATTR_LIST_END
, NULL
}
194 /* idmap_ldap sambaUnixIdPool */
196 ATTRIB_MAP_ENTRY idpool_attr_list
[] = {
197 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
198 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
199 { LDAP_ATTR_OBJCLASS
, "objectClass" },
200 { LDAP_ATTR_LIST_END
, NULL
}
203 ATTRIB_MAP_ENTRY sidmap_attr_list
[] = {
204 { LDAP_ATTR_SID
, LDAP_ATTRIBUTE_SID
},
205 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
206 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
207 { LDAP_ATTR_OBJCLASS
, "objectClass" },
208 { LDAP_ATTR_LIST_END
, NULL
}
211 /**********************************************************************
212 perform a simple table lookup and return the attribute name
213 **********************************************************************/
215 const char* get_attr_key2string( ATTRIB_MAP_ENTRY table
[], int key
)
219 while ( table
[i
].attrib
!= LDAP_ATTR_LIST_END
) {
220 if ( table
[i
].attrib
== key
)
221 return table
[i
].name
;
229 /**********************************************************************
230 Return the list of attribute names from a mapping table
231 **********************************************************************/
233 const char** get_attr_list( TALLOC_CTX
*mem_ctx
, ATTRIB_MAP_ENTRY table
[] )
238 while ( table
[i
].attrib
!= LDAP_ATTR_LIST_END
)
242 names
= TALLOC_ARRAY( mem_ctx
, const char*, i
);
244 DEBUG(0,("get_attr_list: out of memory\n"));
249 while ( table
[i
].attrib
!= LDAP_ATTR_LIST_END
) {
250 names
[i
] = talloc_strdup( names
, table
[i
].name
);
258 /*******************************************************************
259 Search an attribute and return the first value found.
260 ******************************************************************/
262 BOOL
smbldap_get_single_attribute (LDAP
* ldap_struct
, LDAPMessage
* entry
,
263 const char *attribute
, char *value
,
273 if ((values
= ldap_get_values (ldap_struct
, entry
, attribute
)) == NULL
) {
274 DEBUG (10, ("smbldap_get_single_attribute: [%s] = [<does not exist>]\n", attribute
));
279 if (convert_string(CH_UTF8
, CH_UNIX
,values
[0], -1, value
, max_len
, False
) == (size_t)-1) {
280 DEBUG(1, ("smbldap_get_single_attribute: string conversion of [%s] = [%s] failed!\n",
281 attribute
, values
[0]));
282 ldap_value_free(values
);
286 ldap_value_free(values
);
287 #ifdef DEBUG_PASSWORDS
288 DEBUG (100, ("smbldap_get_single_attribute: [%s] = [%s]\n", attribute
, value
));
293 BOOL
smbldap_get_single_pstring (LDAP
* ldap_struct
, LDAPMessage
* entry
,
294 const char *attribute
, pstring value
)
296 return smbldap_get_single_attribute(ldap_struct
, entry
,
301 char * smbldap_talloc_single_attribute(LDAP
*ldap_struct
, LDAPMessage
*entry
,
302 const char *attribute
,
308 if (attribute
== NULL
) {
312 values
= ldap_get_values(ldap_struct
, entry
, attribute
);
314 if (values
== NULL
) {
315 DEBUG(10, ("attribute %s does not exist\n", attribute
));
319 if (ldap_count_values(values
) != 1) {
320 DEBUG(10, ("attribute %s has %d values, expected only one\n",
321 attribute
, ldap_count_values(values
)));
322 ldap_value_free(values
);
326 if (pull_utf8_talloc(mem_ctx
, &result
, values
[0]) < 0) {
327 DEBUG(10, ("pull_utf8_talloc failed\n"));
328 ldap_value_free(values
);
332 ldap_value_free(values
);
334 #ifdef DEBUG_PASSWORDS
335 DEBUG (100, ("smbldap_get_single_attribute: [%s] = [%s]\n",
341 static int ldapmsg_destructor(void *p
) {
342 LDAPMessage
**result
= talloc_get_type_abort(p
, LDAPMessage
*);
343 ldap_msgfree(*result
);
347 void talloc_autofree_ldapmsg(TALLOC_CTX
*mem_ctx
, LDAPMessage
*result
)
349 LDAPMessage
**handle
;
351 if (result
== NULL
) {
355 handle
= TALLOC_P(mem_ctx
, LDAPMessage
*);
356 SMB_ASSERT(handle
!= NULL
);
359 talloc_set_destructor(handle
, ldapmsg_destructor
);
362 static int ldapmod_destructor(void *p
) {
363 LDAPMod
***result
= talloc_get_type_abort(p
, LDAPMod
**);
364 ldap_mods_free(*result
, True
);
368 void talloc_autofree_ldapmod(TALLOC_CTX
*mem_ctx
, LDAPMod
**mod
)
376 handle
= TALLOC_P(mem_ctx
, LDAPMod
**);
377 SMB_ASSERT(handle
!= NULL
);
380 talloc_set_destructor(handle
, ldapmod_destructor
);
383 /************************************************************************
384 Routine to manage the LDAPMod structure array
385 manage memory used by the array, by each struct, and values
386 ***********************************************************************/
388 void smbldap_set_mod (LDAPMod
*** modlist
, int modop
, const char *attribute
, const char *value
)
396 /* sanity checks on the mod values */
398 if (attribute
== NULL
|| *attribute
== '\0') {
402 #if 0 /* commented out after discussion with abartlet. Do not reenable.
403 left here so other do not re-add similar code --jerry */
404 if (value
== NULL
|| *value
== '\0')
409 mods
= SMB_MALLOC_P(LDAPMod
*);
411 smb_panic("smbldap_set_mod: out of memory!\n");
418 for (i
= 0; mods
[i
] != NULL
; ++i
) {
419 if (mods
[i
]->mod_op
== modop
&& strequal(mods
[i
]->mod_type
, attribute
))
423 if (mods
[i
] == NULL
) {
424 mods
= SMB_REALLOC_ARRAY (mods
, LDAPMod
*, i
+ 2);
426 smb_panic("smbldap_set_mod: out of memory!\n");
430 mods
[i
] = SMB_MALLOC_P(LDAPMod
);
431 if (mods
[i
] == NULL
) {
432 smb_panic("smbldap_set_mod: out of memory!\n");
436 mods
[i
]->mod_op
= modop
;
437 mods
[i
]->mod_values
= NULL
;
438 mods
[i
]->mod_type
= SMB_STRDUP(attribute
);
443 char *utf8_value
= NULL
;
446 if (mods
[i
]->mod_values
!= NULL
) {
447 for (; mods
[i
]->mod_values
[j
] != NULL
; j
++);
449 mods
[i
]->mod_values
= SMB_REALLOC_ARRAY(mods
[i
]->mod_values
, char *, j
+ 2);
451 if (mods
[i
]->mod_values
== NULL
) {
452 smb_panic("smbldap_set_mod: out of memory!\n");
457 if (push_utf8_allocate(&utf8_value
, value
) == (size_t)-1) {
458 smb_panic("smbldap_set_mod: String conversion failure!\n");
463 mods
[i
]->mod_values
[j
] = utf8_value
;
465 mods
[i
]->mod_values
[j
+ 1] = NULL
;
470 /**********************************************************************
471 Set attribute to newval in LDAP, regardless of what value the
472 attribute had in LDAP before.
473 *********************************************************************/
475 void smbldap_make_mod(LDAP
*ldap_struct
, LDAPMessage
*existing
,
477 const char *attribute
, const char *newval
)
479 char oldval
[2048]; /* current largest allowed value is mungeddial */
482 if (attribute
== NULL
) {
483 /* This can actually happen for ldapsam_compat where we for
484 * example don't have a password history */
488 if (existing
!= NULL
) {
489 existed
= smbldap_get_single_attribute(ldap_struct
, existing
, attribute
, oldval
, sizeof(oldval
));
495 /* all of our string attributes are case insensitive */
497 if (existed
&& newval
&& (StrCaseCmp(oldval
, newval
) == 0)) {
499 /* Believe it or not, but LDAP will deny a delete and
500 an add at the same time if the values are the
502 DEBUG(10,("smbldap_make_mod: attribute |%s| not changed.\n", attribute
));
507 /* There has been no value before, so don't delete it.
508 * Here's a possible race: We might end up with
509 * duplicate attributes */
510 /* By deleting exactly the value we found in the entry this
511 * should be race-free in the sense that the LDAP-Server will
512 * deny the complete operation if somebody changed the
513 * attribute behind our back. */
514 /* This will also allow modifying single valued attributes
515 * in Novell NDS. In NDS you have to first remove attribute and then
516 * you could add new value */
518 DEBUG(10,("smbldap_make_mod: deleting attribute |%s| values |%s|\n", attribute
, oldval
));
519 smbldap_set_mod(mods
, LDAP_MOD_DELETE
, attribute
, oldval
);
522 /* Regardless of the real operation (add or modify)
523 we add the new value here. We rely on deleting
524 the old value, should it exist. */
526 if ((newval
!= NULL
) && (strlen(newval
) > 0)) {
527 DEBUG(10,("smbldap_make_mod: adding attribute |%s| value |%s|\n", attribute
, newval
));
528 smbldap_set_mod(mods
, LDAP_MOD_ADD
, attribute
, newval
);
532 /**********************************************************************
533 Some varients of the LDAP rebind code do not pass in the third 'arg'
534 pointer to a void*, so we try and work around it by assuming that the
535 value of the 'LDAP *' pointer is the same as the one we had passed in
536 **********************************************************************/
538 struct smbldap_state_lookup
{
540 struct smbldap_state
*smbldap_state
;
541 struct smbldap_state_lookup
*prev
, *next
;
544 static struct smbldap_state_lookup
*smbldap_state_lookup_list
;
546 static struct smbldap_state
*smbldap_find_state(LDAP
*ld
)
548 struct smbldap_state_lookup
*t
;
550 for (t
= smbldap_state_lookup_list
; t
; t
= t
->next
) {
552 return t
->smbldap_state
;
558 static void smbldap_delete_state(struct smbldap_state
*smbldap_state
)
560 struct smbldap_state_lookup
*t
;
562 for (t
= smbldap_state_lookup_list
; t
; t
= t
->next
) {
563 if (t
->smbldap_state
== smbldap_state
) {
564 DLIST_REMOVE(smbldap_state_lookup_list
, t
);
571 static void smbldap_store_state(LDAP
*ld
, struct smbldap_state
*smbldap_state
)
573 struct smbldap_state
*tmp_ldap_state
;
574 struct smbldap_state_lookup
*t
;
575 struct smbldap_state_lookup
*tmp
;
577 if ((tmp_ldap_state
= smbldap_find_state(ld
))) {
578 SMB_ASSERT(tmp_ldap_state
== smbldap_state
);
582 t
= SMB_XMALLOC_P(struct smbldap_state_lookup
);
585 DLIST_ADD_END(smbldap_state_lookup_list
, t
, tmp
);
587 t
->smbldap_state
= smbldap_state
;
590 /********************************************************************
591 start TLS on an existing LDAP connection
592 *******************************************************************/
594 int smb_ldap_start_tls(LDAP
*ldap_struct
, int version
)
598 if (lp_ldap_ssl() != LDAP_SSL_START_TLS
) {
602 #ifdef LDAP_OPT_X_TLS
603 if (version
!= LDAP_VERSION3
) {
604 DEBUG(0, ("Need LDAPv3 for Start TLS\n"));
605 return LDAP_OPERATIONS_ERROR
;
608 if ((rc
= ldap_start_tls_s (ldap_struct
, NULL
, NULL
)) != LDAP_SUCCESS
) {
609 DEBUG(0,("Failed to issue the StartTLS instruction: %s\n",
610 ldap_err2string(rc
)));
614 DEBUG (3, ("StartTLS issued: using a TLS connection\n"));
617 DEBUG(0,("StartTLS not supported by LDAP client libraries!\n"));
618 return LDAP_OPERATIONS_ERROR
;
622 /********************************************************************
623 setup a connection to the LDAP server based on a uri
624 *******************************************************************/
626 int smb_ldap_setup_conn(LDAP
**ldap_struct
, const char *uri
)
630 DEBUG(10, ("smb_ldap_setup_connection: %s\n", uri
));
632 #ifdef HAVE_LDAP_INITIALIZE
634 rc
= ldap_initialize(ldap_struct
, uri
);
636 DEBUG(0, ("ldap_initialize: %s\n", ldap_err2string(rc
)));
642 /* Parse the string manually */
648 SMB_ASSERT(sizeof(protocol
)>10 && sizeof(host
)>254);
651 /* skip leading "URL:" (if any) */
652 if ( strnequal( uri
, "URL:", 4 ) ) {
656 sscanf(uri
, "%10[^:]://%254[^:/]:%d", protocol
, host
, &port
);
659 if (strequal(protocol
, "ldap")) {
661 } else if (strequal(protocol
, "ldaps")) {
664 DEBUG(0, ("unrecognised protocol (%s)!\n", protocol
));
668 if ((*ldap_struct
= ldap_init(host
, port
)) == NULL
) {
669 DEBUG(0, ("ldap_init failed !\n"));
670 return LDAP_OPERATIONS_ERROR
;
673 if (strequal(protocol
, "ldaps")) {
674 #ifdef LDAP_OPT_X_TLS
675 int tls
= LDAP_OPT_X_TLS_HARD
;
676 if (ldap_set_option (*ldap_struct
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
678 DEBUG(0, ("Failed to setup a TLS session\n"));
681 DEBUG(3,("LDAPS option set...!\n"));
683 DEBUG(0,("smbldap_open_connection: Secure connection not supported by LDAP client libraries!\n"));
684 return LDAP_OPERATIONS_ERROR
;
685 #endif /* LDAP_OPT_X_TLS */
689 #endif /* HAVE_LDAP_INITIALIZE */
693 /********************************************************************
694 try to upgrade to Version 3 LDAP if not already, in either case return current
696 *******************************************************************/
698 int smb_ldap_upgrade_conn(LDAP
*ldap_struct
, int *new_version
)
703 /* assume the worst */
704 *new_version
= LDAP_VERSION2
;
706 rc
= ldap_get_option(ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
711 if (version
== LDAP_VERSION3
) {
712 *new_version
= LDAP_VERSION3
;
717 version
= LDAP_VERSION3
;
718 rc
= ldap_set_option (ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
723 *new_version
= LDAP_VERSION3
;
727 /*******************************************************************
728 open a connection to the ldap server (just until the bind)
729 ******************************************************************/
731 int smb_ldap_setup_full_conn(LDAP
**ldap_struct
, const char *uri
)
735 rc
= smb_ldap_setup_conn(ldap_struct
, uri
);
740 rc
= smb_ldap_upgrade_conn(*ldap_struct
, &version
);
745 rc
= smb_ldap_start_tls(*ldap_struct
, version
);
753 /*******************************************************************
754 open a connection to the ldap server.
755 ******************************************************************/
756 static int smbldap_open_connection (struct smbldap_state
*ldap_state
)
759 int rc
= LDAP_SUCCESS
;
761 LDAP
**ldap_struct
= &ldap_state
->ldap_struct
;
763 rc
= smb_ldap_setup_conn(ldap_struct
, ldap_state
->uri
);
768 /* Store the LDAP pointer in a lookup list */
770 smbldap_store_state(*ldap_struct
, ldap_state
);
772 /* Upgrade to LDAPv3 if possible */
774 rc
= smb_ldap_upgrade_conn(*ldap_struct
, &version
);
779 /* Start TLS if required */
781 rc
= smb_ldap_start_tls(*ldap_struct
, version
);
786 DEBUG(2, ("smbldap_open_connection: connection opened\n"));
790 /*******************************************************************
791 a rebind function for authenticated referrals
792 This version takes a void* that we can shove useful stuff in :-)
793 ******************************************************************/
794 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
796 static int rebindproc_with_state (LDAP
* ld
, char **whop
, char **credp
,
797 int *methodp
, int freeit
, void *arg
)
799 struct smbldap_state
*ldap_state
= arg
;
801 /** @TODO Should we be doing something to check what servers we rebind to?
802 Could we get a referral to a machine that we don't want to give our
803 username and password to? */
807 memset(*credp
, '\0', strlen(*credp
));
810 DEBUG(5,("rebind_proc_with_state: Rebinding as \"%s\"\n",
811 ldap_state
->bind_dn
));
813 *whop
= SMB_STRDUP(ldap_state
->bind_dn
);
815 return LDAP_NO_MEMORY
;
817 *credp
= SMB_STRDUP(ldap_state
->bind_secret
);
820 return LDAP_NO_MEMORY
;
822 *methodp
= LDAP_AUTH_SIMPLE
;
825 GetTimeOfDay(&ldap_state
->last_rebind
);
829 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
831 /*******************************************************************
832 a rebind function for authenticated referrals
833 This version takes a void* that we can shove useful stuff in :-)
834 and actually does the connection.
835 ******************************************************************/
836 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
837 static int rebindproc_connect_with_state (LDAP
*ldap_struct
,
838 LDAP_CONST
char *url
,
840 ber_int_t msgid
, void *arg
)
842 struct smbldap_state
*ldap_state
= arg
;
846 DEBUG(5,("rebindproc_connect_with_state: Rebinding to %s as \"%s\"\n",
847 url
, ldap_state
->bind_dn
));
849 /* call START_TLS again (ldaps:// is handled by the OpenLDAP library
850 * itself) before rebinding to another LDAP server to avoid to expose
851 * our credentials. At least *try* to secure the connection - Guenther */
853 smb_ldap_upgrade_conn(ldap_struct
, &version
);
854 smb_ldap_start_tls(ldap_struct
, version
);
856 /** @TODO Should we be doing something to check what servers we rebind to?
857 Could we get a referral to a machine that we don't want to give our
858 username and password to? */
860 rc
= ldap_simple_bind_s(ldap_struct
, ldap_state
->bind_dn
, ldap_state
->bind_secret
);
862 /* only set the last rebind timestamp when we did rebind after a
863 * non-read LDAP operation. That way we avoid the replication sleep
864 * after a simple redirected search operation - Guenther */
868 case LDAP_REQ_MODIFY
:
870 case LDAP_REQ_DELETE
:
872 case LDAP_REQ_EXTENDED
:
873 DEBUG(10,("rebindproc_connect_with_state: "
874 "setting last_rebind timestamp "
875 "(req: 0x%02x)\n", request
));
876 GetTimeOfDay(&ldap_state
->last_rebind
);
879 ZERO_STRUCT(ldap_state
->last_rebind
);
885 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
887 /*******************************************************************
888 Add a rebind function for authenticated referrals
889 ******************************************************************/
890 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
892 # if LDAP_SET_REBIND_PROC_ARGS == 2
893 static int rebindproc (LDAP
*ldap_struct
, char **whop
, char **credp
,
894 int *method
, int freeit
)
896 struct smbldap_state
*ldap_state
= smbldap_find_state(ldap_struct
);
898 return rebindproc_with_state(ldap_struct
, whop
, credp
,
899 method
, freeit
, ldap_state
);
902 # endif /*LDAP_SET_REBIND_PROC_ARGS == 2*/
903 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
905 /*******************************************************************
906 a rebind function for authenticated referrals
907 this also does the connection, but no void*.
908 ******************************************************************/
909 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
910 # if LDAP_SET_REBIND_PROC_ARGS == 2
911 static int rebindproc_connect (LDAP
* ld
, LDAP_CONST
char *url
, int request
,
914 struct smbldap_state
*ldap_state
= smbldap_find_state(ld
);
916 return rebindproc_connect_with_state(ld
, url
, (ber_tag_t
)request
, msgid
,
919 # endif /*LDAP_SET_REBIND_PROC_ARGS == 2*/
920 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
922 /*******************************************************************
923 connect to the ldap server under system privilege.
924 ******************************************************************/
925 static int smbldap_connect_system(struct smbldap_state
*ldap_state
, LDAP
* ldap_struct
)
932 /* get the password */
933 if (!fetch_ldap_pw(&ldap_dn
, &ldap_secret
)) {
934 DEBUG(0, ("ldap_connect_system: Failed to retrieve password from secrets.tdb\n"));
935 return LDAP_INVALID_CREDENTIALS
;
938 ldap_state
->bind_dn
= ldap_dn
;
939 ldap_state
->bind_secret
= ldap_secret
;
941 /* removed the sasl_bind_s "EXTERNAL" stuff, as my testsuite
942 (OpenLDAP) doesnt' seem to support it */
944 DEBUG(10,("ldap_connect_system: Binding to ldap server %s as \"%s\"\n",
945 ldap_state
->uri
, ldap_dn
));
947 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
948 # if LDAP_SET_REBIND_PROC_ARGS == 2
949 ldap_set_rebind_proc(ldap_struct
, &rebindproc_connect
);
951 # if LDAP_SET_REBIND_PROC_ARGS == 3
952 ldap_set_rebind_proc(ldap_struct
, &rebindproc_connect_with_state
, (void *)ldap_state
);
954 #else /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
955 # if LDAP_SET_REBIND_PROC_ARGS == 2
956 ldap_set_rebind_proc(ldap_struct
, &rebindproc
);
958 # if LDAP_SET_REBIND_PROC_ARGS == 3
959 ldap_set_rebind_proc(ldap_struct
, &rebindproc_with_state
, (void *)ldap_state
);
961 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
963 rc
= ldap_simple_bind_s(ldap_struct
, ldap_dn
, ldap_secret
);
965 if (rc
!= LDAP_SUCCESS
) {
966 char *ld_error
= NULL
;
967 ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
969 DEBUG(ldap_state
->num_failures
? 2 : 0,
970 ("failed to bind to server %s with dn=\"%s\" Error: %s\n\t%s\n",
972 ldap_dn
? ldap_dn
: "(unknown)", ldap_err2string(rc
),
973 ld_error
? ld_error
: "(unknown)"));
975 ldap_state
->num_failures
++;
979 ldap_state
->num_failures
= 0;
980 ldap_state
->paged_results
= False
;
982 ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
984 if (smbldap_has_control(ldap_state
, ADS_PAGE_CTL_OID
) && version
== 3) {
985 ldap_state
->paged_results
= True
;
988 DEBUG(3, ("ldap_connect_system: succesful connection to the LDAP server\n"));
989 DEBUGADD(10, ("ldap_connect_system: LDAP server %s support paged results\n",
990 ldap_state
->paged_results
? "does" : "does not"));
994 /**********************************************************************
995 Connect to LDAP server (called before every ldap operation)
996 *********************************************************************/
997 static int smbldap_open(struct smbldap_state
*ldap_state
)
1000 BOOL reopen
= False
;
1001 SMB_ASSERT(ldap_state
);
1003 #ifndef NO_LDAP_SECURITY
1004 if (geteuid() != 0) {
1005 DEBUG(0, ("smbldap_open: cannot access LDAP when not root..\n"));
1006 return LDAP_INSUFFICIENT_ACCESS
;
1010 if ((ldap_state
->ldap_struct
!= NULL
) && ((ldap_state
->last_ping
+ SMBLDAP_DONT_PING_TIME
) < time(NULL
))) {
1012 struct sockaddr_un addr
;
1013 socklen_t len
= sizeof(addr
);
1016 opt_rc
= ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_DESC
, &sd
);
1017 if (opt_rc
== 0 && (getpeername(sd
, (struct sockaddr
*) &addr
, &len
)) < 0 )
1020 #ifdef HAVE_UNIXSOCKET
1021 if (opt_rc
== 0 && addr
.sun_family
== AF_UNIX
)
1025 /* the other end has died. reopen. */
1026 ldap_unbind(ldap_state
->ldap_struct
);
1027 ldap_state
->ldap_struct
= NULL
;
1028 ldap_state
->last_ping
= (time_t)0;
1030 ldap_state
->last_ping
= time(NULL
);
1034 if (ldap_state
->ldap_struct
!= NULL
) {
1035 DEBUG(11,("smbldap_open: already connected to the LDAP server\n"));
1036 return LDAP_SUCCESS
;
1039 if ((rc
= smbldap_open_connection(ldap_state
))) {
1043 if ((rc
= smbldap_connect_system(ldap_state
, ldap_state
->ldap_struct
))) {
1044 ldap_unbind(ldap_state
->ldap_struct
);
1045 ldap_state
->ldap_struct
= NULL
;
1050 ldap_state
->last_ping
= time(NULL
);
1051 ldap_state
->pid
= sys_getpid();
1052 DEBUG(4,("The LDAP server is succesfully connected\n"));
1054 return LDAP_SUCCESS
;
1057 /**********************************************************************
1058 Disconnect from LDAP server
1059 *********************************************************************/
1060 static NTSTATUS
smbldap_close(struct smbldap_state
*ldap_state
)
1063 return NT_STATUS_INVALID_PARAMETER
;
1065 if (ldap_state
->ldap_struct
!= NULL
) {
1066 ldap_unbind(ldap_state
->ldap_struct
);
1067 ldap_state
->ldap_struct
= NULL
;
1070 smbldap_delete_state(ldap_state
);
1072 DEBUG(5,("The connection to the LDAP server was closed\n"));
1073 /* maybe free the results here --metze */
1077 return NT_STATUS_OK
;
1080 static BOOL got_alarm
;
1082 static void (*old_handler
)(int);
1084 static void gotalarm_sig(int dummy
)
1089 static int another_ldap_try(struct smbldap_state
*ldap_state
, int *rc
,
1090 int *attempts
, time_t endtime
)
1092 time_t now
= time(NULL
);
1093 int open_rc
= LDAP_SERVER_DOWN
;
1095 if (*rc
!= LDAP_SERVER_DOWN
)
1098 if (now
>= endtime
) {
1099 smbldap_close(ldap_state
);
1104 if (*attempts
== 0) {
1106 old_handler
= CatchSignal(SIGALRM
, gotalarm_sig
);
1107 alarm(endtime
- now
);
1109 if (ldap_state
->pid
!= sys_getpid())
1110 smbldap_close(ldap_state
);
1120 open_rc
= smbldap_open(ldap_state
);
1122 if (open_rc
== LDAP_SUCCESS
) {
1123 ldap_state
->last_use
= now
;
1127 if (open_rc
== LDAP_INSUFFICIENT_ACCESS
) {
1128 /* The fact that we are non-root or any other
1129 * access-denied condition will not change in the next
1130 * round of trying */
1140 if (open_rc
!= LDAP_SUCCESS
) {
1141 DEBUG(1, ("Connection to LDAP server failed for the "
1142 "%d try!\n", *attempts
));
1147 CatchSignal(SIGALRM
, old_handler
);
1149 ldap_state
->last_use
= now
;
1153 /*********************************************************************
1154 ********************************************************************/
1156 static int smbldap_search_ext(struct smbldap_state
*ldap_state
,
1157 const char *base
, int scope
, const char *filter
,
1158 const char *attrs
[], int attrsonly
,
1159 LDAPControl
**sctrls
, LDAPControl
**cctrls
,
1160 int sizelimit
, LDAPMessage
**res
)
1162 int rc
= LDAP_SERVER_DOWN
;
1165 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1166 struct timeval timeout
;
1168 SMB_ASSERT(ldap_state
);
1170 DEBUG(5,("smbldap_search_ext: base => [%s], filter => [%s], "
1171 "scope => [%d]\n", base
, filter
, scope
));
1173 if (ldap_state
->last_rebind
.tv_sec
> 0) {
1174 struct timeval tval
;
1175 SMB_BIG_INT tdiff
= 0;
1179 GetTimeOfDay(&tval
);
1181 tdiff
= usec_time_diff(&tval
, &ldap_state
->last_rebind
);
1182 tdiff
/= 1000; /* Convert to milliseconds. */
1184 sleep_time
= lp_ldap_replication_sleep()-(int)tdiff
;
1185 sleep_time
= MIN(sleep_time
, MAX_LDAP_REPLICATION_SLEEP_TIME
);
1187 if (sleep_time
> 0) {
1188 /* we wait for the LDAP replication */
1189 DEBUG(5,("smbldap_search_ext: waiting %d milliseconds "
1190 "for LDAP replication.\n",sleep_time
));
1191 smb_msleep(sleep_time
);
1192 DEBUG(5,("smbldap_search_ext: go on!\n"));
1194 ZERO_STRUCT(ldap_state
->last_rebind
);
1197 if (push_utf8_allocate(&utf8_filter
, filter
) == (size_t)-1) {
1198 return LDAP_NO_MEMORY
;
1201 /* Setup timeout for the ldap_search_ext_s call - local and remote. */
1202 timeout
.tv_sec
= lp_ldap_timeout();
1203 timeout
.tv_usec
= 0;
1205 /* Setup alarm timeout.... Do we need both of these ? JRA.
1206 * Yes, I think we do need both of these. The server timeout only
1207 * covers the case where the server's operation takes too long. It
1208 * does not cover the case where the request hangs on its way to the
1209 * server. The server side timeout is not strictly necessary, it's
1210 * just a bit more kind to the server. VL. */
1213 CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
1214 alarm(lp_ldap_timeout());
1215 /* End setup timeout. */
1217 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1218 rc
= ldap_search_ext_s(ldap_state
->ldap_struct
, base
, scope
,
1220 CONST_DISCARD(char **, attrs
),
1221 attrsonly
, sctrls
, cctrls
, &timeout
,
1223 if (rc
!= LDAP_SUCCESS
) {
1224 char *ld_error
= NULL
;
1225 ldap_get_option(ldap_state
->ldap_struct
,
1226 LDAP_OPT_ERROR_STRING
, &ld_error
);
1227 DEBUG(10,("Failed search for base: %s, error: %s "
1228 "(%s)\n", base
, ldap_err2string(rc
),
1229 ld_error
? ld_error
: "unknown"));
1230 SAFE_FREE(ld_error
);
1234 SAFE_FREE(utf8_filter
);
1236 /* Teardown timeout. */
1237 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
1241 return LDAP_TIMELIMIT_EXCEEDED
;
1246 int smbldap_search(struct smbldap_state
*ldap_state
,
1247 const char *base
, int scope
, const char *filter
,
1248 const char *attrs
[], int attrsonly
,
1251 return smbldap_search_ext(ldap_state
, base
, scope
, filter
, attrs
,
1252 attrsonly
, NULL
, NULL
, LDAP_NO_LIMIT
, res
);
1255 int smbldap_search_paged(struct smbldap_state
*ldap_state
,
1256 const char *base
, int scope
, const char *filter
,
1257 const char **attrs
, int attrsonly
, int pagesize
,
1258 LDAPMessage
**res
, void **cookie
)
1261 LDAPControl
**rcontrols
;
1262 LDAPControl
*controls
[2] = { NULL
, NULL
};
1263 BerElement
*cookie_be
= NULL
;
1264 struct berval
*cookie_bv
= NULL
;
1266 BOOL critical
= True
;
1270 DEBUG(3,("smbldap_search_paged: base => [%s], filter => [%s],"
1271 "scope => [%d], pagesize => [%d]\n",
1272 base
, filter
, scope
, pagesize
));
1274 cookie_be
= ber_alloc_t(LBER_USE_DER
);
1275 if (cookie_be
== NULL
) {
1276 DEBUG(0,("smbldap_create_page_control: ber_alloc_t returns "
1278 return LDAP_NO_MEMORY
;
1281 /* construct cookie */
1282 if (*cookie
!= NULL
) {
1283 ber_printf(cookie_be
, "{iO}", (ber_int_t
) pagesize
, *cookie
);
1284 ber_bvfree(*cookie
); /* don't need it from last time */
1287 ber_printf(cookie_be
, "{io}", (ber_int_t
) pagesize
, "", 0);
1289 ber_flatten(cookie_be
, &cookie_bv
);
1291 pr
.ldctl_oid
= CONST_DISCARD(char *, ADS_PAGE_CTL_OID
);
1292 pr
.ldctl_iscritical
= (char) critical
;
1293 pr
.ldctl_value
.bv_len
= cookie_bv
->bv_len
;
1294 pr
.ldctl_value
.bv_val
= cookie_bv
->bv_val
;
1299 rc
= smbldap_search_ext(ldap_state
, base
, scope
, filter
, attrs
,
1300 0, controls
, NULL
, LDAP_NO_LIMIT
, res
);
1302 ber_free(cookie_be
, 1);
1303 ber_bvfree(cookie_bv
);
1306 DEBUG(3,("smbldap_search_paged: smbldap_search_ext(%s) "
1307 "failed with [%s]\n", filter
, ldap_err2string(rc
)));
1311 DEBUG(3,("smbldap_search_paged: search was successfull\n"));
1313 rc
= ldap_parse_result(ldap_state
->ldap_struct
, *res
, NULL
, NULL
,
1314 NULL
, NULL
, &rcontrols
, 0);
1316 DEBUG(3,("smbldap_search_paged: ldap_parse_result failed " \
1317 "with [%s]\n", ldap_err2string(rc
)));
1321 if (rcontrols
== NULL
)
1324 for (i
=0; rcontrols
[i
]; i
++) {
1326 if (strcmp(ADS_PAGE_CTL_OID
, rcontrols
[i
]->ldctl_oid
) != 0)
1329 cookie_be
= ber_init(&rcontrols
[i
]->ldctl_value
);
1330 ber_scanf(cookie_be
,"{iO}", &tmp
, &cookie_bv
);
1331 /* the berval is the cookie, but must be freed when it is all
1333 if (cookie_bv
->bv_len
)
1334 *cookie
=ber_bvdup(cookie_bv
);
1337 ber_bvfree(cookie_bv
);
1338 ber_free(cookie_be
, 1);
1341 ldap_controls_free(rcontrols
);
1346 int smbldap_modify(struct smbldap_state
*ldap_state
, const char *dn
, LDAPMod
*attrs
[])
1348 int rc
= LDAP_SERVER_DOWN
;
1351 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1353 SMB_ASSERT(ldap_state
);
1355 DEBUG(5,("smbldap_modify: dn => [%s]\n", dn
));
1357 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1358 return LDAP_NO_MEMORY
;
1361 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1362 rc
= ldap_modify_s(ldap_state
->ldap_struct
, utf8_dn
, attrs
);
1363 if (rc
!= LDAP_SUCCESS
) {
1364 char *ld_error
= NULL
;
1365 ldap_get_option(ldap_state
->ldap_struct
,
1366 LDAP_OPT_ERROR_STRING
, &ld_error
);
1367 DEBUG(10,("Failed to modify dn: %s, error: %s "
1368 "(%s)\n", dn
, ldap_err2string(rc
),
1369 ld_error
? ld_error
: "unknown"));
1370 SAFE_FREE(ld_error
);
1378 int smbldap_add(struct smbldap_state
*ldap_state
, const char *dn
, LDAPMod
*attrs
[])
1380 int rc
= LDAP_SERVER_DOWN
;
1383 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1385 SMB_ASSERT(ldap_state
);
1387 DEBUG(5,("smbldap_add: dn => [%s]\n", dn
));
1389 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1390 return LDAP_NO_MEMORY
;
1393 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1394 rc
= ldap_add_s(ldap_state
->ldap_struct
, utf8_dn
, attrs
);
1395 if (rc
!= LDAP_SUCCESS
) {
1396 char *ld_error
= NULL
;
1397 ldap_get_option(ldap_state
->ldap_struct
,
1398 LDAP_OPT_ERROR_STRING
, &ld_error
);
1399 DEBUG(10,("Failed to add dn: %s, error: %s "
1400 "(%s)\n", dn
, ldap_err2string(rc
),
1401 ld_error
? ld_error
: "unknown"));
1402 SAFE_FREE(ld_error
);
1410 int smbldap_delete(struct smbldap_state
*ldap_state
, const char *dn
)
1412 int rc
= LDAP_SERVER_DOWN
;
1415 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1417 SMB_ASSERT(ldap_state
);
1419 DEBUG(5,("smbldap_delete: dn => [%s]\n", dn
));
1421 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1422 return LDAP_NO_MEMORY
;
1425 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1426 rc
= ldap_delete_s(ldap_state
->ldap_struct
, utf8_dn
);
1427 if (rc
!= LDAP_SUCCESS
) {
1428 char *ld_error
= NULL
;
1429 ldap_get_option(ldap_state
->ldap_struct
,
1430 LDAP_OPT_ERROR_STRING
, &ld_error
);
1431 DEBUG(10,("Failed to delete dn: %s, error: %s "
1432 "(%s)\n", dn
, ldap_err2string(rc
),
1433 ld_error
? ld_error
: "unknown"));
1434 SAFE_FREE(ld_error
);
1442 int smbldap_extended_operation(struct smbldap_state
*ldap_state
,
1443 LDAP_CONST
char *reqoid
, struct berval
*reqdata
,
1444 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
,
1445 char **retoidp
, struct berval
**retdatap
)
1447 int rc
= LDAP_SERVER_DOWN
;
1449 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1454 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1455 rc
= ldap_extended_operation_s(ldap_state
->ldap_struct
, reqoid
,
1456 reqdata
, serverctrls
,
1457 clientctrls
, retoidp
, retdatap
);
1458 if (rc
!= LDAP_SUCCESS
) {
1459 char *ld_error
= NULL
;
1460 ldap_get_option(ldap_state
->ldap_struct
,
1461 LDAP_OPT_ERROR_STRING
, &ld_error
);
1462 DEBUG(10,("Extended operation failed with error: %s "
1463 "(%s)\n", ldap_err2string(rc
),
1464 ld_error
? ld_error
: "unknown"));
1465 SAFE_FREE(ld_error
);
1472 /*******************************************************************
1473 run the search by name.
1474 ******************************************************************/
1475 int smbldap_search_suffix (struct smbldap_state
*ldap_state
,
1476 const char *filter
, const char **search_attr
,
1477 LDAPMessage
** result
)
1479 return smbldap_search(ldap_state
, lp_ldap_suffix(), LDAP_SCOPE_SUBTREE
,
1480 filter
, search_attr
, 0, result
);
1483 static void smbldap_idle_fn(void **data
, time_t *interval
, time_t now
)
1485 struct smbldap_state
*state
= (struct smbldap_state
*)(*data
);
1487 if (state
->ldap_struct
== NULL
) {
1488 DEBUG(10,("ldap connection not connected...\n"));
1492 if ((state
->last_use
+SMBLDAP_IDLE_TIME
) > now
) {
1493 DEBUG(10,("ldap connection not idle...\n"));
1497 DEBUG(7,("ldap connection idle...closing connection\n"));
1498 smbldap_close(state
);
1501 /**********************************************************************
1503 *********************************************************************/
1505 void smbldap_free_struct(struct smbldap_state
**ldap_state
)
1507 smbldap_close(*ldap_state
);
1509 if ((*ldap_state
)->bind_secret
) {
1510 memset((*ldap_state
)->bind_secret
, '\0', strlen((*ldap_state
)->bind_secret
));
1513 SAFE_FREE((*ldap_state
)->bind_dn
);
1514 SAFE_FREE((*ldap_state
)->bind_secret
);
1516 smb_unregister_idle_event((*ldap_state
)->event_id
);
1520 /* No need to free any further, as it is talloc()ed */
1524 /**********************************************************************
1525 Intitalise the 'general' ldap structures, on which ldap operations may be conducted
1526 *********************************************************************/
1528 NTSTATUS
smbldap_init(TALLOC_CTX
*mem_ctx
, const char *location
, struct smbldap_state
**smbldap_state
)
1530 *smbldap_state
= TALLOC_ZERO_P(mem_ctx
, struct smbldap_state
);
1531 if (!*smbldap_state
) {
1532 DEBUG(0, ("talloc() failed for ldapsam private_data!\n"));
1533 return NT_STATUS_NO_MEMORY
;
1537 (*smbldap_state
)->uri
= talloc_strdup(mem_ctx
, location
);
1539 (*smbldap_state
)->uri
= "ldap://localhost";
1542 (*smbldap_state
)->event_id
=
1543 smb_register_idle_event(smbldap_idle_fn
, (void *)(*smbldap_state
),
1546 if ((*smbldap_state
)->event_id
== SMB_EVENT_ID_INVALID
) {
1547 DEBUG(0,("Failed to register LDAP idle event!\n"));
1548 return NT_STATUS_INVALID_HANDLE
;
1551 return NT_STATUS_OK
;
1554 /*******************************************************************
1555 Return a copy of the DN for a LDAPMessage. Convert from utf8 to CH_UNIX.
1556 ********************************************************************/
1558 char *smbldap_get_dn(LDAP
*ld
, LDAPMessage
*entry
)
1560 char *utf8_dn
, *unix_dn
;
1562 utf8_dn
= ldap_get_dn(ld
, entry
);
1564 DEBUG (5, ("smbldap_get_dn: ldap_get_dn failed\n"));
1567 if (pull_utf8_allocate(&unix_dn
, utf8_dn
) == (size_t)-1) {
1568 DEBUG (0, ("smbldap_get_dn: String conversion failure utf8 [%s]\n", utf8_dn
));
1571 ldap_memfree(utf8_dn
);
1575 const char *smbldap_talloc_dn(TALLOC_CTX
*mem_ctx
, LDAP
*ld
,
1578 char *utf8_dn
, *unix_dn
;
1580 utf8_dn
= ldap_get_dn(ld
, entry
);
1582 DEBUG (5, ("smbldap_get_dn: ldap_get_dn failed\n"));
1585 if (pull_utf8_talloc(mem_ctx
, &unix_dn
, utf8_dn
) == (size_t)-1) {
1586 DEBUG (0, ("smbldap_get_dn: String conversion failure utf8 "
1587 "[%s]\n", utf8_dn
));
1590 ldap_memfree(utf8_dn
);
1594 /*******************************************************************
1595 Check if root-dse has a certain Control or Extension
1596 ********************************************************************/
1598 static BOOL
smbldap_check_root_dse(struct smbldap_state
*ldap_state
, const char **attrs
, const char *value
)
1600 LDAPMessage
*msg
= NULL
;
1601 LDAPMessage
*entry
= NULL
;
1602 char **values
= NULL
;
1603 int rc
, num_result
, num_values
, i
;
1604 BOOL result
= False
;
1607 DEBUG(3,("smbldap_check_root_dse: nothing to look for\n"));
1611 if (!strequal(attrs
[0], "supportedExtension") &&
1612 !strequal(attrs
[0], "supportedControl") &&
1613 !strequal(attrs
[0], "namingContexts")) {
1614 DEBUG(3,("smbldap_check_root_dse: no idea what to query root-dse for: %s ?\n", attrs
[0]));
1618 rc
= ldap_search_s(ldap_state
->ldap_struct
, "", LDAP_SCOPE_BASE
,
1619 "(objectclass=*)", CONST_DISCARD(char **, attrs
), 0 , &msg
);
1621 if (rc
!= LDAP_SUCCESS
) {
1622 DEBUG(3,("smbldap_check_root_dse: Could not search rootDSE\n"));
1626 num_result
= ldap_count_entries(ldap_state
->ldap_struct
, msg
);
1628 if (num_result
!= 1) {
1629 DEBUG(3,("smbldap_check_root_dse: Expected one rootDSE, got %d\n", num_result
));
1633 entry
= ldap_first_entry(ldap_state
->ldap_struct
, msg
);
1635 if (entry
== NULL
) {
1636 DEBUG(3,("smbldap_check_root_dse: Could not retrieve rootDSE\n"));
1640 values
= ldap_get_values(ldap_state
->ldap_struct
, entry
, attrs
[0]);
1642 if (values
== NULL
) {
1643 DEBUG(5,("smbldap_check_root_dse: LDAP Server does not support any %s\n", attrs
[0]));
1647 num_values
= ldap_count_values(values
);
1649 if (num_values
== 0) {
1650 DEBUG(5,("smbldap_check_root_dse: LDAP Server does not have any %s\n", attrs
[0]));
1654 for (i
=0; i
<num_values
; i
++) {
1655 if (strcmp(values
[i
], value
) == 0)
1662 ldap_value_free(values
);
1670 /*******************************************************************
1671 Check if LDAP-Server supports a certain Control (OID in string format)
1672 ********************************************************************/
1674 BOOL
smbldap_has_control(struct smbldap_state
*ldap_state
, const char *control
)
1676 const char *attrs
[] = { "supportedControl", NULL
};
1677 return smbldap_check_root_dse(ldap_state
, attrs
, control
);
1680 /*******************************************************************
1681 Check if LDAP-Server supports a certain Extension (OID in string format)
1682 ********************************************************************/
1684 BOOL
smbldap_has_extension(struct smbldap_state
*ldap_state
, const char *extension
)
1686 const char *attrs
[] = { "supportedExtension", NULL
};
1687 return smbldap_check_root_dse(ldap_state
, attrs
, extension
);
1690 /*******************************************************************
1691 Check if LDAP-Server holds a given namingContext
1692 ********************************************************************/
1694 BOOL
smbldap_has_naming_context(struct smbldap_state
*ldap_state
, const char *naming_context
)
1696 const char *attrs
[] = { "namingContexts", NULL
};
1697 return smbldap_check_root_dse(ldap_state
, attrs
, naming_context
);