r6725: the beginnings of a cldap server
[gd/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 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/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service_task.h"
28 #include "cldap_server/cldap_server.h"
29
30
31 /*
32   handle incoming cldap requests
33 */
34 static void cldapd_request_handler(struct cldap_socket *cldap, 
35                                    struct ldap_message *ldap_msg, 
36                                    const char *src_address, int src_port)
37 {
38         struct cldapd_server *cldapd = talloc_get_type(cldap->incoming.private,
39                                                        struct cldapd_server);
40         if (ldap_msg->type != LDAP_TAG_SearchRequest) {
41                 DEBUG(0,("Invalid CLDAP request type %d from %s:%d\n", 
42                          ldap_msg->type, src_address, src_port));
43                 return;
44         }
45         DEBUG(0,("CLDAP search for '%s'\n", ldap_msg->r.SearchRequest.filter));
46 }
47
48 /*
49   start listening on the given address
50 */
51 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, const char *address)
52 {
53         struct cldap_socket *cldapsock;
54         NTSTATUS status;
55
56         /* listen for unicasts on port 137 */
57         cldapsock = cldap_socket_init(cldapd, cldapd->task->event_ctx);
58         NT_STATUS_HAVE_NO_MEMORY(cldapsock);
59
60         status = socket_listen(cldapsock->sock, address, lp_cldap_port(), 0, 0);
61         if (!NT_STATUS_IS_OK(status)) {
62                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
63                          address, lp_cldap_port(), nt_errstr(status)));
64                 talloc_free(cldapsock);
65                 return status;
66         }
67
68         cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
69
70         return NT_STATUS_OK;
71 }
72
73
74 /*
75   setup our listening sockets on the configured network interfaces
76 */
77 NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd)
78 {
79         int num_interfaces = iface_count();
80         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
81         NTSTATUS status;
82
83         /* if we are allowing incoming packets from any address, then
84            we also need to bind to the wildcard address */
85         if (!lp_bind_interfaces_only()) {
86                 status = cldapd_add_socket(cldapd, "0.0.0.0");
87                 NT_STATUS_NOT_OK_RETURN(status);
88         } else {
89                 int i;
90
91                 for (i=0; i<num_interfaces; i++) {
92                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
93                         status = cldapd_add_socket(cldapd, address);
94                         NT_STATUS_NOT_OK_RETURN(status);
95                 }
96         }
97
98         talloc_free(tmp_ctx);
99
100         return NT_STATUS_OK;
101 }
102
103 /*
104   startup the cldapd task
105 */
106 static void cldapd_task_init(struct task_server *task)
107 {
108         struct cldapd_server *cldapd;
109         NTSTATUS status;
110
111         if (iface_count() == 0) {
112                 task_terminate(task, "cldapd: no network interfaces configured");
113                 return;
114         }
115
116         cldapd = talloc(task, struct cldapd_server);
117         if (cldapd == NULL) {
118                 task_terminate(task, "cldapd: out of memory");
119                 return;
120         }
121
122         cldapd->task = task;
123
124         /* start listening on the configured network interfaces */
125         status = cldapd_startup_interfaces(cldapd);
126         if (!NT_STATUS_IS_OK(status)) {
127                 task_terminate(task, "cldapd failed to setup interfaces");
128                 return;
129         }
130 }
131
132
133 /*
134   initialise the cldapd server
135  */
136 static NTSTATUS cldapd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
137 {
138         return task_server_startup(event_ctx, model_ops, cldapd_task_init);
139 }
140
141
142 /*
143   register ourselves as a available server
144 */
145 NTSTATUS server_service_cldapd_init(void)
146 {
147         return register_server_service("cldap", cldapd_init);
148 }