r13244: Allow control of the location of the Samba3-compatible winbindd pipe
[jelmer/samba4-debian.git] / source / winbind / wb_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Main winbindd server routines
4
5    Copyright (C) Stefan Metzmacher      2005
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 "lib/socket/socket.h"
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "nsswitch/winbind_nss_config.h"
30 #include "winbind/wb_server.h"
31 #include "lib/stream/packet.h"
32
33 void wbsrv_terminate_connection(struct wbsrv_connection *wbconn, const char *reason)
34 {
35         stream_terminate_connection(wbconn->conn, reason);
36 }
37
38 /*
39   called on a tcp recv error
40 */
41 static void wbsrv_recv_error(void *private, NTSTATUS status)
42 {
43         struct wbsrv_connection *wbconn = talloc_get_type(private, struct wbsrv_connection);
44         wbsrv_terminate_connection(wbconn, nt_errstr(status));
45 }
46
47 static void wbsrv_accept(struct stream_connection *conn)
48 {
49         struct wbsrv_listen_socket *listen_socket = talloc_get_type(conn->private, 
50                                                                     struct wbsrv_listen_socket);
51         struct wbsrv_connection *wbconn;
52
53         wbconn = talloc_zero(conn, struct wbsrv_connection);
54         if (!wbconn) {
55                 stream_terminate_connection(conn, "wbsrv_accept: out of memory");
56                 return;
57         }
58         wbconn->conn          = conn;
59         wbconn->listen_socket = listen_socket;
60         conn->private         = wbconn;
61
62         wbconn->packet = packet_init(wbconn);
63         if (wbconn->packet == NULL) {
64                 wbsrv_terminate_connection(wbconn, "wbsrv_accept: out of memory");
65                 return;
66         }
67         packet_set_private(wbconn->packet, wbconn);
68         packet_set_socket(wbconn->packet, conn->socket);
69         packet_set_callback(wbconn->packet, wbsrv_samba3_process);
70         packet_set_full_request(wbconn->packet, wbsrv_samba3_packet_full_request);
71         packet_set_error_handler(wbconn->packet, wbsrv_recv_error);
72         packet_set_event_context(wbconn->packet, conn->event.ctx);
73         packet_set_fde(wbconn->packet, conn->event.fde);
74         packet_set_serialise(wbconn->packet);
75 }
76
77 /*
78   receive some data on a winbind connection
79 */
80 static void wbsrv_recv(struct stream_connection *conn, uint16_t flags)
81 {
82         struct wbsrv_connection *wbconn = talloc_get_type(conn->private, 
83                                                           struct wbsrv_connection);
84         packet_recv(wbconn->packet);
85
86 }
87
88 /*
89   called when we can write to a connection
90 */
91 static void wbsrv_send(struct stream_connection *conn, uint16_t flags)
92 {
93         struct wbsrv_connection *wbconn = talloc_get_type(conn->private, 
94                                                           struct wbsrv_connection);
95         packet_queue_run(wbconn->packet);
96 }
97
98 static const struct stream_server_ops wbsrv_ops = {
99         .name                   = "winbind samba3 protocol",
100         .accept_connection      = wbsrv_accept,
101         .recv_handler           = wbsrv_recv,
102         .send_handler           = wbsrv_send
103 };
104
105 /*
106   startup the winbind task
107 */
108 static void winbind_task_init(struct task_server *task)
109 {
110         uint16_t port = 1;
111         const struct model_ops *model_ops;
112         NTSTATUS status;
113         struct wbsrv_service *service;
114         struct wbsrv_listen_socket *listen_socket;
115
116         /* within the winbind task we want to be a single process, so
117            ask for the single process model ops and pass these to the
118            stream_setup_socket() call. */
119         model_ops = process_model_byname("single");
120         if (!model_ops) {
121                 task_server_terminate(task,
122                                       "Can't find 'single' process model_ops");
123                 return;
124         }
125
126         /* Make sure the directory for the Samba3 socket exists, and is of the correct permissions */
127         if (!directory_create_or_exist(lp_winbindd_socket_directory(), geteuid(), 0755)) {
128                 task_server_terminate(task,
129                                       "Cannot create winbindd pipe directory");
130                 return;
131         }
132
133         service = talloc_zero(task, struct wbsrv_service);
134         if (!service) goto nomem;
135         service->task   = task;
136
137         service->primary_sid = secrets_get_domain_sid(service,
138                                                       lp_workgroup());
139         if (service->primary_sid == NULL) {
140                 task_server_terminate(
141                         task, nt_errstr(NT_STATUS_CANT_ACCESS_DOMAIN_INFO));
142                 return;
143         }
144
145         /* setup the unprivileged samba3 socket */
146         listen_socket = talloc(service, struct wbsrv_listen_socket);
147         if (!listen_socket) goto nomem;
148         listen_socket->socket_path      = talloc_asprintf(listen_socket, "%s/%s", 
149                                                           lp_winbindd_socket_directory(), 
150                                                           WINBINDD_SAMBA3_SOCKET);
151         if (!listen_socket->socket_path) goto nomem;
152         listen_socket->service          = service;
153         listen_socket->privileged       = False;
154         status = stream_setup_socket(task->event_ctx, model_ops,
155                                      &wbsrv_ops, "unix",
156                                      listen_socket->socket_path, &port,
157                                      listen_socket);
158         if (!NT_STATUS_IS_OK(status)) goto listen_failed;
159
160         /* setup the privileged samba3 socket */
161         listen_socket = talloc(service, struct wbsrv_listen_socket);
162         if (!listen_socket) goto nomem;
163         listen_socket->socket_path      =
164                 smbd_tmp_path(listen_socket,
165                               WINBINDD_SAMBA3_PRIVILEGED_SOCKET);
166         if (!listen_socket->socket_path) goto nomem;
167         listen_socket->service          = service;
168         listen_socket->privileged       = True;
169         status = stream_setup_socket(task->event_ctx, model_ops,
170                                      &wbsrv_ops, "unix",
171                                      listen_socket->socket_path, &port,
172                                      listen_socket);
173         if (!NT_STATUS_IS_OK(status)) goto listen_failed;
174
175         return;
176
177 listen_failed:
178         DEBUG(0,("stream_setup_socket(path=%s) failed - %s\n",
179                  listen_socket->socket_path, nt_errstr(status)));
180         task_server_terminate(task, nt_errstr(status));
181         return;
182 nomem:
183         task_server_terminate(task, nt_errstr(NT_STATUS_NO_MEMORY));
184         return;
185 }
186
187 /*
188   initialise the winbind server
189  */
190 static NTSTATUS winbind_init(struct event_context *event_ctx,
191                              const struct model_ops *model_ops)
192 {
193         return task_server_startup(event_ctx, model_ops, winbind_task_init);
194 }
195
196 /*
197   register ourselves as a available server
198 */
199 NTSTATUS server_service_winbind_init(void)
200 {
201         return register_server_service("winbind", winbind_init);
202 }