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