r6817: - fixed empty ldap search elements in filters
[jelmer/samba4-debian.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/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   handle incoming cldap requests
32 */
33 static void cldapd_request_handler(struct cldap_socket *cldap, 
34                                    struct ldap_message *ldap_msg, 
35                                    const char *src_address, int src_port)
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_address, 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->filter, src_address, src_port);
50         } else {
51                 DEBUG(0,("Unknown CLDAP search for '%s'\n", 
52                          ldap_msg->r.SearchRequest.filter));
53                 cldap_empty_reply(cldap, ldap_msg->messageid, src_address, src_port);
54         }
55 }
56
57
58 /*
59   start listening on the given address
60 */
61 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, const char *address)
62 {
63         struct cldap_socket *cldapsock;
64         NTSTATUS status;
65
66         /* listen for unicasts on port 137 */
67         cldapsock = cldap_socket_init(cldapd, cldapd->task->event_ctx);
68         NT_STATUS_HAVE_NO_MEMORY(cldapsock);
69
70         status = socket_listen(cldapsock->sock, address, lp_cldap_port(), 0, 0);
71         if (!NT_STATUS_IS_OK(status)) {
72                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
73                          address, lp_cldap_port(), nt_errstr(status)));
74                 talloc_free(cldapsock);
75                 return status;
76         }
77
78         cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
79
80         return NT_STATUS_OK;
81 }
82
83
84 /*
85   setup our listening sockets on the configured network interfaces
86 */
87 NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd)
88 {
89         int num_interfaces = iface_count();
90         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
91         NTSTATUS status;
92
93         /* if we are allowing incoming packets from any address, then
94            we need to bind to the wildcard address */
95         if (!lp_bind_interfaces_only()) {
96                 status = cldapd_add_socket(cldapd, "0.0.0.0");
97                 NT_STATUS_NOT_OK_RETURN(status);
98         } else {
99                 int i;
100
101                 for (i=0; i<num_interfaces; i++) {
102                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
103                         status = cldapd_add_socket(cldapd, address);
104                         NT_STATUS_NOT_OK_RETURN(status);
105                 }
106         }
107
108         talloc_free(tmp_ctx);
109
110         return NT_STATUS_OK;
111 }
112
113 /*
114   startup the cldapd task
115 */
116 static void cldapd_task_init(struct task_server *task)
117 {
118         struct cldapd_server *cldapd;
119         NTSTATUS status;
120
121         if (iface_count() == 0) {
122                 task_terminate(task, "cldapd: no network interfaces configured");
123                 return;
124         }
125
126         cldapd = talloc(task, struct cldapd_server);
127         if (cldapd == NULL) {
128                 task_terminate(task, "cldapd: out of memory");
129                 return;
130         }
131
132         cldapd->task = task;
133
134         /* start listening on the configured network interfaces */
135         status = cldapd_startup_interfaces(cldapd);
136         if (!NT_STATUS_IS_OK(status)) {
137                 task_terminate(task, "cldapd failed to setup interfaces");
138                 return;
139         }
140 }
141
142
143 /*
144   initialise the cldapd server
145  */
146 static NTSTATUS cldapd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
147 {
148         return task_server_startup(event_ctx, model_ops, cldapd_task_init);
149 }
150
151
152 /*
153   register ourselves as a available server
154 */
155 NTSTATUS server_service_cldapd_init(void)
156 {
157         return register_server_service("cldap", cldapd_init);
158 }