4 * Directory services routines for the CUPS scheduler.
6 * Copyright 2007-2014 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 * Include necessary headers...
23 #if defined(HAVE_DNSSD) && defined(__APPLE__)
25 # include <CoreFoundation/CoreFoundation.h>
26 # include <SystemConfiguration/SystemConfiguration.h>
27 #endif /* HAVE_DNSSD && __APPLE__ */
34 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
35 static char *get_auth_info_required(cupsd_printer_t
*p
,
36 char *buffer
, size_t bufsize
);
37 #endif /* HAVE_DNSSD || HAVE_AVAHI */
39 static int get_hostconfig(const char *name
);
40 #endif /* __APPLE__ */
41 static void update_lpd(int onoff
);
42 static void update_smb(int onoff
);
45 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
47 static void dnssdAddAlias(const void *key
, const void *value
,
49 # endif /* __APPLE__ */
50 static cupsd_txt_t
dnssdBuildTxtRecord(cupsd_printer_t
*p
, int for_lpd
);
51 static void dnssdDeregisterInstance(cupsd_srv_t
*srv
);
52 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
,
54 static const char *dnssdErrorString(int error
);
55 static void dnssdFreeTxtRecord(cupsd_txt_t
*txt
);
57 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
58 DNSServiceFlags flags
,
59 DNSServiceErrorType errorCode
,
65 static void dnssdRegisterCallback(AvahiEntryGroup
*p
,
66 AvahiEntryGroupState state
,
68 # endif /* HAVE_DNSSD */
69 static int dnssdRegisterInstance(cupsd_srv_t
*srv
,
71 char *name
, const char *type
,
72 const char *subtypes
, int port
,
73 cupsd_txt_t
*txt
, int commit
);
74 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
75 static void dnssdStop(void);
77 static void dnssdUpdate(void);
78 # endif /* HAVE_DNSSD */
79 #endif /* HAVE_DNSSD || HAVE_AVAHI */
83 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
84 * local printer and remove any pending
85 * references to remote printers.
89 cupsdDeregisterPrinter(
90 cupsd_printer_t
*p
, /* I - Printer to register */
91 int removeit
) /* I - Printer being permanently removed */
94 * Only deregister if browsing is enabled and it's a local printer...
97 cupsdLogMessage(CUPSD_LOG_DEBUG
,
98 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
101 if (!Browsing
|| !p
->shared
||
102 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
106 * Announce the deletion...
109 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
110 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
111 dnssdDeregisterPrinter(p
, 1);
112 #endif /* HAVE_DNSSD || HAVE_AVAHI */
117 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
118 * printer or update the broadcast contents.
122 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
124 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
127 if (!Browsing
|| !BrowseLocalProtocols
||
128 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
131 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
132 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
133 dnssdRegisterPrinter(p
);
134 #endif /* HAVE_DNSSD || HAVE_AVAHI */
139 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
143 cupsdStartBrowsing(void)
145 cupsd_printer_t
*p
; /* Current printer */
148 if (!Browsing
|| !BrowseLocalProtocols
)
151 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
152 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
154 cupsd_listener_t
*lis
; /* Current listening socket */
156 DNSServiceErrorType error
; /* Error from service creation */
159 * First create a "master" connection for all registrations...
162 if ((error
= DNSServiceCreateConnection(&DNSSDMaster
))
163 != kDNSServiceErr_NoError
)
165 cupsdLogMessage(CUPSD_LOG_ERROR
,
166 "Unable to create master DNS-SD reference: %d", error
);
168 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
169 cupsdEndProcess(getpid(), 0);
174 * Add the master connection to the select list...
177 int fd
= DNSServiceRefSockFD(DNSSDMaster
);
179 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
181 cupsdAddSelect(fd
, (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
184 # else /* HAVE_AVAHI */
185 if ((DNSSDMaster
= avahi_threaded_poll_new()) == NULL
)
187 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create DNS-SD thread.");
189 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
190 cupsdEndProcess(getpid(), 0);
194 int error
; /* Error code, if any */
196 DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), 0,
199 if (DNSSDClient
== NULL
)
201 cupsdLogMessage(CUPSD_LOG_ERROR
,
202 "Unable to communicate with avahi-daemon: %s",
203 dnssdErrorString(error
));
205 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
206 cupsdEndProcess(getpid(), 0);
208 avahi_threaded_poll_free(DNSSDMaster
);
212 avahi_threaded_poll_start(DNSSDMaster
);
214 # endif /* HAVE_DNSSD */
217 * Then get the port we use for registrations. If we are not listening
218 * on any non-local ports, there is no sense sharing local printers via
224 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
226 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
228 if (httpAddrLocalhost(&(lis
->address
)))
231 DNSSDPort
= httpAddrPort(&(lis
->address
));
236 * Set the computer name and register the web interface...
239 cupsdUpdateDNSSDName();
241 #endif /* HAVE_DNSSD || HAVE_AVAHI */
244 * Enable LPD and SMB printer sharing as needed through external programs...
247 if (BrowseLocalProtocols
& BROWSE_LPD
)
250 if (BrowseLocalProtocols
& BROWSE_SMB
)
254 * Register the individual printers
257 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
259 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
260 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
261 cupsdRegisterPrinter(p
);
266 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
270 cupsdStopBrowsing(void)
272 cupsd_printer_t
*p
; /* Current printer */
275 if (!Browsing
|| !BrowseLocalProtocols
)
279 * De-register the individual printers
282 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
284 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
285 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
286 cupsdDeregisterPrinter(p
, 1);
289 * Shut down browsing sockets...
292 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
293 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
295 #endif /* HAVE_DNSSD || HAVE_AVAHI */
298 * Disable LPD and SMB printer sharing as needed through external programs...
301 if (BrowseLocalProtocols
& BROWSE_LPD
)
304 if (BrowseLocalProtocols
& BROWSE_SMB
)
309 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
311 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
315 cupsdUpdateDNSSDName(void)
317 char webif
[1024]; /* Web interface share name */
319 SCDynamicStoreRef sc
; /* Context for dynamic store */
320 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
321 CFStringEncoding nameEncoding
; /* Encoding of computer name */
322 CFStringRef nameRef
; /* Host name CFString */
323 char nameBuffer
[1024]; /* C-string buffer */
324 # endif /* __APPLE__ */
328 * Only share the web interface and printers when non-local listening is
336 * Get the computer name as a c-string...
340 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
345 * Get the computer name from the dynamic store...
348 cupsdClearString(&DNSSDComputerName
);
350 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
352 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
353 kCFStringEncodingUTF8
))
355 cupsdLogMessage(CUPSD_LOG_DEBUG
,
356 "Dynamic store computer name is \"%s\".", nameBuffer
);
357 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
363 if (!DNSSDComputerName
)
366 * Use the ServerName instead...
369 cupsdLogMessage(CUPSD_LOG_DEBUG
,
370 "Using ServerName \"%s\" as computer name.", ServerName
);
371 cupsdSetString(&DNSSDComputerName
, ServerName
);
375 * Get the local hostname from the dynamic store...
378 cupsdClearString(&DNSSDHostName
);
380 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
382 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
383 kCFStringEncodingUTF8
))
385 cupsdLogMessage(CUPSD_LOG_DEBUG
,
386 "Dynamic store host name is \"%s\".", nameBuffer
);
387 cupsdSetString(&DNSSDHostName
, nameBuffer
);
396 * Use the ServerName instead...
399 cupsdLogMessage(CUPSD_LOG_DEBUG
,
400 "Using ServerName \"%s\" as host name.", ServerName
);
401 cupsdSetString(&DNSSDHostName
, ServerName
);
405 * Get any Back-to-My-Mac domains and add them as aliases...
408 cupsdFreeAliases(DNSSDAlias
);
411 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
412 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
414 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
415 (int)CFDictionaryGetCount(btmm
));
416 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
419 cupsdLogMessage(CUPSD_LOG_ERROR
,
420 "Bad Back to My Mac data in dynamic store!");
422 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
430 # endif /* __APPLE__ */
434 const char *host_name
= avahi_client_get_host_name(DNSSDClient
);
435 const char *host_fqdn
= avahi_client_get_host_name_fqdn(DNSSDClient
);
437 cupsdSetString(&DNSSDComputerName
, host_name
? host_name
: ServerName
);
440 cupsdSetString(&DNSSDHostName
, host_fqdn
);
441 else if (strchr(ServerName
, '.'))
442 cupsdSetString(&DNSSDHostName
, ServerName
);
444 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
447 # endif /* HAVE_AVAHI */
449 cupsdSetString(&DNSSDComputerName
, ServerName
);
451 if (strchr(ServerName
, '.'))
452 cupsdSetString(&DNSSDHostName
, ServerName
);
454 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
458 * Then (re)register the web interface if enabled...
463 if (DNSSDComputerName
)
464 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
466 strlcpy(webif
, "CUPS", sizeof(webif
));
468 dnssdDeregisterInstance(&WebIFSrv
);
469 dnssdRegisterInstance(&WebIFSrv
, NULL
, webif
, "_http._tcp", "_printer",
477 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
481 dnssdAddAlias(const void *key
, /* I - Key */
482 const void *value
, /* I - Value (domain) */
483 void *context
) /* I - Unused */
485 char valueStr
[1024], /* Domain string */
486 hostname
[1024], /* Complete hostname */
487 *hostptr
; /* Pointer into hostname */
493 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
494 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
495 kCFStringEncodingUTF8
))
497 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
498 hostptr
= hostname
+ strlen(hostname
) - 1;
500 *hostptr
= '\0'; /* Strip trailing dot */
503 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
505 cupsdAddAlias(DNSSDAlias
, hostname
);
506 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
510 cupsdLogMessage(CUPSD_LOG_ERROR
,
511 "Bad Back to My Mac domain in dynamic store!");
513 # endif /* __APPLE__ */
517 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
520 static cupsd_txt_t
/* O - TXT record */
522 cupsd_printer_t
*p
, /* I - Printer information */
523 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
525 int i
, /* Looping var */
526 count
; /* Count of key/value pairs */
527 char admin_hostname
[256], /* .local hostname for admin page */
528 adminurl_str
[256], /* URL for the admin page */
529 type_str
[32], /* Type to string buffer */
530 state_str
[32], /* State to string buffer */
531 rp_str
[1024], /* Queue name string buffer */
532 air_str
[1024], /* auth-info-required string buffer */
533 *keyvalue
[32][2]; /* Table of key/value pairs */
534 cupsd_txt_t txt
; /* TXT record */
538 * Load up the key value pairs...
543 if (!for_lpd
|| (BrowseLocalProtocols
& BROWSE_LPD
))
545 keyvalue
[count
][0] = "txtvers";
546 keyvalue
[count
++][1] = "1";
548 keyvalue
[count
][0] = "qtotal";
549 keyvalue
[count
++][1] = "1";
551 keyvalue
[count
][0] = "rp";
552 keyvalue
[count
++][1] = rp_str
;
554 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
556 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
557 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
560 keyvalue
[count
][0] = "ty";
561 keyvalue
[count
++][1] = p
->make_model
? p
->make_model
: "Unknown";
563 if (strstr(DNSSDHostName
, ".local"))
564 strlcpy(admin_hostname
, DNSSDHostName
, sizeof(admin_hostname
));
566 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.local.",
568 httpAssembleURIf(HTTP_URI_CODING_ALL
, adminurl_str
, sizeof(adminurl_str
),
573 # endif /* HAVE_SSL */
574 NULL
, admin_hostname
, DNSSDPort
, "/%s/%s",
575 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
577 keyvalue
[count
][0] = "adminurl";
578 keyvalue
[count
++][1] = adminurl_str
;
582 keyvalue
[count
][0] = "note";
583 keyvalue
[count
++][1] = p
->location
;
586 keyvalue
[count
][0] = "priority";
587 keyvalue
[count
++][1] = for_lpd
? "100" : "0";
589 keyvalue
[count
][0] = "product";
590 keyvalue
[count
++][1] = p
->pc
&& p
->pc
->product
? p
->pc
->product
: "Unknown";
592 keyvalue
[count
][0] = "pdl";
593 keyvalue
[count
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
595 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
597 keyvalue
[count
][0] = "air";
598 keyvalue
[count
++][1] = air_str
;
601 keyvalue
[count
][0] = "UUID";
602 keyvalue
[count
++][1] = p
->uuid
+ 9;
605 keyvalue
[count
][0] = "TLS";
606 keyvalue
[count
++][1] = "1.2";
607 #endif /* HAVE_SSL */
609 if (p
->type
& CUPS_PRINTER_FAX
)
611 keyvalue
[count
][0] = "Fax";
612 keyvalue
[count
++][1] = "T";
613 keyvalue
[count
][0] = "rfo";
614 keyvalue
[count
++][1] = rp_str
;
617 if (p
->type
& CUPS_PRINTER_COLOR
)
619 keyvalue
[count
][0] = "Color";
620 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLOR
) ? "T" : "F";
623 if (p
->type
& CUPS_PRINTER_DUPLEX
)
625 keyvalue
[count
][0] = "Duplex";
626 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_DUPLEX
) ? "T" : "F";
629 if (p
->type
& CUPS_PRINTER_STAPLE
)
631 keyvalue
[count
][0] = "Staple";
632 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_STAPLE
) ? "T" : "F";
635 if (p
->type
& CUPS_PRINTER_COPIES
)
637 keyvalue
[count
][0] = "Copies";
638 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COPIES
) ? "T" : "F";
641 if (p
->type
& CUPS_PRINTER_COLLATE
)
643 keyvalue
[count
][0] = "Collate";
644 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLLATE
) ? "T" : "F";
647 if (p
->type
& CUPS_PRINTER_PUNCH
)
649 keyvalue
[count
][0] = "Punch";
650 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_PUNCH
) ? "T" : "F";
653 if (p
->type
& CUPS_PRINTER_BIND
)
655 keyvalue
[count
][0] = "Bind";
656 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_BIND
) ? "T" : "F";
659 if (p
->type
& CUPS_PRINTER_SORT
)
661 keyvalue
[count
][0] = "Sort";
662 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_SORT
) ? "T" : "F";
665 if (p
->type
& CUPS_PRINTER_MFP
)
667 keyvalue
[count
][0] = "Scan";
668 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_MFP
) ? "T" : "F";
671 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
672 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
674 keyvalue
[count
][0] = "printer-state";
675 keyvalue
[count
++][1] = state_str
;
677 keyvalue
[count
][0] = "printer-type";
678 keyvalue
[count
++][1] = type_str
;
682 * Then pack them into a proper txt record...
686 TXTRecordCreate(&txt
, 0, NULL
);
688 for (i
= 0; i
< count
; i
++)
690 size_t len
= strlen(keyvalue
[i
][1]);
693 TXTRecordSetValue(&txt
, keyvalue
[i
][0], (uint8_t)len
, keyvalue
[i
][1]);
697 for (i
= 0, txt
= NULL
; i
< count
; i
++)
698 txt
= avahi_string_list_add_printf(txt
, "%s=%s", keyvalue
[i
][0],
700 # endif /* HAVE_DNSSD */
707 * 'dnssdDeregisterInstance()' - Deregister a DNS-SD service instance.
711 dnssdDeregisterInstance(
712 cupsd_srv_t
*srv
) /* I - Service */
718 DNSServiceRefDeallocate(*srv
);
720 # else /* HAVE_AVAHI */
721 avahi_threaded_poll_lock(DNSSDMaster
);
722 avahi_entry_group_free(*srv
);
723 avahi_threaded_poll_unlock(DNSSDMaster
);
724 # endif /* HAVE_DNSSD */
731 * 'dnssdDeregisterPrinter()' - Deregister all services for a printer.
735 dnssdDeregisterPrinter(
736 cupsd_printer_t
*p
, /* I - Printer */
737 int clear_name
) /* I - Clear the name? */
740 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
741 "dnssdDeregisterPrinter(p=%p(%s), clear_name=%d)", p
, p
->name
,
746 dnssdDeregisterInstance(&p
->ipp_srv
);
750 dnssdDeregisterInstance(&p
->ipps_srv
);
751 # endif /* HAVE_SSL */
752 dnssdDeregisterInstance(&p
->printer_srv
);
753 # endif /* HAVE_DNSSD */
757 * Remove the printer from the array of DNS-SD printers but keep the
761 cupsArrayRemove(DNSSDPrinters
, p
);
764 * Optionally clear the service name...
768 cupsdClearString(&p
->reg_name
);
773 * 'dnssdErrorString()' - Return an error string for an error code.
776 static const char * /* O - Error message */
777 dnssdErrorString(int error
) /* I - Error number */
782 case kDNSServiceErr_NoError
:
786 case kDNSServiceErr_Unknown
:
787 return ("Unknown error.");
789 case kDNSServiceErr_NoSuchName
:
790 return ("Service not found.");
792 case kDNSServiceErr_NoMemory
:
793 return ("Out of memory.");
795 case kDNSServiceErr_BadParam
:
796 return ("Bad parameter.");
798 case kDNSServiceErr_BadReference
:
799 return ("Bad service reference.");
801 case kDNSServiceErr_BadState
:
802 return ("Bad state.");
804 case kDNSServiceErr_BadFlags
:
805 return ("Bad flags.");
807 case kDNSServiceErr_Unsupported
:
808 return ("Unsupported.");
810 case kDNSServiceErr_NotInitialized
:
811 return ("Not initialized.");
813 case kDNSServiceErr_AlreadyRegistered
:
814 return ("Already registered.");
816 case kDNSServiceErr_NameConflict
:
817 return ("Name conflict.");
819 case kDNSServiceErr_Invalid
:
820 return ("Invalid name.");
822 case kDNSServiceErr_Firewall
:
823 return ("Firewall prevents registration.");
825 case kDNSServiceErr_Incompatible
:
826 return ("Client library incompatible.");
828 case kDNSServiceErr_BadInterfaceIndex
:
829 return ("Bad interface index.");
831 case kDNSServiceErr_Refused
:
832 return ("Server prevents registration.");
834 case kDNSServiceErr_NoSuchRecord
:
835 return ("Record not found.");
837 case kDNSServiceErr_NoAuth
:
838 return ("Authentication required.");
840 case kDNSServiceErr_NoSuchKey
:
841 return ("Encryption key not found.");
843 case kDNSServiceErr_NATTraversal
:
844 return ("Unable to traverse NAT boundary.");
846 case kDNSServiceErr_DoubleNAT
:
847 return ("Unable to traverse double-NAT boundary.");
849 case kDNSServiceErr_BadTime
:
850 return ("Bad system time.");
852 case kDNSServiceErr_BadSig
:
853 return ("Bad signature.");
855 case kDNSServiceErr_BadKey
:
856 return ("Bad encryption key.");
858 case kDNSServiceErr_Transient
:
859 return ("Transient error occurred - please try again.");
861 case kDNSServiceErr_ServiceNotRunning
:
862 return ("Server not running.");
864 case kDNSServiceErr_NATPortMappingUnsupported
:
865 return ("NAT doesn't support NAT-PMP or UPnP.");
867 case kDNSServiceErr_NATPortMappingDisabled
:
868 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
870 case kDNSServiceErr_NoRouter
:
871 return ("No Internet/default router configured.");
873 case kDNSServiceErr_PollingMode
:
874 return ("Service polling mode error.");
876 case kDNSServiceErr_Timeout
:
877 return ("Service timeout.");
880 # else /* HAVE_AVAHI */
881 return (avahi_strerror(error
));
882 # endif /* HAVE_DNSSD */
887 * 'dnssdRegisterCallback()' - Free a TXT record.
891 dnssdFreeTxtRecord(cupsd_txt_t
*txt
) /* I - TXT record */
894 TXTRecordDeallocate(txt
);
896 # else /* HAVE_AVAHI */
897 avahi_string_list_free(*txt
);
899 # endif /* HAVE_DNSSD */
904 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
909 dnssdRegisterCallback(
910 DNSServiceRef sdRef
, /* I - DNS Service reference */
911 DNSServiceFlags flags
, /* I - Reserved for future use */
912 DNSServiceErrorType errorCode
, /* I - Error code */
913 const char *name
, /* I - Service name */
914 const char *regtype
, /* I - Service type */
915 const char *domain
, /* I - Domain. ".local" for now */
916 void *context
) /* I - Printer */
918 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
919 /* Current printer */
926 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
927 name
, regtype
, p
? p
->name
: "Web Interface",
928 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
932 cupsdLogMessage(CUPSD_LOG_ERROR
,
933 "DNSServiceRegister failed with error %d", (int)errorCode
);
936 else if (p
&& (!p
->reg_name
|| _cups_strcasecmp(name
, p
->reg_name
)))
938 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
941 cupsArrayRemove(DNSSDPrinters
, p
);
942 cupsdSetString(&p
->reg_name
, name
);
943 cupsArrayAdd(DNSSDPrinters
, p
);
945 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
949 # else /* HAVE_AVAHI */
951 dnssdRegisterCallback(
952 AvahiEntryGroup
*srv
, /* I - Service */
953 AvahiEntryGroupState state
, /* I - Registration state */
954 void *context
) /* I - Printer */
956 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
957 /* Current printer */
959 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
960 "dnssdRegisterCallback(srv=%p, state=%d, context=%p) "
961 "for %s (%s)", srv
, state
, context
,
962 p
? p
->name
: "Web Interface",
963 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
965 /* TODO: Handle collisions with avahi_alternate_service_name(p->reg_name)? */
967 # endif /* HAVE_DNSSD */
971 * 'dnssdRegisterInstance()' - Register an instance of a printer service.
974 static int /* O - 1 on success, 0 on failure */
975 dnssdRegisterInstance(
976 cupsd_srv_t
*srv
, /* O - Service */
977 cupsd_printer_t
*p
, /* I - Printer */
978 char *name
, /* I - DNS-SD service name */
979 const char *type
, /* I - DNS-SD service type */
980 const char *subtypes
, /* I - Subtypes to register or NULL */
981 int port
, /* I - Port number or 0 */
982 cupsd_txt_t
*txt
, /* I - TXT record */
983 int commit
) /* I - Commit registration? */
985 char temp
[256], /* Temporary string */
986 *ptr
; /* Pointer into string */
987 int error
; /* Any error */
990 cupsdLogMessage(CUPSD_LOG_DEBUG
,
991 "Registering \"%s\" with DNS-SD type \"%s\".", name
, type
);
996 * Assign the correct pointer for "srv"...
1000 if (!strcmp(type
, "_printer._tcp"))
1001 srv
= &p
->printer_srv
; /* Target LPD service */
1003 else if (!strcmp(type
, "_ipps._tcp"))
1004 srv
= &p
->ipps_srv
; /* Target IPPS service */
1005 # endif /* HAVE_SSL */
1007 srv
= &p
->ipp_srv
; /* Target IPP service */
1009 # else /* HAVE_AVAHI */
1010 srv
= &p
->ipp_srv
; /* Target service group */
1011 # endif /* HAVE_DNSSD */
1017 # else /* HAVE_AVAHI */
1018 avahi_threaded_poll_lock(DNSSDMaster
);
1021 *srv
= avahi_entry_group_new(DNSSDClient
, dnssdRegisterCallback
, NULL
);
1024 avahi_threaded_poll_unlock(DNSSDMaster
);
1026 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1027 name
, dnssdErrorString(avahi_client_errno(DNSSDClient
)));
1030 # endif /* HAVE_DNSSD */
1033 * Make sure the name is <= 63 octets, and when we truncate be sure to
1034 * properly truncate any UTF-8 characters...
1037 ptr
= name
+ strlen(name
);
1038 while ((ptr
- name
) > 63)
1044 while (ptr
> name
&& (*ptr
& 0xc0) == 0x80);
1051 * Register the service...
1056 snprintf(temp
, sizeof(temp
), "%s,%s", type
, subtypes
);
1058 strlcpy(temp
, type
, sizeof(temp
));
1061 error
= DNSServiceRegister(srv
, kDNSServiceFlagsShareConnection
,
1062 0, name
, temp
, NULL
, NULL
, htons(port
),
1063 txt
? TXTRecordGetLength(txt
) : 0,
1064 txt
? TXTRecordGetBytesPtr(txt
) : NULL
,
1065 dnssdRegisterCallback
, p
);
1067 # else /* HAVE_AVAHI */
1070 AvahiStringList
*temptxt
;
1071 for (temptxt
= *txt
; temptxt
; temptxt
= temptxt
->next
)
1072 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS_SD \"%s\" %s", name
, temptxt
->text
);
1075 error
= avahi_entry_group_add_service_strlst(*srv
, AVAHI_IF_UNSPEC
,
1076 AVAHI_PROTO_UNSPEC
, 0, name
,
1077 type
, NULL
, NULL
, port
,
1080 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD service add for \"%s\" failed.",
1083 if (!error
&& subtypes
)
1086 * Register all of the subtypes...
1089 char *start
, /* Start of subtype */
1090 subtype
[256]; /* Subtype string */
1092 strlcpy(temp
, subtypes
, sizeof(temp
));
1094 for (start
= temp
; *start
; start
= ptr
)
1097 * Skip leading whitespace...
1100 while (*start
&& isspace(*start
& 255))
1104 * Grab everything up to the next comma or the end of the string...
1107 for (ptr
= start
; *ptr
&& *ptr
!= ','; ptr
++);
1116 * Register the subtype...
1119 snprintf(subtype
, sizeof(subtype
), "%s._sub.%s", start
, type
);
1121 error
= avahi_entry_group_add_service_subtype(*srv
, AVAHI_IF_UNSPEC
,
1122 AVAHI_PROTO_UNSPEC
, 0,
1123 name
, type
, NULL
, subtype
);
1126 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1127 "DNS-SD subtype %s registration for \"%s\" failed." ,
1134 if (!error
&& commit
)
1136 if ((error
= avahi_entry_group_commit(*srv
)) != 0)
1137 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD commit of \"%s\" failed.",
1141 avahi_threaded_poll_unlock(DNSSDMaster
);
1142 # endif /* HAVE_DNSSD */
1146 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1147 name
, dnssdErrorString(error
));
1148 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD type: %s", type
);
1150 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD sub-types: %s", subtypes
);
1158 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
1159 * or update the broadcast contents.
1163 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
1165 char name
[256]; /* Service name */
1166 int printer_port
; /* LPD port number */
1167 int status
; /* Registration status */
1168 cupsd_txt_t ipp_txt
, /* IPP(S) TXT record */
1169 printer_txt
; /* LPD TXT record */
1171 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
1172 !p
->ipp_srv
? "new" : "update");
1175 * Remove the current registrations if we have them and then return if
1176 * per-printer sharing was just disabled...
1179 dnssdDeregisterPrinter(p
, 0);
1185 * Set the registered name as needed; the registered name takes the form of
1186 * "<printer-info> @ <computer name>"...
1191 if (p
->info
&& strlen(p
->info
) > 0)
1193 if (DNSSDComputerName
)
1194 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
1196 strlcpy(name
, p
->info
, sizeof(name
));
1198 else if (DNSSDComputerName
)
1199 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
1201 strlcpy(name
, p
->name
, sizeof(name
));
1204 strlcpy(name
, p
->reg_name
, sizeof(name
));
1207 * Register IPP and LPD...
1209 * We always must register the "_printer" service type in order to reserve
1210 * our name, but use port number 0 if we haven't actually configured cups-lpd
1211 * to share via LPD...
1214 ipp_txt
= dnssdBuildTxtRecord(p
, 0);
1215 printer_txt
= dnssdBuildTxtRecord(p
, 1);
1217 if (BrowseLocalProtocols
& BROWSE_LPD
)
1222 status
= dnssdRegisterInstance(NULL
, p
, name
, "_printer._tcp", NULL
,
1223 printer_port
, &printer_txt
, 0);
1227 dnssdRegisterInstance(NULL
, p
, name
, "_ipps._tcp", DNSSDSubTypes
,
1228 DNSSDPort
, &ipp_txt
, 0);
1229 # endif /* HAVE_SSL */
1234 * Use the "_fax-ipp" service type for fax queues, otherwise use "_ipp"...
1237 if (p
->type
& CUPS_PRINTER_FAX
)
1238 status
= dnssdRegisterInstance(NULL
, p
, name
, "_fax-ipp._tcp",
1239 DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1);
1241 status
= dnssdRegisterInstance(NULL
, p
, name
, "_ipp._tcp", DNSSDSubTypes
,
1242 DNSSDPort
, &ipp_txt
, 1);
1245 dnssdFreeTxtRecord(&ipp_txt
);
1246 dnssdFreeTxtRecord(&printer_txt
);
1251 * Save the registered name and add the printer to the array of DNS-SD
1255 cupsdSetString(&p
->reg_name
, name
);
1256 cupsArrayAdd(DNSSDPrinters
, p
);
1261 * Registration failed for this printer...
1264 dnssdDeregisterInstance(&p
->ipp_srv
);
1268 dnssdDeregisterInstance(&p
->ipps_srv
);
1269 # endif /* HAVE_SSL */
1270 dnssdDeregisterInstance(&p
->printer_srv
);
1271 # endif /* HAVE_DNSSD */
1277 * 'dnssdStop()' - Stop all DNS-SD registrations.
1283 cupsd_printer_t
*p
; /* Current printer */
1287 * De-register the individual printers
1290 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1292 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1293 dnssdDeregisterPrinter(p
, 1);
1296 * Shutdown the rest of the service refs...
1299 dnssdDeregisterInstance(&WebIFSrv
);
1302 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDMaster
));
1304 DNSServiceRefDeallocate(DNSSDMaster
);
1307 # else /* HAVE_AVAHI */
1308 avahi_threaded_poll_stop(DNSSDMaster
);
1310 avahi_client_free(DNSSDClient
);
1313 avahi_threaded_poll_free(DNSSDMaster
);
1315 # endif /* HAVE_DNSSD */
1317 cupsArrayDelete(DNSSDPrinters
);
1318 DNSSDPrinters
= NULL
;
1326 * 'dnssdUpdate()' - Handle DNS-SD queries.
1332 DNSServiceErrorType sdErr
; /* Service discovery error */
1335 if ((sdErr
= DNSServiceProcessResult(DNSSDMaster
)) != kDNSServiceErr_NoError
)
1337 cupsdLogMessage(CUPSD_LOG_ERROR
,
1338 "DNS Service Discovery registration error %d!",
1343 # endif /* HAVE_DNSSD */
1347 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
1350 static char * /* O - String or NULL if none */
1351 get_auth_info_required(
1352 cupsd_printer_t
*p
, /* I - Printer */
1353 char *buffer
, /* I - Value buffer */
1354 size_t bufsize
) /* I - Size of value buffer */
1356 cupsd_location_t
*auth
; /* Pointer to authentication element */
1357 char resource
[1024]; /* Printer/class resource path */
1361 * If auth-info-required is set for this printer, return that...
1364 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
1366 int i
; /* Looping var */
1367 char *bufptr
; /* Pointer into buffer */
1369 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
1371 if (bufptr
>= (buffer
+ bufsize
- 2))
1377 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (size_t)(bufptr
- buffer
));
1378 bufptr
+= strlen(bufptr
);
1385 * Figure out the authentication data requirements to advertise...
1388 if (p
->type
& CUPS_PRINTER_CLASS
)
1389 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1391 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1393 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
1394 auth
->type
== CUPSD_AUTH_NONE
)
1395 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1399 int auth_type
; /* Authentication type */
1401 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
1402 auth_type
= cupsdDefaultAuthType();
1406 case CUPSD_AUTH_NONE
:
1409 case CUPSD_AUTH_NEGOTIATE
:
1410 strlcpy(buffer
, "negotiate", bufsize
);
1414 strlcpy(buffer
, "username,password", bufsize
);
1423 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1428 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
1431 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
1432 get_hostconfig(const char *name
) /* I - Name of service */
1434 cups_file_t
*fp
; /* Hostconfig file */
1435 char line
[1024], /* Line from file */
1436 *ptr
; /* Pointer to value */
1437 int state
= 1; /* State of service */
1441 * Try opening the /etc/hostconfig file; if we can't open it, assume that
1442 * the service is enabled/auto.
1445 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
1448 * Read lines from the file until we find the service...
1451 while (cupsFileGets(fp
, line
, sizeof(line
)))
1453 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
1458 if (!_cups_strcasecmp(line
, name
))
1461 * Found the service, see if it is set to "-NO-"...
1464 if (!_cups_strncasecmp(ptr
, "-NO-", 4))
1475 #endif /* __APPLE__ */
1479 * 'update_lpd()' - Update the LPD configuration as needed.
1483 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
1490 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
1491 * setting for backwards-compatibility.
1494 if (onoff
&& !get_hostconfig("CUPS_LPD"))
1496 #endif /* __APPLE__ */
1498 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
1501 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
1504 char newfile
[1024]; /* New cups-lpd.N file */
1505 cups_file_t
*ofp
, /* Original file pointer */
1506 *nfp
; /* New file pointer */
1507 char line
[1024]; /* Line from file */
1510 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
1512 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
1514 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1515 LPDConfigFile
+ 9, strerror(errno
));
1519 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1521 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1522 newfile
, strerror(errno
));
1528 * Copy all of the lines from the cups-lpd file...
1531 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1535 cupsFilePrintf(nfp
, "%s\n", line
);
1536 snprintf(line
, sizeof(line
), "\tdisable = %s",
1537 onoff
? "no" : "yes");
1539 else if (!strstr(line
, "disable ="))
1540 cupsFilePrintf(nfp
, "%s\n", line
);
1545 rename(newfile
, LPDConfigFile
+ 9);
1548 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
1551 * Enable/disable LPD via the launchctl command...
1554 char *argv
[5], /* Arguments for command */
1555 *envp
[MAX_ENV
]; /* Environment for command */
1556 int pid
; /* Process ID */
1559 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1560 argv
[0] = (char *)"launchctl";
1561 argv
[1] = (char *)(onoff
? "load" : "unload");
1562 argv
[2] = (char *)"-w";
1563 argv
[3] = LPDConfigFile
+ 10;
1566 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
1569 #endif /* __APPLE__ */
1571 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
1576 * 'update_smb()' - Update the SMB configuration as needed.
1580 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
1585 if (!strncmp(SMBConfigFile
, "samba:///", 9))
1588 * Enable/disable SMB via the specified smb.conf config file...
1591 char newfile
[1024]; /* New smb.conf.N file */
1592 cups_file_t
*ofp
, /* Original file pointer */
1593 *nfp
; /* New file pointer */
1594 char line
[1024]; /* Line from file */
1595 int in_printers
; /* In [printers] section? */
1598 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
1600 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
1602 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1603 SMBConfigFile
+ 8, strerror(errno
));
1607 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1609 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1610 newfile
, strerror(errno
));
1616 * Copy all of the lines from the smb.conf file...
1621 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1623 if (in_printers
&& strstr(line
, "printable ="))
1624 snprintf(line
, sizeof(line
), " printable = %s",
1625 onoff
? "yes" : "no");
1627 cupsFilePrintf(nfp
, "%s\n", line
);
1630 in_printers
= !strcmp(line
, "[printers]");
1635 rename(newfile
, SMBConfigFile
+ 8);
1638 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");