]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / scheduler / dirsvc.c
1 /*
2 * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $"
3 *
4 * Directory services routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
19 * remote printers.
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * cupsdStartBrowsing() - Start sending and receiving broadcast
27 * information.
28 * cupsdStartPolling() - Start polling servers as needed.
29 * cupsdStopBrowsing() - Stop sending and receiving broadcast
30 * information.
31 * cupsdStopPolling() - Stop polling servers as needed.
32 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
33 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
34 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
35 * dequote() - Remote quotes from a string.
36 * is_local_queue() - Determine whether the URI points at a local
37 * queue.
38 * process_browse_data() - Process new browse data.
39 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
40 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
41 * printer.
42 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
43 * record format.
44 * dnssdRegisterCallback() - DNSServiceRegister callback.
45 * dnssdRegisterPrinter() - Start sending broadcast information for a
46 * printer or update the broadcast contents.
47 * process_implicit_classes() - Create/update implicit classes as needed.
48 * send_cups_browse() - Send new browsing information using the CUPS
49 * protocol.
50 * send_ldap_browse() - Send LDAP printer registrations.
51 * send_slp_browse() - Register the specified printer with SLP.
52 * slp_attr_callback() - SLP attribute callback
53 * slp_dereg_printer() - SLPDereg() the specified printer
54 * slp_get_attr() - Get an attribute from an SLP registration.
55 * slp_reg_callback() - Empty SLPRegReport.
56 * slp_url_callback() - SLP service url callback
57 * update_cups_browse() - Update the browse lists using the CUPS
58 * protocol.
59 * update_lpd() - Update the LPD configuration as needed.
60 * update_polling() - Read status messages from the poll daemons.
61 * update_smb() - Update the SMB configuration as needed.
62 */
63
64 /*
65 * Include necessary headers...
66 */
67
68 #include "cupsd.h"
69 #include <grp.h>
70
71 #ifdef HAVE_DNSSD
72 # include <dns_sd.h>
73 # ifdef __APPLE__
74 # include <nameser.h>
75 # ifdef HAVE_COREFOUNDATION
76 # include <CoreFoundation/CoreFoundation.h>
77 # endif /* HAVE_COREFOUNDATION */
78 # ifdef HAVE_SYSTEMCONFIGURATION
79 # include <SystemConfiguration/SystemConfiguration.h>
80 # endif /* HAVE_SYSTEMCONFIGURATION */
81 # endif /* __APPLE__ */
82 #endif /* HAVE_DNSSD */
83
84
85 /*
86 * Local functions...
87 */
88
89 static char *dequote(char *d, const char *s, int dlen);
90 static int is_local_queue(const char *uri, char *host, int hostlen,
91 char *resource, int resourcelen);
92 static void process_browse_data(const char *uri, const char *host,
93 const char *resource, cups_ptype_t type,
94 ipp_pstate_t state, const char *location,
95 const char *info, const char *make_model,
96 int num_attrs, cups_option_t *attrs);
97 static void process_implicit_classes(int *write_printcap);
98 static void send_cups_browse(cupsd_printer_t *p);
99 #ifdef HAVE_LDAP
100 static void send_ldap_browse(cupsd_printer_t *p);
101 #endif /* HAVE_LDAP */
102 #ifdef HAVE_LIBSLP
103 static void send_slp_browse(cupsd_printer_t *p);
104 #endif /* HAVE_LIBSLP */
105 static void update_cups_browse(void);
106 static void update_lpd(int onoff);
107 static void update_polling(void);
108 static void update_smb(int onoff);
109
110
111 #ifdef HAVE_OPENLDAP
112 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
113 {
114 "printerDescription",
115 "printerLocation",
116 "printerMakeAndModel",
117 "printerType",
118 "printerURI",
119 NULL
120 };
121 #endif /* HAVE_OPENLDAP */
122
123 #ifdef HAVE_LIBSLP
124 /*
125 * SLP definitions...
126 */
127
128 /*
129 * SLP service name for CUPS...
130 */
131
132 # define SLP_CUPS_SRVTYPE "service:printer"
133 # define SLP_CUPS_SRVLEN 15
134
135
136 /*
137 * Printer service URL structure
138 */
139
140 typedef struct _slpsrvurl_s /**** SLP URL list ****/
141 {
142 struct _slpsrvurl_s *next; /* Next URL in list */
143 char url[HTTP_MAX_URI];
144 /* URL */
145 } slpsrvurl_t;
146
147
148 /*
149 * Local functions...
150 */
151
152 static SLPBoolean slp_attr_callback(SLPHandle hslp, const char *attrlist,
153 SLPError errcode, void *cookie);
154 static void slp_dereg_printer(cupsd_printer_t *p);
155 static int slp_get_attr(const char *attrlist, const char *tag,
156 char **valbuf);
157 static void slp_reg_callback(SLPHandle hslp, SLPError errcode,
158 void *cookie);
159 static SLPBoolean slp_url_callback(SLPHandle hslp, const char *srvurl,
160 unsigned short lifetime,
161 SLPError errcode, void *cookie);
162 #endif /* HAVE_LIBSLP */
163
164 #ifdef HAVE_DNSSD
165 /*
166 * For IPP register using a subtype of 'cups' so that shared printer browsing
167 * only finds other CUPS servers (not all IPP based printers).
168 */
169 static char dnssdIPPRegType[] = "_ipp._tcp,_cups";
170 static char dnssdIPPFaxRegType[] = "_fax-ipp._tcp";
171
172 static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p);
173 static void dnssdDeregisterPrinter(cupsd_printer_t *p);
174 static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
175 int count);
176 static void dnssdRegisterCallback(DNSServiceRef sdRef,
177 DNSServiceFlags flags,
178 DNSServiceErrorType errorCode,
179 const char *name, const char *regtype,
180 const char *domain, void *context);
181 static void dnssdRegisterPrinter(cupsd_printer_t *p);
182 #endif /* HAVE_DNSSD */
183
184
185 /*
186 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
187 * local printer and remove any pending
188 * references to remote printers.
189 */
190
191 void
192 cupsdDeregisterPrinter(
193 cupsd_printer_t *p, /* I - Printer to register */
194 int removeit) /* I - Printer being permanently removed */
195 {
196 /*
197 * Only deregister if browsing is enabled and it's a local printers...
198 */
199
200 if (!Browsing || !p->shared ||
201 (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
202 return;
203
204 /*
205 * Announce the deletion...
206 */
207
208 if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
209 {
210 cups_ptype_t savedtype = p->type; /* Saved printer type */
211
212 p->type |= CUPS_PRINTER_DELETE;
213
214 send_cups_browse(p);
215
216 p->type = savedtype;
217 }
218
219 #ifdef HAVE_LIBSLP
220 if (BrowseLocalProtocols & BROWSE_SLP)
221 slp_dereg_printer(p);
222 #endif /* HAVE_LIBSLP */
223
224 #ifdef HAVE_DNSSD
225 if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD))
226 dnssdDeregisterPrinter(p);
227 #endif /* HAVE_DNSSD */
228 }
229
230
231 /*
232 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
233 */
234
235 void
236 cupsdLoadRemoteCache(void)
237 {
238 cups_file_t *fp; /* remote.cache file */
239 int linenum; /* Current line number */
240 char line[1024], /* Line from file */
241 *value, /* Pointer to value */
242 *valueptr, /* Pointer into value */
243 scheme[32], /* Scheme portion of URI */
244 username[64], /* Username portion of URI */
245 host[HTTP_MAX_HOST],
246 /* Hostname portion of URI */
247 resource[HTTP_MAX_URI];
248 /* Resource portion of URI */
249 int port; /* Port number */
250 cupsd_printer_t *p; /* Current printer */
251 time_t now; /* Current time */
252
253
254 /*
255 * Don't load the cache if the CUPS remote protocol is disabled...
256 */
257
258 if (!Browsing || !(BrowseRemoteProtocols & BROWSE_CUPS))
259 {
260 cupsdLogMessage(CUPSD_LOG_DEBUG,
261 "cupsdLoadRemoteCache: Not loading remote cache.");
262 return;
263 }
264
265 /*
266 * Open the remote.cache file...
267 */
268
269 snprintf(line, sizeof(line), "%s/remote.cache", CacheDir);
270 if ((fp = cupsFileOpen(line, "r")) == NULL)
271 return;
272
273 /*
274 * Read printer configurations until we hit EOF...
275 */
276
277 linenum = 0;
278 p = NULL;
279 now = time(NULL);
280
281 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
282 {
283 /*
284 * Decode the directive...
285 */
286
287 if (!strcasecmp(line, "<Printer") ||
288 !strcasecmp(line, "<DefaultPrinter"))
289 {
290 /*
291 * <Printer name> or <DefaultPrinter name>
292 */
293
294 if (p == NULL && value)
295 {
296 /*
297 * Add the printer and a base file type...
298 */
299
300 cupsdLogMessage(CUPSD_LOG_DEBUG,
301 "cupsdLoadRemoteCache: Loading printer %s...", value);
302
303 if ((p = cupsdFindDest(value)) != NULL)
304 {
305 if (p->type & CUPS_PRINTER_CLASS)
306 {
307 cupsdLogMessage(CUPSD_LOG_WARN,
308 "Cached remote printer \"%s\" conflicts with "
309 "existing class!",
310 value);
311 p = NULL;
312 continue;
313 }
314 }
315 else
316 p = cupsdAddPrinter(value);
317
318 p->accepting = 1;
319 p->state = IPP_PRINTER_IDLE;
320 p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
321 p->browse_time = now;
322 p->browse_expire = now + BrowseTimeout;
323
324 /*
325 * Set the default printer as needed...
326 */
327
328 if (!strcasecmp(line, "<DefaultPrinter"))
329 DefaultPrinter = p;
330 }
331 else
332 {
333 cupsdLogMessage(CUPSD_LOG_ERROR,
334 "Syntax error on line %d of remote.cache.", linenum);
335 break;
336 }
337 }
338 else if (!strcasecmp(line, "<Class") ||
339 !strcasecmp(line, "<DefaultClass"))
340 {
341 /*
342 * <Class name> or <DefaultClass name>
343 */
344
345 if (p == NULL && value)
346 {
347 /*
348 * Add the printer and a base file type...
349 */
350
351 cupsdLogMessage(CUPSD_LOG_DEBUG,
352 "cupsdLoadRemoteCache: Loading class %s...", value);
353
354 if ((p = cupsdFindDest(value)) != NULL)
355 p->type = CUPS_PRINTER_CLASS;
356 else
357 p = cupsdAddClass(value);
358
359 p->accepting = 1;
360 p->state = IPP_PRINTER_IDLE;
361 p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
362 p->browse_time = now;
363 p->browse_expire = now + BrowseTimeout;
364
365 /*
366 * Set the default printer as needed...
367 */
368
369 if (!strcasecmp(line, "<DefaultClass"))
370 DefaultPrinter = p;
371 }
372 else
373 {
374 cupsdLogMessage(CUPSD_LOG_ERROR,
375 "Syntax error on line %d of remote.cache.", linenum);
376 break;
377 }
378 }
379 else if (!strcasecmp(line, "</Printer>") ||
380 !strcasecmp(line, "</Class>"))
381 {
382 if (p != NULL)
383 {
384 /*
385 * Close out the current printer...
386 */
387
388 cupsdSetPrinterAttrs(p);
389
390 p = NULL;
391 }
392 else
393 {
394 cupsdLogMessage(CUPSD_LOG_ERROR,
395 "Syntax error on line %d of remote.cache.", linenum);
396 break;
397 }
398 }
399 else if (!p)
400 {
401 cupsdLogMessage(CUPSD_LOG_ERROR,
402 "Syntax error on line %d of remote.cache.", linenum);
403 break;
404 }
405 else if (!strcasecmp(line, "Info"))
406 {
407 if (value)
408 cupsdSetString(&p->info, value);
409 }
410 else if (!strcasecmp(line, "MakeModel"))
411 {
412 if (value)
413 cupsdSetString(&p->make_model, value);
414 }
415 else if (!strcasecmp(line, "Location"))
416 {
417 if (value)
418 cupsdSetString(&p->location, value);
419 }
420 else if (!strcasecmp(line, "DeviceURI"))
421 {
422 if (value)
423 {
424 httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme),
425 username, sizeof(username), host, sizeof(host), &port,
426 resource, sizeof(resource));
427
428 cupsdSetString(&p->hostname, host);
429 cupsdSetString(&p->uri, value);
430 cupsdSetString(&p->device_uri, value);
431 }
432 else
433 {
434 cupsdLogMessage(CUPSD_LOG_ERROR,
435 "Syntax error on line %d of remote.cache.", linenum);
436 break;
437 }
438 }
439 else if (!strcasecmp(line, "Option") && value)
440 {
441 /*
442 * Option name value
443 */
444
445 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
446
447 if (!*valueptr)
448 cupsdLogMessage(CUPSD_LOG_ERROR,
449 "Syntax error on line %d of remote.cache.", linenum);
450 else
451 {
452 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
453
454 p->num_options = cupsAddOption(value, valueptr, p->num_options,
455 &(p->options));
456 }
457 }
458 else if (!strcasecmp(line, "State"))
459 {
460 /*
461 * Set the initial queue state...
462 */
463
464 if (value && !strcasecmp(value, "idle"))
465 p->state = IPP_PRINTER_IDLE;
466 else if (value && !strcasecmp(value, "stopped"))
467 p->state = IPP_PRINTER_STOPPED;
468 else
469 {
470 cupsdLogMessage(CUPSD_LOG_ERROR,
471 "Syntax error on line %d of remote.cache.", linenum);
472 break;
473 }
474 }
475 else if (!strcasecmp(line, "StateMessage"))
476 {
477 /*
478 * Set the initial queue state message...
479 */
480
481 if (value)
482 strlcpy(p->state_message, value, sizeof(p->state_message));
483 }
484 else if (!strcasecmp(line, "Accepting"))
485 {
486 /*
487 * Set the initial accepting state...
488 */
489
490 if (value &&
491 (!strcasecmp(value, "yes") ||
492 !strcasecmp(value, "on") ||
493 !strcasecmp(value, "true")))
494 p->accepting = 1;
495 else if (value &&
496 (!strcasecmp(value, "no") ||
497 !strcasecmp(value, "off") ||
498 !strcasecmp(value, "false")))
499 p->accepting = 0;
500 else
501 {
502 cupsdLogMessage(CUPSD_LOG_ERROR,
503 "Syntax error on line %d of remote.cache.", linenum);
504 break;
505 }
506 }
507 else if (!strcasecmp(line, "Type"))
508 {
509 if (value)
510 p->type = atoi(value);
511 else
512 {
513 cupsdLogMessage(CUPSD_LOG_ERROR,
514 "Syntax error on line %d of remote.cache.", linenum);
515 break;
516 }
517 }
518 else if (!strcasecmp(line, "BrowseTime"))
519 {
520 if (value)
521 {
522 time_t t = atoi(value);
523
524 if (t > p->browse_expire)
525 p->browse_expire = t;
526 }
527 else
528 {
529 cupsdLogMessage(CUPSD_LOG_ERROR,
530 "Syntax error on line %d of remote.cache.", linenum);
531 break;
532 }
533 }
534 else if (!strcasecmp(line, "JobSheets"))
535 {
536 /*
537 * Set the initial job sheets...
538 */
539
540 if (value)
541 {
542 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
543
544 if (*valueptr)
545 *valueptr++ = '\0';
546
547 cupsdSetString(&p->job_sheets[0], value);
548
549 while (isspace(*valueptr & 255))
550 valueptr ++;
551
552 if (*valueptr)
553 {
554 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
555
556 if (*valueptr)
557 *valueptr++ = '\0';
558
559 cupsdSetString(&p->job_sheets[1], value);
560 }
561 }
562 else
563 {
564 cupsdLogMessage(CUPSD_LOG_ERROR,
565 "Syntax error on line %d of remote.cache.", linenum);
566 break;
567 }
568 }
569 else if (!strcasecmp(line, "AllowUser"))
570 {
571 if (value)
572 {
573 p->deny_users = 0;
574 cupsdAddPrinterUser(p, value);
575 }
576 else
577 {
578 cupsdLogMessage(CUPSD_LOG_ERROR,
579 "Syntax error on line %d of remote.cache.", linenum);
580 break;
581 }
582 }
583 else if (!strcasecmp(line, "DenyUser"))
584 {
585 if (value)
586 {
587 p->deny_users = 1;
588 cupsdAddPrinterUser(p, value);
589 }
590 else
591 {
592 cupsdLogMessage(CUPSD_LOG_ERROR,
593 "Syntax error on line %d of remote.cache.", linenum);
594 break;
595 }
596 }
597 else
598 {
599 /*
600 * Something else we don't understand...
601 */
602
603 cupsdLogMessage(CUPSD_LOG_ERROR,
604 "Unknown configuration directive %s on line %d of remote.cache.",
605 line, linenum);
606 }
607 }
608
609 cupsFileClose(fp);
610
611 /*
612 * Do auto-classing if needed...
613 */
614
615 process_implicit_classes(NULL);
616 }
617
618
619 /*
620 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
621 * printer or update the broadcast contents.
622 */
623
624 void
625 cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
626 {
627 if (!Browsing || !BrowseLocalProtocols || !BrowseInterval || !NumBrowsers ||
628 (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
629 return;
630
631 #ifdef HAVE_LIBSLP
632 /* if (BrowseLocalProtocols & BROWSE_SLP)
633 slpRegisterPrinter(p); */
634 #endif /* HAVE_LIBSLP */
635
636 #ifdef HAVE_DNSSD
637 if (BrowseLocalProtocols & BROWSE_DNSSD)
638 dnssdRegisterPrinter(p);
639 #endif /* HAVE_DNSSD */
640 }
641
642
643 /*
644 * 'cupsdRestartPolling()' - Restart polling servers as needed.
645 */
646
647 void
648 cupsdRestartPolling(void)
649 {
650 int i; /* Looping var */
651 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
652
653
654 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
655 if (pollp->pid)
656 kill(pollp->pid, SIGHUP);
657 }
658
659
660 /*
661 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
662 */
663
664 void
665 cupsdSaveRemoteCache(void)
666 {
667 int i; /* Looping var */
668 cups_file_t *fp; /* printers.conf file */
669 char temp[1024]; /* Temporary string */
670 cupsd_printer_t *printer; /* Current printer class */
671 time_t curtime; /* Current time */
672 struct tm *curdate; /* Current date */
673 cups_option_t *option; /* Current option */
674
675
676 /*
677 * Create the remote.cache file...
678 */
679
680 snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir);
681
682 if ((fp = cupsFileOpen(temp, "w")) == NULL)
683 {
684 cupsdLogMessage(CUPSD_LOG_ERROR,
685 "Unable to save remote.cache - %s", strerror(errno));
686 return;
687 }
688 else
689 cupsdLogMessage(CUPSD_LOG_DEBUG, "Saving remote.cache...");
690
691 /*
692 * Restrict access to the file...
693 */
694
695 fchown(cupsFileNumber(fp), getuid(), Group);
696 fchmod(cupsFileNumber(fp), ConfigFilePerm);
697
698 /*
699 * Write a small header to the file...
700 */
701
702 curtime = time(NULL);
703 curdate = localtime(&curtime);
704 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
705
706 cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n");
707 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
708
709 /*
710 * Write each local printer known to the system...
711 */
712
713 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
714 printer;
715 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
716 {
717 /*
718 * Skip local destinations...
719 */
720
721 if (!(printer->type & CUPS_PRINTER_DISCOVERED))
722 continue;
723
724 /*
725 * Write printers as needed...
726 */
727
728 if (printer == DefaultPrinter)
729 cupsFilePuts(fp, "<Default");
730 else
731 cupsFilePutChar(fp, '<');
732
733 if (printer->type & CUPS_PRINTER_CLASS)
734 cupsFilePrintf(fp, "Class %s>\n", printer->name);
735 else
736 cupsFilePrintf(fp, "Printer %s>\n", printer->name);
737
738 cupsFilePrintf(fp, "Type %d\n", printer->type);
739
740 cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire);
741
742 if (printer->info)
743 cupsFilePrintf(fp, "Info %s\n", printer->info);
744
745 if (printer->make_model)
746 cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model);
747
748 if (printer->location)
749 cupsFilePrintf(fp, "Location %s\n", printer->location);
750
751 if (printer->device_uri)
752 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
753
754 if (printer->state == IPP_PRINTER_STOPPED)
755 {
756 cupsFilePuts(fp, "State Stopped\n");
757 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
758 }
759 else
760 cupsFilePuts(fp, "State Idle\n");
761
762 if (printer->accepting)
763 cupsFilePuts(fp, "Accepting Yes\n");
764 else
765 cupsFilePuts(fp, "Accepting No\n");
766
767 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
768 printer->job_sheets[1]);
769
770 for (i = 0; i < printer->num_users; i ++)
771 cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
772 printer->users[i]);
773
774 for (i = printer->num_options, option = printer->options;
775 i > 0;
776 i --, option ++)
777 cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
778
779 if (printer->type & CUPS_PRINTER_CLASS)
780 cupsFilePuts(fp, "</Class>\n");
781 else
782 cupsFilePuts(fp, "</Printer>\n");
783 }
784
785 cupsFileClose(fp);
786 }
787
788
789 /*
790 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
791 */
792
793 void
794 cupsdSendBrowseList(void)
795 {
796 int count; /* Number of dests to update */
797 cupsd_printer_t *p; /* Current printer */
798 time_t ut, /* Minimum update time */
799 to; /* Timeout time */
800 int write_printcap; /* Write the printcap file? */
801
802
803 if (!Browsing || !BrowseLocalProtocols || !Printers)
804 return;
805
806 /*
807 * Compute the update and timeout times...
808 */
809
810 to = time(NULL);
811 ut = to - BrowseInterval;
812
813 /*
814 * Figure out how many printers need an update...
815 */
816
817 if (BrowseInterval > 0)
818 {
819 int max_count; /* Maximum number to update */
820
821
822 /*
823 * Throttle the number of printers we'll be updating this time
824 * around based on the number of queues that need updating and
825 * the maximum number of queues to update each second...
826 */
827
828 max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1;
829
830 for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers);
831 count < max_count && p != NULL;
832 p = (cupsd_printer_t *)cupsArrayNext(Printers))
833 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
834 p->shared && p->browse_time < ut)
835 count ++;
836
837 /*
838 * Loop through all of the printers and send local updates as needed...
839 */
840
841 if (BrowseNext)
842 p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext);
843 else
844 p = (cupsd_printer_t *)cupsArrayFirst(Printers);
845
846 for (;
847 count > 0;
848 p = (cupsd_printer_t *)cupsArrayNext(Printers))
849 {
850 /*
851 * Check for wraparound...
852 */
853
854 if (!p)
855 p = (cupsd_printer_t *)cupsArrayFirst(Printers);
856
857 if (!p)
858 break;
859 else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
860 !p->shared)
861 continue;
862 else if (p->browse_time < ut)
863 {
864 /*
865 * Need to send an update...
866 */
867
868 count --;
869
870 p->browse_time = time(NULL);
871
872 if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
873 send_cups_browse(p);
874
875 #ifdef HAVE_LIBSLP
876 if (BrowseLocalProtocols & BROWSE_SLP)
877 send_slp_browse(p);
878 #endif /* HAVE_LIBSLP */
879
880 #ifdef HAVE_LDAP
881 if (BrowseLocalProtocols & BROWSE_LDAP)
882 send_ldap_browse(p);
883 #endif /* HAVE_LDAP */
884 }
885 }
886
887 /*
888 * Save where we left off so that all printers get updated...
889 */
890
891 BrowseNext = p;
892 }
893
894 /*
895 * Loop through all of the printers and send local updates as needed...
896 */
897
898 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), write_printcap = 0;
899 p;
900 p = (cupsd_printer_t *)cupsArrayNext(Printers))
901 {
902 /*
903 * If this is a remote queue, see if it needs to be timed out...
904 */
905
906 if (p->type & CUPS_PRINTER_DISCOVERED)
907 {
908 if (p->browse_expire < to)
909 {
910 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
911 "%s \'%s\' deleted by directory services (timeout).",
912 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
913 p->name);
914
915 cupsdLogMessage(CUPSD_LOG_DEBUG,
916 "Remote destination \"%s\" has timed out; "
917 "deleting it...",
918 p->name);
919
920 cupsArraySave(Printers);
921 cupsdDeletePrinter(p, 1);
922 cupsArrayRestore(Printers);
923 write_printcap = 1;
924 }
925 }
926 }
927
928 if (write_printcap)
929 cupsdWritePrintcap();
930 }
931
932
933 /*
934 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
935 */
936
937 void
938 cupsdStartBrowsing(void)
939 {
940 int val; /* Socket option value */
941 struct sockaddr_in addr; /* Broadcast address */
942 cupsd_printer_t *p; /* Current printer */
943
944
945 BrowseNext = NULL;
946
947 if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
948 return;
949
950 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
951 {
952 if (BrowseSocket < 0)
953 {
954 /*
955 * Create the broadcast socket...
956 */
957
958 if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
959 {
960 cupsdLogMessage(CUPSD_LOG_ERROR,
961 "Unable to create broadcast socket - %s.",
962 strerror(errno));
963 BrowseLocalProtocols &= ~BROWSE_CUPS;
964 BrowseRemoteProtocols &= ~BROWSE_CUPS;
965 return;
966 }
967
968 /*
969 * Bind the socket to browse port...
970 */
971
972 memset(&addr, 0, sizeof(addr));
973 addr.sin_addr.s_addr = htonl(INADDR_ANY);
974 addr.sin_family = AF_INET;
975 addr.sin_port = htons(BrowsePort);
976
977 if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
978 {
979 cupsdLogMessage(CUPSD_LOG_ERROR,
980 "Unable to bind broadcast socket - %s.",
981 strerror(errno));
982
983 #ifdef WIN32
984 closesocket(BrowseSocket);
985 #else
986 close(BrowseSocket);
987 #endif /* WIN32 */
988
989 BrowseSocket = -1;
990 BrowseLocalProtocols &= ~BROWSE_CUPS;
991 BrowseRemoteProtocols &= ~BROWSE_CUPS;
992 return;
993 }
994 }
995
996 /*
997 * Set the "broadcast" flag...
998 */
999
1000 val = 1;
1001 if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
1002 {
1003 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.",
1004 strerror(errno));
1005
1006 #ifdef WIN32
1007 closesocket(BrowseSocket);
1008 #else
1009 close(BrowseSocket);
1010 #endif /* WIN32 */
1011
1012 BrowseSocket = -1;
1013 BrowseLocalProtocols &= ~BROWSE_CUPS;
1014 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1015 return;
1016 }
1017
1018 /*
1019 * Close the socket on exec...
1020 */
1021
1022 fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
1023
1024 /*
1025 * Finally, add the socket to the input selection set as needed...
1026 */
1027
1028 if (BrowseRemoteProtocols & BROWSE_CUPS)
1029 {
1030 /*
1031 * We only listen if we want remote printers...
1032 */
1033
1034 cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse,
1035 NULL, NULL);
1036 }
1037 }
1038 else
1039 BrowseSocket = -1;
1040
1041 #ifdef HAVE_LIBSLP
1042 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
1043 {
1044 /*
1045 * Open SLP handle...
1046 */
1047
1048 if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
1049 {
1050 cupsdLogMessage(CUPSD_LOG_ERROR,
1051 "Unable to open an SLP handle; disabling SLP browsing!");
1052 BrowseLocalProtocols &= ~BROWSE_SLP;
1053 BrowseRemoteProtocols &= ~BROWSE_SLP;
1054 }
1055
1056 BrowseSLPRefresh = 0;
1057 }
1058 else
1059 BrowseSLPHandle = NULL;
1060 #endif /* HAVE_LIBSLP */
1061
1062 #ifdef HAVE_OPENLDAP
1063 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP)
1064 {
1065 if (!BrowseLDAPDN)
1066 {
1067 cupsdLogMessage(CUPSD_LOG_ERROR,
1068 "Need to set BrowseLDAPDN to use LDAP browsing!");
1069 BrowseLocalProtocols &= ~BROWSE_LDAP;
1070 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1071 }
1072 else
1073 {
1074 /*
1075 * Open LDAP handle...
1076 */
1077
1078 int rc; /* LDAP API status */
1079 int version = 3; /* LDAP version */
1080 struct berval bv = {0, ""}; /* SASL bind value */
1081
1082
1083 /*
1084 * Set the certificate file to use for encrypted LDAP sessions...
1085 */
1086
1087 if (BrowseLDAPCACertFile)
1088 {
1089 cupsdLogMessage(CUPSD_LOG_DEBUG,
1090 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1091 BrowseLDAPCACertFile);
1092
1093 if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
1094 (void *)BrowseLDAPCACertFile))
1095 != LDAP_SUCCESS)
1096 cupsdLogMessage(CUPSD_LOG_ERROR,
1097 "Unable to set CA certificate file for LDAP "
1098 "connections: %d - %s", rc, ldap_err2string(rc));
1099 }
1100
1101 /*
1102 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1103 */
1104
1105 if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
1106 rc = ldap_initialize(&BrowseLDAPHandle, "ldapi:///");
1107 else
1108 rc = ldap_initialize(&BrowseLDAPHandle, BrowseLDAPServer);
1109
1110 if (rc != LDAP_SUCCESS)
1111 {
1112 cupsdLogMessage(CUPSD_LOG_ERROR,
1113 "Unable to initialize LDAP; disabling LDAP browsing!");
1114 BrowseLocalProtocols &= ~BROWSE_LDAP;
1115 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1116 }
1117 else if (ldap_set_option(BrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION,
1118 (const void *)&version) != LDAP_SUCCESS)
1119 {
1120 ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL);
1121 BrowseLDAPHandle = NULL;
1122 cupsdLogMessage(CUPSD_LOG_ERROR,
1123 "Unable to set LDAP protocol version; "
1124 "disabling LDAP browsing!");
1125 BrowseLocalProtocols &= ~BROWSE_LDAP;
1126 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1127 }
1128 else
1129 {
1130 if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
1131 rc = ldap_sasl_bind_s(BrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL,
1132 NULL, NULL);
1133 else
1134 rc = ldap_bind_s(BrowseLDAPHandle, BrowseLDAPBindDN,
1135 BrowseLDAPPassword, LDAP_AUTH_SIMPLE);
1136
1137 if (rc != LDAP_SUCCESS)
1138 {
1139 cupsdLogMessage(CUPSD_LOG_ERROR,
1140 "Unable to bind to LDAP server; "
1141 "disabling LDAP browsing!");
1142 ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL);
1143 BrowseLocalProtocols &= ~BROWSE_LDAP;
1144 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1145 }
1146 }
1147 }
1148
1149 BrowseLDAPRefresh = 0;
1150 }
1151 #endif /* HAVE_OPENLDAP */
1152
1153 /*
1154 * Enable LPD and SMB printer sharing as needed through external programs...
1155 */
1156
1157 if (BrowseLocalProtocols & BROWSE_LPD)
1158 update_lpd(1);
1159
1160 if (BrowseLocalProtocols & BROWSE_SMB)
1161 update_smb(1);
1162
1163 /*
1164 * Register the individual printers
1165 */
1166
1167 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1168 p;
1169 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1170 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
1171 cupsdRegisterPrinter(p);
1172 }
1173
1174
1175 /*
1176 * 'cupsdStartPolling()' - Start polling servers as needed.
1177 */
1178
1179 void
1180 cupsdStartPolling(void)
1181 {
1182 int i; /* Looping var */
1183 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
1184 char polld[1024]; /* Poll daemon path */
1185 char sport[255]; /* Server port */
1186 char bport[255]; /* Browser port */
1187 char interval[255]; /* Poll interval */
1188 int statusfds[2]; /* Status pipe */
1189 char *argv[6]; /* Arguments */
1190 char *envp[100]; /* Environment */
1191
1192
1193 /*
1194 * Don't do anything if we aren't polling...
1195 */
1196
1197 if (NumPolled == 0 || BrowseSocket < 0)
1198 {
1199 PollPipe = -1;
1200 PollStatusBuffer = NULL;
1201 return;
1202 }
1203
1204 /*
1205 * Setup string arguments for polld, port and interval options.
1206 */
1207
1208 snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
1209
1210 sprintf(bport, "%d", BrowsePort);
1211
1212 if (BrowseInterval)
1213 sprintf(interval, "%d", BrowseInterval);
1214 else
1215 strcpy(interval, "30");
1216
1217 argv[0] = "cups-polld";
1218 argv[2] = sport;
1219 argv[3] = interval;
1220 argv[4] = bport;
1221 argv[5] = NULL;
1222
1223 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
1224
1225 /*
1226 * Create a pipe that receives the status messages from each
1227 * polling daemon...
1228 */
1229
1230 if (cupsdOpenPipe(statusfds))
1231 {
1232 cupsdLogMessage(CUPSD_LOG_ERROR,
1233 "Unable to create polling status pipes - %s.",
1234 strerror(errno));
1235 PollPipe = -1;
1236 PollStatusBuffer = NULL;
1237 return;
1238 }
1239
1240 PollPipe = statusfds[0];
1241 PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]");
1242
1243 /*
1244 * Run each polling daemon, redirecting stderr to the polling pipe...
1245 */
1246
1247 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
1248 {
1249 sprintf(sport, "%d", pollp->port);
1250
1251 argv[1] = pollp->hostname;
1252
1253 if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1,
1254 0, DefaultProfile, &(pollp->pid)) < 0)
1255 {
1256 cupsdLogMessage(CUPSD_LOG_ERROR,
1257 "cupsdStartPolling: Unable to fork polling daemon - %s",
1258 strerror(errno));
1259 pollp->pid = 0;
1260 break;
1261 }
1262 else
1263 cupsdLogMessage(CUPSD_LOG_DEBUG,
1264 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1265 pollp->hostname, pollp->port, pollp->pid);
1266 }
1267
1268 close(statusfds[1]);
1269
1270 /*
1271 * Finally, add the pipe to the input selection set...
1272 */
1273
1274 cupsdAddSelect(PollPipe, (cupsd_selfunc_t)update_polling, NULL, NULL);
1275 }
1276
1277
1278 /*
1279 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1280 */
1281
1282 void
1283 cupsdStopBrowsing(void)
1284 {
1285 cupsd_printer_t *p; /* Current printer */
1286
1287
1288 if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
1289 return;
1290
1291 /*
1292 * De-register the individual printers
1293 */
1294
1295 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1296 p;
1297 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1298 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
1299 cupsdDeregisterPrinter(p, 1);
1300
1301 /*
1302 * Shut down browsing sockets...
1303 */
1304
1305 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) &&
1306 BrowseSocket >= 0)
1307 {
1308 /*
1309 * Close the socket and remove it from the input selection set.
1310 */
1311
1312 #ifdef WIN32
1313 closesocket(BrowseSocket);
1314 #else
1315 close(BrowseSocket);
1316 #endif /* WIN32 */
1317
1318 cupsdRemoveSelect(BrowseSocket);
1319 BrowseSocket = -1;
1320 }
1321
1322 #ifdef HAVE_LIBSLP
1323 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
1324 BrowseSLPHandle)
1325 {
1326 /*
1327 * Close SLP handle...
1328 */
1329
1330 SLPClose(BrowseSLPHandle);
1331 BrowseSLPHandle = NULL;
1332 }
1333 #endif /* HAVE_LIBSLP */
1334
1335 #ifdef HAVE_OPENLDAP
1336 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) &&
1337 BrowseLDAPHandle)
1338 {
1339 ldap_unbind(BrowseLDAPHandle);
1340 BrowseLDAPHandle = NULL;
1341 }
1342 #endif /* HAVE_OPENLDAP */
1343
1344 /*
1345 * Disable LPD and SMB printer sharing as needed through external programs...
1346 */
1347
1348 if (BrowseLocalProtocols & BROWSE_LPD)
1349 update_lpd(0);
1350
1351 if (BrowseLocalProtocols & BROWSE_SMB)
1352 update_smb(0);
1353 }
1354
1355
1356 /*
1357 * 'cupsdStopPolling()' - Stop polling servers as needed.
1358 */
1359
1360 void
1361 cupsdStopPolling(void)
1362 {
1363 int i; /* Looping var */
1364 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
1365
1366
1367 if (PollPipe >= 0)
1368 {
1369 cupsdStatBufDelete(PollStatusBuffer);
1370 close(PollPipe);
1371
1372 cupsdRemoveSelect(PollPipe);
1373
1374 PollPipe = -1;
1375 PollStatusBuffer = NULL;
1376 }
1377
1378 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
1379 if (pollp->pid)
1380 cupsdEndProcess(pollp->pid, 0);
1381 }
1382
1383
1384 #ifdef HAVE_DNSSD
1385 /*
1386 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1387 */
1388
1389 void
1390 cupsdUpdateDNSSDBrowse(
1391 cupsd_printer_t *p) /* I - Printer being queried */
1392 {
1393 DNSServiceErrorType sdErr; /* Service discovery error */
1394
1395
1396 if ((sdErr = DNSServiceProcessResult(p->dnssd_ipp_ref))
1397 != kDNSServiceErr_NoError)
1398 {
1399 cupsdLogMessage(CUPSD_LOG_ERROR,
1400 "DNS Service Discovery registration error %d for \"%s\"!",
1401 sdErr, p->name);
1402 cupsdRemoveSelect(p->dnssd_ipp_fd);
1403 DNSServiceRefDeallocate(p->dnssd_ipp_ref);
1404
1405 p->dnssd_ipp_ref = NULL;
1406 p->dnssd_ipp_fd = -1;
1407 }
1408 }
1409 #endif /* HAVE_DNSSD */
1410
1411
1412 #ifdef HAVE_OPENLDAP
1413 /*
1414 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1415 */
1416
1417 void
1418 cupsdUpdateLDAPBrowse(void)
1419 {
1420 char uri[HTTP_MAX_URI], /* Printer URI */
1421 host[HTTP_MAX_URI], /* Hostname */
1422 resource[HTTP_MAX_URI], /* Resource path */
1423 location[1024], /* Printer location */
1424 info[1024], /* Printer information */
1425 make_model[1024], /* Printer make and model */
1426 **value; /* Holds the returned data from LDAP */
1427 int type; /* Printer type */
1428 int rc; /* LDAP status */
1429 int limit; /* Size limit */
1430 LDAPMessage *res, /* LDAP search results */
1431 *e; /* Current entry from search */
1432
1433
1434 /*
1435 * Search for printers...
1436 */
1437
1438 cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName);
1439
1440 BrowseLDAPRefresh = time(NULL) + BrowseInterval;
1441
1442 rc = ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
1443 "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res);
1444 if (rc != LDAP_SUCCESS)
1445 {
1446 cupsdLogMessage(CUPSD_LOG_ERROR,
1447 "LDAP search returned error %d: %s", rc,
1448 ldap_err2string(rc));
1449 return;
1450 }
1451
1452 limit = ldap_count_entries(BrowseLDAPHandle, res);
1453 cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit);
1454 if (limit < 1)
1455 return;
1456
1457 /*
1458 * Loop through the available printers...
1459 */
1460
1461 for (e = ldap_first_entry(BrowseLDAPHandle, res);
1462 e;
1463 e = ldap_next_entry(BrowseLDAPHandle, e))
1464 {
1465 /*
1466 * Get the required values from this entry...
1467 */
1468
1469 if ((value = ldap_get_values(BrowseLDAPHandle, e,
1470 "printerDescription")) == NULL)
1471 continue;
1472
1473 strlcpy(info, *value, sizeof(info));
1474 ldap_value_free(value);
1475
1476 if ((value = ldap_get_values(BrowseLDAPHandle, e,
1477 "printerLocation")) == NULL)
1478 continue;
1479
1480 strlcpy(location, *value, sizeof(location));
1481 ldap_value_free(value);
1482
1483 if ((value = ldap_get_values(BrowseLDAPHandle, e,
1484 "printerMakeAndModel")) == NULL)
1485 continue;
1486
1487 strlcpy(make_model, *value, sizeof(make_model));
1488 ldap_value_free(value);
1489
1490 if ((value = ldap_get_values(BrowseLDAPHandle, e,
1491 "printerType")) == NULL)
1492 continue;
1493
1494 type = atoi(*value);
1495 ldap_value_free(value);
1496
1497 if ((value = ldap_get_values(BrowseLDAPHandle, e,
1498 "printerURI")) == NULL)
1499 continue;
1500
1501 strlcpy(uri, *value, sizeof(uri));
1502 ldap_value_free(value);
1503
1504 /*
1505 * Process the entry as browse data...
1506 */
1507
1508 if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
1509 process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE,
1510 location, info, make_model, 0, NULL);
1511
1512 }
1513 }
1514 #endif /* HAVE_OPENLDAP */
1515
1516
1517 #ifdef HAVE_LIBSLP
1518 /*
1519 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1520 */
1521
1522 void
1523 cupsdUpdateSLPBrowse(void)
1524 {
1525 slpsrvurl_t *s, /* Temporary list of service URLs */
1526 *next; /* Next service in list */
1527 cupsd_printer_t p; /* Printer information */
1528 const char *uri; /* Pointer to printer URI */
1529 char host[HTTP_MAX_URI], /* Host portion of URI */
1530 resource[HTTP_MAX_URI]; /* Resource portion of URI */
1531
1532
1533 /*
1534 * Reset the refresh time...
1535 */
1536
1537 BrowseSLPRefresh = time(NULL) + BrowseInterval;
1538
1539 /*
1540 * Poll for remote printers using SLP...
1541 */
1542
1543 s = NULL;
1544
1545 SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "",
1546 slp_url_callback, &s);
1547
1548 /*
1549 * Loop through the list of available printers...
1550 */
1551
1552 for (; s; s = next)
1553 {
1554 /*
1555 * Save the "next" pointer...
1556 */
1557
1558 next = s->next;
1559
1560 /*
1561 * Load a cupsd_printer_t structure with the SLP service attributes...
1562 */
1563
1564 SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
1565
1566 /*
1567 * Process this printer entry...
1568 */
1569
1570 uri = s->url + SLP_CUPS_SRVLEN + 1;
1571
1572 if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
1573 {
1574 /*
1575 * Pull the URI apart to see if this is a local or remote printer...
1576 */
1577
1578 if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
1579 process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE,
1580 p.location, p.info, p.make_model, 0, NULL);
1581 }
1582
1583 /*
1584 * Free this listing...
1585 */
1586
1587 cupsdClearString(&p.info);
1588 cupsdClearString(&p.location);
1589 cupsdClearString(&p.make_model);
1590
1591 free(s);
1592 }
1593 }
1594 #endif /* HAVE_LIBSLP */
1595
1596
1597 /*
1598 * 'dequote()' - Remote quotes from a string.
1599 */
1600
1601 static char * /* O - Dequoted string */
1602 dequote(char *d, /* I - Destination string */
1603 const char *s, /* I - Source string */
1604 int dlen) /* I - Destination length */
1605 {
1606 char *dptr; /* Pointer into destination */
1607
1608
1609 if (s)
1610 {
1611 for (dptr = d, dlen --; *s && dlen > 0; s ++)
1612 if (*s != '\"')
1613 {
1614 *dptr++ = *s;
1615 dlen --;
1616 }
1617
1618 *dptr = '\0';
1619 }
1620 else
1621 *d = '\0';
1622
1623 return (d);
1624 }
1625
1626
1627 /*
1628 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1629 */
1630
1631 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1632 is_local_queue(const char *uri, /* I - Printer URI */
1633 char *host, /* O - Host string */
1634 int hostlen, /* I - Length of host buffer */
1635 char *resource, /* O - Resource string */
1636 int resourcelen) /* I - Length of resource buffer */
1637 {
1638 char scheme[32], /* Scheme portion of URI */
1639 username[HTTP_MAX_URI]; /* Username portion of URI */
1640 int port; /* Port portion of URI */
1641 cupsd_netif_t *iface; /* Network interface */
1642
1643
1644 /*
1645 * Pull the URI apart to see if this is a local or remote printer...
1646 */
1647
1648 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
1649 username, sizeof(username), host, hostlen, &port,
1650 resource, resourcelen) < HTTP_URI_OK)
1651 return (-1);
1652
1653 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName));
1654
1655 /*
1656 * Check for local server addresses...
1657 */
1658
1659 if (!strcasecmp(host, ServerName) && port == LocalPort)
1660 return (1);
1661
1662 cupsdNetIFUpdate();
1663
1664 for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
1665 iface;
1666 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
1667 if (!strcasecmp(host, iface->hostname) && port == iface->port)
1668 return (1);
1669
1670 /*
1671 * If we get here, the printer is remote...
1672 */
1673
1674 return (0);
1675 }
1676
1677
1678 /*
1679 * 'process_browse_data()' - Process new browse data.
1680 */
1681
1682 static void
1683 process_browse_data(
1684 const char *uri, /* I - URI of printer/class */
1685 const char *host, /* I - Hostname */
1686 const char *resource, /* I - Resource path */
1687 cups_ptype_t type, /* I - Printer type */
1688 ipp_pstate_t state, /* I - Printer state */
1689 const char *location, /* I - Printer location */
1690 const char *info, /* I - Printer information */
1691 const char *make_model, /* I - Printer make and model */
1692 int num_attrs, /* I - Number of attributes */
1693 cups_option_t *attrs) /* I - Attributes */
1694 {
1695 int i; /* Looping var */
1696 int update, /* Update printer attributes? */
1697 write_printcap; /* Write the printcap file? */
1698 char finaluri[HTTP_MAX_URI], /* Final URI for printer */
1699 name[IPP_MAX_NAME], /* Name of printer */
1700 newname[IPP_MAX_NAME], /* New name of printer */
1701 *hptr, /* Pointer into hostname */
1702 *sptr; /* Pointer into ServerName */
1703 char local_make_model[IPP_MAX_NAME];
1704 /* Local make and model */
1705 cupsd_printer_t *p; /* Printer information */
1706 const char *ipp_options, /* ipp-options value */
1707 *lease_duration; /* lease-duration value */
1708
1709
1710 /*
1711 * Determine if the URI contains any illegal characters in it...
1712 */
1713
1714 if (strncmp(uri, "ipp://", 6) || !host[0] ||
1715 (strncmp(resource, "/printers/", 10) &&
1716 strncmp(resource, "/classes/", 9)))
1717 {
1718 cupsdLogMessage(CUPSD_LOG_ERROR,
1719 "process_browse_data: Bad printer URI in browse data: %s",
1720 uri);
1721 return;
1722 }
1723
1724 if (strchr(resource, '?') ||
1725 (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) ||
1726 (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/')))
1727 {
1728 cupsdLogMessage(CUPSD_LOG_ERROR,
1729 "process_browse_data: Bad resource in browse data: %s",
1730 resource);
1731 return;
1732 }
1733
1734 /*
1735 * OK, this isn't a local printer; add any remote options...
1736 */
1737
1738 ipp_options = cupsGetOption("ipp-options", num_attrs, attrs);
1739
1740 if (BrowseRemoteOptions)
1741 {
1742 if (BrowseRemoteOptions[0] == '?')
1743 {
1744 /*
1745 * Override server-supplied options...
1746 */
1747
1748 snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions);
1749 }
1750 else if (ipp_options)
1751 {
1752 /*
1753 * Combine the server and local options...
1754 */
1755
1756 snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options,
1757 BrowseRemoteOptions);
1758 }
1759 else
1760 {
1761 /*
1762 * Just use the local options...
1763 */
1764
1765 snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions);
1766 }
1767
1768 uri = finaluri;
1769 }
1770 else if (ipp_options)
1771 {
1772 /*
1773 * Just use the server-supplied options...
1774 */
1775
1776 snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
1777 uri = finaluri;
1778 }
1779
1780 /*
1781 * See if we already have it listed in the Printers list, and add it if not...
1782 */
1783
1784 type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
1785 type &= ~CUPS_PRINTER_IMPLICIT;
1786 update = 0;
1787 write_printcap = 0;
1788 hptr = strchr(host, '.');
1789 sptr = strchr(ServerName, '.');
1790
1791 if (!ServerNameIsIP && sptr != NULL && hptr != NULL)
1792 {
1793 /*
1794 * Strip the common domain name components...
1795 */
1796
1797 while (hptr != NULL)
1798 {
1799 if (!strcasecmp(hptr, sptr))
1800 {
1801 *hptr = '\0';
1802 break;
1803 }
1804 else
1805 hptr = strchr(hptr + 1, '.');
1806 }
1807 }
1808
1809 if (type & CUPS_PRINTER_CLASS)
1810 {
1811 /*
1812 * Remote destination is a class...
1813 */
1814
1815 if (!strncmp(resource, "/classes/", 9))
1816 snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
1817 else
1818 return;
1819
1820 if (hptr && !*hptr)
1821 *hptr = '.'; /* Resource FQDN */
1822
1823 if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
1824 {
1825 if ((p = cupsdFindDest(resource + 9)) != NULL)
1826 {
1827 if (p->hostname && strcasecmp(p->hostname, host))
1828 {
1829 /*
1830 * Nope, this isn't the same host; if the hostname isn't the local host,
1831 * add it to the other class and then find a class using the full host
1832 * name...
1833 */
1834
1835 if (p->type & CUPS_PRINTER_REMOTE)
1836 {
1837 cupsdLogMessage(CUPSD_LOG_DEBUG,
1838 "Renamed remote class \"%s\" to \"%s@%s\"...",
1839 p->name, p->name, p->hostname);
1840 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
1841 "Class \'%s\' deleted by directory services.",
1842 p->name);
1843
1844 snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
1845 cupsdRenamePrinter(p, newname);
1846
1847 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
1848 "Class \'%s\' added by directory services.",
1849 p->name);
1850 }
1851
1852 p = NULL;
1853 }
1854 else if (!p->hostname)
1855 {
1856 /*
1857 * Hostname not set, so this must be a cached remote printer
1858 * that was created for a pending print job...
1859 */
1860
1861 cupsdSetString(&p->hostname, host);
1862 cupsdSetString(&p->uri, uri);
1863 cupsdSetString(&p->device_uri, uri);
1864 update = 1;
1865 }
1866 }
1867 else
1868 {
1869 /*
1870 * Use the short name for this shared class.
1871 */
1872
1873 strlcpy(name, resource + 9, sizeof(name));
1874 }
1875 }
1876 else if (p && !p->hostname)
1877 {
1878 /*
1879 * Hostname not set, so this must be a cached remote printer
1880 * that was created for a pending print job...
1881 */
1882
1883 cupsdSetString(&p->hostname, host);
1884 cupsdSetString(&p->uri, uri);
1885 cupsdSetString(&p->device_uri, uri);
1886 update = 1;
1887 }
1888
1889 if (!p)
1890 {
1891 /*
1892 * Class doesn't exist; add it...
1893 */
1894
1895 p = cupsdAddClass(name);
1896
1897 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote class \"%s\"...", name);
1898
1899 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
1900 "Class \'%s\' added by directory services.", name);
1901
1902 /*
1903 * Force the URI to point to the real server...
1904 */
1905
1906 p->type = type & ~CUPS_PRINTER_REJECTING;
1907 p->accepting = 1;
1908 cupsdSetString(&p->uri, uri);
1909 cupsdSetString(&p->device_uri, uri);
1910 cupsdSetString(&p->hostname, host);
1911
1912 update = 1;
1913 write_printcap = 1;
1914 }
1915 }
1916 else
1917 {
1918 /*
1919 * Remote destination is a printer...
1920 */
1921
1922 if (!strncmp(resource, "/printers/", 10))
1923 snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
1924 else
1925 return;
1926
1927 if (hptr && !*hptr)
1928 *hptr = '.'; /* Resource FQDN */
1929
1930 if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
1931 {
1932 if ((p = cupsdFindDest(resource + 10)) != NULL)
1933 {
1934 if (p->hostname && strcasecmp(p->hostname, host))
1935 {
1936 /*
1937 * Nope, this isn't the same host; if the hostname isn't the local host,
1938 * add it to the other printer and then find a printer using the full host
1939 * name...
1940 */
1941
1942 if (p->type & CUPS_PRINTER_REMOTE)
1943 {
1944 cupsdLogMessage(CUPSD_LOG_DEBUG,
1945 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1946 p->name, p->name, p->hostname);
1947 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
1948 "Printer \'%s\' deleted by directory services.",
1949 p->name);
1950
1951 snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
1952 cupsdRenamePrinter(p, newname);
1953
1954 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
1955 "Printer \'%s\' added by directory services.",
1956 p->name);
1957 }
1958
1959 p = NULL;
1960 }
1961 else if (!p->hostname)
1962 {
1963 /*
1964 * Hostname not set, so this must be a cached remote printer
1965 * that was created for a pending print job...
1966 */
1967
1968 cupsdSetString(&p->hostname, host);
1969 cupsdSetString(&p->uri, uri);
1970 cupsdSetString(&p->device_uri, uri);
1971 update = 1;
1972 }
1973 }
1974 else
1975 {
1976 /*
1977 * Use the short name for this shared printer.
1978 */
1979
1980 strlcpy(name, resource + 10, sizeof(name));
1981 }
1982 }
1983 else if (p && !p->hostname)
1984 {
1985 /*
1986 * Hostname not set, so this must be a cached remote printer
1987 * that was created for a pending print job...
1988 */
1989
1990 cupsdSetString(&p->hostname, host);
1991 cupsdSetString(&p->uri, uri);
1992 cupsdSetString(&p->device_uri, uri);
1993 update = 1;
1994 }
1995
1996 if (!p)
1997 {
1998 /*
1999 * Printer doesn't exist; add it...
2000 */
2001
2002 p = cupsdAddPrinter(name);
2003
2004 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
2005 "Printer \'%s\' added by directory services.", name);
2006
2007 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote printer \"%s\"...", name);
2008
2009 /*
2010 * Force the URI to point to the real server...
2011 */
2012
2013 p->type = type & ~CUPS_PRINTER_REJECTING;
2014 p->accepting = 1;
2015 cupsdSetString(&p->hostname, host);
2016 cupsdSetString(&p->uri, uri);
2017 cupsdSetString(&p->device_uri, uri);
2018
2019 write_printcap = 1;
2020 update = 1;
2021 }
2022 }
2023
2024 /*
2025 * Update the state...
2026 */
2027
2028 p->state = state;
2029 p->browse_time = time(NULL);
2030
2031 if ((lease_duration = cupsGetOption("lease-duration", num_attrs,
2032 attrs)) != NULL)
2033 {
2034 /*
2035 * Grab the lease-duration for the browse data; anything less then 1
2036 * second or more than 1 week gets the default BrowseTimeout...
2037 */
2038
2039 i = atoi(lease_duration);
2040 if (i < 1 || i > 604800)
2041 i = BrowseTimeout;
2042
2043 p->browse_expire = p->browse_time + i;
2044 }
2045 else
2046 p->browse_expire = p->browse_time + BrowseTimeout;
2047
2048 if (type & CUPS_PRINTER_REJECTING)
2049 {
2050 type &= ~CUPS_PRINTER_REJECTING;
2051
2052 if (p->accepting)
2053 {
2054 update = 1;
2055 p->accepting = 0;
2056 }
2057 }
2058 else if (!p->accepting)
2059 {
2060 update = 1;
2061 p->accepting = 1;
2062 }
2063
2064 if (p->type != type)
2065 {
2066 p->type = type;
2067 update = 1;
2068 }
2069
2070 if (location && (!p->location || strcmp(p->location, location)))
2071 {
2072 cupsdSetString(&p->location, location);
2073 update = 1;
2074 }
2075
2076 if (info && (!p->info || strcmp(p->info, info)))
2077 {
2078 cupsdSetString(&p->info, info);
2079 update = 1;
2080 write_printcap = 1;
2081 }
2082
2083 if (!make_model || !make_model[0])
2084 {
2085 if (type & CUPS_PRINTER_CLASS)
2086 snprintf(local_make_model, sizeof(local_make_model),
2087 "Remote Class on %s", host);
2088 else
2089 snprintf(local_make_model, sizeof(local_make_model),
2090 "Remote Printer on %s", host);
2091 }
2092 else
2093 snprintf(local_make_model, sizeof(local_make_model),
2094 "%s on %s", make_model, host);
2095
2096 if (!p->make_model || strcmp(p->make_model, local_make_model))
2097 {
2098 cupsdSetString(&p->make_model, local_make_model);
2099 update = 1;
2100 }
2101
2102 if (p->num_options)
2103 {
2104 if (!update && !(type & CUPS_PRINTER_DELETE))
2105 {
2106 /*
2107 * See if we need to update the attributes...
2108 */
2109
2110 if (p->num_options != num_attrs)
2111 update = 1;
2112 else
2113 {
2114 for (i = 0; i < num_attrs; i ++)
2115 if (strcmp(attrs[i].name, p->options[i].name) ||
2116 (!attrs[i].value != !p->options[i].value) ||
2117 (attrs[i].value && strcmp(attrs[i].value, p->options[i].value)))
2118 {
2119 update = 1;
2120 break;
2121 }
2122 }
2123 }
2124
2125 /*
2126 * Free the old options...
2127 */
2128
2129 cupsFreeOptions(p->num_options, p->options);
2130 }
2131
2132 p->num_options = num_attrs;
2133 p->options = attrs;
2134
2135 if (type & CUPS_PRINTER_DELETE)
2136 {
2137 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
2138 "%s \'%s\' deleted by directory services.",
2139 (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name);
2140
2141 cupsdExpireSubscriptions(p, NULL);
2142
2143 cupsdDeletePrinter(p, 1);
2144 cupsdUpdateImplicitClasses();
2145 write_printcap = 1;
2146 }
2147 else if (update)
2148 {
2149 cupsdSetPrinterAttrs(p);
2150 cupsdUpdateImplicitClasses();
2151 }
2152
2153 /*
2154 * See if we have a default printer... If not, make the first network
2155 * default printer the default.
2156 */
2157
2158 if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault)
2159 {
2160 /*
2161 * Find the first network default printer and use it...
2162 */
2163
2164 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2165 p;
2166 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2167 if (p->type & CUPS_PRINTER_DEFAULT)
2168 {
2169 DefaultPrinter = p;
2170 write_printcap = 1;
2171 break;
2172 }
2173 }
2174
2175 /*
2176 * Do auto-classing if needed...
2177 */
2178
2179 process_implicit_classes(&write_printcap);
2180
2181 /*
2182 * Update the printcap file...
2183 */
2184
2185 if (write_printcap)
2186 cupsdWritePrintcap();
2187 }
2188
2189
2190 #ifdef HAVE_DNSSD
2191 /*
2192 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2193 */
2194
2195 static char * /* O - TXT record */
2196 dnssdBuildTxtRecord(
2197 int *txt_len, /* O - TXT record length */
2198 cupsd_printer_t *p) /* I - Printer information */
2199 {
2200 int i, j; /* Looping vars */
2201 char type_str[32], /* Type to string buffer */
2202 state_str[32], /* State to string buffer */
2203 rp_str[1024], /* Queue name string buffer */
2204 air_str[1024], /* auth-info-required string buffer */
2205 *keyvalue[32][2]; /* Table of key/value pairs */
2206
2207
2208 /*
2209 * Load up the key value pairs...
2210 */
2211
2212 i = 0;
2213
2214 keyvalue[i ][0] = "txtvers";
2215 keyvalue[i++][1] = "1";
2216
2217 keyvalue[i ][0] = "qtotal";
2218 keyvalue[i++][1] = "1";
2219
2220 keyvalue[i ][0] = "rp";
2221 keyvalue[i++][1] = rp_str;
2222 snprintf(rp_str, sizeof(rp_str), "%s/%s",
2223 (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
2224
2225 keyvalue[i ][0] = "ty";
2226 keyvalue[i++][1] = p->make_model;
2227
2228 if (p->location && *p->location != '\0')
2229 {
2230 keyvalue[i ][0] = "note";
2231 keyvalue[i++][1] = p->location;
2232 }
2233
2234 keyvalue[i ][0] = "product";
2235 keyvalue[i++][1] = p->product ? p->product : "Unknown";
2236
2237 snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
2238 snprintf(state_str, sizeof(state_str), "%d", p->state);
2239
2240 keyvalue[i ][0] = "printer-state";
2241 keyvalue[i++][1] = state_str;
2242
2243 keyvalue[i ][0] = "printer-type";
2244 keyvalue[i++][1] = type_str;
2245
2246 keyvalue[i ][0] = "Transparent";
2247 keyvalue[i++][1] = "T";
2248
2249 keyvalue[i ][0] = "Binary";
2250 keyvalue[i++][1] = "T";
2251
2252 if ((p->type & CUPS_PRINTER_FAX))
2253 {
2254 keyvalue[i ][0] = "Fax";
2255 keyvalue[i++][1] = "T";
2256 }
2257
2258 if ((p->type & CUPS_PRINTER_COLOR))
2259 {
2260 keyvalue[i ][0] = "Color";
2261 keyvalue[i++][1] = "T";
2262 }
2263
2264 if ((p->type & CUPS_PRINTER_DUPLEX))
2265 {
2266 keyvalue[i ][0] = "Duplex";
2267 keyvalue[i++][1] = "T";
2268 }
2269
2270 if ((p->type & CUPS_PRINTER_STAPLE))
2271 {
2272 keyvalue[i ][0] = "Staple";
2273 keyvalue[i++][1] = "T";
2274 }
2275
2276 if ((p->type & CUPS_PRINTER_COPIES))
2277 {
2278 keyvalue[i ][0] = "Copies";
2279 keyvalue[i++][1] = "T";
2280 }
2281
2282 if ((p->type & CUPS_PRINTER_COLLATE))
2283 {
2284 keyvalue[i ][0] = "Collate";
2285 keyvalue[i++][1] = "T";
2286 }
2287
2288 if ((p->type & CUPS_PRINTER_PUNCH))
2289 {
2290 keyvalue[i ][0] = "Punch";
2291 keyvalue[i++][1] = "T";
2292 }
2293
2294 if ((p->type & CUPS_PRINTER_BIND))
2295 {
2296 keyvalue[i ][0] = "Bind";
2297 keyvalue[i++][1] = "T";
2298 }
2299
2300 if ((p->type & CUPS_PRINTER_SORT))
2301 {
2302 keyvalue[i ][0] = "Sort";
2303 keyvalue[i++][1] = "T";
2304 }
2305
2306 keyvalue[i ][0] = "pdl";
2307 keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
2308
2309 if (p->num_auth_info_required)
2310 {
2311 char *air = air_str; /* Pointer into string */
2312
2313
2314 for (j = 0; j < p->num_auth_info_required; j ++)
2315 {
2316 if (air >= (air_str + sizeof(air_str) - 2))
2317 break;
2318
2319 if (j)
2320 *air++ = ',';
2321
2322 strlcpy(air, p->auth_info_required[j], sizeof(air_str) - (air - air_str));
2323 air += strlen(air);
2324 }
2325
2326 keyvalue[i ][0] = "air";
2327 keyvalue[i++][1] = air;
2328 }
2329
2330 /*
2331 * Then pack them into a proper txt record...
2332 */
2333
2334 return (dnssdPackTxtRecord(txt_len, keyvalue, i));
2335 }
2336
2337
2338 /*
2339 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2340 * printer.
2341 */
2342
2343 static void
2344 dnssdDeregisterPrinter(
2345 cupsd_printer_t *p) /* I - Printer */
2346 {
2347 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
2348
2349 /*
2350 * Closing the socket deregisters the service
2351 */
2352
2353 if (p->dnssd_ipp_ref)
2354 {
2355 cupsdRemoveSelect(p->dnssd_ipp_fd);
2356 DNSServiceRefDeallocate(p->dnssd_ipp_ref);
2357 p->dnssd_ipp_ref = NULL;
2358 p->dnssd_ipp_fd = -1;
2359 }
2360
2361 cupsdClearString(&p->reg_name);
2362
2363 if (p->txt_record)
2364 {
2365 /*
2366 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2367 */
2368
2369 free(p->txt_record);
2370 p->txt_record = NULL;
2371 }
2372 }
2373
2374
2375 /*
2376 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2377 * TXT record format.
2378 */
2379
2380 static char * /* O - TXT record */
2381 dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */
2382 char *keyvalue[][2], /* I - Table of key value pairs */
2383 int count) /* I - Items in table */
2384 {
2385 int i; /* Looping var */
2386 int length; /* Length of TXT record */
2387 int length2; /* Length of value */
2388 char *txtRecord; /* TXT record buffer */
2389 char *cursor; /* Looping pointer */
2390
2391
2392 /*
2393 * Calculate the buffer size
2394 */
2395
2396 for (length = i = 0; i < count; i++)
2397 length += 1 + strlen(keyvalue[i][0]) +
2398 (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
2399
2400 /*
2401 * Allocate and fill it
2402 */
2403
2404 txtRecord = malloc(length);
2405 if (txtRecord)
2406 {
2407 *txt_len = length;
2408
2409 for (cursor = txtRecord, i = 0; i < count; i++)
2410 {
2411 /*
2412 * Drop in the p-string style length byte followed by the data
2413 */
2414
2415 length = strlen(keyvalue[i][0]);
2416 length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0;
2417
2418 *cursor++ = (unsigned char)(length + length2);
2419
2420 memcpy(cursor, keyvalue[i][0], length);
2421 cursor += length;
2422
2423 if (length2)
2424 {
2425 length2 --;
2426 *cursor++ = '=';
2427 memcpy(cursor, keyvalue[i][1], length2);
2428 cursor += length2;
2429 }
2430 }
2431 }
2432
2433 return (txtRecord);
2434 }
2435
2436
2437 /*
2438 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2439 */
2440
2441 static void
2442 dnssdRegisterCallback(
2443 DNSServiceRef sdRef, /* I - DNS Service reference */
2444 DNSServiceFlags flags, /* I - Reserved for future use */
2445 DNSServiceErrorType errorCode, /* I - Error code */
2446 const char *name, /* I - Service name */
2447 const char *regtype, /* I - Service type */
2448 const char *domain, /* I - Domain. ".local" for now */
2449 void *context) /* I - User-defined context */
2450 {
2451 (void)context;
2452
2453 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2454 "dnssdRegisterCallback(%s, %s)", name, regtype);
2455
2456 if (errorCode)
2457 {
2458 cupsdLogMessage(CUPSD_LOG_ERROR,
2459 "DNSServiceRegister failed with error %d", (int)errorCode);
2460 return;
2461 }
2462 }
2463
2464
2465 /*
2466 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2467 * or update the broadcast contents.
2468 */
2469
2470 static void
2471 dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
2472 {
2473 DNSServiceErrorType se; /* dnssd errors */
2474 cupsd_listener_t *lis; /* Current listening socket */
2475 char *txt_record, /* TXT record buffer */
2476 *name; /* Service name */
2477 int txt_len, /* TXT record length */
2478 port; /* IPP port number */
2479 char resource[1024], /* Resource path for printer */
2480 str_buffer[1024];
2481 /* C-string buffer */
2482 const char *computerName; /* Computer name c-string ptr */
2483 const char *regtype; /* Registration type */
2484 const char *domain; /* Registration domain */
2485 cupsd_location_t *location, /* Printer location */
2486 *policy; /* Operation policy for Print-Job */
2487 unsigned address[4]; /* INADDR_ANY address */
2488 #ifdef HAVE_COREFOUNDATION_H
2489 CFStringRef computerNameRef;/* Computer name CFString */
2490 CFStringEncoding nameEncoding; /* Computer name encoding */
2491 CFMutableStringRef shortNameRef; /* Mutable name string */
2492 CFIndex nameLength; /* Name string length */
2493 #else
2494 int nameLength; /* Name string length */
2495 #endif /* HAVE_COREFOUNDATION_H */
2496
2497
2498 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2499 !p->dnssd_ipp_ref ? "new" : "update");
2500
2501 /*
2502 * If per-printer sharing was just disabled make sure we're not
2503 * registered before returning.
2504 */
2505
2506 if (!p->shared)
2507 {
2508 dnssdDeregisterPrinter(p);
2509 return;
2510 }
2511
2512 /*
2513 * Get the computer name as a c-string...
2514 */
2515
2516 #ifdef HAVE_COREFOUNDATION_H
2517 computerName = NULL;
2518 if ((computerNameRef = SCDynamicStoreCopyComputerName(NULL, &nameEncoding)))
2519 if ((computerName = CFStringGetCStringPtr(computerNameRef,
2520 kCFStringEncodingUTF8)) == NULL)
2521 if (CFStringGetCString(computerNameRef, str_buffer, sizeof(str_buffer),
2522 kCFStringEncodingUTF8))
2523 computerName = str_buffer;
2524 #else
2525 computerName = ServerName;
2526 #endif /* HAVE_COREFOUNDATION_H */
2527
2528 /*
2529 * The registered name takes the form of "<printer-info> @ <computer name>"...
2530 */
2531
2532 name = NULL;
2533 if (computerName)
2534 cupsdSetStringf(&name, "%s @ %s",
2535 (p->info && strlen(p->info)) ? p->info : p->name,
2536 computerName);
2537 else
2538 cupsdSetString(&name, (p->info && strlen(p->info)) ? p->info : p->name);
2539
2540 #ifdef HAVE_COREFOUNDATION_H
2541 if (computerNameRef)
2542 CFRelease(computerNameRef);
2543 #endif /* HAVE_COREFOUNDATION_H */
2544
2545 /*
2546 * If an existing printer was renamed, unregister it and start over...
2547 */
2548
2549 if (p->reg_name && strcmp(p->reg_name, name))
2550 dnssdDeregisterPrinter(p);
2551
2552 txt_len = 0; /* anti-compiler-warning-code */
2553 txt_record = dnssdBuildTxtRecord(&txt_len, p);
2554
2555 if (!p->dnssd_ipp_ref)
2556 {
2557 /*
2558 * Initial registration...
2559 */
2560
2561 cupsdSetString(&p->reg_name, name);
2562
2563 port = ippPort();
2564
2565 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
2566 lis;
2567 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
2568 {
2569 if (lis->address.addr.sa_family == AF_INET)
2570 {
2571 port = ntohs(lis->address.ipv4.sin_port);
2572 break;
2573 }
2574 else if (lis->address.addr.sa_family == AF_INET6)
2575 {
2576 port = ntohs(lis->address.ipv6.sin6_port);
2577 break;
2578 }
2579 }
2580
2581 /*
2582 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2583 * let dnssd decide on the domain, otherwise restrict it to ".local".
2584 */
2585
2586 if (p->type & CUPS_PRINTER_CLASS)
2587 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
2588 else
2589 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
2590
2591 address[0] = address[1] = address[2] = address[3] = 0;
2592 location = cupsdFindBest(resource, HTTP_POST);
2593 policy = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
2594
2595 if ((location && !cupsdCheckAccess(address, "", 0, location)) ||
2596 (policy && !cupsdCheckAccess(address, "", 0, policy)))
2597 domain = "local.";
2598 else
2599 domain = NULL;
2600
2601 /*
2602 * Use the _fax subtype for fax queues...
2603 */
2604
2605 regtype = (p->type & CUPS_PRINTER_FAX) ? dnssdIPPFaxRegType :
2606 dnssdIPPRegType;
2607
2608
2609 cupsdLogMessage(CUPSD_LOG_DEBUG,
2610 "dnssdRegisterPrinter(%s) type, domain is \"%s\", \"%s\"",
2611 p->name, regtype, domain ? domain : "(null)");
2612
2613 se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, name, regtype,
2614 domain, NULL, htons(port), txt_len, txt_record,
2615 dnssdRegisterCallback, p);
2616
2617 /*
2618 * In case the name is too long, try shortening the string one character
2619 * at a time...
2620 */
2621
2622 if (se == kDNSServiceErr_BadParam)
2623 {
2624 #ifdef HAVE_COREFOUNDATION_H
2625 if ((shortNameRef = CFStringCreateMutable(NULL, 0)) != NULL)
2626 {
2627 CFStringAppendCString(shortNameRef, name, kCFStringEncodingUTF8);
2628 nameLength = CFStringGetLength(shortNameRef);
2629
2630 while (se == kDNSServiceErr_BadParam && nameLength > 1)
2631 {
2632 CFStringDelete(shortNameRef, CFRangeMake(--nameLength, 1));
2633 if (CFStringGetCString(shortNameRef, str_buffer, sizeof(str_buffer),
2634 kCFStringEncodingUTF8))
2635 {
2636 se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, str_buffer,
2637 regtype, NULL, NULL, htons(port),
2638 txt_len, txt_record,
2639 dnssdRegisterCallback, p);
2640 }
2641 }
2642
2643 CFRelease(shortNameRef);
2644 }
2645 #else
2646 nameLength = strlen(name);
2647 while (se == kDNSServiceErr_BadParam && nameLength > 1)
2648 {
2649 name[--nameLength] = '\0';
2650 se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, str_buffer, regtype,
2651 NULL, NULL, htons(port), txt_len, txt_record,
2652 dnssdRegisterCallback, p);
2653 }
2654 #endif /* HAVE_COREFOUNDATION_H */
2655 }
2656
2657 if (se == kDNSServiceErr_NoError)
2658 {
2659 p->dnssd_ipp_fd = DNSServiceRefSockFD(p->dnssd_ipp_ref);
2660 p->txt_record = txt_record;
2661 p->txt_len = txt_len;
2662 txt_record = NULL;
2663
2664 cupsdAddSelect(p->dnssd_ipp_fd, (cupsd_selfunc_t)cupsdUpdateDNSSDBrowse,
2665 NULL, (void *)p);
2666 }
2667 else
2668 cupsdLogMessage(CUPSD_LOG_WARN,
2669 "DNS-SD registration of \"%s\" failed with %d",
2670 p->name, se);
2671 }
2672 else if (txt_len != p->txt_len || memcmp(txt_record, p->txt_record, txt_len))
2673 {
2674 /*
2675 * Update the existing registration...
2676 */
2677
2678 /* A TTL of 0 means use record's original value (Radar 3176248) */
2679 se = DNSServiceUpdateRecord(p->dnssd_ipp_ref, NULL, 0,
2680 txt_len, txt_record, 0);
2681
2682 if (p->txt_record)
2683 free(p->txt_record);
2684
2685 p->txt_record = txt_record;
2686 p->txt_len = txt_len;
2687 txt_record = NULL;
2688 }
2689
2690 if (txt_record)
2691 free(txt_record);
2692
2693 cupsdClearString(&name);
2694 }
2695 #endif /* HAVE_DNSSD */
2696
2697
2698 /*
2699 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2700 */
2701
2702 static void
2703 process_implicit_classes(
2704 int *write_printcap) /* O - Write printcap file? */
2705 {
2706 int i; /* Looping var */
2707 int update; /* Update printer attributes? */
2708 char name[IPP_MAX_NAME], /* Name of printer */
2709 *hptr; /* Pointer into hostname */
2710 cupsd_printer_t *p, /* Printer information */
2711 *pclass, /* Printer class */
2712 *first; /* First printer in class */
2713 int offset, /* Offset of name */
2714 len; /* Length of name */
2715
2716
2717 if (!ImplicitClasses || !Printers)
2718 return;
2719
2720 /*
2721 * Loop through all available printers and create classes as needed...
2722 */
2723
2724 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0,
2725 update = 0, pclass = NULL, first = NULL;
2726 p != NULL;
2727 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2728 {
2729 /*
2730 * Skip implicit classes...
2731 */
2732
2733 if (p->type & CUPS_PRINTER_IMPLICIT)
2734 {
2735 len = 0;
2736 continue;
2737 }
2738
2739 /*
2740 * If len == 0, get the length of this printer name up to the "@"
2741 * sign (if any).
2742 */
2743
2744 cupsArraySave(Printers);
2745
2746 if (len > 0 &&
2747 !strncasecmp(p->name, name + offset, len) &&
2748 (p->name[len] == '\0' || p->name[len] == '@'))
2749 {
2750 /*
2751 * We have more than one printer with the same name; see if
2752 * we have a class, and if this printer is a member...
2753 */
2754
2755 if (pclass && strcasecmp(pclass->name, name))
2756 {
2757 if (update)
2758 cupsdSetPrinterAttrs(pclass);
2759
2760 update = 0;
2761 pclass = NULL;
2762 }
2763
2764 if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
2765 {
2766 /*
2767 * Need to add the class...
2768 */
2769
2770 pclass = cupsdAddPrinter(name);
2771 cupsArrayAdd(ImplicitPrinters, pclass);
2772
2773 pclass->type |= CUPS_PRINTER_IMPLICIT;
2774 pclass->accepting = 1;
2775 pclass->state = IPP_PRINTER_IDLE;
2776
2777 cupsdSetString(&pclass->location, p->location);
2778 cupsdSetString(&pclass->info, p->info);
2779
2780 cupsdSetString(&pclass->job_sheets[0], p->job_sheets[0]);
2781 cupsdSetString(&pclass->job_sheets[1], p->job_sheets[1]);
2782
2783 update = 1;
2784
2785 if (write_printcap)
2786 *write_printcap = 1;
2787
2788 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...",
2789 name);
2790 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
2791 "Implicit class \'%s\' added by directory services.",
2792 name);
2793 }
2794
2795 if (first != NULL)
2796 {
2797 for (i = 0; i < pclass->num_printers; i ++)
2798 if (pclass->printers[i] == first)
2799 break;
2800
2801 if (i >= pclass->num_printers)
2802 {
2803 first->in_implicit_class = 1;
2804 cupsdAddPrinterToClass(pclass, first);
2805 }
2806
2807 first = NULL;
2808 }
2809
2810 for (i = 0; i < pclass->num_printers; i ++)
2811 if (pclass->printers[i] == p)
2812 break;
2813
2814 if (i >= pclass->num_printers)
2815 {
2816 p->in_implicit_class = 1;
2817 cupsdAddPrinterToClass(pclass, p);
2818 update = 1;
2819 }
2820 }
2821 else
2822 {
2823 /*
2824 * First time around; just get name length and mark it as first
2825 * in the list...
2826 */
2827
2828 if ((hptr = strchr(p->name, '@')) != NULL)
2829 len = hptr - p->name;
2830 else
2831 len = strlen(p->name);
2832
2833 strncpy(name, p->name, len);
2834 name[len] = '\0';
2835 offset = 0;
2836
2837 if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
2838 !(first->type & CUPS_PRINTER_IMPLICIT))
2839 {
2840 /*
2841 * Can't use same name as a local printer; add "Any" to the
2842 * front of the name, unless we have explicitly disabled
2843 * the "ImplicitAnyClasses"...
2844 */
2845
2846 if (ImplicitAnyClasses && len < (sizeof(name) - 4))
2847 {
2848 /*
2849 * Add "Any" to the class name...
2850 */
2851
2852 strcpy(name, "Any");
2853 strncpy(name + 3, p->name, len);
2854 name[len + 3] = '\0';
2855 offset = 3;
2856 }
2857 else
2858 {
2859 /*
2860 * Don't create an implicit class if we have a local printer
2861 * with the same name...
2862 */
2863
2864 len = 0;
2865 cupsArrayRestore(Printers);
2866 continue;
2867 }
2868 }
2869
2870 first = p;
2871 }
2872
2873 cupsArrayRestore(Printers);
2874 }
2875
2876 /*
2877 * Update the last printer class as needed...
2878 */
2879
2880 if (pclass && update)
2881 cupsdSetPrinterAttrs(pclass);
2882 }
2883
2884
2885 /*
2886 * 'send_cups_browse()' - Send new browsing information using the CUPS
2887 * protocol.
2888 */
2889
2890 static void
2891 send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */
2892 {
2893 int i; /* Looping var */
2894 cups_ptype_t type; /* Printer type */
2895 cupsd_dirsvc_addr_t *b; /* Browse address */
2896 int bytes; /* Length of packet */
2897 char packet[1453], /* Browse data packet */
2898 uri[1024], /* Printer URI */
2899 location[1024], /* printer-location */
2900 info[1024], /* printer-info */
2901 make_model[1024];
2902 /* printer-make-and-model */
2903 cupsd_netif_t *iface; /* Network interface */
2904
2905
2906 /*
2907 * Figure out the printer type value...
2908 */
2909
2910 type = p->type | CUPS_PRINTER_REMOTE;
2911
2912 if (!p->accepting)
2913 type |= CUPS_PRINTER_REJECTING;
2914
2915 if (p == DefaultPrinter)
2916 type |= CUPS_PRINTER_DEFAULT;
2917
2918 /*
2919 * Remove quotes from printer-info, printer-location, and
2920 * printer-make-and-model attributes...
2921 */
2922
2923 dequote(location, p->location, sizeof(location));
2924 dequote(info, p->info, sizeof(info));
2925
2926 if (p->make_model)
2927 dequote(make_model, p->make_model, sizeof(make_model));
2928 else if (p->type & CUPS_PRINTER_CLASS)
2929 {
2930 if (p->num_printers > 0 && p->printers[0]->make_model)
2931 strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model));
2932 else
2933 strlcpy(make_model, "Local Printer Class", sizeof(make_model));
2934 }
2935 else if (p->raw)
2936 strlcpy(make_model, "Local Raw Printer", sizeof(make_model));
2937 else
2938 strlcpy(make_model, "Local System V Printer", sizeof(make_model));
2939
2940 /*
2941 * Send a packet to each browse address...
2942 */
2943
2944 for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
2945 if (b->iface[0])
2946 {
2947 /*
2948 * Send the browse packet to one or more interfaces...
2949 */
2950
2951 if (!strcmp(b->iface, "*"))
2952 {
2953 /*
2954 * Send to all local interfaces...
2955 */
2956
2957 cupsdNetIFUpdate();
2958
2959 for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
2960 iface;
2961 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
2962 {
2963 /*
2964 * Only send to local, IPv4 interfaces...
2965 */
2966
2967 if (!iface->is_local || !iface->port ||
2968 iface->address.addr.sa_family != AF_INET)
2969 continue;
2970
2971 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
2972 iface->hostname, iface->port,
2973 (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
2974 "/printers/%s",
2975 p->name);
2976 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2977 type, p->state, uri, location, info, make_model,
2978 p->browse_attrs ? p->browse_attrs : "");
2979
2980 bytes = strlen(packet);
2981
2982 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2983 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
2984 iface->name, packet);
2985
2986 iface->broadcast.ipv4.sin_port = htons(BrowsePort);
2987
2988 sendto(BrowseSocket, packet, bytes, 0,
2989 (struct sockaddr *)&(iface->broadcast),
2990 httpAddrLength(&(iface->broadcast)));
2991 }
2992 }
2993 else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
2994 {
2995 /*
2996 * Send to the named interface using the IPv4 address...
2997 */
2998
2999 while (iface)
3000 if (strcmp(b->iface, iface->name))
3001 {
3002 iface = NULL;
3003 break;
3004 }
3005 else if (iface->address.addr.sa_family == AF_INET && iface->port)
3006 break;
3007 else
3008 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
3009
3010 if (iface)
3011 {
3012 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
3013 iface->hostname, iface->port,
3014 (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
3015 "/printers/%s",
3016 p->name);
3017 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3018 type, p->state, uri, location, info, make_model,
3019 p->browse_attrs ? p->browse_attrs : "");
3020
3021 bytes = strlen(packet);
3022
3023 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3024 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
3025 iface->name, packet);
3026
3027 iface->broadcast.ipv4.sin_port = htons(BrowsePort);
3028
3029 sendto(BrowseSocket, packet, bytes, 0,
3030 (struct sockaddr *)&(iface->broadcast),
3031 httpAddrLength(&(iface->broadcast)));
3032 }
3033 }
3034 }
3035 else
3036 {
3037 /*
3038 * Send the browse packet to the indicated address using
3039 * the default server name...
3040 */
3041
3042 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3043 type, p->state, p->uri, location, info, make_model,
3044 p->browse_attrs ? p->browse_attrs : "");
3045
3046 bytes = strlen(packet);
3047 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3048 "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
3049
3050 if (sendto(BrowseSocket, packet, bytes, 0,
3051 (struct sockaddr *)&(b->to),
3052 httpAddrLength(&(b->to))) <= 0)
3053 {
3054 /*
3055 * Unable to send browse packet, so remove this address from the
3056 * list...
3057 */
3058
3059 cupsdLogMessage(CUPSD_LOG_ERROR,
3060 "cupsdSendBrowseList: sendto failed for browser "
3061 "%d - %s.",
3062 (int)(b - Browsers + 1), strerror(errno));
3063
3064 if (i > 1)
3065 memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
3066
3067 b --;
3068 NumBrowsers --;
3069 }
3070 }
3071 }
3072
3073
3074 #ifdef HAVE_OPENLDAP
3075 /*
3076 * 'send_ldap_browse()' - Send LDAP printer registrations.
3077 */
3078
3079 static void
3080 send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */
3081 {
3082 int i; /* Looping var... */
3083 LDAPMod mods[7]; /* The 7 attributes we will be adding */
3084 LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */
3085 LDAPMessage *res; /* Search result token */
3086 char *cn_value[2], /* Change records */
3087 *uri[2],
3088 *info[2],
3089 *location[2],
3090 *make_model[2],
3091 *type[2],
3092 typestring[255], /* String to hold printer-type */
3093 filter[256], /* Search filter for possible UPDATEs */
3094 dn[1024]; /* DN of the printer we are adding */
3095 int rc; /* LDAP status */
3096 static const char * const objectClass_values[] =
3097 { /* The 3 objectClass's we use in */
3098 "top", /* our LDAP entries */
3099 "device",
3100 "cupsPrinter",
3101 NULL
3102 };
3103
3104 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s\n", p->name);
3105
3106 /*
3107 * Everything in ldap is ** so we fudge around it...
3108 */
3109
3110 sprintf(typestring, "%u", p->type);
3111
3112 cn_value[0] = p->name;
3113 cn_value[1] = NULL;
3114 info[0] = p->info ? p->info : "Unknown";
3115 info[1] = NULL;
3116 location[0] = p->location ? p->location : "Unknown";
3117 location[1] = NULL;
3118 make_model[0] = p->make_model ? p->make_model : "Unknown";
3119 make_model[1] = NULL;
3120 type[0] = typestring;
3121 type[1] = NULL;
3122 uri[0] = p->uri;
3123 uri[1] = NULL;
3124
3125 snprintf(filter, sizeof(filter),
3126 "(&(objectclass=cupsPrinter)(printerURI=%s))", p->uri);
3127
3128 ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
3129 filter, (char **)ldap_attrs, 0, &res);
3130 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: Searching \"%s\"",
3131 filter);
3132
3133 mods[0].mod_type = "cn";
3134 mods[0].mod_values = cn_value;
3135 mods[1].mod_type = "printerDescription";
3136 mods[1].mod_values = info;
3137 mods[2].mod_type = "printerURI";
3138 mods[2].mod_values = uri;
3139 mods[3].mod_type = "printerLocation";
3140 mods[3].mod_values = location;
3141 mods[4].mod_type = "printerMakeAndModel";
3142 mods[4].mod_values = make_model;
3143 mods[5].mod_type = "printerType";
3144 mods[5].mod_values = type;
3145 mods[6].mod_type = "objectClass";
3146 mods[6].mod_values = (char **)objectClass_values;
3147
3148 snprintf(dn, sizeof(dn), "cn=%s,ou=printers,%s", p->name, BrowseLDAPDN);
3149 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn);
3150
3151 if (ldap_count_entries(BrowseLDAPHandle, res) > 0)
3152 {
3153 /*
3154 * Printer has already been registered, modify the current
3155 * registration...
3156 */
3157
3158 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3159 "send_ldap_browse: Replacing entry...");
3160
3161 for (i = 0; i < 7; i ++)
3162 {
3163 pmods[i] = mods + i;
3164 pmods[i]->mod_op = LDAP_MOD_REPLACE;
3165 }
3166 pmods[i] = NULL;
3167
3168 if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
3169 cupsdLogMessage(CUPSD_LOG_ERROR,
3170 "LDAP modify for %s failed with status %d: %s",
3171 p->name, rc, ldap_err2string(rc));
3172 }
3173 else
3174 {
3175 /*
3176 * Printer has never been registered, add the current
3177 * registration...
3178 */
3179
3180 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3181 "send_ldap_browse: Adding entry...");
3182
3183 for (i = 0; i < 7; i ++)
3184 {
3185 pmods[i] = mods + i;
3186 pmods[i]->mod_op = LDAP_MOD_ADD;
3187 }
3188 pmods[i] = NULL;
3189
3190 if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
3191 cupsdLogMessage(CUPSD_LOG_ERROR,
3192 "LDAP add for %s failed with status %d: %s",
3193 p->name, rc, ldap_err2string(rc));
3194 }
3195 }
3196 #endif /* HAVE_OPENLDAP */
3197
3198
3199 #ifdef HAVE_LIBSLP
3200 /*
3201 * 'send_slp_browse()' - Register the specified printer with SLP.
3202 */
3203
3204 static void
3205 send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */
3206 {
3207 char srvurl[HTTP_MAX_URI], /* Printer service URI */
3208 attrs[8192], /* Printer attributes */
3209 finishings[1024], /* Finishings to support */
3210 make_model[IPP_MAX_NAME * 2],
3211 /* Make and model, quoted */
3212 location[IPP_MAX_NAME * 2],
3213 /* Location, quoted */
3214 info[IPP_MAX_NAME * 2], /* Info, quoted */
3215 *src, /* Pointer to original string */
3216 *dst; /* Pointer to destination string */
3217 ipp_attribute_t *authentication; /* uri-authentication-supported value */
3218 SLPError error; /* SLP error, if any */
3219
3220
3221 cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p,
3222 p->name);
3223
3224 /*
3225 * Make the SLP service URL that conforms to the IANA
3226 * 'printer:' template.
3227 */
3228
3229 snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
3230
3231 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
3232
3233 /*
3234 * Figure out the finishings string...
3235 */
3236
3237 if (p->type & CUPS_PRINTER_STAPLE)
3238 strcpy(finishings, "staple");
3239 else
3240 finishings[0] = '\0';
3241
3242 if (p->type & CUPS_PRINTER_BIND)
3243 {
3244 if (finishings[0])
3245 strlcat(finishings, ",bind", sizeof(finishings));
3246 else
3247 strcpy(finishings, "bind");
3248 }
3249
3250 if (p->type & CUPS_PRINTER_PUNCH)
3251 {
3252 if (finishings[0])
3253 strlcat(finishings, ",punch", sizeof(finishings));
3254 else
3255 strcpy(finishings, "punch");
3256 }
3257
3258 if (p->type & CUPS_PRINTER_COVER)
3259 {
3260 if (finishings[0])
3261 strlcat(finishings, ",cover", sizeof(finishings));
3262 else
3263 strcpy(finishings, "cover");
3264 }
3265
3266 if (p->type & CUPS_PRINTER_SORT)
3267 {
3268 if (finishings[0])
3269 strlcat(finishings, ",sort", sizeof(finishings));
3270 else
3271 strcpy(finishings, "sort");
3272 }
3273
3274 if (!finishings[0])
3275 strcpy(finishings, "none");
3276
3277 /*
3278 * Quote any commas in the make and model, location, and info strings...
3279 */
3280
3281 for (src = p->make_model, dst = make_model;
3282 src && *src && dst < (make_model + sizeof(make_model) - 2);)
3283 {
3284 if (*src == ',' || *src == '\\' || *src == ')')
3285 *dst++ = '\\';
3286
3287 *dst++ = *src++;
3288 }
3289
3290 *dst = '\0';
3291
3292 if (!make_model[0])
3293 strcpy(make_model, "Unknown");
3294
3295 for (src = p->location, dst = location;
3296 src && *src && dst < (location + sizeof(location) - 2);)
3297 {
3298 if (*src == ',' || *src == '\\' || *src == ')')
3299 *dst++ = '\\';
3300
3301 *dst++ = *src++;
3302 }
3303
3304 *dst = '\0';
3305
3306 if (!location[0])
3307 strcpy(location, "Unknown");
3308
3309 for (src = p->info, dst = info;
3310 src && *src && dst < (info + sizeof(info) - 2);)
3311 {
3312 if (*src == ',' || *src == '\\' || *src == ')')
3313 *dst++ = '\\';
3314
3315 *dst++ = *src++;
3316 }
3317
3318 *dst = '\0';
3319
3320 if (!info[0])
3321 strcpy(info, "Unknown");
3322
3323 /*
3324 * Get the authentication value...
3325 */
3326
3327 authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
3328 IPP_TAG_KEYWORD);
3329
3330 /*
3331 * Make the SLP attribute string list that conforms to
3332 * the IANA 'printer:' template.
3333 */
3334
3335 snprintf(attrs, sizeof(attrs),
3336 "(printer-uri-supported=%s),"
3337 "(uri-authentication-supported=%s>),"
3338 #ifdef HAVE_SSL
3339 "(uri-security-supported=tls>),"
3340 #else
3341 "(uri-security-supported=none>),"
3342 #endif /* HAVE_SSL */
3343 "(printer-name=%s),"
3344 "(printer-location=%s),"
3345 "(printer-info=%s),"
3346 "(printer-more-info=%s),"
3347 "(printer-make-and-model=%s),"
3348 "(printer-type=%d),"
3349 "(charset-supported=utf-8),"
3350 "(natural-language-configured=%s),"
3351 "(natural-language-supported=de,en,es,fr,it),"
3352 "(color-supported=%s),"
3353 "(finishings-supported=%s),"
3354 "(sides-supported=one-sided%s),"
3355 "(multiple-document-jobs-supported=true)"
3356 "(ipp-versions-supported=1.0,1.1)",
3357 p->uri, authentication->values[0].string.text, p->name, location,
3358 info, p->uri, make_model, p->type, DefaultLanguage,
3359 p->type & CUPS_PRINTER_COLOR ? "true" : "false",
3360 finishings,
3361 p->type & CUPS_PRINTER_DUPLEX ?
3362 ",two-sided-long-edge,two-sided-short-edge" : "");
3363
3364 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
3365
3366 /*
3367 * Register the printer with the SLP server...
3368 */
3369
3370 error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
3371 SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
3372
3373 if (error != SLP_OK)
3374 cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
3375 error);
3376 }
3377
3378
3379 /*
3380 * 'slp_attr_callback()' - SLP attribute callback
3381 */
3382
3383 static SLPBoolean /* O - SLP_TRUE for success */
3384 slp_attr_callback(
3385 SLPHandle hslp, /* I - SLP handle */
3386 const char *attrlist, /* I - Attribute list */
3387 SLPError errcode, /* I - Parsing status for this attr */
3388 void *cookie) /* I - Current printer */
3389 {
3390 char *tmp = 0; /* Temporary string */
3391 cupsd_printer_t *p = (cupsd_printer_t*)cookie;
3392 /* Current printer */
3393
3394
3395 (void)hslp; /* anti-compiler-warning-code */
3396
3397 /*
3398 * Bail if there was an error
3399 */
3400
3401 if (errcode != SLP_OK)
3402 return (SLP_TRUE);
3403
3404 /*
3405 * Parse the attrlist to obtain things needed to build CUPS browse packet
3406 */
3407
3408 memset(p, 0, sizeof(cupsd_printer_t));
3409
3410 if (slp_get_attr(attrlist, "(printer-location=", &(p->location)))
3411 return (SLP_FALSE);
3412 if (slp_get_attr(attrlist, "(printer-info=", &(p->info)))
3413 return (SLP_FALSE);
3414 if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model)))
3415 return (SLP_FALSE);
3416 if (!slp_get_attr(attrlist, "(printer-type=", &tmp))
3417 p->type = atoi(tmp);
3418 else
3419 p->type = CUPS_PRINTER_REMOTE;
3420
3421 cupsdClearString(&tmp);
3422
3423 return (SLP_TRUE);
3424 }
3425
3426
3427 /*
3428 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3429 */
3430
3431 static void
3432 slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */
3433 {
3434 char srvurl[HTTP_MAX_URI]; /* Printer service URI */
3435
3436
3437 cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
3438
3439 if (!(p->type & CUPS_PRINTER_REMOTE))
3440 {
3441 /*
3442 * Make the SLP service URL that conforms to the IANA
3443 * 'printer:' template.
3444 */
3445
3446 snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
3447
3448 /*
3449 * Deregister the printer...
3450 */
3451
3452 SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
3453 }
3454 }
3455
3456
3457 /*
3458 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3459 */
3460
3461 static int /* O - 0 on success */
3462 slp_get_attr(const char *attrlist, /* I - Attribute list string */
3463 const char *tag, /* I - Name of attribute */
3464 char **valbuf) /* O - Value */
3465 {
3466 char *ptr1, /* Pointer into string */
3467 *ptr2; /* ... */
3468
3469
3470 cupsdClearString(valbuf);
3471
3472 if ((ptr1 = strstr(attrlist, tag)) != NULL)
3473 {
3474 ptr1 += strlen(tag);
3475
3476 if ((ptr2 = strchr(ptr1,')')) != NULL)
3477 {
3478 /*
3479 * Copy the value...
3480 */
3481
3482 *valbuf = calloc(ptr2 - ptr1 + 1, 1);
3483 strncpy(*valbuf, ptr1, ptr2 - ptr1);
3484
3485 /*
3486 * Dequote the value...
3487 */
3488
3489 for (ptr1 = *valbuf; *ptr1; ptr1 ++)
3490 if (*ptr1 == '\\' && ptr1[1])
3491 _cups_strcpy(ptr1, ptr1 + 1);
3492
3493 return (0);
3494 }
3495 }
3496
3497 return (-1);
3498 }
3499
3500
3501 /*
3502 * 'slp_reg_callback()' - Empty SLPRegReport.
3503 */
3504
3505 static void
3506 slp_reg_callback(SLPHandle hslp, /* I - SLP handle */
3507 SLPError errcode, /* I - Error code, if any */
3508 void *cookie) /* I - App data */
3509 {
3510 (void)hslp;
3511 (void)errcode;
3512 (void)cookie;
3513
3514 return;
3515 }
3516
3517
3518 /*
3519 * 'slp_url_callback()' - SLP service url callback
3520 */
3521
3522 static SLPBoolean /* O - TRUE = OK, FALSE = error */
3523 slp_url_callback(
3524 SLPHandle hslp, /* I - SLP handle */
3525 const char *srvurl, /* I - URL of service */
3526 unsigned short lifetime, /* I - Life of service */
3527 SLPError errcode, /* I - Existing error code */
3528 void *cookie) /* I - Pointer to service list */
3529 {
3530 slpsrvurl_t *s, /* New service entry */
3531 **head; /* Pointer to head of entry */
3532
3533
3534 /*
3535 * Let the compiler know we won't be using these vars...
3536 */
3537
3538 (void)hslp;
3539 (void)lifetime;
3540
3541 /*
3542 * Bail if there was an error
3543 */
3544
3545 if (errcode != SLP_OK)
3546 return (SLP_TRUE);
3547
3548 /*
3549 * Grab the head of the list...
3550 */
3551
3552 head = (slpsrvurl_t**)cookie;
3553
3554 /*
3555 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3556 */
3557
3558 if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
3559 return (SLP_FALSE);
3560
3561 /*
3562 * Copy the SLP service URL...
3563 */
3564
3565 strlcpy(s->url, srvurl, sizeof(s->url));
3566
3567 /*
3568 * Link the SLP service URL into the head of the list
3569 */
3570
3571 if (*head)
3572 s->next = *head;
3573
3574 *head = s;
3575
3576 return (SLP_TRUE);
3577 }
3578 #endif /* HAVE_LIBSLP */
3579
3580
3581 /*
3582 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3583 */
3584
3585 static void
3586 update_cups_browse(void)
3587 {
3588 int i; /* Looping var */
3589 int auth; /* Authorization status */
3590 int len; /* Length of name string */
3591 int bytes; /* Number of bytes left */
3592 char packet[1541], /* Broadcast packet */
3593 *pptr; /* Pointer into packet */
3594 socklen_t srclen; /* Length of source address */
3595 http_addr_t srcaddr; /* Source address */
3596 char srcname[1024]; /* Source hostname */
3597 unsigned address[4]; /* Source address */
3598 unsigned type; /* Printer type */
3599 unsigned state; /* Printer state */
3600 char uri[HTTP_MAX_URI], /* Printer URI */
3601 host[HTTP_MAX_URI], /* Host portion of URI */
3602 resource[HTTP_MAX_URI], /* Resource portion of URI */
3603 info[IPP_MAX_NAME], /* Information string */
3604 location[IPP_MAX_NAME], /* Location string */
3605 make_model[IPP_MAX_NAME];/* Make and model string */
3606 int num_attrs; /* Number of attributes */
3607 cups_option_t *attrs; /* Attributes */
3608
3609
3610 /*
3611 * Read a packet from the browse socket...
3612 */
3613
3614 srclen = sizeof(srcaddr);
3615 if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
3616 (struct sockaddr *)&srcaddr, &srclen)) < 0)
3617 {
3618 /*
3619 * "Connection refused" is returned under Linux if the destination port
3620 * or address is unreachable from a previous sendto(); check for the
3621 * error here and ignore it for now...
3622 */
3623
3624 if (errno != ECONNREFUSED && errno != EAGAIN)
3625 {
3626 cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
3627 strerror(errno));
3628 cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
3629
3630 cupsdStopBrowsing();
3631 Browsing = 0;
3632 }
3633
3634 return;
3635 }
3636
3637 packet[bytes] = '\0';
3638
3639 /*
3640 * If we're about to sleep, ignore incoming browse packets.
3641 */
3642
3643 if (Sleeping)
3644 return;
3645
3646 /*
3647 * Figure out where it came from...
3648 */
3649
3650 #ifdef AF_INET6
3651 if (srcaddr.addr.sa_family == AF_INET6)
3652 {
3653 address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
3654 address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
3655 address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
3656 address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
3657 }
3658 else
3659 #endif /* AF_INET6 */
3660 {
3661 address[0] = 0;
3662 address[1] = 0;
3663 address[2] = 0;
3664 address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
3665 }
3666
3667 if (HostNameLookups)
3668 httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
3669 else
3670 httpAddrString(&srcaddr, srcname, sizeof(srcname));
3671
3672 len = strlen(srcname);
3673
3674 /*
3675 * Do ACL stuff...
3676 */
3677
3678 if (BrowseACL)
3679 {
3680 if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
3681 {
3682 /*
3683 * Access from localhost (127.0.0.1) is always allowed...
3684 */
3685
3686 auth = AUTH_ALLOW;
3687 }
3688 else
3689 {
3690 /*
3691 * Do authorization checks on the domain/address...
3692 */
3693
3694 switch (BrowseACL->order_type)
3695 {
3696 default :
3697 auth = AUTH_DENY; /* anti-compiler-warning-code */
3698 break;
3699
3700 case AUTH_ALLOW : /* Order Deny,Allow */
3701 auth = AUTH_ALLOW;
3702
3703 if (cupsdCheckAuth(address, srcname, len,
3704 BrowseACL->num_deny, BrowseACL->deny))
3705 auth = AUTH_DENY;
3706
3707 if (cupsdCheckAuth(address, srcname, len,
3708 BrowseACL->num_allow, BrowseACL->allow))
3709 auth = AUTH_ALLOW;
3710 break;
3711
3712 case AUTH_DENY : /* Order Allow,Deny */
3713 auth = AUTH_DENY;
3714
3715 if (cupsdCheckAuth(address, srcname, len,
3716 BrowseACL->num_allow, BrowseACL->allow))
3717 auth = AUTH_ALLOW;
3718
3719 if (cupsdCheckAuth(address, srcname, len,
3720 BrowseACL->num_deny, BrowseACL->deny))
3721 auth = AUTH_DENY;
3722 break;
3723 }
3724 }
3725 }
3726 else
3727 auth = AUTH_ALLOW;
3728
3729 if (auth == AUTH_DENY)
3730 {
3731 cupsdLogMessage(CUPSD_LOG_DEBUG,
3732 "update_cups_browse: Refused %d bytes from %s", bytes,
3733 srcname);
3734 return;
3735 }
3736
3737 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3738 "update_cups_browse: (%d bytes from %s) %s", bytes,
3739 srcname, packet);
3740
3741 /*
3742 * Parse packet...
3743 */
3744
3745 if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
3746 {
3747 cupsdLogMessage(CUPSD_LOG_WARN,
3748 "update_cups_browse: Garbled browse packet - %s", packet);
3749 return;
3750 }
3751
3752 strcpy(location, "Location Unknown");
3753 strcpy(info, "No Information Available");
3754 make_model[0] = '\0';
3755 num_attrs = 0;
3756 attrs = NULL;
3757
3758 if ((pptr = strchr(packet, '\"')) != NULL)
3759 {
3760 /*
3761 * Have extended information; can't use sscanf for it because not all
3762 * sscanf's allow empty strings with %[^\"]...
3763 */
3764
3765 for (i = 0, pptr ++;
3766 i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
3767 i ++, pptr ++)
3768 location[i] = *pptr;
3769
3770 if (i)
3771 location[i] = '\0';
3772
3773 if (*pptr == '\"')
3774 pptr ++;
3775
3776 while (*pptr && isspace(*pptr & 255))
3777 pptr ++;
3778
3779 if (*pptr == '\"')
3780 {
3781 for (i = 0, pptr ++;
3782 i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
3783 i ++, pptr ++)
3784 info[i] = *pptr;
3785
3786 info[i] = '\0';
3787
3788 if (*pptr == '\"')
3789 pptr ++;
3790
3791 while (*pptr && isspace(*pptr & 255))
3792 pptr ++;
3793
3794 if (*pptr == '\"')
3795 {
3796 for (i = 0, pptr ++;
3797 i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
3798 i ++, pptr ++)
3799 make_model[i] = *pptr;
3800
3801 if (*pptr == '\"')
3802 pptr ++;
3803
3804 make_model[i] = '\0';
3805
3806 if (*pptr)
3807 num_attrs = cupsParseOptions(pptr, num_attrs, &attrs);
3808 }
3809 }
3810 }
3811
3812 DEBUG_puts(packet);
3813 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3814 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3815 type, state, uri, location, info, make_model));
3816
3817 /*
3818 * Pull the URI apart to see if this is a local or remote printer...
3819 */
3820
3821 if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
3822 {
3823 cupsFreeOptions(num_attrs, attrs);
3824 return;
3825 }
3826
3827 /*
3828 * Do relaying...
3829 */
3830
3831 for (i = 0; i < NumRelays; i ++)
3832 if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from)))
3833 if (sendto(BrowseSocket, packet, bytes, 0,
3834 (struct sockaddr *)&(Relays[i].to),
3835 httpAddrLength(&(Relays[i].to))) <= 0)
3836 {
3837 cupsdLogMessage(CUPSD_LOG_ERROR,
3838 "update_cups_browse: sendto failed for relay %d - %s.",
3839 i + 1, strerror(errno));
3840 cupsFreeOptions(num_attrs, attrs);
3841 return;
3842 }
3843
3844 /*
3845 * Process the browse data...
3846 */
3847
3848 process_browse_data(uri, host, resource, (cups_ptype_t)type,
3849 (ipp_pstate_t)state, location, info, make_model,
3850 num_attrs, attrs);
3851 }
3852
3853
3854 /*
3855 * 'update_lpd()' - Update the LPD configuration as needed.
3856 */
3857
3858 static void
3859 update_lpd(int onoff) /* - 1 = turn on, 0 = turn off */
3860 {
3861 if (!LPDConfigFile)
3862 return;
3863
3864 if (!strncmp(LPDConfigFile, "xinetd:///", 10))
3865 {
3866 /*
3867 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
3868 */
3869
3870 char newfile[1024]; /* New cups-lpd.N file */
3871 cups_file_t *ofp, /* Original file pointer */
3872 *nfp; /* New file pointer */
3873 char line[1024]; /* Line from file */
3874
3875
3876 snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9);
3877
3878 if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL)
3879 {
3880 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
3881 LPDConfigFile + 9, strerror(errno));
3882 return;
3883 }
3884
3885 if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
3886 {
3887 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
3888 newfile, strerror(errno));
3889 cupsFileClose(ofp);
3890 return;
3891 }
3892
3893 /*
3894 * Copy all of the lines from the cups-lpd file...
3895 */
3896
3897 while (cupsFileGets(ofp, line, sizeof(line)))
3898 {
3899 if (line[0] == '{')
3900 {
3901 cupsFilePrintf(nfp, "%s\n", line);
3902 snprintf(line, sizeof(line), "\tdisable = %s",
3903 onoff ? "no" : "yes");
3904 }
3905 else if (strstr(line, "disable ="))
3906 continue;
3907
3908 cupsFilePrintf(nfp, "%s\n", line);
3909 }
3910
3911 cupsFileClose(nfp);
3912 cupsFileClose(ofp);
3913 rename(newfile, LPDConfigFile + 9);
3914 }
3915 else if (!strncmp(LPDConfigFile, "launchd:///", 11))
3916 {
3917 /*
3918 * Enable/disable LPD via the launchctl command...
3919 */
3920
3921 char *argv[5], /* Arguments for command */
3922 *envp[MAX_ENV]; /* Environment for command */
3923 int pid; /* Process ID */
3924
3925
3926 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
3927 argv[0] = (char *)"launchctl";
3928 argv[1] = (char *)(onoff ? "load" : "unload");
3929 argv[2] = (char *)"-w";
3930 argv[3] = LPDConfigFile + 10;
3931 argv[4] = NULL;
3932
3933 cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1,
3934 NULL, &pid);
3935 }
3936 }
3937
3938
3939 /*
3940 * 'update_polling()' - Read status messages from the poll daemons.
3941 */
3942
3943 static void
3944 update_polling(void)
3945 {
3946 char *ptr, /* Pointer to end of line in buffer */
3947 message[1024]; /* Pointer to message text */
3948 int loglevel; /* Log level for message */
3949
3950
3951 while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
3952 message, sizeof(message))) != NULL)
3953 if (!strchr(PollStatusBuffer->buffer, '\n'))
3954 break;
3955
3956 if (ptr == NULL && !PollStatusBuffer->bufused)
3957 {
3958 /*
3959 * All polling processes have died; stop polling...
3960 */
3961
3962 cupsdLogMessage(CUPSD_LOG_ERROR,
3963 "update_polling: all polling processes have exited!");
3964 cupsdStopPolling();
3965 }
3966 }
3967
3968
3969 /*
3970 * 'update_smb()' - Update the SMB configuration as needed.
3971 */
3972
3973 static void
3974 update_smb(int onoff) /* I - 1 = turn on, 0 = turn off */
3975 {
3976 if (!SMBConfigFile)
3977 return;
3978
3979 if (!strncmp(SMBConfigFile, "samba:///", 9))
3980 {
3981 /*
3982 * Enable/disable SMB via the specified smb.conf config file...
3983 */
3984
3985 char newfile[1024]; /* New smb.conf.N file */
3986 cups_file_t *ofp, /* Original file pointer */
3987 *nfp; /* New file pointer */
3988 char line[1024]; /* Line from file */
3989 int in_printers; /* In [printers] section? */
3990
3991
3992 snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8);
3993
3994 if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL)
3995 {
3996 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
3997 SMBConfigFile + 8, strerror(errno));
3998 return;
3999 }
4000
4001 if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
4002 {
4003 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
4004 newfile, strerror(errno));
4005 cupsFileClose(ofp);
4006 return;
4007 }
4008
4009 /*
4010 * Copy all of the lines from the smb.conf file...
4011 */
4012
4013 in_printers = 0;
4014
4015 while (cupsFileGets(ofp, line, sizeof(line)))
4016 {
4017 if (in_printers && strstr(line, "printable ="))
4018 snprintf(line, sizeof(line), " printable = %s",
4019 onoff ? "yes" : "no");
4020
4021 cupsFilePrintf(nfp, "%s\n", line);
4022
4023 if (line[0] == '[')
4024 in_printers = !strcmp(line, "[printers]");
4025 }
4026
4027 cupsFileClose(nfp);
4028 cupsFileClose(ofp);
4029 rename(newfile, SMBConfigFile + 8);
4030 }
4031 else if (!strncmp(SMBConfigFile, "launchd:///", 11))
4032 {
4033 /*
4034 * Enable/disable SMB via the launchctl command...
4035 */
4036
4037 char *argv[5], /* Arguments for command */
4038 *envp[MAX_ENV]; /* Environment for command */
4039 int pid; /* Process ID */
4040
4041
4042 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
4043 argv[0] = (char *)"launchctl";
4044 argv[1] = (char *)(onoff ? "load" : "unload");
4045 argv[2] = (char *)"-w";
4046 argv[3] = SMBConfigFile + 10;
4047 argv[4] = NULL;
4048
4049 cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1,
4050 NULL, &pid);
4051 }
4052 }
4053
4054
4055 /*
4056 * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".
4057 */