s3-lanman: Fix return code for "" printqname in api_DosPrintQGetInfo().
[kamenim/samba.git] / source3 / smbd / lanman.c
index 954c9c84badb284961b48f759dc7a3dcb06ddc96..72d488ecabf870aec550869b5ddbb6c847e76c0b 100644 (file)
 #include "smbd/globals.h"
 #include "../librpc/gen_ndr/cli_samr.h"
 #include "../librpc/gen_ndr/cli_spoolss.h"
+#include "rpc_client/cli_spoolss.h"
+#include "rpc_client/init_spoolss.h"
+#include "../librpc/gen_ndr/cli_srvsvc.h"
 #include "../librpc/gen_ndr/srv_samr.h"
-#include "../librpc/gen_ndr/srv_spoolss.h"
+#include "../librpc/gen_ndr/srv_srvsvc.h"
 #include "../librpc/gen_ndr/rap.h"
 #include "../lib/util/binsearch.h"
+#include "../libcli/auth/libcli_auth.h"
 
 #ifdef CHECK_TYPES
 #undef CHECK_TYPES
@@ -68,14 +72,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, uint16 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, uint16 vuid, char *param, char *data,
                         int mdrcnt, int mprcnt,
                         char **rdata, char **rparam,
                         int *rdata_len, int *rparam_len);
@@ -109,7 +115,7 @@ static int CopyExpanded(connection_struct *conn,
                                conn->connectpath,
                                conn->server_info->utok.gid,
                                conn->server_info->sanitized_username,
-                               pdb_get_domain(conn->server_info->sam_account),
+                               conn->server_info->info3->base.domain.string,
                                buf);
        if (!buf) {
                *p_space_remaining = 0;
@@ -160,7 +166,7 @@ static int StrlenExpanded(connection_struct *conn, int snum, char *s)
                                conn->connectpath,
                                conn->server_info->utok.gid,
                                conn->server_info->sanitized_username,
-                               pdb_get_domain(conn->server_info->sam_account),
+                               conn->server_info->info3->base.domain.string,
                                buf);
        if (!buf) {
                return 0;
@@ -531,21 +537,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,
@@ -758,7 +749,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -827,9 +819,12 @@ 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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_DosPrintQGetInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -841,9 +836,9 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
 
        status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
                                            QueueName,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           PRINTER_ACCESS_USE,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -936,7 +931,7 @@ static bool api_DosPrintQGetInfo(connection_struct *conn, uint16 vuid,
                desc.errcode = ERRbuftoosmall;
 
  out:
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -962,7 +957,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt,
@@ -1018,9 +1014,7 @@ 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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_DosPrintQEnum: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -1071,15 +1065,21 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
 
                uint32_t num_jobs;
                struct policy_handle handle;
+               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,
+                                                   printername,
+                                                   "RAW",
                                                    devmode_ctr,
-                                                   SEC_FLAG_MAXIMUM_ALLOWED,
+                                                   PRINTER_ACCESS_USE,
                                                    &handle,
                                                    &werr);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1127,7 +1127,7 @@ static bool api_DosPrintQEnum(connection_struct *conn, uint16 vuid,
                subcntarr[i] = num_jobs;
                subcnt += subcntarr[i];
 
-               if (is_valid_policy_hnd(&handle)) {
+               if (cli && is_valid_policy_hnd(&handle)) {
                        rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
                }
        }
@@ -1417,7 +1417,8 @@ 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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt, char **rdata, 
@@ -1586,7 +1587,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt, char **rdata,
@@ -1778,7 +1780,8 @@ 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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt, char **rdata, 
@@ -1968,7 +1971,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2031,7 +2035,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int                mdrcnt,
@@ -2138,7 +2143,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2152,12 +2158,17 @@ 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;
+
        if (!str1 || !str2 || !p) {
                return False;
        }
@@ -2178,11 +2189,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;
@@ -2196,7 +2202,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 ? */
@@ -2209,7 +2215,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 ? */
@@ -2228,34 +2234,40 @@ 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_internal(mem_ctx, &ndr_table_srvsvc.syntax_id,
+                                       rpc_srvsvc_dispatch, conn->server_info,
+                                       &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;
-       }
+       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;
 
-       DEBUG(10,("api_RNetShareAdd: Running [%s]\n", command ));
+       info.info2 = &info2;
 
-       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);
+       status = rpccli_srvsvc_NetShareAdd(cli, 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;
@@ -2270,7 +2282,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);
@@ -2287,7 +2299,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2444,7 +2457,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2603,7 +2617,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2761,7 +2776,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2819,7 +2835,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -2830,6 +2847,21 @@ 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;
+       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;
 
        /* Skip 2 strings. */
        p = skip_string(param,tpscnt,np);
@@ -2862,6 +2894,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) {
@@ -2870,59 +2914,138 @@ 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_internal(mem_ctx, &ndr_table_samr.syntax_id,
+                                       rpc_samr_dispatch, conn->server_info,
+                                       &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);
+       status = rpccli_samr_Connect2(cli, mem_ctx,
+                                     global_myname(),
+                                     SAMR_ACCESS_CONNECT_TO_SERVER |
+                                     SAMR_ACCESS_ENUM_DOMAINS |
+                                     SAMR_ACCESS_LOOKUP_DOMAIN,
+                                     &connect_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
 
-               if (NT_STATUS_IS_OK(check_plaintext_password(user,password,&server_info))) {
+       init_lsa_String(&domain_name, get_global_sam_name());
 
-                       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();
+       status = rpccli_samr_LookupDomain(cli, mem_ctx,
+                                         &connect_handle,
+                                         &domain_name,
+                                         &domain_sid);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
 
-                       TALLOC_FREE(server_info);
-               }
-               data_blob_clear_free(&password);
+       status = rpccli_samr_OpenDomain(cli, mem_ctx,
+                                       &connect_handle,
+                                       SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
+                                       domain_sid,
+                                       &domain_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               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.
-        */
+       init_lsa_String(&names, user);
 
-       if(SVAL(*rparam,0) != NERR_Success) {
-               struct samu *hnd = NULL;
+       status = rpccli_samr_LookupNames(cli, mem_ctx,
+                                        &domain_handle,
+                                        1,
+                                        &names,
+                                        &rids,
+                                        &types);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
 
-               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);
-               }
+       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 = rpccli_samr_OpenUser(cli, mem_ctx,
+                                     &domain_handle,
+                                     SAMR_USER_ACCESS_CHANGE_PASSWORD,
+                                     rid,
+                                     &user_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               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, pass1, MIN(strlen(pass2), 16));
+       }
+
+       status = rpccli_samr_ChangePasswordUser(cli, 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 */
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       errcode = NERR_Success;
+ out:
+
+       if (cli && is_valid_policy_hnd(&user_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &user_handle);
+       }
+       if (cli && is_valid_policy_hnd(&domain_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &domain_handle);
+       }
+       if (cli && is_valid_policy_hnd(&connect_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &connect_handle);
        }
 
        memset((char *)pass1,'\0',sizeof(fstring));
        memset((char *)pass2,'\0',sizeof(fstring));      
 
+       SSVAL(*rparam,0,errcode);
+       SSVAL(*rparam,2,0);             /* converter word */
        return(True);
 }
 
@@ -2930,17 +3053,27 @@ 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,uint16 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;
+       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;
+
+       *rparam_len = 4;
        *rparam = smb_realloc_limit(*rparam,*rparam_len);
        if (!*rparam) {
                return False;
@@ -2990,17 +3123,48 @@ 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;
+       }
 
-       (void)map_username(sconn, user);
+       bufsize = get_safe_SVAL(param,tpscnt,p,0,-1);
+       if (bufsize != 532) {
+               errcode = W_ERROR_V(WERR_INVALID_PARAM);
+               goto out;
+       }
 
-       if (NT_STATUS_IS_OK(pass_oem_change(user, (uchar*) data, (uchar *)&data[516], NULL, NULL, NULL))) {
-               SSVAL(*rparam,0,NERR_Success);
+       memcpy(password.data, data, 516);
+       memcpy(hash.hash, data+516, 16);
+
+       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_samr.syntax_id,
+                                       rpc_samr_dispatch, conn->server_info,
+                                       &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;
        }
 
+       init_lsa_AsciiString(&server, global_myname());
+       init_lsa_AsciiString(&account, user);
+
+       status = rpccli_samr_OemChangePasswordUser2(cli, mem_ctx,
+                                                   &server,
+                                                   &account,
+                                                   &password,
+                                                   &hash);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       errcode = NERR_Success;
+ out:
+       SSVAL(*rparam,0,errcode);
+       SSVAL(*rparam,2,0);             /* converter word */
+
        return(True);
 }
 
@@ -3009,7 +3173,8 @@ static bool api_SamOEMChangePassword(connection_struct *conn,uint16 vuid,
   Form: <W> <> 
   ****************************************************************************/
 
-static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
+static bool api_RDosPrintJobDel(struct smbd_server_connection *sconn,
+                               connection_struct *conn,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3058,9 +3223,7 @@ 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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_RDosPrintJobDel: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -3072,9 +3235,9 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
 
        status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
                                            sharename,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           JOB_ACCESS_ADMINISTER,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3122,7 +3285,7 @@ static bool api_RDosPrintJobDel(connection_struct *conn,uint16 vuid,
        }
 
  out:
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -3136,7 +3299,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3180,9 +3344,7 @@ 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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintQueueCtrl: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -3251,7 +3413,7 @@ static bool api_WPrintQueueCtrl(connection_struct *conn,uint16 vuid,
 
  out:
 
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -3292,7 +3454,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3356,9 +3519,7 @@ static bool api_PrintJobInfo(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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_PrintJobInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -3370,9 +3531,9 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
 
        status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
                                            sharename,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           PRINTER_ACCESS_USE,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -3431,7 +3592,7 @@ static bool api_PrintJobInfo(connection_struct *conn, uint16 vuid,
        errcode = NERR_Success;
  out:
 
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -3446,7 +3607,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3460,6 +3622,13 @@ 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;
+
        if (!str1 || !str2 || !p) {
                return False;
        }
@@ -3520,64 +3689,55 @@ static bool api_RNetServerGetInfo(connection_struct *conn,uint16 vuid,
 
        p = *rdata;
        p2 = p + struct_len;
+
+       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_srvsvc.syntax_id,
+                                       rpc_srvsvc_dispatch, conn->server_info,
+                                       &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;
+       }
+
+       status = rpccli_srvsvc_NetSrvGetInfo(cli, 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);
@@ -3592,6 +3752,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;
@@ -3599,7 +3763,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);
 
@@ -3610,7 +3774,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -3871,24 +4036,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, uint16 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);
@@ -3898,15 +4053,18 @@ 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;
+       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;
 
        if (!str1 || !str2 || !UserName || !p) {
                return False;
@@ -3943,9 +4101,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);
@@ -3953,6 +4108,104 @@ 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_internal(mem_ctx, &ndr_table_samr.syntax_id,
+                                       rpc_samr_dispatch, conn->server_info,
+                                       &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;
+       }
+
+       status = rpccli_samr_Connect2(cli, mem_ctx,
+                                     global_myname(),
+                                     SAMR_ACCESS_CONNECT_TO_SERVER |
+                                     SAMR_ACCESS_ENUM_DOMAINS |
+                                     SAMR_ACCESS_LOOKUP_DOMAIN,
+                                     &connect_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       init_lsa_String(&domain_name, get_global_sam_name());
+
+       status = rpccli_samr_LookupDomain(cli, mem_ctx,
+                                         &connect_handle,
+                                         &domain_name,
+                                         &domain_sid);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       status = rpccli_samr_OpenDomain(cli, mem_ctx,
+                                       &connect_handle,
+                                       SAMR_DOMAIN_ACCESS_OPEN_ACCOUNT,
+                                       domain_sid,
+                                       &domain_handle);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       init_lsa_String(&names, UserName);
+
+       status = rpccli_samr_LookupNames(cli, mem_ctx,
+                                        &domain_handle,
+                                        1,
+                                        &names,
+                                        &rids,
+                                        &types);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               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 = rpccli_samr_OpenUser(cli, 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);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
+       status = rpccli_samr_QueryUserInfo2(cli, mem_ctx,
+                                           &user_handle,
+                                           UserAllInformation,
+                                           &info);
+       if (!NT_STATUS_IS_OK(status)) {
+               errcode = W_ERROR_V(ntstatus_to_werror(status));
+               goto out;
+       }
+
        memset(p,0,21);
        fstrcpy(p+usri11_name,UserName); /* 21 bytes - user name */
 
@@ -3978,9 +4231,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;
@@ -3988,11 +4239,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())?
@@ -4052,8 +4299,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) {
@@ -4063,42 +4309,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);
@@ -4128,26 +4371,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 (cli && is_valid_policy_hnd(&user_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &user_handle);
+       }
+       if (cli && is_valid_policy_hnd(&domain_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &domain_handle);
+       }
+       if (cli && is_valid_policy_hnd(&connect_handle)) {
+               rpccli_samr_Close(cli, mem_ctx, &connect_handle);
+       }
+
+       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,uint16 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);
@@ -4224,8 +4482,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->server_info->info3->base.logon_script.string
+                       : ""); /* script path */
                PACKI(&desc,"D",0x00000000);            /* reserved */
        }
 
@@ -4248,7 +4507,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,uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4293,7 +4553,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4342,9 +4603,7 @@ 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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintJobGetInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -4356,9 +4615,9 @@ static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
 
        status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
                                            sharename,
-                                           NULL,
+                                           "RAW",
                                            devmode_ctr,
-                                           SEC_FLAG_MAXIMUM_ALLOWED,
+                                           PRINTER_ACCESS_USE,
                                            &handle,
                                            &werr);
        if (!NT_STATUS_IS_OK(status)) {
@@ -4405,7 +4664,7 @@ static bool api_WPrintJobGetInfo(connection_struct *conn, uint16 vuid,
                *rdata_len = 0;
        }
  out:
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -4425,7 +4684,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4478,9 +4738,7 @@ static bool api_WPrintJobEnumerate(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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintJobEnumerate: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -4538,7 +4796,7 @@ static bool api_WPrintJobEnumerate(connection_struct *conn, uint16 vuid,
                }
        }
  out:
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -4625,7 +4883,8 @@ static void fill_printdest_info(struct spoolss_PrinterInfo2 *info2, int uLevel,
        }
 }
 
-static bool api_WPrintDestGetInfo(connection_struct *conn, uint16 vuid,
+static bool api_WPrintDestGetInfo(struct smbd_server_connection *sconn,
+                                 connection_struct *conn, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4672,9 +4931,7 @@ static bool api_WPrintDestGetInfo(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_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintDestGetInfo: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -4736,7 +4993,7 @@ static bool api_WPrintDestGetInfo(connection_struct *conn, uint16 vuid,
        }
 
  out:
-       if (is_valid_policy_hnd(&handle)) {
+       if (cli && is_valid_policy_hnd(&handle)) {
                rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
        }
 
@@ -4757,7 +5014,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4799,9 +5057,7 @@ static bool api_WPrintDestEnum(connection_struct *conn, uint16 vuid,
 
        queuecnt = 0;
 
-       status = rpc_pipe_open_internal(mem_ctx, &ndr_table_spoolss.syntax_id,
-                                       rpc_spoolss_dispatch, conn->server_info,
-                                       &cli);
+       status = rpc_connect_spoolss_pipe(conn, &cli);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0,("api_WPrintDestEnum: could not connect to spoolss: %s\n",
                          nt_errstr(status)));
@@ -4864,7 +5120,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4927,7 +5184,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -4990,7 +5248,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -5059,7 +5318,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt,int mprcnt,
@@ -5144,7 +5404,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,uint16 vuid, char *param, char *data,
                         int mdrcnt, int mprcnt,
                         char **rdata, char **rparam,
                         int *rdata_len, int *rparam_len)
@@ -5168,7 +5429,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, uint16 vuid,
                                char *param, int tpscnt,
                                char *data, int tdscnt,
                                int mdrcnt, int mprcnt,
@@ -5194,7 +5456,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 *, uint16,
                        char *, int,
                        char *, int,
                        int,int,char **,char **,int *,int *);
@@ -5252,7 +5515,6 @@ void api_reply(connection_struct *conn, uint16 vuid,
               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;
@@ -5301,7 +5563,7 @@ 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);
+               user_struct *user = get_valid_user_struct(req->sconn, vuid);
 
                if (!user || user->server_info->guest) {
                        reply_nterror(req, NT_STATUS_ACCESS_DENIED);
@@ -5327,7 +5589,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 */
@@ -5336,13 +5598,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);
        }