r7593: simplified the memory management in the ldap code. Having a mem_ctx
[tprouty/samba.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 #include "lib/ldb/include/ldb.h"
26
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
48 };
49
50 enum ldap_auth_mechanism {
51         LDAP_AUTH_MECH_SIMPLE = 0,
52         LDAP_AUTH_MECH_SASL = 3
53 };
54
55 enum ldap_result_code {
56         LDAP_SUCCESS                            = 0,
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,
65         LDAP_REFERRAL                           = 10,
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,
83         LDAP_BUSY                               = 51,
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,
94         LDAP_OTHER                              = 80
95 };
96
97 struct ldap_Result {
98         int resultcode;
99         const char *dn;
100         const char *errormessage;
101         const char *referral;
102 };
103
104 struct ldap_attribute {
105         const char *name;
106         int num_values;
107         DATA_BLOB *values;
108 };
109
110 struct ldap_BindRequest {
111         int version;
112         const char *dn;
113         enum ldap_auth_mechanism mechanism;
114         union {
115                 const char *password;
116                 struct {
117                         const char *mechanism;
118                         DATA_BLOB secblob;
119                 } SASL;
120         } creds;
121 };
122
123 struct ldap_BindResponse {
124         struct ldap_Result response;
125         union {
126                 DATA_BLOB secblob;
127         } SASL;
128 };
129
130 struct ldap_UnbindRequest {
131         uint8_t __dummy;
132 };
133
134 enum ldap_scope {
135         LDAP_SEARCH_SCOPE_BASE = 0,
136         LDAP_SEARCH_SCOPE_SINGLE = 1,
137         LDAP_SEARCH_SCOPE_SUB = 2
138 };
139
140 enum ldap_deref {
141         LDAP_DEREFERENCE_NEVER = 0,
142         LDAP_DEREFERENCE_IN_SEARCHING = 1,
143         LDAP_DEREFERENCE_FINDING_BASE = 2,
144         LDAP_DEREFERENCE_ALWAYS
145 };
146
147 struct ldap_SearchRequest {
148         const char *basedn;
149         enum ldap_scope scope;
150         enum ldap_deref deref;
151         uint32_t timelimit;
152         uint32_t sizelimit;
153         BOOL attributesonly;
154         struct ldb_parse_tree *tree;
155         int num_attributes;
156         const char **attributes;
157 };
158
159 struct ldap_SearchResEntry {
160         const char *dn;
161         int num_attributes;
162         struct ldap_attribute *attributes;
163 };
164
165 struct ldap_SearchResRef {
166         const char *referral;
167 };
168
169 enum ldap_modify_type {
170         LDAP_MODIFY_NONE = -1,
171         LDAP_MODIFY_ADD = 0,
172         LDAP_MODIFY_DELETE = 1,
173         LDAP_MODIFY_REPLACE = 2
174 };
175
176 struct ldap_mod {
177         enum ldap_modify_type type;
178         struct ldap_attribute attrib;
179 };
180
181 struct ldap_ModifyRequest {
182         const char *dn;
183         int num_mods;
184         struct ldap_mod *mods;
185 };
186
187 struct ldap_AddRequest {
188         const char *dn;
189         int num_attributes;
190         struct ldap_attribute *attributes;
191 };
192
193 struct ldap_DelRequest {
194         const char *dn;
195 };
196
197 struct ldap_ModifyDNRequest {
198         const char *dn;
199         const char *newrdn;
200         BOOL deleteolddn;
201         const char *newsuperior;
202 };
203
204 struct ldap_CompareRequest {
205         const char *dn;
206         const char *attribute;
207         DATA_BLOB value;
208 };
209
210 struct ldap_AbandonRequest {
211         uint32_t messageid;
212 };
213
214 struct ldap_ExtendedRequest {
215         const char *oid;
216         DATA_BLOB value;
217 };
218
219 struct ldap_ExtendedResponse {
220         struct ldap_Result response;
221         const char *name;
222         DATA_BLOB value;
223 };
224
225 union ldap_Request {
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;
246 };
247
248 struct ldap_Control {
249         const char *oid;
250         BOOL        critical;
251         DATA_BLOB   value;
252 };
253
254 struct ldap_message {
255         uint32_t                messageid;
256         enum ldap_request_tag   type;
257         union ldap_Request      r;
258         int                     num_controls;
259         struct ldap_Control    *controls;
260 };
261
262 struct ldap_queue_entry {
263         struct ldap_queue_entry *next, *prev;
264         int msgid;
265         struct ldap_message *msg;
266 };
267
268 struct ldap_connection {
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 #define LDAP_CONNECTION_TIMEOUT 10000
292
293 /* The following definitions come from libcli/ldap/ldap.c  */
294
295 BOOL ldap_encode(struct ldap_message *msg, DATA_BLOB *result);
296 BOOL ldap_decode(struct asn1_data *data, struct ldap_message *msg);
297 BOOL ldap_parse_basic_url(TALLOC_CTX *mem_ctx, const char *url,
298                           char **host, uint16_t *port, BOOL *ldaps);
299
300 /* The following definitions come from libcli/ldap/ldap_client.c  */
301
302 struct ldap_connection *ldap_connect(TALLOC_CTX *mem_ctx, const char *url);
303 struct ldap_message *new_ldap_message(TALLOC_CTX *mem_ctx);
304 BOOL ldap_send_msg(struct ldap_connection *conn, struct ldap_message *msg,
305                    const struct timeval *endtime);
306 BOOL ldap_receive_msg(struct ldap_connection *conn, struct ldap_message *msg,
307                       const struct timeval *endtime);
308 struct ldap_message *ldap_receive(struct ldap_connection *conn, int msgid,
309                                   const struct timeval *endtime);
310 struct ldap_message *ldap_transaction(struct ldap_connection *conn,
311                                       struct ldap_message *request);
312 int ldap_bind_simple(struct ldap_connection *conn, const char *userdn, const char *password);
313 int ldap_bind_sasl(struct ldap_connection *conn, struct cli_credentials *creds);
314 struct ldap_connection *ldap_setup_connection(TALLOC_CTX *mem_ctx, const char *url, 
315                                                 const char *userdn, const char *password);
316 struct ldap_connection *ldap_setup_connection_with_sasl(TALLOC_CTX *mem_ctx, const char *url,
317                                                         struct cli_credentials *creds);
318 BOOL ldap_abandon_message(struct ldap_connection *conn, int msgid,
319                                  const struct timeval *endtime);
320 BOOL ldap_setsearchent(struct ldap_connection *conn, struct ldap_message *msg,
321                        const struct timeval *endtime);
322 struct ldap_message *ldap_getsearchent(struct ldap_connection *conn,
323                                        const struct timeval *endtime);
324 void ldap_endsearchent(struct ldap_connection *conn,
325                        const struct timeval *endtime);
326 struct ldap_message *ldap_searchone(struct ldap_connection *conn,
327                                     struct ldap_message *msg,
328                                     const struct timeval *endtime);
329 BOOL ldap_find_single_value(struct ldap_message *msg, const char *attr,
330                             DATA_BLOB *value);
331 BOOL ldap_find_single_string(struct ldap_message *msg, const char *attr,
332                              TALLOC_CTX *mem_ctx, char **value);
333 BOOL ldap_find_single_int(struct ldap_message *msg, const char *attr,
334                           int *value);
335 int ldap_error(struct ldap_connection *conn);
336 NTSTATUS ldap2nterror(int ldaperror);
337
338 /* The following definitions come from libcli/ldap/ldap_ldif.c  */
339
340 BOOL add_value_to_attrib(TALLOC_CTX *mem_ctx, struct ldb_val *value,
341                          struct ldap_attribute *attrib);
342 BOOL add_attrib_to_array_talloc(TALLOC_CTX *mem_ctx,
343                                        const struct ldap_attribute *attrib,
344                                        struct ldap_attribute **attribs,
345                                        int *num_attribs);
346 BOOL add_mod_to_array_talloc(TALLOC_CTX *mem_ctx,
347                                     struct ldap_mod *mod,
348                                     struct ldap_mod **mods,
349                                     int *num_mods);
350 struct ldap_message *ldap_ldif2msg(TALLOC_CTX *mem_ctx, const char *s);
351
352 /* The following definitions come from libcli/ldap/ldap_ndr.c  */
353
354 const char *ldap_encode_ndr_uint32(TALLOC_CTX *mem_ctx, uint32_t value);
355 const char *ldap_encode_ndr_dom_sid(TALLOC_CTX *mem_ctx, struct dom_sid *sid);
356 const char *ldap_encode_ndr_GUID(TALLOC_CTX *mem_ctx, struct GUID *guid);
357 NTSTATUS ldap_decode_ndr_GUID(TALLOC_CTX *mem_ctx, struct ldb_val val, struct GUID *guid);
358
359 #endif