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();
201 if (!printer_list_parent_init()) {
205 unlink(cache_path("printing.tdb"));
206 mkdir(cache_path("printing"),0755);
208 /* handle a Samba upgrade */
210 for (snum
= 0; snum
< services
; snum
++) {
211 struct tdb_print_db
*pdb
;
212 if (!lp_print_ok(snum
))
215 pdb
= get_print_db_byname(lp_const_servicename(snum
));
218 if (tdb_lock_bystring(pdb
->tdb
, sversion
) != 0) {
219 DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum
) ));
220 release_print_db(pdb
);
223 if (tdb_fetch_int32(pdb
->tdb
, sversion
) != PRINT_DATABASE_VERSION
) {
224 tdb_wipe_all(pdb
->tdb
);
225 tdb_store_int32(pdb
->tdb
, sversion
, PRINT_DATABASE_VERSION
);
227 tdb_unlock_bystring(pdb
->tdb
, sversion
);
228 release_print_db(pdb
);
231 close_all_print_db(); /* Don't leave any open. */
233 /* do NT print initialization... */
234 return nt_printing_init(msg_ctx
);
237 /****************************************************************************
238 Shut down printing backend. Called once at shutdown to close the tdb.
239 ****************************************************************************/
241 void printing_end(void)
243 close_all_print_db(); /* Don't leave any open. */
246 /****************************************************************************
247 Retrieve the set of printing functions for a given service. This allows
248 us to set the printer function table based on the value of the 'printing'
251 Use the generic interface as the default and only use cups interface only
252 when asked for (and only when supported)
253 ****************************************************************************/
255 static struct printif
*get_printer_fns_from_type( enum printing_types type
)
257 struct printif
*printer_fns
= &generic_printif
;
260 if ( type
== PRINT_CUPS
) {
261 printer_fns
= &cups_printif
;
263 #endif /* HAVE_CUPS */
266 if ( type
== PRINT_IPRINT
) {
267 printer_fns
= &iprint_printif
;
269 #endif /* HAVE_IPRINT */
271 printer_fns
->type
= type
;
276 static struct printif
*get_printer_fns( int snum
)
278 return get_printer_fns_from_type( (enum printing_types
)lp_printing(snum
) );
282 /****************************************************************************
283 Useful function to generate a tdb key.
284 ****************************************************************************/
286 static TDB_DATA
print_key(uint32 jobid
, uint32
*tmp
)
290 SIVAL(tmp
, 0, jobid
);
291 ret
.dptr
= (uint8
*)tmp
;
292 ret
.dsize
= sizeof(*tmp
);
296 /****************************************************************************
297 Pack the devicemode to store it in a tdb.
298 ****************************************************************************/
299 static int pack_devicemode(struct spoolss_DeviceMode
*devmode
, uint8
*buf
, int buflen
)
301 enum ndr_err_code ndr_err
;
306 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(),
308 (ndr_push_flags_fn_t
)
309 ndr_push_spoolss_DeviceMode
);
310 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
311 DEBUG(10, ("pack_devicemode: "
312 "error encoding spoolss_DeviceMode\n"));
319 len
= tdb_pack(buf
, buflen
, "B", blob
.length
, blob
.data
);
322 DEBUG(8, ("Packed devicemode [%s]\n", devmode
->formname
));
329 /****************************************************************************
330 Unpack the devicemode to store it in a tdb.
331 ****************************************************************************/
332 static int unpack_devicemode(TALLOC_CTX
*mem_ctx
,
333 const uint8
*buf
, int buflen
,
334 struct spoolss_DeviceMode
**devmode
)
336 struct spoolss_DeviceMode
*dm
;
337 enum ndr_err_code ndr_err
;
345 len
= tdb_unpack(buf
, buflen
, "B", &data_len
, &data
);
350 dm
= talloc_zero(mem_ctx
, struct spoolss_DeviceMode
);
355 blob
= data_blob_const(data
, data_len
);
357 ndr_err
= ndr_pull_struct_blob(&blob
, dm
, dm
,
358 (ndr_pull_flags_fn_t
)ndr_pull_spoolss_DeviceMode
);
359 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
360 DEBUG(10, ("unpack_devicemode: "
361 "error parsing spoolss_DeviceMode\n"));
365 DEBUG(8, ("Unpacked devicemode [%s](%s)\n",
366 dm
->devicename
, dm
->formname
));
367 if (dm
->driverextra_data
.data
) {
368 DEBUG(8, ("with a private section of %d bytes\n",
369 dm
->__driverextra_length
));
379 /***********************************************************************
380 unpack a pjob from a tdb buffer
381 ***********************************************************************/
383 static int unpack_pjob(uint8
*buf
, int buflen
, struct printjob
*pjob
)
387 uint32 pjpid
, pjjobid
, pjsysjob
, pjfd
, pjstarttime
, pjstatus
;
388 uint32 pjsize
, pjpage_count
, pjspooled
, pjsmbjob
;
394 len
+= tdb_unpack(buf
+len
, buflen
-len
, "ddddddddddfffff",
415 used
= unpack_devicemode(NULL
, buf
+len
, buflen
-len
, &pjob
->devmode
);
423 pjob
->jobid
= pjjobid
;
424 pjob
->sysjob
= pjsysjob
;
426 pjob
->starttime
= pjstarttime
;
427 pjob
->status
= pjstatus
;
429 pjob
->page_count
= pjpage_count
;
430 pjob
->spooled
= pjspooled
;
431 pjob
->smbjob
= pjsmbjob
;
437 /****************************************************************************
438 Useful function to find a print job in the database.
439 ****************************************************************************/
441 static struct printjob
*print_job_find(const char *sharename
, uint32 jobid
)
443 static struct printjob pjob
;
446 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
448 DEBUG(10,("print_job_find: looking up job %u for share %s\n",
449 (unsigned int)jobid
, sharename
));
455 ret
= tdb_fetch_compat(pdb
->tdb
, print_key(jobid
, &tmp
));
456 release_print_db(pdb
);
459 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid
));
463 talloc_free(pjob
.devmode
);
467 if ( unpack_pjob( ret
.dptr
, ret
.dsize
, &pjob
) == -1 ) {
468 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid
));
475 DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
476 (int)pjob
.sysjob
, (unsigned int)jobid
));
477 SMB_ASSERT(pjob
.jobid
== jobid
);
482 /* Convert a unix jobid to a smb jobid */
484 struct unixjob_traverse_state
{
486 uint32 sysjob_to_jobid_value
;
489 static int unixjob_traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
,
490 TDB_DATA data
, void *private_data
)
492 struct printjob
*pjob
;
493 struct unixjob_traverse_state
*state
=
494 (struct unixjob_traverse_state
*)private_data
;
496 if (!data
.dptr
|| data
.dsize
== 0)
499 pjob
= (struct printjob
*)data
.dptr
;
500 if (key
.dsize
!= sizeof(uint32
))
503 if (state
->sysjob
== pjob
->sysjob
) {
504 state
->sysjob_to_jobid_value
= pjob
->jobid
;
511 static uint32
sysjob_to_jobid_pdb(struct tdb_print_db
*pdb
, int sysjob
)
513 struct unixjob_traverse_state state
;
515 state
.sysjob
= sysjob
;
516 state
.sysjob_to_jobid_value
= (uint32
)-1;
518 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
520 return state
.sysjob_to_jobid_value
;
523 /****************************************************************************
524 This is a *horribly expensive call as we have to iterate through all the
525 current printer tdb's. Don't do this often ! JRA.
526 ****************************************************************************/
528 uint32
sysjob_to_jobid(int unix_jobid
)
530 int services
= lp_numservices();
532 struct unixjob_traverse_state state
;
534 state
.sysjob
= unix_jobid
;
535 state
.sysjob_to_jobid_value
= (uint32
)-1;
537 for (snum
= 0; snum
< services
; snum
++) {
538 struct tdb_print_db
*pdb
;
539 if (!lp_print_ok(snum
))
541 pdb
= get_print_db_byname(lp_const_servicename(snum
));
545 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
546 release_print_db(pdb
);
547 if (state
.sysjob_to_jobid_value
!= (uint32
)-1)
548 return state
.sysjob_to_jobid_value
;
553 /****************************************************************************
554 Send notifications based on what has changed after a pjob_store.
555 ****************************************************************************/
557 static const struct {
559 uint32_t spoolss_status
;
560 } lpq_to_spoolss_status_map
[] = {
561 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
562 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
563 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
564 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
565 { LPQ_DELETING
, JOB_STATUS_DELETING
},
566 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
567 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
568 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
569 { LPQ_DELETED
, JOB_STATUS_DELETED
},
570 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED_DEVQ
},
571 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
575 /* Convert a lpq status value stored in printing.tdb into the
576 appropriate win32 API constant. */
578 static uint32
map_to_spoolss_status(uint32 lpq_status
)
582 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
583 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
584 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
591 /***************************************************************************
592 Append a jobid to the 'jobs changed' list.
593 ***************************************************************************/
595 static bool add_to_jobs_changed(struct tdb_print_db
*pdb
, uint32_t jobid
)
598 uint32_t store_jobid
;
600 SIVAL(&store_jobid
, 0, jobid
);
601 data
.dptr
= (uint8
*) &store_jobid
;
604 DEBUG(10,("add_to_jobs_added: Added jobid %u\n", (unsigned int)jobid
));
606 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"),
610 /***************************************************************************
611 Remove a jobid from the 'jobs changed' list.
612 ***************************************************************************/
614 static bool remove_from_jobs_changed(const char* sharename
, uint32_t jobid
)
616 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
620 bool gotlock
= False
;
628 key
= string_tdb_data("INFO/jobs_changed");
630 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) != 0)
635 data
= tdb_fetch_compat(pdb
->tdb
, key
);
637 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
640 job_count
= data
.dsize
/ 4;
641 for (i
= 0; i
< job_count
; i
++) {
644 ch_jobid
= IVAL(data
.dptr
, i
*4);
645 if (ch_jobid
== jobid
) {
646 if (i
< job_count
-1 )
647 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
649 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) != 0)
659 tdb_chainunlock(pdb
->tdb
, key
);
660 SAFE_FREE(data
.dptr
);
661 release_print_db(pdb
);
663 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid
));
665 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid
));
669 static void pjob_store_notify(struct tevent_context
*ev
,
670 struct messaging_context
*msg_ctx
,
671 const char* sharename
, uint32 jobid
,
672 struct printjob
*old_data
,
673 struct printjob
*new_data
,
676 bool new_job
= false;
677 bool changed
= false;
679 if (old_data
== NULL
) {
683 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
684 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
685 time first or else we'll end up with potential alignment
686 errors. I don't think the systemtime should be spooled as
687 a string, but this gets us around that error.
688 --jerry (i'll feel dirty for this) */
691 notify_job_submitted(ev
, msg_ctx
,
692 sharename
, jobid
, new_data
->starttime
);
693 notify_job_username(ev
, msg_ctx
,
694 sharename
, jobid
, new_data
->user
);
695 notify_job_name(ev
, msg_ctx
,
696 sharename
, jobid
, new_data
->jobname
);
697 notify_job_status(ev
, msg_ctx
,
698 sharename
, jobid
, map_to_spoolss_status(new_data
->status
));
699 notify_job_total_bytes(ev
, msg_ctx
,
700 sharename
, jobid
, new_data
->size
);
701 notify_job_total_pages(ev
, msg_ctx
,
702 sharename
, jobid
, new_data
->page_count
);
704 if (!strequal(old_data
->jobname
, new_data
->jobname
)) {
705 notify_job_name(ev
, msg_ctx
, sharename
,
706 jobid
, new_data
->jobname
);
710 if (old_data
->status
!= new_data
->status
) {
711 notify_job_status(ev
, msg_ctx
,
713 map_to_spoolss_status(new_data
->status
));
716 if (old_data
->size
!= new_data
->size
) {
717 notify_job_total_bytes(ev
, msg_ctx
,
718 sharename
, jobid
, new_data
->size
);
721 if (old_data
->page_count
!= new_data
->page_count
) {
722 notify_job_total_pages(ev
, msg_ctx
,
724 new_data
->page_count
);
731 /****************************************************************************
732 Store a job structure back to the database.
733 ****************************************************************************/
735 static bool pjob_store(struct tevent_context
*ev
,
736 struct messaging_context
*msg_ctx
,
737 const char* sharename
, uint32 jobid
,
738 struct printjob
*pjob
)
741 TDB_DATA old_data
, new_data
;
743 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
745 int len
, newlen
, buflen
;
753 old_data
= tdb_fetch_compat(pdb
->tdb
, print_key(jobid
, &tmp
));
755 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
762 len
+= tdb_pack(buf
+len
, buflen
-len
, "ddddddddddfffff",
765 (uint32
)pjob
->sysjob
,
767 (uint32
)pjob
->starttime
,
768 (uint32
)pjob
->status
,
770 (uint32
)pjob
->page_count
,
771 (uint32
)pjob
->spooled
,
772 (uint32
)pjob
->smbjob
,
779 len
+= pack_devicemode(pjob
->devmode
, buf
+len
, buflen
-len
);
782 buf
= (uint8
*)SMB_REALLOC(buf
, len
);
784 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
789 } while ( buflen
!= len
);
795 new_data
.dsize
= len
;
796 ret
= (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), new_data
,
799 /* Send notify updates for what has changed */
802 bool changed
= false;
803 struct printjob old_pjob
;
805 if ( old_data
.dsize
)
807 if ( unpack_pjob( old_data
.dptr
, old_data
.dsize
, &old_pjob
) != -1 )
809 pjob_store_notify(server_event_context(),
811 sharename
, jobid
, &old_pjob
,
814 talloc_free(old_pjob
.devmode
);
817 add_to_jobs_changed(pdb
, jobid
);
824 pjob_store_notify(server_event_context(), msg_ctx
,
825 sharename
, jobid
, NULL
, pjob
,
830 release_print_db(pdb
);
832 SAFE_FREE( old_data
.dptr
);
838 /****************************************************************************
839 Remove a job structure from the database.
840 ****************************************************************************/
842 static void pjob_delete(struct tevent_context
*ev
,
843 struct messaging_context
*msg_ctx
,
844 const char* sharename
, uint32 jobid
)
847 struct printjob
*pjob
;
848 uint32 job_status
= 0;
849 struct tdb_print_db
*pdb
;
851 pdb
= get_print_db_byname( sharename
);
856 pjob
= print_job_find( sharename
, jobid
);
859 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
860 (unsigned int)jobid
));
861 release_print_db(pdb
);
865 /* We must cycle through JOB_STATUS_DELETING and
866 JOB_STATUS_DELETED for the port monitor to delete the job
869 job_status
= JOB_STATUS_DELETING
|JOB_STATUS_DELETED
;
870 notify_job_status(ev
, msg_ctx
, sharename
, jobid
, job_status
);
872 /* Remove from printing.tdb */
874 tdb_delete(pdb
->tdb
, print_key(jobid
, &tmp
));
875 remove_from_jobs_added(sharename
, jobid
);
876 release_print_db( pdb
);
877 rap_jobid_delete(sharename
, jobid
);
880 /****************************************************************************
881 List a unix job in the print database.
882 ****************************************************************************/
884 static void print_unix_job(struct tevent_context
*ev
,
885 struct messaging_context
*msg_ctx
,
886 const char *sharename
, print_queue_struct
*q
,
889 struct printjob pj
, *old_pj
;
891 if (jobid
== (uint32
)-1)
892 jobid
= q
->sysjob
+ UNIX_JOB_START
;
894 /* Preserve the timestamp on an existing unix print job */
896 old_pj
= print_job_find(sharename
, jobid
);
902 pj
.sysjob
= q
->sysjob
;
904 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
905 pj
.status
= q
->status
;
908 fstrcpy(pj
.filename
, old_pj
? old_pj
->filename
: "");
909 if (jobid
< UNIX_JOB_START
) {
911 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: "Remote Downlevel Document");
914 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: q
->fs_file
);
916 fstrcpy(pj
.user
, old_pj
? old_pj
->user
: q
->fs_user
);
917 fstrcpy(pj
.queuename
, old_pj
? old_pj
->queuename
: sharename
);
919 pjob_store(ev
, msg_ctx
, sharename
, jobid
, &pj
);
923 struct traverse_struct
{
924 print_queue_struct
*queue
;
925 int qcount
, snum
, maxcount
, total_jobs
;
926 const char *sharename
;
928 const char *lprm_command
;
929 struct printif
*print_if
;
930 struct tevent_context
*ev
;
931 struct messaging_context
*msg_ctx
;
934 /****************************************************************************
935 Utility fn to delete any jobs that are no longer active.
936 ****************************************************************************/
938 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
940 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
941 struct printjob pjob
;
945 if ( key
.dsize
!= sizeof(jobid
) )
948 if (unpack_pjob(data
.dptr
, data
.dsize
, &pjob
) == -1)
950 talloc_free(pjob
.devmode
);
954 /* remove a unix job if it isn't in the system queue any more */
955 for (i
=0;i
<ts
->qcount
;i
++) {
956 if (ts
->queue
[i
].sysjob
== pjob
.sysjob
) {
960 if (i
== ts
->qcount
) {
961 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
962 (unsigned int)jobid
));
963 pjob_delete(ts
->ev
, ts
->msg_ctx
,
964 ts
->sharename
, jobid
);
968 /* need to continue the the bottom of the function to
969 save the correct attributes */
972 /* maybe it hasn't been spooled yet */
974 /* if a job is not spooled and the process doesn't
975 exist then kill it. This cleans up after smbd
977 if (!process_exists_by_pid(pjob
.pid
)) {
978 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
979 (unsigned int)jobid
, (unsigned int)pjob
.pid
));
980 pjob_delete(ts
->ev
, ts
->msg_ctx
,
981 ts
->sharename
, jobid
);
987 /* this check only makes sense for jobs submitted from Windows clients */
990 for (i
=0;i
<ts
->qcount
;i
++) {
991 if ( pjob
.status
== LPQ_DELETED
)
994 if (ts
->queue
[i
].sysjob
== pjob
.sysjob
) {
996 /* try to clean up any jobs that need to be deleted */
998 if ( pjob
.status
== LPQ_DELETING
) {
1001 result
= (*(ts
->print_if
->job_delete
))(
1002 ts
->sharename
, ts
->lprm_command
, &pjob
);
1004 if ( result
!= 0 ) {
1005 /* if we can't delete, then reset the job status */
1006 pjob
.status
= LPQ_QUEUED
;
1007 pjob_store(ts
->ev
, ts
->msg_ctx
,
1008 ts
->sharename
, jobid
, &pjob
);
1011 /* if we deleted the job, the remove the tdb record */
1014 ts
->sharename
, jobid
);
1015 pjob
.status
= LPQ_DELETED
;
1025 /* The job isn't in the system queue - we have to assume it has
1026 completed, so delete the database entry. */
1028 if (i
== ts
->qcount
) {
1030 /* A race can occur between the time a job is spooled and
1031 when it appears in the lpq output. This happens when
1032 the job is added to printing.tdb when another smbd
1033 running print_queue_update() has completed a lpq and
1034 is currently traversing the printing tdb and deleting jobs.
1035 Don't delete the job if it was submitted after the lpq_time. */
1037 if (pjob
.starttime
< ts
->lpq_time
) {
1038 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
1039 (unsigned int)jobid
,
1040 (unsigned int)pjob
.starttime
,
1041 (unsigned int)ts
->lpq_time
));
1042 pjob_delete(ts
->ev
, ts
->msg_ctx
,
1043 ts
->sharename
, jobid
);
1049 /* Save the pjob attributes we will store. */
1050 ts
->queue
[i
].sysjob
= pjob
.sysjob
;
1051 ts
->queue
[i
].size
= pjob
.size
;
1052 ts
->queue
[i
].page_count
= pjob
.page_count
;
1053 ts
->queue
[i
].status
= pjob
.status
;
1054 ts
->queue
[i
].priority
= 1;
1055 ts
->queue
[i
].time
= pjob
.starttime
;
1056 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
1057 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
1064 /****************************************************************************
1065 Check if the print queue has been updated recently enough.
1066 ****************************************************************************/
1068 static void print_cache_flush(const char *sharename
)
1071 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1075 slprintf(key
, sizeof(key
)-1, "CACHE/%s", sharename
);
1076 tdb_store_int32(pdb
->tdb
, key
, -1);
1077 release_print_db(pdb
);
1080 /****************************************************************************
1081 Check if someone already thinks they are doing the update.
1082 ****************************************************************************/
1084 static pid_t
get_updating_pid(const char *sharename
)
1089 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1093 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
1094 key
= string_tdb_data(keystr
);
1096 data
= tdb_fetch_compat(pdb
->tdb
, key
);
1097 release_print_db(pdb
);
1098 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
)) {
1099 SAFE_FREE(data
.dptr
);
1103 updating_pid
= IVAL(data
.dptr
, 0);
1104 SAFE_FREE(data
.dptr
);
1106 if (process_exists_by_pid(updating_pid
))
1107 return updating_pid
;
1112 /****************************************************************************
1113 Set the fact that we're doing the update, or have finished doing the update
1115 ****************************************************************************/
1117 static void set_updating_pid(const fstring sharename
, bool updating
)
1122 pid_t updating_pid
= getpid();
1125 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1130 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
1131 key
= string_tdb_data(keystr
);
1133 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
1134 updating
? "" : "not ",
1138 tdb_delete(pdb
->tdb
, key
);
1139 release_print_db(pdb
);
1143 SIVAL( buffer
, 0, updating_pid
);
1145 data
.dsize
= 4; /* we always assume this is a 4 byte value */
1147 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1148 release_print_db(pdb
);
1151 /****************************************************************************
1152 Sort print jobs by submittal time.
1153 ****************************************************************************/
1155 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1166 /* Sort on job start time */
1168 if (j1
->time
== j2
->time
)
1170 return (j1
->time
> j2
->time
) ? 1 : -1;
1173 /****************************************************************************
1174 Store the sorted queue representation for later portmon retrieval.
1176 ****************************************************************************/
1178 static void store_queue_struct(struct tdb_print_db
*pdb
, struct traverse_struct
*pts
)
1181 int max_reported_jobs
= lp_max_reported_jobs(pts
->snum
);
1182 print_queue_struct
*queue
= pts
->queue
;
1185 unsigned int qcount
;
1187 if (max_reported_jobs
&& (max_reported_jobs
< pts
->qcount
))
1188 pts
->qcount
= max_reported_jobs
;
1191 /* Work out the size. */
1193 data
.dsize
+= tdb_pack(NULL
, 0, "d", qcount
);
1195 for (i
= 0; i
< pts
->qcount
; i
++) {
1196 if ( queue
[i
].status
== LPQ_DELETED
)
1200 data
.dsize
+= tdb_pack(NULL
, 0, "ddddddff",
1201 (uint32
)queue
[i
].sysjob
,
1202 (uint32
)queue
[i
].size
,
1203 (uint32
)queue
[i
].page_count
,
1204 (uint32
)queue
[i
].status
,
1205 (uint32
)queue
[i
].priority
,
1206 (uint32
)queue
[i
].time
,
1211 if ((data
.dptr
= (uint8
*)SMB_MALLOC(data
.dsize
)) == NULL
)
1215 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "d", qcount
);
1216 for (i
= 0; i
< pts
->qcount
; i
++) {
1217 if ( queue
[i
].status
== LPQ_DELETED
)
1220 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
1221 (uint32
)queue
[i
].sysjob
,
1222 (uint32
)queue
[i
].size
,
1223 (uint32
)queue
[i
].page_count
,
1224 (uint32
)queue
[i
].status
,
1225 (uint32
)queue
[i
].priority
,
1226 (uint32
)queue
[i
].time
,
1231 tdb_store(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"), data
,
1233 SAFE_FREE(data
.dptr
);
1237 static TDB_DATA
get_jobs_added_data(struct tdb_print_db
*pdb
)
1243 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_added"));
1244 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0)) {
1245 SAFE_FREE(data
.dptr
);
1252 static void check_job_added(const char *sharename
, TDB_DATA data
, uint32 jobid
)
1255 unsigned int job_count
= data
.dsize
/ 4;
1257 for (i
= 0; i
< job_count
; i
++) {
1260 ch_jobid
= IVAL(data
.dptr
, i
*4);
1261 if (ch_jobid
== jobid
)
1262 remove_from_jobs_added(sharename
, jobid
);
1266 /****************************************************************************
1267 Check if the print queue has been updated recently enough.
1268 ****************************************************************************/
1270 static bool print_cache_expired(const char *sharename
, bool check_pending
)
1273 time_t last_qscan_time
, time_now
= time(NULL
);
1274 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1275 bool result
= False
;
1280 snprintf(key
, sizeof(key
), "CACHE/%s", sharename
);
1281 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1284 * Invalidate the queue for 3 reasons.
1285 * (1). last queue scan time == -1.
1286 * (2). Current time - last queue scan time > allowed cache time.
1287 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1288 * This last test picks up machines for which the clock has been moved
1289 * forward, an lpq scan done and then the clock moved back. Otherwise
1290 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1293 if (last_qscan_time
== ((time_t)-1)
1294 || (time_now
- last_qscan_time
) >= lp_lpqcachetime()
1295 || last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
))
1298 time_t msg_pending_time
;
1300 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1301 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1302 sharename
, (int)last_qscan_time
, (int)time_now
,
1303 (int)lp_lpqcachetime() ));
1305 /* check if another smbd has already sent a message to update the
1306 queue. Give the pending message one minute to clear and
1307 then send another message anyways. Make sure to check for
1308 clocks that have been run forward and then back again. */
1310 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1313 && tdb_fetch_uint32( pdb
->tdb
, key
, &u
)
1314 && (msg_pending_time
=u
) > 0
1315 && msg_pending_time
<= time_now
1316 && (time_now
- msg_pending_time
) < 60 )
1318 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1327 release_print_db(pdb
);
1331 /****************************************************************************
1332 main work for updating the lpq cache for a printer queue
1333 ****************************************************************************/
1335 static void print_queue_update_internal(struct tevent_context
*ev
,
1336 struct messaging_context
*msg_ctx
,
1337 const char *sharename
,
1338 struct printif
*current_printif
,
1339 char *lpq_command
, char *lprm_command
)
1342 print_queue_struct
*queue
= NULL
;
1343 print_status_struct status
;
1344 print_status_struct old_status
;
1345 struct printjob
*pjob
;
1346 struct traverse_struct tstruct
;
1349 fstring keystr
, cachestr
;
1350 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1356 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1357 sharename
, current_printif
->type
, lpq_command
));
1360 * Update the cache time FIRST ! Stops others even
1361 * attempting to get the lock and doing this
1362 * if the lpq takes a long time.
1365 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", sharename
);
1366 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
1368 /* get the current queue using the appropriate interface */
1369 ZERO_STRUCT(status
);
1371 qcount
= (*(current_printif
->queue_get
))(sharename
,
1372 current_printif
->type
,
1373 lpq_command
, &queue
, &status
);
1375 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1376 qcount
, (qcount
!= 1) ? "s" : "", sharename
));
1378 /* Sort the queue by submission time otherwise they are displayed
1381 TYPESAFE_QSORT(queue
, qcount
, printjob_comp
);
1384 any job in the internal database that is marked as spooled
1385 and doesn't exist in the system queue is considered finished
1386 and removed from the database
1388 any job in the system database but not in the internal database
1389 is added as a unix job
1391 fill in any system job numbers as we go
1394 jcdata
= get_jobs_added_data(pdb
);
1396 for (i
=0; i
<qcount
; i
++) {
1397 uint32 jobid
= sysjob_to_jobid_pdb(pdb
, queue
[i
].sysjob
);
1398 if (jobid
== (uint32
)-1) {
1399 /* assume its a unix print job */
1400 print_unix_job(ev
, msg_ctx
,
1401 sharename
, &queue
[i
], jobid
);
1405 /* we have an active SMB print job - update its status */
1406 pjob
= print_job_find(sharename
, jobid
);
1408 /* err, somethings wrong. Probably smbd was restarted
1409 with jobs in the queue. All we can do is treat them
1410 like unix jobs. Pity. */
1411 DEBUG(1, ("queued print job %d not found in jobs list, "
1412 "assuming unix job\n", jobid
));
1413 print_unix_job(ev
, msg_ctx
,
1414 sharename
, &queue
[i
], jobid
);
1418 /* don't reset the status on jobs to be deleted */
1420 if ( pjob
->status
!= LPQ_DELETING
)
1421 pjob
->status
= queue
[i
].status
;
1423 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
1425 check_job_added(sharename
, jcdata
, jobid
);
1428 SAFE_FREE(jcdata
.dptr
);
1430 /* now delete any queued entries that don't appear in the
1432 tstruct
.queue
= queue
;
1433 tstruct
.qcount
= qcount
;
1435 tstruct
.total_jobs
= 0;
1436 tstruct
.lpq_time
= time(NULL
);
1437 tstruct
.sharename
= sharename
;
1438 tstruct
.lprm_command
= lprm_command
;
1439 tstruct
.print_if
= current_printif
;
1441 tstruct
.msg_ctx
= msg_ctx
;
1443 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
1445 /* Store the linearised queue, max jobs only. */
1446 store_queue_struct(pdb
, &tstruct
);
1448 SAFE_FREE(tstruct
.queue
);
1450 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1451 sharename
, tstruct
.total_jobs
));
1453 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
1455 get_queue_status(sharename
, &old_status
);
1456 if (old_status
.qcount
!= qcount
)
1457 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1458 old_status
.qcount
, qcount
, sharename
));
1460 /* store the new queue status structure */
1461 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
1462 key
= string_tdb_data(keystr
);
1464 status
.qcount
= qcount
;
1465 data
.dptr
= (uint8
*)&status
;
1466 data
.dsize
= sizeof(status
);
1467 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1470 * Update the cache time again. We want to do this call
1471 * as little as possible...
1474 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", sharename
);
1475 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
1477 /* clear the msg pending record for this queue */
1479 snprintf(keystr
, sizeof(keystr
), "MSG_PENDING/%s", sharename
);
1481 if ( !tdb_store_uint32( pdb
->tdb
, keystr
, 0 ) ) {
1482 /* log a message but continue on */
1484 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1488 release_print_db( pdb
);
1493 /****************************************************************************
1494 Update the internal database from the system print queue for a queue.
1495 obtain a lock on the print queue before proceeding (needed when mutiple
1496 smbd processes maytry to update the lpq cache concurrently).
1497 ****************************************************************************/
1499 static void print_queue_update_with_lock( struct tevent_context
*ev
,
1500 struct messaging_context
*msg_ctx
,
1501 const char *sharename
,
1502 struct printif
*current_printif
,
1503 char *lpq_command
, char *lprm_command
)
1506 struct tdb_print_db
*pdb
;
1508 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename
));
1509 pdb
= get_print_db_byname(sharename
);
1513 if ( !print_cache_expired(sharename
, False
) ) {
1514 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename
));
1515 release_print_db(pdb
);
1520 * Check to see if someone else is doing this update.
1521 * This is essentially a mutex on the update.
1524 if (get_updating_pid(sharename
) != -1) {
1525 release_print_db(pdb
);
1529 /* Lock the queue for the database update */
1531 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", sharename
);
1532 /* Only wait 10 seconds for this. */
1533 if (tdb_lock_bystring_with_timeout(pdb
->tdb
, keystr
, 10) != 0) {
1534 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename
));
1535 release_print_db(pdb
);
1540 * Ensure that no one else got in here.
1541 * If the updating pid is still -1 then we are
1545 if (get_updating_pid(sharename
) != -1) {
1547 * Someone else is doing the update, exit.
1549 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1550 release_print_db(pdb
);
1555 * We're going to do the update ourselves.
1558 /* Tell others we're doing the update. */
1559 set_updating_pid(sharename
, True
);
1562 * Allow others to enter and notice we're doing
1566 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1568 /* do the main work now */
1570 print_queue_update_internal(ev
, msg_ctx
,
1571 sharename
, current_printif
,
1572 lpq_command
, lprm_command
);
1574 /* Delete our pid from the db. */
1575 set_updating_pid(sharename
, False
);
1576 release_print_db(pdb
);
1579 /****************************************************************************
1580 this is the receive function of the background lpq updater
1581 ****************************************************************************/
1582 void print_queue_receive(struct messaging_context
*msg
,
1585 struct server_id server_id
,
1589 char *lpqcommand
= NULL
, *lprmcommand
= NULL
;
1593 len
= tdb_unpack( (uint8
*)data
->data
, data
->length
, "fdPP",
1600 SAFE_FREE(lpqcommand
);
1601 SAFE_FREE(lprmcommand
);
1602 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1606 print_queue_update_with_lock(server_event_context(), msg
, sharename
,
1607 get_printer_fns_from_type((enum printing_types
)printing_type
),
1608 lpqcommand
, lprmcommand
);
1610 SAFE_FREE(lpqcommand
);
1611 SAFE_FREE(lprmcommand
);
1615 /****************************************************************************
1616 update the internal database from the system print queue for a queue
1617 ****************************************************************************/
1619 extern pid_t background_lpq_updater_pid
;
1621 static void print_queue_update(struct messaging_context
*msg_ctx
,
1622 int snum
, bool force
)
1626 char *lpqcommand
= NULL
;
1627 char *lprmcommand
= NULL
;
1628 uint8
*buffer
= NULL
;
1631 struct tdb_print_db
*pdb
;
1633 struct printif
*current_printif
;
1634 TALLOC_CTX
*ctx
= talloc_tos();
1636 fstrcpy( sharename
, lp_const_servicename(snum
));
1638 /* don't strip out characters like '$' from the printername */
1640 lpqcommand
= talloc_string_sub2(ctx
,
1641 lp_lpqcommand(snum
),
1643 lp_printername(snum
),
1644 false, false, false);
1648 lpqcommand
= talloc_sub_advanced(ctx
,
1649 lp_servicename(snum
),
1650 current_user_info
.unix_name
,
1652 current_user
.ut
.gid
,
1653 get_current_username(),
1654 current_user_info
.domain
,
1660 lprmcommand
= talloc_string_sub2(ctx
,
1661 lp_lprmcommand(snum
),
1663 lp_printername(snum
),
1664 false, false, false);
1668 lprmcommand
= talloc_sub_advanced(ctx
,
1669 lp_servicename(snum
),
1670 current_user_info
.unix_name
,
1672 current_user
.ut
.gid
,
1673 get_current_username(),
1674 current_user_info
.domain
,
1681 * Make sure that the background queue process exists.
1682 * Otherwise just do the update ourselves
1685 if ( force
|| background_lpq_updater_pid
== -1 ) {
1686 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename
));
1687 current_printif
= get_printer_fns( snum
);
1688 print_queue_update_with_lock(server_event_context(), msg_ctx
,
1689 sharename
, current_printif
,
1690 lpqcommand
, lprmcommand
);
1695 type
= lp_printing(snum
);
1697 /* get the length */
1699 len
= tdb_pack( NULL
, 0, "fdPP",
1705 buffer
= SMB_XMALLOC_ARRAY( uint8
, len
);
1707 /* now pack the buffer */
1708 newlen
= tdb_pack( buffer
, len
, "fdPP",
1714 SMB_ASSERT( newlen
== len
);
1716 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1717 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1718 sharename
, type
, lpqcommand
, lprmcommand
));
1720 /* here we set a msg pending record for other smbd processes
1721 to throttle the number of duplicate print_queue_update msgs
1724 pdb
= get_print_db_byname(sharename
);
1730 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1732 if ( !tdb_store_uint32( pdb
->tdb
, key
, time(NULL
) ) ) {
1733 /* log a message but continue on */
1735 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1739 release_print_db( pdb
);
1741 /* finally send the message */
1743 messaging_send_buf(msg_ctx
, pid_to_procid(background_lpq_updater_pid
),
1744 MSG_PRINTER_UPDATE
, (uint8
*)buffer
, len
);
1746 SAFE_FREE( buffer
);
1751 /****************************************************************************
1752 Create/Update an entry in the print tdb that will allow us to send notify
1753 updates only to interested smbd's.
1754 ****************************************************************************/
1756 bool print_notify_register_pid(int snum
)
1759 struct tdb_print_db
*pdb
= NULL
;
1760 TDB_CONTEXT
*tdb
= NULL
;
1761 const char *printername
;
1762 uint32_t mypid
= (uint32_t)getpid();
1766 /* if (snum == -1), then the change notify request was
1767 on a print server handle and we need to register on
1772 int num_services
= lp_numservices();
1775 for ( idx
=0; idx
<num_services
; idx
++ ) {
1776 if (lp_snum_ok(idx
) && lp_print_ok(idx
) )
1777 print_notify_register_pid(idx
);
1782 else /* register for a specific printer */
1784 printername
= lp_const_servicename(snum
);
1785 pdb
= get_print_db_byname(printername
);
1791 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) != 0) {
1792 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1795 release_print_db(pdb
);
1799 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1801 /* Add ourselves and increase the refcount. */
1803 for (i
= 0; i
< data
.dsize
; i
+= 8) {
1804 if (IVAL(data
.dptr
,i
) == mypid
) {
1805 uint32 new_refcount
= IVAL(data
.dptr
, i
+4) + 1;
1806 SIVAL(data
.dptr
, i
+4, new_refcount
);
1811 if (i
== data
.dsize
) {
1812 /* We weren't in the list. Realloc. */
1813 data
.dptr
= (uint8
*)SMB_REALLOC(data
.dptr
, data
.dsize
+ 8);
1815 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1820 SIVAL(data
.dptr
,data
.dsize
- 8,mypid
);
1821 SIVAL(data
.dptr
,data
.dsize
- 4,1); /* Refcount. */
1824 /* Store back the record. */
1825 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) != 0) {
1826 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1827 list for printer %s\n", printername
));
1835 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1837 release_print_db(pdb
);
1838 SAFE_FREE(data
.dptr
);
1842 /****************************************************************************
1843 Update an entry in the print tdb that will allow us to send notify
1844 updates only to interested smbd's.
1845 ****************************************************************************/
1847 bool print_notify_deregister_pid(int snum
)
1850 struct tdb_print_db
*pdb
= NULL
;
1851 TDB_CONTEXT
*tdb
= NULL
;
1852 const char *printername
;
1853 uint32_t mypid
= (uint32_t)getpid();
1857 /* if ( snum == -1 ), we are deregister a print server handle
1858 which means to deregister on all print queues */
1862 int num_services
= lp_numservices();
1865 for ( idx
=0; idx
<num_services
; idx
++ ) {
1866 if ( lp_snum_ok(idx
) && lp_print_ok(idx
) )
1867 print_notify_deregister_pid(idx
);
1872 else /* deregister a specific printer */
1874 printername
= lp_const_servicename(snum
);
1875 pdb
= get_print_db_byname(printername
);
1881 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) != 0) {
1882 DEBUG(0,("print_notify_register_pid: Failed to lock \
1883 printer %s database\n", printername
));
1885 release_print_db(pdb
);
1889 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1891 /* Reduce refcount. Remove ourselves if zero. */
1893 for (i
= 0; i
< data
.dsize
; ) {
1894 if (IVAL(data
.dptr
,i
) == mypid
) {
1895 uint32 refcount
= IVAL(data
.dptr
, i
+4);
1899 if (refcount
== 0) {
1900 if (data
.dsize
- i
> 8)
1901 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);
1905 SIVAL(data
.dptr
, i
+4, refcount
);
1911 if (data
.dsize
== 0)
1912 SAFE_FREE(data
.dptr
);
1914 /* Store back the record. */
1915 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) != 0) {
1916 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1917 list for printer %s\n", printername
));
1925 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1927 release_print_db(pdb
);
1928 SAFE_FREE(data
.dptr
);
1932 /****************************************************************************
1933 Check if a jobid is valid. It is valid if it exists in the database.
1934 ****************************************************************************/
1936 bool print_job_exists(const char* sharename
, uint32 jobid
)
1938 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1944 ret
= tdb_exists(pdb
->tdb
, print_key(jobid
, &tmp
));
1945 release_print_db(pdb
);
1949 /****************************************************************************
1950 Give the filename used for a jobid.
1951 Only valid for the process doing the spooling and when the job
1952 has not been spooled.
1953 ****************************************************************************/
1955 char *print_job_fname(const char* sharename
, uint32 jobid
)
1957 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1958 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= getpid())
1960 return pjob
->filename
;
1964 /****************************************************************************
1965 Give the filename used for a jobid.
1966 Only valid for the process doing the spooling and when the job
1967 has not been spooled.
1968 ****************************************************************************/
1970 struct spoolss_DeviceMode
*print_job_devmode(const char* sharename
, uint32 jobid
)
1972 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1977 return pjob
->devmode
;
1980 /****************************************************************************
1981 Set the name of a job. Only possible for owner.
1982 ****************************************************************************/
1984 bool print_job_set_name(struct tevent_context
*ev
,
1985 struct messaging_context
*msg_ctx
,
1986 const char *sharename
, uint32 jobid
, const char *name
)
1988 struct printjob
*pjob
;
1990 pjob
= print_job_find(sharename
, jobid
);
1991 if (!pjob
|| pjob
->pid
!= getpid())
1994 fstrcpy(pjob
->jobname
, name
);
1995 return pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
1998 /****************************************************************************
1999 Get the name of a job. Only possible for owner.
2000 ****************************************************************************/
2002 bool print_job_get_name(TALLOC_CTX
*mem_ctx
, const char *sharename
, uint32_t jobid
, char **name
)
2004 struct printjob
*pjob
;
2006 pjob
= print_job_find(sharename
, jobid
);
2007 if (!pjob
|| pjob
->pid
!= getpid()) {
2011 *name
= talloc_strdup(mem_ctx
, pjob
->jobname
);
2020 /***************************************************************************
2021 Remove a jobid from the 'jobs added' list.
2022 ***************************************************************************/
2024 static bool remove_from_jobs_added(const char* sharename
, uint32 jobid
)
2026 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2028 size_t job_count
, i
;
2030 bool gotlock
= False
;
2038 key
= string_tdb_data("INFO/jobs_added");
2040 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) != 0)
2045 data
= tdb_fetch_compat(pdb
->tdb
, key
);
2047 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
2050 job_count
= data
.dsize
/ 4;
2051 for (i
= 0; i
< job_count
; i
++) {
2054 ch_jobid
= IVAL(data
.dptr
, i
*4);
2055 if (ch_jobid
== jobid
) {
2056 if (i
< job_count
-1 )
2057 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
2059 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) != 0)
2069 tdb_chainunlock(pdb
->tdb
, key
);
2070 SAFE_FREE(data
.dptr
);
2071 release_print_db(pdb
);
2073 DEBUG(10,("remove_from_jobs_added: removed jobid %u\n", (unsigned int)jobid
));
2075 DEBUG(10,("remove_from_jobs_added: Failed to remove jobid %u\n", (unsigned int)jobid
));
2079 /****************************************************************************
2080 Delete a print job - don't update queue.
2081 ****************************************************************************/
2083 static bool print_job_delete1(struct tevent_context
*ev
,
2084 struct messaging_context
*msg_ctx
,
2085 int snum
, uint32 jobid
)
2087 const char* sharename
= lp_const_servicename(snum
);
2088 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
2090 struct printif
*current_printif
= get_printer_fns( snum
);
2096 * If already deleting just return.
2099 if (pjob
->status
== LPQ_DELETING
)
2102 /* Hrm - we need to be able to cope with deleting a job before it
2103 has reached the spooler. Just mark it as LPQ_DELETING and
2104 let the print_queue_update() code rmeove the record */
2107 if (pjob
->sysjob
== -1) {
2108 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
2111 /* Set the tdb entry to be deleting. */
2113 pjob
->status
= LPQ_DELETING
;
2114 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2116 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
2118 result
= (*(current_printif
->job_delete
))(
2119 lp_printername(snum
),
2120 lp_lprmcommand(snum
),
2123 /* Delete the tdb entry if the delete succeeded or the job hasn't
2127 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2132 pjob_delete(ev
, msg_ctx
, sharename
, jobid
);
2133 /* Ensure we keep a rough count of the number of total jobs... */
2134 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, -1);
2135 release_print_db(pdb
);
2139 remove_from_jobs_added( sharename
, jobid
);
2141 return (result
== 0);
2144 /****************************************************************************
2145 Return true if the current user owns the print job.
2146 ****************************************************************************/
2148 static bool is_owner(const struct auth_session_info
*server_info
,
2149 const char *servicename
,
2152 struct printjob
*pjob
= print_job_find(servicename
, jobid
);
2154 if (!pjob
|| !server_info
)
2157 return strequal(pjob
->user
, server_info
->unix_info
->sanitized_username
);
2160 /****************************************************************************
2162 ****************************************************************************/
2164 WERROR
print_job_delete(const struct auth_session_info
*server_info
,
2165 struct messaging_context
*msg_ctx
,
2166 int snum
, uint32_t jobid
)
2168 const char* sharename
= lp_const_servicename(snum
);
2169 struct printjob
*pjob
;
2173 owner
= is_owner(server_info
, lp_const_servicename(snum
), jobid
);
2175 /* Check access against security descriptor or whether the user
2179 !print_access_check(server_info
, msg_ctx
, snum
,
2180 JOB_ACCESS_ADMINISTER
)) {
2181 DEBUG(3, ("delete denied by security descriptor\n"));
2183 /* BEGIN_ADMIN_LOG */
2184 sys_adminlog( LOG_ERR
,
2185 "Permission denied-- user not allowed to delete, \
2186 pause, or resume print job. User name: %s. Printer name: %s.",
2187 uidtoname(server_info
->unix_token
->uid
),
2188 lp_printername(snum
) );
2191 return WERR_ACCESS_DENIED
;
2195 * get the spooled filename of the print job
2196 * if this works, then the file has not been spooled
2197 * to the underlying print system. Just delete the
2198 * spool file & return.
2201 fname
= print_job_fname(sharename
, jobid
);
2202 if (fname
!= NULL
) {
2203 /* remove the spool file */
2204 DEBUG(10, ("print_job_delete: "
2205 "Removing spool file [%s]\n", fname
));
2206 if (unlink(fname
) == -1) {
2207 return map_werror_from_unix(errno
);
2211 if (!print_job_delete1(server_event_context(), msg_ctx
, snum
, jobid
)) {
2212 return WERR_ACCESS_DENIED
;
2215 /* force update the database and say the delete failed if the
2218 print_queue_update(msg_ctx
, snum
, True
);
2220 pjob
= print_job_find(sharename
, jobid
);
2221 if (pjob
&& (pjob
->status
!= LPQ_DELETING
)) {
2222 return WERR_ACCESS_DENIED
;
2225 return WERR_PRINTER_HAS_JOBS_QUEUED
;
2228 /****************************************************************************
2230 ****************************************************************************/
2232 bool print_job_pause(const struct auth_session_info
*server_info
,
2233 struct messaging_context
*msg_ctx
,
2234 int snum
, uint32 jobid
, WERROR
*errcode
)
2236 const char* sharename
= lp_const_servicename(snum
);
2237 struct printjob
*pjob
;
2239 struct printif
*current_printif
= get_printer_fns( snum
);
2241 pjob
= print_job_find(sharename
, jobid
);
2243 if (!pjob
|| !server_info
) {
2244 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2245 (unsigned int)jobid
));
2249 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2250 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2251 (int)pjob
->sysjob
, (unsigned int)jobid
));
2255 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2256 !print_access_check(server_info
, msg_ctx
, snum
,
2257 JOB_ACCESS_ADMINISTER
)) {
2258 DEBUG(3, ("pause denied by security descriptor\n"));
2260 /* BEGIN_ADMIN_LOG */
2261 sys_adminlog( LOG_ERR
,
2262 "Permission denied-- user not allowed to delete, \
2263 pause, or resume print job. User name: %s. Printer name: %s.",
2264 uidtoname(server_info
->unix_token
->uid
),
2265 lp_printername(snum
) );
2268 *errcode
= WERR_ACCESS_DENIED
;
2272 /* need to pause the spooled entry */
2273 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
2276 *errcode
= WERR_INVALID_PARAM
;
2280 /* force update the database */
2281 print_cache_flush(lp_const_servicename(snum
));
2283 /* Send a printer notify message */
2285 notify_job_status(server_event_context(), msg_ctx
, sharename
, jobid
,
2288 /* how do we tell if this succeeded? */
2293 /****************************************************************************
2295 ****************************************************************************/
2297 bool print_job_resume(const struct auth_session_info
*server_info
,
2298 struct messaging_context
*msg_ctx
,
2299 int snum
, uint32 jobid
, WERROR
*errcode
)
2301 const char *sharename
= lp_const_servicename(snum
);
2302 struct printjob
*pjob
;
2304 struct printif
*current_printif
= get_printer_fns( snum
);
2306 pjob
= print_job_find(sharename
, jobid
);
2308 if (!pjob
|| !server_info
) {
2309 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2310 (unsigned int)jobid
));
2314 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2315 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2316 (int)pjob
->sysjob
, (unsigned int)jobid
));
2320 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2321 !print_access_check(server_info
, msg_ctx
, snum
,
2322 JOB_ACCESS_ADMINISTER
)) {
2323 DEBUG(3, ("resume denied by security descriptor\n"));
2324 *errcode
= WERR_ACCESS_DENIED
;
2326 /* BEGIN_ADMIN_LOG */
2327 sys_adminlog( LOG_ERR
,
2328 "Permission denied-- user not allowed to delete, \
2329 pause, or resume print job. User name: %s. Printer name: %s.",
2330 uidtoname(server_info
->unix_token
->uid
),
2331 lp_printername(snum
) );
2336 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
2339 *errcode
= WERR_INVALID_PARAM
;
2343 /* force update the database */
2344 print_cache_flush(lp_const_servicename(snum
));
2346 /* Send a printer notify message */
2348 notify_job_status(server_event_context(), msg_ctx
, sharename
, jobid
,
2354 /****************************************************************************
2355 Write to a print file.
2356 ****************************************************************************/
2358 ssize_t
print_job_write(struct tevent_context
*ev
,
2359 struct messaging_context
*msg_ctx
,
2360 int snum
, uint32 jobid
, const char *buf
, size_t size
)
2362 const char* sharename
= lp_const_servicename(snum
);
2363 ssize_t return_code
;
2364 struct printjob
*pjob
;
2366 pjob
= print_job_find(sharename
, jobid
);
2370 /* don't allow another process to get this info - it is meaningless */
2371 if (pjob
->pid
!= getpid())
2374 /* if SMBD is spooling this can't be allowed */
2375 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2379 return_code
= write_data(pjob
->fd
, buf
, size
);
2381 if (return_code
>0) {
2383 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2388 /****************************************************************************
2389 Get the queue status - do not update if db is out of date.
2390 ****************************************************************************/
2392 static int get_queue_status(const char* sharename
, print_status_struct
*status
)
2396 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2400 ZERO_STRUCTP(status
);
2407 fstr_sprintf(keystr
, "STATUS/%s", sharename
);
2408 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data(keystr
));
2410 if (data
.dsize
== sizeof(print_status_struct
))
2411 /* this memcpy is ok since the status struct was
2412 not packed before storing it in the tdb */
2413 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
2414 SAFE_FREE(data
.dptr
);
2417 len
= tdb_fetch_int32(pdb
->tdb
, "INFO/total_jobs");
2418 release_print_db(pdb
);
2419 return (len
== -1 ? 0 : len
);
2422 /****************************************************************************
2423 Determine the number of jobs in a queue.
2424 ****************************************************************************/
2426 int print_queue_length(struct messaging_context
*msg_ctx
, int snum
,
2427 print_status_struct
*pstatus
)
2429 const char* sharename
= lp_const_servicename( snum
);
2430 print_status_struct status
;
2433 ZERO_STRUCT( status
);
2435 /* make sure the database is up to date */
2436 if (print_cache_expired(lp_const_servicename(snum
), True
))
2437 print_queue_update(msg_ctx
, snum
, False
);
2439 /* also fetch the queue status */
2440 memset(&status
, 0, sizeof(status
));
2441 len
= get_queue_status(sharename
, &status
);
2449 /***************************************************************************
2450 Allocate a jobid. Hold the lock for as short a time as possible.
2451 ***************************************************************************/
2453 static WERROR
allocate_print_jobid(struct tdb_print_db
*pdb
, int snum
,
2454 const char *sharename
, uint32
*pjobid
)
2458 enum TDB_ERROR terr
;
2461 *pjobid
= (uint32
)-1;
2463 for (i
= 0; i
< 3; i
++) {
2464 /* Lock the database - only wait 20 seconds. */
2465 ret
= tdb_lock_bystring_with_timeout(pdb
->tdb
,
2466 "INFO/nextjob", 20);
2468 DEBUG(0, ("allocate_print_jobid: "
2469 "Failed to lock printing database %s\n",
2471 terr
= tdb_error(pdb
->tdb
);
2472 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2475 if (!tdb_fetch_uint32(pdb
->tdb
, "INFO/nextjob", &jobid
)) {
2476 terr
= tdb_error(pdb
->tdb
);
2477 if (terr
!= TDB_ERR_NOEXIST
) {
2478 DEBUG(0, ("allocate_print_jobid: "
2479 "Failed to fetch INFO/nextjob "
2480 "for print queue %s\n", sharename
));
2481 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2482 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2484 DEBUG(10, ("allocate_print_jobid: "
2485 "No existing jobid in %s\n", sharename
));
2489 DEBUG(10, ("allocate_print_jobid: "
2490 "Read jobid %u from %s\n", jobid
, sharename
));
2492 jobid
= NEXT_JOBID(jobid
);
2494 ret
= tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
);
2496 terr
= tdb_error(pdb
->tdb
);
2497 DEBUG(3, ("allocate_print_jobid: "
2498 "Failed to store INFO/nextjob.\n"));
2499 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2500 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2503 /* We've finished with the INFO/nextjob lock. */
2504 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2506 if (!print_job_exists(sharename
, jobid
)) {
2509 DEBUG(10, ("allocate_print_jobid: "
2510 "Found jobid %u in %s\n", jobid
, sharename
));
2514 DEBUG(0, ("allocate_print_jobid: "
2515 "Failed to allocate a print job for queue %s\n",
2517 /* Probably full... */
2518 return WERR_NO_SPOOL_SPACE
;
2521 /* Store a dummy placeholder. */
2527 if (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), dum
,
2529 DEBUG(3, ("allocate_print_jobid: "
2530 "jobid (%d) failed to store placeholder.\n",
2532 terr
= tdb_error(pdb
->tdb
);
2533 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2541 /***************************************************************************
2542 Append a jobid to the 'jobs added' list.
2543 ***************************************************************************/
2545 static bool add_to_jobs_added(struct tdb_print_db
*pdb
, uint32 jobid
)
2550 SIVAL(&store_jobid
, 0, jobid
);
2551 data
.dptr
= (uint8
*)&store_jobid
;
2554 DEBUG(10,("add_to_jobs_added: Added jobid %u\n", (unsigned int)jobid
));
2556 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_added"),
2561 /***************************************************************************
2562 Do all checks needed to determine if we can start a job.
2563 ***************************************************************************/
2565 static WERROR
print_job_checks(const struct auth_session_info
*server_info
,
2566 struct messaging_context
*msg_ctx
,
2567 int snum
, int *njobs
)
2569 const char *sharename
= lp_const_servicename(snum
);
2570 uint64_t dspace
, dsize
;
2574 if (!print_access_check(server_info
, msg_ctx
, snum
,
2575 PRINTER_ACCESS_USE
)) {
2576 DEBUG(3, ("print_job_checks: "
2577 "job start denied by security descriptor\n"));
2578 return WERR_ACCESS_DENIED
;
2581 if (!print_time_access_check(server_info
, msg_ctx
, sharename
)) {
2582 DEBUG(3, ("print_job_checks: "
2583 "job start denied by time check\n"));
2584 return WERR_ACCESS_DENIED
;
2587 /* see if we have sufficient disk space */
2588 if (lp_minprintspace(snum
)) {
2589 minspace
= lp_minprintspace(snum
);
2590 ret
= sys_fsusage(lp_pathname(snum
), &dspace
, &dsize
);
2591 if (ret
== 0 && dspace
< 2*minspace
) {
2592 DEBUG(3, ("print_job_checks: "
2593 "disk space check failed.\n"));
2594 return WERR_NO_SPOOL_SPACE
;
2598 /* for autoloaded printers, check that the printcap entry still exists */
2599 if (lp_autoloaded(snum
) && !pcap_printername_ok(sharename
)) {
2600 DEBUG(3, ("print_job_checks: printer name %s check failed.\n",
2602 return WERR_ACCESS_DENIED
;
2605 /* Insure the maximum queue size is not violated */
2606 *njobs
= print_queue_length(msg_ctx
, snum
, NULL
);
2607 if (*njobs
> lp_maxprintjobs(snum
)) {
2608 DEBUG(3, ("print_job_checks: Queue %s number of jobs (%d) "
2609 "larger than max printjobs per queue (%d).\n",
2610 sharename
, *njobs
, lp_maxprintjobs(snum
)));
2611 return WERR_NO_SPOOL_SPACE
;
2617 /***************************************************************************
2619 ***************************************************************************/
2621 static WERROR
print_job_spool_file(int snum
, uint32_t jobid
,
2622 const char *output_file
,
2623 struct printjob
*pjob
)
2630 /* if this file is within the printer path, it means that smbd
2631 * is spooling it and will pass us control when it is finished.
2632 * Verify that the file name is ok, within path, and it is
2633 * already already there */
2635 path
= lp_pathname(snum
);
2637 if (strncmp(output_file
, path
, len
) == 0 &&
2638 (output_file
[len
- 1] == '/' || output_file
[len
] == '/')) {
2640 /* verify path is not too long */
2641 if (strlen(output_file
) >= sizeof(pjob
->filename
)) {
2642 return WERR_INVALID_NAME
;
2645 /* verify that the file exists */
2646 if (sys_stat(output_file
, &st
, false) != 0) {
2647 return WERR_INVALID_NAME
;
2650 fstrcpy(pjob
->filename
, output_file
);
2652 DEBUG(3, ("print_job_spool_file:"
2653 "External spooling activated"));
2655 /* we do not open the file until spooling is done */
2657 pjob
->status
= PJOB_SMBD_SPOOLING
;
2663 slprintf(pjob
->filename
, sizeof(pjob
->filename
)-1,
2664 "%s/%s%.8u.XXXXXX", lp_pathname(snum
),
2665 PRINT_SPOOL_PREFIX
, (unsigned int)jobid
);
2666 pjob
->fd
= mkstemp(pjob
->filename
);
2668 if (pjob
->fd
== -1) {
2669 werr
= map_werror_from_unix(errno
);
2670 if (W_ERROR_EQUAL(werr
, WERR_ACCESS_DENIED
)) {
2671 /* Common setup error, force a report. */
2672 DEBUG(0, ("print_job_spool_file: "
2673 "insufficient permissions to open spool "
2674 "file %s.\n", pjob
->filename
));
2676 /* Normal case, report at level 3 and above. */
2677 DEBUG(3, ("print_job_spool_file: "
2678 "can't open spool file %s\n",
2687 /***************************************************************************
2688 Start spooling a job - return the jobid.
2689 ***************************************************************************/
2691 WERROR
print_job_start(const struct auth_session_info
*server_info
,
2692 struct messaging_context
*msg_ctx
,
2693 const char *clientmachine
,
2694 int snum
, const char *docname
, const char *filename
,
2695 struct spoolss_DeviceMode
*devmode
, uint32_t *_jobid
)
2699 struct printjob pjob
;
2700 const char *sharename
= lp_const_servicename(snum
);
2701 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2706 return WERR_INTERNAL_DB_CORRUPTION
;
2709 path
= lp_pathname(snum
);
2711 werr
= print_job_checks(server_info
, msg_ctx
, snum
, &njobs
);
2712 if (!W_ERROR_IS_OK(werr
)) {
2713 release_print_db(pdb
);
2717 DEBUG(10, ("print_job_start: "
2718 "Queue %s number of jobs (%d), max printjobs = %d\n",
2719 sharename
, njobs
, lp_maxprintjobs(snum
)));
2721 werr
= allocate_print_jobid(pdb
, snum
, sharename
, &jobid
);
2722 if (!W_ERROR_IS_OK(werr
)) {
2726 /* create the database entry */
2730 pjob
.pid
= getpid();
2734 pjob
.starttime
= time(NULL
);
2735 pjob
.status
= LPQ_SPOOLING
;
2737 pjob
.spooled
= False
;
2739 pjob
.devmode
= devmode
;
2741 fstrcpy(pjob
.jobname
, docname
);
2743 fstrcpy(pjob
.clientmachine
, clientmachine
);
2745 fstrcpy(pjob
.user
, lp_printjob_username(snum
));
2746 standard_sub_advanced(sharename
, server_info
->unix_info
->sanitized_username
,
2747 path
, server_info
->unix_token
->gid
,
2748 server_info
->unix_info
->sanitized_username
,
2749 server_info
->info
->domain_name
,
2750 pjob
.user
, sizeof(pjob
.user
));
2752 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
2754 /* we have a job entry - now create the spool file */
2755 werr
= print_job_spool_file(snum
, jobid
, filename
, &pjob
);
2756 if (!W_ERROR_IS_OK(werr
)) {
2760 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, &pjob
);
2762 /* Update the 'jobs added' entry used by print_queue_status. */
2763 add_to_jobs_added(pdb
, jobid
);
2765 /* Ensure we keep a rough count of the number of total jobs... */
2766 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, 1);
2768 release_print_db(pdb
);
2775 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2778 release_print_db(pdb
);
2780 DEBUG(3, ("print_job_start: returning fail. "
2781 "Error = %s\n", win_errstr(werr
)));
2785 /****************************************************************************
2786 Update the number of pages spooled to jobid
2787 ****************************************************************************/
2789 void print_job_endpage(struct messaging_context
*msg_ctx
,
2790 int snum
, uint32 jobid
)
2792 const char* sharename
= lp_const_servicename(snum
);
2793 struct printjob
*pjob
;
2795 pjob
= print_job_find(sharename
, jobid
);
2798 /* don't allow another process to get this info - it is meaningless */
2799 if (pjob
->pid
!= getpid())
2803 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, pjob
);
2806 /****************************************************************************
2807 Print a file - called on closing the file. This spools the job.
2808 If normal close is false then we're tearing down the jobs - treat as an
2810 ****************************************************************************/
2812 NTSTATUS
print_job_end(struct messaging_context
*msg_ctx
, int snum
,
2813 uint32 jobid
, enum file_close_type close_type
)
2815 const char* sharename
= lp_const_servicename(snum
);
2816 struct printjob
*pjob
;
2818 SMB_STRUCT_STAT sbuf
;
2819 struct printif
*current_printif
= get_printer_fns( snum
);
2820 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2822 pjob
= print_job_find(sharename
, jobid
);
2825 return NT_STATUS_PRINT_CANCELLED
;
2828 if (pjob
->spooled
|| pjob
->pid
!= getpid()) {
2829 return NT_STATUS_ACCESS_DENIED
;
2832 if (close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) {
2833 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2834 /* take over the file now, smbd is done */
2835 if (sys_stat(pjob
->filename
, &sbuf
, false) != 0) {
2836 status
= map_nt_error_from_unix(errno
);
2837 DEBUG(3, ("print_job_end: "
2838 "stat file failed for jobid %d\n",
2843 pjob
->status
= LPQ_SPOOLING
;
2847 if ((sys_fstat(pjob
->fd
, &sbuf
, false) != 0)) {
2848 status
= map_nt_error_from_unix(errno
);
2850 DEBUG(3, ("print_job_end: "
2851 "stat file failed for jobid %d\n",
2859 pjob
->size
= sbuf
.st_ex_size
;
2863 * Not a normal close, something has gone wrong. Cleanup.
2865 if (pjob
->fd
!= -1) {
2871 /* Technically, this is not quite right. If the printer has a separator
2872 * page turned on, the NT spooler prints the separator page even if the
2873 * print job is 0 bytes. 010215 JRR */
2874 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
2875 /* don't bother spooling empty files or something being deleted. */
2876 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2877 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
2878 unlink(pjob
->filename
);
2879 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2880 return NT_STATUS_OK
;
2883 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
2886 status
= NT_STATUS_PRINT_CANCELLED
;
2890 /* The print job has been successfully handed over to the back-end */
2892 pjob
->spooled
= True
;
2893 pjob
->status
= LPQ_QUEUED
;
2894 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, pjob
);
2896 /* make sure the database is up to date */
2897 if (print_cache_expired(lp_const_servicename(snum
), True
))
2898 print_queue_update(msg_ctx
, snum
, False
);
2900 return NT_STATUS_OK
;
2904 /* The print job was not successfully started. Cleanup */
2905 /* Still need to add proper error return propagation! 010122:JRR */
2907 unlink(pjob
->filename
);
2908 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2912 /****************************************************************************
2913 Get a snapshot of jobs in the system without traversing.
2914 ****************************************************************************/
2916 static bool get_stored_queue_info(struct messaging_context
*msg_ctx
,
2917 struct tdb_print_db
*pdb
, int snum
,
2918 int *pcount
, print_queue_struct
**ppqueue
)
2920 TDB_DATA data
, cgdata
, jcdata
;
2921 print_queue_struct
*queue
= NULL
;
2923 uint32 extra_count
= 0;
2924 uint32_t changed_count
= 0;
2925 int total_count
= 0;
2928 int max_reported_jobs
= lp_max_reported_jobs(snum
);
2930 const char* sharename
= lp_servicename(snum
);
2932 /* make sure the database is up to date */
2933 if (print_cache_expired(lp_const_servicename(snum
), True
))
2934 print_queue_update(msg_ctx
, snum
, False
);
2940 ZERO_STRUCT(cgdata
);
2942 /* Get the stored queue data. */
2943 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"));
2945 if (data
.dptr
&& data
.dsize
>= sizeof(qcount
))
2946 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "d", &qcount
);
2948 /* Get the added jobs list. */
2949 cgdata
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_added"));
2950 if (cgdata
.dptr
!= NULL
&& (cgdata
.dsize
% 4 == 0))
2951 extra_count
= cgdata
.dsize
/4;
2953 /* Get the changed jobs list. */
2954 jcdata
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
2955 if (jcdata
.dptr
!= NULL
&& (jcdata
.dsize
% 4 == 0))
2956 changed_count
= jcdata
.dsize
/ 4;
2958 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount
, (unsigned int)extra_count
));
2960 /* Allocate the queue size. */
2961 if (qcount
== 0 && extra_count
== 0)
2964 if ((queue
= SMB_MALLOC_ARRAY(print_queue_struct
, qcount
+ extra_count
)) == NULL
)
2967 /* Retrieve the linearised queue data. */
2969 for( i
= 0; i
< qcount
; i
++) {
2970 uint32 qjob
, qsize
, qpage_count
, qstatus
, qpriority
, qtime
;
2971 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
2980 queue
[i
].sysjob
= qjob
;
2981 queue
[i
].size
= qsize
;
2982 queue
[i
].page_count
= qpage_count
;
2983 queue
[i
].status
= qstatus
;
2984 queue
[i
].priority
= qpriority
;
2985 queue
[i
].time
= qtime
;
2988 total_count
= qcount
;
2990 /* Add new jobids to the queue. */
2991 for( i
= 0; i
< extra_count
; i
++) {
2993 struct printjob
*pjob
;
2995 jobid
= IVAL(cgdata
.dptr
, i
*4);
2996 DEBUG(5,("get_stored_queue_info: added job = %u\n", (unsigned int)jobid
));
2997 pjob
= print_job_find(lp_const_servicename(snum
), jobid
);
2999 DEBUG(5,("get_stored_queue_info: failed to find added job = %u\n", (unsigned int)jobid
));
3000 remove_from_jobs_added(sharename
, jobid
);
3004 queue
[total_count
].sysjob
= jobid
;
3005 queue
[total_count
].size
= pjob
->size
;
3006 queue
[total_count
].page_count
= pjob
->page_count
;
3007 queue
[total_count
].status
= pjob
->status
;
3008 queue
[total_count
].priority
= 1;
3009 queue
[total_count
].time
= pjob
->starttime
;
3010 fstrcpy(queue
[total_count
].fs_user
, pjob
->user
);
3011 fstrcpy(queue
[total_count
].fs_file
, pjob
->jobname
);
3015 /* Update the changed jobids. */
3016 for (i
= 0; i
< changed_count
; i
++) {
3017 uint32_t jobid
= IVAL(jcdata
.dptr
, i
* 4);
3021 for (j
= 0; j
< total_count
; j
++) {
3022 if (queue
[j
].sysjob
== jobid
) {
3029 struct printjob
*pjob
;
3031 DEBUG(5,("get_stored_queue_info: changed job: %u\n",
3032 (unsigned int) jobid
));
3034 pjob
= print_job_find(sharename
, jobid
);
3036 DEBUG(5,("get_stored_queue_info: failed to find "
3037 "changed job = %u\n",
3038 (unsigned int) jobid
));
3039 remove_from_jobs_changed(sharename
, jobid
);
3043 queue
[j
].sysjob
= jobid
;
3044 queue
[j
].size
= pjob
->size
;
3045 queue
[j
].page_count
= pjob
->page_count
;
3046 queue
[j
].status
= pjob
->status
;
3047 queue
[j
].priority
= 1;
3048 queue
[j
].time
= pjob
->starttime
;
3049 fstrcpy(queue
[j
].fs_user
, pjob
->user
);
3050 fstrcpy(queue
[j
].fs_file
, pjob
->jobname
);
3052 DEBUG(5,("get_stored_queue_info: updated queue[%u], jobid: %u, jobname: %s\n",
3053 (unsigned int) j
, (unsigned int) jobid
, pjob
->jobname
));
3056 remove_from_jobs_changed(sharename
, jobid
);
3059 /* Sort the queue by submission time otherwise they are displayed
3062 TYPESAFE_QSORT(queue
, total_count
, printjob_comp
);
3064 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count
));
3066 if (max_reported_jobs
&& total_count
> max_reported_jobs
)
3067 total_count
= max_reported_jobs
;
3070 *pcount
= total_count
;
3076 SAFE_FREE(data
.dptr
);
3077 SAFE_FREE(cgdata
.dptr
);
3081 /****************************************************************************
3082 Get a printer queue listing.
3083 set queue = NULL and status = NULL if you just want to update the cache
3084 ****************************************************************************/
3086 int print_queue_status(struct messaging_context
*msg_ctx
, int snum
,
3087 print_queue_struct
**ppqueue
,
3088 print_status_struct
*status
)
3092 const char *sharename
;
3093 struct tdb_print_db
*pdb
;
3096 /* make sure the database is up to date */
3098 if (print_cache_expired(lp_const_servicename(snum
), True
))
3099 print_queue_update(msg_ctx
, snum
, False
);
3101 /* return if we are done */
3102 if ( !ppqueue
|| !status
)
3106 sharename
= lp_const_servicename(snum
);
3107 pdb
= get_print_db_byname(sharename
);
3113 * Fetch the queue status. We must do this first, as there may
3114 * be no jobs in the queue.
3117 ZERO_STRUCTP(status
);
3118 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
3119 key
= string_tdb_data(keystr
);
3121 data
= tdb_fetch_compat(pdb
->tdb
, key
);
3123 if (data
.dsize
== sizeof(*status
)) {
3124 /* this memcpy is ok since the status struct was
3125 not packed before storing it in the tdb */
3126 memcpy(status
, data
.dptr
, sizeof(*status
));
3128 SAFE_FREE(data
.dptr
);
3132 * Now, fetch the print queue information. We first count the number
3133 * of entries, and then only retrieve the queue if necessary.
3136 if (!get_stored_queue_info(msg_ctx
, pdb
, snum
, &count
, ppqueue
)) {
3137 release_print_db(pdb
);
3141 release_print_db(pdb
);
3145 /****************************************************************************
3147 ****************************************************************************/
3149 WERROR
print_queue_pause(const struct auth_session_info
*server_info
,
3150 struct messaging_context
*msg_ctx
, int snum
)
3153 struct printif
*current_printif
= get_printer_fns( snum
);
3155 if (!print_access_check(server_info
, msg_ctx
, snum
,
3156 PRINTER_ACCESS_ADMINISTER
)) {
3157 return WERR_ACCESS_DENIED
;
3163 ret
= (*(current_printif
->queue_pause
))(snum
);
3168 return WERR_INVALID_PARAM
;
3171 /* force update the database */
3172 print_cache_flush(lp_const_servicename(snum
));
3174 /* Send a printer notify message */
3176 notify_printer_status(server_event_context(), msg_ctx
, snum
,
3177 PRINTER_STATUS_PAUSED
);
3182 /****************************************************************************
3184 ****************************************************************************/
3186 WERROR
print_queue_resume(const struct auth_session_info
*server_info
,
3187 struct messaging_context
*msg_ctx
, int snum
)
3190 struct printif
*current_printif
= get_printer_fns( snum
);
3192 if (!print_access_check(server_info
, msg_ctx
, snum
,
3193 PRINTER_ACCESS_ADMINISTER
)) {
3194 return WERR_ACCESS_DENIED
;
3199 ret
= (*(current_printif
->queue_resume
))(snum
);
3204 return WERR_INVALID_PARAM
;
3207 /* make sure the database is up to date */
3208 if (print_cache_expired(lp_const_servicename(snum
), True
))
3209 print_queue_update(msg_ctx
, snum
, True
);
3211 /* Send a printer notify message */
3213 notify_printer_status(server_event_context(), msg_ctx
, snum
,
3219 /****************************************************************************
3220 Purge a queue - implemented by deleting all jobs that we can delete.
3221 ****************************************************************************/
3223 WERROR
print_queue_purge(const struct auth_session_info
*server_info
,
3224 struct messaging_context
*msg_ctx
, int snum
)
3226 print_queue_struct
*queue
;
3227 print_status_struct status
;
3231 /* Force and update so the count is accurate (i.e. not a cached count) */
3232 print_queue_update(msg_ctx
, snum
, True
);
3234 can_job_admin
= print_access_check(server_info
,
3237 JOB_ACCESS_ADMINISTER
);
3238 njobs
= print_queue_status(msg_ctx
, snum
, &queue
, &status
);
3240 if ( can_job_admin
)
3243 for (i
=0;i
<njobs
;i
++) {
3244 bool owner
= is_owner(server_info
, lp_const_servicename(snum
),
3247 if (owner
|| can_job_admin
) {
3248 print_job_delete1(server_event_context(), msg_ctx
,
3249 snum
, queue
[i
].sysjob
);
3253 if ( can_job_admin
)
3256 /* update the cache */
3257 print_queue_update(msg_ctx
, snum
, True
);