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_SN
, "sn" },
56 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
57 { LDAP_ATTR_HOME_PATH
, "smbHome" },
58 { LDAP_ATTR_HOME_DRIVE
, "homeDrive" },
59 { LDAP_ATTR_LOGON_SCRIPT
, "scriptPath" },
60 { LDAP_ATTR_PROFILE_PATH
, "profilePath" },
61 { LDAP_ATTR_DESC
, "description" },
62 { LDAP_ATTR_USER_WKS
, "userWorkstations"},
63 { LDAP_ATTR_USER_RID
, "rid" },
64 { LDAP_ATTR_PRIMARY_GROUP_RID
, "primaryGroupID"},
65 { LDAP_ATTR_LMPW
, "lmPassword" },
66 { LDAP_ATTR_NTPW
, "ntPassword" },
67 { LDAP_ATTR_DOMAIN
, "domain" },
68 { LDAP_ATTR_OBJCLASS
, "objectClass" },
69 { LDAP_ATTR_ACB_INFO
, "acctFlags" },
70 { LDAP_ATTR_MOD_TIMESTAMP
, "modifyTimestamp" },
71 { LDAP_ATTR_LIST_END
, NULL
}
74 ATTRIB_MAP_ENTRY attrib_map_to_delete_v22
[] = {
75 { LDAP_ATTR_PWD_LAST_SET
, "pwdLastSet" },
76 { LDAP_ATTR_PWD_CAN_CHANGE
, "pwdCanChange" },
77 { LDAP_ATTR_PWD_MUST_CHANGE
, "pwdMustChange" },
78 { LDAP_ATTR_LOGON_TIME
, "logonTime" },
79 { LDAP_ATTR_LOGOFF_TIME
, "logoffTime" },
80 { LDAP_ATTR_KICKOFF_TIME
, "kickoffTime" },
81 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
82 { LDAP_ATTR_HOME_PATH
, "smbHome" },
83 { LDAP_ATTR_HOME_DRIVE
, "homeDrives" },
84 { LDAP_ATTR_LOGON_SCRIPT
, "scriptPath" },
85 { LDAP_ATTR_PROFILE_PATH
, "profilePath" },
86 { LDAP_ATTR_USER_WKS
, "userWorkstations"},
87 { LDAP_ATTR_USER_RID
, "rid" },
88 { LDAP_ATTR_PRIMARY_GROUP_RID
, "primaryGroupID"},
89 { LDAP_ATTR_LMPW
, "lmPassword" },
90 { LDAP_ATTR_NTPW
, "ntPassword" },
91 { LDAP_ATTR_DOMAIN
, "domain" },
92 { LDAP_ATTR_ACB_INFO
, "acctFlags" },
93 { LDAP_ATTR_LIST_END
, NULL
}
96 /* attributes used by Samba 3.0's sambaSamAccount */
98 ATTRIB_MAP_ENTRY attrib_map_v30
[] = {
99 { LDAP_ATTR_UID
, "uid" },
100 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
101 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
102 { LDAP_ATTR_UNIX_HOME
, "homeDirectory" },
103 { LDAP_ATTR_PWD_LAST_SET
, "sambaPwdLastSet" },
104 { LDAP_ATTR_PWD_CAN_CHANGE
, "sambaPwdCanChange" },
105 { LDAP_ATTR_PWD_MUST_CHANGE
, "sambaPwdMustChange" },
106 { LDAP_ATTR_LOGON_TIME
, "sambaLogonTime" },
107 { LDAP_ATTR_LOGOFF_TIME
, "sambaLogoffTime" },
108 { LDAP_ATTR_KICKOFF_TIME
, "sambaKickoffTime" },
109 { LDAP_ATTR_CN
, "cn" },
110 { LDAP_ATTR_SN
, "sn" },
111 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
112 { LDAP_ATTR_HOME_DRIVE
, "sambaHomeDrive" },
113 { LDAP_ATTR_HOME_PATH
, "sambaHomePath" },
114 { LDAP_ATTR_LOGON_SCRIPT
, "sambaLogonScript" },
115 { LDAP_ATTR_PROFILE_PATH
, "sambaProfilePath" },
116 { LDAP_ATTR_DESC
, "description" },
117 { LDAP_ATTR_USER_WKS
, "sambaUserWorkstations" },
118 { LDAP_ATTR_USER_SID
, LDAP_ATTRIBUTE_SID
},
119 { LDAP_ATTR_PRIMARY_GROUP_SID
, "sambaPrimaryGroupSID" },
120 { LDAP_ATTR_LMPW
, "sambaLMPassword" },
121 { LDAP_ATTR_NTPW
, "sambaNTPassword" },
122 { LDAP_ATTR_DOMAIN
, "sambaDomainName" },
123 { LDAP_ATTR_OBJCLASS
, "objectClass" },
124 { LDAP_ATTR_ACB_INFO
, "sambaAcctFlags" },
125 { LDAP_ATTR_MUNGED_DIAL
, "sambaMungedDial" },
126 { LDAP_ATTR_BAD_PASSWORD_COUNT
, "sambaBadPasswordCount" },
127 { LDAP_ATTR_BAD_PASSWORD_TIME
, "sambaBadPasswordTime" },
128 { LDAP_ATTR_PWD_HISTORY
, "sambaPasswordHistory" },
129 { LDAP_ATTR_MOD_TIMESTAMP
, "modifyTimestamp" },
130 { LDAP_ATTR_LOGON_HOURS
, "sambaLogonHours" },
131 { LDAP_ATTR_LIST_END
, NULL
}
134 ATTRIB_MAP_ENTRY attrib_map_to_delete_v30
[] = {
135 { LDAP_ATTR_PWD_LAST_SET
, "sambaPwdLastSet" },
136 { LDAP_ATTR_PWD_CAN_CHANGE
, "sambaPwdCanChange" },
137 { LDAP_ATTR_PWD_MUST_CHANGE
, "sambaPwdMustChange" },
138 { LDAP_ATTR_LOGON_TIME
, "sambaLogonTime" },
139 { LDAP_ATTR_LOGOFF_TIME
, "sambaLogoffTime" },
140 { LDAP_ATTR_KICKOFF_TIME
, "sambaKickoffTime" },
141 { LDAP_ATTR_HOME_DRIVE
, "sambaHomeDrive" },
142 { LDAP_ATTR_HOME_PATH
, "sambaHomePath" },
143 { LDAP_ATTR_LOGON_SCRIPT
, "sambaLogonScript" },
144 { LDAP_ATTR_PROFILE_PATH
, "sambaProfilePath" },
145 { LDAP_ATTR_USER_WKS
, "sambaUserWorkstations" },
146 { LDAP_ATTR_USER_SID
, LDAP_ATTRIBUTE_SID
},
147 { LDAP_ATTR_PRIMARY_GROUP_SID
, "sambaPrimaryGroupSID" },
148 { LDAP_ATTR_LMPW
, "sambaLMPassword" },
149 { LDAP_ATTR_NTPW
, "sambaNTPassword" },
150 { LDAP_ATTR_DOMAIN
, "sambaDomainName" },
151 { LDAP_ATTR_ACB_INFO
, "sambaAcctFlags" },
152 { LDAP_ATTR_MUNGED_DIAL
, "sambaMungedDial" },
153 { LDAP_ATTR_BAD_PASSWORD_COUNT
, "sambaBadPasswordCount" },
154 { LDAP_ATTR_BAD_PASSWORD_TIME
, "sambaBadPasswordTime" },
155 { LDAP_ATTR_PWD_HISTORY
, "sambaPasswordHistory" },
156 { LDAP_ATTR_LOGON_HOURS
, "sambaLogonHours" },
157 { LDAP_ATTR_LIST_END
, NULL
}
160 /* attributes used for allocating RIDs */
162 ATTRIB_MAP_ENTRY dominfo_attr_list
[] = {
163 { LDAP_ATTR_DOMAIN
, "sambaDomainName" },
164 { LDAP_ATTR_NEXT_RID
, "sambaNextRid" },
165 { LDAP_ATTR_NEXT_USERRID
, "sambaNextUserRid" },
166 { LDAP_ATTR_NEXT_GROUPRID
, "sambaNextGroupRid" },
167 { LDAP_ATTR_DOM_SID
, LDAP_ATTRIBUTE_SID
},
168 { LDAP_ATTR_ALGORITHMIC_RID_BASE
,"sambaAlgorithmicRidBase"},
169 { LDAP_ATTR_OBJCLASS
, "objectClass" },
170 { LDAP_ATTR_LIST_END
, NULL
},
173 /* Samba 3.0 group mapping attributes */
175 ATTRIB_MAP_ENTRY groupmap_attr_list
[] = {
176 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
177 { LDAP_ATTR_GROUP_SID
, LDAP_ATTRIBUTE_SID
},
178 { LDAP_ATTR_GROUP_TYPE
, "sambaGroupType" },
179 { LDAP_ATTR_SID_LIST
, "sambaSIDList" },
180 { LDAP_ATTR_DESC
, "description" },
181 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
182 { LDAP_ATTR_CN
, "cn" },
183 { LDAP_ATTR_OBJCLASS
, "objectClass" },
184 { LDAP_ATTR_LIST_END
, NULL
}
187 ATTRIB_MAP_ENTRY groupmap_attr_list_to_delete
[] = {
188 { LDAP_ATTR_GROUP_SID
, LDAP_ATTRIBUTE_SID
},
189 { LDAP_ATTR_GROUP_TYPE
, "sambaGroupType" },
190 { LDAP_ATTR_DESC
, "description" },
191 { LDAP_ATTR_DISPLAY_NAME
, "displayName" },
192 { LDAP_ATTR_SID_LIST
, "sambaSIDList" },
193 { LDAP_ATTR_LIST_END
, NULL
}
196 /* idmap_ldap sambaUnixIdPool */
198 ATTRIB_MAP_ENTRY idpool_attr_list
[] = {
199 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
200 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
201 { LDAP_ATTR_OBJCLASS
, "objectClass" },
202 { LDAP_ATTR_LIST_END
, NULL
}
205 ATTRIB_MAP_ENTRY sidmap_attr_list
[] = {
206 { LDAP_ATTR_SID
, LDAP_ATTRIBUTE_SID
},
207 { LDAP_ATTR_UIDNUMBER
, LDAP_ATTRIBUTE_UIDNUMBER
},
208 { LDAP_ATTR_GIDNUMBER
, LDAP_ATTRIBUTE_GIDNUMBER
},
209 { LDAP_ATTR_OBJCLASS
, "objectClass" },
210 { LDAP_ATTR_LIST_END
, NULL
}
213 /**********************************************************************
214 perform a simple table lookup and return the attribute name
215 **********************************************************************/
217 const char* get_attr_key2string( ATTRIB_MAP_ENTRY table
[], int key
)
221 while ( table
[i
].attrib
!= LDAP_ATTR_LIST_END
) {
222 if ( table
[i
].attrib
== key
)
223 return table
[i
].name
;
231 /**********************************************************************
232 Return the list of attribute names from a mapping table
233 **********************************************************************/
235 const char** get_attr_list( TALLOC_CTX
*mem_ctx
, ATTRIB_MAP_ENTRY table
[] )
240 while ( table
[i
].attrib
!= LDAP_ATTR_LIST_END
)
244 names
= TALLOC_ARRAY( mem_ctx
, const char*, i
);
246 DEBUG(0,("get_attr_list: out of memory\n"));
251 while ( table
[i
].attrib
!= LDAP_ATTR_LIST_END
) {
252 names
[i
] = talloc_strdup( names
, table
[i
].name
);
260 /*******************************************************************
261 Search an attribute and return the first value found.
262 ******************************************************************/
264 BOOL
smbldap_get_single_attribute (LDAP
* ldap_struct
, LDAPMessage
* entry
,
265 const char *attribute
, char *value
,
275 if ((values
= ldap_get_values (ldap_struct
, entry
, attribute
)) == NULL
) {
276 DEBUG (10, ("smbldap_get_single_attribute: [%s] = [<does not exist>]\n", attribute
));
281 if (convert_string(CH_UTF8
, CH_UNIX
,values
[0], -1, value
, max_len
, False
) == (size_t)-1) {
282 DEBUG(1, ("smbldap_get_single_attribute: string conversion of [%s] = [%s] failed!\n",
283 attribute
, values
[0]));
284 ldap_value_free(values
);
288 ldap_value_free(values
);
289 #ifdef DEBUG_PASSWORDS
290 DEBUG (100, ("smbldap_get_single_attribute: [%s] = [%s]\n", attribute
, value
));
295 BOOL
smbldap_get_single_pstring (LDAP
* ldap_struct
, LDAPMessage
* entry
,
296 const char *attribute
, pstring value
)
298 return smbldap_get_single_attribute(ldap_struct
, entry
,
303 char * smbldap_talloc_single_attribute(LDAP
*ldap_struct
, LDAPMessage
*entry
,
304 const char *attribute
,
310 if (attribute
== NULL
) {
314 values
= ldap_get_values(ldap_struct
, entry
, attribute
);
316 if (values
== NULL
) {
317 DEBUG(10, ("attribute %s does not exist\n", attribute
));
321 if (ldap_count_values(values
) != 1) {
322 DEBUG(10, ("attribute %s has %d values, expected only one\n",
323 attribute
, ldap_count_values(values
)));
324 ldap_value_free(values
);
328 if (pull_utf8_talloc(mem_ctx
, &result
, values
[0]) == (size_t)-1) {
329 DEBUG(10, ("pull_utf8_talloc failed\n"));
330 ldap_value_free(values
);
334 ldap_value_free(values
);
336 #ifdef DEBUG_PASSWORDS
337 DEBUG (100, ("smbldap_get_single_attribute: [%s] = [%s]\n",
343 static int ldapmsg_destructor(LDAPMessage
**result
) {
344 ldap_msgfree(*result
);
348 void talloc_autofree_ldapmsg(TALLOC_CTX
*mem_ctx
, LDAPMessage
*result
)
350 LDAPMessage
**handle
;
352 if (result
== NULL
) {
356 handle
= TALLOC_P(mem_ctx
, LDAPMessage
*);
357 SMB_ASSERT(handle
!= NULL
);
360 talloc_set_destructor(handle
, ldapmsg_destructor
);
363 static int ldapmod_destructor(LDAPMod
***mod
) {
364 ldap_mods_free(*mod
, 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
;
576 if ((tmp_ldap_state
= smbldap_find_state(ld
))) {
577 SMB_ASSERT(tmp_ldap_state
== smbldap_state
);
581 t
= SMB_XMALLOC_P(struct smbldap_state_lookup
);
584 DLIST_ADD_END(smbldap_state_lookup_list
, t
, struct smbldap_state_lookup
*);
586 t
->smbldap_state
= smbldap_state
;
589 /********************************************************************
590 start TLS on an existing LDAP connection
591 *******************************************************************/
593 int smb_ldap_start_tls(LDAP
*ldap_struct
, int version
)
597 if (lp_ldap_ssl() != LDAP_SSL_START_TLS
) {
601 #ifdef LDAP_OPT_X_TLS
602 if (version
!= LDAP_VERSION3
) {
603 DEBUG(0, ("Need LDAPv3 for Start TLS\n"));
604 return LDAP_OPERATIONS_ERROR
;
607 if ((rc
= ldap_start_tls_s (ldap_struct
, NULL
, NULL
)) != LDAP_SUCCESS
) {
608 DEBUG(0,("Failed to issue the StartTLS instruction: %s\n",
609 ldap_err2string(rc
)));
613 DEBUG (3, ("StartTLS issued: using a TLS connection\n"));
616 DEBUG(0,("StartTLS not supported by LDAP client libraries!\n"));
617 return LDAP_OPERATIONS_ERROR
;
621 /********************************************************************
622 setup a connection to the LDAP server based on a uri
623 *******************************************************************/
625 int smb_ldap_setup_conn(LDAP
**ldap_struct
, const char *uri
)
629 DEBUG(10, ("smb_ldap_setup_connection: %s\n", uri
));
631 #ifdef HAVE_LDAP_INITIALIZE
633 rc
= ldap_initialize(ldap_struct
, uri
);
635 DEBUG(0, ("ldap_initialize: %s\n", ldap_err2string(rc
)));
641 /* Parse the string manually */
647 SMB_ASSERT(sizeof(protocol
)>10 && sizeof(host
)>254);
650 /* skip leading "URL:" (if any) */
651 if ( strnequal( uri
, "URL:", 4 ) ) {
655 sscanf(uri
, "%10[^:]://%254[^:/]:%d", protocol
, host
, &port
);
658 if (strequal(protocol
, "ldap")) {
660 } else if (strequal(protocol
, "ldaps")) {
663 DEBUG(0, ("unrecognised protocol (%s)!\n", protocol
));
667 if ((*ldap_struct
= ldap_init(host
, port
)) == NULL
) {
668 DEBUG(0, ("ldap_init failed !\n"));
669 return LDAP_OPERATIONS_ERROR
;
672 if (strequal(protocol
, "ldaps")) {
673 #ifdef LDAP_OPT_X_TLS
674 int tls
= LDAP_OPT_X_TLS_HARD
;
675 if (ldap_set_option (*ldap_struct
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
677 DEBUG(0, ("Failed to setup a TLS session\n"));
680 DEBUG(3,("LDAPS option set...!\n"));
682 DEBUG(0,("smbldap_open_connection: Secure connection not supported by LDAP client libraries!\n"));
683 return LDAP_OPERATIONS_ERROR
;
684 #endif /* LDAP_OPT_X_TLS */
688 #endif /* HAVE_LDAP_INITIALIZE */
692 /********************************************************************
693 try to upgrade to Version 3 LDAP if not already, in either case return current
695 *******************************************************************/
697 int smb_ldap_upgrade_conn(LDAP
*ldap_struct
, int *new_version
)
702 /* assume the worst */
703 *new_version
= LDAP_VERSION2
;
705 rc
= ldap_get_option(ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
710 if (version
== LDAP_VERSION3
) {
711 *new_version
= LDAP_VERSION3
;
716 version
= LDAP_VERSION3
;
717 rc
= ldap_set_option (ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
722 *new_version
= LDAP_VERSION3
;
726 /*******************************************************************
727 open a connection to the ldap server (just until the bind)
728 ******************************************************************/
730 int smb_ldap_setup_full_conn(LDAP
**ldap_struct
, const char *uri
)
734 rc
= smb_ldap_setup_conn(ldap_struct
, uri
);
739 rc
= smb_ldap_upgrade_conn(*ldap_struct
, &version
);
744 rc
= smb_ldap_start_tls(*ldap_struct
, version
);
752 /*******************************************************************
753 open a connection to the ldap server.
754 ******************************************************************/
755 static int smbldap_open_connection (struct smbldap_state
*ldap_state
)
758 int rc
= LDAP_SUCCESS
;
760 LDAP
**ldap_struct
= &ldap_state
->ldap_struct
;
762 rc
= smb_ldap_setup_conn(ldap_struct
, ldap_state
->uri
);
767 /* Store the LDAP pointer in a lookup list */
769 smbldap_store_state(*ldap_struct
, ldap_state
);
771 /* Upgrade to LDAPv3 if possible */
773 rc
= smb_ldap_upgrade_conn(*ldap_struct
, &version
);
778 /* Start TLS if required */
780 rc
= smb_ldap_start_tls(*ldap_struct
, version
);
785 DEBUG(2, ("smbldap_open_connection: connection opened\n"));
789 /*******************************************************************
790 a rebind function for authenticated referrals
791 This version takes a void* that we can shove useful stuff in :-)
792 ******************************************************************/
793 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
795 static int rebindproc_with_state (LDAP
* ld
, char **whop
, char **credp
,
796 int *methodp
, int freeit
, void *arg
)
798 struct smbldap_state
*ldap_state
= arg
;
800 /** @TODO Should we be doing something to check what servers we rebind to?
801 Could we get a referral to a machine that we don't want to give our
802 username and password to? */
806 memset(*credp
, '\0', strlen(*credp
));
809 DEBUG(5,("rebind_proc_with_state: Rebinding as \"%s\"\n",
810 ldap_state
->bind_dn
));
812 *whop
= SMB_STRDUP(ldap_state
->bind_dn
);
814 return LDAP_NO_MEMORY
;
816 *credp
= SMB_STRDUP(ldap_state
->bind_secret
);
819 return LDAP_NO_MEMORY
;
821 *methodp
= LDAP_AUTH_SIMPLE
;
824 GetTimeOfDay(&ldap_state
->last_rebind
);
828 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
830 /*******************************************************************
831 a rebind function for authenticated referrals
832 This version takes a void* that we can shove useful stuff in :-)
833 and actually does the connection.
834 ******************************************************************/
835 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
836 static int rebindproc_connect_with_state (LDAP
*ldap_struct
,
837 LDAP_CONST
char *url
,
839 ber_int_t msgid
, void *arg
)
841 struct smbldap_state
*ldap_state
=
842 (struct smbldap_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", (unsigned int)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 #ifdef HAVE_LDAP_SET_REBIND_PROC
948 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
949 # if LDAP_SET_REBIND_PROC_ARGS == 2
950 ldap_set_rebind_proc(ldap_struct
, &rebindproc_connect
);
952 # if LDAP_SET_REBIND_PROC_ARGS == 3
953 ldap_set_rebind_proc(ldap_struct
, &rebindproc_connect_with_state
, (void *)ldap_state
);
955 #else /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
956 # if LDAP_SET_REBIND_PROC_ARGS == 2
957 ldap_set_rebind_proc(ldap_struct
, &rebindproc
);
959 # if LDAP_SET_REBIND_PROC_ARGS == 3
960 ldap_set_rebind_proc(ldap_struct
, &rebindproc_with_state
, (void *)ldap_state
);
962 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
965 rc
= ldap_simple_bind_s(ldap_struct
, ldap_dn
, ldap_secret
);
967 if (rc
!= LDAP_SUCCESS
) {
968 char *ld_error
= NULL
;
969 ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
971 DEBUG(ldap_state
->num_failures
? 2 : 0,
972 ("failed to bind to server %s with dn=\"%s\" Error: %s\n\t%s\n",
974 ldap_dn
? ldap_dn
: "(unknown)", ldap_err2string(rc
),
975 ld_error
? ld_error
: "(unknown)"));
977 ldap_state
->num_failures
++;
981 ldap_state
->num_failures
= 0;
982 ldap_state
->paged_results
= False
;
984 ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
986 if (smbldap_has_control(ldap_state
->ldap_struct
, ADS_PAGE_CTL_OID
) && version
== 3) {
987 ldap_state
->paged_results
= True
;
990 DEBUG(3, ("ldap_connect_system: succesful connection to the LDAP server\n"));
991 DEBUGADD(10, ("ldap_connect_system: LDAP server %s support paged results\n",
992 ldap_state
->paged_results
? "does" : "does not"));
996 /**********************************************************************
997 Connect to LDAP server (called before every ldap operation)
998 *********************************************************************/
999 static int smbldap_open(struct smbldap_state
*ldap_state
)
1002 BOOL reopen
= False
;
1003 SMB_ASSERT(ldap_state
);
1005 #ifndef NO_LDAP_SECURITY
1006 if (geteuid() != 0) {
1007 DEBUG(0, ("smbldap_open: cannot access LDAP when not root..\n"));
1008 return LDAP_INSUFFICIENT_ACCESS
;
1012 if ((ldap_state
->ldap_struct
!= NULL
) && ((ldap_state
->last_ping
+ SMBLDAP_DONT_PING_TIME
) < time(NULL
))) {
1014 #ifdef HAVE_UNIXSOCKET
1015 struct sockaddr_un addr
;
1017 struct sockaddr addr
;
1019 socklen_t len
= sizeof(addr
);
1022 opt_rc
= ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_DESC
, &sd
);
1023 if (opt_rc
== 0 && (getpeername(sd
, (struct sockaddr
*) &addr
, &len
)) < 0 )
1026 #ifdef HAVE_UNIXSOCKET
1027 if (opt_rc
== 0 && addr
.sun_family
== AF_UNIX
)
1031 /* the other end has died. reopen. */
1032 ldap_unbind(ldap_state
->ldap_struct
);
1033 ldap_state
->ldap_struct
= NULL
;
1034 ldap_state
->last_ping
= (time_t)0;
1036 ldap_state
->last_ping
= time(NULL
);
1040 if (ldap_state
->ldap_struct
!= NULL
) {
1041 DEBUG(11,("smbldap_open: already connected to the LDAP server\n"));
1042 return LDAP_SUCCESS
;
1045 if ((rc
= smbldap_open_connection(ldap_state
))) {
1049 if ((rc
= smbldap_connect_system(ldap_state
, ldap_state
->ldap_struct
))) {
1050 ldap_unbind(ldap_state
->ldap_struct
);
1051 ldap_state
->ldap_struct
= NULL
;
1056 ldap_state
->last_ping
= time(NULL
);
1057 ldap_state
->pid
= sys_getpid();
1058 DEBUG(4,("The LDAP server is succesfully connected\n"));
1060 return LDAP_SUCCESS
;
1063 /**********************************************************************
1064 Disconnect from LDAP server
1065 *********************************************************************/
1066 static NTSTATUS
smbldap_close(struct smbldap_state
*ldap_state
)
1069 return NT_STATUS_INVALID_PARAMETER
;
1071 if (ldap_state
->ldap_struct
!= NULL
) {
1072 ldap_unbind(ldap_state
->ldap_struct
);
1073 ldap_state
->ldap_struct
= NULL
;
1076 smbldap_delete_state(ldap_state
);
1078 DEBUG(5,("The connection to the LDAP server was closed\n"));
1079 /* maybe free the results here --metze */
1083 return NT_STATUS_OK
;
1086 static BOOL got_alarm
;
1088 static void (*old_handler
)(int);
1090 static void gotalarm_sig(int dummy
)
1095 static int another_ldap_try(struct smbldap_state
*ldap_state
, int *rc
,
1096 int *attempts
, time_t endtime
)
1098 time_t now
= time(NULL
);
1099 int open_rc
= LDAP_SERVER_DOWN
;
1101 if (*rc
!= LDAP_SERVER_DOWN
)
1104 if (now
>= endtime
) {
1105 smbldap_close(ldap_state
);
1110 if (*attempts
== 0) {
1112 old_handler
= CatchSignal(SIGALRM
, gotalarm_sig
);
1113 alarm(endtime
- now
);
1115 if (ldap_state
->pid
!= sys_getpid())
1116 smbldap_close(ldap_state
);
1126 open_rc
= smbldap_open(ldap_state
);
1128 if (open_rc
== LDAP_SUCCESS
) {
1129 ldap_state
->last_use
= now
;
1133 if (open_rc
== LDAP_INSUFFICIENT_ACCESS
) {
1134 /* The fact that we are non-root or any other
1135 * access-denied condition will not change in the next
1136 * round of trying */
1146 if (open_rc
!= LDAP_SUCCESS
) {
1147 DEBUG(1, ("Connection to LDAP server failed for the "
1148 "%d try!\n", *attempts
));
1153 CatchSignal(SIGALRM
, old_handler
);
1155 ldap_state
->last_use
= now
;
1159 /*********************************************************************
1160 ********************************************************************/
1162 static int smbldap_search_ext(struct smbldap_state
*ldap_state
,
1163 const char *base
, int scope
, const char *filter
,
1164 const char *attrs
[], int attrsonly
,
1165 LDAPControl
**sctrls
, LDAPControl
**cctrls
,
1166 int sizelimit
, LDAPMessage
**res
)
1168 int rc
= LDAP_SERVER_DOWN
;
1171 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1172 struct timeval timeout
;
1174 SMB_ASSERT(ldap_state
);
1176 DEBUG(5,("smbldap_search_ext: base => [%s], filter => [%s], "
1177 "scope => [%d]\n", base
, filter
, scope
));
1179 if (ldap_state
->last_rebind
.tv_sec
> 0) {
1180 struct timeval tval
;
1181 SMB_BIG_INT tdiff
= 0;
1185 GetTimeOfDay(&tval
);
1187 tdiff
= usec_time_diff(&tval
, &ldap_state
->last_rebind
);
1188 tdiff
/= 1000; /* Convert to milliseconds. */
1190 sleep_time
= lp_ldap_replication_sleep()-(int)tdiff
;
1191 sleep_time
= MIN(sleep_time
, MAX_LDAP_REPLICATION_SLEEP_TIME
);
1193 if (sleep_time
> 0) {
1194 /* we wait for the LDAP replication */
1195 DEBUG(5,("smbldap_search_ext: waiting %d milliseconds "
1196 "for LDAP replication.\n",sleep_time
));
1197 smb_msleep(sleep_time
);
1198 DEBUG(5,("smbldap_search_ext: go on!\n"));
1200 ZERO_STRUCT(ldap_state
->last_rebind
);
1203 if (push_utf8_allocate(&utf8_filter
, filter
) == (size_t)-1) {
1204 return LDAP_NO_MEMORY
;
1207 /* Setup timeout for the ldap_search_ext_s call - local and remote. */
1208 timeout
.tv_sec
= lp_ldap_timeout();
1209 timeout
.tv_usec
= 0;
1211 /* Setup alarm timeout.... Do we need both of these ? JRA.
1212 * Yes, I think we do need both of these. The server timeout only
1213 * covers the case where the server's operation takes too long. It
1214 * does not cover the case where the request hangs on its way to the
1215 * server. The server side timeout is not strictly necessary, it's
1216 * just a bit more kind to the server. VL. */
1219 CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
1220 alarm(lp_ldap_timeout());
1221 /* End setup timeout. */
1223 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1224 rc
= ldap_search_ext_s(ldap_state
->ldap_struct
, base
, scope
,
1226 CONST_DISCARD(char **, attrs
),
1227 attrsonly
, sctrls
, cctrls
, &timeout
,
1229 if (rc
!= LDAP_SUCCESS
) {
1230 char *ld_error
= NULL
;
1231 ldap_get_option(ldap_state
->ldap_struct
,
1232 LDAP_OPT_ERROR_STRING
, &ld_error
);
1233 DEBUG(10,("Failed search for base: %s, error: %s "
1234 "(%s)\n", base
, ldap_err2string(rc
),
1235 ld_error
? ld_error
: "unknown"));
1236 SAFE_FREE(ld_error
);
1240 SAFE_FREE(utf8_filter
);
1242 /* Teardown timeout. */
1243 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
1247 return LDAP_TIMELIMIT_EXCEEDED
;
1252 int smbldap_search(struct smbldap_state
*ldap_state
,
1253 const char *base
, int scope
, const char *filter
,
1254 const char *attrs
[], int attrsonly
,
1257 return smbldap_search_ext(ldap_state
, base
, scope
, filter
, attrs
,
1258 attrsonly
, NULL
, NULL
, LDAP_NO_LIMIT
, res
);
1261 int smbldap_search_paged(struct smbldap_state
*ldap_state
,
1262 const char *base
, int scope
, const char *filter
,
1263 const char **attrs
, int attrsonly
, int pagesize
,
1264 LDAPMessage
**res
, void **cookie
)
1267 LDAPControl
**rcontrols
;
1268 LDAPControl
*controls
[2] = { NULL
, NULL
};
1269 BerElement
*cookie_be
= NULL
;
1270 struct berval
*cookie_bv
= NULL
;
1272 BOOL critical
= True
;
1276 DEBUG(3,("smbldap_search_paged: base => [%s], filter => [%s],"
1277 "scope => [%d], pagesize => [%d]\n",
1278 base
, filter
, scope
, pagesize
));
1280 cookie_be
= ber_alloc_t(LBER_USE_DER
);
1281 if (cookie_be
== NULL
) {
1282 DEBUG(0,("smbldap_create_page_control: ber_alloc_t returns "
1284 return LDAP_NO_MEMORY
;
1287 /* construct cookie */
1288 if (*cookie
!= NULL
) {
1289 ber_printf(cookie_be
, "{iO}", (ber_int_t
) pagesize
, *cookie
);
1290 ber_bvfree((struct berval
*)*cookie
); /* don't need it from last time */
1293 ber_printf(cookie_be
, "{io}", (ber_int_t
) pagesize
, "", 0);
1295 ber_flatten(cookie_be
, &cookie_bv
);
1297 pr
.ldctl_oid
= CONST_DISCARD(char *, ADS_PAGE_CTL_OID
);
1298 pr
.ldctl_iscritical
= (char) critical
;
1299 pr
.ldctl_value
.bv_len
= cookie_bv
->bv_len
;
1300 pr
.ldctl_value
.bv_val
= cookie_bv
->bv_val
;
1305 rc
= smbldap_search_ext(ldap_state
, base
, scope
, filter
, attrs
,
1306 0, controls
, NULL
, LDAP_NO_LIMIT
, res
);
1308 ber_free(cookie_be
, 1);
1309 ber_bvfree(cookie_bv
);
1312 DEBUG(3,("smbldap_search_paged: smbldap_search_ext(%s) "
1313 "failed with [%s]\n", filter
, ldap_err2string(rc
)));
1317 DEBUG(3,("smbldap_search_paged: search was successfull\n"));
1319 rc
= ldap_parse_result(ldap_state
->ldap_struct
, *res
, NULL
, NULL
,
1320 NULL
, NULL
, &rcontrols
, 0);
1322 DEBUG(3,("smbldap_search_paged: ldap_parse_result failed " \
1323 "with [%s]\n", ldap_err2string(rc
)));
1327 if (rcontrols
== NULL
)
1330 for (i
=0; rcontrols
[i
]; i
++) {
1332 if (strcmp(ADS_PAGE_CTL_OID
, rcontrols
[i
]->ldctl_oid
) != 0)
1335 cookie_be
= ber_init(&rcontrols
[i
]->ldctl_value
);
1336 ber_scanf(cookie_be
,"{iO}", &tmp
, &cookie_bv
);
1337 /* the berval is the cookie, but must be freed when it is all
1339 if (cookie_bv
->bv_len
)
1340 *cookie
=ber_bvdup(cookie_bv
);
1343 ber_bvfree(cookie_bv
);
1344 ber_free(cookie_be
, 1);
1347 ldap_controls_free(rcontrols
);
1352 int smbldap_modify(struct smbldap_state
*ldap_state
, const char *dn
, LDAPMod
*attrs
[])
1354 int rc
= LDAP_SERVER_DOWN
;
1357 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1359 SMB_ASSERT(ldap_state
);
1361 DEBUG(5,("smbldap_modify: dn => [%s]\n", dn
));
1363 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1364 return LDAP_NO_MEMORY
;
1367 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1368 rc
= ldap_modify_s(ldap_state
->ldap_struct
, utf8_dn
, attrs
);
1369 if (rc
!= LDAP_SUCCESS
) {
1370 char *ld_error
= NULL
;
1371 ldap_get_option(ldap_state
->ldap_struct
,
1372 LDAP_OPT_ERROR_STRING
, &ld_error
);
1373 DEBUG(10,("Failed to modify dn: %s, error: %s "
1374 "(%s)\n", dn
, ldap_err2string(rc
),
1375 ld_error
? ld_error
: "unknown"));
1376 SAFE_FREE(ld_error
);
1384 int smbldap_add(struct smbldap_state
*ldap_state
, const char *dn
, LDAPMod
*attrs
[])
1386 int rc
= LDAP_SERVER_DOWN
;
1389 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1391 SMB_ASSERT(ldap_state
);
1393 DEBUG(5,("smbldap_add: dn => [%s]\n", dn
));
1395 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1396 return LDAP_NO_MEMORY
;
1399 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1400 rc
= ldap_add_s(ldap_state
->ldap_struct
, utf8_dn
, attrs
);
1401 if (rc
!= LDAP_SUCCESS
) {
1402 char *ld_error
= NULL
;
1403 ldap_get_option(ldap_state
->ldap_struct
,
1404 LDAP_OPT_ERROR_STRING
, &ld_error
);
1405 DEBUG(10,("Failed to add dn: %s, error: %s "
1406 "(%s)\n", dn
, ldap_err2string(rc
),
1407 ld_error
? ld_error
: "unknown"));
1408 SAFE_FREE(ld_error
);
1416 int smbldap_delete(struct smbldap_state
*ldap_state
, const char *dn
)
1418 int rc
= LDAP_SERVER_DOWN
;
1421 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1423 SMB_ASSERT(ldap_state
);
1425 DEBUG(5,("smbldap_delete: dn => [%s]\n", dn
));
1427 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1428 return LDAP_NO_MEMORY
;
1431 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1432 rc
= ldap_delete_s(ldap_state
->ldap_struct
, utf8_dn
);
1433 if (rc
!= LDAP_SUCCESS
) {
1434 char *ld_error
= NULL
;
1435 ldap_get_option(ldap_state
->ldap_struct
,
1436 LDAP_OPT_ERROR_STRING
, &ld_error
);
1437 DEBUG(10,("Failed to delete dn: %s, error: %s "
1438 "(%s)\n", dn
, ldap_err2string(rc
),
1439 ld_error
? ld_error
: "unknown"));
1440 SAFE_FREE(ld_error
);
1448 int smbldap_extended_operation(struct smbldap_state
*ldap_state
,
1449 LDAP_CONST
char *reqoid
, struct berval
*reqdata
,
1450 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
,
1451 char **retoidp
, struct berval
**retdatap
)
1453 int rc
= LDAP_SERVER_DOWN
;
1455 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1460 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1461 rc
= ldap_extended_operation_s(ldap_state
->ldap_struct
, reqoid
,
1462 reqdata
, serverctrls
,
1463 clientctrls
, retoidp
, retdatap
);
1464 if (rc
!= LDAP_SUCCESS
) {
1465 char *ld_error
= NULL
;
1466 ldap_get_option(ldap_state
->ldap_struct
,
1467 LDAP_OPT_ERROR_STRING
, &ld_error
);
1468 DEBUG(10,("Extended operation failed with error: %s "
1469 "(%s)\n", ldap_err2string(rc
),
1470 ld_error
? ld_error
: "unknown"));
1471 SAFE_FREE(ld_error
);
1478 /*******************************************************************
1479 run the search by name.
1480 ******************************************************************/
1481 int smbldap_search_suffix (struct smbldap_state
*ldap_state
,
1482 const char *filter
, const char **search_attr
,
1483 LDAPMessage
** result
)
1485 return smbldap_search(ldap_state
, lp_ldap_suffix(), LDAP_SCOPE_SUBTREE
,
1486 filter
, search_attr
, 0, result
);
1489 static void smbldap_idle_fn(void **data
, time_t *interval
, time_t now
)
1491 struct smbldap_state
*state
= (struct smbldap_state
*)(*data
);
1493 if (state
->ldap_struct
== NULL
) {
1494 DEBUG(10,("ldap connection not connected...\n"));
1498 if ((state
->last_use
+SMBLDAP_IDLE_TIME
) > now
) {
1499 DEBUG(10,("ldap connection not idle...\n"));
1503 DEBUG(7,("ldap connection idle...closing connection\n"));
1504 smbldap_close(state
);
1507 /**********************************************************************
1509 *********************************************************************/
1511 void smbldap_free_struct(struct smbldap_state
**ldap_state
)
1513 smbldap_close(*ldap_state
);
1515 if ((*ldap_state
)->bind_secret
) {
1516 memset((*ldap_state
)->bind_secret
, '\0', strlen((*ldap_state
)->bind_secret
));
1519 SAFE_FREE((*ldap_state
)->bind_dn
);
1520 SAFE_FREE((*ldap_state
)->bind_secret
);
1522 smb_unregister_idle_event((*ldap_state
)->event_id
);
1526 /* No need to free any further, as it is talloc()ed */
1530 /**********************************************************************
1531 Intitalise the 'general' ldap structures, on which ldap operations may be conducted
1532 *********************************************************************/
1534 NTSTATUS
smbldap_init(TALLOC_CTX
*mem_ctx
, const char *location
, struct smbldap_state
**smbldap_state
)
1536 *smbldap_state
= TALLOC_ZERO_P(mem_ctx
, struct smbldap_state
);
1537 if (!*smbldap_state
) {
1538 DEBUG(0, ("talloc() failed for ldapsam private_data!\n"));
1539 return NT_STATUS_NO_MEMORY
;
1543 (*smbldap_state
)->uri
= talloc_strdup(mem_ctx
, location
);
1545 (*smbldap_state
)->uri
= "ldap://localhost";
1548 (*smbldap_state
)->event_id
=
1549 smb_register_idle_event(smbldap_idle_fn
, (void *)(*smbldap_state
),
1552 if ((*smbldap_state
)->event_id
== SMB_EVENT_ID_INVALID
) {
1553 DEBUG(0,("Failed to register LDAP idle event!\n"));
1554 return NT_STATUS_INVALID_HANDLE
;
1557 return NT_STATUS_OK
;
1560 /*******************************************************************
1561 Return a copy of the DN for a LDAPMessage. Convert from utf8 to CH_UNIX.
1562 ********************************************************************/
1563 char *smbldap_get_dn(LDAP
*ld
, LDAPMessage
*entry
)
1565 char *utf8_dn
, *unix_dn
;
1567 utf8_dn
= ldap_get_dn(ld
, entry
);
1569 DEBUG (5, ("smbldap_get_dn: ldap_get_dn failed\n"));
1572 if (pull_utf8_allocate(&unix_dn
, utf8_dn
) == (size_t)-1) {
1573 DEBUG (0, ("smbldap_get_dn: String conversion failure utf8 [%s]\n", utf8_dn
));
1576 ldap_memfree(utf8_dn
);
1580 const char *smbldap_talloc_dn(TALLOC_CTX
*mem_ctx
, LDAP
*ld
,
1583 char *utf8_dn
, *unix_dn
;
1585 utf8_dn
= ldap_get_dn(ld
, entry
);
1587 DEBUG (5, ("smbldap_get_dn: ldap_get_dn failed\n"));
1590 if (pull_utf8_talloc(mem_ctx
, &unix_dn
, utf8_dn
) == (size_t)-1) {
1591 DEBUG (0, ("smbldap_get_dn: String conversion failure utf8 "
1592 "[%s]\n", utf8_dn
));
1595 ldap_memfree(utf8_dn
);
1599 /*******************************************************************
1600 Check if root-dse has a certain Control or Extension
1601 ********************************************************************/
1603 static BOOL
smbldap_check_root_dse(LDAP
*ld
, const char **attrs
, const char *value
)
1605 LDAPMessage
*msg
= NULL
;
1606 LDAPMessage
*entry
= NULL
;
1607 char **values
= NULL
;
1608 int rc
, num_result
, num_values
, i
;
1609 BOOL result
= False
;
1612 DEBUG(3,("smbldap_check_root_dse: nothing to look for\n"));
1616 if (!strequal(attrs
[0], "supportedExtension") &&
1617 !strequal(attrs
[0], "supportedControl") &&
1618 !strequal(attrs
[0], "namingContexts")) {
1619 DEBUG(3,("smbldap_check_root_dse: no idea what to query root-dse for: %s ?\n", attrs
[0]));
1623 rc
= ldap_search_s(ld
, "", LDAP_SCOPE_BASE
,
1624 "(objectclass=*)", CONST_DISCARD(char **, attrs
), 0 , &msg
);
1626 if (rc
!= LDAP_SUCCESS
) {
1627 DEBUG(3,("smbldap_check_root_dse: Could not search rootDSE\n"));
1631 num_result
= ldap_count_entries(ld
, msg
);
1633 if (num_result
!= 1) {
1634 DEBUG(3,("smbldap_check_root_dse: Expected one rootDSE, got %d\n", num_result
));
1638 entry
= ldap_first_entry(ld
, msg
);
1640 if (entry
== NULL
) {
1641 DEBUG(3,("smbldap_check_root_dse: Could not retrieve rootDSE\n"));
1645 values
= ldap_get_values(ld
, entry
, attrs
[0]);
1647 if (values
== NULL
) {
1648 DEBUG(5,("smbldap_check_root_dse: LDAP Server does not support any %s\n", attrs
[0]));
1652 num_values
= ldap_count_values(values
);
1654 if (num_values
== 0) {
1655 DEBUG(5,("smbldap_check_root_dse: LDAP Server does not have any %s\n", attrs
[0]));
1659 for (i
=0; i
<num_values
; i
++) {
1660 if (strcmp(values
[i
], value
) == 0)
1667 ldap_value_free(values
);
1675 /*******************************************************************
1676 Check if LDAP-Server supports a certain Control (OID in string format)
1677 ********************************************************************/
1679 BOOL
smbldap_has_control(LDAP
*ld
, const char *control
)
1681 const char *attrs
[] = { "supportedControl", NULL
};
1682 return smbldap_check_root_dse(ld
, attrs
, control
);
1685 /*******************************************************************
1686 Check if LDAP-Server supports a certain Extension (OID in string format)
1687 ********************************************************************/
1689 BOOL
smbldap_has_extension(LDAP
*ld
, const char *extension
)
1691 const char *attrs
[] = { "supportedExtension", NULL
};
1692 return smbldap_check_root_dse(ld
, attrs
, extension
);
1695 /*******************************************************************
1696 Check if LDAP-Server holds a given namingContext
1697 ********************************************************************/
1699 BOOL
smbldap_has_naming_context(LDAP
*ld
, const char *naming_context
)
1701 const char *attrs
[] = { "namingContexts", NULL
};
1702 return smbldap_check_root_dse(ld
, attrs
, naming_context
);