2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
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.
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.
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.
22 #include "ldap_server/ldap_server.h"
23 #include "dlinklist.h"
24 #include "libcli/ldap/ldap.h"
27 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
29 struct ldapsrv_reply *reply;
31 reply = talloc(call, struct ldapsrv_reply);
35 reply->msg = talloc(reply, struct ldap_message);
36 if (reply->msg == NULL) {
41 reply->msg->messageid = call->request->messageid;
42 reply->msg->type = type;
43 reply->msg->controls = NULL;
48 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
50 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
53 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn, uint8_t scope)
55 return conn->default_partition;
58 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
60 struct ldapsrv_reply *reply;
61 struct ldap_ExtendedResponse *r;
63 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
65 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
67 return NT_STATUS_NO_MEMORY;
70 r = &reply->msg->r.ExtendedResponse;
71 r->response.resultcode = error;
72 r->response.dn = NULL;
73 r->response.errormessage = NULL;
74 r->response.referral = NULL;
79 ldapsrv_queue_reply(call, reply);
83 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
85 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
86 struct ldapsrv_partition *part;
88 DEBUG(10, ("SearchRequest"));
89 DEBUGADD(10, (" basedn: %s", req->basedn));
90 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
92 part = ldapsrv_get_partition(call->conn, req->basedn, req->scope);
94 if (!part->ops->Search) {
95 struct ldap_Result *done;
96 struct ldapsrv_reply *done_r;
98 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
100 return NT_STATUS_NO_MEMORY;
103 done = &done_r->msg->r.SearchResultDone;
104 done->resultcode = 53;
106 done->errormessage = NULL;
107 done->referral = NULL;
109 ldapsrv_queue_reply(call, done_r);
113 return part->ops->Search(part, call);
116 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
118 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
119 struct ldapsrv_partition *part;
121 DEBUG(10, ("ModifyRequest"));
122 DEBUGADD(10, (" dn: %s", req->dn));
124 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
126 if (!part->ops->Modify) {
127 return ldapsrv_unwilling(call, 53);
130 return part->ops->Modify(part, call);
133 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
135 struct ldap_AddRequest *req = &call->request->r.AddRequest;
136 struct ldapsrv_partition *part;
138 DEBUG(10, ("AddRequest"));
139 DEBUGADD(10, (" dn: %s", req->dn));
141 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
143 if (!part->ops->Add) {
144 return ldapsrv_unwilling(call, 53);
147 return part->ops->Add(part, call);
150 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
152 struct ldap_DelRequest *req = &call->request->r.DelRequest;
153 struct ldapsrv_partition *part;
155 DEBUG(10, ("DelRequest"));
156 DEBUGADD(10, (" dn: %s", req->dn));
158 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
160 if (!part->ops->Del) {
161 return ldapsrv_unwilling(call, 53);
164 return part->ops->Del(part, call);
167 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
169 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
170 struct ldapsrv_partition *part;
172 DEBUG(10, ("ModifyDNRequrest"));
173 DEBUGADD(10, (" dn: %s", req->dn));
174 DEBUGADD(10, (" newrdn: %s", req->newrdn));
176 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
178 if (!part->ops->ModifyDN) {
179 return ldapsrv_unwilling(call, 53);
182 return part->ops->ModifyDN(part, call);
185 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
187 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
188 struct ldapsrv_partition *part;
190 DEBUG(10, ("CompareRequest"));
191 DEBUGADD(10, (" dn: %s", req->dn));
193 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
195 if (!part->ops->Compare) {
196 return ldapsrv_unwilling(call, 53);
199 return part->ops->Compare(part, call);
202 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
204 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
205 DEBUG(10, ("AbandonRequest\n"));
209 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
211 /* struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
212 struct ldapsrv_reply *reply;
214 DEBUG(10, ("Extended\n"));
216 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
218 return NT_STATUS_NO_MEMORY;
221 ZERO_STRUCT(reply->msg->r);
223 ldapsrv_queue_reply(call, reply);
227 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
229 switch(call->request->type) {
230 case LDAP_TAG_BindRequest:
231 return ldapsrv_BindRequest(call);
232 case LDAP_TAG_UnbindRequest:
233 return ldapsrv_UnbindRequest(call);
234 case LDAP_TAG_SearchRequest:
235 return ldapsrv_SearchRequest(call);
236 case LDAP_TAG_ModifyRequest:
237 return ldapsrv_ModifyRequest(call);
238 case LDAP_TAG_AddRequest:
239 return ldapsrv_AddRequest(call);
240 case LDAP_TAG_DelRequest:
241 return ldapsrv_DelRequest(call);
242 case LDAP_TAG_ModifyDNRequest:
243 return ldapsrv_ModifyDNRequest(call);
244 case LDAP_TAG_CompareRequest:
245 return ldapsrv_CompareRequest(call);
246 case LDAP_TAG_AbandonRequest:
247 return ldapsrv_AbandonRequest(call);
248 case LDAP_TAG_ExtendedRequest:
249 return ldapsrv_ExtendedRequest(call);
251 return ldapsrv_unwilling(call, 2);