r5305: removed libcli/ldap/ldap.h from includes.h
[metze/samba/wip.git] / source4 / libcli / ldap / ldap.h
1 /* 
2    Unix SMB/CIFS Implementation.
3    LDAP protocol helper functions for SAMBA
4    Copyright (C) Volker Lendecke 2004
5     
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.
10    
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.
15    
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.
19    
20 */
21
22 #ifndef _SMB_LDAP_H
23 #define _SMB_LDAP_H
24
25 enum ldap_request_tag {
26         LDAP_TAG_BindRequest = 0,
27         LDAP_TAG_BindResponse = 1,
28         LDAP_TAG_UnbindRequest = 2,
29         LDAP_TAG_SearchRequest = 3,
30         LDAP_TAG_SearchResultEntry = 4,
31         LDAP_TAG_SearchResultDone = 5,
32         LDAP_TAG_ModifyRequest = 6,
33         LDAP_TAG_ModifyResponse = 7,
34         LDAP_TAG_AddRequest = 8,
35         LDAP_TAG_AddResponse = 9,
36         LDAP_TAG_DelRequest = 10,
37         LDAP_TAG_DelResponse = 11,
38         LDAP_TAG_ModifyDNRequest = 12,
39         LDAP_TAG_ModifyDNResponse = 13,
40         LDAP_TAG_CompareRequest = 14,
41         LDAP_TAG_CompareResponse = 15,
42         LDAP_TAG_AbandonRequest = 16,
43         LDAP_TAG_SearchResultReference = 19,
44         LDAP_TAG_ExtendedRequest = 23,
45         LDAP_TAG_ExtendedResponse = 24
46 };
47
48 enum ldap_auth_mechanism {
49         LDAP_AUTH_MECH_SIMPLE = 0,
50         LDAP_AUTH_MECH_SASL = 3
51 };
52
53 enum ldap_result_code {
54         LDAP_SUCCESS                            = 0,
55         LDAP_OPERATIONS_ERROR                   = 1,
56         LDAP_PROTOCOL_ERROR                     = 2,
57         LDAP_TIME_LIMIT_EXCEEDED                = 3,
58         LDAP_SIZE_LIMIT_EXCEEDED                = 4,
59         LDAP_COMPARE_FALSE                      = 5,
60         LDAP_COMPARE_TRUE                       = 6,
61         LDAP_AUTH_METHOD_NOT_SUPPORTED          = 7,
62         LDAP_STRONG_AUTH_REQUIRED               = 8,
63         LDAP_REFERRAL                           = 10,
64         LDAP_ADMIN_LIMIT_EXCEEDED               = 11,
65         LDAP_UNAVAILABLE_CRITICAL_EXTENSION     = 12,
66         LDAP_CONFIDENTIALITY_REQUIRED           = 13,
67         LDAP_SASL_BIND_IN_PROGRESS              = 14,
68         LDAP_NO_SUCH_ATTRIBUTE                  = 16,
69         LDAP_UNDEFINED_ATTRIBUTE_TYPE           = 17,
70         LDAP_INAPPROPRIATE_MATCHING             = 18,
71         LDAP_CONSTRAINT_VIOLATION               = 19,
72         LDAP_ATTRIBUTE_OR_VALUE_EXISTS          = 20,
73         LDAP_INVALID_ATTRIBUTE_SYNTAX           = 21,
74         LDAP_NO_SUCH_OBJECT                     = 32,
75         LDAP_ALIAS_PROBLEM                      = 33,
76         LDAP_INVALID_DN_SYNTAX                  = 34,
77         LDAP_ALIAS_DEREFERENCING_PROBLEM        = 36,
78         LDAP_INAPPROPRIATE_AUTHENTICATION       = 48,
79         LDAP_INVALID_CREDENTIALS                = 49,
80         LDAP_INSUFFICIENT_ACCESS_RIGHTs         = 50,
81         LDAP_BUSY                               = 51,
82         LDAP_UNAVAILABLE                        = 52,
83         LDAP_UNWILLING_TO_PERFORM               = 53,
84         LDAP_LOOP_DETECT                        = 54,
85         LDAP_NAMING_VIOLATION                   = 64,
86         LDAP_OBJECT_CLASS_VIOLATION             = 65,
87         LDAP_NOT_ALLOWED_ON_NON_LEAF            = 66,
88         LDAP_NOT_ALLOWED_ON_RDN                 = 67,
89         LDAP_ENTRY_ALREADY_EXISTS               = 68,
90         LDAP_OBJECT_CLASS_MODS_PROHIBITED       = 69,
91         LDAP_AFFECTS_MULTIPLE_DSAS              = 71,
92         LDAP_OTHER                              = 80
93 };
94
95 struct ldap_Result {
96         int resultcode;
97         const char *dn;
98         const char *errormessage;
99         const char *referral;
100 };
101
102 struct ldap_attribute {
103         const char *name;
104         int num_values;
105         DATA_BLOB *values;
106 };
107
108 struct ldap_BindRequest {
109         int version;
110         const char *dn;
111         enum ldap_auth_mechanism mechanism;
112         union {
113                 const char *password;
114                 struct {
115                         const char *mechanism;
116                         DATA_BLOB secblob;
117                 } SASL;
118         } creds;
119 };
120
121 struct ldap_BindResponse {
122         struct ldap_Result response;
123         union {
124                 DATA_BLOB secblob;
125         } SASL;
126 };
127
128 struct ldap_UnbindRequest {
129         uint8_t __dummy;
130 };
131
132 enum ldap_scope {
133         LDAP_SEARCH_SCOPE_BASE = 0,
134         LDAP_SEARCH_SCOPE_SINGLE = 1,
135         LDAP_SEARCH_SCOPE_SUB = 2
136 };
137
138 enum ldap_deref {
139         LDAP_DEREFERENCE_NEVER = 0,
140         LDAP_DEREFERENCE_IN_SEARCHING = 1,
141         LDAP_DEREFERENCE_FINDING_BASE = 2,
142         LDAP_DEREFERENCE_ALWAYS
143 };
144
145 struct ldap_SearchRequest {
146         const char *basedn;
147         enum ldap_scope scope;
148         enum ldap_deref deref;
149         uint32_t timelimit;
150         uint32_t sizelimit;
151         BOOL attributesonly;
152         char *filter;
153         int num_attributes;
154         const char **attributes;
155 };
156
157 struct ldap_SearchResEntry {
158         const char *dn;
159         int num_attributes;
160         struct ldap_attribute *attributes;
161 };
162
163 struct ldap_SearchResRef {
164         const char *referral;
165 };
166
167 enum ldap_modify_type {
168         LDAP_MODIFY_NONE = -1,
169         LDAP_MODIFY_ADD = 0,
170         LDAP_MODIFY_DELETE = 1,
171         LDAP_MODIFY_REPLACE = 2
172 };
173
174 struct ldap_mod {
175         enum ldap_modify_type type;
176         struct ldap_attribute attrib;
177 };
178
179 struct ldap_ModifyRequest {
180         const char *dn;
181         int num_mods;
182         struct ldap_mod *mods;
183 };
184
185 struct ldap_AddRequest {
186         const char *dn;
187         int num_attributes;
188         struct ldap_attribute *attributes;
189 };
190
191 struct ldap_DelRequest {
192         const char *dn;
193 };
194
195 struct ldap_ModifyDNRequest {
196         const char *dn;
197         const char *newrdn;
198         BOOL deleteolddn;
199         const char *newsuperior;
200 };
201
202 struct ldap_CompareRequest {
203         const char *dn;
204         const char *attribute;
205         DATA_BLOB value;
206 };
207
208 struct ldap_AbandonRequest {
209         uint32_t messageid;
210 };
211
212 struct ldap_ExtendedRequest {
213         const char *oid;
214         DATA_BLOB value;
215 };
216
217 struct ldap_ExtendedResponse {
218         struct ldap_Result response;
219         const char *name;
220         DATA_BLOB value;
221 };
222
223 union ldap_Request {
224         struct ldap_BindRequest         BindRequest;
225         struct ldap_BindResponse        BindResponse;
226         struct ldap_UnbindRequest       UnbindRequest;
227         struct ldap_SearchRequest       SearchRequest;
228         struct ldap_SearchResEntry      SearchResultEntry;
229         struct ldap_Result              SearchResultDone;
230         struct ldap_SearchResRef        SearchResultReference;
231         struct ldap_ModifyRequest       ModifyRequest;
232         struct ldap_Result              ModifyResponse;
233         struct ldap_AddRequest          AddRequest;
234         struct ldap_Result              AddResponse;
235         struct ldap_DelRequest          DelRequest;
236         struct ldap_Result              DelResponse;
237         struct ldap_ModifyDNRequest     ModifyDNRequest;
238         struct ldap_Result              ModifyDNResponse;
239         struct ldap_CompareRequest      CompareRequest;
240         struct ldap_Result              CompareResponse;
241         struct ldap_AbandonRequest      AbandonRequest;
242         struct ldap_ExtendedRequest     ExtendedRequest;
243         struct ldap_ExtendedResponse    ExtendedResponse;
244 };
245
246 struct ldap_Control {
247         const char *oid;
248         BOOL        critical;
249         DATA_BLOB   value;
250 };
251
252 struct ldap_message {
253         TALLOC_CTX             *mem_ctx;
254         uint32_t                  messageid;
255         uint8_t                   type;
256         union  ldap_Request     r;
257         int                     num_controls;
258         struct ldap_Control    *controls;
259 };
260
261 struct ldap_queue_entry {
262         struct ldap_queue_entry *next, *prev;
263         int msgid;
264         struct ldap_message *msg;
265 };
266
267 struct ldap_connection {
268         TALLOC_CTX *mem_ctx;
269         int sock;
270         int next_msgid;
271         char *host;
272         uint16_t port;
273         BOOL ldaps;
274
275         const char *auth_dn;
276         const char *simple_pw;
277
278         /* Current outstanding search entry */
279         int searchid;
280
281         /* List for incoming search entries */
282         struct ldap_queue_entry *search_entries;
283
284         /* Outstanding LDAP requests that have not yet been replied to */
285         struct ldap_queue_entry *outstanding;
286
287         /* Let's support SASL */
288         struct gensec_security *gensec;
289 };
290
291 /* Hmm. A blob might be more appropriate here :-) */
292
293 struct ldap_val {
294         unsigned int length;
295         void *data;
296 };
297
298 enum ldap_parse_op {LDAP_OP_SIMPLE, LDAP_OP_AND, LDAP_OP_OR, LDAP_OP_NOT};
299
300 struct ldap_parse_tree {
301         enum ldap_parse_op operation;
302         union {
303                 struct {
304                         char *attr;
305                         struct ldap_val value;
306                 } simple;
307                 struct {
308                         unsigned int num_elements;
309                         struct ldap_parse_tree **elements;
310                 } list;
311                 struct {
312                         struct ldap_parse_tree *child;
313                 } not;
314         } u;
315 };
316
317 #define LDAP_ALL_SEP "()&|=!"
318 #define LDAP_CONNECTION_TIMEOUT 10000
319
320 /* The following definitions come from libcli/ldap/ldap.c  */
321
322 BOOL ldap_encode(struct ldap_message *msg, DATA_BLOB *result);
323 BOOL ldap_decode(struct asn1_data *data, struct ldap_message *msg);
324 BOOL ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
325                           char **host, uint16_t *port, BOOL *ldaps);
326
327 /* The following definitions come from libcli/ldap/ldap_client.c  */
328
329 struct ldap_connection *ldap_connect(TALLOC_CTX *mem_ctx, const char *url);
330 struct ldap_message *new_ldap_message(TALLOC_CTX *mem_ctx);
331 BOOL ldap_send_msg(struct ldap_connection *conn, struct ldap_message *msg,
332                    const struct timeval *endtime);
333 BOOL ldap_receive_msg(struct ldap_connection *conn, struct ldap_message *msg,
334                       const struct timeval *endtime);
335 struct ldap_message *ldap_receive(struct ldap_connection *conn, int msgid,
336                                   const struct timeval *endtime);
337 struct ldap_message *ldap_transaction(struct ldap_connection *conn,
338                                       struct ldap_message *request);
339 int ldap_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password);
340 int ldap_bind_sasl(struct ldap_connection *conn, const char *username, const char *domain, const char *password);
341 struct ldap_connection *ldap_setup_connection(TALLOC_CTX *mem_ctx, const char *url, 
342                                                 const char *userdn, const char *password);
343 struct ldap_connection *ldap_setup_connection_with_sasl(TALLOC_CTX *mem_ctx, const char *url,
344                                                         const char *username, const char *domain, const char *password);
345 BOOL ldap_abandon_message(struct ldap_connection *conn, int msgid,
346                                  const struct timeval *endtime);
347 BOOL ldap_setsearchent(struct ldap_connection *conn, struct ldap_message *msg,
348                        const struct timeval *endtime);
349 struct ldap_message *ldap_getsearchent(struct ldap_connection *conn,
350                                        const struct timeval *endtime);
351 void ldap_endsearchent(struct ldap_connection *conn,
352                        const struct timeval *endtime);
353 struct ldap_message *ldap_searchone(struct ldap_connection *conn,
354                                     struct ldap_message *msg,
355                                     const struct timeval *endtime);
356 BOOL ldap_find_single_value(struct ldap_message *msg, const char *attr,
357                             DATA_BLOB *value);
358 BOOL ldap_find_single_string(struct ldap_message *msg, const char *attr,
359                              TALLOC_CTX *mem_ctx, char **value);
360 BOOL ldap_find_single_int(struct ldap_message *msg, const char *attr,
361                           int *value);
362 int ldap_error(struct ldap_connection *conn);
363 NTSTATUS ldap2nterror(int ldaperror);
364
365 /* The following definitions come from libcli/ldap/ldap_ldif.c  */
366
367 BOOL add_value_to_attrib(TALLOC_CTX *mem_ctx, struct ldap_val *value,
368                          struct ldap_attribute *attrib);
369 BOOL add_attrib_to_array_talloc(TALLOC_CTX *mem_ctx,
370                                        const struct ldap_attribute *attrib,
371                                        struct ldap_attribute **attribs,
372                                        int *num_attribs);
373 BOOL add_mod_to_array_talloc(TALLOC_CTX *mem_ctx,
374                                     struct ldap_mod *mod,
375                                     struct ldap_mod **mods,
376                                     int *num_mods);
377 struct ldap_message *ldap_ldif2msg(TALLOC_CTX *mem_ctx, const char *s);
378
379 #endif