2 Unix SMB/CIFS Implementation.
3 LDAP protocol helper functions for SAMBA
4 Copyright (C) Volker Lendecke 2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/ldb/include/ldb.h"
27 enum ldap_request_tag {
28 LDAP_TAG_BindRequest = 0,
29 LDAP_TAG_BindResponse = 1,
30 LDAP_TAG_UnbindRequest = 2,
31 LDAP_TAG_SearchRequest = 3,
32 LDAP_TAG_SearchResultEntry = 4,
33 LDAP_TAG_SearchResultDone = 5,
34 LDAP_TAG_ModifyRequest = 6,
35 LDAP_TAG_ModifyResponse = 7,
36 LDAP_TAG_AddRequest = 8,
37 LDAP_TAG_AddResponse = 9,
38 LDAP_TAG_DelRequest = 10,
39 LDAP_TAG_DelResponse = 11,
40 LDAP_TAG_ModifyDNRequest = 12,
41 LDAP_TAG_ModifyDNResponse = 13,
42 LDAP_TAG_CompareRequest = 14,
43 LDAP_TAG_CompareResponse = 15,
44 LDAP_TAG_AbandonRequest = 16,
45 LDAP_TAG_SearchResultReference = 19,
46 LDAP_TAG_ExtendedRequest = 23,
47 LDAP_TAG_ExtendedResponse = 24
50 enum ldap_auth_mechanism {
51 LDAP_AUTH_MECH_SIMPLE = 0,
52 LDAP_AUTH_MECH_SASL = 3
55 enum ldap_result_code {
57 LDAP_OPERATIONS_ERROR = 1,
58 LDAP_PROTOCOL_ERROR = 2,
59 LDAP_TIME_LIMIT_EXCEEDED = 3,
60 LDAP_SIZE_LIMIT_EXCEEDED = 4,
61 LDAP_COMPARE_FALSE = 5,
62 LDAP_COMPARE_TRUE = 6,
63 LDAP_AUTH_METHOD_NOT_SUPPORTED = 7,
64 LDAP_STRONG_AUTH_REQUIRED = 8,
66 LDAP_ADMIN_LIMIT_EXCEEDED = 11,
67 LDAP_UNAVAILABLE_CRITICAL_EXTENSION = 12,
68 LDAP_CONFIDENTIALITY_REQUIRED = 13,
69 LDAP_SASL_BIND_IN_PROGRESS = 14,
70 LDAP_NO_SUCH_ATTRIBUTE = 16,
71 LDAP_UNDEFINED_ATTRIBUTE_TYPE = 17,
72 LDAP_INAPPROPRIATE_MATCHING = 18,
73 LDAP_CONSTRAINT_VIOLATION = 19,
74 LDAP_ATTRIBUTE_OR_VALUE_EXISTS = 20,
75 LDAP_INVALID_ATTRIBUTE_SYNTAX = 21,
76 LDAP_NO_SUCH_OBJECT = 32,
77 LDAP_ALIAS_PROBLEM = 33,
78 LDAP_INVALID_DN_SYNTAX = 34,
79 LDAP_ALIAS_DEREFERENCING_PROBLEM = 36,
80 LDAP_INAPPROPRIATE_AUTHENTICATION = 48,
81 LDAP_INVALID_CREDENTIALS = 49,
82 LDAP_INSUFFICIENT_ACCESS_RIGHTs = 50,
84 LDAP_UNAVAILABLE = 52,
85 LDAP_UNWILLING_TO_PERFORM = 53,
86 LDAP_LOOP_DETECT = 54,
87 LDAP_NAMING_VIOLATION = 64,
88 LDAP_OBJECT_CLASS_VIOLATION = 65,
89 LDAP_NOT_ALLOWED_ON_NON_LEAF = 66,
90 LDAP_NOT_ALLOWED_ON_RDN = 67,
91 LDAP_ENTRY_ALREADY_EXISTS = 68,
92 LDAP_OBJECT_CLASS_MODS_PROHIBITED = 69,
93 LDAP_AFFECTS_MULTIPLE_DSAS = 71,
100 const char *errormessage;
101 const char *referral;
104 struct ldap_attribute {
110 struct ldap_BindRequest {
113 enum ldap_auth_mechanism mechanism;
115 const char *password;
117 const char *mechanism;
123 struct ldap_BindResponse {
124 struct ldap_Result response;
130 struct ldap_UnbindRequest {
135 LDAP_SEARCH_SCOPE_BASE = 0,
136 LDAP_SEARCH_SCOPE_SINGLE = 1,
137 LDAP_SEARCH_SCOPE_SUB = 2
141 LDAP_DEREFERENCE_NEVER = 0,
142 LDAP_DEREFERENCE_IN_SEARCHING = 1,
143 LDAP_DEREFERENCE_FINDING_BASE = 2,
144 LDAP_DEREFERENCE_ALWAYS
147 struct ldap_SearchRequest {
149 enum ldap_scope scope;
150 enum ldap_deref deref;
154 struct ldb_parse_tree *tree;
156 const char **attributes;
159 struct ldap_SearchResEntry {
162 struct ldap_attribute *attributes;
165 struct ldap_SearchResRef {
166 const char *referral;
169 enum ldap_modify_type {
170 LDAP_MODIFY_NONE = -1,
172 LDAP_MODIFY_DELETE = 1,
173 LDAP_MODIFY_REPLACE = 2
177 enum ldap_modify_type type;
178 struct ldap_attribute attrib;
181 struct ldap_ModifyRequest {
184 struct ldap_mod *mods;
187 struct ldap_AddRequest {
190 struct ldap_attribute *attributes;
193 struct ldap_DelRequest {
197 struct ldap_ModifyDNRequest {
201 const char *newsuperior;
204 struct ldap_CompareRequest {
206 const char *attribute;
210 struct ldap_AbandonRequest {
214 struct ldap_ExtendedRequest {
219 struct ldap_ExtendedResponse {
220 struct ldap_Result response;
226 struct ldap_BindRequest BindRequest;
227 struct ldap_BindResponse BindResponse;
228 struct ldap_UnbindRequest UnbindRequest;
229 struct ldap_SearchRequest SearchRequest;
230 struct ldap_SearchResEntry SearchResultEntry;
231 struct ldap_Result SearchResultDone;
232 struct ldap_SearchResRef SearchResultReference;
233 struct ldap_ModifyRequest ModifyRequest;
234 struct ldap_Result ModifyResponse;
235 struct ldap_AddRequest AddRequest;
236 struct ldap_Result AddResponse;
237 struct ldap_DelRequest DelRequest;
238 struct ldap_Result DelResponse;
239 struct ldap_ModifyDNRequest ModifyDNRequest;
240 struct ldap_Result ModifyDNResponse;
241 struct ldap_CompareRequest CompareRequest;
242 struct ldap_Result CompareResponse;
243 struct ldap_AbandonRequest AbandonRequest;
244 struct ldap_ExtendedRequest ExtendedRequest;
245 struct ldap_ExtendedResponse ExtendedResponse;
248 struct ldap_Control {
254 struct ldap_message {
257 enum ldap_request_tag type;
258 union ldap_Request r;
260 struct ldap_Control *controls;
263 struct ldap_queue_entry {
264 struct ldap_queue_entry *next, *prev;
266 struct ldap_message *msg;
269 struct ldap_connection {
278 const char *simple_pw;
280 /* Current outstanding search entry */
283 /* List for incoming search entries */
284 struct ldap_queue_entry *search_entries;
286 /* Outstanding LDAP requests that have not yet been replied to */
287 struct ldap_queue_entry *outstanding;
289 /* Let's support SASL */
290 struct gensec_security *gensec;
293 #define LDAP_CONNECTION_TIMEOUT 10000
295 /* The following definitions come from libcli/ldap/ldap.c */
297 BOOL ldap_encode(struct ldap_message *msg, DATA_BLOB *result);
298 BOOL ldap_decode(struct asn1_data *data, struct ldap_message *msg);
299 BOOL ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
300 char **host, uint16_t *port, BOOL *ldaps);
302 /* The following definitions come from libcli/ldap/ldap_client.c */
304 struct ldap_connection *ldap_connect(TALLOC_CTX *mem_ctx, const char *url);
305 struct ldap_message *new_ldap_message(TALLOC_CTX *mem_ctx);
306 BOOL ldap_send_msg(struct ldap_connection *conn, struct ldap_message *msg,
307 const struct timeval *endtime);
308 BOOL ldap_receive_msg(struct ldap_connection *conn, struct ldap_message *msg,
309 const struct timeval *endtime);
310 struct ldap_message *ldap_receive(struct ldap_connection *conn, int msgid,
311 const struct timeval *endtime);
312 struct ldap_message *ldap_transaction(struct ldap_connection *conn,
313 struct ldap_message *request);
314 int ldap_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password);
315 int ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds);
316 struct ldap_connection *ldap_setup_connection(TALLOC_CTX *mem_ctx, const char *url,
317 const char *userdn, const char *password);
318 struct ldap_connection *ldap_setup_connection_with_sasl(TALLOC_CTX *mem_ctx, const char *url,
319 struct cli_credentials *creds);
320 BOOL ldap_abandon_message(struct ldap_connection *conn, int msgid,
321 const struct timeval *endtime);
322 BOOL ldap_setsearchent(struct ldap_connection *conn, struct ldap_message *msg,
323 const struct timeval *endtime);
324 struct ldap_message *ldap_getsearchent(struct ldap_connection *conn,
325 const struct timeval *endtime);
326 void ldap_endsearchent(struct ldap_connection *conn,
327 const struct timeval *endtime);
328 struct ldap_message *ldap_searchone(struct ldap_connection *conn,
329 struct ldap_message *msg,
330 const struct timeval *endtime);
331 BOOL ldap_find_single_value(struct ldap_message *msg, const char *attr,
333 BOOL ldap_find_single_string(struct ldap_message *msg, const char *attr,
334 TALLOC_CTX *mem_ctx, char **value);
335 BOOL ldap_find_single_int(struct ldap_message *msg, const char *attr,
337 int ldap_error(struct ldap_connection *conn);
338 NTSTATUS ldap2nterror(int ldaperror);
340 /* The following definitions come from libcli/ldap/ldap_ldif.c */
342 BOOL add_value_to_attrib(TALLOC_CTX *mem_ctx, struct ldb_val *value,
343 struct ldap_attribute *attrib);
344 BOOL add_attrib_to_array_talloc(TALLOC_CTX *mem_ctx,
345 const struct ldap_attribute *attrib,
346 struct ldap_attribute **attribs,
348 BOOL add_mod_to_array_talloc(TALLOC_CTX *mem_ctx,
349 struct ldap_mod *mod,
350 struct ldap_mod **mods,
352 struct ldap_message *ldap_ldif2msg(TALLOC_CTX *mem_ctx, const char *s);
354 /* The following definitions come from libcli/ldap/ldap_ndr.c */
356 const char *ldap_encode_ndr_uint32(TALLOC_CTX *mem_ctx, uint32_t value);
357 const char *ldap_encode_ndr_dom_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid);
358 const char *ldap_encode_ndr_GUID(TALLOC_CTX *mem_ctx, struct GUID *guid);
359 NTSTATUS ldap_decode_ndr_GUID(TALLOC_CTX *mem_ctx, struct ldb_val val, struct GUID *guid);