]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/dirsvc.c
Update svn:keyword properties.
[thirdparty/cups.git] / scheduler / dirsvc.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
10d09e33 4 * Directory services routines for the CUPS scheduler.
ef416fc2 5 *
f3c17241 6 * Copyright 2007-2012 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * Contents:
16 *
f3c17241
MS
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
19 * remote printers.
20 * cupsdRegisterPrinter() - Start sending broadcast information for a
21 * printer or update the broadcast contents.
22 * cupsdStartBrowsing() - Start sending and receiving broadcast
23 * information.
24 * cupsdStopBrowsing() - Stop sending and receiving broadcast
25 * information.
26 * cupsdUpdateDNSSDName() - Update the computer name we use for
27 * browsing...
28 * dnssdAddAlias() - Add a DNS-SD alias name.
29 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
30 * dnssdDeregisterInstance() - Deregister a DNS-SD service instance.
31 * dnssdDeregisterPrinter() - Deregister all services for a printer.
32 * dnssdErrorString() - Return an error string for an error code.
33 * dnssdRegisterCallback() - Free a TXT record.
34 * dnssdRegisterCallback() - DNSServiceRegister callback.
35 * dnssdRegisterInstance() - Register an instance of a printer service.
36 * dnssdRegisterPrinter() - Start sending broadcast information for a
37 * printer or update the broadcast contents.
38 * dnssdStop() - Stop all DNS-SD registrations.
39 * dnssdUpdate() - Handle DNS-SD queries.
40 * get_auth_info_required() - Get the auth-info-required value to advertise.
41 * get_hostconfig() - Get an /etc/hostconfig service setting.
42 * update_lpd() - Update the LPD configuration as needed.
43 * update_smb() - Update the SMB configuration as needed.
ef416fc2 44 */
45
46/*
47 * Include necessary headers...
48 */
49
50#include "cupsd.h"
51#include <grp.h>
52
f3c17241
MS
53#if defined(HAVE_DNSSD) && defined(__APPLE__)
54# include <nameser.h>
a29fd7dd
MS
55# include <CoreFoundation/CoreFoundation.h>
56# include <SystemConfiguration/SystemConfiguration.h>
f3c17241 57#endif /* HAVE_DNSSD && __APPLE__ */
f7deaa1a 58
ef416fc2 59
60/*
e00b005a 61 * Local functions...
ef416fc2 62 */
63
f3c17241
MS
64#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
65static char *get_auth_info_required(cupsd_printer_t *p,
66 char *buffer, size_t bufsize);
67#endif /* HAVE_DNSSD || HAVE_AVAHI */
a603edef 68#ifdef __APPLE__
f3c17241 69static int get_hostconfig(const char *name);
a603edef 70#endif /* __APPLE__ */
f3c17241
MS
71static void update_lpd(int onoff);
72static void update_smb(int onoff);
f899b121 73
e00b005a 74
f3c17241 75#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
a29fd7dd 76# ifdef __APPLE__
f3c17241
MS
77static void dnssdAddAlias(const void *key, const void *value,
78 void *context);
a29fd7dd 79# endif /* __APPLE__ */
f3c17241
MS
80static cupsd_txt_t dnssdBuildTxtRecord(cupsd_printer_t *p, int for_lpd);
81static void dnssdDeregisterInstance(cupsd_srv_t *srv);
82static void dnssdDeregisterPrinter(cupsd_printer_t *p,
83 int clear_name);
84static const char *dnssdErrorString(int error);
85static void dnssdFreeTxtRecord(cupsd_txt_t *txt);
86# ifdef HAVE_DNSSD
87static void dnssdRegisterCallback(DNSServiceRef sdRef,
88 DNSServiceFlags flags,
89 DNSServiceErrorType errorCode,
90 const char *name,
91 const char *regtype,
92 const char *domain,
93 void *context);
94# else
95static void dnssdRegisterCallback(AvahiEntryGroup *p,
96 AvahiEntryGroupState state,
97 void *context);
98# endif /* HAVE_DNSSD */
99static int dnssdRegisterInstance(cupsd_srv_t *srv,
100 cupsd_printer_t *p,
101 char *name, const char *type,
102 const char *subtypes, int port,
103 cupsd_txt_t *txt, int commit);
104static void dnssdRegisterPrinter(cupsd_printer_t *p);
105static void dnssdStop(void);
106# ifdef HAVE_DNSSD
107static void dnssdUpdate(void);
108# endif /* HAVE_DNSSD */
109#endif /* HAVE_DNSSD || HAVE_AVAHI */
7a14d768 110
f7deaa1a 111
112/*
88f9aafc 113 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
f7deaa1a 114 * local printer and remove any pending
115 * references to remote printers.
116 */
117
118void
119cupsdDeregisterPrinter(
120 cupsd_printer_t *p, /* I - Printer to register */
121 int removeit) /* I - Printer being permanently removed */
122{
123 /*
7a14d768 124 * Only deregister if browsing is enabled and it's a local printer...
f7deaa1a 125 */
126
745129be
MS
127 cupsdLogMessage(CUPSD_LOG_DEBUG,
128 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p, p->name,
129 removeit);
130
f7deaa1a 131 if (!Browsing || !p->shared ||
a2326b5b 132 (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_SCANNER)))
f7deaa1a 133 return;
134
135 /*
136 * Announce the deletion...
137 */
138
a29fd7dd 139#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
f3c17241
MS
140 if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDMaster)
141 dnssdDeregisterPrinter(p, 1);
a29fd7dd 142#endif /* HAVE_DNSSD || HAVE_AVAHI */
a2326b5b 143}
f7deaa1a 144
f7deaa1a 145
a2326b5b
MS
146/*
147 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
148 * printer or update the broadcast contents.
149 */
f7deaa1a 150
a2326b5b
MS
151void
152cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
153{
154 cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdRegisterPrinter(p=%p(%s))", p,
155 p->name);
f7deaa1a 156
a2326b5b
MS
157 if (!Browsing || !BrowseLocalProtocols ||
158 (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_SCANNER)))
159 return;
749b1e90 160
a29fd7dd 161#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
f3c17241 162 if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDMaster)
a2326b5b 163 dnssdRegisterPrinter(p);
a29fd7dd 164#endif /* HAVE_DNSSD || HAVE_AVAHI */
f7deaa1a 165}
166
ef416fc2 167
168/*
a2326b5b 169 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
ef416fc2 170 */
171
172void
a2326b5b 173cupsdStartBrowsing(void)
ef416fc2 174{
ef416fc2 175 cupsd_printer_t *p; /* Current printer */
411affcf 176
411affcf 177
a2326b5b 178 if (!Browsing || !BrowseLocalProtocols)
ef416fc2 179 return;
180
f3c17241 181#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
a2326b5b
MS
182 if (BrowseLocalProtocols & BROWSE_DNSSD)
183 {
a2326b5b 184 cupsd_listener_t *lis; /* Current listening socket */
f3c17241
MS
185# ifdef HAVE_DNSSD
186 DNSServiceErrorType error; /* Error from service creation */
ef416fc2 187
ef416fc2 188 /*
a2326b5b 189 * First create a "master" connection for all registrations...
ef416fc2 190 */
191
f3c17241 192 if ((error = DNSServiceCreateConnection(&DNSSDMaster))
a2326b5b 193 != kDNSServiceErr_NoError)
ef416fc2 194 {
195 cupsdLogMessage(CUPSD_LOG_ERROR,
a2326b5b 196 "Unable to create master DNS-SD reference: %d", error);
bd7854cb 197
a2326b5b
MS
198 if (FatalErrors & CUPSD_FATAL_BROWSE)
199 cupsdEndProcess(getpid(), 0);
ef416fc2 200 }
a2326b5b 201 else
b423cd4c 202 {
203 /*
a2326b5b 204 * Add the master connection to the select list...
b423cd4c 205 */
206
f3c17241 207 int fd = DNSServiceRefSockFD(DNSSDMaster);
b423cd4c 208
a2326b5b 209 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
b423cd4c 210
a2326b5b 211 cupsdAddSelect(fd, (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
f3c17241 212 }
ef416fc2 213
f3c17241
MS
214# else /* HAVE_AVAHI */
215 if ((DNSSDMaster = avahi_threaded_poll_new()) == NULL)
216 {
217 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create DNS-SD thread.");
ef416fc2 218
f3c17241
MS
219 if (FatalErrors & CUPSD_FATAL_BROWSE)
220 cupsdEndProcess(getpid(), 0);
221 }
222 else
223 {
224 int error; /* Error code, if any */
ef416fc2 225
f3c17241
MS
226 DNSSDClient = avahi_client_new(avahi_threaded_poll_get(DNSSDMaster), 0,
227 NULL, NULL, &error);
228
229 if (DNSSDClient == NULL)
ef416fc2 230 {
f3c17241
MS
231 cupsdLogMessage(CUPSD_LOG_ERROR,
232 "Unable to communicate with avahi-daemon: %s",
233 dnssdErrorString(error));
ef416fc2 234
f3c17241
MS
235 if (FatalErrors & CUPSD_FATAL_BROWSE)
236 cupsdEndProcess(getpid(), 0);
ef416fc2 237
5a9febac
MS
238 avahi_threaded_poll_free(DNSSDMaster);
239 DNSSDMaster = NULL;
240 }
241 else
242 avahi_threaded_poll_start(DNSSDMaster);
f3c17241
MS
243 }
244# endif /* HAVE_DNSSD */
245
246 /*
247 * Then get the port we use for registrations. If we are not listening
248 * on any non-local ports, there is no sense sharing local printers via
249 * Bonjour...
250 */
251
252 DNSSDPort = 0;
253
254 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
255 lis;
256 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
257 {
258 if (httpAddrLocalhost(&(lis->address)))
259 continue;
ef416fc2 260
a469f8a5 261 DNSSDPort = httpAddrPort(&(lis->address));
f3c17241 262 break;
ef416fc2 263 }
f3c17241
MS
264
265 /*
266 * Set the computer name and register the web interface...
267 */
268
269 cupsdUpdateDNSSDName();
ef416fc2 270 }
f3c17241 271#endif /* HAVE_DNSSD || HAVE_AVAHI */
a2326b5b
MS
272
273 /*
274 * Enable LPD and SMB printer sharing as needed through external programs...
275 */
276
277 if (BrowseLocalProtocols & BROWSE_LPD)
278 update_lpd(1);
ef416fc2 279
a2326b5b
MS
280 if (BrowseLocalProtocols & BROWSE_SMB)
281 update_smb(1);
ef416fc2 282
283 /*
a2326b5b 284 * Register the individual printers
ef416fc2 285 */
286
a2326b5b
MS
287 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
288 p;
289 p = (cupsd_printer_t *)cupsArrayNext(Printers))
290 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_SCANNER)))
291 cupsdRegisterPrinter(p);
ef416fc2 292}
293
294
f7deaa1a 295/*
a2326b5b 296 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
f7deaa1a 297 */
298
299void
a2326b5b 300cupsdStopBrowsing(void)
f7deaa1a 301{
a2326b5b 302 cupsd_printer_t *p; /* Current printer */
745129be 303
a2326b5b
MS
304
305 if (!Browsing || !BrowseLocalProtocols)
f7deaa1a 306 return;
307
a2326b5b
MS
308 /*
309 * De-register the individual printers
310 */
311
312 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
313 p;
314 p = (cupsd_printer_t *)cupsArrayNext(Printers))
315 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_SCANNER)))
316 cupsdDeregisterPrinter(p, 1);
317
318 /*
319 * Shut down browsing sockets...
320 */
f7deaa1a 321
f3c17241
MS
322#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
323 if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDMaster)
a2326b5b 324 dnssdStop();
f3c17241 325#endif /* HAVE_DNSSD || HAVE_AVAHI */
d09495fa 326
a2326b5b
MS
327 /*
328 * Disable LPD and SMB printer sharing as needed through external programs...
329 */
d09495fa 330
a2326b5b
MS
331 if (BrowseLocalProtocols & BROWSE_LPD)
332 update_lpd(0);
d09495fa 333
a2326b5b
MS
334 if (BrowseLocalProtocols & BROWSE_SMB)
335 update_smb(0);
d09495fa 336}
337
338
f3c17241 339#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
ef416fc2 340/*
a2326b5b 341 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
ef416fc2 342 */
343
344void
a2326b5b 345cupsdUpdateDNSSDName(void)
ef416fc2 346{
a2326b5b 347 char webif[1024]; /* Web interface share name */
a29fd7dd 348# ifdef __APPLE__
a2326b5b
MS
349 SCDynamicStoreRef sc; /* Context for dynamic store */
350 CFDictionaryRef btmm; /* Back-to-My-Mac domains */
351 CFStringEncoding nameEncoding; /* Encoding of computer name */
352 CFStringRef nameRef; /* Host name CFString */
353 char nameBuffer[1024]; /* C-string buffer */
a29fd7dd 354# endif /* __APPLE__ */
ef416fc2 355
ef416fc2 356
357 /*
a2326b5b
MS
358 * Only share the web interface and printers when non-local listening is
359 * enabled...
360 */
ef416fc2 361
a2326b5b 362 if (!DNSSDPort)
ef416fc2 363 return;
ef416fc2 364
e00b005a 365 /*
a2326b5b 366 * Get the computer name as a c-string...
e00b005a 367 */
ef416fc2 368
a29fd7dd 369# ifdef __APPLE__
a2326b5b 370 sc = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("cupsd"), NULL, NULL);
ef416fc2 371
a2326b5b 372 if (sc)
ef416fc2 373 {
374 /*
a2326b5b 375 * Get the computer name from the dynamic store...
ef416fc2 376 */
377
a2326b5b 378 cupsdClearString(&DNSSDComputerName);
ef416fc2 379
a2326b5b 380 if ((nameRef = SCDynamicStoreCopyComputerName(sc, &nameEncoding)) != NULL)
58dc1933 381 {
a2326b5b
MS
382 if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
383 kCFStringEncodingUTF8))
384 {
385 cupsdLogMessage(CUPSD_LOG_DEBUG,
386 "Dynamic store computer name is \"%s\".", nameBuffer);
387 cupsdSetString(&DNSSDComputerName, nameBuffer);
388 }
e00b005a 389
a2326b5b
MS
390 CFRelease(nameRef);
391 }
e00b005a 392
a2326b5b
MS
393 if (!DNSSDComputerName)
394 {
395 /*
396 * Use the ServerName instead...
397 */
e00b005a 398
a2326b5b
MS
399 cupsdLogMessage(CUPSD_LOG_DEBUG,
400 "Using ServerName \"%s\" as computer name.", ServerName);
401 cupsdSetString(&DNSSDComputerName, ServerName);
402 }
e00b005a 403
404 /*
a2326b5b 405 * Get the local hostname from the dynamic store...
e00b005a 406 */
407
a2326b5b 408 cupsdClearString(&DNSSDHostName);
e00b005a 409
a2326b5b
MS
410 if ((nameRef = SCDynamicStoreCopyLocalHostName(sc)) != NULL)
411 {
412 if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
413 kCFStringEncodingUTF8))
414 {
415 cupsdLogMessage(CUPSD_LOG_DEBUG,
416 "Dynamic store host name is \"%s\".", nameBuffer);
417 cupsdSetString(&DNSSDHostName, nameBuffer);
418 }
e00b005a 419
a2326b5b
MS
420 CFRelease(nameRef);
421 }
e00b005a 422
a2326b5b 423 if (!DNSSDHostName)
ef416fc2 424 {
425 /*
a2326b5b 426 * Use the ServerName instead...
ef416fc2 427 */
428
a2326b5b
MS
429 cupsdLogMessage(CUPSD_LOG_DEBUG,
430 "Using ServerName \"%s\" as host name.", ServerName);
431 cupsdSetString(&DNSSDHostName, ServerName);
ef416fc2 432 }
e00b005a 433
434 /*
a2326b5b 435 * Get any Back-to-My-Mac domains and add them as aliases...
e00b005a 436 */
437
a2326b5b
MS
438 cupsdFreeAliases(DNSSDAlias);
439 DNSSDAlias = NULL;
ef416fc2 440
a2326b5b
MS
441 btmm = SCDynamicStoreCopyValue(sc, CFSTR("Setup:/Network/BackToMyMac"));
442 if (btmm && CFGetTypeID(btmm) == CFDictionaryGetTypeID())
ef416fc2 443 {
a2326b5b
MS
444 cupsdLogMessage(CUPSD_LOG_DEBUG, "%d Back to My Mac aliases to add.",
445 (int)CFDictionaryGetCount(btmm));
446 CFDictionaryApplyFunction(btmm, dnssdAddAlias, NULL);
ef416fc2 447 }
a2326b5b
MS
448 else if (btmm)
449 cupsdLogMessage(CUPSD_LOG_ERROR,
450 "Bad Back to My Mac data in dynamic store!");
451 else
452 cupsdLogMessage(CUPSD_LOG_DEBUG, "No Back to My Mac aliases to add.");
749b1e90 453
a2326b5b
MS
454 if (btmm)
455 CFRelease(btmm);
321d8d57 456
a2326b5b
MS
457 CFRelease(sc);
458 }
459 else
a29fd7dd 460# endif /* __APPLE__ */
f3c17241 461# ifdef HAVE_AVAHI
3dd9c340 462 if (DNSSDClient)
f3c17241 463 {
5a9febac
MS
464 const char *host_name = avahi_client_get_host_name(DNSSDClient);
465 const char *host_fqdn = avahi_client_get_host_name_fqdn(DNSSDClient);
466
467 cupsdSetString(&DNSSDComputerName, host_name ? host_name : ServerName);
468
469 if (host_fqdn)
470 cupsdSetString(&DNSSDHostName, host_fqdn);
471 else if (strchr(ServerName, '.'))
472 cupsdSetString(&DNSSDHostName, ServerName);
473 else
474 cupsdSetStringf(&DNSSDHostName, "%s.local", ServerName);
f3c17241 475 }
3dd9c340
MS
476 else
477# endif /* HAVE_AVAHI */
a2326b5b
MS
478 {
479 cupsdSetString(&DNSSDComputerName, ServerName);
5a9febac
MS
480
481 if (strchr(ServerName, '.'))
482 cupsdSetString(&DNSSDHostName, ServerName);
483 else
484 cupsdSetStringf(&DNSSDHostName, "%s.local", ServerName);
a2326b5b 485 }
321d8d57 486
749b1e90 487 /*
a2326b5b 488 * Then (re)register the web interface if enabled...
749b1e90
MS
489 */
490
a2326b5b
MS
491 if (BrowseWebIF)
492 {
493 if (DNSSDComputerName)
494 snprintf(webif, sizeof(webif), "CUPS @ %s", DNSSDComputerName);
495 else
f3c17241
MS
496 strlcpy(webif, "CUPS", sizeof(webif));
497
498 dnssdDeregisterInstance(&WebIFSrv);
499 dnssdRegisterInstance(&WebIFSrv, NULL, webif, "_http._tcp", "_printer",
500 DNSSDPort, NULL, 1);
a2326b5b 501 }
749b1e90
MS
502}
503
749b1e90 504
a29fd7dd 505# ifdef __APPLE__
749b1e90 506/*
a2326b5b 507 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
749b1e90
MS
508 */
509
a2326b5b
MS
510static void
511dnssdAddAlias(const void *key, /* I - Key */
512 const void *value, /* I - Value (domain) */
513 void *context) /* I - Unused */
749b1e90 514{
a2326b5b 515 char valueStr[1024], /* Domain string */
dcb445bc
MS
516 hostname[1024], /* Complete hostname */
517 *hostptr; /* Pointer into hostname */
749b1e90 518
749b1e90 519
a2326b5b
MS
520 (void)key;
521 (void)context;
749b1e90 522
a2326b5b
MS
523 if (CFGetTypeID((CFStringRef)value) == CFStringGetTypeID() &&
524 CFStringGetCString((CFStringRef)value, valueStr, sizeof(valueStr),
525 kCFStringEncodingUTF8))
526 {
527 snprintf(hostname, sizeof(hostname), "%s.%s", DNSSDHostName, valueStr);
dcb445bc
MS
528 hostptr = hostname + strlen(hostname) - 1;
529 if (*hostptr == '.')
530 *hostptr = '\0'; /* Strip trailing dot */
531
a2326b5b
MS
532 if (!DNSSDAlias)
533 DNSSDAlias = cupsArrayNew(NULL, NULL);
749b1e90 534
a2326b5b
MS
535 cupsdAddAlias(DNSSDAlias, hostname);
536 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added Back to My Mac ServerAlias %s",
537 hostname);
749b1e90 538 }
a2326b5b
MS
539 else
540 cupsdLogMessage(CUPSD_LOG_ERROR,
541 "Bad Back to My Mac domain in dynamic store!");
749b1e90 542}
a29fd7dd 543# endif /* __APPLE__ */
749b1e90
MS
544
545
749b1e90 546/*
a2326b5b 547 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
749b1e90
MS
548 */
549
f3c17241 550static cupsd_txt_t /* O - TXT record */
a2326b5b 551dnssdBuildTxtRecord(
a2326b5b
MS
552 cupsd_printer_t *p, /* I - Printer information */
553 int for_lpd) /* I - 1 = LPD, 0 = IPP */
ef416fc2 554{
f3c17241
MS
555 int i, /* Looping var */
556 count; /* Count of key/value pairs */
a2326b5b
MS
557 char admin_hostname[256], /* .local hostname for admin page */
558 adminurl_str[256], /* URL for the admin page */
559 type_str[32], /* Type to string buffer */
560 state_str[32], /* State to string buffer */
561 rp_str[1024], /* Queue name string buffer */
562 air_str[1024], /* auth-info-required string buffer */
563 *keyvalue[32][2]; /* Table of key/value pairs */
f3c17241 564 cupsd_txt_t txt; /* TXT record */
749b1e90 565
749b1e90
MS
566
567 /*
a2326b5b 568 * Load up the key value pairs...
749b1e90
MS
569 */
570
f3c17241 571 count = 0;
749b1e90 572
f3c17241
MS
573 if (!for_lpd || (BrowseLocalProtocols & BROWSE_LPD))
574 {
575 keyvalue[count ][0] = "txtvers";
576 keyvalue[count++][1] = "1";
749b1e90 577
f3c17241
MS
578 keyvalue[count ][0] = "qtotal";
579 keyvalue[count++][1] = "1";
88f9aafc 580
f3c17241
MS
581 keyvalue[count ][0] = "rp";
582 keyvalue[count++][1] = rp_str;
583 if (for_lpd)
584 strlcpy(rp_str, p->name, sizeof(rp_str));
585 else
586 snprintf(rp_str, sizeof(rp_str), "%s/%s",
587 (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
588 p->name);
749b1e90 589
f3c17241
MS
590 keyvalue[count ][0] = "ty";
591 keyvalue[count++][1] = p->make_model ? p->make_model : "Unknown";
749b1e90 592
f3c17241
MS
593 if (strstr(DNSSDHostName, ".local"))
594 strlcpy(admin_hostname, DNSSDHostName, sizeof(admin_hostname));
595 else
596 snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.",
597 DNSSDHostName);
598 httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
599# ifdef HAVE_SSL
600 "https",
601# else
602 "http",
603# endif /* HAVE_SSL */
604 NULL, admin_hostname, DNSSDPort, "/%s/%s",
605 (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
606 p->name);
607 keyvalue[count ][0] = "adminurl";
608 keyvalue[count++][1] = adminurl_str;
609
610 if (p->location)
611 {
612 keyvalue[count ][0] = "note";
613 keyvalue[count++][1] = p->location;
614 }
749b1e90 615
f3c17241
MS
616 keyvalue[count ][0] = "priority";
617 keyvalue[count++][1] = for_lpd ? "100" : "0";
749b1e90 618
f3c17241
MS
619 keyvalue[count ][0] = "product";
620 keyvalue[count++][1] = p->pc && p->pc->product ? p->pc->product : "Unknown";
749b1e90 621
f3c17241
MS
622 keyvalue[count ][0] = "pdl";
623 keyvalue[count++][1] = p->pdl ? p->pdl : "application/postscript";
a2326b5b 624
f3c17241
MS
625 if (get_auth_info_required(p, air_str, sizeof(air_str)))
626 {
627 keyvalue[count ][0] = "air";
628 keyvalue[count++][1] = air_str;
629 }
749b1e90 630
f3c17241
MS
631 keyvalue[count ][0] = "UUID";
632 keyvalue[count++][1] = p->uuid + 9;
749b1e90 633
f3c17241
MS
634 #ifdef HAVE_SSL
635 keyvalue[count ][0] = "TLS";
636 keyvalue[count++][1] = "1.2";
637 #endif /* HAVE_SSL */
749b1e90 638
f3c17241
MS
639 if (p->type & CUPS_PRINTER_FAX)
640 {
641 keyvalue[count ][0] = "Fax";
642 keyvalue[count++][1] = (p->type & CUPS_PRINTER_FAX) ? "T" : "F";
643 }
749b1e90 644
f3c17241
MS
645 if (p->type & CUPS_PRINTER_COLOR)
646 {
647 keyvalue[count ][0] = "Color";
648 keyvalue[count++][1] = (p->type & CUPS_PRINTER_COLOR) ? "T" : "F";
649 }
749b1e90 650
f3c17241
MS
651 if (p->type & CUPS_PRINTER_DUPLEX)
652 {
653 keyvalue[count ][0] = "Duplex";
654 keyvalue[count++][1] = (p->type & CUPS_PRINTER_DUPLEX) ? "T" : "F";
655 }
749b1e90 656
f3c17241
MS
657 if (p->type & CUPS_PRINTER_STAPLE)
658 {
659 keyvalue[count ][0] = "Staple";
660 keyvalue[count++][1] = (p->type & CUPS_PRINTER_STAPLE) ? "T" : "F";
661 }
749b1e90 662
f3c17241
MS
663 if (p->type & CUPS_PRINTER_COPIES)
664 {
665 keyvalue[count ][0] = "Copies";
666 keyvalue[count++][1] = (p->type & CUPS_PRINTER_COPIES) ? "T" : "F";
667 }
749b1e90 668
f3c17241
MS
669 if (p->type & CUPS_PRINTER_COLLATE)
670 {
671 keyvalue[count ][0] = "Collate";
672 keyvalue[count++][1] = (p->type & CUPS_PRINTER_COLLATE) ? "T" : "F";
673 }
749b1e90 674
f3c17241
MS
675 if (p->type & CUPS_PRINTER_PUNCH)
676 {
677 keyvalue[count ][0] = "Punch";
678 keyvalue[count++][1] = (p->type & CUPS_PRINTER_PUNCH) ? "T" : "F";
679 }
749b1e90 680
f3c17241
MS
681 if (p->type & CUPS_PRINTER_BIND)
682 {
683 keyvalue[count ][0] = "Bind";
684 keyvalue[count++][1] = (p->type & CUPS_PRINTER_BIND) ? "T" : "F";
685 }
e1d6a774 686
f3c17241
MS
687 if (p->type & CUPS_PRINTER_SORT)
688 {
689 keyvalue[count ][0] = "Sort";
690 keyvalue[count++][1] = (p->type & CUPS_PRINTER_SORT) ? "T" : "F";
691 }
e1d6a774 692
f3c17241
MS
693 if (p->type & CUPS_PRINTER_MFP)
694 {
695 keyvalue[count ][0] = "Scan";
696 keyvalue[count++][1] = (p->type & CUPS_PRINTER_MFP) ? "T" : "F";
697 }
749b1e90 698
f3c17241
MS
699 snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
700 snprintf(state_str, sizeof(state_str), "%d", p->state);
749b1e90 701
f3c17241
MS
702 keyvalue[count ][0] = "printer-state";
703 keyvalue[count++][1] = state_str;
749b1e90 704
f3c17241
MS
705 keyvalue[count ][0] = "printer-type";
706 keyvalue[count++][1] = type_str;
707 }
749b1e90 708
e1d6a774 709 /*
a2326b5b 710 * Then pack them into a proper txt record...
e1d6a774 711 */
712
f3c17241
MS
713# ifdef HAVE_DNSSD
714 TXTRecordCreate(&txt, 0, NULL);
715
716 for (i = 0; i < count; i ++)
717 {
718 size_t len = strlen(keyvalue[i][1]);
719
720 if (len < 256)
721 TXTRecordSetValue(&txt, keyvalue[i][0], (uint8_t)len, keyvalue[i][1]);
722 }
723
724# else
725 for (i = 0, txt = NULL; i < count; i ++)
726 txt = avahi_string_list_add_printf(txt, "%s=%s", keyvalue[i][0],
727 keyvalue[i][1]);
728# endif /* HAVE_DNSSD */
729
730 return (txt);
a2326b5b 731}
e1d6a774 732
e1d6a774 733
a2326b5b 734/*
f3c17241 735 * 'dnssdDeregisterInstance()' - Deregister a DNS-SD service instance.
a2326b5b 736 */
749b1e90 737
a2326b5b 738static void
f3c17241
MS
739dnssdDeregisterInstance(
740 cupsd_srv_t *srv) /* I - Service */
a2326b5b 741{
f3c17241
MS
742 if (!srv || !*srv)
743 return;
749b1e90 744
f3c17241
MS
745# ifdef HAVE_DNSSD
746 DNSServiceRefDeallocate(*srv);
749b1e90 747
f3c17241
MS
748# else /* HAVE_AVAHI */
749 avahi_threaded_poll_lock(DNSSDMaster);
750 avahi_entry_group_free(*srv);
751 avahi_threaded_poll_unlock(DNSSDMaster);
752# endif /* HAVE_DNSSD */
749b1e90 753
f3c17241
MS
754 *srv = NULL;
755}
749b1e90 756
749b1e90 757
f3c17241
MS
758/*
759 * 'dnssdDeregisterPrinter()' - Deregister all services for a printer.
760 */
a2326b5b 761
f3c17241
MS
762static void
763dnssdDeregisterPrinter(
764 cupsd_printer_t *p, /* I - Printer */
765 int clear_name) /* I - Clear the name? */
e1d6a774 766
f3c17241
MS
767{
768 cupsdLogMessage(CUPSD_LOG_DEBUG2,
769 "dnssdDeregisterPrinter(p=%p(%s), clear_name=%d)", p, p->name,
770 clear_name);
771
772 if (p->ipp_srv)
773 {
774 dnssdDeregisterInstance(&p->ipp_srv);
775
776# ifdef HAVE_DNSSD
777# ifdef HAVE_SSL
778 dnssdDeregisterInstance(&p->ipps_srv);
779# endif /* HAVE_SSL */
780 dnssdDeregisterInstance(&p->printer_srv);
781# endif /* HAVE_DNSSD */
e1d6a774 782 }
749b1e90 783
a2326b5b 784 /*
f3c17241 785 * Remove the printer from the array of DNS-SD printers but keep the
a2326b5b
MS
786 * registered name...
787 */
788
789 cupsArrayRemove(DNSSDPrinters, p);
f3c17241
MS
790
791 /*
792 * Optionally clear the service name...
793 */
794
795 if (clear_name)
796 cupsdClearString(&p->reg_name);
e1d6a774 797}
749b1e90
MS
798
799
800/*
f3c17241 801 * 'dnssdErrorString()' - Return an error string for an error code.
749b1e90
MS
802 */
803
f3c17241
MS
804static const char * /* O - Error message */
805dnssdErrorString(int error) /* I - Error number */
749b1e90 806{
f3c17241
MS
807# ifdef HAVE_DNSSD
808 switch (error)
809 {
810 case kDNSServiceErr_NoError :
811 return ("OK.");
749b1e90 812
f3c17241
MS
813 default :
814 case kDNSServiceErr_Unknown :
815 return ("Unknown error.");
749b1e90 816
f3c17241
MS
817 case kDNSServiceErr_NoSuchName :
818 return ("Service not found.");
749b1e90 819
f3c17241
MS
820 case kDNSServiceErr_NoMemory :
821 return ("Out of memory.");
a2326b5b 822
f3c17241
MS
823 case kDNSServiceErr_BadParam :
824 return ("Bad parameter.");
749b1e90 825
f3c17241
MS
826 case kDNSServiceErr_BadReference :
827 return ("Bad service reference.");
749b1e90 828
f3c17241
MS
829 case kDNSServiceErr_BadState :
830 return ("Bad state.");
749b1e90 831
f3c17241
MS
832 case kDNSServiceErr_BadFlags :
833 return ("Bad flags.");
a2326b5b 834
f3c17241
MS
835 case kDNSServiceErr_Unsupported :
836 return ("Unsupported.");
a2326b5b 837
f3c17241
MS
838 case kDNSServiceErr_NotInitialized :
839 return ("Not initialized.");
a2326b5b 840
f3c17241
MS
841 case kDNSServiceErr_AlreadyRegistered :
842 return ("Already registered.");
749b1e90 843
f3c17241
MS
844 case kDNSServiceErr_NameConflict :
845 return ("Name conflict.");
846
847 case kDNSServiceErr_Invalid :
848 return ("Invalid name.");
849
850 case kDNSServiceErr_Firewall :
851 return ("Firewall prevents registration.");
852
853 case kDNSServiceErr_Incompatible :
854 return ("Client library incompatible.");
855
856 case kDNSServiceErr_BadInterfaceIndex :
857 return ("Bad interface index.");
858
859 case kDNSServiceErr_Refused :
860 return ("Server prevents registration.");
861
862 case kDNSServiceErr_NoSuchRecord :
863 return ("Record not found.");
864
865 case kDNSServiceErr_NoAuth :
866 return ("Authentication required.");
867
868 case kDNSServiceErr_NoSuchKey :
869 return ("Encryption key not found.");
870
871 case kDNSServiceErr_NATTraversal :
872 return ("Unable to traverse NAT boundary.");
873
874 case kDNSServiceErr_DoubleNAT :
875 return ("Unable to traverse double-NAT boundary.");
876
877 case kDNSServiceErr_BadTime :
878 return ("Bad system time.");
879
880 case kDNSServiceErr_BadSig :
881 return ("Bad signature.");
882
883 case kDNSServiceErr_BadKey :
884 return ("Bad encryption key.");
885
886 case kDNSServiceErr_Transient :
887 return ("Transient error occurred - please try again.");
888
889 case kDNSServiceErr_ServiceNotRunning :
890 return ("Server not running.");
891
892 case kDNSServiceErr_NATPortMappingUnsupported :
893 return ("NAT doesn't support NAT-PMP or UPnP.");
894
895 case kDNSServiceErr_NATPortMappingDisabled :
896 return ("NAT supports NAT-PNP or UPnP but it is disabled.");
897
898 case kDNSServiceErr_NoRouter :
899 return ("No Internet/default router configured.");
900
901 case kDNSServiceErr_PollingMode :
902 return ("Service polling mode error.");
903
904 case kDNSServiceErr_Timeout :
905 return ("Service timeout.");
749b1e90 906 }
a2326b5b 907
f3c17241
MS
908# else /* HAVE_AVAHI */
909 return (avahi_strerror(error));
910# endif /* HAVE_DNSSD */
911}
912
913
914/*
915 * 'dnssdRegisterCallback()' - Free a TXT record.
916 */
917
918static void
919dnssdFreeTxtRecord(cupsd_txt_t *txt) /* I - TXT record */
920{
921# ifdef HAVE_DNSSD
922 TXTRecordDeallocate(txt);
923
924# else /* HAVE_AVAHI */
925 avahi_string_list_free(*txt);
926 *txt = NULL;
927# endif /* HAVE_DNSSD */
749b1e90
MS
928}
929
930
ef55b745 931/*
a2326b5b 932 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
ef55b745
MS
933 */
934
f3c17241 935# ifdef HAVE_DNSSD
749b1e90 936static void
a2326b5b
MS
937dnssdRegisterCallback(
938 DNSServiceRef sdRef, /* I - DNS Service reference */
939 DNSServiceFlags flags, /* I - Reserved for future use */
940 DNSServiceErrorType errorCode, /* I - Error code */
941 const char *name, /* I - Service name */
942 const char *regtype, /* I - Service type */
943 const char *domain, /* I - Domain. ".local" for now */
f3c17241 944 void *context) /* I - Printer */
749b1e90 945{
a2326b5b
MS
946 cupsd_printer_t *p = (cupsd_printer_t *)context;
947 /* Current printer */
749b1e90
MS
948
949
a2326b5b
MS
950 (void)sdRef;
951 (void)flags;
952 (void)domain;
749b1e90 953
a2326b5b
MS
954 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s (%s)",
955 name, regtype, p ? p->name : "Web Interface",
956 p ? (p->reg_name ? p->reg_name : "(null)") : "NA");
749b1e90 957
a2326b5b 958 if (errorCode)
749b1e90 959 {
a2326b5b
MS
960 cupsdLogMessage(CUPSD_LOG_ERROR,
961 "DNSServiceRegister failed with error %d", (int)errorCode);
749b1e90
MS
962 return;
963 }
a2326b5b 964 else if (p && (!p->reg_name || _cups_strcasecmp(name, p->reg_name)))
749b1e90 965 {
a2326b5b
MS
966 cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"",
967 name, p->name);
749b1e90 968
a2326b5b
MS
969 cupsArrayRemove(DNSSDPrinters, p);
970 cupsdSetString(&p->reg_name, name);
971 cupsArrayAdd(DNSSDPrinters, p);
749b1e90 972
a2326b5b 973 LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
749b1e90
MS
974 }
975}
e1d6a774 976
f3c17241
MS
977# else /* HAVE_AVAHI */
978static void
979dnssdRegisterCallback(
980 AvahiEntryGroup *srv, /* I - Service */
981 AvahiEntryGroupState state, /* I - Registration state */
982 void *context) /* I - Printer */
983{
984 cupsd_printer_t *p = (cupsd_printer_t *)context;
985 /* Current printer */
986
987 cupsdLogMessage(CUPSD_LOG_DEBUG2,
988 "dnssdRegisterCallback(srv=%p, state=%d, context=%p) "
989 "for %s (%s)", srv, state, context,
990 p ? p->name : "Web Interface",
991 p ? (p->reg_name ? p->reg_name : "(null)") : "NA");
992
993 /* TODO: Handle collisions with avahi_alternate_service_name(p->reg_name)? */
994}
995# endif /* HAVE_DNSSD */
996
e1d6a774 997
e1d6a774 998/*
f3c17241 999 * 'dnssdRegisterInstance()' - Register an instance of a printer service.
e1d6a774 1000 */
1001
f3c17241
MS
1002static int /* O - 1 on success, 0 on failure */
1003dnssdRegisterInstance(
1004 cupsd_srv_t *srv, /* O - Service */
1005 cupsd_printer_t *p, /* I - Printer */
1006 char *name, /* I - DNS-SD service name */
1007 const char *type, /* I - DNS-SD service type */
1008 const char *subtypes, /* I - Subtypes to register or NULL */
1009 int port, /* I - Port number or 0 */
1010 cupsd_txt_t *txt, /* I - TXT record */
1011 int commit) /* I - Commit registration? */
e1d6a774 1012{
f3c17241
MS
1013 char temp[256], /* Temporary string */
1014 *ptr; /* Pointer into string */
1015 int error; /* Any error */
e1d6a774 1016
e1d6a774 1017
f3c17241
MS
1018 cupsdLogMessage(CUPSD_LOG_DEBUG,
1019 "Registering \"%s\" with DNS-SD type \"%s\".", name, type);
e1d6a774 1020
f3c17241
MS
1021 if (p && !srv)
1022 {
1023 /*
1024 * Assign the correct pointer for "srv"...
1025 */
e1d6a774 1026
f3c17241
MS
1027# ifdef HAVE_DNSSD
1028 if (!strcmp(type, "_printer._tcp"))
1029 srv = &p->printer_srv; /* Target LPD service */
1030# ifdef HAVE_SSL
1031 else if (!strcmp(type, "_ipps._tcp"))
1032 srv = &p->ipps_srv; /* Target IPPS service */
1033# endif /* HAVE_SSL */
1034 else
1035 srv = &p->ipp_srv; /* Target IPP service */
1036
1037# else /* HAVE_AVAHI */
1038 srv = &p->ipp_srv; /* Target service group */
1039# endif /* HAVE_DNSSD */
1040 }
1041
1042# ifdef HAVE_DNSSD
1043 (void)commit;
1044
1045# else /* HAVE_AVAHI */
1046 avahi_threaded_poll_lock(DNSSDMaster);
1047
1048 if (!*srv)
1049 *srv = avahi_entry_group_new(DNSSDClient, dnssdRegisterCallback, NULL);
1050 if (!*srv)
e1d6a774 1051 {
f3c17241
MS
1052 avahi_threaded_poll_unlock(DNSSDMaster);
1053
1054 cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s",
1055 name, dnssdErrorString(avahi_client_errno(DNSSDClient)));
1056 return (0);
e1d6a774 1057 }
f3c17241 1058# endif /* HAVE_DNSSD */
e1d6a774 1059
a2326b5b 1060 /*
f3c17241
MS
1061 * Make sure the name is <= 63 octets, and when we truncate be sure to
1062 * properly truncate any UTF-8 characters...
a2326b5b 1063 */
e1d6a774 1064
f3c17241
MS
1065 ptr = name + strlen(name);
1066 while ((ptr - name) > 63)
e1d6a774 1067 {
f3c17241
MS
1068 do
1069 {
1070 ptr --;
1071 }
1072 while (ptr > name && (*ptr & 0xc0) == 0x80);
1073
1074 if (ptr > name)
1075 *ptr = '\0';
e1d6a774 1076 }
e1d6a774 1077
1078 /*
f3c17241 1079 * Register the service...
e1d6a774 1080 */
1081
f3c17241
MS
1082# ifdef HAVE_DNSSD
1083 if (subtypes)
1084 snprintf(temp, sizeof(temp), "%s,%s", type, subtypes);
1085 else
1086 strlcpy(temp, type, sizeof(temp));
e1d6a774 1087
f3c17241
MS
1088 *srv = DNSSDMaster;
1089 error = DNSServiceRegister(srv, kDNSServiceFlagsShareConnection,
1090 0, name, temp, NULL, NULL, htons(port),
1091 txt ? TXTRecordGetLength(txt) : 0,
1092 txt ? TXTRecordGetBytesPtr(txt) : NULL,
1093 dnssdRegisterCallback, p);
1094
1095# else /* HAVE_AVAHI */
1096 if (txt)
e1d6a774 1097 {
f3c17241
MS
1098 AvahiStringList *temptxt;
1099 for (temptxt = *txt; temptxt; temptxt = temptxt->next)
1100 cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS_SD \"%s\" %s", name, temptxt->text);
e1d6a774 1101 }
1102
f3c17241
MS
1103 error = avahi_entry_group_add_service_strlst(*srv, AVAHI_IF_UNSPEC,
1104 AVAHI_PROTO_UNSPEC, 0, name,
1105 type, NULL, NULL, port,
1106 txt ? *txt : NULL);
1107 if (error)
1108 cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD service add for \"%s\" failed.",
1109 name);
e1d6a774 1110
f3c17241 1111 if (!error && subtypes)
a2326b5b
MS
1112 {
1113 /*
f3c17241 1114 * Register all of the subtypes...
a2326b5b 1115 */
ef416fc2 1116
f3c17241
MS
1117 char *start, /* Start of subtype */
1118 subtype[256]; /* Subtype string */
ef416fc2 1119
f3c17241
MS
1120 strlcpy(temp, subtypes, sizeof(temp));
1121
1122 for (start = temp; *start; start = ptr)
a2326b5b
MS
1123 {
1124 /*
f3c17241 1125 * Skip leading whitespace...
a2326b5b 1126 */
ef416fc2 1127
f3c17241
MS
1128 while (*start && isspace(*start & 255))
1129 start ++;
1130
1131 /*
1132 * Grab everything up to the next comma or the end of the string...
1133 */
1134
1135 for (ptr = start; *ptr && *ptr != ','; ptr ++);
ef416fc2 1136
f3c17241
MS
1137 if (*ptr)
1138 *ptr++ = '\0';
1139
1140 if (!*start)
1141 break;
1142
1143 /*
1144 * Register the subtype...
1145 */
1146
1147 snprintf(subtype, sizeof(subtype), "%s._sub.%s", start, type);
1148
1149 error = avahi_entry_group_add_service_subtype(*srv, AVAHI_IF_UNSPEC,
1150 AVAHI_PROTO_UNSPEC, 0,
1151 name, type, NULL, subtype);
1152 if (error)
1153 {
1154 cupsdLogMessage(CUPSD_LOG_DEBUG,
1155 "DNS-SD subtype %s registration for \"%s\" failed." ,
1156 subtype, name);
1157 break;
1158 }
a2326b5b
MS
1159 }
1160 }
ef416fc2 1161
f3c17241 1162 if (!error && commit)
a2326b5b 1163 {
f3c17241
MS
1164 if ((error = avahi_entry_group_commit(*srv)) != 0)
1165 cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD commit of \"%s\" failed.",
1166 name);
1167 }
ef416fc2 1168
f3c17241
MS
1169 avahi_threaded_poll_unlock(DNSSDMaster);
1170# endif /* HAVE_DNSSD */
ef416fc2 1171
f3c17241
MS
1172 if (error)
1173 {
1174 cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s",
1175 name, dnssdErrorString(error));
1176 cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD type: %s", type);
1177 if (subtypes)
1178 cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD sub-types: %s", subtypes);
1179 }
ef416fc2 1180
f3c17241
MS
1181 return (!error);
1182}
ef416fc2 1183
ef416fc2 1184
f3c17241
MS
1185/*
1186 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
1187 * or update the broadcast contents.
1188 */
ef416fc2 1189
f3c17241
MS
1190static void
1191dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
1192{
1193 char name[256]; /* Service name */
1194 int printer_port; /* LPD port number */
1195 int status; /* Registration status */
1196 cupsd_txt_t ipp_txt, /* IPP(S) TXT record */
1197 printer_txt; /* LPD TXT record */
ef416fc2 1198
f3c17241
MS
1199 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
1200 !p->ipp_srv ? "new" : "update");
ef416fc2 1201
f3c17241
MS
1202 /*
1203 * Remove the current registrations if we have them and then return if
1204 * per-printer sharing was just disabled...
1205 */
1206
1207 dnssdDeregisterPrinter(p, 0);
ef416fc2 1208
f3c17241
MS
1209 if (!p->shared)
1210 return;
1211
1212 /*
1213 * Set the registered name as needed; the registered name takes the form of
1214 * "<printer-info> @ <computer name>"...
1215 */
1216
1217 if (!p->reg_name)
1218 {
1219 if (p->info && strlen(p->info) > 0)
a2326b5b 1220 {
f3c17241
MS
1221 if (DNSSDComputerName)
1222 snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDComputerName);
1223 else
1224 strlcpy(name, p->info, sizeof(name));
a2326b5b 1225 }
f3c17241
MS
1226 else if (DNSSDComputerName)
1227 snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDComputerName);
a2326b5b 1228 else
f3c17241 1229 strlcpy(name, p->name, sizeof(name));
a2326b5b 1230 }
f3c17241
MS
1231 else
1232 strlcpy(name, p->reg_name, sizeof(name));
ef416fc2 1233
f3c17241
MS
1234 /*
1235 * Register IPP and LPD...
1236 *
1237 * We always must register the "_printer" service type in order to reserve
1238 * our name, but use port number 0 if we haven't actually configured cups-lpd
1239 * to share via LPD...
1240 */
1241
1242 ipp_txt = dnssdBuildTxtRecord(p, 0);
1243 printer_txt = dnssdBuildTxtRecord(p, 1);
ef416fc2 1244
a2326b5b 1245 if (BrowseLocalProtocols & BROWSE_LPD)
a2326b5b 1246 printer_port = 515;
a2326b5b 1247 else
a2326b5b 1248 printer_port = 0;
ef416fc2 1249
f3c17241
MS
1250 status = dnssdRegisterInstance(NULL, p, name, "_printer._tcp", NULL,
1251 printer_port, &printer_txt, 0);
1252
1253# ifdef HAVE_SSL
1254 if (status)
1255 dnssdRegisterInstance(NULL, p, name, "_ipps._tcp", DNSSDSubTypes,
1256 DNSSDPort, &ipp_txt, 0);
1257# endif /* HAVE_SSL */
1258
1259 if (status)
a2326b5b 1260 {
ef416fc2 1261 /*
f3c17241 1262 * Use the "_fax-ipp" service type for fax queues, otherwise use "_ipp"...
ef416fc2 1263 */
1264
f3c17241
MS
1265 if (p->type & CUPS_PRINTER_FAX)
1266 status = dnssdRegisterInstance(NULL, p, name, "_fax-ipp._tcp",
1267 DNSSDSubTypes, DNSSDPort, &ipp_txt, 1);
a2326b5b 1268 else
f3c17241
MS
1269 status = dnssdRegisterInstance(NULL, p, name, "_ipp._tcp", DNSSDSubTypes,
1270 DNSSDPort, &ipp_txt, 1);
ef416fc2 1271 }
1272
f3c17241
MS
1273 dnssdFreeTxtRecord(&ipp_txt);
1274 dnssdFreeTxtRecord(&printer_txt);
1275
1276 if (status)
a2326b5b
MS
1277 {
1278 /*
f3c17241
MS
1279 * Save the registered name and add the printer to the array of DNS-SD
1280 * printers...
a2326b5b 1281 */
ef416fc2 1282
f3c17241
MS
1283 cupsdSetString(&p->reg_name, name);
1284 cupsArrayAdd(DNSSDPrinters, p);
a2326b5b 1285 }
f3c17241
MS
1286 else
1287 {
1288 /*
1289 * Registration failed for this printer...
1290 */
ef416fc2 1291
f3c17241
MS
1292 dnssdDeregisterInstance(&p->ipp_srv);
1293
1294# ifdef HAVE_DNSSD
1295# ifdef HAVE_SSL
1296 dnssdDeregisterInstance(&p->ipps_srv);
1297# endif /* HAVE_SSL */
1298 dnssdDeregisterInstance(&p->printer_srv);
1299# endif /* HAVE_DNSSD */
1300 }
ef416fc2 1301}
1302
1303
1304/*
a2326b5b 1305 * 'dnssdStop()' - Stop all DNS-SD registrations.
ef416fc2 1306 */
1307
a2326b5b
MS
1308static void
1309dnssdStop(void)
ef416fc2 1310{
a2326b5b 1311 cupsd_printer_t *p; /* Current printer */
ef416fc2 1312
1313
1314 /*
a2326b5b 1315 * De-register the individual printers
ef416fc2 1316 */
1317
a2326b5b
MS
1318 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1319 p;
1320 p = (cupsd_printer_t *)cupsArrayNext(Printers))
f3c17241 1321 dnssdDeregisterPrinter(p, 1);
ef416fc2 1322
1323 /*
a2326b5b 1324 * Shutdown the rest of the service refs...
ef416fc2 1325 */
1326
f3c17241
MS
1327 dnssdDeregisterInstance(&WebIFSrv);
1328
1329# ifdef HAVE_DNSSD
1330 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDMaster));
1331
1332 DNSServiceRefDeallocate(DNSSDMaster);
1333 DNSSDMaster = NULL;
ef416fc2 1334
f3c17241 1335# else /* HAVE_AVAHI */
a469f8a5
MS
1336 avahi_threaded_poll_stop(DNSSDMaster);
1337
f3c17241
MS
1338 avahi_client_free(DNSSDClient);
1339 DNSSDClient = NULL;
ef416fc2 1340
f3c17241
MS
1341 avahi_threaded_poll_free(DNSSDMaster);
1342 DNSSDMaster = NULL;
1343# endif /* HAVE_DNSSD */
ef416fc2 1344
a2326b5b
MS
1345 cupsArrayDelete(DNSSDPrinters);
1346 DNSSDPrinters = NULL;
ef416fc2 1347
a2326b5b
MS
1348 DNSSDPort = 0;
1349}
ef416fc2 1350
ef416fc2 1351
f3c17241 1352# ifdef HAVE_DNSSD
a2326b5b
MS
1353/*
1354 * 'dnssdUpdate()' - Handle DNS-SD queries.
1355 */
ef416fc2 1356
a2326b5b
MS
1357static void
1358dnssdUpdate(void)
1359{
1360 DNSServiceErrorType sdErr; /* Service discovery error */
ef416fc2 1361
ef416fc2 1362
f3c17241 1363 if ((sdErr = DNSServiceProcessResult(DNSSDMaster)) != kDNSServiceErr_NoError)
a2326b5b
MS
1364 {
1365 cupsdLogMessage(CUPSD_LOG_ERROR,
1366 "DNS Service Discovery registration error %d!",
1367 sdErr);
1368 dnssdStop();
1369 }
ef416fc2 1370}
f3c17241 1371# endif /* HAVE_DNSSD */
ef416fc2 1372
1373
1374/*
a2326b5b 1375 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
f899b121 1376 */
1377
a2326b5b
MS
1378static char * /* O - String or NULL if none */
1379get_auth_info_required(
1380 cupsd_printer_t *p, /* I - Printer */
1381 char *buffer, /* I - Value buffer */
1382 size_t bufsize) /* I - Size of value buffer */
f899b121 1383{
a2326b5b
MS
1384 cupsd_location_t *auth; /* Pointer to authentication element */
1385 char resource[1024]; /* Printer/class resource path */
f899b121 1386
1387
1388 /*
a2326b5b 1389 * If auth-info-required is set for this printer, return that...
f899b121 1390 */
1391
a2326b5b 1392 if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
f899b121 1393 {
a2326b5b
MS
1394 int i; /* Looping var */
1395 char *bufptr; /* Pointer into buffer */
f899b121 1396
a2326b5b 1397 for (i = 0, bufptr = buffer; i < p->num_auth_info_required; i ++)
f899b121 1398 {
a2326b5b
MS
1399 if (bufptr >= (buffer + bufsize - 2))
1400 break;
7a14d768 1401
a2326b5b
MS
1402 if (i)
1403 *bufptr++ = ',';
f899b121 1404
a2326b5b
MS
1405 strlcpy(bufptr, p->auth_info_required[i], bufsize - (bufptr - buffer));
1406 bufptr += strlen(bufptr);
f899b121 1407 }
1408
a2326b5b 1409 return (buffer);
f899b121 1410 }
1411
f899b121 1412 /*
a2326b5b 1413 * Figure out the authentication data requirements to advertise...
f899b121 1414 */
1415
a2326b5b
MS
1416 if (p->type & CUPS_PRINTER_CLASS)
1417 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
f899b121 1418 else
a2326b5b 1419 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
f899b121 1420
a2326b5b
MS
1421 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
1422 auth->type == CUPSD_AUTH_NONE)
1423 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
f899b121 1424
a2326b5b 1425 if (auth)
f899b121 1426 {
a2326b5b 1427 int auth_type; /* Authentication type */
f899b121 1428
a2326b5b 1429 if ((auth_type = auth->type) == CUPSD_AUTH_DEFAULT)
dcb445bc 1430 auth_type = cupsdDefaultAuthType();
f899b121 1431
a2326b5b
MS
1432 switch (auth_type)
1433 {
1434 case CUPSD_AUTH_NONE :
1435 return (NULL);
f899b121 1436
a2326b5b
MS
1437 case CUPSD_AUTH_NEGOTIATE :
1438 strlcpy(buffer, "negotiate", bufsize);
1439 break;
f899b121 1440
a2326b5b
MS
1441 default :
1442 strlcpy(buffer, "username,password", bufsize);
1443 break;
1444 }
f899b121 1445
a2326b5b
MS
1446 return (buffer);
1447 }
f899b121 1448
a2326b5b
MS
1449 return ("none");
1450}
f3c17241 1451#endif /* HAVE_DNSSD || HAVE_AVAHI */
f899b121 1452
f899b121 1453
a2326b5b
MS
1454#ifdef __APPLE__
1455/*
1456 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
1457 */
f899b121 1458
a2326b5b
MS
1459static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
1460get_hostconfig(const char *name) /* I - Name of service */
1461{
1462 cups_file_t *fp; /* Hostconfig file */
1463 char line[1024], /* Line from file */
1464 *ptr; /* Pointer to value */
1465 int state = 1; /* State of service */
f899b121 1466
f899b121 1467
1468 /*
a2326b5b
MS
1469 * Try opening the /etc/hostconfig file; if we can't open it, assume that
1470 * the service is enabled/auto.
f899b121 1471 */
1472
a2326b5b 1473 if ((fp = cupsFileOpen("/etc/hostconfig", "r")) != NULL)
f899b121 1474 {
1475 /*
a2326b5b 1476 * Read lines from the file until we find the service...
f899b121 1477 */
1478
a2326b5b 1479 while (cupsFileGets(fp, line, sizeof(line)))
f899b121 1480 {
a2326b5b
MS
1481 if (line[0] == '#' || (ptr = strchr(line, '=')) == NULL)
1482 continue;
f899b121 1483
a2326b5b 1484 *ptr++ = '\0';
f899b121 1485
a2326b5b 1486 if (!_cups_strcasecmp(line, name))
f899b121 1487 {
a2326b5b
MS
1488 /*
1489 * Found the service, see if it is set to "-NO-"...
1490 */
f899b121 1491
a2326b5b
MS
1492 if (!_cups_strncasecmp(ptr, "-NO-", 4))
1493 state = 0;
1494 break;
f899b121 1495 }
1496 }
f899b121 1497
a2326b5b 1498 cupsFileClose(fp);
f899b121 1499 }
1500
a2326b5b 1501 return (state);
f899b121 1502}
a2326b5b 1503#endif /* __APPLE__ */
f899b121 1504
1505
2e4ff8af
MS
1506/*
1507 * 'update_lpd()' - Update the LPD configuration as needed.
1508 */
1509
1510static void
1511update_lpd(int onoff) /* - 1 = turn on, 0 = turn off */
1512{
1513 if (!LPDConfigFile)
1514 return;
1515
a603edef
MS
1516#ifdef __APPLE__
1517 /*
1518 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
1519 * setting for backwards-compatibility.
1520 */
1521
1522 if (onoff && !get_hostconfig("CUPS_LPD"))
1523 onoff = 0;
1524#endif /* __APPLE__ */
1525
2e4ff8af
MS
1526 if (!strncmp(LPDConfigFile, "xinetd:///", 10))
1527 {
1528 /*
1529 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
1530 */
1531
1532 char newfile[1024]; /* New cups-lpd.N file */
1533 cups_file_t *ofp, /* Original file pointer */
1534 *nfp; /* New file pointer */
1535 char line[1024]; /* Line from file */
1536
1537
1538 snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9);
1539
1540 if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL)
1541 {
1542 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
1543 LPDConfigFile + 9, strerror(errno));
1544 return;
1545 }
1546
1547 if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
1548 {
1549 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
1550 newfile, strerror(errno));
1551 cupsFileClose(ofp);
1552 return;
1553 }
1554
1555 /*
1556 * Copy all of the lines from the cups-lpd file...
1557 */
1558
1559 while (cupsFileGets(ofp, line, sizeof(line)))
1560 {
1561 if (line[0] == '{')
1562 {
1563 cupsFilePrintf(nfp, "%s\n", line);
1564 snprintf(line, sizeof(line), "\tdisable = %s",
1565 onoff ? "no" : "yes");
1566 }
568fa3fa
MS
1567 else if (!strstr(line, "disable ="))
1568 cupsFilePrintf(nfp, "%s\n", line);
2e4ff8af
MS
1569 }
1570
1571 cupsFileClose(nfp);
1572 cupsFileClose(ofp);
1573 rename(newfile, LPDConfigFile + 9);
1574 }
568fa3fa 1575#ifdef __APPLE__
2e4ff8af
MS
1576 else if (!strncmp(LPDConfigFile, "launchd:///", 11))
1577 {
1578 /*
1579 * Enable/disable LPD via the launchctl command...
1580 */
1581
1582 char *argv[5], /* Arguments for command */
1583 *envp[MAX_ENV]; /* Environment for command */
1584 int pid; /* Process ID */
1585
1586
1587 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
1588 argv[0] = (char *)"launchctl";
1589 argv[1] = (char *)(onoff ? "load" : "unload");
1590 argv[2] = (char *)"-w";
1591 argv[3] = LPDConfigFile + 10;
1592 argv[4] = NULL;
1593
a4924f6c 1594 cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1,
38e73f87 1595 NULL, NULL, &pid);
2e4ff8af 1596 }
568fa3fa
MS
1597#endif /* __APPLE__ */
1598 else
1599 cupsdLogMessage(CUPSD_LOG_INFO, "Unknown LPDConfigFile scheme!");
2e4ff8af
MS
1600}
1601
1602
f899b121 1603/*
2e4ff8af
MS
1604 * 'update_smb()' - Update the SMB configuration as needed.
1605 */
1606
1607static void
1608update_smb(int onoff) /* I - 1 = turn on, 0 = turn off */
1609{
1610 if (!SMBConfigFile)
1611 return;
1612
1613 if (!strncmp(SMBConfigFile, "samba:///", 9))
1614 {
1615 /*
1616 * Enable/disable SMB via the specified smb.conf config file...
1617 */
1618
1619 char newfile[1024]; /* New smb.conf.N file */
1620 cups_file_t *ofp, /* Original file pointer */
1621 *nfp; /* New file pointer */
1622 char line[1024]; /* Line from file */
1623 int in_printers; /* In [printers] section? */
1624
1625
1626 snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8);
1627
1628 if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL)
1629 {
1630 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
1631 SMBConfigFile + 8, strerror(errno));
1632 return;
1633 }
1634
1635 if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
1636 {
1637 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
1638 newfile, strerror(errno));
1639 cupsFileClose(ofp);
1640 return;
1641 }
1642
1643 /*
1644 * Copy all of the lines from the smb.conf file...
1645 */
1646
1647 in_printers = 0;
1648
1649 while (cupsFileGets(ofp, line, sizeof(line)))
1650 {
1651 if (in_printers && strstr(line, "printable ="))
1652 snprintf(line, sizeof(line), " printable = %s",
1653 onoff ? "yes" : "no");
1654
1655 cupsFilePrintf(nfp, "%s\n", line);
1656
1657 if (line[0] == '[')
1658 in_printers = !strcmp(line, "[printers]");
1659 }
1660
1661 cupsFileClose(nfp);
1662 cupsFileClose(ofp);
1663 rename(newfile, SMBConfigFile + 8);
1664 }
568fa3fa
MS
1665 else
1666 cupsdLogMessage(CUPSD_LOG_INFO, "Unknown SMBConfigFile scheme!");
2e4ff8af
MS
1667}
1668
1669
1670/*
f2d18633 1671 * End of "$Id$".
ef416fc2 1672 */