2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
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 #include "system/network.h"
30 #include "netif/netif.h"
33 handle incoming cldap requests
35 static void cldapd_request_handler(struct cldap_socket *cldap,
36 struct ldap_message *ldap_msg,
37 struct socket_address *src)
39 struct ldap_SearchRequest *search;
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->addr, src->port));
46 search = &ldap_msg->r.SearchRequest;
48 if (search->num_attributes == 1 &&
49 strcasecmp(search->attributes[0], "netlogon") == 0) {
50 cldapd_netlogon_request(cldap, ldap_msg->messageid,
53 DEBUG(0,("Unknown CLDAP search for '%s'\n",
54 ldb_filter_from_tree(ldap_msg,
55 ldap_msg->r.SearchRequest.tree)));
56 cldap_empty_reply(cldap, ldap_msg->messageid, src);
62 start listening on the given address
64 static NTSTATUS cldapd_add_socket(struct cldapd_server *cldapd, const char *address)
66 struct cldap_socket *cldapsock;
67 struct socket_address *socket_address;
70 /* listen for unicasts on the CLDAP port (389) */
71 cldapsock = cldap_socket_init(cldapd, cldapd->task->event_ctx);
72 NT_STATUS_HAVE_NO_MEMORY(cldapsock);
74 socket_address = socket_address_from_strings(cldapsock, cldapsock->sock->backend_name,
75 address, lp_cldap_port());
76 if (!socket_address) {
77 talloc_free(cldapsock);
78 return NT_STATUS_NO_MEMORY;
81 status = socket_listen(cldapsock->sock, socket_address, 0, 0);
82 if (!NT_STATUS_IS_OK(status)) {
83 DEBUG(0,("Failed to bind to %s:%d - %s\n",
84 address, lp_cldap_port(), nt_errstr(status)));
85 talloc_free(cldapsock);
89 talloc_free(socket_address);
91 cldap_set_incoming_handler(cldapsock, cldapd_request_handler, cldapd);
98 setup our listening sockets on the configured network interfaces
100 static NTSTATUS cldapd_startup_interfaces(struct cldapd_server *cldapd)
102 int num_interfaces = iface_count();
103 TALLOC_CTX *tmp_ctx = talloc_new(cldapd);
106 /* if we are allowing incoming packets from any address, then
107 we need to bind to the wildcard address */
108 if (!lp_bind_interfaces_only()) {
109 status = cldapd_add_socket(cldapd, "0.0.0.0");
110 NT_STATUS_NOT_OK_RETURN(status);
114 for (i=0; i<num_interfaces; i++) {
115 const char *address = talloc_strdup(tmp_ctx, iface_n_ip(i));
116 status = cldapd_add_socket(cldapd, address);
117 NT_STATUS_NOT_OK_RETURN(status);
121 talloc_free(tmp_ctx);
127 startup the cldapd task
129 static void cldapd_task_init(struct task_server *task)
131 struct cldapd_server *cldapd;
134 if (iface_count() == 0) {
135 task_server_terminate(task, "cldapd: no network interfaces configured");
139 cldapd = talloc(task, struct cldapd_server);
140 if (cldapd == NULL) {
141 task_server_terminate(task, "cldapd: out of memory");
146 cldapd->samctx = NULL;
148 /* start listening on the configured network interfaces */
149 status = cldapd_startup_interfaces(cldapd);
150 if (!NT_STATUS_IS_OK(status)) {
151 task_server_terminate(task, "cldapd failed to setup interfaces");
155 irpc_add_name(task->msg_ctx, "cldap_server");
160 initialise the cldapd server
162 static NTSTATUS cldapd_init(struct event_context *event_ctx, const struct model_ops *model_ops)
164 return task_server_startup(event_ctx, model_ops, cldapd_task_init);
169 register ourselves as a available server
171 NTSTATUS server_service_cldapd_init(void)
173 return register_server_service("cldap", cldapd_init);