s3-spoolss: use is_valid_policy_hnd to check for valid policy handles.
[tprouty/samba.git] / source3 / utils / net_rpc_printer.c
index e635b78573b8dc367cb735d1bc183abebe373959..99012ddf2dac55b273ad3f808b3d39a3a5d5adc1 100644 (file)
@@ -1,22 +1,21 @@
-/* 
-   Samba Unix/Linux SMB client library 
-   Distributed SMB/CIFS Server Management Utility 
+/*
+   Samba Unix/Linux SMB client library
+   Distributed SMB/CIFS Server Management Utility
    Copyright (C) 2004 Guenther Deschner (gd@samba.org)
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 #include "includes.h"
 #include "utils/net.h"
 
@@ -32,6 +31,7 @@ static const struct table_node archi_table[]= {
 
        {"Windows 4.0",          "WIN40",       0 },
        {"Windows NT x86",       "W32X86",      2 },
+       {"Windows NT x86",       "W32X86",      3 },
        {"Windows NT R4000",     "W32MIPS",     2 },
        {"Windows NT Alpha_AXP", "W32ALPHA",    2 },
        {"Windows NT PowerPC",   "W32PPC",      2 },
@@ -42,140 +42,41 @@ static const struct table_node archi_table[]= {
 
 
 /**
- * The display-functions for Security-Descriptors were taken from rpcclient
- * 
- * They reside here for debugging purpose and should 
- * possibly be removed later on
- *
+ * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
+ * It is here for debugging purpose and should be removed later on.
  **/
-/****************************************************************************
-convert a security permissions into a string
-****************************************************************************/
-char *get_sec_mask_str(uint32 type)
-{
-       static fstring typestr="";
-
-       typestr[0] = 0;
-
-       if (type & GENERIC_ALL_ACCESS)
-               fstrcat(typestr, "Generic all access ");
-       if (type & GENERIC_EXECUTE_ACCESS)
-               fstrcat(typestr, "Generic execute access ");
-       if (type & GENERIC_WRITE_ACCESS)
-               fstrcat(typestr, "Generic write access ");
-       if (type & GENERIC_READ_ACCESS)
-               fstrcat(typestr, "Generic read access ");
-       if (type & MAXIMUM_ALLOWED_ACCESS)
-               fstrcat(typestr, "MAXIMUM_ALLOWED_ACCESS ");
-       if (type & SYSTEM_SECURITY_ACCESS)
-               fstrcat(typestr, "SYSTEM_SECURITY_ACCESS ");
-       if (type & SYNCHRONIZE_ACCESS)
-               fstrcat(typestr, "SYNCHRONIZE_ACCESS ");
-       if (type & WRITE_OWNER_ACCESS)
-               fstrcat(typestr, "WRITE_OWNER_ACCESS ");
-       if (type & WRITE_DAC_ACCESS)
-               fstrcat(typestr, "WRITE_DAC_ACCESS ");
-       if (type & READ_CONTROL_ACCESS)
-               fstrcat(typestr, "READ_CONTROL_ACCESS ");
-       if (type & DELETE_ACCESS)
-               fstrcat(typestr, "DELETE_ACCESS ");
-
-       printf("\t\tSpecific bits: 0x%lx\n", (unsigned long)type&SPECIFIC_RIGHTS_MASK);
-
-       return typestr;
-}
-
 
 /****************************************************************************
- display sec_ace structure
- ****************************************************************************/
-void display_sec_ace(SEC_ACE *ace)
-{
-       fstring sid_str;
-
-       printf("\tACE\n\t\ttype: ");
-       switch (ace->type) {
-               case SEC_ACE_TYPE_ACCESS_ALLOWED:
-                       printf("ACCESS ALLOWED");
-                       break;
-               case SEC_ACE_TYPE_ACCESS_DENIED:
-                       printf("ACCESS DENIED");
-                       break;
-               case SEC_ACE_TYPE_SYSTEM_AUDIT:
-                       printf("SYSTEM AUDIT");
-                       break;
-               case SEC_ACE_TYPE_SYSTEM_ALARM:
-                       printf("SYSTEM ALARM");
-                       break;
-               default:
-                       printf("????");
-                       break;
-       }
-       printf(" (%d) flags: %d\n", ace->type, ace->flags);
-       printf("\t\tPermissions: 0x%x: %s\n", ace->info.mask, get_sec_mask_str(ace->info.mask));
-
-       sid_to_string(sid_str, &ace->trustee);
-       printf("\t\tSID: %s\n\n", sid_str);
-}
-
+ Printer info level 3 display function.
+****************************************************************************/
 
-/****************************************************************************
- display sec_acl structure
- ****************************************************************************/
-void display_sec_acl(SEC_ACL *sec_acl)
+static void display_print_driver3(struct spoolss_DriverInfo3 *r)
 {
        int i;
 
-       printf("\tACL\tNum ACEs:\t%d\trevision:\t%x\n",
-                        sec_acl->num_aces, sec_acl->revision); 
-       printf("\t---\n");
-
-       if (sec_acl->size != 0 && sec_acl->num_aces != 0)
-               for (i = 0; i < sec_acl->num_aces; i++)
-                       display_sec_ace(&sec_acl->ace[i]);
-                               
-}
-
-/****************************************************************************
- display sec_desc structure
- ****************************************************************************/
-void display_sec_desc(SEC_DESC *sec)
-{
-       fstring sid_str;
-
-       if (sec == NULL)
+       if (!r) {
                return;
-
-       if (sec->sacl) {
-               printf("SACL\n");
-               display_sec_acl(sec->sacl);
        }
 
-       if (sec->dacl) {
-               printf("DACL\n");
-               display_sec_acl(sec->dacl);
+       printf("Printer Driver Info 3:\n");
+       printf("\tVersion: [%x]\n", r->version);
+       printf("\tDriver Name: [%s]\n", r->driver_name);
+       printf("\tArchitecture: [%s]\n", r->architecture);
+       printf("\tDriver Path: [%s]\n", r->driver_path);
+       printf("\tDatafile: [%s]\n", r->data_file);
+       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++) {
+               printf("\tDependentfiles: [%s]\n", r->dependent_files[i]);
        }
 
-       if (sec->owner_sid) {
-               sid_to_string(sid_str, sec->owner_sid);
-               printf("\tOwner SID:\t%s\n", sid_str);
-       }
+       printf("\n");
 
-       if (sec->grp_sid) {
-               sid_to_string(sid_str, sec->grp_sid);
-               printf("\tParent SID:\t%s\n", sid_str);
-       }
+       printf("\tMonitorname: [%s]\n", r->monitor_name);
+       printf("\tDefaultdatatype: [%s]\n\n", r->default_datatype);
 }
 
-
-/**
- * This display-printdriver-functions was borrowed from rpcclient/cmd_spoolss.c.
- * It is here for debugging purpose and should be removed later on.
- **/
-
-/****************************************************************************
-printer info level 3 display function
-****************************************************************************/
 static void display_print_driver_3(DRIVER_INFO_3 *i1)
 {
        fstring name = "";
@@ -187,10 +88,10 @@ static void display_print_driver_3(DRIVER_INFO_3 *i1)
        fstring dependentfiles = "";
        fstring monitorname = "";
        fstring defaultdatatype = "";
-       
+
        int length=0;
-       BOOL valid = True;
-       
+       bool valid = true;
+
        if (i1 == NULL)
                return;
 
@@ -214,74 +115,83 @@ static void display_print_driver_3(DRIVER_INFO_3 *i1)
 
        while (valid) {
                rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
-               
+
                length+=strlen(dependentfiles)+1;
-               
+
                if (strlen(dependentfiles) > 0) {
                        d_printf ("\tDependentfiles: [%s]\n", dependentfiles);
                } else {
-                       valid = False;
+                       valid = false;
                }
        }
-       
+
        printf ("\n");
 
        d_printf ("\tMonitorname: [%s]\n", monitorname);
        d_printf ("\tDefaultdatatype: [%s]\n\n", defaultdatatype);
 
-       return; 
+       return;
 }
 
-
-static void display_reg_value(pstring subkey, REGISTRY_VALUE value)
+static void display_reg_value(const char *subkey, REGISTRY_VALUE value)
 {
-       pstring text;
+       char *text;
 
        switch(value.type) {
        case REG_DWORD:
-               d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename, 
+               d_printf("\t[%s:%s]: REG_DWORD: 0x%08x\n", subkey, value.valuename,
                       *((uint32 *) value.data_p));
                break;
 
        case REG_SZ:
-               rpcstr_pull(text, value.data_p, sizeof(text), value.size,
-                           STR_TERMINATE);
+               rpcstr_pull_talloc(talloc_tos(),
+                               &text,
+                               value.data_p,
+                               value.size,
+                               STR_TERMINATE);
+               if (!text) {
+                       break;
+               }
                d_printf("\t[%s:%s]: REG_SZ: %s\n", subkey, value.valuename, text);
                break;
 
-       case REG_BINARY: 
-               d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n", 
+       case REG_BINARY:
+               d_printf("\t[%s:%s]: REG_BINARY: unknown length value not displayed\n",
                         subkey, value.valuename);
                break;
 
        case REG_MULTI_SZ: {
-               uint16 *curstr = (uint16 *) value.data_p;
-               uint8 *start = value.data_p;
-               d_printf("\t[%s:%s]: REG_MULTI_SZ:\n", subkey, value.valuename);
-               while ((*curstr != 0) && 
-                      ((uint8 *) curstr < start + value.size)) {
-                       rpcstr_pull(text, curstr, sizeof(text), -1, 
-                                   STR_TERMINATE);
-                       d_printf("%s\n", text);
-                       curstr += strlen(text) + 1;
+               uint32 i, num_values;
+               char **values;
+
+               if (!W_ERROR_IS_OK(reg_pull_multi_sz(NULL, value.data_p,
+                                                    value.size, &num_values,
+                                                    &values))) {
+                       d_printf("reg_pull_multi_sz failed\n");
+                       break;
+               }
+
+               for (i=0; i<num_values; i++) {
+                       d_printf("%s\n", values[i]);
                }
+               TALLOC_FREE(values);
+               break;
        }
-       break;
 
        default:
                d_printf("\t%s: unknown type %d\n", value.valuename, value.type);
        }
-       
-}
 
+}
 
 /**
- * Copies ACLs, DOS-attributes and timestamps from one 
- * file or directory from one connected share to another connected share 
+ * Copies ACLs, DOS-attributes and timestamps from one
+ * file or directory from one connected share to another connected share
  *
+ * @param c                    A net_context structure
  * @param mem_ctx              A talloc-context
- * @param cli_share_src                A connected cli_state 
- * @param cli_share_dst                A connected cli_state 
+ * @param cli_share_src                A connected cli_state
+ * @param cli_share_dst                A connected cli_state
  * @param src_file             The source file-name
  * @param dst_file             The destination file-name
  * @param copy_acls            Whether to copy acls
@@ -290,35 +200,35 @@ static void display_reg_value(pstring subkey, REGISTRY_VALUE value)
  * @param is_file              Whether this file is a file or a dir
  *
  * @return Normal NTSTATUS return.
- **/ 
-static NTSTATUS 
-net_copy_fileattr(TALLOC_CTX *mem_ctx,
+ **/
+
+NTSTATUS net_copy_fileattr(struct net_context *c,
+                 TALLOC_CTX *mem_ctx,
                  struct cli_state *cli_share_src,
-                 struct cli_state *cli_share_dst, 
-                 char *src_name, char *dst_name,
-                 BOOL copy_acls, BOOL copy_attrs,
-                 BOOL copy_timestamps, BOOL is_file)
+                 struct cli_state *cli_share_dst,
+                 const char *src_name, const char *dst_name,
+                 bool copy_acls, bool copy_attrs,
+                 bool copy_timestamps, bool is_file)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        int fnum_src = 0;
        int fnum_dst = 0;
        SEC_DESC *sd = NULL;
        uint16 attr;
-       time_t atime, ctime, mtime;
+       time_t f_atime, f_ctime, f_mtime;
 
 
        if (!copy_timestamps && !copy_acls && !copy_attrs)
                return NT_STATUS_OK;
 
-
        /* open file/dir on the originating server */
 
-       DEBUGADD(3,("opening %s %s on originating server\n", 
+       DEBUGADD(3,("opening %s %s on originating server\n",
                is_file?"file":"dir", src_name));
 
        fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
        if (fnum_src == -1) {
-               DEBUGADD(0,("cannot open %s %s on originating server %s\n", 
+               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);
                goto out;
@@ -336,7 +246,7 @@ net_copy_fileattr(TALLOC_CTX *mem_ctx,
                        goto out;
                }
 
-               if (opt_verbose && DEBUGLEVEL >= 3)
+               if (c->opt_verbose && DEBUGLEVEL >= 3)
                        display_sec_desc(sd);
        }
 
@@ -344,9 +254,9 @@ net_copy_fileattr(TALLOC_CTX *mem_ctx,
        if (copy_attrs || copy_timestamps) {
 
                /* get file attributes */
-               if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL, 
-                                &ctime, &atime, &mtime)) {
-                       DEBUG(0,("failed to get file-attrs: %s\n", 
+               if (!cli_getattrE(cli_share_src, fnum_src, &attr, NULL,
+                                &f_ctime, &f_atime, &f_mtime)) {
+                       DEBUG(0,("failed to get file-attrs: %s\n",
                                cli_errstr(cli_share_src)));
                        nt_status = cli_nt_error(cli_share_src);
                        goto out;
@@ -354,7 +264,7 @@ net_copy_fileattr(TALLOC_CTX *mem_ctx,
        }
 
 
-       /* open the file/dir on the destination server */ 
+       /* open the file/dir on the destination server */
 
        fnum_dst = cli_nt_create(cli_share_dst, dst_name, WRITE_DAC_ACCESS | WRITE_OWNER_ACCESS);
        if (fnum_dst == -1) {
@@ -367,7 +277,7 @@ net_copy_fileattr(TALLOC_CTX *mem_ctx,
        if (copy_timestamps) {
 
                /* set timestamps */
-               if (!cli_setattrE(cli_share_dst, fnum_dst, ctime, atime, mtime)) {
+               if (!cli_setattrE(cli_share_dst, fnum_dst, f_ctime, f_atime, f_mtime)) {
                        DEBUG(0,("failed to set file-attrs (timestamps): %s\n",
                                cli_errstr(cli_share_dst)));
                        nt_status = cli_nt_error(cli_share_dst);
@@ -401,14 +311,14 @@ net_copy_fileattr(TALLOC_CTX *mem_ctx,
        /* closing files */
 
        if (!cli_close(cli_share_src, fnum_src)) {
-               d_printf("could not close %s on originating server: %s\n", 
+               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);
                goto out;
        }
 
        if (!cli_close(cli_share_dst, fnum_dst)) {
-               d_printf("could not close %s on destination server: %s\n", 
+               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);
                goto out;
@@ -429,13 +339,13 @@ out:
        return nt_status;
 }
 
-
 /**
- * Copy a file or directory from a connected share to another connected share 
+ * Copy a file or directory from a connected share to another connected share
  *
+ * @param c                    A net_context structure
  * @param mem_ctx              A talloc-context
- * @param cli_share_src                A connected cli_state 
- * @param cli_share_dst                A connected cli_state 
+ * @param cli_share_src                A connected cli_state
+ * @param cli_share_dst                A connected cli_state
  * @param src_file             The source file-name
  * @param dst_file             The destination file-name
  * @param copy_acls            Whether to copy acls
@@ -444,13 +354,15 @@ out:
  * @param is_file              Whether this file is a file or a dir
  *
  * @return Normal NTSTATUS return.
- **/ 
-NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
+ **/
+
+NTSTATUS net_copy_file(struct net_context *c,
+                      TALLOC_CTX *mem_ctx,
                       struct cli_state *cli_share_src,
-                      struct cli_state *cli_share_dst, 
-                      char *src_name, char *dst_name,
-                      BOOL copy_acls, BOOL copy_attrs,
-                      BOOL copy_timestamps, BOOL is_file)
+                      struct cli_state *cli_share_dst,
+                      const char *src_name, const char *dst_name,
+                      bool copy_acls, bool copy_attrs,
+                      bool copy_timestamps, bool is_file)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        int fnum_src = 0;
@@ -458,19 +370,17 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
        static int io_bufsize = 64512;
        int read_size = io_bufsize;
        char *data = NULL;
-       off_t start = 0;
        off_t nread = 0;
 
 
        if (!src_name || !dst_name)
                goto out;
 
-       if (cli_share_dst == NULL || cli_share_dst == NULL)
-               goto out; 
-               
+       if (cli_share_src == NULL || cli_share_dst == NULL)
+               goto out;
 
        /* open on the originating server */
-       DEBUGADD(3,("opening %s %s on originating server\n", 
+       DEBUGADD(3,("opening %s %s on originating server\n",
                is_file ? "file":"dir", src_name));
        if (is_file)
                fnum_src = cli_open(cli_share_src, src_name, O_RDONLY, DENY_NONE);
@@ -478,7 +388,8 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
                fnum_src = cli_nt_create(cli_share_src, src_name, READ_CONTROL_ACCESS);
 
        if (fnum_src == -1) {
-               DEBUGADD(0,("cannot open file %s on originating server %s\n", 
+               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);
                goto out;
@@ -489,7 +400,7 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
 
                /* open file on the destination server */
                DEBUGADD(3,("opening file %s on destination server\n", dst_name));
-               fnum_dst = cli_open(cli_share_dst, dst_name, 
+               fnum_dst = cli_open(cli_share_dst, dst_name,
                                O_RDWR|O_CREAT|O_TRUNC, DENY_NONE);
 
                if (fnum_dst == -1) {
@@ -500,8 +411,8 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
                }
 
                /* allocate memory */
-               if (!(data = (char *)malloc(read_size))) {
-                       d_printf("malloc fail for size %d\n", read_size);
+               if (!(data = (char *)SMB_MALLOC(read_size))) {
+                       d_fprintf(stderr, "malloc fail for size %d\n", read_size);
                        nt_status = NT_STATUS_NO_MEMORY;
                        goto out;
                }
@@ -509,13 +420,13 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
        }
 
 
-       if (opt_verbose) {
+       if (c->opt_verbose) {
 
                d_printf("copying [\\\\%s\\%s%s] => [\\\\%s\\%s%s] "
-                        "%s ACLs and %s DOS Attributes %s\n", 
+                        "%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,
-                       copy_acls ?  "with" : "without", 
+                       copy_acls ?  "with" : "without",
                        copy_attrs ? "with" : "without",
                        copy_timestamps ? "(preserving timestamps)" : "" );
        }
@@ -525,17 +436,17 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
 
                /* copying file */
                int n, ret;
-               n = cli_read(cli_share_src, fnum_src, data, nread + start, 
+               n = cli_read(cli_share_src, fnum_src, data, nread,
                                read_size);
 
                if (n <= 0)
                        break;
 
-               ret = cli_write(cli_share_dst, fnum_dst, 0, data, 
-                       nread + start, n);
+               ret = cli_write(cli_share_dst, fnum_dst, 0, data,
+                       nread, n);
 
                if (n != ret) {
-                       d_printf("Error writing file: %s\n", 
+                       d_fprintf(stderr, "Error writing file: %s\n",
                                cli_errstr(cli_share_dst));
                        nt_status = cli_nt_error(cli_share_dst);
                        goto out;
@@ -548,7 +459,7 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
        if (!is_file && !cli_chkpath(cli_share_dst, dst_name)) {
 
                /* creating dir */
-               DEBUGADD(3,("creating dir %s on the destination server\n", 
+               DEBUGADD(3,("creating dir %s on the destination server\n",
                        dst_name));
 
                if (!cli_mkdir(cli_share_dst, dst_name)) {
@@ -558,7 +469,7 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
                }
 
                if (!cli_chkpath(cli_share_dst, dst_name)) {
-                       d_printf("cannot check for directory %s: %s\n",
+                       d_fprintf(stderr, "cannot check for directory %s: %s\n",
                                dst_name, cli_errstr(cli_share_dst));
                        goto out;
                }
@@ -567,22 +478,22 @@ NTSTATUS net_copy_file(TALLOC_CTX *mem_ctx,
 
        /* closing files */
        if (!cli_close(cli_share_src, fnum_src)) {
-               d_printf("could not close file on originating server: %s\n", 
+               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);
                goto out;
        }
 
        if (is_file && !cli_close(cli_share_dst, fnum_dst)) {
-               d_printf("could not close file on destination server: %s\n", 
+               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;
        }
 
        /* possibly we have to copy some file-attributes / acls / sd */
-       nt_status = net_copy_fileattr(mem_ctx, cli_share_src, cli_share_dst, 
-                                     src_name, dst_name, copy_acls, 
+       nt_status = net_copy_fileattr(c, mem_ctx, cli_share_src, cli_share_dst,
+                                     src_name, dst_name, copy_acls,
                                      copy_attrs, copy_timestamps, is_file);
        if (!NT_STATUS_IS_OK(nt_status))
                goto out;
@@ -604,51 +515,57 @@ out:
        return nt_status;
 }
 
-
 /**
- * Copy a driverfile from on connected share to another connected share 
- * This silently assumes that a driver-file is picked up from 
+ * Copy a driverfile from on connected share to another connected share
+ * This silently assumes that a driver-file is picked up from
  *
- *     \\src_server\print$\{arch}\{version}\file 
+ *     \\src_server\print$\{arch}\{version}\file
  *
  * and copied to
  *
- *     \\dst_server\print$\{arch}\file 
- * 
+ *     \\dst_server\print$\{arch}\file
+ *
  * to be added via setdriver-calls later.
+ * @param c                    A net_context structure
  * @param mem_ctx              A talloc-context
  * @param cli_share_src                A cli_state connected to source print$-share
  * @param cli_share_dst                A cli_state connected to destination print$-share
- * @param file                 The file-name to be copied 
+ * @param file                 The file-name to be copied
  * @param short_archi          The name of the driver-architecture (short form)
  *
  * @return Normal NTSTATUS return.
- **/ 
-static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
+ **/
+
+static NTSTATUS net_copy_driverfile(struct net_context *c,
+                                   TALLOC_CTX *mem_ctx,
                                    struct cli_state *cli_share_src,
-                                   struct cli_state *cli_share_dst, 
-                                   char *file, const char *short_archi) {
+                                   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;
-       fstring version;
-       fstring filename;
-       fstring tok;
+       char *version;
+       char *filename;
+       char *tok;
+
+       if (!file) {
+               return NT_STATUS_OK;
+       }
 
-       /* scroll through the file until we have the part 
+       /* scroll through the file until we have the part
           beyond archi_table.short_archi */
        p = file;
-       while (next_token(&p, tok, "\\", sizeof(tok))) {
+       while (next_token_talloc(mem_ctx, &p, &tok, "\\")) {
                if (strequal(tok, short_archi)) {
-                       next_token(&p, version, "\\", sizeof(version));
-                       next_token(&p, filename, "\\", sizeof(filename));
+                       next_token_talloc(mem_ctx, &p, &version, "\\");
+                       next_token_talloc(mem_ctx, &p, &filename, "\\");
                }
        }
 
        /* build source file name */
-       if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 ) 
+       if (asprintf(&src_name, "\\%s\\%s\\%s", short_archi, version, filename) < 0 )
                return NT_STATUS_NO_MEMORY;
 
 
@@ -658,8 +575,8 @@ static NTSTATUS net_copy_driverfile(TALLOC_CTX *mem_ctx,
 
 
        /* finally copy the file */
-       nt_status = net_copy_file(mem_ctx, cli_share_src, cli_share_dst, 
-                                 src_name, dst_name, False, False, False, True);
+       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;
 
@@ -672,7 +589,6 @@ out:
        return nt_status;
 }
 
-
 /**
  * Check for existing Architecture directory on a given server
  *
@@ -681,8 +597,8 @@ out:
  *
  * @return Normal NTSTATUS return.
  **/
-static NTSTATUS 
-check_arch_dir(struct cli_state *cli_share, const char *short_archi)
+
+static NTSTATUS check_arch_dir(struct cli_state *cli_share, const char *short_archi)
 {
 
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
@@ -692,7 +608,7 @@ check_arch_dir(struct cli_state *cli_share, const char *short_archi)
                return NT_STATUS_NO_MEMORY;
        }
 
-       DEBUG(10,("creating print-driver dir for architecture: %s\n", 
+       DEBUG(10,("creating print-driver dir for architecture: %s\n",
                short_archi));
 
        if (!cli_mkdir(cli_share, dir)) {
@@ -702,7 +618,7 @@ check_arch_dir(struct cli_state *cli_share, const char *short_archi)
         }
 
        if (!cli_chkpath(cli_share, dir)) {
-               d_printf("cannot check %s: %s\n", 
+               d_fprintf(stderr, "cannot check %s: %s\n",
                        dir, cli_errstr(cli_share));
                goto out;
        }
@@ -714,11 +630,11 @@ out:
        return nt_status;
 }
 
-
 /**
- * Copy a print-driver (level 3) from one connected print$-share to another 
+ * Copy a print-driver (level 3) from one connected print$-share to another
  * connected print$-share
  *
+ * @param c                    A net_context structure
  * @param mem_ctx              A talloc-context
  * @param cli_share_src                A cli_state connected to a print$-share
  * @param cli_share_dst                A cli_state connected to a print$-share
@@ -727,460 +643,491 @@ out:
  *
  * @return Normal NTSTATUS return.
  **/
-static NTSTATUS 
-copy_print_driver_3(TALLOC_CTX *mem_ctx,
-                   struct cli_state *cli_share_src, 
-                   struct cli_state *cli_share_dst, 
-                   const char *short_archi, DRIVER_INFO_3 *i1)
+
+static NTSTATUS copy_print_driver_3(struct net_context *c,
+                   TALLOC_CTX *mem_ctx,
+                   struct cli_state *cli_share_src,
+                   struct cli_state *cli_share_dst,
+                   const char *short_archi,
+                   struct spoolss_DriverInfo3 *r)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-       int length = 0;
-       BOOL valid = True;
-       
-       fstring name = "";
-       fstring driverpath = "";
-       fstring datafile = "";
-       fstring configfile = "";
-       fstring helpfile = "";
-       fstring dependentfiles = "";
-       
-       if (i1 == NULL)
-               return nt_status;
+       int i;
 
-       rpcstr_pull(name, i1->name.buffer, sizeof(name), -1, STR_TERMINATE);
-       rpcstr_pull(driverpath, i1->driverpath.buffer, sizeof(driverpath), -1, STR_TERMINATE);
-       rpcstr_pull(datafile, i1->datafile.buffer, sizeof(datafile), -1, STR_TERMINATE);
-       rpcstr_pull(configfile, i1->configfile.buffer, sizeof(configfile), -1, STR_TERMINATE);
-       rpcstr_pull(helpfile, i1->helpfile.buffer, sizeof(helpfile), -1, STR_TERMINATE);
+       if (r == NULL) {
+               return nt_status;
+       }
 
+       if (c->opt_verbose)
+               d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n",
+                         r->driver_name, short_archi, r->version);
 
-       if (opt_verbose)
-               d_printf("copying driver: [%s], for architecture: [%s], version: [%d]\n", 
-                         name, short_archi, i1->version);
-       
-       nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst, 
-               driverpath, short_archi);
+       nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
+               r->driver_path, short_archi);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
-               
-       nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst, 
-               datafile, short_archi);
+
+       nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
+               r->data_file, short_archi);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
-               
-       nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst, 
-               configfile, short_archi);
+
+       nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
+               r->config_file, short_archi);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
-               
-       nt_status = net_copy_driverfile(mem_ctx, cli_share_src, cli_share_dst, 
-               helpfile, short_archi);
+
+       nt_status = net_copy_driverfile(c, mem_ctx, cli_share_src, cli_share_dst,
+               r->help_file, short_archi);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
 
-       while (valid) {
-               rpcstr_pull(dependentfiles, i1->dependentfiles+length, sizeof(dependentfiles), -1, STR_TERMINATE);
-               length+=strlen(dependentfiles)+1;
-               if (strlen(dependentfiles) > 0) {
+       for (i=0; r->dependent_files[i] != NULL; i++) {
 
-                       nt_status = net_copy_driverfile(mem_ctx, 
-                                       cli_share_src, cli_share_dst, 
-                                       dependentfiles, short_archi);
-                       if (!NT_STATUS_IS_OK(nt_status))
-                               return nt_status;
-               } else {
-                       valid = False;
+               nt_status = net_copy_driverfile(c, mem_ctx,
+                               cli_share_src, cli_share_dst,
+                               r->dependent_files[i], short_archi);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       return nt_status;
                }
        }
 
        return NT_STATUS_OK;
 }
 
-
 /**
  * net_spoolss-functions
  * =====================
  *
  * the net_spoolss-functions aim to simplify spoolss-client-functions
  * required during the migration-process wrt buffer-sizes, returned
- * error-codes, etc. 
+ * error-codes, etc.
  *
  * this greatly reduces the complexitiy of the migrate-functions.
  *
  **/
 
-static BOOL
-net_spoolss_enum_printers(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                         char *name, uint32 flags, uint32 level, 
-                         uint32 *num_printers, PRINTER_INFO_CTR *ctr)
+static bool net_spoolss_enum_printers(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       char *name,
+                                       uint32 flags,
+                                       uint32 level,
+                                       uint32 *num_printers,
+                                       PRINTER_INFO_CTR *ctr)
 {
-
        WERROR result;
-       uint32 needed;
 
        /* enum printers */
-       result = cli_spoolss_enum_printers(
-               cli, mem_ctx, 0, &needed, name, flags,
+       result = rpccli_spoolss_enum_printers(pipe_hnd, mem_ctx, name, flags,
                level, num_printers, ctr);
 
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_INSUFFICIENT_BUFFER)) 
-               result = cli_spoolss_enum_printers(
-                       cli, mem_ctx, needed, NULL, name, flags,
-                       level, num_printers, ctr);
-       
-
        if (!W_ERROR_IS_OK(result)) {
-               printf("cannot enum printers: %s\n", dos_errstr(result));
-               return False;
+               printf("cannot enum printers: %s\n", win_errstr(result));
+               return false;
        }
 
-       return True;
+       return true;
 }
 
-
-static BOOL
-net_spoolss_open_printer_ex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                           const char *printername, uint32 access_required, 
-                           const char *username, POLICY_HND *hnd)
+static bool net_spoolss_open_printer_ex(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       const char *printername,
+                                       uint32 access_required,
+                                       const char *username,
+                                       POLICY_HND *hnd)
 {
        WERROR result;
-       fstring servername, printername2;
-
-       slprintf(servername, sizeof(servername)-1, "\\\\%s", cli->desthost);
+       fstring printername2;
 
-       fstrcpy(printername2, servername);
+       fstrcpy(printername2, pipe_hnd->srv_name_slash);
        fstrcat(printername2, "\\");
        fstrcat(printername2, printername);
 
-       DEBUG(10,("connecting to: %s as %s for %s and access: %x\n", 
-               servername, username, printername2, access_required));
+       DEBUG(10,("connecting to: %s as %s for %s and access: %x\n",
+               pipe_hnd->srv_name_slash, username, printername2, access_required));
 
        /* open printer */
-       result = cli_spoolss_open_printer_ex(cli, mem_ctx, printername2,
-                       "", access_required,
-                       servername, username, hnd);
+       result = rpccli_spoolss_openprinter_ex(pipe_hnd, mem_ctx,
+                                              printername2,
+                                              access_required,
+                                              hnd);
 
        /* be more verbose */
        if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
-               d_printf("no access to printer [%s] on [%s] for user [%s] granted\n", 
-                       printername2, servername, username);
-               return False;
+               d_fprintf(stderr, "no access to printer [%s] on [%s] for user [%s] granted\n",
+                       printername2, pipe_hnd->srv_name_slash, username);
+               return false;
        }
 
        if (!W_ERROR_IS_OK(result)) {
-               d_printf("cannot open printer %s on server %s: %s\n", 
-                       printername2, servername, dos_errstr(result));
-               return False;
+               d_fprintf(stderr, "cannot open printer %s on server %s: %s\n",
+                       printername2, pipe_hnd->srv_name_slash, win_errstr(result));
+               return false;
        }
 
-       DEBUG(2,("got printer handle for printer: %s, server: %s\n", 
-               printername2, servername));
+       DEBUG(2,("got printer handle for printer: %s, server: %s\n",
+               printername2, pipe_hnd->srv_name_slash));
 
-       return True;
+       return true;
 }
 
-
-static BOOL
-net_spoolss_getprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                      POLICY_HND *hnd, uint32 level, 
-                      PRINTER_INFO_CTR *ctr)
+static bool net_spoolss_getprinter(struct rpc_pipe_client *pipe_hnd,
+                               TALLOC_CTX *mem_ctx,
+                               POLICY_HND *hnd,
+                               uint32 level,
+                               union spoolss_PrinterInfo *info)
 {
        WERROR result;
-       uint32 needed;
 
        /* getprinter call */
-       result = cli_spoolss_getprinter(cli, 
-               mem_ctx, 0, &needed, hnd, level, ctr);
-
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_INSUFFICIENT_BUFFER)) 
-               result = cli_spoolss_getprinter(cli, 
-                       mem_ctx, needed, NULL, hnd, level, ctr);
-                       
+       result = rpccli_spoolss_getprinter(pipe_hnd, mem_ctx,
+                                          hnd,
+                                          level,
+                                          0, /* offered */
+                                          info);
        if (!W_ERROR_IS_OK(result)) {
-               printf("cannot get printer-info: %s\n", dos_errstr(result));
-               return False;
+               printf("cannot get printer-info: %s\n", win_errstr(result));
+               return false;
        }
 
-       return True;
+       return true;
 }
 
-
-static BOOL
-net_spoolss_setprinter(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                      POLICY_HND *hnd, uint32 level, 
-                      PRINTER_INFO_CTR *ctr)
+static bool net_spoolss_setprinter(struct rpc_pipe_client *pipe_hnd,
+                               TALLOC_CTX *mem_ctx,
+                               POLICY_HND *hnd,
+                               uint32 level,
+                               union spoolss_PrinterInfo *info)
 {
        WERROR result;
+       NTSTATUS status;
+       struct spoolss_SetPrinterInfoCtr info_ctr;
+       struct spoolss_DevmodeContainer devmode_ctr;
+       struct sec_desc_buf secdesc_ctr;
+
+       ZERO_STRUCT(devmode_ctr);
+       ZERO_STRUCT(secdesc_ctr);
 
        /* setprinter call */
-       result = cli_spoolss_setprinter(cli, mem_ctx, hnd, level, ctr, 0);
+
+       info_ctr.level = level;
+       switch (level) {
+       case 0:
+               info_ctr.info.info0 = (struct spoolss_SetPrinterInfo0 *)&info->info0;
+               break;
+       case 1:
+               info_ctr.info.info1 = (struct spoolss_SetPrinterInfo1 *)&info->info1;
+               break;
+       case 2:
+               info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info->info2;
+               break;
+       case 3:
+               info_ctr.info.info3 = (struct spoolss_SetPrinterInfo3 *)&info->info3;
+               break;
+       case 4:
+               info_ctr.info.info4 = (struct spoolss_SetPrinterInfo4 *)&info->info4;
+               break;
+       case 5:
+               info_ctr.info.info5 = (struct spoolss_SetPrinterInfo5 *)&info->info5;
+               break;
+       case 6:
+               info_ctr.info.info6 = (struct spoolss_SetPrinterInfo6 *)&info->info6;
+               break;
+       case 7:
+               info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info->info7;
+               break;
+#if 0 /* FIXME GD */
+       case 8:
+               info_ctr.info.info8 = (struct spoolss_SetPrinterInfo8 *)&info->info8;
+               break;
+       case 9:
+               info_ctr.info.info9 = (struct spoolss_SetPrinterInfo9 *)&info->info9;
+               break;
+#endif
+       default:
+               break; /* FIXME */
+       }
+
+       status = rpccli_spoolss_SetPrinter(pipe_hnd, mem_ctx,
+                                          hnd,
+                                          &info_ctr,
+                                          &devmode_ctr,
+                                          &secdesc_ctr,
+                                          0, /* command */
+                                          &result);
 
        if (!W_ERROR_IS_OK(result)) {
-               printf("cannot set printer-info: %s\n", dos_errstr(result));
-               return False;
+               printf("cannot set printer-info: %s\n", win_errstr(result));
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 
-static BOOL
-net_spoolss_setprinterdata(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                          POLICY_HND *hnd, REGISTRY_VALUE *value)
+static bool net_spoolss_setprinterdata(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       POLICY_HND *hnd,
+                                       REGISTRY_VALUE *value)
 {
        WERROR result;
-       
+
        /* setprinterdata call */
-       result = cli_spoolss_setprinterdata(cli, mem_ctx, hnd, value);
+       result = rpccli_spoolss_setprinterdata(pipe_hnd, mem_ctx, hnd, value);
 
        if (!W_ERROR_IS_OK(result)) {
-               printf ("unable to set printerdata: %s\n", dos_errstr(result));
-               return False;
+               printf ("unable to set printerdata: %s\n", win_errstr(result));
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 
-static BOOL
-net_spoolss_enumprinterkey(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                          POLICY_HND *hnd, const char *keyname,
-                          uint16 **keylist)
+static bool net_spoolss_enumprinterkey(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       POLICY_HND *hnd,
+                                       const char *keyname,
+                                       uint16 **keylist)
 {
        WERROR result;
-       uint32 needed, len;
 
        /* enumprinterkey call */
-       result = cli_spoolss_enumprinterkey(
-               cli, mem_ctx, 0, &needed, hnd, keyname, NULL, NULL);
-               
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_MORE_DATA)) 
-               result = cli_spoolss_enumprinterkey(
-                       cli, mem_ctx, needed, NULL, hnd, keyname, keylist,
-                       &len);
+       result = rpccli_spoolss_enumprinterkey(pipe_hnd, mem_ctx, hnd, keyname, keylist, NULL);
 
        if (!W_ERROR_IS_OK(result)) {
-               printf("enumprinterkey failed: %s\n", dos_errstr(result));
-               return False;
+               printf("enumprinterkey failed: %s\n", win_errstr(result));
+               return false;
        }
-       
-       return True;
-}
 
+       return true;
+}
 
-static BOOL
-net_spoolss_enumprinterdataex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                             uint32 offered, 
-                             POLICY_HND *hnd, const char *keyname, 
-                             REGVAL_CTR *ctr) 
+static bool net_spoolss_enumprinterdataex(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       uint32 offered,
+                                       POLICY_HND *hnd,
+                                       const char *keyname,
+                                       REGVAL_CTR *ctr)
 {
        WERROR result;
-       uint32 needed;
 
        /* enumprinterdataex call */
-       result = cli_spoolss_enumprinterdataex(
-               cli, mem_ctx, 0, &needed, hnd, keyname, NULL);
-                       
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_MORE_DATA)) 
-               result = cli_spoolss_enumprinterdataex(
-                       cli, mem_ctx, needed, NULL, hnd, keyname, ctr);
-                                       
+       result = rpccli_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, hnd, keyname, ctr);
+
        if (!W_ERROR_IS_OK(result)) {
-               printf("enumprinterdataex failed: %s\n", dos_errstr(result));
-               return False;
+               printf("enumprinterdataex failed: %s\n", win_errstr(result));
+               return false;
        }
-       
-       return True;
+
+       return true;
 }
 
 
-static BOOL 
-net_spoolss_setprinterdataex(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                            POLICY_HND *hnd, char *keyname, 
-                            REGISTRY_VALUE *value)
+static bool net_spoolss_setprinterdataex(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       POLICY_HND *hnd,
+                                       char *keyname,
+                                       REGISTRY_VALUE *value)
 {
        WERROR result;
+       NTSTATUS status;
 
        /* setprinterdataex call */
-       result = cli_spoolss_setprinterdataex(cli, mem_ctx, hnd, 
-                                             keyname, value);
-       
+       status = rpccli_spoolss_SetPrinterDataEx(pipe_hnd, mem_ctx,
+                                                hnd,
+                                                keyname,
+                                                value->valuename,
+                                                value->type,
+                                                value->data_p,
+                                                value->size,
+                                                &result);
+
        if (!W_ERROR_IS_OK(result)) {
-               printf("could not set printerdataex: %s\n", dos_errstr(result));
-               return False;
+               printf("could not set printerdataex: %s\n", win_errstr(result));
+               return false;
        }
-       
-       return True;
-}
 
+       return true;
+}
 
-static BOOL
-net_spoolss_enumforms(struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                     POLICY_HND *hnd, int level, uint32 *num_forms,
-                     FORM_1 **forms)
-                                                                                      
+static bool net_spoolss_enumforms(struct rpc_pipe_client *pipe_hnd,
+                               TALLOC_CTX *mem_ctx,
+                               POLICY_HND *hnd,
+                               int level,
+                               uint32 *num_forms,
+                               FORM_1 **forms)
 {
        WERROR result;
-       uint32 needed;
 
        /* enumforms call */
-       result = cli_spoolss_enumforms(
-                       cli, mem_ctx, 0, &needed, hnd, level, num_forms, forms);
-
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_INSUFFICIENT_BUFFER)) 
-               result = cli_spoolss_enumforms(
-                               cli, mem_ctx, needed, NULL, hnd, level,
-                               num_forms, forms);
+       result = rpccli_spoolss_enumforms(pipe_hnd, mem_ctx, hnd, level, num_forms, forms);
 
        if (!W_ERROR_IS_OK(result)) {
-               printf("could not enum forms: %s\n", dos_errstr(result));
-               return False;
+               printf("could not enum forms: %s\n", win_errstr(result));
+               return false;
        }
-       
-       return True;
-}
 
+       return true;
+}
 
-static BOOL
-net_spoolss_enumprinterdrivers (struct cli_state *cli, TALLOC_CTX *mem_ctx,
-                               uint32 level, const char *env,
-                               uint32 *num_drivers,
-                               PRINTER_DRIVER_CTR *ctr)
+static bool net_spoolss_enumprinterdrivers (struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       uint32 level, const char *env,
+                                       uint32 *num_drivers,
+                                       PRINTER_DRIVER_CTR *ctr)
 {
        WERROR result;
-       uint32 needed;
 
        /* enumprinterdrivers call */
-       result = cli_spoolss_enumprinterdrivers(
-                       cli, mem_ctx, 0, &needed, level,
+       result = rpccli_spoolss_enumprinterdrivers(
+                       pipe_hnd, mem_ctx, level,
                        env, num_drivers, ctr);
 
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_INSUFFICIENT_BUFFER))
-               result = cli_spoolss_enumprinterdrivers(
-                               cli, mem_ctx, needed, NULL, level,
-                               env, num_drivers, ctr);
-       
        if (!W_ERROR_IS_OK(result)) {
-               printf("cannot enum drivers: %s\n", dos_errstr(result));
-               return False;
+               printf("cannot enum drivers: %s\n", win_errstr(result));
+               return false;
        }
 
-       return True;
+       return true;
 }
 
-
-static BOOL
-net_spoolss_getprinterdriver(struct cli_state *cli, 
-                            TALLOC_CTX *mem_ctx, 
-                            POLICY_HND *hnd, uint32 level, 
-                            const char *env, int version, 
-                            PRINTER_DRIVER_CTR *ctr)
+static bool net_spoolss_getprinterdriver(struct rpc_pipe_client *pipe_hnd,
+                            TALLOC_CTX *mem_ctx,
+                            POLICY_HND *hnd, uint32 level,
+                            const char *env, int version,
+                            union spoolss_DriverInfo *info)
 {
        WERROR result;
-       uint32 needed;
-       
-       /* getprinterdriver call */
-       result = cli_spoolss_getprinterdriver(
-                       cli, mem_ctx, 0, &needed, hnd, level,
-                       env, version, ctr);
-
-       if (W_ERROR_V(result) == W_ERROR_V(WERR_INSUFFICIENT_BUFFER))
-               result = cli_spoolss_getprinterdriver(
-                               cli, mem_ctx, needed, NULL, hnd, level,
-                               env, version, ctr);
+       uint32_t server_major_version;
+       uint32_t server_minor_version;
 
+       /* getprinterdriver call */
+       result = rpccli_spoolss_getprinterdriver2(pipe_hnd, mem_ctx,
+                                                 hnd,
+                                                 env,
+                                                 level,
+                                                 0,
+                                                 version,
+                                                 2,
+                                                 info,
+                                                 &server_major_version,
+                                                 &server_minor_version);
        if (!W_ERROR_IS_OK(result)) {
-               DEBUG(1,("cannot get driver (for architecture: %s): %s\n", 
-                       env, dos_errstr(result)));
+               DEBUG(1,("cannot get driver (for architecture: %s): %s\n",
+                       env, win_errstr(result)));
                if (W_ERROR_V(result) != W_ERROR_V(WERR_UNKNOWN_PRINTER_DRIVER) &&
                    W_ERROR_V(result) != W_ERROR_V(WERR_INVALID_ENVIRONMENT)) {
-                       printf("cannot get driver: %s\n", dos_errstr(result));
+                       printf("cannot get driver: %s\n", win_errstr(result));
                }
-               return False;
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 
-static BOOL
-net_spoolss_addprinterdriver(struct cli_state *cli, 
+static bool net_spoolss_addprinterdriver(struct rpc_pipe_client *pipe_hnd,
                             TALLOC_CTX *mem_ctx, uint32 level,
-                            PRINTER_DRIVER_CTR *ctr)
+                            union spoolss_DriverInfo *info)
 {
        WERROR result;
+       NTSTATUS status;
+       struct spoolss_AddDriverInfoCtr info_ctr;
 
-       /* addprinterdriver call */
-       result = cli_spoolss_addprinterdriver(cli, mem_ctx, level, ctr);
+       info_ctr.level = level;
+
+       switch (level) {
+       case 2:
+               info_ctr.info.info2 = (struct spoolss_AddDriverInfo2 *)&info->info2;
+               break;
+       case 3:
+               info_ctr.info.info3 = (struct spoolss_AddDriverInfo3 *)&info->info3;
+               break;
+       default:
+               printf("unsupported info level: %d\n", level);
+               return false;
+       }
 
+       /* addprinterdriver call */
+       status = rpccli_spoolss_AddPrinterDriver(pipe_hnd, mem_ctx,
+                                                pipe_hnd->srv_name_slash,
+                                                &info_ctr,
+                                                &result);
        /* be more verbose */
        if (W_ERROR_V(result) == W_ERROR_V(WERR_ACCESS_DENIED)) {
                printf("You are not allowed to add drivers\n");
-               return False;
+               return false;
        }
        if (!W_ERROR_IS_OK(result)) {
-               printf("cannot add driver: %s\n", dos_errstr(result));
-               return False;
+               printf("cannot add driver: %s\n", win_errstr(result));
+               return false;
        }
 
-       return True;
+       return true;
 }
 
 /**
- * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr 
- * for a single printer or for all printers depending on argc/argv 
+ * abstraction function to get uint32 num_printers and PRINTER_INFO_CTR ctr
+ * for a single printer or for all printers depending on argc/argv
  **/
-static BOOL
-get_printer_info(struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                int level, int argc, const char **argv, 
-                uint32 *num_printers, PRINTER_INFO_CTR *ctr)
+
+static bool get_printer_info(struct rpc_pipe_client *pipe_hnd,
+                       TALLOC_CTX *mem_ctx,
+                       int level,
+                       int argc,
+                       const char **argv,
+                       uint32 *num_printers,
+                       PRINTER_INFO_CTR *ctr)
 {
 
-       char *sharename;
        POLICY_HND hnd;
+       union spoolss_PrinterInfo info;
 
        /* no arguments given, enumerate all printers */
        if (argc == 0) {
 
-               if (!net_spoolss_enum_printers(cli, mem_ctx, NULL, 
-                               PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED, 
-                               level, num_printers, ctr)) 
-                       return False;
+               if (!net_spoolss_enum_printers(pipe_hnd, mem_ctx, NULL,
+                               PRINTER_ENUM_LOCAL|PRINTER_ENUM_SHARED,
+                               level, num_printers, ctr))
+                       return false;
 
                goto out;
        }
 
+       /* FIXME GD */
+       return false;
 
        /* argument given, get a single printer by name */
-       sharename = strdup(argv[0]);
-
-       if (!net_spoolss_open_printer_ex(cli, mem_ctx, sharename,
-                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd)) 
-               return False;
-
-       if (!net_spoolss_getprinter(cli, mem_ctx, &hnd, level, ctr)) {
-               cli_spoolss_close_printer(cli, mem_ctx, &hnd);
-               return False;
+       if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, argv[0],
+                                        MAXIMUM_ALLOWED_ACCESS,
+                                        pipe_hnd->auth->user_name,
+                                        &hnd))
+               return false;
+
+       if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info)) {
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+               return false;
        }
 
-       cli_spoolss_close_printer(cli, mem_ctx, &hnd);
+       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
 
        *num_printers = 1;
 
 out:
        DEBUG(3,("got %d printers\n", *num_printers));
 
-       return True;
+       return true;
 
 }
 
-
-/** 
+/**
  * List print-queues (including local printers that are not shared)
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1190,43 +1137,56 @@ out:
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                   struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                   int argc, const char **argv)
+
+NTSTATUS rpc_printer_list_internals(struct net_context *c,
+                                       const DOM_SID *domain_sid,
+                                       const char *domain_name,
+                                       struct cli_state *cli,
+                                       struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc,
+                                       const char **argv)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
-       uint32 i, num_printers; 
+       uint32 i, num_printers;
        uint32 level = 2;
-       pstring printername, sharename;
+       char *printername, *sharename;
        PRINTER_INFO_CTR ctr;
 
        printf("listing printers\n");
 
-       if (!get_printer_info(cli, mem_ctx, level, argc, argv, &num_printers, &ctr))
+       if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr))
                return nt_status;
 
        for (i = 0; i < num_printers; i++) {
-
                /* do some initialization */
-               rpcstr_pull(printername, ctr.printers_2[i].printername.buffer, 
-                       sizeof(printername), -1, STR_TERMINATE);
-               rpcstr_pull(sharename, ctr.printers_2[i].sharename.buffer, 
-                       sizeof(sharename), -1, STR_TERMINATE);
-               
-               d_printf("printer %d: %s, shared as: %s\n", 
-                       i+1, printername, sharename);
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+
+               if (printername && sharename) {
+                       d_printf("printer %d: %s, shared as: %s\n",
+                               i+1, printername, sharename);
+               }
        }
 
        return NT_STATUS_OK;
 }
 
-
-/** 
- * List printer-drivers from a server 
+/**
+ * List printer-drivers from a server
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1236,41 +1196,45 @@ NTSTATUS rpc_printer_list_internals(const DOM_SID *domain_sid, const char *domai
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                          struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                          int argc, const char **argv)
+
+NTSTATUS rpc_printer_driver_list_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32 i;
-       uint32 level = 3; 
+       uint32 level = 3;
        PRINTER_DRIVER_CTR drv_ctr_enum;
        int d;
-       
-       ZERO_STRUCT(drv_ctr_enum);
 
+       ZERO_STRUCT(drv_ctr_enum);
 
        printf("listing printer-drivers\n");
 
         for (i=0; archi_table[i].long_archi!=NULL; i++) {
 
-               int num_drivers;
+               uint32 num_drivers;
 
                /* enum remote drivers */
-               if (!net_spoolss_enumprinterdrivers(cli, mem_ctx, level,
-                               archi_table[i].long_archi, 
+               if (!net_spoolss_enumprinterdrivers(pipe_hnd, mem_ctx, level,
+                               archi_table[i].long_archi,
                                &num_drivers, &drv_ctr_enum)) {
-                                                                               
                        nt_status = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
 
                if (num_drivers == 0) {
-                       d_printf ("no drivers found on server for architecture: [%s].\n", 
+                       d_printf ("no drivers found on server for architecture: [%s].\n",
                                archi_table[i].long_archi);
                        continue;
-               } 
-               
-               d_printf("got %d printer-drivers for architecture: [%s]\n", 
+               }
+
+               d_printf("got %d printer-drivers for architecture: [%s]\n",
                        num_drivers, archi_table[i].long_archi);
 
 
@@ -1279,7 +1243,7 @@ NTSTATUS rpc_printer_driver_list_internals(const DOM_SID *domain_sid, const char
                        display_print_driver_3(&(drv_ctr_enum.info3[d]));
                }
        }
-       
+
        nt_status = NT_STATUS_OK;
 
 done:
@@ -1287,13 +1251,255 @@ done:
 
 }
 
+/**
+ * Publish print-queues with args-wrapper
+ *
+ * @param cli A cli_state connected to the server.
+ * @param mem_ctx Talloc context, destoyed on compleation of the function.
+ * @param argc  Standard main() style argc
+ * @param argv  Standard main() style argv.  Initial components are already
+ *              stripped
+ * @param action
+ *
+ * @return Normal NTSTATUS return.
+ **/
+
+static NTSTATUS rpc_printer_publish_internals_args(struct rpc_pipe_client *pipe_hnd,
+                                       TALLOC_CTX *mem_ctx,
+                                       int argc,
+                                       const char **argv,
+                                       uint32 action)
+{
+       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
+       uint32 i, num_printers;
+       uint32 level = 7;
+       char *printername, *sharename;
+       PRINTER_INFO_CTR ctr;
+       union spoolss_PrinterInfo info;
+       struct spoolss_SetPrinterInfoCtr info_ctr;
+       struct spoolss_DevmodeContainer devmode_ctr;
+       struct sec_desc_buf secdesc_ctr;
+       POLICY_HND hnd;
+       WERROR result;
+       const char *action_str;
+
+       if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
+               return nt_status;
+
+       for (i = 0; i < num_printers; i++) {
+               /* do some initialization */
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       goto done;
+               }
+
+               /* open printer handle */
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                       PRINTER_ALL_ACCESS, pipe_hnd->auth->user_name, &hnd))
+                       goto done;
+
+               /* check for existing dst printer */
+               if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
+                       goto done;
+
+               /* check action and set string */
+               switch (action) {
+               case SPOOL_DS_PUBLISH:
+                       action_str = "published";
+                       break;
+               case SPOOL_DS_UPDATE:
+                       action_str = "updated";
+                       break;
+               case SPOOL_DS_UNPUBLISH:
+                       action_str = "unpublished";
+                       break;
+               default:
+                       action_str = "unknown action";
+                       printf("unkown action: %d\n", action);
+                       break;
+               }
+
+               info.info7.action = action;
+               info_ctr.level = 7;
+               info_ctr.info.info7 = (struct spoolss_SetPrinterInfo7 *)&info.info7;
+
+               ZERO_STRUCT(devmode_ctr);
+               ZERO_STRUCT(secdesc_ctr);
+
+               nt_status = rpccli_spoolss_SetPrinter(pipe_hnd, 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))) {
+                       printf("cannot set printer-info: %s\n", win_errstr(result));
+                       goto done;
+               }
+
+               printf("successfully %s printer %s in Active Directory\n", action_str, sharename);
+       }
+
+       nt_status = NT_STATUS_OK;
+
+done:
+       if (is_valid_policy_hnd(&hnd))
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+
+       return nt_status;
+}
+
+NTSTATUS rpc_printer_publish_publish_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
+{
+       return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_PUBLISH);
+}
+
+NTSTATUS rpc_printer_publish_unpublish_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
+{
+       return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UNPUBLISH);
+}
+
+NTSTATUS rpc_printer_publish_update_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
+{
+       return rpc_printer_publish_internals_args(pipe_hnd, mem_ctx, argc, argv, SPOOL_DS_UPDATE);
+}
+
+/**
+ * List print-queues w.r.t. their publishing state
+ *
+ * All parameters are provided by the run_rpc_command function, except for
+ * argc, argv which are passed through.
+ *
+ * @param c    A net_context structure
+ * @param domain_sid The domain sid aquired from the remote server
+ * @param cli A cli_state connected to the server.
+ * @param mem_ctx Talloc context, destoyed on compleation of the function.
+ * @param argc  Standard main() style argc
+ * @param argv  Standard main() style argv.  Initial components are already
+ *              stripped
+ *
+ * @return Normal NTSTATUS return.
+ **/
+
+NTSTATUS rpc_printer_publish_list_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
+{
+       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
+       uint32 i, num_printers;
+       uint32 level = 7;
+       char *printername, *sharename;
+       PRINTER_INFO_CTR ctr, ctr_pub;
+       union spoolss_PrinterInfo info;
+       POLICY_HND hnd;
+       int state;
+
+       if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr))
+               return nt_status;
+
+       for (i = 0; i < num_printers; i++) {
+               ZERO_STRUCT(ctr_pub);
+
+               /* do some initialization */
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       goto done;
+               }
+
+               /* open printer handle */
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                       PRINTER_ALL_ACCESS, cli->user_name, &hnd))
+                       goto done;
+
+               /* check for existing dst printer */
+               if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd, level, &info))
+                       goto done;
+
+               if (!info.info7.guid) {
+                       goto done;
+               }
+               state = info.info7.action;
+               switch (state) {
+                       case SPOOL_DS_PUBLISH:
+                               printf("printer [%s] is published", sharename);
+                               if (c->opt_verbose)
+                                       printf(", guid: %s", info.info7.guid);
+                               printf("\n");
+                               break;
+                       case SPOOL_DS_UNPUBLISH:
+                               printf("printer [%s] is unpublished\n", sharename);
+                               break;
+                       case SPOOL_DS_UPDATE:
+                               printf("printer [%s] is currently updating\n", sharename);
+                               break;
+                       default:
+                               printf("unkown state: %d\n", state);
+                               break;
+               }
+       }
+
+       nt_status = NT_STATUS_OK;
+
+done:
+       if (is_valid_policy_hnd(&hnd))
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd, NULL);
+
+       return nt_status;
+}
+
+/**
  * Migrate Printer-ACLs from a source server to the destination server
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1303,37 +1509,43 @@ done:
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                               struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                               int argc, const char **argv)
+
+NTSTATUS rpc_printer_migrate_security_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
 {
-       /* TODO: what now, info2 or info3 ? 
+       /* TODO: what now, info2 or info3 ?
           convince jerry that we should add clientside setacls level 3 at least
        */
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32 i = 0;
        uint32 num_printers;
        uint32 level = 2;
-       pstring printername = "", sharename = "";
-       BOOL got_hnd_src = False;
-       BOOL got_hnd_dst = False;
-       BOOL got_dst_spoolss_pipe = False;
+       char *printername, *sharename;
+       struct rpc_pipe_client *pipe_hnd_dst = NULL;
        POLICY_HND hnd_src, hnd_dst;
-       PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
+       PRINTER_INFO_CTR ctr_src, ctr_enum;
        struct cli_state *cli_dst = NULL;
+       union spoolss_PrinterInfo info_src, info_dst;
 
        ZERO_STRUCT(ctr_src);
 
        DEBUG(3,("copying printer ACLs\n"));
 
        /* connect destination PI_SPOOLSS */
-       nt_status = connect_pipe(&cli_dst, PI_SPOOLSS, &got_dst_spoolss_pipe);
+       nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
+                                    &syntax_spoolss);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
 
 
        /* enum source printers */
-       if (!get_printer_info(cli, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
+       if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
                nt_status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
@@ -1342,107 +1554,108 @@ NTSTATUS rpc_printer_migrate_security_internals(const DOM_SID *domain_sid, const
                printf ("no printers found on server.\n");
                nt_status = NT_STATUS_OK;
                goto done;
-       } 
-       
+       }
 
        /* do something for all printers */
        for (i = 0; i < num_printers; i++) {
-
                /* do some initialization */
-               rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer, 
-                       sizeof(printername), -1, STR_TERMINATE);
-               rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer, 
-                       sizeof(sharename), -1, STR_TERMINATE);
-               /* we can reset NT_STATUS here because we do not 
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr_enum.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr_enum.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       nt_status = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
+
+               /* we can reset NT_STATUS here because we do not
                   get any real NT_STATUS-codes anymore from now on */
                nt_status = NT_STATUS_UNSUCCESSFUL;
-               
-               d_printf("migrating printer ACLs for:     [%s] / [%s]\n", 
+
+               d_printf("migrating printer ACLs for:     [%s] / [%s]\n",
                        printername, sharename);
 
-               /* according to msdn you have specify these access-rights 
+               /* according to msdn you have specify these access-rights
                   to see the security descriptor
                        - READ_CONTROL (DACL)
                        - ACCESS_SYSTEM_SECURITY (SACL)
                */
 
                /* open src printer handle */
-               if (!net_spoolss_open_printer_ex(cli, mem_ctx, sharename,
-                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
                        goto done;
 
-               got_hnd_src = True;
-
-
                /* open dst printer handle */
-               if (!net_spoolss_open_printer_ex(cli_dst, mem_ctx, sharename,
-                       PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
+                       PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
                        goto done;
 
-               got_hnd_dst = True;
-
-
                /* check for existing dst printer */
-               if (!net_spoolss_getprinter(cli_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) 
+               if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
                        goto done;
 
                /* check for existing src printer */
-               if (!net_spoolss_getprinter(cli, mem_ctx, &hnd_src, 3, &ctr_src)) 
+               if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, 3, &info_src))
                        goto done;
 
-
                /* Copy Security Descriptor */
 
                /* copy secdesc (info level 2) */
-               ctr_dst.printers_2->devmode = NULL; 
-               ctr_dst.printers_2->secdesc = dup_sec_desc(mem_ctx, ctr_src.printers_3->secdesc);
+               info_dst.info2.devmode = NULL;
+               info_dst.info2.secdesc = dup_sec_desc(mem_ctx, info_src.info3.secdesc);
 
-               if (opt_verbose)
-                       display_sec_desc(ctr_dst.printers_2->secdesc);
-               
-               if (!net_spoolss_setprinter(cli_dst, mem_ctx, &hnd_dst, 2, &ctr_dst)) 
+               if (c->opt_verbose)
+                       display_sec_desc(info_dst.info2.secdesc);
+
+               if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
                        goto done;
-               
+
                DEBUGADD(1,("\tSetPrinter of SECDESC succeeded\n"));
 
 
                /* close printer handles here */
-               if (got_hnd_src) {
-                       cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
-                       got_hnd_src = False;
+               if (is_valid_policy_hnd(&hnd_src)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
                }
 
-               if (got_hnd_dst) {
-                       cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
-                       got_hnd_dst = False;
+               if (is_valid_policy_hnd(&hnd_dst)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
                }
 
        }
-       
+
        nt_status = NT_STATUS_OK;
 
 done:
 
-       if (got_hnd_src)
-               cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
+       if (is_valid_policy_hnd(&hnd_src)) {
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
+       }
 
-       if (got_hnd_dst)
-               cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
+       if (is_valid_policy_hnd(&hnd_dst)) {
+               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
+       }
 
-       if (got_dst_spoolss_pipe) {
-               cli_nt_session_close(cli_dst);
+       if (cli_dst) {
                cli_shutdown(cli_dst);
        }
        return nt_status;
 }
 
-
-/** 
+/**
  * Migrate printer-forms from a src server to the dst server
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1452,37 +1665,42 @@ done:
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                            struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                            int argc, const char **argv)
+
+NTSTATUS rpc_printer_migrate_forms_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        WERROR result;
        uint32 i, f;
        uint32 num_printers;
        uint32 level = 1;
-       pstring printername = "", sharename = "";
-       BOOL got_hnd_src = False;
-       BOOL got_hnd_dst = False;
-       BOOL got_dst_spoolss_pipe = False;
+       char *printername, *sharename;
+       struct rpc_pipe_client *pipe_hnd_dst = NULL;
        POLICY_HND hnd_src, hnd_dst;
-       PRINTER_INFO_CTR ctr_enum, ctr_dst;
+       PRINTER_INFO_CTR ctr_enum;
+       union spoolss_PrinterInfo info_dst;
        uint32 num_forms;
        FORM_1 *forms;
        struct cli_state *cli_dst = NULL;
-       
+
        ZERO_STRUCT(ctr_enum);
 
        DEBUG(3,("copying forms\n"));
-       
+
        /* connect destination PI_SPOOLSS */
-       nt_status = connect_pipe(&cli_dst, PI_SPOOLSS, &got_dst_spoolss_pipe);
+       nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
+                                    &syntax_spoolss);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
-       
 
        /* enum src printers */
-       if (!get_printer_info(cli, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
+       if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
                nt_status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
@@ -1491,47 +1709,49 @@ NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid, const ch
                printf ("no printers found on server.\n");
                nt_status = NT_STATUS_OK;
                goto done;
-       } 
-       
+       }
 
        /* do something for all printers */
        for (i = 0; i < num_printers; i++) {
-
                /* do some initialization */
-               rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer, 
-                       sizeof(printername), -1, STR_TERMINATE);
-               rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer, 
-                       sizeof(sharename), -1, STR_TERMINATE);
-               /* we can reset NT_STATUS here because we do not 
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr_enum.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr_enum.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       nt_status = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
+               /* we can reset NT_STATUS here because we do not
                   get any real NT_STATUS-codes anymore from now on */
                nt_status = NT_STATUS_UNSUCCESSFUL;
-               
-               d_printf("migrating printer forms for:    [%s] / [%s]\n", 
+
+               d_printf("migrating printer forms for:    [%s] / [%s]\n",
                        printername, sharename);
 
 
                /* open src printer handle */
-               if (!net_spoolss_open_printer_ex(cli, mem_ctx, sharename,
-                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
                        goto done;
 
-               got_hnd_src = True;
-
-
                /* open dst printer handle */
-               if (!net_spoolss_open_printer_ex(cli_dst, mem_ctx, sharename,
-                       PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
+                       PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
                        goto done;
 
-               got_hnd_dst = True;
-
-
                /* check for existing dst printer */
-               if (!net_spoolss_getprinter(cli_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) 
+               if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst))
                        goto done;
 
                /* finally migrate forms */
-               if (!net_spoolss_enumforms(cli, mem_ctx, &hnd_src, level, &num_forms, &forms))
+               if (!net_spoolss_enumforms(pipe_hnd, mem_ctx, &hnd_src, level, &num_forms, &forms))
                        goto done;
 
                DEBUG(1,("got %d forms for printer\n", num_forms));
@@ -1539,10 +1759,12 @@ NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid, const ch
 
                for (f = 0; f < num_forms; f++) {
 
-                       FORM form;
+                       union spoolss_AddFormInfo info;
+                       struct spoolss_AddFormInfo1 info1;
                        fstring form_name;
-                       
-                       /* only migrate FORM_PRINTER types, according to jerry 
+                       NTSTATUS status;
+
+                       /* only migrate FORM_PRINTER types, according to jerry
                           FORM_BUILTIN-types are hard-coded in samba */
                        if (forms[f].flag != FORM_PRINTER)
                                continue;
@@ -1551,44 +1773,46 @@ NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid, const ch
                                rpcstr_pull(form_name, forms[f].name.buffer,
                                        sizeof(form_name), -1, STR_TERMINATE);
 
-                       if (opt_verbose)
-                               d_printf("\tmigrating form # %d [%s] of type [%d]\n", 
+                       if (c->opt_verbose)
+                               d_printf("\tmigrating form # %d [%s] of type [%d]\n",
                                        f, form_name, forms[f].flag);
 
                        /* is there a more elegant way to do that ? */
-                       form.flags      = FORM_PRINTER;
-                       form.size_x     = forms[f].width;
-                       form.size_y     = forms[f].length;
-                       form.left       = forms[f].left;
-                       form.top        = forms[f].top;
-                       form.right      = forms[f].right;
-                       form.bottom     = forms[f].bottom;
-                       
-                       init_unistr2(&form.name, form_name, UNI_STR_TERMINATE);
-
-                       /* FIXME: there might be something wrong with samba's 
+                       info1.flags             = FORM_PRINTER;
+                       info1.size.width        = forms[f].width;
+                       info1.size.height       = forms[f].length;
+                       info1.area.left         = forms[f].left;
+                       info1.area.top          = forms[f].top;
+                       info1.area.right        = forms[f].right;
+                       info1.area.bottom       = forms[f].bottom;
+                       info1.form_name         = form_name;
+
+                       info.info1 = &info1;
+
+                       /* FIXME: there might be something wrong with samba's
                           builtin-forms */
-                       result = cli_spoolss_addform(cli_dst, mem_ctx, 
-                               &hnd_dst, 1, &form);
+                       status = rpccli_spoolss_AddForm(pipe_hnd_dst, mem_ctx,
+                                                       &hnd_dst,
+                                                       1,
+                                                       info,
+                                                       &result);
                        if (!W_ERROR_IS_OK(result)) {
-                               d_printf("\tAddForm form %d: [%s] refused.\n", 
+                               d_printf("\tAddForm form %d: [%s] refused.\n",
                                        f, form_name);
                                continue;
                        }
-       
+
                        DEBUGADD(1,("\tAddForm of [%s] succeeded\n", form_name));
                }
 
 
                /* close printer handles here */
-               if (got_hnd_src) {
-                       cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
-                       got_hnd_src = False;
+               if (is_valid_policy_hnd(&hnd_src)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
                }
 
-               if (got_hnd_dst) {
-                       cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
-                       got_hnd_dst = False;
+               if (is_valid_policy_hnd(&hnd_dst)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
                }
        }
 
@@ -1596,27 +1820,25 @@ NTSTATUS rpc_printer_migrate_forms_internals(const DOM_SID *domain_sid, const ch
 
 done:
 
-       if (got_hnd_src)
-               cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
+       if (is_valid_policy_hnd(&hnd_src))
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
 
-       if (got_hnd_dst)
-               cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
+       if (is_valid_policy_hnd(&hnd_dst))
+               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
 
-       if (got_dst_spoolss_pipe) {
-               cli_nt_session_close(cli_dst);
+       if (cli_dst) {
                cli_shutdown(cli_dst);
        }
        return nt_status;
-
 }
 
-
-/** 
+/**
  * Migrate printer-drivers from a src server to the dst server
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1626,124 +1848,131 @@ done:
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                              struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                              int argc, const char **argv)
+
+NTSTATUS rpc_printer_migrate_drivers_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
 {
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32 i, p;
        uint32 num_printers;
-       uint32 level = 3; 
-       pstring printername = "", sharename = "";
-       BOOL got_hnd_src = False;
-       BOOL got_hnd_dst = False;
-       BOOL got_dst_spoolss_pipe = False;
-       BOOL got_src_driver_share = False;
-       BOOL got_dst_driver_share = False;
+       uint32 level = 3;
+       char *printername, *sharename;
+       bool got_src_driver_share = false;
+       bool got_dst_driver_share = false;
+       struct rpc_pipe_client *pipe_hnd_dst = NULL;
        POLICY_HND hnd_src, hnd_dst;
-       PRINTER_DRIVER_CTR drv_ctr_src, drv_ctr_dst;
+       union spoolss_DriverInfo drv_info_src;
        PRINTER_INFO_CTR info_ctr_enum, info_ctr_dst;
+       union spoolss_PrinterInfo info_dst;
        struct cli_state *cli_dst = NULL;
        struct cli_state *cli_share_src = NULL;
        struct cli_state *cli_share_dst = NULL;
-       fstring drivername = "";
-       
-       ZERO_STRUCT(drv_ctr_src);
-       ZERO_STRUCT(drv_ctr_dst);
+       const char *drivername = NULL;
+
        ZERO_STRUCT(info_ctr_enum);
        ZERO_STRUCT(info_ctr_dst);
 
-
        DEBUG(3,("copying printer-drivers\n"));
 
-       nt_status = connect_pipe(&cli_dst, PI_SPOOLSS, &got_dst_spoolss_pipe);
+       nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
+                                    &syntax_spoolss);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
-       
 
        /* open print$-share on the src server */
-       nt_status = connect_to_service(&cli_share_src, &cli->dest_ip, 
+       nt_status = connect_to_service(c, &cli_share_src, &cli->dest_ss,
                        cli->desthost, "print$", "A:");
-       if (!NT_STATUS_IS_OK(nt_status)) 
-               return nt_status;
+       if (!NT_STATUS_IS_OK(nt_status))
+               goto done;
 
-       got_src_driver_share = True;
+       got_src_driver_share = true;
 
 
        /* open print$-share on the dst server */
-       nt_status = connect_to_service(&cli_share_dst, &cli_dst->dest_ip, 
+       nt_status = connect_to_service(c, &cli_share_dst, &cli_dst->dest_ss,
                        cli_dst->desthost, "print$", "A:");
-       if (!NT_STATUS_IS_OK(nt_status)) 
+       if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
 
-       got_dst_driver_share = True;
+       got_dst_driver_share = true;
 
 
        /* enum src printers */
-       if (!get_printer_info(cli, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
+       if (!get_printer_info(pipe_hnd, mem_ctx, 2, argc, argv, &num_printers, &info_ctr_enum)) {
                nt_status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
 
-       if (!num_printers) {
+       if (num_printers == 0) {
                printf ("no printers found on server.\n");
                nt_status = NT_STATUS_OK;
                goto done;
-       } 
-       
+       }
+
 
        /* do something for all printers */
        for (p = 0; p < num_printers; p++) {
-
                /* do some initialization */
-               rpcstr_pull(printername, info_ctr_enum.printers_2[p].printername.buffer, 
-                       sizeof(printername), -1, STR_TERMINATE);
-               rpcstr_pull(sharename, info_ctr_enum.printers_2[p].sharename.buffer, 
-                       sizeof(sharename), -1, STR_TERMINATE);
-               /* we can reset NT_STATUS here because we do not 
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               info_ctr_enum.printers_2[p].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               info_ctr_enum.printers_2[p].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       nt_status = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
+
+               /* we can reset NT_STATUS here because we do not
                   get any real NT_STATUS-codes anymore from now on */
                nt_status = NT_STATUS_UNSUCCESSFUL;
 
-               d_printf("migrating printer driver for:   [%s] / [%s]\n", 
+               d_printf("migrating printer driver for:   [%s] / [%s]\n",
                        printername, sharename);
 
                /* open dst printer handle */
-               if (!net_spoolss_open_printer_ex(cli_dst, mem_ctx, sharename,
-                       PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
+                       PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst))
                        goto done;
-                       
-               got_hnd_dst = True;
 
                /* check for existing dst printer */
-               if (!net_spoolss_getprinter(cli_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) 
+               if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst))
                        goto done;
 
 
                /* open src printer handle */
-               if (!net_spoolss_open_printer_ex(cli, mem_ctx, sharename,
-                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                                                MAXIMUM_ALLOWED_ACCESS,
+                                                pipe_hnd->auth->user_name,
+                                                &hnd_src))
                        goto done;
 
-               got_hnd_src = True;
-
-
                /* in a first step call getdriver for each shared printer (per arch)
                   to get a list of all files that have to be copied */
-                  
+
                for (i=0; archi_table[i].long_archi!=NULL; i++) {
 
                        /* getdriver src */
-                       if (!net_spoolss_getprinterdriver(cli, mem_ctx, &hnd_src, 
-                                       level, archi_table[i].long_archi, 
-                                       archi_table[i].version, &drv_ctr_src)) 
+                       if (!net_spoolss_getprinterdriver(pipe_hnd, mem_ctx, &hnd_src,
+                                       level, archi_table[i].long_archi,
+                                       archi_table[i].version, &drv_info_src))
                                continue;
 
-                       rpcstr_pull(drivername, drv_ctr_src.info3->name.buffer, 
-                                       sizeof(drivername), -1, STR_TERMINATE);
-
-                       if (opt_verbose)
-                               display_print_driver_3(drv_ctr_src.info3);
+                       drivername = drv_info_src.info3.driver_name;
 
+                       if (c->opt_verbose)
+                               display_print_driver3(&drv_info_src.info3);
 
                        /* check arch dir */
                        nt_status = check_arch_dir(cli_share_dst, archi_table[i].short_archi);
@@ -1752,45 +1981,49 @@ NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid, const
 
 
                        /* copy driver-files */
-                       nt_status = copy_print_driver_3(mem_ctx, cli_share_src, cli_share_dst, 
-                                                       archi_table[i].short_archi, 
-                                                       drv_ctr_src.info3);
+                       nt_status = copy_print_driver_3(c, mem_ctx, cli_share_src, cli_share_dst,
+                                                       archi_table[i].short_archi,
+                                                       &drv_info_src.info3);
                        if (!NT_STATUS_IS_OK(nt_status))
                                goto done;
 
 
                        /* adddriver dst */
-                       if (!net_spoolss_addprinterdriver(cli_dst, mem_ctx, level, &drv_ctr_src)) { 
+                       if (!net_spoolss_addprinterdriver(pipe_hnd_dst, mem_ctx, level, &drv_info_src)) {
                                nt_status = NT_STATUS_UNSUCCESSFUL;
                                goto done;
                        }
-                               
-                       DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n", 
+
+                       DEBUGADD(1,("Sucessfully added driver [%s] for printer [%s]\n",
                                drivername, printername));
 
                }
 
+               if (strlen(drivername) == 0) {
+                       DEBUGADD(1,("Did not get driver for printer %s\n",
+                                   printername));
+                       goto done;
+               }
+
                /* setdriver dst */
-               init_unistr(&info_ctr_dst.printers_2->drivername, drivername);
-               
-               if (!net_spoolss_setprinter(cli_dst, mem_ctx, &hnd_dst, 2, &info_ctr_dst)) { 
+               info_dst.info2.drivername = drivername;
+
+               if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 2, &info_dst)) {
                        nt_status = NT_STATUS_UNSUCCESSFUL;
                        goto done;
                }
 
-               DEBUGADD(1,("Sucessfully set driver %s for printer %s\n", 
+               DEBUGADD(1,("Sucessfully set driver %s for printer %s\n",
                        drivername, printername));
 
                /* close dst */
-               if (got_hnd_dst) {
-                       cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
-                       got_hnd_dst = False;
+               if (is_valid_policy_hnd(&hnd_dst)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
                }
 
                /* close src */
-               if (got_hnd_src) {
-                       cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
-                       got_hnd_src = False;
+               if (is_valid_policy_hnd(&hnd_src)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
                }
        }
 
@@ -1798,14 +2031,13 @@ NTSTATUS rpc_printer_migrate_drivers_internals(const DOM_SID *domain_sid, const
 
 done:
 
-       if (got_hnd_src)
-               cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
+       if (is_valid_policy_hnd(&hnd_src))
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
 
-       if (got_hnd_dst)
-               cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
+       if (is_valid_policy_hnd(&hnd_dst))
+               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
 
-       if (got_dst_spoolss_pipe) {
-               cli_nt_session_close(cli_dst);
+       if (cli_dst) {
                cli_shutdown(cli_dst);
        }
 
@@ -1819,14 +2051,14 @@ done:
 
 }
 
-
-/** 
+/**
  * Migrate printer-queues from a src to the dst server
  * (requires a working "addprinter command" to be installed for the local smbd)
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1836,32 +2068,38 @@ done:
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                               struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                               int argc, const char **argv)
+
+NTSTATUS rpc_printer_migrate_printers_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
 {
        WERROR result;
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
        uint32 i = 0, num_printers;
        uint32 level = 2;
-       PRINTER_INFO_CTR ctr_src, ctr_dst, ctr_enum;
+       PRINTER_INFO_CTR ctr_enum;
+       union spoolss_PrinterInfo info_dst, info_src;
        struct cli_state *cli_dst = NULL;
        POLICY_HND hnd_dst, hnd_src;
-       pstring printername, sharename;
-       BOOL got_hnd_src = False;
-       BOOL got_hnd_dst = False;
-       BOOL got_dst_spoolss_pipe = False;
+       char *printername, *sharename;
+       struct rpc_pipe_client *pipe_hnd_dst = NULL;
+       struct spoolss_SetPrinterInfoCtr info_ctr;
 
        DEBUG(3,("copying printers\n"));
 
        /* connect destination PI_SPOOLSS */
-       nt_status = connect_pipe(&cli_dst, PI_SPOOLSS, &got_dst_spoolss_pipe);
+       nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
+                                    &syntax_spoolss);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
 
-
        /* enum printers */
-       if (!get_printer_info(cli, mem_ctx, 2, argc, argv, &num_printers, &ctr_enum)) {
+       if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
                nt_status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
@@ -1870,119 +2108,116 @@ NTSTATUS rpc_printer_migrate_printers_internals(const DOM_SID *domain_sid, const
                printf ("no printers found on server.\n");
                nt_status = NT_STATUS_OK;
                goto done;
-       } 
-       
+       }
 
        /* do something for all printers */
        for (i = 0; i < num_printers; i++) {
-
                /* do some initialization */
-               rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer, 
-                       sizeof(printername), -1, STR_TERMINATE);
-               rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer, 
-                       sizeof(sharename), -1, STR_TERMINATE);
-               /* we can reset NT_STATUS here because we do not 
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr_enum.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr_enum.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       nt_status = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
+               /* we can reset NT_STATUS here because we do not
                   get any real NT_STATUS-codes anymore from now on */
                nt_status = NT_STATUS_UNSUCCESSFUL;
-               
-               d_printf("migrating printer queue for:    [%s] / [%s]\n", 
-                       printername, sharename);
 
+               d_printf("migrating printer queue for:    [%s] / [%s]\n",
+                       printername, sharename);
 
                /* open dst printer handle */
-               if (!net_spoolss_open_printer_ex(cli_dst, mem_ctx, sharename, 
+               if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
                        PRINTER_ALL_ACCESS, cli->user_name, &hnd_dst)) {
-                       
+
                        DEBUG(1,("could not open printer: %s\n", sharename));
-               } else {
-                       got_hnd_dst = True;
                }
 
-
                /* check for existing dst printer */
-               if (!net_spoolss_getprinter(cli_dst, mem_ctx, &hnd_dst, level, &ctr_dst)) {
+               if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, level, &info_dst)) {
                        printf ("could not get printer, creating printer.\n");
                } else {
                        DEBUG(1,("printer already exists: %s\n", sharename));
-                       /* close printer handles here */
-                       if (got_hnd_src) {
-                               cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
-                               got_hnd_src = False;
-                       }
-
-                       if (got_hnd_dst) {
-                               cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
-                               got_hnd_dst = False;
+                       /* 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);
                        }
                        continue;
                }
 
-
-               /* now get again src printer ctr via getprinter, 
+               /* now get again src printer ctr via getprinter,
                   we first need a handle for that */
 
                /* open src printer handle */
-               if (!net_spoolss_open_printer_ex(cli, mem_ctx, sharename,
-                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
                        goto done;
 
-               got_hnd_src = True;
-
                /* getprinter on the src server */
-               if (!net_spoolss_getprinter(cli, mem_ctx, &hnd_src, level, &ctr_src)) 
+               if (!net_spoolss_getprinter(pipe_hnd, mem_ctx, &hnd_src, level, &info_src))
                        goto done;
 
-
-               /* copy each src printer to a dst printer 1:1, 
+               /* copy each src printer to a dst printer 1:1,
                   maybe some values have to be changed though */
                d_printf("creating printer: %s\n", printername);
-               result = cli_spoolss_addprinterex (cli_dst, mem_ctx, level, &ctr_src);
+
+               info_ctr.level = level;
+               info_ctr.info.info2 = (struct spoolss_SetPrinterInfo2 *)&info_src.info2;
+
+               result = rpccli_spoolss_addprinterex(pipe_hnd_dst,
+                                                    mem_ctx,
+                                                    &info_ctr);
 
                if (W_ERROR_IS_OK(result))
                        d_printf ("printer [%s] successfully added.\n", printername);
-               else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS)) 
-                       d_printf ("printer [%s] already exists.\n", printername);
+               else if (W_ERROR_V(result) == W_ERROR_V(WERR_PRINTER_ALREADY_EXISTS))
+                       d_fprintf (stderr, "printer [%s] already exists.\n", printername);
                else {
-                       printf ("could not create printer\n");
+                       d_fprintf (stderr, "could not create printer [%s]\n", printername);
                        goto done;
                }
 
                /* close printer handles here */
-               if (got_hnd_src) {
-                       cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
-                       got_hnd_src = False;
+               if (is_valid_policy_hnd(&hnd_src)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
                }
 
-               if (got_hnd_dst) {
-                       cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
-                       got_hnd_dst = False;
+               if (is_valid_policy_hnd(&hnd_dst)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
                }
        }
 
        nt_status = NT_STATUS_OK;
 
 done:
-       if (got_hnd_src)
-               cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
+       if (is_valid_policy_hnd(&hnd_src))
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
 
-       if (got_hnd_dst)
-               cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
+       if (is_valid_policy_hnd(&hnd_dst))
+               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
 
-       if (got_dst_spoolss_pipe) {
-               cli_nt_session_close(cli_dst);
+       if (cli_dst) {
                cli_shutdown(cli_dst);
        }
        return nt_status;
 }
 
-
-/** 
+/**
  * Migrate Printer-Settings from a src server to the dst server
  * (for this to work, printers and drivers already have to be migrated earlier)
  *
  * All parameters are provided by the run_rpc_command function, except for
- * argc, argv which are passed through. 
+ * argc, argv which are passed through.
  *
+ * @param c    A net_context structure
  * @param domain_sid The domain sid aquired from the remote server
  * @param cli A cli_state connected to the server.
  * @param mem_ctx Talloc context, destoyed on compleation of the function.
@@ -1992,9 +2227,15 @@ done:
  *
  * @return Normal NTSTATUS return.
  **/
-NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid, const char *domain_name, 
-                                               struct cli_state *cli, TALLOC_CTX *mem_ctx, 
-                                               int argc, const char **argv)
+
+NTSTATUS rpc_printer_migrate_settings_internals(struct net_context *c,
+                                               const DOM_SID *domain_sid,
+                                               const char *domain_name,
+                                               struct cli_state *cli,
+                                               struct rpc_pipe_client *pipe_hnd,
+                                               TALLOC_CTX *mem_ctx,
+                                               int argc,
+                                               const char **argv)
 {
 
        /* FIXME: Here the nightmare begins */
@@ -2004,32 +2245,32 @@ NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid, const
        uint32 i = 0, p = 0, j = 0;
        uint32 num_printers, val_needed, data_needed;
        uint32 level = 2;
-       pstring printername = "", sharename = "";
-       BOOL got_hnd_src = False;
-       BOOL got_hnd_dst = False;
-       BOOL got_dst_spoolss_pipe = False;
+       char *printername, *sharename;
+       struct rpc_pipe_client *pipe_hnd_dst = NULL;
        POLICY_HND hnd_src, hnd_dst;
-       PRINTER_INFO_CTR ctr_enum, ctr_dst, ctr_dst_publish;
-       REGVAL_CTR reg_ctr;
+       PRINTER_INFO_CTR ctr_enum;
+       union spoolss_PrinterInfo info_dst_publish, info_dst;
+       REGVAL_CTR *reg_ctr;
        struct cli_state *cli_dst = NULL;
        char *devicename = NULL, *unc_name = NULL, *url = NULL;
-       fstring longname;
+       const char *longname;
 
-       const char *keyname = NULL;
        uint16 *keylist = NULL, *curkey;
 
        ZERO_STRUCT(ctr_enum);
+       /* FIXME GD */
+       ZERO_STRUCT(info_dst_publish);
 
        DEBUG(3,("copying printer settings\n"));
 
        /* connect destination PI_SPOOLSS */
-       nt_status = connect_pipe(&cli_dst, PI_SPOOLSS, &got_dst_spoolss_pipe);
+       nt_status = connect_dst_pipe(c, &cli_dst, &pipe_hnd_dst,
+                                    &syntax_spoolss);
        if (!NT_STATUS_IS_OK(nt_status))
                return nt_status;
 
-
        /* enum src printers */
-       if (!get_printer_info(cli, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
+       if (!get_printer_info(pipe_hnd, mem_ctx, level, argc, argv, &num_printers, &ctr_enum)) {
                nt_status = NT_STATUS_UNSUCCESSFUL;
                goto done;
        }
@@ -2038,202 +2279,216 @@ NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid, const
                printf ("no printers found on server.\n");
                nt_status = NT_STATUS_OK;
                goto done;
-       } 
-       
+       }
+
 
        /* needed for dns-strings in regkeys */
-       get_mydnsfullname(longname);
-       
+       longname = get_mydnsfullname();
+       if (!longname) {
+               nt_status = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
        /* do something for all printers */
        for (i = 0; i < num_printers; i++) {
-
                /* do some initialization */
-               rpcstr_pull(printername, ctr_enum.printers_2[i].printername.buffer, 
-                       sizeof(printername), -1, STR_TERMINATE);
-               rpcstr_pull(sharename, ctr_enum.printers_2[i].sharename.buffer, 
-                       sizeof(sharename), -1, STR_TERMINATE);
-               keyname = "";
-               /* we can reset NT_STATUS here because we do not 
+               rpcstr_pull_talloc(mem_ctx,
+                               &printername,
+                               ctr_enum.printers_2[i].printername.buffer,
+                               -1,
+                               STR_TERMINATE);
+               rpcstr_pull_talloc(mem_ctx,
+                               &sharename,
+                               ctr_enum.printers_2[i].sharename.buffer,
+                               -1,
+                               STR_TERMINATE);
+               if (!printername || !sharename) {
+                       nt_status = NT_STATUS_UNSUCCESSFUL;
+                       goto done;
+               }
+               /* we can reset NT_STATUS here because we do not
                   get any real NT_STATUS-codes anymore from now on */
                nt_status = NT_STATUS_UNSUCCESSFUL;
-               
-               d_printf("migrating printer settings for: [%s] / [%s]\n", 
+
+               d_printf("migrating printer settings for: [%s] / [%s]\n",
                        printername, sharename);
 
 
                /* open src printer handle */
-               if (!net_spoolss_open_printer_ex(cli, mem_ctx, sharename,
-                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd, mem_ctx, sharename,
+                       MAXIMUM_ALLOWED_ACCESS, cli->user_name, &hnd_src))
                        goto done;
 
-               got_hnd_src = True;
-
-
                /* open dst printer handle */
-               if (!net_spoolss_open_printer_ex(cli_dst, mem_ctx, sharename,
-                       PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst)) 
+               if (!net_spoolss_open_printer_ex(pipe_hnd_dst, mem_ctx, sharename,
+                       PRINTER_ALL_ACCESS, cli_dst->user_name, &hnd_dst))
                        goto done;
 
-               got_hnd_dst = True;
-
-
                /* check for existing dst printer */
-               if (!net_spoolss_getprinter(cli_dst, mem_ctx, &hnd_dst, 
-                               level, &ctr_dst)) 
+               if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
+                               level, &info_dst))
                        goto done;
 
+#if 0 /* FIXME GD */
 
-               /* STEP 1: COPY DEVICE-MODE and other 
+               /* STEP 1: COPY DEVICE-MODE and other
                           PRINTER_INFO_2-attributes
                */
 
-               ctr_dst.printers_2 = &ctr_enum.printers_2[i];
+               info_dst.info2 = &ctr_enum.printers_2[i];
 
-               /* why is the port always disconnected when the printer 
+               /* why is the port always disconnected when the printer
                   is correctly installed (incl. driver ???) */
-               init_unistr( &ctr_dst.printers_2->portname, SAMBA_PRINTER_PORT_NAME);
+               info_dst.info2.portname = SAMBA_PRINTER_PORT_NAME;
 
-               /* check if printer is published */ 
+               /* check if printer is published */
                if (ctr_enum.printers_2[i].attributes & PRINTER_ATTRIBUTE_PUBLISHED) {
 
                        /* check for existing dst printer */
-                       if (!net_spoolss_getprinter(cli_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish))
+                       if (!net_spoolss_getprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish))
                                goto done;
 
-                       ctr_dst_publish.printers_7->action = SPOOL_DS_PUBLISH;
+                       info_dst_publish.info7.action = SPOOL_DS_PUBLISH;
 
-                       /* ignore False from setprinter due to WERR_IO_PENDING */
-                       net_spoolss_setprinter(cli_dst, mem_ctx, &hnd_dst, 7, &ctr_dst_publish);
+                       /* ignore false from setprinter due to WERR_IO_PENDING */
+                       net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst, 7, &info_dst_publish);
 
                        DEBUG(3,("republished printer\n"));
                }
 
-               /* copy devmode (info level 2) */
-               ctr_dst.printers_2->devmode = talloc_memdup(mem_ctx, 
-                       ctr_enum.printers_2[i].devmode, sizeof(DEVICEMODE));
-
-               /* do not copy security descriptor (we have another command for that) */
-               ctr_dst.printers_2->secdesc = NULL;
+               if (ctr_enum.printers_2[i].devmode != NULL) {
 
+                       /* copy devmode (info level 2) */
+                       info_dst.info2.devmode = (DEVICEMODE *)
+                               TALLOC_MEMDUP(mem_ctx,
+                                             ctr_enum.printers_2[i].devmode,
+                                             sizeof(DEVICEMODE));
 
-               /* devmode->devicename is possibly broken at the moment for all 
-                  strlen(longprinternames) > MAXDEVICENAME (that is 32 chars) 
-                  this fires up thousands of safe_strncpy-debug0-messages 
-                  on my test-servers
-                  TODO: tell jerry, jra, etc. again.
-               */
+                       /* do not copy security descriptor (we have another
+                        * command for that) */
+                       info_dst.info2.secdesc = NULL;
 
 #if 0
-               if (asprintf(&devicename, "\\\\%s\\%s", longname, printername) < 0) {
-                       nt_status = NT_STATUS_NO_MEMORY;
-                       goto done;
-               }
+                       if (asprintf(&devicename, "\\\\%s\\%s", longname,
+                                    printername) < 0) {
+                               nt_status = NT_STATUS_NO_MEMORY;
+                               goto done;
+                       }
 
-               init_unistr(&ctr_dst.printers_2->devmode->devicename, devicename); 
+                       init_unistr(&ctr_dst.printers_2->devmode->devicename,
+                                   devicename);
 #endif
-               if (!net_spoolss_setprinter(cli_dst, mem_ctx, &hnd_dst, 
-                                               level, &ctr_dst)) 
-                       goto done;
-               
-               DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
-
-
+                       if (!net_spoolss_setprinter(pipe_hnd_dst, mem_ctx, &hnd_dst,
+                                                   level, &info_dst))
+                               goto done;
 
+                       DEBUGADD(1,("\tSetPrinter of DEVICEMODE succeeded\n"));
+               }
+#endif
                /* STEP 2: COPY REGISTRY VALUES */
-       
-               /* please keep in mind that samba parse_spools gives horribly 
-                  crippled results when used to cli_spoolss_enumprinterdataex 
-                  a win2k3-server.  
-                  FIXME: IIRC I've seen it too on a win2k-server 
+
+               /* please keep in mind that samba parse_spools gives horribly
+                  crippled results when used to rpccli_spoolss_enumprinterdataex
+                  a win2k3-server.  (Bugzilla #1851)
+                  FIXME: IIRC I've seen it too on a win2k-server
                */
 
                /* enumerate data on src handle */
-               result = cli_spoolss_enumprinterdata(cli, mem_ctx, &hnd_src, p, 0, 0,
+               result = rpccli_spoolss_enumprinterdata(pipe_hnd, mem_ctx, &hnd_src, p, 0, 0,
                        &val_needed, &data_needed, NULL);
 
-               /* loop for all printerdata */
+               /* loop for all printerdata of "PrinterDriverData" */
                while (W_ERROR_IS_OK(result)) {
-                       
+
                        REGISTRY_VALUE value;
-                       
-                       result = cli_spoolss_enumprinterdata(
-                               cli, mem_ctx, &hnd_src, p++, val_needed,
+
+                       result = rpccli_spoolss_enumprinterdata(
+                               pipe_hnd, mem_ctx, &hnd_src, p++, val_needed,
                                data_needed, 0, 0, &value);
 
                        /* loop for all reg_keys */
                        if (W_ERROR_IS_OK(result)) {
 
                                /* display_value */
-                               if (opt_verbose) 
-                                       display_reg_value(NULL, value);
+                               if (c->opt_verbose)
+                                       display_reg_value(SPOOL_PRINTERDATA_KEY, value);
 
                                /* set_value */
-                               if (!net_spoolss_setprinterdata(cli_dst, mem_ctx, 
-                                                               &hnd_dst, &value)) 
+                               if (!net_spoolss_setprinterdata(pipe_hnd_dst, mem_ctx,
+                                                               &hnd_dst, &value))
                                        goto done;
 
-                               DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n", 
+                               DEBUGADD(1,("\tSetPrinterData of [%s] succeeded\n",
                                        value.valuename));
                        }
                }
-               
+
                /* STEP 3: COPY SUBKEY VALUES */
 
-               /* here we need to enum all printer_keys and then work 
+               /* here we need to enum all printer_keys and then work
                   on the result with enum_printer_key_ex. nt4 does not
-                  respond to enumprinterkey, win2k does, so continue 
+                  respond to enumprinterkey, win2k does, so continue
                   in case of an error */
 
-               if (!net_spoolss_enumprinterkey(cli, mem_ctx, &hnd_src, keyname, &keylist)) {
+               if (!net_spoolss_enumprinterkey(pipe_hnd, mem_ctx, &hnd_src, "", &keylist)) {
                        printf("got no key-data\n");
                        continue;
                }
 
 
-               /* work on a list of printer keys 
+               /* work on a list of printer keys
                   each key has to be enumerated to get all required
-                  information.  information is then set via setprinterdataex-calls */ 
+                  information.  information is then set via setprinterdataex-calls */
 
                if (keylist == NULL)
                        continue;
 
                curkey = keylist;
                while (*curkey != 0) {
-
-                       pstring subkey;
-                       rpcstr_pull(subkey, curkey, sizeof(subkey), -1, STR_TERMINATE);
+                       char *subkey;
+                       rpcstr_pull_talloc(mem_ctx,
+                                       &subkey,
+                                       curkey,
+                                       -1,
+                                       STR_TERMINATE);
+                       if (!subkey) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
 
                        curkey += strlen(subkey) + 1;
 
+                       if ( !(reg_ctr = TALLOC_ZERO_P( mem_ctx, REGVAL_CTR )) )
+                               return NT_STATUS_NO_MEMORY;
+
                        /* enumerate all src subkeys */
-                       if (!net_spoolss_enumprinterdataex(cli, mem_ctx, 0, 
-                                                          &hnd_src, subkey, 
-                                                          &reg_ctr)) 
+                       if (!net_spoolss_enumprinterdataex(pipe_hnd, mem_ctx, 0,
+                                                          &hnd_src, subkey,
+                                                          reg_ctr))
                                goto done;
 
-                       for (j=0; j < reg_ctr.num_values; j++) {
-                       
+                       for (j=0; j < reg_ctr->num_values; j++) {
+
                                REGISTRY_VALUE value;
                                UNISTR2 data;
-                       
-                               /* although samba replies with sane data in most cases we 
+
+                               /* although samba replies with sane data in most cases we
                                   should try to avoid writing wrong registry data */
-       
-                               if (strequal(reg_ctr.values[j]->valuename, SPOOL_REG_PORTNAME) || 
-                                   strequal(reg_ctr.values[j]->valuename, SPOOL_REG_UNCNAME) ||
-                                   strequal(reg_ctr.values[j]->valuename, SPOOL_REG_URL) ||
-                                   strequal(reg_ctr.values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
-                                   strequal(reg_ctr.values[j]->valuename, SPOOL_REG_SERVERNAME)) {
-
-                                       if (strequal(reg_ctr.values[j]->valuename, SPOOL_REG_PORTNAME)) {
-                               
+
+                               if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME) ||
+                                   strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME) ||
+                                   strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL) ||
+                                   strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME) ||
+                                   strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
+
+                                       if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_PORTNAME)) {
+
                                                /* although windows uses a multi-sz, we use a sz */
                                                init_unistr2(&data, SAMBA_PRINTER_PORT_NAME, UNI_STR_TERMINATE);
                                                fstrcpy(value.valuename, SPOOL_REG_PORTNAME);
                                        }
-                               
-                                       if (strequal(reg_ctr.values[j]->valuename, SPOOL_REG_UNCNAME)) {
-                                       
+
+                                       if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_UNCNAME)) {
+
                                                if (asprintf(&unc_name, "\\\\%s\\%s", longname, sharename) < 0) {
                                                        nt_status = NT_STATUS_NO_MEMORY;
                                                        goto done;
@@ -2242,7 +2497,7 @@ NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid, const
                                                fstrcpy(value.valuename, SPOOL_REG_UNCNAME);
                                        }
 
-                                       if (strequal(reg_ctr.values[j]->valuename, SPOOL_REG_URL)) {
+                                       if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_URL)) {
 
                                                continue;
 
@@ -2257,13 +2512,13 @@ NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid, const
 #endif
                                        }
 
-                                       if (strequal(reg_ctr.values[j]->valuename, SPOOL_REG_SERVERNAME)) {
+                                       if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SERVERNAME)) {
 
                                                init_unistr2(&data, longname, UNI_STR_TERMINATE);
                                                fstrcpy(value.valuename, SPOOL_REG_SERVERNAME);
                                        }
 
-                                       if (strequal(reg_ctr.values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
+                                       if (strequal(reg_ctr->values[j]->valuename, SPOOL_REG_SHORTSERVERNAME)) {
 
                                                init_unistr2(&data, global_myname(), UNI_STR_TERMINATE);
                                                fstrcpy(value.valuename, SPOOL_REG_SHORTSERVERNAME);
@@ -2271,51 +2526,53 @@ NTSTATUS rpc_printer_migrate_settings_internals(const DOM_SID *domain_sid, const
 
                                        value.type = REG_SZ;
                                        value.size = data.uni_str_len * 2;
-                                       value.data_p = talloc_memdup(mem_ctx, data.buffer, value.size);
+                                       if (value.size) {
+                                               value.data_p = (uint8 *)TALLOC_MEMDUP(mem_ctx, data.buffer, value.size);
+                                       } else {
+                                               value.data_p = NULL;
+                                       }
 
-                                       if (opt_verbose) 
+                                       if (c->opt_verbose)
                                                display_reg_value(subkey, value);
 
                                        /* here we have to set all subkeys on the dst server */
-                                       if (!net_spoolss_setprinterdataex(cli_dst, mem_ctx, &hnd_dst, 
-                                                       subkey, &value)) 
+                                       if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
+                                                       subkey, &value))
                                                goto done;
-                                                       
+
                                } else {
 
-                                       if (opt_verbose) 
-                                               display_reg_value(subkey, *(reg_ctr.values[j]));
+                                       if (c->opt_verbose)
+                                               display_reg_value(subkey, *(reg_ctr->values[j]));
 
                                        /* here we have to set all subkeys on the dst server */
-                                       if (!net_spoolss_setprinterdataex(cli_dst, mem_ctx, &hnd_dst, 
-                                                       subkey, reg_ctr.values[j])) 
+                                       if (!net_spoolss_setprinterdataex(pipe_hnd_dst, mem_ctx, &hnd_dst,
+                                                       subkey, reg_ctr->values[j]))
                                                goto done;
 
                                }
 
-                               DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n", 
-                                               subkey, reg_ctr.values[j]->valuename));
+                               DEBUGADD(1,("\tSetPrinterDataEx of key [%s\\%s] succeeded\n",
+                                               subkey, reg_ctr->values[j]->valuename));
 
                        }
-                                               
-                       regval_ctr_destroy(&reg_ctr);
+
+                       TALLOC_FREE( reg_ctr );
                }
 
-               safe_free(keylist);
+               SAFE_FREE(keylist);
 
                /* close printer handles here */
-               if (got_hnd_src) {
-                       cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
-                       got_hnd_src = False;
+               if (is_valid_policy_hnd(&hnd_src)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
                }
 
-               if (got_hnd_dst) {
-                       cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
-                       got_hnd_dst = False;
+               if (is_valid_policy_hnd(&hnd_dst)) {
+                       rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
                }
 
        }
-       
+
        nt_status = NT_STATUS_OK;
 
 done:
@@ -2323,14 +2580,13 @@ done:
        SAFE_FREE(url);
        SAFE_FREE(unc_name);
 
-       if (got_hnd_src)
-               cli_spoolss_close_printer(cli, mem_ctx, &hnd_src);
+       if (is_valid_policy_hnd(&hnd_src))
+               rpccli_spoolss_ClosePrinter(pipe_hnd, mem_ctx, &hnd_src, NULL);
 
-       if (got_hnd_dst)
-               cli_spoolss_close_printer(cli_dst, mem_ctx, &hnd_dst);
+       if (is_valid_policy_hnd(&hnd_dst))
+               rpccli_spoolss_ClosePrinter(pipe_hnd_dst, mem_ctx, &hnd_dst, NULL);
 
-       if (got_dst_spoolss_pipe) {
-               cli_nt_session_close(cli_dst);
+       if (cli_dst) {
                cli_shutdown(cli_dst);
        }
        return nt_status;