Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : process incoming packets - main loop
4 : Copyright (C) Andrew Tridgell 1992-1998
5 : Copyright (C) Volker Lendecke 2005-2007
6 :
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.
11 :
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.
16 :
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/>.
19 : */
20 :
21 : #include "includes.h"
22 : #include "../lib/tsocket/tsocket.h"
23 : #include "system/filesys.h"
24 : #include "smbd/smbd.h"
25 : #include "smbd/globals.h"
26 : #include "smbd/smbXsrv_open.h"
27 : #include "librpc/gen_ndr/netlogon.h"
28 : #include "../lib/async_req/async_sock.h"
29 : #include "ctdbd_conn.h"
30 : #include "../lib/util/select.h"
31 : #include "printing/queue_process.h"
32 : #include "system/select.h"
33 : #include "passdb.h"
34 : #include "auth.h"
35 : #include "messages.h"
36 : #include "lib/messages_ctdb.h"
37 : #include "smbprofile.h"
38 : #include "rpc_server/spoolss/srv_spoolss_nt.h"
39 : #include "../lib/util/tevent_ntstatus.h"
40 : #include "../libcli/security/dom_sid.h"
41 : #include "../libcli/security/security_token.h"
42 : #include "lib/id_cache.h"
43 : #include "lib/util/sys_rw_data.h"
44 : #include "system/threads.h"
45 : #include "lib/pthreadpool/pthreadpool_tevent.h"
46 : #include "util_event.h"
47 : #include "libcli/smb/smbXcli_base.h"
48 : #include "lib/util/time_basic.h"
49 : #include "source3/lib/substitute.h"
50 :
51 : /* Internal message queue for deferred opens. */
52 : struct pending_message_list {
53 : struct pending_message_list *next, *prev;
54 : struct timeval request_time; /* When was this first issued? */
55 : struct smbd_server_connection *sconn;
56 : struct smbXsrv_connection *xconn;
57 : struct tevent_timer *te;
58 : struct smb_perfcount_data pcd;
59 : uint32_t seqnum;
60 : bool encrypted;
61 : bool processed;
62 : DATA_BLOB buf;
63 : struct deferred_open_record *open_rec;
64 : };
65 :
66 : static struct pending_message_list *get_deferred_open_message_smb(
67 : struct smbd_server_connection *sconn, uint64_t mid);
68 :
69 0 : bool smb2_srv_send(struct smbXsrv_connection *xconn, char *buffer,
70 : bool do_signing, uint32_t seqnum,
71 : bool do_encrypt,
72 : struct smb_perfcount_data *pcd)
73 : {
74 0 : size_t len = 0;
75 : ssize_t ret;
76 0 : char *buf_out = buffer;
77 :
78 0 : if (!NT_STATUS_IS_OK(xconn->transport.status)) {
79 : /*
80 : * we're not supposed to do any io
81 : */
82 0 : return true;
83 : }
84 :
85 0 : len = smb_len_large(buf_out) + 4;
86 :
87 0 : ret = write_data(xconn->transport.sock, buf_out, len);
88 0 : if (ret <= 0) {
89 0 : int saved_errno = errno;
90 : /*
91 : * Try and give an error message saying what
92 : * client failed.
93 : */
94 0 : DEBUG(1,("pid[%d] Error writing %d bytes to client %s. %d. (%s)\n",
95 : (int)getpid(), (int)len,
96 : smbXsrv_connection_dbg(xconn),
97 : (int)ret, strerror(saved_errno)));
98 0 : errno = saved_errno;
99 :
100 0 : srv_free_enc_buffer(xconn, buf_out);
101 0 : goto out;
102 : }
103 :
104 0 : SMB_PERFCOUNT_SET_MSGLEN_OUT(pcd, len);
105 0 : srv_free_enc_buffer(xconn, buf_out);
106 0 : out:
107 0 : SMB_PERFCOUNT_END(pcd);
108 :
109 0 : return (ret > 0);
110 : }
111 :
112 : #if !defined(WITH_SMB1SERVER)
113 : bool smb1_srv_send(struct smbXsrv_connection *xconn, char *buffer,
114 : bool do_signing, uint32_t seqnum,
115 : bool do_encrypt,
116 : struct smb_perfcount_data *pcd)
117 : {
118 : size_t len = 0;
119 : ssize_t ret;
120 : len = smb_len_large(buffer) + 4;
121 : ret = write_data(xconn->transport.sock, buffer, len);
122 : return (ret > 0);
123 : }
124 : #endif
125 :
126 : /*******************************************************************
127 : Setup the word count and byte count for a smb1 message.
128 : ********************************************************************/
129 :
130 712 : size_t srv_smb1_set_message(char *buf,
131 : size_t num_words,
132 : size_t num_bytes,
133 : bool zero)
134 : {
135 712 : if (zero && (num_words || num_bytes)) {
136 0 : memset(buf + smb_size,'\0',num_words*2 + num_bytes);
137 : }
138 712 : SCVAL(buf,smb_wct,num_words);
139 712 : SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
140 712 : smb_setlen(buf,(smb_size + num_words*2 + num_bytes - 4));
141 712 : return (smb_size + num_words*2 + num_bytes);
142 : }
143 :
144 5742 : NTSTATUS read_packet_remainder(int fd, char *buffer,
145 : unsigned int timeout, ssize_t len)
146 : {
147 : NTSTATUS status;
148 :
149 5742 : if (len <= 0) {
150 0 : return NT_STATUS_OK;
151 : }
152 :
153 5742 : status = read_fd_with_timeout(fd, buffer, len, len, timeout, NULL);
154 5742 : if (!NT_STATUS_IS_OK(status)) {
155 : char addr[INET6_ADDRSTRLEN];
156 0 : DEBUG(0, ("read_fd_with_timeout failed for client %s read "
157 : "error = %s.\n",
158 : get_peer_addr(fd, addr, sizeof(addr)),
159 : nt_errstr(status)));
160 : }
161 5742 : return status;
162 : }
163 :
164 : #if !defined(WITH_SMB1SERVER)
165 : static NTSTATUS smb2_receive_raw_talloc(TALLOC_CTX *mem_ctx,
166 : struct smbXsrv_connection *xconn,
167 : int sock,
168 : char **buffer, unsigned int timeout,
169 : size_t *p_unread, size_t *plen)
170 : {
171 : char lenbuf[4];
172 : size_t len;
173 : NTSTATUS status;
174 :
175 : *p_unread = 0;
176 :
177 : status = read_smb_length_return_keepalive(sock, lenbuf, timeout,
178 : &len);
179 : if (!NT_STATUS_IS_OK(status)) {
180 : return status;
181 : }
182 :
183 : /*
184 : * The +4 here can't wrap, we've checked the length above already.
185 : */
186 :
187 : *buffer = talloc_array(mem_ctx, char, len+4);
188 :
189 : if (*buffer == NULL) {
190 : DEBUG(0, ("Could not allocate inbuf of length %d\n",
191 : (int)len+4));
192 : return NT_STATUS_NO_MEMORY;
193 : }
194 :
195 : memcpy(*buffer, lenbuf, sizeof(lenbuf));
196 :
197 : status = read_packet_remainder(sock, (*buffer)+4, timeout, len);
198 : if (!NT_STATUS_IS_OK(status)) {
199 : return status;
200 : }
201 :
202 : *plen = len + 4;
203 : return NT_STATUS_OK;
204 : }
205 :
206 : static NTSTATUS smb2_receive_talloc(TALLOC_CTX *mem_ctx,
207 : struct smbXsrv_connection *xconn,
208 : int sock,
209 : char **buffer, unsigned int timeout,
210 : size_t *p_unread, bool *p_encrypted,
211 : size_t *p_len,
212 : uint32_t *seqnum,
213 : bool trusted_channel)
214 : {
215 : size_t len = 0;
216 : NTSTATUS status;
217 :
218 : *p_encrypted = false;
219 :
220 : status = smb2_receive_raw_talloc(mem_ctx, xconn, sock, buffer, timeout,
221 : p_unread, &len);
222 : if (!NT_STATUS_IS_OK(status)) {
223 : DEBUG(NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)?5:1,
224 : ("smb2_receive_raw_talloc failed for client %s "
225 : "read error = %s.\n",
226 : smbXsrv_connection_dbg(xconn),
227 : nt_errstr(status)) );
228 : return status;
229 : }
230 :
231 : *p_len = len;
232 : return NT_STATUS_OK;
233 : }
234 : #endif
235 :
236 267 : NTSTATUS receive_smb_talloc(TALLOC_CTX *mem_ctx,
237 : struct smbXsrv_connection *xconn,
238 : int sock,
239 : char **buffer, unsigned int timeout,
240 : size_t *p_unread, bool *p_encrypted,
241 : size_t *p_len,
242 : uint32_t *seqnum,
243 : bool trusted_channel)
244 : {
245 : #if defined(WITH_SMB1SERVER)
246 267 : return smb1_receive_talloc(mem_ctx, xconn, sock, buffer, timeout,
247 : p_unread, p_encrypted, p_len, seqnum,
248 : trusted_channel);
249 : #else
250 : return smb2_receive_talloc(mem_ctx, xconn, sock, buffer, timeout,
251 : p_unread, p_encrypted, p_len, seqnum,
252 : trusted_channel);
253 : #endif
254 : }
255 :
256 : /****************************************************************************
257 : Function to delete a sharing violation open message by mid.
258 : ****************************************************************************/
259 :
260 0 : void remove_deferred_open_message_smb(struct smbXsrv_connection *xconn,
261 : uint64_t mid)
262 : {
263 0 : struct smbd_server_connection *sconn = xconn->client->sconn;
264 : struct pending_message_list *pml;
265 :
266 0 : if (sconn->using_smb2) {
267 0 : remove_deferred_open_message_smb2(xconn, mid);
268 0 : return;
269 : }
270 :
271 0 : for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
272 0 : if (mid == (uint64_t)SVAL(pml->buf.data,smb_mid)) {
273 0 : DEBUG(10,("remove_deferred_open_message_smb: "
274 : "deleting mid %llu len %u\n",
275 : (unsigned long long)mid,
276 : (unsigned int)pml->buf.length ));
277 0 : DLIST_REMOVE(sconn->deferred_open_queue, pml);
278 0 : TALLOC_FREE(pml);
279 0 : return;
280 : }
281 : }
282 : }
283 :
284 0 : static void smbd_deferred_open_timer(struct tevent_context *ev,
285 : struct tevent_timer *te,
286 : struct timeval _tval,
287 : void *private_data)
288 : {
289 0 : struct pending_message_list *msg = talloc_get_type(private_data,
290 : struct pending_message_list);
291 0 : struct smbd_server_connection *sconn = msg->sconn;
292 0 : struct smbXsrv_connection *xconn = msg->xconn;
293 0 : TALLOC_CTX *mem_ctx = talloc_tos();
294 0 : uint64_t mid = (uint64_t)SVAL(msg->buf.data,smb_mid);
295 : uint8_t *inbuf;
296 :
297 0 : inbuf = (uint8_t *)talloc_memdup(mem_ctx, msg->buf.data,
298 : msg->buf.length);
299 0 : if (inbuf == NULL) {
300 0 : exit_server("smbd_deferred_open_timer: talloc failed\n");
301 : return;
302 : }
303 :
304 : /* We leave this message on the queue so the open code can
305 : know this is a retry. */
306 0 : DEBUG(5,("smbd_deferred_open_timer: trigger mid %llu.\n",
307 : (unsigned long long)mid ));
308 :
309 : /* Mark the message as processed so this is not
310 : * re-processed in error. */
311 0 : msg->processed = true;
312 :
313 0 : process_smb(xconn, inbuf,
314 : msg->buf.length, 0,
315 0 : msg->seqnum, msg->encrypted, &msg->pcd);
316 :
317 : /* If it's still there and was processed, remove it. */
318 0 : msg = get_deferred_open_message_smb(sconn, mid);
319 0 : if (msg && msg->processed) {
320 0 : remove_deferred_open_message_smb(xconn, mid);
321 : }
322 0 : }
323 :
324 : /****************************************************************************
325 : Move a sharing violation open retry message to the front of the list and
326 : schedule it for immediate processing.
327 : ****************************************************************************/
328 :
329 0 : bool schedule_deferred_open_message_smb(struct smbXsrv_connection *xconn,
330 : uint64_t mid)
331 : {
332 0 : struct smbd_server_connection *sconn = xconn->client->sconn;
333 : struct pending_message_list *pml;
334 0 : int i = 0;
335 :
336 0 : if (sconn->using_smb2) {
337 0 : return schedule_deferred_open_message_smb2(xconn, mid);
338 : }
339 :
340 0 : for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
341 0 : uint64_t msg_mid = (uint64_t)SVAL(pml->buf.data,smb_mid);
342 :
343 0 : DEBUG(10,("schedule_deferred_open_message_smb: [%d] "
344 : "msg_mid = %llu\n",
345 : i++,
346 : (unsigned long long)msg_mid ));
347 :
348 0 : if (mid == msg_mid) {
349 : struct tevent_timer *te;
350 :
351 0 : if (pml->processed) {
352 : /* A processed message should not be
353 : * rescheduled. */
354 0 : DEBUG(0,("schedule_deferred_open_message_smb: LOGIC ERROR "
355 : "message mid %llu was already processed\n",
356 : (unsigned long long)msg_mid ));
357 0 : continue;
358 : }
359 :
360 0 : DEBUG(10,("schedule_deferred_open_message_smb: "
361 : "scheduling mid %llu\n",
362 : (unsigned long long)mid ));
363 :
364 : /*
365 : * smbd_deferred_open_timer() calls
366 : * process_smb() to redispatch the request
367 : * including the required impersonation.
368 : *
369 : * So we can just use the raw tevent_context.
370 : */
371 0 : te = tevent_add_timer(xconn->client->raw_ev_ctx,
372 : pml,
373 : timeval_zero(),
374 : smbd_deferred_open_timer,
375 : pml);
376 0 : if (!te) {
377 0 : DEBUG(10,("schedule_deferred_open_message_smb: "
378 : "event_add_timed() failed, "
379 : "skipping mid %llu\n",
380 : (unsigned long long)msg_mid ));
381 : }
382 :
383 0 : TALLOC_FREE(pml->te);
384 0 : pml->te = te;
385 0 : DLIST_PROMOTE(sconn->deferred_open_queue, pml);
386 0 : return true;
387 : }
388 : }
389 :
390 0 : DEBUG(10,("schedule_deferred_open_message_smb: failed to "
391 : "find message mid %llu\n",
392 : (unsigned long long)mid ));
393 :
394 0 : return false;
395 : }
396 :
397 : /****************************************************************************
398 : Return true if this mid is on the deferred queue and was not yet processed.
399 : ****************************************************************************/
400 :
401 911 : bool open_was_deferred(struct smbXsrv_connection *xconn, uint64_t mid)
402 : {
403 911 : struct smbd_server_connection *sconn = xconn->client->sconn;
404 : struct pending_message_list *pml;
405 :
406 911 : if (sconn->using_smb2) {
407 911 : return open_was_deferred_smb2(xconn, mid);
408 : }
409 :
410 0 : for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
411 0 : if (((uint64_t)SVAL(pml->buf.data,smb_mid)) == mid && !pml->processed) {
412 0 : return True;
413 : }
414 : }
415 0 : return False;
416 : }
417 :
418 : /****************************************************************************
419 : Return the message queued by this mid.
420 : ****************************************************************************/
421 :
422 0 : static struct pending_message_list *get_deferred_open_message_smb(
423 : struct smbd_server_connection *sconn, uint64_t mid)
424 : {
425 : struct pending_message_list *pml;
426 :
427 0 : for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
428 0 : if (((uint64_t)SVAL(pml->buf.data,smb_mid)) == mid) {
429 0 : return pml;
430 : }
431 : }
432 0 : return NULL;
433 : }
434 :
435 : /****************************************************************************
436 : Get the state data queued by this mid.
437 : ****************************************************************************/
438 :
439 19330 : bool get_deferred_open_message_state(struct smb_request *smbreq,
440 : struct timeval *p_request_time,
441 : struct deferred_open_record **open_rec)
442 : {
443 : struct pending_message_list *pml;
444 :
445 19330 : if (smbreq->sconn->using_smb2) {
446 19330 : return get_deferred_open_message_state_smb2(smbreq->smb2req,
447 : p_request_time,
448 : open_rec);
449 : }
450 :
451 0 : pml = get_deferred_open_message_smb(smbreq->sconn, smbreq->mid);
452 0 : if (!pml) {
453 0 : return false;
454 : }
455 0 : if (p_request_time) {
456 0 : *p_request_time = pml->request_time;
457 : }
458 0 : if (open_rec != NULL) {
459 0 : *open_rec = pml->open_rec;
460 : }
461 0 : return true;
462 : }
463 :
464 0 : bool push_deferred_open_message_smb(struct smb_request *req,
465 : struct timeval timeout,
466 : struct file_id id,
467 : struct deferred_open_record *open_rec)
468 : {
469 : #if defined(WITH_SMB1SERVER)
470 0 : if (req->smb2req) {
471 : #endif
472 0 : return push_deferred_open_message_smb2(req->smb2req,
473 : req->request_time,
474 : timeout,
475 : id,
476 : open_rec);
477 : #if defined(WITH_SMB1SERVER)
478 : } else {
479 0 : return push_deferred_open_message_smb1(req, timeout,
480 : id, open_rec);
481 : }
482 : #endif
483 : }
484 :
485 356 : static void construct_smb1_reply_common(uint8_t cmd, const uint8_t *inbuf,
486 : char *outbuf)
487 : {
488 356 : uint16_t in_flags2 = SVAL(inbuf,smb_flg2);
489 356 : uint16_t out_flags2 = common_flags2;
490 :
491 356 : out_flags2 |= in_flags2 & FLAGS2_UNICODE_STRINGS;
492 356 : out_flags2 |= in_flags2 & FLAGS2_SMB_SECURITY_SIGNATURES;
493 356 : out_flags2 |= in_flags2 & FLAGS2_SMB_SECURITY_SIGNATURES_REQUIRED;
494 :
495 356 : srv_smb1_set_message(outbuf,0,0,false);
496 :
497 356 : SCVAL(outbuf, smb_com, cmd);
498 356 : SIVAL(outbuf,smb_rcls,0);
499 356 : SCVAL(outbuf,smb_flg, FLAG_REPLY | (CVAL(inbuf,smb_flg) & FLAG_CASELESS_PATHNAMES));
500 356 : SSVAL(outbuf,smb_flg2, out_flags2);
501 356 : memset(outbuf+smb_pidhigh,'\0',(smb_tid-smb_pidhigh));
502 356 : memcpy(outbuf+smb_ss_field, inbuf+smb_ss_field, 8);
503 :
504 356 : SSVAL(outbuf,smb_tid,SVAL(inbuf,smb_tid));
505 356 : SSVAL(outbuf,smb_pid,SVAL(inbuf,smb_pid));
506 356 : SSVAL(outbuf,smb_pidhigh,SVAL(inbuf,smb_pidhigh));
507 356 : SSVAL(outbuf,smb_uid,SVAL(inbuf,smb_uid));
508 356 : SSVAL(outbuf,smb_mid,SVAL(inbuf,smb_mid));
509 356 : }
510 :
511 0 : void construct_smb1_reply_common_req(struct smb_request *req, char *outbuf)
512 : {
513 0 : construct_smb1_reply_common(req->cmd, req->inbuf, outbuf);
514 0 : }
515 :
516 : /*******************************************************************
517 : allocate and initialize a reply packet
518 : ********************************************************************/
519 :
520 356 : bool create_smb1_outbuf(TALLOC_CTX *mem_ctx, struct smb_request *req,
521 : const uint8_t *inbuf, char **outbuf,
522 : uint8_t num_words, uint32_t num_bytes)
523 : {
524 356 : size_t smb_len = MIN_SMB_SIZE + VWV(num_words) + num_bytes;
525 :
526 : /*
527 : * Protect against integer wrap.
528 : * The SMB layer reply can be up to 0xFFFFFF bytes.
529 : */
530 356 : if ((num_bytes > 0xffffff) || (smb_len > 0xffffff)) {
531 : char *msg;
532 0 : if (asprintf(&msg, "num_bytes too large: %u",
533 : (unsigned)num_bytes) == -1) {
534 0 : msg = discard_const_p(char, "num_bytes too large");
535 : }
536 0 : smb_panic(msg);
537 : }
538 :
539 : /*
540 : * Here we include the NBT header for now.
541 : */
542 356 : *outbuf = talloc_array(mem_ctx, char,
543 : NBT_HDR_SIZE + smb_len);
544 356 : if (*outbuf == NULL) {
545 0 : return false;
546 : }
547 :
548 356 : construct_smb1_reply_common(req->cmd, inbuf, *outbuf);
549 356 : srv_smb1_set_message(*outbuf, num_words, num_bytes, false);
550 : /*
551 : * Zero out the word area, the caller has to take care of the bcc area
552 : * himself
553 : */
554 356 : if (num_words != 0) {
555 318 : memset(*outbuf + (NBT_HDR_SIZE + HDR_VWV), 0, VWV(num_words));
556 : }
557 :
558 356 : return true;
559 : }
560 :
561 356 : void reply_smb1_outbuf(struct smb_request *req, uint8_t num_words, uint32_t num_bytes)
562 : {
563 : char *outbuf;
564 356 : if (!create_smb1_outbuf(req, req, req->inbuf, &outbuf, num_words,
565 : num_bytes)) {
566 0 : smb_panic("could not allocate output buffer\n");
567 : }
568 356 : req->outbuf = (uint8_t *)outbuf;
569 356 : }
570 :
571 5501 : bool valid_smb1_header(const uint8_t *inbuf)
572 : {
573 5501 : if (is_encrypted_packet(inbuf)) {
574 0 : return true;
575 : }
576 : /*
577 : * This used to be (strncmp(smb_base(inbuf),"\377SMB",4) == 0)
578 : * but it just looks weird to call strncmp for this one.
579 : */
580 5501 : return (IVAL(smb_base(inbuf), 0) == 0x424D53FF);
581 : }
582 :
583 : /****************************************************************************
584 : Process an smb from the client
585 : ****************************************************************************/
586 :
587 1 : static void process_smb2(struct smbXsrv_connection *xconn,
588 : uint8_t *inbuf, size_t nread, size_t unread_bytes,
589 : uint32_t seqnum, bool encrypted,
590 : struct smb_perfcount_data *deferred_pcd)
591 : {
592 1 : const uint8_t *inpdu = inbuf + NBT_HDR_SIZE;
593 1 : size_t pdulen = nread - NBT_HDR_SIZE;
594 1 : NTSTATUS status = smbd_smb2_process_negprot(xconn, 0, inpdu, pdulen);
595 1 : if (!NT_STATUS_IS_OK(status)) {
596 0 : exit_server_cleanly("SMB2 negprot fail");
597 : }
598 1 : }
599 :
600 229 : void process_smb(struct smbXsrv_connection *xconn,
601 : uint8_t *inbuf, size_t nread, size_t unread_bytes,
602 : uint32_t seqnum, bool encrypted,
603 : struct smb_perfcount_data *deferred_pcd)
604 : {
605 229 : struct smbd_server_connection *sconn = xconn->client->sconn;
606 229 : int msg_type = CVAL(inbuf,0);
607 :
608 229 : DO_PROFILE_INC(request);
609 :
610 229 : DEBUG( 6, ( "got message type 0x%x of len 0x%x\n", msg_type,
611 : smb_len(inbuf) ) );
612 229 : DEBUG(3, ("Transaction %d of length %d (%u toread)\n",
613 : sconn->trans_num, (int)nread, (unsigned int)unread_bytes));
614 :
615 229 : if (msg_type != NBSSmessage) {
616 : /*
617 : * NetBIOS session request, keepalive, etc.
618 : */
619 16 : reply_special(xconn, (char *)inbuf, nread);
620 16 : goto done;
621 : }
622 :
623 : #if defined(WITH_SMB1SERVER)
624 213 : if (sconn->using_smb2) {
625 : /* At this point we're not really using smb2,
626 : * we make the decision here.. */
627 137 : if (smbd_is_smb2_header(inbuf, nread)) {
628 : #endif
629 1 : process_smb2(xconn, inbuf, nread, unread_bytes, seqnum,
630 : encrypted, deferred_pcd);
631 1 : return;
632 : #if defined(WITH_SMB1SERVER)
633 : }
634 136 : if (nread >= smb_size && valid_smb1_header(inbuf)
635 136 : && CVAL(inbuf, smb_com) != 0x72) {
636 : /* This is a non-negprot SMB1 packet.
637 : Disable SMB2 from now on. */
638 38 : sconn->using_smb2 = false;
639 : }
640 : }
641 212 : process_smb1(xconn, inbuf, nread, unread_bytes, seqnum, encrypted,
642 : deferred_pcd);
643 : #endif
644 :
645 228 : done:
646 228 : sconn->num_requests++;
647 :
648 : /* The timeout_processing function isn't run nearly
649 : often enough to implement 'max log size' without
650 : overrunning the size of the file by many megabytes.
651 : This is especially true if we are running at debug
652 : level 10. Checking every 50 SMBs is a nice
653 : tradeoff of performance vs log file size overrun. */
654 :
655 228 : if ((sconn->num_requests % 50) == 0 &&
656 0 : need_to_check_log_size()) {
657 0 : change_to_root_user();
658 0 : check_log_size();
659 : }
660 : }
661 :
662 5090 : NTSTATUS smbXsrv_connection_init_tables(struct smbXsrv_connection *conn,
663 : enum protocol_types protocol)
664 : {
665 : NTSTATUS status;
666 :
667 5090 : conn->protocol = protocol;
668 :
669 5090 : if (conn->client->session_table != NULL) {
670 50 : return NT_STATUS_OK;
671 : }
672 :
673 5040 : if (protocol >= PROTOCOL_SMB2_02) {
674 5002 : status = smb2srv_session_table_init(conn);
675 5002 : if (!NT_STATUS_IS_OK(status)) {
676 0 : conn->protocol = PROTOCOL_NONE;
677 0 : return status;
678 : }
679 :
680 5002 : status = smb2srv_open_table_init(conn);
681 5002 : if (!NT_STATUS_IS_OK(status)) {
682 0 : conn->protocol = PROTOCOL_NONE;
683 0 : return status;
684 : }
685 : } else {
686 : #if defined(WITH_SMB1SERVER)
687 38 : status = smb1srv_session_table_init(conn);
688 38 : if (!NT_STATUS_IS_OK(status)) {
689 0 : conn->protocol = PROTOCOL_NONE;
690 0 : return status;
691 : }
692 :
693 38 : status = smb1srv_tcon_table_init(conn);
694 38 : if (!NT_STATUS_IS_OK(status)) {
695 0 : conn->protocol = PROTOCOL_NONE;
696 0 : return status;
697 : }
698 :
699 38 : status = smb1srv_open_table_init(conn);
700 38 : if (!NT_STATUS_IS_OK(status)) {
701 0 : conn->protocol = PROTOCOL_NONE;
702 0 : return status;
703 : }
704 : #else
705 : conn->protocol = PROTOCOL_NONE;
706 : return NT_STATUS_INVALID_NETWORK_RESPONSE;
707 : #endif
708 : }
709 :
710 5040 : set_Protocol(protocol);
711 5040 : return NT_STATUS_OK;
712 : }
713 :
714 : /**
715 : * Create a debug string for the connection
716 : *
717 : * This is allocated to talloc_tos() or a string constant
718 : * in certain corner cases. The returned string should
719 : * hence not be free'd directly but only via the talloc stack.
720 : */
721 13 : const char *smbXsrv_connection_dbg(const struct smbXsrv_connection *xconn)
722 : {
723 : const char *ret;
724 : char *addr;
725 : /*
726 : * TODO: this can be improved later
727 : * maybe including the client guid or more
728 : */
729 13 : addr = tsocket_address_string(xconn->remote_address, talloc_tos());
730 13 : if (addr == NULL) {
731 0 : return "<tsocket_address_string() failed>";
732 : }
733 :
734 13 : ret = talloc_asprintf(talloc_tos(), "ptr=%p,id=%llu,addr=%s",
735 13 : xconn, (unsigned long long)xconn->channel_id, addr);
736 13 : TALLOC_FREE(addr);
737 13 : if (ret == NULL) {
738 0 : return "<talloc_asprintf() failed>";
739 : }
740 :
741 13 : return ret;
742 : }
743 :
744 : /*
745 : * Initialize a struct smb_request from an inbuf
746 : */
747 :
748 3677 : bool init_smb1_request(struct smb_request *req,
749 : struct smbd_server_connection *sconn,
750 : struct smbXsrv_connection *xconn,
751 : const uint8_t *inbuf,
752 : size_t unread_bytes, bool encrypted,
753 : uint32_t seqnum)
754 : {
755 : struct smbXsrv_tcon *tcon;
756 : NTSTATUS status;
757 : NTTIME now;
758 3677 : size_t req_size = smb_len(inbuf) + 4;
759 :
760 : /* Ensure we have at least smb_size bytes. */
761 3677 : if (req_size < smb_size) {
762 0 : DEBUG(0,("init_smb1_request: invalid request size %u\n",
763 : (unsigned int)req_size ));
764 0 : return false;
765 : }
766 :
767 3677 : *req = (struct smb_request) { .cmd = 0};
768 :
769 3677 : req->request_time = timeval_current();
770 3677 : now = timeval_to_nttime(&req->request_time);
771 :
772 3677 : req->cmd = CVAL(inbuf, smb_com);
773 3677 : req->flags2 = SVAL(inbuf, smb_flg2);
774 3677 : req->smbpid = SVAL(inbuf, smb_pid);
775 3677 : req->mid = (uint64_t)SVAL(inbuf, smb_mid);
776 3677 : req->seqnum = seqnum;
777 3677 : req->vuid = SVAL(inbuf, smb_uid);
778 3677 : req->tid = SVAL(inbuf, smb_tid);
779 3677 : req->wct = CVAL(inbuf, smb_wct);
780 3677 : req->vwv = (const uint16_t *)(inbuf+smb_vwv);
781 3677 : req->buflen = smb_buflen(inbuf);
782 3677 : req->buf = (const uint8_t *)smb_buf_const(inbuf);
783 3677 : req->unread_bytes = unread_bytes;
784 3677 : req->encrypted = encrypted;
785 3677 : req->sconn = sconn;
786 3677 : req->xconn = xconn;
787 3677 : if (xconn != NULL) {
788 3677 : status = smb1srv_tcon_lookup(xconn, req->tid, now, &tcon);
789 3677 : if (NT_STATUS_IS_OK(status)) {
790 40 : req->conn = tcon->compat;
791 : }
792 : }
793 3677 : req->posix_pathnames = lp_posix_pathnames();
794 3677 : smb_init_perfcount_data(&req->pcd);
795 :
796 : /* Ensure we have at least wct words and 2 bytes of bcc. */
797 3677 : if (smb_size + req->wct*2 > req_size) {
798 0 : DEBUG(0,("init_smb1_request: invalid wct number %u (size %u)\n",
799 : (unsigned int)req->wct,
800 : (unsigned int)req_size));
801 0 : return false;
802 : }
803 : /* Ensure bcc is correct. */
804 3677 : if (((const uint8_t *)smb_buf_const(inbuf)) + req->buflen > inbuf + req_size) {
805 0 : DEBUG(0,("init_smb1_request: invalid bcc number %u "
806 : "(wct = %u, size %u)\n",
807 : (unsigned int)req->buflen,
808 : (unsigned int)req->wct,
809 : (unsigned int)req_size));
810 0 : return false;
811 : }
812 :
813 3677 : return true;
814 : }
815 :
816 : /****************************************************************************
817 : Construct a reply to the incoming packet.
818 : ****************************************************************************/
819 :
820 3465 : static void construct_reply_smb1negprot(struct smbXsrv_connection *xconn,
821 : char *inbuf, int size,
822 : size_t unread_bytes)
823 : {
824 3465 : struct smbd_server_connection *sconn = xconn->client->sconn;
825 : struct smb_request *req;
826 : NTSTATUS status;
827 :
828 3465 : if (!(req = talloc(talloc_tos(), struct smb_request))) {
829 0 : smb_panic("could not allocate smb_request");
830 : }
831 :
832 3465 : if (!init_smb1_request(req, sconn, xconn, (uint8_t *)inbuf, unread_bytes,
833 : false, 0)) {
834 0 : exit_server_cleanly("Invalid SMB request");
835 : }
836 :
837 3465 : req->inbuf = (uint8_t *)talloc_move(req, &inbuf);
838 :
839 3465 : status = smb2_multi_protocol_reply_negprot(req);
840 3261 : if (req->outbuf == NULL) {
841 : /*
842 : * req->outbuf == NULL means we bootstrapped into SMB2.
843 : */
844 3261 : return;
845 : }
846 0 : if (!NT_STATUS_IS_OK(status)) {
847 0 : if (!smb1_srv_send(req->xconn,
848 0 : (char *)req->outbuf,
849 0 : true, req->seqnum+1,
850 0 : IS_CONN_ENCRYPTED(req->conn)||req->encrypted,
851 : &req->pcd)) {
852 0 : exit_server_cleanly("construct_reply_smb1negprot: "
853 : "smb1_srv_send failed.");
854 : }
855 0 : TALLOC_FREE(req);
856 : } else {
857 : /* This code path should only *ever* bootstrap into SMB2. */
858 0 : exit_server_cleanly("Internal error SMB1negprot didn't reply "
859 : "with an SMB2 packet");
860 : }
861 : }
862 :
863 0 : static void smbd_server_connection_write_handler(
864 : struct smbXsrv_connection *xconn)
865 : {
866 : /* TODO: make write nonblocking */
867 0 : }
868 :
869 5518 : static void smbd_smb2_server_connection_read_handler(
870 : struct smbXsrv_connection *xconn, int fd)
871 : {
872 : char lenbuf[NBT_HDR_SIZE];
873 5518 : size_t len = 0;
874 5518 : uint8_t *buffer = NULL;
875 5518 : size_t bufferlen = 0;
876 : NTSTATUS status;
877 5518 : uint8_t msg_type = 0;
878 :
879 : /* Read the first 4 bytes - contains length of remainder. */
880 5518 : status = read_smb_length_return_keepalive(fd, lenbuf, 0, &len);
881 5518 : if (!NT_STATUS_IS_OK(status)) {
882 5 : exit_server_cleanly("failed to receive request length");
883 : return;
884 : }
885 :
886 : /* Integer wrap check. */
887 5513 : if (len + NBT_HDR_SIZE < len) {
888 0 : exit_server_cleanly("Invalid length on initial request");
889 : return;
890 : }
891 :
892 : /*
893 : * The +4 here can't wrap, we've checked the length above already.
894 : */
895 5513 : bufferlen = len+NBT_HDR_SIZE;
896 :
897 5513 : buffer = talloc_array(talloc_tos(), uint8_t, bufferlen);
898 5513 : if (buffer == NULL) {
899 0 : DBG_ERR("Could not allocate request inbuf of length %zu\n",
900 : bufferlen);
901 0 : exit_server_cleanly("talloc fail");
902 : return;
903 : }
904 :
905 : /* Copy the NBT_HDR_SIZE length. */
906 5513 : memcpy(buffer, lenbuf, sizeof(lenbuf));
907 :
908 5513 : status = read_packet_remainder(fd, (char *)buffer+NBT_HDR_SIZE, 0, len);
909 5513 : if (!NT_STATUS_IS_OK(status)) {
910 0 : exit_server_cleanly("Failed to read remainder of initial request");
911 : return;
912 : }
913 :
914 : /* Check the message type. */
915 5513 : msg_type = PULL_LE_U8(buffer,0);
916 5513 : if (msg_type == NBSSrequest) {
917 : /*
918 : * clients can send this request before
919 : * bootstrapping into SMB2. Cope with this
920 : * message only, don't allow any other strange
921 : * NBSS types.
922 : */
923 360 : reply_special(xconn, (char *)buffer, bufferlen);
924 347 : xconn->client->sconn->num_requests++;
925 347 : return;
926 : }
927 :
928 : /* Only a 'normal' message type allowed now. */
929 5153 : if (msg_type != NBSSmessage) {
930 0 : DBG_ERR("Invalid message type %d\n", msg_type);
931 0 : exit_server_cleanly("Invalid message type for initial request");
932 : return;
933 : }
934 :
935 : /* Could this be an SMB1 negprot bootstrap into SMB2 ? */
936 5153 : if (bufferlen < smb_size) {
937 0 : exit_server_cleanly("Invalid initial SMB1 or SMB2 packet");
938 : return;
939 : }
940 5153 : if (valid_smb1_header(buffer)) {
941 : /* Can *only* allow an SMB1 negprot here. */
942 3473 : uint8_t cmd = PULL_LE_U8(buffer, smb_com);
943 3473 : if (cmd != SMBnegprot) {
944 8 : DBG_ERR("Incorrect SMB1 command 0x%hhx, "
945 : "should be SMBnegprot (0x72)\n",
946 : cmd);
947 8 : exit_server_cleanly("Invalid initial SMB1 packet");
948 : }
949 : /* Minimal process_smb(). */
950 3465 : show_msg((char *)buffer);
951 3465 : construct_reply_smb1negprot(xconn, (char *)buffer,
952 : bufferlen, 0);
953 3261 : xconn->client->sconn->trans_num++;
954 3261 : xconn->client->sconn->num_requests++;
955 3261 : return;
956 :
957 1680 : } else if (!smbd_is_smb2_header(buffer, bufferlen)) {
958 0 : exit_server_cleanly("Invalid initial SMB2 packet");
959 : return;
960 : }
961 :
962 : /* Here we know we're a valid SMB2 packet. */
963 :
964 : /*
965 : * Point at the start of the SMB2 PDU.
966 : * len is the length of the SMB2 PDU.
967 : */
968 :
969 1680 : status = smbd_smb2_process_negprot(xconn,
970 : 0,
971 : (const uint8_t *)buffer+NBT_HDR_SIZE,
972 : len);
973 1680 : if (!NT_STATUS_IS_OK(status)) {
974 0 : exit_server_cleanly("SMB2 negprot fail");
975 : }
976 1680 : return;
977 : }
978 :
979 5785 : static void smbd_server_connection_handler(struct tevent_context *ev,
980 : struct tevent_fd *fde,
981 : uint16_t flags,
982 : void *private_data)
983 : {
984 3934 : struct smbXsrv_connection *xconn =
985 1851 : talloc_get_type_abort(private_data,
986 : struct smbXsrv_connection);
987 :
988 5785 : if (!NT_STATUS_IS_OK(xconn->transport.status)) {
989 : /*
990 : * we're not supposed to do any io
991 : */
992 0 : TEVENT_FD_NOT_READABLE(xconn->transport.fde);
993 0 : TEVENT_FD_NOT_WRITEABLE(xconn->transport.fde);
994 0 : return;
995 : }
996 :
997 5785 : if (flags & TEVENT_FD_WRITE) {
998 0 : smbd_server_connection_write_handler(xconn);
999 0 : return;
1000 : }
1001 5785 : if (flags & TEVENT_FD_READ) {
1002 : #if defined(WITH_SMB1SERVER)
1003 5785 : if (lp_server_min_protocol() > PROTOCOL_NT1) {
1004 : #endif
1005 5518 : smbd_smb2_server_connection_read_handler(xconn,
1006 : xconn->transport.sock);
1007 : #if defined(WITH_SMB1SERVER)
1008 : } else {
1009 267 : smbd_smb1_server_connection_read_handler(xconn,
1010 : xconn->transport.sock);
1011 : }
1012 : #endif
1013 5517 : return;
1014 : }
1015 : }
1016 :
1017 : struct smbd_release_ip_state {
1018 : struct smbXsrv_connection *xconn;
1019 : struct tevent_immediate *im;
1020 : char addr[INET6_ADDRSTRLEN];
1021 : };
1022 :
1023 0 : static void smbd_release_ip_immediate(struct tevent_context *ctx,
1024 : struct tevent_immediate *im,
1025 : void *private_data)
1026 : {
1027 0 : struct smbd_release_ip_state *state =
1028 0 : talloc_get_type_abort(private_data,
1029 : struct smbd_release_ip_state);
1030 0 : struct smbXsrv_connection *xconn = state->xconn;
1031 :
1032 0 : if (!NT_STATUS_EQUAL(xconn->transport.status, NT_STATUS_ADDRESS_CLOSED)) {
1033 : /*
1034 : * smbd_server_connection_terminate() already triggered ?
1035 : */
1036 0 : return;
1037 : }
1038 :
1039 0 : smbd_server_connection_terminate(xconn, "CTDB_SRVID_RELEASE_IP");
1040 : }
1041 :
1042 : /****************************************************************************
1043 : received when we should release a specific IP
1044 : ****************************************************************************/
1045 0 : static int release_ip(struct tevent_context *ev,
1046 : uint32_t src_vnn, uint32_t dst_vnn,
1047 : uint64_t dst_srvid,
1048 : const uint8_t *msg, size_t msglen,
1049 : void *private_data)
1050 : {
1051 0 : struct smbd_release_ip_state *state =
1052 0 : talloc_get_type_abort(private_data,
1053 : struct smbd_release_ip_state);
1054 0 : struct smbXsrv_connection *xconn = state->xconn;
1055 : const char *ip;
1056 0 : const char *addr = state->addr;
1057 0 : const char *p = addr;
1058 :
1059 0 : if (msglen == 0) {
1060 0 : return 0;
1061 : }
1062 0 : if (msg[msglen-1] != '\0') {
1063 0 : return 0;
1064 : }
1065 :
1066 0 : ip = (const char *)msg;
1067 :
1068 0 : if (!NT_STATUS_IS_OK(xconn->transport.status)) {
1069 : /* avoid recursion */
1070 0 : return 0;
1071 : }
1072 :
1073 0 : if (strncmp("::ffff:", addr, 7) == 0) {
1074 0 : p = addr + 7;
1075 : }
1076 :
1077 0 : DEBUG(10, ("Got release IP message for %s, "
1078 : "our address is %s\n", ip, p));
1079 :
1080 0 : if ((strcmp(p, ip) == 0) || ((p != addr) && strcmp(addr, ip) == 0)) {
1081 0 : DEBUG(0,("Got release IP message for our IP %s - exiting immediately\n",
1082 : ip));
1083 : /*
1084 : * With SMB2 we should do a clean disconnect,
1085 : * the previous_session_id in the session setup
1086 : * will cleanup the old session, tcons and opens.
1087 : *
1088 : * A clean disconnect is needed in order to support
1089 : * durable handles.
1090 : *
1091 : * Note: typically this is never triggered
1092 : * as we got a TCP RST (triggered by ctdb event scripts)
1093 : * before we get CTDB_SRVID_RELEASE_IP.
1094 : *
1095 : * We used to call _exit(1) here, but as this was mostly never
1096 : * triggered and has implication on our process model,
1097 : * we can just use smbd_server_connection_terminate()
1098 : * (also for SMB1).
1099 : *
1100 : * We don't call smbd_server_connection_terminate() directly
1101 : * as we might be called from within ctdbd_migrate(),
1102 : * we need to defer our action to the next event loop
1103 : */
1104 0 : tevent_schedule_immediate(state->im,
1105 : xconn->client->raw_ev_ctx,
1106 : smbd_release_ip_immediate,
1107 : state);
1108 :
1109 : /*
1110 : * Make sure we don't get any io on the connection.
1111 : */
1112 0 : xconn->transport.status = NT_STATUS_ADDRESS_CLOSED;
1113 0 : return EADDRNOTAVAIL;
1114 : }
1115 :
1116 0 : return 0;
1117 : }
1118 :
1119 0 : static int match_cluster_movable_ip(uint32_t total_ip_count,
1120 : const struct sockaddr_storage *ip,
1121 : bool is_movable_ip,
1122 : void *private_data)
1123 : {
1124 0 : const struct sockaddr_storage *srv = private_data;
1125 0 : struct samba_sockaddr pub_ip = {
1126 : .u = {
1127 : .ss = *ip,
1128 : },
1129 : };
1130 0 : struct samba_sockaddr srv_ip = {
1131 : .u = {
1132 : .ss = *srv,
1133 : },
1134 : };
1135 :
1136 0 : if (is_movable_ip && sockaddr_equal(&pub_ip.u.sa, &srv_ip.u.sa)) {
1137 0 : return EADDRNOTAVAIL;
1138 : }
1139 :
1140 0 : return 0;
1141 : }
1142 :
1143 0 : static NTSTATUS smbd_register_ips(struct smbXsrv_connection *xconn,
1144 : struct sockaddr_storage *srv,
1145 : struct sockaddr_storage *clnt)
1146 : {
1147 : struct smbd_release_ip_state *state;
1148 : struct ctdbd_connection *cconn;
1149 : int ret;
1150 :
1151 0 : cconn = messaging_ctdb_connection();
1152 0 : if (cconn == NULL) {
1153 0 : return NT_STATUS_NO_MEMORY;
1154 : }
1155 :
1156 0 : state = talloc_zero(xconn, struct smbd_release_ip_state);
1157 0 : if (state == NULL) {
1158 0 : return NT_STATUS_NO_MEMORY;
1159 : }
1160 0 : state->xconn = xconn;
1161 0 : state->im = tevent_create_immediate(state);
1162 0 : if (state->im == NULL) {
1163 0 : return NT_STATUS_NO_MEMORY;
1164 : }
1165 0 : if (print_sockaddr(state->addr, sizeof(state->addr), srv) == NULL) {
1166 0 : return NT_STATUS_NO_MEMORY;
1167 : }
1168 :
1169 0 : if (xconn->client->server_multi_channel_enabled) {
1170 0 : ret = ctdbd_public_ip_foreach(cconn,
1171 : match_cluster_movable_ip,
1172 : srv);
1173 0 : if (ret == EADDRNOTAVAIL) {
1174 0 : xconn->has_cluster_movable_ip = true;
1175 0 : DBG_DEBUG("cluster movable IP on %s\n",
1176 : smbXsrv_connection_dbg(xconn));
1177 0 : } else if (ret != 0) {
1178 0 : DBG_ERR("failed to iterate cluster IPs: %s\n",
1179 : strerror(ret));
1180 0 : return NT_STATUS_INTERNAL_ERROR;
1181 : }
1182 : }
1183 :
1184 0 : ret = ctdbd_register_ips(cconn, srv, clnt, release_ip, state);
1185 0 : if (ret != 0) {
1186 0 : return map_nt_error_from_unix(ret);
1187 : }
1188 0 : return NT_STATUS_OK;
1189 : }
1190 :
1191 5320 : static int smbXsrv_connection_destructor(struct smbXsrv_connection *xconn)
1192 : {
1193 5320 : DBG_DEBUG("xconn[%s]\n", smbXsrv_connection_dbg(xconn));
1194 5320 : return 0;
1195 : }
1196 :
1197 5320 : NTSTATUS smbd_add_connection(struct smbXsrv_client *client, int sock_fd,
1198 : NTTIME now, struct smbXsrv_connection **_xconn)
1199 : {
1200 5320 : TALLOC_CTX *frame = talloc_stackframe();
1201 : struct smbXsrv_connection *xconn;
1202 : struct sockaddr_storage ss_srv;
1203 5320 : void *sp_srv = (void *)&ss_srv;
1204 5320 : struct sockaddr *sa_srv = (struct sockaddr *)sp_srv;
1205 : struct sockaddr_storage ss_clnt;
1206 5320 : void *sp_clnt = (void *)&ss_clnt;
1207 5320 : struct sockaddr *sa_clnt = (struct sockaddr *)sp_clnt;
1208 : socklen_t sa_socklen;
1209 5320 : struct tsocket_address *local_address = NULL;
1210 5320 : struct tsocket_address *remote_address = NULL;
1211 5320 : const char *remaddr = NULL;
1212 : char *p;
1213 5320 : const char *rhost = NULL;
1214 : int ret;
1215 : int tmp;
1216 :
1217 5320 : *_xconn = NULL;
1218 :
1219 5320 : DO_PROFILE_INC(connect);
1220 :
1221 5320 : xconn = talloc_zero(client, struct smbXsrv_connection);
1222 5320 : if (xconn == NULL) {
1223 0 : DEBUG(0,("talloc_zero(struct smbXsrv_connection)\n"));
1224 0 : TALLOC_FREE(frame);
1225 0 : return NT_STATUS_NO_MEMORY;
1226 : }
1227 5320 : talloc_set_destructor(xconn, smbXsrv_connection_destructor);
1228 5320 : talloc_steal(frame, xconn);
1229 5320 : xconn->client = client;
1230 5320 : xconn->connect_time = now;
1231 5320 : if (client->next_channel_id != 0) {
1232 5320 : xconn->channel_id = client->next_channel_id++;
1233 : }
1234 :
1235 5320 : xconn->transport.sock = sock_fd;
1236 : #if defined(WITH_SMB1SERVER)
1237 5320 : smbd_echo_init(xconn);
1238 : #endif
1239 5320 : xconn->protocol = PROTOCOL_NONE;
1240 :
1241 : /* Ensure child is set to blocking mode */
1242 5320 : set_blocking(sock_fd,True);
1243 :
1244 5320 : set_socket_options(sock_fd, "SO_KEEPALIVE");
1245 5320 : set_socket_options(sock_fd, lp_socket_options());
1246 :
1247 5320 : sa_socklen = sizeof(ss_clnt);
1248 5320 : ret = getpeername(sock_fd, sa_clnt, &sa_socklen);
1249 5320 : if (ret != 0) {
1250 0 : int saved_errno = errno;
1251 0 : int level = (errno == ENOTCONN)?2:0;
1252 0 : DEBUG(level,("getpeername() failed - %s\n",
1253 : strerror(saved_errno)));
1254 0 : TALLOC_FREE(frame);
1255 0 : return map_nt_error_from_unix_common(saved_errno);
1256 : }
1257 5320 : ret = tsocket_address_bsd_from_sockaddr(xconn,
1258 : sa_clnt, sa_socklen,
1259 : &remote_address);
1260 5320 : if (ret != 0) {
1261 0 : int saved_errno = errno;
1262 0 : DEBUG(0,("%s: tsocket_address_bsd_from_sockaddr remote failed - %s\n",
1263 : __location__, strerror(saved_errno)));
1264 0 : TALLOC_FREE(frame);
1265 0 : return map_nt_error_from_unix_common(saved_errno);
1266 : }
1267 :
1268 5320 : sa_socklen = sizeof(ss_srv);
1269 5320 : ret = getsockname(sock_fd, sa_srv, &sa_socklen);
1270 5320 : if (ret != 0) {
1271 0 : int saved_errno = errno;
1272 0 : int level = (errno == ENOTCONN)?2:0;
1273 0 : DEBUG(level,("getsockname() failed - %s\n",
1274 : strerror(saved_errno)));
1275 0 : TALLOC_FREE(frame);
1276 0 : return map_nt_error_from_unix_common(saved_errno);
1277 : }
1278 5320 : ret = tsocket_address_bsd_from_sockaddr(xconn,
1279 : sa_srv, sa_socklen,
1280 : &local_address);
1281 5320 : if (ret != 0) {
1282 0 : int saved_errno = errno;
1283 0 : DEBUG(0,("%s: tsocket_address_bsd_from_sockaddr remote failed - %s\n",
1284 : __location__, strerror(saved_errno)));
1285 0 : TALLOC_FREE(frame);
1286 0 : return map_nt_error_from_unix_common(saved_errno);
1287 : }
1288 :
1289 5320 : if (tsocket_address_is_inet(remote_address, "ip")) {
1290 5320 : remaddr = tsocket_address_inet_addr_string(remote_address,
1291 : talloc_tos());
1292 5320 : if (remaddr == NULL) {
1293 0 : DEBUG(0,("%s: tsocket_address_inet_addr_string remote failed - %s\n",
1294 : __location__, strerror(errno)));
1295 0 : TALLOC_FREE(frame);
1296 0 : return NT_STATUS_NO_MEMORY;
1297 : }
1298 : } else {
1299 0 : remaddr = "0.0.0.0";
1300 : }
1301 :
1302 : /*
1303 : * Before the first packet, check the global hosts allow/ hosts deny
1304 : * parameters before doing any parsing of packets passed to us by the
1305 : * client. This prevents attacks on our parsing code from hosts not in
1306 : * the hosts allow list.
1307 : */
1308 :
1309 5320 : ret = get_remote_hostname(remote_address,
1310 : &p, talloc_tos());
1311 5320 : if (ret < 0) {
1312 0 : int saved_errno = errno;
1313 0 : DEBUG(0,("%s: get_remote_hostname failed - %s\n",
1314 : __location__, strerror(saved_errno)));
1315 0 : TALLOC_FREE(frame);
1316 0 : return map_nt_error_from_unix_common(saved_errno);
1317 : }
1318 5320 : rhost = p;
1319 5320 : if (strequal(rhost, "UNKNOWN")) {
1320 0 : rhost = remaddr;
1321 : }
1322 :
1323 5320 : xconn->local_address = local_address;
1324 5320 : xconn->remote_address = remote_address;
1325 5320 : xconn->remote_hostname = talloc_strdup(xconn, rhost);
1326 5320 : if (xconn->remote_hostname == NULL) {
1327 0 : return NT_STATUS_NO_MEMORY;
1328 : }
1329 :
1330 5320 : if (!srv_init_signing(xconn)) {
1331 0 : DEBUG(0, ("Failed to init smb_signing\n"));
1332 0 : TALLOC_FREE(frame);
1333 0 : return NT_STATUS_INTERNAL_ERROR;
1334 : }
1335 :
1336 5320 : if (!allow_access(lp_hosts_deny(-1), lp_hosts_allow(-1),
1337 : xconn->remote_hostname,
1338 : remaddr)) {
1339 0 : DEBUG( 1, ("Connection denied from %s to %s\n",
1340 : tsocket_address_string(remote_address, talloc_tos()),
1341 : tsocket_address_string(local_address, talloc_tos())));
1342 :
1343 : /*
1344 : * We return a valid xconn
1345 : * so that the caller can return an error message
1346 : * to the client
1347 : */
1348 0 : DLIST_ADD_END(client->connections, xconn);
1349 0 : talloc_steal(client, xconn);
1350 :
1351 0 : *_xconn = xconn;
1352 0 : TALLOC_FREE(frame);
1353 0 : return NT_STATUS_NETWORK_ACCESS_DENIED;
1354 : }
1355 :
1356 5320 : DEBUG(10, ("Connection allowed from %s to %s\n",
1357 : tsocket_address_string(remote_address, talloc_tos()),
1358 : tsocket_address_string(local_address, talloc_tos())));
1359 :
1360 5320 : if (lp_clustering()) {
1361 : /*
1362 : * We need to tell ctdb about our client's TCP
1363 : * connection, so that for failover ctdbd can send
1364 : * tickle acks, triggering a reconnection by the
1365 : * client.
1366 : */
1367 : NTSTATUS status;
1368 :
1369 0 : status = smbd_register_ips(xconn, &ss_srv, &ss_clnt);
1370 0 : if (!NT_STATUS_IS_OK(status)) {
1371 0 : DEBUG(0, ("ctdbd_register_ips failed: %s\n",
1372 : nt_errstr(status)));
1373 : }
1374 : }
1375 :
1376 5320 : tmp = lp_max_xmit();
1377 5320 : tmp = MAX(tmp, SMB_BUFFER_SIZE_MIN);
1378 5320 : tmp = MIN(tmp, SMB_BUFFER_SIZE_MAX);
1379 :
1380 : #if defined(WITH_SMB1SERVER)
1381 5320 : xconn->smb1.negprot.max_recv = tmp;
1382 :
1383 5320 : xconn->smb1.sessions.done_sesssetup = false;
1384 5320 : xconn->smb1.sessions.max_send = SMB_BUFFER_SIZE_MAX;
1385 : #endif
1386 :
1387 5320 : xconn->transport.fde = tevent_add_fd(client->raw_ev_ctx,
1388 : xconn,
1389 : sock_fd,
1390 : TEVENT_FD_READ,
1391 : smbd_server_connection_handler,
1392 : xconn);
1393 5320 : if (!xconn->transport.fde) {
1394 0 : TALLOC_FREE(frame);
1395 0 : return NT_STATUS_NO_MEMORY;
1396 : }
1397 5320 : tevent_fd_set_auto_close(xconn->transport.fde);
1398 :
1399 : /* for now we only have one connection */
1400 5320 : DLIST_ADD_END(client->connections, xconn);
1401 5320 : talloc_steal(client, xconn);
1402 :
1403 5320 : *_xconn = xconn;
1404 5320 : TALLOC_FREE(frame);
1405 5320 : return NT_STATUS_OK;
1406 : }
1407 :
1408 0 : static bool uid_in_use(struct auth_session_info *session_info,
1409 : uid_t uid)
1410 : {
1411 0 : if (session_info->unix_token->uid == uid) {
1412 0 : return true;
1413 : }
1414 0 : return false;
1415 : }
1416 :
1417 0 : static bool gid_in_use(struct auth_session_info *session_info,
1418 : gid_t gid)
1419 : {
1420 : uint32_t i;
1421 0 : struct security_unix_token *utok = NULL;
1422 :
1423 0 : utok = session_info->unix_token;
1424 0 : if (utok->gid == gid) {
1425 0 : return true;
1426 : }
1427 :
1428 0 : for(i = 0; i < utok->ngroups; i++) {
1429 0 : if (utok->groups[i] == gid) {
1430 0 : return true;
1431 : }
1432 : }
1433 0 : return false;
1434 : }
1435 :
1436 0 : static bool sid_in_use(struct auth_session_info *session_info,
1437 : const struct dom_sid *psid)
1438 : {
1439 0 : struct security_token *tok = NULL;
1440 :
1441 0 : tok = session_info->security_token;
1442 0 : if (tok == NULL) {
1443 : /*
1444 : * Not sure session_info->security_token can
1445 : * ever be NULL. This check might be not
1446 : * necessary.
1447 : */
1448 0 : return false;
1449 : }
1450 0 : if (security_token_has_sid(tok, psid)) {
1451 0 : return true;
1452 : }
1453 0 : return false;
1454 : }
1455 :
1456 : struct id_in_use_state {
1457 : const struct id_cache_ref *id;
1458 : bool match;
1459 : };
1460 :
1461 0 : static int id_in_use_cb(struct smbXsrv_session *session,
1462 : void *private_data)
1463 : {
1464 0 : struct id_in_use_state *state = (struct id_in_use_state *)
1465 : private_data;
1466 0 : struct auth_session_info *session_info =
1467 0 : session->global->auth_session_info;
1468 :
1469 0 : switch(state->id->type) {
1470 0 : case UID:
1471 0 : state->match = uid_in_use(session_info, state->id->id.uid);
1472 0 : break;
1473 0 : case GID:
1474 0 : state->match = gid_in_use(session_info, state->id->id.gid);
1475 0 : break;
1476 0 : case SID:
1477 0 : state->match = sid_in_use(session_info, &state->id->id.sid);
1478 0 : break;
1479 0 : default:
1480 0 : state->match = false;
1481 0 : break;
1482 : }
1483 0 : if (state->match) {
1484 0 : return -1;
1485 : }
1486 0 : return 0;
1487 : }
1488 :
1489 0 : static bool id_in_use(struct smbd_server_connection *sconn,
1490 : const struct id_cache_ref *id)
1491 : {
1492 : struct id_in_use_state state;
1493 : NTSTATUS status;
1494 :
1495 0 : state = (struct id_in_use_state) {
1496 : .id = id,
1497 : .match = false,
1498 : };
1499 :
1500 0 : status = smbXsrv_session_local_traverse(sconn->client,
1501 : id_in_use_cb,
1502 : &state);
1503 0 : if (!NT_STATUS_IS_OK(status)) {
1504 0 : return false;
1505 : }
1506 :
1507 0 : return state.match;
1508 : }
1509 :
1510 : /****************************************************************************
1511 : Check if services need reloading.
1512 : ****************************************************************************/
1513 :
1514 380 : static void check_reload(struct smbd_server_connection *sconn, time_t t)
1515 : {
1516 :
1517 380 : if (last_smb_conf_reload_time == 0) {
1518 63 : last_smb_conf_reload_time = t;
1519 : }
1520 :
1521 380 : if (t >= last_smb_conf_reload_time+SMBD_RELOAD_CHECK) {
1522 80 : reload_services(sconn, conn_snum_used, true);
1523 80 : last_smb_conf_reload_time = t;
1524 : }
1525 380 : }
1526 :
1527 0 : static void msg_kill_client_ip(struct messaging_context *msg_ctx,
1528 : void *private_data, uint32_t msg_type,
1529 : struct server_id server_id, DATA_BLOB *data)
1530 : {
1531 0 : struct smbd_server_connection *sconn = talloc_get_type_abort(
1532 : private_data, struct smbd_server_connection);
1533 0 : const char *ip = (char *) data->data;
1534 : char *client_ip;
1535 :
1536 0 : DBG_DEBUG("Got kill request for client IP %s\n", ip);
1537 :
1538 0 : client_ip = tsocket_address_inet_addr_string(sconn->remote_address,
1539 : talloc_tos());
1540 0 : if (client_ip == NULL) {
1541 0 : return;
1542 : }
1543 :
1544 0 : if (strequal(ip, client_ip)) {
1545 0 : DBG_WARNING("Got kill client message for %s - "
1546 : "exiting immediately\n", ip);
1547 0 : exit_server_cleanly("Forced disconnect for client");
1548 : }
1549 :
1550 0 : TALLOC_FREE(client_ip);
1551 : }
1552 :
1553 : /*
1554 : * Do the recurring check if we're idle
1555 : */
1556 380 : static bool deadtime_fn(const struct timeval *now, void *private_data)
1557 : {
1558 380 : struct smbd_server_connection *sconn =
1559 : (struct smbd_server_connection *)private_data;
1560 :
1561 380 : if ((conn_num_open(sconn) == 0)
1562 380 : || (conn_idle_all(sconn, now->tv_sec))) {
1563 0 : DEBUG( 2, ( "Closing idle connection\n" ) );
1564 0 : messaging_send(sconn->msg_ctx,
1565 0 : messaging_server_id(sconn->msg_ctx),
1566 : MSG_SHUTDOWN, &data_blob_null);
1567 0 : return False;
1568 : }
1569 :
1570 380 : return True;
1571 : }
1572 :
1573 : /*
1574 : * Do the recurring log file and smb.conf reload checks.
1575 : */
1576 :
1577 380 : static bool housekeeping_fn(const struct timeval *now, void *private_data)
1578 : {
1579 380 : struct smbd_server_connection *sconn = talloc_get_type_abort(
1580 : private_data, struct smbd_server_connection);
1581 :
1582 380 : DEBUG(5, ("housekeeping\n"));
1583 :
1584 380 : change_to_root_user();
1585 :
1586 : /* check if we need to reload services */
1587 380 : check_reload(sconn, time_mono(NULL));
1588 :
1589 : /*
1590 : * Force a log file check.
1591 : */
1592 380 : force_check_log_size();
1593 380 : check_log_size();
1594 380 : return true;
1595 : }
1596 :
1597 10 : static void smbd_sig_term_handler(struct tevent_context *ev,
1598 : struct tevent_signal *se,
1599 : int signum,
1600 : int count,
1601 : void *siginfo,
1602 : void *private_data)
1603 : {
1604 10 : exit_server_cleanly("termination signal");
1605 : }
1606 :
1607 5270 : static void smbd_setup_sig_term_handler(struct smbd_server_connection *sconn)
1608 : {
1609 : struct tevent_signal *se;
1610 :
1611 5270 : se = tevent_add_signal(sconn->ev_ctx,
1612 : sconn,
1613 : SIGTERM, 0,
1614 : smbd_sig_term_handler,
1615 : sconn);
1616 5270 : if (!se) {
1617 0 : exit_server("failed to setup SIGTERM handler");
1618 : }
1619 5270 : }
1620 :
1621 0 : static void smbd_sig_hup_handler(struct tevent_context *ev,
1622 : struct tevent_signal *se,
1623 : int signum,
1624 : int count,
1625 : void *siginfo,
1626 : void *private_data)
1627 : {
1628 0 : struct smbd_server_connection *sconn =
1629 0 : talloc_get_type_abort(private_data,
1630 : struct smbd_server_connection);
1631 :
1632 0 : change_to_root_user();
1633 0 : DEBUG(1,("Reloading services after SIGHUP\n"));
1634 0 : reload_services(sconn, conn_snum_used, false);
1635 0 : }
1636 :
1637 5270 : static void smbd_setup_sig_hup_handler(struct smbd_server_connection *sconn)
1638 : {
1639 : struct tevent_signal *se;
1640 :
1641 5270 : se = tevent_add_signal(sconn->ev_ctx,
1642 : sconn,
1643 : SIGHUP, 0,
1644 : smbd_sig_hup_handler,
1645 : sconn);
1646 5270 : if (!se) {
1647 0 : exit_server("failed to setup SIGHUP handler");
1648 : }
1649 5270 : }
1650 :
1651 3 : static void smbd_conf_updated(struct messaging_context *msg,
1652 : void *private_data,
1653 : uint32_t msg_type,
1654 : struct server_id server_id,
1655 : DATA_BLOB *data)
1656 : {
1657 3 : struct smbd_server_connection *sconn =
1658 0 : talloc_get_type_abort(private_data,
1659 : struct smbd_server_connection);
1660 :
1661 3 : DEBUG(10,("smbd_conf_updated: Got message saying smb.conf was "
1662 : "updated. Reloading.\n"));
1663 3 : change_to_root_user();
1664 3 : reload_services(sconn, conn_snum_used, false);
1665 3 : }
1666 :
1667 0 : static void smbd_id_cache_kill(struct messaging_context *msg_ctx,
1668 : void *private_data,
1669 : uint32_t msg_type,
1670 : struct server_id server_id,
1671 : DATA_BLOB* data)
1672 : {
1673 0 : const char *msg = (data && data->data)
1674 0 : ? (const char *)data->data : "<NULL>";
1675 : struct id_cache_ref id;
1676 0 : struct smbd_server_connection *sconn =
1677 0 : talloc_get_type_abort(private_data,
1678 : struct smbd_server_connection);
1679 :
1680 0 : if (!id_cache_ref_parse(msg, &id)) {
1681 0 : DEBUG(0, ("Invalid ?ID: %s\n", msg));
1682 0 : return;
1683 : }
1684 :
1685 0 : if (id_in_use(sconn, &id)) {
1686 0 : exit_server_cleanly(msg);
1687 : }
1688 0 : id_cache_delete_from_cache(&id);
1689 : }
1690 :
1691 : struct smbd_tevent_trace_state {
1692 : struct tevent_context *ev;
1693 : TALLOC_CTX *frame;
1694 : SMBPROFILE_BASIC_ASYNC_STATE(profile_idle);
1695 : };
1696 :
1697 2383734 : static void smbd_tevent_trace_callback(enum tevent_trace_point point,
1698 : void *private_data)
1699 : {
1700 2383734 : struct smbd_tevent_trace_state *state =
1701 : (struct smbd_tevent_trace_state *)private_data;
1702 :
1703 2383734 : switch (point) {
1704 462999 : case TEVENT_TRACE_BEFORE_WAIT:
1705 462999 : if (!smbprofile_dump_pending()) {
1706 : /*
1707 : * If there's no dump pending
1708 : * we don't want to schedule a new 1 sec timer.
1709 : *
1710 : * Instead we want to sleep as long as nothing happens.
1711 : */
1712 462999 : smbprofile_dump_setup(NULL);
1713 : }
1714 462999 : SMBPROFILE_BASIC_ASYNC_START(idle, profile_p, state->profile_idle);
1715 462999 : break;
1716 462999 : case TEVENT_TRACE_AFTER_WAIT:
1717 462999 : SMBPROFILE_BASIC_ASYNC_END(state->profile_idle);
1718 462999 : if (!smbprofile_dump_pending()) {
1719 : /*
1720 : * We need to flush our state after sleeping
1721 : * (hopefully a long time).
1722 : */
1723 462999 : smbprofile_dump();
1724 : /*
1725 : * future profiling events should trigger timers
1726 : * on our main event context.
1727 : */
1728 462999 : smbprofile_dump_setup(state->ev);
1729 : }
1730 462999 : break;
1731 731503 : case TEVENT_TRACE_BEFORE_LOOP_ONCE:
1732 731503 : TALLOC_FREE(state->frame);
1733 731503 : state->frame = talloc_stackframe_pool(8192);
1734 731503 : break;
1735 726233 : case TEVENT_TRACE_AFTER_LOOP_ONCE:
1736 726233 : TALLOC_FREE(state->frame);
1737 726233 : break;
1738 : }
1739 :
1740 2383734 : errno = 0;
1741 2383734 : }
1742 :
1743 : /****************************************************************************
1744 : Process commands from the client
1745 : ****************************************************************************/
1746 :
1747 5270 : void smbd_process(struct tevent_context *ev_ctx,
1748 : struct messaging_context *msg_ctx,
1749 : int sock_fd,
1750 : bool interactive)
1751 : {
1752 10540 : struct smbd_tevent_trace_state trace_state = {
1753 : .ev = ev_ctx,
1754 5270 : .frame = talloc_stackframe(),
1755 : };
1756 3626 : const struct loadparm_substitution *lp_sub =
1757 1644 : loadparm_s3_global_substitution();
1758 5270 : struct smbXsrv_client *client = NULL;
1759 5270 : struct smbd_server_connection *sconn = NULL;
1760 5270 : struct smbXsrv_connection *xconn = NULL;
1761 5270 : const char *locaddr = NULL;
1762 5270 : const char *remaddr = NULL;
1763 : int ret;
1764 : NTSTATUS status;
1765 5270 : struct timeval tv = timeval_current();
1766 5270 : NTTIME now = timeval_to_nttime(&tv);
1767 5270 : char *chroot_dir = NULL;
1768 : int rc;
1769 :
1770 5270 : status = smbXsrv_client_create(ev_ctx, ev_ctx, msg_ctx, now, &client);
1771 5270 : if (!NT_STATUS_IS_OK(status)) {
1772 0 : DBG_ERR("smbXsrv_client_create(): %s\n", nt_errstr(status));
1773 0 : exit_server_cleanly("talloc_zero(struct smbXsrv_client).\n");
1774 : }
1775 :
1776 : /*
1777 : * TODO: remove this...:-)
1778 : */
1779 5270 : global_smbXsrv_client = client;
1780 :
1781 5270 : sconn = talloc_zero(client, struct smbd_server_connection);
1782 5270 : if (sconn == NULL) {
1783 0 : exit_server("failed to create smbd_server_connection");
1784 : }
1785 :
1786 5270 : client->sconn = sconn;
1787 5270 : sconn->client = client;
1788 :
1789 5270 : sconn->ev_ctx = ev_ctx;
1790 5270 : sconn->msg_ctx = msg_ctx;
1791 :
1792 5270 : ret = pthreadpool_tevent_init(sconn, lp_aio_max_threads(),
1793 : &sconn->pool);
1794 5270 : if (ret != 0) {
1795 0 : exit_server("pthreadpool_tevent_init() failed.");
1796 : }
1797 :
1798 : #if defined(WITH_SMB1SERVER)
1799 5270 : if (lp_server_max_protocol() >= PROTOCOL_SMB2_02) {
1800 : #endif
1801 : /*
1802 : * We're not making the decision here,
1803 : * we're just allowing the client
1804 : * to decide between SMB1 and SMB2
1805 : * with the first negprot
1806 : * packet.
1807 : */
1808 5270 : sconn->using_smb2 = true;
1809 : #if defined(WITH_SMB1SERVER)
1810 : }
1811 : #endif
1812 :
1813 5270 : if (!interactive) {
1814 5270 : smbd_setup_sig_term_handler(sconn);
1815 5270 : smbd_setup_sig_hup_handler(sconn);
1816 : }
1817 :
1818 5270 : status = smbd_add_connection(client, sock_fd, now, &xconn);
1819 5270 : if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_ACCESS_DENIED)) {
1820 : /*
1821 : * send a negative session response "not listening on calling
1822 : * name"
1823 : */
1824 0 : unsigned char buf[5] = {0x83, 0, 0, 1, 0x81};
1825 0 : (void)smb1_srv_send(xconn,(char *)buf, false,
1826 : 0, false, NULL);
1827 0 : exit_server_cleanly("connection denied");
1828 5270 : } else if (!NT_STATUS_IS_OK(status)) {
1829 0 : exit_server_cleanly(nt_errstr(status));
1830 : }
1831 :
1832 5270 : sconn->local_address =
1833 5270 : tsocket_address_copy(xconn->local_address, sconn);
1834 5270 : if (sconn->local_address == NULL) {
1835 0 : exit_server_cleanly("tsocket_address_copy() failed");
1836 : }
1837 5270 : sconn->remote_address =
1838 5270 : tsocket_address_copy(xconn->remote_address, sconn);
1839 5270 : if (sconn->remote_address == NULL) {
1840 0 : exit_server_cleanly("tsocket_address_copy() failed");
1841 : }
1842 5270 : sconn->remote_hostname =
1843 5270 : talloc_strdup(sconn, xconn->remote_hostname);
1844 5270 : if (sconn->remote_hostname == NULL) {
1845 0 : exit_server_cleanly("tsocket_strdup() failed");
1846 : }
1847 :
1848 10540 : client->global->local_address =
1849 8896 : tsocket_address_string(sconn->local_address,
1850 5270 : client->global);
1851 5270 : if (client->global->local_address == NULL) {
1852 0 : exit_server_cleanly("tsocket_address_string() failed");
1853 : }
1854 10540 : client->global->remote_address =
1855 8896 : tsocket_address_string(sconn->remote_address,
1856 5270 : client->global);
1857 5270 : if (client->global->remote_address == NULL) {
1858 0 : exit_server_cleanly("tsocket_address_string() failed");
1859 : }
1860 10540 : client->global->remote_name =
1861 8896 : talloc_strdup(client->global, sconn->remote_hostname);
1862 5270 : if (client->global->remote_name == NULL) {
1863 0 : exit_server_cleanly("tsocket_strdup() failed");
1864 : }
1865 :
1866 5270 : if (tsocket_address_is_inet(sconn->local_address, "ip")) {
1867 5270 : locaddr = tsocket_address_inet_addr_string(
1868 : sconn->local_address,
1869 : talloc_tos());
1870 5270 : if (locaddr == NULL) {
1871 0 : DEBUG(0,("%s: tsocket_address_inet_addr_string remote failed - %s\n",
1872 : __location__, strerror(errno)));
1873 0 : exit_server_cleanly("tsocket_address_inet_addr_string remote failed.\n");
1874 : }
1875 : } else {
1876 0 : locaddr = "0.0.0.0";
1877 : }
1878 :
1879 5270 : if (tsocket_address_is_inet(sconn->remote_address, "ip")) {
1880 5270 : remaddr = tsocket_address_inet_addr_string(
1881 : sconn->remote_address,
1882 : talloc_tos());
1883 5270 : if (remaddr == NULL) {
1884 0 : DEBUG(0,("%s: tsocket_address_inet_addr_string remote failed - %s\n",
1885 : __location__, strerror(errno)));
1886 0 : exit_server_cleanly("tsocket_address_inet_addr_string remote failed.\n");
1887 : }
1888 : } else {
1889 0 : remaddr = "0.0.0.0";
1890 : }
1891 :
1892 : /* this is needed so that we get decent entries
1893 : in smbstatus for port 445 connects */
1894 5270 : set_remote_machine_name(remaddr, false);
1895 5270 : reload_services(sconn, conn_snum_used, true);
1896 5270 : sub_set_socket_ids(remaddr,
1897 : sconn->remote_hostname,
1898 : locaddr);
1899 :
1900 5270 : if (lp_preload_modules()) {
1901 0 : smb_load_all_modules_absoute_path(lp_preload_modules());
1902 : }
1903 :
1904 5270 : smb_perfcount_init();
1905 :
1906 5270 : if (!init_account_policy()) {
1907 0 : exit_server("Could not open account policy tdb.\n");
1908 : }
1909 :
1910 5270 : chroot_dir = lp_root_directory(talloc_tos(), lp_sub);
1911 5270 : if (chroot_dir[0] != '\0') {
1912 0 : rc = chdir(chroot_dir);
1913 0 : if (rc != 0) {
1914 0 : DBG_ERR("Failed to chdir to %s\n", chroot_dir);
1915 0 : exit_server("Failed to chdir()");
1916 : }
1917 :
1918 0 : rc = chroot(chroot_dir);
1919 0 : if (rc != 0) {
1920 0 : DBG_ERR("Failed to change root to %s\n", chroot_dir);
1921 0 : exit_server("Failed to chroot()");
1922 : }
1923 0 : DBG_WARNING("Changed root to %s\n", chroot_dir);
1924 :
1925 0 : TALLOC_FREE(chroot_dir);
1926 : }
1927 :
1928 5270 : if (!file_init(sconn)) {
1929 0 : exit_server("file_init() failed");
1930 : }
1931 :
1932 : /* Setup oplocks */
1933 5270 : if (!init_oplocks(sconn))
1934 0 : exit_server("Failed to init oplocks");
1935 :
1936 : /* register our message handlers */
1937 5270 : messaging_register(sconn->msg_ctx, sconn,
1938 : MSG_SMB_FORCE_TDIS, msg_force_tdis);
1939 5270 : messaging_register(
1940 : sconn->msg_ctx,
1941 : sconn,
1942 : MSG_SMB_FORCE_TDIS_DENIED,
1943 : msg_force_tdis_denied);
1944 5270 : messaging_register(sconn->msg_ctx, sconn,
1945 : MSG_SMB_CLOSE_FILE, msg_close_file);
1946 5270 : messaging_register(sconn->msg_ctx, sconn,
1947 : MSG_SMB_FILE_RENAME, msg_file_was_renamed);
1948 :
1949 5270 : id_cache_register_msgs(sconn->msg_ctx);
1950 5270 : messaging_deregister(sconn->msg_ctx, ID_CACHE_KILL, NULL);
1951 5270 : messaging_register(sconn->msg_ctx, sconn,
1952 : ID_CACHE_KILL, smbd_id_cache_kill);
1953 :
1954 5270 : messaging_deregister(sconn->msg_ctx,
1955 5270 : MSG_SMB_CONF_UPDATED, sconn->ev_ctx);
1956 5270 : messaging_register(sconn->msg_ctx, sconn,
1957 : MSG_SMB_CONF_UPDATED, smbd_conf_updated);
1958 :
1959 5270 : messaging_deregister(sconn->msg_ctx, MSG_SMB_KILL_CLIENT_IP,
1960 : NULL);
1961 5270 : messaging_register(sconn->msg_ctx, sconn,
1962 : MSG_SMB_KILL_CLIENT_IP,
1963 : msg_kill_client_ip);
1964 :
1965 5270 : messaging_deregister(sconn->msg_ctx, MSG_SMB_TELL_NUM_CHILDREN, NULL);
1966 :
1967 : /*
1968 : * Use the default MSG_DEBUG handler to avoid rebroadcasting
1969 : * MSGs to all child processes
1970 : */
1971 5270 : messaging_deregister(sconn->msg_ctx,
1972 : MSG_DEBUG, NULL);
1973 5270 : messaging_register(sconn->msg_ctx, NULL,
1974 : MSG_DEBUG, debug_message);
1975 :
1976 : #if defined(WITH_SMB1SERVER)
1977 5270 : if ((lp_keepalive() != 0)
1978 5270 : && !(event_add_idle(ev_ctx, NULL,
1979 5270 : timeval_set(lp_keepalive(), 0),
1980 : "keepalive", keepalive_fn,
1981 : sconn))) {
1982 0 : DEBUG(0, ("Could not add keepalive event\n"));
1983 0 : exit(1);
1984 : }
1985 : #endif
1986 :
1987 5270 : if (!(event_add_idle(ev_ctx, NULL,
1988 : timeval_set(IDLE_CLOSED_TIMEOUT, 0),
1989 : "deadtime", deadtime_fn, sconn))) {
1990 0 : DEBUG(0, ("Could not add deadtime event\n"));
1991 0 : exit(1);
1992 : }
1993 :
1994 5270 : if (!(event_add_idle(ev_ctx, NULL,
1995 : timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
1996 : "housekeeping", housekeeping_fn, sconn))) {
1997 0 : DEBUG(0, ("Could not add housekeeping event\n"));
1998 0 : exit(1);
1999 : }
2000 :
2001 5270 : smbprofile_dump_setup(ev_ctx);
2002 :
2003 5270 : if (!init_dptrs(sconn)) {
2004 0 : exit_server("init_dptrs() failed");
2005 : }
2006 :
2007 5270 : TALLOC_FREE(trace_state.frame);
2008 :
2009 5270 : tevent_set_trace_callback(ev_ctx, smbd_tevent_trace_callback,
2010 : &trace_state);
2011 :
2012 5270 : ret = tevent_loop_wait(ev_ctx);
2013 0 : if (ret != 0) {
2014 0 : DEBUG(1, ("tevent_loop_wait failed: %d, %s,"
2015 : " exiting\n", ret, strerror(errno)));
2016 : }
2017 :
2018 0 : TALLOC_FREE(trace_state.frame);
2019 :
2020 0 : exit_server_cleanly(NULL);
2021 : }
|