spoolss: clear JobInfo on GetJob error
[obnox/samba/samba-obnox.git] / source3 / rpc_server / spoolss / srv_spoolss_nt.c
index 45d60df613ee0db1606c1b07eb84ea62cae825ae..c71eb911097f84292dd434ed73395943cd7e5f22 100644 (file)
@@ -4902,7 +4902,8 @@ static WERROR string_array_from_driver_info(TALLOC_CTX *mem_ctx,
                                                  const char *arch,
                                                  int version)
 {
-       int i, num_strings = 0;
+       int i;
+       size_t num_strings = 0;
        const char **array = NULL;
 
        if (string_array == NULL) {
@@ -7149,27 +7150,6 @@ static WERROR fill_job_info2(TALLOC_CTX *mem_ctx,
        return WERR_OK;
 }
 
-/****************************************************************************
-fill_job_info3
-****************************************************************************/
-
-static WERROR fill_job_info3(TALLOC_CTX *mem_ctx,
-                            struct spoolss_JobInfo3 *r,
-                            const print_queue_struct *queue,
-                            const print_queue_struct *next_queue,
-                            int position, int snum,
-                            struct spoolss_PrinterInfo2 *pinfo2)
-{
-       r->job_id               = queue->sysjob;
-       r->next_job_id          = 0;
-       if (next_queue) {
-               r->next_job_id  = next_queue->sysjob;
-       }
-       r->reserved             = 0;
-
-       return WERR_OK;
-}
-
 /****************************************************************************
  Enumjobs at level 1.
 ****************************************************************************/
@@ -7325,41 +7305,51 @@ static WERROR enumjobs_level3(TALLOC_CTX *mem_ctx,
        union spoolss_JobInfo *info;
        int i;
        WERROR result = WERR_OK;
+       uint32_t num_filled;
+       struct tdb_print_db *pdb;
 
        info = talloc_array(mem_ctx, union spoolss_JobInfo, num_queues);
-       W_ERROR_HAVE_NO_MEMORY(info);
-
-       *count = num_queues;
+       if (info == NULL) {
+               result = WERR_NOMEM;
+               goto err_out;
+       }
 
-       for (i=0; i<*count; i++) {
-               const print_queue_struct *next_queue = NULL;
+       pdb = get_print_db_byname(pinfo2->sharename);
+       if (pdb == NULL) {
+               result = WERR_INVALID_PARAM;
+               goto err_info_free;
+       }
 
-               if (i+1 < *count) {
-                       next_queue = &queue[i+1];
+       num_filled = 0;
+       for (i = 0; i < num_queues; i++) {
+               uint32_t jobid = sysjob_to_jobid_pdb(pdb, queue[i].sysjob);
+               if (jobid == (uint32_t)-1) {
+                       DEBUG(4, ("skipping sysjob %d\n", queue[i].sysjob));
+                       continue;
                }
 
-               result = fill_job_info3(info,
-                                       &info[i].info3,
-                                       &queue[i],
-                                       next_queue,
-                                       i,
-                                       snum,
-                                       pinfo2);
-               if (!W_ERROR_IS_OK(result)) {
-                       goto out;
-               }
-       }
+               info[num_filled].info3.job_id = jobid;
+               /* next_job_id is overwritten on next iteration */
+               info[num_filled].info3.next_job_id = 0;
+               info[num_filled].info3.reserved = 0;
 
- out:
-       if (!W_ERROR_IS_OK(result)) {
-               TALLOC_FREE(info);
-               *count = 0;
-               return result;
+               if (num_filled > 0) {
+                       info[num_filled - 1].info3.next_job_id = jobid;
+               }
+               num_filled++;
        }
 
+       release_print_db(pdb);
        *info_p = info;
+       *count = num_filled;
 
        return WERR_OK;
+
+err_info_free:
+       TALLOC_FREE(info);
+err_out:
+       *count = 0;
+       return result;
 }
 
 /****************************************************************
@@ -9494,7 +9484,8 @@ WERROR _spoolss_GetJob(struct pipes_struct *p,
        /* that's an [in out] buffer */
 
        if (!r->in.buffer && (r->in.offered != 0)) {
-               return WERR_INVALID_PARAM;
+               result = WERR_INVALID_PARAM;
+               goto err_jinfo_free;
        }
 
        DEBUG(5,("_spoolss_GetJob\n"));
@@ -9502,12 +9493,14 @@ WERROR _spoolss_GetJob(struct pipes_struct *p,
        *r->out.needed = 0;
 
        if (!get_printer_snum(p, r->in.handle, &snum, NULL)) {
-               return WERR_BADFID;
+               result = WERR_BADFID;
+               goto err_jinfo_free;
        }
 
        svc_name = lp_const_servicename(snum);
        if (svc_name == NULL) {
-               return WERR_INVALID_PARAM;
+               result = WERR_INVALID_PARAM;
+               goto err_jinfo_free;
        }
 
        result = winreg_get_printer_internal(p->mem_ctx,
@@ -9516,22 +9509,22 @@ WERROR _spoolss_GetJob(struct pipes_struct *p,
                                    svc_name,
                                    &pinfo2);
        if (!W_ERROR_IS_OK(result)) {
-               return result;
+               goto err_jinfo_free;
        }
 
        pdb = get_print_db_byname(svc_name);
        if (pdb == NULL) {
                DEBUG(3, ("failed to get print db for svc %s\n", svc_name));
-               TALLOC_FREE(pinfo2);
-               return WERR_INVALID_PARAM;
+               result = WERR_INVALID_PARAM;
+               goto err_pinfo_free;
        }
 
        sysjob = jobid_to_sysjob_pdb(pdb, r->in.job_id);
        release_print_db(pdb);
        if (sysjob == -1) {
                DEBUG(3, ("no sysjob for spoolss jobid %u\n", r->in.job_id));
-               TALLOC_FREE(pinfo2);
-               return WERR_INVALID_PARAM;
+               result = WERR_INVALID_PARAM;
+               goto err_pinfo_free;
        }
 
        count = print_queue_status(p->msg_ctx, snum, &queue, &prt_status);
@@ -9561,8 +9554,7 @@ WERROR _spoolss_GetJob(struct pipes_struct *p,
        TALLOC_FREE(pinfo2);
 
        if (!W_ERROR_IS_OK(result)) {
-               TALLOC_FREE(r->out.info);
-               return result;
+               goto err_jinfo_free;
        }
 
        *r->out.needed  = SPOOLSS_BUFFER_UNION(spoolss_JobInfo, r->out.info,
@@ -9570,6 +9562,12 @@ WERROR _spoolss_GetJob(struct pipes_struct *p,
        r->out.info     = SPOOLSS_BUFFER_OK(r->out.info, NULL);
 
        return SPOOLSS_BUFFER_OK(WERR_OK, WERR_INSUFFICIENT_BUFFER);
+
+err_pinfo_free:
+       TALLOC_FREE(pinfo2);
+err_jinfo_free:
+       TALLOC_FREE(r->out.info);
+       return result;
 }
 
 /****************************************************************