s3/s4: smbd, rpc, ldap, cldap, kdc services.
[amitay/samba.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_AbandonRequest) {
52                 DEBUG(10,("Got (and ignoring) CLDAP AbandonRequest from %s.",
53                           tsocket_address_string(in->src, in)));
54                 talloc_free(in);
55                 return;
56         }
57
58         if (in->ldap_msg->type != LDAP_TAG_SearchRequest) {
59                 DEBUG(0,("Invalid CLDAP request type %d from %s\n",
60                          in->ldap_msg->type,
61                          tsocket_address_string(in->src, in)));
62                 cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
63                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP request");
64                 talloc_free(in);
65                 return;
66         }
67
68         search = &in->ldap_msg->r.SearchRequest;
69
70         if (strcmp("", search->basedn) != 0) {
71                 DEBUG(0,("Invalid CLDAP basedn '%s' from %s\n",
72                          search->basedn,
73                          tsocket_address_string(in->src, in)));
74                 cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
75                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP basedn");
76                 talloc_free(in);
77                 return;
78         }
79
80         if (search->scope != LDAP_SEARCH_SCOPE_BASE) {
81                 DEBUG(0,("Invalid CLDAP scope %d from %s\n",
82                          search->scope,
83                          tsocket_address_string(in->src, in)));
84                 cldap_error_reply(cldap, in->ldap_msg->messageid, in->src,
85                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP scope");
86                 talloc_free(in);
87                 return;
88         }
89
90         cldapd_rootdse_request(cldap, cldapd, in,
91                                in->ldap_msg->messageid,
92                                search, in->src);
93         talloc_free(in);
94 }
95
96
97 /*
98   start listening on the given address
99 */
100 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, struct loadparm_context *lp_ctx,
101                                   const char *address)
102 {
103         struct cldap_socket *cldapsock;
104         struct tsocket_address *socket_address;
105         NTSTATUS status;
106         int ret;
107
108         ret = tsocket_address_inet_from_strings(cldapd,
109                                                 "ip",
110                                                 address,
111                                                 lpcfg_cldap_port(lp_ctx),
112                                                 &socket_address);
113         if (ret != 0) {
114                 status = map_nt_error_from_unix_common(errno);
115                 DEBUG(0,("invalid address %s:%d - %s:%s\n",
116                          address, lpcfg_cldap_port(lp_ctx),
117                          gai_strerror(ret), nt_errstr(status)));
118                 return status;
119         }
120
121         /* listen for unicasts on the CLDAP port (389) */
122         status = cldap_socket_init(cldapd,
123                                    socket_address,
124                                    NULL,
125                                    &cldapsock);
126         if (!NT_STATUS_IS_OK(status)) {
127                 DEBUG(0,("Failed to bind to %s - %s\n", 
128                          tsocket_address_string(socket_address, socket_address),
129                          nt_errstr(status)));
130                 talloc_free(socket_address);
131                 return status;
132         }
133         talloc_free(socket_address);
134
135         cldap_set_incoming_handler(cldapsock, cldapd->task->event_ctx,
136                                    cldapd_request_handler, cldapd);
137
138         return NT_STATUS_OK;
139 }
140
141 /*
142   setup our listening sockets on the configured network interfaces
143 */
144 static NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd, struct loadparm_context *lp_ctx,
145                                           struct interface *ifaces)
146 {
147         int i, num_interfaces;
148         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
149         NTSTATUS status;
150
151         num_interfaces = iface_list_count(ifaces);
152
153         /* if we are allowing incoming packets from any address, then
154            we need to bind to the wildcard address */
155         if (!lpcfg_bind_interfaces_only(lp_ctx)) {
156                 int num_binds = 0;
157                 char **wcard = iface_list_wildcard(cldapd);
158                 NT_STATUS_HAVE_NO_MEMORY(wcard);
159                 for (i=0; wcard[i]; i++) {
160                         status = cldapd_add_socket(cldapd, lp_ctx, wcard[i]);
161                         if (NT_STATUS_IS_OK(status)) {
162                                 num_binds++;
163                         }
164                 }
165                 talloc_free(wcard);
166                 if (num_binds == 0) {
167                         return NT_STATUS_INVALID_PARAMETER_MIX;
168                 }
169         }
170
171         /* now we have to also listen on the specific interfaces,
172            so that replies always come from the right IP */
173         for (i=0; i<num_interfaces; i++) {
174                 const char *address = talloc_strdup(tmp_ctx, iface_list_n_ip(ifaces, i));
175                 status = cldapd_add_socket(cldapd, lp_ctx, address);
176                 NT_STATUS_NOT_OK_RETURN(status);
177         }
178
179         talloc_free(tmp_ctx);
180
181         return NT_STATUS_OK;
182 }
183
184 /*
185   startup the cldapd task
186 */
187 static void cldapd_task_init(struct task_server *task)
188 {
189         struct cldapd_server *cldapd;
190         NTSTATUS status;
191         struct interface *ifaces;
192         
193         load_interface_list(task, task->lp_ctx, &ifaces);
194
195         if (iface_list_count(ifaces) == 0) {
196                 task_server_terminate(task, "cldapd: no network interfaces configured", false);
197                 return;
198         }
199
200         switch (lpcfg_server_role(task->lp_ctx)) {
201         case ROLE_STANDALONE:
202                 task_server_terminate(task, "cldap_server: no CLDAP server required in standalone configuration", 
203                                       false);
204                 return;
205         case ROLE_DOMAIN_MEMBER:
206                 task_server_terminate(task, "cldap_server: no CLDAP server required in member server configuration",
207                                       false);
208                 return;
209         case ROLE_ACTIVE_DIRECTORY_DC:
210                 /* Yes, we want an CLDAP server */
211                 break;
212         }
213
214         task_server_set_title(task, "task[cldapd]");
215
216         cldapd = talloc(task, struct cldapd_server);
217         if (cldapd == NULL) {
218                 task_server_terminate(task, "cldapd: out of memory", true);
219                 return;
220         }
221
222         cldapd->task = task;
223         cldapd->samctx = samdb_connect(cldapd, task->event_ctx, task->lp_ctx, system_session(task->lp_ctx), 0);
224         if (cldapd->samctx == NULL) {
225                 task_server_terminate(task, "cldapd failed to open samdb", true);
226                 return;
227         }
228
229         /* start listening on the configured network interfaces */
230         status = cldapd_startup_interfaces(cldapd, task->lp_ctx, ifaces);
231         if (!NT_STATUS_IS_OK(status)) {
232                 task_server_terminate(task, "cldapd failed to setup interfaces", true);
233                 return;
234         }
235
236         irpc_add_name(task->msg_ctx, "cldap_server");
237 }
238
239
240 /*
241   register ourselves as a available server
242 */
243 NTSTATUS server_service_cldapd_init(void)
244 {
245         return register_server_service("cldap", cldapd_task_init);
246 }