LCOV - code coverage report
Current view: top level - source4/smb_server/smb - service.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 52 88 59.1 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    service (connection) handling
       4             :    Copyright (C) Andrew Tridgell 1992-2003
       5             :    
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             :    
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             :    
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "smb_server/smb_server.h"
      22             : #include "samba/service_stream.h"
      23             : #include "ntvfs/ntvfs.h"
      24             : #include "param/param.h"
      25             : 
      26             : #undef strcasecmp
      27             : 
      28             : /****************************************************************************
      29             :   Make a connection, given the snum to connect to, and the vuser of the
      30             :   connecting user if appropriate.
      31             :   Does note invoke the NTVFS connection hook
      32             : ****************************************************************************/
      33         953 : static NTSTATUS make_connection_scfg(struct smbsrv_request *req,
      34             :                                      struct share_config *scfg,
      35             :                                      enum ntvfs_type type,
      36             :                                      DATA_BLOB password, 
      37             :                                      const char *dev)
      38             : {
      39           0 :         struct smbsrv_tcon *tcon;
      40           0 :         NTSTATUS status;
      41         953 :         uint64_t ntvfs_caps = 0;
      42             : 
      43         953 :         tcon = smbsrv_smb_tcon_new(req->smb_conn, scfg->name);
      44         953 :         if (!tcon) {
      45           0 :                 DEBUG(0,("Couldn't find free connection.\n"));
      46           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
      47             :         }
      48         953 :         req->tcon = tcon;
      49             : 
      50         953 :         if (req->smb_conn->negotiate.client_caps & CAP_LEVEL_II_OPLOCKS) {
      51         874 :                 ntvfs_caps |= NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS;
      52             :         }
      53             : 
      54             :         /* init ntvfs function pointers */
      55         953 :         status = ntvfs_init_connection(tcon, scfg, type,
      56         953 :                                        req->smb_conn->negotiate.protocol,
      57             :                                        ntvfs_caps,
      58         953 :                                        req->smb_conn->connection->event.ctx,
      59         953 :                                        req->smb_conn->connection->msg_ctx,
      60         953 :                                        req->smb_conn->lp_ctx,
      61         953 :                                        req->smb_conn->connection->server_id,
      62             :                                        &tcon->ntvfs);
      63         953 :         if (!NT_STATUS_IS_OK(status)) {
      64           0 :                 DEBUG(0, ("make_connection_scfg: connection failed for service %s\n", 
      65             :                           scfg->name));
      66           0 :                 goto failed;
      67             :         }
      68             : 
      69         953 :         status = ntvfs_set_oplock_handler(tcon->ntvfs, smbsrv_send_oplock_break, tcon);
      70         953 :         if (!NT_STATUS_IS_OK(status)) {
      71           0 :                 DEBUG(0,("make_connection: NTVFS failed to set the oplock handler!\n"));
      72           0 :                 goto failed;
      73             :         }
      74             : 
      75         953 :         status = ntvfs_set_addresses(tcon->ntvfs,
      76         953 :                                      req->smb_conn->connection->local_address,
      77         953 :                                      req->smb_conn->connection->remote_address);
      78         953 :         if (!NT_STATUS_IS_OK(status)) {
      79           0 :                 DEBUG(0,("make_connection: NTVFS failed to set the addresses!\n"));
      80           0 :                 goto failed;
      81             :         }
      82             : 
      83         953 :         status = ntvfs_set_handle_callbacks(tcon->ntvfs,
      84             :                                             smbsrv_handle_create_new,
      85             :                                             smbsrv_handle_make_valid,
      86             :                                             smbsrv_handle_destroy,
      87             :                                             smbsrv_handle_search_by_wire_key,
      88             :                                             smbsrv_handle_get_wire_key,
      89             :                                             tcon);
      90         953 :         if (!NT_STATUS_IS_OK(status)) {
      91           0 :                 DEBUG(0,("make_connection: NTVFS failed to set the handle callbacks!\n"));
      92           0 :                 goto failed;
      93             :         }
      94             : 
      95         953 :         return NT_STATUS_OK;
      96             : 
      97           0 : failed:
      98           0 :         req->tcon = NULL;
      99           0 :         talloc_free(tcon);
     100           0 :         return status;
     101             : }
     102             : 
     103             : /****************************************************************************
     104             :  Make a connection to a service.
     105             :  *
     106             :  * @param service 
     107             : ****************************************************************************/
     108         972 : static NTSTATUS make_connection(struct smbsrv_request *req,
     109             :                                 const char *service, DATA_BLOB password, 
     110             :                                 const char *dev)
     111             : {
     112           0 :         NTSTATUS status;
     113           0 :         enum ntvfs_type type;
     114           0 :         const char *type_str;
     115           0 :         struct share_config *scfg;
     116           0 :         char *sharetype;
     117             : 
     118             :         /* the service might be of the form \\SERVER\SHARE. Should we put
     119             :            the server name we get from this somewhere? */
     120         972 :         if (strncmp(service, "\\\\", 2) == 0) {
     121         948 :                 char *p = strchr(service+2, '\\');
     122         948 :                 if (p) {
     123         948 :                         service = p + 1;
     124             :                 }
     125             :         }
     126             : 
     127         972 :         status = share_get_config(req, req->smb_conn->share_context, service, &scfg);
     128         972 :         if (!NT_STATUS_IS_OK(status)) {
     129           0 :                 DEBUG(0,("make_connection: couldn't find service %s: %s\n", service, nt_errstr(status)));
     130           0 :                 return NT_STATUS_BAD_NETWORK_NAME;
     131             :         }
     132             : 
     133             :         /* TODO: check the password, when it's share level security! */
     134             : 
     135        1944 :         if (!socket_check_access(req->smb_conn->connection->socket, 
     136         972 :                                  scfg->name, 
     137             :                                  share_string_list_option(req, scfg, SHARE_HOSTS_ALLOW), 
     138             :                                  share_string_list_option(req, scfg, SHARE_HOSTS_DENY))) {
     139           0 :                 return NT_STATUS_ACCESS_DENIED;
     140             :         }
     141             : 
     142             :         /* work out what sort of connection this is */
     143         972 :         sharetype = share_string_option(req, scfg, "type", "DISK");
     144         972 :         if (sharetype && strcmp(sharetype, "IPC") == 0) {
     145          44 :                 type = NTVFS_IPC;
     146          44 :                 type_str = "IPC";
     147         928 :         } else if (sharetype && strcmp(sharetype, "PRINTER") == 0) {
     148           0 :                 type = NTVFS_PRINT;
     149           0 :                 type_str = "LPT:";
     150             :         } else {
     151         928 :                 type = NTVFS_DISK;
     152         928 :                 type_str = "A:";
     153             :         }
     154         972 :         TALLOC_FREE(sharetype);
     155             : 
     156         972 :         if (strcmp(dev, "?????") != 0 && strcasecmp(type_str, dev) != 0) {
     157             :                 /* the client gave us the wrong device type */
     158          19 :                 return NT_STATUS_BAD_DEVICE_TYPE;
     159             :         }
     160             : 
     161         953 :         return make_connection_scfg(req, scfg, type, password, dev);
     162             : }
     163             : 
     164             : /*
     165             :   backend for tree connect call, in preparation for calling ntvfs_connect()
     166             : */
     167         972 : NTSTATUS smbsrv_tcon_backend(struct smbsrv_request *req, union smb_tcon *con)
     168             : {
     169           0 :         NTSTATUS status;
     170             : 
     171         972 :         if (con->generic.level == RAW_TCON_TCON) {
     172           0 :                 DATA_BLOB password;
     173           0 :                 password = data_blob_string_const(con->tcon.in.password);
     174             : 
     175           0 :                 status = make_connection(req, con->tcon.in.service, password, con->tcon.in.dev);
     176             :                 
     177           0 :                 if (!NT_STATUS_IS_OK(status)) {
     178           0 :                         return status;
     179             :                 }
     180             : 
     181           0 :                 con->tcon.out.max_xmit = req->smb_conn->negotiate.max_recv;
     182           0 :                 con->tcon.out.tid = req->tcon->tid;
     183             : 
     184           0 :                 return status;
     185             :         } 
     186             : 
     187             :         /* TODO: take a look at tconx.in.flags! */
     188             : 
     189         972 :         status = make_connection(req, con->tconx.in.path, con->tconx.in.password, 
     190             :                                  con->tconx.in.device);
     191         972 :         if (!NT_STATUS_IS_OK(status)) {
     192          19 :                 return status;
     193             :         }
     194             : 
     195         953 :         con->tconx.out.tid = req->tcon->tid;
     196         953 :         con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (share_int_option(req->tcon->ntvfs->config, SHARE_CSC_POLICY, SHARE_CSC_POLICY_DEFAULT) << 2);
     197         953 :         if (share_bool_option(req->tcon->ntvfs->config, SHARE_MSDFS_ROOT, SHARE_MSDFS_ROOT_DEFAULT) && lpcfg_host_msdfs(req->smb_conn->lp_ctx)) {
     198           0 :                 con->tconx.out.options |= SMB_SHARE_IN_DFS;
     199             :         }
     200             : 
     201         953 :         return status;
     202             : }

Generated by: LCOV version 1.14