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