loadparm: make the source3/ lp_ functions take an explicit TALLOC_CTX *.
[jlayton/samba.git] / source3 / smbd / lanman.c
index 1970c86c33922c83396ee0feeaac5a87cd5890cb..1b5de57ad4e580402dd5ea0ef238e11749f39a17 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    Inter-process communication and named pipe handling
    Copyright (C) Andrew Tridgell 1992-1998
    */
 
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
-#include "../librpc/gen_ndr/cli_samr.h"
-#include "../librpc/gen_ndr/cli_spoolss.h"
-#include "../librpc/gen_ndr/srv_samr.h"
-#include "../librpc/gen_ndr/srv_spoolss.h"
+#include "rpc_client/rpc_client.h"
+#include "../librpc/gen_ndr/ndr_samr_c.h"
+#include "../librpc/gen_ndr/ndr_spoolss_c.h"
+#include "rpc_client/cli_spoolss.h"
+#include "rpc_client/init_spoolss.h"
+#include "../librpc/gen_ndr/ndr_srvsvc_c.h"
 #include "../librpc/gen_ndr/rap.h"
 #include "../lib/util/binsearch.h"
+#include "../libcli/auth/libcli_auth.h"
+#include "rpc_client/init_lsa.h"
+#include "../libcli/security/security.h"
+#include "printing.h"
+#include "passdb/machine_sid.h"
+#include "auth.h"
+#include "rpc_server/rpc_ncacn_np.h"
 
 #ifdef CHECK_TYPES
 #undef CHECK_TYPES
@@ -68,14 +78,16 @@ static char *smb_realloc_limit(void *ptr, size_t size)
        return val;
 }
 
-static bool api_Unsupported(connection_struct *conn, uint16 vuid,
+static bool api_Unsupported(struct smbd_server_connection *sconn,
+                           connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt,
                                char **rdata, char **rparam,
                                int *rdata_len, int *rparam_len);
 
-static bool api_TooSmall(connection_struct *conn, uint16 vuid, char *param, char *data,
+static bool api_TooSmall(struct smbd_server_connection *sconn,
+                        connection_struct *conn, uint64_t vuid, char *param, char *data,
                         int mdrcnt, int mprcnt,
                         char **rdata, char **rparam,
                         int *rdata_len, int *rparam_len);
@@ -98,18 +110,18 @@ static int CopyExpanded(connection_struct *conn,
                *p_space_remaining = 0;
                return 0;
        }
-       buf = talloc_string_sub(ctx, buf,"%S",lp_servicename(snum));
+       buf = talloc_string_sub(ctx, buf,"%S", lp_servicename(ctx, snum));
        if (!buf) {
                *p_space_remaining = 0;
                return 0;
        }
        buf = talloc_sub_advanced(ctx,
-                               lp_servicename(SNUM(conn)),
-                               conn->server_info->unix_name,
+                                 lp_servicename(ctx, SNUM(conn)),
+                               conn->session_info->unix_info->unix_name,
                                conn->connectpath,
-                               conn->server_info->utok.gid,
-                               conn->server_info->sanitized_username,
-                               pdb_get_domain(conn->server_info->sam_account),
+                               conn->session_info->unix_token->gid,
+                               conn->session_info->unix_info->sanitized_username,
+                               conn->session_info->info->domain_name,
                                buf);
        if (!buf) {
                *p_space_remaining = 0;
@@ -150,17 +162,17 @@ static int StrlenExpanded(connection_struct *conn, int snum, char *s)
        if (!buf) {
                return 0;
        }
-       buf = talloc_string_sub(ctx,buf,"%S",lp_servicename(snum));
+       buf = talloc_string_sub(ctx,buf,"%S",lp_servicename(ctx, snum));
        if (!buf) {
                return 0;
        }
        buf = talloc_sub_advanced(ctx,
-                               lp_servicename(SNUM(conn)),
-                               conn->server_info->unix_name,
+                                 lp_servicename(ctx, SNUM(conn)),
+                               conn->session_info->unix_info->unix_name,
                                conn->connectpath,
-                               conn->server_info->utok.gid,
-                               conn->server_info->sanitized_username,
-                               pdb_get_domain(conn->server_info->sam_account),
+                               conn->session_info->unix_token->gid,
+                               conn->session_info->unix_info->sanitized_username,
+                               conn->session_info->info->domain_name,
                                buf);
        if (!buf) {
                return 0;
@@ -184,7 +196,7 @@ struct pack_desc {
        int buflen;        /* remaining size for fixed part; on init: length of base */
        int subcount;       /* count of substructures */
        char *structbuf;  /* pointer into buffer for remaining fixed part */
-       int stringlen;    /* remaining size for variable part */                
+       int stringlen;    /* remaining size for variable part */
        char *stringbuf;  /* pointer into buffer for remaining variable part */
        int neededlen;    /* total needed size */
        int usedlen;        /* total used size (usedlen <= neededlen and usedlen <= buflen) */
@@ -505,7 +517,7 @@ static int check_printq_info(struct pack_desc* desc,
 #define RAP_QUEUE_STATUS_PAUSED 1
 #define RAP_QUEUE_STATUS_ERROR 2
 
-/* turn a print job status into a on the wire status 
+/* turn a print job status into a on the wire status
 */
 static int printj_spoolss_status(int v)
 {
@@ -520,7 +532,7 @@ static int printj_spoolss_status(int v)
        return 0;
 }
 
-/* turn a print queue status into a on the wire status 
+/* turn a print queue status into a on the wire status
 */
 static int printq_spoolss_status(int v)
 {
@@ -531,21 +543,6 @@ static int printq_spoolss_status(int v)
        return RAP_QUEUE_STATUS_ERROR;
 }
 
-static time_t spoolss_Time_to_time_t(const struct spoolss_Time *r)
-{
-       struct tm unixtime;
-
-       unixtime.tm_year        = r->year - 1900;
-       unixtime.tm_mon         = r->month - 1;
-       unixtime.tm_wday        = r->day_of_week;
-       unixtime.tm_mday        = r->day;
-       unixtime.tm_hour        = r->hour;
-       unixtime.tm_min         = r->minute;
-       unixtime.tm_sec         = r->second;
-
-       return mktime(&unixtime);
-}
-
 static void fill_spoolss_printjob_info(int uLevel,
                                       struct pack_desc *desc,
                                       struct spoolss_JobInfo2 *info2,
@@ -615,9 +612,9 @@ static void fill_printq_info_52(struct spoolss_DriverInfo3 *driver,
 {
        int                             i;
        fstring                         location;
-       trim_string((char *)driver->driver_path, "\\print$\\WIN40\\0\\", 0);
-       trim_string((char *)driver->data_file, "\\print$\\WIN40\\0\\", 0);
-       trim_string((char *)driver->help_file, "\\print$\\WIN40\\0\\", 0);
+       trim_string(discard_const_p(char, driver->driver_path), "\\print$\\WIN40\\0\\", 0);
+       trim_string(discard_const_p(char, driver->data_file), "\\print$\\WIN40\\0\\", 0);
+       trim_string(discard_const_p(char, driver->help_file), "\\print$\\WIN40\\0\\", 0);
 
        PACKI(desc, "W", 0x0400);                     /* don't know */
        PACKS(desc, "z", driver->driver_name);        /* long printer name */
@@ -644,7 +641,7 @@ static void fill_printq_info_52(struct spoolss_DriverInfo3 *driver,
 
        for ( i=0; i<count && driver->dependent_files && *driver->dependent_files[i]; i++)
        {
-               trim_string((char *)driver->dependent_files[i], "\\print$\\WIN40\\0\\", 0);
+               trim_string(discard_const_p(char, driver->dependent_files[i]), "\\print$\\WIN40\\0\\", 0);
                PACKS(desc,"z",driver->dependent_files[i]);         /* driver files to copy */
                DEBUG(3,("Dependent File: %s:\n", driver->dependent_files[i]));
        }
@@ -660,6 +657,20 @@ static void fill_printq_info_52(struct spoolss_DriverInfo3 *driver,
 
 }
 
+static const char *strip_unc(const char *unc)
+{
+       char *p;
+
+       if (unc == NULL) {
+               return NULL;
+       }
+
+       if ((p = strrchr(unc, '\\')) != NULL) {
+               return p+1;
+       }
+
+       return unc;
+}
 
 static void fill_printq_info(int uLevel,
                             struct pack_desc* desc,
@@ -669,14 +680,15 @@ static void fill_printq_info(int uLevel,
                             struct spoolss_PrinterInfo2 *printer_info)
 {
        switch (uLevel) {
+       case 0:
        case 1:
        case 2:
-               PACKS(desc,"B13", printer_info->printername);
+               PACKS(desc,"B13", strip_unc(printer_info->printername));
                break;
        case 3:
        case 4:
        case 5:
-               PACKS(desc,"z", printer_info->printername);
+               PACKS(desc,"z", strip_unc(printer_info->printername));
                break;
        case 51:
                PACKI(desc,"K", printq_spoolss_status(printer_info->status));
@@ -690,7 +702,7 @@ static void fill_printq_info(int uLevel,
                PACKI(desc,"W",0);              /* until time */
                PACKS(desc,"z","");             /* pSepFile */
                PACKS(desc,"z","lpd");  /* pPrProc */
-               PACKS(desc,"z", printer_info->printername); /* pDestinations */
+               PACKS(desc,"z", strip_unc(printer_info->printername)); /* pDestinations */
                PACKS(desc,"z","");             /* pParms */
                if (printer_info->printername == NULL) {
                        PACKS(desc,"z","UNKNOWN PRINTER");
@@ -715,7 +727,7 @@ static void fill_printq_info(int uLevel,
                   Win9X/ME printer comments. */
                PACKI(desc,"W", printq_spoolss_status(printer_info->status)); /* fsStatus */
                PACKI(desc,(uLevel == 3 ? "W" : "N"),count);    /* cJobs */
-               PACKS(desc,"z", printer_info->printername); /* pszPrinters */
+               PACKS(desc,"z", strip_unc(printer_info->printername)); /* pszPrinters */
                PACKS(desc,"z", printer_info->drivername);              /* pszDriverName */
                PackDriverData(desc);   /* pDriverData */
        }
@@ -743,7 +755,8 @@ static int get_printerdrivernumber(const struct spoolss_DriverInfo3 *driver)
        return result;
 }
 
-static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
+static bool api_DosPrintQGetInfo(struct smbd_server_connection *sconn,
+                                connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -764,10 +777,11 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
        TALLOC_CTX *mem_ctx = talloc_tos();
        NTSTATUS status;
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct policy_handle handle;
        struct spoolss_DevmodeContainer devmode_ctr;
        union spoolss_DriverInfo driver_info;
-       union spoolss_JobInfo *job_info;
+       union spoolss_JobInfo *job_info = NULL;
        union spoolss_PrinterInfo printer_info;
 
        if (!str1 || !str2 || !p) {
@@ -812,23 +826,32 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
 
        ZERO_STRUCT(handle);
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       if (QueueName == NULL || (strlen(QueueName) < 1)) {
+               desc.errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_DosPrintQGetInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
                                            QueueName,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           PRINTER_ACCESS_USE,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -840,6 +863,16 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
                goto out;
        }
 
+       werr = rpccli_spoolss_getprinter(cli, mem_ctx,
+                                        &handle,
+                                        2,
+                                        0,
+                                        &printer_info);
+       if (!W_ERROR_IS_OK(werr)) {
+               desc.errcode = W_ERROR_V(werr);
+               goto out;
+       }
+
        if (uLevel==52) {
                uint32_t server_major_version;
                uint32_t server_minor_version;
@@ -911,8 +944,8 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
                desc.errcode = ERRbuftoosmall;
 
  out:
-       if (is_valid_policy_hnd(&handle)) {
-               rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
        *rdata_len = desc.usedlen;
@@ -937,7 +970,8 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
  View list of all print jobs on all queues.
 ****************************************************************************/
 
-static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
+static bool api_DosPrintQEnum(struct smbd_server_connection *sconn,
+                             connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt,
@@ -958,9 +992,12 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
        TALLOC_CTX *mem_ctx = talloc_tos();
        NTSTATUS status;
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct spoolss_DevmodeContainer devmode_ctr;
        uint32_t num_printers;
        union spoolss_PrinterInfo *printer_info;
+       union spoolss_DriverInfo *driver_info;
+       union spoolss_JobInfo **job_info;
 
        if (!param_format || !output_format1 || !p) {
                return False;
@@ -991,15 +1028,19 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
                return(True);
        }
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_DosPrintQEnum: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        werr = rpccli_spoolss_enumprinters(cli, mem_ctx,
                                           PRINTER_ENUM_LOCAL,
@@ -1015,6 +1056,16 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
 
        queuecnt = num_printers;
 
+       job_info = talloc_array(mem_ctx, union spoolss_JobInfo *, num_printers);
+       if (job_info == NULL) {
+               goto err;
+       }
+
+       driver_info = talloc_array(mem_ctx, union spoolss_DriverInfo, num_printers);
+       if (driver_info == NULL) {
+               goto err;
+       }
+
        if((subcntarr = SMB_MALLOC_ARRAY(int,queuecnt)) == NULL) {
                DEBUG(0,("api_DosPrintQEnum: malloc fail !\n"));
                goto err;
@@ -1034,17 +1085,21 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
 
                uint32_t num_jobs;
                struct policy_handle handle;
-               union spoolss_DriverInfo driver_info;
-               union spoolss_JobInfo *job_info;
+               const char *printername;
+
+               printername = talloc_strdup(mem_ctx, printer_info[i].info2.printername);
+               if (printername == NULL) {
+                       goto err;
+               }
 
                ZERO_STRUCT(handle);
                ZERO_STRUCT(devmode_ctr);
 
-               status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
-                                                   printer_info[i].info2.printername,
-                                                   NULL,
+               status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
+                                                   printername,
+                                                   "RAW",
                                                    devmode_ctr,
-                                                   SEC_FLAG_MAXIMUM_ALLOWED,
+                                                   PRINTER_ACCESS_USE,
                                                    &handle,
                                                    &werr);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1063,7 +1118,7 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
                                               2, /* level */
                                               0, /* offered */
                                               &num_jobs,
-                                              &job_info);
+                                              &job_info[i]);
                if (!W_ERROR_IS_OK(werr)) {
                        desc.errcode = W_ERROR_V(werr);
                        goto out;
@@ -1080,7 +1135,7 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
                                                                0,
                                                                0, /* version */
                                                                0,
-                                                               &driver_info,
+                                                               &driver_info[i],
                                                                &server_major_version,
                                                                &server_minor_version);
                        if (!W_ERROR_IS_OK(werr)) {
@@ -1092,16 +1147,16 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
                subcntarr[i] = num_jobs;
                subcnt += subcntarr[i];
 
-               if (init_package(&desc,queuecnt,subcnt)) {
-                       fill_printq_info(uLevel,&desc,subcntarr[i], job_info, &driver_info.info3, &printer_info[i].info2);
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
+       }
+
+       if (init_package(&desc,queuecnt,subcnt)) {
+               for (i = 0; i < num_printers; i++) {
+                       fill_printq_info(uLevel,&desc,subcntarr[i], job_info[i], &driver_info[i].info3, &printer_info[i].info2);
                        if (desc.errcode == NERR_Success) {
                                succnt = i;
                        }
                }
-
-               if (is_valid_policy_hnd(&handle)) {
-                       rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
-               }
        }
 
        SAFE_FREE(subcntarr);
@@ -1130,7 +1185,7 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
  Get info level for a server list query.
 ****************************************************************************/
 
-static bool check_server_info(int uLevel, char* id)
+static bool check_session_info(int uLevel, char* id)
 {
        switch( uLevel ) {
                case 0:
@@ -1143,7 +1198,7 @@ static bool check_server_info(int uLevel, char* id)
                                return False;
                        }
                        break;
-               default: 
+               default:
                        return False;
        }
        return True;
@@ -1162,7 +1217,7 @@ struct srv_info_struct {
  number of entries.
 ******************************************************************/
 
-static int get_server_info(uint32 servertype, 
+static int get_session_info(uint32 servertype,
                           struct srv_info_struct **servers,
                           const char *domain)
 {
@@ -1203,7 +1258,7 @@ static int get_server_info(uint32 servertype,
                        alloced += 10;
                        *servers = SMB_REALLOC_ARRAY(*servers,struct srv_info_struct, alloced);
                        if (!*servers) {
-                               DEBUG(0,("get_server_info: failed to enlarge servers info struct!\n"));
+                               DEBUG(0,("get_session_info: failed to enlarge servers info struct!\n"));
                                TALLOC_FREE(lines);
                                return 0;
                        }
@@ -1262,7 +1317,7 @@ static int get_server_info(uint32 servertype,
                        ok = False;
                }
 
-               if ((servertype & SV_TYPE_DOMAIN_ENUM) != 
+               if ((servertype & SV_TYPE_DOMAIN_ENUM) !=
                                (s->type & SV_TYPE_DOMAIN_ENUM)) {
                        DEBUG(4,("s: dom mismatch "));
                        ok = False;
@@ -1294,8 +1349,8 @@ static int get_server_info(uint32 servertype,
  Fill in a server info structure.
 ******************************************************************/
 
-static int fill_srv_info(struct srv_info_struct *service, 
-                        int uLevel, char **buf, int *buflen, 
+static int fill_srv_info(struct srv_info_struct *service,
+                        int uLevel, char **buf, int *buflen,
                         char **stringbuf, int *stringspace, char *baseaddr)
 {
        int struct_len;
@@ -1372,7 +1427,7 @@ static int fill_srv_info(struct srv_info_struct *service,
 
 static int srv_comp(struct srv_info_struct *s1,struct srv_info_struct *s2)
 {
-       return StrCaseCmp(s1->name,s2->name);
+       return strcasecmp_m(s1->name,s2->name);
 }
 
 /****************************************************************************
@@ -1380,10 +1435,11 @@ static int srv_comp(struct srv_info_struct *s1,struct srv_info_struct *s2)
  extracted from lists saved by nmbd on the local host.
 ****************************************************************************/
 
-static bool api_RNetServerEnum2(connection_struct *conn, uint16 vuid,
+static bool api_RNetServerEnum2(struct smbd_server_connection *sconn,
+                               connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
-                               int mdrcnt, int mprcnt, char **rdata, 
+                               int mdrcnt, int mprcnt, char **rdata,
                                char **rparam, int *rdata_len, int *rparam_len)
 {
        char *str1 = get_safe_str_ptr(param, tpscnt, param, 2);
@@ -1415,9 +1471,9 @@ static bool api_RNetServerEnum2(connection_struct *conn, uint16 vuid,
        }
 
        /* If someone sets SV_TYPE_LOCAL_LIST_ONLY but hasn't set
-          any other bit (they may just set this bit on its own) they 
-          want all the locally seen servers. However this bit can be 
-          set on its own so set the requested servers to be 
+          any other bit (they may just set this bit on its own) they
+          want all the locally seen servers. However this bit can be
+          set on its own so set the requested servers to be
           ALL - DOMAIN_ENUM. */
 
        if ((servertype & SV_TYPE_LOCAL_LIST_ONLY) && !(servertype & SV_TYPE_DOMAIN_ENUM)) {
@@ -1432,7 +1488,7 @@ static bool api_RNetServerEnum2(connection_struct *conn, uint16 vuid,
        if (!prefix_ok(str1,"WrLehD")) {
                return False;
        }
-       if (!check_server_info(uLevel,str2)) {
+       if (!check_session_info(uLevel,str2)) {
                return False;
        }
 
@@ -1452,7 +1508,7 @@ static bool api_RNetServerEnum2(connection_struct *conn, uint16 vuid,
        DEBUG(4, ("domain [%s]\n", domain));
 
        if (lp_browse_list()) {
-               total = get_server_info(servertype,&servers,domain);
+               total = get_session_info(servertype,&servers,domain);
        }
 
        data_len = fixed_len = string_len = 0;
@@ -1540,7 +1596,7 @@ static int srv_name_match(const char *n1, const char *n2)
         *  the server will return a list of servers that exist on
         *  the network greater than or equal to the FirstNameToReturn.
         */
-       int ret = StrCaseCmp(n1, n2);
+       int ret = strcasecmp_m(n1, n2);
 
        if (ret <= 0) {
                return 0;
@@ -1549,7 +1605,8 @@ static int srv_name_match(const char *n1, const char *n2)
        return ret;
 }
 
-static bool api_RNetServerEnum3(connection_struct *conn, uint16 vuid,
+static bool api_RNetServerEnum3(struct smbd_server_connection *sconn,
+                               connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt, char **rdata,
@@ -1602,7 +1659,7 @@ static bool api_RNetServerEnum3(connection_struct *conn, uint16 vuid,
        if (strcmp(str1, "WrLehDzz") != 0) {
                return false;
        }
-       if (!check_server_info(uLevel,str2)) {
+       if (!check_session_info(uLevel,str2)) {
                return False;
        }
 
@@ -1627,7 +1684,7 @@ static bool api_RNetServerEnum3(connection_struct *conn, uint16 vuid,
                  domain, first_name));
 
        if (lp_browse_list()) {
-               total = get_server_info(servertype,&servers,domain);
+               total = get_session_info(servertype,&servers,domain);
        }
 
        data_len = fixed_len = string_len = 0;
@@ -1651,7 +1708,7 @@ static bool api_RNetServerEnum3(connection_struct *conn, uint16 vuid,
                         */
                        for (;first > 0;) {
                                int ret;
-                               ret = StrCaseCmp(first_name,
+                               ret = strcasecmp_m(first_name,
                                                 servers[first-1].name);
                                if (ret > 0) {
                                        break;
@@ -1741,10 +1798,11 @@ static bool api_RNetServerEnum3(connection_struct *conn, uint16 vuid,
   command 0x34 - suspected of being a "Lookup Names" stub api
   ****************************************************************************/
 
-static bool api_RNetGroupGetUsers(connection_struct *conn, uint16 vuid,
+static bool api_RNetGroupGetUsers(struct smbd_server_connection *sconn,
+                                 connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
-                               int mdrcnt, int mprcnt, char **rdata, 
+                               int mdrcnt, int mprcnt, char **rdata,
                                char **rparam, int *rdata_len, int *rparam_len)
 {
        char *str1 = get_safe_str_ptr(param,tpscnt,param,2);
@@ -1848,10 +1906,10 @@ static int fill_share_info(connection_struct *conn, int snum, int uLevel,
                len = 0;
 
                if (uLevel > 0) {
-                       len += StrlenExpanded(conn,snum,lp_comment(snum));
+                       len += StrlenExpanded(conn,snum,lp_comment(talloc_tos(), snum));
                }
                if (uLevel > 1) {
-                       len += strlen(lp_pathname(snum)) + 1;
+                       len += strlen(lp_pathname(talloc_tos(), snum)) + 1;
                }
                if (buflen) {
                        *buflen = struct_len;
@@ -1880,7 +1938,7 @@ static int fill_share_info(connection_struct *conn, int snum, int uLevel,
                baseaddr = p;
        }
 
-       push_ascii(p,lp_servicename(snum),13, STR_TERMINATE);
+       push_ascii(p,lp_servicename(talloc_tos(), snum),13, STR_TERMINATE);
 
        if (uLevel > 0) {
                int type;
@@ -1890,12 +1948,12 @@ static int fill_share_info(connection_struct *conn, int snum, int uLevel,
                if (lp_print_ok(snum)) {
                        type = STYPE_PRINTQ;
                }
-               if (strequal("IPC",lp_fstype(snum))) {
+               if (strequal("IPC",lp_fstype(talloc_tos(),snum))) {
                        type = STYPE_IPC;
                }
                SSVAL(p,14,type);               /* device type */
                SIVAL(p,16,PTR_DIFF(p2,baseaddr));
-               len += CopyExpanded(conn,snum,&p2,lp_comment(snum),&l2);
+               len += CopyExpanded(conn,snum,&p2,lp_comment(talloc_tos(),snum),&l2);
        }
 
        if (uLevel > 1) {
@@ -1903,7 +1961,7 @@ static int fill_share_info(connection_struct *conn, int snum, int uLevel,
                SSVALS(p,22,-1);                /* max uses */
                SSVAL(p,24,1); /* current uses */
                SIVAL(p,26,PTR_DIFF(p2,baseaddr)); /* local pathname */
-               len += CopyAndAdvance(&p2,lp_pathname(snum),&l2);
+               len += CopyAndAdvance(&p2,lp_pathname(talloc_tos(),snum),&l2);
                memset(p+30,0,SHPWLEN+2); /* passwd (reserved), pad field */
        }
 
@@ -1931,7 +1989,8 @@ static int fill_share_info(connection_struct *conn, int snum, int uLevel,
        return len;
 }
 
-static bool api_RNetShareGetInfo(connection_struct *conn,uint16 vuid,
+static bool api_RNetShareGetInfo(struct smbd_server_connection *sconn,
+                                connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -1940,17 +1999,18 @@ static bool api_RNetShareGetInfo(connection_struct *conn,uint16 vuid,
 {
        char *str1 = get_safe_str_ptr(param,tpscnt,param,2);
        char *str2 = skip_string(param,tpscnt,str1);
-       char *netname = skip_string(param,tpscnt,str2);
+       char *netname_in = skip_string(param,tpscnt,str2);
+       char *netname = NULL;
        char *p = skip_string(param,tpscnt,netname);
        int uLevel = get_safe_SVAL(param,tpscnt,p,0,-1);
        int snum;
 
-       if (!str1 || !str2 || !netname || !p) {
+       if (!str1 || !str2 || !netname_in || !p) {
                return False;
        }
 
-       snum = find_service(netname);
-       if (snum < 0) {
+       snum = find_service(talloc_tos(), netname_in, &netname);
+       if (snum < 0 || !netname) {
                return False;
        }
 
@@ -1994,7 +2054,8 @@ static bool api_RNetShareGetInfo(connection_struct *conn,uint16 vuid,
   Share names longer than 12 bytes must be skipped.
  ****************************************************************************/
 
-static bool api_RNetShareEnum( connection_struct *conn, uint16 vuid,
+static bool api_RNetShareEnum(struct smbd_server_connection *sconn,
+                             connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int                mdrcnt,
@@ -2031,7 +2092,7 @@ static bool api_RNetShareEnum( connection_struct *conn, uint16 vuid,
        /* Ensure all the usershares are loaded. */
        become_root();
        load_registry_shares();
-       count = load_usershare_shares();
+       count = load_usershare_shares(NULL, connections_snum_used);
        unbecome_root();
 
        data_len = fixed_len = string_len = 0;
@@ -2040,7 +2101,7 @@ static bool api_RNetShareEnum( connection_struct *conn, uint16 vuid,
                if (!(lp_browseable(i) && lp_snum_ok(i))) {
                        continue;
                }
-               push_ascii_fstring(servicename_dos, lp_servicename(i));
+               push_ascii_fstring(servicename_dos, lp_servicename(talloc_tos(), i));
                /* Maximum name length = 13. */
                if( lp_browseable( i ) && lp_snum_ok( i ) && (strlen(servicename_dos) < 13)) {
                        total++;
@@ -2072,7 +2133,8 @@ static bool api_RNetShareEnum( connection_struct *conn, uint16 vuid,
                        continue;
                }
 
-               push_ascii_fstring(servicename_dos, lp_servicename(i));
+               push_ascii_fstring(servicename_dos,
+                                  lp_servicename(talloc_tos(), i));
                if (lp_browseable(i) && lp_snum_ok(i) && (strlen(servicename_dos) < 13)) {
                        if (fill_share_info( conn,i,uLevel,&p,&f_len,&p2,&s_len,*rdata ) < 0) {
                                break;
@@ -2101,7 +2163,8 @@ static bool api_RNetShareEnum( connection_struct *conn, uint16 vuid,
   Add a share
   ****************************************************************************/
 
-static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
+static bool api_RNetShareAdd(struct smbd_server_connection *sconn,
+                            connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2115,12 +2178,18 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
        fstring sharename;
        fstring comment;
        char *pathname = NULL;
-       char *command, *cmdname;
        unsigned int offset;
-       int snum;
        int res = ERRunsup;
        size_t converted_size;
 
+       WERROR werr = WERR_OK;
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       NTSTATUS status;
+       struct rpc_pipe_client *cli = NULL;
+       union srvsvc_NetShareInfo info;
+       struct srvsvc_NetShareInfo2 info2;
+       struct dcerpc_binding_handle *b;
+
        if (!str1 || !str2 || !p) {
                return False;
        }
@@ -2141,11 +2210,6 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
                return False;
        }
        pull_ascii_fstring(sharename,data);
-       snum = find_service(sharename);
-       if (snum >= 0) { /* already exists */
-               res = ERRfilexists;
-               goto error_exit;
-       }
 
        if (mdrcnt < 28) {
                return False;
@@ -2159,7 +2223,7 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
        offset = IVAL(data, 16);
        if (offset >= mdrcnt) {
                res = ERRinvalidparam;
-               goto error_exit;
+               goto out;
        }
 
        /* Do we have a string ? */
@@ -2172,7 +2236,7 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
 
        if (offset >= mdrcnt) {
                res = ERRinvalidparam;
-               goto error_exit;
+               goto out;
        }
 
        /* Do we have a string ? */
@@ -2191,34 +2255,44 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
                return false;
        }
 
-       string_replace(sharename, '"', ' ');
-       string_replace(pathname, '"', ' ');
-       string_replace(comment, '"', ' ');
-
-       cmdname = lp_add_share_cmd();
-
-       if (!cmdname || *cmdname == '\0') {
-               return False;
+       status = rpc_pipe_open_interface(mem_ctx, &ndr_table_srvsvc.syntax_id,
+                                       conn->session_info,
+                                       conn->sconn->remote_address,
+                                       conn->sconn->msg_ctx,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_RNetShareAdd: could not connect to srvsvc: %s\n",
+                         nt_errstr(status)));
+               res = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
        }
 
-       if (asprintf(&command, "%s \"%s\" \"%s\" \"%s\" \"%s\"",
-                    lp_add_share_cmd(), get_dyn_CONFIGFILE(), sharename,
-                    pathname, comment) == -1) {
-               return false;
-       }
+       b = cli->binding_handle;
 
-       DEBUG(10,("api_RNetShareAdd: Running [%s]\n", command ));
+       info2.name              = sharename;
+       info2.type              = STYPE_DISKTREE;
+       info2.comment           = comment;
+       info2.permissions       = 0;
+       info2.max_users         = 0;
+       info2.current_users     = 0;
+       info2.path              = pathname;
+       info2.password          = NULL;
 
-       if ((res = smbrun(command, NULL)) != 0) {
-               DEBUG(1,("api_RNetShareAdd: Running [%s] returned (%d)\n",
-                        command, res ));
-               SAFE_FREE(command);
-               res = ERRnoaccess;
-               goto error_exit;
-       } else {
-               SAFE_FREE(command);
-               message_send_all(smbd_messaging_context(),
-                                MSG_SMB_CONF_UPDATED, NULL, 0, NULL);
+       info.info2 = &info2;
+
+       status = dcerpc_srvsvc_NetShareAdd(b, mem_ctx,
+                                          cli->srv_name_slash,
+                                          2,
+                                          &info,
+                                          NULL,
+                                          &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               res = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!W_ERROR_IS_OK(werr)) {
+               res = W_ERROR_V(werr);
+               goto out;
        }
 
        *rparam_len = 6;
@@ -2233,7 +2307,7 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
 
        return True;
 
-  error_exit:
+  out:
 
        *rparam_len = 4;
        *rparam = smb_realloc_limit(*rparam,*rparam_len);
@@ -2250,7 +2324,8 @@ static bool api_RNetShareAdd(connection_struct *conn,uint16 vuid,
   view list of groups available
   ****************************************************************************/
 
-static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
+static bool api_RNetGroupEnum(struct smbd_server_connection *sconn,
+                             connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2268,7 +2343,8 @@ static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
        uint32_t resume_handle;
        struct rpc_pipe_client *samr_pipe;
        struct policy_handle samr_handle, domain_handle;
-       NTSTATUS status;
+       NTSTATUS status, result;
+       struct dcerpc_binding_handle *b;
 
        if (!str1 || !str2 || !p) {
                return False;
@@ -2278,9 +2354,9 @@ static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
                return False;
        }
 
-       /* parameters  
+       /* parameters
         * W-> resume context (number of users to skip)
-        * r -> return parameter pointer to receive buffer 
+        * r -> return parameter pointer to receive buffer
         * L -> length of receive buffer
         * e -> return parameter number of entries
         * h -> return parameter total number of users
@@ -2290,30 +2366,46 @@ static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
                return False;
        }
 
-       status = rpc_pipe_open_internal(
-               talloc_tos(), &ndr_table_samr.syntax_id, rpc_samr_dispatch,
-               conn->server_info, &samr_pipe);
+       status = rpc_pipe_open_interface(
+               talloc_tos(), &ndr_table_samr.syntax_id,
+               conn->session_info, conn->sconn->remote_address,
+               conn->sconn->msg_ctx, &samr_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: Could not connect to samr: %s\n",
                          nt_errstr(status)));
                return false;
        }
 
-       status = rpccli_samr_Connect2(samr_pipe, talloc_tos(), global_myname(),
-                                     SAMR_ACCESS_LOOKUP_DOMAIN, &samr_handle);
+       b = samr_pipe->binding_handle;
+
+       status = dcerpc_samr_Connect2(b, talloc_tos(), lp_netbios_name(),
+                                     SAMR_ACCESS_LOOKUP_DOMAIN, &samr_handle,
+                                     &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_Connect2 failed: %s\n",
                          nt_errstr(status)));
                return false;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_Connect2 failed: %s\n",
+                         nt_errstr(result)));
+               return false;
+       }
 
-       status = rpccli_samr_OpenDomain(samr_pipe, talloc_tos(), &samr_handle,
+       status = dcerpc_samr_OpenDomain(b, talloc_tos(), &samr_handle,
                                        SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
-                                       get_global_sam_sid(), &domain_handle);
+                                       get_global_sam_sid(), &domain_handle,
+                                       &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_OpenDomain failed: %s\n",
                          nt_errstr(status)));
-               rpccli_samr_Close(samr_pipe, talloc_tos(), &samr_handle);
+               dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
+               return false;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_OpenDomain failed: %s\n",
+                         nt_errstr(result)));
+               dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
                return false;
        }
 
@@ -2338,19 +2430,26 @@ static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
                struct samr_SamArray *sam_entries;
                uint32_t num_entries;
 
-               status = rpccli_samr_EnumDomainGroups(samr_pipe, talloc_tos(),
+               status = dcerpc_samr_EnumDomainGroups(b, talloc_tos(),
                                                      &domain_handle,
                                                      &resume_handle,
                                                      &sam_entries, 1,
-                                                     &num_entries);
+                                                     &num_entries,
+                                                     &result);
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(10, ("rpccli_samr_EnumDomainGroups returned "
+                       DEBUG(10, ("dcerpc_samr_EnumDomainGroups returned "
                                   "%s\n", nt_errstr(status)));
                        break;
                }
+               if (!NT_STATUS_IS_OK(result)) {
+                       status = result;
+                       DEBUG(10, ("dcerpc_samr_EnumDomainGroups returned "
+                                  "%s\n", nt_errstr(result)));
+                       break;
+               }
 
                if (num_entries == 0) {
-                       DEBUG(10, ("rpccli_samr_EnumDomainGroups returned "
+                       DEBUG(10, ("dcerpc_samr_EnumDomainGroups returned "
                                   "no entries -- done\n"));
                        break;
                }
@@ -2385,8 +2484,8 @@ static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
                TALLOC_FREE(sam_entries);
        }
 
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &domain_handle);
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &samr_handle);
+       dcerpc_samr_Close(b, talloc_tos(), &domain_handle, &result);
+       dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
 
        *rdata_len = PTR_DIFF(p,*rdata);
 
@@ -2407,7 +2506,8 @@ static bool api_RNetGroupEnum(connection_struct *conn,uint16 vuid,
  Get groups that a user is a member of.
 ******************************************************************/
 
-static bool api_NetUserGetGroups(connection_struct *conn,uint16 vuid,
+static bool api_NetUserGetGroups(struct smbd_server_connection *sconn,
+                                connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2431,7 +2531,8 @@ static bool api_NetUserGetGroups(connection_struct *conn,uint16 vuid,
        struct lsa_Strings names;
        struct samr_Ids type, rid;
        struct samr_RidWithAttributeArray *rids;
-       NTSTATUS status;
+       NTSTATUS status, result;
+       struct dcerpc_binding_handle *b;
 
        if (!str1 || !str2 || !UserName || !p) {
                return False;
@@ -2471,42 +2572,63 @@ static bool api_NetUserGetGroups(connection_struct *conn,uint16 vuid,
        p = *rdata;
        endp = *rdata + *rdata_len;
 
-       status = rpc_pipe_open_internal(
-               talloc_tos(), &ndr_table_samr.syntax_id, rpc_samr_dispatch,
-               conn->server_info, &samr_pipe);
+       status = rpc_pipe_open_interface(
+               talloc_tos(), &ndr_table_samr.syntax_id,
+               conn->session_info, conn->sconn->remote_address,
+               conn->sconn->msg_ctx, &samr_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: Could not connect to samr: %s\n",
                          nt_errstr(status)));
                return false;
        }
 
-       status = rpccli_samr_Connect2(samr_pipe, talloc_tos(), global_myname(),
-                                     SAMR_ACCESS_LOOKUP_DOMAIN, &samr_handle);
+       b = samr_pipe->binding_handle;
+
+       status = dcerpc_samr_Connect2(b, talloc_tos(), lp_netbios_name(),
+                                     SAMR_ACCESS_LOOKUP_DOMAIN, &samr_handle,
+                                     &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_Connect2 failed: %s\n",
                          nt_errstr(status)));
                return false;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_Connect2 failed: %s\n",
+                         nt_errstr(result)));
+               return false;
+       }
 
-       status = rpccli_samr_OpenDomain(samr_pipe, talloc_tos(), &samr_handle,
+       status = dcerpc_samr_OpenDomain(b, talloc_tos(), &samr_handle,
                                        SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
-                                       get_global_sam_sid(), &domain_handle);
+                                       get_global_sam_sid(), &domain_handle,
+                                       &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_OpenDomain failed: %s\n",
                          nt_errstr(status)));
                goto close_sam;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_OpenDomain failed: %s\n",
+                         nt_errstr(result)));
+               goto close_sam;
+       }
 
        name.string = UserName;
 
-       status = rpccli_samr_LookupNames(samr_pipe, talloc_tos(),
+       status = dcerpc_samr_LookupNames(b, talloc_tos(),
                                         &domain_handle, 1, &name,
-                                        &rid, &type);
+                                        &rid, &type,
+                                        &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_LookupNames failed: %s\n",
                          nt_errstr(status)));
                goto close_domain;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_LookupNames failed: %s\n",
+                         nt_errstr(result)));
+               goto close_domain;
+       }
 
        if (type.ids[0] != SID_NAME_USER) {
                DEBUG(10, ("%s is a %s, not a user\n", UserName,
@@ -2514,31 +2636,44 @@ static bool api_NetUserGetGroups(connection_struct *conn,uint16 vuid,
                goto close_domain;
        }
 
-       status = rpccli_samr_OpenUser(samr_pipe, talloc_tos(),
+       status = dcerpc_samr_OpenUser(b, talloc_tos(),
                                      &domain_handle,
                                      SAMR_USER_ACCESS_GET_GROUPS,
-                                     rid.ids[0], &user_handle);
+                                     rid.ids[0], &user_handle,
+                                     &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_LookupNames failed: %s\n",
                          nt_errstr(status)));
                goto close_domain;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_LookupNames failed: %s\n",
+                         nt_errstr(result)));
+               goto close_domain;
+       }
 
-       status = rpccli_samr_GetGroupsForUser(samr_pipe, talloc_tos(),
-                                             &user_handle, &rids);
+       status = dcerpc_samr_GetGroupsForUser(b, talloc_tos(),
+                                             &user_handle, &rids,
+                                             &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_LookupNames failed: %s\n",
                          nt_errstr(status)));
                goto close_user;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_LookupNames failed: %s\n",
+                         nt_errstr(result)));
+               goto close_user;
+       }
 
        for (i=0; i<rids->count; i++) {
 
-               status = rpccli_samr_LookupRids(samr_pipe, talloc_tos(),
+               status = dcerpc_samr_LookupRids(b, talloc_tos(),
                                                &domain_handle,
                                                1, &rids->rids[i].rid,
-                                               &names, &type);
-               if (NT_STATUS_IS_OK(status) && (names.count == 1)) {
+                                               &names, &type,
+                                               &result);
+               if (NT_STATUS_IS_OK(status) && NT_STATUS_IS_OK(result) && (names.count == 1)) {
                        strlcpy(p, names.names[0].string, PTR_DIFF(endp,p));
                        p += 21;
                        count++;
@@ -2553,11 +2688,11 @@ static bool api_NetUserGetGroups(connection_struct *conn,uint16 vuid,
        ret = True;
 
  close_user:
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &user_handle);
+       dcerpc_samr_Close(b, talloc_tos(), &user_handle, &result);
  close_domain:
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &domain_handle);
+       dcerpc_samr_Close(b, talloc_tos(), &domain_handle, &result);
  close_sam:
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &samr_handle);
+       dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
 
        return ret;
 }
@@ -2566,7 +2701,8 @@ static bool api_NetUserGetGroups(connection_struct *conn,uint16 vuid,
  Get all users.
 ******************************************************************/
 
-static bool api_RNetUserEnum(connection_struct *conn, uint16 vuid,
+static bool api_RNetUserEnum(struct smbd_server_connection *sconn,
+                            connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2581,13 +2717,15 @@ static bool api_RNetUserEnum(connection_struct *conn, uint16 vuid,
 
        struct rpc_pipe_client *samr_pipe;
        struct policy_handle samr_handle, domain_handle;
-       NTSTATUS status;
+       NTSTATUS status, result;
 
        char *str1 = get_safe_str_ptr(param,tpscnt,param,2);
        char *str2 = skip_string(param,tpscnt,str1);
        char *p = skip_string(param,tpscnt,str2);
        char *endp = NULL;
 
+       struct dcerpc_binding_handle *b;
+
        if (!str1 || !str2 || !p) {
                return False;
        }
@@ -2626,30 +2764,46 @@ static bool api_RNetUserEnum(connection_struct *conn, uint16 vuid,
        p = *rdata;
        endp = *rdata + *rdata_len;
 
-       status = rpc_pipe_open_internal(
-               talloc_tos(), &ndr_table_samr.syntax_id, rpc_samr_dispatch,
-               conn->server_info, &samr_pipe);
+       status = rpc_pipe_open_interface(
+               talloc_tos(), &ndr_table_samr.syntax_id,
+               conn->session_info, conn->sconn->remote_address,
+               conn->sconn->msg_ctx, &samr_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: Could not connect to samr: %s\n",
                          nt_errstr(status)));
                return false;
        }
 
-       status = rpccli_samr_Connect2(samr_pipe, talloc_tos(), global_myname(),
-                                     SAMR_ACCESS_LOOKUP_DOMAIN, &samr_handle);
+       b = samr_pipe->binding_handle;
+
+       status = dcerpc_samr_Connect2(b, talloc_tos(), lp_netbios_name(),
+                                     SAMR_ACCESS_LOOKUP_DOMAIN, &samr_handle,
+                                     &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_Connect2 failed: %s\n",
                          nt_errstr(status)));
                return false;
        }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_Connect2 failed: %s\n",
+                         nt_errstr(result)));
+               return false;
+       }
 
-       status = rpccli_samr_OpenDomain(samr_pipe, talloc_tos(), &samr_handle,
+       status = dcerpc_samr_OpenDomain(b, talloc_tos(), &samr_handle,
                                        SAMR_DOMAIN_ACCESS_ENUM_ACCOUNTS,
-                                       get_global_sam_sid(), &domain_handle);
+                                       get_global_sam_sid(), &domain_handle,
+                                       &result);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("api_RNetUserEnum: samr_OpenDomain failed: %s\n",
                          nt_errstr(status)));
-               rpccli_samr_Close(samr_pipe, talloc_tos(), &samr_handle);
+               dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
+               return false;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(0, ("api_RNetUserEnum: samr_OpenDomain failed: %s\n",
+                         nt_errstr(result)));
+               dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
                return false;
        }
 
@@ -2661,20 +2815,26 @@ static bool api_RNetUserEnum(connection_struct *conn, uint16 vuid,
                struct samr_SamArray *sam_entries;
                uint32_t num_entries;
 
-               status = rpccli_samr_EnumDomainUsers(samr_pipe, talloc_tos(),
+               status = dcerpc_samr_EnumDomainUsers(b, talloc_tos(),
                                                     &domain_handle,
                                                     &resume_handle,
                                                     0, &sam_entries, 1,
-                                                    &num_entries);
+                                                    &num_entries,
+                                                    &result);
 
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(10, ("rpccli_samr_EnumDomainUsers returned "
+                       DEBUG(10, ("dcerpc_samr_EnumDomainUsers returned "
                                   "%s\n", nt_errstr(status)));
                        break;
                }
+               if (!NT_STATUS_IS_OK(result)) {
+                       DEBUG(10, ("dcerpc_samr_EnumDomainUsers returned "
+                                  "%s\n", nt_errstr(result)));
+                       break;
+               }
 
                if (num_entries == 0) {
-                       DEBUG(10, ("rpccli_samr_EnumDomainUsers returned "
+                       DEBUG(10, ("dcerpc_samr_EnumDomainUsers returned "
                                   "no entries -- done\n"));
                        break;
                }
@@ -2707,8 +2867,8 @@ static bool api_RNetUserEnum(connection_struct *conn, uint16 vuid,
                TALLOC_FREE(sam_entries);
        }
 
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &domain_handle);
-       rpccli_samr_Close(samr_pipe, talloc_tos(), &samr_handle);
+       dcerpc_samr_Close(b, talloc_tos(), &domain_handle, &result);
+       dcerpc_samr_Close(b, talloc_tos(), &samr_handle, &result);
 
        *rdata_len = PTR_DIFF(p,*rdata);
 
@@ -2724,7 +2884,8 @@ static bool api_RNetUserEnum(connection_struct *conn, uint16 vuid,
  Get the time of day info.
 ****************************************************************************/
 
-static bool api_NetRemoteTOD(connection_struct *conn,uint16 vuid,
+static bool api_NetRemoteTOD(struct smbd_server_connection *sconn,
+                            connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2756,7 +2917,7 @@ static bool api_NetRemoteTOD(connection_struct *conn,uint16 vuid,
                                            by NT in a "net time" operation,
                                            it seems to ignore the one below */
 
-       /* the client expects to get localtime, not GMT, in this bit 
+       /* the client expects to get localtime, not GMT, in this bit
                (I think, this needs testing) */
        t = localtime(&unixdate);
        if (!t) {
@@ -2782,7 +2943,8 @@ static bool api_NetRemoteTOD(connection_struct *conn,uint16 vuid,
  Set the user password.
 *****************************************************************************/
 
-static bool api_SetUserPassword(connection_struct *conn,uint16 vuid,
+static bool api_SetUserPassword(struct smbd_server_connection *sconn,
+                               connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2793,6 +2955,22 @@ static bool api_SetUserPassword(connection_struct *conn,uint16 vuid,
        char *p = NULL;
        fstring user;
        fstring pass1,pass2;
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       NTSTATUS status, result;
+       struct rpc_pipe_client *cli = NULL;
+       struct policy_handle connect_handle, domain_handle, user_handle;
+       struct lsa_String domain_name;
+       struct dom_sid2 *domain_sid;
+       struct lsa_String names;
+       struct samr_Ids rids;
+       struct samr_Ids types;
+       struct samr_Password old_lm_hash;
+       struct samr_Password new_lm_hash;
+       int errcode = NERR_badpass;
+       uint32_t rid;
+       int encrypted;
+       int min_pwd_length;
+       struct dcerpc_binding_handle *b = NULL;
 
        /* Skip 2 strings. */
        p = skip_string(param,tpscnt,np);
@@ -2825,6 +3003,18 @@ static bool api_SetUserPassword(connection_struct *conn,uint16 vuid,
        memcpy(pass1,p,16);
        memcpy(pass2,p+16,16);
 
+       encrypted = get_safe_SVAL(param,tpscnt,p+32,0,-1);
+       if (encrypted == -1) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
+       min_pwd_length = get_safe_SVAL(param,tpscnt,p+34,0,-1);
+       if (min_pwd_length == -1) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
        *rparam_len = 4;
        *rparam = smb_realloc_limit(*rparam,*rparam_len);
        if (!*rparam) {
@@ -2833,59 +3023,172 @@ static bool api_SetUserPassword(connection_struct *conn,uint16 vuid,
 
        *rdata_len = 0;
 
-       SSVAL(*rparam,0,NERR_badpass);
-       SSVAL(*rparam,2,0);             /* converter word */
+       DEBUG(3,("Set password for <%s> (encrypted: %d, min_pwd_length: %d)\n",
+               user, encrypted, min_pwd_length));
 
-       DEBUG(3,("Set password for <%s>\n",user));
+       ZERO_STRUCT(connect_handle);
+       ZERO_STRUCT(domain_handle);
+       ZERO_STRUCT(user_handle);
 
-       /*
-        * Attempt to verify the old password against smbpasswd entries
-        * Win98 clients send old and new password in plaintext for this call.
-        */
+       status = rpc_pipe_open_interface(mem_ctx, &ndr_table_samr.syntax_id,
+                                       conn->session_info,
+                                       conn->sconn->remote_address,
+                                       conn->sconn->msg_ctx,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_SetUserPassword: could not connect to samr: %s\n",
+                         nt_errstr(status)));
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
 
-       {
-               struct auth_serversupplied_info *server_info = NULL;
-               DATA_BLOB password = data_blob(pass1, strlen(pass1)+1);
+       b = cli->binding_handle;
 
-               if (NT_STATUS_IS_OK(check_plaintext_password(user,password,&server_info))) {
+       status = dcerpc_samr_Connect2(b, mem_ctx,
+                                     lp_netbios_name(),
+                                     SAMR_ACCESS_CONNECT_TO_SERVER |
+                                     SAMR_ACCESS_ENUM_DOMAINS |
+                                     SAMR_ACCESS_LOOKUP_DOMAIN,
+                                     &connect_handle,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
 
-                       become_root();
-                       if (NT_STATUS_IS_OK(change_oem_password(server_info->sam_account, pass1, pass2, False, NULL))) {
-                               SSVAL(*rparam,0,NERR_Success);
-                       }
-                       unbecome_root();
+       init_lsa_String(&domain_name, get_global_sam_name());
 
-                       TALLOC_FREE(server_info);
-               }
-               data_blob_clear_free(&password);
+       status = dcerpc_samr_LookupDomain(b, mem_ctx,
+                                         &connect_handle,
+                                         &domain_name,
+                                         &domain_sid,
+                                         &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
        }
 
-       /*
-        * If the plaintext change failed, attempt
-        * the old encrypted method. NT will generate this
-        * after trying the samr method. Note that this
-        * method is done as a last resort as this
-        * password change method loses the NT password hash
-        * and cannot change the UNIX password as no plaintext
-        * is received.
-        */
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
+                                       &connect_handle,
+                                       SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
+                                       domain_sid,
+                                       &domain_handle,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
 
-       if(SVAL(*rparam,0) != NERR_Success) {
-               struct samu *hnd = NULL;
+       init_lsa_String(&names, user);
 
-               if (check_lanman_password(user,(unsigned char *)pass1,(unsigned char *)pass2, &hnd)) {
-                       become_root();
-                       if (change_lanman_password(hnd,(uchar *)pass2)) {
-                               SSVAL(*rparam,0,NERR_Success);
-                       }
-                       unbecome_root();
-                       TALLOC_FREE(hnd);
-               }
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
+                                        &domain_handle,
+                                        1,
+                                        &names,
+                                        &rids,
+                                        &types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       if (rids.count != 1) {
+               errcode = W_ERROR_V(WERR_NO_SUCH_USER);
+               goto out;
+       }
+       if (rids.count != types.count) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+       if (types.ids[0] != SID_NAME_USER) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
+       rid = rids.ids[0];
+
+       status = dcerpc_samr_OpenUser(b, mem_ctx,
+                                     &domain_handle,
+                                     SAMR_USER_ACCESS_CHANGE_PASSWORD,
+                                     rid,
+                                     &user_handle,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       if (encrypted == 0) {
+               E_deshash(pass1, old_lm_hash.hash);
+               E_deshash(pass2, new_lm_hash.hash);
+       } else {
+               ZERO_STRUCT(old_lm_hash);
+               ZERO_STRUCT(new_lm_hash);
+               memcpy(old_lm_hash.hash, pass1, MIN(strlen(pass1), 16));
+               memcpy(new_lm_hash.hash, pass2, MIN(strlen(pass2), 16));
+       }
+
+       status = dcerpc_samr_ChangePasswordUser(b, mem_ctx,
+                                               &user_handle,
+                                               true, /* lm_present */
+                                               &old_lm_hash,
+                                               &new_lm_hash,
+                                               false, /* nt_present */
+                                               NULL, /* old_nt_crypted */
+                                               NULL, /* new_nt_crypted */
+                                               false, /* cross1_present */
+                                               NULL, /* nt_cross */
+                                               false, /* cross2_present */
+                                               NULL, /* lm_cross */
+                                               &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       errcode = NERR_Success;
+ out:
+
+       if (b && is_valid_policy_hnd(&user_handle)) {
+               dcerpc_samr_Close(b, mem_ctx, &user_handle, &result);
+       }
+       if (b && is_valid_policy_hnd(&domain_handle)) {
+               dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
+       }
+       if (b && is_valid_policy_hnd(&connect_handle)) {
+               dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
        }
 
        memset((char *)pass1,'\0',sizeof(fstring));
-       memset((char *)pass2,'\0',sizeof(fstring));      
+       memset((char *)pass2,'\0',sizeof(fstring));
 
+       SSVAL(*rparam,0,errcode);
+       SSVAL(*rparam,2,0);             /* converter word */
        return(True);
 }
 
@@ -2893,17 +3196,28 @@ static bool api_SetUserPassword(connection_struct *conn,uint16 vuid,
   Set the user password (SamOEM version - gets plaintext).
 ****************************************************************************/
 
-static bool api_SamOEMChangePassword(connection_struct *conn,uint16 vuid,
+static bool api_SamOEMChangePassword(struct smbd_server_connection *sconn,
+                                    connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
                                char **rdata,char **rparam,
                                int *rdata_len,int *rparam_len)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
        fstring user;
        char *p = get_safe_str_ptr(param,tpscnt,param,2);
-       *rparam_len = 2;
+
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       NTSTATUS status, result;
+       struct rpc_pipe_client *cli = NULL;
+       struct lsa_AsciiString server, account;
+       struct samr_CryptPassword password;
+       struct samr_Password hash;
+       int errcode = NERR_badpass;
+       int bufsize;
+       struct dcerpc_binding_handle *b;
+
+       *rparam_len = 4;
        *rparam = smb_realloc_limit(*rparam,*rparam_len);
        if (!*rparam) {
                return False;
@@ -2953,26 +3267,67 @@ static bool api_SamOEMChangePassword(connection_struct *conn,uint16 vuid,
 
        DEBUG(3,("api_SamOEMChangePassword: Change password for <%s>\n",user));
 
-       /*
-        * Pass the user through the NT -> unix user mapping
-        * function.
-        */
+       if (tdscnt != 532) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
+       bufsize = get_safe_SVAL(param,tpscnt,p,0,-1);
+       if (bufsize != 532) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
+       memcpy(password.data, data, 516);
+       memcpy(hash.hash, data+516, 16);
+
+       status = rpc_pipe_open_interface(mem_ctx, &ndr_table_samr.syntax_id,
+                                       conn->session_info,
+                                       conn->sconn->remote_address,
+                                       conn->sconn->msg_ctx,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_SamOEMChangePassword: could not connect to samr: %s\n",
+                         nt_errstr(status)));
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
 
-       (void)map_username(sconn, user);
+       b = cli->binding_handle;
 
-       if (NT_STATUS_IS_OK(pass_oem_change(user, (uchar*) data, (uchar *)&data[516], NULL, NULL, NULL))) {
-               SSVAL(*rparam,0,NERR_Success);
+       init_lsa_AsciiString(&server, lp_netbios_name());
+       init_lsa_AsciiString(&account, user);
+
+       status = dcerpc_samr_OemChangePasswordUser2(b, mem_ctx,
+                                                   &server,
+                                                   &account,
+                                                   &password,
+                                                   &hash,
+                                                   &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
        }
 
+       errcode = NERR_Success;
+ out:
+       SSVAL(*rparam,0,errcode);
+       SSVAL(*rparam,2,0);             /* converter word */
+
        return(True);
 }
 
 /****************************************************************************
   delete a print job
-  Form: <W> <> 
+  Form: <W> <>
   ****************************************************************************/
 
-static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
+static bool api_RDosPrintJobDel(struct smbd_server_connection *sconn,
+                               connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2991,6 +3346,7 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
        TALLOC_CTX *mem_ctx = talloc_tos();
        NTSTATUS status;
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct policy_handle handle;
        struct spoolss_DevmodeContainer devmode_ctr;
        enum spoolss_JobControl command;
@@ -3021,23 +3377,27 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
 
        ZERO_STRUCT(handle);
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_RDosPrintJobDel: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
                                            sharename,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           JOB_ACCESS_ADMINISTER,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3069,7 +3429,7 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
                goto out;
        }
 
-       status = rpccli_spoolss_SetJob(cli, mem_ctx,
+       status = dcerpc_spoolss_SetJob(b, mem_ctx,
                                       &handle,
                                       jobid,
                                       NULL, /* unique ptr ctr */
@@ -3085,11 +3445,11 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
        }
 
  out:
-       if (is_valid_policy_hnd(&handle)) {
-               rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
-       SSVAL(*rparam,0,errcode);       
+       SSVAL(*rparam,0,errcode);
        SSVAL(*rparam,2,0);             /* converter word */
 
        return(True);
@@ -3099,7 +3459,8 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
   Purge a print queue - or pause or resume it.
   ****************************************************************************/
 
-static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
+static bool api_WPrintQueueCtrl(struct smbd_server_connection *sconn,
+                               connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3116,6 +3477,7 @@ static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
 
        TALLOC_CTX *mem_ctx = talloc_tos();
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct policy_handle handle;
        struct spoolss_SetPrinterInfoCtr info_ctr;
        struct spoolss_DevmodeContainer devmode_ctr;
@@ -3143,19 +3505,23 @@ static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
 
        ZERO_STRUCT(handle);
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintQueueCtrl: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
                                            QueueName,
                                            NULL,
                                            devmode_ctr,
@@ -3194,7 +3560,7 @@ static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
        ZERO_STRUCT(info_ctr);
        ZERO_STRUCT(secdesc_ctr);
 
-       status = rpccli_spoolss_SetPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
                                           &handle,
                                           &info_ctr,
                                           &devmode_ctr,
@@ -3214,8 +3580,8 @@ static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
 
  out:
 
-       if (is_valid_policy_hnd(&handle)) {
-               rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
        SSVAL(*rparam,0,errcode);
@@ -3228,8 +3594,8 @@ static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
   set the property of a print job (undocumented?)
   ? function = 0xb -> set name of print job
   ? function = 0x6 -> move print job up/down
-  Form: <WWsTP> <WWzWWDDzzzzzzzzzzlz> 
-  or   <WWsTP> <WB21BB16B10zWWzDDz> 
+  Form: <WWsTP> <WWzWWDDzzzzzzzzzzlz>
+  or   <WWsTP> <WB21BB16B10zWWzDDz>
 ****************************************************************************/
 
 static int check_printjob_info(struct pack_desc* desc,
@@ -3255,7 +3621,8 @@ static int check_printjob_info(struct pack_desc* desc,
        return True;
 }
 
-static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
+static bool api_PrintJobInfo(struct smbd_server_connection *sconn,
+                            connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3276,6 +3643,7 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
        WERROR werr;
        NTSTATUS status;
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct policy_handle handle;
        struct spoolss_DevmodeContainer devmode_ctr;
        struct spoolss_JobInfoContainer ctr;
@@ -3300,25 +3668,46 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
                return False;
        }
 
+       *rdata_len = 0;
+
+       /* check it's a supported varient */
+       if ((strcmp(str1,"WWsTP")) ||
+           (!check_printjob_info(&desc,uLevel,str2)))
+               return(False);
+
+       errcode = NERR_notsupported;
+
+       switch (function) {
+       case 0xb:
+               /* change print job name, data gives the name */
+               break;
+       default:
+               goto out;
+       }
+
        ZERO_STRUCT(handle);
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_PrintJobInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
                                            sharename,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           PRINTER_ACCESS_USE,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3330,13 +3719,6 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
                goto out;
        }
 
-       *rdata_len = 0;
-
-       /* check it's a supported varient */
-       if ((strcmp(str1,"WWsTP")) || 
-           (!check_printjob_info(&desc,uLevel,str2)))
-               return(False);
-
        werr = rpccli_spoolss_getjob(cli, mem_ctx,
                                     &handle,
                                     jobid,
@@ -3348,16 +3730,6 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
                goto out;
        }
 
-       errcode = NERR_notsupported;
-
-       switch (function) {
-       case 0xb:
-               /* change print job name, data gives the name */
-               break;
-       default:
-               goto out;
-       }
-
        ZERO_STRUCT(ctr);
 
        info1.job_id            = info.info1.job_id;
@@ -3376,7 +3748,7 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
        ctr.level = 1;
        ctr.info.info1 = &info1;
 
-       status = rpccli_spoolss_SetJob(cli, mem_ctx,
+       status = dcerpc_spoolss_SetJob(b, mem_ctx,
                                       &handle,
                                       jobid,
                                       &ctr,
@@ -3394,8 +3766,8 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
        errcode = NERR_Success;
  out:
 
-       if (is_valid_policy_hnd(&handle)) {
-               rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
        SSVALS(*rparam,0,errcode);
@@ -3409,7 +3781,8 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
  Get info about the server.
 ****************************************************************************/
 
-static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
+static bool api_RNetServerGetInfo(struct smbd_server_connection *sconn,
+                                 connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3423,6 +3796,14 @@ static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
        char *p2;
        int struct_len;
 
+       NTSTATUS status;
+       WERROR werr;
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       struct rpc_pipe_client *cli = NULL;
+       union srvsvc_NetSrvInfo info;
+       int errcode;
+       struct dcerpc_binding_handle *b;
+
        if (!str1 || !str2 || !p) {
                return False;
        }
@@ -3483,64 +3864,59 @@ static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
 
        p = *rdata;
        p2 = p + struct_len;
+
+       status = rpc_pipe_open_interface(mem_ctx, &ndr_table_srvsvc.syntax_id,
+                                       conn->session_info,
+                                       conn->sconn->remote_address,
+                                       conn->sconn->msg_ctx,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_RNetServerGetInfo: could not connect to srvsvc: %s\n",
+                         nt_errstr(status)));
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       b = cli->binding_handle;
+
+       status = dcerpc_srvsvc_NetSrvGetInfo(b, mem_ctx,
+                                            NULL,
+                                            101,
+                                            &info,
+                                            &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!W_ERROR_IS_OK(werr)) {
+               errcode = W_ERROR_V(werr);
+               goto out;
+       }
+
+       if (info.info101 == NULL) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
        if (uLevel != 20) {
-               srvstr_push(NULL, 0, p,global_myname(),16,
+               srvstr_push(NULL, 0, p, info.info101->server_name, 16,
                        STR_ASCII|STR_UPPER|STR_TERMINATE);
        }
        p += 16;
        if (uLevel > 0) {
-               struct srv_info_struct *servers=NULL;
-               int i,count;
-               char *comment = NULL;
-               TALLOC_CTX *ctx = talloc_tos();
-               uint32 servertype= lp_default_server_announce();
-
-               comment = talloc_strdup(ctx,lp_serverstring());
-               if (!comment) {
-                       return false;
-               }
-
-               if ((count=get_server_info(SV_TYPE_ALL,&servers,lp_workgroup()))>0) {
-                       for (i=0;i<count;i++) {
-                               if (strequal(servers[i].name,global_myname())) {
-                                       servertype = servers[i].type;
-                                       TALLOC_FREE(comment);
-                                       comment = talloc_strdup(ctx,
-                                                       servers[i].comment);
-                                       if (comment) {
-                                               return false;
-                                       }
-                               }
-                       }
-               }
-
-               SAFE_FREE(servers);
-
-               SCVAL(p,0,lp_major_announce_version());
-               SCVAL(p,1,lp_minor_announce_version());
-               SIVAL(p,2,servertype);
+               SCVAL(p,0,info.info101->version_major);
+               SCVAL(p,1,info.info101->version_minor);
+               SIVAL(p,2,info.info101->server_type);
 
                if (mdrcnt == struct_len) {
                        SIVAL(p,6,0);
                } else {
                        SIVAL(p,6,PTR_DIFF(p2,*rdata));
-                       comment = talloc_sub_advanced(
-                               ctx,
-                               lp_servicename(SNUM(conn)),
-                               conn->server_info->unix_name,
-                               conn->connectpath,
-                               conn->server_info->utok.gid,
-                               conn->server_info->sanitized_username,
-                               pdb_get_domain(conn->server_info->sam_account),
-                               comment);
-                       if (comment) {
-                               return false;
-                       }
                        if (mdrcnt - struct_len <= 0) {
                                return false;
                        }
                        push_ascii(p2,
-                               comment,
+                               info.info101->comment,
                                MIN(mdrcnt - struct_len,
                                        MAX_SERVER_STRING_LENGTH),
                                STR_TERMINATE);
@@ -3555,6 +3931,10 @@ static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
                return False;           /* not yet implemented */
        }
 
+       errcode = NERR_Success;
+
+ out:
+
        *rdata_len = PTR_DIFF(p2,*rdata);
 
        *rparam_len = 6;
@@ -3562,7 +3942,7 @@ static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
        if (!*rparam) {
                return False;
        }
-       SSVAL(*rparam,0,NERR_Success);
+       SSVAL(*rparam,0,errcode);
        SSVAL(*rparam,2,0);             /* converter word */
        SSVAL(*rparam,4,*rdata_len);
 
@@ -3573,7 +3953,8 @@ static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
  Get info about the server.
 ****************************************************************************/
 
-static bool api_NetWkstaGetInfo(connection_struct *conn,uint16 vuid,
+static bool api_NetWkstaGetInfo(struct smbd_server_connection *sconn,
+                               connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3631,7 +4012,7 @@ static bool api_NetWkstaGetInfo(connection_struct *conn,uint16 vuid,
        p += 4;
 
        SIVAL(p,0,PTR_DIFF(p2,*rdata));
-       strlcpy(p2,conn->server_info->sanitized_username,PTR_DIFF(endp,p2));
+       strlcpy(p2,conn->session_info->unix_info->sanitized_username,PTR_DIFF(endp,p2));
        p2 = skip_string(*rdata,*rdata_len,p2);
        if (!p2) {
                return False;
@@ -3647,8 +4028,8 @@ static bool api_NetWkstaGetInfo(connection_struct *conn,uint16 vuid,
        }
        p += 4;
 
-       SCVAL(p,0,lp_major_announce_version()); /* system version - e.g 4 in 4.1 */
-       SCVAL(p,1,lp_minor_announce_version()); /* system version - e.g .1 in 4.1 */
+       SCVAL(p,0,SAMBA_MAJOR_NBT_ANNOUNCE_VERSION); /* system version - e.g 4 in 4.1 */
+       SCVAL(p,1,SAMBA_MINOR_NBT_ANNOUNCE_VERSION); /* system version - e.g .1 in 4.1 */
        p += 2;
 
        SIVAL(p,0,PTR_DIFF(p2,*rdata));
@@ -3678,9 +4059,9 @@ static bool api_NetWkstaGetInfo(connection_struct *conn,uint16 vuid,
   get info about a user
 
     struct user_info_11 {
-        char                usri11_name[21];  0-20 
-        char                usri11_pad;       21 
-        char                *usri11_comment;  22-25 
+        char                usri11_name[21];  0-20
+        char                usri11_pad;       21
+        char                *usri11_comment;  22-25
         char            *usri11_usr_comment;  26-29
         unsigned short      usri11_priv;      30-31
         unsigned long       usri11_auth_flags; 32-35
@@ -3811,7 +4192,7 @@ There is no auxiliary data in the response.
 
   ****************************************************************************/
 
-#define usri11_name           0 
+#define usri11_name           0
 #define usri11_pad            21
 #define usri11_comment        22
 #define usri11_usr_comment    26
@@ -3834,24 +4215,14 @@ There is no auxiliary data in the response.
 #define usri11_code_page      84
 #define usri11_end            86
 
-#define USER_PRIV_GUEST 0
-#define USER_PRIV_USER 1
-#define USER_PRIV_ADMIN 2
-
-#define AF_OP_PRINT     0 
-#define AF_OP_COMM      1
-#define AF_OP_SERVER    2
-#define AF_OP_ACCOUNTS  3
-
-
-static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
+static bool api_RNetUserGetInfo(struct smbd_server_connection *sconn,
+                               connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
                                char **rdata,char **rparam,
                                int *rdata_len,int *rparam_len)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
        char *str1 = get_safe_str_ptr(param,tpscnt,param,2);
        char *str2 = skip_string(param,tpscnt,str1);
        char *UserName = skip_string(param,tpscnt,str2);
@@ -3861,15 +4232,19 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
        char *endp;
        const char *level_string;
 
-       /* get NIS home of a previously validated user - simeon */
-       /* With share level security vuid will always be zero.
-          Don't depend on vuser being non-null !!. JRA */
-       user_struct *vuser = get_valid_user_struct(sconn, vuid);
-       if(vuser != NULL) {
-               DEBUG(3,("  Username of UID %d is %s\n",
-                        (int)vuser->server_info->utok.uid,
-                        vuser->server_info->unix_name));
-       }
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       NTSTATUS status, result;
+       struct rpc_pipe_client *cli = NULL;
+       struct policy_handle connect_handle, domain_handle, user_handle;
+       struct lsa_String domain_name;
+       struct dom_sid2 *domain_sid;
+       struct lsa_String names;
+       struct samr_Ids rids;
+       struct samr_Ids types;
+       int errcode = W_ERROR_V(WERR_USER_NOT_FOUND);
+       uint32_t rid;
+       union samr_UserInfo *info;
+       struct dcerpc_binding_handle *b = NULL;
 
        if (!str1 || !str2 || !UserName || !p) {
                return False;
@@ -3906,9 +4281,6 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
                return False;
        }
 
-       SSVAL(*rparam,0,NERR_Success);
-       SSVAL(*rparam,2,0);             /* converter word */
-
        p = *rdata;
        endp = *rdata + *rdata_len;
        p2 = get_safe_ptr(*rdata,*rdata_len,p,usri11_end);
@@ -3916,6 +4288,138 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
                return False;
        }
 
+       ZERO_STRUCT(connect_handle);
+       ZERO_STRUCT(domain_handle);
+       ZERO_STRUCT(user_handle);
+
+       status = rpc_pipe_open_interface(mem_ctx, &ndr_table_samr.syntax_id,
+                                       conn->session_info,
+                                       conn->sconn->remote_address,
+                                       conn->sconn->msg_ctx,
+                                       &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_RNetUserGetInfo: could not connect to samr: %s\n",
+                         nt_errstr(status)));
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       b = cli->binding_handle;
+
+       status = dcerpc_samr_Connect2(b, mem_ctx,
+                                     lp_netbios_name(),
+                                     SAMR_ACCESS_CONNECT_TO_SERVER |
+                                     SAMR_ACCESS_ENUM_DOMAINS |
+                                     SAMR_ACCESS_LOOKUP_DOMAIN,
+                                     &connect_handle,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       init_lsa_String(&domain_name, get_global_sam_name());
+
+       status = dcerpc_samr_LookupDomain(b, mem_ctx,
+                                         &connect_handle,
+                                         &domain_name,
+                                         &domain_sid,
+                                         &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       status = dcerpc_samr_OpenDomain(b, mem_ctx,
+                                       &connect_handle,
+                                       SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
+                                       domain_sid,
+                                       &domain_handle,
+                                       &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       init_lsa_String(&names, UserName);
+
+       status = dcerpc_samr_LookupNames(b, mem_ctx,
+                                        &domain_handle,
+                                        1,
+                                        &names,
+                                        &rids,
+                                        &types,
+                                        &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       if (rids.count != 1) {
+               errcode = W_ERROR_V(WERR_NO_SUCH_USER);
+               goto out;
+       }
+       if (rids.count != types.count) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+       if (types.ids[0] != SID_NAME_USER) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
+
+       rid = rids.ids[0];
+
+       status = dcerpc_samr_OpenUser(b, mem_ctx,
+                                     &domain_handle,
+                                     SAMR_USER_ACCESS_GET_LOCALE |
+                                     SAMR_USER_ACCESS_GET_LOGONINFO |
+                                     SAMR_USER_ACCESS_GET_ATTRIBUTES |
+                                     SAMR_USER_ACCESS_GET_GROUPS |
+                                     SAMR_USER_ACCESS_GET_GROUP_MEMBERSHIP |
+                                     SEC_STD_READ_CONTROL,
+                                     rid,
+                                     &user_handle,
+                                     &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
+       status = dcerpc_samr_QueryUserInfo2(b, mem_ctx,
+                                           &user_handle,
+                                           UserAllInformation,
+                                           &info,
+                                           &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       if (!NT_STATUS_IS_OK(result)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(result));
+               goto out;
+       }
+
        memset(p,0,21);
        fstrcpy(p+usri11_name,UserName); /* 21 bytes - user name */
 
@@ -3941,9 +4445,7 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
 
                /* EEK! the cifsrap.txt doesn't have this in!!!! */
                SIVAL(p,usri11_full_name,PTR_DIFF(p2,p)); /* full name */
-               strlcpy(p2,((vuser != NULL)
-                           ? pdb_get_fullname(vuser->server_info->sam_account)
-                           : UserName),PTR_DIFF(endp,p2));
+               strlcpy(p2,info->info21.full_name.string,PTR_DIFF(endp,p2));
                p2 = skip_string(*rdata,*rdata_len,p2);
                if (!p2) {
                        return False;
@@ -3951,11 +4453,7 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
        }
 
        if (uLevel == 11) {
-               const char *homedir = "";
-               if (vuser != NULL) {
-                       homedir = pdb_get_homedir(
-                               vuser->server_info->sam_account);
-               }
+               const char *homedir = info->info21.home_directory.string;
                /* modelled after NTAS 3.51 reply */
                SSVAL(p,usri11_priv,
                        (get_current_uid(conn) == sec_initial_uid())?
@@ -4015,8 +4513,7 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
                        (get_current_uid(conn) == sec_initial_uid())?
                        USER_PRIV_ADMIN:USER_PRIV_USER);
                SIVAL(p,44,PTR_DIFF(p2,*rdata)); /* home dir */
-               strlcpy(p2, vuser ? pdb_get_homedir(
-                               vuser->server_info->sam_account) : "",
+               strlcpy(p2, info->info21.home_directory.string,
                        PTR_DIFF(endp,p2));
                p2 = skip_string(*rdata,*rdata_len,p2);
                if (!p2) {
@@ -4026,42 +4523,39 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
                *p2++ = 0;
                SSVAL(p,52,0);          /* flags */
                SIVAL(p,54,PTR_DIFF(p2,*rdata));                /* script_path */
-               strlcpy(p2, vuser ? pdb_get_logon_script(
-                               vuser->server_info->sam_account) : "",
+               strlcpy(p2, info->info21.logon_script.string,
                        PTR_DIFF(endp,p2));
                p2 = skip_string(*rdata,*rdata_len,p2);
                if (!p2) {
                        return False;
                }
                if (uLevel == 2) {
-                       SIVAL(p,60,0);          /* auth_flags */
-                       SIVAL(p,64,PTR_DIFF(p2,*rdata)); /* full_name */
-                       strlcpy(p2,((vuser != NULL)
-                                   ? pdb_get_fullname(vuser->server_info->sam_account)
-                                   : UserName),PTR_DIFF(endp,p2));
+                       SIVAL(p,58,0);          /* auth_flags */
+                       SIVAL(p,62,PTR_DIFF(p2,*rdata)); /* full_name */
+                       strlcpy(p2,info->info21.full_name.string,PTR_DIFF(endp,p2));
                        p2 = skip_string(*rdata,*rdata_len,p2);
                        if (!p2) {
                                return False;
                        }
-                       SIVAL(p,68,0);          /* urs_comment */
-                       SIVAL(p,72,PTR_DIFF(p2,*rdata)); /* parms */
+                       SIVAL(p,66,0);          /* urs_comment */
+                       SIVAL(p,70,PTR_DIFF(p2,*rdata)); /* parms */
                        strlcpy(p2,"",PTR_DIFF(endp,p2));
                        p2 = skip_string(*rdata,*rdata_len,p2);
                        if (!p2) {
                                return False;
                        }
-                       SIVAL(p,76,0);          /* workstations */
-                       SIVAL(p,80,0);          /* last_logon */
-                       SIVAL(p,84,0);          /* last_logoff */
-                       SIVALS(p,88,-1);                /* acct_expires */
-                       SIVALS(p,92,-1);                /* max_storage */
-                       SSVAL(p,96,168);        /* units_per_week */
-                       SIVAL(p,98,PTR_DIFF(p2,*rdata)); /* logon_hours */
+                       SIVAL(p,74,0);          /* workstations */
+                       SIVAL(p,78,0);          /* last_logon */
+                       SIVAL(p,82,0);          /* last_logoff */
+                       SIVALS(p,86,-1);                /* acct_expires */
+                       SIVALS(p,90,-1);                /* max_storage */
+                       SSVAL(p,94,168);        /* units_per_week */
+                       SIVAL(p,96,PTR_DIFF(p2,*rdata)); /* logon_hours */
                        memset(p2,-1,21);
                        p2 += 21;
-                       SSVALS(p,102,-1);       /* bad_pw_count */
-                       SSVALS(p,104,-1);       /* num_logons */
-                       SIVAL(p,106,PTR_DIFF(p2,*rdata)); /* logon_server */
+                       SSVALS(p,100,-1);       /* bad_pw_count */
+                       SSVALS(p,102,-1);       /* num_logons */
+                       SIVAL(p,104,PTR_DIFF(p2,*rdata)); /* logon_server */
                        {
                                TALLOC_CTX *ctx = talloc_tos();
                                int space_rem = *rdata_len - (p2 - *rdata);
@@ -4091,26 +4585,41 @@ static bool api_RNetUserGetInfo(connection_struct *conn, uint16 vuid,
                        if (!p2) {
                                return False;
                        }
-                       SSVAL(p,110,49);        /* country_code */
-                       SSVAL(p,112,860);       /* code page */
+                       SSVAL(p,108,49);        /* country_code */
+                       SSVAL(p,110,860);       /* code page */
                }
        }
 
+       errcode = NERR_Success;
+
+ out:
        *rdata_len = PTR_DIFF(p2,*rdata);
 
+       if (b && is_valid_policy_hnd(&user_handle)) {
+               dcerpc_samr_Close(b, mem_ctx, &user_handle, &result);
+       }
+       if (b && is_valid_policy_hnd(&domain_handle)) {
+               dcerpc_samr_Close(b, mem_ctx, &domain_handle, &result);
+       }
+       if (b && is_valid_policy_hnd(&connect_handle)) {
+               dcerpc_samr_Close(b, mem_ctx, &connect_handle, &result);
+       }
+
+       SSVAL(*rparam,0,errcode);
+       SSVAL(*rparam,2,0);             /* converter word */
        SSVAL(*rparam,4,*rdata_len);    /* is this right?? */
 
        return(True);
 }
 
-static bool api_WWkstaUserLogon(connection_struct *conn,uint16 vuid,
+static bool api_WWkstaUserLogon(struct smbd_server_connection *sconn,
+                               connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
                                char **rdata,char **rparam,
                                int *rdata_len,int *rparam_len)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
        char *str1 = get_safe_str_ptr(param,tpscnt,param,2);
        char *str2 = skip_string(param,tpscnt,str1);
        char *p = skip_string(param,tpscnt,str2);
@@ -4119,7 +4628,7 @@ static bool api_WWkstaUserLogon(connection_struct *conn,uint16 vuid,
        char* name;
                /* With share level security vuid will always be zero.
                   Don't depend on vuser being non-null !!. JRA */
-       user_struct *vuser = get_valid_user_struct(sconn, vuid);
+       struct user_struct *vuser = get_valid_user_struct(sconn, vuid);
 
        if (!str1 || !str2 || !p) {
                return False;
@@ -4127,8 +4636,8 @@ static bool api_WWkstaUserLogon(connection_struct *conn,uint16 vuid,
 
        if(vuser != NULL) {
                DEBUG(3,("  Username of UID %d is %s\n",
-                        (int)vuser->server_info->utok.uid,
-                        vuser->server_info->unix_name));
+                        (int)vuser->session_info->unix_token->uid,
+                        vuser->session_info->unix_info->unix_name));
        }
 
        uLevel = get_safe_SVAL(param,tpscnt,p,0,-1);
@@ -4187,8 +4696,9 @@ static bool api_WWkstaUserLogon(connection_struct *conn,uint16 vuid,
                }
 
                PACKS(&desc,"z",lp_workgroup());/* domain */
-               PACKS(&desc,"z", vuser ? pdb_get_logon_script(
-                             vuser->server_info->sam_account) : ""); /* script path */
+               PACKS(&desc,"z", vuser ?
+                     vuser->session_info->info->logon_script
+                       : ""); /* script path */
                PACKI(&desc,"D",0x00000000);            /* reserved */
        }
 
@@ -4211,7 +4721,8 @@ static bool api_WWkstaUserLogon(connection_struct *conn,uint16 vuid,
  api_WAccessGetUserPerms
 ****************************************************************************/
 
-static bool api_WAccessGetUserPerms(connection_struct *conn,uint16 vuid,
+static bool api_WAccessGetUserPerms(struct smbd_server_connection *sconn,
+                                   connection_struct *conn,uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4256,7 +4767,8 @@ static bool api_WAccessGetUserPerms(connection_struct *conn,uint16 vuid,
   api_WPrintJobEnumerate
   ****************************************************************************/
 
-static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
+static bool api_WPrintJobGetInfo(struct smbd_server_connection *sconn,
+                                connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4276,6 +4788,7 @@ static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
        WERROR werr;
        NTSTATUS status;
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct policy_handle handle;
        struct spoolss_DevmodeContainer devmode_ctr;
        union spoolss_JobInfo info;
@@ -4305,23 +4818,27 @@ static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
 
        ZERO_STRUCT(handle);
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintJobGetInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
                                            sharename,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           PRINTER_ACCESS_USE,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4368,8 +4885,8 @@ static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
                *rdata_len = 0;
        }
  out:
-       if (is_valid_policy_hnd(&handle)) {
-               rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
        *rparam_len = 6;
@@ -4388,7 +4905,8 @@ static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
        return True;
 }
 
-static bool api_WPrintJobEnumerate(connection_struct *conn, uint16 vuid,
+static bool api_WPrintJobEnumerate(struct smbd_server_connection *sconn,
+                                  connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4407,9 +4925,10 @@ static bool api_WPrintJobEnumerate(connection_struct *conn, uint16 vuid,
        WERROR werr;
        NTSTATUS status;
        struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
        struct policy_handle handle;
        struct spoolss_DevmodeContainer devmode_ctr;
-       uint32_t count;
+       uint32_t count = 0;
        union spoolss_JobInfo *info;
 
        if (!str1 || !str2 || !p) {
@@ -4435,25 +4954,29 @@ static bool api_WPrintJobEnumerate(connection_struct *conn, uint16 vuid,
                return False;   /* defined only for uLevel 0,1,2 */
        }
 
-       if (!check_printjob_info(&desc,uLevel,str2)) { 
+       if (!check_printjob_info(&desc,uLevel,str2)) {
                return False;
        }
 
        ZERO_STRUCT(handle);
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("api_RDosPrintJobDel: could not connect to spoolss: %s\n",
+               DEBUG(0,("api_WPrintJobEnumerate: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
                desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
                goto out;
        }
+       b = cli->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
                                            name,
                                            NULL,
                                            devmode_ctr,
@@ -4501,8 +5024,8 @@ static bool api_WPrintJobEnumerate(connection_struct *conn, uint16 vuid,
                }
        }
  out:
-       if (is_valid_policy_hnd(&handle)) {
-               rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
        *rdata_len = desc.usedlen;
@@ -4545,19 +5068,19 @@ static int check_printdest_info(struct pack_desc* desc,
                        return False;
        }
        if (id == NULL || strcmp(desc->format,id) != 0) {
-               DEBUG(0,("check_printdest_info: invalid string %s\n", 
+               DEBUG(0,("check_printdest_info: invalid string %s\n",
                        id ? id : "<NULL>" ));
                return False;
        }
        return True;
 }
 
-static void fill_printdest_info(connection_struct *conn, int snum, int uLevel,
+static void fill_printdest_info(struct spoolss_PrinterInfo2 *info2, int uLevel,
                                struct pack_desc* desc)
 {
        char buf[100];
 
-       strncpy(buf,SERVICE(snum),sizeof(buf)-1);
+       strncpy(buf, info2->printername, sizeof(buf)-1);
        buf[sizeof(buf)-1] = 0;
        strupper_m(buf);
 
@@ -4588,7 +5111,8 @@ static void fill_printdest_info(connection_struct *conn, int snum, int uLevel,
        }
 }
 
-static bool api_WPrintDestGetInfo(connection_struct *conn, uint16 vuid,
+static bool api_WPrintDestGetInfo(struct smbd_server_connection *sconn,
+                                 connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4601,9 +5125,17 @@ static bool api_WPrintDestGetInfo(connection_struct *conn, uint16 vuid,
        char* PrinterName = p;
        int uLevel;
        struct pack_desc desc;
-       int snum;
        char *tmpdata=NULL;
 
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       WERROR werr;
+       NTSTATUS status;
+       struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
+       struct policy_handle handle;
+       struct spoolss_DevmodeContainer devmode_ctr;
+       union spoolss_PrinterInfo info;
+
        if (!str1 || !str2 || !p) {
                return False;
        }
@@ -4626,33 +5158,82 @@ static bool api_WPrintDestGetInfo(connection_struct *conn, uint16 vuid,
                return False;
        }
 
-       snum = find_service(PrinterName);
-       if ( !(lp_snum_ok(snum) && lp_print_ok(snum)) ) {
+       ZERO_STRUCT(handle);
+
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_WPrintDestGetInfo: could not connect to spoolss: %s\n",
+                         nt_errstr(status)));
+               desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       b = cli->binding_handle;
+
+       ZERO_STRUCT(devmode_ctr);
+
+       status = dcerpc_spoolss_OpenPrinter(b, mem_ctx,
+                                           PrinterName,
+                                           NULL,
+                                           devmode_ctr,
+                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           &handle,
+                                           &werr);
+       if (!NT_STATUS_IS_OK(status)) {
                *rdata_len = 0;
                desc.errcode = NERR_DestNotFound;
                desc.neededlen = 0;
-       } else {
-               if (mdrcnt > 0) {
-                       *rdata = smb_realloc_limit(*rdata,mdrcnt);
-                       if (!*rdata) {
-                               return False;
-                       }
-                       desc.base = *rdata;
-                       desc.buflen = mdrcnt;
-               } else {
-                       /*
-                        * Don't return data but need to get correct length
-                        * init_package will return wrong size if buflen=0
-                        */
-                       desc.buflen = getlen(desc.format);
-                       desc.base = tmpdata = (char *)SMB_MALLOC( desc.buflen );
-               }
-               if (init_package(&desc,1,0)) {
-                       fill_printdest_info(conn,snum,uLevel,&desc);
+               goto out;
+       }
+       if (!W_ERROR_IS_OK(werr)) {
+               *rdata_len = 0;
+               desc.errcode = NERR_DestNotFound;
+               desc.neededlen = 0;
+               goto out;
+       }
+
+       werr = rpccli_spoolss_getprinter(cli, mem_ctx,
+                                        &handle,
+                                        2,
+                                        0,
+                                        &info);
+       if (!W_ERROR_IS_OK(werr)) {
+               *rdata_len = 0;
+               desc.errcode = NERR_DestNotFound;
+               desc.neededlen = 0;
+               goto out;
+       }
+
+       if (mdrcnt > 0) {
+               *rdata = smb_realloc_limit(*rdata,mdrcnt);
+               if (!*rdata) {
+                       return False;
                }
-               *rdata_len = desc.usedlen;
+               desc.base = *rdata;
+               desc.buflen = mdrcnt;
+       } else {
+               /*
+                * Don't return data but need to get correct length
+                * init_package will return wrong size if buflen=0
+                */
+               desc.buflen = getlen(desc.format);
+               desc.base = tmpdata = (char *)SMB_MALLOC( desc.buflen );
+       }
+       if (init_package(&desc,1,0)) {
+               fill_printdest_info(&info.info2, uLevel,&desc);
+       }
+
+ out:
+       if (b && is_valid_policy_hnd(&handle)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &handle, &werr);
        }
 
+       *rdata_len = desc.usedlen;
+
        *rparam_len = 6;
        *rparam = smb_realloc_limit(*rparam,*rparam_len);
        if (!*rparam) {
@@ -4668,7 +5249,8 @@ static bool api_WPrintDestGetInfo(connection_struct *conn, uint16 vuid,
        return True;
 }
 
-static bool api_WPrintDestEnum(connection_struct *conn, uint16 vuid,
+static bool api_WPrintDestEnum(struct smbd_server_connection *sconn,
+                              connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4682,7 +5264,13 @@ static bool api_WPrintDestEnum(connection_struct *conn, uint16 vuid,
        int queuecnt;
        int i, n, succnt=0;
        struct pack_desc desc;
-       int services = lp_numservices();
+
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       WERROR werr;
+       NTSTATUS status;
+       struct rpc_pipe_client *cli = NULL;
+       union spoolss_PrinterInfo *info;
+       uint32_t count;
 
        if (!str1 || !str2 || !p) {
                return False;
@@ -4703,12 +5291,37 @@ static bool api_WPrintDestEnum(connection_struct *conn, uint16 vuid,
        }
 
        queuecnt = 0;
-       for (i = 0; i < services; i++) {
-               if (lp_snum_ok(i) && lp_print_ok(i) && lp_browseable(i)) {
-                       queuecnt++;
-               }
+
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_spoolss.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("api_WPrintDestEnum: could not connect to spoolss: %s\n",
+                         nt_errstr(status)));
+               desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
        }
 
+       werr = rpccli_spoolss_enumprinters(cli, mem_ctx,
+                                          PRINTER_ENUM_LOCAL,
+                                          cli->srv_name_slash,
+                                          2,
+                                          0,
+                                          &count,
+                                          &info);
+       if (!W_ERROR_IS_OK(werr)) {
+               desc.errcode = W_ERROR_V(werr);
+               *rdata_len = 0;
+               desc.errcode = NERR_DestNotFound;
+               desc.neededlen = 0;
+               goto out;
+       }
+
+       queuecnt = count;
+
        if (mdrcnt > 0) {
                *rdata = smb_realloc_limit(*rdata,mdrcnt);
                if (!*rdata) {
@@ -4718,20 +5331,18 @@ static bool api_WPrintDestEnum(connection_struct *conn, uint16 vuid,
 
        desc.base = *rdata;
        desc.buflen = mdrcnt;
-       if (init_package(&desc,queuecnt,0)) {    
+       if (init_package(&desc,queuecnt,0)) {
                succnt = 0;
                n = 0;
-               for (i = 0; i < services; i++) {
-                       if (lp_snum_ok(i) && lp_print_ok(i) && lp_browseable(i)) {
-                               fill_printdest_info(conn,i,uLevel,&desc);
-                               n++;
-                               if (desc.errcode == NERR_Success) {
-                                       succnt = n;
-                               }
+               for (i = 0; i < count; i++) {
+                       fill_printdest_info(&info[i].info2, uLevel,&desc);
+                       n++;
+                       if (desc.errcode == NERR_Success) {
+                               succnt = n;
                        }
                }
        }
-
+ out:
        *rdata_len = desc.usedlen;
 
        *rparam_len = 8;
@@ -4749,7 +5360,8 @@ static bool api_WPrintDestEnum(connection_struct *conn, uint16 vuid,
        return True;
 }
 
-static bool api_WPrintDriverEnum(connection_struct *conn, uint16 vuid,
+static bool api_WPrintDriverEnum(struct smbd_server_connection *sconn,
+                                connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4812,7 +5424,8 @@ static bool api_WPrintDriverEnum(connection_struct *conn, uint16 vuid,
        return True;
 }
 
-static bool api_WPrintQProcEnum(connection_struct *conn, uint16 vuid,
+static bool api_WPrintQProcEnum(struct smbd_server_connection *sconn,
+                               connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4875,7 +5488,8 @@ static bool api_WPrintQProcEnum(connection_struct *conn, uint16 vuid,
        return True;
 }
 
-static bool api_WPrintPortEnum(connection_struct *conn, uint16 vuid,
+static bool api_WPrintPortEnum(struct smbd_server_connection *sconn,
+                              connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4944,7 +5558,8 @@ static bool api_WPrintPortEnum(connection_struct *conn, uint16 vuid,
  List open sessions
  ****************************************************************************/
 
-static bool api_RNetSessionEnum(connection_struct *conn, uint16 vuid,
+static bool api_RNetSessionEnum(struct smbd_server_connection *sconn,
+                               connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4957,14 +5572,22 @@ static bool api_RNetSessionEnum(connection_struct *conn, uint16 vuid,
        char *p = skip_string(param,tpscnt,str2);
        int uLevel;
        struct pack_desc desc;
-       struct sessionid *session_list;
-       int i, num_sessions;
+       int i;
+
+       TALLOC_CTX *mem_ctx = talloc_tos();
+       WERROR werr;
+       NTSTATUS status;
+       struct rpc_pipe_client *cli = NULL;
+       struct dcerpc_binding_handle *b = NULL;
+       struct srvsvc_NetSessInfoCtr info_ctr;
+       uint32_t totalentries, resume_handle = 0;
+       uint32_t count = 0;
 
        if (!str1 || !str2 || !p) {
                return False;
        }
 
-       memset((char *)&desc,'\0',sizeof(desc));
+       ZERO_STRUCT(desc);
 
        uLevel = get_safe_SVAL(param,tpscnt,p,0,-1);
 
@@ -4980,27 +5603,72 @@ static bool api_RNetSessionEnum(connection_struct *conn, uint16 vuid,
                return False;
        }
 
-       num_sessions = list_sessions(talloc_tos(), &session_list);
+       status = rpc_pipe_open_interface(conn,
+                                        &ndr_table_srvsvc.syntax_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
+                                        conn->sconn->msg_ctx,
+                                        &cli);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("RNetSessionEnum: could not connect to srvsvc: %s\n",
+                         nt_errstr(status)));
+               desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+       b = cli->binding_handle;
+
+       info_ctr.level = 1;
+       info_ctr.ctr.ctr1 = talloc_zero(talloc_tos(), struct srvsvc_NetSessCtr1);
+       if (info_ctr.ctr.ctr1 == NULL) {
+               desc.errcode = W_ERROR_V(WERR_NOMEM);
+               goto out;
+       }
+
+       status = dcerpc_srvsvc_NetSessEnum(b, mem_ctx,
+                                          cli->srv_name_slash,
+                                          NULL, /* client */
+                                          NULL, /* user */
+                                          &info_ctr,
+                                          (uint32_t)-1, /* max_buffer */
+                                          &totalentries,
+                                          &resume_handle,
+                                          &werr);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("RNetSessionEnum: dcerpc_srvsvc_NetSessEnum failed: %s\n",
+                         nt_errstr(status)));
+               desc.errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
 
+       if (!W_ERROR_IS_OK(werr)) {
+               DEBUG(0,("RNetSessionEnum: dcerpc_srvsvc_NetSessEnum failed: %s\n",
+                         win_errstr(werr)));
+               desc.errcode = W_ERROR_V(werr);
+               goto out;
+       }
+
+       count = info_ctr.ctr.ctr1->count;
+
+ out:
        if (mdrcnt > 0) {
                *rdata = smb_realloc_limit(*rdata,mdrcnt);
                if (!*rdata) {
                        return False;
                }
        }
-       memset((char *)&desc,'\0',sizeof(desc));
+
        desc.base = *rdata;
        desc.buflen = mdrcnt;
        desc.format = str2;
-       if (!init_package(&desc,num_sessions,0)) {
+       if (!init_package(&desc, count,0)) {
                return False;
        }
 
-       for(i=0; i<num_sessions; i++) {
-               PACKS(&desc, "z", session_list[i].remote_machine);
-               PACKS(&desc, "z", session_list[i].username);
+       for(i=0; i < count; i++) {
+               PACKS(&desc, "z", info_ctr.ctr.ctr1->array[i].client);
+               PACKS(&desc, "z", info_ctr.ctr.ctr1->array[i].user);
                PACKI(&desc, "W", 1); /* num conns */
-               PACKI(&desc, "W", 0); /* num opens */
+               PACKI(&desc, "W", info_ctr.ctr.ctr1->array[i].num_open);
                PACKI(&desc, "W", 1); /* num users */
                PACKI(&desc, "D", 0); /* session time */
                PACKI(&desc, "D", 0); /* idle time */
@@ -5017,7 +5685,7 @@ static bool api_RNetSessionEnum(connection_struct *conn, uint16 vuid,
        }
        SSVALS(*rparam,0,desc.errcode);
        SSVAL(*rparam,2,0); /* converter */
-       SSVAL(*rparam,4,num_sessions); /* count */
+       SSVAL(*rparam,4, count); /* count */
 
        DEBUG(4,("RNetSessionEnum: errorcode %d\n",desc.errcode));
 
@@ -5029,7 +5697,8 @@ static bool api_RNetSessionEnum(connection_struct *conn, uint16 vuid,
  The buffer was too small.
  ****************************************************************************/
 
-static bool api_TooSmall(connection_struct *conn,uint16 vuid, char *param, char *data,
+static bool api_TooSmall(struct smbd_server_connection *sconn,
+                        connection_struct *conn,uint64_t vuid, char *param, char *data,
                         int mdrcnt, int mprcnt,
                         char **rdata, char **rparam,
                         int *rdata_len, int *rparam_len)
@@ -5053,7 +5722,8 @@ static bool api_TooSmall(connection_struct *conn,uint16 vuid, char *param, char
  The request is not supported.
  ****************************************************************************/
 
-static bool api_Unsupported(connection_struct *conn, uint16 vuid,
+static bool api_Unsupported(struct smbd_server_connection *sconn,
+                           connection_struct *conn, uint64_t vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt,
@@ -5079,7 +5749,8 @@ static bool api_Unsupported(connection_struct *conn, uint16 vuid,
 static const struct {
        const char *name;
        int id;
-       bool (*fn)(connection_struct *, uint16,
+       bool (*fn)(struct smbd_server_connection *sconn,
+                  connection_struct *, uint64_t,
                        char *, int,
                        char *, int,
                        int,int,char **,char **,int *,int *);
@@ -5122,7 +5793,7 @@ static const struct {
        {NULL,          -1,     api_Unsupported}
        /*  The following RAP calls are not implemented by Samba:
 
-       RAP_WFileEnum2 - anon not OK 
+       RAP_WFileEnum2 - anon not OK
        */
 };
 
@@ -5131,13 +5802,12 @@ static const struct {
  Handle remote api calls.
 ****************************************************************************/
 
-void api_reply(connection_struct *conn, uint16 vuid,
+void api_reply(connection_struct *conn, uint64_t vuid,
               struct smb_request *req,
               char *data, char *params,
               int tdscnt, int tpscnt,
               int mdrcnt, int mprcnt)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
        int api_command;
        char *rdata = NULL;
        char *rparam = NULL;
@@ -5186,9 +5856,9 @@ void api_reply(connection_struct *conn, uint16 vuid,
        /* Check whether this api call can be done anonymously */
 
        if (api_commands[i].auth_user && lp_restrict_anonymous()) {
-               user_struct *user = get_valid_user_struct(sconn, vuid);
+               struct user_struct *user = get_valid_user_struct(req->sconn, vuid);
 
-               if (!user || user->server_info->guest) {
+               if (!user || security_session_user_level(user->session_info, NULL) < SECURITY_USER) {
                        reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        return;
                }
@@ -5212,7 +5882,7 @@ void api_reply(connection_struct *conn, uint16 vuid,
                return;
        }
 
-       reply = api_commands[i].fn(conn,
+       reply = api_commands[i].fn(req->sconn, conn,
                                vuid,
                                params,tpscnt,  /* params + length */
                                data,tdscnt,    /* data + length */
@@ -5221,13 +5891,16 @@ void api_reply(connection_struct *conn, uint16 vuid,
 
 
        if (rdata_len > mdrcnt || rparam_len > mprcnt) {
-               reply = api_TooSmall(conn,vuid,params,data,mdrcnt,mprcnt,
+               reply = api_TooSmall(req->sconn,conn,vuid,params,data,
+                                    mdrcnt,mprcnt,
                                        &rdata,&rparam,&rdata_len,&rparam_len);
        }
 
        /* if we get False back then it's actually unsupported */
        if (!reply) {
-               reply = api_Unsupported(conn,vuid,params,tpscnt,data,tdscnt,mdrcnt,mprcnt,
+               reply = api_Unsupported(req->sconn,conn,vuid,params,tpscnt,
+                                       data,
+                                       tdscnt,mdrcnt,mprcnt,
                        &rdata,&rparam,&rdata_len,&rparam_len);
        }