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