2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2003
7 Copyright (C) Stefan (metze) Metzmacher 2004
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 2 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 struct server_context *server_service_startup(const char *model)
29 const char **server_services = lp_server_services();
31 struct server_context *srv_ctx;
32 const struct model_ops *model_ops;
34 if (!server_services) {
35 DEBUG(0,("process_model_startup: no endpoint servers configured\n"));
39 model_ops = process_model_startup(model);
41 DEBUG(0,("process_model_startup('%s') failed\n", model));
45 mem_ctx = talloc_init("server_context");
47 DEBUG(0,("talloc_init(server_context) failed\n"));
51 srv_ctx = talloc_p(mem_ctx, struct server_context);
53 DEBUG(0,("talloc_p(mem_ctx, struct server_context) failed\n"));
57 ZERO_STRUCTP(srv_ctx);
58 srv_ctx->mem_ctx = mem_ctx;
60 srv_ctx->events = event_context_init();
61 if (!srv_ctx->events) {
62 DEBUG(0,("event_context_init() failed\n"));
67 for (i=0;server_services[i];i++) {
69 const struct server_service_ops *service_ops;
70 struct server_service *service;
72 service_ops = server_service_byname(server_services[i]);
74 DEBUG(0,("process_model_startup: failed to find server service = '%s'\n", server_services[i]));
78 mem_ctx2 = talloc_init("server_service");
80 service = talloc_p(mem_ctx2, struct server_service);
82 DEBUG(0,("talloc_p(mem_ctx, struct server_service) failed\n"));
86 ZERO_STRUCTP(service);
87 service->mem_ctx = mem_ctx2;
88 service->ops = service_ops;
89 service->model_ops = model_ops;
90 service->srv_ctx = srv_ctx;
92 /* TODO: service_init() should return a result */
93 service->ops->service_init(service, model_ops);
100 setup a listen stream socket
101 if you pass *port == 0, then a port > 1024 is used
103 struct server_socket *service_setup_socket(struct server_service *service,
104 const struct model_ops *model_ops,
105 const char *sock_addr,
109 struct server_socket *srv_sock;
110 struct socket_context *socket_ctx;
115 for (i=SERVER_TCP_LOW_PORT;i<= SERVER_TCP_HIGH_PORT;i++) {
116 status = socket_create("ipv4", SOCKET_TYPE_STREAM, &socket_ctx, 0);
117 if (NT_STATUS_IS_OK(status)) {
123 status = socket_create("ipv4", SOCKET_TYPE_STREAM, &socket_ctx, 0);
126 if (!NT_STATUS_IS_OK(status)) {
127 DEBUG(0,("Failed to open socket on %s:%u - %s\n",
128 sock_addr, *port, nt_errstr(status)));
132 /* ready to listen */
133 status = socket_set_option(socket_ctx, "SO_KEEPALIVE SO_REUSEADDR=1", NULL);
134 if (!NT_STATUS_IS_OK(status)) {
135 DEBUG(0,("socket_set_option(socket_ctx, SO_KEEPALIVE, NULL): %s\n",
137 socket_destroy(socket_ctx);
140 status = socket_set_option(socket_ctx, lp_socket_options(), NULL);
141 if (!NT_STATUS_IS_OK(status)) {
142 DEBUG(0,("socket_set_option(socket_ctx, lp_socket_options(), NULL): %s\n",
144 socket_destroy(socket_ctx);
148 /* TODO: set socket ACL's here when they're implemented */
150 status = socket_listen(socket_ctx, sock_addr, *port, SERVER_LISTEN_BACKLOG, 0);
151 if (!NT_STATUS_IS_OK(status)) {
152 DEBUG(0,("Failed to listen on %s:%u - %s\n",
153 sock_addr, *port, nt_errstr(status)));
154 socket_destroy(socket_ctx);
158 srv_sock = talloc_p(service, struct server_socket);
160 DEBUG(0,("talloc_p(mem_ctx, struct server_socket) failed\n"));
161 socket_destroy(socket_ctx);
165 /* we are only interested in read events on the listen socket */
166 fde.fd = socket_get_fd(socket_ctx);
167 fde.flags = EVENT_FD_READ;
168 fde.private = srv_sock;
169 fde.handler = model_ops->accept_connection;
171 ZERO_STRUCTP(srv_sock);
172 srv_sock->mem_ctx = srv_sock;
173 srv_sock->service = service;
174 srv_sock->socket = socket_ctx;
175 srv_sock->event.ctx = service->srv_ctx->events;
176 srv_sock->event.fde = event_add_fd(srv_sock->event.ctx, &fde);
177 if (!srv_sock->event.fde) {
178 DEBUG(0,("event_add_fd(srv_sock->event.ctx, &fde) failed\n"));
179 socket_destroy(socket_ctx);
183 DLIST_ADD(service->socket_list, srv_sock);
188 struct server_connection *server_setup_connection(struct event_context *ev,
189 struct server_socket *server_socket,
190 struct socket_context *sock,
194 struct timed_event idle;
195 struct server_connection *srv_conn;
197 srv_conn = talloc_p(server_socket, struct server_connection);
199 DEBUG(0,("talloc_p(mem_ctx, struct server_service_connection) failed\n"));
203 ZERO_STRUCTP(srv_conn);
204 srv_conn->mem_ctx = srv_conn;
206 fde.private = srv_conn;
207 fde.fd = socket_get_fd(sock);
208 fde.flags = EVENT_FD_READ;
209 fde.handler = server_io_handler;
211 idle.private = srv_conn;
212 idle.next_event = t + SERVER_DEFAULT_IDLE_TIME;
213 idle.handler = server_idle_handler;
215 srv_conn->event.ctx = ev;
216 srv_conn->event.fde = &fde;
217 srv_conn->event.idle = &idle;
218 srv_conn->event.idle_time = SERVER_DEFAULT_IDLE_TIME;
220 srv_conn->server_socket = server_socket;
221 srv_conn->service = server_socket->service;
222 srv_conn->socket = sock;
224 /* create a smb server context and add it to out event
226 server_socket->service->ops->accept_connection(srv_conn);
228 /* accpect_connection() of the service may changed idle.next_event */
229 srv_conn->event.fde = event_add_fd(ev,&fde);
230 srv_conn->event.idle = event_add_timed(ev,&idle);
232 if (!socket_check_access(sock, "smbd", lp_hostsallow(-1), lp_hostsdeny(-1))) {
233 server_terminate_connection(srv_conn, "denied by access rules");
241 close the socket and shutdown a server_context
243 void server_terminate_connection(struct server_connection *srv_conn, const char *reason)
245 DEBUG(2,("server_terminate_connection\n"));
246 srv_conn->service->model_ops->terminate_connection(srv_conn, reason);
249 void server_destroy_connection(struct server_connection *srv_conn)
251 socket_destroy(srv_conn->socket);
253 event_remove_fd(srv_conn->event.ctx, srv_conn->event.fde);
254 srv_conn->event.fde = NULL;
255 event_remove_timed(srv_conn->event.ctx, srv_conn->event.idle);
256 srv_conn->event.idle = NULL;
258 talloc_destroy(srv_conn->mem_ctx);
261 void server_io_handler(struct event_context *ev, struct fd_event *fde, time_t t, uint16_t flags)
263 struct server_connection *conn = fde->private;
265 conn->event.idle->next_event = t + conn->event.idle_time;
267 if (flags & EVENT_FD_WRITE) {
268 conn->service->ops->send_handler(conn, t, flags);
272 if (flags & EVENT_FD_READ) {
273 conn->service->ops->recv_handler(conn, t, flags);
278 void server_idle_handler(struct event_context *ev, struct timed_event *idle, time_t t)
280 struct server_connection *conn = idle->private;
282 conn->event.idle->next_event = t + conn->event.idle_time;
284 conn->service->ops->idle_handler(conn,t);
287 return the operations structure for a named backend of the specified type
289 const struct server_service_ops *server_service_byname(const char *name)
291 if (strcmp("smb",name)==0) {
292 return smbsrv_get_ops();
294 if (strcmp("rpc",name)==0) {
295 return dcesrv_get_ops();
297 if (strcmp("ldap",name)==0) {
298 return ldapsrv_get_ops();
303 static NTSTATUS register_server_service_ops(const void *_ops)
305 return NT_STATUS_NOT_IMPLEMENTED;
309 initialise the SERVER SERVICE subsystem
311 BOOL server_service_init(void)
315 status = register_subsystem("service", register_server_service_ops);
316 if (!NT_STATUS_IS_OK(status)) {
320 /* FIXME: Perhaps panic if a basic endpoint server, such as EPMAPER, fails to initialise? */
321 static_init_server_service;
323 DEBUG(3,("SERVER SERVICE subsystem version %d initialised\n", SERVER_SERVICE_VERSION));