r1944: put ldif functions in a separate file
[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_SASL_BIND_IN_PROGRESS = 0x0e,
56         LDAP_INVALID_CREDENTIALS = 0x31,
57         LDAP_OTHER = 0x50
58 };
59
60 struct ldap_Result {
61         int resultcode;
62         const char *dn;
63         const char *errormessage;
64         const char *referral;
65 };
66
67 struct ldap_attribute {
68         const char *name;
69         int num_values;
70         DATA_BLOB *values;
71 };
72
73 struct ldap_BindRequest {
74         int version;
75         const char *dn;
76         enum ldap_auth_mechanism mechanism;
77         union {
78                 const char *password;
79                 struct {
80                         const char *mechanism;
81                         DATA_BLOB secblob;
82                 } SASL;
83         } creds;
84 };
85
86 struct ldap_BindResponse {
87         struct ldap_Result response;
88         union {
89                 DATA_BLOB secblob;
90         } SASL;
91 };
92
93 struct ldap_UnbindRequest {
94         uint8_t __dummy;
95 };
96
97 enum ldap_scope {
98         LDAP_SEARCH_SCOPE_BASE = 0,
99         LDAP_SEARCH_SCOPE_SINGLE = 1,
100         LDAP_SEARCH_SCOPE_SUB = 2
101 };
102
103 enum ldap_deref {
104         LDAP_DEREFERENCE_NEVER = 0,
105         LDAP_DEREFERENCE_IN_SEARCHING = 1,
106         LDAP_DEREFERENCE_FINDING_BASE = 2,
107         LDAP_DEREFERENCE_ALWAYS
108 };
109
110 struct ldap_SearchRequest {
111         const char *basedn;
112         enum ldap_scope scope;
113         enum ldap_deref deref;
114         uint32 timelimit;
115         uint32 sizelimit;
116         BOOL attributesonly;
117         char *filter;
118         int num_attributes;
119         const char **attributes;
120 };
121
122 struct ldap_SearchResEntry {
123         const char *dn;
124         int num_attributes;
125         struct ldap_attribute *attributes;
126 };
127
128 struct ldap_SearchResRef {
129         int num_referrals;
130         const char **referrals;
131 };
132
133 enum ldap_modify_type {
134         LDAP_MODIFY_NONE = -1,
135         LDAP_MODIFY_ADD = 0,
136         LDAP_MODIFY_DELETE = 1,
137         LDAP_MODIFY_REPLACE = 2
138 };
139
140 struct ldap_mod {
141         enum ldap_modify_type type;
142         struct ldap_attribute attrib;
143 };
144
145 struct ldap_ModifyRequest {
146         const char *dn;
147         int num_mods;
148         struct ldap_mod *mods;
149 };
150
151 struct ldap_AddRequest {
152         const char *dn;
153         int num_attributes;
154         struct ldap_attribute *attributes;
155 };
156
157 struct ldap_DelRequest {
158         const char *dn;
159 };
160
161 struct ldap_ModifyDNRequest {
162         const char *dn;
163         const char *newrdn;
164         BOOL deleteolddn;
165         const char *newsuperior;
166 };
167
168 struct ldap_CompareRequest {
169         const char *dn;
170         const char *attribute;
171         const char *value;
172 };
173
174 struct ldap_AbandonRequest {
175         uint32 messageid;
176 };
177
178 struct ldap_ExtendedRequest {
179         const char *oid;
180         DATA_BLOB value;
181 };
182
183 struct ldap_ExtendedResponse {
184         struct ldap_Result response;
185         const char *name;
186         DATA_BLOB value;
187 };
188
189 union ldap_Request {
190         struct ldap_BindRequest         BindRequest;
191         struct ldap_BindResponse        BindResponse;
192         struct ldap_UnbindRequest       UnbindRequest;
193         struct ldap_SearchRequest       SearchRequest;
194         struct ldap_SearchResEntry      SearchResultEntry;
195         struct ldap_Result              SearchResultDone;
196         struct ldap_SearchResRef        SearchResultReference;
197         struct ldap_ModifyRequest       ModifyRequest;
198         struct ldap_Result              ModifyResponse;
199         struct ldap_AddRequest          AddRequest;
200         struct ldap_Result              AddResponse;
201         struct ldap_DelRequest          DelRequest;
202         struct ldap_Result              DelResponse;
203         struct ldap_ModifyDNRequest     ModifyDNRequest;
204         struct ldap_Result              ModifyDNResponse;
205         struct ldap_CompareRequest      CompareRequest;
206         struct ldap_Result              CompareResponse;
207         struct ldap_AbandonRequest      AbandonRequest;
208         struct ldap_ExtendedRequest     ExtendedRequest;
209         struct ldap_ExtendedResponse    ExtendedResponse;
210 };
211
212 struct ldap_Control {
213         const char *oid;
214         BOOL        critical;
215         DATA_BLOB   value;
216 };
217
218 struct ldap_message {
219         TALLOC_CTX             *mem_ctx;
220         uint32                  messageid;
221         uint8                   type;
222         union  ldap_Request     r;
223         int                     num_controls;
224         struct ldap_Control    *controls;
225 };
226
227 struct ldap_queue_entry {
228         struct ldap_queue_entry *next, *prev;
229         int msgid;
230         struct ldap_message *msg;
231 };
232
233 struct ldap_connection {
234         TALLOC_CTX *mem_ctx;
235         int sock;
236         int next_msgid;
237         char *host;
238         uint16 port;
239         BOOL ldaps;
240
241         const char *auth_dn;
242         const char *simple_pw;
243
244         /* Current outstanding search entry */
245         int searchid;
246
247         /* List for incoming search entries */
248         struct ldap_queue_entry *search_entries;
249
250         /* Outstanding LDAP requests that have not yet been replied to */
251         struct ldap_queue_entry *outstanding;
252
253         /* Let's support SASL */
254         struct gensec_security *gensec;
255 };
256
257 /* Hmm. A blob might be more appropriate here :-) */
258
259 struct ldap_val {
260         unsigned int length;
261         void *data;
262 };
263
264 enum ldap_parse_op {LDAP_OP_SIMPLE, LDAP_OP_AND, LDAP_OP_OR, LDAP_OP_NOT};
265
266 struct ldap_parse_tree {
267         enum ldap_parse_op operation;
268         union {
269                 struct {
270                         char *attr;
271                         struct ldap_val value;
272                 } simple;
273                 struct {
274                         unsigned int num_elements;
275                         struct ldap_parse_tree **elements;
276                 } list;
277                 struct {
278                         struct ldap_parse_tree *child;
279                 } not;
280         } u;
281 };
282
283 #define LDAP_ALL_SEP "()&|=!"
284 #define LDAP_CONNECTION_TIMEOUT 10000
285
286 #endif