Changeset 745 for trunk/server/source3/smbd/process.c
- Timestamp:
- Nov 27, 2012, 4:43:17 PM (13 years ago)
- Location:
- trunk/server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/server
- Property svn:mergeinfo changed
/vendor/current merged: 581,587,591,594,597,600,615,618,740
- Property svn:mergeinfo changed
-
trunk/server/source3/smbd/process.c
r599 r745 4 4 Copyright (C) Andrew Tridgell 1992-1998 5 5 Copyright (C) Volker Lendecke 2005-2007 6 6 7 7 This program is free software; you can redistribute it and/or modify 8 8 it under the terms of the GNU General Public License as published by 9 9 the Free Software Foundation; either version 3 of the License, or 10 10 (at your option) any later version. 11 11 12 12 This program is distributed in the hope that it will be useful, 13 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 15 GNU General Public License for more details. 16 16 17 17 You should have received a copy of the GNU General Public License 18 18 along with this program. If not, see <http://www.gnu.org/licenses/>. … … 20 20 21 21 #include "includes.h" 22 #include "../lib/tsocket/tsocket.h" 23 #include "system/filesys.h" 24 #include "smbd/smbd.h" 22 25 #include "smbd/globals.h" 23 #include "../librpc/gen_ndr/srv_dfs.h" 24 #include "../librpc/gen_ndr/srv_dssetup.h" 25 #include "../librpc/gen_ndr/srv_echo.h" 26 #include "../librpc/gen_ndr/srv_eventlog.h" 27 #include "../librpc/gen_ndr/srv_initshutdown.h" 28 #include "../librpc/gen_ndr/srv_lsa.h" 29 #include "../librpc/gen_ndr/srv_netlogon.h" 30 #include "../librpc/gen_ndr/srv_ntsvcs.h" 31 #include "../librpc/gen_ndr/srv_samr.h" 32 #include "../librpc/gen_ndr/srv_spoolss.h" 33 #include "../librpc/gen_ndr/srv_srvsvc.h" 34 #include "../librpc/gen_ndr/srv_svcctl.h" 35 #include "../librpc/gen_ndr/srv_winreg.h" 36 #include "../librpc/gen_ndr/srv_wkssvc.h" 26 #include "librpc/gen_ndr/netlogon.h" 27 #include "../lib/async_req/async_sock.h" 28 #include "ctdbd_conn.h" 29 #include "../lib/util/select.h" 30 #include "printing/pcap.h" 31 #include "system/select.h" 32 #include "passdb.h" 33 #include "auth.h" 34 #include "messages.h" 35 #include "smbprofile.h" 36 #include "rpc_server/spoolss/srv_spoolss_nt.h" 37 #include "libsmb/libsmb.h" 38 #ifdef __OS2__ 39 #define pipe(A) os2_pipe(A) 40 #endif 37 41 38 42 extern bool global_machine_password_needs_changing; … … 40 44 static void construct_reply_common(struct smb_request *req, const char *inbuf, 41 45 char *outbuf); 46 static struct pending_message_list *get_deferred_open_message_smb(uint64_t mid); 47 48 static bool smbd_lock_socket_internal(struct smbd_server_connection *sconn) 49 { 50 bool ok; 51 52 if (sconn->smb1.echo_handler.socket_lock_fd == -1) { 53 return true; 54 } 55 56 sconn->smb1.echo_handler.ref_count++; 57 58 if (sconn->smb1.echo_handler.ref_count > 1) { 59 return true; 60 } 61 62 DEBUG(10,("pid[%d] wait for socket lock\n", (int)sys_getpid())); 63 64 do { 65 ok = fcntl_lock( 66 sconn->smb1.echo_handler.socket_lock_fd, 67 SMB_F_SETLKW, 0, 0, F_WRLCK); 68 } while (!ok && (errno == EINTR)); 69 70 if (!ok) { 71 DEBUG(1, ("fcntl_lock failed: %s\n", strerror(errno))); 72 return false; 73 } 74 75 DEBUG(10,("pid[%d] got for socket lock\n", (int)sys_getpid())); 76 77 return true; 78 } 79 80 void smbd_lock_socket(struct smbd_server_connection *sconn) 81 { 82 if (!smbd_lock_socket_internal(sconn)) { 83 exit_server_cleanly("failed to lock socket"); 84 } 85 } 86 87 static bool smbd_unlock_socket_internal(struct smbd_server_connection *sconn) 88 { 89 bool ok; 90 91 if (sconn->smb1.echo_handler.socket_lock_fd == -1) { 92 return true; 93 } 94 95 sconn->smb1.echo_handler.ref_count--; 96 97 if (sconn->smb1.echo_handler.ref_count > 0) { 98 return true; 99 } 100 101 do { 102 ok = fcntl_lock( 103 sconn->smb1.echo_handler.socket_lock_fd, 104 SMB_F_SETLKW, 0, 0, F_UNLCK); 105 } while (!ok && (errno == EINTR)); 106 107 if (!ok) { 108 DEBUG(1, ("fcntl_lock failed: %s\n", strerror(errno))); 109 return false; 110 } 111 112 DEBUG(10,("pid[%d] unlocked socket\n", (int)sys_getpid())); 113 114 return true; 115 } 116 117 void smbd_unlock_socket(struct smbd_server_connection *sconn) 118 { 119 if (!smbd_unlock_socket_internal(sconn)) { 120 exit_server_cleanly("failed to unlock socket"); 121 } 122 } 42 123 43 124 /* Accessor function for smb_read_error for smbd functions. */ … … 47 128 ****************************************************************************/ 48 129 49 bool srv_send_smb( int fd, char *buffer,130 bool srv_send_smb(struct smbd_server_connection *sconn, char *buffer, 50 131 bool do_signing, uint32_t seqnum, 51 132 bool do_encrypt, … … 57 138 char *buf_out = buffer; 58 139 140 smbd_lock_socket(sconn); 141 59 142 if (do_signing) { 60 143 /* Sign the outgoing packet if required. */ 61 srv_calculate_sign_mac(s mbd_server_conn, buf_out, seqnum);144 srv_calculate_sign_mac(sconn, buf_out, seqnum); 62 145 } 63 146 … … 74 157 len = smb_len(buf_out) + 4; 75 158 76 ret = write_data( fd,buf_out+nwritten,len - nwritten);159 ret = write_data(sconn->sock, buf_out+nwritten, len - nwritten); 77 160 if (ret <= 0) { 78 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n", 79 (int)len,(int)ret, strerror(errno) )); 161 162 char addr[INET6_ADDRSTRLEN]; 163 /* 164 * Try and give an error message saying what 165 * client failed. 166 */ 167 DEBUG(1,("pid[%d] Error writing %d bytes to client %s. %d. (%s)\n", 168 (int)sys_getpid(), (int)len, 169 get_peer_addr(sconn->sock, addr, sizeof(addr)), 170 (int)ret, strerror(errno) )); 171 80 172 srv_free_enc_buffer(buf_out); 81 173 goto out; … … 86 178 out: 87 179 SMB_PERFCOUNT_END(pcd); 180 181 smbd_unlock_socket(sconn); 88 182 return true; 89 183 } … … 139 233 unsigned int timeout, ssize_t len) 140 234 { 235 NTSTATUS status; 236 141 237 if (len <= 0) { 142 238 return NT_STATUS_OK; 143 239 } 144 240 145 return read_fd_with_timeout(fd, buffer, len, len, timeout, NULL); 241 status = read_fd_with_timeout(fd, buffer, len, len, timeout, NULL); 242 if (!NT_STATUS_IS_OK(status)) { 243 char addr[INET6_ADDRSTRLEN]; 244 DEBUG(0, ("read_fd_with_timeout failed for client %s read " 245 "error = %s.\n", 246 get_peer_addr(fd, addr, sizeof(addr)), 247 nt_errstr(status))); 248 } 249 return status; 146 250 } 147 251 … … 164 268 static NTSTATUS receive_smb_raw_talloc_partial_read(TALLOC_CTX *mem_ctx, 165 269 const char lenbuf[4], 166 int fd, char **buffer, 270 struct smbd_server_connection *sconn, 271 int sock, 272 char **buffer, 167 273 unsigned int timeout, 168 274 size_t *p_unread, … … 178 284 179 285 status = read_fd_with_timeout( 180 fd, writeX_header + 4,286 sock, writeX_header + 4, 181 287 STANDARD_WRITE_AND_X_HEADER_SIZE, 182 288 STANDARD_WRITE_AND_X_HEADER_SIZE, … … 184 290 185 291 if (!NT_STATUS_IS_OK(status)) { 292 DEBUG(0, ("read_fd_with_timeout failed for client %s read " 293 "error = %s.\n", sconn->client_id.addr, 294 nt_errstr(status))); 186 295 return status; 187 296 } … … 192 301 */ 193 302 194 if (is_valid_writeX_buffer( (uint8_t *)writeX_header)) {303 if (is_valid_writeX_buffer(sconn, (uint8_t *)writeX_header)) { 195 304 /* 196 305 * If the data offset is beyond what … … 202 311 if (doff > STANDARD_WRITE_AND_X_HEADER_SIZE) { 203 312 size_t drain = doff - STANDARD_WRITE_AND_X_HEADER_SIZE; 204 if (drain_socket(s mbd_server_fd(), drain) != drain) {313 if (drain_socket(sock, drain) != drain) { 205 314 smb_panic("receive_smb_raw_talloc_partial_read:" 206 315 " failed to drain pending bytes"); … … 257 366 if(toread > 0) { 258 367 status = read_packet_remainder( 259 fd, (*buffer) + 4 + STANDARD_WRITE_AND_X_HEADER_SIZE, 368 sock, 369 (*buffer) + 4 + STANDARD_WRITE_AND_X_HEADER_SIZE, 260 370 timeout, toread); 261 371 … … 271 381 } 272 382 273 static NTSTATUS receive_smb_raw_talloc(TALLOC_CTX *mem_ctx, int fd, 383 static NTSTATUS receive_smb_raw_talloc(TALLOC_CTX *mem_ctx, 384 struct smbd_server_connection *sconn, 385 int sock, 274 386 char **buffer, unsigned int timeout, 275 387 size_t *p_unread, size_t *plen) … … 282 394 *p_unread = 0; 283 395 284 status = read_smb_length_return_keepalive(fd, lenbuf, timeout, &len); 396 status = read_smb_length_return_keepalive(sock, lenbuf, timeout, 397 &len); 285 398 if (!NT_STATUS_IS_OK(status)) { 286 DEBUG(10, ("receive_smb_raw: %s\n", nt_errstr(status)));287 399 return status; 288 400 } … … 291 403 (smb_len_large(lenbuf) > /* Could be a UNIX large writeX. */ 292 404 (min_recv_size + STANDARD_WRITE_AND_X_HEADER_SIZE)) && 293 !srv_is_signing_active(smbd_server_conn)) { 405 !srv_is_signing_active(sconn) && 406 sconn->smb1.echo_handler.trusted_fde == NULL) { 294 407 295 408 return receive_smb_raw_talloc_partial_read( 296 mem_ctx, lenbuf, fd, buffer, timeout, p_unread, plen); 409 mem_ctx, lenbuf, sconn, sock, buffer, timeout, 410 p_unread, plen); 297 411 } 298 412 … … 315 429 memcpy(*buffer, lenbuf, sizeof(lenbuf)); 316 430 317 status = read_packet_remainder( fd, (*buffer)+4, timeout, len);431 status = read_packet_remainder(sock, (*buffer)+4, timeout, len); 318 432 if (!NT_STATUS_IS_OK(status)) { 319 433 return status; … … 324 438 } 325 439 326 static NTSTATUS receive_smb_talloc(TALLOC_CTX *mem_ctx, int fd, 440 static NTSTATUS receive_smb_talloc(TALLOC_CTX *mem_ctx, 441 struct smbd_server_connection *sconn, 442 int sock, 327 443 char **buffer, unsigned int timeout, 328 444 size_t *p_unread, bool *p_encrypted, 329 445 size_t *p_len, 330 uint32_t *seqnum) 446 uint32_t *seqnum, 447 bool trusted_channel) 331 448 { 332 449 size_t len = 0; … … 335 452 *p_encrypted = false; 336 453 337 status = receive_smb_raw_talloc(mem_ctx, fd, buffer, timeout,454 status = receive_smb_raw_talloc(mem_ctx, sconn, sock, buffer, timeout, 338 455 p_unread, &len); 339 456 if (!NT_STATUS_IS_OK(status)) { 457 DEBUG(1, ("read_smb_length_return_keepalive failed for " 458 "client %s read error = %s.\n", 459 sconn->client_id.addr, nt_errstr(status))); 340 460 return status; 341 461 } … … 353 473 354 474 /* Check the incoming SMB signature. */ 355 if (!srv_check_sign_mac(s mbd_server_conn, *buffer, seqnum)) {475 if (!srv_check_sign_mac(sconn, *buffer, seqnum, trusted_channel)) { 356 476 DEBUG(0, ("receive_smb: SMB Signature verification failed on " 357 477 "incoming packet!\n")); … … 367 487 */ 368 488 369 voidinit_smb_request(struct smb_request *req,370 const uint8 *inbuf,371 size_t unread_bytes,372 bool encrypted)373 { 374 struct smbd_server_connection *sconn = smbd_server_conn; 489 static bool init_smb_request(struct smb_request *req, 490 struct smbd_server_connection *sconn, 491 const uint8 *inbuf, 492 size_t unread_bytes, bool encrypted, 493 uint32_t seqnum) 494 { 375 495 size_t req_size = smb_len(inbuf) + 4; 376 496 /* Ensure we have at least smb_size bytes. */ … … 378 498 DEBUG(0,("init_smb_request: invalid request size %u\n", 379 499 (unsigned int)req_size )); 380 exit_server_cleanly("Invalid SMB request");500 return false; 381 501 } 382 502 req->cmd = CVAL(inbuf, smb_com); 383 503 req->flags2 = SVAL(inbuf, smb_flg2); 384 504 req->smbpid = SVAL(inbuf, smb_pid); 385 req->mid = SVAL(inbuf, smb_mid);386 req->seqnum = 0;505 req->mid = (uint64_t)SVAL(inbuf, smb_mid); 506 req->seqnum = seqnum; 387 507 req->vuid = SVAL(inbuf, smb_uid); 388 508 req->tid = SVAL(inbuf, smb_tid); … … 393 513 req->unread_bytes = unread_bytes; 394 514 req->encrypted = encrypted; 515 req->sconn = sconn; 395 516 req->conn = conn_find(sconn,req->tid); 396 517 req->chain_fsp = NULL; 397 518 req->chain_outbuf = NULL; 398 519 req->done = false; 520 req->smb2req = NULL; 399 521 smb_init_perfcount_data(&req->pcd); 400 522 … … 404 526 (unsigned int)req->wct, 405 527 (unsigned int)req_size)); 406 exit_server_cleanly("Invalid SMB request");528 return false; 407 529 } 408 530 /* Ensure bcc is correct. */ … … 413 535 (unsigned int)req->wct, 414 536 (unsigned int)req_size)); 415 exit_server_cleanly("Invalid SMB request");537 return false; 416 538 } 417 539 418 540 req->outbuf = NULL; 541 return true; 419 542 } 420 543 … … 432 555 struct pending_message_list); 433 556 TALLOC_CTX *mem_ctx = talloc_tos(); 434 uint 16_t mid =SVAL(msg->buf.data,smb_mid);557 uint64_t mid = (uint64_t)SVAL(msg->buf.data,smb_mid); 435 558 uint8_t *inbuf; 436 559 … … 444 567 /* We leave this message on the queue so the open code can 445 568 know this is a retry. */ 446 DEBUG(5,("smbd_deferred_open_timer: trigger mid % u.\n",447 (unsigned int)mid ));569 DEBUG(5,("smbd_deferred_open_timer: trigger mid %llu.\n", 570 (unsigned long long)mid )); 448 571 449 572 /* Mark the message as processed so this is not … … 456 579 457 580 /* If it's still there and was processed, remove it. */ 458 msg = get_ open_deferred_message(mid);581 msg = get_deferred_open_message_smb(mid); 459 582 if (msg && msg->processed) { 460 remove_deferred_open_ smb_message(mid);583 remove_deferred_open_message_smb(mid); 461 584 } 462 585 } … … 528 651 ****************************************************************************/ 529 652 530 void remove_deferred_open_ smb_message(uint16mid)653 void remove_deferred_open_message_smb(uint64_t mid) 531 654 { 532 655 struct pending_message_list *pml; 533 656 657 if (smbd_server_conn->using_smb2) { 658 remove_deferred_open_message_smb2(smbd_server_conn, mid); 659 return; 660 } 661 534 662 for (pml = deferred_open_queue; pml; pml = pml->next) { 535 if (mid == SVAL(pml->buf.data,smb_mid)) {536 DEBUG(10,("remove_deferred_open_ smb_message: "537 "deleting mid % u len %u\n",538 (unsigned int)mid,663 if (mid == (uint64_t)SVAL(pml->buf.data,smb_mid)) { 664 DEBUG(10,("remove_deferred_open_message_smb: " 665 "deleting mid %llu len %u\n", 666 (unsigned long long)mid, 539 667 (unsigned int)pml->buf.length )); 540 668 DLIST_REMOVE(deferred_open_queue, pml); … … 550 678 ****************************************************************************/ 551 679 552 void schedule_deferred_open_ smb_message(uint16mid)680 void schedule_deferred_open_message_smb(uint64_t mid) 553 681 { 554 682 struct pending_message_list *pml; 555 683 int i = 0; 556 684 685 if (smbd_server_conn->using_smb2) { 686 schedule_deferred_open_message_smb2(smbd_server_conn, mid); 687 return; 688 } 689 557 690 for (pml = deferred_open_queue; pml; pml = pml->next) { 558 uint16 msg_mid = SVAL(pml->buf.data,smb_mid); 559 560 DEBUG(10,("schedule_deferred_open_smb_message: [%d] msg_mid = %u\n", i++, 561 (unsigned int)msg_mid )); 691 uint64_t msg_mid = (uint64_t)SVAL(pml->buf.data,smb_mid); 692 693 DEBUG(10,("schedule_deferred_open_message_smb: [%d] " 694 "msg_mid = %llu\n", 695 i++, 696 (unsigned long long)msg_mid )); 562 697 563 698 if (mid == msg_mid) { … … 567 702 /* A processed message should not be 568 703 * rescheduled. */ 569 DEBUG(0,("schedule_deferred_open_ smb_message: LOGIC ERROR "570 "message mid % u was already processed\n",571 msg_mid ));704 DEBUG(0,("schedule_deferred_open_message_smb: LOGIC ERROR " 705 "message mid %llu was already processed\n", 706 (unsigned long long)msg_mid )); 572 707 continue; 573 708 } 574 709 575 DEBUG(10,("schedule_deferred_open_smb_message: scheduling mid %u\n", 576 mid )); 710 DEBUG(10,("schedule_deferred_open_message_smb: " 711 "scheduling mid %llu\n", 712 (unsigned long long)mid )); 577 713 578 714 te = event_add_timed(smbd_event_context(), … … 582 718 pml); 583 719 if (!te) { 584 DEBUG(10,("schedule_deferred_open_smb_message: " 585 "event_add_timed() failed, skipping mid %u\n", 586 mid )); 720 DEBUG(10,("schedule_deferred_open_message_smb: " 721 "event_add_timed() failed, " 722 "skipping mid %llu\n", 723 (unsigned long long)msg_mid )); 587 724 } 588 725 … … 594 731 } 595 732 596 DEBUG(10,("schedule_deferred_open_smb_message: failed to find message mid %u\n", 597 mid )); 733 DEBUG(10,("schedule_deferred_open_message_smb: failed to " 734 "find message mid %llu\n", 735 (unsigned long long)mid )); 598 736 } 599 737 … … 602 740 ****************************************************************************/ 603 741 604 bool open_was_deferred(uint 16mid)742 bool open_was_deferred(uint64_t mid) 605 743 { 606 744 struct pending_message_list *pml; 607 745 746 if (smbd_server_conn->using_smb2) { 747 return open_was_deferred_smb2(smbd_server_conn, mid); 748 } 749 608 750 for (pml = deferred_open_queue; pml; pml = pml->next) { 609 if ( SVAL(pml->buf.data,smb_mid) == mid && !pml->processed) {751 if (((uint64_t)SVAL(pml->buf.data,smb_mid)) == mid && !pml->processed) { 610 752 return True; 611 753 } … … 618 760 ****************************************************************************/ 619 761 620 st ruct pending_message_list *get_open_deferred_message(uint16mid)762 static struct pending_message_list *get_deferred_open_message_smb(uint64_t mid) 621 763 { 622 764 struct pending_message_list *pml; 623 765 624 766 for (pml = deferred_open_queue; pml; pml = pml->next) { 625 if ( SVAL(pml->buf.data,smb_mid) == mid) {767 if (((uint64_t)SVAL(pml->buf.data,smb_mid)) == mid) { 626 768 return pml; 627 769 } 628 770 } 629 771 return NULL; 772 } 773 774 /**************************************************************************** 775 Get the state data queued by this mid. 776 ****************************************************************************/ 777 778 bool get_deferred_open_message_state(struct smb_request *smbreq, 779 struct timeval *p_request_time, 780 void **pp_state) 781 { 782 struct pending_message_list *pml; 783 784 if (smbd_server_conn->using_smb2) { 785 return get_deferred_open_message_state_smb2(smbreq->smb2req, 786 p_request_time, 787 pp_state); 788 } 789 790 pml = get_deferred_open_message_smb(smbreq->mid); 791 if (!pml) { 792 return false; 793 } 794 if (p_request_time) { 795 *p_request_time = pml->request_time; 796 } 797 if (pp_state) { 798 *pp_state = (void *)pml->private_data.data; 799 } 800 return true; 630 801 } 631 802 … … 635 806 ****************************************************************************/ 636 807 637 bool push_deferred_ smb_message(struct smb_request *req,808 bool push_deferred_open_message_smb(struct smb_request *req, 638 809 struct timeval request_time, 639 810 struct timeval timeout, 811 struct file_id id, 640 812 char *private_data, size_t priv_len) 641 813 { 642 814 struct timeval end_time; 643 815 816 if (req->smb2req) { 817 return push_deferred_open_message_smb2(req->smb2req, 818 request_time, 819 timeout, 820 id, 821 private_data, 822 priv_len); 823 } 824 644 825 if (req->unread_bytes) { 645 DEBUG(0,("push_deferred_ smb_message: logic error ! "826 DEBUG(0,("push_deferred_open_message_smb: logic error ! " 646 827 "unread_bytes = %u\n", 647 828 (unsigned int)req->unread_bytes )); 648 smb_panic("push_deferred_ smb_message: "829 smb_panic("push_deferred_open_message_smb: " 649 830 "logic error unread_bytes != 0" ); 650 831 } … … 652 833 end_time = timeval_sum(&request_time, &timeout); 653 834 654 DEBUG(10,("push_deferred_open_smb_message: pushing message len %u mid %u " 655 "timeout time [%u.%06u]\n", 656 (unsigned int) smb_len(req->inbuf)+4, (unsigned int)req->mid, 657 (unsigned int)end_time.tv_sec, 658 (unsigned int)end_time.tv_usec)); 835 DEBUG(10,("push_deferred_open_message_smb: pushing message " 836 "len %u mid %llu timeout time [%u.%06u]\n", 837 (unsigned int) smb_len(req->inbuf)+4, 838 (unsigned long long)req->mid, 839 (unsigned int)end_time.tv_sec, 840 (unsigned int)end_time.tv_usec)); 659 841 660 842 return push_queued_message(req, request_time, end_time, … … 773 955 void *private_data) 774 956 { 957 struct messaging_context *msg_ctx = talloc_get_type_abort( 958 private_data, struct messaging_context); 775 959 change_to_root_user(); 776 960 DEBUG(1,("Reloading services after SIGHUP\n")); 777 reload_services(False); 778 } 779 780 void smbd_setup_sig_hup_handler(void) 961 reload_services(msg_ctx, smbd_server_conn->sock, False); 962 if (am_parent) { 963 pcap_cache_reload(ev, msg_ctx, &reload_pcap_change_notify); 964 } 965 } 966 967 void smbd_setup_sig_hup_handler(struct tevent_context *ev, 968 struct messaging_context *msg_ctx) 781 969 { 782 970 struct tevent_signal *se; 783 971 784 se = tevent_add_signal(smbd_event_context(), 785 smbd_event_context(), 786 SIGHUP, 0, 787 smbd_sig_hup_handler, 788 NULL); 972 se = tevent_add_signal(ev, ev, SIGHUP, 0, smbd_sig_hup_handler, 973 msg_ctx); 789 974 if (!se) { 790 975 exit_server("failed to setup SIGHUP handler"); … … 794 979 static NTSTATUS smbd_server_connection_loop_once(struct smbd_server_connection *conn) 795 980 { 796 fd_set r_fds, w_fds; 797 int selrtn; 798 struct timeval to; 799 int maxfd = 0; 800 801 to.tv_sec = SMBD_SELECT_TIMEOUT; 802 to.tv_usec = 0; 803 804 /* 805 * Setup the select fd sets. 806 */ 807 808 FD_ZERO(&r_fds); 809 FD_ZERO(&w_fds); 981 int timeout; 982 int num_pfds = 0; 983 int ret; 984 bool retry; 985 986 timeout = SMBD_SELECT_TIMEOUT * 1000; 810 987 811 988 /* … … 814 991 */ 815 992 816 { 817 struct timeval now; 818 GetTimeOfDay(&now); 819 820 event_add_to_select_args(smbd_event_context(), &now, 821 &r_fds, &w_fds, &to, &maxfd); 822 } 993 event_add_to_poll_args(smbd_event_context(), conn, 994 &conn->pfds, &num_pfds, &timeout); 823 995 824 996 /* Process a signal and timed events now... */ 825 if (run_events (smbd_event_context(), 0, NULL, NULL)) {997 if (run_events_poll(smbd_event_context(), 0, NULL, 0)) { 826 998 return NT_STATUS_RETRY; 827 999 } … … 831 1003 START_PROFILE(smbd_idle); 832 1004 833 selrtn = sys_select(maxfd+1,&r_fds,&w_fds,NULL,&to);1005 ret = sys_poll(conn->pfds, num_pfds, timeout); 834 1006 sav = errno; 835 1007 … … 838 1010 } 839 1011 840 if (selrtn == -1 && errno != EINTR) { 1012 if (ret == -1) { 1013 if (errno == EINTR) { 1014 return NT_STATUS_RETRY; 1015 } 841 1016 return map_nt_error_from_unix(errno); 842 1017 } 843 1018 844 if (run_events(smbd_event_context(), selrtn, &r_fds, &w_fds)) { 1019 retry = run_events_poll(smbd_event_context(), ret, conn->pfds, 1020 num_pfds); 1021 if (retry) { 845 1022 return NT_STATUS_RETRY; 846 1023 } 847 1024 848 /* Check if error */849 if (selrtn == -1) {850 /* something is wrong. Maybe the socket is dead? */851 return map_nt_error_from_unix(errno);852 }853 854 1025 /* Did we timeout ? */ 855 if ( selrtn== 0) {1026 if (ret == 0) { 856 1027 return NT_STATUS_RETRY; 857 1028 } … … 866 1037 */ 867 1038 868 NTSTATUS allow_new_trans(struct trans_state *list, int mid)1039 NTSTATUS allow_new_trans(struct trans_state *list, uint64_t mid) 869 1040 { 870 1041 int count = 0; … … 1268 1439 uint16 session_tag; 1269 1440 connection_struct *conn = NULL; 1270 struct smbd_server_connection *sconn = smbd_server_conn;1441 struct smbd_server_connection *sconn = req->sconn; 1271 1442 1272 1443 errno = 0; … … 1320 1491 if (vuser) { 1321 1492 set_current_user_info( 1322 vuser->server_info->sanitized_username, 1323 vuser->server_info->unix_name, 1324 pdb_get_domain(vuser->server_info 1325 ->sam_account)); 1493 vuser->session_info->sanitized_username, 1494 vuser->session_info->unix_name, 1495 vuser->session_info->info3->base.domain.string); 1326 1496 } 1327 1497 } … … 1347 1517 if (!change_to_user(conn,session_tag)) { 1348 1518 DEBUG(0, ("Error: Could not change to user. Removing " 1349 "deferred open, mid=%d.\n", req->mid)); 1519 "deferred open, mid=%llu.\n", 1520 (unsigned long long)req->mid)); 1350 1521 reply_force_doserror(req, ERRSRV, ERRbaduid); 1351 1522 return conn; … … 1396 1567 if ((flags & AS_GUEST) 1397 1568 && (!change_to_guest() || 1398 !check_access(smbd_server_fd(), lp_hostsallow(-1), 1399 lp_hostsdeny(-1)))) { 1569 !allow_access(lp_hostsdeny(-1), lp_hostsallow(-1), 1570 sconn->client_id.name, 1571 sconn->client_id.addr))) { 1400 1572 reply_nterror(req, NT_STATUS_ACCESS_DENIED); 1401 1573 return conn; … … 1410 1582 ****************************************************************************/ 1411 1583 1412 static void construct_reply(char *inbuf, int size, size_t unread_bytes, 1584 static void construct_reply(struct smbd_server_connection *sconn, 1585 char *inbuf, int size, size_t unread_bytes, 1413 1586 uint32_t seqnum, bool encrypted, 1414 1587 struct smb_perfcount_data *deferred_pcd) … … 1421 1594 } 1422 1595 1423 init_smb_request(req, (uint8 *)inbuf, unread_bytes, encrypted); 1596 if (!init_smb_request(req, sconn, (uint8 *)inbuf, unread_bytes, 1597 encrypted, seqnum)) { 1598 exit_server_cleanly("Invalid SMB request"); 1599 } 1600 1424 1601 req->inbuf = (uint8_t *)talloc_move(req, &inbuf); 1425 req->seqnum = seqnum;1426 1602 1427 1603 /* we popped this message off the queue - keep original perf data */ … … 1438 1614 if (req->unread_bytes) { 1439 1615 /* writeX failed. drain socket. */ 1440 if (drain_socket( smbd_server_fd(), req->unread_bytes) !=1616 if (drain_socket(req->sconn->sock, req->unread_bytes) != 1441 1617 req->unread_bytes) { 1442 1618 smb_panic("failed to drain pending bytes"); … … 1458 1634 } 1459 1635 1460 if (!srv_send_smb( smbd_server_fd(),1636 if (!srv_send_smb(req->sconn, 1461 1637 (char *)req->outbuf, 1462 1638 true, req->seqnum+1, … … 1474 1650 Process an smb from the client 1475 1651 ****************************************************************************/ 1476 static void process_smb(struct smbd_server_connection * conn,1652 static void process_smb(struct smbd_server_connection *sconn, 1477 1653 uint8_t *inbuf, size_t nread, size_t unread_bytes, 1478 1654 uint32_t seqnum, bool encrypted, … … 1485 1661 DEBUG( 6, ( "got message type 0x%x of len 0x%x\n", msg_type, 1486 1662 smb_len(inbuf) ) ); 1487 DEBUG( 3, ( "Transaction %d of length %d (%u toread)\n", trans_num, 1488 (int)nread, 1489 (unsigned int)unread_bytes )); 1663 DEBUG(3, ("Transaction %d of length %d (%u toread)\n", 1664 sconn->trans_num, (int)nread, (unsigned int)unread_bytes)); 1490 1665 1491 1666 if (msg_type != 0) { … … 1493 1668 * NetBIOS session request, keepalive, etc. 1494 1669 */ 1495 reply_special( (char *)inbuf, nread);1670 reply_special(sconn, (char *)inbuf, nread); 1496 1671 goto done; 1497 1672 } 1498 1673 1499 if (smbd_server_conn->allow_smb2) { 1674 if (sconn->using_smb2) { 1675 /* At this point we're not really using smb2, 1676 * we make the decision here.. */ 1500 1677 if (smbd_is_smb2_header(inbuf, nread)) { 1501 smbd_smb2_first_negprot(s mbd_server_conn, inbuf, nread);1678 smbd_smb2_first_negprot(sconn, inbuf, nread); 1502 1679 return; 1503 } 1504 smbd_server_conn->allow_smb2 = false; 1680 } else if (nread >= smb_size && valid_smb_header(inbuf) 1681 && CVAL(inbuf, smb_com) != 0x72) { 1682 /* This is a non-negprot SMB1 packet. 1683 Disable SMB2 from now on. */ 1684 sconn->using_smb2 = false; 1685 } 1505 1686 } 1506 1687 1507 1688 show_msg((char *)inbuf); 1508 1689 1509 construct_reply((char *)inbuf,nread,unread_bytes,seqnum,encrypted,deferred_pcd); 1510 trans_num++; 1690 construct_reply(sconn, (char *)inbuf, nread, unread_bytes, seqnum, 1691 encrypted, deferred_pcd); 1692 sconn->trans_num++; 1511 1693 1512 1694 done: 1513 conn->smb1.num_requests++;1695 sconn->num_requests++; 1514 1696 1515 1697 /* The timeout_processing function isn't run nearly … … 1520 1702 tradeoff of performance vs log file size overrun. */ 1521 1703 1522 if (( conn->smb1.num_requests % 50) == 0 &&1704 if ((sconn->num_requests % 50) == 0 && 1523 1705 need_to_check_log_size()) { 1524 1706 change_to_root_user(); … … 1559 1741 { 1560 1742 srv_set_message(outbuf,0,0,false); 1561 1743 1562 1744 SCVAL(outbuf, smb_com, req->cmd); 1563 1745 SIVAL(outbuf,smb_rcls,0); … … 1891 2073 talloc_get_size(req->chain_outbuf) - 4); 1892 2074 1893 if (!srv_send_smb( smbd_server_fd(), (char *)req->chain_outbuf,2075 if (!srv_send_smb(req->sconn, (char *)req->chain_outbuf, 1894 2076 true, req->seqnum+1, 1895 2077 IS_CONN_ENCRYPTED(req->conn) … … 2031 2213 show_msg((char *)(req->chain_outbuf)); 2032 2214 2033 if (!srv_send_smb( smbd_server_fd(), (char *)req->chain_outbuf,2215 if (!srv_send_smb(req->sconn, (char *)req->chain_outbuf, 2034 2216 true, req->seqnum+1, 2035 2217 IS_CONN_ENCRYPTED(req->conn)||req->encrypted, 2036 2218 &req->pcd)) { 2037 exit_server_cleanly("c onstruct_reply: srv_send_smb failed.");2219 exit_server_cleanly("chain_reply: srv_send_smb failed."); 2038 2220 } 2039 2221 TALLOC_FREE(req->chain_outbuf); … … 2045 2227 ****************************************************************************/ 2046 2228 2047 void check_reload(time_t t) 2048 { 2049 time_t printcap_cache_time = (time_t)lp_printcap_cache_time(); 2050 2051 if(last_smb_conf_reload_time == 0) { 2229 static void check_reload(struct smbd_server_connection *sconn, time_t t) 2230 { 2231 2232 if (last_smb_conf_reload_time == 0) { 2052 2233 last_smb_conf_reload_time = t; 2053 /* Our printing subsystem might not be ready at smbd start up.2054 Then no printer is available till the first printers check2055 is performed. A lower initial interval circumvents this. */2056 if ( printcap_cache_time > 60 )2057 last_printer_reload_time = t - printcap_cache_time + 60;2058 else2059 last_printer_reload_time = t;2060 }2061 2062 if (mypid != getpid()) { /* First time or fork happened meanwhile */2063 /* randomize over 60 second the printcap reload to avoid all2064 * process hitting cupsd at the same time */2065 int time_range = 60;2066 2067 last_printer_reload_time += random() % time_range;2068 mypid = getpid();2069 2234 } 2070 2235 2071 2236 if (t >= last_smb_conf_reload_time+SMBD_RELOAD_CHECK) { 2072 reload_services( True);2237 reload_services(sconn->msg_ctx, sconn->sock, True); 2073 2238 last_smb_conf_reload_time = t; 2074 2239 } 2075 2076 /* 'printcap cache time = 0' disable the feature */ 2077 2078 if ( printcap_cache_time != 0 ) 2079 { 2080 /* see if it's time to reload or if the clock has been set back */ 2081 2082 if ( (t >= last_printer_reload_time+printcap_cache_time) 2083 || (t-last_printer_reload_time < 0) ) 2084 { 2085 DEBUG( 3,( "Printcap cache time expired.\n")); 2086 pcap_cache_reload(&reload_printers); 2087 last_printer_reload_time = t; 2088 } 2089 } 2240 } 2241 2242 static bool fd_is_readable(int fd) 2243 { 2244 int ret, revents; 2245 2246 ret = poll_one_fd(fd, POLLIN|POLLHUP, 0, &revents); 2247 2248 return ((ret > 0) && ((revents & (POLLIN|POLLHUP|POLLERR)) != 0)); 2249 2090 2250 } 2091 2251 … … 2095 2255 } 2096 2256 2097 static void smbd_server_connection_read_handler(struct smbd_server_connection *conn) 2257 static void smbd_server_connection_read_handler( 2258 struct smbd_server_connection *conn, int fd) 2098 2259 { 2099 2260 uint8_t *inbuf = NULL; … … 2105 2266 uint32_t seqnum; 2106 2267 2107 /* TODO: make this completely nonblocking */ 2108 2109 status = receive_smb_talloc(mem_ctx, smbd_server_fd(), 2110 (char **)(void *)&inbuf, 2111 0, /* timeout */ 2112 &unread_bytes, 2113 &encrypted, 2114 &inbuf_len, &seqnum); 2268 bool from_client = (conn->sock == fd); 2269 2270 if (from_client) { 2271 smbd_lock_socket(conn); 2272 2273 if (lp_async_smb_echo_handler() && !fd_is_readable(fd)) { 2274 DEBUG(10,("the echo listener was faster\n")); 2275 smbd_unlock_socket(conn); 2276 return; 2277 } 2278 2279 /* TODO: make this completely nonblocking */ 2280 status = receive_smb_talloc(mem_ctx, conn, fd, 2281 (char **)(void *)&inbuf, 2282 0, /* timeout */ 2283 &unread_bytes, 2284 &encrypted, 2285 &inbuf_len, &seqnum, 2286 false /* trusted channel */); 2287 smbd_unlock_socket(conn); 2288 } else { 2289 /* TODO: make this completely nonblocking */ 2290 status = receive_smb_talloc(mem_ctx, conn, fd, 2291 (char **)(void *)&inbuf, 2292 0, /* timeout */ 2293 &unread_bytes, 2294 &encrypted, 2295 &inbuf_len, &seqnum, 2296 true /* trusted channel */); 2297 } 2298 2115 2299 if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) { 2116 2300 goto process; … … 2138 2322 if (flags & EVENT_FD_WRITE) { 2139 2323 smbd_server_connection_write_handler(conn); 2140 } else if (flags & EVENT_FD_READ) { 2141 smbd_server_connection_read_handler(conn); 2142 } 2143 } 2144 2324 return; 2325 } 2326 if (flags & EVENT_FD_READ) { 2327 smbd_server_connection_read_handler(conn, conn->sock); 2328 return; 2329 } 2330 } 2331 2332 static void smbd_server_echo_handler(struct event_context *ev, 2333 struct fd_event *fde, 2334 uint16_t flags, 2335 void *private_data) 2336 { 2337 struct smbd_server_connection *conn = talloc_get_type(private_data, 2338 struct smbd_server_connection); 2339 2340 if (flags & EVENT_FD_WRITE) { 2341 smbd_server_connection_write_handler(conn); 2342 return; 2343 } 2344 if (flags & EVENT_FD_READ) { 2345 smbd_server_connection_read_handler( 2346 conn, conn->smb1.echo_handler.trusted_fd); 2347 return; 2348 } 2349 } 2145 2350 2146 2351 /**************************************************************************** … … 2149 2354 static void release_ip(const char *ip, void *priv) 2150 2355 { 2151 char addr[INET6_ADDRSTRLEN]; 2152 char *p = addr; 2153 2154 client_socket_addr(get_client_fd(),addr,sizeof(addr)); 2356 const char *addr = (const char *)priv; 2357 const char *p = addr; 2155 2358 2156 2359 if (strncmp("::ffff:", addr, 7) == 0) { 2157 2360 p = addr + 7; 2158 2361 } 2362 2363 DEBUG(10, ("Got release IP message for %s, " 2364 "our address is %s\n", ip, p)); 2159 2365 2160 2366 if ((strcmp(p, ip) == 0) || ((p != addr) && strcmp(addr, ip) == 0)) { … … 2175 2381 uint32_t msg_type, struct server_id server_id, DATA_BLOB *data) 2176 2382 { 2177 release_ip((char *)data->data, NULL); 2383 struct smbd_server_connection *sconn = talloc_get_type_abort( 2384 private_data, struct smbd_server_connection); 2385 2386 release_ip((char *)data->data, sconn->client_id.addr); 2178 2387 } 2179 2388 2180 2389 #ifdef CLUSTER_SUPPORT 2181 static int client_get_tcp_info( struct sockaddr_storage *server,2390 static int client_get_tcp_info(int sock, struct sockaddr_storage *server, 2182 2391 struct sockaddr_storage *client) 2183 2392 { 2184 2393 socklen_t length; 2185 if (server_fd == -1) { 2394 length = sizeof(*server); 2395 if (getsockname(sock, (struct sockaddr *)server, &length) != 0) { 2186 2396 return -1; 2187 2397 } 2188 length = sizeof(*server);2189 if (getsockname(server_fd, (struct sockaddr *)server, &length) != 0) {2190 return -1;2191 }2192 2398 length = sizeof(*client); 2193 if (getpeername(s erver_fd, (struct sockaddr *)client, &length) != 0) {2399 if (getpeername(sock, (struct sockaddr *)client, &length) != 0) { 2194 2400 return -1; 2195 2401 } … … 2203 2409 static bool keepalive_fn(const struct timeval *now, void *private_data) 2204 2410 { 2205 if (!send_keepalive(smbd_server_fd())) { 2206 DEBUG( 2, ( "Keepalive failed - exiting.\n" ) ); 2411 struct smbd_server_connection *sconn = smbd_server_conn; 2412 bool ret; 2413 2414 if (sconn->using_smb2) { 2415 /* Don't do keepalives on an SMB2 connection. */ 2416 return false; 2417 } 2418 2419 smbd_lock_socket(smbd_server_conn); 2420 ret = send_keepalive(sconn->sock); 2421 smbd_unlock_socket(smbd_server_conn); 2422 2423 if (!ret) { 2424 char addr[INET6_ADDRSTRLEN]; 2425 /* 2426 * Try and give an error message saying what 2427 * client failed. 2428 */ 2429 DEBUG(0, ("send_keepalive failed for client %s. " 2430 "Error %s - exiting\n", 2431 get_peer_addr(sconn->sock, addr, sizeof(addr)), 2432 strerror(errno))); 2207 2433 return False; 2208 2434 } … … 2215 2441 static bool deadtime_fn(const struct timeval *now, void *private_data) 2216 2442 { 2217 struct smbd_server_connection *sconn = smbd_server_conn; 2443 struct smbd_server_connection *sconn = 2444 (struct smbd_server_connection *)private_data; 2445 2218 2446 if ((conn_num_open(sconn) == 0) 2219 2447 || (conn_idle_all(sconn, now->tv_sec))) { 2220 2448 DEBUG( 2, ( "Closing idle connection\n" ) ); 2221 messaging_send(smbd_messaging_context(), procid_self(), 2449 messaging_send(sconn->msg_ctx, 2450 messaging_server_id(sconn->msg_ctx), 2222 2451 MSG_SHUTDOWN, &data_blob_null); 2223 2452 return False; … … 2233 2462 static bool housekeeping_fn(const struct timeval *now, void *private_data) 2234 2463 { 2464 struct smbd_server_connection *sconn = talloc_get_type_abort( 2465 private_data, struct smbd_server_connection); 2466 2467 DEBUG(5, ("housekeeping\n")); 2468 2235 2469 change_to_root_user(); 2236 2470 2237 2471 /* update printer queue caches if necessary */ 2238 update_monitored_printq_cache( );2472 update_monitored_printq_cache(sconn->msg_ctx); 2239 2473 2240 2474 /* check if we need to reload services */ 2241 check_reload( time(NULL));2475 check_reload(sconn, time_mono(NULL)); 2242 2476 2243 2477 /* Change machine password if neccessary. */ … … 2252 2486 } 2253 2487 2488 static int create_unlink_tmp(const char *dir) 2489 { 2490 char *fname; 2491 int fd; 2492 2493 fname = talloc_asprintf(talloc_tos(), "%s/listenerlock_XXXXXX", dir); 2494 if (fname == NULL) { 2495 errno = ENOMEM; 2496 return -1; 2497 } 2498 fd = mkstemp(fname); 2499 if (fd == -1) { 2500 TALLOC_FREE(fname); 2501 return -1; 2502 } 2503 if (unlink(fname) == -1) { 2504 int sys_errno = errno; 2505 close(fd); 2506 TALLOC_FREE(fname); 2507 errno = sys_errno; 2508 return -1; 2509 } 2510 TALLOC_FREE(fname); 2511 return fd; 2512 } 2513 2514 struct smbd_echo_state { 2515 struct tevent_context *ev; 2516 struct iovec *pending; 2517 struct smbd_server_connection *sconn; 2518 int parent_pipe; 2519 2520 struct tevent_fd *parent_fde; 2521 2522 struct tevent_fd *read_fde; 2523 struct tevent_req *write_req; 2524 }; 2525 2526 static void smbd_echo_writer_done(struct tevent_req *req); 2527 2528 static void smbd_echo_activate_writer(struct smbd_echo_state *state) 2529 { 2530 int num_pending; 2531 2532 if (state->write_req != NULL) { 2533 return; 2534 } 2535 2536 num_pending = talloc_array_length(state->pending); 2537 if (num_pending == 0) { 2538 return; 2539 } 2540 2541 state->write_req = writev_send(state, state->ev, NULL, 2542 state->parent_pipe, false, 2543 state->pending, num_pending); 2544 if (state->write_req == NULL) { 2545 DEBUG(1, ("writev_send failed\n")); 2546 exit(1); 2547 } 2548 2549 talloc_steal(state->write_req, state->pending); 2550 state->pending = NULL; 2551 2552 tevent_req_set_callback(state->write_req, smbd_echo_writer_done, 2553 state); 2554 } 2555 2556 static void smbd_echo_writer_done(struct tevent_req *req) 2557 { 2558 struct smbd_echo_state *state = tevent_req_callback_data( 2559 req, struct smbd_echo_state); 2560 ssize_t written; 2561 int err; 2562 2563 written = writev_recv(req, &err); 2564 TALLOC_FREE(req); 2565 state->write_req = NULL; 2566 if (written == -1) { 2567 DEBUG(1, ("writev to parent failed: %s\n", strerror(err))); 2568 exit(1); 2569 } 2570 DEBUG(10,("echo_handler[%d]: forwarded pdu to main\n", (int)sys_getpid())); 2571 smbd_echo_activate_writer(state); 2572 } 2573 2574 static bool smbd_echo_reply(uint8_t *inbuf, size_t inbuf_len, 2575 uint32_t seqnum) 2576 { 2577 struct smb_request req; 2578 uint16_t num_replies; 2579 size_t out_len; 2580 char *outbuf; 2581 bool ok; 2582 2583 if ((inbuf_len == 4) && (CVAL(inbuf, 0) == SMBkeepalive)) { 2584 DEBUG(10, ("Got netbios keepalive\n")); 2585 /* 2586 * Just swallow it 2587 */ 2588 return true; 2589 } 2590 2591 if (inbuf_len < smb_size) { 2592 DEBUG(10, ("Got short packet: %d bytes\n", (int)inbuf_len)); 2593 return false; 2594 } 2595 if (!valid_smb_header(inbuf)) { 2596 DEBUG(10, ("Got invalid SMB header\n")); 2597 return false; 2598 } 2599 2600 if (!init_smb_request(&req, smbd_server_conn, inbuf, 0, false, 2601 seqnum)) { 2602 return false; 2603 } 2604 req.inbuf = inbuf; 2605 2606 DEBUG(10, ("smbecho handler got cmd %d (%s)\n", (int)req.cmd, 2607 smb_messages[req.cmd].name 2608 ? smb_messages[req.cmd].name : "unknown")); 2609 2610 if (req.cmd != SMBecho) { 2611 return false; 2612 } 2613 if (req.wct < 1) { 2614 return false; 2615 } 2616 2617 num_replies = SVAL(req.vwv+0, 0); 2618 if (num_replies != 1) { 2619 /* Not a Windows "Hey, you're still there?" request */ 2620 return false; 2621 } 2622 2623 if (!create_outbuf(talloc_tos(), &req, (char *)req.inbuf, &outbuf, 2624 1, req.buflen)) { 2625 DEBUG(10, ("create_outbuf failed\n")); 2626 return false; 2627 } 2628 req.outbuf = (uint8_t *)outbuf; 2629 2630 SSVAL(req.outbuf, smb_vwv0, num_replies); 2631 2632 if (req.buflen > 0) { 2633 memcpy(smb_buf(req.outbuf), req.buf, req.buflen); 2634 } 2635 2636 out_len = smb_len(req.outbuf) + 4; 2637 2638 ok = srv_send_smb(req.sconn, 2639 (char *)outbuf, 2640 true, seqnum+1, 2641 false, &req.pcd); 2642 TALLOC_FREE(outbuf); 2643 if (!ok) { 2644 exit(1); 2645 } 2646 2647 return true; 2648 } 2649 2650 static void smbd_echo_exit(struct tevent_context *ev, 2651 struct tevent_fd *fde, uint16_t flags, 2652 void *private_data) 2653 { 2654 DEBUG(2, ("smbd_echo_exit: lost connection to parent\n")); 2655 exit(0); 2656 } 2657 2658 static void smbd_echo_reader(struct tevent_context *ev, 2659 struct tevent_fd *fde, uint16_t flags, 2660 void *private_data) 2661 { 2662 struct smbd_echo_state *state = talloc_get_type_abort( 2663 private_data, struct smbd_echo_state); 2664 struct smbd_server_connection *sconn = state->sconn; 2665 size_t unread, num_pending; 2666 NTSTATUS status; 2667 struct iovec *tmp; 2668 size_t iov_len; 2669 uint32_t seqnum = 0; 2670 bool reply; 2671 bool ok; 2672 bool encrypted = false; 2673 2674 smb_msleep(1000); 2675 2676 ok = smbd_lock_socket_internal(sconn); 2677 if (!ok) { 2678 DEBUG(0, ("%s: failed to lock socket\n", 2679 __location__)); 2680 exit(1); 2681 } 2682 2683 if (!fd_is_readable(sconn->sock)) { 2684 DEBUG(10,("echo_handler[%d] the parent smbd was faster\n", 2685 (int)sys_getpid())); 2686 ok = smbd_unlock_socket_internal(sconn); 2687 if (!ok) { 2688 DEBUG(1, ("%s: failed to unlock socket in\n", 2689 __location__)); 2690 exit(1); 2691 } 2692 return; 2693 } 2694 2695 num_pending = talloc_array_length(state->pending); 2696 tmp = talloc_realloc(state, state->pending, struct iovec, 2697 num_pending+1); 2698 if (tmp == NULL) { 2699 DEBUG(1, ("talloc_realloc failed\n")); 2700 exit(1); 2701 } 2702 state->pending = tmp; 2703 2704 DEBUG(10,("echo_handler[%d]: reading pdu\n", (int)sys_getpid())); 2705 2706 status = receive_smb_talloc(state->pending, sconn, sconn->sock, 2707 (char **)(void *)&state->pending[num_pending].iov_base, 2708 0 /* timeout */, 2709 &unread, 2710 &encrypted, 2711 &iov_len, 2712 &seqnum, 2713 false /* trusted_channel*/); 2714 if (!NT_STATUS_IS_OK(status)) { 2715 DEBUG(1, ("echo_handler[%d]: receive_smb_raw_talloc failed: %s\n", 2716 (int)sys_getpid(), nt_errstr(status))); 2717 exit(1); 2718 } 2719 state->pending[num_pending].iov_len = iov_len; 2720 2721 ok = smbd_unlock_socket_internal(sconn); 2722 if (!ok) { 2723 DEBUG(1, ("%s: failed to unlock socket in\n", 2724 __location__)); 2725 exit(1); 2726 } 2727 2728 reply = smbd_echo_reply((uint8_t *)state->pending[num_pending].iov_base, 2729 state->pending[num_pending].iov_len, 2730 seqnum); 2731 if (reply) { 2732 DEBUG(10,("echo_handler[%d]: replied to client\n", (int)sys_getpid())); 2733 /* no check, shrinking by some bytes does not fail */ 2734 state->pending = talloc_realloc(state, state->pending, 2735 struct iovec, 2736 num_pending); 2737 return; 2738 } 2739 2740 if (state->pending[num_pending].iov_len >= smb_size) { 2741 /* 2742 * place the seqnum in the packet so that the main process 2743 * can reply with signing 2744 */ 2745 SIVAL((uint8_t *)state->pending[num_pending].iov_base, 2746 smb_ss_field, seqnum); 2747 SIVAL((uint8_t *)state->pending[num_pending].iov_base, 2748 smb_ss_field+4, NT_STATUS_V(NT_STATUS_OK)); 2749 } 2750 2751 DEBUG(10,("echo_handler[%d]: forward to main\n", (int)sys_getpid())); 2752 smbd_echo_activate_writer(state); 2753 } 2754 2755 static void smbd_echo_loop(struct smbd_server_connection *sconn, 2756 int parent_pipe) 2757 { 2758 struct smbd_echo_state *state; 2759 2760 state = talloc_zero(sconn, struct smbd_echo_state); 2761 if (state == NULL) { 2762 DEBUG(1, ("talloc failed\n")); 2763 return; 2764 } 2765 state->sconn = sconn; 2766 state->parent_pipe = parent_pipe; 2767 state->ev = s3_tevent_context_init(state); 2768 if (state->ev == NULL) { 2769 DEBUG(1, ("tevent_context_init failed\n")); 2770 TALLOC_FREE(state); 2771 return; 2772 } 2773 state->parent_fde = tevent_add_fd(state->ev, state, parent_pipe, 2774 TEVENT_FD_READ, smbd_echo_exit, 2775 state); 2776 if (state->parent_fde == NULL) { 2777 DEBUG(1, ("tevent_add_fd failed\n")); 2778 TALLOC_FREE(state); 2779 return; 2780 } 2781 state->read_fde = tevent_add_fd(state->ev, state, sconn->sock, 2782 TEVENT_FD_READ, smbd_echo_reader, 2783 state); 2784 if (state->read_fde == NULL) { 2785 DEBUG(1, ("tevent_add_fd failed\n")); 2786 TALLOC_FREE(state); 2787 return; 2788 } 2789 2790 while (true) { 2791 if (tevent_loop_once(state->ev) == -1) { 2792 DEBUG(1, ("tevent_loop_once failed: %s\n", 2793 strerror(errno))); 2794 break; 2795 } 2796 } 2797 TALLOC_FREE(state); 2798 } 2799 2800 /* 2801 * Handle SMBecho requests in a forked child process 2802 */ 2803 static bool fork_echo_handler(struct smbd_server_connection *sconn) 2804 { 2805 int listener_pipe[2]; 2806 int res; 2807 pid_t child; 2808 2809 res = pipe(listener_pipe); 2810 if (res == -1) { 2811 DEBUG(1, ("pipe() failed: %s\n", strerror(errno))); 2812 return false; 2813 } 2814 sconn->smb1.echo_handler.socket_lock_fd = create_unlink_tmp(lp_lockdir()); 2815 if (sconn->smb1.echo_handler.socket_lock_fd == -1) { 2816 DEBUG(1, ("Could not create lock fd: %s\n", strerror(errno))); 2817 goto fail; 2818 } 2819 2820 child = sys_fork(); 2821 if (child == 0) { 2822 NTSTATUS status; 2823 2824 close(listener_pipe[0]); 2825 set_blocking(listener_pipe[1], false); 2826 2827 status = reinit_after_fork(sconn->msg_ctx, 2828 smbd_event_context(), 2829 procid_self(), false); 2830 if (!NT_STATUS_IS_OK(status)) { 2831 DEBUG(1, ("reinit_after_fork failed: %s\n", 2832 nt_errstr(status))); 2833 exit(1); 2834 } 2835 smbd_echo_loop(sconn, listener_pipe[1]); 2836 exit(0); 2837 } 2838 close(listener_pipe[1]); 2839 listener_pipe[1] = -1; 2840 sconn->smb1.echo_handler.trusted_fd = listener_pipe[0]; 2841 2842 DEBUG(10,("fork_echo_handler: main[%d] echo_child[%d]\n", (int)sys_getpid(), child)); 2843 2844 /* 2845 * Without smb signing this is the same as the normal smbd 2846 * listener. This needs to change once signing comes in. 2847 */ 2848 sconn->smb1.echo_handler.trusted_fde = event_add_fd(smbd_event_context(), 2849 sconn, 2850 sconn->smb1.echo_handler.trusted_fd, 2851 EVENT_FD_READ, 2852 smbd_server_echo_handler, 2853 sconn); 2854 if (sconn->smb1.echo_handler.trusted_fde == NULL) { 2855 DEBUG(1, ("event_add_fd failed\n")); 2856 goto fail; 2857 } 2858 2859 return true; 2860 2861 fail: 2862 if (listener_pipe[0] != -1) { 2863 close(listener_pipe[0]); 2864 } 2865 if (listener_pipe[1] != -1) { 2866 close(listener_pipe[1]); 2867 } 2868 sconn->smb1.echo_handler.trusted_fd = -1; 2869 if (sconn->smb1.echo_handler.socket_lock_fd != -1) { 2870 close(sconn->smb1.echo_handler.socket_lock_fd); 2871 } 2872 sconn->smb1.echo_handler.trusted_fd = -1; 2873 sconn->smb1.echo_handler.socket_lock_fd = -1; 2874 return false; 2875 } 2876 2877 #if CLUSTER_SUPPORT 2878 2879 static NTSTATUS smbd_register_ips(struct smbd_server_connection *sconn, 2880 struct sockaddr_storage *srv, 2881 struct sockaddr_storage *clnt) 2882 { 2883 struct ctdbd_connection *cconn; 2884 char tmp_addr[INET6_ADDRSTRLEN]; 2885 char *addr; 2886 2887 cconn = messaging_ctdbd_connection(); 2888 if (cconn == NULL) { 2889 return NT_STATUS_NO_MEMORY; 2890 } 2891 2892 client_socket_addr(sconn->sock, tmp_addr, sizeof(tmp_addr)); 2893 addr = talloc_strdup(cconn, tmp_addr); 2894 if (addr == NULL) { 2895 return NT_STATUS_NO_MEMORY; 2896 } 2897 return ctdbd_register_ips(cconn, srv, clnt, release_ip, addr); 2898 } 2899 2900 #endif 2901 2254 2902 /**************************************************************************** 2255 2903 Process commands from the client 2256 2904 ****************************************************************************/ 2257 2905 2258 void smbd_process( void)2906 void smbd_process(struct smbd_server_connection *sconn) 2259 2907 { 2260 2908 TALLOC_CTX *frame = talloc_stackframe(); 2261 char remaddr[INET6_ADDRSTRLEN]; 2909 struct sockaddr_storage ss; 2910 struct sockaddr *sa = NULL; 2911 socklen_t sa_socklen; 2912 struct tsocket_address *local_address = NULL; 2913 struct tsocket_address *remote_address = NULL; 2914 const char *remaddr = NULL; 2915 int ret; 2262 2916 2263 2917 if (lp_maxprotocol() == PROTOCOL_SMB2 && 2264 lp_security() != SEC_SHARE) { 2265 smbd_server_conn->allow_smb2 = true; 2918 !lp_async_smb_echo_handler()) { 2919 /* 2920 * We're not making the decision here, 2921 * we're just allowing the client 2922 * to decide between SMB1 and SMB2 2923 * with the first negprot 2924 * packet. 2925 */ 2926 sconn->using_smb2 = true; 2266 2927 } 2267 2928 2268 2929 /* Ensure child is set to blocking mode */ 2269 set_blocking(smbd_server_fd(),True); 2270 2271 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE"); 2272 set_socket_options(smbd_server_fd(), lp_socket_options()); 2930 set_blocking(sconn->sock,True); 2931 2932 set_socket_options(sconn->sock, "SO_KEEPALIVE"); 2933 set_socket_options(sconn->sock, lp_socket_options()); 2934 2935 sa = (struct sockaddr *)(void *)&ss; 2936 sa_socklen = sizeof(ss); 2937 ret = getpeername(sconn->sock, sa, &sa_socklen); 2938 if (ret != 0) { 2939 int level = (errno == ENOTCONN)?2:0; 2940 DEBUG(level,("getpeername() failed - %s\n", strerror(errno))); 2941 exit_server_cleanly("getpeername() failed.\n"); 2942 } 2943 ret = tsocket_address_bsd_from_sockaddr(sconn, 2944 sa, sa_socklen, 2945 &remote_address); 2946 if (ret != 0) { 2947 DEBUG(0,("%s: tsocket_address_bsd_from_sockaddr remote failed - %s\n", 2948 __location__, strerror(errno))); 2949 exit_server_cleanly("tsocket_address_bsd_from_sockaddr remote failed.\n"); 2950 } 2951 2952 sa = (struct sockaddr *)(void *)&ss; 2953 sa_socklen = sizeof(ss); 2954 ret = getsockname(sconn->sock, sa, &sa_socklen); 2955 if (ret != 0) { 2956 int level = (errno == ENOTCONN)?2:0; 2957 DEBUG(level,("getsockname() failed - %s\n", strerror(errno))); 2958 exit_server_cleanly("getsockname() failed.\n"); 2959 } 2960 ret = tsocket_address_bsd_from_sockaddr(sconn, 2961 sa, sa_socklen, 2962 &local_address); 2963 if (ret != 0) { 2964 DEBUG(0,("%s: tsocket_address_bsd_from_sockaddr remote failed - %s\n", 2965 __location__, strerror(errno))); 2966 exit_server_cleanly("tsocket_address_bsd_from_sockaddr remote failed.\n"); 2967 } 2968 2969 sconn->local_address = local_address; 2970 sconn->remote_address = remote_address; 2971 2972 if (tsocket_address_is_inet(remote_address, "ip")) { 2973 remaddr = tsocket_address_inet_addr_string( 2974 sconn->remote_address, 2975 talloc_tos()); 2976 if (remaddr == NULL) { 2977 2978 } 2979 } else { 2980 remaddr = "0.0.0.0"; 2981 } 2273 2982 2274 2983 /* this is needed so that we get decent entries 2275 2984 in smbstatus for port 445 connects */ 2276 set_remote_machine_name(get_peer_addr(smbd_server_fd(), 2277 remaddr, 2278 sizeof(remaddr)), 2279 false); 2280 reload_services(true); 2985 set_remote_machine_name(remaddr, false); 2986 reload_services(sconn->msg_ctx, sconn->sock, true); 2281 2987 2282 2988 /* … … 2287 2993 */ 2288 2994 2289 if (!check_access(smbd_server_fd(), lp_hostsallow(-1), 2290 lp_hostsdeny(-1))) { 2291 char addr[INET6_ADDRSTRLEN]; 2292 2995 if (!allow_access(lp_hostsdeny(-1), lp_hostsallow(-1), 2996 sconn->client_id.name, 2997 sconn->client_id.addr)) { 2293 2998 /* 2294 2999 * send a negative session response "not listening on calling … … 2296 3001 */ 2297 3002 unsigned char buf[5] = {0x83, 0, 0, 1, 0x81}; 2298 DEBUG( 1, ("Connection denied from %s\n", 2299 client_addr(get_client_fd(),addr,sizeof(addr)) ) ); 2300 (void)srv_send_smb(smbd_server_fd(),(char *)buf, false, 3003 DEBUG( 1, ("Connection denied from %s to %s\n", 3004 tsocket_address_string(remote_address, talloc_tos()), 3005 tsocket_address_string(local_address, talloc_tos()))); 3006 (void)srv_send_smb(sconn,(char *)buf, false, 2301 3007 0, false, NULL); 2302 3008 exit_server_cleanly("connection denied"); 2303 3009 } 2304 3010 2305 static_init_rpc; 3011 DEBUG(10, ("Connection allowed from %s to %s\n", 3012 tsocket_address_string(remote_address, talloc_tos()), 3013 tsocket_address_string(local_address, talloc_tos()))); 2306 3014 2307 3015 init_modules(); … … 2325 3033 } 2326 3034 2327 if (!srv_init_signing(s mbd_server_conn)) {3035 if (!srv_init_signing(sconn)) { 2328 3036 exit_server("Failed to init smb_signing"); 2329 3037 } 2330 3038 3039 if (lp_async_smb_echo_handler() && !fork_echo_handler(sconn)) { 3040 exit_server("Failed to fork echo handler"); 3041 } 3042 2331 3043 /* Setup oplocks */ 2332 if (!init_oplocks(s mbd_messaging_context()))3044 if (!init_oplocks(sconn->msg_ctx)) 2333 3045 exit_server("Failed to init oplocks"); 2334 3046 2335 /* Setup aio signal handler. */2336 initialize_async_io_handler();2337 2338 3047 /* register our message handlers */ 2339 messaging_register(s mbd_messaging_context(), NULL,3048 messaging_register(sconn->msg_ctx, NULL, 2340 3049 MSG_SMB_FORCE_TDIS, msg_force_tdis); 2341 messaging_register(s mbd_messaging_context(), NULL,3050 messaging_register(sconn->msg_ctx, sconn, 2342 3051 MSG_SMB_RELEASE_IP, msg_release_ip); 2343 messaging_register(s mbd_messaging_context(), NULL,3052 messaging_register(sconn->msg_ctx, NULL, 2344 3053 MSG_SMB_CLOSE_FILE, msg_close_file); 2345 3054 … … 2348 3057 * MSGs to all child processes 2349 3058 */ 2350 messaging_deregister(s mbd_messaging_context(),3059 messaging_deregister(sconn->msg_ctx, 2351 3060 MSG_DEBUG, NULL); 2352 messaging_register(s mbd_messaging_context(), NULL,3061 messaging_register(sconn->msg_ctx, NULL, 2353 3062 MSG_DEBUG, debug_message); 2354 3063 … … 2364 3073 if (!(event_add_idle(smbd_event_context(), NULL, 2365 3074 timeval_set(IDLE_CLOSED_TIMEOUT, 0), 2366 "deadtime", deadtime_fn, NULL))) {3075 "deadtime", deadtime_fn, sconn))) { 2367 3076 DEBUG(0, ("Could not add deadtime event\n")); 2368 3077 exit(1); … … 2371 3080 if (!(event_add_idle(smbd_event_context(), NULL, 2372 3081 timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0), 2373 "housekeeping", housekeeping_fn, NULL))) {3082 "housekeeping", housekeeping_fn, sconn))) { 2374 3083 DEBUG(0, ("Could not add housekeeping event\n")); 2375 3084 exit(1); … … 2388 3097 struct sockaddr_storage srv, clnt; 2389 3098 2390 if (client_get_tcp_info(&srv, &clnt) == 0) { 2391 3099 if (client_get_tcp_info(sconn->sock, &srv, &clnt) == 0) { 2392 3100 NTSTATUS status; 2393 2394 status = ctdbd_register_ips( 2395 messaging_ctdbd_connection(), 2396 &srv, &clnt, release_ip, NULL); 2397 3101 status = smbd_register_ips(sconn, &srv, &clnt); 2398 3102 if (!NT_STATUS_IS_OK(status)) { 2399 3103 DEBUG(0, ("ctdbd_register_ips failed: %s\n", … … 2410 3114 #endif 2411 3115 2412 s mbd_server_conn->nbt.got_session = false;2413 2414 s mbd_server_conn->smb1.negprot.max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);2415 2416 s mbd_server_conn->smb1.sessions.done_sesssetup = false;2417 s mbd_server_conn->smb1.sessions.max_send = BUFFER_SIZE;2418 s mbd_server_conn->smb1.sessions.last_session_tag = UID_FIELD_INVALID;3116 sconn->nbt.got_session = false; 3117 3118 sconn->smb1.negprot.max_recv = MIN(lp_maxxmit(),BUFFER_SIZE); 3119 3120 sconn->smb1.sessions.done_sesssetup = false; 3121 sconn->smb1.sessions.max_send = BUFFER_SIZE; 3122 sconn->smb1.sessions.last_session_tag = UID_FIELD_INVALID; 2419 3123 /* users from session setup */ 2420 s mbd_server_conn->smb1.sessions.session_userlist = NULL;3124 sconn->smb1.sessions.session_userlist = NULL; 2421 3125 /* workgroup from session setup. */ 2422 s mbd_server_conn->smb1.sessions.session_workgroup = NULL;3126 sconn->smb1.sessions.session_workgroup = NULL; 2423 3127 /* this holds info on user ids that are already validated for this VC */ 2424 smbd_server_conn->smb1.sessions.validated_users = NULL; 2425 smbd_server_conn->smb1.sessions.next_vuid = VUID_OFFSET; 2426 smbd_server_conn->smb1.sessions.num_validated_vuids = 0; 2427 #ifdef HAVE_NETGROUP 2428 smbd_server_conn->smb1.sessions.my_yp_domain = NULL; 2429 #endif 2430 2431 conn_init(smbd_server_conn); 2432 if (!init_dptrs(smbd_server_conn)) { 3128 sconn->smb1.sessions.validated_users = NULL; 3129 sconn->smb1.sessions.next_vuid = VUID_OFFSET; 3130 sconn->smb1.sessions.num_validated_vuids = 0; 3131 3132 conn_init(sconn); 3133 if (!init_dptrs(sconn)) { 2433 3134 exit_server("init_dptrs() failed"); 2434 3135 } 2435 3136 2436 s mbd_server_conn->smb1.fde = event_add_fd(smbd_event_context(),2437 s mbd_server_conn,2438 s mbd_server_fd(),3137 sconn->smb1.fde = event_add_fd(smbd_event_context(), 3138 sconn, 3139 sconn->sock, 2439 3140 EVENT_FD_READ, 2440 3141 smbd_server_connection_handler, 2441 s mbd_server_conn);2442 if (!s mbd_server_conn->smb1.fde) {3142 sconn); 3143 if (!sconn->smb1.fde) { 2443 3144 exit_server("failed to create smbd_server_connection fde"); 2444 3145 } … … 2453 3154 errno = 0; 2454 3155 2455 status = smbd_server_connection_loop_once(s mbd_server_conn);3156 status = smbd_server_connection_loop_once(sconn); 2456 3157 if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY) && 2457 3158 !NT_STATUS_IS_OK(status)) {
Note:
See TracChangeset
for help on using the changeset viewer.