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