Fix bug #8882 - Broken processing of %U with vfs_full_audit when force user is set.
[samba.git] / source3 / smbd / service.c
index 8039d16586eb164f576f4c4def3a919aac6feff1..2de9384893568d8cec52c5fcf3da8d82c062debe 100644 (file)
@@ -339,7 +339,7 @@ int find_service(fstring service)
                if ((iPrinterService = lp_servicenumber(PRINTERS_NAME)) < 0) {
                        iPrinterService = load_registry_service(PRINTERS_NAME);
                }
-               if (iPrinterService) {
+               if (iPrinterService >= 0) {
                        DEBUG(3,("checking whether %s is a valid printer name...\n", service));
                        if (pcap_printername_ok(service)) {
                                DEBUG(3,("%s is a valid printer name\n", service));
@@ -652,7 +652,6 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
        fstring dev;
        int ret;
        char addr[INET6_ADDRSTRLEN];
-       bool on_err_call_dis_hook = false;
        NTSTATUS status;
 
        fstrcpy(dev, pdev);
@@ -747,6 +746,14 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                        return NULL;
                }
 
+               /* We don't want to replace the original sanitized_username
+                  as it is the original user given in the connect attempt.
+                  This is used in '%U' substitutions. */
+               TALLOC_FREE(forced_serverinfo->sanitized_username);
+               forced_serverinfo->sanitized_username =
+                       talloc_move(forced_serverinfo,
+                                       &conn->server_info->sanitized_username);
+
                TALLOC_FREE(conn->server_info);
                conn->server_info = forced_serverinfo;
 
@@ -850,25 +857,6 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                return NULL;
        }
 
-       /*
-        * If widelinks are disallowed we need to canonicalise the connect
-        * path here to ensure we don't have any symlinks in the
-        * connectpath. We will be checking all paths on this connection are
-        * below this directory. We must do this after the VFS init as we
-        * depend on the realpath() pointer in the vfs table. JRA.
-        */
-       if (!lp_widelinks(snum)) {
-               if (!canonicalize_connect_path(conn)) {
-                       DEBUG(0, ("canonicalize_connect_path failed "
-                       "for service %s, path %s\n",
-                               lp_servicename(snum),
-                               conn->connectpath));
-                       conn_free(conn);
-                       *pstatus = NT_STATUS_BAD_NETWORK_NAME;
-                       return NULL;
-               }
-       }
-
        if ((!conn->printer) && (!conn->ipc)) {
                conn->notify_ctx = notify_init(conn, server_id_self(),
                                               smbd_messaging_context(),
@@ -876,7 +864,11 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                                               conn);
        }
 
-/* ROOT Activities: */ 
+/* ROOT Activities: */
+       /* explicitly check widelinks here so that we can correctly warn
+        * in the logs. */
+       widelinks_warning(snum);
+
        /*
         * Enforce the max connections parameter.
         */
@@ -902,6 +894,30 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                return NULL;
        }  
 
+       /* Invoke VFS make connection hook - must be the first
+          VFS operation we do. */
+
+       if (SMB_VFS_CONNECT(conn, lp_servicename(snum),
+                           conn->server_info->unix_name) < 0) {
+               DEBUG(0,("make_connection: VFS make connection failed!\n"));
+               yield_connection(conn, lp_servicename(snum));
+               conn_free(conn);
+               *pstatus = NT_STATUS_UNSUCCESSFUL;
+               return NULL;
+       }
+
+       /*
+        * Fix compatibility issue pointed out by Volker.
+        * We pass the conn->connectpath to the preexec
+        * scripts as a parameter, so attempt to canonicalize
+        * it here before calling the preexec scripts.
+        * We ignore errors here, as it is possible that
+        * the conn->connectpath doesn't exist yet and
+        * the preexec scripts will create them.
+        */
+
+       (void)canonicalize_connect_path(conn);
+
        /* Preexecs are done here as they might make the dir we are to ChDir
         * to below */
        /* execute any "root preexec = " line */
@@ -920,6 +936,7 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                if (ret != 0 && lp_rootpreexec_close(snum)) {
                        DEBUG(1,("root preexec gave %d - failing "
                                 "connection\n", ret));
+                       SMB_VFS_DISCONNECT(conn);
                        yield_connection(conn, lp_servicename(snum));
                        conn_free(conn);
                        *pstatus = NT_STATUS_ACCESS_DENIED;
@@ -931,6 +948,7 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
        if (!change_to_user(conn, conn->vuid)) {
                /* No point continuing if they fail the basic checks */
                DEBUG(0,("Can't become connected user!\n"));
+               SMB_VFS_DISCONNECT(conn);
                yield_connection(conn, lp_servicename(snum));
                conn_free(conn);
                *pstatus = NT_STATUS_LOGON_FAILURE;
@@ -963,6 +981,24 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                }
        }
 
+       /*
+        * If widelinks are disallowed we need to canonicalise the connect
+        * path here to ensure we don't have any symlinks in the
+        * connectpath. We will be checking all paths on this connection are
+        * below this directory. We must do this after the VFS init as we
+        * depend on the realpath() pointer in the vfs table. JRA.
+        */
+       if (!lp_widelinks(snum)) {
+               if (!canonicalize_connect_path(conn)) {
+                       DEBUG(0, ("canonicalize_connect_path failed "
+                       "for service %s, path %s\n",
+                               lp_servicename(snum),
+                               conn->connectpath));
+                       *pstatus = NT_STATUS_BAD_NETWORK_NAME;
+                       goto err_root_exit;
+               }
+       }
+
 #ifdef WITH_FAKE_KASERVER
        if (lp_afs_share(snum)) {
                afs_login(conn);
@@ -978,20 +1014,6 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                                lp_aio_write_behind(snum));
        }
        
-       /* Invoke VFS make connection hook - do this before the VFS_STAT call
-          to allow any filesystems needing user credentials to initialize
-          themselves. */
-
-       if (SMB_VFS_CONNECT(conn, lp_servicename(snum),
-                           conn->server_info->unix_name) < 0) {
-               DEBUG(0,("make_connection: VFS make connection failed!\n"));
-               *pstatus = NT_STATUS_UNSUCCESSFUL;
-               goto err_root_exit;
-       }
-
-       /* Any error exit after here needs to call the disconnect hook. */
-       on_err_call_dis_hook = true;
-
        status = create_synthetic_smb_fname(talloc_tos(), conn->connectpath,
                                            NULL, NULL, &smb_fname_cpath);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1019,6 +1041,7 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
                *pstatus = NT_STATUS_BAD_NETWORK_NAME;
                goto err_root_exit;
        }
+       conn->base_share_dev = smb_fname_cpath->st.st_ex_dev;
 
        string_set(&conn->origpath,conn->connectpath);
 
@@ -1039,14 +1062,6 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
        }
 #endif
 
-       if (lp_unix_extensions() && lp_widelinks(snum)) {
-               DEBUG(0,("Share '%s' has wide links and unix extensions enabled. "
-                       "These parameters are incompatible. "
-                       "Disabling wide links for this share.\n",
-                       lp_servicename(snum) ));
-               lp_do_parameter(snum, "wide links", "False");
-       }
-
        /* Figure out the characteristics of the underlying filesystem. This
         * assumes that all the filesystem mounted withing a share path have
         * the same characteristics, which is likely but not guaranteed.
@@ -1078,10 +1093,8 @@ connection_struct *make_connection_snum(struct smbd_server_connection *sconn,
   err_root_exit:
        TALLOC_FREE(smb_fname_cpath);
        change_to_root_user();
-       if (on_err_call_dis_hook) {
-               /* Call VFS disconnect hook */
-               SMB_VFS_DISCONNECT(conn);
-       }
+       /* Call VFS disconnect hook */
+       SMB_VFS_DISCONNECT(conn);
        yield_connection(conn, lp_servicename(snum));
        conn_free(conn);
        return NULL;