03a443ac16b941cab2a34ae5bfe0383ff303c982
[ira/wip.git] / source4 / winbind / wb_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main winbindd server routines
4
5    Copyright (C) Stefan Metzmacher      2005-2008
6    Copyright (C) Andrew Tridgell        2005
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2010
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "smbd/process_model.h"
25 #include "winbind/wb_server.h"
26 #include "lib/stream/packet.h"
27 #include "lib/tsocket/tsocket.h"
28 #include "libcli/util/tstream.h"
29 #include "param/param.h"
30 #include "param/secrets.h"
31
32 void wbsrv_terminate_connection(struct wbsrv_connection *wbconn, const char *reason)
33 {
34         stream_terminate_connection(wbconn->conn, reason);
35 }
36
37 static void wbsrv_call_loop(struct tevent_req *subreq)
38 {
39         struct wbsrv_connection *wbsrv_conn = tevent_req_callback_data(subreq,
40                                       struct wbsrv_connection);
41         struct wbsrv_samba3_call *call;
42         NTSTATUS status;
43
44         call = talloc_zero(wbsrv_conn, struct wbsrv_samba3_call);
45         if (call == NULL) {
46                 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_call_loop: "
47                                 "no memory for wbsrv_samba3_call");
48                 return;
49         }
50         call->wbconn = wbsrv_conn;
51
52         status = tstream_read_pdu_blob_recv(subreq,
53                                             call,
54                                             &call->in);
55         TALLOC_FREE(subreq);
56         if (!NT_STATUS_IS_OK(status)) {
57                 const char *reason;
58
59                 reason = talloc_asprintf(call, "wbsrv_call_loop: "
60                                          "tstream_read_pdu_blob_recv() - %s",
61                                          nt_errstr(status));
62                 if (!reason) {
63                         reason = nt_errstr(status);
64                 }
65
66                 wbsrv_terminate_connection(wbsrv_conn, reason);
67                 return;
68         }
69
70         DEBUG(10,("Received winbind TCP packet of length %lu from %s\n",
71                  (long) call->in.length,
72                  tsocket_address_string(wbsrv_conn->conn->remote_address, call)));
73
74         status = wbsrv_samba3_process(call);
75         if (!NT_STATUS_IS_OK(status)) {
76                 const char *reason;
77
78                 reason = talloc_asprintf(call, "wbsrv_call_loop: "
79                                          "tstream_read_pdu_blob_recv() - %s",
80                                          nt_errstr(status));
81                 if (!reason) {
82                         reason = nt_errstr(status);
83                 }
84
85                 wbsrv_terminate_connection(wbsrv_conn, reason);
86                 return;
87         }
88
89         /*
90          * The winbind pdu's has the length as 4 byte (initial_read_size),
91          * wbsrv_samba3_packet_full_request provides the pdu length then.
92          */
93         subreq = tstream_read_pdu_blob_send(wbsrv_conn,
94                                             wbsrv_conn->conn->event.ctx,
95                                             wbsrv_conn->tstream,
96                                             4, /* initial_read_size */
97                                             wbsrv_samba3_packet_full_request,
98                                             wbsrv_conn);
99         if (subreq == NULL) {
100                 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_call_loop: "
101                                 "no memory for tstream_read_pdu_blob_send");
102                 return;
103         }
104         tevent_req_set_callback(subreq, wbsrv_call_loop, wbsrv_conn);
105 }
106
107 static void wbsrv_accept(struct stream_connection *conn)
108 {
109         struct wbsrv_listen_socket *wbsrv_socket = talloc_get_type(conn->private_data,
110                                                                    struct wbsrv_listen_socket);
111         struct wbsrv_connection *wbsrv_conn;
112         struct tevent_req *subreq;
113         int rc;
114
115         wbsrv_conn = talloc_zero(conn, struct wbsrv_connection);
116         if (wbsrv_conn == NULL) {
117                 stream_terminate_connection(conn, "wbsrv_accept: out of memory");
118                 return;
119         }
120
121         wbsrv_conn->send_queue = tevent_queue_create(conn, "wbsrv_accept");
122         if (wbsrv_conn->send_queue == NULL) {
123                 stream_terminate_connection(conn,
124                                 "wbsrv_accept: out of memory");
125                 return;
126         }
127
128         TALLOC_FREE(conn->event.fde);
129
130         rc = tstream_bsd_existing_socket(wbsrv_conn->tstream,
131                         socket_get_fd(conn->socket),
132                         &wbsrv_conn->tstream);
133         if (rc < 0) {
134                 stream_terminate_connection(conn,
135                                 "wbsrv_accept: out of memory");
136                 return;
137         }
138
139         wbsrv_conn->conn = conn;
140         wbsrv_conn->listen_socket = wbsrv_socket;
141         wbsrv_conn->lp_ctx = wbsrv_socket->service->task->lp_ctx;
142         conn->private_data = wbsrv_conn;
143
144         /*
145          * The winbind pdu's has the length as 4 byte (initial_read_size),
146          * wbsrv_samba3_packet_full_request provides the pdu length then.
147          */
148         subreq = tstream_read_pdu_blob_send(wbsrv_conn,
149                                             wbsrv_conn->conn->event.ctx,
150                                             wbsrv_conn->tstream,
151                                             4, /* initial_read_size */
152                                             wbsrv_samba3_packet_full_request,
153                                             wbsrv_conn);
154         if (subreq == NULL) {
155                 wbsrv_terminate_connection(wbsrv_conn, "wbsrv_accept: "
156                                 "no memory for tstream_read_pdu_blob_send");
157                 return;
158         }
159         tevent_req_set_callback(subreq, wbsrv_call_loop, wbsrv_conn);
160 }
161
162 /*
163   called on a tcp recv
164 */
165 static void wbsrv_recv(struct stream_connection *conn, uint16_t flags)
166 {
167         struct wbsrv_connection *wbsrv_conn = talloc_get_type(conn->private_data,
168                                                         struct wbsrv_connection);
169         wbsrv_terminate_connection(wbsrv_conn, "wbsrv_recv: called");
170 }
171
172 /*
173   called when we can write to a connection
174 */
175 static void wbsrv_send(struct stream_connection *conn, uint16_t flags)
176 {
177         struct wbsrv_connection *wbsrv_conn = talloc_get_type(conn->private_data,
178                                                         struct wbsrv_connection);
179         /* this should never be triggered! */
180         wbsrv_terminate_connection(wbsrv_conn, "wbsrv_send: called");
181 }
182
183 static const struct stream_server_ops wbsrv_ops = {
184         .name                   = "winbind samba3 protocol",
185         .accept_connection      = wbsrv_accept,
186         .recv_handler           = wbsrv_recv,
187         .send_handler           = wbsrv_send
188 };
189
190 /*
191   startup the winbind task
192 */
193 static void winbind_task_init(struct task_server *task)
194 {
195         uint16_t port = 1;
196         const struct model_ops *model_ops;
197         NTSTATUS status;
198         struct wbsrv_service *service;
199         struct wbsrv_listen_socket *listen_socket;
200         char *errstring;
201         struct dom_sid *primary_sid;
202
203         task_server_set_title(task, "task[winbind]");
204
205         /* within the winbind task we want to be a single process, so
206            ask for the single process model ops and pass these to the
207            stream_setup_socket() call. */
208         model_ops = process_model_startup(task->event_ctx, "single");
209         if (!model_ops) {
210                 task_server_terminate(task,
211                                       "Can't find 'single' process model_ops", true);
212                 return;
213         }
214
215         /* Make sure the directory for the Samba3 socket exists, and is of the correct permissions */
216         if (!directory_create_or_exist(lp_winbindd_socket_directory(task->lp_ctx), geteuid(), 0755)) {
217                 task_server_terminate(task,
218                                       "Cannot create winbindd pipe directory", true);
219                 return;
220         }
221
222         /* Make sure the directory for the Samba3 socket exists, and is of the correct permissions */
223         if (!directory_create_or_exist(lp_winbindd_privileged_socket_directory(task->lp_ctx), geteuid(), 0750)) {
224                 task_server_terminate(task,
225                                       "Cannot create winbindd privileged pipe directory", true);
226                 return;
227         }
228
229         service = talloc_zero(task, struct wbsrv_service);
230         if (!service) goto nomem;
231         service->task   = task;
232
233
234         /* Find the primary SID, depending if we are a standalone
235          * server (what good is winbind in this case, but anyway...),
236          * or are in a domain as a member or a DC */
237         switch (lp_server_role(service->task->lp_ctx)) {
238         case ROLE_STANDALONE:
239                 primary_sid = secrets_get_domain_sid(service,
240                                                      service->task->event_ctx,
241                                                      service->task->lp_ctx,
242                                                      lp_netbios_name(service->task->lp_ctx), &errstring);
243                 if (!primary_sid) {
244                         char *message = talloc_asprintf(task, "Cannot start Winbind (standalone configuration): %s", errstring);
245                         task_server_terminate(task, message, true);
246                         return;
247                 }
248                 break;
249         case ROLE_DOMAIN_MEMBER:
250         case ROLE_DOMAIN_CONTROLLER:
251                 primary_sid = secrets_get_domain_sid(service,
252                                                      service->task->event_ctx,
253                                                      service->task->lp_ctx,
254                                                      lp_workgroup(service->task->lp_ctx), &errstring);
255                 if (!primary_sid) {
256                         char *message = talloc_asprintf(task, "Cannot start Winbind (domain configuration): %s", errstring);
257                         task_server_terminate(task, message, true);
258                         return;
259                 }
260                 break;
261         }
262         service->primary_sid = primary_sid;
263
264         service->idmap_ctx = idmap_init(service, task->event_ctx, task->lp_ctx);
265         if (service->idmap_ctx == NULL) {
266                 task_server_terminate(task, "Failed to load idmap database", true);
267                 return;
268         }
269
270         /* setup the unprivileged samba3 socket */
271         listen_socket = talloc(service, struct wbsrv_listen_socket);
272         if (!listen_socket) goto nomem;
273         listen_socket->socket_path      = talloc_asprintf(listen_socket, "%s/%s", 
274                                                           lp_winbindd_socket_directory(task->lp_ctx), 
275                                                           WINBINDD_SAMBA3_SOCKET);
276         if (!listen_socket->socket_path) goto nomem;
277         listen_socket->service          = service;
278         listen_socket->privileged       = false;
279         status = stream_setup_socket(task->event_ctx, task->lp_ctx, model_ops,
280                                      &wbsrv_ops, "unix",
281                                      listen_socket->socket_path, &port,
282                                      lp_socket_options(task->lp_ctx), 
283                                      listen_socket);
284         if (!NT_STATUS_IS_OK(status)) goto listen_failed;
285
286         /* setup the privileged samba3 socket */
287         listen_socket = talloc(service, struct wbsrv_listen_socket);
288         if (!listen_socket) goto nomem;
289         listen_socket->socket_path 
290                 = service->priv_socket_path 
291                 = talloc_asprintf(listen_socket, "%s/%s", 
292                                                           lp_winbindd_privileged_socket_directory(task->lp_ctx), 
293                                                           WINBINDD_SAMBA3_SOCKET);
294         if (!listen_socket->socket_path) goto nomem;
295         if (!listen_socket->socket_path) goto nomem;
296         listen_socket->service          = service;
297         listen_socket->privileged       = true;
298         status = stream_setup_socket(task->event_ctx, task->lp_ctx, model_ops,
299                                      &wbsrv_ops, "unix",
300                                      listen_socket->socket_path, &port,
301                                      lp_socket_options(task->lp_ctx), 
302                                      listen_socket);
303         if (!NT_STATUS_IS_OK(status)) goto listen_failed;
304
305         status = wbsrv_init_irpc(service);
306         if (!NT_STATUS_IS_OK(status)) goto irpc_failed;
307
308         return;
309
310 listen_failed:
311         DEBUG(0,("stream_setup_socket(path=%s) failed - %s\n",
312                  listen_socket->socket_path, nt_errstr(status)));
313         task_server_terminate(task, nt_errstr(status), true);
314         return;
315 irpc_failed:
316         DEBUG(0,("wbsrv_init_irpc() failed - %s\n",
317                  nt_errstr(status)));
318         task_server_terminate(task, nt_errstr(status), true);
319         return;
320 nomem:
321         task_server_terminate(task, nt_errstr(NT_STATUS_NO_MEMORY), true);
322         return;
323 }
324
325 /*
326   register ourselves as a available server
327 */
328 NTSTATUS server_service_winbind_init(void)
329 {
330         return register_server_service("winbind", winbind_task_init);
331 }