]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
Load cups into easysw/current.
[thirdparty/cups.git] / scheduler / dirsvc.c
1 /*
2 * "$Id: dirsvc.c 5023 2006-01-29 14:39:44Z mike $"
3 *
4 * Directory services routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * cupsdLoadRemoteCache() - Load the remote printer cache.
27 * cupsdProcessBrowseData() - Process new browse data.
28 * cupsdProcessImplicitClasses() - Create/update implicit classes as needed.
29 * cupsdSaveRemoteCache() - Save the remote printer cache.
30 * cupsdSendBrowseDelete() - Send a "browse delete" message for a
31 * printer.
32 * cupsdSendBrowseList() - Send new browsing information as necessary.
33 * cupsdSendCUPSBrowse() - Send new browsing information using the
34 * CUPS protocol.
35 * cupsdSendSLPBrowse() - Register the specified printer with SLP.
36 * cupsdStartBrowsing() - Start sending and receiving broadcast
37 * information.
38 * cupsdStartPolling() - Start polling servers as needed.
39 * cupsdStopBrowsing() - Stop sending and receiving broadcast
40 * information.
41 * cupsdStopPolling() - Stop polling servers as needed.
42 * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
43 * protocol.
44 * cupsdUpdatePolling() - Read status messages from the poll daemons.
45 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
46 * slp_attr_callback() - SLP attribute callback
47 * slp_dereg_printer() - SLPDereg() the specified printer
48 * slp_get_attr() - Get an attribute from an SLP registration.
49 * slp_reg_callback() - Empty SLPRegReport.
50 * slp_url_callback() - SLP service url callback
51 */
52
53 /*
54 * Include necessary headers...
55 */
56
57 #include "cupsd.h"
58 #include <grp.h>
59
60
61 /*
62 * SLP definitions...
63 */
64
65 #ifdef HAVE_LIBSLP
66 /*
67 * SLP service name for CUPS...
68 */
69
70 # define SLP_CUPS_SRVTYPE "service:printer"
71 # define SLP_CUPS_SRVLEN 15
72
73
74 /*
75 * Printer service URL structure
76 */
77
78 typedef struct _slpsrvurl_s /**** SLP URL list ****/
79 {
80 struct _slpsrvurl_s *next; /* Next URL in list */
81 char url[HTTP_MAX_URI];
82 /* URL */
83 } slpsrvurl_t;
84
85
86 /*
87 * Local functions...
88 */
89
90 static SLPBoolean slp_attr_callback(SLPHandle hslp, const char *attrlist,
91 SLPError errcode, void *cookie);
92 static void slp_dereg_printer(cupsd_printer_t *p);
93 static int slp_get_attr(const char *attrlist, const char *tag,
94 char **valbuf);
95 static void slp_reg_callback(SLPHandle hslp, SLPError errcode,
96 void *cookie);
97 static SLPBoolean slp_url_callback(SLPHandle hslp, const char *srvurl,
98 unsigned short lifetime,
99 SLPError errcode, void *cookie);
100 #endif /* HAVE_LIBSLP */
101
102
103 /*
104 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
105 */
106
107 void
108 cupsdLoadRemoteCache(void)
109 {
110 cups_file_t *fp; /* remote.cache file */
111 int linenum; /* Current line number */
112 char line[1024], /* Line from file */
113 *value, /* Pointer to value */
114 *valueptr; /* Pointer into value */
115 cupsd_printer_t *p; /* Current printer */
116 time_t now; /* Current time */
117
118
119 /*
120 * Open the remote.cache file...
121 */
122
123 snprintf(line, sizeof(line), "%s/remote.cache", CacheDir);
124 if ((fp = cupsFileOpen(line, "r")) == NULL)
125 return;
126
127 /*
128 * Read printer configurations until we hit EOF...
129 */
130
131 linenum = 0;
132 p = NULL;
133 now = time(NULL);
134
135 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
136 {
137 /*
138 * Decode the directive...
139 */
140
141 if (!strcasecmp(line, "<Printer") ||
142 !strcasecmp(line, "<DefaultPrinter"))
143 {
144 /*
145 * <Printer name> or <DefaultPrinter name>
146 */
147
148 if (p == NULL && value)
149 {
150 /*
151 * Add the printer and a base file type...
152 */
153
154 cupsdLogMessage(CUPSD_LOG_DEBUG,
155 "cupsdLoadRemoteCache: Loading printer %s...", value);
156
157 p = cupsdAddPrinter(value);
158 p->accepting = 1;
159 p->state = IPP_PRINTER_IDLE;
160 p->type |= CUPS_PRINTER_REMOTE;
161 p->browse_time = now + BrowseTimeout;
162
163 /*
164 * Set the default printer as needed...
165 */
166
167 if (!strcasecmp(line, "<DefaultPrinter"))
168 DefaultPrinter = p;
169 }
170 else
171 {
172 cupsdLogMessage(CUPSD_LOG_ERROR,
173 "Syntax error on line %d of remote.cache.", linenum);
174 return;
175 }
176 }
177 else if (!strcasecmp(line, "<Class") ||
178 !strcasecmp(line, "<DefaultClass"))
179 {
180 /*
181 * <Class name> or <DefaultClass name>
182 */
183
184 if (p == NULL && value)
185 {
186 /*
187 * Add the printer and a base file type...
188 */
189
190 cupsdLogMessage(CUPSD_LOG_DEBUG,
191 "cupsdLoadRemoteCache: Loading class %s...", value);
192
193 p = cupsdAddClass(value);
194 p->accepting = 1;
195 p->state = IPP_PRINTER_IDLE;
196 p->type |= CUPS_PRINTER_REMOTE;
197 p->browse_time = now + BrowseTimeout;
198
199 /*
200 * Set the default printer as needed...
201 */
202
203 if (!strcasecmp(line, "<DefaultClass"))
204 DefaultPrinter = p;
205 }
206 else
207 {
208 cupsdLogMessage(CUPSD_LOG_ERROR,
209 "Syntax error on line %d of remote.cache.", linenum);
210 return;
211 }
212 }
213 else if (!strcasecmp(line, "</Printer>") ||
214 !strcasecmp(line, "</Class>"))
215 {
216 if (p != NULL)
217 {
218 /*
219 * Close out the current printer...
220 */
221
222 cupsdSetPrinterAttrs(p);
223
224 p = NULL;
225 }
226 else
227 {
228 cupsdLogMessage(CUPSD_LOG_ERROR,
229 "Syntax error on line %d of remote.cache.", linenum);
230 return;
231 }
232 }
233 else if (!p)
234 {
235 cupsdLogMessage(CUPSD_LOG_ERROR,
236 "Syntax error on line %d of remote.cache.", linenum);
237 return;
238 }
239 else if (!strcasecmp(line, "Info"))
240 {
241 if (value)
242 cupsdSetString(&p->info, value);
243 }
244 else if (!strcasecmp(line, "MakeModel"))
245 {
246 if (value)
247 cupsdSetString(&p->make_model, value);
248 }
249 else if (!strcasecmp(line, "Location"))
250 {
251 if (value)
252 cupsdSetString(&p->location, value);
253 }
254 else if (!strcasecmp(line, "DeviceURI"))
255 {
256 if (value)
257 cupsdSetString(&p->device_uri, value);
258 else
259 {
260 cupsdLogMessage(CUPSD_LOG_ERROR,
261 "Syntax error on line %d of remote.cache.", linenum);
262 return;
263 }
264 }
265 else if (!strcasecmp(line, "State"))
266 {
267 /*
268 * Set the initial queue state...
269 */
270
271 if (value && !strcasecmp(value, "idle"))
272 p->state = IPP_PRINTER_IDLE;
273 else if (value && !strcasecmp(value, "stopped"))
274 p->state = IPP_PRINTER_STOPPED;
275 else
276 {
277 cupsdLogMessage(CUPSD_LOG_ERROR,
278 "Syntax error on line %d of remote.cache.", linenum);
279 return;
280 }
281 }
282 else if (!strcasecmp(line, "StateMessage"))
283 {
284 /*
285 * Set the initial queue state message...
286 */
287
288 if (value)
289 strlcpy(p->state_message, value, sizeof(p->state_message));
290 }
291 else if (!strcasecmp(line, "Accepting"))
292 {
293 /*
294 * Set the initial accepting state...
295 */
296
297 if (value &&
298 (!strcasecmp(value, "yes") ||
299 !strcasecmp(value, "on") ||
300 !strcasecmp(value, "true")))
301 p->accepting = 1;
302 else if (value &&
303 (!strcasecmp(value, "no") ||
304 !strcasecmp(value, "off") ||
305 !strcasecmp(value, "false")))
306 p->accepting = 0;
307 else
308 {
309 cupsdLogMessage(CUPSD_LOG_ERROR,
310 "Syntax error on line %d of remote.cache.", linenum);
311 return;
312 }
313 }
314 else if (!strcasecmp(line, "Type"))
315 {
316 if (value)
317 p->type = atoi(value);
318 else
319 {
320 cupsdLogMessage(CUPSD_LOG_ERROR,
321 "Syntax error on line %d of remote.cache.", linenum);
322 return;
323 }
324 }
325 else if (!strcasecmp(line, "BrowseTime"))
326 {
327 if (value)
328 {
329 time_t t = atoi(value);
330
331 if (t > (now + BrowseInterval))
332 p->browse_time = t;
333 }
334 else
335 {
336 cupsdLogMessage(CUPSD_LOG_ERROR,
337 "Syntax error on line %d of remote.cache.", linenum);
338 return;
339 }
340 }
341 else if (!strcasecmp(line, "JobSheets"))
342 {
343 /*
344 * Set the initial job sheets...
345 */
346
347 if (value)
348 {
349 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
350
351 if (*valueptr)
352 *valueptr++ = '\0';
353
354 cupsdSetString(&p->job_sheets[0], value);
355
356 while (isspace(*valueptr & 255))
357 valueptr ++;
358
359 if (*valueptr)
360 {
361 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
362
363 if (*valueptr)
364 *valueptr++ = '\0';
365
366 cupsdSetString(&p->job_sheets[1], value);
367 }
368 }
369 else
370 {
371 cupsdLogMessage(CUPSD_LOG_ERROR,
372 "Syntax error on line %d of remote.cache.", linenum);
373 return;
374 }
375 }
376 else if (!strcasecmp(line, "AllowUser"))
377 {
378 if (value)
379 {
380 p->deny_users = 0;
381 cupsdAddPrinterUser(p, value);
382 }
383 else
384 {
385 cupsdLogMessage(CUPSD_LOG_ERROR,
386 "Syntax error on line %d of remote.cache.", linenum);
387 return;
388 }
389 }
390 else if (!strcasecmp(line, "DenyUser"))
391 {
392 if (value)
393 {
394 p->deny_users = 1;
395 cupsdAddPrinterUser(p, value);
396 }
397 else
398 {
399 cupsdLogMessage(CUPSD_LOG_ERROR,
400 "Syntax error on line %d of remote.cache.", linenum);
401 return;
402 }
403 }
404 else
405 {
406 /*
407 * Something else we don't understand...
408 */
409
410 cupsdLogMessage(CUPSD_LOG_ERROR,
411 "Unknown configuration directive %s on line %d of remote.cache.",
412 line, linenum);
413 }
414 }
415
416 cupsFileClose(fp);
417
418 /*
419 * Do auto-classing if needed...
420 */
421
422 cupsdProcessImplicitClasses();
423 }
424
425
426 /*
427 * 'cupsdProcessBrowseData()' - Process new browse data.
428 */
429
430 void
431 cupsdProcessBrowseData(
432 const char *uri, /* I - URI of printer/class */
433 cups_ptype_t type, /* I - Printer type */
434 ipp_pstate_t state, /* I - Printer state */
435 const char *location, /* I - Printer location */
436 const char *info, /* I - Printer information */
437 const char *make_model, /* I - Printer make and model */
438 int num_attrs, /* I - Number of attributes */
439 cups_option_t *attrs) /* I - Attributes */
440 {
441 int update; /* Update printer attributes? */
442 char finaluri[HTTP_MAX_URI], /* Final URI for printer */
443 method[HTTP_MAX_URI], /* Method portion of URI */
444 username[HTTP_MAX_URI], /* Username portion of URI */
445 host[HTTP_MAX_URI], /* Host portion of URI */
446 resource[HTTP_MAX_URI]; /* Resource portion of URI */
447 int port; /* Port portion of URI */
448 char name[IPP_MAX_NAME], /* Name of printer */
449 *hptr, /* Pointer into hostname */
450 *sptr; /* Pointer into ServerName */
451 char local_make_model[IPP_MAX_NAME];
452 /* Local make and model */
453 cupsd_printer_t *p; /* Printer information */
454 const char *ipp_options; /* ipp-options value */
455
456
457 /*
458 * Pull the URI apart to see if this is a local or remote printer...
459 */
460
461 httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), username,
462 sizeof(username), host, sizeof(host), &port, resource,
463 sizeof(resource));
464
465 /*
466 * Determine if the URI contains any illegal characters in it...
467 */
468
469 if (strncmp(uri, "ipp://", 6) || !host[0] ||
470 (strncmp(resource, "/printers/", 10) &&
471 strncmp(resource, "/classes/", 9)))
472 {
473 cupsdLogMessage(CUPSD_LOG_ERROR,
474 "cupsdProcessBrowseData: Bad printer URI in browse data: %s",
475 uri);
476 return;
477 }
478
479 if (strchr(resource, '?') ||
480 (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) ||
481 (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/')))
482 {
483 cupsdLogMessage(CUPSD_LOG_ERROR,
484 "cupsdProcessBrowseData: Bad resource in browse data: %s",
485 resource);
486 return;
487 }
488
489 /*
490 * OK, this isn't a local printer; add any remote options...
491 */
492
493 ipp_options = cupsGetOption("ipp-options", num_attrs, attrs);
494
495 if (BrowseRemoteOptions)
496 {
497 if (BrowseRemoteOptions[0] == '?')
498 {
499 /*
500 * Override server-supplied options...
501 */
502
503 snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions);
504 }
505 else if (ipp_options)
506 {
507 /*
508 * Combine the server and local options...
509 */
510
511 snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options,
512 BrowseRemoteOptions);
513 }
514 else
515 {
516 /*
517 * Just use the local options...
518 */
519
520 snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions);
521 }
522
523 uri = finaluri;
524 }
525 else if (ipp_options)
526 {
527 /*
528 * Just use the server-supplied options...
529 */
530
531 snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
532 uri = finaluri;
533 }
534
535 /*
536 * See if we already have it listed in the Printers list, and add it if not...
537 */
538
539 type |= CUPS_PRINTER_REMOTE;
540 type &= ~CUPS_PRINTER_IMPLICIT;
541 update = 0;
542 hptr = strchr(host, '.');
543 sptr = strchr(ServerName, '.');
544
545 if (sptr != NULL && hptr != NULL)
546 {
547 /*
548 * Strip the common domain name components...
549 */
550
551 while (hptr != NULL)
552 {
553 if (!strcasecmp(hptr, sptr))
554 {
555 *hptr = '\0';
556 break;
557 }
558 else
559 hptr = strchr(hptr + 1, '.');
560 }
561 }
562
563 if (type & CUPS_PRINTER_CLASS)
564 {
565 /*
566 * Remote destination is a class...
567 */
568
569 if (!strncmp(resource, "/classes/", 9))
570 snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
571 else
572 return;
573
574 if ((p = cupsdFindClass(name)) == NULL && BrowseShortNames)
575 {
576 if ((p = cupsdFindClass(resource + 9)) != NULL)
577 {
578 if (p->hostname && strcasecmp(p->hostname, host))
579 {
580 /*
581 * Nope, this isn't the same host; if the hostname isn't the local host,
582 * add it to the other class and then find a class using the full host
583 * name...
584 */
585
586 if (p->type & CUPS_PRINTER_REMOTE)
587 {
588 cupsdLogMessage(CUPSD_LOG_INFO,
589 "Renamed remote class \"%s\" to \"%s@%s\"...",
590 p->name, p->name, p->hostname);
591 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
592 "Class \'%s\' deleted by directory services.",
593 p->name);
594
595 cupsArrayRemove(Printers, p);
596 cupsdSetStringf(&p->name, "%s@%s", p->name, p->hostname);
597 cupsdSetPrinterAttrs(p);
598 cupsArrayAdd(Printers, p);
599
600 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
601 "Class \'%s\' added by directory services.",
602 p->name);
603 }
604
605 p = NULL;
606 }
607 else if (!p->hostname)
608 {
609 /*
610 * Hostname not set, so this must be a cached remote printer
611 * that was created for a pending print job...
612 */
613
614 cupsdSetString(&p->hostname, host);
615 cupsdSetString(&p->uri, uri);
616 cupsdSetString(&p->device_uri, uri);
617 update = 1;
618 }
619 }
620 else
621 {
622 /*
623 * Use the short name for this shared class.
624 */
625
626 strlcpy(name, resource + 9, sizeof(name));
627 }
628 }
629 else if (p && !p->hostname)
630 {
631 /*
632 * Hostname not set, so this must be a cached remote printer
633 * that was created for a pending print job...
634 */
635
636 cupsdSetString(&p->hostname, host);
637 cupsdSetString(&p->uri, uri);
638 cupsdSetString(&p->device_uri, uri);
639 update = 1;
640 }
641
642 if (!p)
643 {
644 /*
645 * Class doesn't exist; add it...
646 */
647
648 p = cupsdAddClass(name);
649
650 cupsdLogMessage(CUPSD_LOG_INFO, "Added remote class \"%s\"...", name);
651
652 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
653 "Class \'%s\' added by directory services.", name);
654
655 /*
656 * Force the URI to point to the real server...
657 */
658
659 p->type = type & ~CUPS_PRINTER_REJECTING;
660 p->accepting = 1;
661 cupsdSetString(&p->uri, uri);
662 cupsdSetString(&p->device_uri, uri);
663 cupsdSetString(&p->hostname, host);
664
665 update = 1;
666 }
667 }
668 else
669 {
670 /*
671 * Remote destination is a printer...
672 */
673
674 if (strncmp(resource, "/printers/", 10) == 0)
675 snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
676 else
677 return;
678
679 if ((p = cupsdFindPrinter(name)) == NULL && BrowseShortNames)
680 {
681 if ((p = cupsdFindPrinter(resource + 10)) != NULL)
682 {
683 if (p->hostname && strcasecmp(p->hostname, host))
684 {
685 /*
686 * Nope, this isn't the same host; if the hostname isn't the local host,
687 * add it to the other printer and then find a printer using the full host
688 * name...
689 */
690
691 if (p->type & CUPS_PRINTER_REMOTE)
692 {
693 cupsdLogMessage(CUPSD_LOG_INFO,
694 "Renamed remote printer \"%s\" to \"%s@%s\"...",
695 p->name, p->name, p->hostname);
696 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
697 "Printer \'%s\' deleted by directory services.",
698 p->name);
699
700 cupsArrayRemove(Printers, p);
701 cupsdSetStringf(&p->name, "%s@%s", p->name, p->hostname);
702 cupsdSetPrinterAttrs(p);
703 cupsArrayAdd(Printers, p);
704
705 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
706 "Printer \'%s\' added by directory services.",
707 p->name);
708 }
709
710 p = NULL;
711 }
712 else if (!p->hostname)
713 {
714 /*
715 * Hostname not set, so this must be a cached remote printer
716 * that was created for a pending print job...
717 */
718
719 cupsdSetString(&p->hostname, host);
720 cupsdSetString(&p->uri, uri);
721 cupsdSetString(&p->device_uri, uri);
722 update = 1;
723 }
724 }
725 else
726 {
727 /*
728 * Use the short name for this shared printer.
729 */
730
731 strlcpy(name, resource + 10, sizeof(name));
732 }
733 }
734 else if (p && !p->hostname)
735 {
736 /*
737 * Hostname not set, so this must be a cached remote printer
738 * that was created for a pending print job...
739 */
740
741 cupsdSetString(&p->hostname, host);
742 cupsdSetString(&p->uri, uri);
743 cupsdSetString(&p->device_uri, uri);
744 update = 1;
745 }
746
747 if (!p)
748 {
749 /*
750 * Printer doesn't exist; add it...
751 */
752
753 p = cupsdAddPrinter(name);
754
755 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
756 "Printer \'%s\' added by directory services.", name);
757
758 cupsdLogMessage(CUPSD_LOG_INFO, "Added remote printer \"%s\"...", name);
759
760 /*
761 * Force the URI to point to the real server...
762 */
763
764 p->type = type & ~CUPS_PRINTER_REJECTING;
765 p->accepting = 1;
766 cupsdSetString(&p->hostname, host);
767 cupsdSetString(&p->uri, uri);
768 cupsdSetString(&p->device_uri, uri);
769
770 update = 1;
771 }
772 }
773
774 /*
775 * Update the state...
776 */
777
778 p->state = state;
779 p->browse_time = time(NULL);
780
781 if (type & CUPS_PRINTER_REJECTING)
782 {
783 type &= ~CUPS_PRINTER_REJECTING;
784
785 if (p->accepting)
786 {
787 update = 1;
788 p->accepting = 0;
789 }
790 }
791 else if (!p->accepting)
792 {
793 update = 1;
794 p->accepting = 1;
795 }
796
797 if (p->type != type)
798 {
799 p->type = type;
800 update = 1;
801 }
802
803 if (location && (!p->location || strcmp(p->location, location)))
804 {
805 cupsdSetString(&p->location, location);
806 update = 1;
807 }
808
809 if (info && (!p->info || strcmp(p->info, info)))
810 {
811 cupsdSetString(&p->info, info);
812 update = 1;
813 }
814
815 if (!make_model || !make_model[0])
816 {
817 if (type & CUPS_PRINTER_CLASS)
818 snprintf(local_make_model, sizeof(local_make_model),
819 "Remote Class on %s", host);
820 else
821 snprintf(local_make_model, sizeof(local_make_model),
822 "Remote Printer on %s", host);
823 }
824 else
825 snprintf(local_make_model, sizeof(local_make_model),
826 "%s on %s", make_model, host);
827
828 if (!p->make_model || strcmp(p->make_model, local_make_model))
829 {
830 cupsdSetString(&p->make_model, local_make_model);
831 update = 1;
832 }
833
834 if (type & CUPS_PRINTER_DELETE)
835 {
836 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
837 "%s \'%s\' deleted by directory services.",
838 (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name);
839
840 cupsdExpireSubscriptions(p, NULL);
841
842 cupsdDeletePrinter(p, 1);
843 cupsdUpdateImplicitClasses();
844 }
845 else if (update)
846 {
847 cupsdSetPrinterAttrs(p);
848 cupsdUpdateImplicitClasses();
849 }
850
851 /*
852 * See if we have a default printer... If not, make the first printer the
853 * default.
854 */
855
856 if (DefaultPrinter == NULL && Printers != NULL)
857 DefaultPrinter = (cupsd_printer_t *)cupsArrayFirst(Printers);
858
859 /*
860 * Do auto-classing if needed...
861 */
862
863 cupsdProcessImplicitClasses();
864
865 /*
866 * Update the printcap file...
867 */
868
869 cupsdWritePrintcap();
870 }
871
872
873 /*
874 * 'cupsdProcessImplicitClasses()' - Create/update implicit classes as needed.
875 */
876
877 void
878 cupsdProcessImplicitClasses(void)
879 {
880 int i; /* Looping var */
881 int update; /* Update printer attributes? */
882 char name[IPP_MAX_NAME], /* Name of printer */
883 *hptr; /* Pointer into hostname */
884 cupsd_printer_t *p, /* Printer information */
885 *pclass, /* Printer class */
886 *first; /* First printer in class */
887 int offset, /* Offset of name */
888 len; /* Length of name */
889
890
891 if (!ImplicitClasses || !Printers)
892 return;
893
894 /*
895 * Loop through all available printers and create classes as needed...
896 */
897
898 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0,
899 update = 0, pclass = NULL, first = NULL;
900 p != NULL;
901 p = (cupsd_printer_t *)cupsArrayNext(Printers))
902 {
903 /*
904 * Skip implicit classes...
905 */
906
907 if (p->type & CUPS_PRINTER_IMPLICIT)
908 {
909 len = 0;
910 continue;
911 }
912
913 /*
914 * If len == 0, get the length of this printer name up to the "@"
915 * sign (if any).
916 */
917
918 cupsArraySave(Printers);
919
920 if (len > 0 &&
921 strncasecmp(p->name, name + offset, len) == 0 &&
922 (p->name[len] == '\0' || p->name[len] == '@'))
923 {
924 /*
925 * We have more than one printer with the same name; see if
926 * we have a class, and if this printer is a member...
927 */
928
929 if (pclass && strcasecmp(pclass->name, name))
930 {
931 if (update)
932 cupsdSetPrinterAttrs(pclass);
933
934 update = 0;
935 pclass = NULL;
936 }
937
938 if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
939 {
940 /*
941 * Need to add the class...
942 */
943
944 pclass = cupsdAddPrinter(name);
945 cupsArrayAdd(ImplicitPrinters, pclass);
946
947 pclass->type |= CUPS_PRINTER_IMPLICIT;
948 pclass->accepting = 1;
949 pclass->state = IPP_PRINTER_IDLE;
950
951 cupsdSetString(&pclass->location, p->location);
952 cupsdSetString(&pclass->info, p->info);
953
954 update = 1;
955
956 cupsdLogMessage(CUPSD_LOG_INFO, "Added implicit class \"%s\"...",
957 name);
958 }
959
960 if (first != NULL)
961 {
962 for (i = 0; i < pclass->num_printers; i ++)
963 if (pclass->printers[i] == first)
964 break;
965
966 if (i >= pclass->num_printers)
967 {
968 first->in_implicit_class = 1;
969 cupsdAddPrinterToClass(pclass, first);
970 }
971
972 first = NULL;
973 }
974
975 for (i = 0; i < pclass->num_printers; i ++)
976 if (pclass->printers[i] == p)
977 break;
978
979 if (i >= pclass->num_printers)
980 {
981 p->in_implicit_class = 1;
982 cupsdAddPrinterToClass(pclass, p);
983 update = 1;
984 }
985 }
986 else
987 {
988 /*
989 * First time around; just get name length and mark it as first
990 * in the list...
991 */
992
993 if ((hptr = strchr(p->name, '@')) != NULL)
994 len = hptr - p->name;
995 else
996 len = strlen(p->name);
997
998 strncpy(name, p->name, len);
999 name[len] = '\0';
1000 offset = 0;
1001
1002 if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
1003 !(first->type & CUPS_PRINTER_IMPLICIT))
1004 {
1005 /*
1006 * Can't use same name as a local printer; add "Any" to the
1007 * front of the name, unless we have explicitly disabled
1008 * the "ImplicitAnyClasses"...
1009 */
1010
1011 if (ImplicitAnyClasses && len < (sizeof(name) - 4))
1012 {
1013 /*
1014 * Add "Any" to the class name...
1015 */
1016
1017 strcpy(name, "Any");
1018 strncpy(name + 3, p->name, len);
1019 name[len + 3] = '\0';
1020 offset = 3;
1021 }
1022 else
1023 {
1024 /*
1025 * Don't create an implicit class if we have a local printer
1026 * with the same name...
1027 */
1028
1029 len = 0;
1030 cupsArrayRestore(Printers);
1031 continue;
1032 }
1033 }
1034
1035 first = p;
1036 }
1037
1038 cupsArrayRestore(Printers);
1039 }
1040
1041 /*
1042 * Update the last printer class as needed...
1043 */
1044
1045 if (pclass && update)
1046 cupsdSetPrinterAttrs(pclass);
1047 }
1048
1049
1050 /*
1051 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
1052 */
1053
1054 void
1055 cupsdSaveRemoteCache(void)
1056 {
1057 int i; /* Looping var */
1058 cups_file_t *fp; /* printers.conf file */
1059 char temp[1024]; /* Temporary string */
1060 cupsd_printer_t *printer; /* Current printer class */
1061 time_t curtime; /* Current time */
1062 struct tm *curdate; /* Current date */
1063
1064
1065 /*
1066 * Create the remote.cache file...
1067 */
1068
1069 snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir);
1070
1071 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1072 {
1073 cupsdLogMessage(CUPSD_LOG_ERROR,
1074 "Unable to save remote.cache - %s", strerror(errno));
1075 return;
1076 }
1077 else
1078 cupsdLogMessage(CUPSD_LOG_INFO, "Saving remote.cache...");
1079
1080 /*
1081 * Restrict access to the file...
1082 */
1083
1084 fchown(cupsFileNumber(fp), getuid(), Group);
1085 fchmod(cupsFileNumber(fp), ConfigFilePerm);
1086
1087 /*
1088 * Write a small header to the file...
1089 */
1090
1091 curtime = time(NULL);
1092 curdate = localtime(&curtime);
1093 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1094
1095 cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n");
1096 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1097
1098 /*
1099 * Write each local printer known to the system...
1100 */
1101
1102 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
1103 printer;
1104 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
1105 {
1106 /*
1107 * Skip local destinations...
1108 */
1109
1110 if (!(printer->type & CUPS_PRINTER_REMOTE))
1111 continue;
1112
1113 /*
1114 * Write printers as needed...
1115 */
1116
1117 if (printer == DefaultPrinter)
1118 cupsFilePuts(fp, "<Default");
1119 else
1120 cupsFilePutChar(fp, '<');
1121
1122 if (printer->type & CUPS_PRINTER_CLASS)
1123 cupsFilePrintf(fp, "Class %s>\n", printer->name);
1124 else
1125 cupsFilePrintf(fp, "Printer %s>\n", printer->name);
1126
1127 cupsFilePrintf(fp, "Type %d\n", printer->type);
1128
1129 cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_time);
1130
1131 if (printer->info)
1132 cupsFilePrintf(fp, "Info %s\n", printer->info);
1133
1134 if (printer->make_model)
1135 cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model);
1136
1137 if (printer->location)
1138 cupsFilePrintf(fp, "Location %s\n", printer->location);
1139
1140 if (printer->device_uri)
1141 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
1142
1143 if (printer->state == IPP_PRINTER_STOPPED)
1144 {
1145 cupsFilePuts(fp, "State Stopped\n");
1146 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
1147 }
1148 else
1149 cupsFilePuts(fp, "State Idle\n");
1150
1151 if (printer->accepting)
1152 cupsFilePuts(fp, "Accepting Yes\n");
1153 else
1154 cupsFilePuts(fp, "Accepting No\n");
1155
1156 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
1157 printer->job_sheets[1]);
1158
1159 for (i = 0; i < printer->num_users; i ++)
1160 cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
1161 printer->users[i]);
1162
1163 if (printer->type & CUPS_PRINTER_CLASS)
1164 cupsFilePuts(fp, "</Class>\n");
1165 else
1166 cupsFilePuts(fp, "</Printer>\n");
1167 }
1168
1169 cupsFileClose(fp);
1170 }
1171
1172
1173 /*
1174 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
1175 */
1176
1177 void
1178 cupsdSendBrowseDelete(
1179 cupsd_printer_t *p) /* I - Printer to delete */
1180 {
1181 /*
1182 * Only announce if browsing is enabled...
1183 */
1184
1185 if (!Browsing || !p->shared)
1186 return;
1187
1188 /*
1189 * First mark the printer for deletion...
1190 */
1191
1192 p->type |= CUPS_PRINTER_DELETE;
1193
1194 /*
1195 * Announce the deletion...
1196 */
1197
1198 if (BrowseLocalProtocols & BROWSE_CUPS)
1199 cupsdSendCUPSBrowse(p);
1200 #ifdef HAVE_LIBSLP
1201 if (BrowseLocalProtocols & BROWSE_SLP)
1202 slp_dereg_printer(p);
1203 #endif /* HAVE_LIBSLP */
1204 }
1205
1206
1207 /*
1208 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
1209 */
1210
1211 void
1212 cupsdSendBrowseList(void)
1213 {
1214 int count; /* Number of dests to update */
1215 cupsd_printer_t *p; /* Current printer */
1216 time_t ut, /* Minimum update time */
1217 to; /* Timeout time */
1218
1219
1220 if (!Browsing || !BrowseLocalProtocols || !Printers)
1221 return;
1222
1223 /*
1224 * Compute the update and timeout times...
1225 */
1226
1227 ut = time(NULL) - BrowseInterval;
1228 to = time(NULL) - BrowseTimeout;
1229
1230 /*
1231 * Figure out how many printers need an update...
1232 */
1233
1234 if (BrowseInterval > 0)
1235 {
1236 int max_count; /* Maximum number to update */
1237
1238
1239 /*
1240 * Throttle the number of printers we'll be updating this time
1241 * around based on the number of queues that need updating and
1242 * the maximum number of queues to update each second...
1243 */
1244
1245 max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1;
1246
1247 for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1248 count < max_count && p != NULL;
1249 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1250 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
1251 p->shared && p->browse_time < ut)
1252 count ++;
1253
1254 /*
1255 * Loop through all of the printers and send local updates as needed...
1256 */
1257
1258 if (BrowseNext)
1259 p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext);
1260 else
1261 p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1262
1263 for (;
1264 count > 0;
1265 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1266 {
1267 /*
1268 * Check for wraparound...
1269 */
1270
1271 if (!p)
1272 p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1273
1274 if (!p)
1275 break;
1276 else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
1277 !p->shared)
1278 continue;
1279 else if (p->browse_time < ut)
1280 {
1281 /*
1282 * Need to send an update...
1283 */
1284
1285 count --;
1286
1287 p->browse_time = time(NULL);
1288
1289 if (BrowseLocalProtocols & BROWSE_CUPS)
1290 cupsdSendCUPSBrowse(p);
1291
1292 #ifdef HAVE_LIBSLP
1293 if (BrowseLocalProtocols & BROWSE_SLP)
1294 cupsdSendSLPBrowse(p);
1295 #endif /* HAVE_LIBSLP */
1296 }
1297 }
1298
1299 /*
1300 * Save where we left off so that all printers get updated...
1301 */
1302
1303 BrowseNext = p;
1304 }
1305
1306 /*
1307 * Loop through all of the printers and send local updates as needed...
1308 */
1309
1310 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1311 p;
1312 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1313 {
1314 /*
1315 * If this is a remote queue, see if it needs to be timed out...
1316 */
1317
1318 if (p->type & CUPS_PRINTER_REMOTE)
1319 {
1320 if (p->browse_time < to)
1321 {
1322 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
1323 "%s \'%s\' deleted by directory services (timeout).",
1324 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
1325 p->name);
1326
1327 cupsdLogMessage(CUPSD_LOG_INFO,
1328 "Remote destination \"%s\" has timed out; deleting it...",
1329 p->name);
1330
1331 cupsArraySave(Printers);
1332 cupsdDeletePrinter(p, 1);
1333 cupsArrayRestore(Printers);
1334 }
1335 }
1336 }
1337 }
1338
1339
1340 /*
1341 * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
1342 */
1343
1344 void
1345 cupsdSendCUPSBrowse(cupsd_printer_t *p) /* I - Printer to send */
1346 {
1347 int i; /* Looping var */
1348 cups_ptype_t type; /* Printer type */
1349 cupsd_dirsvc_addr_t *b; /* Browse address */
1350 int bytes; /* Length of packet */
1351 char packet[1453]; /* Browse data packet */
1352 char uri[1024]; /* Printer URI */
1353 char options[1024]; /* Browse local options */
1354 cupsd_netif_t *iface; /* Network interface */
1355
1356
1357 /*
1358 * Figure out the printer type value...
1359 */
1360
1361 type = p->type | CUPS_PRINTER_REMOTE;
1362
1363 if (!p->accepting)
1364 type |= CUPS_PRINTER_REJECTING;
1365
1366 /*
1367 * Initialize the browse options...
1368 */
1369
1370 if (BrowseLocalOptions)
1371 snprintf(options, sizeof(options), " ipp-options=%s", BrowseLocalOptions);
1372 else
1373 options[0] = '\0';
1374
1375 /*
1376 * Send a packet to each browse address...
1377 */
1378
1379 for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
1380 if (b->iface[0])
1381 {
1382 /*
1383 * Send the browse packet to one or more interfaces...
1384 */
1385
1386 if (strcmp(b->iface, "*") == 0)
1387 {
1388 /*
1389 * Send to all local interfaces...
1390 */
1391
1392 cupsdNetIFUpdate();
1393
1394 for (iface = NetIFList; iface != NULL; iface = iface->next)
1395 {
1396 /*
1397 * Only send to local, IPv4 interfaces...
1398 */
1399
1400 if (!iface->is_local || !iface->port ||
1401 iface->address.addr.sa_family != AF_INET)
1402 continue;
1403
1404 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1405 iface->hostname, iface->port,
1406 (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
1407 "/printers/%s",
1408 p->name);
1409 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1410 type, p->state, uri, p->location ? p->location : "",
1411 p->info ? p->info : "",
1412 p->make_model ? p->make_model : "Unknown", options);
1413
1414 bytes = strlen(packet);
1415
1416 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1417 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
1418 iface->name, packet);
1419
1420 iface->broadcast.ipv4.sin_port = htons(BrowsePort);
1421
1422 sendto(BrowseSocket, packet, bytes, 0,
1423 (struct sockaddr *)&(iface->broadcast),
1424 sizeof(struct sockaddr_in));
1425 }
1426 }
1427 else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
1428 {
1429 /*
1430 * Send to the named interface using the IPv4 address...
1431 */
1432
1433 while (iface)
1434 if (strcasecmp(b->iface, iface->name))
1435 {
1436 iface = NULL;
1437 break;
1438 }
1439 else if (iface->address.addr.sa_family == AF_INET && iface->port)
1440 break;
1441 else
1442 iface = iface->next;
1443
1444 if (iface)
1445 {
1446 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
1447 iface->hostname, iface->port,
1448 (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
1449 "/printers/%s",
1450 p->name);
1451 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1452 type, p->state, uri, p->location ? p->location : "",
1453 p->info ? p->info : "",
1454 p->make_model ? p->make_model : "Unknown", options);
1455
1456 bytes = strlen(packet);
1457
1458 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1459 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
1460 iface->name, packet);
1461
1462 iface->broadcast.ipv4.sin_port = htons(BrowsePort);
1463
1464 sendto(BrowseSocket, packet, bytes, 0,
1465 (struct sockaddr *)&(iface->broadcast),
1466 sizeof(struct sockaddr_in));
1467 }
1468 }
1469 }
1470 else
1471 {
1472 /*
1473 * Send the browse packet to the indicated address using
1474 * the default server name...
1475 */
1476
1477 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
1478 type, p->state, p->uri,
1479 p->location ? p->location : "",
1480 p->info ? p->info : "",
1481 p->make_model ? p->make_model : "Unknown", options);
1482
1483 bytes = strlen(packet);
1484 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1485 "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
1486
1487 if (sendto(BrowseSocket, packet, bytes, 0,
1488 (struct sockaddr *)&(b->to),
1489 sizeof(struct sockaddr_in)) <= 0)
1490 {
1491 /*
1492 * Unable to send browse packet, so remove this address from the
1493 * list...
1494 */
1495
1496 cupsdLogMessage(CUPSD_LOG_ERROR,
1497 "cupsdSendBrowseList: sendto failed for browser %d - %s.",
1498 b - Browsers + 1, strerror(errno));
1499
1500 if (i > 1)
1501 memcpy(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
1502
1503 b --;
1504 NumBrowsers --;
1505 }
1506 }
1507 }
1508
1509
1510 #ifdef HAVE_LIBSLP
1511 /*
1512 * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP.
1513 */
1514
1515 void
1516 cupsdSendSLPBrowse(cupsd_printer_t *p) /* I - Printer to register */
1517 {
1518 char srvurl[HTTP_MAX_URI], /* Printer service URI */
1519 attrs[8192], /* Printer attributes */
1520 finishings[1024], /* Finishings to support */
1521 make_model[IPP_MAX_NAME * 2],
1522 /* Make and model, quoted */
1523 location[IPP_MAX_NAME * 2],
1524 /* Location, quoted */
1525 info[IPP_MAX_NAME * 2], /* Info, quoted */
1526 *src, /* Pointer to original string */
1527 *dst; /* Pointer to destination string */
1528 ipp_attribute_t *authentication; /* uri-authentication-supported value */
1529 SLPError error; /* SLP error, if any */
1530
1531
1532 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendSLPBrowse(%p = \"%s\")", p,
1533 p->name);
1534
1535 /*
1536 * Make the SLP service URL that conforms to the IANA
1537 * 'printer:' template.
1538 */
1539
1540 snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
1541
1542 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
1543
1544 /*
1545 * Figure out the finishings string...
1546 */
1547
1548 if (p->type & CUPS_PRINTER_STAPLE)
1549 strcpy(finishings, "staple");
1550 else
1551 finishings[0] = '\0';
1552
1553 if (p->type & CUPS_PRINTER_BIND)
1554 {
1555 if (finishings[0])
1556 strlcat(finishings, ",bind", sizeof(finishings));
1557 else
1558 strcpy(finishings, "bind");
1559 }
1560
1561 if (p->type & CUPS_PRINTER_PUNCH)
1562 {
1563 if (finishings[0])
1564 strlcat(finishings, ",punch", sizeof(finishings));
1565 else
1566 strcpy(finishings, "punch");
1567 }
1568
1569 if (p->type & CUPS_PRINTER_COVER)
1570 {
1571 if (finishings[0])
1572 strlcat(finishings, ",cover", sizeof(finishings));
1573 else
1574 strcpy(finishings, "cover");
1575 }
1576
1577 if (p->type & CUPS_PRINTER_SORT)
1578 {
1579 if (finishings[0])
1580 strlcat(finishings, ",sort", sizeof(finishings));
1581 else
1582 strcpy(finishings, "sort");
1583 }
1584
1585 if (!finishings[0])
1586 strcpy(finishings, "none");
1587
1588 /*
1589 * Quote any commas in the make and model, location, and info strings...
1590 */
1591
1592 for (src = p->make_model, dst = make_model;
1593 src && *src && dst < (make_model + sizeof(make_model) - 2);)
1594 {
1595 if (*src == ',' || *src == '\\' || *src == ')')
1596 *dst++ = '\\';
1597
1598 *dst++ = *src++;
1599 }
1600
1601 *dst = '\0';
1602
1603 if (!make_model[0])
1604 strcpy(make_model, "Unknown");
1605
1606 for (src = p->location, dst = location;
1607 src && *src && dst < (location + sizeof(location) - 2);)
1608 {
1609 if (*src == ',' || *src == '\\' || *src == ')')
1610 *dst++ = '\\';
1611
1612 *dst++ = *src++;
1613 }
1614
1615 *dst = '\0';
1616
1617 if (!location[0])
1618 strcpy(location, "Unknown");
1619
1620 for (src = p->info, dst = info;
1621 src && *src && dst < (info + sizeof(info) - 2);)
1622 {
1623 if (*src == ',' || *src == '\\' || *src == ')')
1624 *dst++ = '\\';
1625
1626 *dst++ = *src++;
1627 }
1628
1629 *dst = '\0';
1630
1631 if (!info[0])
1632 strcpy(info, "Unknown");
1633
1634 /*
1635 * Get the authentication value...
1636 */
1637
1638 authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
1639 IPP_TAG_KEYWORD);
1640
1641 /*
1642 * Make the SLP attribute string list that conforms to
1643 * the IANA 'printer:' template.
1644 */
1645
1646 snprintf(attrs, sizeof(attrs),
1647 "(printer-uri-supported=%s),"
1648 "(uri-authentication-supported=%s>),"
1649 #ifdef HAVE_SSL
1650 "(uri-security-supported=tls>),"
1651 #else
1652 "(uri-security-supported=none>),"
1653 #endif /* HAVE_SSL */
1654 "(printer-name=%s),"
1655 "(printer-location=%s),"
1656 "(printer-info=%s),"
1657 "(printer-more-info=%s),"
1658 "(printer-make-and-model=%s),"
1659 "(charset-supported=utf-8),"
1660 "(natural-language-configured=%s),"
1661 "(natural-language-supported=de,en,es,fr,it),"
1662 "(color-supported=%s),"
1663 "(finishings-supported=%s),"
1664 "(sides-supported=one-sided%s),"
1665 "(multiple-document-jobs-supported=true)"
1666 "(ipp-versions-supported=1.0,1.1)",
1667 p->uri, authentication->values[0].string.text, p->name, location,
1668 info, p->uri, make_model, DefaultLanguage,
1669 p->type & CUPS_PRINTER_COLOR ? "true" : "false",
1670 finishings,
1671 p->type & CUPS_PRINTER_DUPLEX ?
1672 ",two-sided-long-edge,two-sided-short-edge" : "");
1673
1674 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
1675
1676 /*
1677 * Register the printer with the SLP server...
1678 */
1679
1680 error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
1681 SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
1682
1683 if (error != SLP_OK)
1684 cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
1685 error);
1686 }
1687 #endif /* HAVE_LIBSLP */
1688
1689
1690 /*
1691 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1692 */
1693
1694 void
1695 cupsdStartBrowsing(void)
1696 {
1697 int val; /* Socket option value */
1698 struct sockaddr_in addr; /* Broadcast address */
1699
1700
1701 BrowseNext = NULL;
1702
1703 if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
1704 return;
1705
1706 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
1707 {
1708 if (BrowseSocket < 0)
1709 {
1710 /*
1711 * Create the broadcast socket...
1712 */
1713
1714 if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1715 {
1716 cupsdLogMessage(CUPSD_LOG_ERROR,
1717 "cupsdStartBrowsing: Unable to create broadcast "
1718 "socket - %s.", strerror(errno));
1719 BrowseLocalProtocols &= ~BROWSE_CUPS;
1720 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1721 return;
1722 }
1723
1724 /*
1725 * Bind the socket to browse port...
1726 */
1727
1728 memset(&addr, 0, sizeof(addr));
1729 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1730 addr.sin_family = AF_INET;
1731 addr.sin_port = htons(BrowsePort);
1732
1733 if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
1734 {
1735 cupsdLogMessage(CUPSD_LOG_ERROR,
1736 "cupsdStartBrowsing: Unable to bind broadcast "
1737 "socket - %s.", strerror(errno));
1738
1739 #ifdef WIN32
1740 closesocket(BrowseSocket);
1741 #else
1742 close(BrowseSocket);
1743 #endif /* WIN32 */
1744
1745 BrowseSocket = -1;
1746 BrowseLocalProtocols &= ~BROWSE_CUPS;
1747 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1748 return;
1749 }
1750 }
1751
1752 /*
1753 * Set the "broadcast" flag...
1754 */
1755
1756 val = 1;
1757 if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
1758 {
1759 cupsdLogMessage(CUPSD_LOG_ERROR,
1760 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1761 strerror(errno));
1762
1763 #ifdef WIN32
1764 closesocket(BrowseSocket);
1765 #else
1766 close(BrowseSocket);
1767 #endif /* WIN32 */
1768
1769 BrowseSocket = -1;
1770 BrowseLocalProtocols &= ~BROWSE_CUPS;
1771 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1772 return;
1773 }
1774
1775 /*
1776 * Close the socket on exec...
1777 */
1778
1779 fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
1780
1781 /*
1782 * Finally, add the socket to the input selection set...
1783 */
1784
1785 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1786 "cupsdStartBrowsing: Adding fd %d to InputSet...",
1787 BrowseSocket);
1788
1789 FD_SET(BrowseSocket, InputSet);
1790 }
1791 else
1792 BrowseSocket = -1;
1793
1794 #ifdef HAVE_LIBSLP
1795 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
1796 {
1797 /*
1798 * Open SLP handle...
1799 */
1800
1801 if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
1802 {
1803 cupsdLogMessage(CUPSD_LOG_ERROR,
1804 "Unable to open an SLP handle; disabling SLP browsing!");
1805 BrowseLocalProtocols &= ~BROWSE_SLP;
1806 BrowseRemoteProtocols &= ~BROWSE_SLP;
1807 }
1808
1809 BrowseSLPRefresh = 0;
1810 }
1811 #endif /* HAVE_LIBSLP */
1812 }
1813
1814
1815 /*
1816 * 'cupsdStartPolling()' - Start polling servers as needed.
1817 */
1818
1819 void
1820 cupsdStartPolling(void)
1821 {
1822 int i; /* Looping var */
1823 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
1824 char polld[1024]; /* Poll daemon path */
1825 char sport[10]; /* Server port */
1826 char bport[10]; /* Browser port */
1827 char interval[10]; /* Poll interval */
1828 int statusfds[2]; /* Status pipe */
1829 char *argv[6]; /* Arguments */
1830 char *envp[100]; /* Environment */
1831
1832
1833 /*
1834 * Don't do anything if we aren't polling...
1835 */
1836
1837 if (NumPolled == 0)
1838 {
1839 PollPipe = -1;
1840 PollStatusBuffer = NULL;
1841 return;
1842 }
1843
1844 /*
1845 * Setup string arguments for polld, port and interval options.
1846 */
1847
1848 snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
1849
1850 sprintf(bport, "%d", BrowsePort);
1851
1852 if (BrowseInterval)
1853 sprintf(interval, "%d", BrowseInterval);
1854 else
1855 strcpy(interval, "30");
1856
1857 argv[0] = "cups-polld";
1858 argv[2] = sport;
1859 argv[3] = interval;
1860 argv[4] = bport;
1861 argv[5] = NULL;
1862
1863 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
1864
1865 /*
1866 * Create a pipe that receives the status messages from each
1867 * polling daemon...
1868 */
1869
1870 if (cupsdOpenPipe(statusfds))
1871 {
1872 cupsdLogMessage(CUPSD_LOG_ERROR,
1873 "Unable to create polling status pipes - %s.",
1874 strerror(errno));
1875 PollPipe = -1;
1876 PollStatusBuffer = NULL;
1877 return;
1878 }
1879
1880 PollPipe = statusfds[0];
1881 PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]");
1882
1883 /*
1884 * Run each polling daemon, redirecting stderr to the polling pipe...
1885 */
1886
1887 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
1888 {
1889 sprintf(sport, "%d", pollp->port);
1890
1891 argv[1] = pollp->hostname;
1892
1893 if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1,
1894 0, &(pollp->pid)) < 0)
1895 {
1896 cupsdLogMessage(CUPSD_LOG_ERROR,
1897 "cupsdStartPolling: Unable to fork polling daemon - %s",
1898 strerror(errno));
1899 pollp->pid = 0;
1900 break;
1901 }
1902 else
1903 cupsdLogMessage(CUPSD_LOG_DEBUG,
1904 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1905 pollp->hostname, pollp->port, pollp->pid);
1906 }
1907
1908 close(statusfds[1]);
1909
1910 /*
1911 * Finally, add the pipe to the input selection set...
1912 */
1913
1914 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1915 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe);
1916
1917 FD_SET(PollPipe, InputSet);
1918 }
1919
1920
1921 /*
1922 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1923 */
1924
1925 void
1926 cupsdStopBrowsing(void)
1927 {
1928 if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
1929 return;
1930
1931 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) &&
1932 BrowseSocket >= 0)
1933 {
1934 /*
1935 * Close the socket and remove it from the input selection set.
1936 */
1937
1938 #ifdef WIN32
1939 closesocket(BrowseSocket);
1940 #else
1941 close(BrowseSocket);
1942 #endif /* WIN32 */
1943
1944 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1945 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1946 BrowseSocket);
1947
1948 FD_CLR(BrowseSocket, InputSet);
1949 BrowseSocket = -1;
1950 }
1951
1952 #ifdef HAVE_LIBSLP
1953 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
1954 {
1955 /*
1956 * Close SLP handle...
1957 */
1958
1959 SLPClose(BrowseSLPHandle);
1960 }
1961 #endif /* HAVE_LIBSLP */
1962 }
1963
1964
1965 /*
1966 * 'cupsdStopPolling()' - Stop polling servers as needed.
1967 */
1968
1969 void
1970 cupsdStopPolling(void)
1971 {
1972 int i; /* Looping var */
1973 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
1974
1975
1976 if (PollPipe >= 0)
1977 {
1978 cupsdStatBufDelete(PollStatusBuffer);
1979 close(PollPipe);
1980
1981 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1982 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe);
1983 FD_CLR(PollPipe, InputSet);
1984
1985 PollPipe = -1;
1986 PollStatusBuffer = NULL;
1987 }
1988
1989 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
1990 if (pollp->pid)
1991 cupsdEndProcess(pollp->pid, 0);
1992 }
1993
1994
1995 /*
1996 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1997 */
1998
1999 void
2000 cupsdUpdateCUPSBrowse(void)
2001 {
2002 int i; /* Looping var */
2003 int auth; /* Authorization status */
2004 int len; /* Length of name string */
2005 int bytes; /* Number of bytes left */
2006 char packet[1541], /* Broadcast packet */
2007 *pptr; /* Pointer into packet */
2008 socklen_t srclen; /* Length of source address */
2009 http_addr_t srcaddr; /* Source address */
2010 char srcname[1024]; /* Source hostname */
2011 unsigned address[4]; /* Source address */
2012 unsigned type; /* Printer type */
2013 unsigned state; /* Printer state */
2014 char uri[HTTP_MAX_URI], /* Printer URI */
2015 method[HTTP_MAX_URI], /* Method portion of URI */
2016 username[HTTP_MAX_URI], /* Username portion of URI */
2017 host[HTTP_MAX_URI], /* Host portion of URI */
2018 resource[HTTP_MAX_URI], /* Resource portion of URI */
2019 info[IPP_MAX_NAME], /* Information string */
2020 location[IPP_MAX_NAME], /* Location string */
2021 make_model[IPP_MAX_NAME];/* Make and model string */
2022 int port; /* Port portion of URI */
2023 cupsd_netif_t *iface; /* Network interface */
2024 int num_attrs; /* Number of attributes */
2025 cups_option_t *attrs; /* Attributes */
2026
2027
2028 /*
2029 * Read a packet from the browse socket...
2030 */
2031
2032 srclen = sizeof(srcaddr);
2033 if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
2034 (struct sockaddr *)&srcaddr, &srclen)) < 0)
2035 {
2036 /*
2037 * "Connection refused" is returned under Linux if the destination port
2038 * or address is unreachable from a previous sendto(); check for the
2039 * error here and ignore it for now...
2040 */
2041
2042 if (errno != ECONNREFUSED && errno != EAGAIN)
2043 {
2044 cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
2045 strerror(errno));
2046 cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
2047
2048 cupsdStopBrowsing();
2049 Browsing = 0;
2050 }
2051
2052 return;
2053 }
2054
2055 packet[bytes] = '\0';
2056
2057 /*
2058 * If we're about to sleep, ignore incoming browse packets.
2059 */
2060
2061 if (Sleeping)
2062 return;
2063
2064 /*
2065 * Figure out where it came from...
2066 */
2067
2068 #ifdef AF_INET6
2069 if (srcaddr.addr.sa_family == AF_INET6)
2070 {
2071 address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
2072 address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
2073 address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
2074 address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
2075 }
2076 else
2077 #endif /* AF_INET6 */
2078 {
2079 address[0] = 0;
2080 address[1] = 0;
2081 address[2] = 0;
2082 address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
2083 }
2084
2085 if (HostNameLookups)
2086 httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
2087 else
2088 httpAddrString(&srcaddr, srcname, sizeof(srcname));
2089
2090 len = strlen(srcname);
2091
2092 /*
2093 * Do ACL stuff...
2094 */
2095
2096 if (BrowseACL)
2097 {
2098 if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
2099 {
2100 /*
2101 * Access from localhost (127.0.0.1) is always allowed...
2102 */
2103
2104 auth = AUTH_ALLOW;
2105 }
2106 else
2107 {
2108 /*
2109 * Do authorization checks on the domain/address...
2110 */
2111
2112 switch (BrowseACL->order_type)
2113 {
2114 default :
2115 auth = AUTH_DENY; /* anti-compiler-warning-code */
2116 break;
2117
2118 case AUTH_ALLOW : /* Order Deny,Allow */
2119 auth = AUTH_ALLOW;
2120
2121 if (cupsdCheckAuth(address, srcname, len,
2122 BrowseACL->num_deny, BrowseACL->deny))
2123 auth = AUTH_DENY;
2124
2125 if (cupsdCheckAuth(address, srcname, len,
2126 BrowseACL->num_allow, BrowseACL->allow))
2127 auth = AUTH_ALLOW;
2128 break;
2129
2130 case AUTH_DENY : /* Order Allow,Deny */
2131 auth = AUTH_DENY;
2132
2133 if (cupsdCheckAuth(address, srcname, len,
2134 BrowseACL->num_allow, BrowseACL->allow))
2135 auth = AUTH_ALLOW;
2136
2137 if (cupsdCheckAuth(address, srcname, len,
2138 BrowseACL->num_deny, BrowseACL->deny))
2139 auth = AUTH_DENY;
2140 break;
2141 }
2142 }
2143 }
2144 else
2145 auth = AUTH_ALLOW;
2146
2147 if (auth == AUTH_DENY)
2148 {
2149 cupsdLogMessage(CUPSD_LOG_DEBUG,
2150 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes,
2151 srcname);
2152 return;
2153 }
2154
2155 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2156 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes,
2157 srcname, packet);
2158
2159 /*
2160 * Parse packet...
2161 */
2162
2163 if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
2164 {
2165 cupsdLogMessage(CUPSD_LOG_WARN,
2166 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet);
2167 return;
2168 }
2169
2170 strcpy(location, "Location Unknown");
2171 strcpy(info, "No Information Available");
2172 make_model[0] = '\0';
2173 num_attrs = 0;
2174 attrs = NULL;
2175
2176 if ((pptr = strchr(packet, '\"')) != NULL)
2177 {
2178 /*
2179 * Have extended information; can't use sscanf for it because not all
2180 * sscanf's allow empty strings with %[^\"]...
2181 */
2182
2183 for (i = 0, pptr ++;
2184 i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
2185 i ++, pptr ++)
2186 location[i] = *pptr;
2187
2188 if (i)
2189 location[i] = '\0';
2190
2191 if (*pptr == '\"')
2192 pptr ++;
2193
2194 while (*pptr && isspace(*pptr & 255))
2195 pptr ++;
2196
2197 if (*pptr == '\"')
2198 {
2199 for (i = 0, pptr ++;
2200 i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
2201 i ++, pptr ++)
2202 info[i] = *pptr;
2203
2204 info[i] = '\0';
2205
2206 if (*pptr == '\"')
2207 pptr ++;
2208
2209 while (*pptr && isspace(*pptr & 255))
2210 pptr ++;
2211
2212 if (*pptr == '\"')
2213 {
2214 for (i = 0, pptr ++;
2215 i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
2216 i ++, pptr ++)
2217 make_model[i] = *pptr;
2218
2219 if (*pptr == '\"')
2220 pptr ++;
2221
2222 make_model[i] = '\0';
2223
2224 if (*pptr)
2225 num_attrs = cupsParseOptions(pptr, num_attrs, &attrs);
2226 }
2227 }
2228 }
2229
2230 DEBUG_puts(packet);
2231 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
2232 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
2233 type, state, uri, location, info, make_model));
2234
2235 /*
2236 * Pull the URI apart to see if this is a local or remote printer...
2237 */
2238
2239 httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), username,
2240 sizeof(username), host, sizeof(host), &port, resource,
2241 sizeof(resource));
2242
2243 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName));
2244
2245 /*
2246 * Check for packets from the local server...
2247 */
2248
2249 if (!strcasecmp(host, ServerName) && port == LocalPort)
2250 {
2251 cupsFreeOptions(num_attrs, attrs);
2252 return;
2253 }
2254
2255 cupsdNetIFUpdate();
2256
2257 for (iface = NetIFList; iface != NULL; iface = iface->next)
2258 if (!strcasecmp(host, iface->hostname) && port == iface->port)
2259 {
2260 cupsFreeOptions(num_attrs, attrs);
2261 return;
2262 }
2263
2264 /*
2265 * Do relaying...
2266 */
2267
2268 for (i = 0; i < NumRelays; i ++)
2269 if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from)))
2270 if (sendto(BrowseSocket, packet, bytes, 0,
2271 (struct sockaddr *)&(Relays[i].to),
2272 sizeof(http_addr_t)) <= 0)
2273 {
2274 cupsdLogMessage(CUPSD_LOG_ERROR,
2275 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
2276 i + 1, strerror(errno));
2277 cupsFreeOptions(num_attrs, attrs);
2278 return;
2279 }
2280
2281 /*
2282 * Process the browse data...
2283 */
2284
2285 cupsdProcessBrowseData(uri, (cups_ptype_t)type, (ipp_pstate_t)state, location,
2286 info, make_model, num_attrs, attrs);
2287 cupsFreeOptions(num_attrs, attrs);
2288 }
2289
2290
2291 /*
2292 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
2293 */
2294
2295 void
2296 cupsdUpdatePolling(void)
2297 {
2298 char *ptr, /* Pointer to end of line in buffer */
2299 message[1024]; /* Pointer to message text */
2300 int loglevel; /* Log level for message */
2301
2302
2303 while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
2304 message, sizeof(message))) != NULL)
2305 if (!strchr(PollStatusBuffer->buffer, '\n'))
2306 break;
2307
2308 if (ptr == NULL)
2309 {
2310 /*
2311 * All polling processes have died; stop polling...
2312 */
2313
2314 cupsdLogMessage(CUPSD_LOG_ERROR,
2315 "cupsdUpdatePolling: all polling processes have exited!");
2316 cupsdStopPolling();
2317 }
2318 }
2319
2320
2321 #ifdef HAVE_LIBSLP
2322 /*
2323 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2324 */
2325
2326 void
2327 cupsdUpdateSLPBrowse(void)
2328 {
2329 slpsrvurl_t *s, /* Temporary list of service URLs */
2330 *next; /* Next service in list */
2331 cupsd_printer_t p; /* Printer information */
2332 const char *uri; /* Pointer to printer URI */
2333 char method[HTTP_MAX_URI],
2334 /* Method portion of URI */
2335 username[HTTP_MAX_URI],
2336 /* Username portion of URI */
2337 host[HTTP_MAX_URI],
2338 /* Host portion of URI */
2339 resource[HTTP_MAX_URI];
2340 /* Resource portion of URI */
2341 int port; /* Port portion of URI */
2342
2343
2344 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdUpdateSLPBrowse() Start...");
2345
2346 /*
2347 * Reset the refresh time...
2348 */
2349
2350 BrowseSLPRefresh = time(NULL) + BrowseInterval;
2351
2352 /*
2353 * Poll for remote printers using SLP...
2354 */
2355
2356 s = NULL;
2357
2358 SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "",
2359 slp_url_callback, &s);
2360
2361 /*
2362 * Loop through the list of available printers...
2363 */
2364
2365 for (; s; s = next)
2366 {
2367 /*
2368 * Save the "next" pointer...
2369 */
2370
2371 next = s->next;
2372
2373 /*
2374 * Load a cupsd_printer_t structure with the SLP service attributes...
2375 */
2376
2377 SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
2378
2379 /*
2380 * Process this printer entry...
2381 */
2382
2383 uri = s->url + SLP_CUPS_SRVLEN + 1;
2384
2385 if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
2386 {
2387 /*
2388 * Pull the URI apart to see if this is a local or remote printer...
2389 */
2390
2391 httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method),
2392 username, sizeof(username), host, sizeof(host), &port,
2393 resource, sizeof(resource));
2394
2395 if (strcasecmp(host, ServerName) == 0)
2396 continue;
2397
2398 /*
2399 * OK, at least an IPP printer, see if it is a CUPS printer or
2400 * class...
2401 */
2402
2403 if (strstr(uri, "/printers/") != NULL)
2404 cupsdProcessBrowseData(uri, p.type, IPP_PRINTER_IDLE, p.location,
2405 p.info, p.make_model, 0, NULL);
2406 else if (strstr(uri, "/classes/") != NULL)
2407 cupsdProcessBrowseData(uri, p.type | CUPS_PRINTER_CLASS, IPP_PRINTER_IDLE,
2408 p.location, p.info, p.make_model, 0, NULL);
2409 }
2410
2411 /*
2412 * Free this listing...
2413 */
2414
2415 free(s);
2416 }
2417
2418 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdUpdateSLPBrowse() End...");
2419 }
2420
2421
2422 /*
2423 * 'slp_attr_callback()' - SLP attribute callback
2424 */
2425
2426 static SLPBoolean /* O - SLP_TRUE for success */
2427 slp_attr_callback(
2428 SLPHandle hslp, /* I - SLP handle */
2429 const char *attrlist, /* I - Attribute list */
2430 SLPError errcode, /* I - Parsing status for this attr */
2431 void *cookie) /* I - Current printer */
2432 {
2433 char *tmp = 0;
2434 cupsd_printer_t *p = (cupsd_printer_t*)cookie;
2435
2436
2437 /*
2438 * Let the compiler know we won't be using these...
2439 */
2440
2441 (void)hslp;
2442
2443 /*
2444 * Bail if there was an error
2445 */
2446
2447 if (errcode != SLP_OK)
2448 return (SLP_TRUE);
2449
2450 /*
2451 * Parse the attrlist to obtain things needed to build CUPS browse packet
2452 */
2453
2454 memset(p, 0, sizeof(cupsd_printer_t));
2455
2456 p->type = CUPS_PRINTER_REMOTE;
2457
2458 if (slp_get_attr(attrlist, "(printer-location=", &(p->location)))
2459 return (SLP_FALSE);
2460 if (slp_get_attr(attrlist, "(printer-info=", &(p->info)))
2461 return (SLP_FALSE);
2462 if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model)))
2463 return (SLP_FALSE);
2464
2465 if (slp_get_attr(attrlist, "(color-supported=", &tmp))
2466 return (SLP_FALSE);
2467 if (strcasecmp(tmp, "true") == 0)
2468 p->type |= CUPS_PRINTER_COLOR;
2469
2470 if (slp_get_attr(attrlist, "(finishings-supported=", &tmp))
2471 return (SLP_FALSE);
2472 if (strstr(tmp, "staple"))
2473 p->type |= CUPS_PRINTER_STAPLE;
2474 if (strstr(tmp, "bind"))
2475 p->type |= CUPS_PRINTER_BIND;
2476 if (strstr(tmp, "punch"))
2477 p->type |= CUPS_PRINTER_PUNCH;
2478
2479 if (slp_get_attr(attrlist, "(sides-supported=", &tmp))
2480 return (SLP_FALSE);
2481 if (strstr(tmp,"two-sided"))
2482 p->type |= CUPS_PRINTER_DUPLEX;
2483
2484 cupsdClearString(&tmp);
2485
2486 return (SLP_TRUE);
2487 }
2488
2489
2490 /*
2491 * 'slp_dereg_printer()' - SLPDereg() the specified printer
2492 */
2493
2494 static void
2495 slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */
2496 {
2497 char srvurl[HTTP_MAX_URI]; /* Printer service URI */
2498
2499
2500 cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
2501
2502 if (!(p->type & CUPS_PRINTER_REMOTE))
2503 {
2504 /*
2505 * Make the SLP service URL that conforms to the IANA
2506 * 'printer:' template.
2507 */
2508
2509 snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
2510
2511 /*
2512 * Deregister the printer...
2513 */
2514
2515 SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
2516 }
2517 }
2518
2519
2520 /*
2521 * 'slp_get_attr()' - Get an attribute from an SLP registration.
2522 */
2523
2524 static int /* O - 0 on success */
2525 slp_get_attr(const char *attrlist, /* I - Attribute list string */
2526 const char *tag, /* I - Name of attribute */
2527 char **valbuf) /* O - Value */
2528 {
2529 char *ptr1, /* Pointer into string */
2530 *ptr2; /* ... */
2531
2532
2533 cupsdClearString(valbuf);
2534
2535 if ((ptr1 = strstr(attrlist, tag)) != NULL)
2536 {
2537 ptr1 += strlen(tag);
2538
2539 if ((ptr2 = strchr(ptr1,')')) != NULL)
2540 {
2541 /*
2542 * Copy the value...
2543 */
2544
2545 *valbuf = calloc(ptr2 - ptr1 + 1, 1);
2546 strncpy(*valbuf, ptr1, ptr2 - ptr1);
2547
2548 /*
2549 * Dequote the value...
2550 */
2551
2552 for (ptr1 = *valbuf; *ptr1; ptr1 ++)
2553 if (*ptr1 == '\\' && ptr1[1])
2554 _cups_strcpy(ptr1, ptr1 + 1);
2555
2556 return (0);
2557 }
2558 }
2559
2560 return (-1);
2561 }
2562
2563
2564 /*
2565 * 'slp_reg_callback()' - Empty SLPRegReport.
2566 */
2567
2568 static void
2569 slp_reg_callback(SLPHandle hslp, /* I - SLP handle */
2570 SLPError errcode, /* I - Error code, if any */
2571 void *cookie) /* I - App data */
2572 {
2573 (void)hslp;
2574 (void)errcode;
2575 (void)cookie;
2576
2577 return;
2578 }
2579
2580
2581 /*
2582 * 'slp_url_callback()' - SLP service url callback
2583 */
2584
2585 static SLPBoolean /* O - TRUE = OK, FALSE = error */
2586 slp_url_callback(
2587 SLPHandle hslp, /* I - SLP handle */
2588 const char *srvurl, /* I - URL of service */
2589 unsigned short lifetime, /* I - Life of service */
2590 SLPError errcode, /* I - Existing error code */
2591 void *cookie) /* I - Pointer to service list */
2592 {
2593 slpsrvurl_t *s, /* New service entry */
2594 **head; /* Pointer to head of entry */
2595
2596
2597 /*
2598 * Let the compiler know we won't be using these vars...
2599 */
2600
2601 (void)hslp;
2602 (void)lifetime;
2603
2604 /*
2605 * Bail if there was an error
2606 */
2607
2608 if (errcode != SLP_OK)
2609 return (SLP_TRUE);
2610
2611 /*
2612 * Grab the head of the list...
2613 */
2614
2615 head = (slpsrvurl_t**)cookie;
2616
2617 /*
2618 * Allocate a *temporary* slpsrvurl_t to hold this entry.
2619 */
2620
2621 if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
2622 return (SLP_FALSE);
2623
2624 /*
2625 * Copy the SLP service URL...
2626 */
2627
2628 strlcpy(s->url, srvurl, sizeof(s->url));
2629
2630 /*
2631 * Link the SLP service URL into the head of the list
2632 */
2633
2634 if (*head)
2635 s->next = *head;
2636
2637 *head = s;
2638
2639 return (SLP_TRUE);
2640 }
2641 #endif /* HAVE_LIBSLP */
2642
2643
2644 /*
2645 * End of "$Id: dirsvc.c 5023 2006-01-29 14:39:44Z mike $".
2646 */