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 DEBUG(0, ("make_a_mod: out of memory!\n"));
417 for (i
= 0; mods
[i
] != NULL
; ++i
) {
418 if (mods
[i
]->mod_op
== modop
&& strequal(mods
[i
]->mod_type
, attribute
))
422 if (mods
[i
] == NULL
) {
423 mods
= SMB_REALLOC_ARRAY (mods
, LDAPMod
*, i
+ 2);
425 DEBUG(0, ("make_a_mod: out of memory!\n"));
428 mods
[i
] = SMB_MALLOC_P(LDAPMod
);
429 if (mods
[i
] == NULL
) {
430 DEBUG(0, ("make_a_mod: out of memory!\n"));
433 mods
[i
]->mod_op
= modop
;
434 mods
[i
]->mod_values
= NULL
;
435 mods
[i
]->mod_type
= SMB_STRDUP(attribute
);
440 char *utf8_value
= NULL
;
443 if (mods
[i
]->mod_values
!= NULL
) {
444 for (; mods
[i
]->mod_values
[j
] != NULL
; j
++);
446 mods
[i
]->mod_values
= SMB_REALLOC_ARRAY(mods
[i
]->mod_values
, char *, j
+ 2);
448 if (mods
[i
]->mod_values
== NULL
) {
449 DEBUG (0, ("make_a_mod: Memory allocation failure!\n"));
453 if (push_utf8_allocate(&utf8_value
, value
) == (size_t)-1) {
454 DEBUG (0, ("make_a_mod: String conversion failure!\n"));
458 mods
[i
]->mod_values
[j
] = utf8_value
;
460 mods
[i
]->mod_values
[j
+ 1] = NULL
;
465 /**********************************************************************
466 Set attribute to newval in LDAP, regardless of what value the
467 attribute had in LDAP before.
468 *********************************************************************/
470 void smbldap_make_mod(LDAP
*ldap_struct
, LDAPMessage
*existing
,
472 const char *attribute
, const char *newval
)
474 char oldval
[2048]; /* current largest allowed value is mungeddial */
477 if (attribute
== NULL
) {
478 /* This can actually happen for ldapsam_compat where we for
479 * example don't have a password history */
483 if (existing
!= NULL
) {
484 existed
= smbldap_get_single_attribute(ldap_struct
, existing
, attribute
, oldval
, sizeof(oldval
));
490 /* all of our string attributes are case insensitive */
492 if (existed
&& newval
&& (StrCaseCmp(oldval
, newval
) == 0)) {
494 /* Believe it or not, but LDAP will deny a delete and
495 an add at the same time if the values are the
497 DEBUG(10,("smbldap_make_mod: attribute |%s| not changed.\n", attribute
));
502 /* There has been no value before, so don't delete it.
503 * Here's a possible race: We might end up with
504 * duplicate attributes */
505 /* By deleting exactly the value we found in the entry this
506 * should be race-free in the sense that the LDAP-Server will
507 * deny the complete operation if somebody changed the
508 * attribute behind our back. */
509 /* This will also allow modifying single valued attributes
510 * in Novell NDS. In NDS you have to first remove attribute and then
511 * you could add new value */
513 DEBUG(10,("smbldap_make_mod: deleting attribute |%s| values |%s|\n", attribute
, oldval
));
514 smbldap_set_mod(mods
, LDAP_MOD_DELETE
, attribute
, oldval
);
517 /* Regardless of the real operation (add or modify)
518 we add the new value here. We rely on deleting
519 the old value, should it exist. */
521 if ((newval
!= NULL
) && (strlen(newval
) > 0)) {
522 DEBUG(10,("smbldap_make_mod: adding attribute |%s| value |%s|\n", attribute
, newval
));
523 smbldap_set_mod(mods
, LDAP_MOD_ADD
, attribute
, newval
);
527 /**********************************************************************
528 Some varients of the LDAP rebind code do not pass in the third 'arg'
529 pointer to a void*, so we try and work around it by assuming that the
530 value of the 'LDAP *' pointer is the same as the one we had passed in
531 **********************************************************************/
533 struct smbldap_state_lookup
{
535 struct smbldap_state
*smbldap_state
;
536 struct smbldap_state_lookup
*prev
, *next
;
539 static struct smbldap_state_lookup
*smbldap_state_lookup_list
;
541 static struct smbldap_state
*smbldap_find_state(LDAP
*ld
)
543 struct smbldap_state_lookup
*t
;
545 for (t
= smbldap_state_lookup_list
; t
; t
= t
->next
) {
547 return t
->smbldap_state
;
553 static void smbldap_delete_state(struct smbldap_state
*smbldap_state
)
555 struct smbldap_state_lookup
*t
;
557 for (t
= smbldap_state_lookup_list
; t
; t
= t
->next
) {
558 if (t
->smbldap_state
== smbldap_state
) {
559 DLIST_REMOVE(smbldap_state_lookup_list
, t
);
566 static void smbldap_store_state(LDAP
*ld
, struct smbldap_state
*smbldap_state
)
568 struct smbldap_state
*tmp_ldap_state
;
569 struct smbldap_state_lookup
*t
;
570 struct smbldap_state_lookup
*tmp
;
572 if ((tmp_ldap_state
= smbldap_find_state(ld
))) {
573 SMB_ASSERT(tmp_ldap_state
== smbldap_state
);
577 t
= SMB_XMALLOC_P(struct smbldap_state_lookup
);
580 DLIST_ADD_END(smbldap_state_lookup_list
, t
, tmp
);
582 t
->smbldap_state
= smbldap_state
;
585 /********************************************************************
586 start TLS on an existing LDAP connection
587 *******************************************************************/
589 int smb_ldap_start_tls(LDAP
*ldap_struct
, int version
)
593 if (lp_ldap_ssl() != LDAP_SSL_START_TLS
) {
597 #ifdef LDAP_OPT_X_TLS
598 if (version
!= LDAP_VERSION3
) {
599 DEBUG(0, ("Need LDAPv3 for Start TLS\n"));
600 return LDAP_OPERATIONS_ERROR
;
603 if ((rc
= ldap_start_tls_s (ldap_struct
, NULL
, NULL
)) != LDAP_SUCCESS
) {
604 DEBUG(0,("Failed to issue the StartTLS instruction: %s\n",
605 ldap_err2string(rc
)));
609 DEBUG (3, ("StartTLS issued: using a TLS connection\n"));
612 DEBUG(0,("StartTLS not supported by LDAP client libraries!\n"));
613 return LDAP_OPERATIONS_ERROR
;
617 /********************************************************************
618 setup a connection to the LDAP server based on a uri
619 *******************************************************************/
621 int smb_ldap_setup_conn(LDAP
**ldap_struct
, const char *uri
)
625 DEBUG(10, ("smb_ldap_setup_connection: %s\n", uri
));
627 #ifdef HAVE_LDAP_INITIALIZE
629 rc
= ldap_initialize(ldap_struct
, uri
);
631 DEBUG(0, ("ldap_initialize: %s\n", ldap_err2string(rc
)));
637 /* Parse the string manually */
643 SMB_ASSERT(sizeof(protocol
)>10 && sizeof(host
)>254);
646 /* skip leading "URL:" (if any) */
647 if ( strnequal( uri
, "URL:", 4 ) ) {
651 sscanf(uri
, "%10[^:]://%254[^:/]:%d", protocol
, host
, &port
);
654 if (strequal(protocol
, "ldap")) {
656 } else if (strequal(protocol
, "ldaps")) {
659 DEBUG(0, ("unrecognised protocol (%s)!\n", protocol
));
663 if ((*ldap_struct
= ldap_init(host
, port
)) == NULL
) {
664 DEBUG(0, ("ldap_init failed !\n"));
665 return LDAP_OPERATIONS_ERROR
;
668 if (strequal(protocol
, "ldaps")) {
669 #ifdef LDAP_OPT_X_TLS
670 int tls
= LDAP_OPT_X_TLS_HARD
;
671 if (ldap_set_option (*ldap_struct
, LDAP_OPT_X_TLS
, &tls
) != LDAP_SUCCESS
)
673 DEBUG(0, ("Failed to setup a TLS session\n"));
676 DEBUG(3,("LDAPS option set...!\n"));
678 DEBUG(0,("smbldap_open_connection: Secure connection not supported by LDAP client libraries!\n"));
679 return LDAP_OPERATIONS_ERROR
;
680 #endif /* LDAP_OPT_X_TLS */
684 #endif /* HAVE_LDAP_INITIALIZE */
688 /********************************************************************
689 try to upgrade to Version 3 LDAP if not already, in either case return current
691 *******************************************************************/
693 int smb_ldap_upgrade_conn(LDAP
*ldap_struct
, int *new_version
)
698 /* assume the worst */
699 *new_version
= LDAP_VERSION2
;
701 rc
= ldap_get_option(ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
706 if (version
== LDAP_VERSION3
) {
707 *new_version
= LDAP_VERSION3
;
712 version
= LDAP_VERSION3
;
713 rc
= ldap_set_option (ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
718 *new_version
= LDAP_VERSION3
;
722 /*******************************************************************
723 open a connection to the ldap server (just until the bind)
724 ******************************************************************/
726 int smb_ldap_setup_full_conn(LDAP
**ldap_struct
, const char *uri
)
730 rc
= smb_ldap_setup_conn(ldap_struct
, uri
);
735 rc
= smb_ldap_upgrade_conn(*ldap_struct
, &version
);
740 rc
= smb_ldap_start_tls(*ldap_struct
, version
);
748 /*******************************************************************
749 open a connection to the ldap server.
750 ******************************************************************/
751 static int smbldap_open_connection (struct smbldap_state
*ldap_state
)
754 int rc
= LDAP_SUCCESS
;
756 LDAP
**ldap_struct
= &ldap_state
->ldap_struct
;
758 rc
= smb_ldap_setup_conn(ldap_struct
, ldap_state
->uri
);
763 /* Store the LDAP pointer in a lookup list */
765 smbldap_store_state(*ldap_struct
, ldap_state
);
767 /* Upgrade to LDAPv3 if possible */
769 rc
= smb_ldap_upgrade_conn(*ldap_struct
, &version
);
774 /* Start TLS if required */
776 rc
= smb_ldap_start_tls(*ldap_struct
, version
);
781 DEBUG(2, ("smbldap_open_connection: connection opened\n"));
785 /*******************************************************************
786 a rebind function for authenticated referrals
787 This version takes a void* that we can shove useful stuff in :-)
788 ******************************************************************/
789 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
791 static int rebindproc_with_state (LDAP
* ld
, char **whop
, char **credp
,
792 int *methodp
, int freeit
, void *arg
)
794 struct smbldap_state
*ldap_state
= arg
;
796 /** @TODO Should we be doing something to check what servers we rebind to?
797 Could we get a referral to a machine that we don't want to give our
798 username and password to? */
802 memset(*credp
, '\0', strlen(*credp
));
805 DEBUG(5,("rebind_proc_with_state: Rebinding as \"%s\"\n",
806 ldap_state
->bind_dn
));
808 *whop
= SMB_STRDUP(ldap_state
->bind_dn
);
810 return LDAP_NO_MEMORY
;
812 *credp
= SMB_STRDUP(ldap_state
->bind_secret
);
815 return LDAP_NO_MEMORY
;
817 *methodp
= LDAP_AUTH_SIMPLE
;
820 GetTimeOfDay(&ldap_state
->last_rebind
);
824 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
826 /*******************************************************************
827 a rebind function for authenticated referrals
828 This version takes a void* that we can shove useful stuff in :-)
829 and actually does the connection.
830 ******************************************************************/
831 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
832 static int rebindproc_connect_with_state (LDAP
*ldap_struct
,
833 LDAP_CONST
char *url
,
835 ber_int_t msgid
, void *arg
)
837 struct smbldap_state
*ldap_state
= arg
;
841 DEBUG(5,("rebindproc_connect_with_state: Rebinding to %s as \"%s\"\n",
842 url
, ldap_state
->bind_dn
));
844 /* call START_TLS again (ldaps:// is handled by the OpenLDAP library
845 * itself) before rebinding to another LDAP server to avoid to expose
846 * our credentials. At least *try* to secure the connection - Guenther */
848 smb_ldap_upgrade_conn(ldap_struct
, &version
);
849 smb_ldap_start_tls(ldap_struct
, version
);
851 /** @TODO Should we be doing something to check what servers we rebind to?
852 Could we get a referral to a machine that we don't want to give our
853 username and password to? */
855 rc
= ldap_simple_bind_s(ldap_struct
, ldap_state
->bind_dn
, ldap_state
->bind_secret
);
857 /* only set the last rebind timestamp when we did rebind after a
858 * non-read LDAP operation. That way we avoid the replication sleep
859 * after a simple redirected search operation - Guenther */
863 case LDAP_REQ_MODIFY
:
865 case LDAP_REQ_DELETE
:
867 case LDAP_REQ_EXTENDED
:
868 DEBUG(10,("rebindproc_connect_with_state: "
869 "setting last_rebind timestamp "
870 "(req: 0x%02x)\n", request
));
871 GetTimeOfDay(&ldap_state
->last_rebind
);
874 ZERO_STRUCT(ldap_state
->last_rebind
);
880 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
882 /*******************************************************************
883 Add a rebind function for authenticated referrals
884 ******************************************************************/
885 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
887 # if LDAP_SET_REBIND_PROC_ARGS == 2
888 static int rebindproc (LDAP
*ldap_struct
, char **whop
, char **credp
,
889 int *method
, int freeit
)
891 struct smbldap_state
*ldap_state
= smbldap_find_state(ldap_struct
);
893 return rebindproc_with_state(ldap_struct
, whop
, credp
,
894 method
, freeit
, ldap_state
);
897 # endif /*LDAP_SET_REBIND_PROC_ARGS == 2*/
898 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
900 /*******************************************************************
901 a rebind function for authenticated referrals
902 this also does the connection, but no void*.
903 ******************************************************************/
904 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
905 # if LDAP_SET_REBIND_PROC_ARGS == 2
906 static int rebindproc_connect (LDAP
* ld
, LDAP_CONST
char *url
, int request
,
909 struct smbldap_state
*ldap_state
= smbldap_find_state(ld
);
911 return rebindproc_connect_with_state(ld
, url
, (ber_tag_t
)request
, msgid
,
914 # endif /*LDAP_SET_REBIND_PROC_ARGS == 2*/
915 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
917 /*******************************************************************
918 connect to the ldap server under system privilege.
919 ******************************************************************/
920 static int smbldap_connect_system(struct smbldap_state
*ldap_state
, LDAP
* ldap_struct
)
927 /* get the password */
928 if (!fetch_ldap_pw(&ldap_dn
, &ldap_secret
)) {
929 DEBUG(0, ("ldap_connect_system: Failed to retrieve password from secrets.tdb\n"));
930 return LDAP_INVALID_CREDENTIALS
;
933 ldap_state
->bind_dn
= ldap_dn
;
934 ldap_state
->bind_secret
= ldap_secret
;
936 /* removed the sasl_bind_s "EXTERNAL" stuff, as my testsuite
937 (OpenLDAP) doesnt' seem to support it */
939 DEBUG(10,("ldap_connect_system: Binding to ldap server %s as \"%s\"\n",
940 ldap_state
->uri
, ldap_dn
));
942 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
943 # if LDAP_SET_REBIND_PROC_ARGS == 2
944 ldap_set_rebind_proc(ldap_struct
, &rebindproc_connect
);
946 # if LDAP_SET_REBIND_PROC_ARGS == 3
947 ldap_set_rebind_proc(ldap_struct
, &rebindproc_connect_with_state
, (void *)ldap_state
);
949 #else /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
950 # if LDAP_SET_REBIND_PROC_ARGS == 2
951 ldap_set_rebind_proc(ldap_struct
, &rebindproc
);
953 # if LDAP_SET_REBIND_PROC_ARGS == 3
954 ldap_set_rebind_proc(ldap_struct
, &rebindproc_with_state
, (void *)ldap_state
);
956 #endif /*defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)*/
958 rc
= ldap_simple_bind_s(ldap_struct
, ldap_dn
, ldap_secret
);
960 if (rc
!= LDAP_SUCCESS
) {
961 char *ld_error
= NULL
;
962 ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_ERROR_STRING
,
964 DEBUG(ldap_state
->num_failures
? 2 : 0,
965 ("failed to bind to server %s with dn=\"%s\" Error: %s\n\t%s\n",
967 ldap_dn
? ldap_dn
: "(unknown)", ldap_err2string(rc
),
968 ld_error
? ld_error
: "(unknown)"));
970 ldap_state
->num_failures
++;
974 ldap_state
->num_failures
= 0;
975 ldap_state
->paged_results
= False
;
977 ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_PROTOCOL_VERSION
, &version
);
979 if (smbldap_has_control(ldap_state
, ADS_PAGE_CTL_OID
) && version
== 3) {
980 ldap_state
->paged_results
= True
;
983 DEBUG(3, ("ldap_connect_system: succesful connection to the LDAP server\n"));
984 DEBUGADD(10, ("ldap_connect_system: LDAP server %s support paged results\n",
985 ldap_state
->paged_results
? "does" : "does not"));
989 /**********************************************************************
990 Connect to LDAP server (called before every ldap operation)
991 *********************************************************************/
992 static int smbldap_open(struct smbldap_state
*ldap_state
)
996 SMB_ASSERT(ldap_state
);
998 #ifndef NO_LDAP_SECURITY
999 if (geteuid() != 0) {
1000 DEBUG(0, ("smbldap_open: cannot access LDAP when not root..\n"));
1001 return LDAP_INSUFFICIENT_ACCESS
;
1005 if ((ldap_state
->ldap_struct
!= NULL
) && ((ldap_state
->last_ping
+ SMBLDAP_DONT_PING_TIME
) < time(NULL
))) {
1007 struct sockaddr_un addr
;
1008 socklen_t len
= sizeof(addr
);
1011 opt_rc
= ldap_get_option(ldap_state
->ldap_struct
, LDAP_OPT_DESC
, &sd
);
1012 if (opt_rc
== 0 && (getpeername(sd
, (struct sockaddr
*) &addr
, &len
)) < 0 )
1015 #ifdef HAVE_UNIXSOCKET
1016 if (opt_rc
== 0 && addr
.sun_family
== AF_UNIX
)
1020 /* the other end has died. reopen. */
1021 ldap_unbind(ldap_state
->ldap_struct
);
1022 ldap_state
->ldap_struct
= NULL
;
1023 ldap_state
->last_ping
= (time_t)0;
1025 ldap_state
->last_ping
= time(NULL
);
1029 if (ldap_state
->ldap_struct
!= NULL
) {
1030 DEBUG(11,("smbldap_open: already connected to the LDAP server\n"));
1031 return LDAP_SUCCESS
;
1034 if ((rc
= smbldap_open_connection(ldap_state
))) {
1038 if ((rc
= smbldap_connect_system(ldap_state
, ldap_state
->ldap_struct
))) {
1039 ldap_unbind(ldap_state
->ldap_struct
);
1040 ldap_state
->ldap_struct
= NULL
;
1045 ldap_state
->last_ping
= time(NULL
);
1046 ldap_state
->pid
= sys_getpid();
1047 DEBUG(4,("The LDAP server is succesfully connected\n"));
1049 return LDAP_SUCCESS
;
1052 /**********************************************************************
1053 Disconnect from LDAP server
1054 *********************************************************************/
1055 static NTSTATUS
smbldap_close(struct smbldap_state
*ldap_state
)
1058 return NT_STATUS_INVALID_PARAMETER
;
1060 if (ldap_state
->ldap_struct
!= NULL
) {
1061 ldap_unbind(ldap_state
->ldap_struct
);
1062 ldap_state
->ldap_struct
= NULL
;
1065 smbldap_delete_state(ldap_state
);
1067 DEBUG(5,("The connection to the LDAP server was closed\n"));
1068 /* maybe free the results here --metze */
1072 return NT_STATUS_OK
;
1075 static BOOL got_alarm
;
1077 static void (*old_handler
)(int);
1079 static void gotalarm_sig(int dummy
)
1084 static int another_ldap_try(struct smbldap_state
*ldap_state
, int *rc
,
1085 int *attempts
, time_t endtime
)
1087 time_t now
= time(NULL
);
1088 int open_rc
= LDAP_SERVER_DOWN
;
1090 if (*rc
!= LDAP_SERVER_DOWN
)
1093 if (now
>= endtime
) {
1094 smbldap_close(ldap_state
);
1099 if (*attempts
== 0) {
1101 old_handler
= CatchSignal(SIGALRM
, gotalarm_sig
);
1102 alarm(endtime
- now
);
1104 if (ldap_state
->pid
!= sys_getpid())
1105 smbldap_close(ldap_state
);
1115 open_rc
= smbldap_open(ldap_state
);
1117 if (open_rc
== LDAP_SUCCESS
) {
1118 ldap_state
->last_use
= now
;
1122 if (open_rc
== LDAP_INSUFFICIENT_ACCESS
) {
1123 /* The fact that we are non-root or any other
1124 * access-denied condition will not change in the next
1125 * round of trying */
1135 if (open_rc
!= LDAP_SUCCESS
) {
1136 DEBUG(1, ("Connection to LDAP server failed for the "
1137 "%d try!\n", *attempts
));
1142 CatchSignal(SIGALRM
, old_handler
);
1144 ldap_state
->last_use
= now
;
1148 /*********************************************************************
1149 ********************************************************************/
1151 static int smbldap_search_ext(struct smbldap_state
*ldap_state
,
1152 const char *base
, int scope
, const char *filter
,
1153 const char *attrs
[], int attrsonly
,
1154 LDAPControl
**sctrls
, LDAPControl
**cctrls
,
1155 int sizelimit
, LDAPMessage
**res
)
1157 int rc
= LDAP_SERVER_DOWN
;
1160 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1161 struct timeval timeout
;
1163 SMB_ASSERT(ldap_state
);
1165 DEBUG(5,("smbldap_search_ext: base => [%s], filter => [%s], "
1166 "scope => [%d]\n", base
, filter
, scope
));
1168 if (ldap_state
->last_rebind
.tv_sec
> 0) {
1169 struct timeval tval
;
1170 SMB_BIG_INT tdiff
= 0;
1174 GetTimeOfDay(&tval
);
1176 tdiff
= usec_time_diff(&tval
, &ldap_state
->last_rebind
);
1177 tdiff
/= 1000; /* Convert to milliseconds. */
1179 sleep_time
= lp_ldap_replication_sleep()-(int)tdiff
;
1180 sleep_time
= MIN(sleep_time
, MAX_LDAP_REPLICATION_SLEEP_TIME
);
1182 if (sleep_time
> 0) {
1183 /* we wait for the LDAP replication */
1184 DEBUG(5,("smbldap_search_ext: waiting %d milliseconds "
1185 "for LDAP replication.\n",sleep_time
));
1186 smb_msleep(sleep_time
);
1187 DEBUG(5,("smbldap_search_ext: go on!\n"));
1189 ZERO_STRUCT(ldap_state
->last_rebind
);
1192 if (push_utf8_allocate(&utf8_filter
, filter
) == (size_t)-1) {
1193 return LDAP_NO_MEMORY
;
1196 /* Setup timeout for the ldap_search_ext_s call - local and remote. */
1197 timeout
.tv_sec
= lp_ldap_timeout();
1198 timeout
.tv_usec
= 0;
1200 /* Setup alarm timeout.... Do we need both of these ? JRA.
1201 * Yes, I think we do need both of these. The server timeout only
1202 * covers the case where the server's operation takes too long. It
1203 * does not cover the case where the request hangs on its way to the
1204 * server. The server side timeout is not strictly necessary, it's
1205 * just a bit more kind to the server. VL. */
1208 CatchSignal(SIGALRM
, SIGNAL_CAST gotalarm_sig
);
1209 alarm(lp_ldap_timeout());
1210 /* End setup timeout. */
1212 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1213 rc
= ldap_search_ext_s(ldap_state
->ldap_struct
, base
, scope
,
1215 CONST_DISCARD(char **, attrs
),
1216 attrsonly
, sctrls
, cctrls
, &timeout
,
1218 if (rc
!= LDAP_SUCCESS
) {
1219 char *ld_error
= NULL
;
1220 ldap_get_option(ldap_state
->ldap_struct
,
1221 LDAP_OPT_ERROR_STRING
, &ld_error
);
1222 DEBUG(10,("Failed search for base: %s, error: %s "
1223 "(%s)\n", base
, ldap_err2string(rc
),
1224 ld_error
? ld_error
: "unknown"));
1225 SAFE_FREE(ld_error
);
1229 SAFE_FREE(utf8_filter
);
1231 /* Teardown timeout. */
1232 CatchSignal(SIGALRM
, SIGNAL_CAST SIG_IGN
);
1236 return LDAP_TIMELIMIT_EXCEEDED
;
1241 int smbldap_search(struct smbldap_state
*ldap_state
,
1242 const char *base
, int scope
, const char *filter
,
1243 const char *attrs
[], int attrsonly
,
1246 return smbldap_search_ext(ldap_state
, base
, scope
, filter
, attrs
,
1247 attrsonly
, NULL
, NULL
, LDAP_NO_LIMIT
, res
);
1250 int smbldap_search_paged(struct smbldap_state
*ldap_state
,
1251 const char *base
, int scope
, const char *filter
,
1252 const char **attrs
, int attrsonly
, int pagesize
,
1253 LDAPMessage
**res
, void **cookie
)
1256 LDAPControl
**rcontrols
;
1257 LDAPControl
*controls
[2] = { NULL
, NULL
};
1258 BerElement
*cookie_be
= NULL
;
1259 struct berval
*cookie_bv
= NULL
;
1261 BOOL critical
= True
;
1265 DEBUG(3,("smbldap_search_paged: base => [%s], filter => [%s],"
1266 "scope => [%d], pagesize => [%d]\n",
1267 base
, filter
, scope
, pagesize
));
1269 cookie_be
= ber_alloc_t(LBER_USE_DER
);
1270 if (cookie_be
== NULL
) {
1271 DEBUG(0,("smbldap_create_page_control: ber_alloc_t returns "
1273 return LDAP_NO_MEMORY
;
1276 /* construct cookie */
1277 if (*cookie
!= NULL
) {
1278 ber_printf(cookie_be
, "{iO}", (ber_int_t
) pagesize
, *cookie
);
1279 ber_bvfree(*cookie
); /* don't need it from last time */
1282 ber_printf(cookie_be
, "{io}", (ber_int_t
) pagesize
, "", 0);
1284 ber_flatten(cookie_be
, &cookie_bv
);
1286 pr
.ldctl_oid
= CONST_DISCARD(char *, ADS_PAGE_CTL_OID
);
1287 pr
.ldctl_iscritical
= (char) critical
;
1288 pr
.ldctl_value
.bv_len
= cookie_bv
->bv_len
;
1289 pr
.ldctl_value
.bv_val
= cookie_bv
->bv_val
;
1294 rc
= smbldap_search_ext(ldap_state
, base
, scope
, filter
, attrs
,
1295 0, controls
, NULL
, LDAP_NO_LIMIT
, res
);
1297 ber_free(cookie_be
, 1);
1298 ber_bvfree(cookie_bv
);
1301 DEBUG(3,("smbldap_search_paged: smbldap_search_ext(%s) "
1302 "failed with [%s]\n", filter
, ldap_err2string(rc
)));
1306 DEBUG(3,("smbldap_search_paged: search was successfull\n"));
1308 rc
= ldap_parse_result(ldap_state
->ldap_struct
, *res
, NULL
, NULL
,
1309 NULL
, NULL
, &rcontrols
, 0);
1311 DEBUG(3,("smbldap_search_paged: ldap_parse_result failed " \
1312 "with [%s]\n", ldap_err2string(rc
)));
1316 if (rcontrols
== NULL
)
1319 for (i
=0; rcontrols
[i
]; i
++) {
1321 if (strcmp(ADS_PAGE_CTL_OID
, rcontrols
[i
]->ldctl_oid
) != 0)
1324 cookie_be
= ber_init(&rcontrols
[i
]->ldctl_value
);
1325 ber_scanf(cookie_be
,"{iO}", &tmp
, &cookie_bv
);
1326 /* the berval is the cookie, but must be freed when it is all
1328 if (cookie_bv
->bv_len
)
1329 *cookie
=ber_bvdup(cookie_bv
);
1332 ber_bvfree(cookie_bv
);
1333 ber_free(cookie_be
, 1);
1336 ldap_controls_free(rcontrols
);
1341 int smbldap_modify(struct smbldap_state
*ldap_state
, const char *dn
, LDAPMod
*attrs
[])
1343 int rc
= LDAP_SERVER_DOWN
;
1346 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1348 SMB_ASSERT(ldap_state
);
1350 DEBUG(5,("smbldap_modify: dn => [%s]\n", dn
));
1352 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1353 return LDAP_NO_MEMORY
;
1356 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1357 rc
= ldap_modify_s(ldap_state
->ldap_struct
, utf8_dn
, attrs
);
1358 if (rc
!= LDAP_SUCCESS
) {
1359 char *ld_error
= NULL
;
1360 ldap_get_option(ldap_state
->ldap_struct
,
1361 LDAP_OPT_ERROR_STRING
, &ld_error
);
1362 DEBUG(10,("Failed to modify dn: %s, error: %s "
1363 "(%s)\n", dn
, ldap_err2string(rc
),
1364 ld_error
? ld_error
: "unknown"));
1365 SAFE_FREE(ld_error
);
1373 int smbldap_add(struct smbldap_state
*ldap_state
, const char *dn
, LDAPMod
*attrs
[])
1375 int rc
= LDAP_SERVER_DOWN
;
1378 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1380 SMB_ASSERT(ldap_state
);
1382 DEBUG(5,("smbldap_add: dn => [%s]\n", dn
));
1384 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1385 return LDAP_NO_MEMORY
;
1388 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1389 rc
= ldap_add_s(ldap_state
->ldap_struct
, utf8_dn
, attrs
);
1390 if (rc
!= LDAP_SUCCESS
) {
1391 char *ld_error
= NULL
;
1392 ldap_get_option(ldap_state
->ldap_struct
,
1393 LDAP_OPT_ERROR_STRING
, &ld_error
);
1394 DEBUG(10,("Failed to add dn: %s, error: %s "
1395 "(%s)\n", dn
, ldap_err2string(rc
),
1396 ld_error
? ld_error
: "unknown"));
1397 SAFE_FREE(ld_error
);
1405 int smbldap_delete(struct smbldap_state
*ldap_state
, const char *dn
)
1407 int rc
= LDAP_SERVER_DOWN
;
1410 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1412 SMB_ASSERT(ldap_state
);
1414 DEBUG(5,("smbldap_delete: dn => [%s]\n", dn
));
1416 if (push_utf8_allocate(&utf8_dn
, dn
) == (size_t)-1) {
1417 return LDAP_NO_MEMORY
;
1420 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1421 rc
= ldap_delete_s(ldap_state
->ldap_struct
, utf8_dn
);
1422 if (rc
!= LDAP_SUCCESS
) {
1423 char *ld_error
= NULL
;
1424 ldap_get_option(ldap_state
->ldap_struct
,
1425 LDAP_OPT_ERROR_STRING
, &ld_error
);
1426 DEBUG(10,("Failed to delete dn: %s, error: %s "
1427 "(%s)\n", dn
, ldap_err2string(rc
),
1428 ld_error
? ld_error
: "unknown"));
1429 SAFE_FREE(ld_error
);
1437 int smbldap_extended_operation(struct smbldap_state
*ldap_state
,
1438 LDAP_CONST
char *reqoid
, struct berval
*reqdata
,
1439 LDAPControl
**serverctrls
, LDAPControl
**clientctrls
,
1440 char **retoidp
, struct berval
**retdatap
)
1442 int rc
= LDAP_SERVER_DOWN
;
1444 time_t endtime
= time(NULL
)+lp_ldap_timeout();
1449 while (another_ldap_try(ldap_state
, &rc
, &attempts
, endtime
)) {
1450 rc
= ldap_extended_operation_s(ldap_state
->ldap_struct
, reqoid
,
1451 reqdata
, serverctrls
,
1452 clientctrls
, retoidp
, retdatap
);
1453 if (rc
!= LDAP_SUCCESS
) {
1454 char *ld_error
= NULL
;
1455 ldap_get_option(ldap_state
->ldap_struct
,
1456 LDAP_OPT_ERROR_STRING
, &ld_error
);
1457 DEBUG(10,("Extended operation failed with error: %s "
1458 "(%s)\n", ldap_err2string(rc
),
1459 ld_error
? ld_error
: "unknown"));
1460 SAFE_FREE(ld_error
);
1467 /*******************************************************************
1468 run the search by name.
1469 ******************************************************************/
1470 int smbldap_search_suffix (struct smbldap_state
*ldap_state
,
1471 const char *filter
, const char **search_attr
,
1472 LDAPMessage
** result
)
1474 return smbldap_search(ldap_state
, lp_ldap_suffix(), LDAP_SCOPE_SUBTREE
,
1475 filter
, search_attr
, 0, result
);
1478 static void smbldap_idle_fn(void **data
, time_t *interval
, time_t now
)
1480 struct smbldap_state
*state
= (struct smbldap_state
*)(*data
);
1482 if (state
->ldap_struct
== NULL
) {
1483 DEBUG(10,("ldap connection not connected...\n"));
1487 if ((state
->last_use
+SMBLDAP_IDLE_TIME
) > now
) {
1488 DEBUG(10,("ldap connection not idle...\n"));
1492 DEBUG(7,("ldap connection idle...closing connection\n"));
1493 smbldap_close(state
);
1496 /**********************************************************************
1498 *********************************************************************/
1500 void smbldap_free_struct(struct smbldap_state
**ldap_state
)
1502 smbldap_close(*ldap_state
);
1504 if ((*ldap_state
)->bind_secret
) {
1505 memset((*ldap_state
)->bind_secret
, '\0', strlen((*ldap_state
)->bind_secret
));
1508 SAFE_FREE((*ldap_state
)->bind_dn
);
1509 SAFE_FREE((*ldap_state
)->bind_secret
);
1511 smb_unregister_idle_event((*ldap_state
)->event_id
);
1515 /* No need to free any further, as it is talloc()ed */
1519 /**********************************************************************
1520 Intitalise the 'general' ldap structures, on which ldap operations may be conducted
1521 *********************************************************************/
1523 NTSTATUS
smbldap_init(TALLOC_CTX
*mem_ctx
, const char *location
, struct smbldap_state
**smbldap_state
)
1525 *smbldap_state
= TALLOC_ZERO_P(mem_ctx
, struct smbldap_state
);
1526 if (!*smbldap_state
) {
1527 DEBUG(0, ("talloc() failed for ldapsam private_data!\n"));
1528 return NT_STATUS_NO_MEMORY
;
1532 (*smbldap_state
)->uri
= talloc_strdup(mem_ctx
, location
);
1534 (*smbldap_state
)->uri
= "ldap://localhost";
1537 (*smbldap_state
)->event_id
=
1538 smb_register_idle_event(smbldap_idle_fn
, (void *)(*smbldap_state
),
1541 if ((*smbldap_state
)->event_id
== SMB_EVENT_ID_INVALID
) {
1542 DEBUG(0,("Failed to register LDAP idle event!\n"));
1543 return NT_STATUS_INVALID_HANDLE
;
1546 return NT_STATUS_OK
;
1549 /*******************************************************************
1550 Return a copy of the DN for a LDAPMessage. Convert from utf8 to CH_UNIX.
1551 ********************************************************************/
1553 char *smbldap_get_dn(LDAP
*ld
, LDAPMessage
*entry
)
1555 char *utf8_dn
, *unix_dn
;
1557 utf8_dn
= ldap_get_dn(ld
, entry
);
1559 DEBUG (5, ("smbldap_get_dn: ldap_get_dn failed\n"));
1562 if (pull_utf8_allocate(&unix_dn
, utf8_dn
) == (size_t)-1) {
1563 DEBUG (0, ("smbldap_get_dn: String conversion failure utf8 [%s]\n", utf8_dn
));
1566 ldap_memfree(utf8_dn
);
1570 const char *smbldap_talloc_dn(TALLOC_CTX
*mem_ctx
, LDAP
*ld
,
1573 char *utf8_dn
, *unix_dn
;
1575 utf8_dn
= ldap_get_dn(ld
, entry
);
1577 DEBUG (5, ("smbldap_get_dn: ldap_get_dn failed\n"));
1580 if (pull_utf8_talloc(mem_ctx
, &unix_dn
, utf8_dn
) == (size_t)-1) {
1581 DEBUG (0, ("smbldap_get_dn: String conversion failure utf8 "
1582 "[%s]\n", utf8_dn
));
1585 ldap_memfree(utf8_dn
);
1589 /*******************************************************************
1590 Check if root-dse has a certain Control or Extension
1591 ********************************************************************/
1593 static BOOL
smbldap_check_root_dse(struct smbldap_state
*ldap_state
, const char **attrs
, const char *value
)
1595 LDAPMessage
*msg
= NULL
;
1596 LDAPMessage
*entry
= NULL
;
1597 char **values
= NULL
;
1598 int rc
, num_result
, num_values
, i
;
1599 BOOL result
= False
;
1602 DEBUG(3,("smbldap_check_root_dse: nothing to look for\n"));
1606 if (!strequal(attrs
[0], "supportedExtension") &&
1607 !strequal(attrs
[0], "supportedControl") &&
1608 !strequal(attrs
[0], "namingContexts")) {
1609 DEBUG(3,("smbldap_check_root_dse: no idea what to query root-dse for: %s ?\n", attrs
[0]));
1613 rc
= ldap_search_s(ldap_state
->ldap_struct
, "", LDAP_SCOPE_BASE
,
1614 "(objectclass=*)", CONST_DISCARD(char **, attrs
), 0 , &msg
);
1616 if (rc
!= LDAP_SUCCESS
) {
1617 DEBUG(3,("smbldap_check_root_dse: Could not search rootDSE\n"));
1621 num_result
= ldap_count_entries(ldap_state
->ldap_struct
, msg
);
1623 if (num_result
!= 1) {
1624 DEBUG(3,("smbldap_check_root_dse: Expected one rootDSE, got %d\n", num_result
));
1628 entry
= ldap_first_entry(ldap_state
->ldap_struct
, msg
);
1630 if (entry
== NULL
) {
1631 DEBUG(3,("smbldap_check_root_dse: Could not retrieve rootDSE\n"));
1635 values
= ldap_get_values(ldap_state
->ldap_struct
, entry
, attrs
[0]);
1637 if (values
== NULL
) {
1638 DEBUG(5,("smbldap_check_root_dse: LDAP Server does not support any %s\n", attrs
[0]));
1642 num_values
= ldap_count_values(values
);
1644 if (num_values
== 0) {
1645 DEBUG(5,("smbldap_check_root_dse: LDAP Server does not have any %s\n", attrs
[0]));
1649 for (i
=0; i
<num_values
; i
++) {
1650 if (strcmp(values
[i
], value
) == 0)
1657 ldap_value_free(values
);
1665 /*******************************************************************
1666 Check if LDAP-Server supports a certain Control (OID in string format)
1667 ********************************************************************/
1669 BOOL
smbldap_has_control(struct smbldap_state
*ldap_state
, const char *control
)
1671 const char *attrs
[] = { "supportedControl", NULL
};
1672 return smbldap_check_root_dse(ldap_state
, attrs
, control
);
1675 /*******************************************************************
1676 Check if LDAP-Server supports a certain Extension (OID in string format)
1677 ********************************************************************/
1679 BOOL
smbldap_has_extension(struct smbldap_state
*ldap_state
, const char *extension
)
1681 const char *attrs
[] = { "supportedExtension", NULL
};
1682 return smbldap_check_root_dse(ldap_state
, attrs
, extension
);
1685 /*******************************************************************
1686 Check if LDAP-Server holds a given namingContext
1687 ********************************************************************/
1689 BOOL
smbldap_has_naming_context(struct smbldap_state
*ldap_state
, const char *naming_context
)
1691 const char *attrs
[] = { "namingContexts", NULL
};
1692 return smbldap_check_root_dse(ldap_state
, attrs
, naming_context
);