summaryrefslogtreecommitdiff
path: root/source4/smb_server/smb_server.c
blob: 3385e88d4a54c5dc137e0bb7cd9ed83c75b032ab (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* 
   Unix SMB/CIFS implementation.
   process incoming packets - main loop
   Copyright (C) Andrew Tridgell	2004-2005
   Copyright (C) Stefan Metzmacher	2004-2005
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "lib/events/events.h"
#include "system/time.h"
#include "dlinklist.h"
#include "smbd/service_stream.h"
#include "smb_server/smb_server.h"
#include "lib/messaging/irpc.h"
#include "lib/stream/packet.h"

static NTSTATUS smbsrv_recv_generic_request(void *private, DATA_BLOB blob)
{
	NTSTATUS status;
	struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
	uint32_t protocol_version;

	/* see if its a special NBT packet */
	if (CVAL(blob.data,0) != 0) {
		status = smbsrv_init_smb_connection(smb_conn);
		NT_STATUS_NOT_OK_RETURN(status);
		packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
		return smbsrv_recv_smb_request(smb_conn, blob);
	}

	if (blob.length < (NBT_HDR_SIZE + MIN_SMB_SIZE)) {
		DEBUG(2,("Invalid SMB packet length count %d\n", blob.length));
		smbsrv_terminate_connection(smb_conn, "Invalid SMB packet");
		return NT_STATUS_OK;
	}

	protocol_version = IVAL(blob.data, NBT_HDR_SIZE);

	switch (protocol_version) {
	case SMB_MAGIC:
		status = smbsrv_init_smb_connection(smb_conn);
		NT_STATUS_NOT_OK_RETURN(status);
		packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request);
		return smbsrv_recv_smb_request(smb_conn, blob);
	}

	DEBUG(2,("Invalid SMB packet: protocl prefix: 0x%08X\n", protocol_version));
	smbsrv_terminate_connection(smb_conn, "NON-SMB packet");
	return NT_STATUS_OK;
}

/*
  close the socket and shutdown a server_context
*/
void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason)
{
	smb_conn->terminate = reason;
}

/*
  called when a SMB socket becomes readable
*/
static void smbsrv_recv(struct stream_connection *conn, uint16_t flags)
{
	struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, struct smbsrv_connection);

	DEBUG(10,("smbsrv_recv\n"));

	packet_recv(smb_conn->packet);
	if (smb_conn->terminate) {
		talloc_free(conn->event.fde);
		conn->event.fde = NULL;
		stream_terminate_connection(smb_conn->connection, smb_conn->terminate);
		return;
	}

	/* free up temporary memory */
	lp_talloc_free();
}

/*
  called when a SMB socket becomes writable
*/
static void smbsrv_send(struct stream_connection *conn, uint16_t flags)
{
	struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, 
							     struct smbsrv_connection);
	packet_queue_run(smb_conn->packet);
}


/*
  handle socket recv errors
*/
static void smbsrv_recv_error(void *private, NTSTATUS status)
{
	struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection);
	
	smbsrv_terminate_connection(smb_conn, nt_errstr(status));
}

/*
  initialise a server_context from a open socket and register a event handler
  for reading from that socket
*/
static void smbsrv_accept(struct stream_connection *conn)
{
	struct smbsrv_connection *smb_conn;

	DEBUG(5,("smbsrv_accept\n"));

	smb_conn = talloc_zero(conn, struct smbsrv_connection);
	if (!smb_conn) return;

	smb_conn->packet = packet_init(smb_conn);
	if (smb_conn->packet == NULL) {
		stream_terminate_connection(conn, "out of memory");
		return;
	}
	packet_set_private(smb_conn->packet, smb_conn);
	packet_set_socket(smb_conn->packet, conn->socket);
	packet_set_callback(smb_conn->packet, smbsrv_recv_generic_request);
	packet_set_full_request(smb_conn->packet, packet_full_request_nbt);
	packet_set_error_handler(smb_conn->packet, smbsrv_recv_error);
	packet_set_event_context(smb_conn->packet, conn->event.ctx);
	packet_set_fde(smb_conn->packet, conn->event.fde);
	packet_set_serialise(smb_conn->packet);

	smb_conn->connection = conn;
	conn->private = smb_conn;

	irpc_add_name(conn->msg_ctx, "smb_server");

	smbsrv_management_init(smb_conn);
}

static const struct stream_server_ops smb_stream_ops = {
	.name			= "smb",
	.accept_connection	= smbsrv_accept,
	.recv_handler		= smbsrv_recv,
	.send_handler		= smbsrv_send,
};

/*
  setup a listening socket on all the SMB ports for a particular address
*/
static NTSTATUS smb_add_socket(struct event_context *event_context,
			       const struct model_ops *model_ops,
			       const char *address)
{
	const char **ports = lp_smb_ports();
	int i;
	NTSTATUS status;

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

	return NT_STATUS_OK;
}

/*
  called on startup of the smb server service It's job is to start
  listening on all configured SMB server sockets
*/
static NTSTATUS smbsrv_init(struct event_context *event_context, const struct model_ops *model_ops)
{	
	NTSTATUS status;

	if (lp_interfaces() && lp_bind_interfaces_only()) {
		int num_interfaces = iface_count();
		int i;

		/* 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_n_ip(i);
			status = smb_add_socket(event_context, model_ops, address);
			NT_STATUS_NOT_OK_RETURN(status);
		}
	} else {
		/* Just bind to lp_socket_address() (usually 0.0.0.0) */
		status = smb_add_socket(event_context, model_ops, lp_socket_address());
		NT_STATUS_NOT_OK_RETURN(status);
	}

	return NT_STATUS_OK;
}

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