r20378: add new error code that says the schema mismatches between DC's
[Samba.git] / source / winbind / wb_server.c
blob2e6978526127a79913d0c0f3138f6c408b5b256e
1 /*
2 Unix SMB/CIFS implementation.
3 Main winbindd server routines
5 Copyright (C) Stefan Metzmacher 2005
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.
23 #include "includes.h"
24 #include "lib/socket/socket.h"
25 #include "lib/util/dlinklist.h"
26 #include "lib/events/events.h"
27 #include "smbd/service_task.h"
28 #include "smbd/process_model.h"
29 #include "smbd/service_stream.h"
30 #include "nsswitch/winbind_nss_config.h"
31 #include "winbind/wb_server.h"
32 #include "lib/stream/packet.h"
33 #include "smbd/service.h"
34 #include "param/secrets.h"
36 void wbsrv_terminate_connection(struct wbsrv_connection *wbconn, const char *reason)
38 stream_terminate_connection(wbconn->conn, reason);
42 called on a tcp recv error
44 static void wbsrv_recv_error(void *private, NTSTATUS status)
46 struct wbsrv_connection *wbconn = talloc_get_type(private, struct wbsrv_connection);
47 wbsrv_terminate_connection(wbconn, nt_errstr(status));
50 static void wbsrv_accept(struct stream_connection *conn)
52 struct wbsrv_listen_socket *listen_socket = talloc_get_type(conn->private,
53 struct wbsrv_listen_socket);
54 struct wbsrv_connection *wbconn;
56 wbconn = talloc_zero(conn, struct wbsrv_connection);
57 if (!wbconn) {
58 stream_terminate_connection(conn, "wbsrv_accept: out of memory");
59 return;
61 wbconn->conn = conn;
62 wbconn->listen_socket = listen_socket;
63 conn->private = wbconn;
65 wbconn->packet = packet_init(wbconn);
66 if (wbconn->packet == NULL) {
67 wbsrv_terminate_connection(wbconn, "wbsrv_accept: out of memory");
68 return;
70 packet_set_private(wbconn->packet, wbconn);
71 packet_set_socket(wbconn->packet, conn->socket);
72 packet_set_callback(wbconn->packet, wbsrv_samba3_process);
73 packet_set_full_request(wbconn->packet, wbsrv_samba3_packet_full_request);
74 packet_set_error_handler(wbconn->packet, wbsrv_recv_error);
75 packet_set_event_context(wbconn->packet, conn->event.ctx);
76 packet_set_fde(wbconn->packet, conn->event.fde);
77 packet_set_serialise(wbconn->packet);
81 receive some data on a winbind connection
83 static void wbsrv_recv(struct stream_connection *conn, uint16_t flags)
85 struct wbsrv_connection *wbconn = talloc_get_type(conn->private,
86 struct wbsrv_connection);
87 packet_recv(wbconn->packet);
92 called when we can write to a connection
94 static void wbsrv_send(struct stream_connection *conn, uint16_t flags)
96 struct wbsrv_connection *wbconn = talloc_get_type(conn->private,
97 struct wbsrv_connection);
98 packet_queue_run(wbconn->packet);
101 static const struct stream_server_ops wbsrv_ops = {
102 .name = "winbind samba3 protocol",
103 .accept_connection = wbsrv_accept,
104 .recv_handler = wbsrv_recv,
105 .send_handler = wbsrv_send
109 startup the winbind task
111 static void winbind_task_init(struct task_server *task)
113 uint16_t port = 1;
114 const struct model_ops *model_ops;
115 NTSTATUS status;
116 struct wbsrv_service *service;
117 struct wbsrv_listen_socket *listen_socket;
119 task_server_set_title(task, "task[winbind]");
121 /* within the winbind task we want to be a single process, so
122 ask for the single process model ops and pass these to the
123 stream_setup_socket() call. */
124 model_ops = process_model_byname("single");
125 if (!model_ops) {
126 task_server_terminate(task,
127 "Can't find 'single' process model_ops");
128 return;
131 /* Make sure the directory for the Samba3 socket exists, and is of the correct permissions */
132 if (!directory_create_or_exist(lp_winbindd_socket_directory(), geteuid(), 0755)) {
133 task_server_terminate(task,
134 "Cannot create winbindd pipe directory");
135 return;
138 service = talloc_zero(task, struct wbsrv_service);
139 if (!service) goto nomem;
140 service->task = task;
142 service->primary_sid = secrets_get_domain_sid(service,
143 lp_workgroup());
144 if (service->primary_sid == NULL) {
145 task_server_terminate(
146 task, nt_errstr(NT_STATUS_CANT_ACCESS_DOMAIN_INFO));
147 return;
150 /* setup the unprivileged samba3 socket */
151 listen_socket = talloc(service, struct wbsrv_listen_socket);
152 if (!listen_socket) goto nomem;
153 listen_socket->socket_path = talloc_asprintf(listen_socket, "%s/%s",
154 lp_winbindd_socket_directory(),
155 WINBINDD_SAMBA3_SOCKET);
156 if (!listen_socket->socket_path) goto nomem;
157 listen_socket->service = service;
158 listen_socket->privileged = False;
159 status = stream_setup_socket(task->event_ctx, model_ops,
160 &wbsrv_ops, "unix",
161 listen_socket->socket_path, &port,
162 listen_socket);
163 if (!NT_STATUS_IS_OK(status)) goto listen_failed;
165 /* setup the privileged samba3 socket */
166 listen_socket = talloc(service, struct wbsrv_listen_socket);
167 if (!listen_socket) goto nomem;
168 listen_socket->socket_path =
169 smbd_tmp_path(listen_socket,
170 WINBINDD_SAMBA3_PRIVILEGED_SOCKET);
171 if (!listen_socket->socket_path) goto nomem;
172 listen_socket->service = service;
173 listen_socket->privileged = True;
174 status = stream_setup_socket(task->event_ctx, model_ops,
175 &wbsrv_ops, "unix",
176 listen_socket->socket_path, &port,
177 listen_socket);
178 if (!NT_STATUS_IS_OK(status)) goto listen_failed;
180 status = wbsrv_init_irpc(service);
181 if (!NT_STATUS_IS_OK(status)) goto irpc_failed;
183 return;
185 listen_failed:
186 DEBUG(0,("stream_setup_socket(path=%s) failed - %s\n",
187 listen_socket->socket_path, nt_errstr(status)));
188 task_server_terminate(task, nt_errstr(status));
189 return;
190 irpc_failed:
191 DEBUG(0,("wbsrv_init_irpc() failed - %s\n",
192 nt_errstr(status)));
193 task_server_terminate(task, nt_errstr(status));
194 return;
195 nomem:
196 task_server_terminate(task, nt_errstr(NT_STATUS_NO_MEMORY));
197 return;
201 initialise the winbind server
203 static NTSTATUS winbind_init(struct event_context *event_ctx,
204 const struct model_ops *model_ops)
206 return task_server_startup(event_ctx, model_ops, winbind_task_init);
210 register ourselves as a available server
212 NTSTATUS server_service_winbind_init(void)
214 return register_server_service("winbind", winbind_init);