libcli/util Rename common map_nt_error_from_unix to avoid duplicate symbol
[metze/samba/wip.git] / source4 / cldap_server / cldap_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    CLDAP server task
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 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 <talloc.h>
24 #include "lib/messaging/irpc.h"
25 #include "smbd/service_task.h"
26 #include "smbd/service.h"
27 #include "cldap_server/cldap_server.h"
28 #include "system/network.h"
29 #include "lib/socket/netif.h"
30 #include <ldb.h>
31 #include <ldb_errors.h>
32 #include "dsdb/samdb/samdb.h"
33 #include "ldb_wrap.h"
34 #include "auth/auth.h"
35 #include "param/param.h"
36 #include "../lib/tsocket/tsocket.h"
37
38 NTSTATUS server_service_cldapd_init(void);
39
40 /*
41   handle incoming cldap requests
42 */
43 static void cldapd_request_handler(struct cldap_socket *cldap,
44                                    void *private_data,
45                                    struct cldap_incoming *in)
46 {
47         struct cldapd_server *cldapd = talloc_get_type(private_data,
48                                        struct cldapd_server);
49         struct ldap_SearchRequest *search;
50
51         if (in->ldap_msg->type != LDAP_TAG_SearchRequest) {
52                 DEBUG(0,("Invalid CLDAP request type %d from %s\n",
53                          in->ldap_msg->type,
54                          tsocket_address_string(in->src, in)));
55                 cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
56                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP request");
57                 talloc_free(in);
58                 return;
59         }
60
61         search = &in->ldap_msg->r.SearchRequest;
62
63         if (strcmp("", search->basedn) != 0) {
64                 DEBUG(0,("Invalid CLDAP basedn '%s' from %s\n",
65                          search->basedn,
66                          tsocket_address_string(in->src, in)));
67                 cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
68                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP basedn");
69                 talloc_free(in);
70                 return;
71         }
72
73         if (search->scope != LDAP_SEARCH_SCOPE_BASE) {
74                 DEBUG(0,("Invalid CLDAP scope %d from %s\n",
75                          search->scope,
76                          tsocket_address_string(in->src, in)));
77                 cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
78                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP scope");
79                 talloc_free(in);
80                 return;
81         }
82
83         if (search->num_attributes == 1 &&
84             strcasecmp(search->attributes[0], "netlogon") == 0) {
85                 cldapd_netlogon_request(cldap,
86                                         cldapd,
87                                         in,
88                                         in->ldap_msg->messageid,
89                                         search->tree,
90                                         in->src);
91                 talloc_free(in);
92                 return;
93         }
94
95         cldapd_rootdse_request(cldap, cldapd, in,
96                                in->ldap_msg->messageid,
97                                search, in->src);
98         talloc_free(in);
99 }
100
101
102 /*
103   start listening on the given address
104 */
105 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, struct loadparm_context *lp_ctx,
106                                   const char *address)
107 {
108         struct cldap_socket *cldapsock;
109         struct tsocket_address *socket_address;
110         NTSTATUS status;
111         int ret;
112
113         ret = tsocket_address_inet_from_strings(cldapd,
114                                                 "ip",
115                                                 address,
116                                                 lpcfg_cldap_port(lp_ctx),
117                                                 &socket_address);
118         if (ret != 0) {
119                 status = map_nt_error_from_unix_common(errno);
120                 DEBUG(0,("invalid address %s:%d - %s:%s\n",
121                          address, lpcfg_cldap_port(lp_ctx),
122                          gai_strerror(ret), nt_errstr(status)));
123                 return status;
124         }
125
126         /* listen for unicasts on the CLDAP port (389) */
127         status = cldap_socket_init(cldapd,
128                                    cldapd->task->event_ctx,
129                                    socket_address,
130                                    NULL,
131                                    &cldapsock);
132         if (!NT_STATUS_IS_OK(status)) {
133                 DEBUG(0,("Failed to bind to %s - %s\n", 
134                          tsocket_address_string(socket_address, socket_address),
135                          nt_errstr(status)));
136                 talloc_free(socket_address);
137                 return status;
138         }
139         talloc_free(socket_address);
140
141         cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
142
143         return NT_STATUS_OK;
144 }
145
146 /*
147   setup our listening sockets on the configured network interfaces
148 */
149 static NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd, struct loadparm_context *lp_ctx,
150                                           struct interface *ifaces)
151 {
152         int i, num_interfaces;
153         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
154         NTSTATUS status;
155
156         num_interfaces = iface_list_count(ifaces);
157
158         /* if we are allowing incoming packets from any address, then
159            we need to bind to the wildcard address */
160         if (!lpcfg_bind_interfaces_only(lp_ctx)) {
161                 const char **wcard = iface_list_wildcard(cldapd, lp_ctx);
162                 NT_STATUS_HAVE_NO_MEMORY(wcard);
163                 for (i=0; wcard[i]; i++) {
164                         status = cldapd_add_socket(cldapd, lp_ctx, wcard[i]);
165                         NT_STATUS_NOT_OK_RETURN(status);
166                 }
167                 talloc_free(wcard);
168         }
169
170         /* now we have to also listen on the specific interfaces,
171            so that replies always come from the right IP */
172         for (i=0; i<num_interfaces; i++) {
173                 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
174                 status = cldapd_add_socket(cldapd, lp_ctx, address);
175                 NT_STATUS_NOT_OK_RETURN(status);
176         }
177
178         talloc_free(tmp_ctx);
179
180         return NT_STATUS_OK;
181 }
182
183 /*
184   startup the cldapd task
185 */
186 static void cldapd_task_init(struct task_server *task)
187 {
188         struct cldapd_server *cldapd;
189         NTSTATUS status;
190         struct interface *ifaces;
191         
192         load_interface_list(task, task->lp_ctx, &ifaces);
193
194         if (iface_list_count(ifaces) == 0) {
195                 task_server_terminate(task, "cldapd: no network interfaces configured", false);
196                 return;
197         }
198
199         switch (lpcfg_server_role(task->lp_ctx)) {
200         case ROLE_STANDALONE:
201                 task_server_terminate(task, "cldap_server: no CLDAP server required in standalone configuration", 
202                                       false);
203                 return;
204         case ROLE_DOMAIN_MEMBER:
205                 task_server_terminate(task, "cldap_server: no CLDAP server required in member server configuration",
206                                       false);
207                 return;
208         case ROLE_DOMAIN_CONTROLLER:
209                 /* Yes, we want an CLDAP server */
210                 break;
211         }
212
213         task_server_set_title(task, "task[cldapd]");
214
215         cldapd = talloc(task, struct cldapd_server);
216         if (cldapd == NULL) {
217                 task_server_terminate(task, "cldapd: out of memory", true);
218                 return;
219         }
220
221         cldapd->task = task;
222         cldapd->samctx = samdb_connect(cldapd, task->event_ctx, task->lp_ctx, system_session(task->lp_ctx), 0);
223         if (cldapd->samctx == NULL) {
224                 task_server_terminate(task, "cldapd failed to open samdb", true);
225                 return;
226         }
227
228         /* start listening on the configured network interfaces */
229         status = cldapd_startup_interfaces(cldapd, task->lp_ctx, ifaces);
230         if (!NT_STATUS_IS_OK(status)) {
231                 task_server_terminate(task, "cldapd failed to setup interfaces", true);
232                 return;
233         }
234
235         irpc_add_name(task->msg_ctx, "cldap_server");
236 }
237
238
239 /*
240   register ourselves as a available server
241 */
242 NTSTATUS server_service_cldapd_init(void)
243 {
244         return register_server_service("cldap", cldapd_task_init);
245 }