2 * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
20 * Include necessary headers...
30 # ifdef HAVE_COREFOUNDATION
31 # include <CoreFoundation/CoreFoundation.h>
32 # endif /* HAVE_COREFOUNDATION */
33 # ifdef HAVE_SYSTEMCONFIGURATION
34 # include <SystemConfiguration/SystemConfiguration.h>
35 # endif /* HAVE_SYSTEMCONFIGURATION */
36 # endif /* __APPLE__ */
37 #endif /* HAVE_DNSSD */
44 static char *dequote(char *d
, const char *s
, int dlen
);
46 static int get_hostconfig(const char *name
);
47 #endif /* __APPLE__ */
48 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
49 char *resource
, int resourcelen
);
50 static void process_browse_data(const char *uri
, const char *host
,
51 const char *resource
, cups_ptype_t type
,
52 ipp_pstate_t state
, const char *location
,
53 const char *info
, const char *make_model
,
54 int num_attrs
, cups_option_t
*attrs
);
55 static void process_implicit_classes(void);
56 static void send_cups_browse(cupsd_printer_t
*p
);
58 static void send_ldap_browse(cupsd_printer_t
*p
);
59 #endif /* HAVE_LDAP */
61 static void send_slp_browse(cupsd_printer_t
*p
);
62 #endif /* HAVE_LIBSLP */
63 static void update_cups_browse(void);
64 static void update_lpd(int onoff
);
65 static void update_polling(void);
66 static void update_smb(int onoff
);
70 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
72 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
73 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
74 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
76 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
77 DNSServiceFlags flags
,
78 DNSServiceErrorType errorCode
,
79 const char *name
, const char *regtype
,
80 const char *domain
, void *context
);
81 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
82 #endif /* HAVE_DNSSD */
85 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
89 "printerMakeAndModel",
94 #endif /* HAVE_OPENLDAP */
102 * SLP service name for CUPS...
105 # define SLP_CUPS_SRVTYPE "service:printer"
106 # define SLP_CUPS_SRVLEN 15
110 * Printer service URL structure
113 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
115 struct _slpsrvurl_s
*next
; /* Next URL in list */
116 char url
[HTTP_MAX_URI
];
125 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
126 SLPError errcode
, void *cookie
);
127 static void slp_dereg_printer(cupsd_printer_t
*p
);
128 static int slp_get_attr(const char *attrlist
, const char *tag
,
130 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
132 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
133 unsigned short lifetime
,
134 SLPError errcode
, void *cookie
);
135 #endif /* HAVE_LIBSLP */
139 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
140 * local printer and remove any pending
141 * references to remote printers.
145 cupsdDeregisterPrinter(
146 cupsd_printer_t
*p
, /* I - Printer to register */
147 int removeit
) /* I - Printer being permanently removed */
150 * Only deregister if browsing is enabled and it's a local printer...
153 if (!Browsing
|| !p
->shared
||
154 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
158 * Announce the deletion...
161 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
163 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
165 p
->type
|= CUPS_PRINTER_DELETE
;
173 if (BrowseLocalProtocols
& BROWSE_SLP
)
174 slp_dereg_printer(p
);
175 #endif /* HAVE_LIBSLP */
178 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDPort
)
179 dnssdDeregisterPrinter(p
);
180 #endif /* HAVE_DNSSD */
185 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
189 cupsdLoadRemoteCache(void)
191 cups_file_t
*fp
; /* remote.cache file */
192 int linenum
; /* Current line number */
193 char line
[1024], /* Line from file */
194 *value
, /* Pointer to value */
195 *valueptr
, /* Pointer into value */
196 scheme
[32], /* Scheme portion of URI */
197 username
[64], /* Username portion of URI */
199 /* Hostname portion of URI */
200 resource
[HTTP_MAX_URI
];
201 /* Resource portion of URI */
202 int port
; /* Port number */
203 cupsd_printer_t
*p
; /* Current printer */
204 time_t now
; /* Current time */
208 * Don't load the cache if the remote protocols are disabled...
213 cupsdLogMessage(CUPSD_LOG_DEBUG
,
214 "cupsdLoadRemoteCache: Not loading remote cache.");
219 * Open the remote.cache file...
222 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
223 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
227 * Read printer configurations until we hit EOF...
234 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
237 * Decode the directive...
240 if (!strcasecmp(line
, "<Printer") ||
241 !strcasecmp(line
, "<DefaultPrinter"))
244 * <Printer name> or <DefaultPrinter name>
247 if (p
== NULL
&& value
)
250 * Add the printer and a base file type...
253 cupsdLogMessage(CUPSD_LOG_DEBUG
,
254 "cupsdLoadRemoteCache: Loading printer %s...", value
);
256 if ((p
= cupsdFindDest(value
)) != NULL
)
258 if (p
->type
& CUPS_PRINTER_CLASS
)
260 cupsdLogMessage(CUPSD_LOG_WARN
,
261 "Cached remote printer \"%s\" conflicts with "
269 p
= cupsdAddPrinter(value
);
272 p
->state
= IPP_PRINTER_IDLE
;
273 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
274 p
->browse_time
= now
;
275 p
->browse_expire
= now
+ BrowseTimeout
;
278 * Set the default printer as needed...
281 if (!strcasecmp(line
, "<DefaultPrinter"))
286 cupsdLogMessage(CUPSD_LOG_ERROR
,
287 "Syntax error on line %d of remote.cache.", linenum
);
291 else if (!strcasecmp(line
, "<Class") ||
292 !strcasecmp(line
, "<DefaultClass"))
295 * <Class name> or <DefaultClass name>
298 if (p
== NULL
&& value
)
301 * Add the printer and a base file type...
304 cupsdLogMessage(CUPSD_LOG_DEBUG
,
305 "cupsdLoadRemoteCache: Loading class %s...", value
);
307 if ((p
= cupsdFindDest(value
)) != NULL
)
308 p
->type
= CUPS_PRINTER_CLASS
;
310 p
= cupsdAddClass(value
);
313 p
->state
= IPP_PRINTER_IDLE
;
314 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
315 p
->browse_time
= now
;
316 p
->browse_expire
= now
+ BrowseTimeout
;
319 * Set the default printer as needed...
322 if (!strcasecmp(line
, "<DefaultClass"))
327 cupsdLogMessage(CUPSD_LOG_ERROR
,
328 "Syntax error on line %d of remote.cache.", linenum
);
332 else if (!strcasecmp(line
, "</Printer>") ||
333 !strcasecmp(line
, "</Class>"))
338 * Close out the current printer...
341 cupsdSetPrinterAttrs(p
);
347 cupsdLogMessage(CUPSD_LOG_ERROR
,
348 "Syntax error on line %d of remote.cache.", linenum
);
354 cupsdLogMessage(CUPSD_LOG_ERROR
,
355 "Syntax error on line %d of remote.cache.", linenum
);
358 else if (!strcasecmp(line
, "Info"))
361 cupsdSetString(&p
->info
, value
);
363 else if (!strcasecmp(line
, "MakeModel"))
366 cupsdSetString(&p
->make_model
, value
);
368 else if (!strcasecmp(line
, "Location"))
371 cupsdSetString(&p
->location
, value
);
373 else if (!strcasecmp(line
, "DeviceURI"))
377 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
378 username
, sizeof(username
), host
, sizeof(host
), &port
,
379 resource
, sizeof(resource
));
381 cupsdSetString(&p
->hostname
, host
);
382 cupsdSetString(&p
->uri
, value
);
383 cupsdSetString(&p
->device_uri
, value
);
387 cupsdLogMessage(CUPSD_LOG_ERROR
,
388 "Syntax error on line %d of remote.cache.", linenum
);
392 else if (!strcasecmp(line
, "Option") && value
)
398 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
401 cupsdLogMessage(CUPSD_LOG_ERROR
,
402 "Syntax error on line %d of remote.cache.", linenum
);
405 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
407 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
411 else if (!strcasecmp(line
, "State"))
414 * Set the initial queue state...
417 if (value
&& !strcasecmp(value
, "idle"))
418 p
->state
= IPP_PRINTER_IDLE
;
419 else if (value
&& !strcasecmp(value
, "stopped"))
420 p
->state
= IPP_PRINTER_STOPPED
;
423 cupsdLogMessage(CUPSD_LOG_ERROR
,
424 "Syntax error on line %d of remote.cache.", linenum
);
428 else if (!strcasecmp(line
, "StateMessage"))
431 * Set the initial queue state message...
435 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
437 else if (!strcasecmp(line
, "Accepting"))
440 * Set the initial accepting state...
444 (!strcasecmp(value
, "yes") ||
445 !strcasecmp(value
, "on") ||
446 !strcasecmp(value
, "true")))
449 (!strcasecmp(value
, "no") ||
450 !strcasecmp(value
, "off") ||
451 !strcasecmp(value
, "false")))
455 cupsdLogMessage(CUPSD_LOG_ERROR
,
456 "Syntax error on line %d of remote.cache.", linenum
);
460 else if (!strcasecmp(line
, "Type"))
463 p
->type
= atoi(value
);
466 cupsdLogMessage(CUPSD_LOG_ERROR
,
467 "Syntax error on line %d of remote.cache.", linenum
);
471 else if (!strcasecmp(line
, "BrowseTime"))
475 time_t t
= atoi(value
);
477 if (t
> p
->browse_expire
)
478 p
->browse_expire
= t
;
482 cupsdLogMessage(CUPSD_LOG_ERROR
,
483 "Syntax error on line %d of remote.cache.", linenum
);
487 else if (!strcasecmp(line
, "JobSheets"))
490 * Set the initial job sheets...
495 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
500 cupsdSetString(&p
->job_sheets
[0], value
);
502 while (isspace(*valueptr
& 255))
507 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
512 cupsdSetString(&p
->job_sheets
[1], value
);
517 cupsdLogMessage(CUPSD_LOG_ERROR
,
518 "Syntax error on line %d of remote.cache.", linenum
);
522 else if (!strcasecmp(line
, "AllowUser"))
527 cupsdAddPrinterUser(p
, value
);
531 cupsdLogMessage(CUPSD_LOG_ERROR
,
532 "Syntax error on line %d of remote.cache.", linenum
);
536 else if (!strcasecmp(line
, "DenyUser"))
541 cupsdAddPrinterUser(p
, value
);
545 cupsdLogMessage(CUPSD_LOG_ERROR
,
546 "Syntax error on line %d of remote.cache.", linenum
);
553 * Something else we don't understand...
556 cupsdLogMessage(CUPSD_LOG_ERROR
,
557 "Unknown configuration directive %s on line %d of remote.cache.",
565 * Do auto-classing if needed...
568 process_implicit_classes();
573 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
574 * printer or update the broadcast contents.
578 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
580 if (!Browsing
|| !BrowseLocalProtocols
||
581 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
585 /* if (BrowseLocalProtocols & BROWSE_SLP)
586 slpRegisterPrinter(p); */
587 #endif /* HAVE_LIBSLP */
590 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDPort
)
591 dnssdRegisterPrinter(p
);
592 #endif /* HAVE_DNSSD */
597 * 'cupsdRestartPolling()' - Restart polling servers as needed.
601 cupsdRestartPolling(void)
603 int i
; /* Looping var */
604 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
607 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
609 kill(pollp
->pid
, SIGHUP
);
614 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
618 cupsdSaveRemoteCache(void)
620 int i
; /* Looping var */
621 cups_file_t
*fp
; /* printers.conf file */
622 char temp
[1024]; /* Temporary string */
623 cupsd_printer_t
*printer
; /* Current printer class */
624 time_t curtime
; /* Current time */
625 struct tm
*curdate
; /* Current date */
626 cups_option_t
*option
; /* Current option */
630 * Create the remote.cache file...
633 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
635 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
637 cupsdLogMessage(CUPSD_LOG_ERROR
,
638 "Unable to save remote.cache - %s", strerror(errno
));
642 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
645 * Restrict access to the file...
648 fchown(cupsFileNumber(fp
), getuid(), Group
);
649 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
652 * Write a small header to the file...
655 curtime
= time(NULL
);
656 curdate
= localtime(&curtime
);
657 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
659 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
660 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
663 * Write each local printer known to the system...
666 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
668 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
671 * Skip local destinations...
674 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
678 * Write printers as needed...
681 if (printer
== DefaultPrinter
)
682 cupsFilePuts(fp
, "<Default");
684 cupsFilePutChar(fp
, '<');
686 if (printer
->type
& CUPS_PRINTER_CLASS
)
687 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
689 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
691 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
693 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
696 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
698 if (printer
->make_model
)
699 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
701 if (printer
->location
)
702 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
704 if (printer
->device_uri
)
705 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
707 if (printer
->state
== IPP_PRINTER_STOPPED
)
709 cupsFilePuts(fp
, "State Stopped\n");
710 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
713 cupsFilePuts(fp
, "State Idle\n");
715 if (printer
->accepting
)
716 cupsFilePuts(fp
, "Accepting Yes\n");
718 cupsFilePuts(fp
, "Accepting No\n");
720 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
721 printer
->job_sheets
[1]);
723 for (i
= 0; i
< printer
->num_users
; i
++)
724 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
727 for (i
= printer
->num_options
, option
= printer
->options
;
730 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
732 if (printer
->type
& CUPS_PRINTER_CLASS
)
733 cupsFilePuts(fp
, "</Class>\n");
735 cupsFilePuts(fp
, "</Printer>\n");
743 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
747 cupsdSendBrowseList(void)
749 int count
; /* Number of dests to update */
750 cupsd_printer_t
*p
; /* Current printer */
751 time_t ut
, /* Minimum update time */
752 to
; /* Timeout time */
755 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
759 * Compute the update and timeout times...
763 ut
= to
- BrowseInterval
;
766 * Figure out how many printers need an update...
769 if (BrowseInterval
> 0)
771 int max_count
; /* Maximum number to update */
775 * Throttle the number of printers we'll be updating this time
776 * around based on the number of queues that need updating and
777 * the maximum number of queues to update each second...
780 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
782 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
783 count
< max_count
&& p
!= NULL
;
784 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
785 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
786 p
->shared
&& p
->browse_time
< ut
)
790 * Loop through all of the printers and send local updates as needed...
794 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
796 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
800 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
803 * Check for wraparound...
807 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
811 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
814 else if (p
->browse_time
< ut
)
817 * Need to send an update...
822 p
->browse_time
= time(NULL
);
824 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
828 if (BrowseLocalProtocols
& BROWSE_SLP
)
830 #endif /* HAVE_LIBSLP */
833 if (BrowseLocalProtocols
& BROWSE_LDAP
)
835 #endif /* HAVE_LDAP */
840 * Save where we left off so that all printers get updated...
847 * Loop through all of the printers and send local updates as needed...
850 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
852 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
855 * If this is a remote queue, see if it needs to be timed out...
858 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
860 if (p
->browse_expire
< to
)
862 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
863 "%s \'%s\' deleted by directory services (timeout).",
864 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
867 cupsdLogMessage(CUPSD_LOG_DEBUG
,
868 "Remote destination \"%s\" has timed out; "
872 cupsArraySave(Printers
);
873 cupsdDeletePrinter(p
, 1);
874 cupsArrayRestore(Printers
);
875 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
883 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
887 cupsdStartBrowsing(void)
889 int val
; /* Socket option value */
890 struct sockaddr_in addr
; /* Broadcast address */
891 cupsd_printer_t
*p
; /* Current printer */
896 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
899 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
901 if (BrowseSocket
< 0)
904 * Create the broadcast socket...
907 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
909 cupsdLogMessage(CUPSD_LOG_ERROR
,
910 "Unable to create broadcast socket - %s.",
912 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
913 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
918 * Bind the socket to browse port...
921 memset(&addr
, 0, sizeof(addr
));
922 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
923 addr
.sin_family
= AF_INET
;
924 addr
.sin_port
= htons(BrowsePort
);
926 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
928 cupsdLogMessage(CUPSD_LOG_ERROR
,
929 "Unable to bind broadcast socket - %s.",
933 closesocket(BrowseSocket
);
939 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
940 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
946 * Set the "broadcast" flag...
950 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
952 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
956 closesocket(BrowseSocket
);
962 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
963 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
968 * Close the socket on exec...
971 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
974 * Finally, add the socket to the input selection set as needed...
977 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
980 * We only listen if we want remote printers...
983 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
991 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
993 DNSServiceErrorType error
; /* Error from service creation */
994 cupsd_listener_t
*lis
; /* Current listening socket */
998 * First create a "master" connection for all registrations...
1001 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1002 != kDNSServiceErr_NoError
)
1003 cupsdLogMessage(CUPSD_LOG_ERROR
,
1004 "Unable to create master DNS-SD reference: %d", error
);
1008 * Add the master connection to the select list...
1011 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1012 (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
, NULL
, NULL
);
1015 * Then get the port we use for registrations. If we are not listening
1016 * on any non-local ports, there is no sense sharing local printers via
1022 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1024 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1026 if (httpAddrLocalhost(&(lis
->address
)))
1029 if (lis
->address
.addr
.sa_family
== AF_INET
)
1031 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1034 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1036 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1042 * Create an array to track the printers we share...
1045 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1046 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1050 * Set the computer name and register the web interface...
1053 cupsdUpdateDNSSDName();
1056 #endif /* HAVE_DNSSD */
1059 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1062 * Open SLP handle...
1065 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1067 cupsdLogMessage(CUPSD_LOG_ERROR
,
1068 "Unable to open an SLP handle; disabling SLP browsing!");
1069 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1070 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1073 BrowseSLPRefresh
= 0;
1076 BrowseSLPHandle
= NULL
;
1077 #endif /* HAVE_LIBSLP */
1079 #ifdef HAVE_OPENLDAP
1080 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1084 cupsdLogMessage(CUPSD_LOG_ERROR
,
1085 "Need to set BrowseLDAPDN to use LDAP browsing!");
1086 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1087 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1092 * Open LDAP handle...
1095 int rc
; /* LDAP API status */
1096 int version
= 3; /* LDAP version */
1097 struct berval bv
= {0, ""}; /* SASL bind value */
1101 * Set the certificate file to use for encrypted LDAP sessions...
1104 if (BrowseLDAPCACertFile
)
1106 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1107 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1108 BrowseLDAPCACertFile
);
1110 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1111 (void *)BrowseLDAPCACertFile
))
1113 cupsdLogMessage(CUPSD_LOG_ERROR
,
1114 "Unable to set CA certificate file for LDAP "
1115 "connections: %d - %s", rc
, ldap_err2string(rc
));
1119 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1122 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1123 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1125 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1127 if (rc
!= LDAP_SUCCESS
)
1129 cupsdLogMessage(CUPSD_LOG_ERROR
,
1130 "Unable to initialize LDAP; disabling LDAP browsing!");
1131 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1132 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1134 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1135 (const void *)&version
) != LDAP_SUCCESS
)
1137 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1138 BrowseLDAPHandle
= NULL
;
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "Unable to set LDAP protocol version; "
1141 "disabling LDAP browsing!");
1142 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1143 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1147 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1148 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1151 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1152 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1154 if (rc
!= LDAP_SUCCESS
)
1156 cupsdLogMessage(CUPSD_LOG_ERROR
,
1157 "Unable to bind to LDAP server; "
1158 "disabling LDAP browsing!");
1159 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1160 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1161 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1166 BrowseLDAPRefresh
= 0;
1168 #endif /* HAVE_OPENLDAP */
1171 * Enable LPD and SMB printer sharing as needed through external programs...
1174 if (BrowseLocalProtocols
& BROWSE_LPD
)
1177 if (BrowseLocalProtocols
& BROWSE_SMB
)
1181 * Register the individual printers
1184 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1186 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1187 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1188 cupsdRegisterPrinter(p
);
1193 * 'cupsdStartPolling()' - Start polling servers as needed.
1197 cupsdStartPolling(void)
1199 int i
; /* Looping var */
1200 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1201 char polld
[1024]; /* Poll daemon path */
1202 char sport
[255]; /* Server port */
1203 char bport
[255]; /* Browser port */
1204 char interval
[255]; /* Poll interval */
1205 int statusfds
[2]; /* Status pipe */
1206 char *argv
[6]; /* Arguments */
1207 char *envp
[100]; /* Environment */
1211 * Don't do anything if we aren't polling...
1214 if (NumPolled
== 0 || BrowseSocket
< 0)
1217 PollStatusBuffer
= NULL
;
1222 * Setup string arguments for polld, port and interval options.
1225 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1227 sprintf(bport
, "%d", BrowsePort
);
1230 sprintf(interval
, "%d", BrowseInterval
);
1232 strcpy(interval
, "30");
1234 argv
[0] = "cups-polld";
1240 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1243 * Create a pipe that receives the status messages from each
1247 if (cupsdOpenPipe(statusfds
))
1249 cupsdLogMessage(CUPSD_LOG_ERROR
,
1250 "Unable to create polling status pipes - %s.",
1253 PollStatusBuffer
= NULL
;
1257 PollPipe
= statusfds
[0];
1258 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1261 * Run each polling daemon, redirecting stderr to the polling pipe...
1264 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1266 sprintf(sport
, "%d", pollp
->port
);
1268 argv
[1] = pollp
->hostname
;
1270 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1271 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1273 cupsdLogMessage(CUPSD_LOG_ERROR
,
1274 "cupsdStartPolling: Unable to fork polling daemon - %s",
1280 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1281 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1282 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1285 close(statusfds
[1]);
1288 * Finally, add the pipe to the input selection set...
1291 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1296 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1300 cupsdStopBrowsing(void)
1302 cupsd_printer_t
*p
; /* Current printer */
1305 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1309 * De-register the individual printers
1312 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1314 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1315 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1316 cupsdDeregisterPrinter(p
, 1);
1319 * Shut down browsing sockets...
1322 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1326 * Close the socket and remove it from the input selection set.
1330 closesocket(BrowseSocket
);
1332 close(BrowseSocket
);
1335 cupsdRemoveSelect(BrowseSocket
);
1340 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1344 DNSServiceRefDeallocate(WebIFRef
);
1350 DNSServiceRefDeallocate(RemoteRef
);
1354 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1356 DNSServiceRefDeallocate(DNSSDRef
);
1359 cupsArrayDelete(DNSSDPrinters
);
1360 DNSSDPrinters
= NULL
;
1362 #endif /* HAVE_DNSSD */
1365 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1369 * Close SLP handle...
1372 SLPClose(BrowseSLPHandle
);
1373 BrowseSLPHandle
= NULL
;
1375 #endif /* HAVE_LIBSLP */
1377 #ifdef HAVE_OPENLDAP
1378 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1381 ldap_unbind(BrowseLDAPHandle
);
1382 BrowseLDAPHandle
= NULL
;
1384 #endif /* HAVE_OPENLDAP */
1387 * Disable LPD and SMB printer sharing as needed through external programs...
1390 if (BrowseLocalProtocols
& BROWSE_LPD
)
1393 if (BrowseLocalProtocols
& BROWSE_SMB
)
1399 * 'cupsdStopPolling()' - Stop polling servers as needed.
1403 cupsdStopPolling(void)
1405 int i
; /* Looping var */
1406 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1411 cupsdStatBufDelete(PollStatusBuffer
);
1414 cupsdRemoveSelect(PollPipe
);
1417 PollStatusBuffer
= NULL
;
1420 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1422 cupsdEndProcess(pollp
->pid
, 0);
1428 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1432 cupsdUpdateDNSSDBrowse(void)
1434 DNSServiceErrorType sdErr
; /* Service discovery error */
1437 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
1438 cupsdLogMessage(CUPSD_LOG_ERROR
,
1439 "DNS Service Discovery registration error %d!",
1445 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1449 cupsdUpdateDNSSDName(void)
1451 DNSServiceErrorType error
; /* Error from service creation */
1452 char webif
[1024]; /* Web interface share name */
1453 #ifdef HAVE_COREFOUNDATION_H
1454 CFStringRef nameRef
; /* Computer name CFString */
1455 char nameBuffer
[1024]; /* C-string buffer */
1456 CFStringEncoding nameEncoding
; /* Computer name encoding */
1457 #endif /* HAVE_COREFOUNDATION_H */
1461 * Only share the web interface and printers when non-local listening is
1469 * Get the computer name as a c-string...
1472 #ifdef HAVE_COREFOUNDATION_H
1473 cupsdClearString(&DNSSDName
);
1475 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1476 &nameEncoding
)) != NULL
)
1478 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1479 kCFStringEncodingUTF8
))
1480 cupsdSetString(&DNSSDName
, nameBuffer
);
1486 cupsdSetString(&DNSSDName
, ServerName
);
1487 #endif /* HAVE_COREFOUNDATION_H */
1490 * Then (re)register the web interface...
1494 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1496 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1499 DNSServiceRefDeallocate(WebIFRef
);
1501 WebIFRef
= DNSSDRef
;
1502 if ((error
= DNSServiceRegister(&WebIFRef
,
1503 kDNSServiceFlagsShareConnection
,
1504 0, webif
, "_http._tcp", NULL
,
1505 NULL
, htons(DNSSDPort
), 7,
1506 "\006path=/", dnssdRegisterCallback
,
1507 NULL
)) != kDNSServiceErr_NoError
)
1508 cupsdLogMessage(CUPSD_LOG_ERROR
,
1509 "DNS-SD web interface registration failed: %d", error
);
1511 #endif /* HAVE_DNSSD */
1514 #ifdef HAVE_OPENLDAP
1516 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1520 cupsdUpdateLDAPBrowse(void)
1522 char uri
[HTTP_MAX_URI
], /* Printer URI */
1523 host
[HTTP_MAX_URI
], /* Hostname */
1524 resource
[HTTP_MAX_URI
], /* Resource path */
1525 location
[1024], /* Printer location */
1526 info
[1024], /* Printer information */
1527 make_model
[1024], /* Printer make and model */
1528 **value
; /* Holds the returned data from LDAP */
1529 int type
; /* Printer type */
1530 int rc
; /* LDAP status */
1531 int limit
; /* Size limit */
1532 LDAPMessage
*res
, /* LDAP search results */
1533 *e
; /* Current entry from search */
1537 * Search for printers...
1540 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1542 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1544 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1545 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1546 if (rc
!= LDAP_SUCCESS
)
1548 cupsdLogMessage(CUPSD_LOG_ERROR
,
1549 "LDAP search returned error %d: %s", rc
,
1550 ldap_err2string(rc
));
1554 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1555 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1560 * Loop through the available printers...
1563 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1565 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1568 * Get the required values from this entry...
1571 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1572 "printerDescription")) == NULL
)
1575 strlcpy(info
, *value
, sizeof(info
));
1576 ldap_value_free(value
);
1578 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1579 "printerLocation")) == NULL
)
1582 strlcpy(location
, *value
, sizeof(location
));
1583 ldap_value_free(value
);
1585 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1586 "printerMakeAndModel")) == NULL
)
1589 strlcpy(make_model
, *value
, sizeof(make_model
));
1590 ldap_value_free(value
);
1592 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1593 "printerType")) == NULL
)
1596 type
= atoi(*value
);
1597 ldap_value_free(value
);
1599 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1600 "printerURI")) == NULL
)
1603 strlcpy(uri
, *value
, sizeof(uri
));
1604 ldap_value_free(value
);
1607 * Process the entry as browse data...
1610 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1611 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1612 location
, info
, make_model
, 0, NULL
);
1616 #endif /* HAVE_OPENLDAP */
1621 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1625 cupsdUpdateSLPBrowse(void)
1627 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1628 *next
; /* Next service in list */
1629 cupsd_printer_t p
; /* Printer information */
1630 const char *uri
; /* Pointer to printer URI */
1631 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1632 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1636 * Reset the refresh time...
1639 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1642 * Poll for remote printers using SLP...
1647 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1648 slp_url_callback
, &s
);
1651 * Loop through the list of available printers...
1657 * Save the "next" pointer...
1663 * Load a cupsd_printer_t structure with the SLP service attributes...
1666 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1669 * Process this printer entry...
1672 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1674 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1677 * Pull the URI apart to see if this is a local or remote printer...
1680 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1681 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1682 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1686 * Free this listing...
1689 cupsdClearString(&p
.info
);
1690 cupsdClearString(&p
.location
);
1691 cupsdClearString(&p
.make_model
);
1696 #endif /* HAVE_LIBSLP */
1700 * 'dequote()' - Remote quotes from a string.
1703 static char * /* O - Dequoted string */
1704 dequote(char *d
, /* I - Destination string */
1705 const char *s
, /* I - Source string */
1706 int dlen
) /* I - Destination length */
1708 char *dptr
; /* Pointer into destination */
1713 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1731 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
1734 static char * /* O - TXT record */
1735 dnssdBuildTxtRecord(
1736 int *txt_len
, /* O - TXT record length */
1737 cupsd_printer_t
*p
, /* I - Printer information */
1738 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
1740 int i
, j
; /* Looping vars */
1741 char type_str
[32], /* Type to string buffer */
1742 state_str
[32], /* State to string buffer */
1743 rp_str
[1024], /* Queue name string buffer */
1744 air_str
[1024], /* auth-info-required string buffer */
1745 *keyvalue
[32][2]; /* Table of key/value pairs */
1749 * Load up the key value pairs...
1754 keyvalue
[i
][0] = "txtvers";
1755 keyvalue
[i
++][1] = "1";
1757 keyvalue
[i
][0] = "qtotal";
1758 keyvalue
[i
++][1] = "1";
1760 keyvalue
[i
][0] = "rp";
1761 keyvalue
[i
++][1] = rp_str
;
1763 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
1765 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
1766 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
1768 keyvalue
[i
][0] = "ty";
1769 keyvalue
[i
++][1] = p
->make_model
;
1771 if (p
->location
&& *p
->location
!= '\0')
1773 keyvalue
[i
][0] = "note";
1774 keyvalue
[i
++][1] = p
->location
;
1777 keyvalue
[i
][0] = "priority";
1778 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
1780 keyvalue
[i
][0] = "product";
1781 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
1783 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
1784 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
1786 keyvalue
[i
][0] = "printer-state";
1787 keyvalue
[i
++][1] = state_str
;
1789 keyvalue
[i
][0] = "printer-type";
1790 keyvalue
[i
++][1] = type_str
;
1792 keyvalue
[i
][0] = "Transparent";
1793 keyvalue
[i
++][1] = "T";
1795 keyvalue
[i
][0] = "Binary";
1796 keyvalue
[i
++][1] = "T";
1798 if ((p
->type
& CUPS_PRINTER_FAX
))
1800 keyvalue
[i
][0] = "Fax";
1801 keyvalue
[i
++][1] = "T";
1804 if ((p
->type
& CUPS_PRINTER_COLOR
))
1806 keyvalue
[i
][0] = "Color";
1807 keyvalue
[i
++][1] = "T";
1810 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
1812 keyvalue
[i
][0] = "Duplex";
1813 keyvalue
[i
++][1] = "T";
1816 if ((p
->type
& CUPS_PRINTER_STAPLE
))
1818 keyvalue
[i
][0] = "Staple";
1819 keyvalue
[i
++][1] = "T";
1822 if ((p
->type
& CUPS_PRINTER_COPIES
))
1824 keyvalue
[i
][0] = "Copies";
1825 keyvalue
[i
++][1] = "T";
1828 if ((p
->type
& CUPS_PRINTER_COLLATE
))
1830 keyvalue
[i
][0] = "Collate";
1831 keyvalue
[i
++][1] = "T";
1834 if ((p
->type
& CUPS_PRINTER_PUNCH
))
1836 keyvalue
[i
][0] = "Punch";
1837 keyvalue
[i
++][1] = "T";
1840 if ((p
->type
& CUPS_PRINTER_BIND
))
1842 keyvalue
[i
][0] = "Bind";
1843 keyvalue
[i
++][1] = "T";
1846 if ((p
->type
& CUPS_PRINTER_SORT
))
1848 keyvalue
[i
][0] = "Sort";
1849 keyvalue
[i
++][1] = "T";
1852 keyvalue
[i
][0] = "pdl";
1853 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
1855 if (p
->num_auth_info_required
)
1857 char *air
= air_str
; /* Pointer into string */
1860 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
1862 if (air
>= (air_str
+ sizeof(air_str
) - 2))
1868 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
1872 keyvalue
[i
][0] = "air";
1873 keyvalue
[i
++][1] = air
;
1877 * Then pack them into a proper txt record...
1880 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
1885 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
1888 static int /* O - Result of comparison */
1889 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
1890 cupsd_printer_t
*b
)/* I - Second printer */
1892 return (strcasecmp(a
->reg_name
, b
->reg_name
));
1897 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
1902 dnssdDeregisterPrinter(
1903 cupsd_printer_t
*p
) /* I - Printer */
1905 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
1908 * Closing the socket deregisters the service
1913 DNSServiceRefDeallocate(p
->ipp_ref
);
1917 cupsArrayRemove(DNSSDPrinters
, p
);
1918 cupsdClearString(&p
->reg_name
);
1923 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
1933 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
1934 * TXT record format.
1937 static char * /* O - TXT record */
1938 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
1939 char *keyvalue
[][2], /* I - Table of key value pairs */
1940 int count
) /* I - Items in table */
1942 int i
; /* Looping var */
1943 int length
; /* Length of TXT record */
1944 int length2
; /* Length of value */
1945 char *txtRecord
; /* TXT record buffer */
1946 char *cursor
; /* Looping pointer */
1950 * Calculate the buffer size
1953 for (length
= i
= 0; i
< count
; i
++)
1954 length
+= 1 + strlen(keyvalue
[i
][0]) +
1955 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
1958 * Allocate and fill it
1961 txtRecord
= malloc(length
);
1966 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
1969 * Drop in the p-string style length byte followed by the data
1972 length
= strlen(keyvalue
[i
][0]);
1973 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
1975 *cursor
++ = (unsigned char)(length
+ length2
);
1977 memcpy(cursor
, keyvalue
[i
][0], length
);
1984 memcpy(cursor
, keyvalue
[i
][1], length2
);
1995 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
1999 dnssdRegisterCallback(
2000 DNSServiceRef sdRef
, /* I - DNS Service reference */
2001 DNSServiceFlags flags
, /* I - Reserved for future use */
2002 DNSServiceErrorType errorCode
, /* I - Error code */
2003 const char *name
, /* I - Service name */
2004 const char *regtype
, /* I - Service type */
2005 const char *domain
, /* I - Domain. ".local" for now */
2006 void *context
) /* I - User-defined context */
2008 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2009 /* Current printer */
2012 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2013 name
, regtype
, p
? p
->name
: "Web Interface");
2017 cupsdLogMessage(CUPSD_LOG_ERROR
,
2018 "DNSServiceRegister failed with error %d", (int)errorCode
);
2021 else if (p
&& strcasecmp(name
, p
->reg_name
))
2023 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2026 cupsArrayRemove(DNSSDPrinters
, p
);
2027 cupsdSetString(&p
->reg_name
, name
);
2028 cupsArrayAdd(DNSSDPrinters
, p
);
2030 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2036 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2037 * or update the broadcast contents.
2041 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2043 DNSServiceErrorType se
; /* dnssd errors */
2044 char *ipp_txt
, /* IPP TXT record buffer */
2045 *printer_txt
, /* LPD TXT record buffer */
2046 name
[1024], /* Service name */
2047 *nameptr
; /* Pointer into name */
2048 int ipp_len
, /* IPP TXT record length */
2049 printer_len
; /* LPD TXT record length */
2050 char resource
[1024]; /* Resource path for printer */
2051 const char *regtype
; /* Registration type */
2052 const char *domain
; /* Registration domain */
2053 cupsd_location_t
*location
, /* Printer location */
2054 *policy
; /* Operation policy for Print-Job */
2055 unsigned address
[4]; /* INADDR_ANY address */
2058 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2059 !p
->ipp_ref
? "new" : "update");
2062 * If per-printer sharing was just disabled make sure we're not
2063 * registered before returning.
2068 dnssdDeregisterPrinter(p
);
2073 * The registered name takes the form of "<printer-info> @ <computer name>"...
2076 if (p
->info
&& strlen(p
->info
) > 0)
2079 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2081 strlcpy(name
, p
->info
, sizeof(name
));
2084 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2086 strlcpy(name
, p
->name
, sizeof(name
));
2089 * If an existing printer was renamed, unregister it and start over...
2092 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2093 dnssdDeregisterPrinter(p
);
2097 cupsdSetString(&p
->reg_name
, name
);
2098 cupsArrayAdd(DNSSDPrinters
, p
);
2102 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2103 * let dnssd decide on the domain, otherwise restrict it to ".local".
2106 if (p
->type
& CUPS_PRINTER_CLASS
)
2107 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2109 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2111 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2112 location
= cupsdFindBest(resource
, HTTP_POST
);
2113 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2115 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2116 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2122 * Register IPP and (optionally) LPD...
2125 ipp_len
= 0; /* anti-compiler-warning-code */
2126 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2131 * Initial registration. Use the _fax subtype for fax queues...
2134 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2137 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2138 "Registering DNS-SD printer %s with name \"%s\", "
2139 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2140 domain
? domain
: "(null)");
2143 * Register the queue, dropping characters as needed until we succeed...
2146 nameptr
= name
+ strlen(name
);
2150 p
->ipp_ref
= DNSSDRef
;
2151 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2152 0, name
, regtype
, domain
, NULL
,
2153 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2154 dnssdRegisterCallback
,
2155 p
)) == kDNSServiceErr_BadParam
)
2158 * Name is too long, drop trailing characters, taking into account
2164 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2171 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2173 if (se
== kDNSServiceErr_NoError
)
2175 p
->ipp_txt
= ipp_txt
;
2176 p
->ipp_len
= ipp_len
;
2180 cupsdLogMessage(CUPSD_LOG_WARN
,
2181 "DNS-SD IPP registration of \"%s\" failed: %d",
2184 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2187 * Update the existing registration...
2190 /* A TTL of 0 means use record's original value (Radar 3176248) */
2191 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2196 p
->ipp_txt
= ipp_txt
;
2197 p
->ipp_len
= ipp_len
;
2204 if (BrowseLocalProtocols
& BROWSE_LPD
)
2206 printer_len
= 0; /* anti-compiler-warning-code */
2207 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2209 if (!p
->printer_ref
)
2212 * Initial registration...
2215 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2216 "Registering DNS-SD printer %s with name \"%s\", "
2217 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2218 name
, domain
? domain
: "(null)");
2220 p
->printer_ref
= DNSSDRef
;
2221 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2222 kDNSServiceFlagsShareConnection
,
2223 0, name
, "_printer._tcp", domain
, NULL
,
2224 htons(515), printer_len
, printer_txt
,
2225 dnssdRegisterCallback
,
2226 p
)) == kDNSServiceErr_NoError
)
2228 p
->printer_txt
= printer_txt
;
2229 p
->printer_len
= printer_len
;
2233 cupsdLogMessage(CUPSD_LOG_WARN
,
2234 "DNS-SD LPD registration of \"%s\" failed: %d",
2237 else if (printer_len
!= p
->printer_len
||
2238 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2241 * Update the existing registration...
2244 /* A TTL of 0 means use record's original value (Radar 3176248) */
2245 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2249 free(p
->printer_txt
);
2251 p
->printer_txt
= printer_txt
;
2252 p
->printer_len
= printer_len
;
2260 #endif /* HAVE_DNSSD */
2265 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2268 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2269 get_hostconfig(const char *name
) /* I - Name of service */
2271 cups_file_t
*fp
; /* Hostconfig file */
2272 char line
[1024], /* Line from file */
2273 *ptr
; /* Pointer to value */
2274 int state
= 1; /* State of service */
2278 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2279 * the service is enabled/auto.
2282 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2285 * Read lines from the file until we find the service...
2288 while (cupsFileGets(fp
, line
, sizeof(line
)))
2290 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2295 if (!strcasecmp(line
, name
))
2298 * Found the service, see if it is set to "-NO-"...
2301 if (!strncasecmp(ptr
, "-NO-", 4))
2312 #endif /* __APPLE__ */
2316 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2319 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2320 is_local_queue(const char *uri
, /* I - Printer URI */
2321 char *host
, /* O - Host string */
2322 int hostlen
, /* I - Length of host buffer */
2323 char *resource
, /* O - Resource string */
2324 int resourcelen
) /* I - Length of resource buffer */
2326 char scheme
[32], /* Scheme portion of URI */
2327 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2328 int port
; /* Port portion of URI */
2329 cupsd_netif_t
*iface
; /* Network interface */
2333 * Pull the URI apart to see if this is a local or remote printer...
2336 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2337 username
, sizeof(username
), host
, hostlen
, &port
,
2338 resource
, resourcelen
) < HTTP_URI_OK
)
2341 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2344 * Check for local server addresses...
2347 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2352 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2354 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2355 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2359 * If we get here, the printer is remote...
2367 * 'process_browse_data()' - Process new browse data.
2371 process_browse_data(
2372 const char *uri
, /* I - URI of printer/class */
2373 const char *host
, /* I - Hostname */
2374 const char *resource
, /* I - Resource path */
2375 cups_ptype_t type
, /* I - Printer type */
2376 ipp_pstate_t state
, /* I - Printer state */
2377 const char *location
, /* I - Printer location */
2378 const char *info
, /* I - Printer information */
2379 const char *make_model
, /* I - Printer make and model */
2380 int num_attrs
, /* I - Number of attributes */
2381 cups_option_t
*attrs
) /* I - Attributes */
2383 int i
; /* Looping var */
2384 int update
; /* Update printer attributes? */
2385 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2386 name
[IPP_MAX_NAME
], /* Name of printer */
2387 newname
[IPP_MAX_NAME
], /* New name of printer */
2388 *hptr
, /* Pointer into hostname */
2389 *sptr
; /* Pointer into ServerName */
2390 char local_make_model
[IPP_MAX_NAME
];
2391 /* Local make and model */
2392 cupsd_printer_t
*p
; /* Printer information */
2393 const char *ipp_options
, /* ipp-options value */
2394 *lease_duration
; /* lease-duration value */
2398 * Determine if the URI contains any illegal characters in it...
2401 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2402 (strncmp(resource
, "/printers/", 10) &&
2403 strncmp(resource
, "/classes/", 9)))
2405 cupsdLogMessage(CUPSD_LOG_ERROR
,
2406 "process_browse_data: Bad printer URI in browse data: %s",
2411 if (strchr(resource
, '?') ||
2412 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2413 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2415 cupsdLogMessage(CUPSD_LOG_ERROR
,
2416 "process_browse_data: Bad resource in browse data: %s",
2422 * OK, this isn't a local printer; add any remote options...
2425 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2427 if (BrowseRemoteOptions
)
2429 if (BrowseRemoteOptions
[0] == '?')
2432 * Override server-supplied options...
2435 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2437 else if (ipp_options
)
2440 * Combine the server and local options...
2443 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2444 BrowseRemoteOptions
);
2449 * Just use the local options...
2452 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2457 else if (ipp_options
)
2460 * Just use the server-supplied options...
2463 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2468 * See if we already have it listed in the Printers list, and add it if not...
2471 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2472 type
&= ~CUPS_PRINTER_IMPLICIT
;
2474 hptr
= strchr(host
, '.');
2475 sptr
= strchr(ServerName
, '.');
2477 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2480 * Strip the common domain name components...
2483 while (hptr
!= NULL
)
2485 if (!strcasecmp(hptr
, sptr
))
2491 hptr
= strchr(hptr
+ 1, '.');
2495 if (type
& CUPS_PRINTER_CLASS
)
2498 * Remote destination is a class...
2501 if (!strncmp(resource
, "/classes/", 9))
2502 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2507 *hptr
= '.'; /* Resource FQDN */
2509 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2511 if ((p
= cupsdFindDest(resource
+ 9)) != NULL
)
2513 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2516 * Nope, this isn't the same host; if the hostname isn't the local host,
2517 * add it to the other class and then find a class using the full host
2521 if (p
->type
& CUPS_PRINTER_REMOTE
)
2523 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2524 "Renamed remote class \"%s\" to \"%s@%s\"...",
2525 p
->name
, p
->name
, p
->hostname
);
2526 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2527 "Class \'%s\' deleted by directory services.",
2530 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2531 cupsdRenamePrinter(p
, newname
);
2533 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2534 "Class \'%s\' added by directory services.",
2540 else if (!p
->hostname
)
2543 * Hostname not set, so this must be a cached remote printer
2544 * that was created for a pending print job...
2547 cupsdSetString(&p
->hostname
, host
);
2548 cupsdSetString(&p
->uri
, uri
);
2549 cupsdSetString(&p
->device_uri
, uri
);
2556 * Use the short name for this shared class.
2559 strlcpy(name
, resource
+ 9, sizeof(name
));
2562 else if (p
&& !p
->hostname
)
2565 * Hostname not set, so this must be a cached remote printer
2566 * that was created for a pending print job...
2569 cupsdSetString(&p
->hostname
, host
);
2570 cupsdSetString(&p
->uri
, uri
);
2571 cupsdSetString(&p
->device_uri
, uri
);
2578 * Class doesn't exist; add it...
2581 p
= cupsdAddClass(name
);
2583 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2585 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2586 "Class \'%s\' added by directory services.", name
);
2589 * Force the URI to point to the real server...
2592 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2594 cupsdSetString(&p
->uri
, uri
);
2595 cupsdSetString(&p
->device_uri
, uri
);
2596 cupsdSetString(&p
->hostname
, host
);
2600 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2606 * Remote destination is a printer...
2609 if (!strncmp(resource
, "/printers/", 10))
2610 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2615 *hptr
= '.'; /* Resource FQDN */
2617 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2619 if ((p
= cupsdFindDest(resource
+ 10)) != NULL
)
2621 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2624 * Nope, this isn't the same host; if the hostname isn't the local host,
2625 * add it to the other printer and then find a printer using the full host
2629 if (p
->type
& CUPS_PRINTER_REMOTE
)
2631 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2632 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2633 p
->name
, p
->name
, p
->hostname
);
2634 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2635 "Printer \'%s\' deleted by directory services.",
2638 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2639 cupsdRenamePrinter(p
, newname
);
2641 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2642 "Printer \'%s\' added by directory services.",
2648 else if (!p
->hostname
)
2651 * Hostname not set, so this must be a cached remote printer
2652 * that was created for a pending print job...
2655 cupsdSetString(&p
->hostname
, host
);
2656 cupsdSetString(&p
->uri
, uri
);
2657 cupsdSetString(&p
->device_uri
, uri
);
2664 * Use the short name for this shared printer.
2667 strlcpy(name
, resource
+ 10, sizeof(name
));
2670 else if (p
&& !p
->hostname
)
2673 * Hostname not set, so this must be a cached remote printer
2674 * that was created for a pending print job...
2677 cupsdSetString(&p
->hostname
, host
);
2678 cupsdSetString(&p
->uri
, uri
);
2679 cupsdSetString(&p
->device_uri
, uri
);
2686 * Printer doesn't exist; add it...
2689 p
= cupsdAddPrinter(name
);
2691 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2692 "Printer \'%s\' added by directory services.", name
);
2694 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2697 * Force the URI to point to the real server...
2700 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2702 cupsdSetString(&p
->hostname
, host
);
2703 cupsdSetString(&p
->uri
, uri
);
2704 cupsdSetString(&p
->device_uri
, uri
);
2708 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2713 * Update the state...
2717 p
->browse_time
= time(NULL
);
2719 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2723 * Grab the lease-duration for the browse data; anything less then 1
2724 * second or more than 1 week gets the default BrowseTimeout...
2727 i
= atoi(lease_duration
);
2728 if (i
< 1 || i
> 604800)
2731 p
->browse_expire
= p
->browse_time
+ i
;
2734 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2736 if (type
& CUPS_PRINTER_REJECTING
)
2738 type
&= ~CUPS_PRINTER_REJECTING
;
2746 else if (!p
->accepting
)
2752 if (p
->type
!= type
)
2758 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2760 cupsdSetString(&p
->location
, location
);
2764 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2766 cupsdSetString(&p
->info
, info
);
2769 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2772 if (!make_model
|| !make_model
[0])
2774 if (type
& CUPS_PRINTER_CLASS
)
2775 snprintf(local_make_model
, sizeof(local_make_model
),
2776 "Remote Class on %s", host
);
2778 snprintf(local_make_model
, sizeof(local_make_model
),
2779 "Remote Printer on %s", host
);
2782 snprintf(local_make_model
, sizeof(local_make_model
),
2783 "%s on %s", make_model
, host
);
2785 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2787 cupsdSetString(&p
->make_model
, local_make_model
);
2793 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2796 * See if we need to update the attributes...
2799 if (p
->num_options
!= num_attrs
)
2803 for (i
= 0; i
< num_attrs
; i
++)
2804 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2805 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2806 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2815 * Free the old options...
2818 cupsFreeOptions(p
->num_options
, p
->options
);
2821 p
->num_options
= num_attrs
;
2824 if (type
& CUPS_PRINTER_DELETE
)
2826 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2827 "%s \'%s\' deleted by directory services.",
2828 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2830 cupsdExpireSubscriptions(p
, NULL
);
2832 cupsdDeletePrinter(p
, 1);
2833 cupsdUpdateImplicitClasses();
2834 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2838 cupsdSetPrinterAttrs(p
);
2839 cupsdUpdateImplicitClasses();
2843 * See if we have a default printer... If not, make the first network
2844 * default printer the default.
2847 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2850 * Find the first network default printer and use it...
2853 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2855 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2856 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2859 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2865 * Do auto-classing if needed...
2868 process_implicit_classes();
2873 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2877 process_implicit_classes(void)
2879 int i
; /* Looping var */
2880 int update
; /* Update printer attributes? */
2881 char name
[IPP_MAX_NAME
], /* Name of printer */
2882 *hptr
; /* Pointer into hostname */
2883 cupsd_printer_t
*p
, /* Printer information */
2884 *pclass
, /* Printer class */
2885 *first
; /* First printer in class */
2886 int offset
, /* Offset of name */
2887 len
; /* Length of name */
2890 if (!ImplicitClasses
|| !Printers
)
2894 * Loop through all available printers and create classes as needed...
2897 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2898 update
= 0, pclass
= NULL
, first
= NULL
;
2900 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2903 * Skip implicit classes...
2906 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2913 * If len == 0, get the length of this printer name up to the "@"
2917 cupsArraySave(Printers
);
2920 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2921 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2924 * We have more than one printer with the same name; see if
2925 * we have a class, and if this printer is a member...
2928 if (pclass
&& strcasecmp(pclass
->name
, name
))
2931 cupsdSetPrinterAttrs(pclass
);
2937 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2940 * Need to add the class...
2943 pclass
= cupsdAddPrinter(name
);
2944 cupsArrayAdd(ImplicitPrinters
, pclass
);
2946 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2947 pclass
->accepting
= 1;
2948 pclass
->state
= IPP_PRINTER_IDLE
;
2950 cupsdSetString(&pclass
->location
, p
->location
);
2951 cupsdSetString(&pclass
->info
, p
->info
);
2953 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2954 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2958 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2960 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2962 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2963 "Implicit class \'%s\' added by directory services.",
2969 for (i
= 0; i
< pclass
->num_printers
; i
++)
2970 if (pclass
->printers
[i
] == first
)
2973 if (i
>= pclass
->num_printers
)
2975 first
->in_implicit_class
= 1;
2976 cupsdAddPrinterToClass(pclass
, first
);
2982 for (i
= 0; i
< pclass
->num_printers
; i
++)
2983 if (pclass
->printers
[i
] == p
)
2986 if (i
>= pclass
->num_printers
)
2988 p
->in_implicit_class
= 1;
2989 cupsdAddPrinterToClass(pclass
, p
);
2996 * First time around; just get name length and mark it as first
3000 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3001 len
= hptr
- p
->name
;
3003 len
= strlen(p
->name
);
3005 strncpy(name
, p
->name
, len
);
3009 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3010 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3013 * Can't use same name as a local printer; add "Any" to the
3014 * front of the name, unless we have explicitly disabled
3015 * the "ImplicitAnyClasses"...
3018 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3021 * Add "Any" to the class name...
3024 strcpy(name
, "Any");
3025 strncpy(name
+ 3, p
->name
, len
);
3026 name
[len
+ 3] = '\0';
3032 * Don't create an implicit class if we have a local printer
3033 * with the same name...
3037 cupsArrayRestore(Printers
);
3045 cupsArrayRestore(Printers
);
3049 * Update the last printer class as needed...
3052 if (pclass
&& update
)
3053 cupsdSetPrinterAttrs(pclass
);
3058 * 'send_cups_browse()' - Send new browsing information using the CUPS
3063 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3065 int i
; /* Looping var */
3066 cups_ptype_t type
; /* Printer type */
3067 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3068 int bytes
; /* Length of packet */
3069 char packet
[1453], /* Browse data packet */
3070 uri
[1024], /* Printer URI */
3071 location
[1024], /* printer-location */
3072 info
[1024], /* printer-info */
3074 /* printer-make-and-model */
3075 cupsd_netif_t
*iface
; /* Network interface */
3079 * Figure out the printer type value...
3082 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3085 type
|= CUPS_PRINTER_REJECTING
;
3087 if (p
== DefaultPrinter
)
3088 type
|= CUPS_PRINTER_DEFAULT
;
3091 * Remove quotes from printer-info, printer-location, and
3092 * printer-make-and-model attributes...
3095 dequote(location
, p
->location
, sizeof(location
));
3096 dequote(info
, p
->info
, sizeof(info
));
3099 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3100 else if (p
->type
& CUPS_PRINTER_CLASS
)
3102 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3103 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3105 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3108 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3110 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3113 * Send a packet to each browse address...
3116 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3120 * Send the browse packet to one or more interfaces...
3123 if (!strcmp(b
->iface
, "*"))
3126 * Send to all local interfaces...
3131 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3133 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3136 * Only send to local, IPv4 interfaces...
3139 if (!iface
->is_local
|| !iface
->port
||
3140 iface
->address
.addr
.sa_family
!= AF_INET
)
3143 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3144 iface
->hostname
, iface
->port
,
3145 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3148 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3149 type
, p
->state
, uri
, location
, info
, make_model
,
3150 p
->browse_attrs
? p
->browse_attrs
: "");
3152 bytes
= strlen(packet
);
3154 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3155 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3156 iface
->name
, packet
);
3158 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3160 sendto(BrowseSocket
, packet
, bytes
, 0,
3161 (struct sockaddr
*)&(iface
->broadcast
),
3162 httpAddrLength(&(iface
->broadcast
)));
3165 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3168 * Send to the named interface using the IPv4 address...
3172 if (strcmp(b
->iface
, iface
->name
))
3177 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3180 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3184 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3185 iface
->hostname
, iface
->port
,
3186 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3189 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3190 type
, p
->state
, uri
, location
, info
, make_model
,
3191 p
->browse_attrs
? p
->browse_attrs
: "");
3193 bytes
= strlen(packet
);
3195 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3196 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3197 iface
->name
, packet
);
3199 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3201 sendto(BrowseSocket
, packet
, bytes
, 0,
3202 (struct sockaddr
*)&(iface
->broadcast
),
3203 httpAddrLength(&(iface
->broadcast
)));
3210 * Send the browse packet to the indicated address using
3211 * the default server name...
3214 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3215 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3216 p
->browse_attrs
? p
->browse_attrs
: "");
3218 bytes
= strlen(packet
);
3219 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3220 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3222 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3223 (struct sockaddr
*)&(b
->to
),
3224 httpAddrLength(&(b
->to
))) <= 0)
3227 * Unable to send browse packet, so remove this address from the
3231 cupsdLogMessage(CUPSD_LOG_ERROR
,
3232 "cupsdSendBrowseList: sendto failed for browser "
3234 (int)(b
- Browsers
+ 1), strerror(errno
));
3237 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3246 #ifdef HAVE_OPENLDAP
3248 * 'send_ldap_browse()' - Send LDAP printer registrations.
3252 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3254 int i
; /* Looping var... */
3255 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3256 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3257 LDAPMessage
*res
; /* Search result token */
3258 char *cn_value
[2], /* Change records */
3264 typestring
[255], /* String to hold printer-type */
3265 filter
[256], /* Search filter for possible UPDATEs */
3266 dn
[1024]; /* DN of the printer we are adding */
3267 int rc
; /* LDAP status */
3268 static const char * const objectClass_values
[] =
3269 { /* The 3 objectClass's we use in */
3270 "top", /* our LDAP entries */
3276 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3279 * Everything in ldap is ** so we fudge around it...
3282 sprintf(typestring
, "%u", p
->type
);
3284 cn_value
[0] = p
->name
;
3286 info
[0] = p
->info
? p
->info
: "Unknown";
3288 location
[0] = p
->location
? p
->location
: "Unknown";
3290 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3291 make_model
[1] = NULL
;
3292 type
[0] = typestring
;
3297 snprintf(filter
, sizeof(filter
),
3298 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3300 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3301 filter
, (char **)ldap_attrs
, 0, &res
);
3302 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3305 mods
[0].mod_type
= "cn";
3306 mods
[0].mod_values
= cn_value
;
3307 mods
[1].mod_type
= "printerDescription";
3308 mods
[1].mod_values
= info
;
3309 mods
[2].mod_type
= "printerURI";
3310 mods
[2].mod_values
= uri
;
3311 mods
[3].mod_type
= "printerLocation";
3312 mods
[3].mod_values
= location
;
3313 mods
[4].mod_type
= "printerMakeAndModel";
3314 mods
[4].mod_values
= make_model
;
3315 mods
[5].mod_type
= "printerType";
3316 mods
[5].mod_values
= type
;
3317 mods
[6].mod_type
= "objectClass";
3318 mods
[6].mod_values
= (char **)objectClass_values
;
3320 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3321 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3323 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3326 * Printer has already been registered, modify the current
3330 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3331 "send_ldap_browse: Replacing entry...");
3333 for (i
= 0; i
< 7; i
++)
3335 pmods
[i
] = mods
+ i
;
3336 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3340 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3341 cupsdLogMessage(CUPSD_LOG_ERROR
,
3342 "LDAP modify for %s failed with status %d: %s",
3343 p
->name
, rc
, ldap_err2string(rc
));
3348 * Printer has never been registered, add the current
3352 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3353 "send_ldap_browse: Adding entry...");
3355 for (i
= 0; i
< 7; i
++)
3357 pmods
[i
] = mods
+ i
;
3358 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3362 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3363 cupsdLogMessage(CUPSD_LOG_ERROR
,
3364 "LDAP add for %s failed with status %d: %s",
3365 p
->name
, rc
, ldap_err2string(rc
));
3368 #endif /* HAVE_OPENLDAP */
3373 * 'send_slp_browse()' - Register the specified printer with SLP.
3377 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3379 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3380 attrs
[8192], /* Printer attributes */
3381 finishings
[1024], /* Finishings to support */
3382 make_model
[IPP_MAX_NAME
* 2],
3383 /* Make and model, quoted */
3384 location
[IPP_MAX_NAME
* 2],
3385 /* Location, quoted */
3386 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3387 *src
, /* Pointer to original string */
3388 *dst
; /* Pointer to destination string */
3389 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3390 SLPError error
; /* SLP error, if any */
3393 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3397 * Make the SLP service URL that conforms to the IANA
3398 * 'printer:' template.
3401 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3403 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3406 * Figure out the finishings string...
3409 if (p
->type
& CUPS_PRINTER_STAPLE
)
3410 strcpy(finishings
, "staple");
3412 finishings
[0] = '\0';
3414 if (p
->type
& CUPS_PRINTER_BIND
)
3417 strlcat(finishings
, ",bind", sizeof(finishings
));
3419 strcpy(finishings
, "bind");
3422 if (p
->type
& CUPS_PRINTER_PUNCH
)
3425 strlcat(finishings
, ",punch", sizeof(finishings
));
3427 strcpy(finishings
, "punch");
3430 if (p
->type
& CUPS_PRINTER_COVER
)
3433 strlcat(finishings
, ",cover", sizeof(finishings
));
3435 strcpy(finishings
, "cover");
3438 if (p
->type
& CUPS_PRINTER_SORT
)
3441 strlcat(finishings
, ",sort", sizeof(finishings
));
3443 strcpy(finishings
, "sort");
3447 strcpy(finishings
, "none");
3450 * Quote any commas in the make and model, location, and info strings...
3453 for (src
= p
->make_model
, dst
= make_model
;
3454 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3456 if (*src
== ',' || *src
== '\\' || *src
== ')')
3465 strcpy(make_model
, "Unknown");
3467 for (src
= p
->location
, dst
= location
;
3468 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3470 if (*src
== ',' || *src
== '\\' || *src
== ')')
3479 strcpy(location
, "Unknown");
3481 for (src
= p
->info
, dst
= info
;
3482 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3484 if (*src
== ',' || *src
== '\\' || *src
== ')')
3493 strcpy(info
, "Unknown");
3496 * Get the authentication value...
3499 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3503 * Make the SLP attribute string list that conforms to
3504 * the IANA 'printer:' template.
3507 snprintf(attrs
, sizeof(attrs
),
3508 "(printer-uri-supported=%s),"
3509 "(uri-authentication-supported=%s>),"
3511 "(uri-security-supported=tls>),"
3513 "(uri-security-supported=none>),"
3514 #endif /* HAVE_SSL */
3515 "(printer-name=%s),"
3516 "(printer-location=%s),"
3517 "(printer-info=%s),"
3518 "(printer-more-info=%s),"
3519 "(printer-make-and-model=%s),"
3520 "(printer-type=%d),"
3521 "(charset-supported=utf-8),"
3522 "(natural-language-configured=%s),"
3523 "(natural-language-supported=de,en,es,fr,it),"
3524 "(color-supported=%s),"
3525 "(finishings-supported=%s),"
3526 "(sides-supported=one-sided%s),"
3527 "(multiple-document-jobs-supported=true)"
3528 "(ipp-versions-supported=1.0,1.1)",
3529 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3530 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3531 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3533 p
->type
& CUPS_PRINTER_DUPLEX
?
3534 ",two-sided-long-edge,two-sided-short-edge" : "");
3536 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3539 * Register the printer with the SLP server...
3542 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3543 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3545 if (error
!= SLP_OK
)
3546 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3552 * 'slp_attr_callback()' - SLP attribute callback
3555 static SLPBoolean
/* O - SLP_TRUE for success */
3557 SLPHandle hslp
, /* I - SLP handle */
3558 const char *attrlist
, /* I - Attribute list */
3559 SLPError errcode
, /* I - Parsing status for this attr */
3560 void *cookie
) /* I - Current printer */
3562 char *tmp
= 0; /* Temporary string */
3563 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3564 /* Current printer */
3567 (void)hslp
; /* anti-compiler-warning-code */
3570 * Bail if there was an error
3573 if (errcode
!= SLP_OK
)
3577 * Parse the attrlist to obtain things needed to build CUPS browse packet
3580 memset(p
, 0, sizeof(cupsd_printer_t
));
3582 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3584 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3586 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3588 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3589 p
->type
= atoi(tmp
);
3591 p
->type
= CUPS_PRINTER_REMOTE
;
3593 cupsdClearString(&tmp
);
3600 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3604 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3606 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3609 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3611 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3614 * Make the SLP service URL that conforms to the IANA
3615 * 'printer:' template.
3618 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3621 * Deregister the printer...
3624 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3630 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3633 static int /* O - 0 on success */
3634 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3635 const char *tag
, /* I - Name of attribute */
3636 char **valbuf
) /* O - Value */
3638 char *ptr1
, /* Pointer into string */
3642 cupsdClearString(valbuf
);
3644 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3646 ptr1
+= strlen(tag
);
3648 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3654 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3655 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3658 * Dequote the value...
3661 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3662 if (*ptr1
== '\\' && ptr1
[1])
3663 _cups_strcpy(ptr1
, ptr1
+ 1);
3674 * 'slp_reg_callback()' - Empty SLPRegReport.
3678 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3679 SLPError errcode
, /* I - Error code, if any */
3680 void *cookie
) /* I - App data */
3691 * 'slp_url_callback()' - SLP service url callback
3694 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3696 SLPHandle hslp
, /* I - SLP handle */
3697 const char *srvurl
, /* I - URL of service */
3698 unsigned short lifetime
, /* I - Life of service */
3699 SLPError errcode
, /* I - Existing error code */
3700 void *cookie
) /* I - Pointer to service list */
3702 slpsrvurl_t
*s
, /* New service entry */
3703 **head
; /* Pointer to head of entry */
3707 * Let the compiler know we won't be using these vars...
3714 * Bail if there was an error
3717 if (errcode
!= SLP_OK
)
3721 * Grab the head of the list...
3724 head
= (slpsrvurl_t
**)cookie
;
3727 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3730 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3734 * Copy the SLP service URL...
3737 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3740 * Link the SLP service URL into the head of the list
3750 #endif /* HAVE_LIBSLP */
3754 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3758 update_cups_browse(void)
3760 int i
; /* Looping var */
3761 int auth
; /* Authorization status */
3762 int len
; /* Length of name string */
3763 int bytes
; /* Number of bytes left */
3764 char packet
[1541], /* Broadcast packet */
3765 *pptr
; /* Pointer into packet */
3766 socklen_t srclen
; /* Length of source address */
3767 http_addr_t srcaddr
; /* Source address */
3768 char srcname
[1024]; /* Source hostname */
3769 unsigned address
[4]; /* Source address */
3770 unsigned type
; /* Printer type */
3771 unsigned state
; /* Printer state */
3772 char uri
[HTTP_MAX_URI
], /* Printer URI */
3773 host
[HTTP_MAX_URI
], /* Host portion of URI */
3774 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3775 info
[IPP_MAX_NAME
], /* Information string */
3776 location
[IPP_MAX_NAME
], /* Location string */
3777 make_model
[IPP_MAX_NAME
];/* Make and model string */
3778 int num_attrs
; /* Number of attributes */
3779 cups_option_t
*attrs
; /* Attributes */
3783 * Read a packet from the browse socket...
3786 srclen
= sizeof(srcaddr
);
3787 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3788 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3791 * "Connection refused" is returned under Linux if the destination port
3792 * or address is unreachable from a previous sendto(); check for the
3793 * error here and ignore it for now...
3796 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3798 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3800 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
3803 closesocket(BrowseSocket
);
3805 close(BrowseSocket
);
3808 cupsdRemoveSelect(BrowseSocket
);
3811 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
3812 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
3818 packet
[bytes
] = '\0';
3821 * If we're about to sleep, ignore incoming browse packets.
3828 * Figure out where it came from...
3832 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3834 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3835 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3836 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3837 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3840 #endif /* AF_INET6 */
3845 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3848 if (HostNameLookups
)
3849 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3851 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3853 len
= strlen(srcname
);
3861 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3864 * Access from localhost (127.0.0.1) is always allowed...
3867 auth
= CUPSD_AUTH_ALLOW
;
3872 * Do authorization checks on the domain/address...
3875 switch (BrowseACL
->order_type
)
3878 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
3881 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
3882 auth
= CUPSD_AUTH_ALLOW
;
3884 if (cupsdCheckAuth(address
, srcname
, len
,
3885 BrowseACL
->num_deny
, BrowseACL
->deny
))
3886 auth
= CUPSD_AUTH_DENY
;
3888 if (cupsdCheckAuth(address
, srcname
, len
,
3889 BrowseACL
->num_allow
, BrowseACL
->allow
))
3890 auth
= CUPSD_AUTH_ALLOW
;
3893 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
3894 auth
= CUPSD_AUTH_DENY
;
3896 if (cupsdCheckAuth(address
, srcname
, len
,
3897 BrowseACL
->num_allow
, BrowseACL
->allow
))
3898 auth
= CUPSD_AUTH_ALLOW
;
3900 if (cupsdCheckAuth(address
, srcname
, len
,
3901 BrowseACL
->num_deny
, BrowseACL
->deny
))
3902 auth
= CUPSD_AUTH_DENY
;
3908 auth
= CUPSD_AUTH_ALLOW
;
3910 if (auth
== CUPSD_AUTH_DENY
)
3912 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3913 "update_cups_browse: Refused %d bytes from %s", bytes
,
3918 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3919 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3926 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3928 cupsdLogMessage(CUPSD_LOG_WARN
,
3929 "update_cups_browse: Garbled browse packet - %s", packet
);
3933 strcpy(location
, "Location Unknown");
3934 strcpy(info
, "No Information Available");
3935 make_model
[0] = '\0';
3939 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3942 * Have extended information; can't use sscanf for it because not all
3943 * sscanf's allow empty strings with %[^\"]...
3946 for (i
= 0, pptr
++;
3947 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3949 location
[i
] = *pptr
;
3957 while (*pptr
&& isspace(*pptr
& 255))
3962 for (i
= 0, pptr
++;
3963 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3972 while (*pptr
&& isspace(*pptr
& 255))
3977 for (i
= 0, pptr
++;
3978 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3980 make_model
[i
] = *pptr
;
3985 make_model
[i
] = '\0';
3988 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3994 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3995 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3996 type
, state
, uri
, location
, info
, make_model
));
3999 * Pull the URI apart to see if this is a local or remote printer...
4002 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
4004 cupsFreeOptions(num_attrs
, attrs
);
4012 for (i
= 0; i
< NumRelays
; i
++)
4013 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
4014 if (sendto(BrowseSocket
, packet
, bytes
, 0,
4015 (struct sockaddr
*)&(Relays
[i
].to
),
4016 httpAddrLength(&(Relays
[i
].to
))) <= 0)
4018 cupsdLogMessage(CUPSD_LOG_ERROR
,
4019 "update_cups_browse: sendto failed for relay %d - %s.",
4020 i
+ 1, strerror(errno
));
4021 cupsFreeOptions(num_attrs
, attrs
);
4026 * Process the browse data...
4029 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
4030 (ipp_pstate_t
)state
, location
, info
, make_model
,
4036 * 'update_lpd()' - Update the LPD configuration as needed.
4040 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
4047 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
4048 * setting for backwards-compatibility.
4051 if (onoff
&& !get_hostconfig("CUPS_LPD"))
4053 #endif /* __APPLE__ */
4055 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
4058 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
4061 char newfile
[1024]; /* New cups-lpd.N file */
4062 cups_file_t
*ofp
, /* Original file pointer */
4063 *nfp
; /* New file pointer */
4064 char line
[1024]; /* Line from file */
4067 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
4069 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
4071 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
4072 LPDConfigFile
+ 9, strerror(errno
));
4076 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4078 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4079 newfile
, strerror(errno
));
4085 * Copy all of the lines from the cups-lpd file...
4088 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4092 cupsFilePrintf(nfp
, "%s\n", line
);
4093 snprintf(line
, sizeof(line
), "\tdisable = %s",
4094 onoff
? "no" : "yes");
4096 else if (!strstr(line
, "disable ="))
4097 cupsFilePrintf(nfp
, "%s\n", line
);
4102 rename(newfile
, LPDConfigFile
+ 9);
4105 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
4108 * Enable/disable LPD via the launchctl command...
4111 char *argv
[5], /* Arguments for command */
4112 *envp
[MAX_ENV
]; /* Environment for command */
4113 int pid
; /* Process ID */
4116 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4117 argv
[0] = (char *)"launchctl";
4118 argv
[1] = (char *)(onoff
? "load" : "unload");
4119 argv
[2] = (char *)"-w";
4120 argv
[3] = LPDConfigFile
+ 10;
4123 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
4126 #endif /* __APPLE__ */
4128 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
4133 * 'update_polling()' - Read status messages from the poll daemons.
4137 update_polling(void)
4139 char *ptr
, /* Pointer to end of line in buffer */
4140 message
[1024]; /* Pointer to message text */
4141 int loglevel
; /* Log level for message */
4144 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
4145 message
, sizeof(message
))) != NULL
)
4146 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
4149 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
4152 * All polling processes have died; stop polling...
4155 cupsdLogMessage(CUPSD_LOG_ERROR
,
4156 "update_polling: all polling processes have exited!");
4163 * 'update_smb()' - Update the SMB configuration as needed.
4167 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
4172 if (!strncmp(SMBConfigFile
, "samba:///", 9))
4175 * Enable/disable SMB via the specified smb.conf config file...
4178 char newfile
[1024]; /* New smb.conf.N file */
4179 cups_file_t
*ofp
, /* Original file pointer */
4180 *nfp
; /* New file pointer */
4181 char line
[1024]; /* Line from file */
4182 int in_printers
; /* In [printers] section? */
4185 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
4187 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
4189 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
4190 SMBConfigFile
+ 8, strerror(errno
));
4194 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4196 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4197 newfile
, strerror(errno
));
4203 * Copy all of the lines from the smb.conf file...
4208 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4210 if (in_printers
&& strstr(line
, "printable ="))
4211 snprintf(line
, sizeof(line
), " printable = %s",
4212 onoff
? "yes" : "no");
4214 cupsFilePrintf(nfp
, "%s\n", line
);
4217 in_printers
= !strcmp(line
, "[printers]");
4222 rename(newfile
, SMBConfigFile
+ 8);
4225 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
4230 * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".