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