LCOV - code coverage report
Current view: top level - source4/smb_server/smb - service.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 52 79 65.8 %
Date: 2024-06-13 04:01:37 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         977 : 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             :         struct smbsrv_tcon *tcon;
      40             :         NTSTATUS status;
      41         977 :         uint64_t ntvfs_caps = 0;
      42             : 
      43         977 :         tcon = smbsrv_smb_tcon_new(req->smb_conn, scfg->name);
      44         977 :         if (!tcon) {
      45           0 :                 DEBUG(0,("Couldn't find free connection.\n"));
      46           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
      47             :         }
      48         977 :         req->tcon = tcon;
      49             : 
      50         977 :         if (req->smb_conn->negotiate.client_caps & CAP_LEVEL_II_OPLOCKS) {
      51         894 :                 ntvfs_caps |= NTVFS_CLIENT_CAP_LEVEL_II_OPLOCKS;
      52             :         }
      53             : 
      54             :         /* init ntvfs function pointers */
      55        4697 :         status = ntvfs_init_connection(tcon, scfg, type,
      56         977 :                                        req->smb_conn->negotiate.protocol,
      57             :                                        ntvfs_caps,
      58         977 :                                        req->smb_conn->connection->event.ctx,
      59         977 :                                        req->smb_conn->connection->msg_ctx,
      60         977 :                                        req->smb_conn->lp_ctx,
      61         977 :                                        req->smb_conn->connection->server_id,
      62             :                                        &tcon->ntvfs);
      63         977 :         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         977 :         status = ntvfs_set_oplock_handler(tcon->ntvfs, smbsrv_send_oplock_break, tcon);
      70         977 :         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         977 :         status = ntvfs_set_addresses(tcon->ntvfs,
      76         977 :                                      req->smb_conn->connection->local_address,
      77         977 :                                      req->smb_conn->connection->remote_address);
      78         977 :         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         977 :         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         977 :         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         977 :         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         996 : static NTSTATUS make_connection(struct smbsrv_request *req,
     109             :                                 const char *service, DATA_BLOB password, 
     110             :                                 const char *dev)
     111             : {
     112             :         NTSTATUS status;
     113             :         enum ntvfs_type type;
     114             :         const char *type_str;
     115             :         struct share_config *scfg;
     116             :         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         996 :         if (strncmp(service, "\\\\", 2) == 0) {
     121         972 :                 char *p = strchr(service+2, '\\');
     122         972 :                 if (p) {
     123         972 :                         service = p + 1;
     124             :                 }
     125             :         }
     126             : 
     127         996 :         status = share_get_config(req, req->smb_conn->share_context, service, &scfg);
     128         996 :         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        1992 :         if (!socket_check_access(req->smb_conn->connection->socket, 
     136         996 :                                  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         996 :         sharetype = share_string_option(req, scfg, "type", "DISK");
     144         996 :         if (sharetype && strcmp(sharetype, "IPC") == 0) {
     145          48 :                 type = NTVFS_IPC;
     146          48 :                 type_str = "IPC";
     147         948 :         } else if (sharetype && strcmp(sharetype, "PRINTER") == 0) {
     148           0 :                 type = NTVFS_PRINT;
     149           0 :                 type_str = "LPT:";
     150             :         } else {
     151         948 :                 type = NTVFS_DISK;
     152         948 :                 type_str = "A:";
     153             :         }
     154         996 :         TALLOC_FREE(sharetype);
     155             : 
     156         996 :         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         977 :         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         996 : NTSTATUS smbsrv_tcon_backend(struct smbsrv_request *req, union smb_tcon *con)
     168             : {
     169             :         NTSTATUS status;
     170             : 
     171         996 :         if (con->generic.level == RAW_TCON_TCON) {
     172             :                 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         996 :         status = make_connection(req, con->tconx.in.path, con->tconx.in.password, 
     190             :                                  con->tconx.in.device);
     191         996 :         if (!NT_STATUS_IS_OK(status)) {
     192          19 :                 return status;
     193             :         }
     194             : 
     195         977 :         con->tconx.out.tid = req->tcon->tid;
     196         977 :         con->tconx.out.options = SMB_SUPPORT_SEARCH_BITS | (share_int_option(req->tcon->ntvfs->config, SHARE_CSC_POLICY, SHARE_CSC_POLICY_DEFAULT) << 2);
     197         977 :         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         977 :         return status;
     202             : }

Generated by: LCOV version 1.13