r19736: handle rootdse call via CLDAP
[jelmer/samba4-debian.git] / source / cldap_server / rootdse.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CLDAP server - rootdse handling
5
6    Copyright (C) Stefan Metzmacher      2006
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 #include "includes.h"
24 #include "libcli/ldap/ldap.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/events/events.h"
28 #include "lib/socket/socket.h"
29 #include "smbd/service_task.h"
30 #include "cldap_server/cldap_server.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "dsdb/samdb/samdb.h"
33 #include "auth/auth.h"
34 #include "db_wrap.h"
35 #include "system/network.h"
36 #include "lib/socket/netif.h"
37
38 static void cldapd_rootdse_fill(struct cldapd_server *cldapd,
39                                 TALLOC_CTX *mem_ctx,
40                                 struct ldap_SearchRequest *search,
41                                 struct ldap_SearchResEntry **response,
42                                 struct ldap_Result *result)
43 {
44         struct ldap_SearchResEntry *ent = NULL;
45         struct ldb_dn *basedn;
46         struct ldb_result *res = NULL;
47         struct ldb_request *lreq;
48         enum ldb_scope scope = LDB_SCOPE_DEFAULT;
49         const char **attrs = NULL;
50         const char *errstr = NULL;
51         int ret = 0;
52         int ldb_ret = -1;
53
54         basedn = ldb_dn_explode(mem_ctx, "");
55         if (basedn == NULL) goto nomem;
56         scope = LDB_SCOPE_BASE;
57
58         if (search->num_attributes >= 1) {
59                 int i;
60
61                 attrs = talloc_array(mem_ctx, const char *, search->num_attributes+1);
62                 if (attrs == NULL) goto nomem;
63
64                 for (i=0; i < search->num_attributes; i++) {
65                         attrs[i] = search->attributes[i];
66                 }
67                 attrs[i] = NULL;
68         }
69
70         lreq = talloc(mem_ctx, struct ldb_request);
71         if (lreq == NULL) goto nomem;
72
73         res = talloc_zero(mem_ctx, struct ldb_result);
74         if (res == NULL) goto nomem;
75
76         lreq->operation = LDB_SEARCH;
77         lreq->op.search.base = basedn;
78         lreq->op.search.scope = scope;
79         lreq->op.search.tree = search->tree;
80         lreq->op.search.attrs = attrs;
81
82         lreq->controls = NULL;
83
84         lreq->context = res;
85         lreq->callback = ldb_search_default_callback;
86
87         /* Copy the timeout from the incoming call */
88         ldb_set_timeout(cldapd->samctx, lreq, search->timelimit);
89
90         ldb_ret = ldb_request(cldapd->samctx, lreq);
91         if (ldb_ret != LDB_SUCCESS) {
92                 goto reply;
93         }
94
95         ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
96         if (ldb_ret != LDB_SUCCESS) {
97                 goto reply;
98         }
99
100         if (res->count > 1) {
101                 errstr = "Internal error: to much replies";
102                 ldb_ret = LDB_ERR_OTHER;
103                 goto reply;
104         } else if (res->count == 1) {
105                 int j;
106
107                 ent = talloc(mem_ctx, struct ldap_SearchResEntry);
108                 if (ent == NULL) goto nomem;
109
110                 ent->dn = ldb_dn_linearize(ent, res->msgs[0]->dn);
111                 if (ent->dn == NULL) goto nomem;
112                 ent->num_attributes = 0;
113                 ent->attributes = NULL;
114                 if (res->msgs[0]->num_elements == 0) {
115                         goto reply;
116                 }
117                 ent->num_attributes = res->msgs[0]->num_elements;
118                 ent->attributes = talloc_array(ent, struct ldb_message_element, ent->num_attributes);
119                 if (ent->attributes == NULL) goto nomem;
120                 for (j=0; j < ent->num_attributes; j++) {
121                         ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[0]->elements[j].name);
122                         ent->attributes[j].num_values = 0;
123                         ent->attributes[j].values = NULL;
124                         if (search->attributesonly && (res->msgs[0]->elements[j].num_values == 0)) {
125                                 continue;
126                         }
127                         ent->attributes[j].num_values = res->msgs[0]->elements[j].num_values;
128                         ent->attributes[j].values = res->msgs[0]->elements[j].values;
129                         talloc_steal(ent->attributes, res->msgs[0]->elements[j].values);
130                 }
131         }
132
133 reply:
134         if (ret) {
135                 /* nothing ... */
136         } else if (ldb_ret == LDB_SUCCESS) {
137                 ret = LDAP_SUCCESS;
138                 errstr = NULL;
139         } else {
140                 ret = ldb_ret;
141                 errstr = ldb_errstring(cldapd->samctx);
142         }
143         goto done;
144 nomem:
145         talloc_free(ent);
146         ret = LDAP_OPERATIONS_ERROR;
147         errstr = "No memory";
148 done:
149         *response = ent;
150         result->resultcode = ret;
151         result->errormessage = (errstr?talloc_strdup(mem_ctx, errstr):NULL);
152 }
153
154 /*
155   handle incoming cldap requests
156 */
157 void cldapd_rootdse_request(struct cldap_socket *cldap, 
158                             uint32_t message_id,
159                             struct ldap_SearchRequest *search,
160                             struct socket_address *src)
161 {
162         struct cldapd_server *cldapd = talloc_get_type(cldap->incoming.private, struct cldapd_server);
163         NTSTATUS status;
164         struct cldap_reply reply;
165         struct ldap_Result result;
166         TALLOC_CTX *tmp_ctx = talloc_new(cldap);
167
168         ZERO_STRUCT(result);
169
170         reply.messageid         = message_id;
171         reply.dest              = src;
172         reply.response          = NULL;
173         reply.result            = &result;
174
175         cldapd_rootdse_fill(cldapd, tmp_ctx, search, &reply.response, reply.result);
176
177         status = cldap_reply_send(cldap, &reply);
178         if (!NT_STATUS_IS_OK(status)) {
179                 DEBUG(2,("cldap rootdse query failed '%s' - %s\n",
180                          ldb_filter_from_tree(tmp_ctx, search->tree), nt_errstr(status)));
181         }
182
183         talloc_free(tmp_ctx);
184         return;
185 }