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