s3-libsmb: rename cli_query_secdesc() to cli_query_secdesc_old()
[kai/samba.git] / source3 / utils / net_rpc_printer.c
index be926864a7bddc116feed265900a6d44ef842d3a..f5019af346039c735a58865ad5e28c0a33e0ef49 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include "includes.h"
+#include "system/filesys.h"
 #include "utils/net.h"
-#include "../librpc/gen_ndr/cli_spoolss.h"
-#include "registry.h"
+#include "rpc_client/rpc_client.h"
+#include "../librpc/gen_ndr/ndr_spoolss_c.h"
+#include "rpc_client/cli_spoolss.h"
+#include "rpc_client/init_spoolss.h"
+#include "nt_printing.h"
+#include "registry/reg_objects.h"
+#include "../libcli/security/security.h"
+#include "../libcli/registry/util_reg.h"
+#include "libsmb/libsmb.h"
 
 /* support itanium as well */
 static const struct print_architecture_table_node archi_table[]= {
@@ -62,7 +70,7 @@ static void display_print_driver3(struct spoolss_DriverInfo3 *r)
        printf(_("\tConfigfile: [%s]\n\n"), r->config_file);
        printf(_("\tHelpfile: [%s]\n\n"), r->help_file);
 
-       for (i=0; r->dependent_files[i] != NULL; i++) {
+       for (i=0; r->dependent_files && r->dependent_files[i] != NULL; i++) {
                printf(_("\tDependentfiles: [%s]\n"), r->dependent_files[i]);
        }
 
@@ -72,44 +80,44 @@ static void display_print_driver3(struct spoolss_DriverInfo3 *r)
        printf(_("\tDefaultdatatype: [%s]\n\n"), r->default_datatype);
 }
 
-static void display_reg_value(const char *subkey, struct regval_blob value)
+static void display_reg_value(const char *subkey, struct regval_blob *value)
 {
        const char *text;
        DATA_BLOB blob;
 
-       switch(value.type) {
+       switch(regval_type(value)) {
        case REG_DWORD:
                d_printf(_("\t[%s:%s]: REG_DWORD: 0x%08x\n"), subkey,
-                       value.valuename, *((uint32_t *) value.data_p));
+                       regval_name(value), *((uint32_t *) regval_data_p(value)));
                break;
 
        case REG_SZ:
-               blob = data_blob_const(value.data_p, value.size);
-               pull_reg_sz(talloc_tos(), NULL, &blob, &text);
+               blob = data_blob_const(regval_data_p(value), regval_size(value));
+               pull_reg_sz(talloc_tos(), &blob, &text);
                if (!text) {
                        break;
                }
-               d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, value.valuename,
+               d_printf(_("\t[%s:%s]: REG_SZ: %s\n"), subkey, regval_name(value),
                         text);
                break;
 
        case REG_BINARY:
                d_printf(_("\t[%s:%s]: REG_BINARY: unknown length value not "
                           "displayed\n"),
-                        subkey, value.valuename);
+                        subkey, regval_name(value));
                break;
 
        case REG_MULTI_SZ: {
                uint32_t i;
                const char **values;
-               blob = data_blob_const(value.data_p, value.size);
+               blob = data_blob_const(regval_data_p(value), regval_size(value));
 
-               if (!pull_reg_multi_sz(NULL, NULL, &blob, &values)) {
+               if (!pull_reg_multi_sz(NULL, &blob, &values)) {
                        d_printf("pull_reg_multi_sz failed\n");
                        break;
                }
 
-               printf("%s: REG_MULTI_SZ: \n", value.valuename);
+               printf("%s: REG_MULTI_SZ: \n", regval_name(value));
                for (i=0; values[i] != NULL; i++) {
                        d_printf("%s\n", values[i]);
                }
@@ -118,8 +126,8 @@ static void display_reg_value(const char *subkey, struct regval_blob value)
        }
 
        default:
-               d_printf(_("\t%s: unknown type %d\n"), value.valuename,
-                        value.type);
+               d_printf(_("\t%s: unknown type %d\n"), regval_name(value),
+                        regval_type(value));
        }
 
 }
@@ -150,14 +158,13 @@ NTSTATUS net_copy_fileattr(struct net_context *c,
                  bool copy_acls, bool copy_attrs,
                  bool copy_timestamps, bool is_file)
 {
-       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS nt_status;
        uint16_t fnum_src = 0;
        uint16_t fnum_dst = 0;
-       SEC_DESC *sd = NULL;
+       struct security_descriptor *sd = NULL;
        uint16_t attr;
        time_t f_atime, f_ctime, f_mtime;
 
-
        if (!copy_timestamps && !copy_acls && !copy_attrs)
                return NT_STATUS_OK;
 
@@ -166,19 +173,19 @@ NTSTATUS net_copy_fileattr(struct net_context *c,
        DEBUGADD(3,("opening %s %s on originating server\n",
                is_file?"file":"dir", src_name));
 
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_src, src_name, 0, READ_CONTROL_ACCESS, 0,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_src))) {
+       nt_status = cli_ntcreate(cli_share_src, src_name, 0,
+                                READ_CONTROL_ACCESS, 0,
+                                FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
+                                0x0, 0x0, &fnum_src);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUGADD(0,("cannot open %s %s on originating server %s\n",
-                       is_file?"file":"dir", src_name, cli_errstr(cli_share_src)));
-               nt_status = cli_nt_error(cli_share_src);
+                       is_file?"file":"dir", src_name, nt_errstr(nt_status)));
                goto out;
        }
 
-
        if (copy_acls) {
-
                /* get the security descriptor */
-               sd = cli_query_secdesc(cli_share_src, fnum_src, mem_ctx);
+               sd = cli_query_secdesc_old(cli_share_src, fnum_src, mem_ctx);
                if (!sd) {
                        DEBUG(0,("failed to get security descriptor: %s\n",
                                cli_errstr(cli_share_src)));
@@ -190,79 +197,74 @@ NTSTATUS net_copy_fileattr(struct net_context *c,
                        display_sec_desc(sd);
        }
 
-
        if (copy_attrs || copy_timestamps) {
 
                /* get file attributes */
-               if (!NT_STATUS_IS_OK(cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
-                                &f_ctime, &f_atime, &f_mtime))) {
+               nt_status = cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
+                                     &f_ctime, &f_atime, &f_mtime);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        DEBUG(0,("failed to get file-attrs: %s\n",
-                               cli_errstr(cli_share_src)));
-                       nt_status = cli_nt_error(cli_share_src);
+                               nt_errstr(nt_status)));
                        goto out;
                }
        }
 
-
        /* open the file/dir on the destination server */
-
-       if (!NT_STATUS_IS_OK(cli_ntcreate(cli_share_dst, dst_name, 0, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0, &fnum_dst))) {
+       nt_status = cli_ntcreate(cli_share_dst, dst_name, 0,
+                                WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS, 0,
+                                FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN,
+                                0x0, 0x0, &fnum_dst);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUG(0,("failed to open %s on the destination server: %s: %s\n",
-                       is_file?"file":"dir", dst_name, cli_errstr(cli_share_dst)));
-               nt_status = cli_nt_error(cli_share_dst);
+                       is_file?"file":"dir", dst_name, nt_errstr(nt_status)));
                goto out;
        }
 
        if (copy_timestamps) {
-
                /* set timestamps */
-               if (!NT_STATUS_IS_OK(cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime))) {
+               nt_status = cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
-                               cli_errstr(cli_share_dst)));
-                       nt_status = cli_nt_error(cli_share_dst);
+                               nt_errstr(nt_status)));
                        goto out;
                }
        }
 
        if (copy_acls) {
-
                /* set acls */
-               if (!cli_set_secdesc(cli_share_dst, fnum_dst, sd)) {
-                       DEBUG(0,("could not set secdesc on %s: %s\n",
-                               dst_name, cli_errstr(cli_share_dst)));
-                       nt_status = cli_nt_error(cli_share_dst);
+               nt_status = cli_set_secdesc(cli_share_dst, fnum_dst, sd);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       DEBUG(0, ("could not set secdesc on %s: %s\n",
+                                 dst_name, nt_errstr(nt_status)));
                        goto out;
                }
        }
 
        if (copy_attrs) {
-
                /* set attrs */
-               if (!NT_STATUS_IS_OK(cli_setatr(cli_share_dst, dst_name, attr, 0))) {
+               nt_status = cli_setatr(cli_share_dst, dst_name, attr, 0);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        DEBUG(0,("failed to set file-attrs: %s\n",
-                               cli_errstr(cli_share_dst)));
-                       nt_status = cli_nt_error(cli_share_dst);
+                               nt_errstr(nt_status)));
                        goto out;
                }
        }
 
 
        /* closing files */
-
-       if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
+       nt_status = cli_close(cli_share_src, fnum_src);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                d_fprintf(stderr,
                        _("could not close %s on originating server: %s\n"),
-                       is_file?"file":"dir", cli_errstr(cli_share_src));
-               nt_status = cli_nt_error(cli_share_src);
+                       is_file?"file":"dir", nt_errstr(nt_status));
                goto out;
        }
 
-       if (!NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
+       nt_status = cli_close(cli_share_dst, fnum_dst);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                d_fprintf(stderr,
                        _("could not close %s on destination server: %s\n"),
-                       is_file?"file":"dir", cli_errstr(cli_share_dst));
-               nt_status = cli_nt_error(cli_share_dst);
+                       is_file?"file":"dir", nt_errstr(nt_status));
                goto out;
        }
 
@@ -333,7 +335,7 @@ NTSTATUS net_copy_file(struct net_context *c,
        if (!NT_STATUS_IS_OK(nt_status)) {
                DEBUGADD(0,("cannot open %s %s on originating server %s\n",
                        is_file ? "file":"dir",
-                       src_name, cli_errstr(cli_share_src)));
+                       src_name, nt_errstr(nt_status)));
                goto out;
        }
 
@@ -347,7 +349,7 @@ NTSTATUS net_copy_file(struct net_context *c,
 
                if (!NT_STATUS_IS_OK(nt_status)) {
                        DEBUGADD(1,("cannot create file %s on destination server: %s\n", 
-                               dst_name, cli_errstr(cli_share_dst)));
+                               dst_name, nt_errstr(nt_status)));
                        goto out;
                }
 
@@ -366,8 +368,10 @@ NTSTATUS net_copy_file(struct net_context *c,
 
                d_printf(_("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
                           "%s ACLs and %s DOS Attributes %s\n"),
-                       cli_share_src->desthost, cli_share_src->share, src_name,
-                       cli_share_dst->desthost, cli_share_dst->share, dst_name,
+                       cli_state_remote_name(cli_share_src),
+                       cli_share_src->share, src_name,
+                       cli_state_remote_name(cli_share_dst),
+                       cli_share_dst->share, dst_name,
                        copy_acls ?  _("with") : _("without"),
                        copy_attrs ? _("with") : _("without"),
                        copy_timestamps ? _("(preserving timestamps)") : "" );
@@ -377,20 +381,31 @@ NTSTATUS net_copy_file(struct net_context *c,
        while (is_file) {
 
                /* copying file */
-               int n, ret;
-               n = cli_read(cli_share_src, fnum_src, data, nread,
-                               read_size);
+               size_t n;
 
-               if (n <= 0)
+               nt_status = cli_read(cli_share_src, fnum_src, data, nread,
+                                    read_size, &n);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       d_fprintf(stderr,
+                                 _("Error reading file [\\\\%s\%s%s]: %s\n"),
+                                 cli_state_remote_name(cli_share_src),
+                                 cli_share_src->share,
+                                 src_name, nt_errstr(nt_status));
+                       goto out;
+               }
+
+               if (n == 0)
                        break;
 
-               ret = cli_write(cli_share_dst, fnum_dst, 0, data,
-                       nread, n);
+               nt_status = cli_writeall(cli_share_dst, fnum_dst, 0,
+                                        (uint8_t *)data, nread, n, NULL);
 
-               if (n != ret) {
-                       d_fprintf(stderr, _("Error writing file: %s\n"),
-                               cli_errstr(cli_share_dst));
-                       nt_status = cli_nt_error(cli_share_dst);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       d_fprintf(stderr,
+                                 _("Error writing file: [\\\\%s\%s%s]: %s\n"),
+                                 cli_state_remote_name(cli_share_dst),
+                                 cli_share_dst->share,
+                                 dst_name, nt_errstr(nt_status));
                        goto out;
                }
 
@@ -404,36 +419,41 @@ NTSTATUS net_copy_file(struct net_context *c,
                DEBUGADD(3,("creating dir %s on the destination server\n",
                        dst_name));
 
-               if (!NT_STATUS_IS_OK(cli_mkdir(cli_share_dst, dst_name))) {
+               nt_status = cli_mkdir(cli_share_dst, dst_name);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        DEBUG(0,("cannot create directory %s: %s\n",
-                               dst_name, cli_errstr(cli_share_dst)));
+                               dst_name, nt_errstr(nt_status)));
                        nt_status = NT_STATUS_NO_SUCH_FILE;
                }
 
-               if (!NT_STATUS_IS_OK(cli_chkpath(cli_share_dst, dst_name))) {
+
+               nt_status = cli_chkpath(cli_share_dst, dst_name);
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        d_fprintf(stderr,
                                _("cannot check for directory %s: %s\n"),
-                               dst_name, cli_errstr(cli_share_dst));
+                               dst_name, nt_errstr(nt_status));
                        goto out;
                }
        }
 
 
        /* closing files */
-       if (!NT_STATUS_IS_OK(cli_close(cli_share_src, fnum_src))) {
+       nt_status = cli_close(cli_share_src, fnum_src);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                d_fprintf(stderr,
                        _("could not close file on originating server: %s\n"),
-                       cli_errstr(cli_share_src));
-               nt_status = cli_nt_error(cli_share_src);
+                       nt_errstr(nt_status));
                goto out;
        }
 
-       if (is_file && !NT_STATUS_IS_OK(cli_close(cli_share_dst, fnum_dst))) {
-               d_fprintf(stderr,
+       if (is_file) {
+               nt_status = cli_close(cli_share_dst, fnum_dst);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       d_fprintf(stderr,
                        _("could not close file on destination server: %s\n"),
-                       cli_errstr(cli_share_dst));
-               nt_status = cli_nt_error(cli_share_dst);
-               goto out;
+                       nt_errstr(nt_status));
+                       goto out;
+               }
        }
 
        /* possibly we have to copy some file-attributes / acls / sd */
@@ -487,12 +507,11 @@ static NTSTATUS net_copy_driverfile(struct net_context *c,
                                    struct cli_state *cli_share_dst,
                                    const char *file, const char *short_archi) {
 
-       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        const char *p;
        char *src_name;
        char *dst_name;
-       char *version;
-       char *filename;
+       char *version = NULL;
+       char *filename = NULL;
        char *tok;
 
        if (!file) {
@@ -509,29 +528,27 @@ static NTSTATUS net_copy_driverfile(struct net_context *c,
                }
        }
 
+       if (version == NULL || filename == NULL) {
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
        /* build source file name */
-       if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
+       src_name = talloc_asprintf(mem_ctx, "\\%s\\%s\\%s",
+                                  short_archi, version, filename);
+       if (src_name == NULL) {
                return NT_STATUS_NO_MEMORY;
-
+       }
 
        /* create destination file name */
-       if (asprintf(&dst_name, "\\%s\\%s", short_archi, filename) < 0 )
-                return NT_STATUS_NO_MEMORY;
+       dst_name = talloc_asprintf(mem_ctx, "\\%s\\%s", short_archi, filename);
+       if (dst_name == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
 
        /* finally copy the file */
-       nt_status = net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
-                                 src_name, dst_name, false, false, false, true);
-       if (!NT_STATUS_IS_OK(nt_status))
-               goto out;
-
-       nt_status = NT_STATUS_OK;
-
-out:
-       SAFE_FREE(src_name);
-       SAFE_FREE(dst_name);
-
-       return nt_status;
+       return net_copy_file(c, mem_ctx, cli_share_src, cli_share_dst,
+                            src_name, dst_name, false, false, false, true);
 }
 
 /**
@@ -556,15 +573,16 @@ static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_ar
        DEBUG(10,("creating print-driver dir for architecture: %s\n",
                short_archi));
 
-       if (!NT_STATUS_IS_OK(cli_mkdir(cli_share, dir))) {
+       nt_status = cli_mkdir(cli_share, dir);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                 DEBUG(1,("cannot create directory %s: %s\n",
-                         dir, cli_errstr(cli_share)));
-                nt_status = NT_STATUS_NO_SUCH_FILE;
+                         dir, nt_errstr(nt_status)));
         }
 
-       if (!NT_STATUS_IS_OK(cli_chkpath(cli_share, dir))) {
+       nt_status = cli_chkpath(cli_share, dir);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                d_fprintf(stderr, _("cannot check %s: %s\n"),
-                       dir, cli_errstr(cli_share));
+                       dir, nt_errstr(nt_status));
                goto out;
        }
 
@@ -752,9 +770,11 @@ static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
                                uint32_t level,
                                union spoolss_PrinterInfo *info)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        WERROR result;
        NTSTATUS status;
        struct spoolss_SetPrinterInfoCtr info_ctr;
+       struct spoolss_SetPrinterInfo2 info2;
        struct spoolss_DevmodeContainer devmode_ctr;
        struct sec_desc_buf secdesc_ctr;
 
@@ -774,8 +794,8 @@ static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
                        (void *)&info->info1;
                break;
        case 2:
-               info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
-                       (void *)&info->info2;
+               spoolss_printerinfo2_to_setprinterinfo2(&info->info2, &info2);
+               info_ctr.info.info2 = &info2;
                break;
        case 3:
                info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)
@@ -811,14 +831,17 @@ static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
                break; /* FIXME */
        }
 
-       status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
+       status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
                                           hnd,
                                           &info_ctr,
                                           &devmode_ctr,
                                           &secdesc_ctr,
                                           0, /* command */
                                           &result);
-
+       if (!NT_STATUS_IS_OK(status)) {
+               printf(_("cannot set printer-info: %s\n"), nt_errstr(status));
+               return false;
+       }
        if (!W_ERROR_IS_OK(result)) {
                printf(_("cannot set printer-info: %s\n"), win_errstr(result));
                return false;
@@ -836,18 +859,23 @@ static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
                                       uint8_t *data,
                                       uint32_t offered)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        WERROR result;
        NTSTATUS status;
 
        /* setprinterdata call */
-       status = rpccli_spoolss_SetPrinterData(pipe_hnd, mem_ctx,
+       status = dcerpc_spoolss_SetPrinterData(b, mem_ctx,
                                               hnd,
                                               value_name,
                                               type,
                                               data,
                                               offered,
                                               &result);
-
+       if (!NT_STATUS_IS_OK(status)) {
+               printf (_("unable to set printerdata: %s\n"),
+                       nt_errstr(status));
+               return false;
+       }
        if (!W_ERROR_IS_OK(result)) {
                printf (_("unable to set printerdata: %s\n"),
                        win_errstr(result));
@@ -910,19 +938,24 @@ static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
                                        const char *keyname,
                                        struct regval_blob *value)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        WERROR result;
        NTSTATUS status;
 
        /* setprinterdataex call */
-       status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
+       status = dcerpc_spoolss_SetPrinterDataEx(b, mem_ctx,
                                                 hnd,
                                                 keyname,
-                                                value->valuename,
-                                                value->type,
-                                                value->data_p,
-                                                value->size,
+                                                regval_name(value),
+                                                regval_type(value),
+                                                regval_data_p(value),
+                                                regval_size(value),
                                                 &result);
-
+       if (!NT_STATUS_IS_OK(status)) {
+               printf(_("could not set printerdataex: %s\n"),
+                      nt_errstr(status));
+               return false;
+       }
        if (!W_ERROR_IS_OK(result)) {
                printf(_("could not set printerdataex: %s\n"),
                       win_errstr(result));
@@ -973,8 +1006,14 @@ static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
                                                   count,
                                                   info);
        if (!W_ERROR_IS_OK(result)) {
-               printf(_("cannot enum drivers: %s\n"), win_errstr(result));
-               return false;
+               if (W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
+                       printf(_("cannot enum drivers for environment %s: %s\n"), env,
+                               win_errstr(result));
+                       return false;
+               } else {
+                       printf(_("Server does not support environment [%s]\n"),
+                               env);
+               }
        }
 
        return true;
@@ -1020,6 +1059,7 @@ static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
                             TALLOC_CTX *mem_ctx, uint32_t level,
                             union spoolss_DriverInfo *info)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        WERROR result;
        NTSTATUS status;
        struct spoolss_AddDriverInfoCtr info_ctr;
@@ -1041,10 +1081,14 @@ static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
        }
 
        /* addprinterdriver call */
-       status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
+       status = dcerpc_spoolss_AddPrinterDriver(b, mem_ctx,
                                                 pipe_hnd->srv_name_slash,
                                                 &info_ctr,
                                                 &result);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf(_("cannot add driver: %s\n"), nt_errstr(status));
+               return false;
+       }
        /* be more verbose */
        if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
                printf(_("You are not allowed to add drivers\n"));
@@ -1071,7 +1115,9 @@ static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
                        uint32_t *num_printers,
                        union spoolss_PrinterInfo **info_p)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        struct policy_handle hnd;
+       WERROR werr;
 
        /* no arguments given, enumerate all printers */
        if (argc == 0) {
@@ -1091,12 +1137,17 @@ static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
                                         &hnd))
                return false;
 
+       *info_p = talloc_zero(mem_ctx, union spoolss_PrinterInfo);
+       if (*info_p == NULL) {
+               return false;
+       }
+
        if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, *info_p)) {
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
                return false;
        }
 
-       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+       dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
 
        *num_printers = 1;
 
@@ -1125,7 +1176,7 @@ out:
  **/
 
 NTSTATUS rpc_printer_list_internals(struct net_context *c,
-                                       const DOM_SID *domain_sid,
+                                       const struct dom_sid *domain_sid,
                                        const char *domain_name,
                                        struct cli_state *cli,
                                        struct rpc_pipe_client *pipe_hnd,
@@ -1177,7 +1228,7 @@ NTSTATUS rpc_printer_list_internals(struct net_context *c,
  **/
 
 NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1248,6 +1299,7 @@ static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_
                                        const char **argv,
                                        uint32_t action)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32_t i, num_printers;
        uint32_t level = 7;
@@ -1295,7 +1347,7 @@ static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_
                        break;
                default:
                        action_str = N_("unknown action");
-                       printf(_("unkown action: %d\n"), action);
+                       printf(_("unknown action: %d\n"), action);
                        break;
                }
 
@@ -1307,17 +1359,26 @@ static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_
                ZERO_STRUCT(devmode_ctr);
                ZERO_STRUCT(secdesc_ctr);
 
-               nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
+               nt_status = dcerpc_spoolss_SetPrinter(b, mem_ctx,
                                                      &hnd,
                                                      &info_ctr,
                                                      &devmode_ctr,
                                                      &secdesc_ctr,
                                                      0, /* command */
                                                      &result);
-
-               if (!W_ERROR_IS_OK(result) && (W_ERROR_V(result) != W_ERROR_V(WERR_IO_PENDING))) {
+               if (!NT_STATUS_IS_OK(nt_status)) {
                        printf(_("cannot set printer-info: %s\n"),
-                              win_errstr(result));
+                              nt_errstr(nt_status));
+                       goto done;
+               }
+               if (!W_ERROR_IS_OK(result) && !W_ERROR_EQUAL(result, WERR_IO_PENDING)) {
+                       if ((action == DSPRINT_UPDATE) && W_ERROR_EQUAL(result, W_ERROR(0x80070002))) {
+                               printf(_("printer not published yet\n"));
+                       } else {
+                               printf(_("cannot set printer-info: %s\n"),
+                                      win_errstr(result));
+                       }
+                       nt_status = werror_to_ntstatus(result);
                        goto done;
                }
 
@@ -1328,14 +1389,15 @@ static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_
        nt_status = NT_STATUS_OK;
 
 done:
-       if (is_valid_policy_hnd(&hnd))
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+       if (is_valid_policy_hnd(&hnd)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &result);
+       }
 
        return nt_status;
 }
 
 NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1347,7 +1409,7 @@ NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
 }
 
 NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1359,7 +1421,7 @@ NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
 }
 
 NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1388,7 +1450,7 @@ NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
  **/
 
 NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1396,6 +1458,7 @@ NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
+       struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32_t i, num_printers;
        uint32_t level = 7;
@@ -1404,6 +1467,7 @@ NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
        union spoolss_PrinterInfo info;
        struct policy_handle hnd;
        int state;
+       WERROR werr;
 
        if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum))
                return nt_status;
@@ -1448,7 +1512,7 @@ NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
                                       sharename);
                                break;
                        default:
-                               printf(_("unkown state: %d\n"), state);
+                               printf(_("unknown state: %d\n"), state);
                                break;
                }
        }
@@ -1456,8 +1520,9 @@ NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
        nt_status = NT_STATUS_OK;
 
 done:
-       if (is_valid_policy_hnd(&hnd))
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+       if (is_valid_policy_hnd(&hnd)) {
+               dcerpc_spoolss_ClosePrinter(b, mem_ctx, &hnd, &werr);
+       }
 
        return nt_status;
 }
@@ -1480,7 +1545,7 @@ done:
  **/
 
 NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1488,6 +1553,7 @@ NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
+       struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
        /* TODO: what now, info2 or info3 ?
           convince jerry that we should add clientside setacls level 3 at least
        */
@@ -1497,19 +1563,22 @@ NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
        uint32_t level = 2;
        const char *printername, *sharename;
        struct rpc_pipe_client *pipe_hnd_dst = NULL;
+       struct dcerpc_binding_handle *b_dst = NULL;
        struct policy_handle hnd_src, hnd_dst;
        union spoolss_PrinterInfo *info_enum;
        struct cli_state *cli_dst = NULL;
        union spoolss_PrinterInfo info_src, info_dst;
+       WERROR werr;
 
        DEBUG(3,("copying printer ACLs\n"));
 
        /* connect destination PI_SPOOLSS */
        nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
                                     &ndr_table_spoolss.syntax_id);
-       if (!NT_STATUS_IS_OK(nt_status))
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
-
+       }
+       b_dst = pipe_hnd_dst->binding_handle;
 
        /* enum source printers */
        if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
@@ -1583,11 +1652,11 @@ NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
 
                /* close printer handles here */
                if (is_valid_policy_hnd(&hnd_src)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
                }
 
                if (is_valid_policy_hnd(&hnd_dst)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
                }
 
        }
@@ -1597,11 +1666,11 @@ NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
 done:
 
        if (is_valid_policy_hnd(&hnd_src)) {
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+               dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
        }
 
        if (is_valid_policy_hnd(&hnd_dst)) {
-               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+               dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
        }
 
        if (cli_dst) {
@@ -1628,7 +1697,7 @@ done:
  **/
 
 NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1636,6 +1705,7 @@ NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
+       struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        WERROR result;
        uint32_t i, f;
@@ -1643,6 +1713,7 @@ NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
        uint32_t level = 1;
        const char *printername, *sharename;
        struct rpc_pipe_client *pipe_hnd_dst = NULL;
+       struct dcerpc_binding_handle *b_dst = NULL;
        struct policy_handle hnd_src, hnd_dst;
        union spoolss_PrinterInfo *info_enum;
        union spoolss_PrinterInfo info_dst;
@@ -1655,8 +1726,10 @@ NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
        /* connect destination PI_SPOOLSS */
        nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
                                     &ndr_table_spoolss.syntax_id);
-       if (!NT_STATUS_IS_OK(nt_status))
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
+       }
+       b_dst = pipe_hnd_dst->binding_handle;
 
        /* enum src printers */
        if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_enum)) {
@@ -1731,11 +1804,16 @@ NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
 
                        /* FIXME: there might be something wrong with samba's
                           builtin-forms */
-                       status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
+                       status = dcerpc_spoolss_AddForm(b_dst, mem_ctx,
                                                        &hnd_dst,
                                                        1,
                                                        info,
                                                        &result);
+                       if (!NT_STATUS_IS_OK(status)) {
+                               d_printf(_("\tdcerpc_spoolss_AddForm form %d: [%s] - %s\n"),
+                                       f, forms[f].info1.form_name, nt_errstr(status));
+                               continue;
+                       }
                        if (!W_ERROR_IS_OK(result)) {
                                d_printf(_("\tAddForm form %d: [%s] refused.\n"),
                                        f, forms[f].info1.form_name);
@@ -1749,11 +1827,11 @@ NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
 
                /* close printer handles here */
                if (is_valid_policy_hnd(&hnd_src)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
                }
 
                if (is_valid_policy_hnd(&hnd_dst)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
                }
        }
 
@@ -1761,11 +1839,13 @@ NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
 
 done:
 
-       if (is_valid_policy_hnd(&hnd_src))
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+       if (is_valid_policy_hnd(&hnd_src)) {
+               dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
+       }
 
-       if (is_valid_policy_hnd(&hnd_dst))
-               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+       if (is_valid_policy_hnd(&hnd_dst)) {
+               dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
+       }
 
        if (cli_dst) {
                cli_shutdown(cli_dst);
@@ -1791,7 +1871,7 @@ done:
  **/
 
 NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -1799,6 +1879,7 @@ NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
+       struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32_t i, p;
        uint32_t num_printers;
@@ -1807,6 +1888,7 @@ NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
        bool got_src_driver_share = false;
        bool got_dst_driver_share = false;
        struct rpc_pipe_client *pipe_hnd_dst = NULL;
+       struct dcerpc_binding_handle *b_dst = NULL;
        struct policy_handle hnd_src, hnd_dst;
        union spoolss_DriverInfo drv_info_src;
        union spoolss_PrinterInfo *info_enum;
@@ -1815,17 +1897,22 @@ NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
        struct cli_state *cli_share_src = NULL;
        struct cli_state *cli_share_dst = NULL;
        const char *drivername = NULL;
+       WERROR werr;
 
        DEBUG(3,("copying printer-drivers\n"));
 
        nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
                                     &ndr_table_spoolss.syntax_id);
-       if (!NT_STATUS_IS_OK(nt_status))
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
+       }
+       b_dst = pipe_hnd_dst->binding_handle;
 
        /* open print$-share on the src server */
-       nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
-                       cli->desthost, "print$", "A:");
+       nt_status = connect_to_service(c, &cli_share_src,
+                                      cli_state_remote_sockaddr(cli),
+                                      cli_state_remote_name(cli),
+                                      "print$", "A:");
        if (!NT_STATUS_IS_OK(nt_status))
                goto done;
 
@@ -1833,8 +1920,10 @@ NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
 
 
        /* open print$-share on the dst server */
-       nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
-                       cli_dst->desthost, "print$", "A:");
+       nt_status = connect_to_service(c, &cli_share_dst,
+                                      cli_state_remote_sockaddr(cli_dst),
+                                      cli_state_remote_name(cli_dst),
+                                      "print$", "A:");
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
 
@@ -1950,12 +2039,12 @@ NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
 
                /* close dst */
                if (is_valid_policy_hnd(&hnd_dst)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
                }
 
                /* close src */
                if (is_valid_policy_hnd(&hnd_src)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
                }
        }
 
@@ -1963,11 +2052,14 @@ NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
 
 done:
 
-       if (is_valid_policy_hnd(&hnd_src))
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+       if (is_valid_policy_hnd(&hnd_dst)) {
+               dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &werr);
+       }
 
-       if (is_valid_policy_hnd(&hnd_dst))
-               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+       /* close src */
+       if (is_valid_policy_hnd(&hnd_src)) {
+               dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &werr);
+       }
 
        if (cli_dst) {
                cli_shutdown(cli_dst);
@@ -2002,7 +2094,7 @@ done:
  **/
 
 NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -2010,6 +2102,7 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
+       struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
        WERROR result;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32_t i = 0, num_printers;
@@ -2020,6 +2113,7 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
        struct policy_handle hnd_dst, hnd_src;
        const char *printername, *sharename;
        struct rpc_pipe_client *pipe_hnd_dst = NULL;
+       struct dcerpc_binding_handle *b_dst = NULL;
        struct spoolss_SetPrinterInfoCtr info_ctr;
 
        DEBUG(3,("copying printers\n"));
@@ -2027,8 +2121,10 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
        /* connect destination PI_SPOOLSS */
        nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
                                     &ndr_table_spoolss.syntax_id);
-       if (!NT_STATUS_IS_OK(nt_status))
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
+       }
+       b_dst = pipe_hnd_dst->binding_handle;
 
        /* enum printers */
        if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
@@ -2045,6 +2141,8 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
        /* do something for all printers */
        for (i = 0; i < num_printers; i++) {
 
+               struct spoolss_SetPrinterInfo2 info2;
+
                /* do some initialization */
                printername = info_enum[i].info2.printername;
                sharename = info_enum[i].info2.sharename;
@@ -2074,7 +2172,7 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
                        DEBUG(1,("printer already exists: %s\n", sharename));
                        /* close printer handle here - dst only, not got src yet. */
                        if (is_valid_policy_hnd(&hnd_dst)) {
-                               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+                               dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
                        }
                        continue;
                }
@@ -2096,8 +2194,8 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
                d_printf(_("creating printer: %s\n"), printername);
 
                info_ctr.level = level;
-               info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)
-                       (void *)&info_src.info2;
+               spoolss_printerinfo2_to_setprinterinfo2(&info_src.info2, &info2);
+               info_ctr.info.info2 = &info2;
 
                result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
                                                     mem_ctx,
@@ -2117,22 +2215,24 @@ NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
 
                /* close printer handles here */
                if (is_valid_policy_hnd(&hnd_src)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
                }
 
                if (is_valid_policy_hnd(&hnd_dst)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
                }
        }
 
        nt_status = NT_STATUS_OK;
 
 done:
-       if (is_valid_policy_hnd(&hnd_src))
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+       if (is_valid_policy_hnd(&hnd_src)) {
+               dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
+       }
 
-       if (is_valid_policy_hnd(&hnd_dst))
-               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+       if (is_valid_policy_hnd(&hnd_dst)) {
+               dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
+       }
 
        if (cli_dst) {
                cli_shutdown(cli_dst);
@@ -2159,7 +2259,7 @@ done:
  **/
 
 NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
-                                               const DOM_SID *domain_sid,
+                                               const struct dom_sid *domain_sid,
                                                const char *domain_name,
                                                struct cli_state *cli,
                                                struct rpc_pipe_client *pipe_hnd,
@@ -2167,16 +2267,18 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
                                                int argc,
                                                const char **argv)
 {
+       struct dcerpc_binding_handle *b_src = pipe_hnd->binding_handle;
 
        /* FIXME: Here the nightmare begins */
 
        WERROR result;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-       uint32_t i = 0, p = 0, j = 0;
+       uint32_t i = 0, j = 0;
        uint32_t num_printers;
        uint32_t level = 2;
        const char *printername, *sharename;
        struct rpc_pipe_client *pipe_hnd_dst = NULL;
+       struct dcerpc_binding_handle *b_dst = NULL;
        struct policy_handle hnd_src, hnd_dst;
        union spoolss_PrinterInfo *info_enum;
        union spoolss_PrinterInfo info_dst_publish;
@@ -2194,8 +2296,10 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
        /* connect destination PI_SPOOLSS */
        nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
                                     &ndr_table_spoolss.syntax_id);
-       if (!NT_STATUS_IS_OK(nt_status))
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return nt_status;
+       }
+       b_dst = pipe_hnd_dst->binding_handle;
 
        /* enum src printers */
        if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &info_enum)) {
@@ -2220,11 +2324,10 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
        /* do something for all printers */
        for (i = 0; i < num_printers; i++) {
 
-               uint32_t value_offered = 0, value_needed;
-               uint32_t data_offered = 0, data_needed;
+               uint32_t value_needed;
+               uint32_t data_needed;
                enum winreg_Type type;
-               uint8_t *buffer = NULL;
-               const char *value_name = NULL;
+               struct spoolss_EnumPrinterData r;
 
                /* do some initialization */
                printername = info_enum[i].info2.printername;
@@ -2316,60 +2419,60 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
                   FIXME: IIRC I've seen it too on a win2k-server
                */
 
+               r.in.handle = &hnd_src;
+               r.in.enum_index = 0;
+               r.in.value_offered = 0;
+               r.in.data_offered = 0;
+               r.out.value_name = NULL;
+               r.out.value_needed = &value_needed;
+               r.out.type = &type;
+               r.out.data = NULL;
+               r.out.data_needed = &data_needed;
+
                /* enumerate data on src handle */
-               nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
-                                                          &hnd_src,
-                                                          p,
-                                                          value_name,
-                                                          value_offered,
-                                                          &value_needed,
-                                                          &type,
-                                                          buffer,
-                                                          data_offered,
-                                                          &data_needed,
-                                                          &result);
-
-               data_offered    = data_needed;
-               value_offered   = value_needed;
-               buffer          = talloc_zero_array(mem_ctx, uint8_t, data_needed);
-               value_name      = talloc_zero_array(mem_ctx, char, value_needed);
+               nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
+
+               r.in.data_offered       = *r.out.data_needed;
+               r.in.value_offered      = *r.out.value_needed;
+               r.out.data              = talloc_zero_array(mem_ctx, uint8_t, r.in.data_offered);
+               r.out.value_name        = talloc_zero_array(mem_ctx, char, r.in.value_offered);
 
                /* loop for all printerdata of "PrinterDriverData" */
-               while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
-
-                       nt_status = rpccli_spoolss_EnumPrinterData(pipe_hnd, mem_ctx,
-                                                                  &hnd_src,
-                                                                  p++,
-                                                                  value_name,
-                                                                  value_offered,
-                                                                  &value_needed,
-                                                                  &type,
-                                                                  buffer,
-                                                                  data_offered,
-                                                                  &data_needed,
-                                                                  &result);
-                       /* loop for all reg_keys */
-                       if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(result)) {
+               while (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
+
+                       r.in.enum_index++;
 
-                               struct regval_blob v;
+                       nt_status = dcerpc_spoolss_EnumPrinterData_r(b_src, mem_ctx, &r);
+
+                       /* loop for all reg_keys */
+                       if (NT_STATUS_IS_OK(nt_status) && W_ERROR_IS_OK(r.out.result)) {
 
                                /* display_value */
                                if (c->opt_verbose) {
-                                       fstrcpy(v.valuename, value_name);
-                                       v.type = type;
-                                       v.size = data_offered;
-                                       v.data_p = buffer;
+                                       struct regval_blob *v;
+
+                                       v = regval_compose(talloc_tos(),
+                                                          r.out.value_name,
+                                                          *r.out.type,
+                                                          r.out.data,
+                                                          r.in.data_offered);
+                                       if (v == NULL) {
+                                               nt_status = NT_STATUS_NO_MEMORY;
+                                               goto done;
+                                       }
+
                                        display_reg_value(SPOOL_PRINTERDATA_KEY, v);
+                                       talloc_free(v);
                                }
 
                                /* set_value */
                                if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
-                                                               &hnd_dst, value_name,
-                                                               type, buffer, data_offered))
+                                                               &hnd_dst, r.out.value_name,
+                                                               *r.out.type, r.out.data, r.in.data_offered))
                                        goto done;
 
                                DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
-                                       v.valuename));
+                                           r.out.value_name));
                        }
                }
 
@@ -2408,9 +2511,11 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
 
                        for (j=0; j < count; j++) {
 
-                               struct regval_blob value;
+                               struct regval_blob *value;
                                DATA_BLOB blob;
 
+                               ZERO_STRUCT(blob);
+
                                /* although samba replies with sane data in most cases we
                                   should try to avoid writing wrong registry data */
 
@@ -2423,8 +2528,7 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
                                        if (strequal(info[j].value_name, SPOOL_REG_PORTNAME)) {
 
                                                /* although windows uses a multi-sz, we use a sz */
-                                               push_reg_sz(mem_ctx, NULL, &blob, SAMBA_PRINTER_PORT_NAME);
-                                               fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
+                                               push_reg_sz(mem_ctx, &blob, SAMBA_PRINTER_PORT_NAME);
                                        }
 
                                        if (strequal(info[j].value_name, SPOOL_REG_UNCNAME)) {
@@ -2433,8 +2537,7 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
                                                        nt_status = NT_STATUS_NO_MEMORY;
                                                        goto done;
                                                }
-                                               push_reg_sz(mem_ctx, NULL, &blob, unc_name);
-                                               fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
+                                               push_reg_sz(mem_ctx, &blob, unc_name);
                                        }
 
                                        if (strequal(info[j].value_name, SPOOL_REG_URL)) {
@@ -2454,22 +2557,22 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
 
                                        if (strequal(info[j].value_name, SPOOL_REG_SERVERNAME)) {
 
-                                               push_reg_sz(mem_ctx, NULL, &blob, longname);
-                                               fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
+                                               push_reg_sz(mem_ctx, &blob, longname);
                                        }
 
                                        if (strequal(info[j].value_name, SPOOL_REG_SHORTSERVERNAME)) {
 
-                                               push_reg_sz(mem_ctx, NULL, &blob, global_myname());
-                                               fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
+                                               push_reg_sz(mem_ctx, &blob, lp_netbios_name());
                                        }
 
-                                       value.type = REG_SZ;
-                                       value.size = blob.length;
-                                       if (value.size) {
-                                               value.data_p = blob.data;
-                                       } else {
-                                               value.data_p = NULL;
+                                       value = regval_compose(talloc_tos(),
+                                                              info[j].value_name,
+                                                              REG_SZ,
+                                                              blob.length == 0 ? NULL : blob.data,
+                                                              blob.length);
+                                       if (value == NULL) {
+                                               nt_status = NT_STATUS_NO_MEMORY;
+                                               goto done;
                                        }
 
                                        if (c->opt_verbose)
@@ -2477,17 +2580,26 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
 
                                        /* here we have to set all subkeys on the dst server */
                                        if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
-                                                       subkey, &value))
+                                                       subkey, value))
+                                       {
+                                               talloc_free(value);
                                                goto done;
+                                       }
 
+                                       talloc_free(value);
                                } else {
 
-                                       struct regval_blob v;
+                                       struct regval_blob *v;
 
-                                       fstrcpy(v.valuename, info[j].value_name);
-                                       v.type = info[j].type;
-                                       v.data_p = info[j].data->data;
-                                       v.size = info[j].data->length;
+                                       v = regval_compose(talloc_tos(),
+                                                          info[j].value_name,
+                                                          info[j].type,
+                                                          info[j].data->data,
+                                                          info[j].data->length);
+                                       if (v == NULL) {
+                                               nt_status = NT_STATUS_NO_MEMORY;
+                                               goto done;
+                                       }
 
                                        if (c->opt_verbose) {
                                                display_reg_value(subkey, v);
@@ -2495,10 +2607,11 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
 
                                        /* here we have to set all subkeys on the dst server */
                                        if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
-                                                       subkey, &v)) {
+                                                       subkey, v)) {
                                                goto done;
                                        }
 
+                                       talloc_free(v);
                                }
 
                                DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
@@ -2511,13 +2624,12 @@ NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
 
                /* close printer handles here */
                if (is_valid_policy_hnd(&hnd_src)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
                }
 
                if (is_valid_policy_hnd(&hnd_dst)) {
-                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+                       dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
                }
-
        }
 
        nt_status = NT_STATUS_OK;
@@ -2527,11 +2639,13 @@ done:
        SAFE_FREE(url);
        SAFE_FREE(unc_name);
 
-       if (is_valid_policy_hnd(&hnd_src))
-               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+       if (is_valid_policy_hnd(&hnd_src)) {
+               dcerpc_spoolss_ClosePrinter(b_src, mem_ctx, &hnd_src, &result);
+       }
 
-       if (is_valid_policy_hnd(&hnd_dst))
-               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+       if (is_valid_policy_hnd(&hnd_dst)) {
+               dcerpc_spoolss_ClosePrinter(b_dst, mem_ctx, &hnd_dst, &result);
+       }
 
        if (cli_dst) {
                cli_shutdown(cli_dst);