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