Use WERR_FILE_EXISTS which is the equivalent of WERR_ALREADY_EXISTS
[ira/wip.git] / source3 / rpc_server / srv_srvsvc_nt.c
index 0573599a81b5416dabff44b632d03bd0839d7f3f..9d9e9af59b0391eea5a5757de6b4cc90901f2e09 100644 (file)
@@ -1,45 +1,43 @@
-/* 
+/*
  *  Unix SMB/CIFS implementation.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1997,
  *  Copyright (C) Jeremy Allison               2001.
  *  Copyright (C) Nigel Williams               2001.
  *  Copyright (C) Gerald (Jerry) Carter        2006.
- *  Copyright (C) Jelmer Vernooij                         2006.
- *  
+ *  Copyright (C) Guenther Deschner            2008.
+ *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation; either version 3 of the License, or
  *  (at your option) any later version.
- *  
+ *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
- *  
+ *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /* This is the implementation of the srvsvc pipe. */
 
 #include "includes.h"
 
-#define MAX_SERVER_DISK_ENTRIES 15
-
-extern struct generic_mapping file_generic_mapping;
-extern userdom_struct current_user_info;
+extern const struct generic_mapping file_generic_mapping;
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
+#define MAX_SERVER_DISK_ENTRIES 15
+
 /* Use for enumerating connections, pipes, & files */
 
 struct file_enum_count {
        TALLOC_CTX *ctx;
-       uint32 count;
-       struct srvsvc_NetFileInfo3 *info;
+       const char *username;
+       struct srvsvc_NetFileCtr3 *ctr3;
 };
 
 struct sess_file_count {
@@ -56,43 +54,48 @@ static int pipe_enum_fn( struct db_record *rec, void *p)
 {
        struct pipe_open_rec prec;
        struct file_enum_count *fenum = (struct file_enum_count *)p;
+       struct srvsvc_NetFileInfo3 *f;
+       int i = fenum->ctr3->count;
+       char *fullpath = NULL;
+       const char *username;
+
        if (rec->value.dsize != sizeof(struct pipe_open_rec))
                return 0;
 
        memcpy(&prec, rec->value.dptr, sizeof(struct pipe_open_rec));
-       if ( process_exists(prec.pid) ) {
-               struct srvsvc_NetFileInfo3 *f;
-               int i = fenum->count;
-               pstring fullpath;
-               
-               snprintf( fullpath, sizeof(fullpath), "\\PIPE\\%s", prec.name );
-               
-               f = TALLOC_REALLOC_ARRAY( fenum->ctx, fenum->info, struct srvsvc_NetFileInfo3, i+1 );
-               if ( !f ) {
-                       DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
-                       return 1;
-               }
 
-               fenum->info = f;
-               
-               fenum->info[i].fid = (uint32)((procid_to_pid(&prec.pid)<<16) & prec.pnum);
-               fenum->info[i].permissions = (FILE_READ_DATA|FILE_WRITE_DATA);
-               fenum->info[i].num_locks = 0;
-               if (!(fenum->info[i].user = talloc_strdup(
-                             fenum->ctx, uidtoname(prec.uid)))) {
-                       /* There's not much we can do here. */
-                       fenum->info[i].user = "";
-               }
-               if (!(fenum->info[i].path = talloc_strdup(
-                             fenum->ctx, fullpath))) {
-                       /* There's not much we can do here. */
-                       fenum->info[i].path = "";
-               }
-                       
-               fenum->count++;
+       if ( !process_exists(prec.pid) ) {
+               return 0;
+       }
+
+       username = uidtoname(prec.uid);
+
+       if ((fenum->username != NULL)
+           && !strequal(username, fenum->username)) {
+               return 0;
+       }
+
+       fullpath = talloc_asprintf(fenum->ctx, "\\PIPE\\%s", prec.name );
+       if (!fullpath) {
+               return 1;
+       }
+
+       f = TALLOC_REALLOC_ARRAY(fenum->ctx, fenum->ctr3->array,
+                                struct srvsvc_NetFileInfo3, i+1);
+       if ( !f ) {
+               DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
+               return 1;
        }
+       fenum->ctr3->array = f;
+
+       init_srvsvc_NetFileInfo3(&fenum->ctr3->array[i],
+                                (((uint32_t)(procid_to_pid(&prec.pid))<<16) | prec.pnum),
+                                (FILE_READ_DATA|FILE_WRITE_DATA),
+                                0,
+                                fullpath,
+                                username);
+
+       fenum->ctr3->count++;
 
        return 0;
 }
@@ -100,205 +103,218 @@ static int pipe_enum_fn( struct db_record *rec, void *p)
 /*******************************************************************
 ********************************************************************/
 
-static WERROR net_enum_pipes( TALLOC_CTX *ctx, struct srvsvc_NetFileInfo3 **info, 
-                              uint32 *count, uint32 *resume )
+static WERROR net_enum_pipes(TALLOC_CTX *ctx,
+                            const char *username,
+                            struct srvsvc_NetFileCtr3 **ctr3,
+                            uint32_t resume )
 {
        struct file_enum_count fenum;
 
        fenum.ctx = ctx;
-       fenum.info = *info;
-       fenum.count = *count;
+       fenum.username = username;
+       fenum.ctr3 = *ctr3;
 
        if (connections_traverse(pipe_enum_fn, &fenum) == -1) {
                DEBUG(0,("net_enum_pipes: traverse of connections.tdb "
                         "failed\n"));
                return WERR_NOMEM;
        }
-       
-       *info  = fenum.info;
-       *count = fenum.count;
-       
+
+       *ctr3 = fenum.ctr3;
+
        return WERR_OK;
 }
 
 /*******************************************************************
 ********************************************************************/
 
-static void enum_file_fn( const struct share_mode_entry *e, 
+static void enum_file_fn( const struct share_mode_entry *e,
                           const char *sharepath, const char *fname,
                          void *private_data )
 {
-       struct file_enum_count *fenum =
-               (struct file_enum_count *)&private_data;
+       struct file_enum_count *fenum =
+               (struct file_enum_count *)private_data;
+
+       struct srvsvc_NetFileInfo3 *f;
+       int i = fenum->ctr3->count;
+       files_struct fsp;
+       struct byte_range_lock *brl;
+       int num_locks = 0;
+       char *fullpath = NULL;
+       uint32 permissions;
+       const char *username;
+
        /* If the pid was not found delete the entry from connections.tdb */
 
-       if ( process_exists(e->pid) ) {
-               struct srvsvc_NetFileInfo3 *f;
-               int i = fenum->count;
-               files_struct fsp;
-               struct byte_range_lock *brl;
-               int num_locks = 0;
-               pstring fullpath;
-               uint32 permissions;
-               
-               f = TALLOC_REALLOC_ARRAY( fenum->ctx, fenum->info, struct srvsvc_NetFileInfo3, i+1 );                   
-               if ( !f ) {
-                       DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
-                       return;
-               }
-               fenum->info = f;
-
-               /* need to count the number of locks on a file */
-               
-               ZERO_STRUCT( fsp );             
-               fsp.file_id = e->id;
-               
-               if ( (brl = brl_get_locks_readonly(NULL,&fsp)) != NULL ) {
-                       num_locks = brl->num_locks;
-                       TALLOC_FREE( brl );
-               }
-               
-               if ( strcmp( fname, "." ) == 0 ) {
-                       pstr_sprintf( fullpath, "C:%s", sharepath );
-               } else {
-                       pstr_sprintf( fullpath, "C:%s/%s", sharepath, fname );
-               }
-               string_replace( fullpath, '/', '\\' );
-               
-               /* mask out create (what ever that is) */
-               permissions = e->share_access & (FILE_READ_DATA|FILE_WRITE_DATA);
-
-               fenum->info[i].fid = e->share_file_id;
-               fenum->info[i].permissions = permissions;
-               fenum->info[i].num_locks = num_locks;
-               if (!(fenum->info[i].user = talloc_strdup(
-                             fenum->ctx, uidtoname(e->uid)))) {
-                       /* There's not much we can do here. */
-                       fenum->info[i].user = "";
-               }
-               if (!(fenum->info[i].path = talloc_strdup(
-                             fenum->ctx, fullpath))) {
-                       /* There's not much we can do here. */
-                       fenum->info[i].path = "";
-               }
-                       
-               fenum->count++;
+       if ( !process_exists(e->pid) ) {
+               return;
        }
 
-       return;
+       username = uidtoname(e->uid);
+
+       if ((fenum->username != NULL)
+           && !strequal(username, fenum->username)) {
+               return;
+       }
+
+       f = TALLOC_REALLOC_ARRAY(fenum->ctx, fenum->ctr3->array,
+                                struct srvsvc_NetFileInfo3, i+1);
+       if ( !f ) {
+               DEBUG(0,("conn_enum_fn: realloc failed for %d items\n", i+1));
+               return;
+       }
+       fenum->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);
+       }
+
+       if ( strcmp( fname, "." ) == 0 ) {
+               fullpath = talloc_asprintf(fenum->ctx, "C:%s", sharepath );
+       } else {
+               fullpath = talloc_asprintf(fenum->ctx, "C:%s/%s",
+                               sharepath, fname );
+       }
+       if (!fullpath) {
+               return;
+       }
+       string_replace( fullpath, '/', '\\' );
 
+       /* mask out create (what ever that is) */
+       permissions = e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA);
+
+       /* now fill in the srvsvc_NetFileInfo3 struct */
+       init_srvsvc_NetFileInfo3(&fenum->ctr3->array[i],
+                                (((uint32_t)(procid_to_pid(&e->pid))<<16) | e->share_file_id),
+                                permissions,
+                                num_locks,
+                                fullpath,
+                                username);
+       fenum->ctr3->count++;
 }
 
 /*******************************************************************
 ********************************************************************/
 
-static WERROR net_enum_files( TALLOC_CTX *ctx, struct srvsvc_NetFileInfo3 **info, 
-                              uint32 *count, uint32 *resume )
+static WERROR net_enum_files(TALLOC_CTX *ctx,
+                            const char *username,
+                            struct srvsvc_NetFileCtr3 **ctr3,
+                            uint32_t resume)
 {
        struct file_enum_count f_enum_cnt;
 
        f_enum_cnt.ctx = ctx;
-       f_enum_cnt.count = *count;
-       f_enum_cnt.info = *info;
-       
+       f_enum_cnt.username = username;
+       f_enum_cnt.ctr3 = *ctr3;
+
        share_mode_forall( enum_file_fn, (void *)&f_enum_cnt );
-       
-       *info  = f_enum_cnt.info;
-       *count = f_enum_cnt.count;
-       
+
+       *ctr3 = f_enum_cnt.ctr3;
+
        return WERR_OK;
 }
 
 /*******************************************************************
- Utility function to get the 'type' of a share from a share definition.
+ Utility function to get the 'type' of a share from an snum.
  ********************************************************************/
-static uint32 get_share_type(const struct share_params *params)
+static uint32 get_share_type(int snum)
 {
-       char *net_name = lp_servicename(params->service);
-       int len_net_name = strlen(net_name);
-       
        /* work out the share type */
        uint32 type = STYPE_DISKTREE;
 
-       if (lp_print_ok(params->service))
+       if (lp_print_ok(snum))
                type = STYPE_PRINTQ;
-       if (strequal(lp_fstype(params->service), "IPC"))
+       if (strequal(lp_fstype(snum), "IPC"))
                type = STYPE_IPC;
-       if (net_name[len_net_name-1] == '$')
+       if (lp_administrative_share(snum))
                type |= STYPE_HIDDEN;
 
        return type;
 }
-       
+
 /*******************************************************************
  Fill in a share info level 0 structure.
  ********************************************************************/
 
-static void init_srv_share_info_0(pipes_struct *p, struct srvsvc_NetShareInfo0 *sh0,
-                                 const struct share_params *params)
+static void init_srv_share_info_0(pipes_struct *p, struct srvsvc_NetShareInfo0 *r, int snum)
 {
-       sh0->name = lp_servicename(params->service);
+       const char *net_name = lp_servicename(snum);
+
+       init_srvsvc_NetShareInfo0(r, net_name);
 }
 
 /*******************************************************************
  Fill in a share info level 1 structure.
  ********************************************************************/
 
-static void init_srv_share_info_1(pipes_struct *p, struct srvsvc_NetShareInfo1 *sh1,
-                                 const struct share_params *params)
+static void init_srv_share_info_1(pipes_struct *p, struct srvsvc_NetShareInfo1 *r, int snum)
 {
-       connection_struct *conn = p->conn;
+       char *net_name = lp_servicename(snum);
+       char *remark = talloc_strdup(p->mem_ctx, lp_comment(snum));
 
-       sh1->comment = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
-                                    conn->user, conn->connectpath, conn->gid,
-                                    get_current_username(),
-                                    current_user_info.domain,
-                                    lp_comment(params->service));
+       if (remark) {
+               remark = talloc_sub_advanced(
+                       p->mem_ctx, lp_servicename(snum),
+                       get_current_username(), lp_pathname(snum),
+                       p->pipe_user.ut.uid, get_current_username(),
+                       "", remark);
+       }
 
-       sh1->name = lp_servicename(params->service);
-       sh1->type = get_share_type(params);
+       init_srvsvc_NetShareInfo1(r, net_name,
+                                 get_share_type(snum),
+                                 remark ? remark : "");
 }
 
 /*******************************************************************
  Fill in a share info level 2 structure.
  ********************************************************************/
 
-static void init_srv_share_info_2(pipes_struct *p, struct srvsvc_NetShareInfo2 *sh2,
-                                 const struct share_params *params)
+static void init_srv_share_info_2(pipes_struct *p, struct srvsvc_NetShareInfo2 *r, int snum)
 {
-       connection_struct *conn = p->conn;
-       char *remark;
-       char *path;
-       int max_connections = lp_max_connections(params->service);
-       uint32 max_uses = max_connections!=0 ? max_connections : 0xffffffff;
+       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;
        int count = 0;
-       char *net_name = lp_servicename(params->service);
-       
-       remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
-                                    conn->user, conn->connectpath, conn->gid,
-                                    get_current_username(),
-                                    current_user_info.domain,
-                                    lp_comment(params->service));
-       path = talloc_asprintf(p->mem_ctx, "C:%s",
-                              lp_pathname(params->service));
+       char *net_name = lp_servicename(snum);
 
-       /*
-        * Change / to \\ so that win2k will see it as a valid path.  This was
-        * added to enable use of browsing in win2k add share dialog.
-        */ 
+       remark = talloc_strdup(p->mem_ctx, lp_comment(snum));
+       if (remark) {
+               remark = talloc_sub_advanced(
+                       p->mem_ctx, lp_servicename(snum),
+                       get_current_username(), lp_pathname(snum),
+                       p->pipe_user.ut.uid, get_current_username(),
+                       "", remark);
+       }
+       path = talloc_asprintf(p->mem_ctx,
+                       "C:%s", lp_pathname(snum));
+
+       if (path) {
+               /*
+                * Change / to \\ so that win2k will see it as a valid path.
+                * This was added to enable use of browsing in win2k add
+                * share dialog.
+                */
 
-       string_replace(path, '/', '\\');
+               string_replace(path, '/', '\\');
+       }
+
+       count = count_current_connections(net_name, false);
 
-       count = count_current_connections( net_name, False  );
-       sh2->name = net_name;
-       sh2->type = get_share_type(params);
-       sh2->comment = remark;
-       sh2->permissions = 0;
-       sh2->max_users = max_uses;
-       sh2->current_users = count;
-       sh2->path = path;
-       sh2->password = "";
+       init_srvsvc_NetShareInfo2(r, net_name,
+                                 get_share_type(snum),
+                                 remark ? remark : "",
+                                 0,
+                                 max_uses,
+                                 count,
+                                 path ? path : "",
+                                 "");
 }
 
 /*******************************************************************
@@ -323,182 +339,157 @@ static void map_generic_share_sd_bits(SEC_DESC *psd)
 
                se_map_generic(&psa->access_mask, &file_generic_mapping);
                psa->access_mask |= orig_mask;
-       }       
+       }
 }
 
 /*******************************************************************
  Fill in a share info level 501 structure.
 ********************************************************************/
 
-static void init_srv_share_info_501(pipes_struct *p, struct srvsvc_NetShareInfo501 *sh501,
-                                   const struct share_params *params)
+static void init_srv_share_info_501(pipes_struct *p, struct srvsvc_NetShareInfo501 *r, int snum)
 {
-       connection_struct *conn = p->conn;
-       char *remark;
-       const char *net_name = lp_servicename(params->service);
-
-       remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
-                                    conn->user, conn->connectpath, conn->gid,
-                                    get_current_username(),
-                                    current_user_info.domain,
-                                    lp_comment(params->service));
+       const char *net_name = lp_servicename(snum);
+       char *remark = talloc_strdup(p->mem_ctx, lp_comment(snum));
 
+       if (remark) {
+               remark = talloc_sub_advanced(
+                       p->mem_ctx, lp_servicename(snum),
+                       get_current_username(), lp_pathname(snum),
+                       p->pipe_user.ut.uid, get_current_username(),
+                       "", remark);
+       }
 
-       sh501->name = net_name;
-       sh501->type = get_share_type(params);
-       sh501->comment = remark;
-       sh501->csc_policy = (lp_csc_policy(params->service) << 4);
+       init_srvsvc_NetShareInfo501(r, net_name,
+                                   get_share_type(snum),
+                                   remark ? remark : "",
+                                   (lp_csc_policy(snum) << 4));
 }
 
 /*******************************************************************
  Fill in a share info level 502 structure.
  ********************************************************************/
 
-static void init_srv_share_info_502(pipes_struct *p, struct srvsvc_NetShareInfo502 *sh502,
-                                   const struct share_params *params)
+static void init_srv_share_info_502(pipes_struct *p, struct srvsvc_NetShareInfo502 *r, int snum)
 {
-       int max_connections = lp_max_connections(params->service);
-       uint32 max_uses = max_connections!=0 ? max_connections : 0xffffffff;
-       connection_struct *conn = p->conn;
-       int count; 
-       char *net_name;
-       char *remark;
-       char *path;
-       SEC_DESC *sd;
-       size_t sd_size;
+       const char *net_name = lp_servicename(snum);
+       char *path = NULL;
+       SEC_DESC *sd = NULL;
+       struct sec_desc_buf *sd_buf = NULL;
+       size_t sd_size = 0;
        TALLOC_CTX *ctx = p->mem_ctx;
+       char *remark = talloc_strdup(ctx, lp_comment(snum));;
 
+       if (remark) {
+               remark = talloc_sub_advanced(
+                       p->mem_ctx, lp_servicename(snum),
+                       get_current_username(), lp_pathname(snum),
+                       p->pipe_user.ut.uid, get_current_username(),
+                       "", remark);
+       }
+       path = talloc_asprintf(ctx, "C:%s", lp_pathname(snum));
+       if (path) {
+               /*
+                * Change / to \\ so that win2k will see it as a valid path.  This was added to
+                * enable use of browsing in win2k add share dialog.
+                */
+               string_replace(path, '/', '\\');
+       }
 
-       ZERO_STRUCTP(sh502);
-
-       net_name = lp_servicename(params->service);
-       count = count_current_connections( net_name, False  );
-
-       remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
-                                    conn->user, conn->connectpath, conn->gid,
-                                    get_current_username(),
-                                    current_user_info.domain,
-                                    lp_comment(params->service));
-
-       path = talloc_asprintf(p->mem_ctx, "C:%s",
-                              lp_pathname(params->service));
-
-       /*
-        * Change / to \\ so that win2k will see it as a valid path.  This was
-        * added to enable use of browsing in win2k add share dialog.
-        */ 
-
-       string_replace(path, '/', '\\');
+       sd = get_share_security(ctx, lp_servicename(snum), &sd_size);
 
-       sd = get_share_security(ctx, lp_servicename(params->service),
-                               &sd_size);
+       sd_buf = make_sec_desc_buf(p->mem_ctx, sd_size, sd);
 
-       sh502->name = net_name;
-       sh502->type = get_share_type(params);
-       sh502->comment = remark;
-       sh502->path = path;
-       sh502->password = "";
-       sh502->sd = sd;
-       sh502->permissions = 0;
-       sh502->max_users = max_uses;
-       sh502->current_users = count;
-       sh502->unknown = 1;
+       init_srvsvc_NetShareInfo502(r, net_name,
+                                   get_share_type(snum),
+                                   remark ? remark : "",
+                                   0,
+                                   (uint32_t)-1,
+                                   1,
+                                   path ? path : "",
+                                   "",
+                                   sd_buf);
 }
 
 /***************************************************************************
  Fill in a share info level 1004 structure.
  ***************************************************************************/
 
-static void init_srv_share_info_1004(pipes_struct *p,
-                                    struct srvsvc_NetShareInfo1004* sh1004,
-                                    const struct share_params *params)
+static void init_srv_share_info_1004(pipes_struct *p, struct srvsvc_NetShareInfo1004 *r, int snum)
 {
-       connection_struct *conn = p->conn;
-       char *remark;
+       char *remark = talloc_strdup(p->mem_ctx, lp_comment(snum));
 
-       remark = talloc_sub_advanced(p->mem_ctx, lp_servicename(SNUM(conn)),
-                                    conn->user, conn->connectpath, conn->gid,
-                                    get_current_username(),
-                                    current_user_info.domain,
-                                    lp_comment(params->service));
-
-       ZERO_STRUCTP(sh1004);
+       if (remark) {
+               remark = talloc_sub_advanced(
+                       p->mem_ctx, lp_servicename(snum),
+                       get_current_username(), lp_pathname(snum),
+                       p->pipe_user.ut.uid, get_current_username(),
+                       "", remark);
+       }
 
-       sh1004->comment = remark;
+       init_srvsvc_NetShareInfo1004(r, remark ? remark : "");
 }
 
 /***************************************************************************
  Fill in a share info level 1005 structure.
  ***************************************************************************/
 
-static void init_srv_share_info_1005(pipes_struct *p,
-                                    struct srvsvc_NetShareInfo1005* sh1005,
-                                    const struct share_params *params)
+static void init_srv_share_info_1005(pipes_struct *p, struct srvsvc_NetShareInfo1005 *r, int snum)
 {
-       sh1005->dfs_flags = 0;
+       uint32_t dfs_flags = 0;
 
-       if(lp_host_msdfs() && lp_msdfs_root(params->service))
-               sh1005->dfs_flags |= 
-                       SHARE_1005_IN_DFS | SHARE_1005_DFS_ROOT;
-       sh1005->dfs_flags |= 
-               lp_csc_policy(params->service) << SHARE_1005_CSC_POLICY_SHIFT;
+       if (lp_host_msdfs() && lp_msdfs_root(snum)) {
+               dfs_flags |= SHARE_1005_IN_DFS | SHARE_1005_DFS_ROOT;
+       }
+
+       dfs_flags |= lp_csc_policy(snum) << SHARE_1005_CSC_POLICY_SHIFT;
+
+       init_srvsvc_NetShareInfo1005(r, dfs_flags);
 }
+
 /***************************************************************************
  Fill in a share info level 1006 structure.
  ***************************************************************************/
 
-static void init_srv_share_info_1006(pipes_struct *p,
-                                    struct srvsvc_NetShareInfo1006* sh1006,
-                                    const struct share_params *params)
+static void init_srv_share_info_1006(pipes_struct *p, struct srvsvc_NetShareInfo1006 *r, int snum)
 {
-       sh1006->max_users = -1;
+       init_srvsvc_NetShareInfo1006(r, (uint32_t)-1);
 }
 
 /***************************************************************************
  Fill in a share info level 1007 structure.
  ***************************************************************************/
 
-static void init_srv_share_info_1007(pipes_struct *p,
-                                    struct srvsvc_NetShareInfo1007* sh1007,
-                                    const struct share_params *params)
+static void init_srv_share_info_1007(pipes_struct *p, struct srvsvc_NetShareInfo1007 *r, int snum)
 {
        uint32 flags = 0;
 
-       ZERO_STRUCTP(sh1007);
-  
-       sh1007->flags = flags;
-       sh1007->alternate_directory_name = "";
+       init_srvsvc_NetShareInfo1007(r, flags, "");
 }
 
 /*******************************************************************
  Fill in a share info level 1501 structure.
  ********************************************************************/
 
-static void init_srv_share_info_1501(pipes_struct *p,
-                                    struct sec_desc_buf *sh1501,
-                                    const struct share_params *params)
+static void init_srv_share_info_1501(pipes_struct *p, struct sec_desc_buf *r, int snum)
 {
        SEC_DESC *sd;
        size_t sd_size;
        TALLOC_CTX *ctx = p->mem_ctx;
 
-       ZERO_STRUCTP(sh1501);
+       sd = get_share_security(ctx, lp_servicename(snum), &sd_size);
 
-       sd = get_share_security(ctx, lp_servicename(params->service),
-                               &sd_size);
-
-       sh1501->sd = sd;
+       r = make_sec_desc_buf(p->mem_ctx, sd_size, sd);
 }
 
 /*******************************************************************
  True if it ends in '$'.
  ********************************************************************/
 
-static BOOL is_hidden_share(const struct share_params *params)
+static bool is_hidden_share(int snum)
 {
-       const char *net_name = lp_servicename(params->service);
+       const char *net_name = lp_servicename(snum);
 
-       return (net_name[strlen(net_name) - 1] == '$');
+       return (net_name[strlen(net_name) - 1] == '$') ? True : False;
 }
 
 /*******************************************************************
@@ -506,307 +497,299 @@ static BOOL is_hidden_share(const struct share_params *params)
  ********************************************************************/
 
 static WERROR init_srv_share_info_ctr(pipes_struct *p,
-                                     union srvsvc_NetShareCtr *ctr,
-                                     uint32 info_level, uint32 *resume_hnd,
-                                     uint32 *total_entries, BOOL all_shares)
-{
+                                     struct srvsvc_NetShareInfoCtr *info_ctr,
+                                     uint32_t *resume_handle_p,
+                                     uint32_t *total_entries,
+                                     bool all_shares)
+{
+       int num_entries = 0;
+       int alloc_entries = 0;
+       int num_services = 0;
+       int snum;
        TALLOC_CTX *ctx = p->mem_ctx;
-       struct share_iterator *shares;
-       struct share_params *share;
-       WERROR result = WERR_NOMEM;
+       int i = 0;
+       int valid_share_count = 0;
+       union srvsvc_NetShareCtr ctr;
+       uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
 
        DEBUG(5,("init_srv_share_info_ctr\n"));
 
-       ZERO_STRUCTP(ctr);
-
-       if (resume_hnd) {
-               *resume_hnd = 0;
-       }
-
        /* Ensure all the usershares are loaded. */
        become_root();
        load_usershare_shares();
        load_registry_shares();
+       num_services = lp_numservices();
        unbecome_root();
 
-       *total_entries = 0;
+       /* Count the number of entries. */
+       for (snum = 0; snum < num_services; snum++) {
+               if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) ) {
+                       DEBUG(10, ("counting service %s\n", lp_servicename(snum)));
+                       num_entries++;
+               } else {
+                       DEBUG(10, ("NOT counting service %s\n", lp_servicename(snum)));
+               }
+       }
 
-       if (!(shares = share_list_all(ctx))) {
-               DEBUG(5, ("Could not list shares\n"));
-               return WERR_ACCESS_DENIED;
+       if (!num_entries || (resume_handle >= num_entries)) {
+               return WERR_OK;
        }
 
-       switch (info_level) {
+       /* Calculate alloc entries. */
+       alloc_entries = num_entries - resume_handle;
+       switch (info_ctr->level) {
        case 0:
-               if (!(ctr->ctr0 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr0))) {
-                       goto done;
+               ctr.ctr0 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr0);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr0);
+
+               ctr.ctr0->count = alloc_entries;
+               ctr.ctr0->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo0, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr0->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_0(p, &ctr.ctr0->array[i++], snum);
+                       }
                }
+
                break;
+
        case 1:
-               if (!(ctr->ctr1 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr1))) {
-                       goto done;
+               ctr.ctr1 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr1);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1);
+
+               ctr.ctr1->count = alloc_entries;
+               ctr.ctr1->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo1, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_1(p, &ctr.ctr1->array[i++], snum);
+                       }
                }
+
                break;
+
        case 2:
-               if (!(ctr->ctr2 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr2))) {
-                       goto done;
+               ctr.ctr2 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr2);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr2);
+
+               ctr.ctr2->count = alloc_entries;
+               ctr.ctr2->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo2, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr2->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_2(p, &ctr.ctr2->array[i++], snum);
+                       }
                }
+
                break;
+
        case 501:
-               if (!(ctr->ctr501 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr501))) {
-                       goto done;
+               ctr.ctr501 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr501);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr501);
+
+               ctr.ctr501->count = alloc_entries;
+               ctr.ctr501->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo501, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr501->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_501(p, &ctr.ctr501->array[i++], snum);
+                       }
                }
+
                break;
+
        case 502:
-               if (!(ctr->ctr502 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr502))) {
-                       goto done;
+               ctr.ctr502 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr502);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr502);
+
+               ctr.ctr502->count = alloc_entries;
+               ctr.ctr502->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo502, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr502->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_502(p, &ctr.ctr502->array[i++], snum);
+                       }
                }
+
                break;
+
        case 1004:
-               if (!(ctr->ctr1004 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr1004))) {
-                       goto done;
+               ctr.ctr1004 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr1004);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1004);
+
+               ctr.ctr1004->count = alloc_entries;
+               ctr.ctr1004->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo1004, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1004->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_1004(p, &ctr.ctr1004->array[i++], snum);
+                       }
                }
+
                break;
+
        case 1005:
-               if (!(ctr->ctr1005 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr1005))) {
-                       goto done;
+               ctr.ctr1005 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr1005);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1005);
+
+               ctr.ctr1005->count = alloc_entries;
+               ctr.ctr1005->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo1005, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1005->array);
+
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_1005(p, &ctr.ctr1005->array[i++], snum);
+                       }
                }
+
                break;
+
        case 1006:
-               if (!(ctr->ctr1006 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr1006))) {
-                       goto done;
-               }
-               break;
-       case 1007:
-               if (!(ctr->ctr1007 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr1007))) {
-                       goto done;
-               }
-               break;
-       case 1501:
-               if (!(ctr->ctr1501 = TALLOC_ZERO_P(
-                             p->mem_ctx, struct srvsvc_NetShareCtr1501))) {
-                       goto done;
-               }
-               break;
-       default:
-               DEBUG(5,("init_srv_share_info_ctr: unsupported switch "
-                        "value %d\n", info_level));
-               return WERR_UNKNOWN_LEVEL;
-       }
+               ctr.ctr1006 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr1006);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1006);
 
-       while ((share = next_share(shares)) != NULL) {
-               if (!lp_browseable(share->service)) {
-                       continue;
-               }
-               if (!all_shares && is_hidden_share(share)) {
-                       continue;
-               }
+               ctr.ctr1006->count = alloc_entries;
+               ctr.ctr1006->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo1006, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1006->array);
 
-               switch (info_level) {
-               case 0:
-               {
-                       struct srvsvc_NetShareInfo0 i;
-                       init_srv_share_info_0(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo0, i,
-                                    &ctr->ctr0->array, &ctr->ctr0->count);
-                       if (ctr->ctr0->array == NULL) {
-                               return WERR_NOMEM;
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_1006(p, &ctr.ctr1006->array[i++], snum);
                        }
-                       *total_entries = ctr->ctr0->count;
-                       break;
                }
 
-               case 1:
-               {
-                       struct srvsvc_NetShareInfo1 i;
-                       init_srv_share_info_1(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1, i,
-                                    &ctr->ctr1->array, &ctr->ctr1->count);
-                       if (ctr->ctr1->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr1->count;
-                       break;
-               }
+               break;
 
-               case 2:
-               {
-                       struct srvsvc_NetShareInfo2 i;
-                       init_srv_share_info_2(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo2, i,
-                                    &ctr->ctr2->array, &ctr->ctr2->count);
-                       if (ctr->ctr2->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr2->count;
-                       break;
-               }
+       case 1007:
+               ctr.ctr1007 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr1007);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1007);
 
-               case 501:
-               {
-                       struct srvsvc_NetShareInfo501 i;
-                       init_srv_share_info_501(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo501, i,
-                                    &ctr->ctr501->array, &ctr->ctr501->count);
-                       if (ctr->ctr501->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr501->count;
-                       break;
-               }
+               ctr.ctr1007->count = alloc_entries;
+               ctr.ctr1007->array = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetShareInfo1007, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1007->array);
 
-               case 502:
-               {
-                       struct srvsvc_NetShareInfo502 i;
-                       init_srv_share_info_502(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo502, i,
-                                    &ctr->ctr502->array, &ctr->ctr502->count);
-                       if (ctr->ctr502->array == NULL) {
-                               return WERR_NOMEM;
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_1007(p, &ctr.ctr1007->array[i++], snum);
                        }
-                       *total_entries = ctr->ctr502->count;
-                       break;
                }
 
-               /* here for completeness but not currently used with enum
-                * (1004 - 1501)*/
-       
-               case 1004:
-               {
-                       struct srvsvc_NetShareInfo1004 i;
-                       init_srv_share_info_1004(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1004, i,
-                                    &ctr->ctr1004->array, &ctr->ctr1004->count);
-                       if (ctr->ctr1004->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr1004->count;
-                       break;
-               }
+               break;
 
-               case 1005:
-               {
-                       struct srvsvc_NetShareInfo1005 i;
-                       init_srv_share_info_1005(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1005, i,
-                                    &ctr->ctr1005->array, &ctr->ctr1005->count);
-                       if (ctr->ctr1005->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr1005->count;
-                       break;
-               }
+       case 1501:
+               ctr.ctr1501 = TALLOC_ZERO_P(ctx, struct srvsvc_NetShareCtr1501);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1501);
 
-               case 1006:
-               {
-                       struct srvsvc_NetShareInfo1006 i;
-                       init_srv_share_info_1006(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1006, i,
-                                    &ctr->ctr1006->array, &ctr->ctr1006->count);
-                       if (ctr->ctr1006->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr1006->count;
-                       break;
-               }
+               ctr.ctr1501->count = alloc_entries;
+               ctr.ctr1501->array = TALLOC_ZERO_ARRAY(ctx, struct sec_desc_buf, alloc_entries);
+               W_ERROR_HAVE_NO_MEMORY(ctr.ctr1501->array);
 
-               case 1007:
-               {
-                       struct srvsvc_NetShareInfo1007 i;
-                       init_srv_share_info_1007(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct srvsvc_NetShareInfo1007, i,
-                                    &ctr->ctr1007->array, &ctr->ctr1007->count);
-                       if (ctr->ctr1007->array == NULL) {
-                               return WERR_NOMEM;
+               for (snum = 0; snum < num_services; snum++) {
+                       if (lp_browseable(snum) && lp_snum_ok(snum) && (all_shares || !is_hidden_share(snum)) &&
+                           (resume_handle <= (i + valid_share_count++)) ) {
+                               init_srv_share_info_1501(p, &ctr.ctr1501->array[i++], snum);
                        }
-                       *total_entries = ctr->ctr1007->count;
-                       break;
                }
 
-               case 1501:
-               {
-                       struct sec_desc_buf i;
-                       init_srv_share_info_1501(p, &i, share);
-                       ADD_TO_ARRAY(ctx, struct sec_desc_buf, i,
-                                    &ctr->ctr1501->array, &ctr->ctr1501->count);
-                       if (ctr->ctr1501->array == NULL) {
-                               return WERR_NOMEM;
-                       }
-                       *total_entries = ctr->ctr1501->count;
-                       break;
-               }
-               }
+               break;
 
-               TALLOC_FREE(share);
+       default:
+               DEBUG(5,("init_srv_share_info_ctr: unsupported switch value %d\n",
+                       info_ctr->level));
+               return WERR_UNKNOWN_LEVEL;
        }
 
-       result = WERR_OK;
- done:
-       TALLOC_FREE(shares);
-       return result;
+       *total_entries = alloc_entries;
+       if (resume_handle_p) {
+               if (all_shares) {
+                       *resume_handle_p = (num_entries == 0) ? *resume_handle_p : 0;
+               } else {
+                       *resume_handle_p = num_entries;
+               }
+       }
+
+       info_ctr->ctr = ctr;
+
+       return WERR_OK;
 }
 
 /*******************************************************************
  fill in a sess info level 0 structure.
  ********************************************************************/
 
-static void init_srv_sess_info_0(pipes_struct *p, struct srvsvc_NetSessCtr0 *ss0, uint32 *snum, uint32 *stot)
+static WERROR init_srv_sess_info_0(pipes_struct *p,
+                                  struct srvsvc_NetSessCtr0 *ctr0,
+                                  uint32_t *resume_handle_p,
+                                  uint32_t *total_entries)
 {
        struct sessionid *session_list;
-       uint32 num_entries = 0;
-       (*stot) = list_sessions(p->mem_ctx, &session_list);
+       uint32_t num_entries = 0;
+       uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
+       *total_entries = list_sessions(p->mem_ctx, &session_list);
 
-       if (ss0 == NULL) {
-               if (snum) {
-                       (*snum) = 0;
+       DEBUG(5,("init_srv_sess_info_0\n"));
+
+       if (ctr0 == NULL) {
+               if (resume_handle_p) {
+                       *resume_handle_p = 0;
                }
-               return;
+               return WERR_OK;
        }
 
-       DEBUG(5,("init_srv_sess_0_ss0\n"));
+       for (; resume_handle < *total_entries; resume_handle++) {
 
-       ss0->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetSessInfo0, *stot);
+               ctr0->array = TALLOC_REALLOC_ARRAY(p->mem_ctx,
+                                                  ctr0->array,
+                                                  struct srvsvc_NetSessInfo0,
+                                                  num_entries+1);
+               W_ERROR_HAVE_NO_MEMORY(ctr0->array);
 
-       if (snum) {
-               for (; (*snum) < (*stot); (*snum)++) {
-                       ss0->array[num_entries].client = session_list[(*snum)].remote_machine;
-                       num_entries++;
-               }
+               init_srvsvc_NetSessInfo0(&ctr0->array[num_entries],
+                                        session_list[resume_handle].remote_machine);
+               num_entries++;
+       }
 
-               ss0->count = num_entries;
-               
-               if ((*snum) >= (*stot)) {
-                       (*snum) = 0;
-               }
+       ctr0->count = num_entries;
 
-       } else {
-               ss0->array = NULL;
-               ss0->count = 0;
+       if (resume_handle_p) {
+               if (*resume_handle_p >= *total_entries) {
+                       *resume_handle_p = 0;
+               } else {
+                       *resume_handle_p = resume_handle;
+               }
        }
+
+       return WERR_OK;
 }
 
 /*******************************************************************
 ********************************************************************/
 
-static void sess_file_fn( const struct share_mode_entry *e, 
+static void sess_file_fn( const struct share_mode_entry *e,
                           const char *sharepath, const char *fname,
-                         void *private_data )
+                         void *data )
 {
-       struct sess_file_count *sess = (struct sess_file_count *)private_data;
+       struct sess_file_count *sess = (struct sess_file_count *)data;
+
        if ( procid_equal(&e->pid, &sess->pid) && (sess->uid == e->uid) ) {
                sess->count++;
        }
-       
+
        return;
 }
 
@@ -820,9 +803,9 @@ static int net_count_files( uid_t uid, struct server_id pid )
        s_file_cnt.count = 0;
        s_file_cnt.uid = uid;
        s_file_cnt.pid = pid;
-       
-       share_mode_forall( sess_file_fn, (void *)&s_file_cnt );
-       
+
+       share_mode_forall( sess_file_fn, &s_file_cnt );
+
        return s_file_cnt.count;
 }
 
@@ -830,277 +813,251 @@ static int net_count_files( uid_t uid, struct server_id pid )
  fill in a sess info level 1 structure.
  ********************************************************************/
 
-static void init_srv_sess_info_1(pipes_struct *p, struct srvsvc_NetSessCtr1 *ss1, uint32 *snum, uint32 *stot)
+static WERROR init_srv_sess_info_1(pipes_struct *p,
+                                  struct srvsvc_NetSessCtr1 *ctr1,
+                                  uint32_t *resume_handle_p,
+                                  uint32_t *total_entries)
 {
        struct sessionid *session_list;
-       uint32 num_entries = 0;
+       uint32_t num_entries = 0;
        time_t now = time(NULL);
+       uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
 
-       if ( !snum ) {
-               ss1->count = 0;
-               ss1->array = NULL;
-               
-               (*stot) = 0;
+       ZERO_STRUCTP(ctr1);
 
-               return;
-       }
-       
-       if (ss1 == NULL) {
-               if (snum != NULL)
-                       (*snum) = 0;
-               return;
+       if (ctr1 == NULL) {
+               if (resume_handle_p) {
+                       *resume_handle_p = 0;
+               }
+               return WERR_OK;
        }
 
-       (*stot) = list_sessions(p->mem_ctx, &session_list);
+       *total_entries = list_sessions(p->mem_ctx, &session_list);
 
-       ss1->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetSessInfo1, *stot);
-       
-       for (; (*snum) < (*stot); (*snum)++) {
+       for (; resume_handle < *total_entries; resume_handle++) {
                uint32 num_files;
                uint32 connect_time;
-               struct passwd *pw = sys_getpwnam(session_list[*snum].username);
-               BOOL guest;
-                       
+               struct passwd *pw = sys_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[*snum].username));
+                               session_list[resume_handle].username));
                        continue;
                }
-                               
-               connect_time = (uint32)(now - session_list[*snum].connect_start);
-               num_files = net_count_files(pw->pw_uid, session_list[*snum].pid);
-               guest = strequal( session_list[*snum].username, lp_guestaccount() );
-                                       
-               if (!(ss1->array[num_entries].client = talloc_strdup(
-                             ss1->array, session_list[*snum].remote_machine))) {
-                       ss1->array[num_entries].client = "";
-               }
-               if (!(ss1->array[num_entries].user = talloc_strdup(
-                             ss1->array, session_list[*snum].username))) {
-                       ss1->array[num_entries].user = "";
-               }
-               ss1->array[num_entries].num_open = num_files;
-               ss1->array[num_entries].time = connect_time;
-               ss1->array[num_entries].idle_time = 0;
-               ss1->array[num_entries].user_flags = guest;
 
-               num_entries++;
-       }
+               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() );
 
-       ss1->count = num_entries;
-       
-       if ((*snum) >= (*stot)) {
-               (*snum) = 0;
-       }
-}
+               ctr1->array = TALLOC_REALLOC_ARRAY(p->mem_ctx,
+                                                  ctr1->array,
+                                                  struct srvsvc_NetSessInfo1,
+                                                  num_entries+1);
+               W_ERROR_HAVE_NO_MEMORY(ctr1->array);
 
-/*******************************************************************
- makes a SRV_R_NET_SESS_ENUM structure.
-********************************************************************/
+               init_srvsvc_NetSessInfo1(&ctr1->array[num_entries],
+                                        session_list[resume_handle].remote_machine,
+                                        session_list[resume_handle].username,
+                                        num_files,
+                                        connect_time,
+                                        0,
+                                        guest);
+               num_entries++;
+       }
 
-static WERROR init_srv_sess_info_ctr(pipes_struct *p, union srvsvc_NetSessCtr *ctr,
-                               int switch_value, uint32 *resume_hnd, uint32 *total_entries)
-{
-       WERROR status = WERR_OK;
-       DEBUG(5,("init_srv_sess_info_ctr: %d\n", __LINE__));
+       ctr1->count = num_entries;
 
-       switch (switch_value) {
-       case 0:
-               ctr->ctr0 = talloc(p->mem_ctx, struct srvsvc_NetSessCtr0);
-               init_srv_sess_info_0(p, ctr->ctr0, resume_hnd, total_entries);
-               break;
-       case 1:
-               ctr->ctr1 = talloc(p->mem_ctx, struct srvsvc_NetSessCtr1);
-               init_srv_sess_info_1(p, ctr->ctr1, resume_hnd, total_entries);
-               break;
-       default:
-               DEBUG(5,("init_srv_sess_info_ctr: unsupported switch value %d\n", switch_value));
-               if (resume_hnd != NULL)
-                       (*resume_hnd) = 0;
-               (*total_entries) = 0;
-               ctr->ctr0 = NULL;
-               status = WERR_UNKNOWN_LEVEL;
-               break;
+       if (resume_handle_p) {
+               if (*resume_handle_p >= *total_entries) {
+                       *resume_handle_p = 0;
+               } else {
+                       *resume_handle_p = resume_handle;
+               }
        }
 
-       return status;
+       return WERR_OK;
 }
 
 /*******************************************************************
  fill in a conn info level 0 structure.
  ********************************************************************/
 
-static void init_srv_conn_info_0(pipes_struct *p, struct srvsvc_NetConnCtr0 *ss0, uint32 *snum, uint32 *stot)
+static WERROR init_srv_conn_info_0(struct srvsvc_NetConnCtr0 *ctr0,
+                                  uint32_t *resume_handle_p,
+                                  uint32_t *total_entries)
 {
-       uint32 num_entries = 0;
-       (*stot) = 1;
+       uint32_t num_entries = 0;
+       uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
 
-       if (ss0 == NULL) {
-               if (snum != NULL)
-                       (*snum) = 0;
-               return;
+       DEBUG(5,("init_srv_conn_info_0\n"));
+
+       if (ctr0 == NULL) {
+               if (resume_handle_p) {
+                       *resume_handle_p = 0;
+               }
+               return WERR_OK;
        }
 
-       DEBUG(5,("init_srv_conn_0_ss0\n"));
+       *total_entries = 1;
 
-       if (snum) {
-               ss0->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetConnInfo0, *stot);
-               for (; (*snum) < (*stot); (*snum)++) {
+       ZERO_STRUCTP(ctr0);
 
-                       ss0->array[num_entries].conn_id = (*stot);
+       for (; resume_handle < *total_entries; resume_handle++) {
 
-                       /* move on to creating next connection */
-                       /* move on to creating next conn */
-                       num_entries++;
+               ctr0->array = TALLOC_REALLOC_ARRAY(talloc_tos(),
+                                                  ctr0->array,
+                                                  struct srvsvc_NetConnInfo0,
+                                                  num_entries+1);
+               if (!ctr0->array) {
+                       return WERR_NOMEM;
                }
 
-               ss0->count = num_entries;
-               
-               if ((*snum) >= (*stot)) {
-                       (*snum) = 0;
-               }
+               init_srvsvc_NetConnInfo0(&ctr0->array[num_entries],
+                                        (*total_entries));
 
-       } else {
-               ss0->array = NULL;
-               ss0->count = 0;
+               /* move on to creating next connection */
+               num_entries++;
+       }
+
+       ctr0->count = num_entries;
+       *total_entries = num_entries;
 
-               (*stot) = 0;
+       if (resume_handle_p) {
+               if (*resume_handle_p >= *total_entries) {
+                       *resume_handle_p = 0;
+               } else {
+                       *resume_handle_p = resume_handle;
+               }
        }
+
+       return WERR_OK;
 }
 
 /*******************************************************************
  fill in a conn info level 1 structure.
  ********************************************************************/
 
-static void init_srv_conn_info_1(pipes_struct *p, struct srvsvc_NetConnCtr1 *ss1, uint32 *snum, uint32 *stot)
+static WERROR init_srv_conn_info_1(struct srvsvc_NetConnCtr1 *ctr1,
+                                  uint32_t *resume_handle_p,
+                                  uint32_t *total_entries)
 {
-       uint32 num_entries = 0;
-       (*stot) = 1;
+       uint32_t num_entries = 0;
+       uint32_t resume_handle = resume_handle_p ? *resume_handle_p : 0;
 
-       if (ss1 == NULL) {
-               if (snum != NULL)
-                       (*snum) = 0;
-               return;
+       DEBUG(5,("init_srv_conn_info_1\n"));
+
+       if (ctr1 == NULL) {
+               if (resume_handle_p) {
+                       *resume_handle_p = 0;
+               }
+               return WERR_OK;
        }
 
-       DEBUG(5,("init_srv_conn_1_ss1\n"));
+       *total_entries = 1;
 
-       if (snum) {
-               ss1->array = TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetConnInfo1, *stot);
-               for (; (*snum) < (*stot); (*snum)++) {
-                       ss1->array[num_entries].conn_id = (*stot);
-                       ss1->array[num_entries].conn_type = 0x3;
-                       ss1->array[num_entries].num_open = 1;
-                       ss1->array[num_entries].num_users = 1;
-                       ss1->array[num_entries].conn_time = 3;
-                       ss1->array[num_entries].user = "dummy_user";
-                       ss1->array[num_entries].share = "IPC$";
+       ZERO_STRUCTP(ctr1);
 
-                       /* move on to creating next connection */
-                       /* move on to creating next conn */
-                       num_entries++;
+       for (; resume_handle < *total_entries; resume_handle++) {
+
+               ctr1->array = TALLOC_REALLOC_ARRAY(talloc_tos(),
+                                                  ctr1->array,
+                                                  struct srvsvc_NetConnInfo1,
+                                                  num_entries+1);
+               if (!ctr1->array) {
+                       return WERR_NOMEM;
                }
 
-               ss1->count = num_entries;
+               init_srvsvc_NetConnInfo1(&ctr1->array[num_entries],
+                                        (*total_entries),
+                                        0x3,
+                                        1,
+                                        1,
+                                        3,
+                                        "dummy_user",
+                                        "IPC$");
 
-               if ((*snum) >= (*stot)) {
-                       (*snum) = 0;
-               }
+               /* move on to creating next connection */
+               num_entries++;
+       }
 
-       } else {
-               ss1->count = 0;
-               ss1->array = NULL;
-               
-               (*stot) = 0;
+       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;
+               }
        }
+
+       return WERR_OK;
 }
 
 /*******************************************************************
- makes a SRV_R_NET_CONN_ENUM structure.
-********************************************************************/
+ _srvsvc_NetFileEnum
+*******************************************************************/
 
-static WERROR init_srv_conn_info_ctr(pipes_struct *p, union srvsvc_NetConnCtr *ctr,
-                               int switch_value, uint32 *resume_hnd, uint32 *total_entries)
+WERROR _srvsvc_NetFileEnum(pipes_struct *p,
+                          struct srvsvc_NetFileEnum *r)
 {
-       WERROR status = WERR_OK;
-       DEBUG(5,("init_srv_conn_info_ctr: %d\n", __LINE__));
+       TALLOC_CTX *ctx = NULL;
+       struct srvsvc_NetFileCtr3 *ctr3;
+       uint32_t resume_hnd = 0;
+       WERROR werr;
 
-       switch (switch_value) {
-       case 0:
-               init_srv_conn_info_0(p, ctr->ctr0, resume_hnd, total_entries);
-               break;
-       case 1:
-               init_srv_conn_info_1(p, ctr->ctr1, resume_hnd, total_entries);
+       switch (r->in.info_ctr->level) {
+       case 3:
                break;
        default:
-               DEBUG(5,("init_srv_conn_info_ctr: unsupported switch value %d\n", switch_value));
-               ctr->ctr0 = NULL;
-               (*resume_hnd) = 0;
-               (*total_entries) = 0;
-               status = WERR_UNKNOWN_LEVEL;
-               break;
+               return WERR_UNKNOWN_LEVEL;
        }
 
-       return status;
-}
-
-/*******************************************************************
- makes a SRV_R_NET_FILE_ENUM structure.
-********************************************************************/
-
-static WERROR net_file_enum_3(pipes_struct *p, union srvsvc_NetFileCtr *ctr, uint32 *resume_hnd, uint32 *num_entries )
-{
-       TALLOC_CTX *ctx = get_talloc_ctx();
-       WERROR status;
+       ctx = talloc_tos();
+       ctr3 = r->in.info_ctr->ctr.ctr3;
+       if (!ctr3) {
+               werr = WERR_INVALID_PARAM;
+               goto done;
+       }
 
-       /* TODO -- Windows enumerates 
+       /* TODO -- Windows enumerates
           (b) active pipes
           (c) open directories and files */
 
-       ctr->ctr3 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetFileCtr3);
-       
-       status = net_enum_files( ctx, &ctr->ctr3->array, num_entries, resume_hnd );
-       if ( !W_ERROR_IS_OK(status))
-               return status;
-               
-       status = net_enum_pipes( ctx, &ctr->ctr3->array, num_entries, resume_hnd );
-       if ( !W_ERROR_IS_OK(status))
-               return status;
-
-       ctr->ctr3->count = *num_entries;
-       
-       return WERR_OK;
-}
-
-/*******************************************************************
-*******************************************************************/
+       werr = net_enum_files(ctx, r->in.user, &ctr3, resume_hnd);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
 
-WERROR _srvsvc_NetFileEnum(pipes_struct *p, struct srvsvc_NetFileEnum *r)
-{
-       switch ( *r->in.level ) {
-       case 3:
-               return net_file_enum_3(p, r->in.ctr, r->in.resume_handle, r->out.totalentries );        
-       default:
-               return WERR_UNKNOWN_LEVEL;
+       werr = net_enum_pipes(ctx, r->in.user, &ctr3, resume_hnd);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
        }
-       
-       return WERR_OK;
+
+       *r->out.totalentries = ctr3->count;
+       r->out.info_ctr->ctr.ctr3->array = ctr3->array;
+       r->out.info_ctr->ctr.ctr3->count = ctr3->count;
+
+       werr = WERR_OK;
+
+ done:
+       return werr;
 }
 
 /*******************************************************************
-net server get info
+ _srvsvc_NetSrvGetInfo
 ********************************************************************/
 
-WERROR _srvsvc_NetSrvGetInfo(pipes_struct *p, struct srvsvc_NetSrvGetInfo *r)
+WERROR _srvsvc_NetSrvGetInfo(pipes_struct *p,
+                            struct srvsvc_NetSrvGetInfo *r)
 {
        WERROR status = WERR_OK;
 
-       ZERO_STRUCTP(r->out.info);
-
-       DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetSrvGetInfo: %d\n", __LINE__));
 
        if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to srv_net_srv_get_info\n"));
+               DEBUG(3, ("access denied to _srvsvc_NetSrvGetInfo\n"));
                return WERR_ACCESS_DENIED;
        }
 
@@ -1110,117 +1067,190 @@ WERROR _srvsvc_NetSrvGetInfo(pipes_struct *p, struct srvsvc_NetSrvGetInfo *r)
                   Administrators but there isn't anything super-secret
                   here, as most of it is made up. */
 
-       case 102:
-               r->out.info->info102 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetSrvInfo102);
-
-               r->out.info->info102->platform_id = 500;
-               r->out.info->info102->version_major = lp_major_announce_version();
-               r->out.info->info102->version_minor = lp_minor_announce_version();
-               r->out.info->info102->server_name = global_myname(); 
-               r->out.info->info102->server_type = lp_default_server_announce();
-               r->out.info->info102->userpath = "C:\\";
-               r->out.info->info102->licenses = 10000;
-               r->out.info->info102->anndelta = 3000;
-               r->out.info->info102->disc = 0xf;
-               r->out.info->info102->users = 0xffffffff;
-               r->out.info->info102->hidden = 0;
-               r->out.info->info102->announce = 240;
-               r->out.info->info102->comment = lp_serverstring();
+       case 102: {
+               struct srvsvc_NetSrvInfo102 *info102;
+
+               info102 = TALLOC_P(p->mem_ctx, struct srvsvc_NetSrvInfo102);
+               if (!info102) {
+                       return WERR_NOMEM;
+               }
+
+               init_srvsvc_NetSrvInfo102(info102,
+                                         PLATFORM_ID_NT,
+                                         global_myname(),
+                                         lp_major_announce_version(),
+                                         lp_minor_announce_version(),
+                                         lp_default_server_announce(),
+                                         string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH),
+                                         0xffffffff, /* users */
+                                         0xf, /* disc */
+                                         0, /* hidden */
+                                         240, /* announce */
+                                         3000, /* announce delta */
+                                         100000, /* licenses */
+                                         "c:\\"); /* user path */
+               r->out.info->info102 = info102;
                break;
-       case 101:
-               r->out.info->info101 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetSrvInfo101);
-                       r->out.info->info101->platform_id = 500;
-                       r->out.info->info101->server_name = global_myname();
-                       r->out.info->info101->version_major = lp_major_announce_version();
-                       r->out.info->info101->version_minor = lp_minor_announce_version();
-                       r->out.info->info101->server_type = lp_default_server_announce();
-                       r->out.info->info101->comment = lp_serverstring();
+       }
+       case 101: {
+               struct srvsvc_NetSrvInfo101 *info101;
+
+               info101 = TALLOC_P(p->mem_ctx, struct srvsvc_NetSrvInfo101);
+               if (!info101) {
+                       return WERR_NOMEM;
+               }
+
+               init_srvsvc_NetSrvInfo101(info101,
+                                         PLATFORM_ID_NT,
+                                         global_myname(),
+                                         lp_major_announce_version(),
+                                         lp_minor_announce_version(),
+                                         lp_default_server_announce(),
+                                         string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH));
+               r->out.info->info101 = info101;
                break;
-       case 100:
-               r->out.info->info100 = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetSrvInfo100);
-               r->out.info->info100->platform_id = 500;
-               r->out.info->info100->server_name = global_myname();
+       }
+       case 100: {
+               struct srvsvc_NetSrvInfo100 *info100;
+
+               info100 = TALLOC_P(p->mem_ctx, struct srvsvc_NetSrvInfo100);
+               if (!info100) {
+                       return WERR_NOMEM;
+               }
+
+               init_srvsvc_NetSrvInfo100(info100,
+                                         PLATFORM_ID_NT,
+                                         global_myname());
+               r->out.info->info100 = info100;
+
                break;
+       }
        default:
-               return WERR_UNKNOWN_LEVEL;
+               status = WERR_UNKNOWN_LEVEL;
                break;
        }
 
-       DEBUG(5,("srv_net_srv_get_info: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetSrvGetInfo: %d\n", __LINE__));
 
        return status;
 }
 
 /*******************************************************************
-net server set info
+ _srvsvc_NetSrvSetInfo
 ********************************************************************/
 
-WERROR _srvsvc_NetSrvSetInfo(pipes_struct *p, struct srvsvc_NetSrvSetInfo *r)
+WERROR _srvsvc_NetSrvSetInfo(pipes_struct *p,
+                            struct srvsvc_NetSrvSetInfo *r)
 {
+       WERROR status = WERR_OK;
+
+       DEBUG(5,("_srvsvc_NetSrvSetInfo: %d\n", __LINE__));
+
        /* Set up the net server set info structure. */
-       if (r->out.parm_error) {
-               *r->out.parm_error = 0;
-       }
-       return WERR_OK;
+
+       DEBUG(5,("_srvsvc_NetSrvSetInfo: %d\n", __LINE__));
+
+       return status;
 }
 
 /*******************************************************************
-net conn enum
+ _srvsvc_NetConnEnum
 ********************************************************************/
 
-WERROR _srvsvc_NetConnEnum(pipes_struct *p, struct srvsvc_NetConnEnum *r)
+WERROR _srvsvc_NetConnEnum(pipes_struct *p,
+                          struct srvsvc_NetConnEnum *r)
 {
-       DEBUG(5,("srv_net_conn_enum: %d\n", __LINE__));
+       WERROR werr;
 
-       ZERO_STRUCTP(r->out.ctr);
+       DEBUG(5,("_srvsvc_NetConnEnum: %d\n", __LINE__));
 
-       /* set up the */
-       return init_srv_conn_info_ctr(p, r->out.ctr, *r->in.level, r->in.resume_handle, r->out.totalentries);
+       switch (r->in.info_ctr->level) {
+               case 0:
+                       werr = init_srv_conn_info_0(r->in.info_ctr->ctr.ctr0,
+                                                   r->in.resume_handle,
+                                                   r->out.totalentries);
+                       break;
+               case 1:
+                       werr = init_srv_conn_info_1(r->in.info_ctr->ctr.ctr1,
+                                                   r->in.resume_handle,
+                                                   r->out.totalentries);
+                       break;
+               default:
+                       return WERR_UNKNOWN_LEVEL;
+       }
+
+       DEBUG(5,("_srvsvc_NetConnEnum: %d\n", __LINE__));
+
+       return werr;
 }
 
 /*******************************************************************
-net sess enum
+ _srvsvc_NetSessEnum
 ********************************************************************/
 
-WERROR _srvsvc_NetSessEnum(pipes_struct *p, struct srvsvc_NetSessEnum *r)
+WERROR _srvsvc_NetSessEnum(pipes_struct *p,
+                          struct srvsvc_NetSessEnum *r)
 {
-       DEBUG(5,("_srv_net_sess_enum: %d\n", __LINE__));
+       WERROR werr;
 
-       ZERO_STRUCTP(r->out.ctr);
+       DEBUG(5,("_srvsvc_NetSessEnum: %d\n", __LINE__));
 
-       /* set up the */
-       return init_srv_sess_info_ctr(p, r->out.ctr,
-                               *r->in.level, 
-                               r->in.resume_handle,
-                               r->out.totalentries);
+       switch (r->in.info_ctr->level) {
+               case 0:
+                       werr = init_srv_sess_info_0(p,
+                                                   r->in.info_ctr->ctr.ctr0,
+                                                   r->in.resume_handle,
+                                                   r->out.totalentries);
+                       break;
+               case 1:
+                       werr = init_srv_sess_info_1(p,
+                                                   r->in.info_ctr->ctr.ctr1,
+                                                   r->in.resume_handle,
+                                                   r->out.totalentries);
+                       break;
+               default:
+                       return WERR_UNKNOWN_LEVEL;
+       }
+
+       DEBUG(5,("_srvsvc_NetSessEnum: %d\n", __LINE__));
+
+       return werr;
 }
 
 /*******************************************************************
-net sess del
+ _srvsvc_NetSessDel
 ********************************************************************/
 
-WERROR _srvsvc_NetSessDel(pipes_struct *p, struct srvsvc_NetSessDel *r)
+WERROR _srvsvc_NetSessDel(pipes_struct *p,
+                         struct srvsvc_NetSessDel *r)
 {
        struct sessionid *session_list;
+       struct current_user user;
        int num_sessions, snum;
-       WERROR status;
+       const char *username;
+       const char *machine;
+       bool not_root = False;
+       WERROR werr;
 
-       char *machine = talloc_strdup(p->mem_ctx, r->in.server_unc);
+       username = r->in.user;
+       machine = r->in.client;
 
        /* strip leading backslashes if any */
-       while (machine[0] == '\\') {
-               memmove(machine, &machine[1], strlen(machine));
+       if (machine && machine[0] == '\\' && machine[1] == '\\') {
+               machine += 2;
        }
 
        num_sessions = list_sessions(p->mem_ctx, &session_list);
 
-       DEBUG(5,("_srv_net_sess_del: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetSessDel: %d\n", __LINE__));
 
-       status = WERR_ACCESS_DENIED;
+       werr = WERR_ACCESS_DENIED;
+
+       get_current_user(&user, p);
 
        /* fail out now if you are not root or not a domain admin */
 
-       if ((p->pipe_user.ut.uid != sec_initial_uid()) && 
+       if ((user.ut.uid != sec_initial_uid()) &&
                ( ! nt_token_check_domain_rid(p->pipe_user.nt_user_token, DOMAIN_GROUP_RID_ADMINS))) {
 
                goto done;
@@ -1228,446 +1258,249 @@ WERROR _srvsvc_NetSessDel(pipes_struct *p, struct srvsvc_NetSessDel *r)
 
        for (snum = 0; snum < num_sessions; snum++) {
 
-               if ((strequal(session_list[snum].username, r->in.user) || r->in.user[0] == '\0' ) &&
-                    strequal(session_list[snum].remote_machine, machine)) {
+               if ((strequal(session_list[snum].username, username) || username[0] == '\0' ) &&
+                   strequal(session_list[snum].remote_machine, machine)) {
+
                        NTSTATUS ntstat;
 
+                       if (user.ut.uid != sec_initial_uid()) {
+                               not_root = True;
+                               become_root();
+                       }
+
                        ntstat = messaging_send(smbd_messaging_context(),
                                                session_list[snum].pid,
                                                MSG_SHUTDOWN, &data_blob_null);
-               
+
                        if (NT_STATUS_IS_OK(ntstat))
-                               status = WERR_OK;
+                               werr = WERR_OK;
+
+                       if (not_root)
+                               unbecome_root();
                }
        }
 
-       DEBUG(5,("_srv_net_sess_del: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetSessDel: %d\n", __LINE__));
 
 done:
-       return status;
+
+       return werr;
 }
 
 /*******************************************************************
- Net share enum all.
+ _srvsvc_NetShareEnumAll
 ********************************************************************/
 
-WERROR _srvsvc_NetShareEnumAll(pipes_struct *p, struct srvsvc_NetShareEnumAll *r)
+WERROR _srvsvc_NetShareEnumAll(pipes_struct *p,
+                              struct srvsvc_NetShareEnumAll *r)
 {
-       DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
+       WERROR werr;
+
+       DEBUG(5,("_srvsvc_NetShareEnumAll: %d\n", __LINE__));
 
        if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to srv_net_share_enum_all\n"));
+               DEBUG(3, ("access denied to _srvsvc_NetShareEnumAll\n"));
                return WERR_ACCESS_DENIED;
        }
 
        /* Create the list of shares for the response. */
-       return init_srv_share_info_ctr(p, r->out.ctr, *r->in.level,
-                                             r->in.resume_handle, r->out.totalentries, True);
+       werr = init_srv_share_info_ctr(p,
+                                      r->in.info_ctr,
+                                      r->in.resume_handle,
+                                      r->out.totalentries,
+                                      true);
+
+       DEBUG(5,("_srvsvc_NetShareEnumAll: %d\n", __LINE__));
+
+       return werr;
 }
 
 /*******************************************************************
- Net share enum.
+ _srvsvc_NetShareEnum
 ********************************************************************/
 
-WERROR _srvsvc_NetShareEnum(pipes_struct *p, struct srvsvc_NetShareEnum *r)
+WERROR _srvsvc_NetShareEnum(pipes_struct *p,
+                           struct srvsvc_NetShareEnum *r)
 {
-       DEBUG(5,("_srv_net_share_enum: %d\n", __LINE__));
+       WERROR werr;
+
+       DEBUG(5,("_srvsvc_NetShareEnum: %d\n", __LINE__));
 
        if (!pipe_access_check(p)) {
-               DEBUG(3, ("access denied to srv_net_share_enum\n"));
+               DEBUG(3, ("access denied to _srvsvc_NetShareEnum\n"));
                return WERR_ACCESS_DENIED;
        }
 
        /* Create the list of shares for the response. */
-       return init_srv_share_info_ctr(p, r->out.ctr, *r->in.level,
-                                             r->in.resume_handle, r->out.totalentries, False);
-}
-
-/*******************************************************************
- Net share get info.
-********************************************************************/
+       werr = init_srv_share_info_ctr(p,
+                                      r->in.info_ctr,
+                                      r->in.resume_handle,
+                                      r->out.totalentries,
+                                      false);
 
-WERROR _srvsvc_NetShareGetInfo(pipes_struct *p, struct srvsvc_NetShareGetInfo *r)
-{
-       const struct share_params *params;
-
-       params = get_share_params(p->mem_ctx, r->in.share_name);
+       DEBUG(5,("_srvsvc_NetShareEnum: %d\n", __LINE__));
 
-       if (params != NULL) {
-               switch (r->in.level) {
-               case 0:
-                       r->out.info->info0 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo0);
-                       init_srv_share_info_0(p, r->out.info->info0, params);
-                       break;
-               case 1:
-                       r->out.info->info1 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1);
-                       init_srv_share_info_1(p, r->out.info->info1, params);
-                       break;
-               case 2:
-                       r->out.info->info2 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo2);
-                       init_srv_share_info_2(p, r->out.info->info2, params);
-                       break;
-               case 501:
-                       r->out.info->info501 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo501);
-                       init_srv_share_info_501(p, r->out.info->info501, params);
-                       break;
-               case 502:
-                       r->out.info->info502 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo502);
-                       init_srv_share_info_502(p, r->out.info->info502, params);
-                       break;
-
-                       /* here for completeness */
-               case 1004:
-                       r->out.info->info1004 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1004);
-                       init_srv_share_info_1004(p, r->out.info->info1004, params);
-                       break;
-               case 1005:
-                       r->out.info->info1005 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1005);
-                       init_srv_share_info_1005(p, r->out.info->info1005, params);
-                       break;
-
-                       /* here for completeness 1006 - 1501 */
-               case 1006:
-                       r->out.info->info1006 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1006);
-                       init_srv_share_info_1006(p, r->out.info->info1006,
-                                                params);
-                       break;
-               case 1007:
-                       r->out.info->info1007 = talloc(p->mem_ctx, struct srvsvc_NetShareInfo1007);
-                       init_srv_share_info_1007(p, r->out.info->info1007,
-                                                params);
-                       break;
-               case 1501:
-                       r->out.info->info1501 = talloc(p->mem_ctx, struct sec_desc_buf);
-                       init_srv_share_info_1501(p, r->out.info->info1501,
-                                                params);
-                       break;
-               default:
-                       DEBUG(5,("init_srv_net_share_get_info: unsupported "
-                                "switch value %d\n", r->in.level));
-                       return WERR_UNKNOWN_LEVEL;
-                       break;
-               }
-       } else {
-               return WERR_INVALID_NAME;
-       }
-
-       return WERR_OK;
+       return werr;
 }
 
 /*******************************************************************
- Check a given DOS pathname is valid for a share.
+ _srvsvc_NetShareGetInfo
 ********************************************************************/
 
-char *valid_share_pathname(char *dos_pathname)
-{
-       char *ptr;
-
-       /* Convert any '\' paths to '/' */
-       unix_format(dos_pathname);
-       unix_clean_name(dos_pathname);
-
-       /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
-       ptr = dos_pathname;
-       if (strlen(dos_pathname) > 2 && ptr[1] == ':' && ptr[0] != '/')
-               ptr += 2;
-
-       /* Only absolute paths allowed. */
-       if (*ptr != '/')
-               return NULL;
-
-       return ptr;
-}
-
-static void setval_helper(struct registry_key *key, const char *name,
-                         const char *value, WERROR *err)
-{
-       struct registry_value val;
-
-       if (!W_ERROR_IS_OK(*err)) {
-               return;
-       }
-
-       ZERO_STRUCT(val);
-       val.type = REG_SZ;
-       val.v.sz.str = CONST_DISCARD(char *, value);
-       val.v.sz.len = strlen(value)+1;
-
-       *err = reg_setvalue(key, name, &val);
-}
-
-static WERROR add_share(const char *share_name, const char *path,
-                       const char *comment, uint32 max_connections,
-                       const struct nt_user_token *token,
-                       BOOL is_disk_op)
-{
-       if (lp_add_share_cmd() && *lp_add_share_cmd()) {
-               char *command;
-               int ret;
-
-               if (asprintf(&command, "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
-                            lp_add_share_cmd(), dyn_CONFIGFILE, share_name,
-                            path, comment, max_connections) == -1) {
-                       return WERR_NOMEM;
-               }
-
-               DEBUG(10,("add_share: Running [%s]\n", command ));
-
-               /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
-       
-               if ( is_disk_op )
-                       become_root();
-
-               if ( (ret = smbrun(command, NULL)) == 0 ) {
-                       /* Tell everyone we updated smb.conf. */
-                       message_send_all(smbd_messaging_context(),
-                                        MSG_SMB_CONF_UPDATED,
-                                        NULL, 0, NULL);
-               }
-
-               if ( is_disk_op )
-                       unbecome_root();
-               
-               /********* END SeDiskOperatorPrivilege BLOCK *********/
-
-               DEBUG(3,("_srv_net_share_add: Running [%s] returned (%d)\n",
-                        command, ret ));
-
-               /*
-                * No fallback to registry shares, the user did define a add
-                * share command, so fail here.
-                */
-
-               SAFE_FREE(command);
-               return (ret == 0) ? WERR_OK : WERR_ACCESS_DENIED;
-       }
-
-       if (lp_registry_shares()) {
-               char *keyname;
-               struct registry_key *key;
-               enum winreg_CreateAction action;
-               WERROR err;
-               TALLOC_CTX *mem_ctx;
-
-               if (!(keyname = talloc_asprintf(NULL, "%s\\%s", KEY_SMBCONF,
-                                               share_name))) {
-                       return WERR_NOMEM;
-               }
-
-               mem_ctx = (TALLOC_CTX *)keyname;
-
-               err = reg_create_path(mem_ctx, keyname, REG_KEY_WRITE,
-                                     is_disk_op ? get_root_nt_token():token,
-                                     &action, &key);
-
-               if (action != REG_CREATED_NEW_KEY) {
-                       err = WERR_ALREADY_EXISTS;
-               }
-
-               if (!W_ERROR_IS_OK(err)) {
-                       TALLOC_FREE(mem_ctx);
-                       return err;
-               }
-
-               setval_helper(key, "path", path, &err);
-               if ((comment != NULL) && (comment[0] != '\0')) {
-                       setval_helper(key, "comment", comment, &err);
-               }
-               if (max_connections != 0) {
-                       char tmp[16];
-                       snprintf(tmp, sizeof(tmp), "%d", max_connections);
-                       setval_helper(key, "max connections", tmp, &err);
-               }
-
-               if (!W_ERROR_IS_OK(err)) {
-                       /*
-                        * Hmmmm. We'd need transactions on the registry to
-                        * get this right....
-                        */
-                       reg_delete_path(is_disk_op ? get_root_nt_token():token,
-                                       keyname);
-               }
-               TALLOC_FREE(mem_ctx);
-               return err;
-       }
-
-       return WERR_ACCESS_DENIED;
-}
-
-static WERROR delete_share(const char *sharename, 
-                          const struct nt_user_token *token,
-                          BOOL is_disk_op)
-{
-       if (lp_delete_share_cmd() && *lp_delete_share_cmd()) {
-               char *command;
-               int ret;
-
-               if (asprintf(&command, "%s \"%s\" \"%s\"",
-                            lp_delete_share_cmd(), dyn_CONFIGFILE,
-                            sharename)) {
-                       return WERR_NOMEM;
-               }
-
-               DEBUG(10,("delete_share: Running [%s]\n", command ));
-
-               /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
-       
-               if ( is_disk_op )
-                       become_root();
-
-               if ( (ret = smbrun(command, NULL)) == 0 ) {
-                       /* Tell everyone we updated smb.conf. */
-                       message_send_all(smbd_messaging_context(),
-                                        MSG_SMB_CONF_UPDATED,
-                                        NULL, 0, NULL);
-               }
-
-               if ( is_disk_op )
-                       unbecome_root();
-
-               /********* END SeDiskOperatorPrivilege BLOCK *********/
-
-               SAFE_FREE(command);
-
-               DEBUG(3,("_srv_net_share_del: Running [%s] returned (%d)\n",
-                        command, ret ));
-               return (ret == 0) ? WERR_OK : WERR_ACCESS_DENIED;
-       }
-
-       if (lp_registry_shares()) {
-               char *keyname;
-               WERROR err;
-
-               if (asprintf(&keyname, "%s\\%s", KEY_SMBCONF,
-                            sharename) == -1) {
-                       return WERR_NOMEM;
-               }
-
-               err = reg_delete_path(is_disk_op ? get_root_nt_token():token,
-                                     keyname);
-               SAFE_FREE(keyname);
-               return err;
-       }
-
-       return WERR_ACCESS_DENIED;
-}
-
-static WERROR change_share(const char *share_name, const char *path,
-                          const char *comment, uint32 max_connections,
-                          const struct nt_user_token *token,
-                          BOOL is_disk_op)
-{
-       if (lp_change_share_cmd() && *lp_change_share_cmd()) {
-               char *command;
-               int ret;
-
-               if (asprintf(&command, "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
-                            lp_change_share_cmd(), dyn_CONFIGFILE, share_name,
-                            path, comment, max_connections) == -1) {
-                       return WERR_NOMEM;
-               }
-
-               DEBUG(10,("_srv_net_share_set_info: Running [%s]\n", command));
-                               
-               /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
-       
-               if ( is_disk_op )
-                       become_root();
-                       
-               if ( (ret = smbrun(command, NULL)) == 0 ) {
-                       /* Tell everyone we updated smb.conf. */
-                       message_send_all(smbd_messaging_context(),
-                                        MSG_SMB_CONF_UPDATED,
-                                        NULL, 0, NULL);
-               }
-               
-               if ( is_disk_op )
-                       unbecome_root();
-                       
-               /********* END SeDiskOperatorPrivilege BLOCK *********/
-
-               DEBUG(3,("_srv_net_share_set_info: Running [%s] returned "
-                        "(%d)\n", command, ret ));
+WERROR _srvsvc_NetShareGetInfo(pipes_struct *p,
+                              struct srvsvc_NetShareGetInfo *r)
+{
+       WERROR status = WERR_OK;
+       fstring share_name;
+       int snum;
+       union srvsvc_NetShareInfo *info = r->out.info;
+
+       DEBUG(5,("_srvsvc_NetShareGetInfo: %d\n", __LINE__));
 
-               SAFE_FREE(command);
+       fstrcpy(share_name, r->in.share_name);
 
-               return (ret == 0) ? WERR_OK : WERR_ACCESS_DENIED;
+       snum = find_service(share_name);
+       if (snum < 0) {
+               return WERR_INVALID_NAME;
+       }
+
+       switch (r->in.level) {
+               case 0:
+                       info->info0 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo0);
+                       W_ERROR_HAVE_NO_MEMORY(info->info0);
+                       init_srv_share_info_0(p, info->info0, snum);
+                       break;
+               case 1:
+                       info->info1 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo1);
+                       W_ERROR_HAVE_NO_MEMORY(info->info1);
+                       init_srv_share_info_1(p, info->info1, snum);
+                       break;
+               case 2:
+                       info->info2 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo2);
+                       W_ERROR_HAVE_NO_MEMORY(info->info2);
+                       init_srv_share_info_2(p, info->info2, snum);
+                       break;
+               case 501:
+                       info->info501 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo501);
+                       W_ERROR_HAVE_NO_MEMORY(info->info501);
+                       init_srv_share_info_501(p, info->info501, snum);
+                       break;
+               case 502:
+                       info->info502 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo502);
+                       W_ERROR_HAVE_NO_MEMORY(info->info502);
+                       init_srv_share_info_502(p, info->info502, snum);
+                       break;
+               case 1004:
+                       info->info1004 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo1004);
+                       W_ERROR_HAVE_NO_MEMORY(info->info1004);
+                       init_srv_share_info_1004(p, info->info1004, snum);
+                       break;
+               case 1005:
+                       info->info1005 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo1005);
+                       W_ERROR_HAVE_NO_MEMORY(info->info1005);
+                       init_srv_share_info_1005(p, info->info1005, snum);
+                       break;
+               case 1006:
+                       info->info1006 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo1006);
+                       W_ERROR_HAVE_NO_MEMORY(info->info1006);
+                       init_srv_share_info_1006(p, info->info1006, snum);
+                       break;
+               case 1007:
+                       info->info1007 = TALLOC_P(p->mem_ctx, struct srvsvc_NetShareInfo1007);
+                       W_ERROR_HAVE_NO_MEMORY(info->info1007);
+                       init_srv_share_info_1007(p, info->info1007, snum);
+                       break;
+               case 1501:
+                       init_srv_share_info_1501(p, info->info1501, snum);
+                       break;
+               default:
+                       DEBUG(5,("_srvsvc_NetShareGetInfo: unsupported switch value %d\n",
+                               r->in.level));
+                       status = WERR_UNKNOWN_LEVEL;
+                       break;
        }
 
-       if (lp_registry_shares()) {
-               char *keyname;
-               struct registry_key *key;
-               WERROR err;
-               TALLOC_CTX *mem_ctx;
+       DEBUG(5,("_srvsvc_NetShareGetInfo: %d\n", __LINE__));
 
-               if (!(keyname = talloc_asprintf(NULL, "%s\\%s", KEY_SMBCONF,
-                                               share_name))) {
-                       return WERR_NOMEM;
-               }
+       return status;
+}
 
-               mem_ctx = (TALLOC_CTX *)keyname;
+/*******************************************************************
+ Check a given DOS pathname is valid for a share.
+********************************************************************/
 
-               err = reg_open_path(mem_ctx, keyname, REG_KEY_WRITE,
-                                   is_disk_op ? get_root_nt_token():token,
-                                   &key);
-               if (!W_ERROR_IS_OK(err)) {
-                       TALLOC_FREE(mem_ctx);
-                       return err;
-               }
+char *valid_share_pathname(TALLOC_CTX *ctx, const char *dos_pathname)
+{
+       char *ptr = NULL;
 
-               setval_helper(key, "path", path, &err);
+       if (!dos_pathname) {
+               return NULL;
+       }
 
-               reg_deletevalue(key, "comment");
-               if ((comment != NULL) && (comment[0] != '\0')) {
-                       setval_helper(key, "comment", comment, &err);
-               }
+       ptr = talloc_strdup(ctx, dos_pathname);
+       if (!ptr) {
+               return NULL;
+       }
+       /* Convert any '\' paths to '/' */
+       unix_format(ptr);
+       ptr = unix_clean_name(ctx, ptr);
+       if (!ptr) {
+               return NULL;
+       }
 
-               reg_deletevalue(key, "max connections");
-               if (max_connections != 0) {
-                       char tmp[16];
-                       snprintf(tmp, sizeof(tmp), "%d", max_connections);
-                       setval_helper(key, "max connections", tmp, &err);
-               }
+       /* NT is braindead - it wants a C: prefix to a pathname ! So strip it. */
+       if (strlen(ptr) > 2 && ptr[1] == ':' && ptr[0] != '/')
+               ptr += 2;
 
-               TALLOC_FREE(mem_ctx);
-               return err;
-       }               
+       /* Only absolute paths allowed. */
+       if (*ptr != '/')
+               return NULL;
 
-       return WERR_ACCESS_DENIED;
+       return ptr;
 }
 
 /*******************************************************************
Net share set info. Modify share details.
_srvsvc_NetShareSetInfo. Modify share details.
 ********************************************************************/
 
-WERROR _srvsvc_NetShareSetInfo(pipes_struct *p, struct srvsvc_NetShareSetInfo *r)
+WERROR _srvsvc_NetShareSetInfo(pipes_struct *p,
+                              struct srvsvc_NetShareSetInfo *r)
 {
-       pstring comment;
-       pstring pathname;
+       struct current_user user;
+       char *command = NULL;
+       char *share_name = NULL;
+       char *comment = NULL;
+       const char *pathname = NULL;
        int type;
        int snum;
-       char *path;
+       int ret;
+       char *path = NULL;
        SEC_DESC *psd = NULL;
        SE_PRIV se_diskop = SE_DISK_OPERATOR;
-       BOOL is_disk_op = False;
+       bool is_disk_op = False;
        int max_connections = 0;
-       fstring tmp_share_name;
+       TALLOC_CTX *ctx = p->mem_ctx;
+       union srvsvc_NetShareInfo *info = r->in.info;
 
-       DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetShareSetInfo: %d\n", __LINE__));
+
+       share_name = talloc_strdup(p->mem_ctx, r->in.share_name);
+       if (!share_name) {
+               return WERR_NOMEM;
+       }
 
        if (r->out.parm_error) {
                *r->out.parm_error = 0;
        }
 
-       if ( strequal(r->in.share_name,"IPC$") 
-               || ( lp_enable_asu_support() && strequal(r->in.share_name,"ADMIN$") )
-               || strequal(r->in.share_name,"global") )
+       if ( strequal(share_name,"IPC$")
+               || ( lp_enable_asu_support() && strequal(share_name,"ADMIN$") )
+               || strequal(share_name,"global") )
        {
                return WERR_ACCESS_DENIED;
        }
 
-       fstrcpy(tmp_share_name, r->in.share_name);
-       snum = find_service(tmp_share_name);
+       snum = find_service(share_name);
 
        /* Does this share exist ? */
        if (snum < 0)
@@ -1677,39 +1510,48 @@ WERROR _srvsvc_NetShareSetInfo(pipes_struct *p, struct srvsvc_NetShareSetInfo *r
        if (lp_print_ok(snum))
                return WERR_ACCESS_DENIED;
 
-       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token,
-                                         &se_diskop );
-       
+       get_current_user(&user,p);
+
+       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
+
        /* fail out now if you are not root and not a disk op */
-       
-       if ( p->pipe_user.ut.uid != sec_initial_uid() && !is_disk_op )
+
+       if ( user.ut.uid != sec_initial_uid() && !is_disk_op )
                return WERR_ACCESS_DENIED;
 
        switch (r->in.level) {
        case 1:
-               pstrcpy(pathname, lp_pathname(snum));
-               pstrcpy(comment, r->in.info.info1->comment);
-               type = r->in.info.info1->type;
+               pathname = talloc_strdup(ctx, lp_pathname(snum));
+               comment = talloc_strdup(ctx, info->info1->comment);
+               type = info->info1->type;
                psd = NULL;
                break;
        case 2:
-               pstrcpy(comment, r->in.info.info2->comment);
-               pstrcpy(pathname, r->in.info.info2->path);
-               type = r->in.info.info2->type;
-               max_connections = (r->in.info.info2->max_users == 0xffffffff) ?
-                       0 : r->in.info.info2->max_users;
+               comment = talloc_strdup(ctx, info->info2->comment);
+               pathname = info->info2->path;
+               type = info->info2->type;
+               max_connections = (info->info2->max_users == (uint32_t)-1) ?
+                       0 : info->info2->max_users;
+               psd = NULL;
+               break;
+#if 0
+               /* not supported on set but here for completeness */
+       case 501:
+               comment = talloc_strdup(ctx, info->info501->comment);
+               type = info->info501->type;
                psd = NULL;
                break;
+#endif
        case 502:
-               pstrcpy(comment, r->in.info.info502->comment);
-               pstrcpy(pathname, r->in.info.info502->path);
-               type = r->in.info.info502->type;
-               psd = r->in.info.info502->sd;
+               comment = talloc_strdup(ctx, info->info502->comment);
+               pathname = info->info502->path;
+               type = info->info502->type;
+               psd = info->info502->sd_buf.sd;
                map_generic_share_sd_bits(psd);
                break;
        case 1004:
-               pstrcpy(pathname, lp_pathname(snum));
-               pstrcpy(comment, r->in.info.info1004->comment);
+               pathname = talloc_strdup(ctx, lp_pathname(snum));
+               comment = talloc_strdup(ctx, info->info1004->comment);
                type = STYPE_DISKTREE;
                break;
        case 1005:
@@ -1717,63 +1559,100 @@ WERROR _srvsvc_NetShareSetInfo(pipes_struct *p, struct srvsvc_NetShareSetInfo *r
                   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 (((r->in.info.info1005->dfs_flags &
+               if (((info->info1005->dfs_flags &
                      SHARE_1005_CSC_POLICY_MASK) >>
                     SHARE_1005_CSC_POLICY_SHIFT) == lp_csc_policy(snum))
                        return WERR_OK;
                else {
-                       DEBUG(3, ("_srv_net_share_set_info: client is trying "
-                                 "to change csc policy from the network; "
-                                 "must be done with smb.conf\n"));
+                       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;
                }
        case 1006:
        case 1007:
                return WERR_ACCESS_DENIED;
        case 1501:
-               pstrcpy(pathname, lp_pathname(snum));
-               pstrcpy(comment, lp_comment(snum));
-               psd = r->in.info.info1501->sd;
+               pathname = talloc_strdup(ctx, lp_pathname(snum));
+               comment = talloc_strdup(ctx, lp_comment(snum));
+               psd = info->info1501->sd;
                map_generic_share_sd_bits(psd);
                type = STYPE_DISKTREE;
                break;
        default:
-               DEBUG(5,("_srv_net_share_set_info: unsupported switch value "
-                        "%d\n", r->in.level));
+               DEBUG(5,("_srvsvc_NetShareSetInfo: unsupported switch value %d\n",
+                       r->in.level));
                return WERR_UNKNOWN_LEVEL;
        }
 
        /* We can only modify disk shares. */
        if (type != STYPE_DISKTREE)
                return WERR_ACCESS_DENIED;
-               
+
+       if (comment == NULL) {
+               return WERR_NOMEM;
+       }
+
        /* Check if the pathname is valid. */
-       if (!(path = valid_share_pathname( pathname )))
+       if (!(path = valid_share_pathname(p->mem_ctx, pathname )))
                return WERR_OBJECT_PATH_INVALID;
 
-       /* Ensure share name, pathname and comment don't contain '"'
-        * characters. */
-       string_replace(tmp_share_name, '"', ' ');
+       /* Ensure share name, pathname and comment don't contain '"' characters. */
+       string_replace(share_name, '"', ' ');
        string_replace(path, '"', ' ');
        string_replace(comment, '"', ' ');
 
-       DEBUG(10,("_srv_net_share_set_info: change share command = %s\n",
-                 lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));
+       DEBUG(10,("_srvsvc_NetShareSetInfo: change share command = %s\n",
+               lp_change_share_cmd() ? lp_change_share_cmd() : "NULL" ));
 
        /* Only call modify function if something changed. */
-       
-       if (strcmp(path, lp_pathname(snum))
-           || strcmp(comment, lp_comment(snum)) 
-           || (lp_max_connections(snum) != max_connections) ) {
-               WERROR err;
-
-               err = change_share(tmp_share_name, path, comment,
-                                  max_connections, p->pipe_user.nt_user_token,
-                                  is_disk_op);
-
-               if (!W_ERROR_IS_OK(err)) {
-                       return err;
+
+       if (strcmp(path, lp_pathname(snum)) || strcmp(comment, lp_comment(snum))
+                       || (lp_max_connections(snum) != max_connections)) {
+               if (!lp_change_share_cmd() || !*lp_change_share_cmd()) {
+                       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(),
+                               get_dyn_CONFIGFILE(),
+                               share_name,
+                               path,
+                               comment ? comment : "",
+                               max_connections);
+               if (!command) {
+                       return WERR_NOMEM;
+               }
+
+               DEBUG(10,("_srvsvc_NetShareSetInfo: Running [%s]\n", command ));
+
+               /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
+
+               if (is_disk_op)
+                       become_root();
+
+               if ( (ret = smbrun(command, NULL)) == 0 ) {
+                       /* Tell everyone we updated smb.conf. */
+                       message_send_all(smbd_messaging_context(),
+                                        MSG_SMB_CONF_UPDATED, NULL, 0,
+                                        NULL);
                }
+
+               if ( is_disk_op )
+                       unbecome_root();
+
+               /********* END SeDiskOperatorPrivilege BLOCK *********/
+
+               DEBUG(3,("_srvsvc_NetShareSetInfo: Running [%s] returned (%d)\n",
+                       command, ret ));
+
+               TALLOC_FREE(command);
+
+               if ( ret != 0 )
+                       return WERR_ACCESS_DENIED;
+       } else {
+               DEBUG(10,("_srvsvc_NetShareSetInfo: No change to share name (%s)\n",
+                       share_name ));
        }
 
        /* Replace SD if changed. */
@@ -1781,54 +1660,62 @@ WERROR _srvsvc_NetShareSetInfo(pipes_struct *p, struct srvsvc_NetShareSetInfo *r
                SEC_DESC *old_sd;
                size_t sd_size;
 
-               old_sd = get_share_security(p->mem_ctx, lp_servicename(snum),
-                                           &sd_size);
+               old_sd = get_share_security(p->mem_ctx, lp_servicename(snum), &sd_size);
 
                if (old_sd && !sec_desc_equal(old_sd, psd)) {
-                       if (!set_share_security(r->in.share_name, psd)) {
-                               DEBUG(0,("_srv_net_share_set_info: Failed to "
-                                        "change security info in share %s.\n",
-                                        r->in.share_name ));
-                       }
+                       if (!set_share_security(share_name, psd))
+                               DEBUG(0,("_srvsvc_NetShareSetInfo: Failed to change security info in share %s.\n",
+                                       share_name ));
                }
        }
-                       
-       DEBUG(5,("_srv_net_share_set_info: %d\n", __LINE__));
+
+       DEBUG(5,("_srvsvc_NetShareSetInfo: %d\n", __LINE__));
 
        return WERR_OK;
 }
 
-
 /*******************************************************************
- Net share add. Call 'add_share_command "sharename" "pathname" 
+ _srvsvc_NetShareAdd.
+ Call 'add_share_command "sharename" "pathname"
  "comment" "max connections = "
 ********************************************************************/
 
-WERROR _srvsvc_NetShareAdd(pipes_struct *p, struct srvsvc_NetShareAdd *r)
+WERROR _srvsvc_NetShareAdd(pipes_struct *p,
+                          struct srvsvc_NetShareAdd *r)
 {
-       pstring share_name;
-       pstring comment;
-       pstring pathname;
-       char *path;
+       struct current_user user;
+       char *command = NULL;
+       char *share_name = NULL;
+       char *comment = NULL;
+       char *pathname = NULL;
        int type;
+       int snum;
+       int ret;
+       char *path;
        SEC_DESC *psd = NULL;
        SE_PRIV se_diskop = SE_DISK_OPERATOR;
-       BOOL is_disk_op;
-       uint32 max_connections = 0;
-       WERROR err;
+       bool is_disk_op;
+       int max_connections = 0;
+       TALLOC_CTX *ctx = p->mem_ctx;
 
-       DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetShareAdd: %d\n", __LINE__));
 
        if (r->out.parm_error) {
                *r->out.parm_error = 0;
        }
 
-       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token,
-                                         &se_diskop );
+       get_current_user(&user,p);
+
+       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
 
-       if (p->pipe_user.ut.uid != sec_initial_uid()  && !is_disk_op ) 
+       if (user.ut.uid != sec_initial_uid()  && !is_disk_op )
                return WERR_ACCESS_DENIED;
 
+       if (!lp_add_share_cmd() || !*lp_add_share_cmd()) {
+               DEBUG(10,("_srvsvc_NetShareAdd: No add share command\n"));
+               return WERR_ACCESS_DENIED;
+       }
+
        switch (r->in.level) {
        case 0:
                /* No path. Not enough info in a level 0 to do anything. */
@@ -1837,27 +1724,28 @@ WERROR _srvsvc_NetShareAdd(pipes_struct *p, struct srvsvc_NetShareAdd *r)
                /* Not enough info in a level 1 to do anything. */
                return WERR_ACCESS_DENIED;
        case 2:
-               pstrcpy(share_name, r->in.info.info2->name);
-               pstrcpy(comment, r->in.info.info2->comment);
-               pstrcpy(pathname, r->in.info.info2->path);
-               max_connections = (r->in.info.info2->max_users == 0xffffffff) ?
-                       0 : r->in.info.info2->max_users;
-               type = r->in.info.info2->type;
+               share_name = talloc_strdup(ctx, r->in.info->info2->name);
+               comment = talloc_strdup(ctx, r->in.info->info2->comment);
+               pathname = talloc_strdup(ctx, r->in.info->info2->path);
+               max_connections = (r->in.info->info2->max_users == (uint32_t)-1) ?
+                       0 : r->in.info->info2->max_users;
+               type = r->in.info->info2->type;
                break;
        case 501:
                /* No path. Not enough info in a level 501 to do anything. */
                return WERR_ACCESS_DENIED;
        case 502:
-               pstrcpy(share_name, r->in.info.info502->name);
-               pstrcpy(comment, r->in.info.info502->comment);
-               pstrcpy(pathname, r->in.info.info502->path);
-               type = r->in.info.info502->type;
-               psd = r->in.info.info502->sd;
+               share_name = talloc_strdup(ctx, r->in.info->info502->name);
+               comment = talloc_strdup(ctx, r->in.info->info502->comment);
+               pathname = talloc_strdup(ctx, r->in.info->info502->path);
+               max_connections = (r->in.info->info502->max_users == (uint32_t)-1) ?
+                       0 : r->in.info->info502->max_users;
+               type = r->in.info->info502->type;
+               psd = r->in.info->info502->sd_buf.sd;
                map_generic_share_sd_bits(psd);
                break;
 
-               /* none of the following contain share names.  NetShareAdd
-                * does not have a separate parameter for the share name */ 
+               /* none of the following contain share names.  NetShareAdd does not have a separate parameter for the share name */
 
        case 1004:
        case 1005:
@@ -1868,57 +1756,95 @@ WERROR _srvsvc_NetShareAdd(pipes_struct *p, struct srvsvc_NetShareAdd *r)
                /* DFS only level. */
                return WERR_ACCESS_DENIED;
        default:
-               DEBUG(5,("_srv_net_share_add: unsupported switch value %d\n",
-                        r->in.level));
+               DEBUG(5,("_srvsvc_NetShareAdd: unsupported switch value %d\n",
+                       r->in.level));
                return WERR_UNKNOWN_LEVEL;
        }
 
        /* check for invalid share names */
 
-       if ( !validate_net_name( share_name, INVALID_SHARENAME_CHARS,
-                                sizeof(share_name) ) ) {
-               DEBUG(5,("_srv_net_name_validate: Bad sharename \"%s\"\n",
-                        share_name));
+       if (!share_name || !validate_net_name(share_name,
+                               INVALID_SHARENAME_CHARS,
+                               strlen(share_name))) {
+               DEBUG(5,("_srvsvc_NetShareAdd: Bad sharename \"%s\"\n",
+                                       share_name ? share_name : ""));
                return WERR_INVALID_NAME;
        }
 
-       if ( strequal(share_name,"IPC$") || strequal(share_name,"global")
-            || ( lp_enable_asu_support() && strequal(share_name,"ADMIN$") ) )
-       {
+       if (strequal(share_name,"IPC$") || strequal(share_name,"global")
+                       || (lp_enable_asu_support() &&
+                                       strequal(share_name,"ADMIN$"))) {
                return WERR_ACCESS_DENIED;
        }
 
-       if (get_share_params(p->mem_ctx, share_name) != NULL) {
-               /* Share already exists. */
-               return WERR_ALREADY_EXISTS;
+       snum = find_service(share_name);
+
+       /* Share already exists. */
+       if (snum >= 0) {
+               return WERR_FILE_EXISTS;
        }
 
        /* We can only add disk shares. */
-       if (type != STYPE_DISKTREE)
+       if (type != STYPE_DISKTREE) {
                return WERR_ACCESS_DENIED;
-               
+       }
+
        /* Check if the pathname is valid. */
-       if (!(path = valid_share_pathname( pathname )))
+       if (!(path = valid_share_pathname(p->mem_ctx, pathname))) {
                return WERR_OBJECT_PATH_INVALID;
+       }
 
-       /* Ensure share name, pathname and comment don't contain '"'
-        * characters. */
-
+       /* Ensure share name, pathname and comment don't contain '"' characters. */
        string_replace(share_name, '"', ' ');
        string_replace(path, '"', ' ');
-       string_replace(comment, '"', ' ');
+       if (comment) {
+               string_replace(comment, '"', ' ');
+       }
+
+       command = talloc_asprintf(ctx,
+                       "%s \"%s\" \"%s\" \"%s\" \"%s\" %d",
+                       lp_add_share_cmd(),
+                       get_dyn_CONFIGFILE(),
+                       share_name,
+                       path,
+                       comment ? comment : "",
+                       max_connections);
+       if (!command) {
+               return WERR_NOMEM;
+       }
+
+       DEBUG(10,("_srvsvc_NetShareAdd: Running [%s]\n", command ));
+
+       /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
+
+       if ( is_disk_op )
+               become_root();
 
-       err = add_share(share_name, path, comment, max_connections,
-                       p->pipe_user.nt_user_token, is_disk_op);
+       /* FIXME: use libnetconf here - gd */
 
-       if (!W_ERROR_IS_OK(err)) {
-               return err;
+       if ( (ret = smbrun(command, NULL)) == 0 ) {
+               /* Tell everyone we updated smb.conf. */
+               message_send_all(smbd_messaging_context(),
+                                MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
        }
 
+       if ( is_disk_op )
+               unbecome_root();
+
+       /********* END SeDiskOperatorPrivilege BLOCK *********/
+
+       DEBUG(3,("_srvsvc_NetShareAdd: Running [%s] returned (%d)\n",
+               command, ret ));
+
+       TALLOC_FREE(command);
+
+       if ( ret != 0 )
+               return WERR_ACCESS_DENIED;
+
        if (psd) {
                if (!set_share_security(share_name, psd)) {
-                       DEBUG(0,("_srv_net_share_add: Failed to add security "
-                                "info to share %s.\n", share_name ));
+                       DEBUG(0,("_srvsvc_NetShareAdd: Failed to add security info to share %s.\n",
+                               share_name ));
                }
        }
 
@@ -1928,320 +1854,403 @@ WERROR _srvsvc_NetShareAdd(pipes_struct *p, struct srvsvc_NetShareAdd *r)
         * from the client. JRA.
         */
 
-       DEBUG(5,("_srv_net_share_add: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetShareAdd: %d\n", __LINE__));
 
        return WERR_OK;
 }
 
 /*******************************************************************
- Net share delete. Call "delete share command" with the share name as
+ _srvsvc_NetShareDel
+ Call "delete share command" with the share name as
  a parameter.
 ********************************************************************/
 
-WERROR _srvsvc_NetShareDel(pipes_struct *p, struct srvsvc_NetShareDel *r)
+WERROR _srvsvc_NetShareDel(pipes_struct *p,
+                          struct srvsvc_NetShareDel *r)
 {
-       struct share_params *params;
+       struct current_user user;
+       char *command = NULL;
+       char *share_name = NULL;
+       int ret;
+       int snum;
        SE_PRIV se_diskop = SE_DISK_OPERATOR;
-       BOOL is_disk_op;
-       WERROR err;
+       bool is_disk_op;
+       struct share_params *params;
+       TALLOC_CTX *ctx = p->mem_ctx;
 
-       DEBUG(5,("_srv_net_share_del: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetShareDel: %d\n", __LINE__));
 
-       if ( strequal(r->in.share_name, "IPC$") 
-            || ( lp_enable_asu_support() && strequal(r->in.share_name,"ADMIN$") )
-            || strequal(r->in.share_name, "global") )
+       share_name = talloc_strdup(p->mem_ctx, r->in.share_name);
+       if (!share_name) {
+               return WERR_NET_NAME_NOT_FOUND;
+       }
+       if ( strequal(share_name,"IPC$")
+               || ( lp_enable_asu_support() && strequal(share_name,"ADMIN$") )
+               || strequal(share_name,"global") )
        {
                return WERR_ACCESS_DENIED;
        }
 
-       if (!(params = get_share_params(p->mem_ctx, r->in.share_name))) {
+       if (!(params = get_share_params(p->mem_ctx, share_name))) {
                return WERR_NO_SUCH_SHARE;
        }
 
+       snum = find_service(share_name);
+
        /* No change to printer shares. */
-       if (lp_print_ok(params->service))
+       if (lp_print_ok(snum))
                return WERR_ACCESS_DENIED;
 
-       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token,
-                                         &se_diskop );
+       get_current_user(&user,p);
+
+       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
+
+       if (user.ut.uid != sec_initial_uid()  && !is_disk_op )
+               return WERR_ACCESS_DENIED;
 
-       if (p->pipe_user.ut.uid != sec_initial_uid()  && !is_disk_op ) 
+       if (!lp_delete_share_cmd() || !*lp_delete_share_cmd()) {
+               DEBUG(10,("_srvsvc_NetShareDel: No delete share command\n"));
                return WERR_ACCESS_DENIED;
+       }
+
+       command = talloc_asprintf(ctx,
+                       "%s \"%s\" \"%s\"",
+                       lp_delete_share_cmd(),
+                       get_dyn_CONFIGFILE(),
+                       lp_servicename(snum));
+       if (!command) {
+               return WERR_NOMEM;
+       }
+
+       DEBUG(10,("_srvsvc_NetShareDel: Running [%s]\n", command ));
 
-       err = delete_share(lp_servicename(params->service),
-                          p->pipe_user.nt_user_token, is_disk_op);
+       /********* BEGIN SeDiskOperatorPrivilege BLOCK *********/
 
-       if (!W_ERROR_IS_OK(err)) {
-               return err;
+       if ( is_disk_op )
+               become_root();
+
+       if ( (ret = smbrun(command, NULL)) == 0 ) {
+               /* Tell everyone we updated smb.conf. */
+               message_send_all(smbd_messaging_context(),
+                                MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
        }
 
+       if ( is_disk_op )
+               unbecome_root();
+
+       /********* END SeDiskOperatorPrivilege BLOCK *********/
+
+       DEBUG(3,("_srvsvc_NetShareDel: Running [%s] returned (%d)\n", command, ret ));
+
+       if ( ret != 0 )
+               return WERR_ACCESS_DENIED;
+
        /* Delete the SD in the database. */
-       delete_share_security(params);
+       delete_share_security(lp_servicename(params->service));
 
        lp_killservice(params->service);
 
        return WERR_OK;
 }
 
-WERROR _srvsvc_NetShareDelSticky(pipes_struct *p, struct srvsvc_NetShareDelSticky *r)
+/*******************************************************************
+ _srvsvc_NetShareDelSticky
+********************************************************************/
+
+WERROR _srvsvc_NetShareDelSticky(pipes_struct *p,
+                                struct srvsvc_NetShareDelSticky *r)
 {
-       struct srvsvc_NetShareDel s;
+       struct srvsvc_NetShareDel q;
 
-       DEBUG(5,("_srv_net_share_del_stick: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetShareDelSticky: %d\n", __LINE__));
 
-       s.in.server_unc = r->in.server_unc;
-       s.in.share_name = r->in.share_name;
-       s.in.reserved = r->in.reserved;
+       q.in.server_unc         = r->in.server_unc;
+       q.in.share_name         = r->in.share_name;
+       q.in.reserved           = r->in.reserved;
 
-       return _srvsvc_NetShareDel(p, &s);
+       return _srvsvc_NetShareDel(p, &q);
 }
 
 /*******************************************************************
-time of day
+ _srvsvc_NetRemoteTOD
 ********************************************************************/
 
-WERROR _srvsvc_NetRemoteTOD(pipes_struct *p, struct srvsvc_NetRemoteTOD *r)
+WERROR _srvsvc_NetRemoteTOD(pipes_struct *p,
+                           struct srvsvc_NetRemoteTOD *r)
 {
+       struct srvsvc_NetRemoteTODInfo *tod;
        struct tm *t;
        time_t unixdate = time(NULL);
-       WERROR status = WERR_OK;
 
        /* 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;
 
-       DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
+       DEBUG(5,("_srvsvc_NetRemoteTOD: %d\n", __LINE__));
+
+       if ( !(tod = TALLOC_ZERO_P(p->mem_ctx, struct srvsvc_NetRemoteTODInfo)) )
+               return WERR_NOMEM;
+
+       *r->out.info = tod;
+
+       DEBUG(5,("_srvsvc_NetRemoteTOD: %d\n", __LINE__));
 
        t = gmtime(&unixdate);
 
        /* set up the */
-       r->out.info->elapsed = unixdate;
-       r->out.info->msecs = 0;
-       r->out.info->hours = t->tm_hour;
-       r->out.info->mins = t->tm_min;
-       r->out.info->secs = t->tm_sec;
-       r->out.info->hunds = 0;
-       r->out.info->timezone = zone;
-       r->out.info->tinterval = 10000;
-       r->out.info->day = t->tm_mday;
-       r->out.info->month = t->tm_mon + 1;
-       r->out.info->year = 1900+t->tm_year;
-       r->out.info->weekday = t->tm_wday;
-       
-       DEBUG(5,("_srv_net_remote_tod: %d\n", __LINE__));
+       init_srvsvc_NetRemoteTODInfo(tod,
+                                    unixdate,
+                                    0,
+                                    t->tm_hour,
+                                    t->tm_min,
+                                    t->tm_sec,
+                                    0,
+                                    zone,
+                                    10000,
+                                    t->tm_mday,
+                                    t->tm_mon + 1,
+                                    1900+t->tm_year,
+                                    t->tm_wday);
+
+       DEBUG(5,("_srvsvc_NetRemoteTOD: %d\n", __LINE__));
 
-       return status;
+       return WERR_OK;
 }
 
 /***********************************************************************************
+ _srvsvc_NetGetFileSecurity
  Win9x NT tools get security descriptor.
 ***********************************************************************************/
 
-WERROR _srvsvc_NetGetFileSecurity(pipes_struct *p, struct srvsvc_NetGetFileSecurity *r)
+WERROR _srvsvc_NetGetFileSecurity(pipes_struct *p,
+                                 struct srvsvc_NetGetFileSecurity *r)
 {
        SEC_DESC *psd = NULL;
        size_t sd_size;
-       DATA_BLOB null_pw;
-       files_struct *fsp = NULL;
+       fstring servicename;
        SMB_STRUCT_STAT st;
        NTSTATUS nt_status;
+       WERROR werr;
        connection_struct *conn = NULL;
-       BOOL became_user = False; 
-       WERROR status = WERR_OK;
-       pstring tmp_file;
+       struct sec_desc_buf *sd_buf = NULL;
+       files_struct *fsp = NULL;
+       int snum;
+       char *oldcwd = NULL;
 
        ZERO_STRUCT(st);
 
+       fstrcpy(servicename, r->in.share);
 
-       /* Null password is ok - we are already an authenticated user... */
-       null_pw = data_blob_null;
-
-       become_root();
-       conn = make_connection(r->in.share, null_pw, "A:", p->pipe_user.vuid, &nt_status);
-       unbecome_root();
-
-       if (conn == NULL) {
-               DEBUG(3,("_srv_net_file_query_secdesc: Unable to connect to %s\n", r->in.share));
-               status = ntstatus_to_werror(nt_status);
-               goto error_exit;
-       }
-
-       if (!become_user(conn, conn->vuid)) {
-               DEBUG(0,("_srv_net_file_query_secdesc: Can't become connected user!\n"));
-               status = WERR_ACCESS_DENIED;
+       snum = find_service(servicename);
+       if (snum == -1) {
+               DEBUG(10, ("Could not find service %s\n", servicename));
+               werr = WERR_NET_NAME_NOT_FOUND;
                goto error_exit;
        }
-       became_user = True;
 
-       pstrcpy(tmp_file, r->in.file);
-       nt_status = unix_convert(conn, tmp_file, False, NULL, &st);
+       nt_status = create_conn_struct(talloc_tos(), &conn, snum,
+                                      lp_pathname(snum), &oldcwd);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(3,("_srv_net_file_query_secdesc: bad pathname %s\n", r->in.file));
-               status = WERR_ACCESS_DENIED;
+               DEBUG(10, ("create_conn_struct failed: %s\n",
+                          nt_errstr(nt_status)));
+               werr = ntstatus_to_werror(nt_status);
                goto error_exit;
        }
 
-       nt_status = check_name(conn, r->in.file);
+       conn->server_info = p->server_info;
+
+       nt_status = create_file(
+               conn,                                   /* conn */
+               NULL,                                   /* req */
+               0,                                      /* root_dir_fid */
+               r->in.file,                             /* fname */
+               FILE_READ_ATTRIBUTES,                   /* access_mask */
+               FILE_SHARE_READ|FILE_SHARE_WRITE,       /* share_access */
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               0,                                      /* file_attributes */
+               INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               NULL,                                   /* pinfo */
+               NULL);                                  /* psbuf */
+
        if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(3,("_srv_net_file_query_secdesc: can't access %s\n", r->in.file));
-               status = WERR_ACCESS_DENIED;
+               DEBUG(3,("_srvsvc_NetGetFileSecurity: can't open %s\n",
+                        r->in.file));
+               werr = ntstatus_to_werror(nt_status);
                goto error_exit;
        }
 
-       nt_status = open_file_stat(conn, r->in.file, &st, &fsp);
+       nt_status = SMB_VFS_FGET_NT_ACL(fsp,
+                                      (OWNER_SECURITY_INFORMATION
+                                       |GROUP_SECURITY_INFORMATION
+                                       |DACL_SECURITY_INFORMATION), &psd);
+
        if (!NT_STATUS_IS_OK(nt_status)) {
-               /* Perhaps it is a directory */
-               if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_IS_A_DIRECTORY))
-                       nt_status = open_directory(conn, r->in.file, &st,
-                                       READ_CONTROL_ACCESS,
-                                       FILE_SHARE_READ|FILE_SHARE_WRITE,
-                                       FILE_OPEN,
-                                       0,
-                                       FILE_ATTRIBUTE_DIRECTORY,
-                                       NULL, &fsp);
-
-               if (!NT_STATUS_IS_OK(nt_status)) {
-                       DEBUG(3,("_srv_net_file_query_secdesc: Unable to open file %s\n", r->in.file));
-                       status = WERR_ACCESS_DENIED;
-                       goto error_exit;
-               }
+               DEBUG(3,("_srvsvc_NetGetFileSecurity: Unable to get NT ACL "
+                        "for file %s\n", r->in.file));
+               werr = ntstatus_to_werror(nt_status);
+               goto error_exit;
        }
 
-       sd_size = SMB_VFS_GET_NT_ACL(fsp, fsp->fsp_name, (OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION), &psd);
+       sd_size = ndr_size_security_descriptor(psd, 0);
 
-       if (sd_size == 0) {
-               DEBUG(3,("_srv_net_file_query_secdesc: Unable to get NT ACL for file %s\n", r->in.file));
-               status = WERR_ACCESS_DENIED;
+       sd_buf = TALLOC_ZERO_P(p->mem_ctx, struct sec_desc_buf);
+       if (!sd_buf) {
+               werr = WERR_NOMEM;
                goto error_exit;
        }
 
-       r->out.sd_buf->sd_size= sd_size;
-       r->out.sd_buf->sd = psd;
+       sd_buf->sd_size = sd_size;
+       sd_buf->sd = psd;
 
-       psd->dacl->revision = (uint16) NT4_ACL_REVISION;
+       *r->out.sd_buf = sd_buf;
 
-       close_file(fsp, NORMAL_CLOSE);
-       unbecome_user();
-       close_cnum(conn, p->pipe_user.vuid);
-       return status;
+       psd->dacl->revision = NT4_ACL_REVISION;
+
+       close_file(NULL, fsp, NORMAL_CLOSE);
+       vfs_ChDir(conn, oldcwd);
+       conn_free_internal(conn);
+       return WERR_OK;
 
 error_exit:
 
-       if(fsp) {
-               close_file(fsp, NORMAL_CLOSE);
+       if (fsp) {
+               close_file(NULL, fsp, NORMAL_CLOSE);
        }
 
-       if (became_user)
-               unbecome_user();
+       if (oldcwd) {
+               vfs_ChDir(conn, oldcwd);
+       }
 
-       if (conn) 
-               close_cnum(conn, p->pipe_user.vuid);
+       if (conn) {
+               conn_free_internal(conn);
+       }
 
-       return status;
+       return werr;
 }
 
 /***********************************************************************************
+ _srvsvc_NetSetFileSecurity
  Win9x NT tools set security descriptor.
 ***********************************************************************************/
 
-WERROR _srvsvc_NetSetFileSecurity(pipes_struct *p, struct srvsvc_NetSetFileSecurity *r)
+WERROR _srvsvc_NetSetFileSecurity(pipes_struct *p,
+                                 struct srvsvc_NetSetFileSecurity *r)
 {
-       DATA_BLOB null_pw;
+       fstring servicename;
        files_struct *fsp = NULL;
        SMB_STRUCT_STAT st;
        NTSTATUS nt_status;
+       WERROR werr;
        connection_struct *conn = NULL;
-       BOOL became_user = False;
-       WERROR status = WERR_OK;
-       pstring tmp_file;
+       int snum;
+       char *oldcwd = NULL;
+       struct security_descriptor *psd = NULL;
+       uint32_t security_info_sent = 0;
 
        ZERO_STRUCT(st);
 
-       /* Null password is ok - we are already an authenticated user... */
-       null_pw = data_blob_null;
-
-       become_root();
-       conn = make_connection(r->in.share, null_pw, "A:", p->pipe_user.vuid, &nt_status);
-       unbecome_root();
-
-       if (conn == NULL) {
-               DEBUG(3,("_srv_net_file_set_secdesc: Unable to connect to %s\n", r->in.share));
-               status = ntstatus_to_werror(nt_status);
-               goto error_exit;
-       }
+       fstrcpy(servicename, r->in.share);
 
-       if (!become_user(conn, conn->vuid)) {
-               DEBUG(0,("_srv_net_file_set_secdesc: Can't become connected user!\n"));
-               status = WERR_ACCESS_DENIED;
+       snum = find_service(servicename);
+       if (snum == -1) {
+               DEBUG(10, ("Could not find service %s\n", servicename));
+               werr = WERR_NET_NAME_NOT_FOUND;
                goto error_exit;
        }
-       became_user = True;
 
-       pstrcpy(tmp_file, r->in.file);
-       nt_status = unix_convert(conn, tmp_file, False, NULL, &st);
+       nt_status = create_conn_struct(talloc_tos(), &conn, snum,
+                                      lp_pathname(snum), &oldcwd);
        if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(3,("_srv_net_file_set_secdesc: bad pathname %s\n", r->in.file));
-               status = WERR_ACCESS_DENIED;
+               DEBUG(10, ("create_conn_struct failed: %s\n",
+                          nt_errstr(nt_status)));
+               werr = ntstatus_to_werror(nt_status);
                goto error_exit;
        }
 
-       nt_status = check_name(conn, r->in.file);
+       conn->server_info = p->server_info;
+
+       nt_status = create_file(
+               conn,                                   /* conn */
+               NULL,                                   /* req */
+               0,                                      /* root_dir_fid */
+               r->in.file,                             /* fname */
+               FILE_WRITE_ATTRIBUTES,                  /* access_mask */
+               FILE_SHARE_READ|FILE_SHARE_WRITE,       /* share_access */
+               FILE_OPEN,                              /* create_disposition*/
+               0,                                      /* create_options */
+               0,                                      /* file_attributes */
+               INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               NULL,                                   /* pinfo */
+               NULL);                                  /* psbuf */
+
        if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(3,("_srv_net_file_set_secdesc: can't access %s\n", r->in.file));
-               status = WERR_ACCESS_DENIED;
+               DEBUG(3,("_srvsvc_NetSetFileSecurity: can't open %s\n",
+                        r->in.file));
+               werr = ntstatus_to_werror(nt_status);
                goto error_exit;
        }
 
+       psd = r->in.sd_buf->sd;
+       security_info_sent = r->in.securityinformation;
 
-       nt_status = open_file_stat(conn, r->in.file, &st, &fsp);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               /* Perhaps it is a directory */
-               if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_IS_A_DIRECTORY))
-                       nt_status = open_directory(conn, r->in.file, &st,
-                                               FILE_READ_ATTRIBUTES,
-                                               FILE_SHARE_READ|FILE_SHARE_WRITE,
-                                               FILE_OPEN,
-                                               0,
-                                               FILE_ATTRIBUTE_DIRECTORY,
-                                               NULL, &fsp);
-
-               if (!NT_STATUS_IS_OK(nt_status)) {
-                       DEBUG(3,("_srv_net_file_set_secdesc: Unable to open file %s\n", r->in.file));
-                       status = WERR_ACCESS_DENIED;
-                       goto error_exit;
-               }
+       if (psd->owner_sid==0) {
+               security_info_sent &= ~OWNER_SECURITY_INFORMATION;
+       }
+       if (psd->group_sid==0) {
+               security_info_sent &= ~GROUP_SECURITY_INFORMATION;
+       }
+       if (psd->sacl==0) {
+               security_info_sent &= ~SACL_SECURITY_INFORMATION;
+       }
+       if (psd->dacl==0) {
+               security_info_sent &= ~DACL_SECURITY_INFORMATION;
        }
 
-       nt_status = SMB_VFS_SET_NT_ACL(fsp, fsp->fsp_name, r->in.securityinformation, r->in.sd_buf.sd);
+       /* Convert all the generic bits. */
+       security_acl_map_generic(psd->dacl, &file_generic_mapping);
+       security_acl_map_generic(psd->sacl, &file_generic_mapping);
 
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               DEBUG(3,("_srv_net_file_set_secdesc: Unable to set NT ACL on file %s\n", r->in.file));
-               status = WERR_ACCESS_DENIED;
+       nt_status = SMB_VFS_FSET_NT_ACL(fsp,
+                                       security_info_sent,
+                                       psd);
+
+       if (!NT_STATUS_IS_OK(nt_status) ) {
+               DEBUG(3,("_srvsvc_NetSetFileSecurity: Unable to set NT ACL "
+                        "on file %s\n", r->in.share));
+               werr = WERR_ACCESS_DENIED;
                goto error_exit;
        }
 
-       close_file(fsp, NORMAL_CLOSE);
-       unbecome_user();
-       close_cnum(conn, p->pipe_user.vuid);
-       return status;
+       close_file(NULL, fsp, NORMAL_CLOSE);
+       vfs_ChDir(conn, oldcwd);
+       conn_free_internal(conn);
+       return WERR_OK;
 
 error_exit:
 
-       if(fsp) {
-               close_file(fsp, NORMAL_CLOSE);
+       if (fsp) {
+               close_file(NULL, fsp, NORMAL_CLOSE);
        }
 
-       if (became_user) {
-               unbecome_user();
+       if (oldcwd) {
+               vfs_ChDir(conn, oldcwd);
        }
 
        if (conn) {
-               close_cnum(conn, p->pipe_user.vuid);
+               conn_free_internal(conn);
        }
 
-       return status;
+       return werr;
 }
 
 /***********************************************************************************
@@ -2290,68 +2299,67 @@ static const char *next_server_disk_enum(uint32 *resume)
        return disk;
 }
 
-WERROR _srvsvc_NetDiskEnum(pipes_struct *p, struct srvsvc_NetDiskEnum *r)
+/********************************************************************
+ _srvsvc_NetDiskEnum
+********************************************************************/
+
+WERROR _srvsvc_NetDiskEnum(pipes_struct *p,
+                          struct srvsvc_NetDiskEnum *r)
 {
        uint32 i;
        const char *disk_name;
+       TALLOC_CTX *ctx = p->mem_ctx;
+       WERROR werr;
+       uint32_t resume = r->in.resume_handle ? *r->in.resume_handle : 0;
 
-       WERROR status = WERR_OK;
+       werr = WERR_OK;
 
-       *r->out.totalentries = init_server_disk_enum(r->in.resume_handle);
-       r->out.info->count = 0;
+       *r->out.totalentries = init_server_disk_enum(&resume);
 
-       if(!(r->out.info->disks =  TALLOC_ARRAY(p->mem_ctx, struct srvsvc_NetDiskInfo0, MAX_SERVER_DISK_ENTRIES))) {
-               return WERR_NOMEM;
-       }
+       r->out.info->disks = TALLOC_ZERO_ARRAY(ctx, struct srvsvc_NetDiskInfo0,
+                                              MAX_SERVER_DISK_ENTRIES);
+       W_ERROR_HAVE_NO_MEMORY(r->out.info->disks);
 
        /*allow one struct srvsvc_NetDiskInfo0 for null terminator*/
 
-       for(i = 0; i < MAX_SERVER_DISK_ENTRIES -1 && (disk_name = next_server_disk_enum(r->in.resume_handle)); i++) {
+       for(i = 0; i < MAX_SERVER_DISK_ENTRIES -1 && (disk_name = next_server_disk_enum(&resume)); i++) {
 
                r->out.info->count++;
-               (*r->out.totalentries)++;
 
                /*copy disk name into a unicode string*/
 
-               r->out.info->disks[i].disk = disk_name; 
+               r->out.info->disks[i].disk = talloc_strdup(ctx, disk_name);
+               W_ERROR_HAVE_NO_MEMORY(r->out.info->disks[i].disk);
        }
 
        /* add a terminating null string.  Is this there if there is more data to come? */
 
        r->out.info->count++;
-       (*r->out.totalentries)++;
 
-       r->out.info->disks[i].disk = "";
+       r->out.info->disks[i].disk = talloc_strdup(ctx, "");
+       W_ERROR_HAVE_NO_MEMORY(r->out.info->disks[i].disk);
 
-       return status;
+       if (r->out.resume_handle) {
+               *r->out.resume_handle = resume;
+       }
+
+       return werr;
 }
 
 /********************************************************************
+ _srvsvc_NetNameValidate
 ********************************************************************/
 
-WERROR _srvsvc_NetNameValidate(pipes_struct *p, struct srvsvc_NetNameValidate *r)
+WERROR _srvsvc_NetNameValidate(pipes_struct *p,
+                              struct srvsvc_NetNameValidate *r)
 {
-       int len;
-
-       if ((r->in.flags != 0x0) && (r->in.flags != 0x80000000)) {
-               return WERR_INVALID_PARAM;
-       }
-
-       switch ( r->in.name_type ) {
+       switch (r->in.name_type) {
        case 0x9:
-               len = strlen_m(r->in.name);
-
-               if ((r->in.flags == 0x0) && (len > 81)) {
-                       DEBUG(5,("_srv_net_name_validate: share name too long (%s > 81 chars)\n", r->in.name));
-                       return WERR_INVALID_NAME;
-               }
-               if ((r->in.flags == 0x80000000) && (len > 13)) {
-                       DEBUG(5,("_srv_net_name_validate: share name too long (%s > 13 chars)\n", r->in.name));
-                       return WERR_INVALID_NAME;
-               }
-
-               if ( ! validate_net_name( r->in.name, INVALID_SHARENAME_CHARS, sizeof(r->in.name) ) ) {
-                       DEBUG(5,("_srv_net_name_validate: Bad sharename \"%s\"\n", r->in.name));
+               if (!validate_net_name(r->in.name, INVALID_SHARENAME_CHARS,
+                                      strlen_m(r->in.name)))
+               {
+                       DEBUG(5,("_srvsvc_NetNameValidate: Bad sharename \"%s\"\n",
+                               r->in.name));
                        return WERR_INVALID_NAME;
                }
                break;
@@ -2363,15 +2371,71 @@ WERROR _srvsvc_NetNameValidate(pipes_struct *p, struct srvsvc_NetNameValidate *r
        return WERR_OK;
 }
 
+/*******************************************************************
+********************************************************************/
+
+static void enum_file_close_fn( const struct share_mode_entry *e,
+                          const char *sharepath, const char *fname,
+                         void *private_data )
+{
+       char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE];
+       struct srvsvc_NetFileClose *r =
+               (struct srvsvc_NetFileClose *)private_data;
+       uint32_t fid = (((uint32_t)(procid_to_pid(&e->pid))<<16) | e->share_file_id);
+
+       if (fid != r->in.fid) {
+               return; /* Not this file. */
+       }
+
+       if (!process_exists(e->pid) ) {
+               return;
+       }
+
+       /* 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) ));
+
+       share_mode_entry_to_message(msg, e);
+
+       r->out.result = ntstatus_to_werror(
+                       messaging_send_buf(smbd_messaging_context(),
+                               e->pid, MSG_SMB_CLOSE_FILE,
+                               (uint8 *)msg,
+                               MSG_SMB_SHARE_MODE_ENTRY_SIZE));
+}
 
 /********************************************************************
+ Close a file given a 32-bit file id.
 ********************************************************************/
 
 WERROR _srvsvc_NetFileClose(pipes_struct *p, struct srvsvc_NetFileClose *r)
 {
-       return WERR_ACCESS_DENIED;
+       struct current_user user;
+       SE_PRIV se_diskop = SE_DISK_OPERATOR;
+       bool is_disk_op;
+
+       DEBUG(5,("_srvsvc_NetFileClose: %d\n", __LINE__));
+
+       get_current_user(&user,p);
+
+       is_disk_op = user_has_privileges( p->pipe_user.nt_user_token, &se_diskop );
+
+       if (user.ut.uid != sec_initial_uid() && !is_disk_op) {
+               return WERR_ACCESS_DENIED;
+       }
+
+       /* enum_file_close_fn sends the close message to
+        * the relevent smbd process. */
+
+       r->out.result = WERR_BADFILE;
+       share_mode_forall( enum_file_close_fn, (void *)r);
+       return r->out.result;
 }
 
+/********************************************************************
+********************************************************************/
+
 WERROR _srvsvc_NetCharDevEnum(pipes_struct *p, struct srvsvc_NetCharDevEnum *r)
 {
        p->rng_fault_state = True;
@@ -2534,7 +2598,7 @@ WERROR _srvsvc_NETRDFSDELETELOCALPARTITION(pipes_struct *p, struct srvsvc_NETRDF
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _srvsvc_NETRDFSSETLOCALVOLUMESTATE(pipes_struct *p, struct srvsvc_NETRDFSSETLOCALVOLUMESTATE *R)
+WERROR _srvsvc_NETRDFSSETLOCALVOLUMESTATE(pipes_struct *p, struct srvsvc_NETRDFSSETLOCALVOLUMESTATE *r)
 {
        p->rng_fault_state = True;
        return WERR_NOT_SUPPORTED;
@@ -2558,26 +2622,27 @@ WERROR _srvsvc_NETRDFSDELETEEXITPOINT(pipes_struct *p, struct srvsvc_NETRDFSDELE
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _srvsvc_NETRSERVERTRANSPORTDELEX(pipes_struct *p, struct srvsvc_NETRSERVERTRANSPORTDELEX *r)
+WERROR _srvsvc_NETRDFSMODIFYPREFIX(pipes_struct *p, struct srvsvc_NETRDFSMODIFYPREFIX *r)
 {
        p->rng_fault_state = True;
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _srvsvc_NETRDFSMANAGERREPORTSITEINFO(pipes_struct *p, struct srvsvc_NETRDFSMANAGERREPORTSITEINFO *r)
+WERROR _srvsvc_NETRDFSFIXLOCALVOLUME(pipes_struct *p, struct srvsvc_NETRDFSFIXLOCALVOLUME *r)
 {
        p->rng_fault_state = True;
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _srvsvc_NETRDFSMODIFYPREFIX(pipes_struct *p, struct srvsvc_NETRDFSMODIFYPREFIX *r)
+WERROR _srvsvc_NETRDFSMANAGERREPORTSITEINFO(pipes_struct *p, struct srvsvc_NETRDFSMANAGERREPORTSITEINFO *r)
 {
        p->rng_fault_state = True;
        return WERR_NOT_SUPPORTED;
 }
 
-WERROR _srvsvc_NETRDFSFIXLOCALVOLUME(pipes_struct *p, struct srvsvc_NETRDFSFIXLOCALVOLUME *r)
+WERROR _srvsvc_NETRSERVERTRANSPORTDELEX(pipes_struct *p, struct srvsvc_NETRSERVERTRANSPORTDELEX *r)
 {
        p->rng_fault_state = True;
        return WERR_NOT_SUPPORTED;
 }
+