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 /****************************************************************************
512 This is a *horribly expensive call as we have to iterate through all the
513 current printer tdb's. Don't do this often ! JRA.
514 ****************************************************************************/
516 uint32
sysjob_to_jobid(int unix_jobid
)
518 int services
= lp_numservices();
520 struct unixjob_traverse_state state
;
522 state
.sysjob
= unix_jobid
;
523 state
.sysjob_to_jobid_value
= (uint32
)-1;
525 for (snum
= 0; snum
< services
; snum
++) {
526 struct tdb_print_db
*pdb
;
527 if (!lp_print_ok(snum
))
529 pdb
= get_print_db_byname(lp_const_servicename(snum
));
533 tdb_traverse(pdb
->tdb
, unixjob_traverse_fn
, &state
);
534 release_print_db(pdb
);
535 if (state
.sysjob_to_jobid_value
!= (uint32
)-1)
536 return state
.sysjob_to_jobid_value
;
541 /****************************************************************************
542 Send notifications based on what has changed after a pjob_store.
543 ****************************************************************************/
545 static const struct {
547 uint32_t spoolss_status
;
548 } lpq_to_spoolss_status_map
[] = {
549 { LPQ_QUEUED
, JOB_STATUS_QUEUED
},
550 { LPQ_PAUSED
, JOB_STATUS_PAUSED
},
551 { LPQ_SPOOLING
, JOB_STATUS_SPOOLING
},
552 { LPQ_PRINTING
, JOB_STATUS_PRINTING
},
553 { LPQ_DELETING
, JOB_STATUS_DELETING
},
554 { LPQ_OFFLINE
, JOB_STATUS_OFFLINE
},
555 { LPQ_PAPEROUT
, JOB_STATUS_PAPEROUT
},
556 { LPQ_PRINTED
, JOB_STATUS_PRINTED
},
557 { LPQ_DELETED
, JOB_STATUS_DELETED
},
558 { LPQ_BLOCKED
, JOB_STATUS_BLOCKED_DEVQ
},
559 { LPQ_USER_INTERVENTION
, JOB_STATUS_USER_INTERVENTION
},
563 /* Convert a lpq status value stored in printing.tdb into the
564 appropriate win32 API constant. */
566 static uint32
map_to_spoolss_status(uint32 lpq_status
)
570 while (lpq_to_spoolss_status_map
[i
].lpq_status
!= -1) {
571 if (lpq_to_spoolss_status_map
[i
].lpq_status
== lpq_status
)
572 return lpq_to_spoolss_status_map
[i
].spoolss_status
;
579 /***************************************************************************
580 Append a jobid to the 'jobs changed' list.
581 ***************************************************************************/
583 static bool add_to_jobs_changed(struct tdb_print_db
*pdb
, uint32_t jobid
)
586 uint32_t store_jobid
;
588 SIVAL(&store_jobid
, 0, jobid
);
589 data
.dptr
= (uint8
*) &store_jobid
;
592 DEBUG(10,("add_to_jobs_added: Added jobid %u\n", (unsigned int)jobid
));
594 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"),
598 /***************************************************************************
599 Remove a jobid from the 'jobs changed' list.
600 ***************************************************************************/
602 static bool remove_from_jobs_changed(const char* sharename
, uint32_t jobid
)
604 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
608 bool gotlock
= False
;
616 key
= string_tdb_data("INFO/jobs_changed");
618 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) != 0)
623 data
= tdb_fetch_compat(pdb
->tdb
, key
);
625 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
628 job_count
= data
.dsize
/ 4;
629 for (i
= 0; i
< job_count
; i
++) {
632 ch_jobid
= IVAL(data
.dptr
, i
*4);
633 if (ch_jobid
== jobid
) {
634 if (i
< job_count
-1 )
635 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
637 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) != 0)
647 tdb_chainunlock(pdb
->tdb
, key
);
648 SAFE_FREE(data
.dptr
);
649 release_print_db(pdb
);
651 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid
));
653 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid
));
657 static void pjob_store_notify(struct tevent_context
*ev
,
658 struct messaging_context
*msg_ctx
,
659 const char* sharename
, uint32 jobid
,
660 struct printjob
*old_data
,
661 struct printjob
*new_data
,
664 bool new_job
= false;
665 bool changed
= false;
667 if (old_data
== NULL
) {
671 /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
672 NOTIFY_INFO_DATA buffer, we *have* to send the job submission
673 time first or else we'll end up with potential alignment
674 errors. I don't think the systemtime should be spooled as
675 a string, but this gets us around that error.
676 --jerry (i'll feel dirty for this) */
679 notify_job_submitted(ev
, msg_ctx
,
680 sharename
, jobid
, new_data
->starttime
);
681 notify_job_username(ev
, msg_ctx
,
682 sharename
, jobid
, new_data
->user
);
683 notify_job_name(ev
, msg_ctx
,
684 sharename
, jobid
, new_data
->jobname
);
685 notify_job_status(ev
, msg_ctx
,
686 sharename
, jobid
, map_to_spoolss_status(new_data
->status
));
687 notify_job_total_bytes(ev
, msg_ctx
,
688 sharename
, jobid
, new_data
->size
);
689 notify_job_total_pages(ev
, msg_ctx
,
690 sharename
, jobid
, new_data
->page_count
);
692 if (!strequal(old_data
->jobname
, new_data
->jobname
)) {
693 notify_job_name(ev
, msg_ctx
, sharename
,
694 jobid
, new_data
->jobname
);
698 if (old_data
->status
!= new_data
->status
) {
699 notify_job_status(ev
, msg_ctx
,
701 map_to_spoolss_status(new_data
->status
));
704 if (old_data
->size
!= new_data
->size
) {
705 notify_job_total_bytes(ev
, msg_ctx
,
706 sharename
, jobid
, new_data
->size
);
709 if (old_data
->page_count
!= new_data
->page_count
) {
710 notify_job_total_pages(ev
, msg_ctx
,
712 new_data
->page_count
);
719 /****************************************************************************
720 Store a job structure back to the database.
721 ****************************************************************************/
723 static bool pjob_store(struct tevent_context
*ev
,
724 struct messaging_context
*msg_ctx
,
725 const char* sharename
, uint32 jobid
,
726 struct printjob
*pjob
)
729 TDB_DATA old_data
, new_data
;
731 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
733 int len
, newlen
, buflen
;
741 old_data
= tdb_fetch_compat(pdb
->tdb
, print_key(jobid
, &tmp
));
743 /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
750 len
+= tdb_pack(buf
+len
, buflen
-len
, "ddddddddddfffff",
753 (uint32
)pjob
->sysjob
,
755 (uint32
)pjob
->starttime
,
756 (uint32
)pjob
->status
,
758 (uint32
)pjob
->page_count
,
759 (uint32
)pjob
->spooled
,
760 (uint32
)pjob
->smbjob
,
767 len
+= pack_devicemode(pjob
->devmode
, buf
+len
, buflen
-len
);
770 buf
= (uint8
*)SMB_REALLOC(buf
, len
);
772 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
777 } while ( buflen
!= len
);
783 new_data
.dsize
= len
;
784 ret
= (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), new_data
,
787 /* Send notify updates for what has changed */
790 bool changed
= false;
791 struct printjob old_pjob
;
793 if ( old_data
.dsize
)
795 if ( unpack_pjob( old_data
.dptr
, old_data
.dsize
, &old_pjob
) != -1 )
797 pjob_store_notify(server_event_context(),
799 sharename
, jobid
, &old_pjob
,
802 talloc_free(old_pjob
.devmode
);
805 add_to_jobs_changed(pdb
, jobid
);
812 pjob_store_notify(server_event_context(), msg_ctx
,
813 sharename
, jobid
, NULL
, pjob
,
818 release_print_db(pdb
);
820 SAFE_FREE( old_data
.dptr
);
826 /****************************************************************************
827 Remove a job structure from the database.
828 ****************************************************************************/
830 static void pjob_delete(struct tevent_context
*ev
,
831 struct messaging_context
*msg_ctx
,
832 const char* sharename
, uint32 jobid
)
835 struct printjob
*pjob
;
836 uint32 job_status
= 0;
837 struct tdb_print_db
*pdb
;
839 pdb
= get_print_db_byname( sharename
);
844 pjob
= print_job_find( sharename
, jobid
);
847 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
848 (unsigned int)jobid
));
849 release_print_db(pdb
);
853 /* We must cycle through JOB_STATUS_DELETING and
854 JOB_STATUS_DELETED for the port monitor to delete the job
857 job_status
= JOB_STATUS_DELETING
|JOB_STATUS_DELETED
;
858 notify_job_status(ev
, msg_ctx
, sharename
, jobid
, job_status
);
860 /* Remove from printing.tdb */
862 tdb_delete(pdb
->tdb
, print_key(jobid
, &tmp
));
863 remove_from_jobs_added(sharename
, jobid
);
864 release_print_db( pdb
);
865 rap_jobid_delete(sharename
, jobid
);
868 /****************************************************************************
869 List a unix job in the print database.
870 ****************************************************************************/
872 static void print_unix_job(struct tevent_context
*ev
,
873 struct messaging_context
*msg_ctx
,
874 const char *sharename
, print_queue_struct
*q
,
877 struct printjob pj
, *old_pj
;
879 if (jobid
== (uint32
)-1)
880 jobid
= q
->job
+ UNIX_JOB_START
;
882 /* Preserve the timestamp on an existing unix print job */
884 old_pj
= print_job_find(sharename
, jobid
);
892 pj
.starttime
= old_pj
? old_pj
->starttime
: q
->time
;
893 pj
.status
= q
->status
;
896 fstrcpy(pj
.filename
, old_pj
? old_pj
->filename
: "");
897 if (jobid
< UNIX_JOB_START
) {
899 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: "Remote Downlevel Document");
902 fstrcpy(pj
.jobname
, old_pj
? old_pj
->jobname
: q
->fs_file
);
904 fstrcpy(pj
.user
, old_pj
? old_pj
->user
: q
->fs_user
);
905 fstrcpy(pj
.queuename
, old_pj
? old_pj
->queuename
: sharename
);
907 pjob_store(ev
, msg_ctx
, sharename
, jobid
, &pj
);
911 struct traverse_struct
{
912 print_queue_struct
*queue
;
913 int qcount
, snum
, maxcount
, total_jobs
;
914 const char *sharename
;
916 const char *lprm_command
;
917 struct printif
*print_if
;
918 struct tevent_context
*ev
;
919 struct messaging_context
*msg_ctx
;
922 /****************************************************************************
923 Utility fn to delete any jobs that are no longer active.
924 ****************************************************************************/
926 static int traverse_fn_delete(TDB_CONTEXT
*t
, TDB_DATA key
, TDB_DATA data
, void *state
)
928 struct traverse_struct
*ts
= (struct traverse_struct
*)state
;
929 struct printjob pjob
;
933 if ( key
.dsize
!= sizeof(jobid
) )
936 if (unpack_pjob(data
.dptr
, data
.dsize
, &pjob
) == -1)
938 talloc_free(pjob
.devmode
);
942 /* remove a unix job if it isn't in the system queue any more */
944 for (i
=0;i
<ts
->qcount
;i
++) {
945 uint32 u_jobid
= (ts
->queue
[i
].job
+ UNIX_JOB_START
);
946 if (jobid
== u_jobid
)
949 if (i
== ts
->qcount
) {
950 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
951 (unsigned int)jobid
));
952 pjob_delete(ts
->ev
, ts
->msg_ctx
,
953 ts
->sharename
, jobid
);
957 /* need to continue the the bottom of the function to
958 save the correct attributes */
961 /* maybe it hasn't been spooled yet */
963 /* if a job is not spooled and the process doesn't
964 exist then kill it. This cleans up after smbd
966 if (!process_exists_by_pid(pjob
.pid
)) {
967 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
968 (unsigned int)jobid
, (unsigned int)pjob
.pid
));
969 pjob_delete(ts
->ev
, ts
->msg_ctx
,
970 ts
->sharename
, jobid
);
976 /* this check only makes sense for jobs submitted from Windows clients */
979 for (i
=0;i
<ts
->qcount
;i
++) {
982 if ( pjob
.status
== LPQ_DELETED
)
985 curr_jobid
= print_parse_jobid(ts
->queue
[i
].fs_file
);
987 if (jobid
== curr_jobid
) {
989 /* try to clean up any jobs that need to be deleted */
991 if ( pjob
.status
== LPQ_DELETING
) {
994 result
= (*(ts
->print_if
->job_delete
))(
995 ts
->sharename
, ts
->lprm_command
, &pjob
);
998 /* if we can't delete, then reset the job status */
999 pjob
.status
= LPQ_QUEUED
;
1000 pjob_store(ts
->ev
, ts
->msg_ctx
,
1001 ts
->sharename
, jobid
, &pjob
);
1004 /* if we deleted the job, the remove the tdb record */
1007 ts
->sharename
, jobid
);
1008 pjob
.status
= LPQ_DELETED
;
1018 /* The job isn't in the system queue - we have to assume it has
1019 completed, so delete the database entry. */
1021 if (i
== ts
->qcount
) {
1023 /* A race can occur between the time a job is spooled and
1024 when it appears in the lpq output. This happens when
1025 the job is added to printing.tdb when another smbd
1026 running print_queue_update() has completed a lpq and
1027 is currently traversing the printing tdb and deleting jobs.
1028 Don't delete the job if it was submitted after the lpq_time. */
1030 if (pjob
.starttime
< ts
->lpq_time
) {
1031 DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
1032 (unsigned int)jobid
,
1033 (unsigned int)pjob
.starttime
,
1034 (unsigned int)ts
->lpq_time
));
1035 pjob_delete(ts
->ev
, ts
->msg_ctx
,
1036 ts
->sharename
, jobid
);
1042 /* Save the pjob attributes we will store.
1043 FIXME!!! This is the only place where queue->job
1044 represents the SMB jobid --jerry */
1046 ts
->queue
[i
].job
= jobid
;
1047 ts
->queue
[i
].size
= pjob
.size
;
1048 ts
->queue
[i
].page_count
= pjob
.page_count
;
1049 ts
->queue
[i
].status
= pjob
.status
;
1050 ts
->queue
[i
].priority
= 1;
1051 ts
->queue
[i
].time
= pjob
.starttime
;
1052 fstrcpy(ts
->queue
[i
].fs_user
, pjob
.user
);
1053 fstrcpy(ts
->queue
[i
].fs_file
, pjob
.jobname
);
1060 /****************************************************************************
1061 Check if the print queue has been updated recently enough.
1062 ****************************************************************************/
1064 static void print_cache_flush(const char *sharename
)
1067 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1071 slprintf(key
, sizeof(key
)-1, "CACHE/%s", sharename
);
1072 tdb_store_int32(pdb
->tdb
, key
, -1);
1073 release_print_db(pdb
);
1076 /****************************************************************************
1077 Check if someone already thinks they are doing the update.
1078 ****************************************************************************/
1080 static pid_t
get_updating_pid(const char *sharename
)
1085 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1089 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
1090 key
= string_tdb_data(keystr
);
1092 data
= tdb_fetch_compat(pdb
->tdb
, key
);
1093 release_print_db(pdb
);
1094 if (!data
.dptr
|| data
.dsize
!= sizeof(pid_t
)) {
1095 SAFE_FREE(data
.dptr
);
1099 updating_pid
= IVAL(data
.dptr
, 0);
1100 SAFE_FREE(data
.dptr
);
1102 if (process_exists_by_pid(updating_pid
))
1103 return updating_pid
;
1108 /****************************************************************************
1109 Set the fact that we're doing the update, or have finished doing the update
1111 ****************************************************************************/
1113 static void set_updating_pid(const fstring sharename
, bool updating
)
1118 pid_t updating_pid
= getpid();
1121 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1126 slprintf(keystr
, sizeof(keystr
)-1, "UPDATING/%s", sharename
);
1127 key
= string_tdb_data(keystr
);
1129 DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
1130 updating
? "" : "not ",
1134 tdb_delete(pdb
->tdb
, key
);
1135 release_print_db(pdb
);
1139 SIVAL( buffer
, 0, updating_pid
);
1141 data
.dsize
= 4; /* we always assume this is a 4 byte value */
1143 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1144 release_print_db(pdb
);
1147 /****************************************************************************
1148 Sort print jobs by submittal time.
1149 ****************************************************************************/
1151 static int printjob_comp(print_queue_struct
*j1
, print_queue_struct
*j2
)
1162 /* Sort on job start time */
1164 if (j1
->time
== j2
->time
)
1166 return (j1
->time
> j2
->time
) ? 1 : -1;
1169 /****************************************************************************
1170 Store the sorted queue representation for later portmon retrieval.
1172 ****************************************************************************/
1174 static void store_queue_struct(struct tdb_print_db
*pdb
, struct traverse_struct
*pts
)
1177 int max_reported_jobs
= lp_max_reported_jobs(pts
->snum
);
1178 print_queue_struct
*queue
= pts
->queue
;
1181 unsigned int qcount
;
1183 if (max_reported_jobs
&& (max_reported_jobs
< pts
->qcount
))
1184 pts
->qcount
= max_reported_jobs
;
1187 /* Work out the size. */
1189 data
.dsize
+= tdb_pack(NULL
, 0, "d", qcount
);
1191 for (i
= 0; i
< pts
->qcount
; i
++) {
1192 if ( queue
[i
].status
== LPQ_DELETED
)
1196 data
.dsize
+= tdb_pack(NULL
, 0, "ddddddff",
1197 (uint32
)queue
[i
].job
,
1198 (uint32
)queue
[i
].size
,
1199 (uint32
)queue
[i
].page_count
,
1200 (uint32
)queue
[i
].status
,
1201 (uint32
)queue
[i
].priority
,
1202 (uint32
)queue
[i
].time
,
1207 if ((data
.dptr
= (uint8
*)SMB_MALLOC(data
.dsize
)) == NULL
)
1211 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "d", qcount
);
1212 for (i
= 0; i
< pts
->qcount
; i
++) {
1213 if ( queue
[i
].status
== LPQ_DELETED
)
1216 len
+= tdb_pack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
1217 (uint32
)queue
[i
].job
,
1218 (uint32
)queue
[i
].size
,
1219 (uint32
)queue
[i
].page_count
,
1220 (uint32
)queue
[i
].status
,
1221 (uint32
)queue
[i
].priority
,
1222 (uint32
)queue
[i
].time
,
1227 tdb_store(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"), data
,
1229 SAFE_FREE(data
.dptr
);
1233 static TDB_DATA
get_jobs_added_data(struct tdb_print_db
*pdb
)
1239 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_added"));
1240 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0)) {
1241 SAFE_FREE(data
.dptr
);
1248 static void check_job_added(const char *sharename
, TDB_DATA data
, uint32 jobid
)
1251 unsigned int job_count
= data
.dsize
/ 4;
1253 for (i
= 0; i
< job_count
; i
++) {
1256 ch_jobid
= IVAL(data
.dptr
, i
*4);
1257 if (ch_jobid
== jobid
)
1258 remove_from_jobs_added(sharename
, jobid
);
1262 /****************************************************************************
1263 Check if the print queue has been updated recently enough.
1264 ****************************************************************************/
1266 static bool print_cache_expired(const char *sharename
, bool check_pending
)
1269 time_t last_qscan_time
, time_now
= time(NULL
);
1270 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1271 bool result
= False
;
1276 snprintf(key
, sizeof(key
), "CACHE/%s", sharename
);
1277 last_qscan_time
= (time_t)tdb_fetch_int32(pdb
->tdb
, key
);
1280 * Invalidate the queue for 3 reasons.
1281 * (1). last queue scan time == -1.
1282 * (2). Current time - last queue scan time > allowed cache time.
1283 * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1284 * This last test picks up machines for which the clock has been moved
1285 * forward, an lpq scan done and then the clock moved back. Otherwise
1286 * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1289 if (last_qscan_time
== ((time_t)-1)
1290 || (time_now
- last_qscan_time
) >= lp_lpqcachetime()
1291 || last_qscan_time
> (time_now
+ MAX_CACHE_VALID_TIME
))
1294 time_t msg_pending_time
;
1296 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1297 "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1298 sharename
, (int)last_qscan_time
, (int)time_now
,
1299 (int)lp_lpqcachetime() ));
1301 /* check if another smbd has already sent a message to update the
1302 queue. Give the pending message one minute to clear and
1303 then send another message anyways. Make sure to check for
1304 clocks that have been run forward and then back again. */
1306 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1309 && tdb_fetch_uint32( pdb
->tdb
, key
, &u
)
1310 && (msg_pending_time
=u
) > 0
1311 && msg_pending_time
<= time_now
1312 && (time_now
- msg_pending_time
) < 60 )
1314 DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
1323 release_print_db(pdb
);
1327 /****************************************************************************
1328 main work for updating the lpq cache for a printer queue
1329 ****************************************************************************/
1331 static void print_queue_update_internal( struct tevent_context
*ev
,
1332 struct messaging_context
*msg_ctx
,
1333 const char *sharename
,
1334 struct printif
*current_printif
,
1335 char *lpq_command
, char *lprm_command
)
1338 print_queue_struct
*queue
= NULL
;
1339 print_status_struct status
;
1340 print_status_struct old_status
;
1341 struct printjob
*pjob
;
1342 struct traverse_struct tstruct
;
1345 fstring keystr
, cachestr
;
1346 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1352 DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1353 sharename
, current_printif
->type
, lpq_command
));
1356 * Update the cache time FIRST ! Stops others even
1357 * attempting to get the lock and doing this
1358 * if the lpq takes a long time.
1361 slprintf(cachestr
, sizeof(cachestr
)-1, "CACHE/%s", sharename
);
1362 tdb_store_int32(pdb
->tdb
, cachestr
, (int)time(NULL
));
1364 /* get the current queue using the appropriate interface */
1365 ZERO_STRUCT(status
);
1367 qcount
= (*(current_printif
->queue_get
))(sharename
,
1368 current_printif
->type
,
1369 lpq_command
, &queue
, &status
);
1371 DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1372 qcount
, (qcount
!= 1) ? "s" : "", sharename
));
1374 /* Sort the queue by submission time otherwise they are displayed
1377 TYPESAFE_QSORT(queue
, qcount
, printjob_comp
);
1380 any job in the internal database that is marked as spooled
1381 and doesn't exist in the system queue is considered finished
1382 and removed from the database
1384 any job in the system database but not in the internal database
1385 is added as a unix job
1387 fill in any system job numbers as we go
1390 jcdata
= get_jobs_added_data(pdb
);
1392 for (i
=0; i
<qcount
; i
++) {
1393 uint32 jobid
= print_parse_jobid(queue
[i
].fs_file
);
1395 if (jobid
== (uint32
)-1) {
1396 /* assume its a unix print job */
1397 print_unix_job(ev
, msg_ctx
,
1398 sharename
, &queue
[i
], jobid
);
1402 /* we have an active SMB print job - update its status */
1403 pjob
= print_job_find(sharename
, jobid
);
1405 /* err, somethings wrong. Probably smbd was restarted
1406 with jobs in the queue. All we can do is treat them
1407 like unix jobs. Pity. */
1408 print_unix_job(ev
, msg_ctx
,
1409 sharename
, &queue
[i
], jobid
);
1413 pjob
->sysjob
= queue
[i
].job
;
1415 /* don't reset the status on jobs to be deleted */
1417 if ( pjob
->status
!= LPQ_DELETING
)
1418 pjob
->status
= queue
[i
].status
;
1420 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
1422 check_job_added(sharename
, jcdata
, jobid
);
1425 SAFE_FREE(jcdata
.dptr
);
1427 /* now delete any queued entries that don't appear in the
1429 tstruct
.queue
= queue
;
1430 tstruct
.qcount
= qcount
;
1432 tstruct
.total_jobs
= 0;
1433 tstruct
.lpq_time
= time(NULL
);
1434 tstruct
.sharename
= sharename
;
1435 tstruct
.lprm_command
= lprm_command
;
1436 tstruct
.print_if
= current_printif
;
1438 tstruct
.msg_ctx
= msg_ctx
;
1440 tdb_traverse(pdb
->tdb
, traverse_fn_delete
, (void *)&tstruct
);
1442 /* Store the linearised queue, max jobs only. */
1443 store_queue_struct(pdb
, &tstruct
);
1445 SAFE_FREE(tstruct
.queue
);
1447 DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1448 sharename
, tstruct
.total_jobs
));
1450 tdb_store_int32(pdb
->tdb
, "INFO/total_jobs", tstruct
.total_jobs
);
1452 get_queue_status(sharename
, &old_status
);
1453 if (old_status
.qcount
!= qcount
)
1454 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1455 old_status
.qcount
, qcount
, sharename
));
1457 /* store the new queue status structure */
1458 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
1459 key
= string_tdb_data(keystr
);
1461 status
.qcount
= qcount
;
1462 data
.dptr
= (uint8
*)&status
;
1463 data
.dsize
= sizeof(status
);
1464 tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
);
1467 * Update the cache time again. We want to do this call
1468 * as little as possible...
1471 slprintf(keystr
, sizeof(keystr
)-1, "CACHE/%s", sharename
);
1472 tdb_store_int32(pdb
->tdb
, keystr
, (int32
)time(NULL
));
1474 /* clear the msg pending record for this queue */
1476 snprintf(keystr
, sizeof(keystr
), "MSG_PENDING/%s", sharename
);
1478 if ( !tdb_store_uint32( pdb
->tdb
, keystr
, 0 ) ) {
1479 /* log a message but continue on */
1481 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1485 release_print_db( pdb
);
1490 /****************************************************************************
1491 Update the internal database from the system print queue for a queue.
1492 obtain a lock on the print queue before proceeding (needed when mutiple
1493 smbd processes maytry to update the lpq cache concurrently).
1494 ****************************************************************************/
1496 static void print_queue_update_with_lock( struct tevent_context
*ev
,
1497 struct messaging_context
*msg_ctx
,
1498 const char *sharename
,
1499 struct printif
*current_printif
,
1500 char *lpq_command
, char *lprm_command
)
1503 struct tdb_print_db
*pdb
;
1505 DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename
));
1506 pdb
= get_print_db_byname(sharename
);
1510 if ( !print_cache_expired(sharename
, False
) ) {
1511 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename
));
1512 release_print_db(pdb
);
1517 * Check to see if someone else is doing this update.
1518 * This is essentially a mutex on the update.
1521 if (get_updating_pid(sharename
) != -1) {
1522 release_print_db(pdb
);
1526 /* Lock the queue for the database update */
1528 slprintf(keystr
, sizeof(keystr
) - 1, "LOCK/%s", sharename
);
1529 /* Only wait 10 seconds for this. */
1530 if (tdb_lock_bystring_with_timeout(pdb
->tdb
, keystr
, 10) != 0) {
1531 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename
));
1532 release_print_db(pdb
);
1537 * Ensure that no one else got in here.
1538 * If the updating pid is still -1 then we are
1542 if (get_updating_pid(sharename
) != -1) {
1544 * Someone else is doing the update, exit.
1546 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1547 release_print_db(pdb
);
1552 * We're going to do the update ourselves.
1555 /* Tell others we're doing the update. */
1556 set_updating_pid(sharename
, True
);
1559 * Allow others to enter and notice we're doing
1563 tdb_unlock_bystring(pdb
->tdb
, keystr
);
1565 /* do the main work now */
1567 print_queue_update_internal(ev
, msg_ctx
,
1568 sharename
, current_printif
,
1569 lpq_command
, lprm_command
);
1571 /* Delete our pid from the db. */
1572 set_updating_pid(sharename
, False
);
1573 release_print_db(pdb
);
1576 /****************************************************************************
1577 this is the receive function of the background lpq updater
1578 ****************************************************************************/
1579 void print_queue_receive(struct messaging_context
*msg
,
1582 struct server_id server_id
,
1586 char *lpqcommand
= NULL
, *lprmcommand
= NULL
;
1590 len
= tdb_unpack( (uint8
*)data
->data
, data
->length
, "fdPP",
1597 SAFE_FREE(lpqcommand
);
1598 SAFE_FREE(lprmcommand
);
1599 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1603 print_queue_update_with_lock(server_event_context(), msg
, sharename
,
1604 get_printer_fns_from_type((enum printing_types
)printing_type
),
1605 lpqcommand
, lprmcommand
);
1607 SAFE_FREE(lpqcommand
);
1608 SAFE_FREE(lprmcommand
);
1612 /****************************************************************************
1613 update the internal database from the system print queue for a queue
1614 ****************************************************************************/
1616 extern pid_t background_lpq_updater_pid
;
1618 static void print_queue_update(struct messaging_context
*msg_ctx
,
1619 int snum
, bool force
)
1623 char *lpqcommand
= NULL
;
1624 char *lprmcommand
= NULL
;
1625 uint8
*buffer
= NULL
;
1628 struct tdb_print_db
*pdb
;
1630 struct printif
*current_printif
;
1631 TALLOC_CTX
*ctx
= talloc_tos();
1633 fstrcpy( sharename
, lp_const_servicename(snum
));
1635 /* don't strip out characters like '$' from the printername */
1637 lpqcommand
= talloc_string_sub2(ctx
,
1638 lp_lpqcommand(snum
),
1640 lp_printername(snum
),
1641 false, false, false);
1645 lpqcommand
= talloc_sub_advanced(ctx
,
1646 lp_servicename(snum
),
1647 current_user_info
.unix_name
,
1649 current_user
.ut
.gid
,
1650 get_current_username(),
1651 current_user_info
.domain
,
1657 lprmcommand
= talloc_string_sub2(ctx
,
1658 lp_lprmcommand(snum
),
1660 lp_printername(snum
),
1661 false, false, false);
1665 lprmcommand
= talloc_sub_advanced(ctx
,
1666 lp_servicename(snum
),
1667 current_user_info
.unix_name
,
1669 current_user
.ut
.gid
,
1670 get_current_username(),
1671 current_user_info
.domain
,
1678 * Make sure that the background queue process exists.
1679 * Otherwise just do the update ourselves
1682 if ( force
|| background_lpq_updater_pid
== -1 ) {
1683 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename
));
1684 current_printif
= get_printer_fns( snum
);
1685 print_queue_update_with_lock(server_event_context(), msg_ctx
,
1686 sharename
, current_printif
,
1687 lpqcommand
, lprmcommand
);
1692 type
= lp_printing(snum
);
1694 /* get the length */
1696 len
= tdb_pack( NULL
, 0, "fdPP",
1702 buffer
= SMB_XMALLOC_ARRAY( uint8
, len
);
1704 /* now pack the buffer */
1705 newlen
= tdb_pack( buffer
, len
, "fdPP",
1711 SMB_ASSERT( newlen
== len
);
1713 DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1714 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1715 sharename
, type
, lpqcommand
, lprmcommand
));
1717 /* here we set a msg pending record for other smbd processes
1718 to throttle the number of duplicate print_queue_update msgs
1721 pdb
= get_print_db_byname(sharename
);
1727 snprintf(key
, sizeof(key
), "MSG_PENDING/%s", sharename
);
1729 if ( !tdb_store_uint32( pdb
->tdb
, key
, time(NULL
) ) ) {
1730 /* log a message but continue on */
1732 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1736 release_print_db( pdb
);
1738 /* finally send the message */
1740 messaging_send_buf(msg_ctx
, pid_to_procid(background_lpq_updater_pid
),
1741 MSG_PRINTER_UPDATE
, (uint8
*)buffer
, len
);
1743 SAFE_FREE( buffer
);
1748 /****************************************************************************
1749 Create/Update an entry in the print tdb that will allow us to send notify
1750 updates only to interested smbd's.
1751 ****************************************************************************/
1753 bool print_notify_register_pid(int snum
)
1756 struct tdb_print_db
*pdb
= NULL
;
1757 TDB_CONTEXT
*tdb
= NULL
;
1758 const char *printername
;
1759 uint32_t mypid
= (uint32_t)getpid();
1763 /* if (snum == -1), then the change notify request was
1764 on a print server handle and we need to register on
1769 int num_services
= lp_numservices();
1772 for ( idx
=0; idx
<num_services
; idx
++ ) {
1773 if (lp_snum_ok(idx
) && lp_print_ok(idx
) )
1774 print_notify_register_pid(idx
);
1779 else /* register for a specific printer */
1781 printername
= lp_const_servicename(snum
);
1782 pdb
= get_print_db_byname(printername
);
1788 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) != 0) {
1789 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1792 release_print_db(pdb
);
1796 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1798 /* Add ourselves and increase the refcount. */
1800 for (i
= 0; i
< data
.dsize
; i
+= 8) {
1801 if (IVAL(data
.dptr
,i
) == mypid
) {
1802 uint32 new_refcount
= IVAL(data
.dptr
, i
+4) + 1;
1803 SIVAL(data
.dptr
, i
+4, new_refcount
);
1808 if (i
== data
.dsize
) {
1809 /* We weren't in the list. Realloc. */
1810 data
.dptr
= (uint8
*)SMB_REALLOC(data
.dptr
, data
.dsize
+ 8);
1812 DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1817 SIVAL(data
.dptr
,data
.dsize
- 8,mypid
);
1818 SIVAL(data
.dptr
,data
.dsize
- 4,1); /* Refcount. */
1821 /* Store back the record. */
1822 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) != 0) {
1823 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1824 list for printer %s\n", printername
));
1832 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1834 release_print_db(pdb
);
1835 SAFE_FREE(data
.dptr
);
1839 /****************************************************************************
1840 Update an entry in the print tdb that will allow us to send notify
1841 updates only to interested smbd's.
1842 ****************************************************************************/
1844 bool print_notify_deregister_pid(int snum
)
1847 struct tdb_print_db
*pdb
= NULL
;
1848 TDB_CONTEXT
*tdb
= NULL
;
1849 const char *printername
;
1850 uint32_t mypid
= (uint32_t)getpid();
1854 /* if ( snum == -1 ), we are deregister a print server handle
1855 which means to deregister on all print queues */
1859 int num_services
= lp_numservices();
1862 for ( idx
=0; idx
<num_services
; idx
++ ) {
1863 if ( lp_snum_ok(idx
) && lp_print_ok(idx
) )
1864 print_notify_deregister_pid(idx
);
1869 else /* deregister a specific printer */
1871 printername
= lp_const_servicename(snum
);
1872 pdb
= get_print_db_byname(printername
);
1878 if (tdb_lock_bystring_with_timeout(tdb
, NOTIFY_PID_LIST_KEY
, 10) != 0) {
1879 DEBUG(0,("print_notify_register_pid: Failed to lock \
1880 printer %s database\n", printername
));
1882 release_print_db(pdb
);
1886 data
= get_printer_notify_pid_list( tdb
, printername
, True
);
1888 /* Reduce refcount. Remove ourselves if zero. */
1890 for (i
= 0; i
< data
.dsize
; ) {
1891 if (IVAL(data
.dptr
,i
) == mypid
) {
1892 uint32 refcount
= IVAL(data
.dptr
, i
+4);
1896 if (refcount
== 0) {
1897 if (data
.dsize
- i
> 8)
1898 memmove( &data
.dptr
[i
], &data
.dptr
[i
+8], data
.dsize
- i
- 8);
1902 SIVAL(data
.dptr
, i
+4, refcount
);
1908 if (data
.dsize
== 0)
1909 SAFE_FREE(data
.dptr
);
1911 /* Store back the record. */
1912 if (tdb_store_bystring(tdb
, NOTIFY_PID_LIST_KEY
, data
, TDB_REPLACE
) != 0) {
1913 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1914 list for printer %s\n", printername
));
1922 tdb_unlock_bystring(tdb
, NOTIFY_PID_LIST_KEY
);
1924 release_print_db(pdb
);
1925 SAFE_FREE(data
.dptr
);
1929 /****************************************************************************
1930 Check if a jobid is valid. It is valid if it exists in the database.
1931 ****************************************************************************/
1933 bool print_job_exists(const char* sharename
, uint32 jobid
)
1935 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
1941 ret
= tdb_exists(pdb
->tdb
, print_key(jobid
, &tmp
));
1942 release_print_db(pdb
);
1946 /****************************************************************************
1947 Give the filename used for a jobid.
1948 Only valid for the process doing the spooling and when the job
1949 has not been spooled.
1950 ****************************************************************************/
1952 char *print_job_fname(const char* sharename
, uint32 jobid
)
1954 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1955 if (!pjob
|| pjob
->spooled
|| pjob
->pid
!= getpid())
1957 return pjob
->filename
;
1961 /****************************************************************************
1962 Give the filename used for a jobid.
1963 Only valid for the process doing the spooling and when the job
1964 has not been spooled.
1965 ****************************************************************************/
1967 struct spoolss_DeviceMode
*print_job_devmode(const char* sharename
, uint32 jobid
)
1969 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
1974 return pjob
->devmode
;
1977 /****************************************************************************
1978 Set the name of a job. Only possible for owner.
1979 ****************************************************************************/
1981 bool print_job_set_name(struct tevent_context
*ev
,
1982 struct messaging_context
*msg_ctx
,
1983 const char *sharename
, uint32 jobid
, const char *name
)
1985 struct printjob
*pjob
;
1987 pjob
= print_job_find(sharename
, jobid
);
1988 if (!pjob
|| pjob
->pid
!= getpid())
1991 fstrcpy(pjob
->jobname
, name
);
1992 return pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
1995 /****************************************************************************
1996 Get the name of a job. Only possible for owner.
1997 ****************************************************************************/
1999 bool print_job_get_name(TALLOC_CTX
*mem_ctx
, const char *sharename
, uint32_t jobid
, char **name
)
2001 struct printjob
*pjob
;
2003 pjob
= print_job_find(sharename
, jobid
);
2004 if (!pjob
|| pjob
->pid
!= getpid()) {
2008 *name
= talloc_strdup(mem_ctx
, pjob
->jobname
);
2017 /***************************************************************************
2018 Remove a jobid from the 'jobs added' list.
2019 ***************************************************************************/
2021 static bool remove_from_jobs_added(const char* sharename
, uint32 jobid
)
2023 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2025 size_t job_count
, i
;
2027 bool gotlock
= False
;
2035 key
= string_tdb_data("INFO/jobs_added");
2037 if (tdb_chainlock_with_timeout(pdb
->tdb
, key
, 5) != 0)
2042 data
= tdb_fetch_compat(pdb
->tdb
, key
);
2044 if (data
.dptr
== NULL
|| data
.dsize
== 0 || (data
.dsize
% 4 != 0))
2047 job_count
= data
.dsize
/ 4;
2048 for (i
= 0; i
< job_count
; i
++) {
2051 ch_jobid
= IVAL(data
.dptr
, i
*4);
2052 if (ch_jobid
== jobid
) {
2053 if (i
< job_count
-1 )
2054 memmove(data
.dptr
+ (i
*4), data
.dptr
+ (i
*4) + 4, (job_count
- i
- 1)*4 );
2056 if (tdb_store(pdb
->tdb
, key
, data
, TDB_REPLACE
) != 0)
2066 tdb_chainunlock(pdb
->tdb
, key
);
2067 SAFE_FREE(data
.dptr
);
2068 release_print_db(pdb
);
2070 DEBUG(10,("remove_from_jobs_added: removed jobid %u\n", (unsigned int)jobid
));
2072 DEBUG(10,("remove_from_jobs_added: Failed to remove jobid %u\n", (unsigned int)jobid
));
2076 /****************************************************************************
2077 Delete a print job - don't update queue.
2078 ****************************************************************************/
2080 static bool print_job_delete1(struct tevent_context
*ev
,
2081 struct messaging_context
*msg_ctx
,
2082 int snum
, uint32 jobid
)
2084 const char* sharename
= lp_const_servicename(snum
);
2085 struct printjob
*pjob
= print_job_find(sharename
, jobid
);
2087 struct printif
*current_printif
= get_printer_fns( snum
);
2093 * If already deleting just return.
2096 if (pjob
->status
== LPQ_DELETING
)
2099 /* Hrm - we need to be able to cope with deleting a job before it
2100 has reached the spooler. Just mark it as LPQ_DELETING and
2101 let the print_queue_update() code rmeove the record */
2104 if (pjob
->sysjob
== -1) {
2105 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid
));
2108 /* Set the tdb entry to be deleting. */
2110 pjob
->status
= LPQ_DELETING
;
2111 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2113 if (pjob
->spooled
&& pjob
->sysjob
!= -1)
2115 result
= (*(current_printif
->job_delete
))(
2116 lp_printername(snum
),
2117 lp_lprmcommand(snum
),
2120 /* Delete the tdb entry if the delete succeeded or the job hasn't
2124 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2129 pjob_delete(ev
, msg_ctx
, sharename
, jobid
);
2130 /* Ensure we keep a rough count of the number of total jobs... */
2131 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, -1);
2132 release_print_db(pdb
);
2136 remove_from_jobs_added( sharename
, jobid
);
2138 return (result
== 0);
2141 /****************************************************************************
2142 Return true if the current user owns the print job.
2143 ****************************************************************************/
2145 static bool is_owner(const struct auth_session_info
*server_info
,
2146 const char *servicename
,
2149 struct printjob
*pjob
= print_job_find(servicename
, jobid
);
2151 if (!pjob
|| !server_info
)
2154 return strequal(pjob
->user
, server_info
->unix_info
->sanitized_username
);
2157 /****************************************************************************
2159 ****************************************************************************/
2161 WERROR
print_job_delete(const struct auth_session_info
*server_info
,
2162 struct messaging_context
*msg_ctx
,
2163 int snum
, uint32_t jobid
)
2165 const char* sharename
= lp_const_servicename(snum
);
2166 struct printjob
*pjob
;
2170 owner
= is_owner(server_info
, lp_const_servicename(snum
), jobid
);
2172 /* Check access against security descriptor or whether the user
2176 !print_access_check(server_info
, msg_ctx
, snum
,
2177 JOB_ACCESS_ADMINISTER
)) {
2178 DEBUG(3, ("delete denied by security descriptor\n"));
2180 /* BEGIN_ADMIN_LOG */
2181 sys_adminlog( LOG_ERR
,
2182 "Permission denied-- user not allowed to delete, \
2183 pause, or resume print job. User name: %s. Printer name: %s.",
2184 uidtoname(server_info
->unix_token
->uid
),
2185 lp_printername(snum
) );
2188 return WERR_ACCESS_DENIED
;
2192 * get the spooled filename of the print job
2193 * if this works, then the file has not been spooled
2194 * to the underlying print system. Just delete the
2195 * spool file & return.
2198 fname
= print_job_fname(sharename
, jobid
);
2199 if (fname
!= NULL
) {
2200 /* remove the spool file */
2201 DEBUG(10, ("print_job_delete: "
2202 "Removing spool file [%s]\n", fname
));
2203 if (unlink(fname
) == -1) {
2204 return map_werror_from_unix(errno
);
2208 if (!print_job_delete1(server_event_context(), msg_ctx
, snum
, jobid
)) {
2209 return WERR_ACCESS_DENIED
;
2212 /* force update the database and say the delete failed if the
2215 print_queue_update(msg_ctx
, snum
, True
);
2217 pjob
= print_job_find(sharename
, jobid
);
2218 if (pjob
&& (pjob
->status
!= LPQ_DELETING
)) {
2219 return WERR_ACCESS_DENIED
;
2222 return WERR_PRINTER_HAS_JOBS_QUEUED
;
2225 /****************************************************************************
2227 ****************************************************************************/
2229 bool print_job_pause(const struct auth_session_info
*server_info
,
2230 struct messaging_context
*msg_ctx
,
2231 int snum
, uint32 jobid
, WERROR
*errcode
)
2233 const char* sharename
= lp_const_servicename(snum
);
2234 struct printjob
*pjob
;
2236 struct printif
*current_printif
= get_printer_fns( snum
);
2238 pjob
= print_job_find(sharename
, jobid
);
2240 if (!pjob
|| !server_info
) {
2241 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2242 (unsigned int)jobid
));
2246 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2247 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2248 (int)pjob
->sysjob
, (unsigned int)jobid
));
2252 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2253 !print_access_check(server_info
, msg_ctx
, snum
,
2254 JOB_ACCESS_ADMINISTER
)) {
2255 DEBUG(3, ("pause denied by security descriptor\n"));
2257 /* BEGIN_ADMIN_LOG */
2258 sys_adminlog( LOG_ERR
,
2259 "Permission denied-- user not allowed to delete, \
2260 pause, or resume print job. User name: %s. Printer name: %s.",
2261 uidtoname(server_info
->unix_token
->uid
),
2262 lp_printername(snum
) );
2265 *errcode
= WERR_ACCESS_DENIED
;
2269 /* need to pause the spooled entry */
2270 ret
= (*(current_printif
->job_pause
))(snum
, pjob
);
2273 *errcode
= WERR_INVALID_PARAM
;
2277 /* force update the database */
2278 print_cache_flush(lp_const_servicename(snum
));
2280 /* Send a printer notify message */
2282 notify_job_status(server_event_context(), msg_ctx
, sharename
, jobid
,
2285 /* how do we tell if this succeeded? */
2290 /****************************************************************************
2292 ****************************************************************************/
2294 bool print_job_resume(const struct auth_session_info
*server_info
,
2295 struct messaging_context
*msg_ctx
,
2296 int snum
, uint32 jobid
, WERROR
*errcode
)
2298 const char *sharename
= lp_const_servicename(snum
);
2299 struct printjob
*pjob
;
2301 struct printif
*current_printif
= get_printer_fns( snum
);
2303 pjob
= print_job_find(sharename
, jobid
);
2305 if (!pjob
|| !server_info
) {
2306 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2307 (unsigned int)jobid
));
2311 if (!pjob
->spooled
|| pjob
->sysjob
== -1) {
2312 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2313 (int)pjob
->sysjob
, (unsigned int)jobid
));
2317 if (!is_owner(server_info
, lp_const_servicename(snum
), jobid
) &&
2318 !print_access_check(server_info
, msg_ctx
, snum
,
2319 JOB_ACCESS_ADMINISTER
)) {
2320 DEBUG(3, ("resume denied by security descriptor\n"));
2321 *errcode
= WERR_ACCESS_DENIED
;
2323 /* BEGIN_ADMIN_LOG */
2324 sys_adminlog( LOG_ERR
,
2325 "Permission denied-- user not allowed to delete, \
2326 pause, or resume print job. User name: %s. Printer name: %s.",
2327 uidtoname(server_info
->unix_token
->uid
),
2328 lp_printername(snum
) );
2333 ret
= (*(current_printif
->job_resume
))(snum
, pjob
);
2336 *errcode
= WERR_INVALID_PARAM
;
2340 /* force update the database */
2341 print_cache_flush(lp_const_servicename(snum
));
2343 /* Send a printer notify message */
2345 notify_job_status(server_event_context(), msg_ctx
, sharename
, jobid
,
2351 /****************************************************************************
2352 Write to a print file.
2353 ****************************************************************************/
2355 ssize_t
print_job_write(struct tevent_context
*ev
,
2356 struct messaging_context
*msg_ctx
,
2357 int snum
, uint32 jobid
, const char *buf
, size_t size
)
2359 const char* sharename
= lp_const_servicename(snum
);
2360 ssize_t return_code
;
2361 struct printjob
*pjob
;
2363 pjob
= print_job_find(sharename
, jobid
);
2367 /* don't allow another process to get this info - it is meaningless */
2368 if (pjob
->pid
!= getpid())
2371 /* if SMBD is spooling this can't be allowed */
2372 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2376 return_code
= write_data(pjob
->fd
, buf
, size
);
2378 if (return_code
>0) {
2380 pjob_store(ev
, msg_ctx
, sharename
, jobid
, pjob
);
2385 /****************************************************************************
2386 Get the queue status - do not update if db is out of date.
2387 ****************************************************************************/
2389 static int get_queue_status(const char* sharename
, print_status_struct
*status
)
2393 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2397 ZERO_STRUCTP(status
);
2404 fstr_sprintf(keystr
, "STATUS/%s", sharename
);
2405 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data(keystr
));
2407 if (data
.dsize
== sizeof(print_status_struct
))
2408 /* this memcpy is ok since the status struct was
2409 not packed before storing it in the tdb */
2410 memcpy(status
, data
.dptr
, sizeof(print_status_struct
));
2411 SAFE_FREE(data
.dptr
);
2414 len
= tdb_fetch_int32(pdb
->tdb
, "INFO/total_jobs");
2415 release_print_db(pdb
);
2416 return (len
== -1 ? 0 : len
);
2419 /****************************************************************************
2420 Determine the number of jobs in a queue.
2421 ****************************************************************************/
2423 int print_queue_length(struct messaging_context
*msg_ctx
, int snum
,
2424 print_status_struct
*pstatus
)
2426 const char* sharename
= lp_const_servicename( snum
);
2427 print_status_struct status
;
2430 ZERO_STRUCT( status
);
2432 /* make sure the database is up to date */
2433 if (print_cache_expired(lp_const_servicename(snum
), True
))
2434 print_queue_update(msg_ctx
, snum
, False
);
2436 /* also fetch the queue status */
2437 memset(&status
, 0, sizeof(status
));
2438 len
= get_queue_status(sharename
, &status
);
2446 /***************************************************************************
2447 Allocate a jobid. Hold the lock for as short a time as possible.
2448 ***************************************************************************/
2450 static WERROR
allocate_print_jobid(struct tdb_print_db
*pdb
, int snum
,
2451 const char *sharename
, uint32
*pjobid
)
2455 enum TDB_ERROR terr
;
2458 *pjobid
= (uint32
)-1;
2460 for (i
= 0; i
< 3; i
++) {
2461 /* Lock the database - only wait 20 seconds. */
2462 ret
= tdb_lock_bystring_with_timeout(pdb
->tdb
,
2463 "INFO/nextjob", 20);
2465 DEBUG(0, ("allocate_print_jobid: "
2466 "Failed to lock printing database %s\n",
2468 terr
= tdb_error(pdb
->tdb
);
2469 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2472 if (!tdb_fetch_uint32(pdb
->tdb
, "INFO/nextjob", &jobid
)) {
2473 terr
= tdb_error(pdb
->tdb
);
2474 if (terr
!= TDB_ERR_NOEXIST
) {
2475 DEBUG(0, ("allocate_print_jobid: "
2476 "Failed to fetch INFO/nextjob "
2477 "for print queue %s\n", sharename
));
2478 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2479 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2481 DEBUG(10, ("allocate_print_jobid: "
2482 "No existing jobid in %s\n", sharename
));
2486 DEBUG(10, ("allocate_print_jobid: "
2487 "Read jobid %u from %s\n", jobid
, sharename
));
2489 jobid
= NEXT_JOBID(jobid
);
2491 ret
= tdb_store_int32(pdb
->tdb
, "INFO/nextjob", jobid
);
2493 terr
= tdb_error(pdb
->tdb
);
2494 DEBUG(3, ("allocate_print_jobid: "
2495 "Failed to store INFO/nextjob.\n"));
2496 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2497 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2500 /* We've finished with the INFO/nextjob lock. */
2501 tdb_unlock_bystring(pdb
->tdb
, "INFO/nextjob");
2503 if (!print_job_exists(sharename
, jobid
)) {
2506 DEBUG(10, ("allocate_print_jobid: "
2507 "Found jobid %u in %s\n", jobid
, sharename
));
2511 DEBUG(0, ("allocate_print_jobid: "
2512 "Failed to allocate a print job for queue %s\n",
2514 /* Probably full... */
2515 return WERR_NO_SPOOL_SPACE
;
2518 /* Store a dummy placeholder. */
2524 if (tdb_store(pdb
->tdb
, print_key(jobid
, &tmp
), dum
,
2526 DEBUG(3, ("allocate_print_jobid: "
2527 "jobid (%d) failed to store placeholder.\n",
2529 terr
= tdb_error(pdb
->tdb
);
2530 return ntstatus_to_werror(map_nt_error_from_tdb(terr
));
2538 /***************************************************************************
2539 Append a jobid to the 'jobs added' list.
2540 ***************************************************************************/
2542 static bool add_to_jobs_added(struct tdb_print_db
*pdb
, uint32 jobid
)
2547 SIVAL(&store_jobid
, 0, jobid
);
2548 data
.dptr
= (uint8
*)&store_jobid
;
2551 DEBUG(10,("add_to_jobs_added: Added jobid %u\n", (unsigned int)jobid
));
2553 return (tdb_append(pdb
->tdb
, string_tdb_data("INFO/jobs_added"),
2558 /***************************************************************************
2559 Do all checks needed to determine if we can start a job.
2560 ***************************************************************************/
2562 static WERROR
print_job_checks(const struct auth_session_info
*server_info
,
2563 struct messaging_context
*msg_ctx
,
2564 int snum
, int *njobs
)
2566 const char *sharename
= lp_const_servicename(snum
);
2567 uint64_t dspace
, dsize
;
2571 if (!print_access_check(server_info
, msg_ctx
, snum
,
2572 PRINTER_ACCESS_USE
)) {
2573 DEBUG(3, ("print_job_checks: "
2574 "job start denied by security descriptor\n"));
2575 return WERR_ACCESS_DENIED
;
2578 if (!print_time_access_check(server_info
, msg_ctx
, sharename
)) {
2579 DEBUG(3, ("print_job_checks: "
2580 "job start denied by time check\n"));
2581 return WERR_ACCESS_DENIED
;
2584 /* see if we have sufficient disk space */
2585 if (lp_minprintspace(snum
)) {
2586 minspace
= lp_minprintspace(snum
);
2587 ret
= sys_fsusage(lp_pathname(snum
), &dspace
, &dsize
);
2588 if (ret
== 0 && dspace
< 2*minspace
) {
2589 DEBUG(3, ("print_job_checks: "
2590 "disk space check failed.\n"));
2591 return WERR_NO_SPOOL_SPACE
;
2595 /* for autoloaded printers, check that the printcap entry still exists */
2596 if (lp_autoloaded(snum
) && !pcap_printername_ok(sharename
)) {
2597 DEBUG(3, ("print_job_checks: printer name %s check failed.\n",
2599 return WERR_ACCESS_DENIED
;
2602 /* Insure the maximum queue size is not violated */
2603 *njobs
= print_queue_length(msg_ctx
, snum
, NULL
);
2604 if (*njobs
> lp_maxprintjobs(snum
)) {
2605 DEBUG(3, ("print_job_checks: Queue %s number of jobs (%d) "
2606 "larger than max printjobs per queue (%d).\n",
2607 sharename
, *njobs
, lp_maxprintjobs(snum
)));
2608 return WERR_NO_SPOOL_SPACE
;
2614 /***************************************************************************
2616 ***************************************************************************/
2618 static WERROR
print_job_spool_file(int snum
, uint32_t jobid
,
2619 const char *output_file
,
2620 struct printjob
*pjob
)
2627 /* if this file is within the printer path, it means that smbd
2628 * is spooling it and will pass us control when it is finished.
2629 * Verify that the file name is ok, within path, and it is
2630 * already already there */
2632 path
= lp_pathname(snum
);
2634 if (strncmp(output_file
, path
, len
) == 0 &&
2635 (output_file
[len
- 1] == '/' || output_file
[len
] == '/')) {
2637 /* verify path is not too long */
2638 if (strlen(output_file
) >= sizeof(pjob
->filename
)) {
2639 return WERR_INVALID_NAME
;
2642 /* verify that the file exists */
2643 if (sys_stat(output_file
, &st
, false) != 0) {
2644 return WERR_INVALID_NAME
;
2647 fstrcpy(pjob
->filename
, output_file
);
2649 DEBUG(3, ("print_job_spool_file:"
2650 "External spooling activated"));
2652 /* we do not open the file until spooling is done */
2654 pjob
->status
= PJOB_SMBD_SPOOLING
;
2660 slprintf(pjob
->filename
, sizeof(pjob
->filename
)-1,
2661 "%s/%s%.8u.XXXXXX", lp_pathname(snum
),
2662 PRINT_SPOOL_PREFIX
, (unsigned int)jobid
);
2663 pjob
->fd
= mkstemp(pjob
->filename
);
2665 if (pjob
->fd
== -1) {
2666 werr
= map_werror_from_unix(errno
);
2667 if (W_ERROR_EQUAL(werr
, WERR_ACCESS_DENIED
)) {
2668 /* Common setup error, force a report. */
2669 DEBUG(0, ("print_job_spool_file: "
2670 "insufficient permissions to open spool "
2671 "file %s.\n", pjob
->filename
));
2673 /* Normal case, report at level 3 and above. */
2674 DEBUG(3, ("print_job_spool_file: "
2675 "can't open spool file %s\n",
2684 /***************************************************************************
2685 Start spooling a job - return the jobid.
2686 ***************************************************************************/
2688 WERROR
print_job_start(const struct auth_session_info
*server_info
,
2689 struct messaging_context
*msg_ctx
,
2690 const char *clientmachine
,
2691 int snum
, const char *docname
, const char *filename
,
2692 struct spoolss_DeviceMode
*devmode
, uint32_t *_jobid
)
2696 struct printjob pjob
;
2697 const char *sharename
= lp_const_servicename(snum
);
2698 struct tdb_print_db
*pdb
= get_print_db_byname(sharename
);
2703 return WERR_INTERNAL_DB_CORRUPTION
;
2706 path
= lp_pathname(snum
);
2708 werr
= print_job_checks(server_info
, msg_ctx
, snum
, &njobs
);
2709 if (!W_ERROR_IS_OK(werr
)) {
2710 release_print_db(pdb
);
2714 DEBUG(10, ("print_job_start: "
2715 "Queue %s number of jobs (%d), max printjobs = %d\n",
2716 sharename
, njobs
, lp_maxprintjobs(snum
)));
2718 werr
= allocate_print_jobid(pdb
, snum
, sharename
, &jobid
);
2719 if (!W_ERROR_IS_OK(werr
)) {
2723 /* create the database entry */
2727 pjob
.pid
= getpid();
2731 pjob
.starttime
= time(NULL
);
2732 pjob
.status
= LPQ_SPOOLING
;
2734 pjob
.spooled
= False
;
2736 pjob
.devmode
= devmode
;
2738 fstrcpy(pjob
.jobname
, docname
);
2740 fstrcpy(pjob
.clientmachine
, clientmachine
);
2742 fstrcpy(pjob
.user
, lp_printjob_username(snum
));
2743 standard_sub_advanced(sharename
, server_info
->unix_info
->sanitized_username
,
2744 path
, server_info
->unix_token
->gid
,
2745 server_info
->unix_info
->sanitized_username
,
2746 server_info
->info
->domain_name
,
2747 pjob
.user
, sizeof(pjob
.user
));
2749 fstrcpy(pjob
.queuename
, lp_const_servicename(snum
));
2751 /* we have a job entry - now create the spool file */
2752 werr
= print_job_spool_file(snum
, jobid
, filename
, &pjob
);
2753 if (!W_ERROR_IS_OK(werr
)) {
2757 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, &pjob
);
2759 /* Update the 'jobs added' entry used by print_queue_status. */
2760 add_to_jobs_added(pdb
, jobid
);
2762 /* Ensure we keep a rough count of the number of total jobs... */
2763 tdb_change_int32_atomic(pdb
->tdb
, "INFO/total_jobs", &njobs
, 1);
2765 release_print_db(pdb
);
2772 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2775 release_print_db(pdb
);
2777 DEBUG(3, ("print_job_start: returning fail. "
2778 "Error = %s\n", win_errstr(werr
)));
2782 /****************************************************************************
2783 Update the number of pages spooled to jobid
2784 ****************************************************************************/
2786 void print_job_endpage(struct messaging_context
*msg_ctx
,
2787 int snum
, uint32 jobid
)
2789 const char* sharename
= lp_const_servicename(snum
);
2790 struct printjob
*pjob
;
2792 pjob
= print_job_find(sharename
, jobid
);
2795 /* don't allow another process to get this info - it is meaningless */
2796 if (pjob
->pid
!= getpid())
2800 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, pjob
);
2803 /****************************************************************************
2804 Print a file - called on closing the file. This spools the job.
2805 If normal close is false then we're tearing down the jobs - treat as an
2807 ****************************************************************************/
2809 NTSTATUS
print_job_end(struct messaging_context
*msg_ctx
, int snum
,
2810 uint32 jobid
, enum file_close_type close_type
)
2812 const char* sharename
= lp_const_servicename(snum
);
2813 struct printjob
*pjob
;
2815 SMB_STRUCT_STAT sbuf
;
2816 struct printif
*current_printif
= get_printer_fns( snum
);
2817 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
2819 pjob
= print_job_find(sharename
, jobid
);
2822 return NT_STATUS_PRINT_CANCELLED
;
2825 if (pjob
->spooled
|| pjob
->pid
!= getpid()) {
2826 return NT_STATUS_ACCESS_DENIED
;
2829 if (close_type
== NORMAL_CLOSE
|| close_type
== SHUTDOWN_CLOSE
) {
2830 if (pjob
->status
== PJOB_SMBD_SPOOLING
) {
2831 /* take over the file now, smbd is done */
2832 if (sys_stat(pjob
->filename
, &sbuf
, false) != 0) {
2833 status
= map_nt_error_from_unix(errno
);
2834 DEBUG(3, ("print_job_end: "
2835 "stat file failed for jobid %d\n",
2840 pjob
->status
= LPQ_SPOOLING
;
2844 if ((sys_fstat(pjob
->fd
, &sbuf
, false) != 0)) {
2845 status
= map_nt_error_from_unix(errno
);
2847 DEBUG(3, ("print_job_end: "
2848 "stat file failed for jobid %d\n",
2856 pjob
->size
= sbuf
.st_ex_size
;
2860 * Not a normal close, something has gone wrong. Cleanup.
2862 if (pjob
->fd
!= -1) {
2868 /* Technically, this is not quite right. If the printer has a separator
2869 * page turned on, the NT spooler prints the separator page even if the
2870 * print job is 0 bytes. 010215 JRR */
2871 if (pjob
->size
== 0 || pjob
->status
== LPQ_DELETING
) {
2872 /* don't bother spooling empty files or something being deleted. */
2873 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2874 pjob
->filename
, pjob
->size
? "deleted" : "zero length" ));
2875 unlink(pjob
->filename
);
2876 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2877 return NT_STATUS_OK
;
2880 ret
= (*(current_printif
->job_submit
))(snum
, pjob
);
2883 status
= NT_STATUS_PRINT_CANCELLED
;
2887 /* The print job has been successfully handed over to the back-end */
2889 pjob
->spooled
= True
;
2890 pjob
->status
= LPQ_QUEUED
;
2891 pjob_store(server_event_context(), msg_ctx
, sharename
, jobid
, pjob
);
2893 /* make sure the database is up to date */
2894 if (print_cache_expired(lp_const_servicename(snum
), True
))
2895 print_queue_update(msg_ctx
, snum
, False
);
2897 return NT_STATUS_OK
;
2901 /* The print job was not successfully started. Cleanup */
2902 /* Still need to add proper error return propagation! 010122:JRR */
2904 unlink(pjob
->filename
);
2905 pjob_delete(server_event_context(), msg_ctx
, sharename
, jobid
);
2909 /****************************************************************************
2910 Get a snapshot of jobs in the system without traversing.
2911 ****************************************************************************/
2913 static bool get_stored_queue_info(struct messaging_context
*msg_ctx
,
2914 struct tdb_print_db
*pdb
, int snum
,
2915 int *pcount
, print_queue_struct
**ppqueue
)
2917 TDB_DATA data
, cgdata
, jcdata
;
2918 print_queue_struct
*queue
= NULL
;
2920 uint32 extra_count
= 0;
2921 uint32_t changed_count
= 0;
2922 int total_count
= 0;
2925 int max_reported_jobs
= lp_max_reported_jobs(snum
);
2927 const char* sharename
= lp_servicename(snum
);
2929 /* make sure the database is up to date */
2930 if (print_cache_expired(lp_const_servicename(snum
), True
))
2931 print_queue_update(msg_ctx
, snum
, False
);
2937 ZERO_STRUCT(cgdata
);
2939 /* Get the stored queue data. */
2940 data
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/linear_queue_array"));
2942 if (data
.dptr
&& data
.dsize
>= sizeof(qcount
))
2943 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "d", &qcount
);
2945 /* Get the added jobs list. */
2946 cgdata
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_added"));
2947 if (cgdata
.dptr
!= NULL
&& (cgdata
.dsize
% 4 == 0))
2948 extra_count
= cgdata
.dsize
/4;
2950 /* Get the changed jobs list. */
2951 jcdata
= tdb_fetch_compat(pdb
->tdb
, string_tdb_data("INFO/jobs_changed"));
2952 if (jcdata
.dptr
!= NULL
&& (jcdata
.dsize
% 4 == 0))
2953 changed_count
= jcdata
.dsize
/ 4;
2955 DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount
, (unsigned int)extra_count
));
2957 /* Allocate the queue size. */
2958 if (qcount
== 0 && extra_count
== 0)
2961 if ((queue
= SMB_MALLOC_ARRAY(print_queue_struct
, qcount
+ extra_count
)) == NULL
)
2964 /* Retrieve the linearised queue data. */
2966 for( i
= 0; i
< qcount
; i
++) {
2967 uint32 qjob
, qsize
, qpage_count
, qstatus
, qpriority
, qtime
;
2968 len
+= tdb_unpack(data
.dptr
+ len
, data
.dsize
- len
, "ddddddff",
2977 queue
[i
].job
= qjob
;
2978 queue
[i
].size
= qsize
;
2979 queue
[i
].page_count
= qpage_count
;
2980 queue
[i
].status
= qstatus
;
2981 queue
[i
].priority
= qpriority
;
2982 queue
[i
].time
= qtime
;
2985 total_count
= qcount
;
2987 /* Add new jobids to the queue. */
2988 for( i
= 0; i
< extra_count
; i
++) {
2990 struct printjob
*pjob
;
2992 jobid
= IVAL(cgdata
.dptr
, i
*4);
2993 DEBUG(5,("get_stored_queue_info: added job = %u\n", (unsigned int)jobid
));
2994 pjob
= print_job_find(lp_const_servicename(snum
), jobid
);
2996 DEBUG(5,("get_stored_queue_info: failed to find added job = %u\n", (unsigned int)jobid
));
2997 remove_from_jobs_added(sharename
, jobid
);
3001 queue
[total_count
].job
= jobid
;
3002 queue
[total_count
].size
= pjob
->size
;
3003 queue
[total_count
].page_count
= pjob
->page_count
;
3004 queue
[total_count
].status
= pjob
->status
;
3005 queue
[total_count
].priority
= 1;
3006 queue
[total_count
].time
= pjob
->starttime
;
3007 fstrcpy(queue
[total_count
].fs_user
, pjob
->user
);
3008 fstrcpy(queue
[total_count
].fs_file
, pjob
->jobname
);
3012 /* Update the changed jobids. */
3013 for (i
= 0; i
< changed_count
; i
++) {
3014 uint32_t jobid
= IVAL(jcdata
.dptr
, i
* 4);
3018 for (j
= 0; j
< total_count
; j
++) {
3019 if (queue
[j
].job
== jobid
) {
3026 struct printjob
*pjob
;
3028 DEBUG(5,("get_stored_queue_info: changed job: %u\n",
3029 (unsigned int) jobid
));
3031 pjob
= print_job_find(sharename
, jobid
);
3033 DEBUG(5,("get_stored_queue_info: failed to find "
3034 "changed job = %u\n",
3035 (unsigned int) jobid
));
3036 remove_from_jobs_changed(sharename
, jobid
);
3040 queue
[j
].job
= jobid
;
3041 queue
[j
].size
= pjob
->size
;
3042 queue
[j
].page_count
= pjob
->page_count
;
3043 queue
[j
].status
= pjob
->status
;
3044 queue
[j
].priority
= 1;
3045 queue
[j
].time
= pjob
->starttime
;
3046 fstrcpy(queue
[j
].fs_user
, pjob
->user
);
3047 fstrcpy(queue
[j
].fs_file
, pjob
->jobname
);
3049 DEBUG(5,("get_stored_queue_info: updated queue[%u], jobid: %u, jobname: %s\n",
3050 (unsigned int) j
, (unsigned int) jobid
, pjob
->jobname
));
3053 remove_from_jobs_changed(sharename
, jobid
);
3056 /* Sort the queue by submission time otherwise they are displayed
3059 TYPESAFE_QSORT(queue
, total_count
, printjob_comp
);
3061 DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count
));
3063 if (max_reported_jobs
&& total_count
> max_reported_jobs
)
3064 total_count
= max_reported_jobs
;
3067 *pcount
= total_count
;
3073 SAFE_FREE(data
.dptr
);
3074 SAFE_FREE(cgdata
.dptr
);
3078 /****************************************************************************
3079 Get a printer queue listing.
3080 set queue = NULL and status = NULL if you just want to update the cache
3081 ****************************************************************************/
3083 int print_queue_status(struct messaging_context
*msg_ctx
, int snum
,
3084 print_queue_struct
**ppqueue
,
3085 print_status_struct
*status
)
3089 const char *sharename
;
3090 struct tdb_print_db
*pdb
;
3093 /* make sure the database is up to date */
3095 if (print_cache_expired(lp_const_servicename(snum
), True
))
3096 print_queue_update(msg_ctx
, snum
, False
);
3098 /* return if we are done */
3099 if ( !ppqueue
|| !status
)
3103 sharename
= lp_const_servicename(snum
);
3104 pdb
= get_print_db_byname(sharename
);
3110 * Fetch the queue status. We must do this first, as there may
3111 * be no jobs in the queue.
3114 ZERO_STRUCTP(status
);
3115 slprintf(keystr
, sizeof(keystr
)-1, "STATUS/%s", sharename
);
3116 key
= string_tdb_data(keystr
);
3118 data
= tdb_fetch_compat(pdb
->tdb
, key
);
3120 if (data
.dsize
== sizeof(*status
)) {
3121 /* this memcpy is ok since the status struct was
3122 not packed before storing it in the tdb */
3123 memcpy(status
, data
.dptr
, sizeof(*status
));
3125 SAFE_FREE(data
.dptr
);
3129 * Now, fetch the print queue information. We first count the number
3130 * of entries, and then only retrieve the queue if necessary.
3133 if (!get_stored_queue_info(msg_ctx
, pdb
, snum
, &count
, ppqueue
)) {
3134 release_print_db(pdb
);
3138 release_print_db(pdb
);
3142 /****************************************************************************
3144 ****************************************************************************/
3146 WERROR
print_queue_pause(const struct auth_session_info
*server_info
,
3147 struct messaging_context
*msg_ctx
, int snum
)
3150 struct printif
*current_printif
= get_printer_fns( snum
);
3152 if (!print_access_check(server_info
, msg_ctx
, snum
,
3153 PRINTER_ACCESS_ADMINISTER
)) {
3154 return WERR_ACCESS_DENIED
;
3160 ret
= (*(current_printif
->queue_pause
))(snum
);
3165 return WERR_INVALID_PARAM
;
3168 /* force update the database */
3169 print_cache_flush(lp_const_servicename(snum
));
3171 /* Send a printer notify message */
3173 notify_printer_status(server_event_context(), msg_ctx
, snum
,
3174 PRINTER_STATUS_PAUSED
);
3179 /****************************************************************************
3181 ****************************************************************************/
3183 WERROR
print_queue_resume(const struct auth_session_info
*server_info
,
3184 struct messaging_context
*msg_ctx
, int snum
)
3187 struct printif
*current_printif
= get_printer_fns( snum
);
3189 if (!print_access_check(server_info
, msg_ctx
, snum
,
3190 PRINTER_ACCESS_ADMINISTER
)) {
3191 return WERR_ACCESS_DENIED
;
3196 ret
= (*(current_printif
->queue_resume
))(snum
);
3201 return WERR_INVALID_PARAM
;
3204 /* make sure the database is up to date */
3205 if (print_cache_expired(lp_const_servicename(snum
), True
))
3206 print_queue_update(msg_ctx
, snum
, True
);
3208 /* Send a printer notify message */
3210 notify_printer_status(server_event_context(), msg_ctx
, snum
,
3216 /****************************************************************************
3217 Purge a queue - implemented by deleting all jobs that we can delete.
3218 ****************************************************************************/
3220 WERROR
print_queue_purge(const struct auth_session_info
*server_info
,
3221 struct messaging_context
*msg_ctx
, int snum
)
3223 print_queue_struct
*queue
;
3224 print_status_struct status
;
3228 /* Force and update so the count is accurate (i.e. not a cached count) */
3229 print_queue_update(msg_ctx
, snum
, True
);
3231 can_job_admin
= print_access_check(server_info
,
3234 JOB_ACCESS_ADMINISTER
);
3235 njobs
= print_queue_status(msg_ctx
, snum
, &queue
, &status
);
3237 if ( can_job_admin
)
3240 for (i
=0;i
<njobs
;i
++) {
3241 bool owner
= is_owner(server_info
, lp_const_servicename(snum
),
3244 if (owner
|| can_job_admin
) {
3245 print_job_delete1(server_event_context(), msg_ctx
,
3246 snum
, queue
[i
].job
);
3250 if ( can_job_admin
)
3253 /* update the cache */
3254 print_queue_update(msg_ctx
, snum
, True
);