| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 |
|
|---|
| 4 | process incoming connections and fork a samba3 in inetd mode
|
|---|
| 5 |
|
|---|
| 6 | Copyright (C) Stefan Metzmacher 2008
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 | #include "smbd/service.h"
|
|---|
| 24 | #include "libcli/smb2/smb2.h"
|
|---|
| 25 | #include "system/network.h"
|
|---|
| 26 | #include "lib/socket/netif.h"
|
|---|
| 27 | #include "param/param.h"
|
|---|
| 28 | #include "dynconfig/dynconfig.h"
|
|---|
| 29 | #include "smbd/process_model.h"
|
|---|
| 30 |
|
|---|
| 31 | /*
|
|---|
| 32 | initialise a server_context from a open socket and register a event handler
|
|---|
| 33 | for reading from that socket
|
|---|
| 34 | */
|
|---|
| 35 | static void samba3_smb_accept(struct stream_connection *conn)
|
|---|
| 36 | {
|
|---|
| 37 | int i;
|
|---|
| 38 | int fd = socket_get_fd(conn->socket);
|
|---|
| 39 | const char *prog;
|
|---|
| 40 | char *argv[2];
|
|---|
| 41 | char *reason;
|
|---|
| 42 |
|
|---|
| 43 | close(0);
|
|---|
| 44 | close(1);
|
|---|
| 45 | dup2(fd, 0);
|
|---|
| 46 | dup2(fd, 1);
|
|---|
| 47 | dup2(fd, 2);
|
|---|
| 48 | for (i=3;i<256;i++) {
|
|---|
| 49 | close(i);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | prog = lpcfg_parm_string(conn->lp_ctx, NULL, "samba3", "smbd");
|
|---|
| 53 |
|
|---|
| 54 | if (prog == NULL) {
|
|---|
| 55 | argv[0] = talloc_asprintf(conn, "%s/%s", dyn_BINDIR, "smbd3");
|
|---|
| 56 | }
|
|---|
| 57 | else {
|
|---|
| 58 | argv[0] = talloc_strdup(conn, prog);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | if (argv[0] == NULL) {
|
|---|
| 62 | stream_terminate_connection(conn, "out of memory");
|
|---|
| 63 | return;
|
|---|
| 64 | }
|
|---|
| 65 | argv[1] = NULL;
|
|---|
| 66 |
|
|---|
| 67 | execv(argv[0], argv);
|
|---|
| 68 |
|
|---|
| 69 | /*
|
|---|
| 70 | * Should never get here
|
|---|
| 71 | */
|
|---|
| 72 | reason = talloc_asprintf(conn, "Could not execute %s", argv[0]);
|
|---|
| 73 | if (reason == NULL) {
|
|---|
| 74 | stream_terminate_connection(conn, "out of memory");
|
|---|
| 75 | return;
|
|---|
| 76 | }
|
|---|
| 77 | stream_terminate_connection(conn, reason);
|
|---|
| 78 | talloc_free(reason);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | static const struct stream_server_ops samba3_smb_stream_ops = {
|
|---|
| 82 | .name = "samba3",
|
|---|
| 83 | .accept_connection = samba3_smb_accept,
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | /*
|
|---|
| 87 | setup a listening socket on all the SMB ports for a particular address
|
|---|
| 88 | */
|
|---|
| 89 | static NTSTATUS samba3_add_socket(struct task_server *task,
|
|---|
| 90 | struct tevent_context *event_context,
|
|---|
| 91 | struct loadparm_context *lp_ctx,
|
|---|
| 92 | const struct model_ops *model_ops,
|
|---|
| 93 | const char *address)
|
|---|
| 94 | {
|
|---|
| 95 | const char **ports = lpcfg_smb_ports(lp_ctx);
|
|---|
| 96 | int i;
|
|---|
| 97 | NTSTATUS status;
|
|---|
| 98 |
|
|---|
| 99 | for (i=0;ports[i];i++) {
|
|---|
| 100 | uint16_t port = atoi(ports[i]);
|
|---|
| 101 | if (port == 0) continue;
|
|---|
| 102 | status = stream_setup_socket(task, event_context, lp_ctx,
|
|---|
| 103 | model_ops, &samba3_smb_stream_ops,
|
|---|
| 104 | "ip", address, &port,
|
|---|
| 105 | lpcfg_socket_options(lp_ctx),
|
|---|
| 106 | NULL);
|
|---|
| 107 | NT_STATUS_NOT_OK_RETURN(status);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | return NT_STATUS_OK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | /*
|
|---|
| 115 | open the smb server sockets
|
|---|
| 116 | */
|
|---|
| 117 | static void samba3_smb_task_init(struct task_server *task)
|
|---|
| 118 | {
|
|---|
| 119 | NTSTATUS status;
|
|---|
| 120 | const struct model_ops *model_ops;
|
|---|
| 121 |
|
|---|
| 122 | model_ops = process_model_startup("standard");
|
|---|
| 123 |
|
|---|
| 124 | if (model_ops == NULL) {
|
|---|
| 125 | goto failed;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | task_server_set_title(task, "task[samba3_smb]");
|
|---|
| 129 |
|
|---|
| 130 | if (lpcfg_interfaces(task->lp_ctx)
|
|---|
| 131 | && lpcfg_bind_interfaces_only(task->lp_ctx)) {
|
|---|
| 132 | int num_interfaces;
|
|---|
| 133 | int i;
|
|---|
| 134 | struct interface *ifaces;
|
|---|
| 135 |
|
|---|
| 136 | load_interfaces(task, lpcfg_interfaces(task->lp_ctx), &ifaces);
|
|---|
| 137 |
|
|---|
| 138 | num_interfaces = iface_count(ifaces);
|
|---|
| 139 |
|
|---|
| 140 | /* We have been given an interfaces line, and been
|
|---|
| 141 | told to only bind to those interfaces. Create a
|
|---|
| 142 | socket per interface and bind to only these.
|
|---|
| 143 | */
|
|---|
| 144 | for(i = 0; i < num_interfaces; i++) {
|
|---|
| 145 | const char *address = iface_n_ip(ifaces, i);
|
|---|
| 146 | status = samba3_add_socket(task,
|
|---|
| 147 | task->event_ctx,
|
|---|
| 148 | task->lp_ctx,
|
|---|
| 149 | model_ops, address);
|
|---|
| 150 | if (!NT_STATUS_IS_OK(status)) goto failed;
|
|---|
| 151 | }
|
|---|
| 152 | } else {
|
|---|
| 153 | /* Just bind to lpcfg_socket_address() (usually 0.0.0.0) */
|
|---|
| 154 | status = samba3_add_socket(task,
|
|---|
| 155 | task->event_ctx, task->lp_ctx,
|
|---|
| 156 | model_ops,
|
|---|
| 157 | lpcfg_socket_address(task->lp_ctx));
|
|---|
| 158 | if (!NT_STATUS_IS_OK(status)) goto failed;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | return;
|
|---|
| 162 | failed:
|
|---|
| 163 | task_server_terminate(task, "Failed to startup samba3 smb task", true);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /* called at smbd startup - register ourselves as a server service */
|
|---|
| 167 | NTSTATUS server_service_samba3_smb_init(void)
|
|---|
| 168 | {
|
|---|
| 169 | return register_server_service("samba3_smb", samba3_smb_task_init);
|
|---|
| 170 | }
|
|---|