s3-spoolss: dont overwrite location change notify.
[samba.git] / source3 / printing / notify.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    printing backend routines
5    Copyright (C) Tim Potter, 2002
6    Copyright (C) Gerald Carter,         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/spoolss.h"
26 #include "nt_printing.h"
27
28 static TALLOC_CTX *send_ctx;
29
30 static unsigned int num_messages;
31
32 static struct notify_queue {
33         struct notify_queue *next, *prev;
34         struct spoolss_notify_msg *msg;
35         struct timeval tv;
36         uint8 *buf;
37         size_t buflen;
38 } *notify_queue_head = NULL;
39
40 static struct tevent_timer *notify_event;
41
42 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
43                                   size_t *p_num_pids, pid_t **pp_pid_list);
44
45 static bool create_send_ctx(void)
46 {
47         if (!send_ctx)
48                 send_ctx = talloc_init("print notify queue");
49
50         if (!send_ctx)
51                 return False;
52
53         return True;
54 }
55
56 /****************************************************************************
57  Turn a queue name into a snum.
58 ****************************************************************************/
59
60 int print_queue_snum(const char *qname)
61 {
62         int snum = lp_servicenumber(qname);
63         if (snum == -1 || !lp_print_ok(snum))
64                 return -1;
65         return snum;
66 }
67
68 /*******************************************************************
69  Used to decide if we need a short select timeout.
70 *******************************************************************/
71
72 static bool print_notify_messages_pending(void)
73 {
74         return (notify_queue_head != NULL);
75 }
76
77 /*******************************************************************
78  Flatten data into a message.
79 *******************************************************************/
80
81 static bool flatten_message(struct notify_queue *q)
82 {
83         struct spoolss_notify_msg *msg = q->msg;
84         uint8 *buf = NULL;
85         size_t buflen = 0, len;
86
87 again:
88         len = 0;
89
90         /* Pack header */
91
92         len += tdb_pack(buf + len, buflen - len, "f", msg->printer);
93
94         len += tdb_pack(buf + len, buflen - len, "ddddddd",
95                         (uint32)q->tv.tv_sec, (uint32)q->tv.tv_usec,
96                         msg->type, msg->field, msg->id, msg->len, msg->flags);
97
98         /* Pack data */
99
100         if (msg->len == 0)
101                 len += tdb_pack(buf + len, buflen - len, "dd",
102                                 msg->notify.value[0], msg->notify.value[1]);
103         else
104                 len += tdb_pack(buf + len, buflen - len, "B",
105                                 msg->len, msg->notify.data);
106
107         if (buflen != len) {
108                 buf = (uint8 *)TALLOC_REALLOC(send_ctx, buf, len);
109                 if (!buf)
110                         return False;
111                 buflen = len;
112                 goto again;
113         }
114
115         q->buf = buf;
116         q->buflen = buflen;
117
118         return True;
119 }
120
121 /*******************************************************************
122  Send the batched messages - on a per-printer basis.
123 *******************************************************************/
124
125 static void print_notify_send_messages_to_printer(struct messaging_context *msg_ctx,
126                                                   const char *printer,
127                                                   unsigned int timeout)
128 {
129         char *buf;
130         struct notify_queue *pq, *pq_next;
131         size_t msg_count = 0, offset = 0;
132         size_t num_pids = 0;
133         size_t i;
134         pid_t *pid_list = NULL;
135         struct timeval end_time = timeval_zero();
136
137         /* Count the space needed to send the messages. */
138         for (pq = notify_queue_head; pq; pq = pq->next) {
139                 if (strequal(printer, pq->msg->printer)) {
140                         if (!flatten_message(pq)) {
141                                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
142                                 talloc_free_children(send_ctx);
143                                 num_messages = 0;
144                                 return;
145                         }
146                         offset += (pq->buflen + 4);
147                         msg_count++;
148                 }       
149         }
150         offset += 4; /* For count. */
151
152         buf = (char *)TALLOC(send_ctx, offset);
153         if (!buf) {
154                 DEBUG(0,("print_notify_send_messages: Out of memory\n"));
155                 talloc_free_children(send_ctx);
156                 num_messages = 0;
157                 return;
158         }
159
160         offset = 0;
161         SIVAL(buf,offset,msg_count);
162         offset += 4;
163         for (pq = notify_queue_head; pq; pq = pq_next) {
164                 pq_next = pq->next;
165
166                 if (strequal(printer, pq->msg->printer)) {
167                         SIVAL(buf,offset,pq->buflen);
168                         offset += 4;
169                         memcpy(buf + offset, pq->buf, pq->buflen);
170                         offset += pq->buflen;
171
172                         /* Remove from list. */
173                         DLIST_REMOVE(notify_queue_head, pq);
174                 }
175         }
176
177         DEBUG(5, ("print_notify_send_messages_to_printer: sending %lu print notify message%s to printer %s\n", 
178                   (unsigned long)msg_count, msg_count != 1 ? "s" : "", printer));
179
180         /*
181          * Get the list of PID's to send to.
182          */
183
184         if (!print_notify_pid_list(printer, send_ctx, &num_pids, &pid_list))
185                 return;
186
187         if (timeout != 0) {
188                 end_time = timeval_current_ofs(timeout, 0);
189         }
190
191         for (i = 0; i < num_pids; i++) {
192                 messaging_send_buf(msg_ctx,
193                                    pid_to_procid(pid_list[i]),
194                                    MSG_PRINTER_NOTIFY2 | MSG_FLAG_LOWPRIORITY,
195                                    (uint8 *)buf, offset);
196
197                 if ((timeout != 0) && timeval_expired(&end_time)) {
198                         break;
199                 }
200         }
201 }
202
203 /*******************************************************************
204  Actually send the batched messages.
205 *******************************************************************/
206
207 void print_notify_send_messages(struct messaging_context *msg_ctx,
208                                 unsigned int timeout)
209 {
210         if (!print_notify_messages_pending())
211                 return;
212
213         if (!create_send_ctx())
214                 return;
215
216         while (print_notify_messages_pending())
217                 print_notify_send_messages_to_printer(
218                         msg_ctx, notify_queue_head->msg->printer, timeout);
219
220         talloc_free_children(send_ctx);
221         num_messages = 0;
222 }
223
224 /*******************************************************************
225  Event handler to send the messages.
226 *******************************************************************/
227
228 static void print_notify_event_send_messages(struct tevent_context *event_ctx,
229                                              struct tevent_timer *te,
230                                              struct timeval now,
231                                              void *private_data)
232 {
233         struct messaging_context *msg_ctx = talloc_get_type_abort(
234                 private_data, struct messaging_context);
235         /* Remove this timed event handler. */
236         TALLOC_FREE(notify_event);
237
238         change_to_root_user();
239         print_notify_send_messages(msg_ctx, 0);
240 }
241
242 /**********************************************************************
243  deep copy a SPOOLSS_NOTIFY_MSG structure using a TALLOC_CTX
244  *********************************************************************/
245  
246 static bool copy_notify2_msg( SPOOLSS_NOTIFY_MSG *to, SPOOLSS_NOTIFY_MSG *from )
247 {
248
249         if ( !to || !from )
250                 return False;
251         
252         memcpy( to, from, sizeof(SPOOLSS_NOTIFY_MSG) );
253         
254         if ( from->len ) {
255                 to->notify.data = (char *)TALLOC_MEMDUP(send_ctx, from->notify.data, from->len );
256                 if ( !to->notify.data ) {
257                         DEBUG(0,("copy_notify2_msg: TALLOC_MEMDUP() of size [%d] failed!\n", from->len ));
258                         return False;
259                 }
260         }
261         
262
263         return True;
264 }
265
266 /*******************************************************************
267  Batch up print notify messages.
268 *******************************************************************/
269
270 static void send_spoolss_notify2_msg(struct tevent_context *ev,
271                                      struct messaging_context *msg_ctx,
272                                      SPOOLSS_NOTIFY_MSG *msg)
273 {
274         struct notify_queue *pnqueue, *tmp_ptr;
275
276         /*
277          * Ensure we only have one job total_bytes and job total_pages for
278          * each job. There is no point in sending multiple messages that match
279          * as they will just cause flickering updates in the client.
280          */
281
282         if ((num_messages < 100) && (msg->type == JOB_NOTIFY_TYPE) 
283                 && (msg->field == JOB_NOTIFY_FIELD_TOTAL_BYTES
284                     || msg->field == JOB_NOTIFY_FIELD_TOTAL_PAGES ))
285         {
286
287                 for (tmp_ptr = notify_queue_head; tmp_ptr; tmp_ptr = tmp_ptr->next) 
288                 {
289                         if (tmp_ptr->msg->type == msg->type &&
290                                         tmp_ptr->msg->field == msg->field &&
291                                         tmp_ptr->msg->id == msg->id &&
292                                         tmp_ptr->msg->flags == msg->flags &&
293                                         strequal(tmp_ptr->msg->printer, msg->printer)) {
294
295                                 DEBUG(5,("send_spoolss_notify2_msg: replacing message 0x%02x/0x%02x for "
296                                          "printer %s in notify_queue\n", msg->type, msg->field, msg->printer));
297
298                                 tmp_ptr->msg = msg;
299                                 return;
300                         }
301                 }
302         }
303
304         /* Store the message on the pending queue. */
305
306         pnqueue = TALLOC_P(send_ctx, struct notify_queue);
307         if (!pnqueue) {
308                 DEBUG(0,("send_spoolss_notify2_msg: Out of memory.\n"));
309                 return;
310         }
311
312         /* allocate a new msg structure and copy the fields */
313         
314         if ( !(pnqueue->msg = TALLOC_P(send_ctx, SPOOLSS_NOTIFY_MSG)) ) {
315                 DEBUG(0,("send_spoolss_notify2_msg: talloc() of size [%lu] failed!\n", 
316                         (unsigned long)sizeof(SPOOLSS_NOTIFY_MSG)));
317                 return;
318         }
319         copy_notify2_msg(pnqueue->msg, msg);
320         GetTimeOfDay(&pnqueue->tv);
321         pnqueue->buf = NULL;
322         pnqueue->buflen = 0;
323
324         DEBUG(5, ("send_spoolss_notify2_msg: appending message 0x%02x/0x%02x for printer %s \
325 to notify_queue_head\n", msg->type, msg->field, msg->printer));
326
327         /*
328          * Note we add to the end of the list to ensure
329          * the messages are sent in the order they were received. JRA.
330          */
331
332         DLIST_ADD_END(notify_queue_head, pnqueue, struct notify_queue *);
333         num_messages++;
334
335         if ((notify_event == NULL) && (ev != NULL)) {
336                 /* Add an event for 1 second's time to send this queue. */
337                 notify_event = tevent_add_timer(
338                         ev, NULL, timeval_current_ofs(1,0),
339                         print_notify_event_send_messages, msg_ctx);
340         }
341
342 }
343
344 static void send_notify_field_values(struct tevent_context *ev,
345                                      struct messaging_context *msg_ctx,
346                                      const char *sharename, uint32 type,
347                                      uint32 field, uint32 id, uint32 value1, 
348                                      uint32 value2, uint32 flags)
349 {
350         struct spoolss_notify_msg *msg;
351
352         if (lp_disable_spoolss())
353                 return;
354
355         if (!create_send_ctx())
356                 return;
357
358         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
359         if (!msg)
360                 return;
361
362         ZERO_STRUCTP(msg);
363
364         fstrcpy(msg->printer, sharename);
365         msg->type = type;
366         msg->field = field;
367         msg->id = id;
368         msg->notify.value[0] = value1;
369         msg->notify.value[1] = value2;
370         msg->flags = flags;
371
372         send_spoolss_notify2_msg(ev, msg_ctx, msg);
373 }
374
375 static void send_notify_field_buffer(struct tevent_context *ev,
376                                      struct messaging_context *msg_ctx,
377                                      const char *sharename, uint32 type,
378                                      uint32 field, uint32 id, uint32 len,
379                                      const char *buffer)
380 {
381         struct spoolss_notify_msg *msg;
382
383         if (lp_disable_spoolss())
384                 return;
385
386         if (!create_send_ctx())
387                 return;
388
389         msg = TALLOC_P(send_ctx, struct spoolss_notify_msg);
390         if (!msg)
391                 return;
392
393         ZERO_STRUCTP(msg);
394
395         fstrcpy(msg->printer, sharename);
396         msg->type = type;
397         msg->field = field;
398         msg->id = id;
399         msg->len = len;
400         msg->notify.data = CONST_DISCARD(char *,buffer);
401
402         send_spoolss_notify2_msg(ev, msg_ctx, msg);
403 }
404
405 /* Send a message that the printer status has changed */
406
407 void notify_printer_status_byname(struct tevent_context *ev,
408                                   struct messaging_context *msg_ctx,
409                                   const char *sharename, uint32 status)
410 {
411         /* Printer status stored in value1 */
412
413         int snum = print_queue_snum(sharename);
414
415         send_notify_field_values(ev, msg_ctx, sharename, PRINTER_NOTIFY_TYPE,
416                                  PRINTER_NOTIFY_FIELD_STATUS, snum,
417                                  status, 0, 0);
418 }
419
420 void notify_printer_status(struct tevent_context *ev,
421                            struct messaging_context *msg_ctx,
422                            int snum, uint32 status)
423 {
424         const char *sharename = lp_servicename(snum);
425
426         if (sharename)
427                 notify_printer_status_byname(ev, msg_ctx, sharename, status);
428 }
429
430 void notify_job_status_byname(struct tevent_context *ev,
431                               struct messaging_context *msg_ctx,
432                               const char *sharename, uint32 jobid,
433                               uint32 status,
434                               uint32 flags)
435 {
436         /* Job id stored in id field, status in value1 */
437
438         send_notify_field_values(ev, msg_ctx,
439                                  sharename, JOB_NOTIFY_TYPE,
440                                  JOB_NOTIFY_FIELD_STATUS, jobid,
441                                  status, 0, flags);
442 }
443
444 void notify_job_status(struct tevent_context *ev,
445                        struct messaging_context *msg_ctx,
446                        const char *sharename, uint32 jobid, uint32 status)
447 {
448         notify_job_status_byname(ev, msg_ctx, sharename, jobid, status, 0);
449 }
450
451 void notify_job_total_bytes(struct tevent_context *ev,
452                             struct messaging_context *msg_ctx,
453                             const char *sharename, uint32 jobid,
454                             uint32 size)
455 {
456         /* Job id stored in id field, status in value1 */
457
458         send_notify_field_values(ev, msg_ctx,
459                                  sharename, JOB_NOTIFY_TYPE,
460                                  JOB_NOTIFY_FIELD_TOTAL_BYTES, jobid,
461                                  size, 0, 0);
462 }
463
464 void notify_job_total_pages(struct tevent_context *ev,
465                             struct messaging_context *msg_ctx,
466                             const char *sharename, uint32 jobid,
467                             uint32 pages)
468 {
469         /* Job id stored in id field, status in value1 */
470
471         send_notify_field_values(ev, msg_ctx,
472                                  sharename, JOB_NOTIFY_TYPE,
473                                  JOB_NOTIFY_FIELD_TOTAL_PAGES, jobid,
474                                  pages, 0, 0);
475 }
476
477 void notify_job_username(struct tevent_context *ev,
478                          struct messaging_context *msg_ctx,
479                          const char *sharename, uint32 jobid, char *name)
480 {
481         send_notify_field_buffer(
482                 ev, msg_ctx,
483                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_USER_NAME,
484                 jobid, strlen(name) + 1, name);
485 }
486
487 void notify_job_name(struct tevent_context *ev,
488                      struct messaging_context *msg_ctx,
489                      const char *sharename, uint32 jobid, char *name)
490 {
491         send_notify_field_buffer(
492                 ev, msg_ctx,
493                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_DOCUMENT,
494                 jobid, strlen(name) + 1, name);
495 }
496
497 void notify_job_submitted(struct tevent_context *ev,
498                           struct messaging_context *msg_ctx,
499                           const char *sharename, uint32 jobid,
500                           time_t submitted)
501 {
502         send_notify_field_buffer(
503                 ev, msg_ctx,
504                 sharename, JOB_NOTIFY_TYPE, JOB_NOTIFY_FIELD_SUBMITTED,
505                 jobid, sizeof(submitted), (char *)&submitted);
506 }
507
508 void notify_printer_driver(struct tevent_context *ev,
509                            struct messaging_context *msg_ctx,
510                            int snum, const char *driver_name)
511 {
512         const char *sharename = lp_servicename(snum);
513
514         send_notify_field_buffer(
515                 ev, msg_ctx,
516                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_DRIVER_NAME,
517                 snum, strlen(driver_name) + 1, driver_name);
518 }
519
520 void notify_printer_comment(struct tevent_context *ev,
521                             struct messaging_context *msg_ctx,
522                             int snum, const char *comment)
523 {
524         const char *sharename = lp_servicename(snum);
525
526         send_notify_field_buffer(
527                 ev, msg_ctx,
528                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_COMMENT,
529                 snum, strlen(comment) + 1, comment);
530 }
531
532 void notify_printer_sharename(struct tevent_context *ev,
533                               struct messaging_context *msg_ctx,
534                               int snum, const char *share_name)
535 {
536         const char *sharename = lp_servicename(snum);
537
538         send_notify_field_buffer(
539                 ev, msg_ctx,
540                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SHARE_NAME,
541                 snum, strlen(share_name) + 1, share_name);
542 }
543
544 void notify_printer_printername(struct tevent_context *ev,
545                                 struct messaging_context *msg_ctx,
546                                 int snum, const char *printername)
547 {
548         const char *sharename = lp_servicename(snum);
549
550         send_notify_field_buffer(
551                 ev, msg_ctx,
552                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PRINTER_NAME,
553                 snum, strlen(printername) + 1, printername);
554 }
555
556 void notify_printer_port(struct tevent_context *ev,
557                          struct messaging_context *msg_ctx,
558                          int snum, const char *port_name)
559 {
560         const char *sharename = lp_servicename(snum);
561
562         send_notify_field_buffer(
563                 ev, msg_ctx,
564                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_PORT_NAME,
565                 snum, strlen(port_name) + 1, port_name);
566 }
567
568 void notify_printer_location(struct tevent_context *ev,
569                              struct messaging_context *msg_ctx,
570                              int snum, const char *location)
571 {
572         const char *sharename = lp_servicename(snum);
573
574         send_notify_field_buffer(
575                 ev, msg_ctx,
576                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_LOCATION,
577                 snum, strlen(location) + 1, location);
578 }
579
580 void notify_printer_sepfile(struct tevent_context *ev,
581                             struct messaging_context *msg_ctx,
582                             int snum, const char *sepfile)
583 {
584         const char *sharename = lp_servicename(snum);
585
586         send_notify_field_buffer(
587                 ev, msg_ctx,
588                 sharename, PRINTER_NOTIFY_TYPE, PRINTER_NOTIFY_FIELD_SEPFILE,
589                 snum, strlen(sepfile) + 1, sepfile);
590 }
591
592
593 void notify_printer_byname(struct tevent_context *ev,
594                            struct messaging_context *msg_ctx,
595                            const char *printername, uint32 change,
596                            const char *value)
597 {
598         int snum = print_queue_snum(printername);
599         int type = PRINTER_NOTIFY_TYPE;
600         
601         if ( snum == -1 )
602                 return;
603                 
604         send_notify_field_buffer(
605                 ev, msg_ctx,
606                 printername, type, change, snum, strlen(value)+1, value );
607
608
609
610 /****************************************************************************
611  Return a malloced list of pid_t's that are interested in getting update
612  messages on this print queue. Used in printing/notify to send the messages.
613 ****************************************************************************/
614
615 static bool print_notify_pid_list(const char *printername, TALLOC_CTX *mem_ctx,
616                                   size_t *p_num_pids, pid_t **pp_pid_list)
617 {
618         struct tdb_print_db *pdb = NULL;
619         TDB_CONTEXT *tdb = NULL;
620         TDB_DATA data;
621         bool ret = True;
622         size_t i, num_pids, offset;
623         pid_t *pid_list;
624
625         *p_num_pids = 0;
626         *pp_pid_list = NULL;
627
628         pdb = get_print_db_byname(printername);
629         if (!pdb)
630                 return False;
631         tdb = pdb->tdb;
632
633         if (tdb_read_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
634                 DEBUG(0,("print_notify_pid_list: Failed to lock printer %s database\n",
635                                         printername));
636                 if (pdb)
637                         release_print_db(pdb);
638                 return False;
639         }
640
641         data = get_printer_notify_pid_list( tdb, printername, True );
642
643         if (!data.dptr) {
644                 ret = True;
645                 goto done;
646         }
647
648         num_pids = data.dsize / 8;
649
650         if (num_pids) {
651                 if ((pid_list = TALLOC_ARRAY(mem_ctx, pid_t, num_pids)) == NULL) {
652                         ret = False;
653                         goto done;
654                 }
655         } else {
656                 pid_list = NULL;
657         }
658
659         for( i = 0, offset = 0; i < num_pids; offset += 8, i++)
660                 pid_list[i] = (pid_t)IVAL(data.dptr, offset);
661
662         *pp_pid_list = pid_list;
663         *p_num_pids = num_pids;
664
665         ret = True;
666
667   done:
668
669         tdb_read_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
670         if (pdb)
671                 release_print_db(pdb);
672         SAFE_FREE(data.dptr);
673         return ret;
674 }