r5302: fixed a compilation problem on solaris caused by the recent include
[metze/samba/wb-ndr.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 "system/filesys.h"
25 #include "dlinklist.h"
26 #include "lib/events/events.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29
30 #define WINBINDD_DIR "/tmp/.winbindd/"
31 #define WINBINDD_ECHO_SOCKET  WINBINDD_DIR"echo"
32 #define WINBINDD_ADDR_PREFIX "127.0.255."
33 #define WINBINDD_ECHO_ADDR WINBINDD_ADDR_PREFIX"1"
34 #define WINBINDD_ECHO_PORT 55555
35
36 /*
37   state of an open winbind connection
38 */
39 struct wbserver_connection {
40         DATA_BLOB blob;
41         struct send_queue {
42                 struct send_queue *next, *prev;
43                 DATA_BLOB blob;
44         } *queue;
45 };
46
47
48 /*
49   called when we get a new connection
50 */
51 static void winbind_accept(struct stream_connection *conn)
52 {
53         struct wbserver_connection *wbconn;
54
55         wbconn = talloc_zero(conn, struct wbserver_connection);
56         wbconn->blob = data_blob_talloc(wbconn, NULL, 1024);
57         
58         conn->private = wbconn;
59 }
60
61 /*
62   receive some data on a winbind connection
63 */
64 static void winbind_recv(struct stream_connection *conn, uint16_t flags)
65 {
66         struct wbserver_connection *wbconn = talloc_get_type(conn->private, struct wbserver_connection);
67         NTSTATUS status;
68         size_t nread;
69         struct send_queue *q;
70
71         status = socket_recv(conn->socket, wbconn->blob.data, wbconn->blob.length, &nread, 0);
72         if (NT_STATUS_IS_ERR(status)) {
73                 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
74                 stream_terminate_connection(conn, "socket_recv: failed\n");
75                 return;
76         }
77
78         /* just reflect the data back down the socket */
79         q = talloc(wbconn, struct send_queue);
80         if (q == NULL) {
81                 stream_terminate_connection(conn, "winbind_recv: out of memory\n");
82                 return;
83         }
84
85         q->blob = data_blob_talloc(q, wbconn->blob.data, nread);
86         if (q->blob.data == NULL) {
87                 stream_terminate_connection(conn, "winbind_recv: out of memory\n");
88                 return;
89         }
90
91         DLIST_ADD_END(wbconn->queue, q, struct send_queue *);
92
93         EVENT_FD_WRITEABLE(conn->event.fde);
94 }
95
96 /*
97   called when we can write to a connection
98 */
99 static void winbind_send(struct stream_connection *conn, uint16_t flags)
100 {
101         struct wbserver_connection *wbconn = talloc_get_type(conn->private, struct wbserver_connection);
102
103         while (wbconn->queue) {
104                 struct send_queue *q = wbconn->queue;
105                 NTSTATUS status;
106                 size_t sendlen;
107
108                 status = socket_send(conn->socket, &q->blob, &sendlen, 0);
109                 if (NT_STATUS_IS_ERR(status)) {
110                         DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
111                         stream_terminate_connection(conn, "socket_send: failed\n");
112                         return;
113                 }
114                 if (!NT_STATUS_IS_OK(status)) {
115                         return;
116                 }
117
118                 q->blob.length -= sendlen;
119                 q->blob.data   += sendlen;
120
121                 if (q->blob.length == 0) {
122                         DLIST_REMOVE(wbconn->queue, q);
123                         talloc_free(q);
124                 }
125         }
126
127         EVENT_FD_NOT_WRITEABLE(conn->event.fde);
128 }
129
130 static const struct stream_server_ops winbind_stream_ops = {
131         .name                   = "winbind_echo",
132         .accept_connection      = winbind_accept,
133         .recv_handler           = winbind_recv,
134         .send_handler           = winbind_send,
135 };
136
137 /*
138   startup the winbind task
139 */
140 static void winbind_task_init(struct task_server *task)
141 {
142         uint16_t port = 1;
143         const struct model_ops *model_ops;
144         NTSTATUS status;
145
146         /* within the winbind task we want to be a single process, so
147            ask for the single process model ops and pass these to the
148            stream_setup_socket() call. */
149         model_ops = process_model_byname("single");
150         if (!model_ops) {
151                 task_terminate(task, "Can't find 'single' process model_ops");
152                 return;
153         }
154
155         /* Make sure the directory for NCALRPC exists */
156         if (!directory_exist(WINBINDD_DIR)) {
157                 mkdir(WINBINDD_DIR, 0755);
158         }
159
160         status = stream_setup_socket(task->event_ctx, model_ops, &winbind_stream_ops, 
161                                      "unix", WINBINDD_ECHO_SOCKET, &port, NULL);
162         if (!NT_STATUS_IS_OK(status)) {
163                 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
164                          WINBINDD_ECHO_SOCKET, nt_errstr(status)));
165                 task_terminate(task, "winbind Failed to find to ECHO unix socket");
166                 return;
167         }
168
169         port = WINBINDD_ECHO_PORT;
170
171         status = stream_setup_socket(task->event_ctx, model_ops, &winbind_stream_ops,
172                                      "ipv4", WINBINDD_ECHO_ADDR, &port, NULL);
173         if (!NT_STATUS_IS_OK(status)) {
174                 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
175                          WINBINDD_ECHO_ADDR, port, nt_errstr(status)));
176                 task_terminate(task, "winbind Failed to find to ECHO tcp socket");
177                 return;
178         }
179 }
180
181 /*
182   initialise the winbind server
183  */
184 static NTSTATUS winbind_init(struct event_context *event_ctx, const struct model_ops *model_ops)
185 {
186         return task_server_startup(event_ctx, model_ops, winbind_task_init);
187 }
188
189 /*
190   register ourselves as a available server
191 */
192 NTSTATUS server_service_winbind_init(void)
193 {
194         return register_server_service("winbind", winbind_init);
195 }