d84fc7699b73857b960d3c09fe08d56d5a60f92d
[abartlet/samba.git/.git] / source3 / printing / printing.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    printing backend routines
5    Copyright (C) Andrew Tridgell 1992-2000
6    Copyright (C) Jeremy Allison 2002
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "printing.h"
24 #include "librpc/gen_ndr/messaging.h"
25 #include "../librpc/gen_ndr/ndr_spoolss.h"
26 #include "nt_printing.h"
27 #include "../librpc/gen_ndr/netlogon.h"
28
29 extern struct current_user current_user;
30 extern userdom_struct current_user_info;
31
32 /* Current printer interface */
33 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid);
34
35 /*
36    the printing backend revolves around a tdb database that stores the
37    SMB view of the print queue
38
39    The key for this database is a jobid - a internally generated number that
40    uniquely identifies a print job
41
42    reading the print queue involves two steps:
43      - possibly running lpq and updating the internal database from that
44      - reading entries from the database
45
46    jobids are assigned when a job starts spooling.
47 */
48
49 static TDB_CONTEXT *rap_tdb;
50 static uint16 next_rap_jobid;
51 struct rap_jobid_key {
52         fstring sharename;
53         uint32  jobid;
54 };
55
56 /***************************************************************************
57  Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
58  bit RPC jobids.... JRA.
59 ***************************************************************************/
60
61 uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
62 {
63         uint16 rap_jobid;
64         TDB_DATA data, key;
65         struct rap_jobid_key jinfo;
66         uint8 buf[2];
67
68         DEBUG(10,("pjobid_to_rap: called.\n"));
69
70         if (!rap_tdb) {
71                 /* Create the in-memory tdb. */
72                 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
73                 if (!rap_tdb)
74                         return 0;
75         }
76
77         ZERO_STRUCT( jinfo );
78         fstrcpy( jinfo.sharename, sharename );
79         jinfo.jobid = jobid;
80         key.dptr = (uint8 *)&jinfo;
81         key.dsize = sizeof(jinfo);
82
83         data = tdb_fetch(rap_tdb, key);
84         if (data.dptr && data.dsize == sizeof(uint16)) {
85                 rap_jobid = SVAL(data.dptr, 0);
86                 SAFE_FREE(data.dptr);
87                 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
88                         (unsigned int)jobid, (unsigned int)rap_jobid));
89                 return rap_jobid;
90         }
91         SAFE_FREE(data.dptr);
92         /* Not found - create and store mapping. */
93         rap_jobid = ++next_rap_jobid;
94         if (rap_jobid == 0)
95                 rap_jobid = ++next_rap_jobid;
96         SSVAL(buf,0,rap_jobid);
97         data.dptr = buf;
98         data.dsize = sizeof(rap_jobid);
99         tdb_store(rap_tdb, key, data, TDB_REPLACE);
100         tdb_store(rap_tdb, data, key, TDB_REPLACE);
101
102         DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
103                 (unsigned int)jobid, (unsigned int)rap_jobid));
104         return rap_jobid;
105 }
106
107 bool rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
108 {
109         TDB_DATA data, key;
110         uint8 buf[2];
111
112         DEBUG(10,("rap_to_pjobid called.\n"));
113
114         if (!rap_tdb)
115                 return False;
116
117         SSVAL(buf,0,rap_jobid);
118         key.dptr = buf;
119         key.dsize = sizeof(rap_jobid);
120         data = tdb_fetch(rap_tdb, key);
121         if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
122         {
123                 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
124                 if (sharename != NULL) {
125                         fstrcpy( sharename, jinfo->sharename );
126                 }
127                 *pjobid = jinfo->jobid;
128                 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
129                         (unsigned int)*pjobid, (unsigned int)rap_jobid));
130                 SAFE_FREE(data.dptr);
131                 return True;
132         }
133
134         DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
135                 (unsigned int)rap_jobid));
136         SAFE_FREE(data.dptr);
137         return False;
138 }
139
140 void rap_jobid_delete(const char* sharename, uint32 jobid)
141 {
142         TDB_DATA key, data;
143         uint16 rap_jobid;
144         struct rap_jobid_key jinfo;
145         uint8 buf[2];
146
147         DEBUG(10,("rap_jobid_delete: called.\n"));
148
149         if (!rap_tdb)
150                 return;
151
152         ZERO_STRUCT( jinfo );
153         fstrcpy( jinfo.sharename, sharename );
154         jinfo.jobid = jobid;
155         key.dptr = (uint8 *)&jinfo;
156         key.dsize = sizeof(jinfo);
157
158         data = tdb_fetch(rap_tdb, key);
159         if (!data.dptr || (data.dsize != sizeof(uint16))) {
160                 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
161                         (unsigned int)jobid ));
162                 SAFE_FREE(data.dptr);
163                 return;
164         }
165
166         DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
167                 (unsigned int)jobid ));
168
169         rap_jobid = SVAL(data.dptr, 0);
170         SAFE_FREE(data.dptr);
171         SSVAL(buf,0,rap_jobid);
172         data.dptr = buf;
173         data.dsize = sizeof(rap_jobid);
174         tdb_delete(rap_tdb, key);
175         tdb_delete(rap_tdb, data);
176 }
177
178 static int get_queue_status(const char* sharename, print_status_struct *);
179
180 /****************************************************************************
181  Initialise the printing backend. Called once at startup before the fork().
182 ****************************************************************************/
183
184 bool print_backend_init(struct messaging_context *msg_ctx)
185 {
186         const char *sversion = "INFO/version";
187         int services = lp_numservices();
188         int snum;
189
190         unlink(cache_path("printing.tdb"));
191         mkdir(cache_path("printing"),0755);
192
193         /* handle a Samba upgrade */
194
195         for (snum = 0; snum < services; snum++) {
196                 struct tdb_print_db *pdb;
197                 if (!lp_print_ok(snum))
198                         continue;
199
200                 pdb = get_print_db_byname(lp_const_servicename(snum));
201                 if (!pdb)
202                         continue;
203                 if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
204                         DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
205                         release_print_db(pdb);
206                         return False;
207                 }
208                 if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
209                         tdb_wipe_all(pdb->tdb);
210                         tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
211                 }
212                 tdb_unlock_bystring(pdb->tdb, sversion);
213                 release_print_db(pdb);
214         }
215
216         close_all_print_db(); /* Don't leave any open. */
217
218         /* do NT print initialization... */
219         return nt_printing_init(msg_ctx);
220 }
221
222 /****************************************************************************
223  Shut down printing backend. Called once at shutdown to close the tdb.
224 ****************************************************************************/
225
226 void printing_end(void)
227 {
228         close_all_print_db(); /* Don't leave any open. */
229 }
230
231 /****************************************************************************
232  Retrieve the set of printing functions for a given service.  This allows
233  us to set the printer function table based on the value of the 'printing'
234  service parameter.
235
236  Use the generic interface as the default and only use cups interface only
237  when asked for (and only when supported)
238 ****************************************************************************/
239
240 static struct printif *get_printer_fns_from_type( enum printing_types type )
241 {
242         struct printif *printer_fns = &generic_printif;
243
244 #ifdef HAVE_CUPS
245         if ( type == PRINT_CUPS ) {
246                 printer_fns = &cups_printif;
247         }
248 #endif /* HAVE_CUPS */
249
250 #ifdef HAVE_IPRINT
251         if ( type == PRINT_IPRINT ) {
252                 printer_fns = &iprint_printif;
253         }
254 #endif /* HAVE_IPRINT */
255
256         printer_fns->type = type;
257
258         return printer_fns;
259 }
260
261 static struct printif *get_printer_fns( int snum )
262 {
263         return get_printer_fns_from_type( (enum printing_types)lp_printing(snum) );
264 }
265
266
267 /****************************************************************************
268  Useful function to generate a tdb key.
269 ****************************************************************************/
270
271 static TDB_DATA print_key(uint32 jobid, uint32 *tmp)
272 {
273         TDB_DATA ret;
274
275         SIVAL(tmp, 0, jobid);
276         ret.dptr = (uint8 *)tmp;
277         ret.dsize = sizeof(*tmp);
278         return ret;
279 }
280
281 /****************************************************************************
282  Pack the devicemode to store it in a tdb.
283 ****************************************************************************/
284 static int pack_devicemode(struct spoolss_DeviceMode *devmode, uint8 *buf, int buflen)
285 {
286         enum ndr_err_code ndr_err;
287         DATA_BLOB blob;
288         int len = 0;
289
290         if (devmode) {
291                 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(),
292                                                devmode,
293                                                (ndr_push_flags_fn_t)
294                                                ndr_push_spoolss_DeviceMode);
295                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
296                         DEBUG(10, ("pack_devicemode: "
297                                    "error encoding spoolss_DeviceMode\n"));
298                         goto done;
299                 }
300         } else {
301                 ZERO_STRUCT(blob);
302         }
303
304         len = tdb_pack(buf, buflen, "B", blob.length, blob.data);
305
306         if (devmode) {
307                 DEBUG(8, ("Packed devicemode [%s]\n", devmode->formname));
308         }
309
310 done:
311         return len;
312 }
313
314 /****************************************************************************
315  Unpack the devicemode to store it in a tdb.
316 ****************************************************************************/
317 static int unpack_devicemode(TALLOC_CTX *mem_ctx,
318                       const uint8 *buf, int buflen,
319                       struct spoolss_DeviceMode **devmode)
320 {
321         struct spoolss_DeviceMode *dm;
322         enum ndr_err_code ndr_err;
323         char *data = NULL;
324         int data_len = 0;
325         DATA_BLOB blob;
326         int len = 0;
327
328         *devmode = NULL;
329
330         len = tdb_unpack(buf, buflen, "B", &data_len, &data);
331         if (!data) {
332                 return len;
333         }
334
335         dm = talloc_zero(mem_ctx, struct spoolss_DeviceMode);
336         if (!dm) {
337                 goto done;
338         }
339
340         blob = data_blob_const(data, data_len);
341
342         ndr_err = ndr_pull_struct_blob(&blob, dm, dm,
343                         (ndr_pull_flags_fn_t)ndr_pull_spoolss_DeviceMode);
344         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
345                 DEBUG(10, ("unpack_devicemode: "
346                            "error parsing spoolss_DeviceMode\n"));
347                 goto done;
348         }
349
350         DEBUG(8, ("Unpacked devicemode [%s](%s)\n",
351                   dm->devicename, dm->formname));
352         if (dm->driverextra_data.data) {
353                 DEBUG(8, ("with a private section of %d bytes\n",
354                           dm->__driverextra_length));
355         }
356
357         *devmode = dm;
358
359 done:
360         SAFE_FREE(data);
361         return len;
362 }
363
364 /***********************************************************************
365  unpack a pjob from a tdb buffer
366 ***********************************************************************/
367
368 int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
369 {
370         int     len = 0;
371         int     used;
372         uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
373         uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
374
375         if ( !buf || !pjob )
376                 return -1;
377
378         len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
379                                 &pjpid,
380                                 &pjsysjob,
381                                 &pjfd,
382                                 &pjstarttime,
383                                 &pjstatus,
384                                 &pjsize,
385                                 &pjpage_count,
386                                 &pjspooled,
387                                 &pjsmbjob,
388                                 pjob->filename,
389                                 pjob->jobname,
390                                 pjob->user,
391                                 pjob->queuename);
392
393         if ( len == -1 )
394                 return -1;
395
396         used = unpack_devicemode(NULL, buf+len, buflen-len, &pjob->devmode);
397         if (used == -1) {
398                 return -1;
399         }
400
401         len += used;
402
403         pjob->pid = pjpid;
404         pjob->sysjob = pjsysjob;
405         pjob->fd = pjfd;
406         pjob->starttime = pjstarttime;
407         pjob->status = pjstatus;
408         pjob->size = pjsize;
409         pjob->page_count = pjpage_count;
410         pjob->spooled = pjspooled;
411         pjob->smbjob = pjsmbjob;
412
413         return len;
414
415 }
416
417 /****************************************************************************
418  Useful function to find a print job in the database.
419 ****************************************************************************/
420
421 static struct printjob *print_job_find(const char *sharename, uint32 jobid)
422 {
423         static struct printjob  pjob;
424         uint32_t tmp;
425         TDB_DATA                ret;
426         struct tdb_print_db     *pdb = get_print_db_byname(sharename);
427
428         DEBUG(10,("print_job_find: looking up job %u for share %s\n",
429                         (unsigned int)jobid, sharename ));
430
431         if (!pdb) {
432                 return NULL;
433         }
434
435         ret = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
436         release_print_db(pdb);
437
438         if (!ret.dptr) {
439                 DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid ));
440                 return NULL;
441         }
442
443         talloc_free(pjob.devmode);
444
445         ZERO_STRUCT( pjob );
446
447         if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
448                 DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid ));
449                 SAFE_FREE(ret.dptr);
450                 return NULL;
451         }
452
453         SAFE_FREE(ret.dptr);
454
455         DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
456                         (int)pjob.sysjob, (unsigned int)jobid ));
457
458         return &pjob;
459 }
460
461 /* Convert a unix jobid to a smb jobid */
462
463 struct unixjob_traverse_state {
464         int sysjob;
465         uint32 sysjob_to_jobid_value;
466 };
467
468 static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
469                                TDB_DATA data, void *private_data)
470 {
471         struct printjob *pjob;
472         struct unixjob_traverse_state *state =
473                 (struct unixjob_traverse_state *)private_data;
474
475         if (!data.dptr || data.dsize == 0)
476                 return 0;
477
478         pjob = (struct printjob *)data.dptr;
479         if (key.dsize != sizeof(uint32))
480                 return 0;
481
482         if (state->sysjob == pjob->sysjob) {
483                 uint32 jobid = IVAL(key.dptr,0);
484
485                 state->sysjob_to_jobid_value = jobid;
486                 return 1;
487         }
488
489         return 0;
490 }
491
492 /****************************************************************************
493  This is a *horribly expensive call as we have to iterate through all the
494  current printer tdb's. Don't do this often ! JRA.
495 ****************************************************************************/
496
497 uint32 sysjob_to_jobid(int unix_jobid)
498 {
499         int services = lp_numservices();
500         int snum;
501         struct unixjob_traverse_state state;
502
503         state.sysjob = unix_jobid;
504         state.sysjob_to_jobid_value = (uint32)-1;
505
506         for (snum = 0; snum < services; snum++) {
507                 struct tdb_print_db *pdb;
508                 if (!lp_print_ok(snum))
509                         continue;
510                 pdb = get_print_db_byname(lp_const_servicename(snum));
511                 if (!pdb) {
512                         continue;
513                 }
514                 tdb_traverse(pdb->tdb, unixjob_traverse_fn, &state);
515                 release_print_db(pdb);
516                 if (state.sysjob_to_jobid_value != (uint32)-1)
517                         return state.sysjob_to_jobid_value;
518         }
519         return (uint32)-1;
520 }
521
522 /****************************************************************************
523  Send notifications based on what has changed after a pjob_store.
524 ****************************************************************************/
525
526 static const struct {
527         uint32 lpq_status;
528         uint32 spoolss_status;
529 } lpq_to_spoolss_status_map[] = {
530         { LPQ_QUEUED, JOB_STATUS_QUEUED },
531         { LPQ_PAUSED, JOB_STATUS_PAUSED },
532         { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
533         { LPQ_PRINTING, JOB_STATUS_PRINTING },
534         { LPQ_DELETING, JOB_STATUS_DELETING },
535         { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
536         { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
537         { LPQ_PRINTED, JOB_STATUS_PRINTED },
538         { LPQ_DELETED, JOB_STATUS_DELETED },
539         { LPQ_BLOCKED, JOB_STATUS_BLOCKED_DEVQ },
540         { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
541         { -1, 0 }
542 };
543
544 /* Convert a lpq status value stored in printing.tdb into the
545    appropriate win32 API constant. */
546
547 static uint32 map_to_spoolss_status(uint32 lpq_status)
548 {
549         int i = 0;
550
551         while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
552                 if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
553                         return lpq_to_spoolss_status_map[i].spoolss_status;
554                 i++;
555         }
556
557         return 0;
558 }
559
560 static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
561                               struct printjob *new_data)
562 {
563         bool new_job = False;
564
565         if (!old_data)
566                 new_job = True;
567
568         /* Job attributes that can't be changed.  We only send
569            notification for these on a new job. */
570
571         /* ACHTUNG!  Due to a bug in Samba's spoolss parsing of the
572            NOTIFY_INFO_DATA buffer, we *have* to send the job submission
573            time first or else we'll end up with potential alignment
574            errors.  I don't think the systemtime should be spooled as
575            a string, but this gets us around that error.
576            --jerry (i'll feel dirty for this) */
577
578         if (new_job) {
579                 notify_job_submitted(sharename, jobid, new_data->starttime);
580                 notify_job_username(sharename, jobid, new_data->user);
581         }
582
583         if (new_job || !strequal(old_data->jobname, new_data->jobname))
584                 notify_job_name(sharename, jobid, new_data->jobname);
585
586         /* Job attributes of a new job or attributes that can be
587            modified. */
588
589         if (new_job || !strequal(old_data->jobname, new_data->jobname))
590                 notify_job_name(sharename, jobid, new_data->jobname);
591
592         if (new_job || old_data->status != new_data->status)
593                 notify_job_status(server_event_context(),
594                                   server_messaging_context(),
595                                   sharename, jobid,
596                                   map_to_spoolss_status(new_data->status));
597
598         if (new_job || old_data->size != new_data->size)
599                 notify_job_total_bytes(server_event_context(),
600                                        server_messaging_context(),
601                                        sharename, jobid, new_data->size);
602
603         if (new_job || old_data->page_count != new_data->page_count)
604                 notify_job_total_pages(server_event_context(),
605                                        server_messaging_context(),
606                                        sharename, jobid, new_data->page_count);
607 }
608
609 /****************************************************************************
610  Store a job structure back to the database.
611 ****************************************************************************/
612
613 static bool pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
614 {
615         uint32_t tmp;
616         TDB_DATA                old_data, new_data;
617         bool                    ret = False;
618         struct tdb_print_db     *pdb = get_print_db_byname(sharename);
619         uint8                   *buf = NULL;
620         int                     len, newlen, buflen;
621
622
623         if (!pdb)
624                 return False;
625
626         /* Get old data */
627
628         old_data = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
629
630         /* Doh!  Now we have to pack/unpack data since the NT_DEVICEMODE was added */
631
632         newlen = 0;
633
634         do {
635                 len = 0;
636                 buflen = newlen;
637                 len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
638                                 (uint32)pjob->pid,
639                                 (uint32)pjob->sysjob,
640                                 (uint32)pjob->fd,
641                                 (uint32)pjob->starttime,
642                                 (uint32)pjob->status,
643                                 (uint32)pjob->size,
644                                 (uint32)pjob->page_count,
645                                 (uint32)pjob->spooled,
646                                 (uint32)pjob->smbjob,
647                                 pjob->filename,
648                                 pjob->jobname,
649                                 pjob->user,
650                                 pjob->queuename);
651
652                 len += pack_devicemode(pjob->devmode, buf+len, buflen-len);
653
654                 if (buflen != len) {
655                         buf = (uint8 *)SMB_REALLOC(buf, len);
656                         if (!buf) {
657                                 DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
658                                 goto done;
659                         }
660                         newlen = len;
661                 }
662         } while ( buflen != len );
663
664
665         /* Store new data */
666
667         new_data.dptr = buf;
668         new_data.dsize = len;
669         ret = (tdb_store(pdb->tdb, print_key(jobid, &tmp), new_data,
670                          TDB_REPLACE) == 0);
671
672         release_print_db(pdb);
673
674         /* Send notify updates for what has changed */
675
676         if ( ret ) {
677                 struct printjob old_pjob;
678
679                 if ( old_data.dsize )
680                 {
681                         if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
682                         {
683                                 pjob_store_notify( sharename, jobid, &old_pjob , pjob );
684                                 talloc_free(old_pjob.devmode);
685                         }
686                 }
687                 else {
688                         /* new job */
689                         pjob_store_notify( sharename, jobid, NULL, pjob );
690                 }
691         }
692
693 done:
694         SAFE_FREE( old_data.dptr );
695         SAFE_FREE( buf );
696
697         return ret;
698 }
699
700 /****************************************************************************
701  Remove a job structure from the database.
702 ****************************************************************************/
703
704 void pjob_delete(const char* sharename, uint32 jobid)
705 {
706         uint32_t tmp;
707         struct printjob *pjob;
708         uint32 job_status = 0;
709         struct tdb_print_db *pdb;
710
711         pdb = get_print_db_byname( sharename );
712
713         if (!pdb)
714                 return;
715
716         pjob = print_job_find( sharename, jobid );
717
718         if (!pjob) {
719                 DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
720                                         (unsigned int)jobid));
721                 release_print_db(pdb);
722                 return;
723         }
724
725         /* We must cycle through JOB_STATUS_DELETING and
726            JOB_STATUS_DELETED for the port monitor to delete the job
727            properly. */
728
729         job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
730         notify_job_status(server_event_context(),
731                           server_messaging_context(),
732                           sharename, jobid, job_status);
733
734         /* Remove from printing.tdb */
735
736         tdb_delete(pdb->tdb, print_key(jobid, &tmp));
737         remove_from_jobs_changed(sharename, jobid);
738         release_print_db( pdb );
739         rap_jobid_delete(sharename, jobid);
740 }
741
742 /****************************************************************************
743  List a unix job in the print database.
744 ****************************************************************************/
745
746 static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
747 {
748         struct printjob pj, *old_pj;
749
750         if (jobid == (uint32)-1)
751                 jobid = q->job + UNIX_JOB_START;
752
753         /* Preserve the timestamp on an existing unix print job */
754
755         old_pj = print_job_find(sharename, jobid);
756
757         ZERO_STRUCT(pj);
758
759         pj.pid = (pid_t)-1;
760         pj.sysjob = q->job;
761         pj.fd = -1;
762         pj.starttime = old_pj ? old_pj->starttime : q->time;
763         pj.status = q->status;
764         pj.size = q->size;
765         pj.spooled = True;
766         fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
767         if (jobid < UNIX_JOB_START) {
768                 pj.smbjob = True;
769                 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
770         } else {
771                 pj.smbjob = False;
772                 fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
773         }
774         fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
775         fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
776
777         pjob_store(sharename, jobid, &pj);
778 }
779
780
781 struct traverse_struct {
782         print_queue_struct *queue;
783         int qcount, snum, maxcount, total_jobs;
784         const char *sharename;
785         time_t lpq_time;
786         const char *lprm_command;
787         struct printif *print_if;
788 };
789
790 /****************************************************************************
791  Utility fn to delete any jobs that are no longer active.
792 ****************************************************************************/
793
794 static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
795 {
796         struct traverse_struct *ts = (struct traverse_struct *)state;
797         struct printjob pjob;
798         uint32 jobid;
799         int i = 0;
800
801         if (  key.dsize != sizeof(jobid) )
802                 return 0;
803
804         jobid = IVAL(key.dptr, 0);
805         if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
806                 return 0;
807         talloc_free(pjob.devmode);
808
809
810         if (!pjob.smbjob) {
811                 /* remove a unix job if it isn't in the system queue any more */
812
813                 for (i=0;i<ts->qcount;i++) {
814                         uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
815                         if (jobid == u_jobid)
816                                 break;
817                 }
818                 if (i == ts->qcount) {
819                         DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
820                                                 (unsigned int)jobid ));
821                         pjob_delete(ts->sharename, jobid);
822                         return 0;
823                 }
824
825                 /* need to continue the the bottom of the function to
826                    save the correct attributes */
827         }
828
829         /* maybe it hasn't been spooled yet */
830         if (!pjob.spooled) {
831                 /* if a job is not spooled and the process doesn't
832                    exist then kill it. This cleans up after smbd
833                    deaths */
834                 if (!process_exists_by_pid(pjob.pid)) {
835                         DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
836                                                 (unsigned int)jobid, (unsigned int)pjob.pid ));
837                         pjob_delete(ts->sharename, jobid);
838                 } else
839                         ts->total_jobs++;
840                 return 0;
841         }
842
843         /* this check only makes sense for jobs submitted from Windows clients */
844
845         if ( pjob.smbjob ) {
846                 for (i=0;i<ts->qcount;i++) {
847                         uint32 curr_jobid;
848
849                         if ( pjob.status == LPQ_DELETED )
850                                 continue;
851
852                         curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
853
854                         if (jobid == curr_jobid) {
855
856                                 /* try to clean up any jobs that need to be deleted */
857
858                                 if ( pjob.status == LPQ_DELETING ) {
859                                         int result;
860
861                                         result = (*(ts->print_if->job_delete))(
862                                                 ts->sharename, ts->lprm_command, &pjob );
863
864                                         if ( result != 0 ) {
865                                                 /* if we can't delete, then reset the job status */
866                                                 pjob.status = LPQ_QUEUED;
867                                                 pjob_store(ts->sharename, jobid, &pjob);
868                                         }
869                                         else {
870                                                 /* if we deleted the job, the remove the tdb record */
871                                                 pjob_delete(ts->sharename, jobid);
872                                                 pjob.status = LPQ_DELETED;
873                                         }
874
875                                 }
876
877                                 break;
878                         }
879                 }
880         }
881
882         /* The job isn't in the system queue - we have to assume it has
883            completed, so delete the database entry. */
884
885         if (i == ts->qcount) {
886
887                 /* A race can occur between the time a job is spooled and
888                    when it appears in the lpq output.  This happens when
889                    the job is added to printing.tdb when another smbd
890                    running print_queue_update() has completed a lpq and
891                    is currently traversing the printing tdb and deleting jobs.
892                    Don't delete the job if it was submitted after the lpq_time. */
893
894                 if (pjob.starttime < ts->lpq_time) {
895                         DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
896                                                 (unsigned int)jobid,
897                                                 (unsigned int)pjob.starttime,
898                                                 (unsigned int)ts->lpq_time ));
899                         pjob_delete(ts->sharename, jobid);
900                 } else
901                         ts->total_jobs++;
902                 return 0;
903         }
904
905         /* Save the pjob attributes we will store.
906            FIXME!!! This is the only place where queue->job
907            represents the SMB jobid      --jerry */
908
909         ts->queue[i].job = jobid;
910         ts->queue[i].size = pjob.size;
911         ts->queue[i].page_count = pjob.page_count;
912         ts->queue[i].status = pjob.status;
913         ts->queue[i].priority = 1;
914         ts->queue[i].time = pjob.starttime;
915         fstrcpy(ts->queue[i].fs_user, pjob.user);
916         fstrcpy(ts->queue[i].fs_file, pjob.jobname);
917
918         ts->total_jobs++;
919
920         return 0;
921 }
922
923 /****************************************************************************
924  Check if the print queue has been updated recently enough.
925 ****************************************************************************/
926
927 static void print_cache_flush(const char *sharename)
928 {
929         fstring key;
930         struct tdb_print_db *pdb = get_print_db_byname(sharename);
931
932         if (!pdb)
933                 return;
934         slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
935         tdb_store_int32(pdb->tdb, key, -1);
936         release_print_db(pdb);
937 }
938
939 /****************************************************************************
940  Check if someone already thinks they are doing the update.
941 ****************************************************************************/
942
943 static pid_t get_updating_pid(const char *sharename)
944 {
945         fstring keystr;
946         TDB_DATA data, key;
947         pid_t updating_pid;
948         struct tdb_print_db *pdb = get_print_db_byname(sharename);
949
950         if (!pdb)
951                 return (pid_t)-1;
952         slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
953         key = string_tdb_data(keystr);
954
955         data = tdb_fetch(pdb->tdb, key);
956         release_print_db(pdb);
957         if (!data.dptr || data.dsize != sizeof(pid_t)) {
958                 SAFE_FREE(data.dptr);
959                 return (pid_t)-1;
960         }
961
962         updating_pid = IVAL(data.dptr, 0);
963         SAFE_FREE(data.dptr);
964
965         if (process_exists_by_pid(updating_pid))
966                 return updating_pid;
967
968         return (pid_t)-1;
969 }
970
971 /****************************************************************************
972  Set the fact that we're doing the update, or have finished doing the update
973  in the tdb.
974 ****************************************************************************/
975
976 static void set_updating_pid(const fstring sharename, bool updating)
977 {
978         fstring keystr;
979         TDB_DATA key;
980         TDB_DATA data;
981         pid_t updating_pid = sys_getpid();
982         uint8 buffer[4];
983
984         struct tdb_print_db *pdb = get_print_db_byname(sharename);
985
986         if (!pdb)
987                 return;
988
989         slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
990         key = string_tdb_data(keystr);
991
992         DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
993                 updating ? "" : "not ",
994                 sharename ));
995
996         if ( !updating ) {
997                 tdb_delete(pdb->tdb, key);
998                 release_print_db(pdb);
999                 return;
1000         }
1001
1002         SIVAL( buffer, 0, updating_pid);
1003         data.dptr = buffer;
1004         data.dsize = 4;         /* we always assume this is a 4 byte value */
1005
1006         tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1007         release_print_db(pdb);
1008 }
1009
1010 /****************************************************************************
1011  Sort print jobs by submittal time.
1012 ****************************************************************************/
1013
1014 static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
1015 {
1016         /* Silly cases */
1017
1018         if (!j1 && !j2)
1019                 return 0;
1020         if (!j1)
1021                 return -1;
1022         if (!j2)
1023                 return 1;
1024
1025         /* Sort on job start time */
1026
1027         if (j1->time == j2->time)
1028                 return 0;
1029         return (j1->time > j2->time) ? 1 : -1;
1030 }
1031
1032 /****************************************************************************
1033  Store the sorted queue representation for later portmon retrieval.
1034  Skip deleted jobs
1035 ****************************************************************************/
1036
1037 static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
1038 {
1039         TDB_DATA data;
1040         int max_reported_jobs = lp_max_reported_jobs(pts->snum);
1041         print_queue_struct *queue = pts->queue;
1042         size_t len;
1043         size_t i;
1044         unsigned int qcount;
1045
1046         if (max_reported_jobs && (max_reported_jobs < pts->qcount))
1047                 pts->qcount = max_reported_jobs;
1048         qcount = 0;
1049
1050         /* Work out the size. */
1051         data.dsize = 0;
1052         data.dsize += tdb_pack(NULL, 0, "d", qcount);
1053
1054         for (i = 0; i < pts->qcount; i++) {
1055                 if ( queue[i].status == LPQ_DELETED )
1056                         continue;
1057
1058                 qcount++;
1059                 data.dsize += tdb_pack(NULL, 0, "ddddddff",
1060                                 (uint32)queue[i].job,
1061                                 (uint32)queue[i].size,
1062                                 (uint32)queue[i].page_count,
1063                                 (uint32)queue[i].status,
1064                                 (uint32)queue[i].priority,
1065                                 (uint32)queue[i].time,
1066                                 queue[i].fs_user,
1067                                 queue[i].fs_file);
1068         }
1069
1070         if ((data.dptr = (uint8 *)SMB_MALLOC(data.dsize)) == NULL)
1071                 return;
1072
1073         len = 0;
1074         len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
1075         for (i = 0; i < pts->qcount; i++) {
1076                 if ( queue[i].status == LPQ_DELETED )
1077                         continue;
1078
1079                 len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
1080                                 (uint32)queue[i].job,
1081                                 (uint32)queue[i].size,
1082                                 (uint32)queue[i].page_count,
1083                                 (uint32)queue[i].status,
1084                                 (uint32)queue[i].priority,
1085                                 (uint32)queue[i].time,
1086                                 queue[i].fs_user,
1087                                 queue[i].fs_file);
1088         }
1089
1090         tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
1091                   TDB_REPLACE);
1092         SAFE_FREE(data.dptr);
1093         return;
1094 }
1095
1096 static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
1097 {
1098         TDB_DATA data;
1099
1100         ZERO_STRUCT(data);
1101
1102         data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
1103         if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
1104                 SAFE_FREE(data.dptr);
1105                 ZERO_STRUCT(data);
1106         }
1107
1108         return data;
1109 }
1110
1111 static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
1112 {
1113         unsigned int i;
1114         unsigned int job_count = data.dsize / 4;
1115
1116         for (i = 0; i < job_count; i++) {
1117                 uint32 ch_jobid;
1118
1119                 ch_jobid = IVAL(data.dptr, i*4);
1120                 if (ch_jobid == jobid)
1121                         remove_from_jobs_changed(sharename, jobid);
1122         }
1123 }
1124
1125 /****************************************************************************
1126  Check if the print queue has been updated recently enough.
1127 ****************************************************************************/
1128
1129 static bool print_cache_expired(const char *sharename, bool check_pending)
1130 {
1131         fstring key;
1132         time_t last_qscan_time, time_now = time(NULL);
1133         struct tdb_print_db *pdb = get_print_db_byname(sharename);
1134         bool result = False;
1135
1136         if (!pdb)
1137                 return False;
1138
1139         snprintf(key, sizeof(key), "CACHE/%s", sharename);
1140         last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
1141
1142         /*
1143          * Invalidate the queue for 3 reasons.
1144          * (1). last queue scan time == -1.
1145          * (2). Current time - last queue scan time > allowed cache time.
1146          * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
1147          * This last test picks up machines for which the clock has been moved
1148          * forward, an lpq scan done and then the clock moved back. Otherwise
1149          * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
1150          */
1151
1152         if (last_qscan_time == ((time_t)-1)
1153                 || (time_now - last_qscan_time) >= lp_lpqcachetime()
1154                 || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
1155         {
1156                 uint32 u;
1157                 time_t msg_pending_time;
1158
1159                 DEBUG(4, ("print_cache_expired: cache expired for queue %s "
1160                         "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
1161                         sharename, (int)last_qscan_time, (int)time_now,
1162                         (int)lp_lpqcachetime() ));
1163
1164                 /* check if another smbd has already sent a message to update the
1165                    queue.  Give the pending message one minute to clear and
1166                    then send another message anyways.  Make sure to check for
1167                    clocks that have been run forward and then back again. */
1168
1169                 snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1170
1171                 if ( check_pending
1172                         && tdb_fetch_uint32( pdb->tdb, key, &u )
1173                         && (msg_pending_time=u) > 0
1174                         && msg_pending_time <= time_now
1175                         && (time_now - msg_pending_time) < 60 )
1176                 {
1177                         DEBUG(4,("print_cache_expired: message already pending for %s.  Accepting cache\n",
1178                                 sharename));
1179                         goto done;
1180                 }
1181
1182                 result = True;
1183         }
1184
1185 done:
1186         release_print_db(pdb);
1187         return result;
1188 }
1189
1190 /****************************************************************************
1191  main work for updating the lpq cahe for a printer queue
1192 ****************************************************************************/
1193
1194 static void print_queue_update_internal( const char *sharename,
1195                                          struct printif *current_printif,
1196                                          char *lpq_command, char *lprm_command )
1197 {
1198         int i, qcount;
1199         print_queue_struct *queue = NULL;
1200         print_status_struct status;
1201         print_status_struct old_status;
1202         struct printjob *pjob;
1203         struct traverse_struct tstruct;
1204         TDB_DATA data, key;
1205         TDB_DATA jcdata;
1206         fstring keystr, cachestr;
1207         struct tdb_print_db *pdb = get_print_db_byname(sharename);
1208
1209         if (!pdb) {
1210                 return;
1211         }
1212
1213         DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
1214                 sharename, current_printif->type, lpq_command));
1215
1216         /*
1217          * Update the cache time FIRST ! Stops others even
1218          * attempting to get the lock and doing this
1219          * if the lpq takes a long time.
1220          */
1221
1222         slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
1223         tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
1224
1225         /* get the current queue using the appropriate interface */
1226         ZERO_STRUCT(status);
1227
1228         qcount = (*(current_printif->queue_get))(sharename,
1229                 current_printif->type,
1230                 lpq_command, &queue, &status);
1231
1232         DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
1233                 qcount, (qcount != 1) ? "s" : "", sharename));
1234
1235         /* Sort the queue by submission time otherwise they are displayed
1236            in hash order. */
1237
1238         TYPESAFE_QSORT(queue, qcount, printjob_comp);
1239
1240         /*
1241           any job in the internal database that is marked as spooled
1242           and doesn't exist in the system queue is considered finished
1243           and removed from the database
1244
1245           any job in the system database but not in the internal database
1246           is added as a unix job
1247
1248           fill in any system job numbers as we go
1249         */
1250
1251         jcdata = get_jobs_changed_data(pdb);
1252
1253         for (i=0; i<qcount; i++) {
1254                 uint32 jobid = print_parse_jobid(queue[i].fs_file);
1255
1256                 if (jobid == (uint32)-1) {
1257                         /* assume its a unix print job */
1258                         print_unix_job(sharename, &queue[i], jobid);
1259                         continue;
1260                 }
1261
1262                 /* we have an active SMB print job - update its status */
1263                 pjob = print_job_find(sharename, jobid);
1264                 if (!pjob) {
1265                         /* err, somethings wrong. Probably smbd was restarted
1266                            with jobs in the queue. All we can do is treat them
1267                            like unix jobs. Pity. */
1268                         print_unix_job(sharename, &queue[i], jobid);
1269                         continue;
1270                 }
1271
1272                 pjob->sysjob = queue[i].job;
1273
1274                 /* don't reset the status on jobs to be deleted */
1275
1276                 if ( pjob->status != LPQ_DELETING )
1277                         pjob->status = queue[i].status;
1278
1279                 pjob_store(sharename, jobid, pjob);
1280
1281                 check_job_changed(sharename, jcdata, jobid);
1282         }
1283
1284         SAFE_FREE(jcdata.dptr);
1285
1286         /* now delete any queued entries that don't appear in the
1287            system queue */
1288         tstruct.queue = queue;
1289         tstruct.qcount = qcount;
1290         tstruct.snum = -1;
1291         tstruct.total_jobs = 0;
1292         tstruct.lpq_time = time(NULL);
1293         tstruct.sharename = sharename;
1294         tstruct.lprm_command = lprm_command;
1295         tstruct.print_if = current_printif;
1296
1297         tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
1298
1299         /* Store the linearised queue, max jobs only. */
1300         store_queue_struct(pdb, &tstruct);
1301
1302         SAFE_FREE(tstruct.queue);
1303
1304         DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
1305                                 sharename, tstruct.total_jobs ));
1306
1307         tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
1308
1309         get_queue_status(sharename, &old_status);
1310         if (old_status.qcount != qcount)
1311                 DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
1312                                         old_status.qcount, qcount, sharename));
1313
1314         /* store the new queue status structure */
1315         slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
1316         key = string_tdb_data(keystr);
1317
1318         status.qcount = qcount;
1319         data.dptr = (uint8 *)&status;
1320         data.dsize = sizeof(status);
1321         tdb_store(pdb->tdb, key, data, TDB_REPLACE);
1322
1323         /*
1324          * Update the cache time again. We want to do this call
1325          * as little as possible...
1326          */
1327
1328         slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
1329         tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
1330
1331         /* clear the msg pending record for this queue */
1332
1333         snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
1334
1335         if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
1336                 /* log a message but continue on */
1337
1338                 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1339                         sharename));
1340         }
1341
1342         release_print_db( pdb );
1343
1344         return;
1345 }
1346
1347 /****************************************************************************
1348  Update the internal database from the system print queue for a queue.
1349  obtain a lock on the print queue before proceeding (needed when mutiple
1350  smbd processes maytry to update the lpq cache concurrently).
1351 ****************************************************************************/
1352
1353 static void print_queue_update_with_lock( const char *sharename,
1354                                           struct printif *current_printif,
1355                                           char *lpq_command, char *lprm_command )
1356 {
1357         fstring keystr;
1358         struct tdb_print_db *pdb;
1359
1360         DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
1361         pdb = get_print_db_byname(sharename);
1362         if (!pdb)
1363                 return;
1364
1365         if ( !print_cache_expired(sharename, False) ) {
1366                 DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
1367                 release_print_db(pdb);
1368                 return;
1369         }
1370
1371         /*
1372          * Check to see if someone else is doing this update.
1373          * This is essentially a mutex on the update.
1374          */
1375
1376         if (get_updating_pid(sharename) != -1) {
1377                 release_print_db(pdb);
1378                 return;
1379         }
1380
1381         /* Lock the queue for the database update */
1382
1383         slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
1384         /* Only wait 10 seconds for this. */
1385         if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
1386                 DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
1387                 release_print_db(pdb);
1388                 return;
1389         }
1390
1391         /*
1392          * Ensure that no one else got in here.
1393          * If the updating pid is still -1 then we are
1394          * the winner.
1395          */
1396
1397         if (get_updating_pid(sharename) != -1) {
1398                 /*
1399                  * Someone else is doing the update, exit.
1400                  */
1401                 tdb_unlock_bystring(pdb->tdb, keystr);
1402                 release_print_db(pdb);
1403                 return;
1404         }
1405
1406         /*
1407          * We're going to do the update ourselves.
1408          */
1409
1410         /* Tell others we're doing the update. */
1411         set_updating_pid(sharename, True);
1412
1413         /*
1414          * Allow others to enter and notice we're doing
1415          * the update.
1416          */
1417
1418         tdb_unlock_bystring(pdb->tdb, keystr);
1419
1420         /* do the main work now */
1421
1422         print_queue_update_internal( sharename, current_printif,
1423                 lpq_command, lprm_command );
1424
1425         /* Delete our pid from the db. */
1426         set_updating_pid(sharename, False);
1427         release_print_db(pdb);
1428 }
1429
1430 /****************************************************************************
1431 this is the receive function of the background lpq updater
1432 ****************************************************************************/
1433 static void print_queue_receive(struct messaging_context *msg,
1434                                 void *private_data,
1435                                 uint32_t msg_type,
1436                                 struct server_id server_id,
1437                                 DATA_BLOB *data)
1438 {
1439         fstring sharename;
1440         char *lpqcommand = NULL, *lprmcommand = NULL;
1441         int printing_type;
1442         size_t len;
1443
1444         len = tdb_unpack( (uint8 *)data->data, data->length, "fdPP",
1445                 sharename,
1446                 &printing_type,
1447                 &lpqcommand,
1448                 &lprmcommand );
1449
1450         if ( len == -1 ) {
1451                 SAFE_FREE(lpqcommand);
1452                 SAFE_FREE(lprmcommand);
1453                 DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
1454                 return;
1455         }
1456
1457         print_queue_update_with_lock(sharename,
1458                 get_printer_fns_from_type((enum printing_types)printing_type),
1459                 lpqcommand, lprmcommand );
1460
1461         SAFE_FREE(lpqcommand);
1462         SAFE_FREE(lprmcommand);
1463         return;
1464 }
1465
1466 static void printing_pause_fd_handler(struct tevent_context *ev,
1467                                       struct tevent_fd *fde,
1468                                       uint16_t flags,
1469                                       void *private_data)
1470 {
1471         /*
1472          * If pause_pipe[1] is closed it means the parent smbd
1473          * and children exited or aborted.
1474          */
1475         exit_server_cleanly(NULL);
1476 }
1477
1478 extern struct child_pid *children;
1479 extern int num_children;
1480
1481 static void add_child_pid(pid_t pid)
1482 {
1483         struct child_pid *child;
1484
1485         child = SMB_MALLOC_P(struct child_pid);
1486         if (child == NULL) {
1487                 DEBUG(0, ("Could not add child struct -- malloc failed\n"));
1488                 return;
1489         }
1490         child->pid = pid;
1491         DLIST_ADD(children, child);
1492         num_children += 1;
1493 }
1494
1495 static pid_t background_lpq_updater_pid = -1;
1496
1497 /****************************************************************************
1498 main thread of the background lpq updater
1499 ****************************************************************************/
1500 void start_background_queue(struct tevent_context *ev,
1501                             struct messaging_context *msg_ctx)
1502 {
1503         /* Use local variables for this as we don't
1504          * need to save the parent side of this, just
1505          * ensure it closes when the process exits.
1506          */
1507         int pause_pipe[2];
1508
1509         DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
1510
1511         if (pipe(pause_pipe) == -1) {
1512                 DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
1513                 exit(1);
1514         }
1515
1516         background_lpq_updater_pid = sys_fork();
1517
1518         if (background_lpq_updater_pid == -1) {
1519                 DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
1520                 exit(1);
1521         }
1522
1523         /* Track the printing pid along with other smbd children */
1524         add_child_pid(background_lpq_updater_pid);
1525
1526         if(background_lpq_updater_pid == 0) {
1527                 struct tevent_fd *fde;
1528                 int ret;
1529                 NTSTATUS status;
1530
1531                 /* Child. */
1532                 DEBUG(5,("start_background_queue: background LPQ thread started\n"));
1533
1534                 close(pause_pipe[0]);
1535                 pause_pipe[0] = -1;
1536
1537                 status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
1538
1539                 if (!NT_STATUS_IS_OK(status)) {
1540                         DEBUG(0,("reinit_after_fork() failed\n"));
1541                         smb_panic("reinit_after_fork() failed");
1542                 }
1543
1544                 smbd_setup_sig_term_handler();
1545                 smbd_setup_sig_hup_handler(ev, msg_ctx);
1546
1547                 if (!serverid_register(procid_self(),
1548                                        FLAG_MSG_GENERAL|FLAG_MSG_SMBD
1549                                        |FLAG_MSG_PRINT_GENERAL)) {
1550                         exit(1);
1551                 }
1552
1553                 if (!locking_init()) {
1554                         exit(1);
1555                 }
1556
1557                 messaging_register(msg_ctx, NULL, MSG_PRINTER_UPDATE,
1558                                    print_queue_receive);
1559
1560                 fde = tevent_add_fd(ev, ev, pause_pipe[1], TEVENT_FD_READ,
1561                                     printing_pause_fd_handler,
1562                                     NULL);
1563                 if (!fde) {
1564                         DEBUG(0,("tevent_add_fd() failed for pause_pipe\n"));
1565                         smb_panic("tevent_add_fd() failed for pause_pipe");
1566                 }
1567
1568                 DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
1569                 ret = tevent_loop_wait(ev);
1570                 /* should not be reached */
1571                 DEBUG(0,("background_queue: tevent_loop_wait() exited with %d - %s\n",
1572                          ret, (ret == 0) ? "out of events" : strerror(errno)));
1573                 exit(1);
1574         }
1575
1576         close(pause_pipe[1]);
1577 }
1578
1579 /****************************************************************************
1580 update the internal database from the system print queue for a queue
1581 ****************************************************************************/
1582
1583 static void print_queue_update(struct messaging_context *msg_ctx,
1584                                int snum, bool force)
1585 {
1586         fstring key;
1587         fstring sharename;
1588         char *lpqcommand = NULL;
1589         char *lprmcommand = NULL;
1590         uint8 *buffer = NULL;
1591         size_t len = 0;
1592         size_t newlen;
1593         struct tdb_print_db *pdb;
1594         int type;
1595         struct printif *current_printif;
1596         TALLOC_CTX *ctx = talloc_tos();
1597
1598         fstrcpy( sharename, lp_const_servicename(snum));
1599
1600         /* don't strip out characters like '$' from the printername */
1601
1602         lpqcommand = talloc_string_sub2(ctx,
1603                         lp_lpqcommand(snum),
1604                         "%p",
1605                         lp_printername(snum),
1606                         false, false, false);
1607         if (!lpqcommand) {
1608                 return;
1609         }
1610         lpqcommand = talloc_sub_advanced(ctx,
1611                         lp_servicename(snum),
1612                         current_user_info.unix_name,
1613                         "",
1614                         current_user.ut.gid,
1615                         get_current_username(),
1616                         current_user_info.domain,
1617                         lpqcommand);
1618         if (!lpqcommand) {
1619                 return;
1620         }
1621
1622         lprmcommand = talloc_string_sub2(ctx,
1623                         lp_lprmcommand(snum),
1624                         "%p",
1625                         lp_printername(snum),
1626                         false, false, false);
1627         if (!lprmcommand) {
1628                 return;
1629         }
1630         lprmcommand = talloc_sub_advanced(ctx,
1631                         lp_servicename(snum),
1632                         current_user_info.unix_name,
1633                         "",
1634                         current_user.ut.gid,
1635                         get_current_username(),
1636                         current_user_info.domain,
1637                         lprmcommand);
1638         if (!lprmcommand) {
1639                 return;
1640         }
1641
1642         /*
1643          * Make sure that the background queue process exists.
1644          * Otherwise just do the update ourselves
1645          */
1646
1647         if ( force || background_lpq_updater_pid == -1 ) {
1648                 DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
1649                 current_printif = get_printer_fns( snum );
1650                 print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
1651
1652                 return;
1653         }
1654
1655         type = lp_printing(snum);
1656
1657         /* get the length */
1658
1659         len = tdb_pack( NULL, 0, "fdPP",
1660                 sharename,
1661                 type,
1662                 lpqcommand,
1663                 lprmcommand );
1664
1665         buffer = SMB_XMALLOC_ARRAY( uint8, len );
1666
1667         /* now pack the buffer */
1668         newlen = tdb_pack( buffer, len, "fdPP",
1669                 sharename,
1670                 type,
1671                 lpqcommand,
1672                 lprmcommand );
1673
1674         SMB_ASSERT( newlen == len );
1675
1676         DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
1677                 "type = %d, lpq command = [%s] lprm command = [%s]\n",
1678                 sharename, type, lpqcommand, lprmcommand ));
1679
1680         /* here we set a msg pending record for other smbd processes
1681            to throttle the number of duplicate print_queue_update msgs
1682            sent.  */
1683
1684         pdb = get_print_db_byname(sharename);
1685         if (!pdb) {
1686                 SAFE_FREE(buffer);
1687                 return;
1688         }
1689
1690         snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
1691
1692         if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
1693                 /* log a message but continue on */
1694
1695                 DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
1696                         sharename));
1697         }
1698
1699         release_print_db( pdb );
1700
1701         /* finally send the message */
1702
1703         messaging_send_buf(msg_ctx, pid_to_procid(background_lpq_updater_pid),
1704                            MSG_PRINTER_UPDATE, (uint8 *)buffer, len);
1705
1706         SAFE_FREE( buffer );
1707
1708         return;
1709 }
1710
1711 /****************************************************************************
1712  Create/Update an entry in the print tdb that will allow us to send notify
1713  updates only to interested smbd's.
1714 ****************************************************************************/
1715
1716 bool print_notify_register_pid(int snum)
1717 {
1718         TDB_DATA data;
1719         struct tdb_print_db *pdb = NULL;
1720         TDB_CONTEXT *tdb = NULL;
1721         const char *printername;
1722         uint32 mypid = (uint32)sys_getpid();
1723         bool ret = False;
1724         size_t i;
1725
1726         /* if (snum == -1), then the change notify request was
1727            on a print server handle and we need to register on
1728            all print queus */
1729
1730         if (snum == -1)
1731         {
1732                 int num_services = lp_numservices();
1733                 int idx;
1734
1735                 for ( idx=0; idx<num_services; idx++ ) {
1736                         if (lp_snum_ok(idx) && lp_print_ok(idx) )
1737                                 print_notify_register_pid(idx);
1738                 }
1739
1740                 return True;
1741         }
1742         else /* register for a specific printer */
1743         {
1744                 printername = lp_const_servicename(snum);
1745                 pdb = get_print_db_byname(printername);
1746                 if (!pdb)
1747                         return False;
1748                 tdb = pdb->tdb;
1749         }
1750
1751         if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1752                 DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
1753                                         printername));
1754                 if (pdb)
1755                         release_print_db(pdb);
1756                 return False;
1757         }
1758
1759         data = get_printer_notify_pid_list( tdb, printername, True );
1760
1761         /* Add ourselves and increase the refcount. */
1762
1763         for (i = 0; i < data.dsize; i += 8) {
1764                 if (IVAL(data.dptr,i) == mypid) {
1765                         uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
1766                         SIVAL(data.dptr, i+4, new_refcount);
1767                         break;
1768                 }
1769         }
1770
1771         if (i == data.dsize) {
1772                 /* We weren't in the list. Realloc. */
1773                 data.dptr = (uint8 *)SMB_REALLOC(data.dptr, data.dsize + 8);
1774                 if (!data.dptr) {
1775                         DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
1776                                                 printername));
1777                         goto done;
1778                 }
1779                 data.dsize += 8;
1780                 SIVAL(data.dptr,data.dsize - 8,mypid);
1781                 SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
1782         }
1783
1784         /* Store back the record. */
1785         if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1786                 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1787 list for printer %s\n", printername));
1788                 goto done;
1789         }
1790
1791         ret = True;
1792
1793  done:
1794
1795         tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1796         if (pdb)
1797                 release_print_db(pdb);
1798         SAFE_FREE(data.dptr);
1799         return ret;
1800 }
1801
1802 /****************************************************************************
1803  Update an entry in the print tdb that will allow us to send notify
1804  updates only to interested smbd's.
1805 ****************************************************************************/
1806
1807 bool print_notify_deregister_pid(int snum)
1808 {
1809         TDB_DATA data;
1810         struct tdb_print_db *pdb = NULL;
1811         TDB_CONTEXT *tdb = NULL;
1812         const char *printername;
1813         uint32 mypid = (uint32)sys_getpid();
1814         size_t i;
1815         bool ret = False;
1816
1817         /* if ( snum == -1 ), we are deregister a print server handle
1818            which means to deregister on all print queues */
1819
1820         if (snum == -1)
1821         {
1822                 int num_services = lp_numservices();
1823                 int idx;
1824
1825                 for ( idx=0; idx<num_services; idx++ ) {
1826                         if ( lp_snum_ok(idx) && lp_print_ok(idx) )
1827                                 print_notify_deregister_pid(idx);
1828                 }
1829
1830                 return True;
1831         }
1832         else /* deregister a specific printer */
1833         {
1834                 printername = lp_const_servicename(snum);
1835                 pdb = get_print_db_byname(printername);
1836                 if (!pdb)
1837                         return False;
1838                 tdb = pdb->tdb;
1839         }
1840
1841         if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
1842                 DEBUG(0,("print_notify_register_pid: Failed to lock \
1843 printer %s database\n", printername));
1844                 if (pdb)
1845                         release_print_db(pdb);
1846                 return False;
1847         }
1848
1849         data = get_printer_notify_pid_list( tdb, printername, True );
1850
1851         /* Reduce refcount. Remove ourselves if zero. */
1852
1853         for (i = 0; i < data.dsize; ) {
1854                 if (IVAL(data.dptr,i) == mypid) {
1855                         uint32 refcount = IVAL(data.dptr, i+4);
1856
1857                         refcount--;
1858
1859                         if (refcount == 0) {
1860                                 if (data.dsize - i > 8)
1861                                         memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
1862                                 data.dsize -= 8;
1863                                 continue;
1864                         }
1865                         SIVAL(data.dptr, i+4, refcount);
1866                 }
1867
1868                 i += 8;
1869         }
1870
1871         if (data.dsize == 0)
1872                 SAFE_FREE(data.dptr);
1873
1874         /* Store back the record. */
1875         if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
1876                 DEBUG(0,("print_notify_register_pid: Failed to update pid \
1877 list for printer %s\n", printername));
1878                 goto done;
1879         }
1880
1881         ret = True;
1882
1883   done:
1884
1885         tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
1886         if (pdb)
1887                 release_print_db(pdb);
1888         SAFE_FREE(data.dptr);
1889         return ret;
1890 }
1891
1892 /****************************************************************************
1893  Check if a jobid is valid. It is valid if it exists in the database.
1894 ****************************************************************************/
1895
1896 bool print_job_exists(const char* sharename, uint32 jobid)
1897 {
1898         struct tdb_print_db *pdb = get_print_db_byname(sharename);
1899         bool ret;
1900         uint32_t tmp;
1901
1902         if (!pdb)
1903                 return False;
1904         ret = tdb_exists(pdb->tdb, print_key(jobid, &tmp));
1905         release_print_db(pdb);
1906         return ret;
1907 }
1908
1909 /****************************************************************************
1910  Give the filename used for a jobid.
1911  Only valid for the process doing the spooling and when the job
1912  has not been spooled.
1913 ****************************************************************************/
1914
1915 char *print_job_fname(const char* sharename, uint32 jobid)
1916 {
1917         struct printjob *pjob = print_job_find(sharename, jobid);
1918         if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
1919                 return NULL;
1920         return pjob->filename;
1921 }
1922
1923
1924 /****************************************************************************
1925  Give the filename used for a jobid.
1926  Only valid for the process doing the spooling and when the job
1927  has not been spooled.
1928 ****************************************************************************/
1929
1930 struct spoolss_DeviceMode *print_job_devmode(const char* sharename, uint32 jobid)
1931 {
1932         struct printjob *pjob = print_job_find(sharename, jobid);
1933
1934         if ( !pjob )
1935                 return NULL;
1936
1937         return pjob->devmode;
1938 }
1939
1940 /****************************************************************************
1941  Set the name of a job. Only possible for owner.
1942 ****************************************************************************/
1943
1944 bool print_job_set_name(const char *sharename, uint32 jobid, const char *name)
1945 {
1946         struct printjob *pjob;
1947
1948         pjob = print_job_find(sharename, jobid);
1949         if (!pjob || pjob->pid != sys_getpid())
1950                 return False;
1951
1952         fstrcpy(pjob->jobname, name);
1953         return pjob_store(sharename, jobid, pjob);
1954 }
1955
1956 /****************************************************************************
1957  Get the name of a job. Only possible for owner.
1958 ****************************************************************************/
1959
1960 bool print_job_get_name(TALLOC_CTX *mem_ctx, const char *sharename, uint32_t jobid, char **name)
1961 {
1962         struct printjob *pjob;
1963
1964         pjob = print_job_find(sharename, jobid);
1965         if (!pjob || pjob->pid != sys_getpid()) {
1966                 return false;
1967         }
1968
1969         *name = talloc_strdup(mem_ctx, pjob->jobname);
1970         if (!*name) {
1971                 return false;
1972         }
1973
1974         return true;
1975 }
1976
1977
1978 /***************************************************************************
1979  Remove a jobid from the 'jobs changed' list.
1980 ***************************************************************************/
1981
1982 static bool remove_from_jobs_changed(const char* sharename, uint32 jobid)
1983 {
1984         struct tdb_print_db *pdb = get_print_db_byname(sharename);
1985         TDB_DATA data, key;
1986         size_t job_count, i;
1987         bool ret = False;
1988         bool gotlock = False;
1989
1990         if (!pdb) {
1991                 return False;
1992         }
1993
1994         ZERO_STRUCT(data);
1995
1996         key = string_tdb_data("INFO/jobs_changed");
1997
1998         if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
1999                 goto out;
2000
2001         gotlock = True;
2002
2003         data = tdb_fetch(pdb->tdb, key);
2004
2005         if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
2006                 goto out;
2007
2008         job_count = data.dsize / 4;
2009         for (i = 0; i < job_count; i++) {
2010                 uint32 ch_jobid;
2011
2012                 ch_jobid = IVAL(data.dptr, i*4);
2013                 if (ch_jobid == jobid) {
2014                         if (i < job_count -1 )
2015                                 memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
2016                         data.dsize -= 4;
2017                         if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
2018                                 goto out;
2019                         break;
2020                 }
2021         }
2022
2023         ret = True;
2024   out:
2025
2026         if (gotlock)
2027                 tdb_chainunlock(pdb->tdb, key);
2028         SAFE_FREE(data.dptr);
2029         release_print_db(pdb);
2030         if (ret)
2031                 DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
2032         else
2033                 DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
2034         return ret;
2035 }
2036
2037 /****************************************************************************
2038  Delete a print job - don't update queue.
2039 ****************************************************************************/
2040
2041 static bool print_job_delete1(int snum, uint32 jobid)
2042 {
2043         const char* sharename = lp_const_servicename(snum);
2044         struct printjob *pjob = print_job_find(sharename, jobid);
2045         int result = 0;
2046         struct printif *current_printif = get_printer_fns( snum );
2047
2048         if (!pjob)
2049                 return False;
2050
2051         /*
2052          * If already deleting just return.
2053          */
2054
2055         if (pjob->status == LPQ_DELETING)
2056                 return True;
2057
2058         /* Hrm - we need to be able to cope with deleting a job before it
2059            has reached the spooler.  Just mark it as LPQ_DELETING and
2060            let the print_queue_update() code rmeove the record */
2061
2062
2063         if (pjob->sysjob == -1) {
2064                 DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
2065         }
2066
2067         /* Set the tdb entry to be deleting. */
2068
2069         pjob->status = LPQ_DELETING;
2070         pjob_store(sharename, jobid, pjob);
2071
2072         if (pjob->spooled && pjob->sysjob != -1)
2073         {
2074                 result = (*(current_printif->job_delete))(
2075                         lp_printername(snum),
2076                         lp_lprmcommand(snum),
2077                         pjob);
2078
2079                 /* Delete the tdb entry if the delete succeeded or the job hasn't
2080                    been spooled. */
2081
2082                 if (result == 0) {
2083                         struct tdb_print_db *pdb = get_print_db_byname(sharename);
2084                         int njobs = 1;
2085
2086                         if (!pdb)
2087                                 return False;
2088                         pjob_delete(sharename, jobid);
2089                         /* Ensure we keep a rough count of the number of total jobs... */
2090                         tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
2091                         release_print_db(pdb);
2092                 }
2093         }
2094
2095         remove_from_jobs_changed( sharename, jobid );
2096
2097         return (result == 0);
2098 }
2099
2100 /****************************************************************************
2101  Return true if the current user owns the print job.
2102 ****************************************************************************/
2103
2104 static bool is_owner(struct auth_serversupplied_info *server_info,
2105                      const char *servicename,
2106                      uint32 jobid)
2107 {
2108         struct printjob *pjob = print_job_find(servicename, jobid);
2109
2110         if (!pjob || !server_info)
2111                 return False;
2112
2113         return strequal(pjob->user, server_info->sanitized_username);
2114 }
2115
2116 /****************************************************************************
2117  Delete a print job.
2118 ****************************************************************************/
2119
2120 WERROR print_job_delete(struct auth_serversupplied_info *server_info,
2121                         struct messaging_context *msg_ctx,
2122                         int snum, uint32_t jobid)
2123 {
2124         const char* sharename = lp_const_servicename(snum);
2125         struct printjob *pjob;
2126         bool    owner;
2127         char    *fname;
2128
2129         owner = is_owner(server_info, lp_const_servicename(snum), jobid);
2130
2131         /* Check access against security descriptor or whether the user
2132            owns their job. */
2133
2134         if (!owner &&
2135             !print_access_check(server_info, msg_ctx, snum,
2136                                 JOB_ACCESS_ADMINISTER)) {
2137                 DEBUG(3, ("delete denied by security descriptor\n"));
2138
2139                 /* BEGIN_ADMIN_LOG */
2140                 sys_adminlog( LOG_ERR,
2141                               "Permission denied-- user not allowed to delete, \
2142 pause, or resume print job. User name: %s. Printer name: %s.",
2143                               uidtoname(server_info->utok.uid),
2144                               lp_printername(snum) );
2145                 /* END_ADMIN_LOG */
2146
2147                 return WERR_ACCESS_DENIED;
2148         }
2149
2150         /*
2151          * get the spooled filename of the print job
2152          * if this works, then the file has not been spooled
2153          * to the underlying print system.  Just delete the
2154          * spool file & return.
2155          */
2156
2157         fname = print_job_fname(sharename, jobid);
2158         if (fname != NULL) {
2159                 /* remove the spool file */
2160                 DEBUG(10, ("print_job_delete: "
2161                            "Removing spool file [%s]\n", fname));
2162                 if (unlink(fname) == -1) {
2163                         return map_werror_from_unix(errno);
2164                 }
2165         }
2166
2167         if (!print_job_delete1(snum, jobid)) {
2168                 return WERR_ACCESS_DENIED;
2169         }
2170
2171         /* force update the database and say the delete failed if the
2172            job still exists */
2173
2174         print_queue_update(msg_ctx, snum, True);
2175
2176         pjob = print_job_find(sharename, jobid);
2177         if (pjob && (pjob->status != LPQ_DELETING)) {
2178                 return WERR_ACCESS_DENIED;
2179         }
2180
2181         return WERR_PRINTER_HAS_JOBS_QUEUED;
2182 }
2183
2184 /****************************************************************************
2185  Pause a job.
2186 ****************************************************************************/
2187
2188 bool print_job_pause(struct auth_serversupplied_info *server_info,
2189                      struct messaging_context *msg_ctx,
2190                      int snum, uint32 jobid, WERROR *errcode)
2191 {
2192         const char* sharename = lp_const_servicename(snum);
2193         struct printjob *pjob;
2194         int ret = -1;
2195         struct printif *current_printif = get_printer_fns( snum );
2196
2197         pjob = print_job_find(sharename, jobid);
2198
2199         if (!pjob || !server_info) {
2200                 DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
2201                         (unsigned int)jobid ));
2202                 return False;
2203         }
2204
2205         if (!pjob->spooled || pjob->sysjob == -1) {
2206                 DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
2207                         (int)pjob->sysjob, (unsigned int)jobid ));
2208                 return False;
2209         }
2210
2211         if (!is_owner(server_info, lp_const_servicename(snum), jobid) &&
2212             !print_access_check(server_info, msg_ctx, snum,
2213                                 JOB_ACCESS_ADMINISTER)) {
2214                 DEBUG(3, ("pause denied by security descriptor\n"));
2215
2216                 /* BEGIN_ADMIN_LOG */
2217                 sys_adminlog( LOG_ERR,
2218                         "Permission denied-- user not allowed to delete, \
2219 pause, or resume print job. User name: %s. Printer name: %s.",
2220                               uidtoname(server_info->utok.uid),
2221                               lp_printername(snum) );
2222                 /* END_ADMIN_LOG */
2223
2224                 *errcode = WERR_ACCESS_DENIED;
2225                 return False;
2226         }
2227
2228         /* need to pause the spooled entry */
2229         ret = (*(current_printif->job_pause))(snum, pjob);
2230
2231         if (ret != 0) {
2232                 *errcode = WERR_INVALID_PARAM;
2233                 return False;
2234         }
2235
2236         /* force update the database */
2237         print_cache_flush(lp_const_servicename(snum));
2238
2239         /* Send a printer notify message */
2240
2241         notify_job_status(server_event_context(), msg_ctx, sharename, jobid,
2242                           JOB_STATUS_PAUSED);
2243
2244         /* how do we tell if this succeeded? */
2245
2246         return True;
2247 }
2248
2249 /****************************************************************************
2250  Resume a job.
2251 ****************************************************************************/
2252
2253 bool print_job_resume(struct auth_serversupplied_info *server_info,
2254                       struct messaging_context *msg_ctx,
2255                       int snum, uint32 jobid, WERROR *errcode)
2256 {
2257         const char *sharename = lp_const_servicename(snum);
2258         struct printjob *pjob;
2259         int ret;
2260         struct printif *current_printif = get_printer_fns( snum );
2261
2262         pjob = print_job_find(sharename, jobid);
2263
2264         if (!pjob || !server_info) {
2265                 DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
2266                         (unsigned int)jobid ));
2267                 return False;
2268         }
2269
2270         if (!pjob->spooled || pjob->sysjob == -1) {
2271                 DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
2272                         (int)pjob->sysjob, (unsigned int)jobid ));
2273                 return False;
2274         }
2275
2276         if (!is_owner(server_info, lp_const_servicename(snum), jobid) &&
2277             !print_access_check(server_info, msg_ctx, snum,
2278                                 JOB_ACCESS_ADMINISTER)) {
2279                 DEBUG(3, ("resume denied by security descriptor\n"));
2280                 *errcode = WERR_ACCESS_DENIED;
2281
2282                 /* BEGIN_ADMIN_LOG */
2283                 sys_adminlog( LOG_ERR,
2284                          "Permission denied-- user not allowed to delete, \
2285 pause, or resume print job. User name: %s. Printer name: %s.",
2286                               uidtoname(server_info->utok.uid),
2287                               lp_printername(snum) );
2288                 /* END_ADMIN_LOG */
2289                 return False;
2290         }
2291
2292         ret = (*(current_printif->job_resume))(snum, pjob);
2293
2294         if (ret != 0) {
2295                 *errcode = WERR_INVALID_PARAM;
2296                 return False;
2297         }
2298
2299         /* force update the database */
2300         print_cache_flush(lp_const_servicename(snum));
2301
2302         /* Send a printer notify message */
2303
2304         notify_job_status(server_event_context(), msg_ctx, sharename, jobid,
2305                           JOB_STATUS_QUEUED);
2306
2307         return True;
2308 }
2309
2310 /****************************************************************************
2311  Write to a print file.
2312 ****************************************************************************/
2313
2314 ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
2315 {
2316         const char* sharename = lp_const_servicename(snum);
2317         ssize_t return_code;
2318         struct printjob *pjob;
2319
2320         pjob = print_job_find(sharename, jobid);
2321
2322         if (!pjob)
2323                 return -1;
2324         /* don't allow another process to get this info - it is meaningless */
2325         if (pjob->pid != sys_getpid())
2326                 return -1;
2327
2328         /* if SMBD is spooling this can't be allowed */
2329         if (pjob->status == PJOB_SMBD_SPOOLING) {
2330                 return -1;
2331         }
2332
2333         return_code = write_data_at_offset(pjob->fd, buf, size, pos);
2334
2335         if (return_code>0) {
2336                 pjob->size += size;
2337                 pjob_store(sharename, jobid, pjob);
2338         }
2339         return return_code;
2340 }
2341
2342 /****************************************************************************
2343  Get the queue status - do not update if db is out of date.
2344 ****************************************************************************/
2345
2346 static int get_queue_status(const char* sharename, print_status_struct *status)
2347 {
2348         fstring keystr;
2349         TDB_DATA data;
2350         struct tdb_print_db *pdb = get_print_db_byname(sharename);
2351         int len;
2352
2353         if (status) {
2354                 ZERO_STRUCTP(status);
2355         }
2356
2357         if (!pdb)
2358                 return 0;
2359
2360         if (status) {
2361                 fstr_sprintf(keystr, "STATUS/%s", sharename);
2362                 data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
2363                 if (data.dptr) {
2364                         if (data.dsize == sizeof(print_status_struct))
2365                                 /* this memcpy is ok since the status struct was
2366                                    not packed before storing it in the tdb */
2367                                 memcpy(status, data.dptr, sizeof(print_status_struct));
2368                         SAFE_FREE(data.dptr);
2369                 }
2370         }
2371         len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
2372         release_print_db(pdb);
2373         return (len == -1 ? 0 : len);
2374 }
2375
2376 /****************************************************************************
2377  Determine the number of jobs in a queue.
2378 ****************************************************************************/
2379
2380 int print_queue_length(struct messaging_context *msg_ctx, int snum,
2381                        print_status_struct *pstatus)
2382 {
2383         const char* sharename = lp_const_servicename( snum );
2384         print_status_struct status;
2385         int len;
2386
2387         ZERO_STRUCT( status );
2388
2389         /* make sure the database is up to date */
2390         if (print_cache_expired(lp_const_servicename(snum), True))
2391                 print_queue_update(msg_ctx, snum, False);
2392
2393         /* also fetch the queue status */
2394         memset(&status, 0, sizeof(status));
2395         len = get_queue_status(sharename, &status);
2396
2397         if (pstatus)
2398                 *pstatus = status;
2399
2400         return len;
2401 }
2402
2403 /***************************************************************************
2404  Allocate a jobid. Hold the lock for as short a time as possible.
2405 ***************************************************************************/
2406
2407 static WERROR allocate_print_jobid(struct tdb_print_db *pdb, int snum,
2408                                    const char *sharename, uint32 *pjobid)
2409 {
2410         int i;
2411         uint32 jobid;
2412         enum TDB_ERROR terr;
2413         int ret;
2414
2415         *pjobid = (uint32)-1;
2416
2417         for (i = 0; i < 3; i++) {
2418                 /* Lock the database - only wait 20 seconds. */
2419                 ret = tdb_lock_bystring_with_timeout(pdb->tdb,
2420                                                      "INFO/nextjob", 20);
2421                 if (ret == -1) {
2422                         DEBUG(0, ("allocate_print_jobid: "
2423                                   "Failed to lock printing database %s\n",
2424                                   sharename));
2425                         terr = tdb_error(pdb->tdb);
2426                         return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2427                 }
2428
2429                 if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
2430                         terr = tdb_error(pdb->tdb);
2431                         if (terr != TDB_ERR_NOEXIST) {
2432                                 DEBUG(0, ("allocate_print_jobid: "
2433                                           "Failed to fetch INFO/nextjob "
2434                                           "for print queue %s\n", sharename));
2435                                 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2436                                 return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2437                         }
2438                         DEBUG(10, ("allocate_print_jobid: "
2439                                    "No existing jobid in %s\n", sharename));
2440                         jobid = 0;
2441                 }
2442
2443                 DEBUG(10, ("allocate_print_jobid: "
2444                            "Read jobid %u from %s\n", jobid, sharename));
2445
2446                 jobid = NEXT_JOBID(jobid);
2447
2448                 ret = tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid);
2449                 if (ret == -1) {
2450                         terr = tdb_error(pdb->tdb);
2451                         DEBUG(3, ("allocate_print_jobid: "
2452                                   "Failed to store INFO/nextjob.\n"));
2453                         tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2454                         return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2455                 }
2456
2457                 /* We've finished with the INFO/nextjob lock. */
2458                 tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
2459
2460                 if (!print_job_exists(sharename, jobid)) {
2461                         break;
2462                 }
2463                 DEBUG(10, ("allocate_print_jobid: "
2464                            "Found jobid %u in %s\n", jobid, sharename));
2465         }
2466
2467         if (i > 2) {
2468                 DEBUG(0, ("allocate_print_jobid: "
2469                           "Failed to allocate a print job for queue %s\n",
2470                           sharename));
2471                 /* Probably full... */
2472                 return WERR_NO_SPOOL_SPACE;
2473         }
2474
2475         /* Store a dummy placeholder. */
2476         {
2477                 uint32_t tmp;
2478                 TDB_DATA dum;
2479                 dum.dptr = NULL;
2480                 dum.dsize = 0;
2481                 if (tdb_store(pdb->tdb, print_key(jobid, &tmp), dum,
2482                               TDB_INSERT) == -1) {
2483                         DEBUG(3, ("allocate_print_jobid: "
2484                                   "jobid (%d) failed to store placeholder.\n",
2485                                   jobid ));
2486                         terr = tdb_error(pdb->tdb);
2487                         return ntstatus_to_werror(map_nt_error_from_tdb(terr));
2488                 }
2489         }
2490
2491         *pjobid = jobid;
2492         return WERR_OK;
2493 }
2494
2495 /***************************************************************************
2496  Append a jobid to the 'jobs changed' list.
2497 ***************************************************************************/
2498
2499 static bool add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
2500 {
2501         TDB_DATA data;
2502         uint32 store_jobid;
2503
2504         SIVAL(&store_jobid, 0, jobid);
2505         data.dptr = (uint8 *)&store_jobid;
2506         data.dsize = 4;
2507
2508         DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
2509
2510         return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
2511                            data) == 0);
2512 }
2513
2514
2515 /***************************************************************************
2516  Do all checks needed to determine if we can start a job.
2517 ***************************************************************************/
2518
2519 static WERROR print_job_checks(struct auth_serversupplied_info *server_info,
2520                                struct messaging_context *msg_ctx,
2521                                int snum, int *njobs)
2522 {
2523         const char *sharename = lp_const_servicename(snum);
2524         uint64_t dspace, dsize;
2525         uint64_t minspace;
2526         int ret;
2527
2528         if (!print_access_check(server_info, msg_ctx, snum,
2529                                 PRINTER_ACCESS_USE)) {
2530                 DEBUG(3, ("print_job_checks: "
2531                           "job start denied by security descriptor\n"));
2532                 return WERR_ACCESS_DENIED;
2533         }
2534
2535         if (!print_time_access_check(server_info, msg_ctx, sharename)) {
2536                 DEBUG(3, ("print_job_checks: "
2537                           "job start denied by time check\n"));
2538                 return WERR_ACCESS_DENIED;
2539         }
2540
2541         /* see if we have sufficient disk space */
2542         if (lp_minprintspace(snum)) {
2543                 minspace = lp_minprintspace(snum);
2544                 ret = sys_fsusage(lp_pathname(snum), &dspace, &dsize);
2545                 if (ret == 0 && dspace < 2*minspace) {
2546                         DEBUG(3, ("print_job_checks: "
2547                                   "disk space check failed.\n"));
2548                         return WERR_NO_SPOOL_SPACE;
2549                 }
2550         }
2551
2552         /* for autoloaded printers, check that the printcap entry still exists */
2553         if (lp_autoloaded(snum) && !pcap_printername_ok(sharename)) {
2554                 DEBUG(3, ("print_job_checks: printer name %s check failed.\n",
2555                           sharename));
2556                 return WERR_ACCESS_DENIED;
2557         }
2558
2559         /* Insure the maximum queue size is not violated */
2560         *njobs = print_queue_length(msg_ctx, snum, NULL);
2561         if (*njobs > lp_maxprintjobs(snum)) {
2562                 DEBUG(3, ("print_job_checks: Queue %s number of jobs (%d) "
2563                           "larger than max printjobs per queue (%d).\n",
2564                           sharename, *njobs, lp_maxprintjobs(snum)));
2565                 return WERR_NO_SPOOL_SPACE;
2566         }
2567
2568         return WERR_OK;
2569 }
2570
2571 /***************************************************************************
2572  Create a job file.
2573 ***************************************************************************/
2574
2575 static WERROR print_job_spool_file(int snum, uint32_t jobid,
2576                                    const char *output_file,
2577                                    struct printjob *pjob)
2578 {
2579         WERROR werr;
2580         SMB_STRUCT_STAT st;
2581         const char *path;
2582         int len;
2583
2584         /* if this file is within the printer path, it means that smbd
2585          * is spooling it and will pass us control when it is finished.
2586          * Verify that the file name is ok, within path, and it is
2587          * already already there */
2588         if (output_file) {
2589                 path = lp_pathname(snum);
2590                 len = strlen(path);
2591                 if (strncmp(output_file, path, len) == 0 &&
2592                     (output_file[len - 1] == '/' || output_file[len] == '/')) {
2593
2594                         /* verify path is not too long */
2595                         if (strlen(output_file) >= sizeof(pjob->filename)) {
2596                                 return WERR_INVALID_NAME;
2597                         }
2598
2599                         /* verify that the file exists */
2600                         if (sys_stat(output_file, &st, false) != 0) {
2601                                 return WERR_INVALID_NAME;
2602                         }
2603
2604                         fstrcpy(pjob->filename, output_file);
2605
2606                         DEBUG(3, ("print_job_spool_file:"
2607                                   "External spooling activated"));
2608
2609                         /* we do not open the file until spooling is done */
2610                         pjob->fd = -1;
2611                         pjob->status = PJOB_SMBD_SPOOLING;
2612
2613                         return WERR_OK;
2614                 }
2615         }
2616
2617         slprintf(pjob->filename, sizeof(pjob->filename)-1,
2618                  "%s/%s%.8u.XXXXXX", lp_pathname(snum),
2619                  PRINT_SPOOL_PREFIX, (unsigned int)jobid);
2620         pjob->fd = mkstemp(pjob->filename);
2621
2622         if (pjob->fd == -1) {
2623                 werr = map_werror_from_unix(errno);
2624                 if (W_ERROR_EQUAL(werr, WERR_ACCESS_DENIED)) {
2625                         /* Common setup error, force a report. */
2626                         DEBUG(0, ("print_job_spool_file: "
2627                                   "insufficient permissions to open spool "
2628                                   "file %s.\n", pjob->filename));
2629                 } else {
2630                         /* Normal case, report at level 3 and above. */
2631                         DEBUG(3, ("print_job_spool_file: "
2632                                   "can't open spool file %s\n",
2633                                   pjob->filename));
2634                 }
2635                 return werr;
2636         }
2637
2638         return WERR_OK;
2639 }
2640
2641 /***************************************************************************
2642  Start spooling a job - return the jobid.
2643 ***************************************************************************/
2644
2645 WERROR print_job_start(struct auth_serversupplied_info *server_info,
2646                        struct messaging_context *msg_ctx,
2647                        int snum, const char *docname, const char *filename,
2648                        struct spoolss_DeviceMode *devmode, uint32_t *_jobid)
2649 {
2650         uint32_t jobid;
2651         char *path;
2652         struct printjob pjob;
2653         const char *sharename = lp_const_servicename(snum);
2654         struct tdb_print_db *pdb = get_print_db_byname(sharename);
2655         int njobs;
2656         WERROR werr;
2657
2658         if (!pdb) {
2659                 return WERR_INTERNAL_DB_CORRUPTION;
2660         }
2661
2662         path = lp_pathname(snum);
2663
2664         werr = print_job_checks(server_info, msg_ctx, snum, &njobs);
2665         if (!W_ERROR_IS_OK(werr)) {
2666                 release_print_db(pdb);
2667                 return werr;
2668         }
2669
2670         DEBUG(10, ("print_job_start: "
2671                    "Queue %s number of jobs (%d), max printjobs = %d\n",
2672                    sharename, njobs, lp_maxprintjobs(snum)));
2673
2674         werr = allocate_print_jobid(pdb, snum, sharename, &jobid);
2675         if (!W_ERROR_IS_OK(werr)) {
2676                 goto fail;
2677         }
2678
2679         /* create the database entry */
2680
2681         ZERO_STRUCT(pjob);
2682
2683         pjob.pid = sys_getpid();
2684         pjob.sysjob = -1;
2685         pjob.fd = -1;
2686         pjob.starttime = time(NULL);
2687         pjob.status = LPQ_SPOOLING;
2688         pjob.size = 0;
2689         pjob.spooled = False;
2690         pjob.smbjob = True;
2691         pjob.devmode = devmode;
2692
2693         fstrcpy(pjob.jobname, docname);
2694
2695         fstrcpy(pjob.user, lp_printjob_username(snum));
2696         standard_sub_advanced(sharename, server_info->sanitized_username,
2697                               path, server_info->utok.gid,
2698                               server_info->sanitized_username,
2699                               server_info->info3->base.domain.string,
2700                               pjob.user, sizeof(pjob.user)-1);
2701         /* ensure NULL termination */
2702         pjob.user[sizeof(pjob.user)-1] = '\0';
2703
2704         fstrcpy(pjob.queuename, lp_const_servicename(snum));
2705
2706         /* we have a job entry - now create the spool file */
2707         werr = print_job_spool_file(snum, jobid, filename, &pjob);
2708         if (!W_ERROR_IS_OK(werr)) {
2709                 goto fail;
2710         }
2711
2712         pjob_store(sharename, jobid, &pjob);
2713
2714         /* Update the 'jobs changed' entry used by print_queue_status. */
2715         add_to_jobs_changed(pdb, jobid);
2716
2717         /* Ensure we keep a rough count of the number of total jobs... */
2718         tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
2719
2720         release_print_db(pdb);
2721
2722         *_jobid = jobid;
2723         return WERR_OK;
2724
2725 fail:
2726         if (jobid != -1) {
2727                 pjob_delete(sharename, jobid);
2728         }
2729
2730         release_print_db(pdb);
2731
2732         DEBUG(3, ("print_job_start: returning fail. "
2733                   "Error = %s\n", win_errstr(werr)));
2734         return werr;
2735 }
2736
2737 /****************************************************************************
2738  Update the number of pages spooled to jobid
2739 ****************************************************************************/
2740
2741 void print_job_endpage(int snum, uint32 jobid)
2742 {
2743         const char* sharename = lp_const_servicename(snum);
2744         struct printjob *pjob;
2745
2746         pjob = print_job_find(sharename, jobid);
2747         if (!pjob)
2748                 return;
2749         /* don't allow another process to get this info - it is meaningless */
2750         if (pjob->pid != sys_getpid())
2751                 return;
2752
2753         pjob->page_count++;
2754         pjob_store(sharename, jobid, pjob);
2755 }
2756
2757 /****************************************************************************
2758  Print a file - called on closing the file. This spools the job.
2759  If normal close is false then we're tearing down the jobs - treat as an
2760  error.
2761 ****************************************************************************/
2762
2763 NTSTATUS print_job_end(struct messaging_context *msg_ctx, int snum,
2764                        uint32 jobid, enum file_close_type close_type)
2765 {
2766         const char* sharename = lp_const_servicename(snum);
2767         struct printjob *pjob;
2768         int ret;
2769         SMB_STRUCT_STAT sbuf;
2770         struct printif *current_printif = get_printer_fns( snum );
2771         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
2772
2773         pjob = print_job_find(sharename, jobid);
2774
2775         if (!pjob) {
2776                 return NT_STATUS_PRINT_CANCELLED;
2777         }
2778
2779         if (pjob->spooled || pjob->pid != sys_getpid()) {
2780                 return NT_STATUS_ACCESS_DENIED;
2781         }
2782
2783         if (close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) {
2784                 if (pjob->status == PJOB_SMBD_SPOOLING) {
2785                         /* take over the file now, smbd is done */
2786                         if (sys_stat(pjob->filename, &sbuf, false) != 0) {
2787                                 status = map_nt_error_from_unix(errno);
2788                                 DEBUG(3, ("print_job_end: "
2789                                           "stat file failed for jobid %d\n",
2790                                           jobid));
2791                                 goto fail;
2792                         }
2793
2794                         pjob->status = LPQ_SPOOLING;
2795
2796                 } else {
2797
2798                         if ((sys_fstat(pjob->fd, &sbuf, false) != 0)) {
2799                                 status = map_nt_error_from_unix(errno);
2800                                 close(pjob->fd);
2801                                 DEBUG(3, ("print_job_end: "
2802                                           "stat file failed for jobid %d\n",
2803                                           jobid));
2804                                 goto fail;
2805                         }
2806
2807                         close(pjob->fd);
2808                 }
2809
2810                 pjob->size = sbuf.st_ex_size;
2811         } else {
2812
2813                 /*
2814                  * Not a normal close, something has gone wrong. Cleanup.
2815                  */
2816                 if (pjob->fd != -1) {
2817                         close(pjob->fd);
2818                 }
2819                 goto fail;
2820         }
2821
2822         /* Technically, this is not quite right. If the printer has a separator
2823          * page turned on, the NT spooler prints the separator page even if the
2824          * print job is 0 bytes. 010215 JRR */
2825         if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
2826                 /* don't bother spooling empty files or something being deleted. */
2827                 DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
2828                         pjob->filename, pjob->size ? "deleted" : "zero length" ));
2829                 unlink(pjob->filename);
2830                 pjob_delete(sharename, jobid);
2831                 return NT_STATUS_OK;
2832         }
2833
2834         ret = (*(current_printif->job_submit))(snum, pjob);
2835
2836         if (ret) {
2837                 status = NT_STATUS_PRINT_CANCELLED;
2838                 goto fail;
2839         }
2840
2841         /* The print job has been successfully handed over to the back-end */
2842
2843         pjob->spooled = True;
2844         pjob->status = LPQ_QUEUED;
2845         pjob_store(sharename, jobid, pjob);
2846
2847         /* make sure the database is up to date */
2848         if (print_cache_expired(lp_const_servicename(snum), True))
2849                 print_queue_update(msg_ctx, snum, False);
2850
2851         return NT_STATUS_OK;
2852
2853 fail:
2854
2855         /* The print job was not successfully started. Cleanup */
2856         /* Still need to add proper error return propagation! 010122:JRR */
2857         pjob->fd = -1;
2858         unlink(pjob->filename);
2859         pjob_delete(sharename, jobid);
2860         return status;
2861 }
2862
2863 /****************************************************************************
2864  Get a snapshot of jobs in the system without traversing.
2865 ****************************************************************************/
2866
2867 static bool get_stored_queue_info(struct messaging_context *msg_ctx,
2868                                   struct tdb_print_db *pdb, int snum,
2869                                   int *pcount, print_queue_struct **ppqueue)
2870 {
2871         TDB_DATA data, cgdata;
2872         print_queue_struct *queue = NULL;
2873         uint32 qcount = 0;
2874         uint32 extra_count = 0;
2875         int total_count = 0;
2876         size_t len = 0;
2877         uint32 i;
2878         int max_reported_jobs = lp_max_reported_jobs(snum);
2879         bool ret = False;
2880         const char* sharename = lp_servicename(snum);
2881
2882         /* make sure the database is up to date */
2883         if (print_cache_expired(lp_const_servicename(snum), True))
2884                 print_queue_update(msg_ctx, snum, False);
2885
2886         *pcount = 0;
2887         *ppqueue = NULL;
2888
2889         ZERO_STRUCT(data);
2890         ZERO_STRUCT(cgdata);
2891
2892         /* Get the stored queue data. */
2893         data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
2894
2895         if (data.dptr && data.dsize >= sizeof(qcount))
2896                 len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
2897
2898         /* Get the changed jobs list. */
2899         cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
2900         if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
2901                 extra_count = cgdata.dsize/4;
2902
2903         DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
2904
2905         /* Allocate the queue size. */
2906         if (qcount == 0 && extra_count == 0)
2907                 goto out;
2908
2909         if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
2910                 goto out;
2911
2912         /* Retrieve the linearised queue data. */
2913
2914         for( i  = 0; i < qcount; i++) {
2915                 uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
2916                 len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
2917                                 &qjob,
2918                                 &qsize,
2919                                 &qpage_count,
2920                                 &qstatus,
2921                                 &qpriority,
2922                                 &qtime,
2923                                 queue[i].fs_user,
2924                                 queue[i].fs_file);
2925                 queue[i].job = qjob;
2926                 queue[i].size = qsize;
2927                 queue[i].page_count = qpage_count;
2928                 queue[i].status = qstatus;
2929                 queue[i].priority = qpriority;
2930                 queue[i].time = qtime;
2931         }
2932
2933         total_count = qcount;
2934
2935         /* Add in the changed jobids. */
2936         for( i  = 0; i < extra_count; i++) {
2937                 uint32 jobid;
2938                 struct printjob *pjob;
2939
2940                 jobid = IVAL(cgdata.dptr, i*4);
2941                 DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
2942                 pjob = print_job_find(lp_const_servicename(snum), jobid);
2943                 if (!pjob) {
2944                         DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
2945                         remove_from_jobs_changed(sharename, jobid);
2946                         continue;
2947                 }
2948
2949                 queue[total_count].job = jobid;
2950                 queue[total_count].size = pjob->size;
2951                 queue[total_count].page_count = pjob->page_count;
2952                 queue[total_count].status = pjob->status;
2953                 queue[total_count].priority = 1;
2954                 queue[total_count].time = pjob->starttime;
2955                 fstrcpy(queue[total_count].fs_user, pjob->user);
2956                 fstrcpy(queue[total_count].fs_file, pjob->jobname);
2957                 total_count++;
2958         }
2959
2960         /* Sort the queue by submission time otherwise they are displayed
2961            in hash order. */
2962
2963         TYPESAFE_QSORT(queue, total_count, printjob_comp);
2964
2965         DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
2966
2967         if (max_reported_jobs && total_count > max_reported_jobs)
2968                 total_count = max_reported_jobs;
2969
2970         *ppqueue = queue;
2971         *pcount = total_count;
2972
2973         ret = True;
2974
2975   out:
2976
2977         SAFE_FREE(data.dptr);
2978         SAFE_FREE(cgdata.dptr);
2979         return ret;
2980 }
2981
2982 /****************************************************************************
2983  Get a printer queue listing.
2984  set queue = NULL and status = NULL if you just want to update the cache
2985 ****************************************************************************/
2986
2987 int print_queue_status(struct messaging_context *msg_ctx, int snum,
2988                        print_queue_struct **ppqueue,
2989                        print_status_struct *status)
2990 {
2991         fstring keystr;
2992         TDB_DATA data, key;
2993         const char *sharename;
2994         struct tdb_print_db *pdb;
2995         int count = 0;
2996
2997         /* make sure the database is up to date */
2998
2999         if (print_cache_expired(lp_const_servicename(snum), True))
3000                 print_queue_update(msg_ctx, snum, False);
3001
3002         /* return if we are done */
3003         if ( !ppqueue || !status )
3004                 return 0;
3005
3006         *ppqueue = NULL;
3007         sharename = lp_const_servicename(snum);
3008         pdb = get_print_db_byname(sharename);
3009
3010         if (!pdb)
3011                 return 0;
3012
3013         /*
3014          * Fetch the queue status.  We must do this first, as there may
3015          * be no jobs in the queue.
3016          */
3017
3018         ZERO_STRUCTP(status);
3019         slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
3020         key = string_tdb_data(keystr);
3021
3022         data = tdb_fetch(pdb->tdb, key);
3023         if (data.dptr) {
3024                 if (data.dsize == sizeof(*status)) {
3025                         /* this memcpy is ok since the status struct was
3026                            not packed before storing it in the tdb */
3027                         memcpy(status, data.dptr, sizeof(*status));
3028                 }
3029                 SAFE_FREE(data.dptr);
3030         }
3031
3032         /*
3033          * Now, fetch the print queue information.  We first count the number
3034          * of entries, and then only retrieve the queue if necessary.
3035          */
3036
3037         if (!get_stored_queue_info(msg_ctx, pdb, snum, &count, ppqueue)) {
3038                 release_print_db(pdb);
3039                 return 0;
3040         }
3041
3042         release_print_db(pdb);
3043         return count;
3044 }
3045
3046 /****************************************************************************
3047  Pause a queue.
3048 ****************************************************************************/
3049
3050 WERROR print_queue_pause(struct auth_serversupplied_info *server_info,
3051                          struct messaging_context *msg_ctx, int snum)
3052 {
3053         int ret;
3054         struct printif *current_printif = get_printer_fns( snum );
3055
3056         if (!print_access_check(server_info, msg_ctx, snum,
3057                                 PRINTER_ACCESS_ADMINISTER)) {
3058                 return WERR_ACCESS_DENIED;
3059         }
3060
3061
3062         become_root();
3063
3064         ret = (*(current_printif->queue_pause))(snum);
3065
3066         unbecome_root();
3067
3068         if (ret != 0) {
3069                 return WERR_INVALID_PARAM;
3070         }
3071
3072         /* force update the database */
3073         print_cache_flush(lp_const_servicename(snum));
3074
3075         /* Send a printer notify message */
3076
3077         notify_printer_status(server_event_context(), msg_ctx, snum,
3078                               PRINTER_STATUS_PAUSED);
3079
3080         return WERR_OK;
3081 }
3082
3083 /****************************************************************************
3084  Resume a queue.
3085 ****************************************************************************/
3086
3087 WERROR print_queue_resume(struct auth_serversupplied_info *server_info,
3088                           struct messaging_context *msg_ctx, int snum)
3089 {
3090         int ret;
3091         struct printif *current_printif = get_printer_fns( snum );
3092
3093         if (!print_access_check(server_info, msg_ctx, snum,
3094                                 PRINTER_ACCESS_ADMINISTER)) {
3095                 return WERR_ACCESS_DENIED;
3096         }
3097
3098         become_root();
3099
3100         ret = (*(current_printif->queue_resume))(snum);
3101
3102         unbecome_root();
3103
3104         if (ret != 0) {
3105                 return WERR_INVALID_PARAM;
3106         }
3107
3108         /* make sure the database is up to date */
3109         if (print_cache_expired(lp_const_servicename(snum), True))
3110                 print_queue_update(msg_ctx, snum, True);
3111
3112         /* Send a printer notify message */
3113
3114         notify_printer_status(server_event_context(), msg_ctx, snum,
3115                               PRINTER_STATUS_OK);
3116
3117         return WERR_OK;
3118 }
3119
3120 /****************************************************************************
3121  Purge a queue - implemented by deleting all jobs that we can delete.
3122 ****************************************************************************/
3123
3124 WERROR print_queue_purge(struct auth_serversupplied_info *server_info,
3125                          struct messaging_context *msg_ctx, int snum)
3126 {
3127         print_queue_struct *queue;
3128         print_status_struct status;
3129         int njobs, i;
3130         bool can_job_admin;
3131
3132         /* Force and update so the count is accurate (i.e. not a cached count) */
3133         print_queue_update(msg_ctx, snum, True);
3134
3135         can_job_admin = print_access_check(server_info,
3136                                            msg_ctx,
3137                                            snum,
3138                                            JOB_ACCESS_ADMINISTER);
3139         njobs = print_queue_status(msg_ctx, snum, &queue, &status);
3140
3141         if ( can_job_admin )
3142                 become_root();
3143
3144         for (i=0;i<njobs;i++) {
3145                 bool owner = is_owner(server_info, lp_const_servicename(snum),
3146                                       queue[i].job);
3147
3148                 if (owner || can_job_admin) {
3149                         print_job_delete1(snum, queue[i].job);
3150                 }
3151         }
3152
3153         if ( can_job_admin )
3154                 unbecome_root();
3155
3156         /* update the cache */
3157         print_queue_update(msg_ctx, snum, True);
3158
3159         SAFE_FREE(queue);
3160
3161         return WERR_OK;
3162 }