2 Unix SMB/CIFS implementation.
3 process incoming packets - main loop
4 Copyright (C) Andrew Tridgell 2004-2005
5 Copyright (C) Stefan Metzmacher 2004-2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/service_task.h"
23 #include "smbd/service_stream.h"
24 #include "smbd/service.h"
25 #include "smb_server/smb_server.h"
26 #include "smb_server/service_smb_proto.h"
27 #include "lib/messaging/irpc.h"
28 #include "lib/stream/packet.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"
37 static NTSTATUS
smbsrv_recv_generic_request(void *private_data
, DATA_BLOB blob
)
40 struct smbsrv_connection
*smb_conn
= talloc_get_type(private_data
, struct smbsrv_connection
);
41 uint32_t protocol_version
;
43 /* see if its a special NBT packet */
44 if (CVAL(blob
.data
,0) != 0) {
45 status
= smbsrv_init_smb_connection(smb_conn
, smb_conn
->lp_ctx
);
46 NT_STATUS_NOT_OK_RETURN(status
);
47 packet_set_callback(smb_conn
->packet
, smbsrv_recv_smb_request
);
48 return smbsrv_recv_smb_request(smb_conn
, blob
);
51 if (blob
.length
< (NBT_HDR_SIZE
+ MIN_SMB_SIZE
)) {
52 DEBUG(2,("Invalid SMB packet length count %ld\n", (long)blob
.length
));
53 smbsrv_terminate_connection(smb_conn
, "Invalid SMB packet");
57 protocol_version
= IVAL(blob
.data
, NBT_HDR_SIZE
);
59 switch (protocol_version
) {
61 status
= smbsrv_init_smb_connection(smb_conn
, smb_conn
->lp_ctx
);
62 NT_STATUS_NOT_OK_RETURN(status
);
63 packet_set_callback(smb_conn
->packet
, smbsrv_recv_smb_request
);
64 return smbsrv_recv_smb_request(smb_conn
, blob
);
66 if (lpcfg_server_max_protocol(smb_conn
->lp_ctx
) < PROTOCOL_SMB2_02
) break;
67 status
= smbsrv_init_smb2_connection(smb_conn
);
68 NT_STATUS_NOT_OK_RETURN(status
);
69 packet_set_callback(smb_conn
->packet
, smbsrv_recv_smb2_request
);
70 return smbsrv_recv_smb2_request(smb_conn
, blob
);
73 DEBUG(2,("Invalid SMB packet: protocol prefix: 0x%08X\n", protocol_version
));
74 smbsrv_terminate_connection(smb_conn
, "NON-SMB packet");
79 close the socket and shutdown a server_context
81 void smbsrv_terminate_connection(struct smbsrv_connection
*smb_conn
, const char *reason
)
83 stream_terminate_connection(smb_conn
->connection
, reason
);
87 called when a SMB socket becomes readable
89 static void smbsrv_recv(struct stream_connection
*conn
, uint16_t flags
)
91 struct smbsrv_connection
*smb_conn
= talloc_get_type(conn
->private_data
,
92 struct smbsrv_connection
);
94 DEBUG(10,("smbsrv_recv\n"));
96 packet_recv(smb_conn
->packet
);
100 called when a SMB socket becomes writable
102 static void smbsrv_send(struct stream_connection
*conn
, uint16_t flags
)
104 struct smbsrv_connection
*smb_conn
= talloc_get_type(conn
->private_data
,
105 struct smbsrv_connection
);
106 packet_queue_run(smb_conn
->packet
);
110 handle socket recv errors
112 static void smbsrv_recv_error(void *private_data
, NTSTATUS status
)
114 struct smbsrv_connection
*smb_conn
= talloc_get_type(private_data
, struct smbsrv_connection
);
116 smbsrv_terminate_connection(smb_conn
, nt_errstr(status
));
120 initialise a server_context from a open socket and register a event handler
121 for reading from that socket
123 static void smbsrv_accept(struct stream_connection
*conn
)
125 struct smbsrv_connection
*smb_conn
;
127 DEBUG(5,("smbsrv_accept\n"));
129 smb_conn
= talloc_zero(conn
, struct smbsrv_connection
);
131 stream_terminate_connection(conn
, "out of memory");
135 smb_conn
->packet
= packet_init(smb_conn
);
136 if (!smb_conn
->packet
) {
137 smbsrv_terminate_connection(smb_conn
, "out of memory");
140 packet_set_private(smb_conn
->packet
, smb_conn
);
141 packet_set_socket(smb_conn
->packet
, conn
->socket
);
142 packet_set_callback(smb_conn
->packet
, smbsrv_recv_generic_request
);
143 packet_set_full_request(smb_conn
->packet
, packet_full_request_nbt
);
144 packet_set_error_handler(smb_conn
->packet
, smbsrv_recv_error
);
145 packet_set_event_context(smb_conn
->packet
, conn
->event
.ctx
);
146 packet_set_fde(smb_conn
->packet
, conn
->event
.fde
);
147 packet_set_serialise(smb_conn
->packet
);
148 packet_set_initial_read(smb_conn
->packet
, 4);
150 smb_conn
->lp_ctx
= conn
->lp_ctx
;
151 smb_conn
->connection
= conn
;
152 conn
->private_data
= smb_conn
;
154 smb_conn
->statistics
.connect_time
= timeval_current();
156 smbsrv_management_init(smb_conn
);
158 irpc_add_name(conn
->msg_ctx
, "smb_server");
160 if (!NT_STATUS_IS_OK(share_get_context_by_name(smb_conn
, lpcfg_share_backend(smb_conn
->lp_ctx
),
161 smb_conn
->connection
->event
.ctx
,
162 smb_conn
->lp_ctx
, &(smb_conn
->share_context
)))) {
163 smbsrv_terminate_connection(smb_conn
, "share_init failed!");
168 static const struct stream_server_ops smb_stream_ops
= {
170 .accept_connection
= smbsrv_accept
,
171 .recv_handler
= smbsrv_recv
,
172 .send_handler
= smbsrv_send
,
176 setup a listening socket on all the SMB ports for a particular address
178 _PUBLIC_ NTSTATUS
smbsrv_add_socket(TALLOC_CTX
*mem_ctx
,
179 struct tevent_context
*event_context
,
180 struct loadparm_context
*lp_ctx
,
181 const struct model_ops
*model_ops
,
184 const char **ports
= lpcfg_smb_ports(lp_ctx
);
188 for (i
=0;ports
[i
];i
++) {
189 uint16_t port
= atoi(ports
[i
]);
190 if (port
== 0) continue;
191 status
= stream_setup_socket(mem_ctx
, event_context
, lp_ctx
,
192 model_ops
, &smb_stream_ops
,
193 "ip", address
, &port
,
194 lpcfg_socket_options(lp_ctx
),
196 NT_STATUS_NOT_OK_RETURN(status
);