2 * Directory services routines for the CUPS scheduler.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
21 #if defined(HAVE_DNSSD) && defined(__APPLE__)
23 # include <CoreFoundation/CoreFoundation.h>
24 # include <SystemConfiguration/SystemConfiguration.h>
25 #endif /* HAVE_DNSSD && __APPLE__ */
33 static int avahi_running
= 0;
34 #endif /* HAVE_AVAHI */
41 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
42 static char *get_auth_info_required(cupsd_printer_t
*p
,
43 char *buffer
, size_t bufsize
);
44 #endif /* HAVE_DNSSD || HAVE_AVAHI */
46 static int get_hostconfig(const char *name
);
47 #endif /* __APPLE__ */
48 static void update_lpd(int onoff
);
49 static void update_smb(int onoff
);
52 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
54 static void dnssdAddAlias(const void *key
, const void *value
,
56 # endif /* __APPLE__ */
57 static cupsd_txt_t
dnssdBuildTxtRecord(cupsd_printer_t
*p
, int for_lpd
);
59 static void dnssdClientCallback(AvahiClient
*c
, AvahiClientState state
, void *userdata
);
60 # endif /* HAVE_AVAHI */
61 static void dnssdDeregisterAllPrinters(int from_callback
);
62 static void dnssdDeregisterInstance(cupsd_srv_t
*srv
, int from_callback
);
63 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
, int clear_name
, int from_callback
);
64 static const char *dnssdErrorString(int error
);
65 static void dnssdFreeTxtRecord(cupsd_txt_t
*txt
);
66 static void dnssdRegisterAllPrinters(int from_callback
);
68 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
69 DNSServiceFlags flags
,
70 DNSServiceErrorType errorCode
,
76 static void dnssdRegisterCallback(AvahiEntryGroup
*p
,
77 AvahiEntryGroupState state
,
79 # endif /* HAVE_DNSSD */
80 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
);
81 static void dnssdRegisterPrinter(cupsd_printer_t
*p
, int from_callback
);
82 static void dnssdStop(void);
84 static void dnssdUpdate(void);
85 # endif /* HAVE_DNSSD */
86 static void dnssdUpdateDNSSDName(int from_callback
);
87 #endif /* HAVE_DNSSD || HAVE_AVAHI */
91 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
92 * local printer and remove any pending
93 * references to remote printers.
97 cupsdDeregisterPrinter(
98 cupsd_printer_t
*p
, /* I - Printer to register */
99 int removeit
) /* I - Printer being permanently removed */
102 * Only deregister if browsing is enabled and it's a local printer...
105 cupsdLogMessage(CUPSD_LOG_DEBUG
,
106 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
109 if (!Browsing
|| !p
->shared
||
110 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
114 * Announce the deletion...
117 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
118 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
119 dnssdDeregisterPrinter(p
, 1, 0);
120 #endif /* HAVE_DNSSD || HAVE_AVAHI */
125 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
126 * printer or update the broadcast contents.
130 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
132 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
135 if (!Browsing
|| !BrowseLocalProtocols
||
136 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
139 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
140 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
141 dnssdRegisterPrinter(p
, 0);
142 #endif /* HAVE_DNSSD || HAVE_AVAHI */
147 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
151 cupsdStartBrowsing(void)
153 if (!Browsing
|| !BrowseLocalProtocols
)
156 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
157 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
160 DNSServiceErrorType error
; /* Error from service creation */
163 * First create a "master" connection for all registrations...
166 if ((error
= DNSServiceCreateConnection(&DNSSDMaster
))
167 != kDNSServiceErr_NoError
)
169 cupsdLogMessage(CUPSD_LOG_ERROR
,
170 "Unable to create master DNS-SD reference: %d", error
);
172 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
173 cupsdEndProcess(getpid(), 0);
178 * Add the master connection to the select list...
181 int fd
= DNSServiceRefSockFD(DNSSDMaster
);
183 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
185 cupsdAddSelect(fd
, (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
189 * Set the computer name and register the web interface...
193 cupsdUpdateDNSSDName();
195 # else /* HAVE_AVAHI */
196 if ((DNSSDMaster
= avahi_threaded_poll_new()) == NULL
)
198 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create DNS-SD thread.");
200 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
201 cupsdEndProcess(getpid(), 0);
205 int error
; /* Error code, if any */
207 DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), AVAHI_CLIENT_NO_FAIL
, dnssdClientCallback
, NULL
, &error
);
209 if (DNSSDClient
== NULL
)
211 cupsdLogMessage(CUPSD_LOG_ERROR
,
212 "Unable to communicate with avahi-daemon: %s",
213 dnssdErrorString(error
));
215 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
216 cupsdEndProcess(getpid(), 0);
218 avahi_threaded_poll_free(DNSSDMaster
);
222 avahi_threaded_poll_start(DNSSDMaster
);
224 # endif /* HAVE_DNSSD */
226 #endif /* HAVE_DNSSD || HAVE_AVAHI */
229 * Enable LPD and SMB printer sharing as needed through external programs...
232 if (BrowseLocalProtocols
& BROWSE_LPD
)
235 if (BrowseLocalProtocols
& BROWSE_SMB
)
238 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
240 * Register the individual printers
243 dnssdRegisterAllPrinters(0);
244 #endif /* HAVE_DNSSD || HAVE_AVAHI */
249 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
253 cupsdStopBrowsing(void)
255 if (!Browsing
|| !BrowseLocalProtocols
)
258 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
260 * De-register the individual printers
263 dnssdDeregisterAllPrinters(0);
266 * Shut down browsing sockets...
269 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDMaster
)
271 #endif /* HAVE_DNSSD || HAVE_AVAHI */
274 * Disable LPD and SMB printer sharing as needed through external programs...
277 if (BrowseLocalProtocols
& BROWSE_LPD
)
280 if (BrowseLocalProtocols
& BROWSE_SMB
)
285 #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
287 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
291 cupsdUpdateDNSSDName(void)
293 dnssdUpdateDNSSDName(0);
299 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
303 dnssdAddAlias(const void *key
, /* I - Key */
304 const void *value
, /* I - Value (domain) */
305 void *context
) /* I - Unused */
307 char valueStr
[1024], /* Domain string */
308 hostname
[1024], /* Complete hostname */
309 *hostptr
; /* Pointer into hostname */
315 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
316 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
317 kCFStringEncodingUTF8
))
319 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
320 hostptr
= hostname
+ strlen(hostname
) - 1;
322 *hostptr
= '\0'; /* Strip trailing dot */
325 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
327 cupsdAddAlias(DNSSDAlias
, hostname
);
328 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
332 cupsdLogMessage(CUPSD_LOG_ERROR
,
333 "Bad Back to My Mac domain in dynamic store!");
335 # endif /* __APPLE__ */
339 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
342 static cupsd_txt_t
/* O - TXT record */
344 cupsd_printer_t
*p
, /* I - Printer information */
345 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
347 int i
, /* Looping var */
348 count
; /* Count of key/value pairs */
349 char admin_hostname
[256], /* .local hostname for admin page */
350 adminurl_str
[256], /* URL for the admin page */
351 type_str
[32], /* Type to string buffer */
352 state_str
[32], /* State to string buffer */
353 rp_str
[1024], /* Queue name string buffer */
354 air_str
[1024], /* auth-info-required string buffer */
355 *keyvalue
[32][2]; /* Table of key/value pairs */
356 cupsd_txt_t txt
; /* TXT record */
360 * Load up the key value pairs...
365 if (!for_lpd
|| (BrowseLocalProtocols
& BROWSE_LPD
))
367 keyvalue
[count
][0] = "txtvers";
368 keyvalue
[count
++][1] = "1";
370 keyvalue
[count
][0] = "qtotal";
371 keyvalue
[count
++][1] = "1";
373 keyvalue
[count
][0] = "rp";
374 keyvalue
[count
++][1] = rp_str
;
376 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
378 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
379 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
382 keyvalue
[count
][0] = "ty";
383 keyvalue
[count
++][1] = p
->make_model
? p
->make_model
: "Unknown";
385 if (strstr(DNSSDHostName
, ".local"))
386 strlcpy(admin_hostname
, DNSSDHostName
, sizeof(admin_hostname
));
388 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.local.",
390 httpAssembleURIf(HTTP_URI_CODING_ALL
, adminurl_str
, sizeof(adminurl_str
),
395 # endif /* HAVE_SSL */
396 NULL
, admin_hostname
, DNSSDPort
, "/%s/%s",
397 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
399 keyvalue
[count
][0] = "adminurl";
400 keyvalue
[count
++][1] = adminurl_str
;
404 keyvalue
[count
][0] = "note";
405 keyvalue
[count
++][1] = p
->location
;
408 keyvalue
[count
][0] = "priority";
409 keyvalue
[count
++][1] = for_lpd
? "100" : "0";
411 keyvalue
[count
][0] = "product";
412 keyvalue
[count
++][1] = p
->pc
&& p
->pc
->product
? p
->pc
->product
: "Unknown";
414 keyvalue
[count
][0] = "pdl";
415 keyvalue
[count
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
417 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
419 keyvalue
[count
][0] = "air";
420 keyvalue
[count
++][1] = air_str
;
423 keyvalue
[count
][0] = "UUID";
424 keyvalue
[count
++][1] = p
->uuid
+ 9;
427 keyvalue
[count
][0] = "TLS";
428 keyvalue
[count
++][1] = "1.2";
429 #endif /* HAVE_SSL */
431 if (p
->type
& CUPS_PRINTER_FAX
)
433 keyvalue
[count
][0] = "Fax";
434 keyvalue
[count
++][1] = "T";
435 keyvalue
[count
][0] = "rfo";
436 keyvalue
[count
++][1] = rp_str
;
439 if (p
->type
& CUPS_PRINTER_COLOR
)
441 keyvalue
[count
][0] = "Color";
442 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLOR
) ? "T" : "F";
445 if (p
->type
& CUPS_PRINTER_DUPLEX
)
447 keyvalue
[count
][0] = "Duplex";
448 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_DUPLEX
) ? "T" : "F";
451 if (p
->type
& CUPS_PRINTER_STAPLE
)
453 keyvalue
[count
][0] = "Staple";
454 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_STAPLE
) ? "T" : "F";
457 if (p
->type
& CUPS_PRINTER_COPIES
)
459 keyvalue
[count
][0] = "Copies";
460 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COPIES
) ? "T" : "F";
463 if (p
->type
& CUPS_PRINTER_COLLATE
)
465 keyvalue
[count
][0] = "Collate";
466 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_COLLATE
) ? "T" : "F";
469 if (p
->type
& CUPS_PRINTER_PUNCH
)
471 keyvalue
[count
][0] = "Punch";
472 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_PUNCH
) ? "T" : "F";
475 if (p
->type
& CUPS_PRINTER_BIND
)
477 keyvalue
[count
][0] = "Bind";
478 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_BIND
) ? "T" : "F";
481 if (p
->type
& CUPS_PRINTER_SORT
)
483 keyvalue
[count
][0] = "Sort";
484 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_SORT
) ? "T" : "F";
487 if (p
->type
& CUPS_PRINTER_MFP
)
489 keyvalue
[count
][0] = "Scan";
490 keyvalue
[count
++][1] = (p
->type
& CUPS_PRINTER_MFP
) ? "T" : "F";
493 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
494 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
496 keyvalue
[count
][0] = "printer-state";
497 keyvalue
[count
++][1] = state_str
;
499 keyvalue
[count
][0] = "printer-type";
500 keyvalue
[count
++][1] = type_str
;
504 * Then pack them into a proper txt record...
508 TXTRecordCreate(&txt
, 0, NULL
);
510 for (i
= 0; i
< count
; i
++)
512 size_t len
= strlen(keyvalue
[i
][1]);
515 TXTRecordSetValue(&txt
, keyvalue
[i
][0], (uint8_t)len
, keyvalue
[i
][1]);
519 for (i
= 0, txt
= NULL
; i
< count
; i
++)
520 txt
= avahi_string_list_add_printf(txt
, "%s=%s", keyvalue
[i
][0],
522 # endif /* HAVE_DNSSD */
530 * 'dnssdClientCallback()' - Client callback for Avahi.
532 * Called whenever the client or server state changes...
537 AvahiClient
*c
, /* I - Client */
538 AvahiClientState state
, /* I - Current state */
539 void *userdata
) /* I - User data (unused) */
541 int error
; /* Error code, if any */
550 * Make sure DNSSDClient is already set also if this callback function is
551 * already running before avahi_client_new() in dnssdStartBrowsing()
560 case AVAHI_CLIENT_S_REGISTERING
:
561 case AVAHI_CLIENT_S_RUNNING
:
562 case AVAHI_CLIENT_S_COLLISION
:
563 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Avahi server connection now available, registering printers for Bonjour broadcasting.");
566 * Mark that Avahi server is running...
572 * Set the computer name and register the web interface...
576 dnssdUpdateDNSSDName(1);
579 * Register the individual printers
582 dnssdRegisterAllPrinters(1);
585 case AVAHI_CLIENT_FAILURE
:
586 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
)
588 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Avahi server disappeared, unregistering printers for Bonjour broadcasting.");
591 * Unregister everything and close the client...
594 dnssdDeregisterAllPrinters(1);
595 dnssdDeregisterInstance(&WebIFSrv
, 1);
596 avahi_client_free(DNSSDClient
);
600 * Mark that Avahi server is not running...
606 * Renew Avahi client...
609 DNSSDClient
= avahi_client_new(avahi_threaded_poll_get(DNSSDMaster
), AVAHI_CLIENT_NO_FAIL
, dnssdClientCallback
, NULL
, &error
);
613 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to communicate with avahi-daemon: %s", dnssdErrorString(error
));
614 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
615 cupsdEndProcess(getpid(), 0);
620 cupsdLogMessage(CUPSD_LOG_ERROR
, "Communication with avahi-daemon has failed: %s", avahi_strerror(avahi_client_errno(c
)));
621 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
622 cupsdEndProcess(getpid(), 0);
630 # endif /* HAVE_AVAHI */
634 * 'dnssdDeregisterAllPrinters()' - Deregister all printers.
638 dnssdDeregisterAllPrinters(
639 int from_callback
) /* I - Deregistering because of callback? */
641 cupsd_printer_t
*p
; /* Current printer */
647 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
649 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
650 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
651 dnssdDeregisterPrinter(p
, 1, from_callback
);
656 * 'dnssdDeregisterInstance()' - Deregister a DNS-SD service instance.
660 dnssdDeregisterInstance(
661 cupsd_srv_t
*srv
, /* I - Service */
662 int from_callback
) /* I - Called from callback? */
670 DNSServiceRefDeallocate(*srv
);
672 # else /* HAVE_AVAHI */
674 avahi_threaded_poll_lock(DNSSDMaster
);
676 avahi_entry_group_free(*srv
);
679 avahi_threaded_poll_unlock(DNSSDMaster
);
680 # endif /* HAVE_DNSSD */
687 * 'dnssdDeregisterPrinter()' - Deregister all services for a printer.
691 dnssdDeregisterPrinter(
692 cupsd_printer_t
*p
, /* I - Printer */
693 int clear_name
, /* I - Clear the name? */
694 int from_callback
) /* I - Called from callback? */
697 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
698 "dnssdDeregisterPrinter(p=%p(%s), clear_name=%d)", p
, p
->name
,
703 dnssdDeregisterInstance(&p
->ipp_srv
, from_callback
);
707 dnssdDeregisterInstance(&p
->ipps_srv
, from_callback
);
708 # endif /* HAVE_SSL */
709 dnssdDeregisterInstance(&p
->printer_srv
, from_callback
);
710 # endif /* HAVE_DNSSD */
714 * Remove the printer from the array of DNS-SD printers but keep the
718 cupsArrayRemove(DNSSDPrinters
, p
);
721 * Optionally clear the service name...
725 cupsdClearString(&p
->reg_name
);
730 * 'dnssdErrorString()' - Return an error string for an error code.
733 static const char * /* O - Error message */
734 dnssdErrorString(int error
) /* I - Error number */
739 case kDNSServiceErr_NoError
:
743 case kDNSServiceErr_Unknown
:
744 return ("Unknown error.");
746 case kDNSServiceErr_NoSuchName
:
747 return ("Service not found.");
749 case kDNSServiceErr_NoMemory
:
750 return ("Out of memory.");
752 case kDNSServiceErr_BadParam
:
753 return ("Bad parameter.");
755 case kDNSServiceErr_BadReference
:
756 return ("Bad service reference.");
758 case kDNSServiceErr_BadState
:
759 return ("Bad state.");
761 case kDNSServiceErr_BadFlags
:
762 return ("Bad flags.");
764 case kDNSServiceErr_Unsupported
:
765 return ("Unsupported.");
767 case kDNSServiceErr_NotInitialized
:
768 return ("Not initialized.");
770 case kDNSServiceErr_AlreadyRegistered
:
771 return ("Already registered.");
773 case kDNSServiceErr_NameConflict
:
774 return ("Name conflict.");
776 case kDNSServiceErr_Invalid
:
777 return ("Invalid name.");
779 case kDNSServiceErr_Firewall
:
780 return ("Firewall prevents registration.");
782 case kDNSServiceErr_Incompatible
:
783 return ("Client library incompatible.");
785 case kDNSServiceErr_BadInterfaceIndex
:
786 return ("Bad interface index.");
788 case kDNSServiceErr_Refused
:
789 return ("Server prevents registration.");
791 case kDNSServiceErr_NoSuchRecord
:
792 return ("Record not found.");
794 case kDNSServiceErr_NoAuth
:
795 return ("Authentication required.");
797 case kDNSServiceErr_NoSuchKey
:
798 return ("Encryption key not found.");
800 case kDNSServiceErr_NATTraversal
:
801 return ("Unable to traverse NAT boundary.");
803 case kDNSServiceErr_DoubleNAT
:
804 return ("Unable to traverse double-NAT boundary.");
806 case kDNSServiceErr_BadTime
:
807 return ("Bad system time.");
809 case kDNSServiceErr_BadSig
:
810 return ("Bad signature.");
812 case kDNSServiceErr_BadKey
:
813 return ("Bad encryption key.");
815 case kDNSServiceErr_Transient
:
816 return ("Transient error occurred - please try again.");
818 case kDNSServiceErr_ServiceNotRunning
:
819 return ("Server not running.");
821 case kDNSServiceErr_NATPortMappingUnsupported
:
822 return ("NAT doesn't support NAT-PMP or UPnP.");
824 case kDNSServiceErr_NATPortMappingDisabled
:
825 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
827 case kDNSServiceErr_NoRouter
:
828 return ("No Internet/default router configured.");
830 case kDNSServiceErr_PollingMode
:
831 return ("Service polling mode error.");
833 case kDNSServiceErr_Timeout
:
834 return ("Service timeout.");
837 # else /* HAVE_AVAHI */
838 return (avahi_strerror(error
));
839 # endif /* HAVE_DNSSD */
844 * 'dnssdRegisterCallback()' - Free a TXT record.
848 dnssdFreeTxtRecord(cupsd_txt_t
*txt
) /* I - TXT record */
851 TXTRecordDeallocate(txt
);
853 # else /* HAVE_AVAHI */
854 avahi_string_list_free(*txt
);
856 # endif /* HAVE_DNSSD */
861 * 'dnssdRegisterAllPrinters()' - Register all printers.
865 dnssdRegisterAllPrinters(int from_callback
) /* I - Called from callback? */
867 cupsd_printer_t
*p
; /* Current printer */
873 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
875 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
876 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_SCANNER
)))
877 dnssdRegisterPrinter(p
, from_callback
);
882 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
887 dnssdRegisterCallback(
888 DNSServiceRef sdRef
, /* I - DNS Service reference */
889 DNSServiceFlags flags
, /* I - Reserved for future use */
890 DNSServiceErrorType errorCode
, /* I - Error code */
891 const char *name
, /* I - Service name */
892 const char *regtype
, /* I - Service type */
893 const char *domain
, /* I - Domain. ".local" for now */
894 void *context
) /* I - Printer */
896 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
897 /* Current printer */
904 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
905 name
, regtype
, p
? p
->name
: "Web Interface",
906 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
910 cupsdLogMessage(CUPSD_LOG_ERROR
,
911 "DNSServiceRegister failed with error %d", (int)errorCode
);
914 else if (p
&& (!p
->reg_name
|| _cups_strcasecmp(name
, p
->reg_name
)))
916 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
919 cupsArrayRemove(DNSSDPrinters
, p
);
920 cupsdSetString(&p
->reg_name
, name
);
921 cupsArrayAdd(DNSSDPrinters
, p
);
923 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
927 # else /* HAVE_AVAHI */
929 dnssdRegisterCallback(
930 AvahiEntryGroup
*srv
, /* I - Service */
931 AvahiEntryGroupState state
, /* I - Registration state */
932 void *context
) /* I - Printer */
934 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
935 /* Current printer */
937 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
938 "dnssdRegisterCallback(srv=%p, state=%d, context=%p) "
939 "for %s (%s)", srv
, state
, context
,
940 p
? p
->name
: "Web Interface",
941 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
943 /* TODO: Handle collisions with avahi_alternate_service_name(p->reg_name)? */
945 # endif /* HAVE_DNSSD */
949 * 'dnssdRegisterInstance()' - Register an instance of a printer service.
952 static int /* O - 1 on success, 0 on failure */
953 dnssdRegisterInstance(
954 cupsd_srv_t
*srv
, /* O - Service */
955 cupsd_printer_t
*p
, /* I - Printer */
956 char *name
, /* I - DNS-SD service name */
957 const char *type
, /* I - DNS-SD service type */
958 const char *subtypes
, /* I - Subtypes to register or NULL */
959 int port
, /* I - Port number or 0 */
960 cupsd_txt_t
*txt
, /* I - TXT record */
961 int commit
, /* I - Commit registration? */
962 int from_callback
) /* I - Called from callback? */
964 char temp
[256], /* Temporary string */
965 *ptr
; /* Pointer into string */
966 int error
; /* Any error */
971 # endif /* HAVE_DNSSD */
973 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Registering \"%s\" with DNS-SD type \"%s\".", name
, type
);
978 * Assign the correct pointer for "srv"...
982 if (!strcmp(type
, "_printer._tcp"))
983 srv
= &p
->printer_srv
; /* Target LPD service */
985 else if (!strcmp(type
, "_ipps._tcp"))
986 srv
= &p
->ipps_srv
; /* Target IPPS service */
987 # endif /* HAVE_SSL */
989 srv
= &p
->ipp_srv
; /* Target IPP service */
991 # else /* HAVE_AVAHI */
992 srv
= &p
->ipp_srv
; /* Target service group */
993 # endif /* HAVE_DNSSD */
999 # else /* HAVE_AVAHI */
1001 avahi_threaded_poll_lock(DNSSDMaster
);
1004 *srv
= avahi_entry_group_new(DNSSDClient
, dnssdRegisterCallback
, NULL
);
1008 avahi_threaded_poll_unlock(DNSSDMaster
);
1010 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1011 name
, dnssdErrorString(avahi_client_errno(DNSSDClient
)));
1014 # endif /* HAVE_DNSSD */
1017 * Make sure the name is <= 63 octets, and when we truncate be sure to
1018 * properly truncate any UTF-8 characters...
1021 ptr
= name
+ strlen(name
);
1022 while ((ptr
- name
) > 63)
1028 while (ptr
> name
&& (*ptr
& 0xc0) == 0x80);
1035 * Register the service...
1040 snprintf(temp
, sizeof(temp
), "%s,%s", type
, subtypes
);
1042 strlcpy(temp
, type
, sizeof(temp
));
1045 error
= DNSServiceRegister(srv
, kDNSServiceFlagsShareConnection
,
1046 0, name
, temp
, NULL
, NULL
, htons(port
),
1047 txt
? TXTRecordGetLength(txt
) : 0,
1048 txt
? TXTRecordGetBytesPtr(txt
) : NULL
,
1049 dnssdRegisterCallback
, p
);
1051 # else /* HAVE_AVAHI */
1054 AvahiStringList
*temptxt
;
1055 for (temptxt
= *txt
; temptxt
; temptxt
= temptxt
->next
)
1056 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS_SD \"%s\" %s", name
, temptxt
->text
);
1059 error
= avahi_entry_group_add_service_strlst(*srv
, AVAHI_IF_UNSPEC
,
1060 AVAHI_PROTO_UNSPEC
, 0, name
,
1061 type
, NULL
, NULL
, port
,
1064 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD service add for \"%s\" failed.",
1067 if (!error
&& subtypes
)
1070 * Register all of the subtypes...
1073 char *start
, /* Start of subtype */
1074 subtype
[256]; /* Subtype string */
1076 strlcpy(temp
, subtypes
, sizeof(temp
));
1078 for (start
= temp
; *start
; start
= ptr
)
1081 * Skip leading whitespace...
1084 while (*start
&& isspace(*start
& 255))
1088 * Grab everything up to the next comma or the end of the string...
1091 for (ptr
= start
; *ptr
&& *ptr
!= ','; ptr
++);
1100 * Register the subtype...
1103 snprintf(subtype
, sizeof(subtype
), "%s._sub.%s", start
, type
);
1105 error
= avahi_entry_group_add_service_subtype(*srv
, AVAHI_IF_UNSPEC
,
1106 AVAHI_PROTO_UNSPEC
, 0,
1107 name
, type
, NULL
, subtype
);
1110 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1111 "DNS-SD subtype %s registration for \"%s\" failed." ,
1118 if (!error
&& commit
)
1120 if ((error
= avahi_entry_group_commit(*srv
)) != 0)
1121 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD commit of \"%s\" failed.",
1126 avahi_threaded_poll_unlock(DNSSDMaster
);
1127 # endif /* HAVE_DNSSD */
1131 cupsdLogMessage(CUPSD_LOG_WARN
, "DNS-SD registration of \"%s\" failed: %s",
1132 name
, dnssdErrorString(error
));
1133 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD type: %s", type
);
1135 cupsdLogMessage(CUPSD_LOG_DEBUG
, "DNS-SD sub-types: %s", subtypes
);
1143 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
1144 * or update the broadcast contents.
1148 dnssdRegisterPrinter(
1149 cupsd_printer_t
*p
, /* I - Printer */
1150 int from_callback
) /* I - Called from callback? */
1152 char name
[256]; /* Service name */
1153 int printer_port
; /* LPD port number */
1154 int status
; /* Registration status */
1155 cupsd_txt_t ipp_txt
, /* IPP(S) TXT record */
1156 printer_txt
; /* LPD TXT record */
1159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
1160 !p
->ipp_srv
? "new" : "update");
1165 # endif /* HAVE_AVAHI */
1168 * Remove the current registrations if we have them and then return if
1169 * per-printer sharing was just disabled...
1172 dnssdDeregisterPrinter(p
, 0, from_callback
);
1178 * Set the registered name as needed; the registered name takes the form of
1179 * "<printer-info> @ <computer name>"...
1184 if (p
->info
&& strlen(p
->info
) > 0)
1186 if (DNSSDComputerName
)
1187 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
1189 strlcpy(name
, p
->info
, sizeof(name
));
1191 else if (DNSSDComputerName
)
1192 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
1194 strlcpy(name
, p
->name
, sizeof(name
));
1197 strlcpy(name
, p
->reg_name
, sizeof(name
));
1200 * Register IPP and LPD...
1202 * We always must register the "_printer" service type in order to reserve
1203 * our name, but use port number 0 if we haven't actually configured cups-lpd
1204 * to share via LPD...
1207 ipp_txt
= dnssdBuildTxtRecord(p
, 0);
1208 printer_txt
= dnssdBuildTxtRecord(p
, 1);
1210 if (BrowseLocalProtocols
& BROWSE_LPD
)
1215 status
= dnssdRegisterInstance(NULL
, p
, name
, "_printer._tcp", NULL
, printer_port
, &printer_txt
, 0, from_callback
);
1219 dnssdRegisterInstance(NULL
, p
, name
, "_ipps._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 0, from_callback
);
1220 # endif /* HAVE_SSL */
1225 * Use the "_fax-ipp" service type for fax queues, otherwise use "_ipp"...
1228 if (p
->type
& CUPS_PRINTER_FAX
)
1229 status
= dnssdRegisterInstance(NULL
, p
, name
, "_fax-ipp._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1, from_callback
);
1231 status
= dnssdRegisterInstance(NULL
, p
, name
, "_ipp._tcp", DNSSDSubTypes
, DNSSDPort
, &ipp_txt
, 1, from_callback
);
1234 dnssdFreeTxtRecord(&ipp_txt
);
1235 dnssdFreeTxtRecord(&printer_txt
);
1240 * Save the registered name and add the printer to the array of DNS-SD
1244 cupsdSetString(&p
->reg_name
, name
);
1245 cupsArrayAdd(DNSSDPrinters
, p
);
1250 * Registration failed for this printer...
1253 dnssdDeregisterInstance(&p
->ipp_srv
, from_callback
);
1257 dnssdDeregisterInstance(&p
->ipps_srv
, from_callback
);
1258 # endif /* HAVE_SSL */
1259 dnssdDeregisterInstance(&p
->printer_srv
, from_callback
);
1260 # endif /* HAVE_DNSSD */
1266 * 'dnssdStop()' - Stop all DNS-SD registrations.
1272 cupsd_printer_t
*p
; /* Current printer */
1276 * De-register the individual printers
1279 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1281 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1282 dnssdDeregisterPrinter(p
, 1, 0);
1285 * Shutdown the rest of the service refs...
1288 dnssdDeregisterInstance(&WebIFSrv
, 0);
1291 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDMaster
));
1293 DNSServiceRefDeallocate(DNSSDMaster
);
1296 # else /* HAVE_AVAHI */
1298 avahi_threaded_poll_stop(DNSSDMaster
);
1302 avahi_client_free(DNSSDClient
);
1308 avahi_threaded_poll_free(DNSSDMaster
);
1311 # endif /* HAVE_DNSSD */
1313 cupsArrayDelete(DNSSDPrinters
);
1314 DNSSDPrinters
= NULL
;
1322 * 'dnssdUpdate()' - Handle DNS-SD queries.
1328 DNSServiceErrorType sdErr
; /* Service discovery error */
1331 if ((sdErr
= DNSServiceProcessResult(DNSSDMaster
)) != kDNSServiceErr_NoError
)
1333 cupsdLogMessage(CUPSD_LOG_ERROR
,
1334 "DNS Service Discovery registration error %d!",
1339 # endif /* HAVE_DNSSD */
1343 * 'dnssdUpdateDNSSDName()' - Update the listen port, computer name, and web interface registration.
1347 dnssdUpdateDNSSDName(int from_callback
) /* I - Called from callback? */
1349 char webif
[1024]; /* Web interface share name */
1351 SCDynamicStoreRef sc
; /* Context for dynamic store */
1352 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1353 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1354 CFStringRef nameRef
; /* Host name CFString */
1355 char nameBuffer
[1024]; /* C-string buffer */
1356 # endif /* __APPLE__ */
1360 * Only share the web interface and printers when non-local listening is
1367 * Get the port we use for registrations. If we are not listening on any
1368 * non-local ports, there is no sense sharing local printers via Bonjour...
1371 cupsd_listener_t
*lis
; /* Current listening socket */
1373 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1375 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1377 if (httpAddrLocalhost(&(lis
->address
)))
1380 DNSSDPort
= httpAddrPort(&(lis
->address
));
1389 * Get the computer name as a c-string...
1393 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1398 * Get the computer name from the dynamic store...
1401 cupsdClearString(&DNSSDComputerName
);
1403 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1405 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1406 kCFStringEncodingUTF8
))
1408 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1409 "Dynamic store computer name is \"%s\".", nameBuffer
);
1410 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1416 if (!DNSSDComputerName
)
1419 * Use the ServerName instead...
1422 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1423 "Using ServerName \"%s\" as computer name.", ServerName
);
1424 cupsdSetString(&DNSSDComputerName
, ServerName
);
1428 * Get the local hostname from the dynamic store...
1431 cupsdClearString(&DNSSDHostName
);
1433 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1435 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1436 kCFStringEncodingUTF8
))
1438 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1439 "Dynamic store host name is \"%s\".", nameBuffer
);
1440 cupsdSetString(&DNSSDHostName
, nameBuffer
);
1449 * Use the ServerName instead...
1452 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1453 "Using ServerName \"%s\" as host name.", ServerName
);
1454 cupsdSetString(&DNSSDHostName
, ServerName
);
1458 * Get any Back-to-My-Mac domains and add them as aliases...
1461 cupsdFreeAliases(DNSSDAlias
);
1464 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
1465 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
1467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
1468 (int)CFDictionaryGetCount(btmm
));
1469 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
1472 cupsdLogMessage(CUPSD_LOG_ERROR
,
1473 "Bad Back to My Mac data in dynamic store!");
1475 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
1483 # endif /* __APPLE__ */
1487 const char *host_name
= avahi_client_get_host_name(DNSSDClient
);
1488 const char *host_fqdn
= avahi_client_get_host_name_fqdn(DNSSDClient
);
1490 cupsdSetString(&DNSSDComputerName
, host_name
? host_name
: ServerName
);
1493 cupsdSetString(&DNSSDHostName
, host_fqdn
);
1494 else if (strchr(ServerName
, '.'))
1495 cupsdSetString(&DNSSDHostName
, ServerName
);
1497 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
1500 # endif /* HAVE_AVAHI */
1502 cupsdSetString(&DNSSDComputerName
, ServerName
);
1504 if (strchr(ServerName
, '.'))
1505 cupsdSetString(&DNSSDHostName
, ServerName
);
1507 cupsdSetStringf(&DNSSDHostName
, "%s.local", ServerName
);
1511 * Then (re)register the web interface if enabled...
1516 if (DNSSDComputerName
)
1517 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
1519 strlcpy(webif
, "CUPS", sizeof(webif
));
1521 dnssdDeregisterInstance(&WebIFSrv
, from_callback
);
1522 dnssdRegisterInstance(&WebIFSrv
, NULL
, webif
, "_http._tcp", "_printer", DNSSDPort
, NULL
, 1, from_callback
);
1528 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
1531 static char * /* O - String or NULL if none */
1532 get_auth_info_required(
1533 cupsd_printer_t
*p
, /* I - Printer */
1534 char *buffer
, /* I - Value buffer */
1535 size_t bufsize
) /* I - Size of value buffer */
1537 cupsd_location_t
*auth
; /* Pointer to authentication element */
1538 char resource
[1024]; /* Printer/class resource path */
1542 * If auth-info-required is set for this printer, return that...
1545 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
1547 int i
; /* Looping var */
1548 char *bufptr
; /* Pointer into buffer */
1550 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
1552 if (bufptr
>= (buffer
+ bufsize
- 2))
1558 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (size_t)(bufptr
- buffer
));
1559 bufptr
+= strlen(bufptr
);
1566 * Figure out the authentication data requirements to advertise...
1569 if (p
->type
& CUPS_PRINTER_CLASS
)
1570 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
1572 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
1574 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
1575 auth
->type
== CUPSD_AUTH_NONE
)
1576 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
1580 int auth_type
; /* Authentication type */
1582 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
1583 auth_type
= cupsdDefaultAuthType();
1587 case CUPSD_AUTH_NONE
:
1590 case CUPSD_AUTH_NEGOTIATE
:
1591 strlcpy(buffer
, "negotiate", bufsize
);
1595 strlcpy(buffer
, "username,password", bufsize
);
1604 #endif /* HAVE_DNSSD || HAVE_AVAHI */
1609 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
1612 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
1613 get_hostconfig(const char *name
) /* I - Name of service */
1615 cups_file_t
*fp
; /* Hostconfig file */
1616 char line
[1024], /* Line from file */
1617 *ptr
; /* Pointer to value */
1618 int state
= 1; /* State of service */
1622 * Try opening the /etc/hostconfig file; if we can't open it, assume that
1623 * the service is enabled/auto.
1626 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
1629 * Read lines from the file until we find the service...
1632 while (cupsFileGets(fp
, line
, sizeof(line
)))
1634 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
1639 if (!_cups_strcasecmp(line
, name
))
1642 * Found the service, see if it is set to "-NO-"...
1645 if (!_cups_strncasecmp(ptr
, "-NO-", 4))
1656 #endif /* __APPLE__ */
1660 * 'update_lpd()' - Update the LPD configuration as needed.
1664 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
1671 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
1672 * setting for backwards-compatibility.
1675 if (onoff
&& !get_hostconfig("CUPS_LPD"))
1677 #endif /* __APPLE__ */
1679 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
1682 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
1685 char newfile
[1024]; /* New cups-lpd.N file */
1686 cups_file_t
*ofp
, /* Original file pointer */
1687 *nfp
; /* New file pointer */
1688 char line
[1024]; /* Line from file */
1691 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
1693 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
1695 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1696 LPDConfigFile
+ 9, strerror(errno
));
1700 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1702 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1703 newfile
, strerror(errno
));
1709 * Copy all of the lines from the cups-lpd file...
1712 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1716 cupsFilePrintf(nfp
, "%s\n", line
);
1717 snprintf(line
, sizeof(line
), "\tdisable = %s",
1718 onoff
? "no" : "yes");
1720 else if (!strstr(line
, "disable ="))
1721 cupsFilePrintf(nfp
, "%s\n", line
);
1726 rename(newfile
, LPDConfigFile
+ 9);
1729 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
1732 * Enable/disable LPD via the launchctl command...
1735 char *argv
[5], /* Arguments for command */
1736 *envp
[MAX_ENV
]; /* Environment for command */
1737 int pid
; /* Process ID */
1740 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1741 argv
[0] = (char *)"launchctl";
1742 argv
[1] = (char *)(onoff
? "load" : "unload");
1743 argv
[2] = (char *)"-w";
1744 argv
[3] = LPDConfigFile
+ 10;
1747 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
1750 #endif /* __APPLE__ */
1752 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
1757 * 'update_smb()' - Update the SMB configuration as needed.
1761 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
1766 if (!strncmp(SMBConfigFile
, "samba:///", 9))
1769 * Enable/disable SMB via the specified smb.conf config file...
1772 char newfile
[1024]; /* New smb.conf.N file */
1773 cups_file_t
*ofp
, /* Original file pointer */
1774 *nfp
; /* New file pointer */
1775 char line
[1024]; /* Line from file */
1776 int in_printers
; /* In [printers] section? */
1779 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
1781 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
1783 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
1784 SMBConfigFile
+ 8, strerror(errno
));
1788 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
1790 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
1791 newfile
, strerror(errno
));
1797 * Copy all of the lines from the smb.conf file...
1802 while (cupsFileGets(ofp
, line
, sizeof(line
)))
1804 if (in_printers
&& strstr(line
, "printable ="))
1805 snprintf(line
, sizeof(line
), " printable = %s",
1806 onoff
? "yes" : "no");
1808 cupsFilePrintf(nfp
, "%s\n", line
);
1811 in_printers
= !strcmp(line
, "[printers]");
1816 rename(newfile
, SMBConfigFile
+ 8);
1819 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");