smbd: use metadata_fsp() with SMB_VFS_FGET_NT_ACL()
[samba.git] / source3 / rpc_server / srvsvc / srv_srvsvc_nt.c
index 2d5846685050f41c285d6113362343bbd01899dd..2608e92db4bce7479008a86856d972eddc90820a 100644 (file)
 
 #include "includes.h"
 #include "system/passwd.h"
+#include "lib/util/server_id.h"
 #include "ntdomain.h"
-#include "../librpc/gen_ndr/srv_srvsvc.h"
+#include "librpc/gen_ndr/ndr_srvsvc.h"
+#include "librpc/gen_ndr/ndr_srvsvc_scompat.h"
 #include "../libcli/security/security.h"
 #include "../librpc/gen_ndr/ndr_security.h"
 #include "../librpc/gen_ndr/open_files.h"
 #include "dbwrap/dbwrap.h"
 #include "session.h"
 #include "../lib/util/util_pw.h"
+#include "locking/share_mode_lock.h"
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
 #include "auth.h"
 #include "messages.h"
-#include "lib/conn_tdb.h"
+#include "serverid.h"
+#include "lib/global_contexts.h"
+#include "source3/lib/substitute.h"
 
 extern const struct generic_mapping file_generic_mapping;
 
@@ -52,72 +57,96 @@ struct file_enum_count {
        TALLOC_CTX *ctx;
        const char *username;
        struct srvsvc_NetFileCtr3 *ctr3;
+       struct file_id *fids;
 };
 
-struct sess_file_count {
-       struct server_id pid;
-       uid_t uid;
+struct sess_file_info {
+       struct srvsvc_NetSessCtr1 *ctr;
+       struct sessionid *session_list;
+       uint32_t resume_handle;
+       uint32_t num_entries;
+};
+
+struct share_file_stat {
+       struct srvsvc_NetConnInfo1 *netconn_arr;
+       struct server_id *svrid_arr;
+       const char *in_sharepath;
+       uint32_t resp_entries;
+       uint32_t total_entries;
+};
+
+struct share_conn_stat {
+       TALLOC_CTX *ctx;
+       const char *sharename;
+       struct server_id *svrid_arr;
        int count;
 };
 
 /*******************************************************************
 ********************************************************************/
 
-static void enum_file_fn( const struct share_mode_entry *e,
-                          const char *sharepath, const char *fname,
-                         void *private_data )
+static int enum_file_fn(struct file_id id,
+                       const struct share_mode_data *d,
+                       const struct share_mode_entry *e,
+                       void *private_data)
 {
        struct file_enum_count *fenum =
                (struct file_enum_count *)private_data;
-
+       struct srvsvc_NetFileCtr3 *ctr3 = fenum->ctr3;
        struct srvsvc_NetFileInfo3 *f;
-       int i = fenum->ctr3->count;
-       files_struct fsp;
-       struct byte_range_lock *brl;
-       int num_locks = 0;
+       struct file_id *fids = NULL;
        char *fullpath = NULL;
-       uint32 permissions;
+       uint32_t permissions;
        const char *username;
 
        /* If the pid was not found delete the entry from connections.tdb */
 
        if ( !process_exists(e->pid) ) {
-               return;
+               return 0;
        }
 
        username = uidtoname(e->uid);
 
        if ((fenum->username != NULL)
            && !strequal(username, fenum->username)) {
-               return;
+               return 0;
        }
 
-       f = talloc_realloc(fenum->ctx, fenum->ctr3->array,
-                                struct srvsvc_NetFileInfo3, i+1);
+       f = talloc_realloc(
+               fenum->ctx,
+               ctr3->array,
+               struct srvsvc_NetFileInfo3,
+               ctr3->count+1);
        if ( !f ) {
-               DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
-               return;
+               DBG_ERR("realloc failed for %"PRIu32" items\n", ctr3->count+1);
+               return 0;
        }
-       fenum->ctr3->array = f;
+       ctr3->array = f;
 
-       /* need to count the number of locks on a file */
-
-       ZERO_STRUCT( fsp );
-       fsp.file_id = e->id;
-
-       if ( (brl = brl_get_locks(talloc_tos(), &fsp)) != NULL ) {
-               num_locks = brl->num_locks;
-               TALLOC_FREE(brl);
+       fids = talloc_realloc(
+               fenum->ctx, fenum->fids, struct file_id, ctr3->count+1);
+       if (fids == NULL) {
+               DBG_ERR("realloc failed for %"PRIu32" items\n", ctr3->count+1);
+               return 0;
        }
+       fids[ctr3->count] = id;
+       fenum->fids = fids;
 
-       if ( strcmp( fname, "." ) == 0 ) {
-               fullpath = talloc_asprintf(fenum->ctx, "C:%s", sharepath );
+       if ( strcmp(d->base_name, "." ) == 0 ) {
+               fullpath = talloc_asprintf(
+                       fenum->ctx,
+                       "C:%s",
+                       d->servicepath);
        } else {
-               fullpath = talloc_asprintf(fenum->ctx, "C:%s/%s",
-                               sharepath, fname );
+               fullpath = talloc_asprintf(
+                       fenum->ctx,
+                       "C:%s/%s%s",
+                       d->servicepath,
+                       d->base_name,
+                       (d->stream_name != NULL) ? d->stream_name : "");
        }
        if (!fullpath) {
-               return;
+               return 0;
        }
        string_replace( fullpath, '/', '\\' );
 
@@ -126,14 +155,17 @@ static void enum_file_fn( const struct share_mode_entry *e,
 
        /* now fill in the srvsvc_NetFileInfo3 struct */
 
-       fenum->ctr3->array[i].fid               =
-               (((uint32_t)(procid_to_pid(&e->pid))<<16) | e->share_file_id);
-       fenum->ctr3->array[i].permissions       = permissions;
-       fenum->ctr3->array[i].num_locks         = num_locks;
-       fenum->ctr3->array[i].path              = fullpath;
-       fenum->ctr3->array[i].user              = username;
+       ctr3->array[ctr3->count] = (struct srvsvc_NetFileInfo3) {
+               .fid            = (((uint32_t)(procid_to_pid(&e->pid))<<16) |
+                                  e->share_file_id),
+               .permissions    = permissions,
+               .path           = fullpath,
+               .user           = username,
+       };
+
+       ctr3->count++;
 
-       fenum->ctr3->count++;
+       return 0;
 }
 
 /*******************************************************************
@@ -144,16 +176,31 @@ static WERROR net_enum_files(TALLOC_CTX *ctx,
                             struct srvsvc_NetFileCtr3 **ctr3,
                             uint32_t resume)
 {
-       struct file_enum_count f_enum_cnt;
+       struct file_enum_count f_enum_cnt = {
+               .ctx = ctx, .username = username, .ctr3 = *ctr3,
+       };
+       uint32_t i;
 
-       f_enum_cnt.ctx = ctx;
-       f_enum_cnt.username = username;
-       f_enum_cnt.ctr3 = *ctr3;
-
-       share_mode_forall( enum_file_fn, (void *)&f_enum_cnt );
+       share_entry_forall(enum_file_fn, (void *)&f_enum_cnt );
 
        *ctr3 = f_enum_cnt.ctr3;
 
+       /* need to count the number of locks on a file */
+
+       for (i=0; i<(*ctr3)->count; i++) {
+               struct files_struct fsp = { .file_id = f_enum_cnt.fids[i], };
+               struct byte_range_lock *brl = NULL;
+
+               brl = brl_get_locks(ctx, &fsp);
+               if (brl == NULL) {
+                       continue;
+               }
+
+               (*ctr3)->array[i].num_locks = brl_num_locks(brl);
+
+               TALLOC_FREE(brl);
+       }
+
        return WERR_OK;
 }
 
@@ -165,11 +212,11 @@ static enum srvsvc_ShareType get_share_type(int snum)
        /* work out the share type */
        enum srvsvc_ShareType type = STYPE_DISKTREE;
 
-       if (lp_print_ok(snum)) {
+       if (lp_printable(snum)) {
                type = lp_administrative_share(snum)
                        ? STYPE_PRINTQ_HIDDEN : STYPE_PRINTQ;
        }
-       if (strequal(lp_fstype(talloc_tos(), snum), "IPC")) {
+       if (strequal(lp_fstype(snum), "IPC")) {
                type = lp_administrative_share(snum)
                        ? STYPE_IPC_HIDDEN : STYPE_IPC;
        }
@@ -183,7 +230,10 @@ static enum srvsvc_ShareType get_share_type(int snum)
 static void init_srv_share_info_0(struct pipes_struct *p,
                                  struct srvsvc_NetShareInfo0 *r, int snum)
 {
-       r->name         = lp_servicename(talloc_tos(), snum);
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+
+       r->name         = lp_servicename(talloc_tos(), lp_sub, snum);
 }
 
 /*******************************************************************
@@ -194,13 +244,15 @@ static void init_srv_share_info_1(struct pipes_struct *p,
                                  struct srvsvc_NetShareInfo1 *r,
                                  int snum)
 {
-       char *net_name = lp_servicename(talloc_tos(), snum);
-       char *remark = lp_comment(p->mem_ctx, snum);
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+       char *net_name = lp_servicename(talloc_tos(), lp_sub, snum);
+       char *remark = lp_comment(p->mem_ctx, lp_sub, snum);
 
        if (remark) {
-               remark = talloc_sub_advanced(
-                       p->mem_ctx, lp_servicename(talloc_tos(), snum),
-                       get_current_username(), lp_pathname(talloc_tos(), snum),
+               remark = talloc_sub_full(
+                       p->mem_ctx, lp_servicename(talloc_tos(), lp_sub, snum),
+                       get_current_username(), lp_path(talloc_tos(), lp_sub, snum),
                        p->session_info->unix_token->uid, get_current_username(),
                        "", remark);
        }
@@ -218,22 +270,24 @@ static void init_srv_share_info_2(struct pipes_struct *p,
                                  struct srvsvc_NetShareInfo2 *r,
                                  int snum)
 {
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        char *remark = NULL;
        char *path = NULL;
        int max_connections = lp_max_connections(snum);
        uint32_t max_uses = max_connections!=0 ? max_connections : (uint32_t)-1;
-       char *net_name = lp_servicename(talloc_tos(), snum);
+       char *net_name = lp_servicename(talloc_tos(), lp_sub, snum);
 
-       remark = lp_comment(p->mem_ctx, snum);
+       remark = lp_comment(p->mem_ctx, lp_sub, snum);
        if (remark) {
-               remark = talloc_sub_advanced(
-                       p->mem_ctx, lp_servicename(talloc_tos(), snum),
-                       get_current_username(), lp_pathname(talloc_tos(), snum),
+               remark = talloc_sub_full(
+                       p->mem_ctx, lp_servicename(talloc_tos(), lp_sub, snum),
+                       get_current_username(), lp_path(talloc_tos(), lp_sub, snum),
                        p->session_info->unix_token->uid, get_current_username(),
                        "", remark);
        }
        path = talloc_asprintf(p->mem_ctx,
-                       "C:%s", lp_pathname(talloc_tos(), snum));
+                       "C:%s", lp_path(talloc_tos(), lp_sub, snum));
 
        if (path) {
                /*
@@ -250,7 +304,7 @@ static void init_srv_share_info_2(struct pipes_struct *p,
        r->comment              = remark ? remark : "";
        r->permissions          = 0;
        r->max_users            = max_uses;
-       r->current_users        = count_current_connections(net_name, false);
+       r->current_users        = 0; /* computed later */
        r->path                 = path ? path : "";
        r->password             = "";
 }
@@ -273,7 +327,7 @@ static void map_generic_share_sd_bits(struct security_descriptor *psd)
 
        for (i = 0; i < ps_dacl->num_aces; i++) {
                struct security_ace *psa = &ps_dacl->aces[i];
-               uint32 orig_mask = psa->access_mask;
+               uint32_t orig_mask = psa->access_mask;
 
                se_map_generic(&psa->access_mask, &file_generic_mapping);
                psa->access_mask |= orig_mask;
@@ -287,13 +341,15 @@ static void map_generic_share_sd_bits(struct security_descriptor *psd)
 static void init_srv_share_info_501(struct pipes_struct *p,
                                    struct srvsvc_NetShareInfo501 *r, int snum)
 {
-       const char *net_name = lp_servicename(talloc_tos(), snum);
-       char *remark = lp_comment(p->mem_ctx, snum);
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+       const char *net_name = lp_servicename(talloc_tos(), lp_sub, snum);
+       char *remark = lp_comment(p->mem_ctx, lp_sub, snum);
 
        if (remark) {
-               remark = talloc_sub_advanced(
-                       p->mem_ctx, lp_servicename(talloc_tos(), snum),
-                       get_current_username(), lp_pathname(talloc_tos(), snum),
+               remark = talloc_sub_full(
+                       p->mem_ctx, lp_servicename(talloc_tos(), lp_sub, snum),
+                       get_current_username(), lp_path(talloc_tos(), lp_sub, snum),
                        p->session_info->unix_token->uid, get_current_username(),
                        "", remark);
        }
@@ -301,7 +357,12 @@ static void init_srv_share_info_501(struct pipes_struct *p,
        r->name         = net_name;
        r->type         = get_share_type(snum);
        r->comment      = remark ? remark : "";
-       r->csc_policy   = (lp_csc_policy(snum) << 4);
+
+       /*
+        * According to [MS-SRVS] 2.2.4.25, the flags field is the same as in
+        * level 1005.
+        */
+       r->csc_policy   = (lp_csc_policy(snum) << SHARE_1005_CSC_POLICY_SHIFT);
 }
 
 /*******************************************************************
@@ -311,22 +372,24 @@ static void init_srv_share_info_501(struct pipes_struct *p,
 static void init_srv_share_info_502(struct pipes_struct *p,
                                    struct srvsvc_NetShareInfo502 *r, int snum)
 {
-       const char *net_name = lp_servicename(talloc_tos(), snum);
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+       const char *net_name = lp_servicename(talloc_tos(), lp_sub, snum);
        char *path = NULL;
        struct security_descriptor *sd = NULL;
        struct sec_desc_buf *sd_buf = NULL;
        size_t sd_size = 0;
        TALLOC_CTX *ctx = p->mem_ctx;
-       char *remark = lp_comment(ctx, snum);
+       char *remark = lp_comment(ctx, lp_sub, snum);
 
        if (remark) {
-               remark = talloc_sub_advanced(
-                       p->mem_ctx, lp_servicename(talloc_tos(), snum),
-                       get_current_username(), lp_pathname(talloc_tos(), snum),
+               remark = talloc_sub_full(
+                       p->mem_ctx, lp_servicename(talloc_tos(), lp_sub, snum),
+                       get_current_username(), lp_path(talloc_tos(), lp_sub, snum),
                        p->session_info->unix_token->uid, get_current_username(),
                        "", remark);
        }
-       path = talloc_asprintf(ctx, "C:%s", lp_pathname(talloc_tos(), snum));
+       path = talloc_asprintf(ctx, "C:%s", lp_path(talloc_tos(), lp_sub, snum));
        if (path) {
                /*
                 * Change / to \\ so that win2k will see it as a valid path.  This was added to
@@ -335,7 +398,7 @@ static void init_srv_share_info_502(struct pipes_struct *p,
                string_replace(path, '/', '\\');
        }
 
-       sd = get_share_security(ctx, lp_servicename(talloc_tos(), snum), &sd_size);
+       sd = get_share_security(ctx, lp_servicename(talloc_tos(), lp_sub, snum), &sd_size);
 
        sd_buf = make_sec_desc_buf(p->mem_ctx, sd_size, sd);
 
@@ -358,12 +421,14 @@ static void init_srv_share_info_1004(struct pipes_struct *p,
                                     struct srvsvc_NetShareInfo1004 *r,
                                     int snum)
 {
-       char *remark = lp_comment(p->mem_ctx, snum);
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+       char *remark = lp_comment(p->mem_ctx, lp_sub, snum);
 
        if (remark) {
-               remark = talloc_sub_advanced(
-                       p->mem_ctx, lp_servicename(talloc_tos(), snum),
-                       get_current_username(), lp_pathname(talloc_tos(), snum),
+               remark = talloc_sub_full(
+                       p->mem_ctx, lp_servicename(talloc_tos(), lp_sub, snum),
+                       get_current_username(), lp_path(talloc_tos(), lp_sub, snum),
                        p->session_info->unix_token->uid, get_current_username(),
                        "", remark);
        }
@@ -421,12 +486,14 @@ static void init_srv_share_info_1501(struct pipes_struct *p,
                                     struct sec_desc_buf **r,
                                     int snum)
 {
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        struct security_descriptor *sd;
        struct sec_desc_buf *sd_buf = NULL;
        size_t sd_size;
        TALLOC_CTX *ctx = p->mem_ctx;
 
-       sd = get_share_security(ctx, lp_servicename(talloc_tos(), snum), &sd_size);
+       sd = get_share_security(ctx, lp_servicename(talloc_tos(), lp_sub, snum), &sd_size);
        if (sd) {
                sd_buf = make_sec_desc_buf(p->mem_ctx, sd_size, sd);
        }
@@ -440,7 +507,9 @@ static void init_srv_share_info_1501(struct pipes_struct *p,
 
 static bool is_hidden_share(int snum)
 {
-       const char *net_name = lp_servicename(talloc_tos(), snum);
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+       const char *net_name = lp_servicename(talloc_tos(), lp_sub, snum);
 
        return (net_name[strlen(net_name) - 1] == '$') ? True : False;
 }
@@ -451,12 +520,65 @@ static bool is_hidden_share(int snum)
 static bool is_enumeration_allowed(struct pipes_struct *p,
                                    int snum)
 {
-    if (!lp_access_based_share_enum(snum))
-        return true;
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+
+       if (!lp_access_based_share_enum(snum)) {
+               return true;
+       }
+
+       if (!user_ok_token(p->session_info->unix_info->unix_name,
+                          p->session_info->info->domain_name,
+                          p->session_info->security_token, snum)) {
+               return false;
+       }
+
+       return share_access_check(p->session_info->security_token,
+                                 lp_servicename(talloc_tos(), lp_sub, snum),
+                                 FILE_READ_DATA, NULL);
+}
+
+/****************************************************************************
+ Count an entry against the respective service.
+****************************************************************************/
+
+static int count_for_all_fn(struct smbXsrv_tcon_global0 *tcon, void *udp)
+{
+       union srvsvc_NetShareCtr *ctr = NULL;
+       struct srvsvc_NetShareInfo2 *info2 = NULL;
+       int share_entries = 0;
+       int i = 0;
+
+       ctr = (union srvsvc_NetShareCtr *) udp;
+
+       /* for level 2 */
+       share_entries  = ctr->ctr2->count;
+       info2 = &ctr->ctr2->array[0];
+
+       for (i = 0; i < share_entries; i++, info2++) {
+               if (strequal(tcon->share_name, info2->name)) {
+                       info2->current_users++;
+                       break;
+               }
+       }
+
+       return 0;
+}
+
+/****************************************************************************
+ Count the entries belonging to all services in the connection db.
+****************************************************************************/
+
+static void count_connections_for_all_shares(union srvsvc_NetShareCtr *ctr)
+{
+       NTSTATUS status;
+       status = smbXsrv_tcon_global_traverse(count_for_all_fn, ctr);
 
-    return share_access_check(p->session_info->security_token,
-                             lp_servicename(talloc_tos(), snum),
-                             FILE_READ_DATA, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("count_connections_for_all_shares: traverse of "
+                       "smbXsrv_tcon_global.tdb failed - %s\n",
+                       nt_errstr(status)));
+       }
 }
 
 /*******************************************************************
@@ -469,13 +591,15 @@ static WERROR init_srv_share_info_ctr(struct pipes_struct *p,
                                      uint32_t *total_entries,
                                      bool all_shares)
 {
-       int num_entries = 0;
-       int alloc_entries = 0;
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
+       uint32_t num_entries = 0;
+       uint32_t alloc_entries = 0;
        int num_services = 0;
        int snum;
        TALLOC_CTX *ctx = p->mem_ctx;
-       int i = 0;
-       int valid_share_count = 0;
+       uint32_t i = 0;
+       uint32_t valid_share_count = 0;
        bool *allowed = 0;
        union srvsvc_NetShareCtr ctr;
        uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
@@ -484,6 +608,7 @@ static WERROR init_srv_share_info_ctr(struct pipes_struct *p,
 
        /* Ensure all the usershares are loaded. */
        become_root();
+       delete_and_reload_printers();
        load_usershare_shares(NULL, connections_snum_used);
        load_registry_shares();
        num_services = lp_numservices();
@@ -498,12 +623,12 @@ static WERROR init_srv_share_info_ctr(struct pipes_struct *p,
                     is_enumeration_allowed(p, snum) &&
                     (all_shares || !is_hidden_share(snum)) ) {
                         DEBUG(10, ("counting service %s\n",
-                               lp_servicename(talloc_tos(), snum) ? lp_servicename(talloc_tos(), snum) : "(null)"));
+                               lp_servicename(talloc_tos(), lp_sub, snum) ? lp_servicename(talloc_tos(), lp_sub, snum) : "(null)"));
                         allowed[snum] = true;
                         num_entries++;
                 } else {
                         DEBUG(10, ("NOT counting service %s\n",
-                               lp_servicename(talloc_tos(), snum) ? lp_servicename(talloc_tos(), snum) : "(null)"));
+                               lp_servicename(talloc_tos(), lp_sub, snum) ? lp_servicename(talloc_tos(), lp_sub, snum) : "(null)"));
                 }
         }
 
@@ -563,6 +688,7 @@ static WERROR init_srv_share_info_ctr(struct pipes_struct *p,
                        }
                }
 
+               count_connections_for_all_shares(&ctr);
                break;
 
        case 501:
@@ -689,7 +815,7 @@ static WERROR init_srv_share_info_ctr(struct pipes_struct *p,
        default:
                DEBUG(5,("init_srv_share_info_ctr: unsupported switch value %d\n",
                        info_ctr->level));
-               return WERR_UNKNOWN_LEVEL;
+               return WERR_INVALID_LEVEL;
        }
 
        *total_entries = alloc_entries;
@@ -756,36 +882,54 @@ static WERROR init_srv_sess_info_0(struct pipes_struct *p,
        return WERR_OK;
 }
 
-/*******************************************************************
-********************************************************************/
+/***********************************************************************
+ * find out the session on which this file is open and bump up its count
+ **********************************************************************/
 
-static void sess_file_fn( const struct share_mode_entry *e,
-                          const char *sharepath, const char *fname,
-                         void *data )
+static int count_sess_files_fn(struct file_id fid,
+                              const struct share_mode_data *d,
+                              const struct share_mode_entry *e,
+                              void *data)
 {
-       struct sess_file_count *sess = (struct sess_file_count *)data;
+       struct sess_file_info *info = data;
+       uint32_t rh = info->resume_handle;
+       int i;
 
-       if (serverid_equal(&e->pid, &sess->pid) && (sess->uid == e->uid)) {
-               sess->count++;
+       for (i=0; i < info->num_entries; i++) {
+               /* rh+info->num_entries is safe, as we've
+                  ensured that:
+                  *total_entries > resume_handle &&
+                  info->num_entries = *total_entries - resume_handle;
+                  inside init_srv_sess_info_1() below.
+               */
+               struct sessionid *sess = &info->session_list[rh + i];
+               if ((e->uid == sess->uid) &&
+                    server_id_equal(&e->pid, &sess->pid)) {
+
+                       info->ctr->array[i].num_open++;
+                       return 0;
+               }
        }
-
-       return;
+       return 0;
 }
 
 /*******************************************************************
-********************************************************************/
+ * count the num of open files on all sessions
+ *******************************************************************/
 
-static int net_count_files( uid_t uid, struct server_id pid )
+static void net_count_files_for_all_sess(struct srvsvc_NetSessCtr1 *ctr1,
+                                        struct sessionid *session_list,
+                                        uint32_t resume_handle,
+                                        uint32_t num_entries)
 {
-       struct sess_file_count s_file_cnt;
-
-       s_file_cnt.count = 0;
-       s_file_cnt.uid = uid;
-       s_file_cnt.pid = pid;
+       struct sess_file_info s_file_info;
 
-       share_mode_forall( sess_file_fn, &s_file_cnt );
+       s_file_info.ctr = ctr1;
+       s_file_info.session_list = session_list;
+       s_file_info.resume_handle = resume_handle;
+       s_file_info.num_entries = num_entries;
 
-       return s_file_cnt.count;
+       share_entry_forall(count_sess_files_fn, &s_file_info);
 }
 
 /*******************************************************************
@@ -813,40 +957,46 @@ static WERROR init_srv_sess_info_1(struct pipes_struct *p,
 
        *total_entries = list_sessions(p->mem_ctx, &session_list);
 
-       for (; resume_handle < *total_entries; resume_handle++) {
-               uint32 num_files;
-               uint32 connect_time;
-               struct passwd *pw = getpwnam(session_list[resume_handle].username);
-               bool guest;
-
-               if ( !pw ) {
-                       DEBUG(10,("init_srv_sess_info_1: failed to find owner: %s\n",
-                               session_list[resume_handle].username));
-                       continue;
+       if (resume_handle >= *total_entries) {
+               if (resume_handle_p) {
+                       *resume_handle_p = 0;
                }
+               return WERR_OK;
+       }
 
-               connect_time = (uint32_t)(now - session_list[resume_handle].connect_start);
-               num_files = net_count_files(pw->pw_uid, session_list[resume_handle].pid);
-               guest = strequal( session_list[resume_handle].username, lp_guestaccount() );
+       /* We know num_entries must be positive, due to
+          the check resume_handle >= *total_entries above. */
 
-               ctr1->array = talloc_realloc(p->mem_ctx,
-                                                  ctr1->array,
-                                                  struct srvsvc_NetSessInfo1,
-                                                  num_entries+1);
-               W_ERROR_HAVE_NO_MEMORY(ctr1->array);
+       num_entries = *total_entries - resume_handle;
+
+       ctr1->array = talloc_zero_array(p->mem_ctx,
+                                  struct srvsvc_NetSessInfo1,
+                                  num_entries);
+
+       W_ERROR_HAVE_NO_MEMORY(ctr1->array);
+
+       for (num_entries = 0; resume_handle < *total_entries; num_entries++, resume_handle++) {
+               uint32_t connect_time;
+               bool guest;
+
+               connect_time = (uint32_t)(now - session_list[resume_handle].connect_start);
+               guest = strequal( session_list[resume_handle].username, lp_guest_account() );
 
                ctr1->array[num_entries].client         = session_list[resume_handle].remote_machine;
                ctr1->array[num_entries].user           = session_list[resume_handle].username;
-               ctr1->array[num_entries].num_open       = num_files;
+               ctr1->array[num_entries].num_open       = 0;/* computed later */
                ctr1->array[num_entries].time           = connect_time;
                ctr1->array[num_entries].idle_time      = 0;
                ctr1->array[num_entries].user_flags     = guest;
-
-               num_entries++;
        }
 
        ctr1->count = num_entries;
 
+       /* count open files on all sessions in single tdb traversal */
+       net_count_files_for_all_sess(ctr1, session_list,
+                                    resume_handle_p ? *resume_handle_p : 0,
+                                    num_entries);
+
        if (resume_handle_p) {
                if (*resume_handle_p >= *total_entries) {
                        *resume_handle_p = 0;
@@ -858,6 +1008,107 @@ static WERROR init_srv_sess_info_1(struct pipes_struct *p,
        return WERR_OK;
 }
 
+/*******************************************************************
+ find the share connection on which this open exists.
+ ********************************************************************/
+
+static int share_file_fn(struct file_id fid,
+                        const struct share_mode_data *d,
+                        const struct share_mode_entry *e,
+                        void *data)
+{
+       struct share_file_stat *sfs = data;
+       uint32_t i;
+       uint32_t offset = sfs->total_entries - sfs->resp_entries;
+
+       if (strequal(d->servicepath, sfs->in_sharepath)) {
+               for (i=0; i < sfs->resp_entries; i++) {
+                       if (server_id_equal(
+                                   &e->pid, &sfs->svrid_arr[offset + i])) {
+                               sfs->netconn_arr[i].num_open ++;
+                               return 0;
+                       }
+               }
+       }
+       return 0;
+}
+
+/*******************************************************************
+ count number of open files on given share connections.
+ ********************************************************************/
+
+static void count_share_opens(struct srvsvc_NetConnInfo1 *arr,
+                             struct server_id *svrid_arr, char *sharepath,
+                             uint32_t resp_entries, uint32_t total_entries)
+{
+       struct share_file_stat sfs;
+
+       sfs.netconn_arr = arr;
+       sfs.svrid_arr = svrid_arr;
+       sfs.in_sharepath = sharepath;
+       sfs.resp_entries = resp_entries;
+       sfs.total_entries = total_entries;
+
+       share_entry_forall(share_file_fn, &sfs);
+}
+
+/****************************************************************************
+ process an entry from the connection db.
+****************************************************************************/
+
+static int share_conn_fn(struct smbXsrv_tcon_global0 *tcon,
+                         void *data)
+{
+       struct share_conn_stat *scs = data;
+
+       if (!process_exists(tcon->server_id)) {
+               return 0;
+       }
+
+       if (strequal(tcon->share_name, scs->sharename)) {
+               scs->svrid_arr = talloc_realloc(scs->ctx, scs->svrid_arr,
+                                               struct server_id,
+                                               scs->count + 1);
+               if (!scs->svrid_arr) {
+                       return 0;
+               }
+
+               scs->svrid_arr[scs->count] = tcon->server_id;
+               scs->count++;
+       }
+
+       return 0;
+}
+
+/****************************************************************************
+ Count the connections to a share. Build an array of serverid's owning these
+ connections.
+****************************************************************************/
+
+static uint32_t count_share_conns(TALLOC_CTX *ctx, const char *sharename,
+                                 struct server_id **arr)
+{
+       struct share_conn_stat scs;
+       NTSTATUS status;
+
+       scs.ctx = ctx;
+       scs.sharename = sharename;
+       scs.svrid_arr = NULL;
+       scs.count = 0;
+
+       status = smbXsrv_tcon_global_traverse(share_conn_fn, &scs);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("count_share_conns: traverse of "
+                        "smbXsrv_tcon_global.tdb failed - %s\n",
+                        nt_errstr(status)));
+               return 0;
+       }
+
+       *arr = scs.svrid_arr;
+       return scs.count;
+}
+
 /*******************************************************************
  fill in a conn info level 0 structure.
  ********************************************************************/
@@ -889,7 +1140,7 @@ static WERROR init_srv_conn_info_0(struct srvsvc_NetConnCtr0 *ctr0,
                                                   struct srvsvc_NetConnInfo0,
                                                   num_entries+1);
                if (!ctr0->array) {
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
                }
 
                ctr0->array[num_entries].conn_id = *total_entries;
@@ -916,12 +1167,18 @@ static WERROR init_srv_conn_info_0(struct srvsvc_NetConnCtr0 *ctr0,
  fill in a conn info level 1 structure.
  ********************************************************************/
 
-static WERROR init_srv_conn_info_1(struct srvsvc_NetConnCtr1 *ctr1,
+static WERROR init_srv_conn_info_1(const char *name,
+                                  struct srvsvc_NetConnCtr1 *ctr1,
                                   uint32_t *resume_handle_p,
                                   uint32_t *total_entries)
 {
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        uint32_t num_entries = 0;
+       int snum = 0;
        uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
+       char *share_name = NULL;
+       struct server_id *svrid_arr = NULL;
 
        DEBUG(5,("init_srv_conn_info_1\n"));
 
@@ -932,41 +1189,109 @@ static WERROR init_srv_conn_info_1(struct srvsvc_NetConnCtr1 *ctr1,
                return WERR_OK;
        }
 
-       *total_entries = 1;
+       /* check if this is a server name or a share name */
+       if (name && (strlen(name) > 2)  && (name[0] == '\\') &&
+                       (name[1] == '\\')) {
 
-       ZERO_STRUCTP(ctr1);
+               /* 'name' is a server name - this part is unimplemented */
+               *total_entries = 1;
+       } else {
+                /* 'name' is a share name */
+               snum = find_service(talloc_tos(), name, &share_name);
 
-       for (; resume_handle < *total_entries; resume_handle++) {
+               if (!share_name) {
+                       return WERR_NOT_ENOUGH_MEMORY;
+               }
 
-               ctr1->array = talloc_realloc(talloc_tos(),
-                                                  ctr1->array,
-                                                  struct srvsvc_NetConnInfo1,
-                                                  num_entries+1);
-               if (!ctr1->array) {
-                       return WERR_NOMEM;
+               if (snum < 0) {
+                       return WERR_INVALID_NAME;
                }
 
+               /*
+                * count the num of connections to this share. Also,
+                * build a list of serverid's that own these
+                * connections. The serverid list is used later to
+                * identify the share connection on which an open exists.
+                */
+
+               *total_entries = count_share_conns(talloc_tos(),
+                                                  share_name,
+                                                  &svrid_arr);
+       }
+
+       if (resume_handle >= *total_entries) {
+               if (resume_handle_p) {
+                       *resume_handle_p = 0;
+               }
+               return WERR_OK;
+       }
+
+       /*
+        * We know num_entries must be positive, due to
+        * the check resume_handle >= *total_entries above.
+        */
+
+       num_entries = *total_entries - resume_handle;
+
+       ZERO_STRUCTP(ctr1);
+
+       ctr1->array = talloc_zero_array(talloc_tos(),
+                                       struct srvsvc_NetConnInfo1,
+                                       num_entries);
+
+       W_ERROR_HAVE_NO_MEMORY(ctr1->array);
+
+       for (num_entries = 0; resume_handle < *total_entries;
+               num_entries++, resume_handle++) {
+
                ctr1->array[num_entries].conn_id        = *total_entries;
                ctr1->array[num_entries].conn_type      = 0x3;
-               ctr1->array[num_entries].num_open       = 1;
+
+               /*
+                * if these are connections to a share, we are going to
+                * compute the opens on them later. If it's for the server,
+                * it's unimplemented.
+                */
+
+               if (!share_name) {
+                       ctr1->array[num_entries].num_open = 1;
+               }
+
                ctr1->array[num_entries].num_users      = 1;
                ctr1->array[num_entries].conn_time      = 3;
                ctr1->array[num_entries].user           = "dummy_user";
                ctr1->array[num_entries].share          = "IPC$";
+       }
+
+       /* now compute open files on the share connections */
+
+       if (share_name) {
+
+               /*
+                * the locking tdb, which has the open files information,
+                * does not store share name or share (service) number, but
+                * just the share path. So, we can compute open files only
+                * on the share path. If more than one shares are  defined
+                * on a share path, open files on all of them are included
+                * in the count.
+                *
+                * To have the correct behavior in case multiple shares
+                * are defined on the same path, changes to tdb records
+                * would be required. That would be lot more effort, so
+                * this seems a good stopgap fix.
+                */
+
+               count_share_opens(ctr1->array, svrid_arr,
+                                 lp_path(talloc_tos(), lp_sub, snum),
+                                 num_entries, *total_entries);
 
-               /* move on to creating next connection */
-               num_entries++;
        }
 
        ctr1->count = num_entries;
        *total_entries = num_entries;
 
        if (resume_handle_p) {
-               if (*resume_handle_p >= *total_entries) {
-                       *resume_handle_p = 0;
-               } else {
-                       *resume_handle_p = resume_handle;
-               }
+               *resume_handle_p = resume_handle;
        }
 
        return WERR_OK;
@@ -988,7 +1313,7 @@ WERROR _srvsvc_NetFileEnum(struct pipes_struct *p,
        case 3:
                break;
        default:
-               return WERR_UNKNOWN_LEVEL;
+               return WERR_INVALID_LEVEL;
        }
 
        if (!nt_token_check_sid(&global_sid_Builtin_Administrators,
@@ -1001,7 +1326,7 @@ WERROR _srvsvc_NetFileEnum(struct pipes_struct *p,
        ctx = talloc_tos();
        ctr3 = r->in.info_ctr->ctr.ctr3;
        if (!ctr3) {
-               werr = WERR_INVALID_PARAM;
+               werr = WERR_INVALID_PARAMETER;
                goto done;
        }
 
@@ -1031,6 +1356,8 @@ WERROR _srvsvc_NetFileEnum(struct pipes_struct *p,
 WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
                             struct srvsvc_NetSrvGetInfo *r)
 {
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        WERROR status = WERR_OK;
 
        DEBUG(5,("_srvsvc_NetSrvGetInfo: %d\n", __LINE__));
@@ -1051,7 +1378,7 @@ WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
 
                info102 = talloc(p->mem_ctx, struct srvsvc_NetSrvInfo102);
                if (!info102) {
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
                }
 
                info102->platform_id    = PLATFORM_ID_NT;
@@ -1059,7 +1386,7 @@ WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
                info102->version_major  = SAMBA_MAJOR_NBT_ANNOUNCE_VERSION;
                info102->version_minor  = SAMBA_MINOR_NBT_ANNOUNCE_VERSION;
                info102->server_type    = lp_default_server_announce();
-               info102->comment        = string_truncate(lp_serverstring(talloc_tos()),
+               info102->comment        = string_truncate(lp_server_string(talloc_tos(), lp_sub),
                                                MAX_SERVER_STRING_LENGTH);
                info102->users          = 0xffffffff;
                info102->disc           = 0xf;
@@ -1077,7 +1404,7 @@ WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
 
                info101 = talloc(p->mem_ctx, struct srvsvc_NetSrvInfo101);
                if (!info101) {
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
                }
 
                info101->platform_id    = PLATFORM_ID_NT;
@@ -1085,7 +1412,7 @@ WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
                info101->version_major  = SAMBA_MAJOR_NBT_ANNOUNCE_VERSION;
                info101->version_minor  = SAMBA_MINOR_NBT_ANNOUNCE_VERSION;
                info101->server_type    = lp_default_server_announce();
-               info101->comment        = string_truncate(lp_serverstring(talloc_tos()),
+               info101->comment        = string_truncate(lp_server_string(talloc_tos(), lp_sub),
                                                MAX_SERVER_STRING_LENGTH);
 
                r->out.info->info101 = info101;
@@ -1096,7 +1423,7 @@ WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
 
                info100 = talloc(p->mem_ctx, struct srvsvc_NetSrvInfo100);
                if (!info100) {
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
                }
 
                info100->platform_id    = PLATFORM_ID_NT;
@@ -1107,7 +1434,7 @@ WERROR _srvsvc_NetSrvGetInfo(struct pipes_struct *p,
                break;
        }
        default:
-               status = WERR_UNKNOWN_LEVEL;
+               status = WERR_INVALID_LEVEL;
                break;
        }
 
@@ -1159,12 +1486,13 @@ WERROR _srvsvc_NetConnEnum(struct pipes_struct *p,
                                                    r->out.totalentries);
                        break;
                case 1:
-                       werr = init_srv_conn_info_1(r->in.info_ctr->ctr.ctr1,
+                       werr = init_srv_conn_info_1(r->in.path,
+                                                   r->in.info_ctr->ctr.ctr1,
                                                    r->in.resume_handle,
                                                    r->out.totalentries);
                        break;
                default:
-                       return WERR_UNKNOWN_LEVEL;
+                       return WERR_INVALID_LEVEL;
        }
 
        DEBUG(5,("_srvsvc_NetConnEnum: %d\n", __LINE__));
@@ -1204,7 +1532,7 @@ WERROR _srvsvc_NetSessEnum(struct pipes_struct *p,
                                                    r->out.totalentries);
                        break;
                default:
-                       return WERR_UNKNOWN_LEVEL;
+                       return WERR_INVALID_LEVEL;
        }
 
        DEBUG(5,("_srvsvc_NetSessEnum: %d\n", __LINE__));
@@ -1226,16 +1554,6 @@ WERROR _srvsvc_NetSessDel(struct pipes_struct *p,
        bool not_root = False;
        WERROR werr;
 
-       username = r->in.user;
-       machine = r->in.client;
-
-       /* strip leading backslashes if any */
-       if (machine && machine[0] == '\\' && machine[1] == '\\') {
-               machine += 2;
-       }
-
-       num_sessions = list_sessions(p->mem_ctx, &session_list);
-
        DEBUG(5,("_srvsvc_NetSessDel: %d\n", __LINE__));
 
        werr = WERR_ACCESS_DENIED;
@@ -1249,28 +1567,35 @@ WERROR _srvsvc_NetSessDel(struct pipes_struct *p,
                goto done;
        }
 
-       for (snum = 0; snum < num_sessions; snum++) {
-
-               if ((strequal(session_list[snum].username, username) || username[0] == '\0' ) &&
-                   strequal(session_list[snum].remote_machine, machine)) {
+       username = r->in.user;
+       machine = r->in.client;
 
-                       NTSTATUS ntstat;
+       /* strip leading backslashes if any */
+       if (machine && machine[0] == '\\' && machine[1] == '\\') {
+               machine += 2;
+       }
 
-                       if (p->session_info->unix_token->uid != sec_initial_uid()) {
-                               not_root = True;
-                               become_root();
-                       }
+       num_sessions = find_sessions(p->mem_ctx, username, machine,
+                                    &session_list);
 
-                       ntstat = messaging_send(p->msg_ctx,
-                                               session_list[snum].pid,
-                                               MSG_SHUTDOWN, &data_blob_null);
+       for (snum = 0; snum < num_sessions; snum++) {
 
-                       if (NT_STATUS_IS_OK(ntstat))
-                               werr = WERR_OK;
+               NTSTATUS ntstat;
 
-                       if (not_root)
-                               unbecome_root();
+               if (p->session_info->unix_token->uid != sec_initial_uid()) {
+                       not_root = True;
+                       become_root();
                }
+
+               ntstat = messaging_send(p->msg_ctx,
+                                       session_list[snum].pid,
+                                       MSG_SHUTDOWN, &data_blob_null);
+
+               if (NT_STATUS_IS_OK(ntstat))
+                       werr = WERR_OK;
+
+               if (not_root)
+                       unbecome_root();
        }
 
        DEBUG(5,("_srvsvc_NetSessDel: %d\n", __LINE__));
@@ -1356,7 +1681,7 @@ WERROR _srvsvc_NetShareGetInfo(struct pipes_struct *p,
 
        snum = find_service(talloc_tos(), r->in.share_name, &share_name);
        if (!share_name) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
        if (snum < 0) {
                return WERR_INVALID_NAME;
@@ -1377,6 +1702,8 @@ WERROR _srvsvc_NetShareGetInfo(struct pipes_struct *p,
                        info->info2 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo2);
                        W_ERROR_HAVE_NO_MEMORY(info->info2);
                        init_srv_share_info_2(p, info->info2, snum);
+                       info->info2->current_users =
+                         count_current_connections(info->info2->name, false);
                        break;
                case 501:
                        info->info501 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo501);
@@ -1414,7 +1741,7 @@ WERROR _srvsvc_NetShareGetInfo(struct pipes_struct *p,
                default:
                        DEBUG(5,("_srvsvc_NetShareGetInfo: unsupported switch value %d\n",
                                r->in.level));
-                       status = WERR_UNKNOWN_LEVEL;
+                       status = WERR_INVALID_LEVEL;
                        break;
        }
 
@@ -1430,6 +1757,8 @@ WERROR _srvsvc_NetShareGetInfo(struct pipes_struct *p,
 WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
                               struct srvsvc_NetShareSetInfo *r)
 {
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        char *command = NULL;
        char *share_name = NULL;
        char *comment = NULL;
@@ -1440,6 +1769,11 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
        char *path = NULL;
        struct security_descriptor *psd = NULL;
        bool is_disk_op = False;
+       const char *csc_policy = NULL;
+       bool csc_policy_changed = false;
+       const char *csc_policies[] = {"manual", "documents", "programs",
+                                     "disable"};
+       uint32_t client_csc_policy;
        int max_connections = 0;
        TALLOC_CTX *ctx = p->mem_ctx;
        union srvsvc_NetShareInfo *info = r->in.info;
@@ -1466,15 +1800,15 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
 
        snum = find_service(talloc_tos(), r->in.share_name, &share_name);
        if (!share_name) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
 
        /* Does this share exist ? */
        if (snum < 0)
-               return WERR_NET_NAME_NOT_FOUND;
+               return WERR_NERR_NETNAMENOTFOUND;
 
        /* No change to printer shares. */
-       if (lp_print_ok(snum))
+       if (lp_printable(snum))
                return WERR_ACCESS_DENIED;
 
        is_disk_op = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_DISK_OPERATOR);
@@ -1490,9 +1824,12 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
+       max_connections = lp_max_connections(snum);
+       csc_policy = csc_policies[lp_csc_policy(snum)];
+
        switch (r->in.level) {
        case 1:
-               pathname = lp_pathname(ctx, snum);
+               pathname = lp_path(ctx, lp_sub, snum);
                comment = talloc_strdup(ctx, info->info1->comment);
                type = info->info1->type;
                psd = NULL;
@@ -1521,7 +1858,7 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
                map_generic_share_sd_bits(psd);
                break;
        case 1004:
-               pathname = lp_pathname(ctx, snum);
+               pathname = lp_path(ctx, lp_sub, snum);
                comment = talloc_strdup(ctx, info->info1004->comment);
                type = STYPE_DISKTREE;
                break;
@@ -1530,20 +1867,27 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
                   user, so we must compare it to see if it's what is set in
                   smb.conf, so that we can contine other ops like setting
                   ACLs on a share */
-               if (((info->info1005->dfs_flags &
-                     SHARE_1005_CSC_POLICY_MASK) >>
-                    SHARE_1005_CSC_POLICY_SHIFT) == lp_csc_policy(snum))
+               client_csc_policy = (info->info1005->dfs_flags &
+                                    SHARE_1005_CSC_POLICY_MASK) >>
+                                   SHARE_1005_CSC_POLICY_SHIFT;
+
+               if (client_csc_policy == lp_csc_policy(snum))
                        return WERR_OK;
                else {
-                       DEBUG(3, ("_srvsvc_NetShareSetInfo: client is trying to change csc policy from the network; must be done with smb.conf\n"));
-                       return WERR_ACCESS_DENIED;
+                       csc_policy = csc_policies[client_csc_policy];
+                       csc_policy_changed = true;
                }
+
+               pathname = lp_path(ctx, lp_sub, snum);
+               comment = lp_comment(ctx, lp_sub, snum);
+               type = STYPE_DISKTREE;
+               break;
        case 1006:
        case 1007:
                return WERR_ACCESS_DENIED;
        case 1501:
-               pathname = lp_pathname(ctx, snum);
-               comment = lp_comment(ctx, snum);
+               pathname = lp_path(ctx, lp_sub, snum);
+               comment = lp_comment(ctx, lp_sub, snum);
                psd = info->info1501->sd;
                map_generic_share_sd_bits(psd);
                type = STYPE_DISKTREE;
@@ -1551,7 +1895,7 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
        default:
                DEBUG(5,("_srvsvc_NetShareSetInfo: unsupported switch value %d\n",
                        r->in.level));
-               return WERR_UNKNOWN_LEVEL;
+               return WERR_INVALID_LEVEL;
        }
 
        /* We can only modify disk shares. */
@@ -1563,14 +1907,14 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
        }
 
        if (comment == NULL) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
 
        /* Check if the pathname is valid. */
        if (!(path = valid_share_pathname(p->mem_ctx, pathname ))) {
                DEBUG(5,("_srvsvc_NetShareSetInfo: invalid pathname %s\n",
                        pathname ));
-               return WERR_OBJECT_PATH_INVALID;
+               return WERR_BAD_PATHNAME;
        }
 
        /* Ensure share name, pathname and comment don't contain '"' characters. */
@@ -1579,27 +1923,31 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
        string_replace(comment, '"', ' ');
 
        DEBUG(10,("_srvsvc_NetShareSetInfo: change share command = %s\n",
-               lp_change_share_cmd(talloc_tos()) ? lp_change_share_cmd(talloc_tos()) : "NULL" ));
+               lp_change_share_command(talloc_tos(), lp_sub) ? lp_change_share_command(talloc_tos(), lp_sub) : "NULL" ));
 
        /* Only call modify function if something changed. */
 
-       if (strcmp(path, lp_pathname(talloc_tos(), snum)) || strcmp(comment, lp_comment(talloc_tos(), snum))
-                       || (lp_max_connections(snum) != max_connections)) {
-               if (!lp_change_share_cmd(talloc_tos()) || !*lp_change_share_cmd(talloc_tos())) {
+       if (strcmp(path, lp_path(talloc_tos(), lp_sub, snum))
+                       || strcmp(comment, lp_comment(talloc_tos(), lp_sub, snum))
+                       || (lp_max_connections(snum) != max_connections)
+                       || csc_policy_changed) {
+
+               if (!lp_change_share_command(talloc_tos(), lp_sub) || !*lp_change_share_command(talloc_tos(), lp_sub)) {
                        DEBUG(10,("_srvsvc_NetShareSetInfo: No change share command\n"));
                        return WERR_ACCESS_DENIED;
                }
 
                command = talloc_asprintf(p->mem_ctx,
-                               "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
-                               lp_change_share_cmd(talloc_tos()),
+                               "%s \"%s\" \"%s\" \"%s\" \"%s\" %d \"%s\"",
+                               lp_change_share_command(talloc_tos(), lp_sub),
                                get_dyn_CONFIGFILE(),
                                share_name,
                                path,
-                               comment ? comment : "",
-                               max_connections);
+                               comment,
+                               max_connections,
+                               csc_policy);
                if (!command) {
-                       return WERR_NOMEM;
+                       return WERR_NOT_ENOUGH_MEMORY;
                }
 
                DEBUG(10,("_srvsvc_NetShareSetInfo: Running [%s]\n", command ));
@@ -1609,10 +1957,13 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
                if (is_disk_op)
                        become_root();
 
-               if ( (ret = smbrun(command, NULL)) == 0 ) {
+               ret = smbrun(command, NULL, NULL);
+               if (ret == 0) {
+                       reload_services(NULL, NULL, false);
+
                        /* Tell everyone we updated smb.conf. */
-                       message_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED,
-                                        NULL, 0, NULL);
+                       messaging_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED,
+                                          NULL, 0);
                }
 
                if ( is_disk_op )
@@ -1636,13 +1987,16 @@ WERROR _srvsvc_NetShareSetInfo(struct pipes_struct *p,
        if (psd) {
                struct security_descriptor *old_sd;
                size_t sd_size;
+               NTSTATUS status;
 
-               old_sd = get_share_security(p->mem_ctx, lp_servicename(talloc_tos(), snum), &sd_size);
+               old_sd = get_share_security(p->mem_ctx, lp_servicename(talloc_tos(), lp_sub, snum), &sd_size);
 
                if (old_sd && !security_descriptor_equal(old_sd, psd)) {
-                       if (!set_share_security(share_name, psd))
+                       status = set_share_security(share_name, psd);
+                       if (!NT_STATUS_IS_OK(status)) {
                                DEBUG(0,("_srvsvc_NetShareSetInfo: Failed to change security info in share %s.\n",
                                        share_name ));
+                       }
                }
        }
 
@@ -1672,7 +2026,10 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
        struct security_descriptor *psd = NULL;
        bool is_disk_op;
        int max_connections = 0;
+       SMB_STRUCT_STAT st;
        TALLOC_CTX *ctx = p->mem_ctx;
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
 
        DEBUG(5,("_srvsvc_NetShareAdd: %d\n", __LINE__));
 
@@ -1685,8 +2042,8 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
        if (p->session_info->unix_token->uid != sec_initial_uid()  && !is_disk_op )
                return WERR_ACCESS_DENIED;
 
-       if (!lp_add_share_cmd(talloc_tos()) || !*lp_add_share_cmd(talloc_tos())) {
-               DEBUG(10,("_srvsvc_NetShareAdd: No add share command\n"));
+       if (!lp_add_share_command(talloc_tos(), lp_sub) || !*lp_add_share_command(talloc_tos(), lp_sub)) {
+               DBG_WARNING("_srvsvc_NetShareAdd: No \"add share command\" parameter set in smb.conf.\n");
                return WERR_ACCESS_DENIED;
        }
 
@@ -1732,7 +2089,7 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
        default:
                DEBUG(5,("_srvsvc_NetShareAdd: unsupported switch value %d\n",
                        r->in.level));
-               return WERR_UNKNOWN_LEVEL;
+               return WERR_INVALID_LEVEL;
        }
 
        /* check for invalid share names */
@@ -1753,7 +2110,7 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
 
        snum = find_service(ctx, share_name_in, &share_name);
        if (!share_name) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
 
        /* Share already exists. */
@@ -1768,7 +2125,17 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
 
        /* Check if the pathname is valid. */
        if (!(path = valid_share_pathname(p->mem_ctx, pathname))) {
-               return WERR_OBJECT_PATH_INVALID;
+               return WERR_BAD_PATHNAME;
+       }
+
+       ret = sys_lstat(path, &st, false);
+       if (ret == -1 && (errno != EACCES)) {
+               /*
+                * If path has any other than permission
+                * problem, return WERR_FILE_NOT_FOUND (as Windows
+                * does.
+                */
+               return WERR_FILE_NOT_FOUND;
        }
 
        /* Ensure share name, pathname and comment don't contain '"' characters. */
@@ -1781,14 +2148,14 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
 
        command = talloc_asprintf(ctx,
                        "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
-                       lp_add_share_cmd(talloc_tos()),
+                       lp_add_share_command(talloc_tos(), lp_sub),
                        get_dyn_CONFIGFILE(),
                        share_name_in,
                        path,
                        comment ? comment : "",
                        max_connections);
        if (!command) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
 
        DEBUG(10,("_srvsvc_NetShareAdd: Running [%s]\n", command ));
@@ -1800,10 +2167,10 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
 
        /* FIXME: use libnetconf here - gd */
 
-       if ( (ret = smbrun(command, NULL)) == 0 ) {
+       ret = smbrun(command, NULL, NULL);
+       if (ret == 0) {
                /* Tell everyone we updated smb.conf. */
-               message_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0,
-                                NULL);
+               messaging_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0);
        }
 
        if ( is_disk_op )
@@ -1820,9 +2187,11 @@ WERROR _srvsvc_NetShareAdd(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
 
        if (psd) {
+               NTSTATUS status;
                /* Note we use share_name here, not share_name_in as
                   we need a canonicalized name for setting security. */
-               if (!set_share_security(share_name, psd)) {
+               status = set_share_security(share_name, psd);
+               if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(0,("_srvsvc_NetShareAdd: Failed to add security info to share %s.\n",
                                share_name ));
                }
@@ -1853,13 +2222,14 @@ WERROR _srvsvc_NetShareDel(struct pipes_struct *p,
        int ret;
        int snum;
        bool is_disk_op;
-       struct share_params *params;
        TALLOC_CTX *ctx = p->mem_ctx;
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
 
        DEBUG(5,("_srvsvc_NetShareDel: %d\n", __LINE__));
 
        if (!r->in.share_name) {
-               return WERR_NET_NAME_NOT_FOUND;
+               return WERR_NERR_NETNAMENOTFOUND;
        }
 
        if ( strequal(r->in.share_name,"IPC$")
@@ -1871,19 +2241,15 @@ WERROR _srvsvc_NetShareDel(struct pipes_struct *p,
 
        snum = find_service(talloc_tos(), r->in.share_name, &share_name);
        if (!share_name) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
 
        if (snum < 0) {
-               return WERR_NO_SUCH_SHARE;
-       }
-
-       if (!(params = get_share_params(p->mem_ctx, share_name))) {
-               return WERR_NO_SUCH_SHARE;
+               return WERR_BAD_NET_NAME;
        }
 
        /* No change to printer shares. */
-       if (lp_print_ok(snum))
+       if (lp_printable(snum))
                return WERR_ACCESS_DENIED;
 
        is_disk_op = security_token_has_privilege(p->session_info->security_token, SEC_PRIV_DISK_OPERATOR);
@@ -1891,18 +2257,18 @@ WERROR _srvsvc_NetShareDel(struct pipes_struct *p,
        if (p->session_info->unix_token->uid != sec_initial_uid()  && !is_disk_op )
                return WERR_ACCESS_DENIED;
 
-       if (!lp_delete_share_cmd(talloc_tos()) || !*lp_delete_share_cmd(talloc_tos())) {
-               DEBUG(10,("_srvsvc_NetShareDel: No delete share command\n"));
+       if (!lp_delete_share_command(talloc_tos(), lp_sub) || !*lp_delete_share_command(talloc_tos(), lp_sub)) {
+               DBG_WARNING("_srvsvc_NetShareDel: No \"delete share command\" parameter set in smb.conf.\n");
                return WERR_ACCESS_DENIED;
        }
 
        command = talloc_asprintf(ctx,
                        "%s \"%s\" \"%s\"",
-                       lp_delete_share_cmd(talloc_tos()),
+                       lp_delete_share_command(talloc_tos(), lp_sub),
                        get_dyn_CONFIGFILE(),
-                       lp_servicename(talloc_tos(), snum));
+                       share_name);
        if (!command) {
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
        }
 
        DEBUG(10,("_srvsvc_NetShareDel: Running [%s]\n", command ));
@@ -1912,10 +2278,10 @@ WERROR _srvsvc_NetShareDel(struct pipes_struct *p,
        if ( is_disk_op )
                become_root();
 
-       if ( (ret = smbrun(command, NULL)) == 0 ) {
+       ret = smbrun(command, NULL, NULL);
+       if (ret == 0) {
                /* Tell everyone we updated smb.conf. */
-               message_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0,
-                                NULL);
+               messaging_send_all(p->msg_ctx, MSG_SMB_CONF_UPDATED, NULL, 0);
        }
 
        if ( is_disk_op )
@@ -1929,9 +2295,9 @@ WERROR _srvsvc_NetShareDel(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
 
        /* Delete the SD in the database. */
-       delete_share_security(lp_servicename(talloc_tos(), params->service));
+       delete_share_security(share_name);
 
-       lp_killservice(params->service);
+       lp_killservice(snum);
 
        return WERR_OK;
 }
@@ -1968,12 +2334,12 @@ WERROR _srvsvc_NetRemoteTOD(struct pipes_struct *p,
        /* We do this call first as if we do it *after* the gmtime call
           it overwrites the pointed-to values. JRA */
 
-       uint32 zone = get_time_zone(unixdate)/60;
+       uint32_t zone = get_time_zone(unixdate)/60;
 
        DEBUG(5,("_srvsvc_NetRemoteTOD: %d\n", __LINE__));
 
        if ( !(tod = talloc_zero(p->mem_ctx, struct srvsvc_NetRemoteTODInfo)) )
-               return WERR_NOMEM;
+               return WERR_NOT_ENOUGH_MEMORY;
 
        *r->out.info = tod;
 
@@ -2008,54 +2374,57 @@ WERROR _srvsvc_NetRemoteTOD(struct pipes_struct *p,
 WERROR _srvsvc_NetGetFileSecurity(struct pipes_struct *p,
                                  struct srvsvc_NetGetFileSecurity *r)
 {
+       TALLOC_CTX *frame = talloc_stackframe();
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        struct smb_filename *smb_fname = NULL;
        size_t sd_size;
        char *servicename = NULL;
        SMB_STRUCT_STAT st;
        NTSTATUS nt_status;
        WERROR werr;
+       struct conn_struct_tos *c = NULL;
        connection_struct *conn = NULL;
        struct sec_desc_buf *sd_buf = NULL;
        files_struct *fsp = NULL;
        int snum;
-       char *oldcwd = NULL;
+       uint32_t ucf_flags = 0;
 
        ZERO_STRUCT(st);
 
        if (!r->in.share) {
-               werr = WERR_NET_NAME_NOT_FOUND;
+               werr = WERR_NERR_NETNAMENOTFOUND;
                goto error_exit;
        }
-       snum = find_service(talloc_tos(), r->in.share, &servicename);
+       snum = find_service(frame, r->in.share, &servicename);
        if (!servicename) {
-               werr = WERR_NOMEM;
+               werr = WERR_NOT_ENOUGH_MEMORY;
                goto error_exit;
        }
        if (snum == -1) {
                DEBUG(10, ("Could not find service %s\n", servicename));
-               werr = WERR_NET_NAME_NOT_FOUND;
+               werr = WERR_NERR_NETNAMENOTFOUND;
                goto error_exit;
        }
 
-       nt_status = create_conn_struct_cwd(talloc_tos(),
-                                          server_event_context(),
-                                          server_messaging_context(),
-                                          &conn,
-                                          snum, lp_pathname(talloc_tos(), snum),
-                                          p->session_info, &oldcwd);
+       nt_status = create_conn_struct_tos_cwd(global_messaging_context(),
+                                              snum,
+                                              lp_path(frame, lp_sub, snum),
+                                              p->session_info,
+                                              &c);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(10, ("create_conn_struct failed: %s\n",
                           nt_errstr(nt_status)));
                werr = ntstatus_to_werror(nt_status);
                goto error_exit;
        }
+       conn = c->conn;
 
-       nt_status = filename_convert(talloc_tos(),
+       nt_status = filename_convert(frame,
                                        conn,
-                                       false,
                                        r->in.file,
+                                       ucf_flags,
                                        0,
-                                       NULL,
                                        &smb_fname);
        if (!NT_STATUS_IS_OK(nt_status)) {
                werr = ntstatus_to_werror(nt_status);
@@ -2065,7 +2434,6 @@ WERROR _srvsvc_NetGetFileSecurity(struct pipes_struct *p,
        nt_status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                NULL,                                   /* req */
-               0,                                      /* root_dir_fid */
                smb_fname,                              /* fname */
                FILE_READ_ATTRIBUTES,                   /* access_mask */
                FILE_SHARE_READ|FILE_SHARE_WRITE,       /* share_access */
@@ -2073,12 +2441,14 @@ WERROR _srvsvc_NetGetFileSecurity(struct pipes_struct *p,
                0,                                      /* create_options */
                0,                                      /* file_attributes */
                INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               NULL);                                  /* pinfo */
+               NULL,                                   /* pinfo */
+               NULL, NULL);                            /* create context */
 
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(3,("_srvsvc_NetGetFileSecurity: can't open %s\n",
@@ -2089,11 +2459,11 @@ WERROR _srvsvc_NetGetFileSecurity(struct pipes_struct *p,
 
        sd_buf = talloc_zero(p->mem_ctx, struct sec_desc_buf);
        if (!sd_buf) {
-               werr = WERR_NOMEM;
+               werr = WERR_NOT_ENOUGH_MEMORY;
                goto error_exit;
        }
 
-       nt_status = SMB_VFS_FGET_NT_ACL(fsp,
+       nt_status = SMB_VFS_FGET_NT_ACL(metadata_fsp(fsp),
                                       (SECINFO_OWNER
                                        |SECINFO_GROUP
                                        |SECINFO_DACL), sd_buf, &sd_buf->sd);
@@ -2116,12 +2486,7 @@ WERROR _srvsvc_NetGetFileSecurity(struct pipes_struct *p,
 
        *r->out.sd_buf = sd_buf;
 
-       close_file(NULL, fsp, NORMAL_CLOSE);
-       vfs_ChDir(conn, oldcwd);
-       SMB_VFS_DISCONNECT(conn);
-       conn_free(conn);
        werr = WERR_OK;
-       goto done;
 
 error_exit:
 
@@ -2129,19 +2494,7 @@ error_exit:
                close_file(NULL, fsp, NORMAL_CLOSE);
        }
 
-       if (oldcwd) {
-               vfs_ChDir(conn, oldcwd);
-       }
-
-       if (conn) {
-               SMB_VFS_DISCONNECT(conn);
-               conn_free(conn);
-       }
-
- done:
-
-       TALLOC_FREE(smb_fname);
-
+       TALLOC_FREE(frame);
        return werr;
 }
 
@@ -2153,56 +2506,59 @@ error_exit:
 WERROR _srvsvc_NetSetFileSecurity(struct pipes_struct *p,
                                  struct srvsvc_NetSetFileSecurity *r)
 {
+       TALLOC_CTX *frame = talloc_stackframe();
+       const struct loadparm_substitution *lp_sub =
+               loadparm_s3_global_substitution();
        struct smb_filename *smb_fname = NULL;
        char *servicename = NULL;
        files_struct *fsp = NULL;
        SMB_STRUCT_STAT st;
        NTSTATUS nt_status;
        WERROR werr;
+       struct conn_struct_tos *c = NULL;
        connection_struct *conn = NULL;
        int snum;
-       char *oldcwd = NULL;
        struct security_descriptor *psd = NULL;
        uint32_t security_info_sent = 0;
+       uint32_t ucf_flags = 0;
 
        ZERO_STRUCT(st);
 
        if (!r->in.share) {
-               werr = WERR_NET_NAME_NOT_FOUND;
+               werr = WERR_NERR_NETNAMENOTFOUND;
                goto error_exit;
        }
 
-       snum = find_service(talloc_tos(), r->in.share, &servicename);
+       snum = find_service(frame, r->in.share, &servicename);
        if (!servicename) {
-               werr = WERR_NOMEM;
+               werr = WERR_NOT_ENOUGH_MEMORY;
                goto error_exit;
        }
 
        if (snum == -1) {
                DEBUG(10, ("Could not find service %s\n", servicename));
-               werr = WERR_NET_NAME_NOT_FOUND;
+               werr = WERR_NERR_NETNAMENOTFOUND;
                goto error_exit;
        }
 
-       nt_status = create_conn_struct_cwd(talloc_tos(),
-                                          server_event_context(),
-                                          server_messaging_context(),
-                                          &conn,
-                                          snum, lp_pathname(talloc_tos(), snum),
-                                          p->session_info, &oldcwd);
+       nt_status = create_conn_struct_tos_cwd(global_messaging_context(),
+                                              snum,
+                                              lp_path(frame, lp_sub, snum),
+                                              p->session_info,
+                                              &c);
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(10, ("create_conn_struct failed: %s\n",
                           nt_errstr(nt_status)));
                werr = ntstatus_to_werror(nt_status);
                goto error_exit;
        }
+       conn = c->conn;
 
-       nt_status = filename_convert(talloc_tos(),
+       nt_status = filename_convert(frame,
                                        conn,
-                                       false,
                                        r->in.file,
+                                       ucf_flags,
                                        0,
-                                       NULL,
                                        &smb_fname);
        if (!NT_STATUS_IS_OK(nt_status)) {
                werr = ntstatus_to_werror(nt_status);
@@ -2212,7 +2568,6 @@ WERROR _srvsvc_NetSetFileSecurity(struct pipes_struct *p,
        nt_status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                NULL,                                   /* req */
-               0,                                      /* root_dir_fid */
                smb_fname,                              /* fname */
                FILE_WRITE_ATTRIBUTES,                  /* access_mask */
                FILE_SHARE_READ|FILE_SHARE_WRITE,       /* share_access */
@@ -2220,12 +2575,14 @@ WERROR _srvsvc_NetSetFileSecurity(struct pipes_struct *p,
                0,                                      /* create_options */
                0,                                      /* file_attributes */
                INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               NULL);                                  /* pinfo */
+               NULL,                                   /* pinfo */
+               NULL, NULL);                            /* create context */
 
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(3,("_srvsvc_NetSetFileSecurity: can't open %s\n",
@@ -2246,12 +2603,7 @@ WERROR _srvsvc_NetSetFileSecurity(struct pipes_struct *p,
                goto error_exit;
        }
 
-       close_file(NULL, fsp, NORMAL_CLOSE);
-       vfs_ChDir(conn, oldcwd);
-       SMB_VFS_DISCONNECT(conn);
-       conn_free(conn);
        werr = WERR_OK;
-       goto done;
 
 error_exit:
 
@@ -2259,18 +2611,7 @@ error_exit:
                close_file(NULL, fsp, NORMAL_CLOSE);
        }
 
-       if (oldcwd) {
-               vfs_ChDir(conn, oldcwd);
-       }
-
-       if (conn) {
-               SMB_VFS_DISCONNECT(conn);
-               conn_free(conn);
-       }
-
- done:
-       TALLOC_FREE(smb_fname);
-
+       TALLOC_FREE(frame);
        return werr;
 }
 
@@ -2284,14 +2625,14 @@ error_exit:
 
 static const char *server_disks[] = {"C:"};
 
-static uint32 get_server_disk_count(void)
+static uint32_t get_server_disk_count(void)
 {
        return sizeof(server_disks)/sizeof(server_disks[0]);
 }
 
-static uint32 init_server_disk_enum(uint32 *resume)
+static uint32_t init_server_disk_enum(uint32_t *resume)
 {
-       uint32 server_disk_count = get_server_disk_count();
+       uint32_t server_disk_count = get_server_disk_count();
 
        /*resume can be an offset into the list for now*/
 
@@ -2304,7 +2645,7 @@ static uint32 init_server_disk_enum(uint32 *resume)
        return server_disk_count - *resume;
 }
 
-static const char *next_server_disk_enum(uint32 *resume)
+static const char *next_server_disk_enum(uint32_t *resume)
 {
        const char *disk;
 
@@ -2327,7 +2668,7 @@ static const char *next_server_disk_enum(uint32 *resume)
 WERROR _srvsvc_NetDiskEnum(struct pipes_struct *p,
                           struct srvsvc_NetDiskEnum *r)
 {
-       uint32 i;
+       uint32_t i;
        const char *disk_name;
        TALLOC_CTX *ctx = p->mem_ctx;
        WERROR werr;
@@ -2388,7 +2729,7 @@ WERROR _srvsvc_NetNameValidate(struct pipes_struct *p,
                break;
 
        default:
-               return WERR_UNKNOWN_LEVEL;
+               return WERR_INVALID_LEVEL;
        }
 
        return WERR_OK;
@@ -2402,9 +2743,10 @@ struct enum_file_close_state {
        struct messaging_context *msg_ctx;
 };
 
-static void enum_file_close_fn( const struct share_mode_entry *e,
-                          const char *sharepath, const char *fname,
-                         void *private_data )
+static int enum_file_close_fn(struct file_id id,
+                             const struct share_mode_data *d,
+                             const struct share_mode_entry *e,
+                             void *private_data)
 {
        char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
        struct enum_file_close_state *state =
@@ -2412,25 +2754,25 @@ static void enum_file_close_fn( const struct share_mode_entry *e,
        uint32_t fid = (((uint32_t)(procid_to_pid(&e->pid))<<16) | e->share_file_id);
 
        if (fid != state->r->in.fid) {
-               return; /* Not this file. */
+               return 0; /* Not this file. */
        }
 
        if (!process_exists(e->pid) ) {
-               return;
+               return 0;
        }
 
        /* Ok - send the close message. */
-       DEBUG(10,("enum_file_close_fn: request to close file %s, %s\n",
-               sharepath,
-               share_mode_str(talloc_tos(), 0, e) ));
+       DBG_DEBUG("request to close file %s, %s\n", d->servicepath,
+                 share_mode_str(talloc_tos(), 0, &id, e));
 
-       share_mode_entry_to_message(msg, e);
+       share_mode_entry_to_message(msg, &id, e);
 
        state->r->out.result = ntstatus_to_werror(
                messaging_send_buf(state->msg_ctx,
                                e->pid, MSG_SMB_CLOSE_FILE,
-                               (uint8 *)msg,
-                               MSG_SMB_SHARE_MODE_ENTRY_SIZE));
+                               (uint8_t *)msg, sizeof(msg)));
+
+       return 0;
 }
 
 /********************************************************************
@@ -2452,12 +2794,12 @@ WERROR _srvsvc_NetFileClose(struct pipes_struct *p,
        }
 
        /* enum_file_close_fn sends the close message to
-        * the relevent smbd process. */
+        * the relevant smbd process. */
 
-       r->out.result = WERR_BADFILE;
+       r->out.result = WERR_FILE_NOT_FOUND;
        state.r = r;
        state.msg_ctx = p->msg_ctx;
-       share_mode_forall(enum_file_close_fn, &state);
+       share_entry_forall(enum_file_close_fn, &state);
        return r->out.result;
 }
 
@@ -2708,3 +3050,6 @@ WERROR _srvsvc_NETRSERVERTRANSPORTDELEX(struct pipes_struct *p,
        p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
+
+/* include the generated boilerplate */
+#include "librpc/gen_ndr/ndr_srvsvc_scompat.c"