r12608: Remove some unused #include lines.
[kamenim/samba.git] / source4 / libcli / ldap / ldap_ildap.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    ildap api - an api similar to the traditional ldap api
5    
6    Copyright (C) Andrew Tridgell  2005
7     
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25 #include "libcli/ldap/ldap_client.h"
26
27 /*
28   delete a record
29  */
30 NTSTATUS ildap_delete(struct ldap_connection *conn, const char *dn)
31 {
32         struct ldap_message *msg;
33         NTSTATUS status;
34
35         msg = new_ldap_message(conn);
36         NT_STATUS_HAVE_NO_MEMORY(msg);
37
38         msg->type = LDAP_TAG_DelRequest;
39         msg->r.DelRequest.dn = dn;
40
41         status = ldap_transaction(conn, msg);
42
43         talloc_free(msg);
44
45         return status;
46 }
47
48 /*
49   add a record
50  */
51 NTSTATUS ildap_add(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
52 {
53         struct ldap_message *msg;
54         int n, i;
55         NTSTATUS status;
56
57         msg = new_ldap_message(conn);
58         NT_STATUS_HAVE_NO_MEMORY(msg);
59
60         for (n=0;mods[n];n++) /* noop */ ;
61
62         msg->type = LDAP_TAG_AddRequest;
63         msg->r.AddRequest.dn = dn;
64         msg->r.AddRequest.num_attributes = n;
65         msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
66         if (msg->r.AddRequest.attributes == NULL) {
67                 talloc_free(msg);
68                 return NT_STATUS_NO_MEMORY;
69         }
70         for (i=0;i<n;i++) {
71                 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
72         }
73
74         status = ldap_transaction(conn, msg);
75
76         talloc_free(msg);
77
78         return status;
79 }
80
81
82 /*
83   modify a record
84  */
85 NTSTATUS ildap_modify(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
86 {
87         struct ldap_message *msg;
88         int n, i;
89         NTSTATUS status;
90
91         msg = new_ldap_message(conn);
92         NT_STATUS_HAVE_NO_MEMORY(msg);
93
94         for (n=0;mods[n];n++) /* noop */ ;
95
96         msg->type = LDAP_TAG_ModifyRequest;
97         msg->r.ModifyRequest.dn = dn;
98         msg->r.ModifyRequest.num_mods = n;
99         msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
100         if (msg->r.ModifyRequest.mods == NULL) {
101                 talloc_free(msg);
102                 return NT_STATUS_NO_MEMORY;
103         }
104         for (i=0;i<n;i++) {
105                 msg->r.ModifyRequest.mods[i] = *mods[i];
106         }
107
108         status = ldap_transaction(conn, msg);
109
110         talloc_free(msg);
111
112         return status;
113 }
114
115
116 /*
117   rename a record
118  */
119 NTSTATUS ildap_rename(struct ldap_connection *conn, const char *dn, const char *newrdn, 
120                       const char *parentdn, BOOL deleteolddn)
121 {
122         struct ldap_message *msg;
123         NTSTATUS status;
124
125         msg = new_ldap_message(conn);
126         NT_STATUS_HAVE_NO_MEMORY(msg);
127
128         msg->type = LDAP_TAG_ModifyDNRequest;
129         msg->r.ModifyDNRequest.dn = dn;
130         msg->r.ModifyDNRequest.newrdn = newrdn;
131         msg->r.ModifyDNRequest.deleteolddn = deleteolddn;
132         msg->r.ModifyDNRequest.newsuperior = parentdn;
133
134         status = ldap_transaction(conn, msg);
135
136         talloc_free(msg);
137
138         return status;
139 }
140
141
142 /*
143   count the returned search entries
144 */
145 int ildap_count_entries(struct ldap_connection *conn, struct ldap_message **res)
146 {
147         int i;
148         for (i=0;res && res[i];i++) /* noop */ ;
149         return i;
150 }
151
152
153 /*
154   perform a ldap search
155 */
156 NTSTATUS ildap_search_bytree(struct ldap_connection *conn, const char *basedn, 
157                              int scope, struct ldb_parse_tree *tree,
158                              const char * const *attrs, BOOL attributesonly, 
159                              struct ldap_message ***results)
160 {
161         struct ldap_message *msg;
162         int n, i;
163         NTSTATUS status;
164         struct ldap_request *req;
165
166         *results = NULL;
167
168         msg = new_ldap_message(conn);
169         NT_STATUS_HAVE_NO_MEMORY(msg);
170
171         for (n=0;attrs && attrs[n];n++) /* noop */ ;
172         
173         msg->type = LDAP_TAG_SearchRequest;
174         msg->r.SearchRequest.basedn = basedn;
175         msg->r.SearchRequest.scope  = scope;
176         msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
177         msg->r.SearchRequest.timelimit = 0;
178         msg->r.SearchRequest.sizelimit = 0;
179         msg->r.SearchRequest.attributesonly = attributesonly;
180         msg->r.SearchRequest.tree = tree;
181         msg->r.SearchRequest.num_attributes = n;
182         msg->r.SearchRequest.attributes = discard_const(attrs);
183
184         req = ldap_request_send(conn, msg);
185         talloc_steal(msg, req);
186         
187         for (i=n=0;True;i++) {
188                 struct ldap_message *res;
189                 status = ldap_result_n(req, i, &res);
190                 if (!NT_STATUS_IS_OK(status)) break;
191
192                 if (res->type == LDAP_TAG_SearchResultDone) {
193                         status = ldap_check_response(conn, &res->r.GeneralResult);
194                         break;
195                 }
196
197                 if (res->type != LDAP_TAG_SearchResultEntry) continue;
198                 
199                 (*results) = talloc_realloc(conn, *results, struct ldap_message *, n+2);
200                 if (*results == NULL) {
201                         talloc_free(msg);
202                         return NT_STATUS_NO_MEMORY;
203                 }
204                 (*results)[n] = talloc_steal(*results, res);
205                 (*results)[n+1] = NULL;
206                 n++;
207         }
208
209         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
210                 status = NT_STATUS_OK;
211         }
212
213         return status;
214 }
215
216 /*
217   perform a ldap search
218 */
219 NTSTATUS ildap_search(struct ldap_connection *conn, const char *basedn, 
220                       int scope, const char *expression, 
221                       const char * const *attrs, BOOL attributesonly, 
222                       struct ldap_message ***results)
223 {
224         struct ldb_parse_tree *tree = ldb_parse_tree(conn, expression);
225         NTSTATUS status;
226         status = ildap_search_bytree(conn, basedn, scope, tree, attrs,
227                                      attributesonly, results);
228         talloc_free(tree);
229         return status;
230 }