r8272: added the hooks for adding a name to a messaging context, so we will
[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 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 "lib/messaging/irpc.h"
28 #include "smbd/service_task.h"
29 #include "cldap_server/cldap_server.h"
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 ldap_SearchRequest *search;
39         if (ldap_msg->type != LDAP_TAG_SearchRequest) {
40                 DEBUG(0,("Invalid CLDAP request type %d from %s:%d\n", 
41                          ldap_msg->type, src_address, src_port));
42                 return;
43         }
44
45         search = &ldap_msg->r.SearchRequest;
46
47         if (search->num_attributes == 1 &&
48             strcasecmp(search->attributes[0], "netlogon") == 0) {
49                 cldapd_netlogon_request(cldap, ldap_msg->messageid,
50                                         search->tree, src_address, src_port);
51         } else {
52                 DEBUG(0,("Unknown CLDAP search for '%s'\n", 
53                          ldb_filter_from_tree(ldap_msg, 
54                                               ldap_msg->r.SearchRequest.tree)));
55                 cldap_empty_reply(cldap, ldap_msg->messageid, src_address, src_port);
56         }
57 }
58
59
60 /*
61   start listening on the given address
62 */
63 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, const char *address)
64 {
65         struct cldap_socket *cldapsock;
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         status = socket_listen(cldapsock->sock, address, lp_cldap_port(), 0, 0);
73         if (!NT_STATUS_IS_OK(status)) {
74                 DEBUG(0,("Failed to bind to %s:%d - %s\n", 
75                          address, lp_cldap_port(), nt_errstr(status)));
76                 talloc_free(cldapsock);
77                 return status;
78         }
79
80         cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
81
82         return NT_STATUS_OK;
83 }
84
85
86 /*
87   setup our listening sockets on the configured network interfaces
88 */
89 NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd)
90 {
91         int num_interfaces = iface_count();
92         TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
93         NTSTATUS status;
94
95         /* if we are allowing incoming packets from any address, then
96            we need to bind to the wildcard address */
97         if (!lp_bind_interfaces_only()) {
98                 status = cldapd_add_socket(cldapd, "0.0.0.0");
99                 NT_STATUS_NOT_OK_RETURN(status);
100         } else {
101                 int i;
102
103                 for (i=0; i<num_interfaces; i++) {
104                         const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
105                         status = cldapd_add_socket(cldapd, address);
106                         NT_STATUS_NOT_OK_RETURN(status);
107                 }
108         }
109
110         talloc_free(tmp_ctx);
111
112         return NT_STATUS_OK;
113 }
114
115 /*
116   startup the cldapd task
117 */
118 static void cldapd_task_init(struct task_server *task)
119 {
120         struct cldapd_server *cldapd;
121         NTSTATUS status;
122
123         if (iface_count() == 0) {
124                 task_server_terminate(task, "cldapd: no network interfaces configured");
125                 return;
126         }
127
128         cldapd = talloc(task, struct cldapd_server);
129         if (cldapd == NULL) {
130                 task_server_terminate(task, "cldapd: out of memory");
131                 return;
132         }
133
134         cldapd->task = task;
135         cldapd->samctx = NULL;
136
137         /* start listening on the configured network interfaces */
138         status = cldapd_startup_interfaces(cldapd);
139         if (!NT_STATUS_IS_OK(status)) {
140                 task_server_terminate(task, "cldapd failed to setup interfaces");
141                 return;
142         }
143
144         irpc_add_name(task->msg_ctx, "cldap_server");
145 }
146
147
148 /*
149   initialise the cldapd server
150  */
151 static NTSTATUS cldapd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
152 {
153         return task_server_startup(event_ctx, model_ops, cldapd_task_init);
154 }
155
156
157 /*
158   register ourselves as a available server
159 */
160 NTSTATUS server_service_cldapd_init(void)
161 {
162         return register_server_service("cldap", cldapd_init);
163 }