s3-spoolss: Don't leak memory.
[metze/samba/wip.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
index 1b83528d74c5bae7d4be97b9eb17dab26ee69ac4..891f4296b93e2859686089e65fb3633b63774832 100644 (file)
    up, all the errors returned are DOS errors, not NT status codes. */
 
 #include "includes.h"
+#include "ntdomain.h"
 #include "nt_printing.h"
 #include "srv_spoolss_util.h"
 #include "../librpc/gen_ndr/srv_spoolss.h"
 #include "../librpc/gen_ndr/ndr_spoolss_c.h"
 #include "rpc_client/init_spoolss.h"
 #include "rpc_client/cli_pipe.h"
-#include "librpc/gen_ndr/messaging.h"
 #include "../libcli/security/security.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "registry.h"
-#include "registry/reg_objects.h"
 #include "include/printing.h"
 #include "secrets.h"
 #include "../librpc/gen_ndr/netlogon.h"
 #include "serverid.h"
 #include "../libcli/registry/util_reg.h"
 #include "smbd/smbd.h"
+#include "smbd/globals.h"
+#include "auth.h"
+#include "messages.h"
+#include "rpc_server/spoolss/srv_spoolss_nt.h"
+#include "util_tdb.h"
+#include "libsmb/libsmb.h"
+#include "printing/printer_list.h"
+#include "../lib/tsocket/tsocket.h"
+#include "rpc_client/cli_winreg_spoolss.h"
+#include "../libcli/smb/smbXcli_base.h"
 
 /* macros stolen from s4 spoolss server */
 #define SPOOLSS_BUFFER_UNION(fn,info,level) \
@@ -129,7 +138,6 @@ struct notify_back_channel {
 
        /* print notify back-channel pipe handle*/
        struct rpc_pipe_client *cli_pipe;
-       struct dcerpc_binding_handle *binding_handle;
        uint32_t active_connections;
 };
 
@@ -242,6 +250,8 @@ static void srv_spoolss_replycloseprinter(int snum,
 
        /* weird if the test succeeds !!! */
        if (prn_hnd->notify.cli_chan == NULL ||
+           prn_hnd->notify.cli_chan->cli_pipe == NULL ||
+           prn_hnd->notify.cli_chan->cli_pipe->binding_handle == NULL ||
            prn_hnd->notify.cli_chan->active_connections == 0) {
                DEBUG(0, ("Trying to close unexisting backchannel!\n"));
                DLIST_REMOVE(back_channels, prn_hnd->notify.cli_chan);
@@ -250,7 +260,7 @@ static void srv_spoolss_replycloseprinter(int snum,
        }
 
        status = dcerpc_spoolss_ReplyClosePrinter(
-                                       prn_hnd->notify.cli_chan->binding_handle,
+                                       prn_hnd->notify.cli_chan->cli_pipe->binding_handle,
                                        talloc_tos(),
                                        &prn_hnd->notify.cli_hnd,
                                        &result);
@@ -266,7 +276,6 @@ static void srv_spoolss_replycloseprinter(int snum,
        /* if it's the last connection, deconnect the IPC$ share */
        if (prn_hnd->notify.cli_chan->active_connections == 1) {
 
-               prn_hnd->notify.cli_chan->binding_handle = NULL;
                cli_shutdown(rpc_pipe_np_smb_conn(prn_hnd->notify.cli_chan->cli_pipe));
                DLIST_REMOVE(back_channels, prn_hnd->notify.cli_chan);
                TALLOC_FREE(prn_hnd->notify.cli_chan);
@@ -274,20 +283,12 @@ static void srv_spoolss_replycloseprinter(int snum,
                if (prn_hnd->notify.msg_ctx != NULL) {
                        messaging_deregister(prn_hnd->notify.msg_ctx,
                                             MSG_PRINTER_NOTIFY2, NULL);
-
-                       /*
-                        * Tell the serverid.tdb we're no longer
-                        * interested in printer notify messages.
-                        */
-
-                       serverid_register_msg_flags(
-                               messaging_server_id(prn_hnd->notify.msg_ctx),
-                               false, FLAG_MSG_PRINT_NOTIFY);
                }
        }
 
        if (prn_hnd->notify.cli_chan) {
                prn_hnd->notify.cli_chan->active_connections--;
+               prn_hnd->notify.cli_chan = NULL;
        }
 }
 
@@ -373,7 +374,7 @@ static WERROR delete_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
                                  const char *sharename,
                                  struct messaging_context *msg_ctx)
 {
-       char *cmd = lp_deleteprinter_cmd();
+       char *cmd = lp_deleteprinter_cmd(talloc_tos());
        char *command = NULL;
        int ret;
        bool is_print_op = false;
@@ -416,14 +417,6 @@ static WERROR delete_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
        if (ret != 0)
                return WERR_BADFID; /* What to return here? */
 
-       /* go ahead and re-read the services immediately */
-       become_root();
-       reload_services(msg_ctx, -1, false);
-       unbecome_root();
-
-       if ( lp_servicenumber( sharename ) >= 0 )
-               return WERR_ACCESS_DENIED;
-
        return WERR_OK;
 }
 
@@ -457,7 +450,7 @@ static WERROR delete_printer_handle(struct pipes_struct *p, struct policy_handle
        /* this does not need a become root since the access check has been
           done on the handle already */
 
-       result = winreg_delete_printer_key(p->mem_ctx,
+       result = winreg_delete_printer_key_internal(p->mem_ctx,
                                           get_session_info_system(),
                                           p->msg_ctx,
                                           Printer->sharename,
@@ -545,7 +538,7 @@ static void prune_printername_cache(void)
 ****************************************************************************/
 
 static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
-                                  const struct auth_serversupplied_info *session_info,
+                                  const struct auth_session_info *session_info,
                                   struct messaging_context *msg_ctx,
                                   struct printer_handle *Printer,
                                   const char *handlename)
@@ -572,7 +565,7 @@ static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
        DEBUG(4,("Setting printer name=%s (len=%lu)\n", handlename,
                (unsigned long)strlen(handlename)));
 
-       aprinter = CONST_DISCARD(char *, handlename);
+       aprinter = discard_const_p(char, handlename);
        if ( *handlename == '\\' ) {
                servername = canon_servername(handlename);
                if ( (aprinter = strchr_m( servername, '\\' )) != NULL ) {
@@ -679,7 +672,7 @@ static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
                        continue;
                }
 
-               result = winreg_get_printer(mem_ctx,
+               result = winreg_get_printer_internal(mem_ctx,
                                            session_info,
                                            msg_ctx,
                                            sname,
@@ -724,7 +717,7 @@ static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
 
        DEBUGADD(4,("set_printer_hnd_name: Printer found: %s -> %s\n", aprinter, sname));
 
-       fstrcpy(Printer->sharename, sname);
+       strlcpy(Printer->sharename, sname, sizeof(Printer->sharename));
 
        return WERR_OK;
 }
@@ -1087,7 +1080,7 @@ static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MS
        if ( i == ctr->num_groups ) {
                ctr->num_groups++;
 
-               if ( !(groups = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
+               if ( !(groups = talloc_realloc( ctr->ctx, ctr->msg_groups, SPOOLSS_NOTIFY_MSG_GROUP, ctr->num_groups)) ) {
                        DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed!\n"));
                        return 0;
                }
@@ -1105,7 +1098,7 @@ static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MS
 
        msg_grp->num_msgs++;
 
-       if ( !(msg_list = TALLOC_REALLOC_ARRAY( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
+       if ( !(msg_list = talloc_realloc( ctr->ctx, msg_grp->msgs, SPOOLSS_NOTIFY_MSG, msg_grp->num_msgs )) ) {
                DEBUG(0,("notify_msg_ctr_addmsg: talloc_realloc() failed for new message [%d]!\n", msg_grp->num_msgs));
                return 0;
        }
@@ -1118,7 +1111,7 @@ static int notify_msg_ctr_addmsg( SPOOLSS_NOTIFY_MSG_CTR *ctr, SPOOLSS_NOTIFY_MS
 
        if ( msg->len != 0 )
                msg_grp->msgs[new_slot].notify.data = (char *)
-                       TALLOC_MEMDUP( ctr->ctx, msg->notify.data, msg->len );
+                       talloc_memdup( ctr->ctx, msg->notify.data, msg->len );
 
        return ctr->num_groups;
 }
@@ -1246,6 +1239,8 @@ static int send_notify2_printer(TALLOC_CTX *mem_ctx,
 
        /* Is there notification on this handle? */
        if (prn_hnd->notify.cli_chan == NULL ||
+           prn_hnd->notify.cli_chan->cli_pipe == NULL ||
+           prn_hnd->notify.cli_chan->cli_pipe->binding_handle == NULL ||
            prn_hnd->notify.cli_chan->active_connections == 0) {
                return 0;
        }
@@ -1278,7 +1273,7 @@ static int send_notify2_printer(TALLOC_CTX *mem_ctx,
        info.info0 = &info0;
 
        status = dcerpc_spoolss_RouterReplyPrinterEx(
-                               prn_hnd->notify.cli_chan->binding_handle,
+                               prn_hnd->notify.cli_chan->cli_pipe->binding_handle,
                                mem_ctx,
                                &prn_hnd->notify.cli_hnd,
                                prn_hnd->notify.change, /* color */
@@ -1498,7 +1493,7 @@ static bool srv_spoolss_drv_upgrade_printer(const char *drivername,
 
        messaging_send_buf(msg_ctx, messaging_server_id(msg_ctx),
                           MSG_PRINTER_DRVUPGRADE,
-                          (uint8_t *)drivername, len+1);
+                          (const uint8_t *)drivername, len+1);
 
        return true;
 }
@@ -1527,24 +1522,17 @@ void do_drv_upgrade_printer(struct messaging_context *msg,
                            DATA_BLOB *data)
 {
        TALLOC_CTX *tmp_ctx;
-       struct auth_serversupplied_info *session_info = NULL;
+       const struct auth_session_info *session_info = get_session_info_system();
        struct spoolss_PrinterInfo2 *pinfo2;
-       NTSTATUS status;
        WERROR result;
        const char *drivername;
        int snum;
        int n_services = lp_numservices();
+       struct dcerpc_binding_handle *b = NULL;
 
        tmp_ctx = talloc_new(NULL);
        if (!tmp_ctx) return;
 
-       status = make_session_info_system(tmp_ctx, &session_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("do_drv_upgrade_printer: "
-                         "Could not create system session_info\n"));
-               goto done;
-       }
-
        drivername = talloc_strndup(tmp_ctx, (const char *)data->data, data->length);
        if (!drivername) {
                DEBUG(0, ("do_drv_upgrade_printer: Out of memoery ?!\n"));
@@ -1566,7 +1554,17 @@ void do_drv_upgrade_printer(struct messaging_context *msg,
                        continue;
                }
 
-               result = winreg_get_printer(tmp_ctx, session_info, msg,
+               if (b == NULL) {
+                       result = winreg_printer_binding_handle(tmp_ctx,
+                                                              session_info,
+                                                              msg,
+                                                              &b);
+                       if (!W_ERROR_IS_OK(result)) {
+                               break;
+                       }
+               }
+
+               result = winreg_get_printer(tmp_ctx, b,
                                            lp_const_servicename(snum),
                                            &pinfo2);
 
@@ -1585,9 +1583,7 @@ void do_drv_upgrade_printer(struct messaging_context *msg,
                DEBUG(6,("Updating printer [%s]\n", pinfo2->printername));
 
                /* all we care about currently is the change_id */
-               result = winreg_printer_update_changeid(tmp_ctx,
-                                                       session_info,
-                                                       msg,
+               result = winreg_printer_update_changeid(tmp_ctx, b,
                                                        pinfo2->printername);
 
                if (!W_ERROR_IS_OK(result)) {
@@ -1636,15 +1632,17 @@ WERROR _spoolss_OpenPrinter(struct pipes_struct *p,
                            struct spoolss_OpenPrinter *r)
 {
        struct spoolss_OpenPrinterEx e;
+       struct spoolss_UserLevel1 level1;
        WERROR werr;
 
-       ZERO_STRUCT(e.in.userlevel);
+       ZERO_STRUCT(level1);
 
        e.in.printername        = r->in.printername;
        e.in.datatype           = r->in.datatype;
        e.in.devmode_ctr        = r->in.devmode_ctr;
        e.in.access_mask        = r->in.access_mask;
-       e.in.level              = 0;
+       e.in.userlevel_ctr.level                = 1;
+       e.in.userlevel_ctr.user_info.level1     = &level1;
 
        e.out.handle            = r->out.handle;
 
@@ -1704,19 +1702,26 @@ WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
                              struct spoolss_OpenPrinterEx *r)
 {
        int snum;
+       char *raddr;
+       char *rhost;
        struct printer_handle *Printer=NULL;
        WERROR result;
+       int rc;
 
        if (!r->in.printername) {
                return WERR_INVALID_PARAM;
        }
 
-       if (r->in.level > 3) {
+       if (!*r->in.printername) {
+               return WERR_INVALID_PARAM;
+       }
+
+       if (r->in.userlevel_ctr.level > 3) {
                return WERR_INVALID_PARAM;
        }
-       if ((r->in.level == 1 && !r->in.userlevel.level1) ||
-           (r->in.level == 2 && !r->in.userlevel.level2) ||
-           (r->in.level == 3 && !r->in.userlevel.level3)) {
+       if ((r->in.userlevel_ctr.level == 1 && !r->in.userlevel_ctr.user_info.level1) ||
+           (r->in.userlevel_ctr.level == 2 && !r->in.userlevel_ctr.user_info.level2) ||
+           (r->in.userlevel_ctr.level == 3 && !r->in.userlevel_ctr.user_info.level3)) {
                return WERR_INVALID_PARAM;
        }
 
@@ -1806,15 +1811,10 @@ WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
                        /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
                           and not a printer admin, then fail */
 
-                       if ((p->session_info->utok.uid != sec_initial_uid()) &&
+                       if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
                            !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
-                           !nt_token_check_sid(&global_sid_Builtin_Print_Operators, p->session_info->security_token) &&
-                           !token_contains_name_in_list(
-                                   uidtoname(p->session_info->utok.uid),
-                                   p->session_info->info3->base.domain.string,
-                                   NULL,
-                                   p->session_info->security_token,
-                                   lp_printer_admin(snum))) {
+                           !nt_token_check_sid(&global_sid_Builtin_Print_Operators,
+                                               p->session_info->security_token)) {
                                close_printer_handle(p, r->out.handle);
                                ZERO_STRUCTP(r->out.handle);
                                DEBUG(3,("access DENIED as user is not root, "
@@ -1870,15 +1870,30 @@ WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
                }
 
                /* check smb.conf parameters and the the sec_desc */
+               raddr = tsocket_address_inet_addr_string(p->remote_address,
+                                                        p->mem_ctx);
+               if (raddr == NULL) {
+                       return WERR_NOMEM;
+               }
+
+               rc = get_remote_hostname(p->remote_address,
+                                        &rhost,
+                                        p->mem_ctx);
+               if (rc < 0) {
+                       return WERR_NOMEM;
+               }
+               if (strequal(rhost, "UNKNOWN")) {
+                       rhost = raddr;
+               }
 
                if (!allow_access(lp_hostsdeny(snum), lp_hostsallow(snum),
-                                 p->client_id->name, p->client_id->addr)) {
+                                 rhost, raddr)) {
                        DEBUG(3, ("access DENIED (hosts allow/deny) for printer open\n"));
                        ZERO_STRUCTP(r->out.handle);
                        return WERR_ACCESS_DENIED;
                }
 
-               if (!user_ok_token(uidtoname(p->session_info->utok.uid), NULL,
+               if (!user_ok_token(uidtoname(p->session_info->unix_token->uid), NULL,
                                   p->session_info->security_token, snum) ||
                    !print_access_check(p->session_info,
                                        p->msg_ctx,
@@ -1905,7 +1920,7 @@ WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
                DEBUG(4,("Setting printer access = %s\n", (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
                        ? "PRINTER_ACCESS_ADMINISTER" : "PRINTER_ACCESS_USE" ));
 
-               winreg_create_printer(p->mem_ctx,
+               winreg_create_printer_internal(p->mem_ctx,
                                      get_session_info_system(),
                                      p->msg_ctx,
                                      lp_const_servicename(snum));
@@ -1925,24 +1940,12 @@ WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
         * save it here in case we get a job submission on this handle
         */
 
-        if ((Printer->printer_type != SPLHND_SERVER) &&
-            r->in.devmode_ctr.devmode) {
+        if ((Printer->printer_type != SPLHND_SERVER)
+         && (r->in.devmode_ctr.devmode != NULL)) {
                copy_devicemode(NULL, r->in.devmode_ctr.devmode,
                                &Printer->devmode);
         }
 
-#if 0  /* JERRY -- I'm doubtful this is really effective */
-       /* HACK ALERT!!! Sleep for 1/3 of a second to try trigger a LAN/WAN
-          optimization in Windows 2000 clients  --jerry */
-
-       if ( (r->in.access_mask == PRINTER_ACCESS_ADMINISTER)
-               && (RA_WIN2K == get_remote_arch()) )
-       {
-               DEBUG(10,("_spoolss_OpenPrinterEx: Enabling LAN/WAN hack for Win2k clients.\n"));
-               sys_usleep( 500000 );
-       }
-#endif
-
        return WERR_OK;
 }
 
@@ -1996,7 +1999,7 @@ WERROR _spoolss_DeletePrinter(struct pipes_struct *p,
        }
 
        if (get_printer_snum(p, r->in.handle, &snum, NULL)) {
-               winreg_delete_printer_key(p->mem_ctx,
+               winreg_delete_printer_key_internal(p->mem_ctx,
                                          get_session_info_system(),
                                          p->msg_ctx,
                                          lp_const_servicename(snum),
@@ -2025,6 +2028,12 @@ static const struct print_architecture_table_node archi_table[]= {
        {NULL,                   "",            -1 }
 };
 
+static const int drv_cversion[] = {SPOOLSS_DRIVER_VERSION_9X,
+                                  SPOOLSS_DRIVER_VERSION_NT35,
+                                  SPOOLSS_DRIVER_VERSION_NT4,
+                                  SPOOLSS_DRIVER_VERSION_200X,
+                                  -1};
+
 static int get_version_id(const char *arch)
 {
        int i;
@@ -2047,99 +2056,135 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
 {
 
        struct spoolss_DriverInfo8 *info = NULL;
-       struct spoolss_DriverInfo8 *info_win2k = NULL;
        int                             version;
        WERROR                          status;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
+       int i;
+       bool found;
 
        /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
           and not a printer admin, then fail */
 
-       if ( (p->session_info->utok.uid != sec_initial_uid())
-            && !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR)
-               && !token_contains_name_in_list(
-                       uidtoname(p->session_info->utok.uid),
-                       p->session_info->info3->base.domain.string,
-                       NULL,
-                       p->session_info->security_token,
-                       lp_printer_admin(-1)) )
-       {
+       if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
+           !security_token_has_privilege(p->session_info->security_token,
+                                         SEC_PRIV_PRINT_OPERATOR)) {
                return WERR_ACCESS_DENIED;
        }
 
        /* check that we have a valid driver name first */
 
-       if ((version = get_version_id(r->in.architecture)) == -1)
+       if ((version = get_version_id(r->in.architecture)) == -1) {
                return WERR_INVALID_ENVIRONMENT;
+       }
 
-       status = winreg_get_driver(p->mem_ctx,
-                                  get_session_info_system(),
-                                  p->msg_ctx,
-                                  r->in.architecture, r->in.driver,
-                                  version, &info);
-       if (!W_ERROR_IS_OK(status)) {
-               /* try for Win2k driver if "Windows NT x86" */
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
 
-               if ( version == 2 ) {
-                       version = 3;
+       status = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
+       }
 
-                       status = winreg_get_driver(p->mem_ctx,
-                                                  get_session_info_system(),
-                                                  p->msg_ctx,
-                                                  r->in.architecture,
-                                                  r->in.driver,
-                                                  version, &info);
-                       if (!W_ERROR_IS_OK(status)) {
-                               status = WERR_UNKNOWN_PRINTER_DRIVER;
-                               goto done;
-                       }
+       for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
+               status = winreg_get_driver(tmp_ctx, b,
+                                          r->in.architecture, r->in.driver,
+                                          drv_cversion[i], &info);
+               if (!W_ERROR_IS_OK(status)) {
+                       DEBUG(5, ("skipping del of driver with version %d\n",
+                                 drv_cversion[i]));
+                       continue;
                }
-               /* otherwise it was a failure */
-               else {
-                       status = WERR_UNKNOWN_PRINTER_DRIVER;
+               found = true;
+
+               if (printer_driver_in_use(tmp_ctx, b, info)) {
+                       status = WERR_PRINTER_DRIVER_IN_USE;
                        goto done;
                }
 
+               status = winreg_del_driver(tmp_ctx, b, info, drv_cversion[i]);
+               if (!W_ERROR_IS_OK(status)) {
+                       DEBUG(0, ("failed del of driver with version %d\n",
+                                 drv_cversion[i]));
+                       goto done;
+               }
+       }
+       if (found == false) {
+               DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
+               status = WERR_UNKNOWN_PRINTER_DRIVER;
+       } else {
+               status = WERR_OK;
        }
 
-       if (printer_driver_in_use(p->mem_ctx,
-                                 get_session_info_system(),
-                                 p->msg_ctx,
-                                 info)) {
+done:
+       talloc_free(tmp_ctx);
+
+       return status;
+}
+
+static WERROR spoolss_dpd_version(TALLOC_CTX *mem_ctx,
+                                 struct pipes_struct *p,
+                                 struct spoolss_DeletePrinterDriverEx *r,
+                                 struct dcerpc_binding_handle *b,
+                                 struct spoolss_DriverInfo8 *info)
+{
+       WERROR status;
+       bool delete_files;
+
+       if (printer_driver_in_use(mem_ctx, b, info)) {
                status = WERR_PRINTER_DRIVER_IN_USE;
                goto done;
        }
 
-       if (version == 2) {
-               status = winreg_get_driver(p->mem_ctx,
-                                          get_session_info_system(),
-                                          p->msg_ctx,
-                                          r->in.architecture,
-                                          r->in.driver, 3, &info_win2k);
-               if (W_ERROR_IS_OK(status)) {
-                       /* if we get to here, we now have 2 driver info structures to remove */
-                       /* remove the Win2k driver first*/
+       /*
+        * we have a couple of cases to consider.
+        * (1) Are any files in use?  If so and DPD_DELETE_ALL_FILES is set,
+        *     then the delete should fail if **any** files overlap with
+        *     other drivers
+        * (2) If DPD_DELETE_UNUSED_FILES is set, then delete all
+        *     non-overlapping files
+        * (3) If neither DPD_DELETE_ALL_FILES nor DPD_DELETE_UNUSED_FILES
+        *     are set, then do not delete any files
+        * Refer to MSDN docs on DeletePrinterDriverEx() for details.
+        */
 
-                       status = winreg_del_driver(p->mem_ctx,
-                                                  get_session_info_system(),
-                                                  p->msg_ctx,
-                                                  info_win2k, 3);
-                       talloc_free(info_win2k);
+       delete_files = r->in.delete_flags
+                       & (DPD_DELETE_ALL_FILES | DPD_DELETE_UNUSED_FILES);
 
-                       /* this should not have failed---if it did, report to client */
-                       if (!W_ERROR_IS_OK(status)) {
-                               goto done;
-                       }
+
+       if (delete_files) {
+               bool in_use = printer_driver_files_in_use(mem_ctx, b, info);
+               if (in_use && (r->in.delete_flags & DPD_DELETE_ALL_FILES)) {
+                       status = WERR_PRINTER_DRIVER_IN_USE;
+                       goto done;
                }
+               /*
+                * printer_driver_files_in_use() has trimmed overlapping files
+                * from info so they are not removed on DPD_DELETE_UNUSED_FILES
+                */
        }
 
-       status = winreg_del_driver(p->mem_ctx,
-                                  get_session_info_system(),
-                                  p->msg_ctx,
-                                  info, version);
 
-done:
-       talloc_free(info);
+       status = winreg_del_driver(mem_ctx, b, info, info->version);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
+       }
+
+       /*
+        * now delete any associated files if delete_files is
+        * true. Even if this part failes, we return succes
+        * because the driver doesn not exist any more
+        */
+       if (delete_files) {
+               delete_driver_files(p->session_info, info);
+       }
 
+done:
        return status;
 }
 
@@ -2150,174 +2195,74 @@ done:
 WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                                      struct spoolss_DeletePrinterDriverEx *r)
 {
-       struct spoolss_DriverInfo8      *info = NULL;
-       struct spoolss_DriverInfo8      *info_win2k = NULL;
-       int                             version;
-       bool                            delete_files;
+       struct spoolss_DriverInfo8 *info = NULL;
        WERROR                          status;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
+       int i;
+       bool found;
 
        /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
           and not a printer admin, then fail */
 
-       if ( (p->session_info->utok.uid != sec_initial_uid())
-               && !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR)
-               && !token_contains_name_in_list(
-                       uidtoname(p->session_info->utok.uid),
-                       p->session_info->info3->base.domain.string,
-                       NULL,
-                       p->session_info->security_token, lp_printer_admin(-1)) )
-       {
+       if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
+           !security_token_has_privilege(p->session_info->security_token,
+                                         SEC_PRIV_PRINT_OPERATOR)) {
                return WERR_ACCESS_DENIED;
        }
 
        /* check that we have a valid driver name first */
-       if ((version = get_version_id(r->in.architecture)) == -1) {
+       if (get_version_id(r->in.architecture) == -1) {
                /* this is what NT returns */
                return WERR_INVALID_ENVIRONMENT;
        }
 
-       if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
-               version = r->in.version;
-
-       status = winreg_get_driver(p->mem_ctx,
-                                  get_session_info_system(),
-                                  p->msg_ctx,
-                                  r->in.architecture,
-                                  r->in.driver,
-                                  version,
-                                  &info);
-       if (!W_ERROR_IS_OK(status)) {
-               status = WERR_UNKNOWN_PRINTER_DRIVER;
-
-               /*
-                * if the client asked for a specific version,
-                * or this is something other than Windows NT x86,
-                * then we've failed
-                */
-
-               if ( (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) || (version !=2) )
-                       goto done;
-
-               /* try for Win2k driver if "Windows NT x86" */
-
-               version = 3;
-               status = winreg_get_driver(info,
-                                          get_session_info_system(),
-                                          p->msg_ctx,
-                                          r->in.architecture,
-                                          r->in.driver,
-                                          version, &info);
-               if (!W_ERROR_IS_OK(status)) {
-                       status = WERR_UNKNOWN_PRINTER_DRIVER;
-                       goto done;
-               }
-       }
-
-       if (printer_driver_in_use(info,
-                                 get_session_info_system(),
-                                 p->msg_ctx,
-                                 info)) {
-               status = WERR_PRINTER_DRIVER_IN_USE;
-               goto done;
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
        }
 
-       /*
-        * we have a couple of cases to consider.
-        * (1) Are any files in use?  If so and DPD_DELTE_ALL_FILE is set,
-        *     then the delete should fail if **any** files overlap with
-        *     other drivers
-        * (2) If DPD_DELTE_UNUSED_FILES is sert, then delete all
-        *     non-overlapping files
-        * (3) If neither DPD_DELTE_ALL_FILE nor DPD_DELTE_ALL_FILES
-        *     is set, the do not delete any files
-        * Refer to MSDN docs on DeletePrinterDriverEx() for details.
-        */
-
-       delete_files = r->in.delete_flags & (DPD_DELETE_ALL_FILES|DPD_DELETE_UNUSED_FILES);
-
-       /* fail if any files are in use and DPD_DELETE_ALL_FILES is set */
-
-       if (delete_files &&
-           (r->in.delete_flags & DPD_DELETE_ALL_FILES) &&
-           printer_driver_files_in_use(info,
-                                       get_session_info_system(),
-                                       p->msg_ctx,
-                                       info)) {
-               /* no idea of the correct error here */
-               status = WERR_ACCESS_DENIED;
+       status = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
                goto done;
        }
 
+       for (found = false, i = 0; drv_cversion[i] >= 0; i++) {
+               if ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
+                && (drv_cversion[i] != r->in.version)) {
+                       continue;
+               }
 
-       /* also check for W32X86/3 if necessary; maybe we already have? */
-
-       if ( (version == 2) && ((r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) != DPD_DELETE_SPECIFIC_VERSION)  ) {
-               status = winreg_get_driver(info,
-                                          get_session_info_system(),
-                                          p->msg_ctx,
-                                          r->in.architecture,
-                                          r->in.driver, 3, &info_win2k);
-               if (W_ERROR_IS_OK(status)) {
-
-                       if (delete_files &&
-                           (r->in.delete_flags & DPD_DELETE_ALL_FILES) &&
-                           printer_driver_files_in_use(info,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
-                                                       info_win2k)) {
-                               /* no idea of the correct error here */
-                               talloc_free(info_win2k);
-                               status = WERR_ACCESS_DENIED;
-                               goto done;
-                       }
-
-                       /* if we get to here, we now have 2 driver info structures to remove */
-                       /* remove the Win2k driver first*/
-
-                       status = winreg_del_driver(info,
-                                                  get_session_info_system(),
-                                                  p->msg_ctx,
-                                                  info_win2k,
-                                                  3);
-
-                       /* this should not have failed---if it did, report to client */
-
-                       if (!W_ERROR_IS_OK(status)) {
-                               goto done;
-                       }
-
-                       /*
-                        * now delete any associated files if delete_files is
-                        * true. Even if this part failes, we return succes
-                        * because the driver doesn not exist any more
-                        */
-                       if (delete_files) {
-                               delete_driver_files(get_session_info_system(),
-                                                   info_win2k);
-                       }
+               /* check if a driver with this version exists before delete */
+               status = winreg_get_driver(tmp_ctx, b,
+                                          r->in.architecture, r->in.driver,
+                                          drv_cversion[i], &info);
+               if (!W_ERROR_IS_OK(status)) {
+                       DEBUG(5, ("skipping del of driver with version %d\n",
+                                 drv_cversion[i]));
+                       continue;
                }
-       }
+               found = true;
 
-       status = winreg_del_driver(info,
-                                  get_session_info_system(),
-                                  p->msg_ctx,
-                                  info,
-                                  version);
-       if (!W_ERROR_IS_OK(status)) {
-               goto done;
+               status = spoolss_dpd_version(tmp_ctx, p, r, b, info);
+               if (!W_ERROR_IS_OK(status)) {
+                       DEBUG(0, ("failed to delete driver with version %d\n",
+                                 drv_cversion[i]));
+                       goto done;
+               }
        }
-
-       /*
-        * now delete any associated files if delete_files is
-        * true. Even if this part failes, we return succes
-        * because the driver doesn not exist any more
-        */
-       if (delete_files) {
-               delete_driver_files(get_session_info_system(), info);
+       if (found == false) {
+               DEBUG(0, ("driver %s not found for deletion\n", r->in.driver));
+               status = WERR_UNKNOWN_PRINTER_DRIVER;
+       } else {
+               status = WERR_OK;
        }
 
 done:
-       talloc_free(info);
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -2333,32 +2278,32 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
 {
        DEBUG(8,("getprinterdata_printer_server:%s\n", value));
 
-       if (!StrCaseCmp(value, "W3SvcInstalled")) {
+       if (!strcasecmp_m(value, "W3SvcInstalled")) {
                *type = REG_DWORD;
-               data->value = 0x00;
+               SIVAL(&data->value, 0, 0x00);
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "BeepEnabled")) {
+       if (!strcasecmp_m(value, "BeepEnabled")) {
                *type = REG_DWORD;
-               data->value = 0x00;
+               SIVAL(&data->value, 0, 0x00);
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "EventLog")) {
+       if (!strcasecmp_m(value, "EventLog")) {
                *type = REG_DWORD;
                /* formally was 0x1b */
-               data->value = 0x00;
+               SIVAL(&data->value, 0, 0x00);
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "NetPopup")) {
+       if (!strcasecmp_m(value, "NetPopup")) {
                *type = REG_DWORD;
-               data->value = 0x00;
+               SIVAL(&data->value, 0, 0x00);
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "MajorVersion")) {
+       if (!strcasecmp_m(value, "MajorVersion")) {
                *type = REG_DWORD;
 
                /* Windows NT 4.0 seems to not allow uploading of drivers
@@ -2367,17 +2312,17 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
                   -- jerry */
 
                if (RA_WINNT == get_remote_arch()) {
-                       data->value = 0x02;
+                       SIVAL(&data->value, 0, 0x02);
                } else {
-                       data->value = 0x03;
+                       SIVAL(&data->value, 0, 0x03);
                }
 
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "MinorVersion")) {
+       if (!strcasecmp_m(value, "MinorVersion")) {
                *type = REG_DWORD;
-               data->value = 0x00;
+               SIVAL(&data->value, 0, 0x00);
                return WERR_OK;
        }
 
@@ -2388,14 +2333,18 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
         *  uint32_t build       = [2195|2600]
         *  extra unicode string = e.g. "Service Pack 3"
         */
-       if (!StrCaseCmp(value, "OSVersion")) {
+       if (!strcasecmp_m(value, "OSVersion")) {
                DATA_BLOB blob;
                enum ndr_err_code ndr_err;
                struct spoolss_OSVersion os;
 
-               os.major                = 5;    /* Windows 2000 == 5.0 */
-               os.minor                = 0;
-               os.build                = 2195; /* build */
+               os.major                = lp_parm_int(GLOBAL_SECTION_SNUM,
+                                                     "spoolss", "os_major", 5);
+                                                     /* Windows 2000 == 5.0 */
+               os.minor                = lp_parm_int(GLOBAL_SECTION_SNUM,
+                                                     "spoolss", "os_minor", 0);
+               os.build                = lp_parm_int(GLOBAL_SECTION_SNUM,
+                                                     "spoolss", "os_build", 2195);
                os.extra_string         = "";   /* leave extra string empty */
 
                ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &os,
@@ -2404,6 +2353,10 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
                        return WERR_GENERAL_FAILURE;
                }
 
+               if (DEBUGLEVEL >= 10) {
+                       NDR_PRINT_DEBUG(spoolss_OSVersion, &os);
+               }
+
                *type = REG_BINARY;
                data->binary = blob;
 
@@ -2411,7 +2364,7 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
        }
 
 
-       if (!StrCaseCmp(value, "DefaultSpoolDirectory")) {
+       if (!strcasecmp_m(value, "DefaultSpoolDirectory")) {
                *type = REG_SZ;
 
                data->string = talloc_strdup(mem_ctx, "C:\\PRINTERS");
@@ -2420,7 +2373,7 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "Architecture")) {
+       if (!strcasecmp_m(value, "Architecture")) {
                *type = REG_SZ;
                data->string = talloc_strdup(mem_ctx,
                        lp_parm_const_string(GLOBAL_SECTION_SNUM, "spoolss", "architecture", SPOOLSS_ARCHITECTURE_NT_X86));
@@ -2429,21 +2382,21 @@ static WERROR getprinterdata_printer_server(TALLOC_CTX *mem_ctx,
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "DsPresent")) {
+       if (!strcasecmp_m(value, "DsPresent")) {
                *type = REG_DWORD;
 
                /* only show the publish check box if we are a
                   member of a AD domain */
 
                if (lp_security() == SEC_ADS) {
-                       data->value = 0x01;
+                       SIVAL(&data->value, 0, 0x01);
                } else {
-                       data->value = 0x00;
+                       SIVAL(&data->value, 0, 0x00);
                }
                return WERR_OK;
        }
 
-       if (!StrCaseCmp(value, "DNSMachineName")) {
+       if (!strcasecmp_m(value, "DNSMachineName")) {
                const char *hostname = get_mydnsfullname();
 
                if (!hostname) {
@@ -2516,7 +2469,7 @@ static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
        }
 
        /* setup the connection */
-       ret = cli_full_connection( &the_cli, global_myname(), remote_machine,
+       ret = cli_full_connection( &the_cli, lp_netbios_name(), remote_machine,
                &rm_addr, 0, "IPC$", "IPC",
                "", /* username */
                "", /* domain */
@@ -2529,7 +2482,7 @@ static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
                return false;
        }
 
-       if ( the_cli->protocol != PROTOCOL_NT1 ) {
+       if ( smbXcli_conn_protocol(the_cli->conn) != PROTOCOL_NT1 ) {
                DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
                cli_shutdown(the_cli);
                return false;
@@ -2584,7 +2537,7 @@ static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
                /* the +2 is to strip the leading 2 backslashs */
                fstrcpy(unix_printer, printer + 2);
 
-               chan = talloc_zero(back_channels, struct notify_back_channel);
+               chan = talloc_zero(NULL, struct notify_back_channel);
                if (!chan) {
                        return false;
                }
@@ -2594,16 +2547,21 @@ static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
                        TALLOC_FREE(chan);
                        return false;
                }
-               chan->binding_handle = chan->cli_pipe->binding_handle;
 
                DLIST_ADD(back_channels, chan);
 
                messaging_register(msg_ctx, NULL, MSG_PRINTER_NOTIFY2,
                                   receive_notify2_message_list);
-               /* Tell the connections db we're now interested in printer
-                * notify messages. */
-               serverid_register_msg_flags(messaging_server_id(msg_ctx),
-                                           true, FLAG_MSG_PRINT_NOTIFY);
+       }
+
+       if (chan->cli_pipe == NULL ||
+           chan->cli_pipe->binding_handle == NULL) {
+               DEBUG(0, ("srv_spoolss_replyopenprinter: error - "
+                       "NULL %s for printer %s\n",
+                       chan->cli_pipe == NULL ?
+                       "chan->cli_pipe" : "chan->cli_pipe->binding_handle",
+                       printer));
+               return false;
        }
 
        /*
@@ -2616,7 +2574,7 @@ static bool srv_spoolss_replyopenprinter(int snum, const char *printer,
                          printer));
        }
 
-       status = dcerpc_spoolss_ReplyOpenPrinter(chan->binding_handle,
+       status = dcerpc_spoolss_ReplyOpenPrinter(chan->cli_pipe->binding_handle,
                                                 talloc_tos(),
                                                 printer,
                                                 localprinter,
@@ -2705,6 +2663,7 @@ WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
        int snum = -1;
        struct spoolss_NotifyOption *option = r->in.notify_options;
        struct sockaddr_storage client_ss;
+       ssize_t client_len;
 
        /* store the notify value in the printer struct */
 
@@ -2736,7 +2695,8 @@ WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
                return WERR_BADFID;
 
        DEBUG(10,("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
-               "client_address is %s\n", p->client_id->addr));
+                 "remote_address is %s\n",
+                 tsocket_address_string(p->remote_address, p->mem_ctx)));
 
        if (!lp_print_notify_backchannel(snum)) {
                DEBUG(10, ("_spoolss_RemoteFindFirstPrinterChangeNotifyEx: "
@@ -2744,9 +2704,11 @@ WERROR _spoolss_RemoteFindFirstPrinterChangeNotifyEx(struct pipes_struct *p,
                return WERR_SERVER_UNAVAILABLE;
        }
 
-       if (!interpret_string_addr(&client_ss, p->client_id->addr,
-                                  AI_NUMERICHOST)) {
-               return WERR_SERVER_UNAVAILABLE;
+       client_len = tsocket_address_bsd_sockaddr(p->remote_address,
+                                                 (struct sockaddr *) &client_ss,
+                                                 sizeof(struct sockaddr_storage));
+       if (client_len < 0) {
+               return WERR_NOMEM;
        }
 
        if(!srv_spoolss_replyopenprinter(snum, Printer->notify.localmachine,
@@ -2808,7 +2770,7 @@ static void spoolss_notify_share_name(struct messaging_context *msg_ctx,
                                      struct spoolss_PrinterInfo2 *pinfo2,
                                      TALLOC_CTX *mem_ctx)
 {
-       SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(snum));
+       SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, lp_servicename(talloc_tos(), snum));
 }
 
 /*******************************************************************
@@ -2854,7 +2816,7 @@ static void spoolss_notify_comment(struct messaging_context *msg_ctx,
        const char *p;
 
        if (*pinfo2->comment == '\0') {
-               p = lp_comment(snum);
+               p = lp_comment(talloc_tos(), snum);
        } else {
                p = pinfo2->comment;
        }
@@ -2874,7 +2836,21 @@ static void spoolss_notify_location(struct messaging_context *msg_ctx,
                                    struct spoolss_PrinterInfo2 *pinfo2,
                                    TALLOC_CTX *mem_ctx)
 {
-       SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, pinfo2->location);
+       const char *loc = pinfo2->location;
+       NTSTATUS status;
+
+       status = printer_list_get_printer(mem_ctx,
+                                         pinfo2->sharename,
+                                         NULL,
+                                         &loc,
+                                         NULL);
+       if (NT_STATUS_IS_OK(status)) {
+               if (loc == NULL) {
+                       loc = pinfo2->location;
+               }
+       }
+
+       SETUP_SPOOLSS_NOTIFY_DATA_STRING(data, loc);
 }
 
 /*******************************************************************
@@ -3234,7 +3210,7 @@ static void spoolss_notify_job_position(struct messaging_context *msg_ctx,
                                        struct spoolss_PrinterInfo2 *pinfo2,
                                        TALLOC_CTX *mem_ctx)
 {
-       SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->job);
+       SETUP_SPOOLSS_NOTIFY_DATA_INTEGER(data, queue->sysjob);
 }
 
 /*******************************************************************
@@ -3402,13 +3378,12 @@ static bool construct_notify_printer_info(struct messaging_context *msg_ctx,
        uint16_t field;
 
        struct spoolss_Notify *current_data;
-       print_queue_struct *queue=NULL;
 
        type = option_type->type;
 
        DEBUG(4,("construct_notify_printer_info: Notify type: [%s], number of notify info: [%d] on printer: [%s]\n",
                (type == PRINTER_NOTIFY_TYPE ? "PRINTER_NOTIFY_TYPE" : "JOB_NOTIFY_TYPE"),
-               option_type->count, lp_servicename(snum)));
+               option_type->count, lp_servicename(talloc_tos(), snum)));
 
        for(field_num=0; field_num < option_type->count; field_num++) {
                field = option_type->fields[field_num].field;
@@ -3418,7 +3393,7 @@ static bool construct_notify_printer_info(struct messaging_context *msg_ctx,
                if (!search_notify(type, field, &j) )
                        continue;
 
-               info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
+               info->notifies = talloc_realloc(info, info->notifies,
                                                      struct spoolss_Notify,
                                                      info->count + 1);
                if (info->notifies == NULL) {
@@ -3436,7 +3411,7 @@ static bool construct_notify_printer_info(struct messaging_context *msg_ctx,
                           pinfo2->printername));
 
                notify_info_data_table[j].fn(msg_ctx, snum, current_data,
-                                            queue, pinfo2, mem_ctx);
+                                            NULL, pinfo2, mem_ctx);
 
                info->count++;
        }
@@ -3478,7 +3453,7 @@ static bool construct_notify_jobs_info(struct messaging_context *msg_ctx,
                if (!search_notify(type, field, &j) )
                        continue;
 
-               info->notifies = TALLOC_REALLOC_ARRAY(info, info->notifies,
+               info->notifies = talloc_realloc(info, info->notifies,
                                                      struct spoolss_Notify,
                                                      info->count + 1);
                if (info->notifies == NULL) {
@@ -3572,15 +3547,15 @@ static WERROR printserver_notify_info(struct pipes_struct *p,
                        }
 
                        /* Maybe we should use the SYSTEM session_info here... */
-                       result = winreg_get_printer(mem_ctx,
+                       result = winreg_get_printer_internal(mem_ctx,
                                                    get_session_info_system(),
                                                    p->msg_ctx,
-                                                   lp_servicename(snum),
+                                                   lp_servicename(talloc_tos(), snum),
                                                    &pinfo2);
                        if (!W_ERROR_IS_OK(result)) {
                                DEBUG(4, ("printserver_notify_info: "
                                          "Failed to get printer [%s]\n",
-                                         lp_servicename(snum)));
+                                         lp_servicename(talloc_tos(), snum)));
                                continue;
                        }
 
@@ -3660,14 +3635,23 @@ static WERROR printer_notify_info(struct pipes_struct *p,
        }
 
        /* Maybe we should use the SYSTEM session_info here... */
-       result = winreg_get_printer(mem_ctx,
+       result = winreg_get_printer_internal(mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
-                                   lp_servicename(snum), &pinfo2);
+                                   lp_servicename(talloc_tos(), snum), &pinfo2);
        if (!W_ERROR_IS_OK(result)) {
                return WERR_BADFID;
        }
 
+       /*
+        * When sending a PRINTER_NOTIFY_FIELD_SERVER_NAME we should send the
+        * correct servername.
+        */
+       pinfo2->servername = talloc_strdup(pinfo2, Printer->servername);
+       if (pinfo2->servername == NULL) {
+               return WERR_NOMEM;
+       }
+
        for (i=0; i<option->count; i++) {
                option_type = option->types[i];
 
@@ -3692,7 +3676,7 @@ static WERROR printer_notify_info(struct pipes_struct *p,
                                                           &queue[j], info,
                                                           pinfo2, snum,
                                                           &option_type,
-                                                          queue[j].job,
+                                                          queue[j].sysjob,
                                                           mem_ctx);
                        }
 
@@ -3836,7 +3820,7 @@ static void compose_devicemode_devicename(struct spoolss_DeviceMode *dm,
  ********************************************************************/
 
 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
-                                     const struct auth_serversupplied_info *session_info,
+                                     const struct auth_session_info *session_info,
                                      struct messaging_context *msg_ctx,
                                      struct spoolss_PrinterInfo2 *info2,
                                      const char *servername,
@@ -3909,7 +3893,7 @@ static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
        r->high_part_total_bytes        = 0x0;
 
        /* ChangeID in milliseconds*/
-       winreg_printer_get_changeid(mem_ctx, session_info, msg_ctx,
+       winreg_printer_get_changeid_internal(mem_ctx, session_info, msg_ctx,
                                    info2->sharename, &r->change_id);
 
        r->last_error                   = WERR_OK;
@@ -3943,7 +3927,7 @@ static WERROR construct_printer_info1(TALLOC_CTX *mem_ctx,
        r->flags                = flags;
 
        if (info2->comment == NULL || info2->comment[0] == '\0') {
-               r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
+               r->comment      = lp_comment(mem_ctx, snum);
        } else {
                r->comment      = talloc_strdup(mem_ctx, info2->comment); /* saved comment */
        }
@@ -3993,7 +3977,7 @@ static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
                return result;
        }
 
-       r->sharename            = talloc_strdup(mem_ctx, lp_servicename(snum));
+       r->sharename            = lp_servicename(mem_ctx, snum);
        W_ERROR_HAVE_NO_MEMORY(r->sharename);
        r->portname             = talloc_strdup(mem_ctx, info2->portname);
        W_ERROR_HAVE_NO_MEMORY(r->portname);
@@ -4001,14 +3985,30 @@ static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
        W_ERROR_HAVE_NO_MEMORY(r->drivername);
 
        if (info2->comment[0] == '\0') {
-               r->comment      = talloc_strdup(mem_ctx, lp_comment(snum));
+               r->comment      = lp_comment(mem_ctx, snum);
        } else {
                r->comment      = talloc_strdup(mem_ctx, info2->comment);
        }
        W_ERROR_HAVE_NO_MEMORY(r->comment);
 
-       r->location             = talloc_strdup(mem_ctx, info2->location);
+       r->location     = talloc_strdup(mem_ctx, info2->location);
+       if (info2->location[0] == '\0') {
+               const char *loc = NULL;
+               NTSTATUS nt_status;
+
+               nt_status = printer_list_get_printer(mem_ctx,
+                                                    info2->sharename,
+                                                    NULL,
+                                                    &loc,
+                                                    NULL);
+               if (NT_STATUS_IS_OK(nt_status)) {
+                       if (loc != NULL) {
+                               r->location = talloc_strdup(mem_ctx, loc);
+                       }
+               }
+       }
        W_ERROR_HAVE_NO_MEMORY(r->location);
+
        r->sepfile              = talloc_strdup(mem_ctx, info2->sepfile);
        W_ERROR_HAVE_NO_MEMORY(r->sepfile);
        r->printprocessor       = talloc_strdup(mem_ctx, info2->printprocessor);
@@ -4028,8 +4028,22 @@ static WERROR construct_printer_info2(TALLOC_CTX *mem_ctx,
        r->cjobs                = count;
        r->averageppm           = info2->averageppm;
 
-       copy_devicemode(mem_ctx, info2->devmode, &r->devmode);
-       if (!r->devmode) {
+       if (info2->devmode != NULL) {
+               result = copy_devicemode(mem_ctx,
+                                        info2->devmode,
+                                        &r->devmode);
+               if (!W_ERROR_IS_OK(result)) {
+                       return result;
+               }
+       } else if (lp_default_devmode(snum)) {
+               result = spoolss_create_default_devmode(mem_ctx,
+                                                       info2->printername,
+                                                       &r->devmode);
+               if (!W_ERROR_IS_OK(result)) {
+                       return result;
+               }
+       } else {
+               r->devmode = NULL;
                DEBUG(8,("Returning NULL Devicemode!\n"));
        }
 
@@ -4143,10 +4157,9 @@ static WERROR construct_printer_info6(TALLOC_CTX *mem_ctx,
                                      struct spoolss_PrinterInfo6 *r,
                                      int snum)
 {
-       int count;
        print_status_struct status;
 
-       count = print_queue_length(msg_ctx, snum, &status);
+       print_queue_length(msg_ctx, snum, &status);
 
        r->status = nt_printq_status(status.status);
 
@@ -4164,20 +4177,12 @@ static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
                                      struct spoolss_PrinterInfo7 *r,
                                      int snum)
 {
-       struct auth_serversupplied_info *session_info;
+       const struct auth_session_info *session_info = get_session_info_system();
        struct GUID guid;
-       NTSTATUS status;
-
-       status = make_session_info_system(mem_ctx, &session_info);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("construct_printer_info7: "
-                         "Could not create system session_info\n"));
-               return WERR_NOMEM;
-       }
 
        if (is_printer_published(mem_ctx, session_info, msg_ctx,
                                 servername,
-                                lp_servicename(snum), &guid, NULL)) {
+                                lp_servicename(talloc_tos(), snum), &guid, NULL)) {
                r->guid = talloc_strdup_upper(mem_ctx, GUID_string2(mem_ctx, &guid));
                r->action = DSPRINT_PUBLISH;
        } else {
@@ -4186,7 +4191,6 @@ static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
        }
        W_ERROR_HAVE_NO_MEMORY(r->guid);
 
-       TALLOC_FREE(session_info);
        return WERR_OK;
 }
 
@@ -4209,8 +4213,22 @@ static WERROR construct_printer_info8(TALLOC_CTX *mem_ctx,
                return result;
        }
 
-       copy_devicemode(mem_ctx, info2->devmode, &r->devmode);
-       if (!r->devmode) {
+       if (info2->devmode != NULL) {
+               result = copy_devicemode(mem_ctx,
+                                        info2->devmode,
+                                        &r->devmode);
+               if (!W_ERROR_IS_OK(result)) {
+                       return result;
+               }
+       } else if (lp_default_devmode(snum)) {
+               result = spoolss_create_default_devmode(mem_ctx,
+                                                       info2->printername,
+                                                       &r->devmode);
+               if (!W_ERROR_IS_OK(result)) {
+                       return result;
+               }
+       } else {
+               r->devmode = NULL;
                DEBUG(8,("Returning NULL Devicemode!\n"));
        }
 
@@ -4233,7 +4251,7 @@ static bool snum_is_shared_printer(int snum)
 ********************************************************************/
 
 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
-                                          const struct auth_serversupplied_info *session_info,
+                                          const struct auth_session_info *session_info,
                                           struct messaging_context *msg_ctx,
                                           const char *servername,
                                           uint32_t level,
@@ -4246,6 +4264,13 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
        union spoolss_PrinterInfo *info = NULL;
        uint32_t count = 0;
        WERROR result = WERR_OK;
+       struct dcerpc_binding_handle *b = NULL;
+       TALLOC_CTX *tmp_ctx = NULL;
+
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
 
        *count_p = 0;
        *info_p = NULL;
@@ -4264,15 +4289,23 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
                DEBUG(4,("Found a printer in smb.conf: %s[%x]\n",
                        printer, snum));
 
-               result = winreg_create_printer(mem_ctx,
-                                              session_info,
-                                              msg_ctx,
+               if (b == NULL) {
+                       result = winreg_printer_binding_handle(tmp_ctx,
+                                                              session_info,
+                                                              msg_ctx,
+                                                              &b);
+                       if (!W_ERROR_IS_OK(result)) {
+                               goto out;
+                       }
+               }
+
+               result = winreg_create_printer(tmp_ctx, b,
                                               printer);
                if (!W_ERROR_IS_OK(result)) {
                        goto out;
                }
 
-               info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+               info = talloc_realloc(tmp_ctx, info,
                                            union spoolss_PrinterInfo,
                                            count + 1);
                if (!info) {
@@ -4280,7 +4313,7 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
                        goto out;
                }
 
-               result = winreg_get_printer(mem_ctx, session_info, msg_ctx,
+               result = winreg_get_printer(tmp_ctx, b,
                                            printer, &info2);
                if (!W_ERROR_IS_OK(result)) {
                        goto out;
@@ -4326,18 +4359,15 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
                count++;
        }
 
-       *count_p = count;
-       *info_p = info;
-
- out:
-       if (!W_ERROR_IS_OK(result)) {
-               TALLOC_FREE(info);
-               return result;
+out:
+       if (W_ERROR_IS_OK(result)) {
+               *info_p = talloc_move(mem_ctx, &info);
+               *count_p = count;
        }
 
-       *info_p = info;
+       talloc_free(tmp_ctx);
 
-       return WERR_OK;
+       return result;
 }
 
 /********************************************************************
@@ -4345,7 +4375,7 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
-                                 const struct auth_serversupplied_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4363,7 +4393,7 @@ static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
 ********************************************************************/
 
 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
-                                      const struct auth_serversupplied_info *session_info,
+                                      const struct auth_session_info *session_info,
                                       struct messaging_context *msg_ctx,
                                       const char *servername,
                                       uint32_t flags,
@@ -4381,7 +4411,7 @@ static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
 *********************************************************************/
 
 static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
-                                            const struct auth_serversupplied_info *session_info,
+                                            const struct auth_session_info *session_info,
                                             struct messaging_context *msg_ctx,
                                             const char *servername,
                                             union spoolss_PrinterInfo **info,
@@ -4398,7 +4428,7 @@ static WERROR enum_all_printers_info_1_local(TALLOC_CTX *mem_ctx,
 *********************************************************************/
 
 static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
-                                           const struct auth_serversupplied_info *session_info,
+                                           const struct auth_session_info *session_info,
                                            struct messaging_context *msg_ctx,
                                            const char *servername,
                                            union spoolss_PrinterInfo **info,
@@ -4408,7 +4438,8 @@ static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
 
        DEBUG(4,("enum_all_printers_info_1_name\n"));
 
-       if ((servername[0] == '\\') && (servername[1] == '\\')) {
+       if (servername != NULL &&
+           (servername[0] == '\\') && (servername[1] == '\\')) {
                s = servername + 2;
        }
 
@@ -4425,7 +4456,7 @@ static WERROR enum_all_printers_info_1_name(TALLOC_CTX *mem_ctx,
 *********************************************************************/
 
 static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
-                                              const struct auth_serversupplied_info *session_info,
+                                              const struct auth_session_info *session_info,
                                               struct messaging_context *msg_ctx,
                                               const char *servername,
                                               union spoolss_PrinterInfo **info,
@@ -4443,7 +4474,8 @@ static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
           listed. Windows responds to this call with a
           WERR_CAN_NOT_COMPLETE so we should do the same. */
 
-       if (servername[0] == '\\' && servername[1] == '\\') {
+       if (servername != NULL &&
+           (servername[0] == '\\') && (servername[1] == '\\')) {
                 s = servername + 2;
        }
 
@@ -4462,7 +4494,7 @@ static WERROR enum_all_printers_info_1_network(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
-                                      const struct auth_serversupplied_info *session_info,
+                                      const struct auth_session_info *session_info,
                                       struct messaging_context *msg_ctx,
                                       const char *servername,
                                       union spoolss_PrinterInfo **info,
@@ -4479,7 +4511,7 @@ static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
-                                 const struct auth_serversupplied_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4513,7 +4545,7 @@ static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
-                                 const struct auth_serversupplied_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4549,7 +4581,7 @@ static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
-                                 const struct auth_serversupplied_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4568,7 +4600,7 @@ static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
-                                 const struct auth_serversupplied_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4588,7 +4620,7 @@ static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
 WERROR _spoolss_EnumPrinters(struct pipes_struct *p,
                             struct spoolss_EnumPrinters *r)
 {
-       const struct auth_serversupplied_info *session_info = get_session_info_system();
+       const struct auth_session_info *session_info = get_session_info_system();
        WERROR result;
 
        /* that's an [in out] buffer */
@@ -4697,7 +4729,7 @@ WERROR _spoolss_GetPrinter(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
-       result = winreg_get_printer(p->mem_ctx,
+       result = winreg_get_printer_internal(p->mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
                                    lp_const_servicename(snum),
@@ -4761,6 +4793,7 @@ WERROR _spoolss_GetPrinter(struct pipes_struct *p,
                result = WERR_UNKNOWN_LEVEL;
                break;
        }
+       TALLOC_FREE(info2);
 
  out:
        if (!W_ERROR_IS_OK(result)) {
@@ -4834,8 +4867,10 @@ static WERROR string_array_from_driver_info(TALLOC_CTX *mem_ctx,
                             &array, &num_strings);
        }
 
-       if (presult) {
+       if (presult != NULL) {
                *presult = array;
+       } else {
+               talloc_free(array);
        }
 
        return WERR_OK;
@@ -5312,7 +5347,7 @@ static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
        *count_p = 0;
 
        if (strlen(driver->driver_path)) {
-               info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+               info = talloc_realloc(mem_ctx, info,
                                            struct spoolss_DriverFileInfo,
                                            count + 1);
                W_ERROR_HAVE_NO_MEMORY(info);
@@ -5327,7 +5362,7 @@ static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
        }
 
        if (strlen(driver->config_file)) {
-               info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+               info = talloc_realloc(mem_ctx, info,
                                            struct spoolss_DriverFileInfo,
                                            count + 1);
                W_ERROR_HAVE_NO_MEMORY(info);
@@ -5342,7 +5377,7 @@ static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
        }
 
        if (strlen(driver->data_file)) {
-               info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+               info = talloc_realloc(mem_ctx, info,
                                            struct spoolss_DriverFileInfo,
                                            count + 1);
                W_ERROR_HAVE_NO_MEMORY(info);
@@ -5357,7 +5392,7 @@ static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
        }
 
        if (strlen(driver->help_file)) {
-               info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+               info = talloc_realloc(mem_ctx, info,
                                            struct spoolss_DriverFileInfo,
                                            count + 1);
                W_ERROR_HAVE_NO_MEMORY(info);
@@ -5372,7 +5407,7 @@ static WERROR spoolss_DriverFileInfo_from_driver(TALLOC_CTX *mem_ctx,
        }
 
        for (i=0; driver->dependent_files[i] && driver->dependent_files[i][0] != '\0'; i++) {
-               info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+               info = talloc_realloc(mem_ctx, info,
                                            struct spoolss_DriverFileInfo,
                                            count + 1);
                W_ERROR_HAVE_NO_MEMORY(info);
@@ -5458,7 +5493,7 @@ static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
-                                                 const struct auth_serversupplied_info *session_info,
+                                                 const struct auth_session_info *session_info,
                                                  struct messaging_context *msg_ctx,
                                                  uint32_t level,
                                                  union spoolss_DriverInfo *r,
@@ -5470,14 +5505,27 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
        struct spoolss_PrinterInfo2 *pinfo2 = NULL;
        struct spoolss_DriverInfo8 *driver;
        WERROR result;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        if (level == 101) {
                return WERR_UNKNOWN_LEVEL;
        }
 
-       result = winreg_get_printer(mem_ctx,
-                                   session_info,
-                                   msg_ctx,
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              session_info,
+                                              msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
+       }
+
+       result = winreg_get_printer(tmp_ctx, b,
                                    lp_const_servicename(snum),
                                    &pinfo2);
 
@@ -5485,10 +5533,11 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
                win_errstr(result)));
 
        if (!W_ERROR_IS_OK(result)) {
-               return WERR_INVALID_PRINTER_NAME;
+               result = WERR_INVALID_PRINTER_NAME;
+               goto done;
        }
 
-       result = winreg_get_driver(mem_ctx, session_info, msg_ctx,
+       result = winreg_get_driver(tmp_ctx, b,
                                   architecture,
                                   pinfo2->drivername, version, &driver);
 
@@ -5501,24 +5550,26 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
                 */
 
                if (version < 3) {
-                       talloc_free(pinfo2);
-                       return WERR_UNKNOWN_PRINTER_DRIVER;
+                       result = WERR_UNKNOWN_PRINTER_DRIVER;
+                       goto done;
                }
 
                /* Yes - try again with a WinNT driver. */
                version = 2;
-               result = winreg_get_driver(mem_ctx, session_info, msg_ctx,
+               result = winreg_get_driver(tmp_ctx, b,
                                           architecture,
                                           pinfo2->drivername,
                                           version, &driver);
                DEBUG(8,("construct_printer_driver_level: status: %s\n",
                        win_errstr(result)));
                if (!W_ERROR_IS_OK(result)) {
-                       talloc_free(pinfo2);
-                       return WERR_UNKNOWN_PRINTER_DRIVER;
+                       result = WERR_UNKNOWN_PRINTER_DRIVER;
+                       goto done;
                }
        }
 
+       /* these are allocated on mem_ctx and not tmp_ctx because they are
+        * the 'return value' and need to utlive this call */
        switch (level) {
        case 1:
                result = fill_printer_driver_info1(mem_ctx, &r->info1, driver, servername);
@@ -5551,9 +5602,8 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
                break;
        }
 
-       talloc_free(pinfo2);
-       talloc_free(driver);
-
+done:
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -5566,6 +5616,7 @@ WERROR _spoolss_GetPrinterDriver2(struct pipes_struct *p,
 {
        struct printer_handle *printer;
        WERROR result;
+       uint32_t version = r->in.client_major_version;
 
        int snum;
 
@@ -5590,13 +5641,19 @@ WERROR _spoolss_GetPrinterDriver2(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
+       if (r->in.client_major_version == SPOOLSS_DRIVER_VERSION_2012) {
+               DEBUG(3,("_spoolss_GetPrinterDriver2: v4 driver requested, "
+                       "downgrading to v3\n"));
+               version = SPOOLSS_DRIVER_VERSION_200X;
+       }
+
        result = construct_printer_driver_info_level(p->mem_ctx,
                                                     get_session_info_system(),
                                                     p->msg_ctx,
                                                     r->in.level, r->out.info,
                                                     snum, printer->servername,
                                                     r->in.architecture,
-                                                    r->in.client_major_version);
+                                                    version);
        if (!W_ERROR_IS_OK(result)) {
                TALLOC_FREE(r->out.info);
                return result;
@@ -5666,6 +5723,8 @@ WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
        int snum;
        struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
        WERROR werr;
+       char *rhost;
+       int rc;
 
        if (!Printer) {
                DEBUG(2,("_spoolss_StartDocPrinter: "
@@ -5681,11 +5740,11 @@ WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
                return WERR_INVALID_HANDLE;
        }
 
-       if (r->in.level != 1) {
+       if (r->in.info_ctr->level != 1) {
                return WERR_UNKNOWN_LEVEL;
        }
 
-       info_1 = r->in.info.info1;
+       info_1 = r->in.info_ctr->info.info1;
 
        /*
         * a nice thing with NT is it doesn't listen to what you tell it.
@@ -5707,9 +5766,23 @@ WERROR _spoolss_StartDocPrinter(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
+       rc = get_remote_hostname(p->remote_address,
+                                &rhost,
+                                p->mem_ctx);
+       if (rc < 0) {
+               return WERR_NOMEM;
+       }
+       if (strequal(rhost,"UNKNOWN")) {
+               rhost = tsocket_address_inet_addr_string(p->remote_address,
+                                                        p->mem_ctx);
+               if (rhost == NULL) {
+                       return WERR_NOMEM;
+               }
+       }
+
        werr = print_job_start(p->session_info,
                               p->msg_ctx,
-                              p->client_id->name,
+                              rhost,
                               snum,
                               info_1->document_name,
                               info_1->output_file,
@@ -5810,7 +5883,7 @@ WERROR _spoolss_WritePrinter(struct pipes_struct *p,
 static WERROR control_printer(struct policy_handle *handle, uint32_t command,
                              struct pipes_struct *p)
 {
-       const struct auth_serversupplied_info *session_info = p->session_info;
+       const struct auth_session_info *session_info = p->session_info;
        int snum;
        WERROR errcode = WERR_BADFUNC;
        struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
@@ -5891,8 +5964,9 @@ static WERROR update_printer_sec(struct policy_handle *handle,
        const char *printer;
        WERROR result;
        int snum;
-
        struct printer_handle *Printer = find_printer_index_by_hnd(p, handle);
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
                DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
@@ -5920,15 +5994,26 @@ static WERROR update_printer_sec(struct policy_handle *handle,
                goto done;
        }
 
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
+       }
+
        /* NT seems to like setting the security descriptor even though
           nothing may have actually changed. */
-       result = winreg_get_printer_secdesc(p->mem_ctx,
-                                           get_session_info_system(),
-                                           p->msg_ctx,
+       result = winreg_get_printer_secdesc(tmp_ctx, b,
                                            printer,
                                            &old_secdesc);
        if (!W_ERROR_IS_OK(result)) {
-               DEBUG(2,("update_printer_sec: winreg_get_printer_secdesc() failed\n"));
+               DEBUG(2,("update_printer_sec: winreg_get_printer_secdesc_internal() failed\n"));
                result = WERR_BADFID;
                goto done;
        }
@@ -5963,7 +6048,7 @@ static WERROR update_printer_sec(struct policy_handle *handle,
                }
        }
 
-       new_secdesc = sec_desc_merge(p->mem_ctx, secdesc_ctr->sd, old_secdesc);
+       new_secdesc = sec_desc_merge(tmp_ctx, secdesc_ctr->sd, old_secdesc);
        if (new_secdesc == NULL) {
                result = WERR_NOMEM;
                goto done;
@@ -5974,13 +6059,12 @@ static WERROR update_printer_sec(struct policy_handle *handle,
                goto done;
        }
 
-       result = winreg_set_printer_secdesc(p->mem_ctx,
-                                           get_session_info_system(),
-                                           p->msg_ctx,
+       result = winreg_set_printer_secdesc(tmp_ctx, b,
                                            printer,
                                            new_secdesc);
 
- done:
+done:
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -6002,7 +6086,7 @@ static bool check_printer_ok(TALLOC_CTX *mem_ctx,
                info2->location));
 
        /* we force some elements to "correct" values */
-       info2->servername = talloc_asprintf(mem_ctx, "\\\\%s", global_myname());
+       info2->servername = talloc_asprintf(mem_ctx, "\\\\%s", lp_netbios_name());
        if (info2->servername == NULL) {
                return false;
        }
@@ -6014,7 +6098,7 @@ static bool check_printer_ok(TALLOC_CTX *mem_ctx,
        /* check to see if we allow printername != sharename */
        if (lp_force_printername(snum)) {
                info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
-                                       global_myname(), info2->sharename);
+                                       lp_netbios_name(), info2->sharename);
        } else {
                /* make sure printername is in \\server\printername format */
                fstrcpy(printername, info2->printername);
@@ -6025,7 +6109,7 @@ static bool check_printer_ok(TALLOC_CTX *mem_ctx,
                }
 
                info2->printername = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
-                                       global_myname(), p);
+                                       lp_netbios_name(), p);
        }
        if (info2->printername == NULL) {
                return false;
@@ -6042,7 +6126,7 @@ static bool check_printer_ok(TALLOC_CTX *mem_ctx,
 
 static WERROR add_port_hook(TALLOC_CTX *ctx, struct security_token *token, const char *portname, const char *uri)
 {
-       char *cmd = lp_addport_cmd();
+       char *cmd = lp_addport_cmd(talloc_tos());
        char *command = NULL;
        int ret;
        bool is_print_op = false;
@@ -6088,12 +6172,22 @@ static WERROR add_port_hook(TALLOC_CTX *ctx, struct security_token *token, const
 /****************************************************************************
 ****************************************************************************/
 
+static bool spoolss_conn_snum_used(struct smbd_server_connection *sconn,
+                                  int snum)
+{
+       /*
+        * As we do not know if we are embedded in the file server process
+        * or not, we have to pretend that all shares are in use.
+        */
+       return true;
+}
+
 static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
                             struct spoolss_SetPrinterInfo2 *info2,
                             const char *remote_machine,
                             struct messaging_context *msg_ctx)
 {
-       char *cmd = lp_addprinter_cmd();
+       char *cmd = lp_addprinter_cmd(talloc_tos());
        char **qlines;
        char *command = NULL;
        int numlines;
@@ -6146,7 +6240,7 @@ static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
 
        /* reload our services immediately */
        become_root();
-       reload_services(msg_ctx, -1, false);
+       reload_services(NULL, spoolss_conn_snum_used, false);
        unbecome_root();
 
        numlines = 0;
@@ -6169,7 +6263,7 @@ static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
 }
 
 static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
-                              const struct auth_serversupplied_info *session_info,
+                              const struct auth_session_info *session_info,
                               struct messaging_context *msg_ctx,
                               int snum,
                               struct spoolss_SetPrinterInfo2 *printer,
@@ -6182,18 +6276,43 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        const char *spooling;
        DATA_BLOB buffer;
        WERROR result = WERR_OK;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
+       bool ok;
+
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              session_info,
+                                              msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
+       }
 
-       if (force_update || !strequal(printer->drivername, old_printer->drivername)) {
-               push_reg_sz(mem_ctx, &buffer, printer->drivername);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+       if (printer->drivername != NULL &&
+           (force_update ||
+            !strequal(printer->drivername, old_printer->drivername))) {
+               ok = push_reg_sz(tmp_ctx, &buffer, printer->drivername);
+               if (!ok) {
+                       DEBUG(0, ("%s data corrupted\n", SPOOL_REG_DRIVERNAME));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_DRIVERNAME,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DRIVERNAME));
+                       goto done;
+               }
 
                if (!force_update) {
                        DEBUG(10,("update_printer: changing driver [%s]!  Sending event!\n",
@@ -6205,17 +6324,26 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || !strequal(printer->comment, old_printer->comment)) {
-               push_reg_sz(mem_ctx, &buffer, printer->comment);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+       if (printer->comment != NULL &&
+           (force_update ||
+            !strequal(printer->comment, old_printer->comment))) {
+               ok = push_reg_sz(tmp_ctx, &buffer, printer->comment);
+               if (!ok) {
+                       DEBUG(0, ("comment data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_DESCRIPTION,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_DESCRIPTION));
+                       goto done;
+               }
 
                if (!force_update) {
                        notify_printer_comment(server_event_context(), msg_ctx,
@@ -6224,17 +6352,26 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || !strequal(printer->sharename, old_printer->sharename)) {
-               push_reg_sz(mem_ctx, &buffer, printer->sharename);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+       if (printer->sharename != NULL &&
+           (force_update ||
+            !strequal(printer->sharename, old_printer->sharename))) {
+               ok = push_reg_sz(tmp_ctx, &buffer, printer->sharename);
+               if (!ok) {
+                       DEBUG(0, ("sharename data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSHARENAME,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
+                       goto done;
+               }
 
                if (!force_update) {
                        notify_printer_sharename(server_event_context(),
@@ -6244,7 +6381,9 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || !strequal(printer->printername, old_printer->printername)) {
+       if (printer->printername != NULL &&
+           (force_update ||
+            !strequal(printer->printername, old_printer->printername))) {
                const char *p;
 
                p = strrchr(printer->printername, '\\' );
@@ -6254,16 +6393,23 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                        p = printer->printername;
                }
 
-               push_reg_sz(mem_ctx, &buffer, p);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               ok = push_reg_sz(tmp_ctx, &buffer, p);
+               if (!ok) {
+                       DEBUG(0, ("printername data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTERNAME,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSHARENAME));
+                       goto done;
+               }
 
                if (!force_update) {
                        notify_printer_printername(server_event_context(),
@@ -6271,17 +6417,26 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || !strequal(printer->portname, old_printer->portname)) {
-               push_reg_sz(mem_ctx, &buffer, printer->portname);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+       if (printer->portname != NULL &&
+           (force_update ||
+            !strequal(printer->portname, old_printer->portname))) {
+               ok = push_reg_sz(tmp_ctx, &buffer, printer->portname);
+               if (!ok) {
+                       DEBUG(0, ("portname data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PORTNAME,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PORTNAME));
+                       goto done;
+               }
 
                if (!force_update) {
                        notify_printer_port(server_event_context(),
@@ -6290,17 +6445,26 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || !strequal(printer->location, old_printer->location)) {
-               push_reg_sz(mem_ctx, &buffer, printer->location);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+       if (printer->location != NULL &&
+           (force_update ||
+            !strequal(printer->location, old_printer->location))) {
+               ok = push_reg_sz(tmp_ctx, &buffer, printer->location);
+               if (!ok) {
+                       DEBUG(0, ("location data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_LOCATION,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_LOCATION));
+                       goto done;
+               }
 
                if (!force_update) {
                        notify_printer_location(server_event_context(),
@@ -6310,17 +6474,26 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || !strequal(printer->sepfile, old_printer->sepfile)) {
-               push_reg_sz(mem_ctx, &buffer, printer->sepfile);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+       if (printer->sepfile != NULL &&
+           (force_update ||
+            !strequal(printer->sepfile, old_printer->sepfile))) {
+               ok = push_reg_sz(tmp_ctx, &buffer, printer->sepfile);
+               if (!ok) {
+                       DEBUG(0, ("sepfile data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSEPARATORFILE,
                                          REG_SZ,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSEPARATORFILE));
+                       goto done;
+               }
 
                if (!force_update) {
                        notify_printer_sepfile(server_event_context(),
@@ -6330,61 +6503,73 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
        }
 
-       if (force_update || printer->starttime != old_printer->starttime) {
-               buffer = data_blob_talloc(mem_ctx, NULL, 4);
+       if (printer->starttime != 0 &&
+           (force_update ||
+            printer->starttime != old_printer->starttime)) {
+               buffer = data_blob_talloc(tmp_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->starttime);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSTARTTIME,
                                          REG_DWORD,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTSTARTTIME));
+                       goto done;
+               }
        }
 
-       if (force_update || printer->untiltime != old_printer->untiltime) {
-               buffer = data_blob_talloc(mem_ctx, NULL, 4);
+       if (printer->untiltime != 0 &&
+           (force_update ||
+            printer->untiltime != old_printer->untiltime)) {
+               buffer = data_blob_talloc(tmp_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->untiltime);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTENDTIME,
                                          REG_DWORD,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
+                       goto done;
+               }
        }
 
        if (force_update || printer->priority != old_printer->priority) {
-               buffer = data_blob_talloc(mem_ctx, NULL, 4);
+               buffer = data_blob_talloc(tmp_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->priority);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRIORITY,
                                          REG_DWORD,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
+                       goto done;
+               }
        }
 
        if (force_update || printer->attributes != old_printer->attributes) {
-               buffer = data_blob_talloc(mem_ctx, NULL, 4);
+               buffer = data_blob_talloc(tmp_ctx, NULL, 4);
                SIVAL(buffer.data, 0, (printer->attributes &
                                       PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               result = winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTKEEPPRINTEDJOBS,
                                          REG_DWORD,
                                          buffer.data,
                                          buffer.length);
+               if (!W_ERROR_IS_OK(result)) {
+                       DEBUG(0, ("Failed to set %s\n", SPOOL_REG_PRINTENDTIME));
+                       goto done;
+               }
 
                switch (printer->attributes & 0x3) {
                        case 0:
@@ -6399,10 +6584,13 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                        default:
                                spooling = "unknown";
                }
-               push_reg_sz(mem_ctx, &buffer, spooling);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               ok = push_reg_sz(tmp_ctx, &buffer, spooling);
+               if (!ok) {
+                       DEBUG(0, ("printSpooling data corrupted\n"));
+                       result = WERR_INVALID_DATA;
+                       goto done;
+               }
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSPOOLING,
@@ -6411,53 +6599,75 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                                          buffer.length);
        }
 
-       push_reg_sz(mem_ctx, &buffer, global_myname());
-       winreg_set_printer_dataex(mem_ctx,
-                                 session_info,
-                                 msg_ctx,
+       ok = push_reg_sz(tmp_ctx, &buffer, lp_netbios_name());
+       if (!ok) {
+               DEBUG(0, ("shortServerName data corrupted\n"));
+               result = WERR_INVALID_DATA;
+               goto done;
+       }
+       result = winreg_set_printer_dataex(tmp_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_SHORTSERVERNAME,
                                  REG_SZ,
                                  buffer.data,
                                  buffer.length);
+       if (!W_ERROR_IS_OK(result)) {
+               DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SHORTSERVERNAME));
+               goto done;
+       }
 
        dnsdomname = get_mydnsfullname();
        if (dnsdomname != NULL && dnsdomname[0] != '\0') {
-               longname = talloc_strdup(mem_ctx, dnsdomname);
+               longname = talloc_strdup(tmp_ctx, dnsdomname);
        } else {
-               longname = talloc_strdup(mem_ctx, global_myname());
+               longname = talloc_strdup(tmp_ctx, lp_netbios_name());
        }
        if (longname == NULL) {
                result = WERR_NOMEM;
                goto done;
        }
 
-       push_reg_sz(mem_ctx, &buffer, longname);
-       winreg_set_printer_dataex(mem_ctx,
-                                 session_info,
-                                 msg_ctx,
-                                 printer->sharename,
-                                 SPOOL_DSSPOOLER_KEY,
-                                 SPOOL_REG_SERVERNAME,
-                                 REG_SZ,
-                                 buffer.data,
-                                 buffer.length);
+       ok = push_reg_sz(tmp_ctx, &buffer, longname);
+       if (!ok) {
+               DEBUG(0, ("longname data corrupted\n"));
+               result = WERR_INVALID_DATA;
+               goto done;
+       }
+       result = winreg_set_printer_dataex(tmp_ctx, b,
+                                          printer->sharename,
+                                          SPOOL_DSSPOOLER_KEY,
+                                          SPOOL_REG_SERVERNAME,
+                                          REG_SZ,
+                                          buffer.data,
+                                          buffer.length);
+       if (!W_ERROR_IS_OK(result)) {
+               DEBUG(0, ("Failed to set %s\n", SPOOL_REG_SERVERNAME));
+               goto done;
+       }
 
-       uncname = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
-                                 global_myname(), printer->sharename);
-       push_reg_sz(mem_ctx, &buffer, uncname);
-       winreg_set_printer_dataex(mem_ctx,
-                                 session_info,
-                                 msg_ctx,
+       uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
+                                 lp_netbios_name(), printer->sharename);
+       ok = push_reg_sz(tmp_ctx, &buffer, uncname);
+       if (!ok) {
+               DEBUG(0, ("uncName data corrupted\n"));
+               result = WERR_INVALID_DATA;
+               goto done;
+       }
+       result = winreg_set_printer_dataex(tmp_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_UNCNAME,
                                  REG_SZ,
                                  buffer.data,
                                  buffer.length);
+       if (!W_ERROR_IS_OK(result)) {
+               DEBUG(0, ("Failed to set %s\n", SPOOL_REG_UNCNAME));
+               goto done;
+       }
 
 done:
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -6478,6 +6688,7 @@ static WERROR update_printer(struct pipes_struct *p,
        int snum;
        WERROR result = WERR_OK;
        TALLOC_CTX *tmp_ctx;
+       struct dcerpc_binding_handle *b;
 
        DEBUG(8,("update_printer\n"));
 
@@ -6496,9 +6707,15 @@ static WERROR update_printer(struct pipes_struct *p,
                goto done;
        }
 
-       result = winreg_get_printer(tmp_ctx,
-                                   get_session_info_system(),
-                                   p->msg_ctx,
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
+       }
+
+       result = winreg_get_printer(tmp_ctx, b,
                                    lp_const_servicename(snum),
                                    &old_printer);
        if (!W_ERROR_IS_OK(result)) {
@@ -6525,36 +6742,45 @@ static WERROR update_printer(struct pipes_struct *p,
        /* Call addprinter hook */
        /* Check changes to see if this is really needed */
 
-       if (*lp_addprinter_cmd() &&
+       if (*lp_addprinter_cmd(talloc_tos()) &&
                        (!strequal(printer->drivername, old_printer->drivername) ||
                         !strequal(printer->comment, old_printer->comment) ||
                         !strequal(printer->portname, old_printer->portname) ||
                         !strequal(printer->location, old_printer->location)) )
        {
+               char *raddr;
+
+               raddr = tsocket_address_inet_addr_string(p->remote_address,
+                                                        p->mem_ctx);
+               if (raddr == NULL) {
+                       return WERR_NOMEM;
+               }
+
                /* add_printer_hook() will call reload_services() */
                if (!add_printer_hook(tmp_ctx, p->session_info->security_token,
-                                     printer, p->client_id->addr,
+                                     printer, raddr,
                                      p->msg_ctx)) {
                        result = WERR_ACCESS_DENIED;
                        goto done;
                }
        }
 
-       update_dsspooler(tmp_ctx,
-                        get_session_info_system(),
-                        p->msg_ctx,
-                        snum,
-                        printer,
-                        old_printer);
+       result = update_dsspooler(tmp_ctx,
+                                 get_session_info_system(),
+                                 p->msg_ctx,
+                                 snum,
+                                 printer,
+                                 old_printer);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
+       }
 
        printer_mask &= ~SPOOLSS_PRINTER_INFO_SECDESC;
 
        if (devmode == NULL) {
                printer_mask &= ~SPOOLSS_PRINTER_INFO_DEVMODE;
        }
-       result = winreg_update_printer(tmp_ctx,
-                                      get_session_info_system(),
-                                      p->msg_ctx,
+       result = winreg_update_printer(tmp_ctx, b,
                                       printer->sharename,
                                       printer_mask,
                                       printer,
@@ -6593,10 +6819,10 @@ static WERROR publish_or_unpublish_printer(struct pipes_struct *p,
        if (!get_printer_snum(p, handle, &snum, NULL))
                return WERR_BADFID;
 
-       result = winreg_get_printer(p->mem_ctx,
+       result = winreg_get_printer_internal(p->mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
-                                   lp_servicename(snum),
+                                   lp_servicename(talloc_tos(), snum),
                                    &pinfo2);
        if (!W_ERROR_IS_OK(result)) {
                return WERR_BADFID;
@@ -6642,7 +6868,7 @@ static WERROR update_printer_devmode(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       return winreg_update_printer(p->mem_ctx,
+       return winreg_update_printer_internal(p->mem_ctx,
                                     get_session_info_system(),
                                     p->msg_ctx,
                                     lp_const_servicename(snum),
@@ -6770,9 +6996,9 @@ static WERROR fill_job_info1(TALLOC_CTX *mem_ctx,
 
        t = gmtime(&queue->time);
 
-       r->job_id               = queue->job;
+       r->job_id               = queue->sysjob;
 
-       r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
+       r->printer_name         = lp_servicename(mem_ctx, snum);
        W_ERROR_HAVE_NO_MEMORY(r->printer_name);
        r->server_name          = talloc_strdup(mem_ctx, pinfo2->servername);
        W_ERROR_HAVE_NO_MEMORY(r->server_name);
@@ -6811,9 +7037,9 @@ static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
 
        t = gmtime(&queue->time);
 
-       r->job_id               = queue->job;
+       r->job_id               = queue->sysjob;
 
-       r->printer_name         = talloc_strdup(mem_ctx, lp_servicename(snum));
+       r->printer_name         = lp_servicename(mem_ctx, snum);
        W_ERROR_HAVE_NO_MEMORY(r->printer_name);
        r->server_name          = talloc_strdup(mem_ctx, pinfo2->servername);
        W_ERROR_HAVE_NO_MEMORY(r->server_name);
@@ -6864,10 +7090,10 @@ static WERROR fill_job_info3(TALLOC_CTX *mem_ctx,
                             int position, int snum,
                             struct spoolss_PrinterInfo2 *pinfo2)
 {
-       r->job_id               = queue->job;
+       r->job_id               = queue->sysjob;
        r->next_job_id          = 0;
        if (next_queue) {
-               r->next_job_id  = next_queue->job;
+               r->next_job_id  = next_queue->sysjob;
        }
        r->reserved             = 0;
 
@@ -6889,7 +7115,7 @@ static WERROR enumjobs_level1(TALLOC_CTX *mem_ctx,
        int i;
        WERROR result = WERR_OK;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
+       info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = num_queues;
@@ -6933,7 +7159,7 @@ static WERROR enumjobs_level2(TALLOC_CTX *mem_ctx,
        int i;
        WERROR result = WERR_OK;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
+       info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = num_queues;
@@ -6988,7 +7214,7 @@ static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
        int i;
        WERROR result = WERR_OK;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_JobInfo, num_queues);
+       info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = num_queues;
@@ -7056,7 +7282,7 @@ WERROR _spoolss_EnumJobs(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
-       result = winreg_get_printer(p->mem_ctx,
+       result = winreg_get_printer_internal(p->mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
                                    lp_const_servicename(snum),
@@ -7154,7 +7380,7 @@ static WERROR spoolss_setjob_1(TALLOC_CTX *mem_ctx,
 WERROR _spoolss_SetJob(struct pipes_struct *p,
                       struct spoolss_SetJob *r)
 {
-       const struct auth_serversupplied_info *session_info = p->session_info;
+       const struct auth_session_info *session_info = p->session_info;
        int snum;
        WERROR errcode = WERR_BADFUNC;
 
@@ -7176,17 +7402,13 @@ WERROR _spoolss_SetJob(struct pipes_struct *p,
                }
                break;
        case SPOOLSS_JOB_CONTROL_PAUSE:
-               if (print_job_pause(session_info, p->msg_ctx,
-                                   snum, r->in.job_id, &errcode)) {
-                       errcode = WERR_OK;
-               }
+               errcode = print_job_pause(session_info, p->msg_ctx,
+                                         snum, r->in.job_id);
                break;
        case SPOOLSS_JOB_CONTROL_RESTART:
        case SPOOLSS_JOB_CONTROL_RESUME:
-               if (print_job_resume(session_info, p->msg_ctx,
-                                    snum, r->in.job_id, &errcode)) {
-                       errcode = WERR_OK;
-               }
+               errcode = print_job_resume(session_info, p->msg_ctx,
+                                          snum, r->in.job_id);
                break;
        case 0:
                errcode = WERR_OK;
@@ -7225,7 +7447,7 @@ WERROR _spoolss_SetJob(struct pipes_struct *p,
 ****************************************************************************/
 
 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
-                                                      const struct auth_serversupplied_info *session_info,
+                                                      const struct auth_session_info *session_info,
                                                       struct messaging_context *msg_ctx,
                                                       const char *servername,
                                                       const char *architecture,
@@ -7241,12 +7463,27 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
        WERROR result = WERR_OK;
        uint32_t num_drivers;
        const char **drivers;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        *count_p = 0;
        *info_p = NULL;
 
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              session_info,
+                                              msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto out;
+       }
+
        for (version=0; version<DRIVER_MAX_VERSION; version++) {
-               result = winreg_get_driver_list(mem_ctx, session_info, msg_ctx,
+               result = winreg_get_driver_list(tmp_ctx, b,
                                                architecture, version,
                                                &num_drivers, &drivers);
                if (!W_ERROR_IS_OK(result)) {
@@ -7257,7 +7494,7 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
                          num_drivers, architecture, version));
 
                if (num_drivers != 0) {
-                       info = TALLOC_REALLOC_ARRAY(mem_ctx, info,
+                       info = talloc_realloc(tmp_ctx, info,
                                                    union spoolss_DriverInfo,
                                                    count + num_drivers);
                        if (!info) {
@@ -7271,8 +7508,7 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
                for (i = 0; i < num_drivers; i++) {
                        DEBUG(5, ("\tdriver: [%s]\n", drivers[i]));
 
-                       result = winreg_get_driver(mem_ctx, session_info,
-                                                  msg_ctx,
+                       result = winreg_get_driver(tmp_ctx, b,
                                                   architecture, drivers[i],
                                                   version, &driver);
                        if (!W_ERROR_IS_OK(result)) {
@@ -7324,18 +7560,14 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
                TALLOC_FREE(drivers);
        }
 
- out:
-       TALLOC_FREE(drivers);
-
-       if (!W_ERROR_IS_OK(result)) {
-               TALLOC_FREE(info);
-               return result;
+out:
+       if (W_ERROR_IS_OK(result)) {
+               *info_p = talloc_move(mem_ctx, &info);
+               *count_p = count;
        }
 
-       *info_p = info;
-       *count_p = count;
-
-       return WERR_OK;
+       talloc_free(tmp_ctx);
+       return result;
 }
 
 /****************************************************************************
@@ -7343,7 +7575,7 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
 ****************************************************************************/
 
 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
-                                      const struct auth_serversupplied_info *session_info,
+                                      const struct auth_session_info *session_info,
                                       struct messaging_context *msg_ctx,
                                       const char *servername,
                                       const char *architecture,
@@ -7467,7 +7699,7 @@ WERROR _spoolss_EnumForms(struct pipes_struct *p,
 
        switch (r->in.level) {
        case 1:
-               result = winreg_printer_enumforms1(p->mem_ctx,
+               result = winreg_printer_enumforms1_internal(p->mem_ctx,
                                                   get_session_info_system(),
                                                   p->msg_ctx,
                                                   r->out.count,
@@ -7517,7 +7749,7 @@ WERROR _spoolss_GetForm(struct pipes_struct *p,
 
        switch (r->in.level) {
        case 1:
-               result = winreg_printer_getform1(p->mem_ctx,
+               result = winreg_printer_getform1_internal(p->mem_ctx,
                                                 get_session_info_system(),
                                                 p->msg_ctx,
                                                 r->in.form_name,
@@ -7584,7 +7816,7 @@ static WERROR fill_port_2(TALLOC_CTX *mem_ctx,
 
 static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
 {
-       char *cmd = lp_enumports_cmd();
+       char *cmd = lp_enumports_cmd(talloc_tos());
        char **qlines = NULL;
        char *command = NULL;
        int numlines;
@@ -7597,7 +7829,7 @@ static WERROR enumports_hook(TALLOC_CTX *ctx, int *count, char ***lines)
        /* if no hook then just fill in the default port */
 
        if ( !*cmd ) {
-               if (!(qlines = TALLOC_ARRAY( NULL, char*, 2 ))) {
+               if (!(qlines = talloc_array( NULL, char*, 2 ))) {
                        return WERR_NOMEM;
                }
                if (!(qlines[0] = talloc_strdup(qlines, SAMBA_PRINTER_PORT_NAME ))) {
@@ -7658,7 +7890,7 @@ static WERROR enumports_level_1(TALLOC_CTX *mem_ctx,
        }
 
        if (numlines) {
-               info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
+               info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
                if (!info) {
                        DEBUG(10,("Returning WERR_NOMEM\n"));
                        result = WERR_NOMEM;
@@ -7710,7 +7942,7 @@ static WERROR enumports_level_2(TALLOC_CTX *mem_ctx,
        }
 
        if (numlines) {
-               info = TALLOC_ARRAY(mem_ctx, union spoolss_PortInfo, numlines);
+               info = talloc_array(mem_ctx, union spoolss_PortInfo, numlines);
                if (!info) {
                        DEBUG(10,("Returning WERR_NOMEM\n"));
                        result = WERR_NOMEM;
@@ -7848,15 +8080,23 @@ static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
        /* FIXME!!!  smbd should check to see if the driver is installed before
           trying to add a printer like this  --jerry */
 
-       if (*lp_addprinter_cmd() ) {
+       if (*lp_addprinter_cmd(talloc_tos()) ) {
+               char *raddr;
+
+               raddr = tsocket_address_inet_addr_string(p->remote_address,
+                                                        p->mem_ctx);
+               if (raddr == NULL) {
+                       return WERR_NOMEM;
+               }
+
                if ( !add_printer_hook(p->mem_ctx, p->session_info->security_token,
-                                      info2, p->client_id->addr,
+                                      info2, raddr,
                                       p->msg_ctx) ) {
                        return WERR_ACCESS_DENIED;
                }
        } else {
-               DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no"
-                       "smb.conf parameter \"addprinter command\" is defined. This"
+               DEBUG(0,("spoolss_addprinterex_level_2: add printer for printer %s called and no "
+                       "smb.conf parameter \"addprinter command\" is defined. This "
                        "parameter must exist for this call to succeed\n",
                        info2->sharename ));
        }
@@ -7885,14 +8125,17 @@ static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
                info2_mask = ~SPOOLSS_PRINTER_INFO_DEVMODE;
        }
 
-       update_dsspooler(p->mem_ctx,
-                        get_session_info_system(),
-                        p->msg_ctx,
-                        0,
-                        info2,
-                        NULL);
+       err = update_dsspooler(p->mem_ctx,
+                              get_session_info_system(),
+                              p->msg_ctx,
+                              0,
+                              info2,
+                              NULL);
+       if (!W_ERROR_IS_OK(err)) {
+               return err;
+       }
 
-       err = winreg_update_printer(p->mem_ctx,
+       err = winreg_update_printer_internal(p->mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
                                    info2->sharename,
@@ -8009,17 +8252,17 @@ WERROR _spoolss_AddPrinterDriverEx(struct pipes_struct *p,
        }
 
        DEBUG(5,("Cleaning driver's information\n"));
-       err = clean_up_driver_struct(p->mem_ctx, p, r->in.info_ctr);
+       err = clean_up_driver_struct(p->mem_ctx, p->session_info, r->in.info_ctr);
        if (!W_ERROR_IS_OK(err))
                goto done;
 
        DEBUG(5,("Moving driver to final destination\n"));
-       err = move_driver_to_download_area(p, r->in.info_ctr);
+       err = move_driver_to_download_area(p->session_info, r->in.info_ctr);
        if (!W_ERROR_IS_OK(err)) {
                goto done;
        }
 
-       err = winreg_add_driver(p->mem_ctx,
+       err = winreg_add_driver_internal(p->mem_ctx,
                                get_session_info_system(),
                                p->msg_ctx,
                                r->in.info_ctr,
@@ -8094,7 +8337,7 @@ static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
                                          char **path)
 {
        const char *pservername = NULL;
-       const char *long_archi = SPOOLSS_ARCHITECTURE_NT_X86;
+       const char *long_archi;
        const char *short_archi;
 
        *path = NULL;
@@ -8102,6 +8345,10 @@ static WERROR compose_spoolss_server_path(TALLOC_CTX *mem_ctx,
        /* environment may be empty */
        if (environment && strlen(environment)) {
                long_archi = environment;
+       } else {
+               long_archi = lp_parm_const_string(GLOBAL_SECTION_SNUM,
+                                                 "spoolss", "architecture",
+                                                 SPOOLSS_ARCHITECTURE_NT_X86);
        }
 
        /* servername may be empty */
@@ -8423,11 +8670,12 @@ WERROR _spoolss_DeletePrinterData(struct pipes_struct *p,
 WERROR _spoolss_AddForm(struct pipes_struct *p,
                        struct spoolss_AddForm *r)
 {
-       struct spoolss_AddFormInfo1 *form = r->in.info.info1;
+       struct spoolss_AddFormInfo1 *form;
        int snum = -1;
        WERROR status = WERR_OK;
-
        struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        DEBUG(5,("_spoolss_AddForm\n"));
 
@@ -8440,17 +8688,22 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
        /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
           and not a printer admin, then fail */
 
-       if ((p->session_info->utok.uid != sec_initial_uid()) &&
-           !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
-           !token_contains_name_in_list(uidtoname(p->session_info->utok.uid),
-                                         p->session_info->info3->base.domain.string,
-                                         NULL,
-                                         p->session_info->security_token,
-                                         lp_printer_admin(snum))) {
+       if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
+           !security_token_has_privilege(p->session_info->security_token,
+                                         SEC_PRIV_PRINT_OPERATOR)) {
                DEBUG(2,("_spoolss_Addform: denied by insufficient permissions.\n"));
                return WERR_ACCESS_DENIED;
        }
 
+       if (r->in.info_ctr->level != 1) {
+               return WERR_INVALID_LEVEL;
+       }
+
+       form = r->in.info_ctr->info.info1;
+       if (!form) {
+               return WERR_INVALID_PARAM;
+       }
+
        switch (form->flags) {
        case SPOOLSS_FORM_USER:
        case SPOOLSS_FORM_BUILTIN:
@@ -8460,12 +8713,22 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
                return WERR_INVALID_PARAM;
        }
 
-       status = winreg_printer_addform1(p->mem_ctx,
-                                        get_session_info_system(),
-                                        p->msg_ctx,
-                                        form);
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       status = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
+       }
+
+       status = winreg_printer_addform1(tmp_ctx, b, form);
        if (!W_ERROR_IS_OK(status)) {
-               return status;
+               goto done;
        }
 
        /*
@@ -8473,18 +8736,16 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
         */
        if (Printer->printer_type == SPLHND_PRINTER) {
                if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
-                       return WERR_BADFID;
+                       status = WERR_BADFID;
+                       goto done;
                }
 
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(tmp_ctx, b,
                                                        lp_const_servicename(snum));
-               if (!W_ERROR_IS_OK(status)) {
-                       return status;
-               }
        }
 
+done:
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -8499,6 +8760,8 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
        struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
        int snum = -1;
        WERROR status = WERR_OK;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        DEBUG(5,("_spoolss_DeleteForm\n"));
 
@@ -8508,23 +8771,29 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
-       if ((p->session_info->utok.uid != sec_initial_uid()) &&
-           !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
-           !token_contains_name_in_list(uidtoname(p->session_info->utok.uid),
-                                         p->session_info->info3->base.domain.string,
-                                         NULL,
-                                         p->session_info->security_token,
-                                         lp_printer_admin(snum))) {
+       if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
+           !security_token_has_privilege(p->session_info->security_token,
+                                         SEC_PRIV_PRINT_OPERATOR)) {
                DEBUG(2,("_spoolss_DeleteForm: denied by insufficient permissions.\n"));
                return WERR_ACCESS_DENIED;
        }
 
-       status = winreg_printer_deleteform1(p->mem_ctx,
-                                           get_session_info_system(),
-                                           p->msg_ctx,
-                                           form_name);
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       status = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
        if (!W_ERROR_IS_OK(status)) {
-               return status;
+               goto done;
+       }
+
+       status = winreg_printer_deleteform1(tmp_ctx, b, form_name);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
        }
 
        /*
@@ -8532,18 +8801,16 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
         */
        if (Printer->printer_type == SPLHND_PRINTER) {
                if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
-                       return WERR_BADFID;
+                       status = WERR_BADFID;
+                       goto done;
                }
 
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(tmp_ctx, b,
                                                        lp_const_servicename(snum));
-               if (!W_ERROR_IS_OK(status)) {
-                       return status;
-               }
        }
 
+done:
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -8554,10 +8821,12 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
 WERROR _spoolss_SetForm(struct pipes_struct *p,
                        struct spoolss_SetForm *r)
 {
-       struct spoolss_AddFormInfo1 *form = r->in.info.info1;
+       struct spoolss_AddFormInfo1 *form;
        const char *form_name = r->in.form_name;
        int snum = -1;
        WERROR status = WERR_OK;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
 
@@ -8572,24 +8841,40 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
        /* if the user is not root, doesn't have SE_PRINT_OPERATOR privilege,
           and not a printer admin, then fail */
 
-       if ((p->session_info->utok.uid != sec_initial_uid()) &&
-            !security_token_has_privilege(p->session_info->security_token, SEC_PRIV_PRINT_OPERATOR) &&
-            !token_contains_name_in_list(uidtoname(p->session_info->utok.uid),
-                                         p->session_info->info3->base.domain.string,
-                                         NULL,
-                                         p->session_info->security_token,
-                                         lp_printer_admin(snum))) {
+       if ((p->session_info->unix_token->uid != sec_initial_uid()) &&
+            !security_token_has_privilege(p->session_info->security_token,
+                                          SEC_PRIV_PRINT_OPERATOR)) {
                DEBUG(2,("_spoolss_Setform: denied by insufficient permissions.\n"));
                return WERR_ACCESS_DENIED;
        }
 
-       status = winreg_printer_setform1(p->mem_ctx,
-                                        get_session_info_system(),
-                                        p->msg_ctx,
+       if (r->in.info_ctr->level != 1) {
+               return WERR_INVALID_LEVEL;
+       }
+
+       form = r->in.info_ctr->info.info1;
+       if (!form) {
+               return WERR_INVALID_PARAM;
+       }
+
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       status = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
+       }
+
+       status = winreg_printer_setform1(tmp_ctx, b,
                                         form_name,
                                         form);
        if (!W_ERROR_IS_OK(status)) {
-               return status;
+               goto done;
        }
 
        /*
@@ -8597,18 +8882,16 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
         */
        if (Printer->printer_type == SPLHND_PRINTER) {
                if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
-                       return WERR_BADFID;
+                       status = WERR_BADFID;
+                       goto done;
                }
 
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(tmp_ctx, b,
                                                        lp_const_servicename(snum));
-               if (!W_ERROR_IS_OK(status)) {
-                       return status;
-               }
        }
 
+done:
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -8637,7 +8920,7 @@ static WERROR enumprintprocessors_level_1(TALLOC_CTX *mem_ctx,
        union spoolss_PrintProcessorInfo *info;
        WERROR result;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcessorInfo, 1);
+       info = talloc_array(mem_ctx, union spoolss_PrintProcessorInfo, 1);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = 1;
@@ -8739,7 +9022,7 @@ static WERROR enumprintprocdatatypes_level_1(TALLOC_CTX *mem_ctx,
        WERROR result;
        union spoolss_PrintProcDataTypesInfo *info;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
+       info = talloc_array(mem_ctx, union spoolss_PrintProcDataTypesInfo, 1);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = 1;
@@ -8796,6 +9079,10 @@ WERROR _spoolss_EnumPrintProcDataTypes(struct pipes_struct *p,
                return WERR_UNKNOWN_LEVEL;
        }
 
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
+       }
+
        *r->out.needed  = SPOOLSS_BUFFER_UNION_ARRAY(p->mem_ctx,
                                                     spoolss_EnumPrintProcDataTypes,
                                                     *r->out.info, r->in.level,
@@ -8851,7 +9138,7 @@ static WERROR enumprintmonitors_level_1(TALLOC_CTX *mem_ctx,
        union spoolss_MonitorInfo *info;
        WERROR result = WERR_OK;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
+       info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = 2;
@@ -8891,7 +9178,7 @@ static WERROR enumprintmonitors_level_2(TALLOC_CTX *mem_ctx,
        union spoolss_MonitorInfo *info;
        WERROR result = WERR_OK;
 
-       info = TALLOC_ARRAY(mem_ctx, union spoolss_MonitorInfo, 2);
+       info = talloc_array(mem_ctx, union spoolss_MonitorInfo, 2);
        W_ERROR_HAVE_NO_MEMORY(info);
 
        *count = 2;
@@ -8993,7 +9280,7 @@ static WERROR getjob_level_1(TALLOC_CTX *mem_ctx,
        bool found = false;
 
        for (i=0; i<count; i++) {
-               if (queue[i].job == (int)jobid) {
+               if (queue[i].sysjob == (int)jobid) {
                        found = true;
                        break;
                }
@@ -9028,7 +9315,7 @@ static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
        WERROR result;
 
        for (i=0; i<count; i++) {
-               if (queue[i].job == (int)jobid) {
+               if (queue[i].sysjob == (int)jobid) {
                        found = true;
                        break;
                }
@@ -9046,7 +9333,7 @@ static WERROR getjob_level_2(TALLOC_CTX *mem_ctx,
         *  a failure condition
         */
 
-       devmode = print_job_devmode(lp_const_servicename(snum), jobid);
+       devmode = print_job_devmode(mem_ctx, lp_const_servicename(snum), jobid);
        if (!devmode) {
                result = spoolss_create_default_devmode(mem_ctx,
                                                pinfo2->printername,
@@ -9094,7 +9381,7 @@ WERROR _spoolss_GetJob(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
-       result = winreg_get_printer(p->mem_ctx,
+       result = winreg_get_printer_internal(p->mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
                                    lp_const_servicename(snum),
@@ -9152,10 +9439,11 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
        int                     snum = 0;
        WERROR result = WERR_OK;
        DATA_BLOB blob;
-       enum winreg_Type val_type;
-       uint8_t *val_data;
-       uint32_t val_size;
-
+       enum winreg_Type val_type = REG_NONE;
+       uint8_t *val_data = NULL;
+       uint32_t val_size = 0;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
 
        DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
 
@@ -9167,6 +9455,11 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
        *r->out.needed  = 0;
        *r->out.type    = REG_NONE;
 
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
        if (!Printer) {
                DEBUG(2,("_spoolss_GetPrinterDataEx: Invalid handle (%s:%u:%u).\n",
                        OUR_HANDLE(r->in.handle)));
@@ -9174,24 +9467,30 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                goto done;
        }
 
+       /* check to see if the keyname is valid */
+       if (!strlen(r->in.key_name)) {
+               result = WERR_INVALID_PARAM;
+               goto done;
+       }
+
        /* Is the handle to a printer or to the server? */
 
        if (Printer->printer_type == SPLHND_SERVER) {
 
                union spoolss_PrinterData data;
 
-               result = getprinterdata_printer_server(p->mem_ctx,
+               result = getprinterdata_printer_server(tmp_ctx,
                                                       r->in.value_name,
                                                       r->out.type,
                                                       &data);
                if (!W_ERROR_IS_OK(result)) {
-                       return result;
+                       goto done;
                }
 
-               result = push_spoolss_PrinterData(p->mem_ctx, &blob,
+               result = push_spoolss_PrinterData(tmp_ctx, &blob,
                                                  *r->out.type, &data);
                if (!W_ERROR_IS_OK(result)) {
-                       return result;
+                       goto done;
                }
 
                *r->out.needed = blob.length;
@@ -9200,17 +9499,22 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                        memcpy(r->out.data, blob.data, blob.length);
                }
 
-               return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
+               result = WERR_OK;
+               goto done;
        }
 
        if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
-               return WERR_BADFID;
+               result = WERR_BADFID;
+               goto done;
        }
        printer = lp_const_servicename(snum);
 
-       /* check to see if the keyname is valid */
-       if (!strlen(r->in.key_name)) {
-               return WERR_INVALID_PARAM;
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
        }
 
        /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
@@ -9221,13 +9525,11 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                if (r->in.offered >= *r->out.needed) {
                        uint32_t changeid = 0;
 
-                       result = winreg_printer_get_changeid(p->mem_ctx,
-                                                            get_session_info_system(),
-                                                            p->msg_ctx,
+                       result = winreg_printer_get_changeid(tmp_ctx, b,
                                                             printer,
                                                             &changeid);
                        if (!W_ERROR_IS_OK(result)) {
-                               return result;
+                               goto done;
                        }
 
                        SIVAL(r->out.data, 0, changeid);
@@ -9236,9 +9538,7 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                goto done;
        }
 
-       result = winreg_get_printer_dataex(p->mem_ctx,
-                                          get_session_info_system(),
-                                          p->msg_ctx,
+       result = winreg_get_printer_dataex(tmp_ctx, b,
                                           printer,
                                           r->in.key_name,
                                           r->in.value_name,
@@ -9246,7 +9546,7 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                                           &val_data,
                                           &val_size);
        if (!W_ERROR_IS_OK(result)) {
-               return result;
+               goto done;
        }
 
        *r->out.needed = val_size;
@@ -9256,11 +9556,15 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                memcpy(r->out.data, val_data, val_size);
        }
 
- done:
-       /* retain type when returning WERR_MORE_DATA */
-       r->out.data     = SPOOLSS_BUFFER_OK(r->out.data, r->out.data);
+done:
+       /* NOTE: do not replace type when returning WERR_MORE_DATA */
 
-       return SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
+       if (W_ERROR_IS_OK(result)) {
+               result = SPOOLSS_BUFFER_OK(WERR_OK, WERR_MORE_DATA);
+       }
+
+       talloc_free(tmp_ctx);
+       return result;
 }
 
 /****************************************************************
@@ -9275,6 +9579,8 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
        WERROR                  result = WERR_OK;
        struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
        char                    *oid_string;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
 
        DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
 
@@ -9311,13 +9617,24 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       result = winreg_get_printer(Printer,
-                                   get_session_info_system(),
-                                   p->msg_ctx,
-                                   lp_servicename(snum),
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       result = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               goto done;
+       }
+
+       result = winreg_get_printer(tmp_ctx, b,
+                                   lp_servicename(talloc_tos(), snum),
                                    &pinfo2);
        if (!W_ERROR_IS_OK(result)) {
-               return result;
+               goto done;
        }
 
        /* check for OID in valuename */
@@ -9330,9 +9647,7 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
 
        /* save the registry data */
 
-       result = winreg_set_printer_dataex(p->mem_ctx,
-                                          get_session_info_system(),
-                                          p->msg_ctx,
+       result = winreg_set_printer_dataex(tmp_ctx, b,
                                           pinfo2->sharename,
                                           r->in.key_name,
                                           r->in.value_name,
@@ -9343,7 +9658,7 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
        if (W_ERROR_IS_OK(result)) {
                /* save the OID if one was specified */
                if (oid_string) {
-                       char *str = talloc_asprintf(p->mem_ctx, "%s\\%s",
+                       char *str = talloc_asprintf(tmp_ctx, "%s\\%s",
                                r->in.key_name, SPOOL_OID_KEY);
                        if (!str) {
                                result = WERR_NOMEM;
@@ -9356,9 +9671,7 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
                         * previous set_printer_dataex() call.  I have no idea if
                         * this is right.    --jerry
                         */
-                       winreg_set_printer_dataex(p->mem_ctx,
-                                                 get_session_info_system(),
-                                                 p->msg_ctx,
+                       winreg_set_printer_dataex(tmp_ctx, b,
                                                  pinfo2->sharename,
                                                  str,
                                                  r->in.value_name,
@@ -9367,15 +9680,13 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
                                                  strlen(oid_string) + 1);
                }
 
-               result = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               result = winreg_printer_update_changeid(tmp_ctx, b,
                                                        lp_const_servicename(snum));
 
        }
 
 done:
-       talloc_free(pinfo2);
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -9415,14 +9726,14 @@ WERROR _spoolss_DeletePrinterDataEx(struct pipes_struct *p,
        }
        printer = lp_const_servicename(snum);
 
-       status = winreg_delete_printer_dataex(p->mem_ctx,
+       status = winreg_delete_printer_dataex_internal(p->mem_ctx,
                                              get_session_info_system(),
                                              p->msg_ctx,
                                              printer,
                                              r->in.key_name,
                                              r->in.value_name);
        if (W_ERROR_IS_OK(status)) {
-               status = winreg_printer_update_changeid(p->mem_ctx,
+               status = winreg_printer_update_changeid_internal(p->mem_ctx,
                                                        get_session_info_system(),
                                                        p->msg_ctx,
                                                        printer);
@@ -9457,7 +9768,7 @@ WERROR _spoolss_EnumPrinterKey(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
-       result = winreg_enum_printer_key(p->mem_ctx,
+       result = winreg_enum_printer_key_internal(p->mem_ctx,
                                         get_session_info_system(),
                                         p->msg_ctx,
                                         lp_const_servicename(snum),
@@ -9505,6 +9816,8 @@ WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
        int                     snum=0;
        WERROR                  status;
        const char *printer;
+       struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
 
        DEBUG(5,("_spoolss_DeletePrinterKey\n"));
 
@@ -9530,19 +9843,30 @@ WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
 
        printer = lp_const_servicename(snum);
 
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
+
+       status = winreg_printer_binding_handle(tmp_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
+       }
+
        /* delete the key and all subkeys */
-       status = winreg_delete_printer_key(p->mem_ctx,
-                                          get_session_info_system(),
-                                          p->msg_ctx,
+       status = winreg_delete_printer_key(tmp_ctx, b,
                                           printer,
                                           r->in.key_name);
        if (W_ERROR_IS_OK(status)) {
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(tmp_ctx, b,
                                                        printer);
        }
 
+done:
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -9588,7 +9912,7 @@ WERROR _spoolss_EnumPrinterDataEx(struct pipes_struct *p,
        }
 
        /* now look for a match on the key name */
-       result = winreg_enum_printer_dataex(p->mem_ctx,
+       result = winreg_enum_printer_dataex_internal(p->mem_ctx,
                                            get_session_info_system(),
                                            p->msg_ctx,
                                            lp_const_servicename(snum),
@@ -10061,7 +10385,7 @@ WERROR _spoolss_AddPort(struct pipes_struct *p,
 WERROR _spoolss_GetPrinterDriver(struct pipes_struct *p,
                                 struct spoolss_GetPrinterDriver *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10072,7 +10396,7 @@ WERROR _spoolss_GetPrinterDriver(struct pipes_struct *p,
 WERROR _spoolss_ReadPrinter(struct pipes_struct *p,
                            struct spoolss_ReadPrinter *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10083,7 +10407,7 @@ WERROR _spoolss_ReadPrinter(struct pipes_struct *p,
 WERROR _spoolss_WaitForPrinterChange(struct pipes_struct *p,
                                     struct spoolss_WaitForPrinterChange *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10094,7 +10418,7 @@ WERROR _spoolss_WaitForPrinterChange(struct pipes_struct *p,
 WERROR _spoolss_ConfigurePort(struct pipes_struct *p,
                              struct spoolss_ConfigurePort *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10105,7 +10429,7 @@ WERROR _spoolss_ConfigurePort(struct pipes_struct *p,
 WERROR _spoolss_DeletePort(struct pipes_struct *p,
                           struct spoolss_DeletePort *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10116,7 +10440,7 @@ WERROR _spoolss_DeletePort(struct pipes_struct *p,
 WERROR _spoolss_CreatePrinterIC(struct pipes_struct *p,
                                struct spoolss_CreatePrinterIC *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10127,7 +10451,7 @@ WERROR _spoolss_CreatePrinterIC(struct pipes_struct *p,
 WERROR _spoolss_PlayGDIScriptOnPrinterIC(struct pipes_struct *p,
                                         struct spoolss_PlayGDIScriptOnPrinterIC *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10138,7 +10462,7 @@ WERROR _spoolss_PlayGDIScriptOnPrinterIC(struct pipes_struct *p,
 WERROR _spoolss_DeletePrinterIC(struct pipes_struct *p,
                                struct spoolss_DeletePrinterIC *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10149,7 +10473,7 @@ WERROR _spoolss_DeletePrinterIC(struct pipes_struct *p,
 WERROR _spoolss_AddPrinterConnection(struct pipes_struct *p,
                                     struct spoolss_AddPrinterConnection *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10160,7 +10484,7 @@ WERROR _spoolss_AddPrinterConnection(struct pipes_struct *p,
 WERROR _spoolss_DeletePrinterConnection(struct pipes_struct *p,
                                        struct spoolss_DeletePrinterConnection *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10171,7 +10495,7 @@ WERROR _spoolss_DeletePrinterConnection(struct pipes_struct *p,
 WERROR _spoolss_PrinterMessageBox(struct pipes_struct *p,
                                  struct spoolss_PrinterMessageBox *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10182,7 +10506,7 @@ WERROR _spoolss_PrinterMessageBox(struct pipes_struct *p,
 WERROR _spoolss_AddMonitor(struct pipes_struct *p,
                           struct spoolss_AddMonitor *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10193,7 +10517,7 @@ WERROR _spoolss_AddMonitor(struct pipes_struct *p,
 WERROR _spoolss_DeleteMonitor(struct pipes_struct *p,
                              struct spoolss_DeleteMonitor *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10204,7 +10528,7 @@ WERROR _spoolss_DeleteMonitor(struct pipes_struct *p,
 WERROR _spoolss_DeletePrintProcessor(struct pipes_struct *p,
                                     struct spoolss_DeletePrintProcessor *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10215,7 +10539,7 @@ WERROR _spoolss_DeletePrintProcessor(struct pipes_struct *p,
 WERROR _spoolss_AddPrintProvidor(struct pipes_struct *p,
                                 struct spoolss_AddPrintProvidor *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10226,7 +10550,7 @@ WERROR _spoolss_AddPrintProvidor(struct pipes_struct *p,
 WERROR _spoolss_DeletePrintProvidor(struct pipes_struct *p,
                                    struct spoolss_DeletePrintProvidor *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10237,7 +10561,7 @@ WERROR _spoolss_DeletePrintProvidor(struct pipes_struct *p,
 WERROR _spoolss_FindFirstPrinterChangeNotification(struct pipes_struct *p,
                                                   struct spoolss_FindFirstPrinterChangeNotification *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10248,7 +10572,7 @@ WERROR _spoolss_FindFirstPrinterChangeNotification(struct pipes_struct *p,
 WERROR _spoolss_FindNextPrinterChangeNotification(struct pipes_struct *p,
                                                  struct spoolss_FindNextPrinterChangeNotification *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10259,7 +10583,7 @@ WERROR _spoolss_FindNextPrinterChangeNotification(struct pipes_struct *p,
 WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(struct pipes_struct *p,
                                                            struct spoolss_RouterFindFirstPrinterChangeNotificationOld *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10270,7 +10594,7 @@ WERROR _spoolss_RouterFindFirstPrinterChangeNotificationOld(struct pipes_struct
 WERROR _spoolss_ReplyOpenPrinter(struct pipes_struct *p,
                                 struct spoolss_ReplyOpenPrinter *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10281,7 +10605,7 @@ WERROR _spoolss_ReplyOpenPrinter(struct pipes_struct *p,
 WERROR _spoolss_RouterReplyPrinter(struct pipes_struct *p,
                                   struct spoolss_RouterReplyPrinter *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10292,7 +10616,7 @@ WERROR _spoolss_RouterReplyPrinter(struct pipes_struct *p,
 WERROR _spoolss_ReplyClosePrinter(struct pipes_struct *p,
                                  struct spoolss_ReplyClosePrinter *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10303,7 +10627,7 @@ WERROR _spoolss_ReplyClosePrinter(struct pipes_struct *p,
 WERROR _spoolss_AddPortEx(struct pipes_struct *p,
                          struct spoolss_AddPortEx *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10314,7 +10638,7 @@ WERROR _spoolss_AddPortEx(struct pipes_struct *p,
 WERROR _spoolss_RouterFindFirstPrinterChangeNotification(struct pipes_struct *p,
                                                         struct spoolss_RouterFindFirstPrinterChangeNotification *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10325,7 +10649,7 @@ WERROR _spoolss_RouterFindFirstPrinterChangeNotification(struct pipes_struct *p,
 WERROR _spoolss_SpoolerInit(struct pipes_struct *p,
                            struct spoolss_SpoolerInit *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10336,7 +10660,7 @@ WERROR _spoolss_SpoolerInit(struct pipes_struct *p,
 WERROR _spoolss_ResetPrinterEx(struct pipes_struct *p,
                               struct spoolss_ResetPrinterEx *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10347,7 +10671,7 @@ WERROR _spoolss_ResetPrinterEx(struct pipes_struct *p,
 WERROR _spoolss_RouterReplyPrinterEx(struct pipes_struct *p,
                                     struct spoolss_RouterReplyPrinterEx *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10358,7 +10682,7 @@ WERROR _spoolss_RouterReplyPrinterEx(struct pipes_struct *p,
 WERROR _spoolss_44(struct pipes_struct *p,
                   struct spoolss_44 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10369,7 +10693,7 @@ WERROR _spoolss_44(struct pipes_struct *p,
 WERROR _spoolss_SetPort(struct pipes_struct *p,
                        struct spoolss_SetPort *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10380,7 +10704,7 @@ WERROR _spoolss_SetPort(struct pipes_struct *p,
 WERROR _spoolss_4a(struct pipes_struct *p,
                   struct spoolss_4a *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10391,7 +10715,7 @@ WERROR _spoolss_4a(struct pipes_struct *p,
 WERROR _spoolss_4b(struct pipes_struct *p,
                   struct spoolss_4b *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10402,7 +10726,7 @@ WERROR _spoolss_4b(struct pipes_struct *p,
 WERROR _spoolss_4c(struct pipes_struct *p,
                   struct spoolss_4c *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10413,7 +10737,7 @@ WERROR _spoolss_4c(struct pipes_struct *p,
 WERROR _spoolss_53(struct pipes_struct *p,
                   struct spoolss_53 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10424,7 +10748,7 @@ WERROR _spoolss_53(struct pipes_struct *p,
 WERROR _spoolss_AddPerMachineConnection(struct pipes_struct *p,
                                        struct spoolss_AddPerMachineConnection *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10435,7 +10759,7 @@ WERROR _spoolss_AddPerMachineConnection(struct pipes_struct *p,
 WERROR _spoolss_DeletePerMachineConnection(struct pipes_struct *p,
                                           struct spoolss_DeletePerMachineConnection *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10446,7 +10770,7 @@ WERROR _spoolss_DeletePerMachineConnection(struct pipes_struct *p,
 WERROR _spoolss_EnumPerMachineConnections(struct pipes_struct *p,
                                          struct spoolss_EnumPerMachineConnections *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10457,7 +10781,7 @@ WERROR _spoolss_EnumPerMachineConnections(struct pipes_struct *p,
 WERROR _spoolss_5a(struct pipes_struct *p,
                   struct spoolss_5a *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10468,7 +10792,7 @@ WERROR _spoolss_5a(struct pipes_struct *p,
 WERROR _spoolss_5b(struct pipes_struct *p,
                   struct spoolss_5b *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10479,7 +10803,7 @@ WERROR _spoolss_5b(struct pipes_struct *p,
 WERROR _spoolss_5c(struct pipes_struct *p,
                   struct spoolss_5c *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10490,7 +10814,7 @@ WERROR _spoolss_5c(struct pipes_struct *p,
 WERROR _spoolss_5d(struct pipes_struct *p,
                   struct spoolss_5d *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10501,7 +10825,7 @@ WERROR _spoolss_5d(struct pipes_struct *p,
 WERROR _spoolss_5e(struct pipes_struct *p,
                   struct spoolss_5e *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10512,7 +10836,7 @@ WERROR _spoolss_5e(struct pipes_struct *p,
 WERROR _spoolss_5f(struct pipes_struct *p,
                   struct spoolss_5f *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10523,7 +10847,7 @@ WERROR _spoolss_5f(struct pipes_struct *p,
 WERROR _spoolss_60(struct pipes_struct *p,
                   struct spoolss_60 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10534,7 +10858,7 @@ WERROR _spoolss_60(struct pipes_struct *p,
 WERROR _spoolss_61(struct pipes_struct *p,
                   struct spoolss_61 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10545,7 +10869,7 @@ WERROR _spoolss_61(struct pipes_struct *p,
 WERROR _spoolss_62(struct pipes_struct *p,
                   struct spoolss_62 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10556,7 +10880,7 @@ WERROR _spoolss_62(struct pipes_struct *p,
 WERROR _spoolss_63(struct pipes_struct *p,
                   struct spoolss_63 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10567,7 +10891,7 @@ WERROR _spoolss_63(struct pipes_struct *p,
 WERROR _spoolss_64(struct pipes_struct *p,
                   struct spoolss_64 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10578,7 +10902,7 @@ WERROR _spoolss_64(struct pipes_struct *p,
 WERROR _spoolss_65(struct pipes_struct *p,
                   struct spoolss_65 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10589,7 +10913,7 @@ WERROR _spoolss_65(struct pipes_struct *p,
 WERROR _spoolss_GetCorePrinterDrivers(struct pipes_struct *p,
                                      struct spoolss_GetCorePrinterDrivers *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10600,7 +10924,7 @@ WERROR _spoolss_GetCorePrinterDrivers(struct pipes_struct *p,
 WERROR _spoolss_67(struct pipes_struct *p,
                   struct spoolss_67 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10611,7 +10935,7 @@ WERROR _spoolss_67(struct pipes_struct *p,
 WERROR _spoolss_GetPrinterDriverPackagePath(struct pipes_struct *p,
                                            struct spoolss_GetPrinterDriverPackagePath *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10622,7 +10946,7 @@ WERROR _spoolss_GetPrinterDriverPackagePath(struct pipes_struct *p,
 WERROR _spoolss_69(struct pipes_struct *p,
                   struct spoolss_69 *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10633,7 +10957,7 @@ WERROR _spoolss_69(struct pipes_struct *p,
 WERROR _spoolss_6a(struct pipes_struct *p,
                   struct spoolss_6a *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10644,7 +10968,7 @@ WERROR _spoolss_6a(struct pipes_struct *p,
 WERROR _spoolss_6b(struct pipes_struct *p,
                   struct spoolss_6b *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10655,7 +10979,7 @@ WERROR _spoolss_6b(struct pipes_struct *p,
 WERROR _spoolss_6c(struct pipes_struct *p,
                   struct spoolss_6c *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }
 
@@ -10666,6 +10990,50 @@ WERROR _spoolss_6c(struct pipes_struct *p,
 WERROR _spoolss_6d(struct pipes_struct *p,
                   struct spoolss_6d *r)
 {
-       p->rng_fault_state = true;
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+       return WERR_NOT_SUPPORTED;
+}
+
+/****************************************************************
+ _spoolss_RpcGetJobNamedPropertyValue
+****************************************************************/
+
+WERROR _spoolss_RpcGetJobNamedPropertyValue(struct pipes_struct *p,
+                                           struct spoolss_RpcGetJobNamedPropertyValue *r)
+{
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+       return WERR_NOT_SUPPORTED;
+}
+
+/****************************************************************
+ _spoolss_RpcSetJobNamedProperty
+****************************************************************/
+
+WERROR _spoolss_RpcSetJobNamedProperty(struct pipes_struct *p,
+                                      struct spoolss_RpcSetJobNamedProperty *r)
+{
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+       return WERR_NOT_SUPPORTED;
+}
+
+/****************************************************************
+ _spoolss_RpcDeleteJobNamedProperty
+****************************************************************/
+
+WERROR _spoolss_RpcDeleteJobNamedProperty(struct pipes_struct *p,
+                                         struct spoolss_RpcDeleteJobNamedProperty *r)
+{
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+       return WERR_NOT_SUPPORTED;
+}
+
+/****************************************************************
+ _spoolss_RpcEnumJobNamedProperties
+****************************************************************/
+
+WERROR _spoolss_RpcEnumJobNamedProperties(struct pipes_struct *p,
+                                         struct spoolss_RpcEnumJobNamedProperties *r)
+{
+       p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
        return WERR_NOT_SUPPORTED;
 }