2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
4 * Directory services routines for the CUPS scheduler.
6 * Copyright 2007-2012 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
20 * cupsdRegisterPrinter() - Start sending broadcast information for a
21 * printer or update the broadcast contents.
22 * cupsdStartBrowsing() - Start sending and receiving broadcast
24 * cupsdStopBrowsing() - Stop sending and receiving broadcast
26 * cupsdUpdateDNSSDName() - Update the computer name we use for
28 * dnssdAddAlias() - Add a DNS-SD alias name.
29 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
30 * dnssdDeregisterInstance() - Deregister a DNS-SD service instance.
31 * dnssdDeregisterPrinter() - Deregister all services for a printer.
32 * dnssdErrorString() - Return an error string for an error code.
33 * dnssdRegisterCallback() - Free a TXT record.
34 * dnssdRegisterCallback() - DNSServiceRegister callback.
35 * dnssdRegisterInstance() - Register an instance of a printer service.
36 * dnssdRegisterPrinter() - Start sending broadcast information for a
37 * printer or update the broadcast contents.
38 * dnssdStop() - Stop all DNS-SD registrations.
39 * dnssdUpdate() - Handle DNS-SD queries.
40 * get_auth_info_required() - Get the auth-info-required value to advertise.
41 * get_hostconfig() - Get an /etc/hostconfig service setting.
42 * update_lpd() - Update the LPD configuration as needed.
43 * update_smb() - Update the SMB configuration as needed.
47 * Include necessary headers...
53 #if defined(HAVE_DNSSD) && defined(__APPLE__)
55 # include <CoreFoundation/CoreFoundation.h>
56 # include <SystemConfiguration/SystemConfiguration.h>
57 #endif /* HAVE_DNSSD && __APPLE__ */
64 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
65 static char *get_auth_info_required(cupsd_printer_t
*p
,
66 char *buffer
, size_t bufsize
);
67 #endif /* HAVE_DNSSD || HAVE_AVAHI */
69 static int get_hostconfig(const char *name
);
70 #endif /* __APPLE__ */
71 static void update_lpd(int onoff
);
72 static void update_smb(int onoff
);
75 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
77 static void dnssdAddAlias(const void *key
, const void *value
,
79 # endif /* __APPLE__ */
80 static cupsd_txt_t
dnssdBuildTxtRecord(cupsd_printer_t
*p
, int for_lpd
);
81 static void dnssdDeregisterInstance(cupsd_srv_t
*srv
);
82 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
,
84 static const char *dnssdErrorString(int error
);
85 static void dnssdFreeTxtRecord(cupsd_txt_t
*txt
);
87 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
88 DNSServiceFlags flags
,
89 DNSServiceErrorType errorCode
,
95 static void dnssdRegisterCallback(AvahiEntryGroup
*p
,
96 AvahiEntryGroupState state
,
98 # endif /* HAVE_DNSSD */
99 static int dnssdRegisterInstance(cupsd_srv_t
*srv
,
101 char *name
, const char *type
,
102 const char *subtypes
, int port
,
103 cupsd_txt_t
*txt
, int commit
);
104 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
105 static void dnssdStop(void);
107 static void dnssdUpdate(void);
108 # endif /* HAVE_DNSSD */
109 #endif /* HAVE_DNSSD || HAVE_AVAHI */
113 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
114 * local printer and remove any pending
115 * references to remote printers.
119 cupsdDeregisterPrinter(
120 cupsd_printer_t
*p
, /* I - Printer to register */
121 int removeit
) /* I - Printer being permanently removed */
124 * Only deregister if browsing is enabled and it's a local printer...
127 cupsdLogMessage(CUPSD_LOG_DEBUG
,
128 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
131 if (!Browsing
|| !p
->shared
||
132 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
136 * Announce the deletion...
139 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
140 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
141 dnssdDeregisterPrinter(p
, 1);
142 #endif /* HAVE_DNSSD || HAVE_AVAHI */
147 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
148 * printer or update the broadcast contents.
152 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
154 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
157 if (!Browsing
|| !BrowseLocalProtocols
||
158 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
161 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
162 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
163 dnssdRegisterPrinter(p
);
164 #endif /* HAVE_DNSSD || HAVE_AVAHI */
169 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
173 cupsdStartBrowsing(void)
175 cupsd_printer_t
*p
; /* Current printer */
178 if (!Browsing
|| !BrowseLocalProtocols
)
181 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
182 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
184 cupsd_listener_t
*lis
; /* Current listening socket */
186 DNSServiceErrorType error
; /* Error from service creation */
189 * First create a "master" connection for all registrations...
192 if ((error
= DNSServiceCreateConnection(&DNSSDMaster
))
193 != kDNSServiceErr_NoError
)
195 cupsdLogMessage(CUPSD_LOG_ERROR
,
196 "Unable to create master DNS-SD reference: %d", error
);
198 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
199 cupsdEndProcess(getpid(), 0);
204 * Add the master connection to the select list...
207 int fd
= DNSServiceRefSockFD(DNSSDMaster
);
209 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
211 cupsdAddSelect(fd
, (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
214 # else /* HAVE_AVAHI */
215 if ((DNSSDMaster
= avahi_threaded_poll_new()) == NULL
)
217 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create DNS-SD thread.");
219 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
220 cupsdEndProcess(getpid(), 0);
224 int error
; /* Error code, if any */
226 DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), 0,
229 if (DNSSDClient
== NULL
)
231 cupsdLogMessage(CUPSD_LOG_ERROR
,
232 "Unable to communicate with avahi-daemon: %s",
233 dnssdErrorString(error
));
235 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
236 cupsdEndProcess(getpid(), 0);
238 avahi_threaded_poll_free(DNSSDMaster
);
242 avahi_threaded_poll_start(DNSSDMaster
);
244 # endif /* HAVE_DNSSD */
247 * Then get the port we use for registrations. If we are not listening
248 * on any non-local ports, there is no sense sharing local printers via
254 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
256 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
258 if (httpAddrLocalhost(&(lis
->address
)))
261 DNSSDPort
= _httpAddrPort(&(lis
->address
));
266 * Set the computer name and register the web interface...
269 cupsdUpdateDNSSDName();
271 #endif /* HAVE_DNSSD || HAVE_AVAHI */
274 * Enable LPD and SMB printer sharing as needed through external programs...
277 if (BrowseLocalProtocols
& BROWSE_LPD
)
280 if (BrowseLocalProtocols
& BROWSE_SMB
)
284 * Register the individual printers
287 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
289 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
290 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
291 cupsdRegisterPrinter(p
);
296 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
300 cupsdStopBrowsing(void)
302 cupsd_printer_t
*p
; /* Current printer */
305 if (!Browsing
|| !BrowseLocalProtocols
)
309 * De-register the individual printers
312 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
314 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
315 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
316 cupsdDeregisterPrinter(p
, 1);
319 * Shut down browsing sockets...
322 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
323 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
325 #endif /* HAVE_DNSSD || HAVE_AVAHI */
328 * Disable LPD and SMB printer sharing as needed through external programs...
331 if (BrowseLocalProtocols
& BROWSE_LPD
)
334 if (BrowseLocalProtocols
& BROWSE_SMB
)
339 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
341 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
345 cupsdUpdateDNSSDName(void)
347 char webif
[1024]; /* Web interface share name */
349 SCDynamicStoreRef sc
; /* Context for dynamic store */
350 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
351 CFStringEncoding nameEncoding
; /* Encoding of computer name */
352 CFStringRef nameRef
; /* Host name CFString */
353 char nameBuffer
[1024]; /* C-string buffer */
354 # endif /* __APPLE__ */
358 * Only share the web interface and printers when non-local listening is
366 * Get the computer name as a c-string...
370 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
375 * Get the computer name from the dynamic store...
378 cupsdClearString(&DNSSDComputerName
);
380 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
382 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
383 kCFStringEncodingUTF8
))
385 cupsdLogMessage(CUPSD_LOG_DEBUG
,
386 "Dynamic store computer name is \"%s\".", nameBuffer
);
387 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
393 if (!DNSSDComputerName
)
396 * Use the ServerName instead...
399 cupsdLogMessage(CUPSD_LOG_DEBUG
,
400 "Using ServerName \"%s\" as computer name.", ServerName
);
401 cupsdSetString(&DNSSDComputerName
, ServerName
);
405 * Get the local hostname from the dynamic store...
408 cupsdClearString(&DNSSDHostName
);
410 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
412 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
413 kCFStringEncodingUTF8
))
415 cupsdLogMessage(CUPSD_LOG_DEBUG
,
416 "Dynamic store host name is \"%s\".", nameBuffer
);
417 cupsdSetString(&DNSSDHostName
, nameBuffer
);
426 * Use the ServerName instead...
429 cupsdLogMessage(CUPSD_LOG_DEBUG
,
430 "Using ServerName \"%s\" as host name.", ServerName
);
431 cupsdSetString(&DNSSDHostName
, ServerName
);
435 * Get any Back-to-My-Mac domains and add them as aliases...
438 cupsdFreeAliases(DNSSDAlias
);
441 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
442 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
444 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
445 (int)CFDictionaryGetCount(btmm
));
446 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
449 cupsdLogMessage(CUPSD_LOG_ERROR
,
450 "Bad Back to My Mac data in dynamic store!");
452 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
460 # endif /* __APPLE__ */
463 const char *host_name
= avahi_client_get_host_name(DNSSDClient
);
464 const char *host_fqdn
= avahi_client_get_host_name_fqdn(DNSSDClient
);
466 cupsdSetString(&DNSSDComputerName
, host_name
? host_name
: ServerName
);
469 cupsdSetString(&DNSSDHostName
, host_fqdn
);
470 else if (strchr(ServerName
, '.'))
471 cupsdSetString(&DNSSDHostName
, ServerName
);
473 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
475 # else /* HAVE_DNSSD */
477 cupsdSetString(&DNSSDComputerName
, ServerName
);
479 if (strchr(ServerName
, '.'))
480 cupsdSetString(&DNSSDHostName
, ServerName
);
482 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
484 # endif /* HAVE_AVAHI */
487 * Then (re)register the web interface if enabled...
492 if (DNSSDComputerName
)
493 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
495 strlcpy(webif
, "CUPS", sizeof(webif
));
497 dnssdDeregisterInstance(&WebIFSrv
);
498 dnssdRegisterInstance(&WebIFSrv
, NULL
, webif
, "_http._tcp", "_printer",
506 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
510 dnssdAddAlias(const void *key
, /* I - Key */
511 const void *value
, /* I - Value (domain) */
512 void *context
) /* I - Unused */
514 char valueStr
[1024], /* Domain string */
515 hostname
[1024], /* Complete hostname */
516 *hostptr
; /* Pointer into hostname */
522 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
523 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
524 kCFStringEncodingUTF8
))
526 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
527 hostptr
= hostname
+ strlen(hostname
) - 1;
529 *hostptr
= '\0'; /* Strip trailing dot */
532 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
534 cupsdAddAlias(DNSSDAlias
, hostname
);
535 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
539 cupsdLogMessage(CUPSD_LOG_ERROR
,
540 "Bad Back to My Mac domain in dynamic store!");
542 # endif /* __APPLE__ */
546 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
549 static cupsd_txt_t
/* O - TXT record */
551 cupsd_printer_t
*p
, /* I - Printer information */
552 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
554 int i
, /* Looping var */
555 count
; /* Count of key/value pairs */
556 char admin_hostname
[256], /* .local hostname for admin page */
557 adminurl_str
[256], /* URL for the admin page */
558 type_str
[32], /* Type to string buffer */
559 state_str
[32], /* State to string buffer */
560 rp_str
[1024], /* Queue name string buffer */
561 air_str
[1024], /* auth-info-required string buffer */
562 *keyvalue
[32][2]; /* Table of key/value pairs */
563 cupsd_txt_t txt
; /* TXT record */
567 * Load up the key value pairs...
572 if (!for_lpd
|| (BrowseLocalProtocols
& BROWSE_LPD
))
574 keyvalue
[count
][0] = "txtvers";
575 keyvalue
[count
++][1] = "1";
577 keyvalue
[count
][0] = "qtotal";
578 keyvalue
[count
++][1] = "1";
580 keyvalue
[count
][0] = "rp";
581 keyvalue
[count
++][1] = rp_str
;
583 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
585 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
586 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
589 keyvalue
[count
][0] = "ty";
590 keyvalue
[count
++][1] = p
->make_model
? p
->make_model
: "Unknown";
592 if (strstr(DNSSDHostName
, ".local"))
593 strlcpy(admin_hostname
, DNSSDHostName
, sizeof(admin_hostname
));
595 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.local.",
597 httpAssembleURIf(HTTP_URI_CODING_ALL
, adminurl_str
, sizeof(adminurl_str
),
602 # endif /* HAVE_SSL */
603 NULL
, admin_hostname
, DNSSDPort
, "/%s/%s",
604 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
606 keyvalue
[count
][0] = "adminurl";
607 keyvalue
[count
++][1] = adminurl_str
;
611 keyvalue
[count
][0] = "note";
612 keyvalue
[count
++][1] = p
->location
;
615 keyvalue
[count
][0] = "priority";
616 keyvalue
[count
++][1] = for_lpd
? "100" : "0";
618 keyvalue
[count
][0] = "product";
619 keyvalue
[count
++][1] = p
->pc
&& p
->pc
->product
? p
->pc
->product
: "Unknown";
621 keyvalue
[count
][0] = "pdl";
622 keyvalue
[count
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
624 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
626 keyvalue
[count
][0] = "air";
627 keyvalue
[count
++][1] = air_str
;
630 keyvalue
[count
][0] = "UUID";
631 keyvalue
[count
++][1] = p
->uuid
+ 9;
634 keyvalue
[count
][0] = "TLS";
635 keyvalue
[count
++][1] = "1.2";
636 #endif /* HAVE_SSL */
638 if (p
->type
& CUPS_PRINTER_FAX
)
640 keyvalue
[count
][0] = "Fax";
641 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_FAX
) ? "T" : "F";
644 if (p
->type
& CUPS_PRINTER_COLOR
)
646 keyvalue
[count
][0] = "Color";
647 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLOR
) ? "T" : "F";
650 if (p
->type
& CUPS_PRINTER_DUPLEX
)
652 keyvalue
[count
][0] = "Duplex";
653 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_DUPLEX
) ? "T" : "F";
656 if (p
->type
& CUPS_PRINTER_STAPLE
)
658 keyvalue
[count
][0] = "Staple";
659 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_STAPLE
) ? "T" : "F";
662 if (p
->type
& CUPS_PRINTER_COPIES
)
664 keyvalue
[count
][0] = "Copies";
665 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COPIES
) ? "T" : "F";
668 if (p
->type
& CUPS_PRINTER_COLLATE
)
670 keyvalue
[count
][0] = "Collate";
671 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLLATE
) ? "T" : "F";
674 if (p
->type
& CUPS_PRINTER_PUNCH
)
676 keyvalue
[count
][0] = "Punch";
677 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_PUNCH
) ? "T" : "F";
680 if (p
->type
& CUPS_PRINTER_BIND
)
682 keyvalue
[count
][0] = "Bind";
683 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_BIND
) ? "T" : "F";
686 if (p
->type
& CUPS_PRINTER_SORT
)
688 keyvalue
[count
][0] = "Sort";
689 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_SORT
) ? "T" : "F";
692 if (p
->type
& CUPS_PRINTER_MFP
)
694 keyvalue
[count
][0] = "Scan";
695 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_MFP
) ? "T" : "F";
698 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
699 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
701 keyvalue
[count
][0] = "printer-state";
702 keyvalue
[count
++][1] = state_str
;
704 keyvalue
[count
][0] = "printer-type";
705 keyvalue
[count
++][1] = type_str
;
709 * Then pack them into a proper txt record...
713 TXTRecordCreate(&txt
, 0, NULL
);
715 for (i
= 0; i
< count
; i
++)
717 size_t len
= strlen(keyvalue
[i
][1]);
720 TXTRecordSetValue(&txt
, keyvalue
[i
][0], (uint8_t)len
, keyvalue
[i
][1]);
724 for (i
= 0, txt
= NULL
; i
< count
; i
++)
725 txt
= avahi_string_list_add_printf(txt
, "%s=%s", keyvalue
[i
][0],
727 # endif /* HAVE_DNSSD */
734 * 'dnssdDeregisterInstance()' - Deregister a DNS-SD service instance.
738 dnssdDeregisterInstance(
739 cupsd_srv_t
*srv
) /* I - Service */
745 DNSServiceRefDeallocate(*srv
);
747 # else /* HAVE_AVAHI */
748 avahi_threaded_poll_lock(DNSSDMaster
);
749 avahi_entry_group_free(*srv
);
750 avahi_threaded_poll_unlock(DNSSDMaster
);
751 # endif /* HAVE_DNSSD */
758 * 'dnssdDeregisterPrinter()' - Deregister all services for a printer.
762 dnssdDeregisterPrinter(
763 cupsd_printer_t
*p
, /* I - Printer */
764 int clear_name
) /* I - Clear the name? */
767 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
768 "dnssdDeregisterPrinter(p=%p(%s), clear_name=%d)", p
, p
->name
,
773 dnssdDeregisterInstance(&p
->ipp_srv
);
777 dnssdDeregisterInstance(&p
->ipps_srv
);
778 # endif /* HAVE_SSL */
779 dnssdDeregisterInstance(&p
->printer_srv
);
780 # endif /* HAVE_DNSSD */
784 * Remove the printer from the array of DNS-SD printers but keep the
788 cupsArrayRemove(DNSSDPrinters
, p
);
791 * Optionally clear the service name...
795 cupsdClearString(&p
->reg_name
);
800 * 'dnssdErrorString()' - Return an error string for an error code.
803 static const char * /* O - Error message */
804 dnssdErrorString(int error
) /* I - Error number */
809 case kDNSServiceErr_NoError
:
813 case kDNSServiceErr_Unknown
:
814 return ("Unknown error.");
816 case kDNSServiceErr_NoSuchName
:
817 return ("Service not found.");
819 case kDNSServiceErr_NoMemory
:
820 return ("Out of memory.");
822 case kDNSServiceErr_BadParam
:
823 return ("Bad parameter.");
825 case kDNSServiceErr_BadReference
:
826 return ("Bad service reference.");
828 case kDNSServiceErr_BadState
:
829 return ("Bad state.");
831 case kDNSServiceErr_BadFlags
:
832 return ("Bad flags.");
834 case kDNSServiceErr_Unsupported
:
835 return ("Unsupported.");
837 case kDNSServiceErr_NotInitialized
:
838 return ("Not initialized.");
840 case kDNSServiceErr_AlreadyRegistered
:
841 return ("Already registered.");
843 case kDNSServiceErr_NameConflict
:
844 return ("Name conflict.");
846 case kDNSServiceErr_Invalid
:
847 return ("Invalid name.");
849 case kDNSServiceErr_Firewall
:
850 return ("Firewall prevents registration.");
852 case kDNSServiceErr_Incompatible
:
853 return ("Client library incompatible.");
855 case kDNSServiceErr_BadInterfaceIndex
:
856 return ("Bad interface index.");
858 case kDNSServiceErr_Refused
:
859 return ("Server prevents registration.");
861 case kDNSServiceErr_NoSuchRecord
:
862 return ("Record not found.");
864 case kDNSServiceErr_NoAuth
:
865 return ("Authentication required.");
867 case kDNSServiceErr_NoSuchKey
:
868 return ("Encryption key not found.");
870 case kDNSServiceErr_NATTraversal
:
871 return ("Unable to traverse NAT boundary.");
873 case kDNSServiceErr_DoubleNAT
:
874 return ("Unable to traverse double-NAT boundary.");
876 case kDNSServiceErr_BadTime
:
877 return ("Bad system time.");
879 case kDNSServiceErr_BadSig
:
880 return ("Bad signature.");
882 case kDNSServiceErr_BadKey
:
883 return ("Bad encryption key.");
885 case kDNSServiceErr_Transient
:
886 return ("Transient error occurred - please try again.");
888 case kDNSServiceErr_ServiceNotRunning
:
889 return ("Server not running.");
891 case kDNSServiceErr_NATPortMappingUnsupported
:
892 return ("NAT doesn't support NAT-PMP or UPnP.");
894 case kDNSServiceErr_NATPortMappingDisabled
:
895 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
897 case kDNSServiceErr_NoRouter
:
898 return ("No Internet/default router configured.");
900 case kDNSServiceErr_PollingMode
:
901 return ("Service polling mode error.");
903 case kDNSServiceErr_Timeout
:
904 return ("Service timeout.");
907 # else /* HAVE_AVAHI */
908 return (avahi_strerror(error
));
909 # endif /* HAVE_DNSSD */
914 * 'dnssdRegisterCallback()' - Free a TXT record.
918 dnssdFreeTxtRecord(cupsd_txt_t
*txt
) /* I - TXT record */
921 TXTRecordDeallocate(txt
);
923 # else /* HAVE_AVAHI */
924 avahi_string_list_free(*txt
);
926 # endif /* HAVE_DNSSD */
931 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
936 dnssdRegisterCallback(
937 DNSServiceRef sdRef
, /* I - DNS Service reference */
938 DNSServiceFlags flags
, /* I - Reserved for future use */
939 DNSServiceErrorType errorCode
, /* I - Error code */
940 const char *name
, /* I - Service name */
941 const char *regtype
, /* I - Service type */
942 const char *domain
, /* I - Domain. ".local" for now */
943 void *context
) /* I - Printer */
945 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
946 /* Current printer */
953 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
954 name
, regtype
, p
? p
->name
: "Web Interface",
955 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
959 cupsdLogMessage(CUPSD_LOG_ERROR
,
960 "DNSServiceRegister failed with error %d", (int)errorCode
);
963 else if (p
&& (!p
->reg_name
|| _cups_strcasecmp(name
, p
->reg_name
)))
965 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
968 cupsArrayRemove(DNSSDPrinters
, p
);
969 cupsdSetString(&p
->reg_name
, name
);
970 cupsArrayAdd(DNSSDPrinters
, p
);
972 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
976 # else /* HAVE_AVAHI */
978 dnssdRegisterCallback(
979 AvahiEntryGroup
*srv
, /* I - Service */
980 AvahiEntryGroupState state
, /* I - Registration state */
981 void *context
) /* I - Printer */
983 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
984 /* Current printer */
986 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
987 "dnssdRegisterCallback(srv=%p, state=%d, context=%p) "
988 "for %s (%s)", srv
, state
, context
,
989 p
? p
->name
: "Web Interface",
990 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
992 /* TODO: Handle collisions with avahi_alternate_service_name(p->reg_name)? */
994 # endif /* HAVE_DNSSD */
998 * 'dnssdRegisterInstance()' - Register an instance of a printer service.
1001 static int /* O - 1 on success, 0 on failure */
1002 dnssdRegisterInstance(
1003 cupsd_srv_t
*srv
, /* O - Service */
1004 cupsd_printer_t
*p
, /* I - Printer */
1005 char *name
, /* I - DNS-SD service name */
1006 const char *type
, /* I - DNS-SD service type */
1007 const char *subtypes
, /* I - Subtypes to register or NULL */
1008 int port
, /* I - Port number or 0 */
1009 cupsd_txt_t
*txt
, /* I - TXT record */
1010 int commit
) /* I - Commit registration? */
1012 char temp
[256], /* Temporary string */
1013 *ptr
; /* Pointer into string */
1014 int error
; /* Any error */
1017 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1018 "Registering \"%s\" with DNS-SD type \"%s\".", name
, type
);
1023 * Assign the correct pointer for "srv"...
1027 if (!strcmp(type
, "_printer._tcp"))
1028 srv
= &p
->printer_srv
; /* Target LPD service */
1030 else if (!strcmp(type
, "_ipps._tcp"))
1031 srv
= &p
->ipps_srv
; /* Target IPPS service */
1032 # endif /* HAVE_SSL */
1034 srv
= &p
->ipp_srv
; /* Target IPP service */
1036 # else /* HAVE_AVAHI */
1037 srv
= &p
->ipp_srv
; /* Target service group */
1038 # endif /* HAVE_DNSSD */
1044 # else /* HAVE_AVAHI */
1045 avahi_threaded_poll_lock(DNSSDMaster
);
1048 *srv
= avahi_entry_group_new(DNSSDClient
, dnssdRegisterCallback
, NULL
);
1051 avahi_threaded_poll_unlock(DNSSDMaster
);
1053 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1054 name
, dnssdErrorString(avahi_client_errno(DNSSDClient
)));
1057 # endif /* HAVE_DNSSD */
1060 * Make sure the name is <= 63 octets, and when we truncate be sure to
1061 * properly truncate any UTF-8 characters...
1064 ptr
= name
+ strlen(name
);
1065 while ((ptr
- name
) > 63)
1071 while (ptr
> name
&& (*ptr
& 0xc0) == 0x80);
1078 * Register the service...
1083 snprintf(temp
, sizeof(temp
), "%s,%s", type
, subtypes
);
1085 strlcpy(temp
, type
, sizeof(temp
));
1088 error
= DNSServiceRegister(srv
, kDNSServiceFlagsShareConnection
,
1089 0, name
, temp
, NULL
, NULL
, htons(port
),
1090 txt
? TXTRecordGetLength(txt
) : 0,
1091 txt
? TXTRecordGetBytesPtr(txt
) : NULL
,
1092 dnssdRegisterCallback
, p
);
1094 # else /* HAVE_AVAHI */
1097 AvahiStringList
*temptxt
;
1098 for (temptxt
= *txt
; temptxt
; temptxt
= temptxt
->next
)
1099 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS_SD \"%s\" %s", name
, temptxt
->text
);
1102 error
= avahi_entry_group_add_service_strlst(*srv
, AVAHI_IF_UNSPEC
,
1103 AVAHI_PROTO_UNSPEC
, 0, name
,
1104 type
, NULL
, NULL
, port
,
1107 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD service add for \"%s\" failed.",
1110 if (!error
&& subtypes
)
1113 * Register all of the subtypes...
1116 char *start
, /* Start of subtype */
1117 subtype
[256]; /* Subtype string */
1119 strlcpy(temp
, subtypes
, sizeof(temp
));
1121 for (start
= temp
; *start
; start
= ptr
)
1124 * Skip leading whitespace...
1127 while (*start
&& isspace(*start
& 255))
1131 * Grab everything up to the next comma or the end of the string...
1134 for (ptr
= start
; *ptr
&& *ptr
!= ','; ptr
++);
1143 * Register the subtype...
1146 snprintf(subtype
, sizeof(subtype
), "%s._sub.%s", start
, type
);
1148 error
= avahi_entry_group_add_service_subtype(*srv
, AVAHI_IF_UNSPEC
,
1149 AVAHI_PROTO_UNSPEC
, 0,
1150 name
, type
, NULL
, subtype
);
1153 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1154 "DNS-SD subtype %s registration for \"%s\" failed." ,
1161 if (!error
&& commit
)
1163 if ((error
= avahi_entry_group_commit(*srv
)) != 0)
1164 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD commit of \"%s\" failed.",
1168 avahi_threaded_poll_unlock(DNSSDMaster
);
1169 # endif /* HAVE_DNSSD */
1173 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1174 name
, dnssdErrorString(error
));
1175 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD type: %s", type
);
1177 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD sub-types: %s", subtypes
);
1185 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
1186 * or update the broadcast contents.
1190 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
1192 char name
[256]; /* Service name */
1193 int printer_port
; /* LPD port number */
1194 int status
; /* Registration status */
1195 cupsd_txt_t ipp_txt
, /* IPP(S) TXT record */
1196 printer_txt
; /* LPD TXT record */
1198 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
1199 !p
->ipp_srv
? "new" : "update");
1202 * Remove the current registrations if we have them and then return if
1203 * per-printer sharing was just disabled...
1206 dnssdDeregisterPrinter(p
, 0);
1212 * Set the registered name as needed; the registered name takes the form of
1213 * "<printer-info> @ <computer name>"...
1218 if (p
->info
&& strlen(p
->info
) > 0)
1220 if (DNSSDComputerName
)
1221 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
1223 strlcpy(name
, p
->info
, sizeof(name
));
1225 else if (DNSSDComputerName
)
1226 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
1228 strlcpy(name
, p
->name
, sizeof(name
));
1231 strlcpy(name
, p
->reg_name
, sizeof(name
));
1234 * Register IPP and LPD...
1236 * We always must register the "_printer" service type in order to reserve
1237 * our name, but use port number 0 if we haven't actually configured cups-lpd
1238 * to share via LPD...
1241 ipp_txt
= dnssdBuildTxtRecord(p
, 0);
1242 printer_txt
= dnssdBuildTxtRecord(p
, 1);
1244 if (BrowseLocalProtocols
& BROWSE_LPD
)
1249 status
= dnssdRegisterInstance(NULL
, p
, name
, "_printer._tcp", NULL
,
1250 printer_port
, &printer_txt
, 0);
1254 dnssdRegisterInstance(NULL
, p
, name
, "_ipps._tcp", DNSSDSubTypes
,
1255 DNSSDPort
, &ipp_txt
, 0);
1256 # endif /* HAVE_SSL */
1261 * Use the "_fax-ipp" service type for fax queues, otherwise use "_ipp"...
1264 if (p
->type
& CUPS_PRINTER_FAX
)
1265 status
= dnssdRegisterInstance(NULL
, p
, name
, "_fax-ipp._tcp",
1266 DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1);
1268 status
= dnssdRegisterInstance(NULL
, p
, name
, "_ipp._tcp", DNSSDSubTypes
,
1269 DNSSDPort
, &ipp_txt
, 1);
1272 dnssdFreeTxtRecord(&ipp_txt
);
1273 dnssdFreeTxtRecord(&printer_txt
);
1278 * Save the registered name and add the printer to the array of DNS-SD
1282 cupsdSetString(&p
->reg_name
, name
);
1283 cupsArrayAdd(DNSSDPrinters
, p
);
1288 * Registration failed for this printer...
1291 dnssdDeregisterInstance(&p
->ipp_srv
);
1295 dnssdDeregisterInstance(&p
->ipps_srv
);
1296 # endif /* HAVE_SSL */
1297 dnssdDeregisterInstance(&p
->printer_srv
);
1298 # endif /* HAVE_DNSSD */
1304 * 'dnssdStop()' - Stop all DNS-SD registrations.
1310 cupsd_printer_t
*p
; /* Current printer */
1314 * De-register the individual printers
1317 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1319 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1320 dnssdDeregisterPrinter(p
, 1);
1323 * Shutdown the rest of the service refs...
1326 dnssdDeregisterInstance(&WebIFSrv
);
1329 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDMaster
));
1331 DNSServiceRefDeallocate(DNSSDMaster
);
1334 # else /* HAVE_AVAHI */
1335 avahi_client_free(DNSSDClient
);
1338 avahi_threaded_poll_free(DNSSDMaster
);
1340 # endif /* HAVE_DNSSD */
1342 cupsArrayDelete(DNSSDPrinters
);
1343 DNSSDPrinters
= NULL
;
1351 * 'dnssdUpdate()' - Handle DNS-SD queries.
1357 DNSServiceErrorType sdErr
; /* Service discovery error */
1360 if ((sdErr
= DNSServiceProcessResult(DNSSDMaster
)) != kDNSServiceErr_NoError
)
1362 cupsdLogMessage(CUPSD_LOG_ERROR
,
1363 "DNS Service Discovery registration error %d!",
1368 # endif /* HAVE_DNSSD */
1372 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
1375 static char * /* O - String or NULL if none */
1376 get_auth_info_required(
1377 cupsd_printer_t
*p
, /* I - Printer */
1378 char *buffer
, /* I - Value buffer */
1379 size_t bufsize
) /* I - Size of value buffer */
1381 cupsd_location_t
*auth
; /* Pointer to authentication element */
1382 char resource
[1024]; /* Printer/class resource path */
1386 * If auth-info-required is set for this printer, return that...
1389 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
1391 int i
; /* Looping var */
1392 char *bufptr
; /* Pointer into buffer */
1394 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
1396 if (bufptr
>= (buffer
+ bufsize
- 2))
1402 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
1403 bufptr
+= strlen(bufptr
);
1410 * Figure out the authentication data requirements to advertise...
1413 if (p
->type
& CUPS_PRINTER_CLASS
)
1414 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1416 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1418 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
1419 auth
->type
== CUPSD_AUTH_NONE
)
1420 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1424 int auth_type
; /* Authentication type */
1426 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
1427 auth_type
= cupsdDefaultAuthType();
1431 case CUPSD_AUTH_NONE
:
1434 case CUPSD_AUTH_NEGOTIATE
:
1435 strlcpy(buffer
, "negotiate", bufsize
);
1439 strlcpy(buffer
, "username,password", bufsize
);
1448 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1453 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
1456 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
1457 get_hostconfig(const char *name
) /* I - Name of service */
1459 cups_file_t
*fp
; /* Hostconfig file */
1460 char line
[1024], /* Line from file */
1461 *ptr
; /* Pointer to value */
1462 int state
= 1; /* State of service */
1466 * Try opening the /etc/hostconfig file; if we can't open it, assume that
1467 * the service is enabled/auto.
1470 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
1473 * Read lines from the file until we find the service...
1476 while (cupsFileGets(fp
, line
, sizeof(line
)))
1478 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
1483 if (!_cups_strcasecmp(line
, name
))
1486 * Found the service, see if it is set to "-NO-"...
1489 if (!_cups_strncasecmp(ptr
, "-NO-", 4))
1500 #endif /* __APPLE__ */
1504 * 'update_lpd()' - Update the LPD configuration as needed.
1508 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
1515 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
1516 * setting for backwards-compatibility.
1519 if (onoff
&& !get_hostconfig("CUPS_LPD"))
1521 #endif /* __APPLE__ */
1523 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
1526 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
1529 char newfile
[1024]; /* New cups-lpd.N file */
1530 cups_file_t
*ofp
, /* Original file pointer */
1531 *nfp
; /* New file pointer */
1532 char line
[1024]; /* Line from file */
1535 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
1537 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
1539 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1540 LPDConfigFile
+ 9, strerror(errno
));
1544 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1546 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1547 newfile
, strerror(errno
));
1553 * Copy all of the lines from the cups-lpd file...
1556 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1560 cupsFilePrintf(nfp
, "%s\n", line
);
1561 snprintf(line
, sizeof(line
), "\tdisable = %s",
1562 onoff
? "no" : "yes");
1564 else if (!strstr(line
, "disable ="))
1565 cupsFilePrintf(nfp
, "%s\n", line
);
1570 rename(newfile
, LPDConfigFile
+ 9);
1573 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
1576 * Enable/disable LPD via the launchctl command...
1579 char *argv
[5], /* Arguments for command */
1580 *envp
[MAX_ENV
]; /* Environment for command */
1581 int pid
; /* Process ID */
1584 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1585 argv
[0] = (char *)"launchctl";
1586 argv
[1] = (char *)(onoff
? "load" : "unload");
1587 argv
[2] = (char *)"-w";
1588 argv
[3] = LPDConfigFile
+ 10;
1591 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
1594 #endif /* __APPLE__ */
1596 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
1601 * 'update_smb()' - Update the SMB configuration as needed.
1605 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
1610 if (!strncmp(SMBConfigFile
, "samba:///", 9))
1613 * Enable/disable SMB via the specified smb.conf config file...
1616 char newfile
[1024]; /* New smb.conf.N file */
1617 cups_file_t
*ofp
, /* Original file pointer */
1618 *nfp
; /* New file pointer */
1619 char line
[1024]; /* Line from file */
1620 int in_printers
; /* In [printers] section? */
1623 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
1625 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
1627 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1628 SMBConfigFile
+ 8, strerror(errno
));
1632 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1634 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1635 newfile
, strerror(errno
));
1641 * Copy all of the lines from the smb.conf file...
1646 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1648 if (in_printers
&& strstr(line
, "printable ="))
1649 snprintf(line
, sizeof(line
), " printable = %s",
1650 onoff
? "yes" : "no");
1652 cupsFilePrintf(nfp
, "%s\n", line
);
1655 in_printers
= !strcmp(line
, "[printers]");
1660 rename(newfile
, SMBConfigFile
+ 8);
1663 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
1668 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".