2 * Directory services routines for the CUPS scheduler.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
17 #if defined(HAVE_DNSSD) && defined(__APPLE__)
19 # include <CoreFoundation/CoreFoundation.h>
20 # include <SystemConfiguration/SystemConfiguration.h>
21 #endif /* HAVE_DNSSD && __APPLE__ */
29 static int avahi_running
= 0;
30 #endif /* HAVE_AVAHI */
37 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
38 static char *get_auth_info_required(cupsd_printer_t
*p
,
39 char *buffer
, size_t bufsize
);
40 #endif /* HAVE_DNSSD || HAVE_AVAHI */
42 static int get_hostconfig(const char *name
);
43 #endif /* __APPLE__ */
44 static void update_lpd(int onoff
);
45 static void update_smb(int onoff
);
48 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
50 static void dnssdAddAlias(const void *key
, const void *value
,
52 # endif /* __APPLE__ */
53 static cupsd_txt_t
dnssdBuildTxtRecord(cupsd_printer_t
*p
, int for_lpd
);
55 static void dnssdClientCallback(AvahiClient
*c
, AvahiClientState state
, void *userdata
);
56 # endif /* HAVE_AVAHI */
57 static void dnssdDeregisterAllPrinters(int from_callback
);
58 static void dnssdDeregisterInstance(cupsd_srv_t
*srv
, int from_callback
);
59 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
, int clear_name
, int from_callback
);
60 static const char *dnssdErrorString(int error
);
61 static void dnssdFreeTxtRecord(cupsd_txt_t
*txt
);
62 static void dnssdRegisterAllPrinters(int from_callback
);
64 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
65 DNSServiceFlags flags
,
66 DNSServiceErrorType errorCode
,
72 static void dnssdRegisterCallback(AvahiEntryGroup
*p
,
73 AvahiEntryGroupState state
,
75 # endif /* HAVE_DNSSD */
76 static int dnssdRegisterInstance(cupsd_srv_t
*srv
, cupsd_printer_t
*p
, char *name
, const char *type
, const char *subtypes
, int port
, cupsd_txt_t
*txt
, int commit
, int from_callback
);
77 static void dnssdRegisterPrinter(cupsd_printer_t
*p
, int from_callback
);
78 static void dnssdStop(void);
80 static void dnssdUpdate(void);
81 # endif /* HAVE_DNSSD */
82 static void dnssdUpdateDNSSDName(int from_callback
);
83 #endif /* HAVE_DNSSD || HAVE_AVAHI */
87 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
88 * local printer and remove any pending
89 * references to remote printers.
93 cupsdDeregisterPrinter(
94 cupsd_printer_t
*p
, /* I - Printer to register */
95 int removeit
) /* I - Printer being permanently removed */
98 * Only deregister if browsing is enabled and it's a local printer...
101 cupsdLogMessage(CUPSD_LOG_DEBUG
,
102 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
105 if (!Browsing
|| !p
->shared
||
106 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
110 * Announce the deletion...
113 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
114 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
115 dnssdDeregisterPrinter(p
, 1, 0);
116 #endif /* HAVE_DNSSD || HAVE_AVAHI */
121 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
122 * printer or update the broadcast contents.
126 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
128 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
131 if (!Browsing
|| !BrowseLocalProtocols
||
132 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
135 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
136 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
137 dnssdRegisterPrinter(p
, 0);
138 #endif /* HAVE_DNSSD || HAVE_AVAHI */
143 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
147 cupsdStartBrowsing(void)
149 if (!Browsing
|| !BrowseLocalProtocols
)
152 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
153 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
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
);
185 * Set the computer name and register the web interface...
189 cupsdUpdateDNSSDName();
191 # else /* HAVE_AVAHI */
192 if ((DNSSDMaster
= avahi_threaded_poll_new()) == NULL
)
194 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create DNS-SD thread.");
196 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
197 cupsdEndProcess(getpid(), 0);
201 int error
; /* Error code, if any */
203 DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), AVAHI_CLIENT_NO_FAIL
, dnssdClientCallback
, NULL
, &error
);
205 if (DNSSDClient
== NULL
)
207 cupsdLogMessage(CUPSD_LOG_ERROR
,
208 "Unable to communicate with avahi-daemon: %s",
209 dnssdErrorString(error
));
211 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
212 cupsdEndProcess(getpid(), 0);
214 avahi_threaded_poll_free(DNSSDMaster
);
218 avahi_threaded_poll_start(DNSSDMaster
);
220 # endif /* HAVE_DNSSD */
222 #endif /* HAVE_DNSSD || HAVE_AVAHI */
225 * Enable LPD and SMB printer sharing as needed through external programs...
228 if (BrowseLocalProtocols
& BROWSE_LPD
)
231 if (BrowseLocalProtocols
& BROWSE_SMB
)
234 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
236 * Register the individual printers
239 dnssdRegisterAllPrinters(0);
240 #endif /* HAVE_DNSSD || HAVE_AVAHI */
245 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
249 cupsdStopBrowsing(void)
251 if (!Browsing
|| !BrowseLocalProtocols
)
254 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
256 * De-register the individual printers
259 dnssdDeregisterAllPrinters(0);
262 * Shut down browsing sockets...
265 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
267 #endif /* HAVE_DNSSD || HAVE_AVAHI */
270 * Disable LPD and SMB printer sharing as needed through external programs...
273 if (BrowseLocalProtocols
& BROWSE_LPD
)
276 if (BrowseLocalProtocols
& BROWSE_SMB
)
281 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
283 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
287 cupsdUpdateDNSSDName(void)
289 dnssdUpdateDNSSDName(0);
295 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
299 dnssdAddAlias(const void *key
, /* I - Key */
300 const void *value
, /* I - Value (domain) */
301 void *context
) /* I - Unused */
303 char valueStr
[1024], /* Domain string */
304 hostname
[1024], /* Complete hostname */
305 *hostptr
; /* Pointer into hostname */
311 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
312 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
313 kCFStringEncodingUTF8
))
315 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
316 hostptr
= hostname
+ strlen(hostname
) - 1;
318 *hostptr
= '\0'; /* Strip trailing dot */
321 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
323 cupsdAddAlias(DNSSDAlias
, hostname
);
324 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
328 cupsdLogMessage(CUPSD_LOG_ERROR
,
329 "Bad Back to My Mac domain in dynamic store!");
331 # endif /* __APPLE__ */
335 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
338 static cupsd_txt_t
/* O - TXT record */
340 cupsd_printer_t
*p
, /* I - Printer information */
341 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
343 int i
, /* Looping var */
344 count
; /* Count of key/value pairs */
345 char admin_hostname
[256], /* Hostname for admin page */
346 adminurl_str
[256], /* URL for the admin page */
347 type_str
[32], /* Type to string buffer */
348 state_str
[32], /* State to string buffer */
349 rp_str
[1024], /* Queue name string buffer */
350 air_str
[1024], /* auth-info-required string buffer */
351 *keyvalue
[32][2], /* Table of key/value pairs */
352 *ptr
; /* Pointer in string */
353 cupsd_txt_t txt
; /* TXT record */
354 cupsd_listener_t
*lis
; /* Current listener */
355 const char *admin_scheme
= "http"; /* Admin page URL scheme */
359 * Load up the key value pairs...
364 if (!for_lpd
|| (BrowseLocalProtocols
& BROWSE_LPD
))
366 keyvalue
[count
][0] = "txtvers";
367 keyvalue
[count
++][1] = "1";
369 keyvalue
[count
][0] = "qtotal";
370 keyvalue
[count
++][1] = "1";
372 keyvalue
[count
][0] = "rp";
373 keyvalue
[count
++][1] = rp_str
;
375 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
377 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
378 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
381 keyvalue
[count
][0] = "ty";
382 keyvalue
[count
++][1] = p
->make_model
? p
->make_model
: "Unknown";
385 * Get the hostname for the admin page...
388 if (strchr(DNSSDHostName
, '.'))
391 * Use the provided hostname, but make sure it ends with a period...
394 if ((ptr
= DNSSDHostName
+ strlen(DNSSDHostName
) - 1) >= DNSSDHostName
&& *ptr
== '.')
395 strlcpy(admin_hostname
, DNSSDHostName
, sizeof(admin_hostname
));
397 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.", DNSSDHostName
);
402 * Unqualified hostname gets ".local." added to it...
405 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.local.", DNSSDHostName
);
409 * Get the URL scheme for the admin page...
413 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
); lis
; lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
415 if (lis
->encryption
!= HTTP_ENCRYPTION_NEVER
)
417 admin_scheme
= "https";
421 # endif /* HAVE_SSL */
423 httpAssembleURIf(HTTP_URI_CODING_ALL
, adminurl_str
, sizeof(adminurl_str
), admin_scheme
, NULL
, admin_hostname
, DNSSDPort
, "/%s/%s", (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
424 keyvalue
[count
][0] = "adminurl";
425 keyvalue
[count
++][1] = adminurl_str
;
429 keyvalue
[count
][0] = "note";
430 keyvalue
[count
++][1] = p
->location
;
433 keyvalue
[count
][0] = "priority";
434 keyvalue
[count
++][1] = for_lpd
? "100" : "0";
436 keyvalue
[count
][0] = "product";
437 keyvalue
[count
++][1] = p
->pc
&& p
->pc
->product
? p
->pc
->product
: "Unknown";
439 keyvalue
[count
][0] = "pdl";
440 keyvalue
[count
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
442 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
444 keyvalue
[count
][0] = "air";
445 keyvalue
[count
++][1] = air_str
;
448 keyvalue
[count
][0] = "UUID";
449 keyvalue
[count
++][1] = p
->uuid
+ 9;
452 keyvalue
[count
][0] = "TLS";
453 keyvalue
[count
++][1] = "1.2";
454 #endif /* HAVE_SSL */
456 if (p
->type
& CUPS_PRINTER_FAX
)
458 keyvalue
[count
][0] = "Fax";
459 keyvalue
[count
++][1] = "T";
460 keyvalue
[count
][0] = "rfo";
461 keyvalue
[count
++][1] = rp_str
;
464 if (p
->type
& CUPS_PRINTER_COLOR
)
466 keyvalue
[count
][0] = "Color";
467 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLOR
) ? "T" : "F";
470 if (p
->type
& CUPS_PRINTER_DUPLEX
)
472 keyvalue
[count
][0] = "Duplex";
473 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_DUPLEX
) ? "T" : "F";
476 if (p
->type
& CUPS_PRINTER_STAPLE
)
478 keyvalue
[count
][0] = "Staple";
479 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_STAPLE
) ? "T" : "F";
482 if (p
->type
& CUPS_PRINTER_COPIES
)
484 keyvalue
[count
][0] = "Copies";
485 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COPIES
) ? "T" : "F";
488 if (p
->type
& CUPS_PRINTER_COLLATE
)
490 keyvalue
[count
][0] = "Collate";
491 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLLATE
) ? "T" : "F";
494 if (p
->type
& CUPS_PRINTER_PUNCH
)
496 keyvalue
[count
][0] = "Punch";
497 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_PUNCH
) ? "T" : "F";
500 if (p
->type
& CUPS_PRINTER_BIND
)
502 keyvalue
[count
][0] = "Bind";
503 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_BIND
) ? "T" : "F";
506 if (p
->type
& CUPS_PRINTER_SORT
)
508 keyvalue
[count
][0] = "Sort";
509 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_SORT
) ? "T" : "F";
512 if (p
->type
& CUPS_PRINTER_MFP
)
514 keyvalue
[count
][0] = "Scan";
515 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_MFP
) ? "T" : "F";
518 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
519 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
521 keyvalue
[count
][0] = "printer-state";
522 keyvalue
[count
++][1] = state_str
;
524 keyvalue
[count
][0] = "printer-type";
525 keyvalue
[count
++][1] = type_str
;
529 * Then pack them into a proper txt record...
533 TXTRecordCreate(&txt
, 0, NULL
);
535 for (i
= 0; i
< count
; i
++)
537 size_t len
= strlen(keyvalue
[i
][1]);
540 TXTRecordSetValue(&txt
, keyvalue
[i
][0], (uint8_t)len
, keyvalue
[i
][1]);
544 for (i
= 0, txt
= NULL
; i
< count
; i
++)
545 txt
= avahi_string_list_add_printf(txt
, "%s=%s", keyvalue
[i
][0],
547 # endif /* HAVE_DNSSD */
555 * 'dnssdClientCallback()' - Client callback for Avahi.
557 * Called whenever the client or server state changes...
562 AvahiClient
*c
, /* I - Client */
563 AvahiClientState state
, /* I - Current state */
564 void *userdata
) /* I - User data (unused) */
566 int error
; /* Error code, if any */
575 * Make sure DNSSDClient is already set also if this callback function is
576 * already running before avahi_client_new() in dnssdStartBrowsing()
585 case AVAHI_CLIENT_S_REGISTERING
:
586 case AVAHI_CLIENT_S_RUNNING
:
587 case AVAHI_CLIENT_S_COLLISION
:
588 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Avahi server connection now available, registering printers for Bonjour broadcasting.");
591 * Mark that Avahi server is running...
597 * Set the computer name and register the web interface...
601 dnssdUpdateDNSSDName(1);
604 * Register the individual printers
607 dnssdRegisterAllPrinters(1);
610 case AVAHI_CLIENT_FAILURE
:
611 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
)
613 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Avahi server disappeared, unregistering printers for Bonjour broadcasting.");
616 * Unregister everything and close the client...
619 dnssdDeregisterAllPrinters(1);
620 dnssdDeregisterInstance(&WebIFSrv
, 1);
621 avahi_client_free(DNSSDClient
);
625 * Mark that Avahi server is not running...
631 * Renew Avahi client...
634 DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), AVAHI_CLIENT_NO_FAIL
, dnssdClientCallback
, NULL
, &error
);
638 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to communicate with avahi-daemon: %s", dnssdErrorString(error
));
639 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
640 cupsdEndProcess(getpid(), 0);
645 cupsdLogMessage(CUPSD_LOG_ERROR
, "Communication with avahi-daemon has failed: %s", avahi_strerror(avahi_client_errno(c
)));
646 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
647 cupsdEndProcess(getpid(), 0);
655 # endif /* HAVE_AVAHI */
659 * 'dnssdDeregisterAllPrinters()' - Deregister all printers.
663 dnssdDeregisterAllPrinters(
664 int from_callback
) /* I - Deregistering because of callback? */
666 cupsd_printer_t
*p
; /* Current printer */
672 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
674 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
675 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
676 dnssdDeregisterPrinter(p
, 1, from_callback
);
681 * 'dnssdDeregisterInstance()' - Deregister a DNS-SD service instance.
685 dnssdDeregisterInstance(
686 cupsd_srv_t
*srv
, /* I - Service */
687 int from_callback
) /* I - Called from callback? */
695 DNSServiceRefDeallocate(*srv
);
697 # else /* HAVE_AVAHI */
699 avahi_threaded_poll_lock(DNSSDMaster
);
702 avahi_entry_group_free(*srv
);
705 avahi_threaded_poll_unlock(DNSSDMaster
);
706 # endif /* HAVE_DNSSD */
713 * 'dnssdDeregisterPrinter()' - Deregister all services for a printer.
717 dnssdDeregisterPrinter(
718 cupsd_printer_t
*p
, /* I - Printer */
719 int clear_name
, /* I - Clear the name? */
720 int from_callback
) /* I - Called from callback? */
723 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
724 "dnssdDeregisterPrinter(p=%p(%s), clear_name=%d)", p
, p
->name
,
729 dnssdDeregisterInstance(&p
->ipp_srv
, from_callback
);
733 dnssdDeregisterInstance(&p
->ipps_srv
, from_callback
);
734 # endif /* HAVE_SSL */
735 dnssdDeregisterInstance(&p
->printer_srv
, from_callback
);
736 # endif /* HAVE_DNSSD */
740 * Remove the printer from the array of DNS-SD printers but keep the
744 cupsArrayRemove(DNSSDPrinters
, p
);
747 * Optionally clear the service name...
751 cupsdClearString(&p
->reg_name
);
756 * 'dnssdErrorString()' - Return an error string for an error code.
759 static const char * /* O - Error message */
760 dnssdErrorString(int error
) /* I - Error number */
765 case kDNSServiceErr_NoError
:
769 case kDNSServiceErr_Unknown
:
770 return ("Unknown error.");
772 case kDNSServiceErr_NoSuchName
:
773 return ("Service not found.");
775 case kDNSServiceErr_NoMemory
:
776 return ("Out of memory.");
778 case kDNSServiceErr_BadParam
:
779 return ("Bad parameter.");
781 case kDNSServiceErr_BadReference
:
782 return ("Bad service reference.");
784 case kDNSServiceErr_BadState
:
785 return ("Bad state.");
787 case kDNSServiceErr_BadFlags
:
788 return ("Bad flags.");
790 case kDNSServiceErr_Unsupported
:
791 return ("Unsupported.");
793 case kDNSServiceErr_NotInitialized
:
794 return ("Not initialized.");
796 case kDNSServiceErr_AlreadyRegistered
:
797 return ("Already registered.");
799 case kDNSServiceErr_NameConflict
:
800 return ("Name conflict.");
802 case kDNSServiceErr_Invalid
:
803 return ("Invalid name.");
805 case kDNSServiceErr_Firewall
:
806 return ("Firewall prevents registration.");
808 case kDNSServiceErr_Incompatible
:
809 return ("Client library incompatible.");
811 case kDNSServiceErr_BadInterfaceIndex
:
812 return ("Bad interface index.");
814 case kDNSServiceErr_Refused
:
815 return ("Server prevents registration.");
817 case kDNSServiceErr_NoSuchRecord
:
818 return ("Record not found.");
820 case kDNSServiceErr_NoAuth
:
821 return ("Authentication required.");
823 case kDNSServiceErr_NoSuchKey
:
824 return ("Encryption key not found.");
826 case kDNSServiceErr_NATTraversal
:
827 return ("Unable to traverse NAT boundary.");
829 case kDNSServiceErr_DoubleNAT
:
830 return ("Unable to traverse double-NAT boundary.");
832 case kDNSServiceErr_BadTime
:
833 return ("Bad system time.");
835 case kDNSServiceErr_BadSig
:
836 return ("Bad signature.");
838 case kDNSServiceErr_BadKey
:
839 return ("Bad encryption key.");
841 case kDNSServiceErr_Transient
:
842 return ("Transient error occurred - please try again.");
844 case kDNSServiceErr_ServiceNotRunning
:
845 return ("Server not running.");
847 case kDNSServiceErr_NATPortMappingUnsupported
:
848 return ("NAT doesn't support NAT-PMP or UPnP.");
850 case kDNSServiceErr_NATPortMappingDisabled
:
851 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
853 case kDNSServiceErr_NoRouter
:
854 return ("No Internet/default router configured.");
856 case kDNSServiceErr_PollingMode
:
857 return ("Service polling mode error.");
859 case kDNSServiceErr_Timeout
:
860 return ("Service timeout.");
863 # else /* HAVE_AVAHI */
864 return (avahi_strerror(error
));
865 # endif /* HAVE_DNSSD */
870 * 'dnssdRegisterCallback()' - Free a TXT record.
874 dnssdFreeTxtRecord(cupsd_txt_t
*txt
) /* I - TXT record */
877 TXTRecordDeallocate(txt
);
879 # else /* HAVE_AVAHI */
880 avahi_string_list_free(*txt
);
882 # endif /* HAVE_DNSSD */
887 * 'dnssdRegisterAllPrinters()' - Register all printers.
891 dnssdRegisterAllPrinters(int from_callback
) /* I - Called from callback? */
893 cupsd_printer_t
*p
; /* Current printer */
899 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
901 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
902 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
903 dnssdRegisterPrinter(p
, from_callback
);
908 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
913 dnssdRegisterCallback(
914 DNSServiceRef sdRef
, /* I - DNS Service reference */
915 DNSServiceFlags flags
, /* I - Reserved for future use */
916 DNSServiceErrorType errorCode
, /* I - Error code */
917 const char *name
, /* I - Service name */
918 const char *regtype
, /* I - Service type */
919 const char *domain
, /* I - Domain. ".local" for now */
920 void *context
) /* I - Printer */
922 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
923 /* Current printer */
930 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
931 name
, regtype
, p
? p
->name
: "Web Interface",
932 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
936 cupsdLogMessage(CUPSD_LOG_ERROR
,
937 "DNSServiceRegister failed with error %d", (int)errorCode
);
940 else if (p
&& (!p
->reg_name
|| _cups_strcasecmp(name
, p
->reg_name
)))
942 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
945 cupsArrayRemove(DNSSDPrinters
, p
);
946 cupsdSetString(&p
->reg_name
, name
);
947 cupsArrayAdd(DNSSDPrinters
, p
);
949 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
953 # else /* HAVE_AVAHI */
955 dnssdRegisterCallback(
956 AvahiEntryGroup
*srv
, /* I - Service */
957 AvahiEntryGroupState state
, /* I - Registration state */
958 void *context
) /* I - Printer */
960 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
961 /* Current printer */
963 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
964 "dnssdRegisterCallback(srv=%p, state=%d, context=%p) "
965 "for %s (%s)", srv
, state
, context
,
966 p
? p
->name
: "Web Interface",
967 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
969 /* TODO: Handle collisions with avahi_alternate_service_name(p->reg_name)? */
971 # endif /* HAVE_DNSSD */
975 * 'dnssdRegisterInstance()' - Register an instance of a printer service.
978 static int /* O - 1 on success, 0 on failure */
979 dnssdRegisterInstance(
980 cupsd_srv_t
*srv
, /* O - Service */
981 cupsd_printer_t
*p
, /* I - Printer */
982 char *name
, /* I - DNS-SD service name */
983 const char *type
, /* I - DNS-SD service type */
984 const char *subtypes
, /* I - Subtypes to register or NULL */
985 int port
, /* I - Port number or 0 */
986 cupsd_txt_t
*txt
, /* I - TXT record */
987 int commit
, /* I - Commit registration? */
988 int from_callback
) /* I - Called from callback? */
990 char temp
[256], /* Temporary string */
991 *ptr
; /* Pointer into string */
992 int error
; /* Any error */
997 # endif /* HAVE_DNSSD */
999 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Registering \"%s\" with DNS-SD type \"%s\".", name
, type
);
1004 * Assign the correct pointer for "srv"...
1008 if (!strcmp(type
, "_printer._tcp"))
1009 srv
= &p
->printer_srv
; /* Target LPD service */
1011 else if (!strcmp(type
, "_ipps._tcp"))
1012 srv
= &p
->ipps_srv
; /* Target IPPS service */
1013 # endif /* HAVE_SSL */
1015 srv
= &p
->ipp_srv
; /* Target IPP service */
1017 # else /* HAVE_AVAHI */
1018 srv
= &p
->ipp_srv
; /* Target service group */
1019 # endif /* HAVE_DNSSD */
1025 # else /* HAVE_AVAHI */
1027 avahi_threaded_poll_lock(DNSSDMaster
);
1030 *srv
= avahi_entry_group_new(DNSSDClient
, dnssdRegisterCallback
, NULL
);
1034 avahi_threaded_poll_unlock(DNSSDMaster
);
1036 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1037 name
, dnssdErrorString(avahi_client_errno(DNSSDClient
)));
1040 # endif /* HAVE_DNSSD */
1043 * Make sure the name is <= 63 octets, and when we truncate be sure to
1044 * properly truncate any UTF-8 characters...
1047 ptr
= name
+ strlen(name
);
1048 while ((ptr
- name
) > 63)
1054 while (ptr
> name
&& (*ptr
& 0xc0) == 0x80);
1061 * Register the service...
1066 snprintf(temp
, sizeof(temp
), "%s,%s", type
, subtypes
);
1068 strlcpy(temp
, type
, sizeof(temp
));
1071 error
= DNSServiceRegister(srv
, kDNSServiceFlagsShareConnection
,
1072 0, name
, temp
, NULL
, DNSSDHostName
, htons(port
),
1073 txt
? TXTRecordGetLength(txt
) : 0,
1074 txt
? TXTRecordGetBytesPtr(txt
) : NULL
,
1075 dnssdRegisterCallback
, p
);
1077 # else /* HAVE_AVAHI */
1080 AvahiStringList
*temptxt
;
1081 for (temptxt
= *txt
; temptxt
; temptxt
= temptxt
->next
)
1082 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS_SD \"%s\" %s", name
, temptxt
->text
);
1085 error
= avahi_entry_group_add_service_strlst(*srv
, AVAHI_IF_UNSPEC
,
1086 AVAHI_PROTO_UNSPEC
, 0, name
,
1087 type
, NULL
, DNSSDHostName
, port
,
1090 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD service add for \"%s\" failed.",
1093 if (!error
&& subtypes
)
1096 * Register all of the subtypes...
1099 char *start
, /* Start of subtype */
1100 subtype
[256]; /* Subtype string */
1102 strlcpy(temp
, subtypes
, sizeof(temp
));
1104 for (start
= temp
; *start
; start
= ptr
)
1107 * Skip leading whitespace...
1110 while (*start
&& isspace(*start
& 255))
1114 * Grab everything up to the next comma or the end of the string...
1117 for (ptr
= start
; *ptr
&& *ptr
!= ','; ptr
++);
1126 * Register the subtype...
1129 snprintf(subtype
, sizeof(subtype
), "%s._sub.%s", start
, type
);
1131 error
= avahi_entry_group_add_service_subtype(*srv
, AVAHI_IF_UNSPEC
,
1132 AVAHI_PROTO_UNSPEC
, 0,
1133 name
, type
, NULL
, subtype
);
1136 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1137 "DNS-SD subtype %s registration for \"%s\" failed." ,
1144 if (!error
&& commit
)
1146 if ((error
= avahi_entry_group_commit(*srv
)) != 0)
1147 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD commit of \"%s\" failed.",
1152 avahi_threaded_poll_unlock(DNSSDMaster
);
1153 # endif /* HAVE_DNSSD */
1157 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1158 name
, dnssdErrorString(error
));
1159 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD type: %s", type
);
1161 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD sub-types: %s", subtypes
);
1169 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
1170 * or update the broadcast contents.
1174 dnssdRegisterPrinter(
1175 cupsd_printer_t
*p
, /* I - Printer */
1176 int from_callback
) /* I - Called from callback? */
1178 char name
[256]; /* Service name */
1179 int printer_port
; /* LPD port number */
1180 int status
; /* Registration status */
1181 cupsd_txt_t ipp_txt
, /* IPP(S) TXT record */
1182 printer_txt
; /* LPD TXT record */
1185 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
1186 !p
->ipp_srv
? "new" : "update");
1191 # endif /* HAVE_AVAHI */
1194 * Remove the current registrations if we have them and then return if
1195 * per-printer sharing was just disabled...
1198 dnssdDeregisterPrinter(p
, 0, from_callback
);
1204 * Set the registered name as needed; the registered name takes the form of
1205 * "<printer-info> @ <computer name>"...
1210 if (p
->info
&& strlen(p
->info
) > 0)
1212 if (DNSSDComputerName
)
1213 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
1215 strlcpy(name
, p
->info
, sizeof(name
));
1217 else if (DNSSDComputerName
)
1218 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
1220 strlcpy(name
, p
->name
, sizeof(name
));
1223 strlcpy(name
, p
->reg_name
, sizeof(name
));
1226 * Register IPP and LPD...
1228 * We always must register the "_printer" service type in order to reserve
1229 * our name, but use port number 0 if we haven't actually configured cups-lpd
1230 * to share via LPD...
1233 ipp_txt
= dnssdBuildTxtRecord(p
, 0);
1234 printer_txt
= dnssdBuildTxtRecord(p
, 1);
1236 if (BrowseLocalProtocols
& BROWSE_LPD
)
1241 status
= dnssdRegisterInstance(NULL
, p
, name
, "_printer._tcp", NULL
, printer_port
, &printer_txt
, 0, from_callback
);
1245 dnssdRegisterInstance(NULL
, p
, name
, "_ipps._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 0, from_callback
);
1246 # endif /* HAVE_SSL */
1251 * Use the "_fax-ipp" service type for fax queues, otherwise use "_ipp"...
1254 if (p
->type
& CUPS_PRINTER_FAX
)
1255 status
= dnssdRegisterInstance(NULL
, p
, name
, "_fax-ipp._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1, from_callback
);
1257 status
= dnssdRegisterInstance(NULL
, p
, name
, "_ipp._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1, from_callback
);
1260 dnssdFreeTxtRecord(&ipp_txt
);
1261 dnssdFreeTxtRecord(&printer_txt
);
1266 * Save the registered name and add the printer to the array of DNS-SD
1270 cupsdSetString(&p
->reg_name
, name
);
1271 cupsArrayAdd(DNSSDPrinters
, p
);
1276 * Registration failed for this printer...
1279 dnssdDeregisterInstance(&p
->ipp_srv
, from_callback
);
1283 dnssdDeregisterInstance(&p
->ipps_srv
, from_callback
);
1284 # endif /* HAVE_SSL */
1285 dnssdDeregisterInstance(&p
->printer_srv
, from_callback
);
1286 # endif /* HAVE_DNSSD */
1292 * 'dnssdStop()' - Stop all DNS-SD registrations.
1298 cupsd_printer_t
*p
; /* Current printer */
1302 * De-register the individual printers
1305 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1307 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1308 dnssdDeregisterPrinter(p
, 1, 0);
1311 * Shutdown the rest of the service refs...
1314 dnssdDeregisterInstance(&WebIFSrv
, 0);
1317 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDMaster
));
1319 DNSServiceRefDeallocate(DNSSDMaster
);
1322 # else /* HAVE_AVAHI */
1324 avahi_threaded_poll_stop(DNSSDMaster
);
1328 avahi_client_free(DNSSDClient
);
1334 avahi_threaded_poll_free(DNSSDMaster
);
1337 # endif /* HAVE_DNSSD */
1339 cupsArrayDelete(DNSSDPrinters
);
1340 DNSSDPrinters
= NULL
;
1348 * 'dnssdUpdate()' - Handle DNS-SD queries.
1354 DNSServiceErrorType sdErr
; /* Service discovery error */
1357 if ((sdErr
= DNSServiceProcessResult(DNSSDMaster
)) != kDNSServiceErr_NoError
)
1359 cupsdLogMessage(CUPSD_LOG_ERROR
,
1360 "DNS Service Discovery registration error %d!",
1365 # endif /* HAVE_DNSSD */
1369 * 'dnssdUpdateDNSSDName()' - Update the listen port, computer name, and web interface registration.
1373 dnssdUpdateDNSSDName(int from_callback
) /* I - Called from callback? */
1375 char webif
[1024]; /* Web interface share name */
1377 SCDynamicStoreRef sc
; /* Context for dynamic store */
1378 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1379 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1380 CFStringRef nameRef
; /* Host name CFString */
1381 char nameBuffer
[1024]; /* C-string buffer */
1382 # endif /* __APPLE__ */
1386 * Only share the web interface and printers when non-local listening is
1393 * Get the port we use for registrations. If we are not listening on any
1394 * non-local ports, there is no sense sharing local printers via Bonjour...
1397 cupsd_listener_t
*lis
; /* Current listening socket */
1399 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1401 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1403 if (httpAddrLocalhost(&(lis
->address
)))
1406 DNSSDPort
= httpAddrPort(&(lis
->address
));
1415 * Get the computer name as a c-string...
1419 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1424 * Get the computer name from the dynamic store...
1427 cupsdClearString(&DNSSDComputerName
);
1429 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1431 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1432 kCFStringEncodingUTF8
))
1434 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1435 "Dynamic store computer name is \"%s\".", nameBuffer
);
1436 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1442 if (!DNSSDComputerName
)
1445 * Use the ServerName instead...
1448 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1449 "Using ServerName \"%s\" as computer name.", ServerName
);
1450 cupsdSetString(&DNSSDComputerName
, ServerName
);
1456 * Get the local hostname from the dynamic store...
1459 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1461 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1462 kCFStringEncodingUTF8
))
1464 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Dynamic store host name is \"%s\".", nameBuffer
);
1466 if (strchr(nameBuffer
, '.'))
1467 cupsdSetString(&DNSSDHostName
, nameBuffer
);
1469 cupsdSetStringf(&DNSSDHostName
, "%s.local", nameBuffer
);
1471 cupsdLogMessage(CUPSD_LOG_INFO
, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName
);
1481 * Use the ServerName instead...
1484 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Using ServerName \"%s\" as host name.", ServerName
);
1485 cupsdSetString(&DNSSDHostName
, ServerName
);
1487 cupsdLogMessage(CUPSD_LOG_INFO
, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName
);
1491 * Get any Back-to-My-Mac domains and add them as aliases...
1494 cupsdFreeAliases(DNSSDAlias
);
1497 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
1498 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
1500 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
1501 (int)CFDictionaryGetCount(btmm
));
1502 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
1505 cupsdLogMessage(CUPSD_LOG_ERROR
,
1506 "Bad Back to My Mac data in dynamic store!");
1508 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
1516 # endif /* __APPLE__ */
1520 const char *host_name
= avahi_client_get_host_name(DNSSDClient
);
1522 cupsdSetString(&DNSSDComputerName
, host_name
? host_name
: ServerName
);
1526 const char *host_fqdn
= avahi_client_get_host_name_fqdn(DNSSDClient
);
1529 cupsdSetString(&DNSSDHostName
, host_fqdn
);
1530 else if (strchr(ServerName
, '.'))
1531 cupsdSetString(&DNSSDHostName
, ServerName
);
1533 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
1535 cupsdLogMessage(CUPSD_LOG_INFO
, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName
);
1539 # endif /* HAVE_AVAHI */
1541 cupsdSetString(&DNSSDComputerName
, ServerName
);
1545 if (strchr(ServerName
, '.'))
1546 cupsdSetString(&DNSSDHostName
, ServerName
);
1548 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
1550 cupsdLogMessage(CUPSD_LOG_INFO
, "Defaulting to \"DNSSDHostName %s\".", DNSSDHostName
);
1555 * Then (re)register the web interface if enabled...
1560 if (DNSSDComputerName
)
1561 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
1563 strlcpy(webif
, "CUPS", sizeof(webif
));
1565 dnssdDeregisterInstance(&WebIFSrv
, from_callback
);
1566 dnssdRegisterInstance(&WebIFSrv
, NULL
, webif
, "_http._tcp", "_printer", DNSSDPort
, NULL
, 1, from_callback
);
1572 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
1575 static char * /* O - String or NULL if none */
1576 get_auth_info_required(
1577 cupsd_printer_t
*p
, /* I - Printer */
1578 char *buffer
, /* I - Value buffer */
1579 size_t bufsize
) /* I - Size of value buffer */
1581 cupsd_location_t
*auth
; /* Pointer to authentication element */
1582 char resource
[1024]; /* Printer/class resource path */
1586 * If auth-info-required is set for this printer, return that...
1589 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
1591 int i
; /* Looping var */
1592 char *bufptr
; /* Pointer into buffer */
1594 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
1596 if (bufptr
>= (buffer
+ bufsize
- 2))
1602 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (size_t)(bufptr
- buffer
));
1603 bufptr
+= strlen(bufptr
);
1610 * Figure out the authentication data requirements to advertise...
1613 if (p
->type
& CUPS_PRINTER_CLASS
)
1614 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1616 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1618 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
1619 auth
->type
== CUPSD_AUTH_NONE
)
1620 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1624 int auth_type
; /* Authentication type */
1626 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
1627 auth_type
= cupsdDefaultAuthType();
1631 case CUPSD_AUTH_NONE
:
1634 case CUPSD_AUTH_NEGOTIATE
:
1635 strlcpy(buffer
, "negotiate", bufsize
);
1639 strlcpy(buffer
, "username,password", bufsize
);
1648 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1653 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
1656 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
1657 get_hostconfig(const char *name
) /* I - Name of service */
1659 cups_file_t
*fp
; /* Hostconfig file */
1660 char line
[1024], /* Line from file */
1661 *ptr
; /* Pointer to value */
1662 int state
= 1; /* State of service */
1666 * Try opening the /etc/hostconfig file; if we can't open it, assume that
1667 * the service is enabled/auto.
1670 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
1673 * Read lines from the file until we find the service...
1676 while (cupsFileGets(fp
, line
, sizeof(line
)))
1678 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
1683 if (!_cups_strcasecmp(line
, name
))
1686 * Found the service, see if it is set to "-NO-"...
1689 if (!_cups_strncasecmp(ptr
, "-NO-", 4))
1700 #endif /* __APPLE__ */
1704 * 'update_lpd()' - Update the LPD configuration as needed.
1708 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
1715 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
1716 * setting for backwards-compatibility.
1719 if (onoff
&& !get_hostconfig("CUPS_LPD"))
1721 #endif /* __APPLE__ */
1723 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
1726 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
1729 char newfile
[1024]; /* New cups-lpd.N file */
1730 cups_file_t
*ofp
, /* Original file pointer */
1731 *nfp
; /* New file pointer */
1732 char line
[1024]; /* Line from file */
1735 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
1737 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
1739 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1740 LPDConfigFile
+ 9, strerror(errno
));
1744 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1746 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1747 newfile
, strerror(errno
));
1753 * Copy all of the lines from the cups-lpd file...
1756 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1760 cupsFilePrintf(nfp
, "%s\n", line
);
1761 snprintf(line
, sizeof(line
), "\tdisable = %s",
1762 onoff
? "no" : "yes");
1764 else if (!strstr(line
, "disable ="))
1765 cupsFilePrintf(nfp
, "%s\n", line
);
1770 rename(newfile
, LPDConfigFile
+ 9);
1773 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
1776 * Enable/disable LPD via the launchctl command...
1779 char *argv
[5], /* Arguments for command */
1780 *envp
[MAX_ENV
]; /* Environment for command */
1781 int pid
; /* Process ID */
1784 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1785 argv
[0] = (char *)"launchctl";
1786 argv
[1] = (char *)(onoff
? "load" : "unload");
1787 argv
[2] = (char *)"-w";
1788 argv
[3] = LPDConfigFile
+ 10;
1791 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
1794 #endif /* __APPLE__ */
1796 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
1801 * 'update_smb()' - Update the SMB configuration as needed.
1805 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
1810 if (!strncmp(SMBConfigFile
, "samba:///", 9))
1813 * Enable/disable SMB via the specified smb.conf config file...
1816 char newfile
[1024]; /* New smb.conf.N file */
1817 cups_file_t
*ofp
, /* Original file pointer */
1818 *nfp
; /* New file pointer */
1819 char line
[1024]; /* Line from file */
1820 int in_printers
; /* In [printers] section? */
1823 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
1825 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
1827 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1828 SMBConfigFile
+ 8, strerror(errno
));
1832 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1834 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1835 newfile
, strerror(errno
));
1841 * Copy all of the lines from the smb.conf file...
1846 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1848 if (in_printers
&& strstr(line
, "printable ="))
1849 snprintf(line
, sizeof(line
), " printable = %s",
1850 onoff
? "yes" : "no");
1852 cupsFilePrintf(nfp
, "%s\n", line
);
1855 in_printers
= !strcmp(line
, "[printers]");
1860 rename(newfile
, SMBConfigFile
+ 8);
1863 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");