2 * Directory services routines for the CUPS scheduler.
4 * Copyright 2007-2017 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 */
701 avahi_threaded_poll_lock(DNSSDMaster
);
703 avahi_entry_group_free(*srv
);
706 avahi_threaded_poll_unlock(DNSSDMaster
);
708 # endif /* HAVE_DNSSD */
715 * 'dnssdDeregisterPrinter()' - Deregister all services for a printer.
719 dnssdDeregisterPrinter(
720 cupsd_printer_t
*p
, /* I - Printer */
721 int clear_name
, /* I - Clear the name? */
722 int from_callback
) /* I - Called from callback? */
725 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
726 "dnssdDeregisterPrinter(p=%p(%s), clear_name=%d)", p
, p
->name
,
731 dnssdDeregisterInstance(&p
->ipp_srv
, from_callback
);
735 dnssdDeregisterInstance(&p
->ipps_srv
, from_callback
);
736 # endif /* HAVE_SSL */
737 dnssdDeregisterInstance(&p
->printer_srv
, from_callback
);
738 # endif /* HAVE_DNSSD */
742 * Remove the printer from the array of DNS-SD printers but keep the
746 cupsArrayRemove(DNSSDPrinters
, p
);
749 * Optionally clear the service name...
753 cupsdClearString(&p
->reg_name
);
758 * 'dnssdErrorString()' - Return an error string for an error code.
761 static const char * /* O - Error message */
762 dnssdErrorString(int error
) /* I - Error number */
767 case kDNSServiceErr_NoError
:
771 case kDNSServiceErr_Unknown
:
772 return ("Unknown error.");
774 case kDNSServiceErr_NoSuchName
:
775 return ("Service not found.");
777 case kDNSServiceErr_NoMemory
:
778 return ("Out of memory.");
780 case kDNSServiceErr_BadParam
:
781 return ("Bad parameter.");
783 case kDNSServiceErr_BadReference
:
784 return ("Bad service reference.");
786 case kDNSServiceErr_BadState
:
787 return ("Bad state.");
789 case kDNSServiceErr_BadFlags
:
790 return ("Bad flags.");
792 case kDNSServiceErr_Unsupported
:
793 return ("Unsupported.");
795 case kDNSServiceErr_NotInitialized
:
796 return ("Not initialized.");
798 case kDNSServiceErr_AlreadyRegistered
:
799 return ("Already registered.");
801 case kDNSServiceErr_NameConflict
:
802 return ("Name conflict.");
804 case kDNSServiceErr_Invalid
:
805 return ("Invalid name.");
807 case kDNSServiceErr_Firewall
:
808 return ("Firewall prevents registration.");
810 case kDNSServiceErr_Incompatible
:
811 return ("Client library incompatible.");
813 case kDNSServiceErr_BadInterfaceIndex
:
814 return ("Bad interface index.");
816 case kDNSServiceErr_Refused
:
817 return ("Server prevents registration.");
819 case kDNSServiceErr_NoSuchRecord
:
820 return ("Record not found.");
822 case kDNSServiceErr_NoAuth
:
823 return ("Authentication required.");
825 case kDNSServiceErr_NoSuchKey
:
826 return ("Encryption key not found.");
828 case kDNSServiceErr_NATTraversal
:
829 return ("Unable to traverse NAT boundary.");
831 case kDNSServiceErr_DoubleNAT
:
832 return ("Unable to traverse double-NAT boundary.");
834 case kDNSServiceErr_BadTime
:
835 return ("Bad system time.");
837 case kDNSServiceErr_BadSig
:
838 return ("Bad signature.");
840 case kDNSServiceErr_BadKey
:
841 return ("Bad encryption key.");
843 case kDNSServiceErr_Transient
:
844 return ("Transient error occurred - please try again.");
846 case kDNSServiceErr_ServiceNotRunning
:
847 return ("Server not running.");
849 case kDNSServiceErr_NATPortMappingUnsupported
:
850 return ("NAT doesn't support NAT-PMP or UPnP.");
852 case kDNSServiceErr_NATPortMappingDisabled
:
853 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
855 case kDNSServiceErr_NoRouter
:
856 return ("No Internet/default router configured.");
858 case kDNSServiceErr_PollingMode
:
859 return ("Service polling mode error.");
861 case kDNSServiceErr_Timeout
:
862 return ("Service timeout.");
865 # else /* HAVE_AVAHI */
866 return (avahi_strerror(error
));
867 # endif /* HAVE_DNSSD */
872 * 'dnssdRegisterCallback()' - Free a TXT record.
876 dnssdFreeTxtRecord(cupsd_txt_t
*txt
) /* I - TXT record */
879 TXTRecordDeallocate(txt
);
881 # else /* HAVE_AVAHI */
882 avahi_string_list_free(*txt
);
884 # endif /* HAVE_DNSSD */
889 * 'dnssdRegisterAllPrinters()' - Register all printers.
893 dnssdRegisterAllPrinters(int from_callback
) /* I - Called from callback? */
895 cupsd_printer_t
*p
; /* Current printer */
901 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
903 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
904 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
905 dnssdRegisterPrinter(p
, from_callback
);
910 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
915 dnssdRegisterCallback(
916 DNSServiceRef sdRef
, /* I - DNS Service reference */
917 DNSServiceFlags flags
, /* I - Reserved for future use */
918 DNSServiceErrorType errorCode
, /* I - Error code */
919 const char *name
, /* I - Service name */
920 const char *regtype
, /* I - Service type */
921 const char *domain
, /* I - Domain. ".local" for now */
922 void *context
) /* I - Printer */
924 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
925 /* Current printer */
932 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
933 name
, regtype
, p
? p
->name
: "Web Interface",
934 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
938 cupsdLogMessage(CUPSD_LOG_ERROR
,
939 "DNSServiceRegister failed with error %d", (int)errorCode
);
942 else if (p
&& (!p
->reg_name
|| _cups_strcasecmp(name
, p
->reg_name
)))
944 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
947 cupsArrayRemove(DNSSDPrinters
, p
);
948 cupsdSetString(&p
->reg_name
, name
);
949 cupsArrayAdd(DNSSDPrinters
, p
);
951 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
955 # else /* HAVE_AVAHI */
957 dnssdRegisterCallback(
958 AvahiEntryGroup
*srv
, /* I - Service */
959 AvahiEntryGroupState state
, /* I - Registration state */
960 void *context
) /* I - Printer */
962 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
963 /* Current printer */
965 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
966 "dnssdRegisterCallback(srv=%p, state=%d, context=%p) "
967 "for %s (%s)", srv
, state
, context
,
968 p
? p
->name
: "Web Interface",
969 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
971 /* TODO: Handle collisions with avahi_alternate_service_name(p->reg_name)? */
973 # endif /* HAVE_DNSSD */
977 * 'dnssdRegisterInstance()' - Register an instance of a printer service.
980 static int /* O - 1 on success, 0 on failure */
981 dnssdRegisterInstance(
982 cupsd_srv_t
*srv
, /* O - Service */
983 cupsd_printer_t
*p
, /* I - Printer */
984 char *name
, /* I - DNS-SD service name */
985 const char *type
, /* I - DNS-SD service type */
986 const char *subtypes
, /* I - Subtypes to register or NULL */
987 int port
, /* I - Port number or 0 */
988 cupsd_txt_t
*txt
, /* I - TXT record */
989 int commit
, /* I - Commit registration? */
990 int from_callback
) /* I - Called from callback? */
992 char temp
[256], /* Temporary string */
993 *ptr
; /* Pointer into string */
994 int error
; /* Any error */
999 # endif /* HAVE_DNSSD */
1001 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Registering \"%s\" with DNS-SD type \"%s\".", name
, type
);
1006 * Assign the correct pointer for "srv"...
1010 if (!strcmp(type
, "_printer._tcp"))
1011 srv
= &p
->printer_srv
; /* Target LPD service */
1013 else if (!strcmp(type
, "_ipps._tcp"))
1014 srv
= &p
->ipps_srv
; /* Target IPPS service */
1015 # endif /* HAVE_SSL */
1017 srv
= &p
->ipp_srv
; /* Target IPP service */
1019 # else /* HAVE_AVAHI */
1020 srv
= &p
->ipp_srv
; /* Target service group */
1021 # endif /* HAVE_DNSSD */
1027 # else /* HAVE_AVAHI */
1029 avahi_threaded_poll_lock(DNSSDMaster
);
1032 *srv
= avahi_entry_group_new(DNSSDClient
, dnssdRegisterCallback
, NULL
);
1036 avahi_threaded_poll_unlock(DNSSDMaster
);
1038 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1039 name
, dnssdErrorString(avahi_client_errno(DNSSDClient
)));
1042 # endif /* HAVE_DNSSD */
1045 * Make sure the name is <= 63 octets, and when we truncate be sure to
1046 * properly truncate any UTF-8 characters...
1049 ptr
= name
+ strlen(name
);
1050 while ((ptr
- name
) > 63)
1056 while (ptr
> name
&& (*ptr
& 0xc0) == 0x80);
1063 * Register the service...
1068 snprintf(temp
, sizeof(temp
), "%s,%s", type
, subtypes
);
1070 strlcpy(temp
, type
, sizeof(temp
));
1073 error
= DNSServiceRegister(srv
, kDNSServiceFlagsShareConnection
,
1074 0, name
, temp
, NULL
, DNSSDHostName
, htons(port
),
1075 txt
? TXTRecordGetLength(txt
) : 0,
1076 txt
? TXTRecordGetBytesPtr(txt
) : NULL
,
1077 dnssdRegisterCallback
, p
);
1079 # else /* HAVE_AVAHI */
1082 AvahiStringList
*temptxt
;
1083 for (temptxt
= *txt
; temptxt
; temptxt
= temptxt
->next
)
1084 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS_SD \"%s\" %s", name
, temptxt
->text
);
1087 error
= avahi_entry_group_add_service_strlst(*srv
, AVAHI_IF_UNSPEC
,
1088 AVAHI_PROTO_UNSPEC
, 0, name
,
1089 type
, NULL
, DNSSDHostName
, port
,
1092 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD service add for \"%s\" failed.",
1095 if (!error
&& subtypes
)
1098 * Register all of the subtypes...
1101 char *start
, /* Start of subtype */
1102 subtype
[256]; /* Subtype string */
1104 strlcpy(temp
, subtypes
, sizeof(temp
));
1106 for (start
= temp
; *start
; start
= ptr
)
1109 * Skip leading whitespace...
1112 while (*start
&& isspace(*start
& 255))
1116 * Grab everything up to the next comma or the end of the string...
1119 for (ptr
= start
; *ptr
&& *ptr
!= ','; ptr
++);
1128 * Register the subtype...
1131 snprintf(subtype
, sizeof(subtype
), "%s._sub.%s", start
, type
);
1133 error
= avahi_entry_group_add_service_subtype(*srv
, AVAHI_IF_UNSPEC
,
1134 AVAHI_PROTO_UNSPEC
, 0,
1135 name
, type
, NULL
, subtype
);
1138 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1139 "DNS-SD subtype %s registration for \"%s\" failed." ,
1146 if (!error
&& commit
)
1148 if ((error
= avahi_entry_group_commit(*srv
)) != 0)
1149 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD commit of \"%s\" failed.",
1154 avahi_threaded_poll_unlock(DNSSDMaster
);
1155 # endif /* HAVE_DNSSD */
1159 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1160 name
, dnssdErrorString(error
));
1161 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD type: %s", type
);
1163 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD sub-types: %s", subtypes
);
1171 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
1172 * or update the broadcast contents.
1176 dnssdRegisterPrinter(
1177 cupsd_printer_t
*p
, /* I - Printer */
1178 int from_callback
) /* I - Called from callback? */
1180 char name
[256]; /* Service name */
1181 int printer_port
; /* LPD port number */
1182 int status
; /* Registration status */
1183 cupsd_txt_t ipp_txt
, /* IPP(S) TXT record */
1184 printer_txt
; /* LPD TXT record */
1187 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
1188 !p
->ipp_srv
? "new" : "update");
1193 # endif /* HAVE_AVAHI */
1196 * Remove the current registrations if we have them and then return if
1197 * per-printer sharing was just disabled...
1200 dnssdDeregisterPrinter(p
, 0, from_callback
);
1206 * Set the registered name as needed; the registered name takes the form of
1207 * "<printer-info> @ <computer name>"...
1212 if (p
->info
&& strlen(p
->info
) > 0)
1214 if (DNSSDComputerName
)
1215 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
1217 strlcpy(name
, p
->info
, sizeof(name
));
1219 else if (DNSSDComputerName
)
1220 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
1222 strlcpy(name
, p
->name
, sizeof(name
));
1225 strlcpy(name
, p
->reg_name
, sizeof(name
));
1228 * Register IPP and LPD...
1230 * We always must register the "_printer" service type in order to reserve
1231 * our name, but use port number 0 if we haven't actually configured cups-lpd
1232 * to share via LPD...
1235 ipp_txt
= dnssdBuildTxtRecord(p
, 0);
1236 printer_txt
= dnssdBuildTxtRecord(p
, 1);
1238 if (BrowseLocalProtocols
& BROWSE_LPD
)
1243 status
= dnssdRegisterInstance(NULL
, p
, name
, "_printer._tcp", NULL
, printer_port
, &printer_txt
, 0, from_callback
);
1247 dnssdRegisterInstance(NULL
, p
, name
, "_ipps._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 0, from_callback
);
1248 # endif /* HAVE_SSL */
1253 * Use the "_fax-ipp" service type for fax queues, otherwise use "_ipp"...
1256 if (p
->type
& CUPS_PRINTER_FAX
)
1257 status
= dnssdRegisterInstance(NULL
, p
, name
, "_fax-ipp._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1, from_callback
);
1259 status
= dnssdRegisterInstance(NULL
, p
, name
, "_ipp._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1, from_callback
);
1262 dnssdFreeTxtRecord(&ipp_txt
);
1263 dnssdFreeTxtRecord(&printer_txt
);
1268 * Save the registered name and add the printer to the array of DNS-SD
1272 cupsdSetString(&p
->reg_name
, name
);
1273 cupsArrayAdd(DNSSDPrinters
, p
);
1278 * Registration failed for this printer...
1281 dnssdDeregisterInstance(&p
->ipp_srv
, from_callback
);
1285 dnssdDeregisterInstance(&p
->ipps_srv
, from_callback
);
1286 # endif /* HAVE_SSL */
1287 dnssdDeregisterInstance(&p
->printer_srv
, from_callback
);
1288 # endif /* HAVE_DNSSD */
1294 * 'dnssdStop()' - Stop all DNS-SD registrations.
1300 cupsd_printer_t
*p
; /* Current printer */
1304 * De-register the individual printers
1307 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1309 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1310 dnssdDeregisterPrinter(p
, 1, 0);
1313 * Shutdown the rest of the service refs...
1316 dnssdDeregisterInstance(&WebIFSrv
, 0);
1319 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDMaster
));
1321 DNSServiceRefDeallocate(DNSSDMaster
);
1324 # else /* HAVE_AVAHI */
1326 avahi_threaded_poll_stop(DNSSDMaster
);
1330 avahi_client_free(DNSSDClient
);
1336 avahi_threaded_poll_free(DNSSDMaster
);
1339 # endif /* HAVE_DNSSD */
1341 cupsArrayDelete(DNSSDPrinters
);
1342 DNSSDPrinters
= NULL
;
1350 * 'dnssdUpdate()' - Handle DNS-SD queries.
1356 DNSServiceErrorType sdErr
; /* Service discovery error */
1359 if ((sdErr
= DNSServiceProcessResult(DNSSDMaster
)) != kDNSServiceErr_NoError
)
1361 cupsdLogMessage(CUPSD_LOG_ERROR
,
1362 "DNS Service Discovery registration error %d!",
1367 # endif /* HAVE_DNSSD */
1371 * 'dnssdUpdateDNSSDName()' - Update the listen port, computer name, and web interface registration.
1375 dnssdUpdateDNSSDName(int from_callback
) /* I - Called from callback? */
1377 char webif
[1024]; /* Web interface share name */
1379 SCDynamicStoreRef sc
; /* Context for dynamic store */
1380 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1381 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1382 CFStringRef nameRef
; /* Host name CFString */
1383 char nameBuffer
[1024]; /* C-string buffer */
1384 # endif /* __APPLE__ */
1388 * Only share the web interface and printers when non-local listening is
1395 * Get the port we use for registrations. If we are not listening on any
1396 * non-local ports, there is no sense sharing local printers via Bonjour...
1399 cupsd_listener_t
*lis
; /* Current listening socket */
1401 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1403 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1405 if (httpAddrLocalhost(&(lis
->address
)))
1408 DNSSDPort
= httpAddrPort(&(lis
->address
));
1417 * Get the computer name as a c-string...
1421 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1426 * Get the computer name from the dynamic store...
1429 cupsdClearString(&DNSSDComputerName
);
1431 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1433 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1434 kCFStringEncodingUTF8
))
1436 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1437 "Dynamic store computer name is \"%s\".", nameBuffer
);
1438 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1444 if (!DNSSDComputerName
)
1447 * Use the ServerName instead...
1450 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1451 "Using ServerName \"%s\" as computer name.", ServerName
);
1452 cupsdSetString(&DNSSDComputerName
, ServerName
);
1458 * Get the local hostname from the dynamic store...
1461 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1463 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1464 kCFStringEncodingUTF8
))
1466 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1467 "Dynamic store host name is \"%s\".", nameBuffer
);
1468 cupsdSetString(&DNSSDHostName
, nameBuffer
);
1478 * Use the ServerName instead...
1481 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1482 "Using ServerName \"%s\" as host name.", ServerName
);
1483 cupsdSetString(&DNSSDHostName
, ServerName
);
1487 * Get any Back-to-My-Mac domains and add them as aliases...
1490 cupsdFreeAliases(DNSSDAlias
);
1493 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
1494 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
1496 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
1497 (int)CFDictionaryGetCount(btmm
));
1498 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
1501 cupsdLogMessage(CUPSD_LOG_ERROR
,
1502 "Bad Back to My Mac data in dynamic store!");
1504 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
1512 # endif /* __APPLE__ */
1516 const char *host_name
= avahi_client_get_host_name(DNSSDClient
);
1518 cupsdSetString(&DNSSDComputerName
, host_name
? host_name
: ServerName
);
1522 const char *host_fqdn
= avahi_client_get_host_name_fqdn(DNSSDClient
);
1525 cupsdSetString(&DNSSDHostName
, host_fqdn
);
1526 else if (strchr(ServerName
, '.'))
1527 cupsdSetString(&DNSSDHostName
, ServerName
);
1529 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
1533 # endif /* HAVE_AVAHI */
1535 cupsdSetString(&DNSSDComputerName
, ServerName
);
1539 if (strchr(ServerName
, '.'))
1540 cupsdSetString(&DNSSDHostName
, ServerName
);
1542 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
1547 * Then (re)register the web interface if enabled...
1552 if (DNSSDComputerName
)
1553 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
1555 strlcpy(webif
, "CUPS", sizeof(webif
));
1557 dnssdDeregisterInstance(&WebIFSrv
, from_callback
);
1558 dnssdRegisterInstance(&WebIFSrv
, NULL
, webif
, "_http._tcp", "_printer", DNSSDPort
, NULL
, 1, from_callback
);
1564 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
1567 static char * /* O - String or NULL if none */
1568 get_auth_info_required(
1569 cupsd_printer_t
*p
, /* I - Printer */
1570 char *buffer
, /* I - Value buffer */
1571 size_t bufsize
) /* I - Size of value buffer */
1573 cupsd_location_t
*auth
; /* Pointer to authentication element */
1574 char resource
[1024]; /* Printer/class resource path */
1578 * If auth-info-required is set for this printer, return that...
1581 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
1583 int i
; /* Looping var */
1584 char *bufptr
; /* Pointer into buffer */
1586 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
1588 if (bufptr
>= (buffer
+ bufsize
- 2))
1594 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (size_t)(bufptr
- buffer
));
1595 bufptr
+= strlen(bufptr
);
1602 * Figure out the authentication data requirements to advertise...
1605 if (p
->type
& CUPS_PRINTER_CLASS
)
1606 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1608 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1610 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
1611 auth
->type
== CUPSD_AUTH_NONE
)
1612 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1616 int auth_type
; /* Authentication type */
1618 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
1619 auth_type
= cupsdDefaultAuthType();
1623 case CUPSD_AUTH_NONE
:
1626 case CUPSD_AUTH_NEGOTIATE
:
1627 strlcpy(buffer
, "negotiate", bufsize
);
1631 strlcpy(buffer
, "username,password", bufsize
);
1640 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1645 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
1648 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
1649 get_hostconfig(const char *name
) /* I - Name of service */
1651 cups_file_t
*fp
; /* Hostconfig file */
1652 char line
[1024], /* Line from file */
1653 *ptr
; /* Pointer to value */
1654 int state
= 1; /* State of service */
1658 * Try opening the /etc/hostconfig file; if we can't open it, assume that
1659 * the service is enabled/auto.
1662 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
1665 * Read lines from the file until we find the service...
1668 while (cupsFileGets(fp
, line
, sizeof(line
)))
1670 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
1675 if (!_cups_strcasecmp(line
, name
))
1678 * Found the service, see if it is set to "-NO-"...
1681 if (!_cups_strncasecmp(ptr
, "-NO-", 4))
1692 #endif /* __APPLE__ */
1696 * 'update_lpd()' - Update the LPD configuration as needed.
1700 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
1707 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
1708 * setting for backwards-compatibility.
1711 if (onoff
&& !get_hostconfig("CUPS_LPD"))
1713 #endif /* __APPLE__ */
1715 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
1718 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
1721 char newfile
[1024]; /* New cups-lpd.N file */
1722 cups_file_t
*ofp
, /* Original file pointer */
1723 *nfp
; /* New file pointer */
1724 char line
[1024]; /* Line from file */
1727 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
1729 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
1731 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1732 LPDConfigFile
+ 9, strerror(errno
));
1736 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1738 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1739 newfile
, strerror(errno
));
1745 * Copy all of the lines from the cups-lpd file...
1748 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1752 cupsFilePrintf(nfp
, "%s\n", line
);
1753 snprintf(line
, sizeof(line
), "\tdisable = %s",
1754 onoff
? "no" : "yes");
1756 else if (!strstr(line
, "disable ="))
1757 cupsFilePrintf(nfp
, "%s\n", line
);
1762 rename(newfile
, LPDConfigFile
+ 9);
1765 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
1768 * Enable/disable LPD via the launchctl command...
1771 char *argv
[5], /* Arguments for command */
1772 *envp
[MAX_ENV
]; /* Environment for command */
1773 int pid
; /* Process ID */
1776 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1777 argv
[0] = (char *)"launchctl";
1778 argv
[1] = (char *)(onoff
? "load" : "unload");
1779 argv
[2] = (char *)"-w";
1780 argv
[3] = LPDConfigFile
+ 10;
1783 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
1786 #endif /* __APPLE__ */
1788 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
1793 * 'update_smb()' - Update the SMB configuration as needed.
1797 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
1802 if (!strncmp(SMBConfigFile
, "samba:///", 9))
1805 * Enable/disable SMB via the specified smb.conf config file...
1808 char newfile
[1024]; /* New smb.conf.N file */
1809 cups_file_t
*ofp
, /* Original file pointer */
1810 *nfp
; /* New file pointer */
1811 char line
[1024]; /* Line from file */
1812 int in_printers
; /* In [printers] section? */
1815 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
1817 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
1819 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1820 SMBConfigFile
+ 8, strerror(errno
));
1824 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1826 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1827 newfile
, strerror(errno
));
1833 * Copy all of the lines from the smb.conf file...
1838 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1840 if (in_printers
&& strstr(line
, "printable ="))
1841 snprintf(line
, sizeof(line
), " printable = %s",
1842 onoff
? "yes" : "no");
1844 cupsFilePrintf(nfp
, "%s\n", line
);
1847 in_printers
= !strcmp(line
, "[printers]");
1852 rename(newfile
, SMBConfigFile
+ 8);
1855 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");