/* Unix SMB/CIFS implementation. process incoming packets - main loop Copyright (C) Andrew Tridgell 2004-2005 Copyright (C) Stefan Metzmacher 2004-2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "includes.h" #include "lib/events/events.h" #include "system/time.h" #include "dlinklist.h" #include "smbd/service_stream.h" #include "smb_server/smb_server.h" #include "lib/messaging/irpc.h" #include "lib/stream/packet.h" /* close the socket and shutdown a server_context */ void smbsrv_terminate_connection(struct smbsrv_connection *smb_conn, const char *reason) { smb_conn->terminate = reason; } /* called when a SMB socket becomes readable */ static void smbsrv_recv(struct stream_connection *conn, uint16_t flags) { struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, struct smbsrv_connection); DEBUG(10,("smbsrv_recv\n")); packet_recv(smb_conn->packet); if (smb_conn->terminate) { talloc_free(conn->event.fde); conn->event.fde = NULL; stream_terminate_connection(smb_conn->connection, smb_conn->terminate); return; } /* free up temporary memory */ lp_talloc_free(); } /* called when a SMB socket becomes writable */ static void smbsrv_send(struct stream_connection *conn, uint16_t flags) { struct smbsrv_connection *smb_conn = talloc_get_type(conn->private, struct smbsrv_connection); packet_queue_run(smb_conn->packet); } /* handle socket recv errors */ static void smbsrv_recv_error(void *private, NTSTATUS status) { struct smbsrv_connection *smb_conn = talloc_get_type(private, struct smbsrv_connection); smbsrv_terminate_connection(smb_conn, nt_errstr(status)); } /* initialise a server_context from a open socket and register a event handler for reading from that socket */ static void smbsrv_accept(struct stream_connection *conn) { struct smbsrv_connection *smb_conn; DEBUG(5,("smbsrv_accept\n")); smb_conn = talloc_zero(conn, struct smbsrv_connection); if (!smb_conn) return; /* now initialise a few default values associated with this smb socket */ smb_conn->negotiate.max_send = 0xFFFF; /* this is the size that w2k uses, and it appears to be important for good performance */ smb_conn->negotiate.max_recv = lp_max_xmit(); smb_conn->negotiate.zone_offset = get_time_zone(time(NULL)); smb_conn->negotiate.called_name = NULL; smb_conn->negotiate.calling_name = NULL; smb_conn->packet = packet_init(smb_conn); if (smb_conn->packet == NULL) { stream_terminate_connection(conn, "out of memory"); return; } packet_set_private(smb_conn->packet, smb_conn); packet_set_socket(smb_conn->packet, conn->socket); packet_set_callback(smb_conn->packet, smbsrv_recv_smb_request); packet_set_full_request(smb_conn->packet, packet_full_request_nbt); packet_set_error_handler(smb_conn->packet, smbsrv_recv_error); packet_set_event_context(smb_conn->packet, conn->event.ctx); packet_set_fde(smb_conn->packet, conn->event.fde); packet_set_serialise(smb_conn->packet); smbsrv_vuid_init(smb_conn); srv_init_signing(smb_conn); smbsrv_tcon_init(smb_conn); smb_conn->connection = conn; smb_conn->config.security = lp_security(); smb_conn->config.nt_status_support = lp_nt_status_support(); conn->private = smb_conn; irpc_add_name(conn->msg_ctx, "smb_server"); smbsrv_management_init(smb_conn); } static const struct stream_server_ops smb_stream_ops = { .name = "smb", .accept_connection = smbsrv_accept, .recv_handler = smbsrv_recv, .send_handler = smbsrv_send, }; /* setup a listening socket on all the SMB ports for a particular address */ static NTSTATUS smb_add_socket(struct event_context *event_context, const struct model_ops *model_ops, const char *address) { const char **ports = lp_smb_ports(); int i; NTSTATUS status; for (i=0;ports[i];i++) { uint16_t port = atoi(ports[i]); if (port == 0) continue; status = stream_setup_socket(event_context, model_ops, &smb_stream_ops, "ipv4", address, &port, NULL); NT_STATUS_NOT_OK_RETURN(status); } return NT_STATUS_OK; } /* called on startup of the smb server service It's job is to start listening on all configured SMB server sockets */ static NTSTATUS smbsrv_init(struct event_context *event_context, const struct model_ops *model_ops) { NTSTATUS status; if (lp_interfaces() && lp_bind_interfaces_only()) { int num_interfaces = iface_count(); int i; /* We have been given an interfaces line, and been told to only bind to those interfaces. Create a socket per interface and bind to only these. */ for(i = 0; i < num_interfaces; i++) { const char *address = iface_n_ip(i); status = smb_add_socket(event_context, model_ops, address); NT_STATUS_NOT_OK_RETURN(status); } } else { /* Just bind to lp_socket_address() (usually 0.0.0.0) */ status = smb_add_socket(event_context, model_ops, lp_socket_address()); NT_STATUS_NOT_OK_RETURN(status); } return NT_STATUS_OK; } /* called at smbd startup - register ourselves as a server service */ NTSTATUS server_service_smb_init(void) { return register_server_service("smb", smbsrv_init); }