r23792: convert Samba4 to GPLv3
[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 "libcli/ldap/ldap.h"
24 #include "lib/socket/socket.h"
25 #include "lib/messaging/irpc.h"
26 #include "smbd/service_task.h"
27 #include "smbd/service.h"
28 #include "cldap_server/cldap_server.h"
29 #include "system/network.h"
30 #include "lib/socket/netif.h"
31 #include "lib/ldb/include/ldb.h"
32 #include "lib/ldb/include/ldb_errors.h"
33 #include "dsdb/samdb/samdb.h"
34 #include "db_wrap.h"
35 #include "auth/auth.h"
36
37 /*
38   handle incoming cldap requests
39 */
40 static void cldapd_request_handler(struct cldap_socket *cldap, 
41                                    struct ldap_message *ldap_msg, 
42                                    struct socket_address *src)
43 {
44         struct ldap_SearchRequest *search;
45         if (ldap_msg->type != LDAP_TAG_SearchRequest) {
46                 DEBUG(0,("Invalid CLDAP request type %d from %s:%d\n", 
47                          ldap_msg->type, src->addr, src->port));
48                 cldap_error_reply(cldap, ldap_msg->messageid, src,
49                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP request");
50                 return;
51         }
52
53         search = &ldap_msg->r.SearchRequest;
54
55         if (strcmp("", search->basedn) != 0) {
56                 DEBUG(0,("Invalid CLDAP basedn '%s' from %s:%d\n", 
57                          search->basedn, src->addr, src->port));
58                 cldap_error_reply(cldap, ldap_msg->messageid, src,
59                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP basedn");
60                 return;
61         }
62
63         if (search->scope != LDAP_SEARCH_SCOPE_BASE) {
64                 DEBUG(0,("Invalid CLDAP scope %d from %s:%d\n", 
65                          search->scope, src->addr, src->port));
66                 cldap_error_reply(cldap, ldap_msg->messageid, src,
67                                   LDAP_OPERATIONS_ERROR, "Invalid CLDAP scope");
68                 return;
69         }
70
71         if (search->num_attributes == 1 &&
72             strcasecmp(search->attributes[0], "netlogon") == 0) {
73                 cldapd_netlogon_request(cldap, ldap_msg->messageid,
74                                         search->tree, src);
75                 return;
76         }
77
78         cldapd_rootdse_request(cldap, ldap_msg->messageid,
79                                search, src);
80 }
81
82
83 /*
84   start listening on the given address
85 */
86 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, const char *address)
87 {
88         struct cldap_socket *cldapsock;
89         struct socket_address *socket_address;
90         NTSTATUS status;
91
92         /* listen for unicasts on the CLDAP port (389) */
93         cldapsock = cldap_socket_init(cldapd, cldapd->task->event_ctx);
94         NT_STATUS_HAVE_NO_MEMORY(cldapsock);
95
96         socket_address = socket_address_from_strings(cldapsock, cldapsock->sock->backend_name, 
97                                                      address, lp_cldap_port());
98         if (!socket_address) {
99                 talloc_free(cldapsock);
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         status = socket_listen(cldapsock->sock, socket_address, 0, 0);
104         if (!NT_STATUS_IS_OK(status)) {
105                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
106                          address, lp_cldap_port(), nt_errstr(status)));
107                 talloc_free(cldapsock);
108                 return status;
109         }
110
111         talloc_free(socket_address);
112
113         cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
114
115         return NT_STATUS_OK;
116 }
117
118
119 /*
120   setup our listening sockets on the configured network interfaces
121 */
122 static NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd)
123 {
124         int num_interfaces = iface_count();
125         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
126         NTSTATUS status;
127
128         /* if we are allowing incoming packets from any address, then
129            we need to bind to the wildcard address */
130         if (!lp_bind_interfaces_only()) {
131                 status = cldapd_add_socket(cldapd, "0.0.0.0");
132                 NT_STATUS_NOT_OK_RETURN(status);
133         } else {
134                 int i;
135
136                 for (i=0; i<num_interfaces; i++) {
137                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
138                         status = cldapd_add_socket(cldapd, address);
139                         NT_STATUS_NOT_OK_RETURN(status);
140                 }
141         }
142
143         talloc_free(tmp_ctx);
144
145         return NT_STATUS_OK;
146 }
147
148 /*
149   startup the cldapd task
150 */
151 static void cldapd_task_init(struct task_server *task)
152 {
153         struct cldapd_server *cldapd;
154         NTSTATUS status;
155
156         if (iface_count() == 0) {
157                 task_server_terminate(task, "cldapd: no network interfaces configured");
158                 return;
159         }
160
161         task_server_set_title(task, "task[cldapd]");
162
163         cldapd = talloc(task, struct cldapd_server);
164         if (cldapd == NULL) {
165                 task_server_terminate(task, "cldapd: out of memory");
166                 return;
167         }
168
169         cldapd->task = task;
170         cldapd->samctx = samdb_connect(cldapd, anonymous_session(cldapd));
171         if (cldapd->samctx == NULL) {
172                 task_server_terminate(task, "cldapd failed to open samdb");
173                 return;
174         }
175
176         /* start listening on the configured network interfaces */
177         status = cldapd_startup_interfaces(cldapd);
178         if (!NT_STATUS_IS_OK(status)) {
179                 task_server_terminate(task, "cldapd failed to setup interfaces");
180                 return;
181         }
182
183         irpc_add_name(task->msg_ctx, "cldap_server");
184 }
185
186
187 /*
188   initialise the cldapd server
189  */
190 static NTSTATUS cldapd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
191 {
192         return task_server_startup(event_ctx, model_ops, cldapd_task_init);
193 }
194
195
196 /*
197   register ourselves as a available server
198 */
199 NTSTATUS server_service_cldapd_init(void)
200 {
201         return register_server_service("cldap", cldapd_init);
202 }