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"
26 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
28 struct ldapsrv_reply *reply;
30 reply = talloc(call, struct ldapsrv_reply);
34 reply->msg = talloc(reply, struct ldap_message);
35 if (reply->msg == NULL) {
40 reply->msg->messageid = call->request->messageid;
41 reply->msg->type = type;
46 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
48 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
51 struct ldapsrv_partition *ldapsrv_get_partition(struct ldapsrv_connection *conn, const char *dn, uint8_t scope)
53 return conn->default_partition;
56 NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
58 struct ldapsrv_reply *reply;
59 struct ldap_ExtendedResponse *r;
61 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
63 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
65 return NT_STATUS_NO_MEMORY;
68 r = &reply->msg->r.ExtendedResponse;
69 r->response.resultcode = error;
70 r->response.dn = NULL;
71 r->response.errormessage = NULL;
72 r->response.referral = NULL;
77 ldapsrv_queue_reply(call, reply);
81 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
83 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
84 struct ldapsrv_partition *part;
86 DEBUG(10, ("SearchRequest"));
87 DEBUGADD(10, (" basedn: %s", req->basedn));
88 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
90 part = ldapsrv_get_partition(call->conn, req->basedn, req->scope);
92 if (!part->ops->Search) {
93 struct ldap_Result *done;
94 struct ldapsrv_reply *done_r;
96 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
98 return NT_STATUS_NO_MEMORY;
101 done = &done_r->msg->r.SearchResultDone;
102 done->resultcode = 53;
104 done->errormessage = NULL;
105 done->referral = NULL;
107 ldapsrv_queue_reply(call, done_r);
111 return part->ops->Search(part, call, req);
114 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
116 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
117 struct ldapsrv_partition *part;
119 DEBUG(10, ("ModifyRequest"));
120 DEBUGADD(10, (" dn: %s", req->dn));
122 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
124 if (!part->ops->Modify) {
125 return ldapsrv_unwilling(call, 53);
128 return part->ops->Modify(part, call, req);
131 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
133 struct ldap_AddRequest *req = &call->request->r.AddRequest;
134 struct ldapsrv_partition *part;
136 DEBUG(10, ("AddRequest"));
137 DEBUGADD(10, (" dn: %s", req->dn));
139 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
141 if (!part->ops->Add) {
142 return ldapsrv_unwilling(call, 53);
145 return part->ops->Add(part, call, req);
148 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
150 struct ldap_DelRequest *req = &call->request->r.DelRequest;
151 struct ldapsrv_partition *part;
153 DEBUG(10, ("DelRequest"));
154 DEBUGADD(10, (" dn: %s", req->dn));
156 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
158 if (!part->ops->Del) {
159 return ldapsrv_unwilling(call, 53);
162 return part->ops->Del(part, call, req);
165 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
167 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
168 struct ldapsrv_partition *part;
170 DEBUG(10, ("ModifyDNRequrest"));
171 DEBUGADD(10, (" dn: %s", req->dn));
172 DEBUGADD(10, (" newrdn: %s", req->newrdn));
174 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
176 if (!part->ops->ModifyDN) {
177 return ldapsrv_unwilling(call, 53);
180 return part->ops->ModifyDN(part, call, req);
183 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
185 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
186 struct ldapsrv_partition *part;
188 DEBUG(10, ("CompareRequest"));
189 DEBUGADD(10, (" dn: %s", req->dn));
191 part = ldapsrv_get_partition(call->conn, req->dn, LDAP_SEARCH_SCOPE_SUB);
193 if (!part->ops->Compare) {
194 return ldapsrv_unwilling(call, 53);
197 return part->ops->Compare(part, call, req);
200 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
202 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
203 DEBUG(10, ("AbandonRequest\n"));
207 static NTSTATUS ldapsrv_ExtendedRequest(struct ldapsrv_call *call)
209 /* struct ldap_ExtendedRequest *req = &call->request.r.ExtendedRequest;*/
210 struct ldapsrv_reply *reply;
212 DEBUG(10, ("Extended\n"));
214 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
216 return NT_STATUS_NO_MEMORY;
219 ZERO_STRUCT(reply->msg->r);
221 ldapsrv_queue_reply(call, reply);
225 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
227 switch(call->request->type) {
228 case LDAP_TAG_BindRequest:
229 return ldapsrv_BindRequest(call);
230 case LDAP_TAG_UnbindRequest:
231 return ldapsrv_UnbindRequest(call);
232 case LDAP_TAG_SearchRequest:
233 return ldapsrv_SearchRequest(call);
234 case LDAP_TAG_ModifyRequest:
235 return ldapsrv_ModifyRequest(call);
236 case LDAP_TAG_AddRequest:
237 return ldapsrv_AddRequest(call);
238 case LDAP_TAG_DelRequest:
239 return ldapsrv_DelRequest(call);
240 case LDAP_TAG_ModifyDNRequest:
241 return ldapsrv_ModifyDNRequest(call);
242 case LDAP_TAG_CompareRequest:
243 return ldapsrv_CompareRequest(call);
244 case LDAP_TAG_AbandonRequest:
245 return ldapsrv_AbandonRequest(call);
246 case LDAP_TAG_ExtendedRequest:
247 return ldapsrv_ExtendedRequest(call);
249 return ldapsrv_unwilling(call, 2);