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