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