summaryrefslogtreecommitdiff
path: root/source4/smb_server/smb_samba3.c
blob: 86e67ec20da34100772c33e7166f65615ca296e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
   Unix SMB/CIFS implementation.

   process incoming connections and fork a samba3 in inetd mode

   Copyright (C) Stefan Metzmacher	2008

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "smbd/service.h"
#include "libcli/smb2/smb2.h"
#include "system/network.h"
#include "lib/socket/netif.h"
#include "param/param.h"
#include "dynconfig/dynconfig.h"
#include "smbd/process_model.h"

NTSTATUS server_service_samba3_smb_init(void);

/*
  initialise a server_context from a open socket and register a event handler
  for reading from that socket
*/
static void samba3_smb_accept(struct stream_connection *conn)
{
	int i;
	int fd = socket_get_fd(conn->socket);
	const char *prog;
	char *argv[2];
	char *reason;

	close(0);
	close(1);
	dup2(fd, 0);
	dup2(fd, 1);
	dup2(fd, 2);
	for (i=3;i<256;i++) {
		close(i);
	}

	prog = lpcfg_parm_string(conn->lp_ctx, NULL, "samba3", "smbd");

	if (prog == NULL) {
		argv[0] = talloc_asprintf(conn, "%s/%s", dyn_BINDIR, "smbd3");
	}
	else {
		argv[0] = talloc_strdup(conn, prog);
	}

	if (argv[0] == NULL) {
		stream_terminate_connection(conn, "out of memory");
		return;
	}
	argv[1] = NULL;

	execv(argv[0], argv);

	/*
	 * Should never get here
	 */
	reason = talloc_asprintf(conn, "Could not execute %s", argv[0]);
	if (reason == NULL) {
		stream_terminate_connection(conn, "out of memory");
		return;
	}
	stream_terminate_connection(conn, reason);
	talloc_free(reason);
}

static const struct stream_server_ops samba3_smb_stream_ops = {
	.name			= "samba3",
	.accept_connection	= samba3_smb_accept,
};

/*
  setup a listening socket on all the SMB ports for a particular address
*/
static NTSTATUS samba3_add_socket(struct task_server *task,
				  struct tevent_context *event_context,
				  struct loadparm_context *lp_ctx,
				  const struct model_ops *model_ops,
				  const char *address)
{
	const char **ports = lpcfg_smb_ports(lp_ctx);
	int i;
	NTSTATUS status;

	for (i=0;ports[i];i++) {
		uint16_t port = atoi(ports[i]);
		if (port == 0) continue;
		status = stream_setup_socket(task, event_context, lp_ctx,
					     model_ops, &samba3_smb_stream_ops,
					     "ip", address, &port,
					     lpcfg_socket_options(lp_ctx),
					     NULL);
		NT_STATUS_NOT_OK_RETURN(status);
	}

	return NT_STATUS_OK;
}


/*
  open the smb server sockets
*/
static void samba3_smb_task_init(struct task_server *task)
{
	NTSTATUS status;
	const struct model_ops *model_ops;

	model_ops = process_model_startup("standard");

	if (model_ops == NULL) {
		goto failed;
	}

	task_server_set_title(task, "task[samba3_smb]");

	if (lpcfg_interfaces(task->lp_ctx)
	    && lpcfg_bind_interfaces_only(task->lp_ctx)) {
		int num_interfaces;
		int i;
		struct interface *ifaces;

		load_interface_list(task, lpcfg_interfaces(task->lp_ctx), &ifaces);

		num_interfaces = iface_list_count(ifaces);

		/* We have been given an interfaces line, and been
		   told to only bind to those interfaces. Create a
		   socket per interface and bind to only these.
		*/
		for(i = 0; i < num_interfaces; i++) {
			const char *address = iface_list_n_ip(ifaces, i);
			status = samba3_add_socket(task,
						   task->event_ctx,
						   task->lp_ctx,
						   model_ops, address);
			if (!NT_STATUS_IS_OK(status)) goto failed;
		}
	} else {
		/* Just bind to lpcfg_socket_address() (usually 0.0.0.0) */
		status = samba3_add_socket(task,
					   task->event_ctx, task->lp_ctx,
					   model_ops,
					   lpcfg_socket_address(task->lp_ctx));
		if (!NT_STATUS_IS_OK(status)) goto failed;
	}

	return;
failed:
	task_server_terminate(task, "Failed to startup samba3 smb task", true);
}

/* called at smbd startup - register ourselves as a server service */
NTSTATUS server_service_samba3_smb_init(void)
{
	return register_server_service("samba3_smb", samba3_smb_task_init);
}