s3:rpc_server/spoolss: remove reload_services check from delete_printer_hook()
[kai/samba.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
index 6d62bcb526f7e36fe7070c498e2ec0c9ff548e1a..491d96566a8e10b80bd3ab0e9d40287b8dfda156 100644 (file)
@@ -138,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;
 };
 
@@ -251,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);
@@ -259,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);
@@ -275,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);
@@ -283,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;
        }
 }
 
@@ -425,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;
 }
 
@@ -554,7 +538,7 @@ static void prune_printername_cache(void)
 ****************************************************************************/
 
 static WERROR set_printer_hnd_name(TALLOC_CTX *mem_ctx,
-                                  const struct auth3_session_info *session_info,
+                                  const struct auth_session_info *session_info,
                                   struct messaging_context *msg_ctx,
                                   struct printer_handle *Printer,
                                   const char *handlename)
@@ -1255,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;
        }
@@ -1287,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 */
@@ -1536,9 +1522,8 @@ void do_drv_upgrade_printer(struct messaging_context *msg,
                            DATA_BLOB *data)
 {
        TALLOC_CTX *tmp_ctx;
-       struct auth3_session_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;
@@ -1548,13 +1533,6 @@ void do_drv_upgrade_printer(struct messaging_context *msg,
        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"));
@@ -1827,12 +1805,12 @@ 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,
+                                   uidtoname(p->session_info->unix_token->uid),
+                                   p->session_info->info->domain_name,
                                    NULL,
                                    p->session_info->security_token,
                                    lp_printer_admin(snum))) {
@@ -1914,7 +1892,7 @@ WERROR _spoolss_OpenPrinterEx(struct pipes_struct *p,
                        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,
@@ -2087,15 +2065,16 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
        int                             version;
        WERROR                          status;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        /* 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)
                && !token_contains_name_in_list(
-                       uidtoname(p->session_info->utok.uid),
-                       p->session_info->info3->base.domain.string,
+                       uidtoname(p->session_info->unix_token->uid),
+                       p->session_info->info->domain_name,
                        NULL,
                        p->session_info->security_token,
                        lp_printer_admin(-1)) )
@@ -2105,18 +2084,24 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
 
        /* 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;
+       }
+
+       tmp_ctx = talloc_new(p->mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
 
-       status = winreg_printer_binding_handle(p->mem_ctx,
+       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_get_driver(p->mem_ctx, b,
+       status = winreg_get_driver(tmp_ctx, b,
                                   r->in.architecture, r->in.driver,
                                   version, &info);
        if (!W_ERROR_IS_OK(status)) {
@@ -2125,7 +2110,7 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
                if ( version == 2 ) {
                        version = 3;
 
-                       status = winreg_get_driver(p->mem_ctx, b,
+                       status = winreg_get_driver(tmp_ctx, b,
                                                   r->in.architecture,
                                                   r->in.driver,
                                                   version, &info);
@@ -2142,23 +2127,22 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
 
        }
 
-       if (printer_driver_in_use(p->mem_ctx,
-                                 get_session_info_system(),
-                                 p->msg_ctx,
+       if (printer_driver_in_use(tmp_ctx,
+                                 b,
                                  info)) {
                status = WERR_PRINTER_DRIVER_IN_USE;
                goto done;
        }
 
        if (version == 2) {
-               status = winreg_get_driver(p->mem_ctx, b,
+               status = winreg_get_driver(tmp_ctx, b,
                                           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*/
 
-                       status = winreg_del_driver(p->mem_ctx, b,
+                       status = winreg_del_driver(tmp_ctx, b,
                                                   info_win2k, 3);
                        talloc_free(info_win2k);
 
@@ -2169,11 +2153,11 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
                }
        }
 
-       status = winreg_del_driver(p->mem_ctx, b,
+       status = winreg_del_driver(tmp_ctx, b,
                                   info, version);
 
 done:
-       talloc_free(info);
+       talloc_free(tmp_ctx);
 
        return status;
 }
@@ -2191,15 +2175,16 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
        bool                            delete_files;
        WERROR                          status;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        /* 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)
                && !token_contains_name_in_list(
-                       uidtoname(p->session_info->utok.uid),
-                       p->session_info->info3->base.domain.string,
+                       uidtoname(p->session_info->unix_token->uid),
+                       p->session_info->info->domain_name,
                        NULL,
                        p->session_info->security_token, lp_printer_admin(-1)) )
        {
@@ -2212,18 +2197,24 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                return WERR_INVALID_ENVIRONMENT;
        }
 
-       if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION)
+       if (r->in.delete_flags & DPD_DELETE_SPECIFIC_VERSION) {
                version = r->in.version;
+       }
 
-       status = winreg_printer_binding_handle(p->mem_ctx,
+       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_get_driver(p->mem_ctx, b,
+       status = winreg_get_driver(tmp_ctx, b,
                                   r->in.architecture,
                                   r->in.driver,
                                   version,
@@ -2243,7 +2234,7 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                /* try for Win2k driver if "Windows NT x86" */
 
                version = 3;
-               status = winreg_get_driver(info, b,
+               status = winreg_get_driver(tmp_ctx, b,
                                           r->in.architecture,
                                           r->in.driver,
                                           version, &info);
@@ -2253,9 +2244,8 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                }
        }
 
-       if (printer_driver_in_use(info,
-                                 get_session_info_system(),
-                                 p->msg_ctx,
+       if (printer_driver_in_use(tmp_ctx,
+                                 b,
                                  info)) {
                status = WERR_PRINTER_DRIVER_IN_USE;
                goto done;
@@ -2279,9 +2269,8 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
 
        if (delete_files &&
            (r->in.delete_flags & DPD_DELETE_ALL_FILES) &&
-           printer_driver_files_in_use(info,
-                                       get_session_info_system(),
-                                       p->msg_ctx,
+           printer_driver_files_in_use(tmp_ctx,
+                                       b,
                                        info)) {
                /* no idea of the correct error here */
                status = WERR_ACCESS_DENIED;
@@ -2292,7 +2281,7 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
        /* 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, b,
+               status = winreg_get_driver(tmp_ctx, b,
                                           r->in.architecture,
                                           r->in.driver, 3, &info_win2k);
                if (W_ERROR_IS_OK(status)) {
@@ -2300,8 +2289,7 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                        if (delete_files &&
                            (r->in.delete_flags & DPD_DELETE_ALL_FILES) &&
                            printer_driver_files_in_use(info,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+                                                       b,
                                                        info_win2k)) {
                                /* no idea of the correct error here */
                                talloc_free(info_win2k);
@@ -2312,7 +2300,7 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                        /* if we get to here, we now have 2 driver info structures to remove */
                        /* remove the Win2k driver first*/
 
-                       status = winreg_del_driver(info, b,
+                       status = winreg_del_driver(tmp_ctx, b,
                                                   info_win2k,
                                                   3);
 
@@ -2334,7 +2322,7 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
                }
        }
 
-       status = winreg_del_driver(info, b,
+       status = winreg_del_driver(tmp_ctx, b,
                                   info,
                                   version);
        if (!W_ERROR_IS_OK(status)) {
@@ -2351,7 +2339,7 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
        }
 
 done:
-       talloc_free(info);
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -2563,7 +2551,7 @@ static bool spoolss_connect_to_client(struct rpc_pipe_client **pp_pipe,
                return false;
        }
 
-       if ( the_cli->protocol != PROTOCOL_NT1 ) {
+       if ( cli_state_protocol(the_cli) != PROTOCOL_NT1 ) {
                DEBUG(0,("spoolss_connect_to_client: machine %s didn't negotiate NT protocol.\n", remote_machine));
                cli_shutdown(the_cli);
                return false;
@@ -2618,7 +2606,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;
                }
@@ -2628,16 +2616,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;
        }
 
        /*
@@ -2650,7 +2643,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,
@@ -3719,6 +3712,15 @@ static WERROR printer_notify_info(struct pipes_struct *p,
                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];
 
@@ -3887,7 +3889,7 @@ static void compose_devicemode_devicename(struct spoolss_DeviceMode *dm,
  ********************************************************************/
 
 static WERROR construct_printer_info0(TALLOC_CTX *mem_ctx,
-                                     const struct auth3_session_info *session_info,
+                                     const struct auth_session_info *session_info,
                                      struct messaging_context *msg_ctx,
                                      struct spoolss_PrinterInfo2 *info2,
                                      const char *servername,
@@ -4230,16 +4232,8 @@ static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
                                      struct spoolss_PrinterInfo7 *r,
                                      int snum)
 {
-       struct auth3_session_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,
@@ -4252,7 +4246,6 @@ static WERROR construct_printer_info7(TALLOC_CTX *mem_ctx,
        }
        W_ERROR_HAVE_NO_MEMORY(r->guid);
 
-       TALLOC_FREE(session_info);
        return WERR_OK;
 }
 
@@ -4299,7 +4292,7 @@ static bool snum_is_shared_printer(int snum)
 ********************************************************************/
 
 static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
-                                          const struct auth3_session_info *session_info,
+                                          const struct auth_session_info *session_info,
                                           struct messaging_context *msg_ctx,
                                           const char *servername,
                                           uint32_t level,
@@ -4313,6 +4306,12 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
        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;
@@ -4332,7 +4331,7 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
                        printer, snum));
 
                if (b == NULL) {
-                       result = winreg_printer_binding_handle(mem_ctx,
+                       result = winreg_printer_binding_handle(tmp_ctx,
                                                               session_info,
                                                               msg_ctx,
                                                               &b);
@@ -4341,13 +4340,13 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
                        }
                }
 
-               result = winreg_create_printer(mem_ctx, b,
+               result = winreg_create_printer(tmp_ctx, b,
                                               printer);
                if (!W_ERROR_IS_OK(result)) {
                        goto out;
                }
 
-               info = talloc_realloc(mem_ctx, info,
+               info = talloc_realloc(tmp_ctx, info,
                                            union spoolss_PrinterInfo,
                                            count + 1);
                if (!info) {
@@ -4355,7 +4354,7 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
                        goto out;
                }
 
-               result = winreg_get_printer(mem_ctx, b,
+               result = winreg_get_printer(tmp_ctx, b,
                                            printer, &info2);
                if (!W_ERROR_IS_OK(result)) {
                        goto out;
@@ -4401,18 +4400,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;
 }
 
 /********************************************************************
@@ -4420,7 +4416,7 @@ static WERROR enum_all_printers_info_level(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
-                                 const struct auth3_session_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4438,7 +4434,7 @@ static WERROR enumprinters_level0(TALLOC_CTX *mem_ctx,
 ********************************************************************/
 
 static WERROR enum_all_printers_info_1(TALLOC_CTX *mem_ctx,
-                                      const struct auth3_session_info *session_info,
+                                      const struct auth_session_info *session_info,
                                       struct messaging_context *msg_ctx,
                                       const char *servername,
                                       uint32_t flags,
@@ -4456,7 +4452,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 auth3_session_info *session_info,
+                                            const struct auth_session_info *session_info,
                                             struct messaging_context *msg_ctx,
                                             const char *servername,
                                             union spoolss_PrinterInfo **info,
@@ -4473,7 +4469,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 auth3_session_info *session_info,
+                                           const struct auth_session_info *session_info,
                                            struct messaging_context *msg_ctx,
                                            const char *servername,
                                            union spoolss_PrinterInfo **info,
@@ -4500,7 +4496,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 auth3_session_info *session_info,
+                                              const struct auth_session_info *session_info,
                                               struct messaging_context *msg_ctx,
                                               const char *servername,
                                               union spoolss_PrinterInfo **info,
@@ -4537,7 +4533,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 auth3_session_info *session_info,
+                                      const struct auth_session_info *session_info,
                                       struct messaging_context *msg_ctx,
                                       const char *servername,
                                       union spoolss_PrinterInfo **info,
@@ -4554,7 +4550,7 @@ static WERROR enum_all_printers_info_2(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
-                                 const struct auth3_session_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4588,7 +4584,7 @@ static WERROR enumprinters_level1(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
-                                 const struct auth3_session_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4624,7 +4620,7 @@ static WERROR enumprinters_level2(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
-                                 const struct auth3_session_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4643,7 +4639,7 @@ static WERROR enumprinters_level4(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
-                                 const struct auth3_session_info *session_info,
+                                 const struct auth_session_info *session_info,
                                  struct messaging_context *msg_ctx,
                                  uint32_t flags,
                                  const char *servername,
@@ -4663,7 +4659,7 @@ static WERROR enumprinters_level5(TALLOC_CTX *mem_ctx,
 WERROR _spoolss_EnumPrinters(struct pipes_struct *p,
                             struct spoolss_EnumPrinters *r)
 {
-       const struct auth3_session_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 */
@@ -4836,6 +4832,7 @@ WERROR _spoolss_GetPrinter(struct pipes_struct *p,
                result = WERR_UNKNOWN_LEVEL;
                break;
        }
+       TALLOC_FREE(info2);
 
  out:
        if (!W_ERROR_IS_OK(result)) {
@@ -5533,7 +5530,7 @@ static WERROR fill_printer_driver_info101(TALLOC_CTX *mem_ctx,
  ********************************************************************/
 
 static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
-                                                 const struct auth3_session_info *session_info,
+                                                 const struct auth_session_info *session_info,
                                                  struct messaging_context *msg_ctx,
                                                  uint32_t level,
                                                  union spoolss_DriverInfo *r,
@@ -5546,20 +5543,26 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
        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_printer_binding_handle(mem_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)) {
-               return result;
+               goto done;
        }
 
-       result = winreg_get_printer(mem_ctx, b,
+       result = winreg_get_printer(tmp_ctx, b,
                                    lp_const_servicename(snum),
                                    &pinfo2);
 
@@ -5567,10 +5570,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, b,
+       result = winreg_get_driver(tmp_ctx, b,
                                   architecture,
                                   pinfo2->drivername, version, &driver);
 
@@ -5583,24 +5587,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, b,
+               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);
@@ -5633,9 +5639,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;
 }
 
@@ -5908,7 +5913,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 auth3_session_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);
@@ -5991,6 +5996,7 @@ static WERROR update_printer_sec(struct policy_handle *handle,
        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",
@@ -6018,7 +6024,12 @@ static WERROR update_printer_sec(struct policy_handle *handle,
                goto done;
        }
 
-       result = winreg_printer_binding_handle(p->mem_ctx,
+       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);
@@ -6028,7 +6039,7 @@ static WERROR update_printer_sec(struct policy_handle *handle,
 
        /* NT seems to like setting the security descriptor even though
           nothing may have actually changed. */
-       result = winreg_get_printer_secdesc(p->mem_ctx, b,
+       result = winreg_get_printer_secdesc(tmp_ctx, b,
                                            printer,
                                            &old_secdesc);
        if (!W_ERROR_IS_OK(result)) {
@@ -6067,7 +6078,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;
@@ -6078,11 +6089,12 @@ static WERROR update_printer_sec(struct policy_handle *handle,
                goto done;
        }
 
-       result = winreg_set_printer_secdesc(p->mem_ctx, b,
+       result = winreg_set_printer_secdesc(tmp_ctx, b,
                                            printer,
                                            new_secdesc);
 
- done:
+done:
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -6271,7 +6283,7 @@ static bool add_printer_hook(TALLOC_CTX *ctx, struct security_token *token,
 }
 
 static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
-                              const struct auth3_session_info *session_info,
+                              const struct auth_session_info *session_info,
                               struct messaging_context *msg_ctx,
                               int snum,
                               struct spoolss_SetPrinterInfo2 *printer,
@@ -6285,18 +6297,24 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        DATA_BLOB buffer;
        WERROR result = WERR_OK;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
+
+       tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return WERR_NOMEM;
+       }
 
-       result = winreg_printer_binding_handle(mem_ctx,
+       result = winreg_printer_binding_handle(tmp_ctx,
                                               session_info,
                                               msg_ctx,
                                               &b);
        if (!W_ERROR_IS_OK(result)) {
-               return 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, b,
+               push_reg_sz(tmp_ctx, &buffer, printer->drivername);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_DRIVERNAME,
@@ -6315,8 +6333,8 @@ 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, b,
+               push_reg_sz(tmp_ctx, &buffer, printer->comment);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_DESCRIPTION,
@@ -6332,8 +6350,8 @@ 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, b,
+               push_reg_sz(tmp_ctx, &buffer, printer->sharename);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSHARENAME,
@@ -6359,8 +6377,8 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                        p = printer->printername;
                }
 
-               push_reg_sz(mem_ctx, &buffer, p);
-               winreg_set_printer_dataex(mem_ctx, b,
+               push_reg_sz(tmp_ctx, &buffer, p);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTERNAME,
@@ -6375,8 +6393,8 @@ 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, b,
+               push_reg_sz(tmp_ctx, &buffer, printer->portname);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PORTNAME,
@@ -6392,8 +6410,8 @@ 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, b,
+               push_reg_sz(tmp_ctx, &buffer, printer->location);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_LOCATION,
@@ -6410,8 +6428,8 @@ 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, b,
+               push_reg_sz(tmp_ctx, &buffer, printer->sepfile);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSEPARATORFILE,
@@ -6428,9 +6446,9 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        }
 
        if (force_update || printer->starttime != old_printer->starttime) {
-               buffer = data_blob_talloc(mem_ctx, NULL, 4);
+               buffer = data_blob_talloc(tmp_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->starttime);
-               winreg_set_printer_dataex(mem_ctx, b,
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSTARTTIME,
@@ -6440,9 +6458,9 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        }
 
        if (force_update || printer->untiltime != old_printer->untiltime) {
-               buffer = data_blob_talloc(mem_ctx, NULL, 4);
+               buffer = data_blob_talloc(tmp_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->untiltime);
-               winreg_set_printer_dataex(mem_ctx, b,
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTENDTIME,
@@ -6452,9 +6470,9 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        }
 
        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, b,
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRIORITY,
@@ -6464,10 +6482,10 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        }
 
        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, b,
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTKEEPPRINTEDJOBS,
@@ -6488,8 +6506,8 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                        default:
                                spooling = "unknown";
                }
-               push_reg_sz(mem_ctx, &buffer, spooling);
-               winreg_set_printer_dataex(mem_ctx, b,
+               push_reg_sz(tmp_ctx, &buffer, spooling);
+               winreg_set_printer_dataex(tmp_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSPOOLING,
@@ -6498,8 +6516,8 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                                          buffer.length);
        }
 
-       push_reg_sz(mem_ctx, &buffer, lp_netbios_name());
-       winreg_set_printer_dataex(mem_ctx, b,
+       push_reg_sz(tmp_ctx, &buffer, lp_netbios_name());
+       winreg_set_printer_dataex(tmp_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_SHORTSERVERNAME,
@@ -6509,17 +6527,17 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
 
        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, lp_netbios_name());
+               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, b,
+       push_reg_sz(tmp_ctx, &buffer, longname);
+       winreg_set_printer_dataex(tmp_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_SERVERNAME,
@@ -6527,10 +6545,10 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                                  buffer.data,
                                  buffer.length);
 
-       uncname = talloc_asprintf(mem_ctx, "\\\\%s\\%s",
+       uncname = talloc_asprintf(tmp_ctx, "\\\\%s\\%s",
                                  lp_netbios_name(), printer->sharename);
-       push_reg_sz(mem_ctx, &buffer, uncname);
-       winreg_set_printer_dataex(mem_ctx, b,
+       push_reg_sz(tmp_ctx, &buffer, uncname);
+       winreg_set_printer_dataex(tmp_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_UNCNAME,
@@ -6539,6 +6557,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                                  buffer.length);
 
 done:
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -7248,7 +7267,7 @@ static WERROR spoolss_setjob_1(TALLOC_CTX *mem_ctx,
 WERROR _spoolss_SetJob(struct pipes_struct *p,
                       struct spoolss_SetJob *r)
 {
-       const struct auth3_session_info *session_info = p->session_info;
+       const struct auth_session_info *session_info = p->session_info;
        int snum;
        WERROR errcode = WERR_BADFUNC;
 
@@ -7319,7 +7338,7 @@ WERROR _spoolss_SetJob(struct pipes_struct *p,
 ****************************************************************************/
 
 static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
-                                                      const struct auth3_session_info *session_info,
+                                                      const struct auth_session_info *session_info,
                                                       struct messaging_context *msg_ctx,
                                                       const char *servername,
                                                       const char *architecture,
@@ -7336,11 +7355,17 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
        uint32_t num_drivers;
        const char **drivers;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        *count_p = 0;
        *info_p = NULL;
 
-       result = winreg_printer_binding_handle(mem_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);
@@ -7349,7 +7374,7 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
        }
 
        for (version=0; version<DRIVER_MAX_VERSION; version++) {
-               result = winreg_get_driver_list(mem_ctx, b,
+               result = winreg_get_driver_list(tmp_ctx, b,
                                                architecture, version,
                                                &num_drivers, &drivers);
                if (!W_ERROR_IS_OK(result)) {
@@ -7360,7 +7385,7 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
                          num_drivers, architecture, version));
 
                if (num_drivers != 0) {
-                       info = talloc_realloc(mem_ctx, info,
+                       info = talloc_realloc(tmp_ctx, info,
                                                    union spoolss_DriverInfo,
                                                    count + num_drivers);
                        if (!info) {
@@ -7374,7 +7399,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, b,
+                       result = winreg_get_driver(tmp_ctx, b,
                                                   architecture, drivers[i],
                                                   version, &driver);
                        if (!W_ERROR_IS_OK(result)) {
@@ -7426,18 +7451,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;
 }
 
 /****************************************************************************
@@ -7445,7 +7466,7 @@ static WERROR enumprinterdrivers_level_by_architecture(TALLOC_CTX *mem_ctx,
 ****************************************************************************/
 
 static WERROR enumprinterdrivers_level(TALLOC_CTX *mem_ctx,
-                                      const struct auth3_session_info *session_info,
+                                      const struct auth_session_info *session_info,
                                       struct messaging_context *msg_ctx,
                                       const char *servername,
                                       const char *architecture,
@@ -8538,6 +8559,7 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
        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"));
 
@@ -8550,10 +8572,10 @@ 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()) &&
+       if ((p->session_info->unix_token->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,
+           !token_contains_name_in_list(uidtoname(p->session_info->unix_token->uid),
+                                         p->session_info->info->domain_name,
                                          NULL,
                                          p->session_info->security_token,
                                          lp_printer_admin(snum))) {
@@ -8570,18 +8592,22 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
                return WERR_INVALID_PARAM;
        }
 
-       status = winreg_printer_binding_handle(p->mem_ctx,
+       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_addform1(p->mem_ctx, b,
-                                        form);
+       status = winreg_printer_addform1(tmp_ctx, b, form);
        if (!W_ERROR_IS_OK(status)) {
-               return status;
+               goto done;
        }
 
        /*
@@ -8589,16 +8615,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, b,
+               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;
 }
 
@@ -8614,6 +8640,7 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
        int snum = -1;
        WERROR status = WERR_OK;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx = NULL;
 
        DEBUG(5,("_spoolss_DeleteForm\n"));
 
@@ -8623,10 +8650,10 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
                return WERR_BADFID;
        }
 
-       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) &&
-           !token_contains_name_in_list(uidtoname(p->session_info->utok.uid),
-                                         p->session_info->info3->base.domain.string,
+           !token_contains_name_in_list(uidtoname(p->session_info->unix_token->uid),
+                                         p->session_info->info->domain_name,
                                          NULL,
                                          p->session_info->security_token,
                                          lp_printer_admin(snum))) {
@@ -8634,18 +8661,22 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       status = winreg_printer_binding_handle(p->mem_ctx,
+       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(p->mem_ctx, b,
-                                           form_name);
+       status = winreg_printer_deleteform1(tmp_ctx, b, form_name);
        if (!W_ERROR_IS_OK(status)) {
-               return status;
+               goto done;
        }
 
        /*
@@ -8653,16 +8684,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, b,
+               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;
 }
 
@@ -8678,6 +8709,7 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
        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);
 
@@ -8692,10 +8724,10 @@ 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()) &&
+       if ((p->session_info->unix_token->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,
+            !token_contains_name_in_list(uidtoname(p->session_info->unix_token->uid),
+                                         p->session_info->info->domain_name,
                                          NULL,
                                          p->session_info->security_token,
                                          lp_printer_admin(snum))) {
@@ -8703,19 +8735,24 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       status = winreg_printer_binding_handle(p->mem_ctx,
+       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_setform1(p->mem_ctx, b,
+       status = winreg_printer_setform1(tmp_ctx, b,
                                         form_name,
                                         form);
        if (!W_ERROR_IS_OK(status)) {
-               return status;
+               goto done;
        }
 
        /*
@@ -8723,16 +8760,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, b,
+               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;
 }
 
@@ -9284,6 +9321,7 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
        uint8_t *val_data = NULL;
        uint32_t val_size = 0;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
 
        DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
 
@@ -9295,6 +9333,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)));
@@ -9302,24 +9345,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;
@@ -9328,25 +9377,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(p->mem_ctx,
+       result = winreg_printer_binding_handle(tmp_ctx,
                                               get_session_info_system(),
                                               p->msg_ctx,
                                               &b);
        if (!W_ERROR_IS_OK(result)) {
-               return result;
+               goto done;
        }
 
        /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
@@ -9357,11 +9403,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, b,
+                       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);
@@ -9370,7 +9416,7 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                goto done;
        }
 
-       result = winreg_get_printer_dataex(p->mem_ctx, b,
+       result = winreg_get_printer_dataex(tmp_ctx, b,
                                           printer,
                                           r->in.key_name,
                                           r->in.value_name,
@@ -9378,7 +9424,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;
@@ -9388,11 +9434,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;
 }
 
 /****************************************************************
@@ -9408,6 +9458,7 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
        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"));
 
@@ -9444,19 +9495,24 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       result = winreg_printer_binding_handle(p->mem_ctx,
+       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)) {
-               return result;
+               goto done;
        }
 
-       result = winreg_get_printer(Printer, b,
+       result = winreg_get_printer(tmp_ctx, b,
                                    lp_servicename(snum),
                                    &pinfo2);
        if (!W_ERROR_IS_OK(result)) {
-               return result;
+               goto done;
        }
 
        /* check for OID in valuename */
@@ -9469,7 +9525,7 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
 
        /* save the registry data */
 
-       result = winreg_set_printer_dataex(p->mem_ctx, b,
+       result = winreg_set_printer_dataex(tmp_ctx, b,
                                           pinfo2->sharename,
                                           r->in.key_name,
                                           r->in.value_name,
@@ -9480,7 +9536,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;
@@ -9493,7 +9549,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, b,
+                       winreg_set_printer_dataex(tmp_ctx, b,
                                                  pinfo2->sharename,
                                                  str,
                                                  r->in.value_name,
@@ -9502,13 +9558,13 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
                                                  strlen(oid_string) + 1);
                }
 
-               result = winreg_printer_update_changeid(p->mem_ctx, b,
+               result = winreg_printer_update_changeid(tmp_ctx, b,
                                                        lp_const_servicename(snum));
 
        }
 
 done:
-       talloc_free(pinfo2);
+       talloc_free(tmp_ctx);
        return result;
 }
 
@@ -9639,6 +9695,7 @@ WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
        WERROR                  status;
        const char *printer;
        struct dcerpc_binding_handle *b;
+       TALLOC_CTX *tmp_ctx;
 
        DEBUG(5,("_spoolss_DeletePrinterKey\n"));
 
@@ -9664,23 +9721,30 @@ WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
 
        printer = lp_const_servicename(snum);
 
-       status = winreg_printer_binding_handle(p->mem_ctx,
+       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;
        }
 
        /* delete the key and all subkeys */
-       status = winreg_delete_printer_key(p->mem_ctx, b,
+       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, b,
+               status = winreg_printer_update_changeid(tmp_ctx, b,
                                                        printer);
        }
 
+done:
+       talloc_free(tmp_ctx);
        return status;
 }