r12804: This patch reworks the Samba4 sockets layer to use a socket_address
[ira/wip.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/ldap/ldap.h"
25 #include "lib/socket/socket.h"
26 #include "lib/messaging/irpc.h"
27 #include "smbd/service_task.h"
28 #include "cldap_server/cldap_server.h"
29
30 /*
31   handle incoming cldap requests
32 */
33 static void cldapd_request_handler(struct cldap_socket *cldap, 
34                                    struct ldap_message *ldap_msg, 
35                                    struct socket_address *src)
36 {
37         struct ldap_SearchRequest *search;
38         if (ldap_msg->type != LDAP_TAG_SearchRequest) {
39                 DEBUG(0,("Invalid CLDAP request type %d from %s:%d\n", 
40                          ldap_msg->type, src->addr, src->port));
41                 return;
42         }
43
44         search = &ldap_msg->r.SearchRequest;
45
46         if (search->num_attributes == 1 &&
47             strcasecmp(search->attributes[0], "netlogon") == 0) {
48                 cldapd_netlogon_request(cldap, ldap_msg->messageid,
49                                         search->tree, src);
50         } else {
51                 DEBUG(0,("Unknown CLDAP search for '%s'\n", 
52                          ldb_filter_from_tree(ldap_msg, 
53                                               ldap_msg->r.SearchRequest.tree)));
54                 cldap_empty_reply(cldap, ldap_msg->messageid, src);
55         }
56 }
57
58
59 /*
60   start listening on the given address
61 */
62 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, const char *address)
63 {
64         struct cldap_socket *cldapsock;
65         struct socket_address *socket_address;
66         NTSTATUS status;
67
68         /* listen for unicasts on the CLDAP port (389) */
69         cldapsock = cldap_socket_init(cldapd, cldapd->task->event_ctx);
70         NT_STATUS_HAVE_NO_MEMORY(cldapsock);
71
72         socket_address = socket_address_from_strings(cldapsock, cldapsock->sock->backend_name, 
73                                                      address, lp_cldap_port());
74         if (!socket_address) {
75                 talloc_free(cldapsock);
76                 return NT_STATUS_NO_MEMORY;
77         }
78
79         status = socket_listen(cldapsock->sock, socket_address, 0, 0);
80         if (!NT_STATUS_IS_OK(status)) {
81                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
82                          address, lp_cldap_port(), nt_errstr(status)));
83                 talloc_free(cldapsock);
84                 return status;
85         }
86
87         talloc_free(socket_address);
88
89         cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
90
91         return NT_STATUS_OK;
92 }
93
94
95 /*
96   setup our listening sockets on the configured network interfaces
97 */
98 NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd)
99 {
100         int num_interfaces = iface_count();
101         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
102         NTSTATUS status;
103
104         /* if we are allowing incoming packets from any address, then
105            we need to bind to the wildcard address */
106         if (!lp_bind_interfaces_only()) {
107                 status = cldapd_add_socket(cldapd, "0.0.0.0");
108                 NT_STATUS_NOT_OK_RETURN(status);
109         } else {
110                 int i;
111
112                 for (i=0; i<num_interfaces; i++) {
113                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
114                         status = cldapd_add_socket(cldapd, address);
115                         NT_STATUS_NOT_OK_RETURN(status);
116                 }
117         }
118
119         talloc_free(tmp_ctx);
120
121         return NT_STATUS_OK;
122 }
123
124 /*
125   startup the cldapd task
126 */
127 static void cldapd_task_init(struct task_server *task)
128 {
129         struct cldapd_server *cldapd;
130         NTSTATUS status;
131
132         if (iface_count() == 0) {
133                 task_server_terminate(task, "cldapd: no network interfaces configured");
134                 return;
135         }
136
137         cldapd = talloc(task, struct cldapd_server);
138         if (cldapd == NULL) {
139                 task_server_terminate(task, "cldapd: out of memory");
140                 return;
141         }
142
143         cldapd->task = task;
144         cldapd->samctx = NULL;
145
146         /* start listening on the configured network interfaces */
147         status = cldapd_startup_interfaces(cldapd);
148         if (!NT_STATUS_IS_OK(status)) {
149                 task_server_terminate(task, "cldapd failed to setup interfaces");
150                 return;
151         }
152
153         irpc_add_name(task->msg_ctx, "cldap_server");
154 }
155
156
157 /*
158   initialise the cldapd server
159  */
160 static NTSTATUS cldapd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
161 {
162         return task_server_startup(event_ctx, model_ops, cldapd_task_init);
163 }
164
165
166 /*
167   register ourselves as a available server
168 */
169 NTSTATUS server_service_cldapd_init(void)
170 {
171         return register_server_service("cldap", cldapd_init);
172 }