2 Unix SMB/Netbios implementation.
4 printing backend routines
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Jeremy Allison 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/syslog.h"
24 #include "system/filesys.h"
26 #include "../librpc/gen_ndr/ndr_spoolss.h"
27 #include "nt_printing.h"
28 #include "../librpc/gen_ndr/netlogon.h"
29 #include "printing/notify.h"
30 #include "printing/pcap.h"
31 #include "printing/printer_list.h"
32 #include "printing/queue_process.h"
34 #include "smbd/smbd.h"
38 #include "lib/param/loadparm.h"
40 extern struct current_user current_user
;
41 extern userdom_struct current_user_info
;
43 /* Current printer interface */
44 static bool remove_from_jobs_added(const char* sharename
, uint32 jobid
);
47 the printing backend revolves around a tdb database that stores the
48 SMB view of the print queue
50 The key for this database is a jobid - a internally generated number that
51 uniquely identifies a print job
53 reading the print queue involves two steps:
54 - possibly running lpq and updating the internal database from that
55 - reading entries from the database
57 jobids are assigned when a job starts spooling.
60 static TDB_CONTEXT
*rap_tdb
;
61 static uint16 next_rap_jobid
;
62 struct rap_jobid_key
{
67 /***************************************************************************
68 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
69 bit RPC jobids.... JRA.
70 ***************************************************************************/
72 uint16
pjobid_to_rap(const char* sharename
, uint32 jobid
)
76 struct rap_jobid_key jinfo
;
79 DEBUG(10,("pjobid_to_rap: called.\n"));
82 /* Create the in-memory tdb. */
83 rap_tdb
= tdb_open_log(NULL
, 0, TDB_INTERNAL
, (O_RDWR
|O_CREAT
), 0644);
89 fstrcpy( jinfo
.sharename
, sharename
);
91 key
.dptr
= (uint8
*)&jinfo
;
92 key
.dsize
= sizeof(jinfo
);
94 data
= tdb_fetch_compat(rap_tdb
, key
);
95 if (data
.dptr
&& data
.dsize
== sizeof(uint16
)) {
96 rap_jobid
= SVAL(data
.dptr
, 0);
98 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
99 (unsigned int)jobid
, (unsigned int)rap_jobid
));
102 SAFE_FREE(data
.dptr
);
103 /* Not found - create and store mapping. */
104 rap_jobid
= ++next_rap_jobid
;
106 rap_jobid
= ++next_rap_jobid
;
107 SSVAL(buf
,0,rap_jobid
);
109 data
.dsize
= sizeof(rap_jobid
);
110 tdb_store(rap_tdb
, key
, data
, TDB_REPLACE
);
111 tdb_store(rap_tdb
, data
, key
, TDB_REPLACE
);
113 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
114 (unsigned int)jobid
, (unsigned int)rap_jobid
));
118 bool rap_to_pjobid(uint16 rap_jobid
, fstring sharename
, uint32
*pjobid
)
123 DEBUG(10,("rap_to_pjobid called.\n"));
128 SSVAL(buf
,0,rap_jobid
);
130 key
.dsize
= sizeof(rap_jobid
);
131 data
= tdb_fetch_compat(rap_tdb
, key
);
132 if ( data
.dptr
&& data
.dsize
== sizeof(struct rap_jobid_key
) )
134 struct rap_jobid_key
*jinfo
= (struct rap_jobid_key
*)data
.dptr
;
135 if (sharename
!= NULL
) {
136 fstrcpy( sharename
, jinfo
->sharename
);
138 *pjobid
= jinfo
->jobid
;
139 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
140 (unsigned int)*pjobid
, (unsigned int)rap_jobid
));
141 SAFE_FREE(data
.dptr
);
145 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
146 (unsigned int)rap_jobid
));
147 SAFE_FREE(data
.dptr
);
151 void rap_jobid_delete(const char* sharename
, uint32 jobid
)
155 struct rap_jobid_key jinfo
;
158 DEBUG(10,("rap_jobid_delete: called.\n"));
163 ZERO_STRUCT( jinfo
);
164 fstrcpy( jinfo
.sharename
, sharename
);
166 key
.dptr
= (uint8
*)&jinfo
;
167 key
.dsize
= sizeof(jinfo
);
169 data
= tdb_fetch_compat(rap_tdb
, key
);
170 if (!data
.dptr
|| (data
.dsize
!= sizeof(uint16
))) {
171 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
172 (unsigned int)jobid
));
173 SAFE_FREE(data
.dptr
);
177 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
178 (unsigned int)jobid
));
180 rap_jobid
= SVAL(data
.dptr
, 0);
181 SAFE_FREE(data
.dptr
);
182 SSVAL(buf
,0,rap_jobid
);
184 data
.dsize
= sizeof(rap_jobid
);
185 tdb_delete(rap_tdb
, key
);
186 tdb_delete(rap_tdb
, data
);
189 static int get_queue_status(const char* sharename
, print_status_struct
*);
191 /****************************************************************************
192 Initialise the printing backend. Called once at startup before the fork().
193 ****************************************************************************/
195 bool print_backend_init(struct messaging_context
*msg_ctx
)
197 const char *sversion
= "INFO/version";
198 int services
= lp_numservices();
202 if (!printer_list_parent_init()) {
206 ok
= directory_create_or_exist(cache_path("printing"), geteuid(), 0755);
211 unlink(cache_path("printing.tdb"));
213 /* handle a Samba upgrade */
215 for (snum
= 0; snum
< services
; snum
++) {
216 struct tdb_print_db
*pdb
;
217 if (!lp_print_ok(snum
))
220 pdb
= get_print_db_byname(lp_const_servicename(snum
));
223 if (tdb_lock_bystring(pdb
->tdb
, sversion
) != 0) {
224 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum
) ));
225 release_print_db(pdb
);
228 if (tdb_fetch_int32(pdb
->tdb
, sversion
) != PRINT_DATABASE_VERSION
) {
229 tdb_wipe_all(pdb
->tdb
);
230 tdb_store_int32(pdb
->tdb
, sversion
, PRINT_DATABASE_VERSION
);
232 tdb_unlock_bystring(pdb
->tdb
, sversion
);
233 release_print_db(pdb
);
236 close_all_print_db(); /* Don't leave any open. */
238 /* do NT print initialization... */
239 return nt_printing_init(msg_ctx
);
242 /****************************************************************************
243 Shut down printing backend. Called once at shutdown to close the tdb.
244 ****************************************************************************/
246 void printing_end(void)
248 close_all_print_db(); /* Don't leave any open. */
251 /****************************************************************************
252 Retrieve the set of printing functions for a given service. This allows
253 us to set the printer function table based on the value of the 'printing'
256 Use the generic interface as the default and only use cups interface only
257 when asked for (and only when supported)
258 ****************************************************************************/
260 static struct printif
*get_printer_fns_from_type( enum printing_types type
)
262 struct printif
*printer_fns
= &generic_printif
;
265 if ( type
== PRINT_CUPS
) {
266 printer_fns
= &cups_printif
;
268 #endif /* HAVE_CUPS */
271 if ( type
== PRINT_IPRINT
) {
272 printer_fns
= &iprint_printif
;
274 #endif /* HAVE_IPRINT */
276 printer_fns
->type
= type
;
281 static struct printif
*get_printer_fns( int snum
)
283 return get_printer_fns_from_type( (enum printing_types
)lp_printing(snum
) );
287 /****************************************************************************
288 Useful function to generate a tdb key.
289 ****************************************************************************/
291 static TDB_DATA
print_key(uint32 jobid
, uint32
*tmp
)
295 SIVAL(tmp
, 0, jobid
);
296 ret
.dptr
= (uint8
*)tmp
;
297 ret
.dsize
= sizeof(*tmp
);
301 /****************************************************************************
302 Pack the devicemode to store it in a tdb.
303 ****************************************************************************/
304 static int pack_devicemode(struct spoolss_DeviceMode
*devmode
, uint8
*buf
, int buflen
)
306 enum ndr_err_code ndr_err
;
311 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(),
313 (ndr_push_flags_fn_t
)
314 ndr_push_spoolss_DeviceMode
);
315 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
316 DEBUG(10, ("pack_devicemode: "
317 "error encoding spoolss_DeviceMode\n"));
324 len
= tdb_pack(buf
, buflen
, "B", blob
.length
, blob
.data
);
327 DEBUG(8, ("Packed devicemode [%s]\n", devmode
->formname
));
334 /****************************************************************************
335 Unpack the devicemode to store it in a tdb.
336 ****************************************************************************/
337 static int unpack_devicemode(TALLOC_CTX
*mem_ctx
,
338 const uint8
*buf
, int buflen
,
339 struct spoolss_DeviceMode
**devmode
)
341 struct spoolss_DeviceMode
*dm
;
342 enum ndr_err_code ndr_err
;
350 len
= tdb_unpack(buf
, buflen
, "B", &data_len
, &data
);
355 dm
= talloc_zero(mem_ctx
, struct spoolss_DeviceMode
);
360 blob
= data_blob_const(data
, data_len
);
362 ndr_err
= ndr_pull_struct_blob(&blob
, dm
, dm
,
363 (ndr_pull_flags_fn_t
)ndr_pull_spoolss_DeviceMode
);
364 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
365 DEBUG(10, ("unpack_devicemode: "
366 "error parsing spoolss_DeviceMode\n"));
370 DEBUG(8, ("Unpacked devicemode [%s](%s)\n",
371 dm
->devicename
, dm
->formname
));
372 if (dm
->driverextra_data
.data
) {
373 DEBUG(8, ("with a private section of %d bytes\n",
374 dm
->__driverextra_length
));
384 /***********************************************************************
385 unpack a pjob from a tdb buffer
386 ***********************************************************************/
388 static int unpack_pjob(TALLOC_CTX
*mem_ctx
, uint8
*buf
, int buflen
,
389 struct printjob
*pjob
)
393 uint32 pjpid
, pjjobid
, pjsysjob
, pjfd
, pjstarttime
, pjstatus
;
394 uint32 pjsize
, pjpage_count
, pjspooled
, pjsmbjob
;
400 len
+= tdb_unpack(buf
+len
, buflen
-len
, "ddddddddddfffff",
421 used
= unpack_devicemode(mem_ctx
, buf
+len
, buflen
-len
, &pjob
->devmode
);
429 pjob
->jobid
= pjjobid
;
430 pjob
->sysjob
= pjsysjob
;
432 pjob
->starttime
= pjstarttime
;
433 pjob
->status
= pjstatus
;
435 pjob
->page_count
= pjpage_count
;
436 pjob
->spooled
= pjspooled
;
437 pjob
->smbjob
= pjsmbjob
;
443 /****************************************************************************
444 Useful function to find a print job in the database.
445 ****************************************************************************/
447 static struct printjob
*print_job_find(TALLOC_CTX
*mem_ctx
,
448 const char *sharename
,
451 struct printjob
*pjob
;
454 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
456 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
457 (unsigned int)jobid
, sharename
));
463 ret
= tdb_fetch_compat(pdb
->tdb
, print_key(jobid
, &tmp
));
464 release_print_db(pdb
);
467 DEBUG(10, ("print_job_find: failed to find jobid %u.\n",
472 pjob
= talloc_zero(mem_ctx
, struct printjob
);
477 if (unpack_pjob(mem_ctx
, ret
.dptr
, ret
.dsize
, pjob
) == -1) {
478 DEBUG(10, ("failed to unpack jobid %u.\n", jobid
));
484 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
485 pjob
->sysjob
, jobid
));
486 SMB_ASSERT(pjob
->jobid
== jobid
);
493 /* Convert a unix jobid to a smb jobid */
495 struct unixjob_traverse_state
{
497 uint32 sysjob_to_jobid_value
;
500 static int unixjob_traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
501 TDB_DATA data
, void *private_data
)
503 struct printjob
*pjob
;
504 struct unixjob_traverse_state
*state
=
505 (struct unixjob_traverse_state
*)private_data
;
507 if (!data
.dptr
|| data
.dsize
== 0)
510 pjob
= (struct printjob
*)data
.dptr
;
511 if (key
.dsize
!= sizeof(uint32
))
514 if (state
->sysjob
== pjob
->sysjob
) {
515 state
->sysjob_to_jobid_value
= pjob
->jobid
;
522 uint32
sysjob_to_jobid_pdb(struct tdb_print_db
*pdb
, int sysjob
)
524 struct unixjob_traverse_state state
;
526 state
.sysjob
= sysjob
;
527 state
.sysjob_to_jobid_value
= (uint32
)-1;
529 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
531 return state
.sysjob_to_jobid_value
;
534 /****************************************************************************
535 This is a *horribly expensive call as we have to iterate through all the
536 current printer tdb's. Don't do this often ! JRA.
537 ****************************************************************************/
539 uint32
sysjob_to_jobid(int unix_jobid
)
541 int services
= lp_numservices();
543 struct unixjob_traverse_state state
;
545 state
.sysjob
= unix_jobid
;
546 state
.sysjob_to_jobid_value
= (uint32
)-1;
548 for (snum
= 0; snum
< services
; snum
++) {
549 struct tdb_print_db
*pdb
;
550 if (!lp_print_ok(snum
))
552 pdb
= get_print_db_byname(lp_const_servicename(snum
));
556 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
557 release_print_db(pdb
);
558 if (state
.sysjob_to_jobid_value
!= (uint32
)-1)
559 return state
.sysjob_to_jobid_value
;
564 /****************************************************************************
565 Send notifications based on what has changed after a pjob_store.
566 ****************************************************************************/
568 static const struct {
570 uint32_t spoolss_status
;
571 } lpq_to_spoolss_status_map
[] = {
572 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
573 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
574 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
575 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
576 { LPQ_DELETING
, JOB_STATUS_DELETING
},
577 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
578 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
579 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
580 { LPQ_DELETED
, JOB_STATUS_DELETED
},
581 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED_DEVQ
},
582 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
586 /* Convert a lpq status value stored in printing.tdb into the
587 appropriate win32 API constant. */
589 static uint32
map_to_spoolss_status(uint32 lpq_status
)
593 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
594 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
595 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
602 /***************************************************************************
603 Append a jobid to the 'jobs changed' list.
604 ***************************************************************************/
606 static bool add_to_jobs_changed(struct tdb_print_db
*pdb
, uint32_t jobid
)
609 uint32_t store_jobid
;
611 SIVAL(&store_jobid
, 0, jobid
);
612 data
.dptr
= (uint8
*) &store_jobid
;
615 DEBUG(10,("add_to_jobs_added: Added jobid %u\n", (unsigned int)jobid
));
617 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"),
621 /***************************************************************************
622 Remove a jobid from the 'jobs changed' list.
623 ***************************************************************************/
625 static bool remove_from_jobs_changed(const char* sharename
, uint32_t jobid
)
627 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
631 bool gotlock
= False
;
639 key
= string_tdb_data("INFO/jobs_changed");
641 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) != 0)
646 data
= tdb_fetch_compat(pdb
->tdb
, key
);
648 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
651 job_count
= data
.dsize
/ 4;
652 for (i
= 0; i
< job_count
; i
++) {
655 ch_jobid
= IVAL(data
.dptr
, i
*4);
656 if (ch_jobid
== jobid
) {
657 if (i
< job_count
-1 )
658 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
660 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) != 0)
670 tdb_chainunlock(pdb
->tdb
, key
);
671 SAFE_FREE(data
.dptr
);
672 release_print_db(pdb
);
674 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid
));
676 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid
));
680 static void pjob_store_notify(struct tevent_context
*ev
,
681 struct messaging_context
*msg_ctx
,
682 const char* sharename
, uint32 jobid
,
683 struct printjob
*old_data
,
684 struct printjob
*new_data
,
687 bool new_job
= false;
688 bool changed
= false;
690 if (old_data
== NULL
) {
694 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
695 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
696 time first or else we'll end up with potential alignment
697 errors. I don't think the systemtime should be spooled as
698 a string, but this gets us around that error.
699 --jerry (i'll feel dirty for this) */
702 notify_job_submitted(ev
, msg_ctx
,
703 sharename
, jobid
, new_data
->starttime
);
704 notify_job_username(ev
, msg_ctx
,
705 sharename
, jobid
, new_data
->user
);
706 notify_job_name(ev
, msg_ctx
,
707 sharename
, jobid
, new_data
->jobname
);
708 notify_job_status(ev
, msg_ctx
,
709 sharename
, jobid
, map_to_spoolss_status(new_data
->status
));
710 notify_job_total_bytes(ev
, msg_ctx
,
711 sharename
, jobid
, new_data
->size
);
712 notify_job_total_pages(ev
, msg_ctx
,
713 sharename
, jobid
, new_data
->page_count
);
715 if (!strequal(old_data
->jobname
, new_data
->jobname
)) {
716 notify_job_name(ev
, msg_ctx
, sharename
,
717 jobid
, new_data
->jobname
);
721 if (old_data
->status
!= new_data
->status
) {
722 notify_job_status(ev
, msg_ctx
,
724 map_to_spoolss_status(new_data
->status
));
727 if (old_data
->size
!= new_data
->size
) {
728 notify_job_total_bytes(ev
, msg_ctx
,
729 sharename
, jobid
, new_data
->size
);
732 if (old_data
->page_count
!= new_data
->page_count
) {
733 notify_job_total_pages(ev
, msg_ctx
,
735 new_data
->page_count
);
742 /****************************************************************************
743 Store a job structure back to the database.
744 ****************************************************************************/
746 static bool pjob_store(struct tevent_context
*ev
,
747 struct messaging_context
*msg_ctx
,
748 const char* sharename
, uint32 jobid
,
749 struct printjob
*pjob
)
752 TDB_DATA old_data
, new_data
;
754 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
756 int len
, newlen
, buflen
;
764 old_data
= tdb_fetch_compat(pdb
->tdb
, print_key(jobid
, &tmp
));
766 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
773 len
+= tdb_pack(buf
+len
, buflen
-len
, "ddddddddddfffff",
776 (uint32
)pjob
->sysjob
,
778 (uint32
)pjob
->starttime
,
779 (uint32
)pjob
->status
,
781 (uint32
)pjob
->page_count
,
782 (uint32
)pjob
->spooled
,
783 (uint32
)pjob
->smbjob
,
790 len
+= pack_devicemode(pjob
->devmode
, buf
+len
, buflen
-len
);
793 buf
= (uint8
*)SMB_REALLOC(buf
, len
);
795 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
800 } while ( buflen
!= len
);
806 new_data
.dsize
= len
;
807 ret
= (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), new_data
,
810 /* Send notify updates for what has changed */
813 bool changed
= false;
814 struct printjob old_pjob
;
816 if (old_data
.dsize
) {
817 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
821 len
= unpack_pjob(tmp_ctx
, old_data
.dptr
,
822 old_data
.dsize
, &old_pjob
);
824 pjob_store_notify(ev
,
826 sharename
, jobid
, &old_pjob
,
830 add_to_jobs_changed(pdb
, jobid
);
833 talloc_free(tmp_ctx
);
837 pjob_store_notify(ev
, msg_ctx
,
838 sharename
, jobid
, NULL
, pjob
,
844 release_print_db(pdb
);
845 SAFE_FREE( old_data
.dptr
);
851 /****************************************************************************
852 Remove a job structure from the database.
853 ****************************************************************************/
855 static void pjob_delete(struct tevent_context
*ev
,
856 struct messaging_context
*msg_ctx
,
857 const char* sharename
, uint32 jobid
)
860 struct printjob
*pjob
;
861 uint32 job_status
= 0;
862 struct tdb_print_db
*pdb
;
863 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
864 if (tmp_ctx
== NULL
) {
868 pdb
= get_print_db_byname(sharename
);
873 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
875 DEBUG(5, ("we were asked to delete nonexistent job %u\n",
880 /* We must cycle through JOB_STATUS_DELETING and
881 JOB_STATUS_DELETED for the port monitor to delete the job
884 job_status
= JOB_STATUS_DELETING
|JOB_STATUS_DELETED
;
885 notify_job_status(ev
, msg_ctx
, sharename
, jobid
, job_status
);
887 /* Remove from printing.tdb */
889 tdb_delete(pdb
->tdb
, print_key(jobid
, &tmp
));
890 remove_from_jobs_added(sharename
, jobid
);
891 rap_jobid_delete(sharename
, jobid
);
893 release_print_db(pdb
);
895 talloc_free(tmp_ctx
);
898 /****************************************************************************
899 List a unix job in the print database.
900 ****************************************************************************/
902 static void print_unix_job(struct tevent_context
*ev
,
903 struct messaging_context
*msg_ctx
,
904 const char *sharename
, print_queue_struct
*q
,
907 struct printjob pj
, *old_pj
;
908 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
909 if (tmp_ctx
== NULL
) {
913 if (jobid
== (uint32
)-1) {
914 jobid
= q
->sysjob
+ UNIX_JOB_START
;
917 /* Preserve the timestamp on an existing unix print job */
919 old_pj
= print_job_find(tmp_ctx
, sharename
, jobid
);
925 pj
.sysjob
= q
->sysjob
;
927 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
928 pj
.status
= q
->status
;
931 fstrcpy(pj
.filename
, old_pj
? old_pj
->filename
: "");
932 if (jobid
< UNIX_JOB_START
) {
934 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: "Remote Downlevel Document");
937 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: q
->fs_file
);
939 fstrcpy(pj
.user
, old_pj
? old_pj
->user
: q
->fs_user
);
940 fstrcpy(pj
.queuename
, old_pj
? old_pj
->queuename
: sharename
);
942 pjob_store(ev
, msg_ctx
, sharename
, jobid
, &pj
);
943 talloc_free(tmp_ctx
);
947 struct traverse_struct
{
948 print_queue_struct
*queue
;
949 int qcount
, snum
, maxcount
, total_jobs
;
950 const char *sharename
;
952 const char *lprm_command
;
953 struct printif
*print_if
;
954 struct tevent_context
*ev
;
955 struct messaging_context
*msg_ctx
;
959 /****************************************************************************
960 Utility fn to delete any jobs that are no longer active.
961 ****************************************************************************/
963 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
965 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
966 struct printjob pjob
;
970 if ( key
.dsize
!= sizeof(jobid
) )
973 if (unpack_pjob(ts
->mem_ctx
, data
.dptr
, data
.dsize
, &pjob
) == -1)
975 talloc_free(pjob
.devmode
);
979 /* remove a unix job if it isn't in the system queue any more */
980 for (i
=0;i
<ts
->qcount
;i
++) {
981 if (ts
->queue
[i
].sysjob
== pjob
.sysjob
) {
985 if (i
== ts
->qcount
) {
986 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
987 (unsigned int)jobid
));
988 pjob_delete(ts
->ev
, ts
->msg_ctx
,
989 ts
->sharename
, jobid
);
993 /* need to continue the the bottom of the function to
994 save the correct attributes */
997 /* maybe it hasn't been spooled yet */
999 /* if a job is not spooled and the process doesn't
1000 exist then kill it. This cleans up after smbd
1002 if (!process_exists_by_pid(pjob
.pid
)) {
1003 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
1004 (unsigned int)jobid
, (unsigned int)pjob
.pid
));
1005 pjob_delete(ts
->ev
, ts
->msg_ctx
,
1006 ts
->sharename
, jobid
);
1012 /* this check only makes sense for jobs submitted from Windows clients */
1015 for (i
=0;i
<ts
->qcount
;i
++) {
1016 if ( pjob
.status
== LPQ_DELETED
)
1019 if (ts
->queue
[i
].sysjob
== pjob
.sysjob
) {
1021 /* try to clean up any jobs that need to be deleted */
1023 if ( pjob
.status
== LPQ_DELETING
) {
1026 result
= (*(ts
->print_if
->job_delete
))(
1027 ts
->sharename
, ts
->lprm_command
, &pjob
);
1029 if ( result
!= 0 ) {
1030 /* if we can't delete, then reset the job status */
1031 pjob
.status
= LPQ_QUEUED
;
1032 pjob_store(ts
->ev
, ts
->msg_ctx
,
1033 ts
->sharename
, jobid
, &pjob
);
1036 /* if we deleted the job, the remove the tdb record */
1039 ts
->sharename
, jobid
);
1040 pjob
.status
= LPQ_DELETED
;
1050 /* The job isn't in the system queue - we have to assume it has
1051 completed, so delete the database entry. */
1053 if (i
== ts
->qcount
) {
1055 /* A race can occur between the time a job is spooled and
1056 when it appears in the lpq output. This happens when
1057 the job is added to printing.tdb when another smbd
1058 running print_queue_update() has completed a lpq and
1059 is currently traversing the printing tdb and deleting jobs.
1060 Don't delete the job if it was submitted after the lpq_time. */
1062 if (pjob
.starttime
< ts
->lpq_time
) {
1063 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
1064 (unsigned int)jobid
,
1065 (unsigned int)pjob
.starttime
,
1066 (unsigned int)ts
->lpq_time
));
1067 pjob_delete(ts
->ev
, ts
->msg_ctx
,
1068 ts
->sharename
, jobid
);
1074 /* Save the pjob attributes we will store. */
1075 ts
->queue
[i
].sysjob
= pjob
.sysjob
;
1076 ts
->queue
[i
].size
= pjob
.size
;
1077 ts
->queue
[i
].page_count
= pjob
.page_count
;
1078 ts
->queue
[i
].status
= pjob
.status
;
1079 ts
->queue
[i
].priority
= 1;
1080 ts
->queue
[i
].time
= pjob
.starttime
;
1081 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
1082 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
1089 /****************************************************************************
1090 Check if the print queue has been updated recently enough.
1091 ****************************************************************************/
1093 static void print_cache_flush(const char *sharename
)
1096 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1100 slprintf(key
, sizeof(key
)-1, "CACHE/%s", sharename
);
1101 tdb_store_int32(pdb
->tdb
, key
, -1);
1102 release_print_db(pdb
);
1105 /****************************************************************************
1106 Check if someone already thinks they are doing the update.
1107 ****************************************************************************/
1109 static pid_t
get_updating_pid(const char *sharename
)
1114 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1118 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
1119 key
= string_tdb_data(keystr
);
1121 data
= tdb_fetch_compat(pdb
->tdb
, key
);
1122 release_print_db(pdb
);
1123 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
)) {
1124 SAFE_FREE(data
.dptr
);
1128 updating_pid
= IVAL(data
.dptr
, 0);
1129 SAFE_FREE(data
.dptr
);
1131 if (process_exists_by_pid(updating_pid
))
1132 return updating_pid
;
1137 /****************************************************************************
1138 Set the fact that we're doing the update, or have finished doing the update
1140 ****************************************************************************/
1142 static void set_updating_pid(const fstring sharename
, bool updating
)
1147 pid_t updating_pid
= getpid();
1150 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1155 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
1156 key
= string_tdb_data(keystr
);
1158 DEBUG(5, ("set_updating_pid: %supdating lpq cache for print share %s\n",
1159 updating
? "" : "not ",
1163 tdb_delete(pdb
->tdb
, key
);
1164 release_print_db(pdb
);
1168 SIVAL( buffer
, 0, updating_pid
);
1170 data
.dsize
= 4; /* we always assume this is a 4 byte value */
1172 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1173 release_print_db(pdb
);
1176 /****************************************************************************
1177 Sort print jobs by submittal time.
1178 ****************************************************************************/
1180 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1191 /* Sort on job start time */
1193 if (j1
->time
== j2
->time
)
1195 return (j1
->time
> j2
->time
) ? 1 : -1;
1198 /****************************************************************************
1199 Store the sorted queue representation for later portmon retrieval.
1201 ****************************************************************************/
1203 static void store_queue_struct(struct tdb_print_db
*pdb
, struct traverse_struct
*pts
)
1206 int max_reported_jobs
= lp_max_reported_jobs(pts
->snum
);
1207 print_queue_struct
*queue
= pts
->queue
;
1210 unsigned int qcount
;
1212 if (max_reported_jobs
&& (max_reported_jobs
< pts
->qcount
))
1213 pts
->qcount
= max_reported_jobs
;
1216 /* Work out the size. */
1218 data
.dsize
+= tdb_pack(NULL
, 0, "d", qcount
);
1220 for (i
= 0; i
< pts
->qcount
; i
++) {
1221 if ( queue
[i
].status
== LPQ_DELETED
)
1225 data
.dsize
+= tdb_pack(NULL
, 0, "ddddddff",
1226 (uint32
)queue
[i
].sysjob
,
1227 (uint32
)queue
[i
].size
,
1228 (uint32
)queue
[i
].page_count
,
1229 (uint32
)queue
[i
].status
,
1230 (uint32
)queue
[i
].priority
,
1231 (uint32
)queue
[i
].time
,
1236 if ((data
.dptr
= (uint8
*)SMB_MALLOC(data
.dsize
)) == NULL
)
1240 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "d", qcount
);
1241 for (i
= 0; i
< pts
->qcount
; i
++) {
1242 if ( queue
[i
].status
== LPQ_DELETED
)
1245 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
1246 (uint32
)queue
[i
].sysjob
,
1247 (uint32
)queue
[i
].size
,
1248 (uint32
)queue
[i
].page_count
,
1249 (uint32
)queue
[i
].status
,
1250 (uint32
)queue
[i
].priority
,
1251 (uint32
)queue
[i
].time
,
1256 tdb_store(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"), data
,
1258 SAFE_FREE(data
.dptr
);
1262 static TDB_DATA
get_jobs_added_data(struct tdb_print_db
*pdb
)
1268 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_added"));
1269 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0)) {
1270 SAFE_FREE(data
.dptr
);
1277 static void check_job_added(const char *sharename
, TDB_DATA data
, uint32 jobid
)
1280 unsigned int job_count
= data
.dsize
/ 4;
1282 for (i
= 0; i
< job_count
; i
++) {
1285 ch_jobid
= IVAL(data
.dptr
, i
*4);
1286 if (ch_jobid
== jobid
)
1287 remove_from_jobs_added(sharename
, jobid
);
1291 /****************************************************************************
1292 Check if the print queue has been updated recently enough.
1293 ****************************************************************************/
1295 static bool print_cache_expired(const char *sharename
, bool check_pending
)
1298 time_t last_qscan_time
, time_now
= time(NULL
);
1299 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1300 bool result
= False
;
1305 snprintf(key
, sizeof(key
), "CACHE/%s", sharename
);
1306 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1309 * Invalidate the queue for 3 reasons.
1310 * (1). last queue scan time == -1.
1311 * (2). Current time - last queue scan time > allowed cache time.
1312 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1313 * This last test picks up machines for which the clock has been moved
1314 * forward, an lpq scan done and then the clock moved back. Otherwise
1315 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1318 if (last_qscan_time
== ((time_t)-1)
1319 || (time_now
- last_qscan_time
) >= lp_lpqcachetime()
1320 || last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
))
1323 time_t msg_pending_time
;
1325 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1326 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1327 sharename
, (int)last_qscan_time
, (int)time_now
,
1328 (int)lp_lpqcachetime() ));
1330 /* check if another smbd has already sent a message to update the
1331 queue. Give the pending message one minute to clear and
1332 then send another message anyways. Make sure to check for
1333 clocks that have been run forward and then back again. */
1335 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1338 && tdb_fetch_uint32( pdb
->tdb
, key
, &u
)
1339 && (msg_pending_time
=u
) > 0
1340 && msg_pending_time
<= time_now
1341 && (time_now
- msg_pending_time
) < 60 )
1343 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1352 release_print_db(pdb
);
1356 /****************************************************************************
1357 main work for updating the lpq cache for a printer queue
1358 ****************************************************************************/
1360 static void print_queue_update_internal(struct tevent_context
*ev
,
1361 struct messaging_context
*msg_ctx
,
1362 const char *sharename
,
1363 struct printif
*current_printif
,
1364 char *lpq_command
, char *lprm_command
)
1367 print_queue_struct
*queue
= NULL
;
1368 print_status_struct status
;
1369 print_status_struct old_status
;
1370 struct printjob
*pjob
;
1371 struct traverse_struct tstruct
;
1374 fstring keystr
, cachestr
;
1375 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1376 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
1378 if ((pdb
== NULL
) || (tmp_ctx
== NULL
)) {
1382 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1383 sharename
, current_printif
->type
, lpq_command
));
1386 * Update the cache time FIRST ! Stops others even
1387 * attempting to get the lock and doing this
1388 * if the lpq takes a long time.
1391 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", sharename
);
1392 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
1394 /* get the current queue using the appropriate interface */
1395 ZERO_STRUCT(status
);
1397 qcount
= (*(current_printif
->queue_get
))(sharename
,
1398 current_printif
->type
,
1399 lpq_command
, &queue
, &status
);
1401 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1402 qcount
, (qcount
!= 1) ? "s" : "", sharename
));
1404 /* Sort the queue by submission time otherwise they are displayed
1407 TYPESAFE_QSORT(queue
, qcount
, printjob_comp
);
1410 any job in the internal database that is marked as spooled
1411 and doesn't exist in the system queue is considered finished
1412 and removed from the database
1414 any job in the system database but not in the internal database
1415 is added as a unix job
1417 fill in any system job numbers as we go
1419 jcdata
= get_jobs_added_data(pdb
);
1421 for (i
=0; i
<qcount
; i
++) {
1422 uint32 jobid
= sysjob_to_jobid_pdb(pdb
, queue
[i
].sysjob
);
1423 if (jobid
== (uint32
)-1) {
1424 /* assume its a unix print job */
1425 print_unix_job(ev
, msg_ctx
,
1426 sharename
, &queue
[i
], jobid
);
1430 /* we have an active SMB print job - update its status */
1431 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
1433 /* err, somethings wrong. Probably smbd was restarted
1434 with jobs in the queue. All we can do is treat them
1435 like unix jobs. Pity. */
1436 DEBUG(1, ("queued print job %d not found in jobs list, "
1437 "assuming unix job\n", jobid
));
1438 print_unix_job(ev
, msg_ctx
,
1439 sharename
, &queue
[i
], jobid
);
1443 /* don't reset the status on jobs to be deleted */
1445 if ( pjob
->status
!= LPQ_DELETING
)
1446 pjob
->status
= queue
[i
].status
;
1448 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
1450 check_job_added(sharename
, jcdata
, jobid
);
1453 SAFE_FREE(jcdata
.dptr
);
1455 /* now delete any queued entries that don't appear in the
1457 tstruct
.queue
= queue
;
1458 tstruct
.qcount
= qcount
;
1460 tstruct
.total_jobs
= 0;
1461 tstruct
.lpq_time
= time(NULL
);
1462 tstruct
.sharename
= sharename
;
1463 tstruct
.lprm_command
= lprm_command
;
1464 tstruct
.print_if
= current_printif
;
1466 tstruct
.msg_ctx
= msg_ctx
;
1467 tstruct
.mem_ctx
= tmp_ctx
;
1469 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
1471 /* Store the linearised queue, max jobs only. */
1472 store_queue_struct(pdb
, &tstruct
);
1474 SAFE_FREE(tstruct
.queue
);
1475 talloc_free(tmp_ctx
);
1477 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1478 sharename
, tstruct
.total_jobs
));
1480 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
1482 get_queue_status(sharename
, &old_status
);
1483 if (old_status
.qcount
!= qcount
)
1484 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1485 old_status
.qcount
, qcount
, sharename
));
1487 /* store the new queue status structure */
1488 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
1489 key
= string_tdb_data(keystr
);
1491 status
.qcount
= qcount
;
1492 data
.dptr
= (uint8
*)&status
;
1493 data
.dsize
= sizeof(status
);
1494 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1497 * Update the cache time again. We want to do this call
1498 * as little as possible...
1501 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", sharename
);
1502 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
1504 /* clear the msg pending record for this queue */
1506 snprintf(keystr
, sizeof(keystr
), "MSG_PENDING/%s", sharename
);
1508 if ( !tdb_store_uint32( pdb
->tdb
, keystr
, 0 ) ) {
1509 /* log a message but continue on */
1511 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1515 release_print_db( pdb
);
1520 /****************************************************************************
1521 Update the internal database from the system print queue for a queue.
1522 obtain a lock on the print queue before proceeding (needed when mutiple
1523 smbd processes maytry to update the lpq cache concurrently).
1524 ****************************************************************************/
1526 static void print_queue_update_with_lock( struct tevent_context
*ev
,
1527 struct messaging_context
*msg_ctx
,
1528 const char *sharename
,
1529 struct printif
*current_printif
,
1530 char *lpq_command
, char *lprm_command
)
1533 struct tdb_print_db
*pdb
;
1535 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename
));
1536 pdb
= get_print_db_byname(sharename
);
1540 if ( !print_cache_expired(sharename
, False
) ) {
1541 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename
));
1542 release_print_db(pdb
);
1547 * Check to see if someone else is doing this update.
1548 * This is essentially a mutex on the update.
1551 if (get_updating_pid(sharename
) != -1) {
1552 release_print_db(pdb
);
1556 /* Lock the queue for the database update */
1558 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", sharename
);
1559 /* Only wait 10 seconds for this. */
1560 if (tdb_lock_bystring_with_timeout(pdb
->tdb
, keystr
, 10) != 0) {
1561 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename
));
1562 release_print_db(pdb
);
1567 * Ensure that no one else got in here.
1568 * If the updating pid is still -1 then we are
1572 if (get_updating_pid(sharename
) != -1) {
1574 * Someone else is doing the update, exit.
1576 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1577 release_print_db(pdb
);
1582 * We're going to do the update ourselves.
1585 /* Tell others we're doing the update. */
1586 set_updating_pid(sharename
, True
);
1589 * Allow others to enter and notice we're doing
1593 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1595 /* do the main work now */
1597 print_queue_update_internal(ev
, msg_ctx
,
1598 sharename
, current_printif
,
1599 lpq_command
, lprm_command
);
1601 /* Delete our pid from the db. */
1602 set_updating_pid(sharename
, False
);
1603 release_print_db(pdb
);
1606 /****************************************************************************
1607 this is the receive function of the background lpq updater
1608 ****************************************************************************/
1609 void print_queue_receive(struct messaging_context
*msg
,
1612 struct server_id server_id
,
1616 char *lpqcommand
= NULL
, *lprmcommand
= NULL
;
1620 len
= tdb_unpack( (uint8
*)data
->data
, data
->length
, "fdPP",
1627 SAFE_FREE(lpqcommand
);
1628 SAFE_FREE(lprmcommand
);
1629 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1633 print_queue_update_with_lock(server_event_context(), msg
, sharename
,
1634 get_printer_fns_from_type((enum printing_types
)printing_type
),
1635 lpqcommand
, lprmcommand
);
1637 SAFE_FREE(lpqcommand
);
1638 SAFE_FREE(lprmcommand
);
1642 /****************************************************************************
1643 update the internal database from the system print queue for a queue
1644 ****************************************************************************/
1646 extern pid_t background_lpq_updater_pid
;
1648 static void print_queue_update(struct messaging_context
*msg_ctx
,
1649 int snum
, bool force
)
1653 char *lpqcommand
= NULL
;
1654 char *lprmcommand
= NULL
;
1655 uint8
*buffer
= NULL
;
1658 struct tdb_print_db
*pdb
;
1660 struct printif
*current_printif
;
1661 TALLOC_CTX
*ctx
= talloc_tos();
1663 fstrcpy( sharename
, lp_const_servicename(snum
));
1665 /* don't strip out characters like '$' from the printername */
1667 lpqcommand
= talloc_string_sub2(ctx
,
1668 lp_lpqcommand(talloc_tos(), snum
),
1670 lp_printername(talloc_tos(), snum
),
1671 false, false, false);
1675 lpqcommand
= talloc_sub_advanced(ctx
,
1676 lp_servicename(talloc_tos(), snum
),
1677 current_user_info
.unix_name
,
1679 current_user
.ut
.gid
,
1680 get_current_username(),
1681 current_user_info
.domain
,
1687 lprmcommand
= talloc_string_sub2(ctx
,
1688 lp_lprmcommand(talloc_tos(), snum
),
1690 lp_printername(talloc_tos(), snum
),
1691 false, false, false);
1695 lprmcommand
= talloc_sub_advanced(ctx
,
1696 lp_servicename(talloc_tos(), snum
),
1697 current_user_info
.unix_name
,
1699 current_user
.ut
.gid
,
1700 get_current_username(),
1701 current_user_info
.domain
,
1708 * Make sure that the background queue process exists.
1709 * Otherwise just do the update ourselves
1712 if ( force
|| background_lpq_updater_pid
== -1 ) {
1713 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename
));
1714 current_printif
= get_printer_fns( snum
);
1715 print_queue_update_with_lock(server_event_context(), msg_ctx
,
1716 sharename
, current_printif
,
1717 lpqcommand
, lprmcommand
);
1722 type
= lp_printing(snum
);
1724 /* get the length */
1726 len
= tdb_pack( NULL
, 0, "fdPP",
1732 buffer
= SMB_XMALLOC_ARRAY( uint8
, len
);
1734 /* now pack the buffer */
1735 newlen
= tdb_pack( buffer
, len
, "fdPP",
1741 SMB_ASSERT( newlen
== len
);
1743 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1744 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1745 sharename
, type
, lpqcommand
, lprmcommand
));
1747 /* here we set a msg pending record for other smbd processes
1748 to throttle the number of duplicate print_queue_update msgs
1751 pdb
= get_print_db_byname(sharename
);
1757 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1759 if ( !tdb_store_uint32( pdb
->tdb
, key
, time(NULL
) ) ) {
1760 /* log a message but continue on */
1762 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1766 release_print_db( pdb
);
1768 /* finally send the message */
1770 messaging_send_buf(msg_ctx
, pid_to_procid(background_lpq_updater_pid
),
1771 MSG_PRINTER_UPDATE
, (uint8
*)buffer
, len
);
1773 SAFE_FREE( buffer
);
1778 /****************************************************************************
1779 Create/Update an entry in the print tdb that will allow us to send notify
1780 updates only to interested smbd's.
1781 ****************************************************************************/
1783 bool print_notify_register_pid(int snum
)
1786 struct tdb_print_db
*pdb
= NULL
;
1787 TDB_CONTEXT
*tdb
= NULL
;
1788 const char *printername
;
1789 uint32_t mypid
= (uint32_t)getpid();
1793 /* if (snum == -1), then the change notify request was
1794 on a print server handle and we need to register on
1799 int num_services
= lp_numservices();
1802 for ( idx
=0; idx
<num_services
; idx
++ ) {
1803 if (lp_snum_ok(idx
) && lp_print_ok(idx
) )
1804 print_notify_register_pid(idx
);
1809 else /* register for a specific printer */
1811 printername
= lp_const_servicename(snum
);
1812 pdb
= get_print_db_byname(printername
);
1818 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) != 0) {
1819 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1822 release_print_db(pdb
);
1826 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1828 /* Add ourselves and increase the refcount. */
1830 for (i
= 0; i
< data
.dsize
; i
+= 8) {
1831 if (IVAL(data
.dptr
,i
) == mypid
) {
1832 uint32 new_refcount
= IVAL(data
.dptr
, i
+4) + 1;
1833 SIVAL(data
.dptr
, i
+4, new_refcount
);
1838 if (i
== data
.dsize
) {
1839 /* We weren't in the list. Realloc. */
1840 data
.dptr
= (uint8
*)SMB_REALLOC(data
.dptr
, data
.dsize
+ 8);
1842 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1847 SIVAL(data
.dptr
,data
.dsize
- 8,mypid
);
1848 SIVAL(data
.dptr
,data
.dsize
- 4,1); /* Refcount. */
1851 /* Store back the record. */
1852 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) != 0) {
1853 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1854 list for printer %s\n", printername
));
1862 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1864 release_print_db(pdb
);
1865 SAFE_FREE(data
.dptr
);
1869 /****************************************************************************
1870 Update an entry in the print tdb that will allow us to send notify
1871 updates only to interested smbd's.
1872 ****************************************************************************/
1874 bool print_notify_deregister_pid(int snum
)
1877 struct tdb_print_db
*pdb
= NULL
;
1878 TDB_CONTEXT
*tdb
= NULL
;
1879 const char *printername
;
1880 uint32_t mypid
= (uint32_t)getpid();
1884 /* if ( snum == -1 ), we are deregister a print server handle
1885 which means to deregister on all print queues */
1889 int num_services
= lp_numservices();
1892 for ( idx
=0; idx
<num_services
; idx
++ ) {
1893 if ( lp_snum_ok(idx
) && lp_print_ok(idx
) )
1894 print_notify_deregister_pid(idx
);
1899 else /* deregister a specific printer */
1901 printername
= lp_const_servicename(snum
);
1902 pdb
= get_print_db_byname(printername
);
1908 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) != 0) {
1909 DEBUG(0,("print_notify_register_pid: Failed to lock \
1910 printer %s database\n", printername
));
1912 release_print_db(pdb
);
1916 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1918 /* Reduce refcount. Remove ourselves if zero. */
1920 for (i
= 0; i
< data
.dsize
; ) {
1921 if (IVAL(data
.dptr
,i
) == mypid
) {
1922 uint32 refcount
= IVAL(data
.dptr
, i
+4);
1926 if (refcount
== 0) {
1927 if (data
.dsize
- i
> 8)
1928 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);
1932 SIVAL(data
.dptr
, i
+4, refcount
);
1938 if (data
.dsize
== 0)
1939 SAFE_FREE(data
.dptr
);
1941 /* Store back the record. */
1942 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) != 0) {
1943 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1944 list for printer %s\n", printername
));
1952 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1954 release_print_db(pdb
);
1955 SAFE_FREE(data
.dptr
);
1959 /****************************************************************************
1960 Check if a jobid is valid. It is valid if it exists in the database.
1961 ****************************************************************************/
1963 bool print_job_exists(const char* sharename
, uint32 jobid
)
1965 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1971 ret
= tdb_exists(pdb
->tdb
, print_key(jobid
, &tmp
));
1972 release_print_db(pdb
);
1976 /****************************************************************************
1977 Return the device mode asigned to a specific print job.
1978 Only valid for the process doing the spooling and when the job
1979 has not been spooled.
1980 ****************************************************************************/
1982 struct spoolss_DeviceMode
*print_job_devmode(TALLOC_CTX
*mem_ctx
,
1983 const char *sharename
,
1986 struct printjob
*pjob
= print_job_find(mem_ctx
, sharename
, jobid
);
1991 return pjob
->devmode
;
1994 /****************************************************************************
1995 Set the name of a job. Only possible for owner.
1996 ****************************************************************************/
1998 bool print_job_set_name(struct tevent_context
*ev
,
1999 struct messaging_context
*msg_ctx
,
2000 const char *sharename
, uint32 jobid
, const char *name
)
2002 struct printjob
*pjob
;
2004 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
2005 if (tmp_ctx
== NULL
) {
2009 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2010 if (!pjob
|| pjob
->pid
!= getpid()) {
2015 fstrcpy(pjob
->jobname
, name
);
2016 ret
= pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2018 talloc_free(tmp_ctx
);
2022 /****************************************************************************
2023 Get the name of a job. Only possible for owner.
2024 ****************************************************************************/
2026 bool print_job_get_name(TALLOC_CTX
*mem_ctx
, const char *sharename
, uint32_t jobid
, char **name
)
2028 struct printjob
*pjob
;
2030 pjob
= print_job_find(mem_ctx
, sharename
, jobid
);
2031 if (!pjob
|| pjob
->pid
!= getpid()) {
2035 *name
= pjob
->jobname
;
2040 /***************************************************************************
2041 Remove a jobid from the 'jobs added' list.
2042 ***************************************************************************/
2044 static bool remove_from_jobs_added(const char* sharename
, uint32 jobid
)
2046 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2048 size_t job_count
, i
;
2050 bool gotlock
= False
;
2058 key
= string_tdb_data("INFO/jobs_added");
2060 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) != 0)
2065 data
= tdb_fetch_compat(pdb
->tdb
, key
);
2067 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
2070 job_count
= data
.dsize
/ 4;
2071 for (i
= 0; i
< job_count
; i
++) {
2074 ch_jobid
= IVAL(data
.dptr
, i
*4);
2075 if (ch_jobid
== jobid
) {
2076 if (i
< job_count
-1 )
2077 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
2079 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) != 0)
2089 tdb_chainunlock(pdb
->tdb
, key
);
2090 SAFE_FREE(data
.dptr
);
2091 release_print_db(pdb
);
2093 DEBUG(10,("remove_from_jobs_added: removed jobid %u\n", (unsigned int)jobid
));
2095 DEBUG(10,("remove_from_jobs_added: Failed to remove jobid %u\n", (unsigned int)jobid
));
2099 /****************************************************************************
2100 Delete a print job - don't update queue.
2101 ****************************************************************************/
2103 static bool print_job_delete1(struct tevent_context
*ev
,
2104 struct messaging_context
*msg_ctx
,
2105 int snum
, uint32 jobid
)
2107 const char* sharename
= lp_const_servicename(snum
);
2108 struct printjob
*pjob
;
2110 struct printif
*current_printif
= get_printer_fns( snum
);
2112 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
2113 if (tmp_ctx
== NULL
) {
2117 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2124 * If already deleting just return.
2127 if (pjob
->status
== LPQ_DELETING
) {
2132 /* Hrm - we need to be able to cope with deleting a job before it
2133 has reached the spooler. Just mark it as LPQ_DELETING and
2134 let the print_queue_update() code rmeove the record */
2137 if (pjob
->sysjob
== -1) {
2138 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
2141 /* Set the tdb entry to be deleting. */
2143 pjob
->status
= LPQ_DELETING
;
2144 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2146 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
2148 result
= (*(current_printif
->job_delete
))(
2149 lp_printername(talloc_tos(), snum
),
2150 lp_lprmcommand(talloc_tos(), snum
),
2153 /* Delete the tdb entry if the delete succeeded or the job hasn't
2157 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2164 pjob_delete(ev
, msg_ctx
, sharename
, jobid
);
2165 /* Ensure we keep a rough count of the number of total jobs... */
2166 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, -1);
2167 release_print_db(pdb
);
2171 remove_from_jobs_added( sharename
, jobid
);
2173 ret
= (result
== 0);
2175 talloc_free(tmp_ctx
);
2179 /****************************************************************************
2180 Return true if the current user owns the print job.
2181 ****************************************************************************/
2183 static bool is_owner(const struct auth_session_info
*server_info
,
2184 const char *servicename
,
2187 struct printjob
*pjob
;
2189 TALLOC_CTX
*tmp_ctx
= talloc_new(server_info
);
2190 if (tmp_ctx
== NULL
) {
2194 pjob
= print_job_find(tmp_ctx
, servicename
, jobid
);
2195 if (!pjob
|| !server_info
) {
2200 ret
= strequal(pjob
->user
, server_info
->unix_info
->sanitized_username
);
2202 talloc_free(tmp_ctx
);
2206 /****************************************************************************
2208 ****************************************************************************/
2210 WERROR
print_job_delete(const struct auth_session_info
*server_info
,
2211 struct messaging_context
*msg_ctx
,
2212 int snum
, uint32_t jobid
)
2214 const char* sharename
= lp_const_servicename(snum
);
2215 struct printjob
*pjob
;
2218 TALLOC_CTX
*tmp_ctx
= talloc_new(msg_ctx
);
2219 if (tmp_ctx
== NULL
) {
2220 return WERR_NOT_ENOUGH_MEMORY
;
2223 owner
= is_owner(server_info
, lp_const_servicename(snum
), jobid
);
2225 /* Check access against security descriptor or whether the user
2229 !W_ERROR_IS_OK(print_access_check(server_info
, msg_ctx
, snum
,
2230 JOB_ACCESS_ADMINISTER
))) {
2231 DEBUG(0, ("print job delete denied."
2232 "User name: %s, Printer name: %s.",
2233 uidtoname(server_info
->unix_token
->uid
),
2234 lp_printername(tmp_ctx
, snum
)));
2236 werr
= WERR_ACCESS_DENIED
;
2241 * get the spooled filename of the print job
2242 * if this works, then the file has not been spooled
2243 * to the underlying print system. Just delete the
2244 * spool file & return.
2247 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2248 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= getpid()) {
2249 DEBUG(10, ("Skipping spool file removal for job %u\n", jobid
));
2251 DEBUG(10, ("Removing spool file [%s]\n", pjob
->filename
));
2252 if (unlink(pjob
->filename
) == -1) {
2253 werr
= map_werror_from_unix(errno
);
2258 if (!print_job_delete1(server_event_context(), msg_ctx
, snum
, jobid
)) {
2259 werr
= WERR_ACCESS_DENIED
;
2263 /* force update the database and say the delete failed if the
2266 print_queue_update(msg_ctx
, snum
, True
);
2268 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2269 if (pjob
&& (pjob
->status
!= LPQ_DELETING
)) {
2270 werr
= WERR_ACCESS_DENIED
;
2273 werr
= WERR_PRINTER_HAS_JOBS_QUEUED
;
2276 talloc_free(tmp_ctx
);
2280 /****************************************************************************
2282 ****************************************************************************/
2284 WERROR
print_job_pause(const struct auth_session_info
*server_info
,
2285 struct messaging_context
*msg_ctx
,
2286 int snum
, uint32 jobid
)
2288 const char* sharename
= lp_const_servicename(snum
);
2289 struct printjob
*pjob
;
2291 struct printif
*current_printif
= get_printer_fns( snum
);
2293 TALLOC_CTX
*tmp_ctx
= talloc_new(msg_ctx
);
2294 if (tmp_ctx
== NULL
) {
2295 return WERR_NOT_ENOUGH_MEMORY
;
2298 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2299 if (!pjob
|| !server_info
) {
2300 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2301 (unsigned int)jobid
));
2302 werr
= WERR_INVALID_PARAM
;
2306 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2307 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2308 (int)pjob
->sysjob
, (unsigned int)jobid
));
2309 werr
= WERR_INVALID_PARAM
;
2313 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2314 !W_ERROR_IS_OK(print_access_check(server_info
, msg_ctx
, snum
,
2315 JOB_ACCESS_ADMINISTER
))) {
2316 DEBUG(0, ("print job pause denied."
2317 "User name: %s, Printer name: %s.",
2318 uidtoname(server_info
->unix_token
->uid
),
2319 lp_printername(tmp_ctx
, snum
)));
2321 werr
= WERR_ACCESS_DENIED
;
2325 /* need to pause the spooled entry */
2326 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
2329 werr
= WERR_INVALID_PARAM
;
2333 /* force update the database */
2334 print_cache_flush(lp_const_servicename(snum
));
2336 /* Send a printer notify message */
2338 notify_job_status(server_event_context(), msg_ctx
, sharename
, jobid
,
2341 /* how do we tell if this succeeded? */
2344 talloc_free(tmp_ctx
);
2348 /****************************************************************************
2350 ****************************************************************************/
2352 WERROR
print_job_resume(const struct auth_session_info
*server_info
,
2353 struct messaging_context
*msg_ctx
,
2354 int snum
, uint32 jobid
)
2356 const char *sharename
= lp_const_servicename(snum
);
2357 struct printjob
*pjob
;
2359 struct printif
*current_printif
= get_printer_fns( snum
);
2361 TALLOC_CTX
*tmp_ctx
= talloc_new(msg_ctx
);
2362 if (tmp_ctx
== NULL
)
2363 return WERR_NOT_ENOUGH_MEMORY
;
2365 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2366 if (!pjob
|| !server_info
) {
2367 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2368 (unsigned int)jobid
));
2369 werr
= WERR_INVALID_PARAM
;
2373 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2374 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2375 (int)pjob
->sysjob
, (unsigned int)jobid
));
2376 werr
= WERR_INVALID_PARAM
;
2380 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2381 !W_ERROR_IS_OK(print_access_check(server_info
, msg_ctx
, snum
,
2382 JOB_ACCESS_ADMINISTER
))) {
2383 DEBUG(0, ("print job resume denied."
2384 "User name: %s, Printer name: %s.",
2385 uidtoname(server_info
->unix_token
->uid
),
2386 lp_printername(tmp_ctx
, snum
)));
2388 werr
= WERR_ACCESS_DENIED
;
2392 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
2395 werr
= WERR_INVALID_PARAM
;
2399 /* force update the database */
2400 print_cache_flush(lp_const_servicename(snum
));
2402 /* Send a printer notify message */
2404 notify_job_status(server_event_context(), msg_ctx
, sharename
, jobid
,
2409 talloc_free(tmp_ctx
);
2413 /****************************************************************************
2414 Write to a print file.
2415 ****************************************************************************/
2417 ssize_t
print_job_write(struct tevent_context
*ev
,
2418 struct messaging_context
*msg_ctx
,
2419 int snum
, uint32 jobid
, const char *buf
, size_t size
)
2421 const char* sharename
= lp_const_servicename(snum
);
2422 ssize_t return_code
;
2423 struct printjob
*pjob
;
2424 TALLOC_CTX
*tmp_ctx
= talloc_new(ev
);
2425 if (tmp_ctx
== NULL
) {
2429 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2435 /* don't allow another process to get this info - it is meaningless */
2436 if (pjob
->pid
!= getpid()) {
2441 /* if SMBD is spooling this can't be allowed */
2442 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2447 return_code
= write_data(pjob
->fd
, buf
, size
);
2448 if (return_code
> 0) {
2450 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2453 talloc_free(tmp_ctx
);
2457 /****************************************************************************
2458 Get the queue status - do not update if db is out of date.
2459 ****************************************************************************/
2461 static int get_queue_status(const char* sharename
, print_status_struct
*status
)
2465 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2469 ZERO_STRUCTP(status
);
2476 fstr_sprintf(keystr
, "STATUS/%s", sharename
);
2477 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data(keystr
));
2479 if (data
.dsize
== sizeof(print_status_struct
))
2480 /* this memcpy is ok since the status struct was
2481 not packed before storing it in the tdb */
2482 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
2483 SAFE_FREE(data
.dptr
);
2486 len
= tdb_fetch_int32(pdb
->tdb
, "INFO/total_jobs");
2487 release_print_db(pdb
);
2488 return (len
== -1 ? 0 : len
);
2491 /****************************************************************************
2492 Determine the number of jobs in a queue.
2493 ****************************************************************************/
2495 int print_queue_length(struct messaging_context
*msg_ctx
, int snum
,
2496 print_status_struct
*pstatus
)
2498 const char* sharename
= lp_const_servicename( snum
);
2499 print_status_struct status
;
2502 ZERO_STRUCT( status
);
2504 /* make sure the database is up to date */
2505 if (print_cache_expired(lp_const_servicename(snum
), True
))
2506 print_queue_update(msg_ctx
, snum
, False
);
2508 /* also fetch the queue status */
2509 memset(&status
, 0, sizeof(status
));
2510 len
= get_queue_status(sharename
, &status
);
2518 /***************************************************************************
2519 Allocate a jobid. Hold the lock for as short a time as possible.
2520 ***************************************************************************/
2522 static WERROR
allocate_print_jobid(struct tdb_print_db
*pdb
, int snum
,
2523 const char *sharename
, uint32
*pjobid
)
2527 enum TDB_ERROR terr
;
2530 *pjobid
= (uint32
)-1;
2532 for (i
= 0; i
< 3; i
++) {
2533 /* Lock the database - only wait 20 seconds. */
2534 ret
= tdb_lock_bystring_with_timeout(pdb
->tdb
,
2535 "INFO/nextjob", 20);
2537 DEBUG(0, ("allocate_print_jobid: "
2538 "Failed to lock printing database %s\n",
2540 terr
= tdb_error(pdb
->tdb
);
2541 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2544 if (!tdb_fetch_uint32(pdb
->tdb
, "INFO/nextjob", &jobid
)) {
2545 terr
= tdb_error(pdb
->tdb
);
2546 if (terr
!= TDB_ERR_NOEXIST
) {
2547 DEBUG(0, ("allocate_print_jobid: "
2548 "Failed to fetch INFO/nextjob "
2549 "for print queue %s\n", sharename
));
2550 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2551 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2553 DEBUG(10, ("allocate_print_jobid: "
2554 "No existing jobid in %s\n", sharename
));
2558 DEBUG(10, ("allocate_print_jobid: "
2559 "Read jobid %u from %s\n", jobid
, sharename
));
2561 jobid
= NEXT_JOBID(jobid
);
2563 ret
= tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
);
2565 terr
= tdb_error(pdb
->tdb
);
2566 DEBUG(3, ("allocate_print_jobid: "
2567 "Failed to store INFO/nextjob.\n"));
2568 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2569 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2572 /* We've finished with the INFO/nextjob lock. */
2573 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2575 if (!print_job_exists(sharename
, jobid
)) {
2578 DEBUG(10, ("allocate_print_jobid: "
2579 "Found jobid %u in %s\n", jobid
, sharename
));
2583 DEBUG(0, ("allocate_print_jobid: "
2584 "Failed to allocate a print job for queue %s\n",
2586 /* Probably full... */
2587 return WERR_NO_SPOOL_SPACE
;
2590 /* Store a dummy placeholder. */
2596 if (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), dum
,
2598 DEBUG(3, ("allocate_print_jobid: "
2599 "jobid (%d) failed to store placeholder.\n",
2601 terr
= tdb_error(pdb
->tdb
);
2602 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2610 /***************************************************************************
2611 Append a jobid to the 'jobs added' list.
2612 ***************************************************************************/
2614 static bool add_to_jobs_added(struct tdb_print_db
*pdb
, uint32 jobid
)
2619 SIVAL(&store_jobid
, 0, jobid
);
2620 data
.dptr
= (uint8
*)&store_jobid
;
2623 DEBUG(10,("add_to_jobs_added: Added jobid %u\n", (unsigned int)jobid
));
2625 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_added"),
2630 /***************************************************************************
2631 Do all checks needed to determine if we can start a job.
2632 ***************************************************************************/
2634 static WERROR
print_job_checks(const struct auth_session_info
*server_info
,
2635 struct messaging_context
*msg_ctx
,
2636 int snum
, int *njobs
)
2638 const char *sharename
= lp_const_servicename(snum
);
2639 uint64_t dspace
, dsize
;
2643 if (!W_ERROR_IS_OK(print_access_check(server_info
, msg_ctx
, snum
,
2644 PRINTER_ACCESS_USE
))) {
2645 DEBUG(3, ("print_job_checks: "
2646 "job start denied by security descriptor\n"));
2647 return WERR_ACCESS_DENIED
;
2650 if (!print_time_access_check(server_info
, msg_ctx
, sharename
)) {
2651 DEBUG(3, ("print_job_checks: "
2652 "job start denied by time check\n"));
2653 return WERR_ACCESS_DENIED
;
2656 /* see if we have sufficient disk space */
2657 if (lp_minprintspace(snum
)) {
2658 minspace
= lp_minprintspace(snum
);
2659 ret
= sys_fsusage(lp_path(talloc_tos(), snum
), &dspace
, &dsize
);
2660 if (ret
== 0 && dspace
< 2*minspace
) {
2661 DEBUG(3, ("print_job_checks: "
2662 "disk space check failed.\n"));
2663 return WERR_NO_SPOOL_SPACE
;
2667 /* for autoloaded printers, check that the printcap entry still exists */
2668 if (lp_autoloaded(snum
) && !pcap_printername_ok(sharename
)) {
2669 DEBUG(3, ("print_job_checks: printer name %s check failed.\n",
2671 return WERR_ACCESS_DENIED
;
2674 /* Insure the maximum queue size is not violated */
2675 *njobs
= print_queue_length(msg_ctx
, snum
, NULL
);
2676 if (*njobs
> lp_maxprintjobs(snum
)) {
2677 DEBUG(3, ("print_job_checks: Queue %s number of jobs (%d) "
2678 "larger than max printjobs per queue (%d).\n",
2679 sharename
, *njobs
, lp_maxprintjobs(snum
)));
2680 return WERR_NO_SPOOL_SPACE
;
2686 /***************************************************************************
2688 ***************************************************************************/
2690 static WERROR
print_job_spool_file(int snum
, uint32_t jobid
,
2691 const char *output_file
,
2692 struct printjob
*pjob
)
2700 /* if this file is within the printer path, it means that smbd
2701 * is spooling it and will pass us control when it is finished.
2702 * Verify that the file name is ok, within path, and it is
2703 * already already there */
2705 path
= lp_path(talloc_tos(), snum
);
2707 if (strncmp(output_file
, path
, len
) == 0 &&
2708 (output_file
[len
- 1] == '/' || output_file
[len
] == '/')) {
2710 /* verify path is not too long */
2711 if (strlen(output_file
) >= sizeof(pjob
->filename
)) {
2712 return WERR_INVALID_NAME
;
2715 /* verify that the file exists */
2716 if (sys_stat(output_file
, &st
, false) != 0) {
2717 return WERR_INVALID_NAME
;
2720 fstrcpy(pjob
->filename
, output_file
);
2722 DEBUG(3, ("print_job_spool_file:"
2723 "External spooling activated\n"));
2725 /* we do not open the file until spooling is done */
2727 pjob
->status
= PJOB_SMBD_SPOOLING
;
2733 slprintf(pjob
->filename
, sizeof(pjob
->filename
)-1,
2734 "%s/%sXXXXXX", lp_path(talloc_tos(), snum
),
2735 PRINT_SPOOL_PREFIX
);
2736 mask
= umask(S_IRWXO
| S_IRWXG
);
2737 pjob
->fd
= mkstemp(pjob
->filename
);
2740 if (pjob
->fd
== -1) {
2741 werr
= map_werror_from_unix(errno
);
2742 if (W_ERROR_EQUAL(werr
, WERR_ACCESS_DENIED
)) {
2743 /* Common setup error, force a report. */
2744 DEBUG(0, ("print_job_spool_file: "
2745 "insufficient permissions to open spool "
2746 "file %s.\n", pjob
->filename
));
2748 /* Normal case, report at level 3 and above. */
2749 DEBUG(3, ("print_job_spool_file: "
2750 "can't open spool file %s\n",
2759 /***************************************************************************
2760 Start spooling a job - return the jobid.
2761 ***************************************************************************/
2763 WERROR
print_job_start(const struct auth_session_info
*server_info
,
2764 struct messaging_context
*msg_ctx
,
2765 const char *clientmachine
,
2766 int snum
, const char *docname
, const char *filename
,
2767 struct spoolss_DeviceMode
*devmode
, uint32_t *_jobid
)
2771 struct printjob pjob
;
2772 const char *sharename
= lp_const_servicename(snum
);
2773 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2778 return WERR_INTERNAL_DB_CORRUPTION
;
2781 path
= lp_path(talloc_tos(), snum
);
2783 werr
= print_job_checks(server_info
, msg_ctx
, snum
, &njobs
);
2784 if (!W_ERROR_IS_OK(werr
)) {
2785 release_print_db(pdb
);
2789 DEBUG(10, ("print_job_start: "
2790 "Queue %s number of jobs (%d), max printjobs = %d\n",
2791 sharename
, njobs
, lp_maxprintjobs(snum
)));
2793 werr
= allocate_print_jobid(pdb
, snum
, sharename
, &jobid
);
2794 if (!W_ERROR_IS_OK(werr
)) {
2798 /* create the database entry */
2802 pjob
.pid
= getpid();
2806 pjob
.starttime
= time(NULL
);
2807 pjob
.status
= LPQ_SPOOLING
;
2809 pjob
.spooled
= False
;
2811 pjob
.devmode
= devmode
;
2813 fstrcpy(pjob
.jobname
, docname
);
2815 fstrcpy(pjob
.clientmachine
, clientmachine
);
2817 fstrcpy(pjob
.user
, lp_printjob_username(snum
));
2818 standard_sub_advanced(sharename
, server_info
->unix_info
->sanitized_username
,
2819 path
, server_info
->unix_token
->gid
,
2820 server_info
->unix_info
->sanitized_username
,
2821 server_info
->info
->domain_name
,
2822 pjob
.user
, sizeof(pjob
.user
));
2824 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
2826 /* we have a job entry - now create the spool file */
2827 werr
= print_job_spool_file(snum
, jobid
, filename
, &pjob
);
2828 if (!W_ERROR_IS_OK(werr
)) {
2832 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, &pjob
);
2834 /* Update the 'jobs added' entry used by print_queue_status. */
2835 add_to_jobs_added(pdb
, jobid
);
2837 /* Ensure we keep a rough count of the number of total jobs... */
2838 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, 1);
2840 release_print_db(pdb
);
2847 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2850 release_print_db(pdb
);
2852 DEBUG(3, ("print_job_start: returning fail. "
2853 "Error = %s\n", win_errstr(werr
)));
2857 /****************************************************************************
2858 Update the number of pages spooled to jobid
2859 ****************************************************************************/
2861 void print_job_endpage(struct messaging_context
*msg_ctx
,
2862 int snum
, uint32 jobid
)
2864 const char* sharename
= lp_const_servicename(snum
);
2865 struct printjob
*pjob
;
2866 TALLOC_CTX
*tmp_ctx
= talloc_new(msg_ctx
);
2867 if (tmp_ctx
== NULL
) {
2871 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2875 /* don't allow another process to get this info - it is meaningless */
2876 if (pjob
->pid
!= getpid()) {
2881 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, pjob
);
2883 talloc_free(tmp_ctx
);
2886 /****************************************************************************
2887 Print a file - called on closing the file. This spools the job.
2888 If normal close is false then we're tearing down the jobs - treat as an
2890 ****************************************************************************/
2892 NTSTATUS
print_job_end(struct messaging_context
*msg_ctx
, int snum
,
2893 uint32 jobid
, enum file_close_type close_type
)
2895 const char* sharename
= lp_const_servicename(snum
);
2896 struct printjob
*pjob
;
2898 SMB_STRUCT_STAT sbuf
;
2899 struct printif
*current_printif
= get_printer_fns(snum
);
2900 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2902 TALLOC_CTX
*tmp_ctx
= talloc_new(msg_ctx
);
2903 if (tmp_ctx
== NULL
) {
2904 return NT_STATUS_NO_MEMORY
;
2907 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
2909 status
= NT_STATUS_PRINT_CANCELLED
;
2913 if (pjob
->spooled
|| pjob
->pid
!= getpid()) {
2914 status
= NT_STATUS_ACCESS_DENIED
;
2918 if (close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) {
2919 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2920 /* take over the file now, smbd is done */
2921 if (sys_stat(pjob
->filename
, &sbuf
, false) != 0) {
2922 status
= map_nt_error_from_unix(errno
);
2923 DEBUG(3, ("print_job_end: "
2924 "stat file failed for jobid %d\n",
2929 pjob
->status
= LPQ_SPOOLING
;
2933 if ((sys_fstat(pjob
->fd
, &sbuf
, false) != 0)) {
2934 status
= map_nt_error_from_unix(errno
);
2936 DEBUG(3, ("print_job_end: "
2937 "stat file failed for jobid %d\n",
2945 pjob
->size
= sbuf
.st_ex_size
;
2949 * Not a normal close, something has gone wrong. Cleanup.
2951 if (pjob
->fd
!= -1) {
2957 /* Technically, this is not quite right. If the printer has a separator
2958 * page turned on, the NT spooler prints the separator page even if the
2959 * print job is 0 bytes. 010215 JRR */
2960 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
2961 /* don't bother spooling empty files or something being deleted. */
2962 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2963 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
2964 unlink(pjob
->filename
);
2965 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2966 return NT_STATUS_OK
;
2969 /* don't strip out characters like '$' from the printername */
2970 lpq_cmd
= talloc_string_sub2(tmp_ctx
,
2971 lp_lpqcommand(talloc_tos(), snum
),
2973 lp_printername(talloc_tos(), snum
),
2974 false, false, false);
2975 if (lpq_cmd
== NULL
) {
2976 status
= NT_STATUS_PRINT_CANCELLED
;
2979 lpq_cmd
= talloc_sub_advanced(tmp_ctx
,
2980 lp_servicename(talloc_tos(), snum
),
2981 current_user_info
.unix_name
,
2983 current_user
.ut
.gid
,
2984 get_current_username(),
2985 current_user_info
.domain
,
2987 if (lpq_cmd
== NULL
) {
2988 status
= NT_STATUS_PRINT_CANCELLED
;
2992 ret
= (*(current_printif
->job_submit
))(snum
, pjob
,
2993 current_printif
->type
, lpq_cmd
);
2995 status
= NT_STATUS_PRINT_CANCELLED
;
2999 /* The print job has been successfully handed over to the back-end */
3001 pjob
->spooled
= True
;
3002 pjob
->status
= LPQ_QUEUED
;
3003 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, pjob
);
3005 /* make sure the database is up to date */
3006 if (print_cache_expired(lp_const_servicename(snum
), True
))
3007 print_queue_update(msg_ctx
, snum
, False
);
3009 return NT_STATUS_OK
;
3013 /* The print job was not successfully started. Cleanup */
3014 /* Still need to add proper error return propagation! 010122:JRR */
3016 unlink(pjob
->filename
);
3017 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
3019 talloc_free(tmp_ctx
);
3023 /****************************************************************************
3024 Get a snapshot of jobs in the system without traversing.
3025 ****************************************************************************/
3027 static bool get_stored_queue_info(struct messaging_context
*msg_ctx
,
3028 struct tdb_print_db
*pdb
, int snum
,
3029 int *pcount
, print_queue_struct
**ppqueue
)
3031 TDB_DATA data
, cgdata
, jcdata
;
3032 print_queue_struct
*queue
= NULL
;
3034 uint32 extra_count
= 0;
3035 uint32_t changed_count
= 0;
3036 int total_count
= 0;
3039 int max_reported_jobs
= lp_max_reported_jobs(snum
);
3041 const char* sharename
= lp_servicename(talloc_tos(), snum
);
3042 TALLOC_CTX
*tmp_ctx
= talloc_new(msg_ctx
);
3043 if (tmp_ctx
== NULL
) {
3047 /* make sure the database is up to date */
3048 if (print_cache_expired(lp_const_servicename(snum
), True
))
3049 print_queue_update(msg_ctx
, snum
, False
);
3055 ZERO_STRUCT(cgdata
);
3057 /* Get the stored queue data. */
3058 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"));
3060 if (data
.dptr
&& data
.dsize
>= sizeof(qcount
))
3061 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "d", &qcount
);
3063 /* Get the added jobs list. */
3064 cgdata
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_added"));
3065 if (cgdata
.dptr
!= NULL
&& (cgdata
.dsize
% 4 == 0))
3066 extra_count
= cgdata
.dsize
/4;
3068 /* Get the changed jobs list. */
3069 jcdata
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
3070 if (jcdata
.dptr
!= NULL
&& (jcdata
.dsize
% 4 == 0))
3071 changed_count
= jcdata
.dsize
/ 4;
3073 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount
, (unsigned int)extra_count
));
3075 /* Allocate the queue size. */
3076 if (qcount
== 0 && extra_count
== 0)
3079 if ((queue
= SMB_MALLOC_ARRAY(print_queue_struct
, qcount
+ extra_count
)) == NULL
)
3082 /* Retrieve the linearised queue data. */
3084 for(i
= 0; i
< qcount
; i
++) {
3085 uint32 qjob
, qsize
, qpage_count
, qstatus
, qpriority
, qtime
;
3086 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
3095 queue
[i
].sysjob
= qjob
;
3096 queue
[i
].size
= qsize
;
3097 queue
[i
].page_count
= qpage_count
;
3098 queue
[i
].status
= qstatus
;
3099 queue
[i
].priority
= qpriority
;
3100 queue
[i
].time
= qtime
;
3103 total_count
= qcount
;
3105 /* Add new jobids to the queue. */
3106 for (i
= 0; i
< extra_count
; i
++) {
3108 struct printjob
*pjob
;
3110 jobid
= IVAL(cgdata
.dptr
, i
*4);
3111 DEBUG(5,("get_stored_queue_info: added job = %u\n", (unsigned int)jobid
));
3112 pjob
= print_job_find(tmp_ctx
, lp_const_servicename(snum
), jobid
);
3114 DEBUG(5,("get_stored_queue_info: failed to find added job = %u\n", (unsigned int)jobid
));
3115 remove_from_jobs_added(sharename
, jobid
);
3119 queue
[total_count
].sysjob
= pjob
->sysjob
;
3120 queue
[total_count
].size
= pjob
->size
;
3121 queue
[total_count
].page_count
= pjob
->page_count
;
3122 queue
[total_count
].status
= pjob
->status
;
3123 queue
[total_count
].priority
= 1;
3124 queue
[total_count
].time
= pjob
->starttime
;
3125 fstrcpy(queue
[total_count
].fs_user
, pjob
->user
);
3126 fstrcpy(queue
[total_count
].fs_file
, pjob
->jobname
);
3131 /* Update the changed jobids. */
3132 for (i
= 0; i
< changed_count
; i
++) {
3133 uint32_t jobid
= IVAL(jcdata
.dptr
, i
* 4);
3134 struct printjob
*pjob
;
3138 pjob
= print_job_find(tmp_ctx
, sharename
, jobid
);
3140 DEBUG(5,("get_stored_queue_info: failed to find "
3141 "changed job = %u\n",
3142 (unsigned int)jobid
));
3143 remove_from_jobs_changed(sharename
, jobid
);
3147 for (j
= 0; j
< total_count
; j
++) {
3148 if (queue
[j
].sysjob
== pjob
->sysjob
) {
3155 DEBUG(5,("get_stored_queue_info: changed job: %u\n",
3156 (unsigned int)jobid
));
3158 queue
[j
].sysjob
= pjob
->sysjob
;
3159 queue
[j
].size
= pjob
->size
;
3160 queue
[j
].page_count
= pjob
->page_count
;
3161 queue
[j
].status
= pjob
->status
;
3162 queue
[j
].priority
= 1;
3163 queue
[j
].time
= pjob
->starttime
;
3164 fstrcpy(queue
[j
].fs_user
, pjob
->user
);
3165 fstrcpy(queue
[j
].fs_file
, pjob
->jobname
);
3168 DEBUG(5,("updated queue[%u], jobid: %u, sysjob: %u, "
3170 (unsigned int)j
, (unsigned int)jobid
,
3171 (unsigned int)queue
[j
].sysjob
, pjob
->jobname
));
3174 remove_from_jobs_changed(sharename
, jobid
);
3177 /* Sort the queue by submission time otherwise they are displayed
3180 TYPESAFE_QSORT(queue
, total_count
, printjob_comp
);
3182 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count
));
3184 if (max_reported_jobs
&& total_count
> max_reported_jobs
)
3185 total_count
= max_reported_jobs
;
3188 *pcount
= total_count
;
3194 SAFE_FREE(data
.dptr
);
3195 SAFE_FREE(cgdata
.dptr
);
3196 talloc_free(tmp_ctx
);
3200 /****************************************************************************
3201 Get a printer queue listing.
3202 set queue = NULL and status = NULL if you just want to update the cache
3203 ****************************************************************************/
3205 int print_queue_status(struct messaging_context
*msg_ctx
, int snum
,
3206 print_queue_struct
**ppqueue
,
3207 print_status_struct
*status
)
3211 const char *sharename
;
3212 struct tdb_print_db
*pdb
;
3215 /* make sure the database is up to date */
3217 if (print_cache_expired(lp_const_servicename(snum
), True
))
3218 print_queue_update(msg_ctx
, snum
, False
);
3220 /* return if we are done */
3221 if ( !ppqueue
|| !status
)
3225 sharename
= lp_const_servicename(snum
);
3226 pdb
= get_print_db_byname(sharename
);
3232 * Fetch the queue status. We must do this first, as there may
3233 * be no jobs in the queue.
3236 ZERO_STRUCTP(status
);
3237 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
3238 key
= string_tdb_data(keystr
);
3240 data
= tdb_fetch_compat(pdb
->tdb
, key
);
3242 if (data
.dsize
== sizeof(*status
)) {
3243 /* this memcpy is ok since the status struct was
3244 not packed before storing it in the tdb */
3245 memcpy(status
, data
.dptr
, sizeof(*status
));
3247 SAFE_FREE(data
.dptr
);
3251 * Now, fetch the print queue information. We first count the number
3252 * of entries, and then only retrieve the queue if necessary.
3255 if (!get_stored_queue_info(msg_ctx
, pdb
, snum
, &count
, ppqueue
)) {
3256 release_print_db(pdb
);
3260 release_print_db(pdb
);
3264 /****************************************************************************
3266 ****************************************************************************/
3268 WERROR
print_queue_pause(const struct auth_session_info
*server_info
,
3269 struct messaging_context
*msg_ctx
, int snum
)
3272 struct printif
*current_printif
= get_printer_fns( snum
);
3274 if (!W_ERROR_IS_OK(print_access_check(server_info
, msg_ctx
, snum
,
3275 PRINTER_ACCESS_ADMINISTER
))) {
3276 return WERR_ACCESS_DENIED
;
3282 ret
= (*(current_printif
->queue_pause
))(snum
);
3287 return WERR_INVALID_PARAM
;
3290 /* force update the database */
3291 print_cache_flush(lp_const_servicename(snum
));
3293 /* Send a printer notify message */
3295 notify_printer_status(server_event_context(), msg_ctx
, snum
,
3296 PRINTER_STATUS_PAUSED
);
3301 /****************************************************************************
3303 ****************************************************************************/
3305 WERROR
print_queue_resume(const struct auth_session_info
*server_info
,
3306 struct messaging_context
*msg_ctx
, int snum
)
3309 struct printif
*current_printif
= get_printer_fns( snum
);
3311 if (!W_ERROR_IS_OK(print_access_check(server_info
, msg_ctx
, snum
,
3312 PRINTER_ACCESS_ADMINISTER
))) {
3313 return WERR_ACCESS_DENIED
;
3318 ret
= (*(current_printif
->queue_resume
))(snum
);
3323 return WERR_INVALID_PARAM
;
3326 /* make sure the database is up to date */
3327 if (print_cache_expired(lp_const_servicename(snum
), True
))
3328 print_queue_update(msg_ctx
, snum
, True
);
3330 /* Send a printer notify message */
3332 notify_printer_status(server_event_context(), msg_ctx
, snum
,
3338 /****************************************************************************
3339 Purge a queue - implemented by deleting all jobs that we can delete.
3340 ****************************************************************************/
3342 WERROR
print_queue_purge(const struct auth_session_info
*server_info
,
3343 struct messaging_context
*msg_ctx
, int snum
)
3345 print_queue_struct
*queue
;
3346 print_status_struct status
;
3350 /* Force and update so the count is accurate (i.e. not a cached count) */
3351 print_queue_update(msg_ctx
, snum
, True
);
3353 can_job_admin
= W_ERROR_IS_OK(print_access_check(server_info
,
3356 JOB_ACCESS_ADMINISTER
));
3357 njobs
= print_queue_status(msg_ctx
, snum
, &queue
, &status
);
3359 if ( can_job_admin
)
3362 for (i
=0;i
<njobs
;i
++) {
3363 bool owner
= is_owner(server_info
, lp_const_servicename(snum
),
3366 if (owner
|| can_job_admin
) {
3367 print_job_delete1(server_event_context(), msg_ctx
,
3368 snum
, queue
[i
].sysjob
);
3372 if ( can_job_admin
)
3375 /* update the cache */
3376 print_queue_update(msg_ctx
, snum
, True
);