r11271: Fix a warning and an infinite recursion
[mdw/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.h"
26 #include "libcli/ldap/ldap_client.h"
27
28 /*
29   delete a record
30  */
31 NTSTATUS ildap_delete(struct ldap_connection *conn, const char *dn)
32 {
33         struct ldap_message *msg;
34         NTSTATUS status;
35
36         msg = new_ldap_message(conn);
37         NT_STATUS_HAVE_NO_MEMORY(msg);
38
39         msg->type = LDAP_TAG_DelRequest;
40         msg->r.DelRequest.dn = dn;
41
42         status = ldap_transaction(conn, msg);
43
44         talloc_free(msg);
45
46         return status;
47 }
48
49 /*
50   add a record
51  */
52 NTSTATUS ildap_add(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
53 {
54         struct ldap_message *msg;
55         int n, i;
56         NTSTATUS status;
57
58         msg = new_ldap_message(conn);
59         NT_STATUS_HAVE_NO_MEMORY(msg);
60
61         for (n=0;mods[n];n++) /* noop */ ;
62
63         msg->type = LDAP_TAG_AddRequest;
64         msg->r.AddRequest.dn = dn;
65         msg->r.AddRequest.num_attributes = n;
66         msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
67         if (msg->r.AddRequest.attributes == NULL) {
68                 talloc_free(msg);
69                 return NT_STATUS_NO_MEMORY;
70         }
71         for (i=0;i<n;i++) {
72                 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
73         }
74
75         status = ldap_transaction(conn, msg);
76
77         talloc_free(msg);
78
79         return status;
80 }
81
82
83 /*
84   modify a record
85  */
86 NTSTATUS ildap_modify(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
87 {
88         struct ldap_message *msg;
89         int n, i;
90         NTSTATUS status;
91
92         msg = new_ldap_message(conn);
93         NT_STATUS_HAVE_NO_MEMORY(msg);
94
95         for (n=0;mods[n];n++) /* noop */ ;
96
97         msg->type = LDAP_TAG_ModifyRequest;
98         msg->r.ModifyRequest.dn = dn;
99         msg->r.ModifyRequest.num_mods = n;
100         msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
101         if (msg->r.ModifyRequest.mods == NULL) {
102                 talloc_free(msg);
103                 return NT_STATUS_NO_MEMORY;
104         }
105         for (i=0;i<n;i++) {
106                 msg->r.ModifyRequest.mods[i] = *mods[i];
107         }
108
109         status = ldap_transaction(conn, msg);
110
111         talloc_free(msg);
112
113         return status;
114 }
115
116
117 /*
118   rename a record
119  */
120 NTSTATUS ildap_rename(struct ldap_connection *conn, const char *dn, const char *newrdn, 
121                       const char *parentdn, BOOL deleteolddn)
122 {
123         struct ldap_message *msg;
124         NTSTATUS status;
125
126         msg = new_ldap_message(conn);
127         NT_STATUS_HAVE_NO_MEMORY(msg);
128
129         msg->type = LDAP_TAG_ModifyDNRequest;
130         msg->r.ModifyDNRequest.dn = dn;
131         msg->r.ModifyDNRequest.newrdn = newrdn;
132         msg->r.ModifyDNRequest.deleteolddn = deleteolddn;
133         msg->r.ModifyDNRequest.newsuperior = parentdn;
134
135         status = ldap_transaction(conn, msg);
136
137         talloc_free(msg);
138
139         return status;
140 }
141
142
143 /*
144   count the returned search entries
145 */
146 int ildap_count_entries(struct ldap_connection *conn, struct ldap_message **res)
147 {
148         int i;
149         for (i=0;res && res[i];i++) /* noop */ ;
150         return i;
151 }
152
153
154 /*
155   perform a ldap search
156 */
157 NTSTATUS ildap_search_bytree(struct ldap_connection *conn, const char *basedn, 
158                              int scope, struct ldb_parse_tree *tree,
159                              const char * const *attrs, BOOL attributesonly, 
160                              struct ldap_message ***results)
161 {
162         struct ldap_message *msg;
163         int n, i;
164         NTSTATUS status;
165         struct ldap_request *req;
166
167         *results = NULL;
168
169         msg = new_ldap_message(conn);
170         NT_STATUS_HAVE_NO_MEMORY(msg);
171
172         for (n=0;attrs && attrs[n];n++) /* noop */ ;
173         
174         msg->type = LDAP_TAG_SearchRequest;
175         msg->r.SearchRequest.basedn = basedn;
176         msg->r.SearchRequest.scope  = scope;
177         msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
178         msg->r.SearchRequest.timelimit = 0;
179         msg->r.SearchRequest.sizelimit = 0;
180         msg->r.SearchRequest.attributesonly = attributesonly;
181         msg->r.SearchRequest.tree = tree;
182         msg->r.SearchRequest.num_attributes = n;
183         msg->r.SearchRequest.attributes = discard_const(attrs);
184
185         req = ldap_request_send(conn, msg);
186         talloc_steal(msg, req);
187         
188         for (i=n=0;True;i++) {
189                 struct ldap_message *res;
190                 status = ldap_result_n(req, i, &res);
191                 if (!NT_STATUS_IS_OK(status)) break;
192
193                 if (res->type == LDAP_TAG_SearchResultDone) {
194                         status = ldap_check_response(conn, &res->r.GeneralResult);
195                         break;
196                 }
197
198                 if (res->type != LDAP_TAG_SearchResultEntry) continue;
199                 
200                 (*results) = talloc_realloc(conn, *results, struct ldap_message *, n+2);
201                 if (*results == NULL) {
202                         talloc_free(msg);
203                         return NT_STATUS_NO_MEMORY;
204                 }
205                 (*results)[n] = talloc_steal(*results, res);
206                 (*results)[n+1] = NULL;
207                 n++;
208         }
209
210         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
211                 status = NT_STATUS_OK;
212         }
213
214         return status;
215 }
216
217 /*
218   perform a ldap search
219 */
220 NTSTATUS ildap_search(struct ldap_connection *conn, const char *basedn, 
221                       int scope, const char *expression, 
222                       const char * const *attrs, BOOL attributesonly, 
223                       struct ldap_message ***results)
224 {
225         struct ldb_parse_tree *tree = ldb_parse_tree(conn, expression);
226         NTSTATUS status;
227         status = ildap_search_bytree(conn, basedn, scope, tree, attrs,
228                                      attributesonly, results);
229         talloc_free(tree);
230         return status;
231 }