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