7b3229892f79002ff3ca82f07de44f87f2150c9f
[sfrench/samba-autobuild/.git] / source4 / smb_server / smb_samba3.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    process incoming connections and fork a samba3 in inetd mode
5
6    Copyright (C) Stefan Metzmacher      2008
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 "smbd/service_task.h"
24 #include "smbd/service_stream.h"
25 #include "smbd/service.h"
26 #include "lib/messaging/irpc.h"
27 #include "lib/stream/packet.h"
28 #include "lib/socket/socket.h"
29 #include "libcli/smb2/smb2.h"
30 #include "smb_server/smb2/smb2_server.h"
31 #include "system/network.h"
32 #include "lib/socket/netif.h"
33 #include "param/share.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "param/param.h"
36 #include "dynconfig/dynconfig.h"
37 #include "smbd/process_model.h"
38
39 /*
40   initialise a server_context from a open socket and register a event handler
41   for reading from that socket
42 */
43 static void samba3_smb_accept(struct stream_connection *conn)
44 {
45         int i;
46         int fd = socket_get_fd(conn->socket);
47         const char *prog;
48         char *argv[2];
49         char *reason;
50
51         close(0);
52         close(1);
53         dup2(fd, 0);
54         dup2(fd, 1);
55         for (i=2;i<256;i++) {
56                 close(i);
57         }
58
59         prog = lp_parm_string(conn->lp_ctx, NULL, "samba3", "smbd");
60
61         if (prog == NULL) {
62                 argv[0] = talloc_asprintf(conn, "%s/%s", dyn_BINDIR, "smbd3");
63         }
64         else {
65                 argv[0] = talloc_strdup(conn, prog);
66         }
67
68         if (argv[0] == NULL) {
69                 stream_terminate_connection(conn, "out of memory");
70                 return;
71         }
72         argv[1] = NULL;
73
74         execve(argv[0], argv, environ);
75
76         /*
77          * Should never get here
78          */
79         reason = talloc_asprintf(conn, "Could not execute %s", argv[0]);
80         if (reason == NULL) {
81                 stream_terminate_connection(conn, "out of memory");
82                 return;
83         }
84         stream_terminate_connection(conn, reason);
85         talloc_free(reason);
86 }
87
88 static const struct stream_server_ops samba3_smb_stream_ops = {
89         .name                   = "samba3",
90         .accept_connection      = samba3_smb_accept,
91 };
92
93 /*
94   setup a listening socket on all the SMB ports for a particular address
95 */
96 static NTSTATUS samba3_add_socket(struct event_context *event_context,
97                                   struct loadparm_context *lp_ctx,
98                                   const struct model_ops *model_ops,
99                                   const char *address)
100 {
101         const char **ports = lp_smb_ports(lp_ctx);
102         int i;
103         NTSTATUS status;
104
105         for (i=0;ports[i];i++) {
106                 uint16_t port = atoi(ports[i]);
107                 if (port == 0) continue;
108                 status = stream_setup_socket(event_context, lp_ctx,
109                                              model_ops, &samba3_smb_stream_ops,
110                                              "ip", address, &port,
111                                              lp_socket_options(lp_ctx),
112                                              NULL);
113                 NT_STATUS_NOT_OK_RETURN(status);
114         }
115
116         return NT_STATUS_OK;
117 }
118
119
120 /*
121   open the smb server sockets
122 */
123 static void samba3_smb_task_init(struct task_server *task)
124 {
125         NTSTATUS status;
126         const struct model_ops *model_ops;
127
128         model_ops = process_model_startup(task->event_ctx, "standard");
129
130         if (model_ops == NULL) {
131                 goto failed;
132         }
133
134         task_server_set_title(task, "task[samba3_smb]");
135
136         if (lp_interfaces(task->lp_ctx)
137             && lp_bind_interfaces_only(task->lp_ctx)) {
138                 int num_interfaces;
139                 int i;
140                 struct interface *ifaces;
141
142                 load_interfaces(task, lp_interfaces(task->lp_ctx), &ifaces);
143
144                 num_interfaces = iface_count(ifaces);
145
146                 /* We have been given an interfaces line, and been
147                    told to only bind to those interfaces. Create a
148                    socket per interface and bind to only these.
149                 */
150                 for(i = 0; i < num_interfaces; i++) {
151                         const char *address = iface_n_ip(ifaces, i);
152                         status = samba3_add_socket(task->event_ctx,
153                                                    task->lp_ctx,
154                                                    model_ops, address);
155                         if (!NT_STATUS_IS_OK(status)) goto failed;
156                 }
157         } else {
158                 /* Just bind to lp_socket_address() (usually 0.0.0.0) */
159                 status = samba3_add_socket(task->event_ctx, task->lp_ctx,
160                                            model_ops,
161                                            lp_socket_address(task->lp_ctx));
162                 if (!NT_STATUS_IS_OK(status)) goto failed;
163         }
164
165         return;
166 failed:
167         task_server_terminate(task, "Failed to startup samba3 smb task");
168 }
169
170 /* called at smbd startup - register ourselves as a server service */
171 NTSTATUS server_service_samba3_smb_init(void)
172 {
173         return register_server_service("samba3_smb", samba3_smb_task_init);
174 }