s3-printing: pass a talloc ctx to unpack_pjob
[ddiss/samba.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
index 8a4f17d85c0c95dda19cb649e9800b06c0fb3191..8868a9894e666e6e1f120f91097d0c269db5aebf 100644 (file)
@@ -295,6 +295,7 @@ static void srv_spoolss_replycloseprinter(int snum,
 
        if (prn_hnd->notify.cli_chan) {
                prn_hnd->notify.cli_chan->active_connections--;
+               prn_hnd->notify.cli_chan = NULL;
        }
 }
 
@@ -464,7 +465,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,
@@ -686,7 +687,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,
@@ -1541,6 +1542,7 @@ void do_drv_upgrade_printer(struct messaging_context *msg,
        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;
@@ -1573,7 +1575,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);
 
@@ -1592,9 +1604,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)) {
@@ -1912,7 +1922,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));
@@ -2003,7 +2013,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),
@@ -2032,6 +2042,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;
@@ -2054,9 +2070,12 @@ 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 */
@@ -2078,75 +2097,115 @@ WERROR _spoolss_DeletePrinterDriver(struct pipes_struct *p,
        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, get_session_info_system(),
+                                         p->msg_ctx, 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, get_session_info_system(),
+                                 p->msg_ctx, 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
+        *     is 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,
+                                               get_session_info_system(),
+                                                         p->msg_ctx,
+                                                         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);
+       status = winreg_del_driver(mem_ctx, b, info, info->version);
+       if (!W_ERROR_IS_OK(status)) {
+               goto done;
+       }
 
-done:
-       talloc_free(info);
+       /*
+        * 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);
+       }
 
+done:
        return status;
 }
 
@@ -2157,11 +2216,12 @@ 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 */
@@ -2178,153 +2238,57 @@ WERROR _spoolss_DeletePrinterDriverEx(struct pipes_struct *p,
        }
 
        /* 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;
 }
 
@@ -3255,7 +3219,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);
 }
 
 /*******************************************************************
@@ -3592,7 +3556,7 @@ 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),
@@ -3680,7 +3644,7 @@ 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);
@@ -3688,6 +3652,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];
 
@@ -3712,7 +3685,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);
                        }
 
@@ -3929,7 +3902,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;
@@ -4282,6 +4255,7 @@ 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;
 
        *count_p = 0;
        *info_p = NULL;
@@ -4300,9 +4274,17 @@ 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(mem_ctx,
+                                                              session_info,
+                                                              msg_ctx,
+                                                              &b);
+                       if (!W_ERROR_IS_OK(result)) {
+                               goto out;
+                       }
+               }
+
+               result = winreg_create_printer(mem_ctx, b,
                                               printer);
                if (!W_ERROR_IS_OK(result)) {
                        goto out;
@@ -4316,7 +4298,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(mem_ctx, b,
                                            printer, &info2);
                if (!W_ERROR_IS_OK(result)) {
                        goto out;
@@ -4733,7 +4715,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),
@@ -5506,14 +5488,21 @@ 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;
 
        if (level == 101) {
                return WERR_UNKNOWN_LEVEL;
        }
 
-       result = winreg_get_printer(mem_ctx,
-                                   session_info,
-                                   msg_ctx,
+       result = winreg_printer_binding_handle(mem_ctx,
+                                              session_info,
+                                              msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
+       }
+
+       result = winreg_get_printer(mem_ctx, b,
                                    lp_const_servicename(snum),
                                    &pinfo2);
 
@@ -5524,7 +5513,7 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
                return WERR_INVALID_PRINTER_NAME;
        }
 
-       result = winreg_get_driver(mem_ctx, session_info, msg_ctx,
+       result = winreg_get_driver(mem_ctx, b,
                                   architecture,
                                   pinfo2->drivername, version, &driver);
 
@@ -5543,7 +5532,7 @@ static WERROR construct_printer_driver_info_level(TALLOC_CTX *mem_ctx,
 
                /* Yes - try again with a WinNT driver. */
                version = 2;
-               result = winreg_get_driver(mem_ctx, session_info, msg_ctx,
+               result = winreg_get_driver(mem_ctx, b,
                                           architecture,
                                           pinfo2->drivername,
                                           version, &driver);
@@ -5927,8 +5916,8 @@ 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;
 
        if (!Printer || !get_printer_snum(p, handle, &snum, NULL)) {
                DEBUG(2,("update_printer_sec: Invalid handle (%s:%u:%u)\n",
@@ -5956,15 +5945,21 @@ static WERROR update_printer_sec(struct policy_handle *handle,
                goto done;
        }
 
+       result = winreg_printer_binding_handle(p->mem_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(p->mem_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;
        }
@@ -6010,9 +6005,7 @@ 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(p->mem_ctx, b,
                                            printer,
                                            new_secdesc);
 
@@ -6218,12 +6211,19 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        const char *spooling;
        DATA_BLOB buffer;
        WERROR result = WERR_OK;
+       struct dcerpc_binding_handle *b;
+
+       result = winreg_printer_binding_handle(mem_ctx,
+                                              session_info,
+                                              msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
+       }
 
        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,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_DRIVERNAME,
@@ -6243,9 +6243,7 @@ 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,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_DESCRIPTION,
@@ -6262,9 +6260,7 @@ 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,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSHARENAME,
@@ -6291,9 +6287,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                }
 
                push_reg_sz(mem_ctx, &buffer, p);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTERNAME,
@@ -6309,9 +6303,7 @@ 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,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PORTNAME,
@@ -6328,9 +6320,7 @@ 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,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_LOCATION,
@@ -6348,9 +6338,7 @@ 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,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSEPARATORFILE,
@@ -6369,9 +6357,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        if (force_update || printer->starttime != old_printer->starttime) {
                buffer = data_blob_talloc(mem_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->starttime);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSTARTTIME,
@@ -6383,9 +6369,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        if (force_update || printer->untiltime != old_printer->untiltime) {
                buffer = data_blob_talloc(mem_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->untiltime);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTENDTIME,
@@ -6397,9 +6381,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        if (force_update || printer->priority != old_printer->priority) {
                buffer = data_blob_talloc(mem_ctx, NULL, 4);
                SIVAL(buffer.data, 0, printer->priority);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRIORITY,
@@ -6412,9 +6394,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                buffer = data_blob_talloc(mem_ctx, NULL, 4);
                SIVAL(buffer.data, 0, (printer->attributes &
                                       PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS));
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTKEEPPRINTEDJOBS,
@@ -6436,9 +6416,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
                                spooling = "unknown";
                }
                push_reg_sz(mem_ctx, &buffer, spooling);
-               winreg_set_printer_dataex(mem_ctx,
-                                         session_info,
-                                         msg_ctx,
+               winreg_set_printer_dataex(mem_ctx, b,
                                          printer->sharename,
                                          SPOOL_DSSPOOLER_KEY,
                                          SPOOL_REG_PRINTSPOOLING,
@@ -6448,9 +6426,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        }
 
        push_reg_sz(mem_ctx, &buffer, global_myname());
-       winreg_set_printer_dataex(mem_ctx,
-                                 session_info,
-                                 msg_ctx,
+       winreg_set_printer_dataex(mem_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_SHORTSERVERNAME,
@@ -6470,9 +6446,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        }
 
        push_reg_sz(mem_ctx, &buffer, longname);
-       winreg_set_printer_dataex(mem_ctx,
-                                 session_info,
-                                 msg_ctx,
+       winreg_set_printer_dataex(mem_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_SERVERNAME,
@@ -6483,9 +6457,7 @@ static WERROR update_dsspooler(TALLOC_CTX *mem_ctx,
        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,
+       winreg_set_printer_dataex(mem_ctx, b,
                                  printer->sharename,
                                  SPOOL_DSSPOOLER_KEY,
                                  SPOOL_REG_UNCNAME,
@@ -6514,6 +6486,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"));
 
@@ -6532,9 +6505,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)) {
@@ -6588,9 +6567,7 @@ static WERROR update_printer(struct pipes_struct *p,
        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,
@@ -6629,7 +6606,7 @@ 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),
@@ -6678,7 +6655,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),
@@ -6806,7 +6783,7 @@ 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));
        W_ERROR_HAVE_NO_MEMORY(r->printer_name);
@@ -6847,7 +6824,7 @@ 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));
        W_ERROR_HAVE_NO_MEMORY(r->printer_name);
@@ -6900,10 +6877,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;
 
@@ -7092,7 +7069,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),
@@ -7212,17 +7189,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;
@@ -7277,12 +7250,21 @@ 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;
 
        *count_p = 0;
        *info_p = NULL;
 
+       result = winreg_printer_binding_handle(mem_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(mem_ctx, b,
                                                architecture, version,
                                                &num_drivers, &drivers);
                if (!W_ERROR_IS_OK(result)) {
@@ -7307,8 +7289,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(mem_ctx, b,
                                                   architecture, drivers[i],
                                                   version, &driver);
                        if (!W_ERROR_IS_OK(result)) {
@@ -7503,7 +7484,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,
@@ -7553,7 +7534,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,
@@ -7928,7 +7909,7 @@ static WERROR spoolss_addprinterex_level_2(struct pipes_struct *p,
                         info2,
                         NULL);
 
-       err = winreg_update_printer(p->mem_ctx,
+       err = winreg_update_printer_internal(p->mem_ctx,
                                    get_session_info_system(),
                                    p->msg_ctx,
                                    info2->sharename,
@@ -8055,7 +8036,7 @@ WERROR _spoolss_AddPrinterDriverEx(struct pipes_struct *p,
                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,
@@ -8462,8 +8443,8 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
        struct spoolss_AddFormInfo1 *form = r->in.info.info1;
        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;
 
        DEBUG(5,("_spoolss_AddForm\n"));
 
@@ -8496,9 +8477,15 @@ 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,
+       status = winreg_printer_binding_handle(p->mem_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               return status;
+       }
+
+       status = winreg_printer_addform1(p->mem_ctx, b,
                                         form);
        if (!W_ERROR_IS_OK(status)) {
                return status;
@@ -8512,9 +8499,7 @@ WERROR _spoolss_AddForm(struct pipes_struct *p,
                        return WERR_BADFID;
                }
 
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(p->mem_ctx, b,
                                                        lp_const_servicename(snum));
                if (!W_ERROR_IS_OK(status)) {
                        return status;
@@ -8535,6 +8520,7 @@ 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;
 
        DEBUG(5,("_spoolss_DeleteForm\n"));
 
@@ -8555,9 +8541,15 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       status = winreg_printer_deleteform1(p->mem_ctx,
-                                           get_session_info_system(),
-                                           p->msg_ctx,
+       status = winreg_printer_binding_handle(p->mem_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               return status;
+       }
+
+       status = winreg_printer_deleteform1(p->mem_ctx, b,
                                            form_name);
        if (!W_ERROR_IS_OK(status)) {
                return status;
@@ -8571,9 +8563,7 @@ WERROR _spoolss_DeleteForm(struct pipes_struct *p,
                        return WERR_BADFID;
                }
 
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(p->mem_ctx, b,
                                                        lp_const_servicename(snum));
                if (!W_ERROR_IS_OK(status)) {
                        return status;
@@ -8594,6 +8584,7 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
        const char *form_name = r->in.form_name;
        int snum = -1;
        WERROR status = WERR_OK;
+       struct dcerpc_binding_handle *b;
 
        struct printer_handle *Printer = find_printer_index_by_hnd(p, r->in.handle);
 
@@ -8619,9 +8610,15 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       status = winreg_printer_setform1(p->mem_ctx,
-                                        get_session_info_system(),
-                                        p->msg_ctx,
+       status = winreg_printer_binding_handle(p->mem_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               return status;
+       }
+
+       status = winreg_printer_setform1(p->mem_ctx, b,
                                         form_name,
                                         form);
        if (!W_ERROR_IS_OK(status)) {
@@ -8636,9 +8633,7 @@ WERROR _spoolss_SetForm(struct pipes_struct *p,
                        return WERR_BADFID;
                }
 
-               status = winreg_printer_update_changeid(p->mem_ctx,
-                                                       get_session_info_system(),
-                                                       p->msg_ctx,
+               status = winreg_printer_update_changeid(p->mem_ctx, b,
                                                        lp_const_servicename(snum));
                if (!W_ERROR_IS_OK(status)) {
                        return status;
@@ -9029,7 +9024,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;
                }
@@ -9064,7 +9059,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;
                }
@@ -9082,7 +9077,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,
@@ -9130,7 +9125,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),
@@ -9191,7 +9186,7 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
        enum winreg_Type val_type = REG_NONE;
        uint8_t *val_data = NULL;
        uint32_t val_size = 0;
-
+       struct dcerpc_binding_handle *b;
 
        DEBUG(4,("_spoolss_GetPrinterDataEx\n"));
 
@@ -9249,6 +9244,14 @@ WERROR _spoolss_GetPrinterDataEx(struct pipes_struct *p,
                return WERR_INVALID_PARAM;
        }
 
+       result = winreg_printer_binding_handle(p->mem_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
+       }
+
        /* XP sends this and wants the ChangeID value from PRINTER_INFO_0 */
        if (strequal(r->in.key_name, SPOOL_PRINTERDATA_KEY) &&
            strequal(r->in.value_name, "ChangeId")) {
@@ -9257,9 +9260,7 @@ 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(p->mem_ctx, b,
                                                             printer,
                                                             &changeid);
                        if (!W_ERROR_IS_OK(result)) {
@@ -9272,9 +9273,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(p->mem_ctx, b,
                                           printer,
                                           r->in.key_name,
                                           r->in.value_name,
@@ -9311,6 +9310,7 @@ 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;
 
        DEBUG(4,("_spoolss_SetPrinterDataEx\n"));
 
@@ -9347,9 +9347,15 @@ WERROR _spoolss_SetPrinterDataEx(struct pipes_struct *p,
                return WERR_ACCESS_DENIED;
        }
 
-       result = winreg_get_printer(Printer,
-                                   get_session_info_system(),
-                                   p->msg_ctx,
+       result = winreg_printer_binding_handle(p->mem_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(result)) {
+               return result;
+       }
+
+       result = winreg_get_printer(Printer, b,
                                    lp_servicename(snum),
                                    &pinfo2);
        if (!W_ERROR_IS_OK(result)) {
@@ -9366,9 +9372,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(p->mem_ctx, b,
                                           pinfo2->sharename,
                                           r->in.key_name,
                                           r->in.value_name,
@@ -9392,9 +9396,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(p->mem_ctx, b,
                                                  pinfo2->sharename,
                                                  str,
                                                  r->in.value_name,
@@ -9403,9 +9405,7 @@ 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(p->mem_ctx, b,
                                                        lp_const_servicename(snum));
 
        }
@@ -9451,14 +9451,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);
@@ -9493,7 +9493,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),
@@ -9541,6 +9541,7 @@ WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
        int                     snum=0;
        WERROR                  status;
        const char *printer;
+       struct dcerpc_binding_handle *b;
 
        DEBUG(5,("_spoolss_DeletePrinterKey\n"));
 
@@ -9566,16 +9567,20 @@ WERROR _spoolss_DeletePrinterKey(struct pipes_struct *p,
 
        printer = lp_const_servicename(snum);
 
+       status = winreg_printer_binding_handle(p->mem_ctx,
+                                              get_session_info_system(),
+                                              p->msg_ctx,
+                                              &b);
+       if (!W_ERROR_IS_OK(status)) {
+               return status;
+       }
+
        /* 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(p->mem_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(p->mem_ctx, b,
                                                        printer);
        }
 
@@ -9624,7 +9629,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),