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