r6182: Jelmer, I think we need to initialize the switch_list, else we are
[Samba/gebeck_regimport.git] / source4 / winbind / wb_server.c
blob63d186f9dd90266370dd77251f5ecd25e9c92ae1
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 "system/filesys.h"
26 #include "dlinklist.h"
27 #include "lib/events/events.h"
28 #include "smbd/service_task.h"
29 #include "smbd/service_stream.h"
31 #define WINBINDD_DIR "/tmp/.winbindd/"
32 #define WINBINDD_ECHO_SOCKET WINBINDD_DIR"echo"
33 #define WINBINDD_ADDR_PREFIX "127.0.255."
34 #define WINBINDD_ECHO_ADDR WINBINDD_ADDR_PREFIX"1"
35 #define WINBINDD_ECHO_PORT 55555
38 state of an open winbind connection
40 struct wbserver_connection {
41 DATA_BLOB blob;
42 struct send_queue {
43 struct send_queue *next, *prev;
44 DATA_BLOB blob;
45 } *queue;
50 called when we get a new connection
52 static void winbind_accept(struct stream_connection *conn)
54 struct wbserver_connection *wbconn;
56 wbconn = talloc_zero(conn, struct wbserver_connection);
57 wbconn->blob = data_blob_talloc(wbconn, NULL, 1024);
59 conn->private = wbconn;
63 receive some data on a winbind connection
65 static void winbind_recv(struct stream_connection *conn, uint16_t flags)
67 struct wbserver_connection *wbconn = talloc_get_type(conn->private, struct wbserver_connection);
68 NTSTATUS status;
69 size_t nread;
70 struct send_queue *q;
72 status = socket_recv(conn->socket, wbconn->blob.data, wbconn->blob.length, &nread, 0);
73 if (NT_STATUS_IS_ERR(status)) {
74 DEBUG(10,("socket_recv: %s\n",nt_errstr(status)));
75 stream_terminate_connection(conn, "socket_recv: failed\n");
76 return;
79 /* just reflect the data back down the socket */
80 q = talloc(wbconn, struct send_queue);
81 if (q == NULL) {
82 stream_terminate_connection(conn, "winbind_recv: out of memory\n");
83 return;
86 q->blob = data_blob_talloc(q, wbconn->blob.data, nread);
87 if (q->blob.data == NULL) {
88 stream_terminate_connection(conn, "winbind_recv: out of memory\n");
89 return;
92 DLIST_ADD_END(wbconn->queue, q, struct send_queue *);
94 EVENT_FD_WRITEABLE(conn->event.fde);
98 called when we can write to a connection
100 static void winbind_send(struct stream_connection *conn, uint16_t flags)
102 struct wbserver_connection *wbconn = talloc_get_type(conn->private, struct wbserver_connection);
104 while (wbconn->queue) {
105 struct send_queue *q = wbconn->queue;
106 NTSTATUS status;
107 size_t sendlen;
109 status = socket_send(conn->socket, &q->blob, &sendlen, 0);
110 if (NT_STATUS_IS_ERR(status)) {
111 DEBUG(10,("socket_send() %s\n",nt_errstr(status)));
112 stream_terminate_connection(conn, "socket_send: failed\n");
113 return;
115 if (!NT_STATUS_IS_OK(status)) {
116 return;
119 q->blob.length -= sendlen;
120 q->blob.data += sendlen;
122 if (q->blob.length == 0) {
123 DLIST_REMOVE(wbconn->queue, q);
124 talloc_free(q);
128 EVENT_FD_NOT_WRITEABLE(conn->event.fde);
131 static const struct stream_server_ops winbind_stream_ops = {
132 .name = "winbind_echo",
133 .accept_connection = winbind_accept,
134 .recv_handler = winbind_recv,
135 .send_handler = winbind_send,
139 startup the winbind task
141 static void winbind_task_init(struct task_server *task)
143 uint16_t port = 1;
144 const struct model_ops *model_ops;
145 NTSTATUS status;
147 /* within the winbind task we want to be a single process, so
148 ask for the single process model ops and pass these to the
149 stream_setup_socket() call. */
150 model_ops = process_model_byname("single");
151 if (!model_ops) {
152 task_terminate(task, "Can't find 'single' process model_ops");
153 return;
156 /* Make sure the directory for NCALRPC exists */
157 if (!directory_exist(WINBINDD_DIR)) {
158 mkdir(WINBINDD_DIR, 0755);
161 status = stream_setup_socket(task->event_ctx, model_ops, &winbind_stream_ops,
162 "unix", WINBINDD_ECHO_SOCKET, &port, NULL);
163 if (!NT_STATUS_IS_OK(status)) {
164 DEBUG(0,("service_setup_stream_socket(path=%s) failed - %s\n",
165 WINBINDD_ECHO_SOCKET, nt_errstr(status)));
166 task_terminate(task, "winbind Failed to find to ECHO unix socket");
167 return;
170 port = WINBINDD_ECHO_PORT;
172 status = stream_setup_socket(task->event_ctx, model_ops, &winbind_stream_ops,
173 "ipv4", WINBINDD_ECHO_ADDR, &port, NULL);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0,("service_setup_stream_socket(address=%s,port=%u) failed - %s\n",
176 WINBINDD_ECHO_ADDR, port, nt_errstr(status)));
177 task_terminate(task, "winbind Failed to find to ECHO tcp socket");
178 return;
183 initialise the winbind server
185 static NTSTATUS winbind_init(struct event_context *event_ctx, const struct model_ops *model_ops)
187 return task_server_startup(event_ctx, model_ops, winbind_task_init);
191 register ourselves as a available server
193 NTSTATUS server_service_winbind_init(void)
195 return register_server_service("winbind", winbind_init);