2 * "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z 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/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * cupsdStartBrowsing() - Start sending and receiving broadcast
28 * cupsdStartPolling() - Start polling servers as needed.
29 * cupsdStopBrowsing() - Stop sending and receiving broadcast
31 * cupsdStopPolling() - Stop polling servers as needed.
32 * cupsdUpdateDNSSDName() - Update the computer name we use for
34 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
35 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
36 * dequote() - Remote quotes from a string.
37 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
38 * dnssdComparePrinters() - Compare the registered names of two printers.
39 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
41 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
43 * dnssdRegisterCallback() - DNSServiceRegister callback.
44 * dnssdRegisterPrinter() - Start sending broadcast information for a
45 * printer or update the broadcast contents.
46 * dnssdUpdate() - Handle DNS-SD queries.
47 * get_hostconfig() - Get an /etc/hostconfig service setting.
48 * is_local_queue() - Determine whether the URI points at a local
50 * process_browse_data() - Process new browse data.
51 * process_implicit_classes() - Create/update implicit classes as needed.
52 * send_cups_browse() - Send new browsing information using the CUPS
54 * send_ldap_browse() - Send LDAP printer registrations.
55 * send_slp_browse() - Register the specified printer with SLP.
56 * slp_attr_callback() - SLP attribute callback
57 * slp_dereg_printer() - SLPDereg() the specified printer
58 * slp_get_attr() - Get an attribute from an SLP registration.
59 * slp_reg_callback() - Empty SLPRegReport.
60 * slp_url_callback() - SLP service url callback
61 * update_cups_browse() - Update the browse lists using the CUPS
63 * update_lpd() - Update the LPD configuration as needed.
64 * update_polling() - Read status messages from the poll daemons.
65 * update_smb() - Update the SMB configuration as needed.
69 * Include necessary headers...
79 # ifdef HAVE_COREFOUNDATION
80 # include <CoreFoundation/CoreFoundation.h>
81 # endif /* HAVE_COREFOUNDATION */
82 # ifdef HAVE_SYSTEMCONFIGURATION
83 # include <SystemConfiguration/SystemConfiguration.h>
84 # endif /* HAVE_SYSTEMCONFIGURATION */
85 # endif /* __APPLE__ */
86 #endif /* HAVE_DNSSD */
93 static char *dequote(char *d
, const char *s
, int dlen
);
95 static int get_hostconfig(const char *name
);
96 #endif /* __APPLE__ */
97 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
98 char *resource
, int resourcelen
);
99 static void process_browse_data(const char *uri
, const char *host
,
100 const char *resource
, cups_ptype_t type
,
101 ipp_pstate_t state
, const char *location
,
102 const char *info
, const char *make_model
,
103 int num_attrs
, cups_option_t
*attrs
);
104 static void process_implicit_classes(void);
105 static void send_cups_browse(cupsd_printer_t
*p
);
107 static void send_ldap_browse(cupsd_printer_t
*p
);
108 #endif /* HAVE_LDAP */
110 static void send_slp_browse(cupsd_printer_t
*p
);
111 #endif /* HAVE_LIBSLP */
112 static void update_cups_browse(void);
113 static void update_lpd(int onoff
);
114 static void update_polling(void);
115 static void update_smb(int onoff
);
119 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
121 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
122 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
123 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
125 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
126 DNSServiceFlags flags
,
127 DNSServiceErrorType errorCode
,
128 const char *name
, const char *regtype
,
129 const char *domain
, void *context
);
130 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
131 static void dnssdUpdate(void);
132 #endif /* HAVE_DNSSD */
135 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
137 "printerDescription",
139 "printerMakeAndModel",
144 #endif /* HAVE_OPENLDAP */
152 * SLP service name for CUPS...
155 # define SLP_CUPS_SRVTYPE "service:printer"
156 # define SLP_CUPS_SRVLEN 15
160 * Printer service URL structure
163 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
165 struct _slpsrvurl_s
*next
; /* Next URL in list */
166 char url
[HTTP_MAX_URI
];
175 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
176 SLPError errcode
, void *cookie
);
177 static void slp_dereg_printer(cupsd_printer_t
*p
);
178 static int slp_get_attr(const char *attrlist
, const char *tag
,
180 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
182 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
183 unsigned short lifetime
,
184 SLPError errcode
, void *cookie
);
185 #endif /* HAVE_LIBSLP */
189 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
190 * local printer and remove any pending
191 * references to remote printers.
195 cupsdDeregisterPrinter(
196 cupsd_printer_t
*p
, /* I - Printer to register */
197 int removeit
) /* I - Printer being permanently removed */
200 * Only deregister if browsing is enabled and it's a local printer...
203 if (!Browsing
|| !p
->shared
||
204 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
208 * Announce the deletion...
211 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
213 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
215 p
->type
|= CUPS_PRINTER_DELETE
;
223 if (BrowseLocalProtocols
& BROWSE_SLP
)
224 slp_dereg_printer(p
);
225 #endif /* HAVE_LIBSLP */
228 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
229 dnssdDeregisterPrinter(p
);
230 #endif /* HAVE_DNSSD */
235 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
239 cupsdLoadRemoteCache(void)
241 cups_file_t
*fp
; /* remote.cache file */
242 int linenum
; /* Current line number */
243 char line
[1024], /* Line from file */
244 *value
, /* Pointer to value */
245 *valueptr
, /* Pointer into value */
246 scheme
[32], /* Scheme portion of URI */
247 username
[64], /* Username portion of URI */
249 /* Hostname portion of URI */
250 resource
[HTTP_MAX_URI
];
251 /* Resource portion of URI */
252 int port
; /* Port number */
253 cupsd_printer_t
*p
; /* Current printer */
254 time_t now
; /* Current time */
258 * Don't load the cache if the remote protocols are disabled...
263 cupsdLogMessage(CUPSD_LOG_DEBUG
,
264 "cupsdLoadRemoteCache: Not loading remote cache.");
269 * Open the remote.cache file...
272 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
273 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
277 * Read printer configurations until we hit EOF...
284 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
287 * Decode the directive...
290 if (!strcasecmp(line
, "<Printer") ||
291 !strcasecmp(line
, "<DefaultPrinter"))
294 * <Printer name> or <DefaultPrinter name>
297 if (p
== NULL
&& value
)
300 * Add the printer and a base file type...
303 cupsdLogMessage(CUPSD_LOG_DEBUG
,
304 "cupsdLoadRemoteCache: Loading printer %s...", value
);
306 if ((p
= cupsdFindDest(value
)) != NULL
)
308 if (p
->type
& CUPS_PRINTER_CLASS
)
310 cupsdLogMessage(CUPSD_LOG_WARN
,
311 "Cached remote printer \"%s\" conflicts with "
319 p
= cupsdAddPrinter(value
);
322 p
->state
= IPP_PRINTER_IDLE
;
323 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
324 p
->browse_time
= now
;
325 p
->browse_expire
= now
+ BrowseTimeout
;
328 * Set the default printer as needed...
331 if (!strcasecmp(line
, "<DefaultPrinter"))
336 cupsdLogMessage(CUPSD_LOG_ERROR
,
337 "Syntax error on line %d of remote.cache.", linenum
);
341 else if (!strcasecmp(line
, "<Class") ||
342 !strcasecmp(line
, "<DefaultClass"))
345 * <Class name> or <DefaultClass name>
348 if (p
== NULL
&& value
)
351 * Add the printer and a base file type...
354 cupsdLogMessage(CUPSD_LOG_DEBUG
,
355 "cupsdLoadRemoteCache: Loading class %s...", value
);
357 if ((p
= cupsdFindDest(value
)) != NULL
)
358 p
->type
= CUPS_PRINTER_CLASS
;
360 p
= cupsdAddClass(value
);
363 p
->state
= IPP_PRINTER_IDLE
;
364 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
365 p
->browse_time
= now
;
366 p
->browse_expire
= now
+ BrowseTimeout
;
369 * Set the default printer as needed...
372 if (!strcasecmp(line
, "<DefaultClass"))
377 cupsdLogMessage(CUPSD_LOG_ERROR
,
378 "Syntax error on line %d of remote.cache.", linenum
);
382 else if (!strcasecmp(line
, "</Printer>") ||
383 !strcasecmp(line
, "</Class>"))
388 * Close out the current printer...
391 cupsdSetPrinterAttrs(p
);
397 cupsdLogMessage(CUPSD_LOG_ERROR
,
398 "Syntax error on line %d of remote.cache.", linenum
);
404 cupsdLogMessage(CUPSD_LOG_ERROR
,
405 "Syntax error on line %d of remote.cache.", linenum
);
408 else if (!strcasecmp(line
, "Info"))
411 cupsdSetString(&p
->info
, value
);
413 else if (!strcasecmp(line
, "MakeModel"))
416 cupsdSetString(&p
->make_model
, value
);
418 else if (!strcasecmp(line
, "Location"))
421 cupsdSetString(&p
->location
, value
);
423 else if (!strcasecmp(line
, "DeviceURI"))
427 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
428 username
, sizeof(username
), host
, sizeof(host
), &port
,
429 resource
, sizeof(resource
));
431 cupsdSetString(&p
->hostname
, host
);
432 cupsdSetString(&p
->uri
, value
);
433 cupsdSetString(&p
->device_uri
, value
);
437 cupsdLogMessage(CUPSD_LOG_ERROR
,
438 "Syntax error on line %d of remote.cache.", linenum
);
442 else if (!strcasecmp(line
, "Option") && value
)
448 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
451 cupsdLogMessage(CUPSD_LOG_ERROR
,
452 "Syntax error on line %d of remote.cache.", linenum
);
455 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
457 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
461 else if (!strcasecmp(line
, "State"))
464 * Set the initial queue state...
467 if (value
&& !strcasecmp(value
, "idle"))
468 p
->state
= IPP_PRINTER_IDLE
;
469 else if (value
&& !strcasecmp(value
, "stopped"))
470 p
->state
= IPP_PRINTER_STOPPED
;
473 cupsdLogMessage(CUPSD_LOG_ERROR
,
474 "Syntax error on line %d of remote.cache.", linenum
);
478 else if (!strcasecmp(line
, "StateMessage"))
481 * Set the initial queue state message...
485 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
487 else if (!strcasecmp(line
, "Accepting"))
490 * Set the initial accepting state...
494 (!strcasecmp(value
, "yes") ||
495 !strcasecmp(value
, "on") ||
496 !strcasecmp(value
, "true")))
499 (!strcasecmp(value
, "no") ||
500 !strcasecmp(value
, "off") ||
501 !strcasecmp(value
, "false")))
505 cupsdLogMessage(CUPSD_LOG_ERROR
,
506 "Syntax error on line %d of remote.cache.", linenum
);
510 else if (!strcasecmp(line
, "Type"))
513 p
->type
= atoi(value
);
516 cupsdLogMessage(CUPSD_LOG_ERROR
,
517 "Syntax error on line %d of remote.cache.", linenum
);
521 else if (!strcasecmp(line
, "BrowseTime"))
525 time_t t
= atoi(value
);
527 if (t
> p
->browse_expire
)
528 p
->browse_expire
= t
;
532 cupsdLogMessage(CUPSD_LOG_ERROR
,
533 "Syntax error on line %d of remote.cache.", linenum
);
537 else if (!strcasecmp(line
, "JobSheets"))
540 * Set the initial job sheets...
545 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
550 cupsdSetString(&p
->job_sheets
[0], value
);
552 while (isspace(*valueptr
& 255))
557 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
562 cupsdSetString(&p
->job_sheets
[1], value
);
567 cupsdLogMessage(CUPSD_LOG_ERROR
,
568 "Syntax error on line %d of remote.cache.", linenum
);
572 else if (!strcasecmp(line
, "AllowUser"))
577 cupsdAddPrinterUser(p
, value
);
581 cupsdLogMessage(CUPSD_LOG_ERROR
,
582 "Syntax error on line %d of remote.cache.", linenum
);
586 else if (!strcasecmp(line
, "DenyUser"))
591 cupsdAddPrinterUser(p
, value
);
595 cupsdLogMessage(CUPSD_LOG_ERROR
,
596 "Syntax error on line %d of remote.cache.", linenum
);
603 * Something else we don't understand...
606 cupsdLogMessage(CUPSD_LOG_ERROR
,
607 "Unknown configuration directive %s on line %d of remote.cache.",
615 * Do auto-classing if needed...
618 process_implicit_classes();
623 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
624 * printer or update the broadcast contents.
628 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
630 if (!Browsing
|| !BrowseLocalProtocols
||
631 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
635 /* if (BrowseLocalProtocols & BROWSE_SLP)
636 slpRegisterPrinter(p); */
637 #endif /* HAVE_LIBSLP */
640 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
641 dnssdRegisterPrinter(p
);
642 #endif /* HAVE_DNSSD */
647 * 'cupsdRestartPolling()' - Restart polling servers as needed.
651 cupsdRestartPolling(void)
653 int i
; /* Looping var */
654 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
657 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
659 kill(pollp
->pid
, SIGHUP
);
664 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
668 cupsdSaveRemoteCache(void)
670 int i
; /* Looping var */
671 cups_file_t
*fp
; /* printers.conf file */
672 char temp
[1024]; /* Temporary string */
673 cupsd_printer_t
*printer
; /* Current printer class */
674 time_t curtime
; /* Current time */
675 struct tm
*curdate
; /* Current date */
676 cups_option_t
*option
; /* Current option */
680 * Create the remote.cache file...
683 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
685 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
687 cupsdLogMessage(CUPSD_LOG_ERROR
,
688 "Unable to save remote.cache - %s", strerror(errno
));
692 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
695 * Restrict access to the file...
698 fchown(cupsFileNumber(fp
), getuid(), Group
);
699 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
702 * Write a small header to the file...
705 curtime
= time(NULL
);
706 curdate
= localtime(&curtime
);
707 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
709 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
710 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
713 * Write each local printer known to the system...
716 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
718 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
721 * Skip local destinations...
724 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
728 * Write printers as needed...
731 if (printer
== DefaultPrinter
)
732 cupsFilePuts(fp
, "<Default");
734 cupsFilePutChar(fp
, '<');
736 if (printer
->type
& CUPS_PRINTER_CLASS
)
737 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
739 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
741 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
743 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
746 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
748 if (printer
->make_model
)
749 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
751 if (printer
->location
)
752 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
754 if (printer
->device_uri
)
755 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
757 if (printer
->state
== IPP_PRINTER_STOPPED
)
759 cupsFilePuts(fp
, "State Stopped\n");
760 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
763 cupsFilePuts(fp
, "State Idle\n");
765 if (printer
->accepting
)
766 cupsFilePuts(fp
, "Accepting Yes\n");
768 cupsFilePuts(fp
, "Accepting No\n");
770 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
771 printer
->job_sheets
[1]);
773 for (i
= 0; i
< printer
->num_users
; i
++)
774 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
777 for (i
= printer
->num_options
, option
= printer
->options
;
780 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
782 if (printer
->type
& CUPS_PRINTER_CLASS
)
783 cupsFilePuts(fp
, "</Class>\n");
785 cupsFilePuts(fp
, "</Printer>\n");
793 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
797 cupsdSendBrowseList(void)
799 int count
; /* Number of dests to update */
800 cupsd_printer_t
*p
; /* Current printer */
801 time_t ut
, /* Minimum update time */
802 to
; /* Timeout time */
805 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
809 * Compute the update and timeout times...
813 ut
= to
- BrowseInterval
;
816 * Figure out how many printers need an update...
819 if (BrowseInterval
> 0)
821 int max_count
; /* Maximum number to update */
825 * Throttle the number of printers we'll be updating this time
826 * around based on the number of queues that need updating and
827 * the maximum number of queues to update each second...
830 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
832 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
833 count
< max_count
&& p
!= NULL
;
834 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
835 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
836 p
->shared
&& p
->browse_time
< ut
)
840 * Loop through all of the printers and send local updates as needed...
844 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
846 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
850 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
853 * Check for wraparound...
857 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
861 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
864 else if (p
->browse_time
< ut
)
867 * Need to send an update...
872 p
->browse_time
= time(NULL
);
874 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
878 if (BrowseLocalProtocols
& BROWSE_SLP
)
880 #endif /* HAVE_LIBSLP */
883 if (BrowseLocalProtocols
& BROWSE_LDAP
)
885 #endif /* HAVE_LDAP */
890 * Save where we left off so that all printers get updated...
897 * Loop through all of the printers and send local updates as needed...
900 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
902 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
905 * If this is a remote queue, see if it needs to be timed out...
908 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
910 if (p
->browse_expire
< to
)
912 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
913 "%s \'%s\' deleted by directory services (timeout).",
914 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
917 cupsdLogMessage(CUPSD_LOG_DEBUG
,
918 "Remote destination \"%s\" has timed out; "
922 cupsArraySave(Printers
);
923 cupsdDeletePrinter(p
, 1);
924 cupsArrayRestore(Printers
);
925 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
933 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
937 cupsdStartBrowsing(void)
939 int val
; /* Socket option value */
940 struct sockaddr_in addr
; /* Broadcast address */
941 cupsd_printer_t
*p
; /* Current printer */
946 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
949 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
951 if (BrowseSocket
< 0)
954 * Create the broadcast socket...
957 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
959 cupsdLogMessage(CUPSD_LOG_ERROR
,
960 "Unable to create broadcast socket - %s.",
962 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
963 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
968 * Bind the socket to browse port...
971 memset(&addr
, 0, sizeof(addr
));
972 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
973 addr
.sin_family
= AF_INET
;
974 addr
.sin_port
= htons(BrowsePort
);
976 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
978 cupsdLogMessage(CUPSD_LOG_ERROR
,
979 "Unable to bind broadcast socket - %s.",
983 closesocket(BrowseSocket
);
989 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
990 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
996 * Set the "broadcast" flag...
1000 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1002 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1006 closesocket(BrowseSocket
);
1008 close(BrowseSocket
);
1012 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1013 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1018 * Close the socket on exec...
1021 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1024 * Finally, add the socket to the input selection set as needed...
1027 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1030 * We only listen if we want remote printers...
1033 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1041 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1043 DNSServiceErrorType error
; /* Error from service creation */
1044 cupsd_listener_t
*lis
; /* Current listening socket */
1048 * First create a "master" connection for all registrations...
1051 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1052 != kDNSServiceErr_NoError
)
1053 cupsdLogMessage(CUPSD_LOG_ERROR
,
1054 "Unable to create master DNS-SD reference: %d", error
);
1058 * Add the master connection to the select list...
1061 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1062 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1065 * Then get the port we use for registrations. If we are not listening
1066 * on any non-local ports, there is no sense sharing local printers via
1072 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1074 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1076 if (httpAddrLocalhost(&(lis
->address
)))
1079 if (lis
->address
.addr
.sa_family
== AF_INET
)
1081 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1084 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1086 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1092 * Create an array to track the printers we share...
1095 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1096 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1100 * Set the computer name and register the web interface...
1103 cupsdUpdateDNSSDName();
1106 #endif /* HAVE_DNSSD */
1109 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1112 * Open SLP handle...
1115 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1117 cupsdLogMessage(CUPSD_LOG_ERROR
,
1118 "Unable to open an SLP handle; disabling SLP browsing!");
1119 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1120 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1123 BrowseSLPRefresh
= 0;
1126 BrowseSLPHandle
= NULL
;
1127 #endif /* HAVE_LIBSLP */
1129 #ifdef HAVE_OPENLDAP
1130 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1134 cupsdLogMessage(CUPSD_LOG_ERROR
,
1135 "Need to set BrowseLDAPDN to use LDAP browsing!");
1136 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1137 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1142 * Open LDAP handle...
1145 int rc
; /* LDAP API status */
1146 int version
= 3; /* LDAP version */
1147 struct berval bv
= {0, ""}; /* SASL bind value */
1151 * Set the certificate file to use for encrypted LDAP sessions...
1154 if (BrowseLDAPCACertFile
)
1156 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1157 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1158 BrowseLDAPCACertFile
);
1160 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1161 (void *)BrowseLDAPCACertFile
))
1163 cupsdLogMessage(CUPSD_LOG_ERROR
,
1164 "Unable to set CA certificate file for LDAP "
1165 "connections: %d - %s", rc
, ldap_err2string(rc
));
1169 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1172 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1173 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1175 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1177 if (rc
!= LDAP_SUCCESS
)
1179 cupsdLogMessage(CUPSD_LOG_ERROR
,
1180 "Unable to initialize LDAP; disabling LDAP browsing!");
1181 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1182 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1184 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1185 (const void *)&version
) != LDAP_SUCCESS
)
1187 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1188 BrowseLDAPHandle
= NULL
;
1189 cupsdLogMessage(CUPSD_LOG_ERROR
,
1190 "Unable to set LDAP protocol version; "
1191 "disabling LDAP browsing!");
1192 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1193 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1197 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1198 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1201 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1202 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1204 if (rc
!= LDAP_SUCCESS
)
1206 cupsdLogMessage(CUPSD_LOG_ERROR
,
1207 "Unable to bind to LDAP server; "
1208 "disabling LDAP browsing!");
1209 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1210 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1211 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1216 BrowseLDAPRefresh
= 0;
1218 #endif /* HAVE_OPENLDAP */
1221 * Enable LPD and SMB printer sharing as needed through external programs...
1224 if (BrowseLocalProtocols
& BROWSE_LPD
)
1227 if (BrowseLocalProtocols
& BROWSE_SMB
)
1231 * Register the individual printers
1234 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1236 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1237 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1238 cupsdRegisterPrinter(p
);
1243 * 'cupsdStartPolling()' - Start polling servers as needed.
1247 cupsdStartPolling(void)
1249 int i
; /* Looping var */
1250 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1251 char polld
[1024]; /* Poll daemon path */
1252 char sport
[255]; /* Server port */
1253 char bport
[255]; /* Browser port */
1254 char interval
[255]; /* Poll interval */
1255 int statusfds
[2]; /* Status pipe */
1256 char *argv
[6]; /* Arguments */
1257 char *envp
[100]; /* Environment */
1261 * Don't do anything if we aren't polling...
1264 if (NumPolled
== 0 || BrowseSocket
< 0)
1267 PollStatusBuffer
= NULL
;
1272 * Setup string arguments for polld, port and interval options.
1275 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1277 sprintf(bport
, "%d", BrowsePort
);
1280 sprintf(interval
, "%d", BrowseInterval
);
1282 strcpy(interval
, "30");
1284 argv
[0] = "cups-polld";
1290 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1293 * Create a pipe that receives the status messages from each
1297 if (cupsdOpenPipe(statusfds
))
1299 cupsdLogMessage(CUPSD_LOG_ERROR
,
1300 "Unable to create polling status pipes - %s.",
1303 PollStatusBuffer
= NULL
;
1307 PollPipe
= statusfds
[0];
1308 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1311 * Run each polling daemon, redirecting stderr to the polling pipe...
1314 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1316 sprintf(sport
, "%d", pollp
->port
);
1318 argv
[1] = pollp
->hostname
;
1320 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1321 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1323 cupsdLogMessage(CUPSD_LOG_ERROR
,
1324 "cupsdStartPolling: Unable to fork polling daemon - %s",
1330 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1331 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1332 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1335 close(statusfds
[1]);
1338 * Finally, add the pipe to the input selection set...
1341 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1346 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1350 cupsdStopBrowsing(void)
1352 cupsd_printer_t
*p
; /* Current printer */
1355 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1359 * De-register the individual printers
1362 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1364 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1365 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1366 cupsdDeregisterPrinter(p
, 1);
1369 * Shut down browsing sockets...
1372 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1376 * Close the socket and remove it from the input selection set.
1380 closesocket(BrowseSocket
);
1382 close(BrowseSocket
);
1385 cupsdRemoveSelect(BrowseSocket
);
1390 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1394 DNSServiceRefDeallocate(WebIFRef
);
1400 DNSServiceRefDeallocate(RemoteRef
);
1404 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1406 DNSServiceRefDeallocate(DNSSDRef
);
1409 cupsArrayDelete(DNSSDPrinters
);
1410 DNSSDPrinters
= NULL
;
1414 #endif /* HAVE_DNSSD */
1417 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1421 * Close SLP handle...
1424 SLPClose(BrowseSLPHandle
);
1425 BrowseSLPHandle
= NULL
;
1427 #endif /* HAVE_LIBSLP */
1429 #ifdef HAVE_OPENLDAP
1430 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1433 ldap_unbind(BrowseLDAPHandle
);
1434 BrowseLDAPHandle
= NULL
;
1436 #endif /* HAVE_OPENLDAP */
1439 * Disable LPD and SMB printer sharing as needed through external programs...
1442 if (BrowseLocalProtocols
& BROWSE_LPD
)
1445 if (BrowseLocalProtocols
& BROWSE_SMB
)
1451 * 'cupsdStopPolling()' - Stop polling servers as needed.
1455 cupsdStopPolling(void)
1457 int i
; /* Looping var */
1458 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1463 cupsdStatBufDelete(PollStatusBuffer
);
1466 cupsdRemoveSelect(PollPipe
);
1469 PollStatusBuffer
= NULL
;
1472 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1474 cupsdEndProcess(pollp
->pid
, 0);
1480 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1484 cupsdUpdateDNSSDName(void)
1486 DNSServiceErrorType error
; /* Error from service creation */
1487 char webif
[1024]; /* Web interface share name */
1488 #ifdef HAVE_COREFOUNDATION_H
1489 CFStringRef nameRef
; /* Computer name CFString */
1490 char nameBuffer
[1024]; /* C-string buffer */
1491 CFStringEncoding nameEncoding
; /* Computer name encoding */
1492 #endif /* HAVE_COREFOUNDATION_H */
1496 * Only share the web interface and printers when non-local listening is
1504 * Get the computer name as a c-string...
1507 #ifdef HAVE_COREFOUNDATION_H
1508 cupsdClearString(&DNSSDName
);
1510 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1511 &nameEncoding
)) != NULL
)
1513 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1514 kCFStringEncodingUTF8
))
1515 cupsdSetString(&DNSSDName
, nameBuffer
);
1521 cupsdSetString(&DNSSDName
, ServerName
);
1522 #endif /* HAVE_COREFOUNDATION_H */
1525 * Then (re)register the web interface...
1529 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1531 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1534 DNSServiceRefDeallocate(WebIFRef
);
1536 WebIFRef
= DNSSDRef
;
1537 if ((error
= DNSServiceRegister(&WebIFRef
,
1538 kDNSServiceFlagsShareConnection
,
1539 0, webif
, "_http._tcp", NULL
,
1540 NULL
, htons(DNSSDPort
), 7,
1541 "\006path=/", dnssdRegisterCallback
,
1542 NULL
)) != kDNSServiceErr_NoError
)
1543 cupsdLogMessage(CUPSD_LOG_ERROR
,
1544 "DNS-SD web interface registration failed: %d", error
);
1546 #endif /* HAVE_DNSSD */
1549 #ifdef HAVE_OPENLDAP
1551 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1555 cupsdUpdateLDAPBrowse(void)
1557 char uri
[HTTP_MAX_URI
], /* Printer URI */
1558 host
[HTTP_MAX_URI
], /* Hostname */
1559 resource
[HTTP_MAX_URI
], /* Resource path */
1560 location
[1024], /* Printer location */
1561 info
[1024], /* Printer information */
1562 make_model
[1024], /* Printer make and model */
1563 **value
; /* Holds the returned data from LDAP */
1564 int type
; /* Printer type */
1565 int rc
; /* LDAP status */
1566 int limit
; /* Size limit */
1567 LDAPMessage
*res
, /* LDAP search results */
1568 *e
; /* Current entry from search */
1572 * Search for printers...
1575 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1577 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1579 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1580 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1581 if (rc
!= LDAP_SUCCESS
)
1583 cupsdLogMessage(CUPSD_LOG_ERROR
,
1584 "LDAP search returned error %d: %s", rc
,
1585 ldap_err2string(rc
));
1589 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1590 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1595 * Loop through the available printers...
1598 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1600 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1603 * Get the required values from this entry...
1606 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1607 "printerDescription")) == NULL
)
1610 strlcpy(info
, *value
, sizeof(info
));
1611 ldap_value_free(value
);
1613 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1614 "printerLocation")) == NULL
)
1617 strlcpy(location
, *value
, sizeof(location
));
1618 ldap_value_free(value
);
1620 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1621 "printerMakeAndModel")) == NULL
)
1624 strlcpy(make_model
, *value
, sizeof(make_model
));
1625 ldap_value_free(value
);
1627 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1628 "printerType")) == NULL
)
1631 type
= atoi(*value
);
1632 ldap_value_free(value
);
1634 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1635 "printerURI")) == NULL
)
1638 strlcpy(uri
, *value
, sizeof(uri
));
1639 ldap_value_free(value
);
1642 * Process the entry as browse data...
1645 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1646 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1647 location
, info
, make_model
, 0, NULL
);
1651 #endif /* HAVE_OPENLDAP */
1656 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1660 cupsdUpdateSLPBrowse(void)
1662 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1663 *next
; /* Next service in list */
1664 cupsd_printer_t p
; /* Printer information */
1665 const char *uri
; /* Pointer to printer URI */
1666 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1667 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1671 * Reset the refresh time...
1674 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1677 * Poll for remote printers using SLP...
1682 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1683 slp_url_callback
, &s
);
1686 * Loop through the list of available printers...
1692 * Save the "next" pointer...
1698 * Load a cupsd_printer_t structure with the SLP service attributes...
1701 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1704 * Process this printer entry...
1707 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1709 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1712 * Pull the URI apart to see if this is a local or remote printer...
1715 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1716 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1717 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1721 * Free this listing...
1724 cupsdClearString(&p
.info
);
1725 cupsdClearString(&p
.location
);
1726 cupsdClearString(&p
.make_model
);
1731 #endif /* HAVE_LIBSLP */
1735 * 'dequote()' - Remote quotes from a string.
1738 static char * /* O - Dequoted string */
1739 dequote(char *d
, /* I - Destination string */
1740 const char *s
, /* I - Source string */
1741 int dlen
) /* I - Destination length */
1743 char *dptr
; /* Pointer into destination */
1748 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1766 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
1769 static char * /* O - TXT record */
1770 dnssdBuildTxtRecord(
1771 int *txt_len
, /* O - TXT record length */
1772 cupsd_printer_t
*p
, /* I - Printer information */
1773 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
1775 int i
, j
; /* Looping vars */
1776 char type_str
[32], /* Type to string buffer */
1777 state_str
[32], /* State to string buffer */
1778 rp_str
[1024], /* Queue name string buffer */
1779 air_str
[1024], /* auth-info-required string buffer */
1780 *keyvalue
[32][2]; /* Table of key/value pairs */
1784 * Load up the key value pairs...
1789 keyvalue
[i
][0] = "txtvers";
1790 keyvalue
[i
++][1] = "1";
1792 keyvalue
[i
][0] = "qtotal";
1793 keyvalue
[i
++][1] = "1";
1795 keyvalue
[i
][0] = "rp";
1796 keyvalue
[i
++][1] = rp_str
;
1798 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
1800 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
1801 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
1803 keyvalue
[i
][0] = "ty";
1804 keyvalue
[i
++][1] = p
->make_model
;
1806 if (p
->location
&& *p
->location
!= '\0')
1808 keyvalue
[i
][0] = "note";
1809 keyvalue
[i
++][1] = p
->location
;
1812 keyvalue
[i
][0] = "priority";
1813 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
1815 keyvalue
[i
][0] = "product";
1816 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
1818 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
1819 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
1821 keyvalue
[i
][0] = "printer-state";
1822 keyvalue
[i
++][1] = state_str
;
1824 keyvalue
[i
][0] = "printer-type";
1825 keyvalue
[i
++][1] = type_str
;
1827 keyvalue
[i
][0] = "Transparent";
1828 keyvalue
[i
++][1] = "T";
1830 keyvalue
[i
][0] = "Binary";
1831 keyvalue
[i
++][1] = "T";
1833 if ((p
->type
& CUPS_PRINTER_FAX
))
1835 keyvalue
[i
][0] = "Fax";
1836 keyvalue
[i
++][1] = "T";
1839 if ((p
->type
& CUPS_PRINTER_COLOR
))
1841 keyvalue
[i
][0] = "Color";
1842 keyvalue
[i
++][1] = "T";
1845 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
1847 keyvalue
[i
][0] = "Duplex";
1848 keyvalue
[i
++][1] = "T";
1851 if ((p
->type
& CUPS_PRINTER_STAPLE
))
1853 keyvalue
[i
][0] = "Staple";
1854 keyvalue
[i
++][1] = "T";
1857 if ((p
->type
& CUPS_PRINTER_COPIES
))
1859 keyvalue
[i
][0] = "Copies";
1860 keyvalue
[i
++][1] = "T";
1863 if ((p
->type
& CUPS_PRINTER_COLLATE
))
1865 keyvalue
[i
][0] = "Collate";
1866 keyvalue
[i
++][1] = "T";
1869 if ((p
->type
& CUPS_PRINTER_PUNCH
))
1871 keyvalue
[i
][0] = "Punch";
1872 keyvalue
[i
++][1] = "T";
1875 if ((p
->type
& CUPS_PRINTER_BIND
))
1877 keyvalue
[i
][0] = "Bind";
1878 keyvalue
[i
++][1] = "T";
1881 if ((p
->type
& CUPS_PRINTER_SORT
))
1883 keyvalue
[i
][0] = "Sort";
1884 keyvalue
[i
++][1] = "T";
1887 keyvalue
[i
][0] = "pdl";
1888 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
1890 if (p
->num_auth_info_required
)
1892 char *air
= air_str
; /* Pointer into string */
1895 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
1897 if (air
>= (air_str
+ sizeof(air_str
) - 2))
1903 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
1907 keyvalue
[i
][0] = "air";
1908 keyvalue
[i
++][1] = air
;
1912 * Then pack them into a proper txt record...
1915 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
1920 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
1923 static int /* O - Result of comparison */
1924 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
1925 cupsd_printer_t
*b
)/* I - Second printer */
1927 return (strcasecmp(a
->reg_name
, b
->reg_name
));
1932 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
1937 dnssdDeregisterPrinter(
1938 cupsd_printer_t
*p
) /* I - Printer */
1940 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
1943 * Closing the socket deregisters the service
1948 DNSServiceRefDeallocate(p
->ipp_ref
);
1952 cupsArrayRemove(DNSSDPrinters
, p
);
1953 cupsdClearString(&p
->reg_name
);
1958 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
1968 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
1969 * TXT record format.
1972 static char * /* O - TXT record */
1973 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
1974 char *keyvalue
[][2], /* I - Table of key value pairs */
1975 int count
) /* I - Items in table */
1977 int i
; /* Looping var */
1978 int length
; /* Length of TXT record */
1979 int length2
; /* Length of value */
1980 char *txtRecord
; /* TXT record buffer */
1981 char *cursor
; /* Looping pointer */
1985 * Calculate the buffer size
1988 for (length
= i
= 0; i
< count
; i
++)
1989 length
+= 1 + strlen(keyvalue
[i
][0]) +
1990 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
1993 * Allocate and fill it
1996 txtRecord
= malloc(length
);
2001 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2004 * Drop in the p-string style length byte followed by the data
2007 length
= strlen(keyvalue
[i
][0]);
2008 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2010 *cursor
++ = (unsigned char)(length
+ length2
);
2012 memcpy(cursor
, keyvalue
[i
][0], length
);
2019 memcpy(cursor
, keyvalue
[i
][1], length2
);
2030 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2034 dnssdRegisterCallback(
2035 DNSServiceRef sdRef
, /* I - DNS Service reference */
2036 DNSServiceFlags flags
, /* I - Reserved for future use */
2037 DNSServiceErrorType errorCode
, /* I - Error code */
2038 const char *name
, /* I - Service name */
2039 const char *regtype
, /* I - Service type */
2040 const char *domain
, /* I - Domain. ".local" for now */
2041 void *context
) /* I - User-defined context */
2043 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2044 /* Current printer */
2047 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2048 name
, regtype
, p
? p
->name
: "Web Interface");
2052 cupsdLogMessage(CUPSD_LOG_ERROR
,
2053 "DNSServiceRegister failed with error %d", (int)errorCode
);
2056 else if (p
&& strcasecmp(name
, p
->reg_name
))
2058 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2061 cupsArrayRemove(DNSSDPrinters
, p
);
2062 cupsdSetString(&p
->reg_name
, name
);
2063 cupsArrayAdd(DNSSDPrinters
, p
);
2065 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2071 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2072 * or update the broadcast contents.
2076 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2078 DNSServiceErrorType se
; /* dnssd errors */
2079 char *ipp_txt
, /* IPP TXT record buffer */
2080 *printer_txt
, /* LPD TXT record buffer */
2081 name
[1024], /* Service name */
2082 *nameptr
; /* Pointer into name */
2083 int ipp_len
, /* IPP TXT record length */
2084 printer_len
; /* LPD TXT record length */
2085 char resource
[1024]; /* Resource path for printer */
2086 const char *regtype
; /* Registration type */
2087 const char *domain
; /* Registration domain */
2088 cupsd_location_t
*location
, /* Printer location */
2089 *policy
; /* Operation policy for Print-Job */
2090 unsigned address
[4]; /* INADDR_ANY address */
2093 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2094 !p
->ipp_ref
? "new" : "update");
2097 * If per-printer sharing was just disabled make sure we're not
2098 * registered before returning.
2103 dnssdDeregisterPrinter(p
);
2108 * The registered name takes the form of "<printer-info> @ <computer name>"...
2111 if (p
->info
&& strlen(p
->info
) > 0)
2114 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2116 strlcpy(name
, p
->info
, sizeof(name
));
2119 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2121 strlcpy(name
, p
->name
, sizeof(name
));
2124 * If an existing printer was renamed, unregister it and start over...
2127 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2128 dnssdDeregisterPrinter(p
);
2132 cupsdSetString(&p
->reg_name
, name
);
2133 cupsArrayAdd(DNSSDPrinters
, p
);
2137 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2138 * let dnssd decide on the domain, otherwise restrict it to ".local".
2141 if (p
->type
& CUPS_PRINTER_CLASS
)
2142 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2144 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2146 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2147 location
= cupsdFindBest(resource
, HTTP_POST
);
2148 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2150 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2151 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2157 * Register IPP and (optionally) LPD...
2160 ipp_len
= 0; /* anti-compiler-warning-code */
2161 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2166 * Initial registration. Use the _fax subtype for fax queues...
2169 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2172 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2173 "Registering DNS-SD printer %s with name \"%s\", "
2174 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2175 domain
? domain
: "(null)");
2178 * Register the queue, dropping characters as needed until we succeed...
2181 nameptr
= name
+ strlen(name
);
2185 p
->ipp_ref
= DNSSDRef
;
2186 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2187 0, name
, regtype
, domain
, NULL
,
2188 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2189 dnssdRegisterCallback
,
2190 p
)) == kDNSServiceErr_BadParam
)
2193 * Name is too long, drop trailing characters, taking into account
2199 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2206 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2208 if (se
== kDNSServiceErr_NoError
)
2210 p
->ipp_txt
= ipp_txt
;
2211 p
->ipp_len
= ipp_len
;
2215 cupsdLogMessage(CUPSD_LOG_WARN
,
2216 "DNS-SD IPP registration of \"%s\" failed: %d",
2219 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2222 * Update the existing registration...
2225 /* A TTL of 0 means use record's original value (Radar 3176248) */
2226 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2231 p
->ipp_txt
= ipp_txt
;
2232 p
->ipp_len
= ipp_len
;
2239 if (BrowseLocalProtocols
& BROWSE_LPD
)
2241 printer_len
= 0; /* anti-compiler-warning-code */
2242 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2244 if (!p
->printer_ref
)
2247 * Initial registration...
2250 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2251 "Registering DNS-SD printer %s with name \"%s\", "
2252 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2253 name
, domain
? domain
: "(null)");
2255 p
->printer_ref
= DNSSDRef
;
2256 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2257 kDNSServiceFlagsShareConnection
,
2258 0, name
, "_printer._tcp", domain
, NULL
,
2259 htons(515), printer_len
, printer_txt
,
2260 dnssdRegisterCallback
,
2261 p
)) == kDNSServiceErr_NoError
)
2263 p
->printer_txt
= printer_txt
;
2264 p
->printer_len
= printer_len
;
2268 cupsdLogMessage(CUPSD_LOG_WARN
,
2269 "DNS-SD LPD registration of \"%s\" failed: %d",
2272 else if (printer_len
!= p
->printer_len
||
2273 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2276 * Update the existing registration...
2279 /* A TTL of 0 means use record's original value (Radar 3176248) */
2280 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2284 free(p
->printer_txt
);
2286 p
->printer_txt
= printer_txt
;
2287 p
->printer_len
= printer_len
;
2298 * 'dnssdUpdate()' - Handle DNS-SD queries.
2304 DNSServiceErrorType sdErr
; /* Service discovery error */
2307 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2308 cupsdLogMessage(CUPSD_LOG_ERROR
,
2309 "DNS Service Discovery registration error %d!",
2312 #endif /* HAVE_DNSSD */
2317 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2320 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2321 get_hostconfig(const char *name
) /* I - Name of service */
2323 cups_file_t
*fp
; /* Hostconfig file */
2324 char line
[1024], /* Line from file */
2325 *ptr
; /* Pointer to value */
2326 int state
= 1; /* State of service */
2330 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2331 * the service is enabled/auto.
2334 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2337 * Read lines from the file until we find the service...
2340 while (cupsFileGets(fp
, line
, sizeof(line
)))
2342 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2347 if (!strcasecmp(line
, name
))
2350 * Found the service, see if it is set to "-NO-"...
2353 if (!strncasecmp(ptr
, "-NO-", 4))
2364 #endif /* __APPLE__ */
2368 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2371 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2372 is_local_queue(const char *uri
, /* I - Printer URI */
2373 char *host
, /* O - Host string */
2374 int hostlen
, /* I - Length of host buffer */
2375 char *resource
, /* O - Resource string */
2376 int resourcelen
) /* I - Length of resource buffer */
2378 char scheme
[32], /* Scheme portion of URI */
2379 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2380 int port
; /* Port portion of URI */
2381 cupsd_netif_t
*iface
; /* Network interface */
2385 * Pull the URI apart to see if this is a local or remote printer...
2388 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2389 username
, sizeof(username
), host
, hostlen
, &port
,
2390 resource
, resourcelen
) < HTTP_URI_OK
)
2393 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2396 * Check for local server addresses...
2399 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2404 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2406 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2407 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2411 * If we get here, the printer is remote...
2419 * 'process_browse_data()' - Process new browse data.
2423 process_browse_data(
2424 const char *uri
, /* I - URI of printer/class */
2425 const char *host
, /* I - Hostname */
2426 const char *resource
, /* I - Resource path */
2427 cups_ptype_t type
, /* I - Printer type */
2428 ipp_pstate_t state
, /* I - Printer state */
2429 const char *location
, /* I - Printer location */
2430 const char *info
, /* I - Printer information */
2431 const char *make_model
, /* I - Printer make and model */
2432 int num_attrs
, /* I - Number of attributes */
2433 cups_option_t
*attrs
) /* I - Attributes */
2435 int i
; /* Looping var */
2436 int update
; /* Update printer attributes? */
2437 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2438 name
[IPP_MAX_NAME
], /* Name of printer */
2439 newname
[IPP_MAX_NAME
], /* New name of printer */
2440 *hptr
, /* Pointer into hostname */
2441 *sptr
; /* Pointer into ServerName */
2442 char local_make_model
[IPP_MAX_NAME
];
2443 /* Local make and model */
2444 cupsd_printer_t
*p
; /* Printer information */
2445 const char *ipp_options
, /* ipp-options value */
2446 *lease_duration
; /* lease-duration value */
2450 * Determine if the URI contains any illegal characters in it...
2453 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2454 (strncmp(resource
, "/printers/", 10) &&
2455 strncmp(resource
, "/classes/", 9)))
2457 cupsdLogMessage(CUPSD_LOG_ERROR
,
2458 "process_browse_data: Bad printer URI in browse data: %s",
2463 if (strchr(resource
, '?') ||
2464 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2465 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2467 cupsdLogMessage(CUPSD_LOG_ERROR
,
2468 "process_browse_data: Bad resource in browse data: %s",
2474 * OK, this isn't a local printer; add any remote options...
2477 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2479 if (BrowseRemoteOptions
)
2481 if (BrowseRemoteOptions
[0] == '?')
2484 * Override server-supplied options...
2487 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2489 else if (ipp_options
)
2492 * Combine the server and local options...
2495 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2496 BrowseRemoteOptions
);
2501 * Just use the local options...
2504 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2509 else if (ipp_options
)
2512 * Just use the server-supplied options...
2515 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2520 * See if we already have it listed in the Printers list, and add it if not...
2523 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2524 type
&= ~CUPS_PRINTER_IMPLICIT
;
2526 hptr
= strchr(host
, '.');
2527 sptr
= strchr(ServerName
, '.');
2529 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2532 * Strip the common domain name components...
2535 while (hptr
!= NULL
)
2537 if (!strcasecmp(hptr
, sptr
))
2543 hptr
= strchr(hptr
+ 1, '.');
2547 if (type
& CUPS_PRINTER_CLASS
)
2550 * Remote destination is a class...
2553 if (!strncmp(resource
, "/classes/", 9))
2554 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2559 *hptr
= '.'; /* Resource FQDN */
2561 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2563 if ((p
= cupsdFindDest(resource
+ 9)) != NULL
)
2565 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2568 * Nope, this isn't the same host; if the hostname isn't the local host,
2569 * add it to the other class and then find a class using the full host
2573 if (p
->type
& CUPS_PRINTER_REMOTE
)
2575 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2576 "Renamed remote class \"%s\" to \"%s@%s\"...",
2577 p
->name
, p
->name
, p
->hostname
);
2578 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2579 "Class \'%s\' deleted by directory services.",
2582 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2583 cupsdRenamePrinter(p
, newname
);
2585 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2586 "Class \'%s\' added by directory services.",
2592 else if (!p
->hostname
)
2595 * Hostname not set, so this must be a cached remote printer
2596 * that was created for a pending print job...
2599 cupsdSetString(&p
->hostname
, host
);
2600 cupsdSetString(&p
->uri
, uri
);
2601 cupsdSetString(&p
->device_uri
, uri
);
2608 * Use the short name for this shared class.
2611 strlcpy(name
, resource
+ 9, sizeof(name
));
2614 else if (p
&& !p
->hostname
)
2617 * Hostname not set, so this must be a cached remote printer
2618 * that was created for a pending print job...
2621 cupsdSetString(&p
->hostname
, host
);
2622 cupsdSetString(&p
->uri
, uri
);
2623 cupsdSetString(&p
->device_uri
, uri
);
2630 * Class doesn't exist; add it...
2633 p
= cupsdAddClass(name
);
2635 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2637 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2638 "Class \'%s\' added by directory services.", name
);
2641 * Force the URI to point to the real server...
2644 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2646 cupsdSetString(&p
->uri
, uri
);
2647 cupsdSetString(&p
->device_uri
, uri
);
2648 cupsdSetString(&p
->hostname
, host
);
2652 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2658 * Remote destination is a printer...
2661 if (!strncmp(resource
, "/printers/", 10))
2662 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2667 *hptr
= '.'; /* Resource FQDN */
2669 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2671 if ((p
= cupsdFindDest(resource
+ 10)) != NULL
)
2673 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2676 * Nope, this isn't the same host; if the hostname isn't the local host,
2677 * add it to the other printer and then find a printer using the full host
2681 if (p
->type
& CUPS_PRINTER_REMOTE
)
2683 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2684 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2685 p
->name
, p
->name
, p
->hostname
);
2686 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2687 "Printer \'%s\' deleted by directory services.",
2690 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2691 cupsdRenamePrinter(p
, newname
);
2693 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2694 "Printer \'%s\' added by directory services.",
2700 else if (!p
->hostname
)
2703 * Hostname not set, so this must be a cached remote printer
2704 * that was created for a pending print job...
2707 cupsdSetString(&p
->hostname
, host
);
2708 cupsdSetString(&p
->uri
, uri
);
2709 cupsdSetString(&p
->device_uri
, uri
);
2716 * Use the short name for this shared printer.
2719 strlcpy(name
, resource
+ 10, sizeof(name
));
2722 else if (p
&& !p
->hostname
)
2725 * Hostname not set, so this must be a cached remote printer
2726 * that was created for a pending print job...
2729 cupsdSetString(&p
->hostname
, host
);
2730 cupsdSetString(&p
->uri
, uri
);
2731 cupsdSetString(&p
->device_uri
, uri
);
2738 * Printer doesn't exist; add it...
2741 p
= cupsdAddPrinter(name
);
2743 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2744 "Printer \'%s\' added by directory services.", name
);
2746 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2749 * Force the URI to point to the real server...
2752 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2754 cupsdSetString(&p
->hostname
, host
);
2755 cupsdSetString(&p
->uri
, uri
);
2756 cupsdSetString(&p
->device_uri
, uri
);
2760 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2765 * Update the state...
2769 p
->browse_time
= time(NULL
);
2771 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2775 * Grab the lease-duration for the browse data; anything less then 1
2776 * second or more than 1 week gets the default BrowseTimeout...
2779 i
= atoi(lease_duration
);
2780 if (i
< 1 || i
> 604800)
2783 p
->browse_expire
= p
->browse_time
+ i
;
2786 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2788 if (type
& CUPS_PRINTER_REJECTING
)
2790 type
&= ~CUPS_PRINTER_REJECTING
;
2798 else if (!p
->accepting
)
2804 if (p
->type
!= type
)
2810 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2812 cupsdSetString(&p
->location
, location
);
2816 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2818 cupsdSetString(&p
->info
, info
);
2821 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2824 if (!make_model
|| !make_model
[0])
2826 if (type
& CUPS_PRINTER_CLASS
)
2827 snprintf(local_make_model
, sizeof(local_make_model
),
2828 "Remote Class on %s", host
);
2830 snprintf(local_make_model
, sizeof(local_make_model
),
2831 "Remote Printer on %s", host
);
2834 snprintf(local_make_model
, sizeof(local_make_model
),
2835 "%s on %s", make_model
, host
);
2837 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2839 cupsdSetString(&p
->make_model
, local_make_model
);
2845 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2848 * See if we need to update the attributes...
2851 if (p
->num_options
!= num_attrs
)
2855 for (i
= 0; i
< num_attrs
; i
++)
2856 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2857 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2858 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2867 * Free the old options...
2870 cupsFreeOptions(p
->num_options
, p
->options
);
2873 p
->num_options
= num_attrs
;
2876 if (type
& CUPS_PRINTER_DELETE
)
2878 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2879 "%s \'%s\' deleted by directory services.",
2880 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2882 cupsdExpireSubscriptions(p
, NULL
);
2884 cupsdDeletePrinter(p
, 1);
2885 cupsdUpdateImplicitClasses();
2886 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2890 cupsdSetPrinterAttrs(p
);
2891 cupsdUpdateImplicitClasses();
2895 * See if we have a default printer... If not, make the first network
2896 * default printer the default.
2899 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2902 * Find the first network default printer and use it...
2905 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2907 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2908 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2911 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2917 * Do auto-classing if needed...
2920 process_implicit_classes();
2925 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2929 process_implicit_classes(void)
2931 int i
; /* Looping var */
2932 int update
; /* Update printer attributes? */
2933 char name
[IPP_MAX_NAME
], /* Name of printer */
2934 *hptr
; /* Pointer into hostname */
2935 cupsd_printer_t
*p
, /* Printer information */
2936 *pclass
, /* Printer class */
2937 *first
; /* First printer in class */
2938 int offset
, /* Offset of name */
2939 len
; /* Length of name */
2942 if (!ImplicitClasses
|| !Printers
)
2946 * Loop through all available printers and create classes as needed...
2949 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2950 update
= 0, pclass
= NULL
, first
= NULL
;
2952 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2955 * Skip implicit classes...
2958 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2965 * If len == 0, get the length of this printer name up to the "@"
2969 cupsArraySave(Printers
);
2972 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2973 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2976 * We have more than one printer with the same name; see if
2977 * we have a class, and if this printer is a member...
2980 if (pclass
&& strcasecmp(pclass
->name
, name
))
2983 cupsdSetPrinterAttrs(pclass
);
2989 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2992 * Need to add the class...
2995 pclass
= cupsdAddPrinter(name
);
2996 cupsArrayAdd(ImplicitPrinters
, pclass
);
2998 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2999 pclass
->accepting
= 1;
3000 pclass
->state
= IPP_PRINTER_IDLE
;
3002 cupsdSetString(&pclass
->location
, p
->location
);
3003 cupsdSetString(&pclass
->info
, p
->info
);
3005 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3006 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3010 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3012 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3014 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3015 "Implicit class \'%s\' added by directory services.",
3021 for (i
= 0; i
< pclass
->num_printers
; i
++)
3022 if (pclass
->printers
[i
] == first
)
3025 if (i
>= pclass
->num_printers
)
3027 first
->in_implicit_class
= 1;
3028 cupsdAddPrinterToClass(pclass
, first
);
3034 for (i
= 0; i
< pclass
->num_printers
; i
++)
3035 if (pclass
->printers
[i
] == p
)
3038 if (i
>= pclass
->num_printers
)
3040 p
->in_implicit_class
= 1;
3041 cupsdAddPrinterToClass(pclass
, p
);
3048 * First time around; just get name length and mark it as first
3052 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3053 len
= hptr
- p
->name
;
3055 len
= strlen(p
->name
);
3057 strncpy(name
, p
->name
, len
);
3061 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3062 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3065 * Can't use same name as a local printer; add "Any" to the
3066 * front of the name, unless we have explicitly disabled
3067 * the "ImplicitAnyClasses"...
3070 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3073 * Add "Any" to the class name...
3076 strcpy(name
, "Any");
3077 strncpy(name
+ 3, p
->name
, len
);
3078 name
[len
+ 3] = '\0';
3084 * Don't create an implicit class if we have a local printer
3085 * with the same name...
3089 cupsArrayRestore(Printers
);
3097 cupsArrayRestore(Printers
);
3101 * Update the last printer class as needed...
3104 if (pclass
&& update
)
3105 cupsdSetPrinterAttrs(pclass
);
3110 * 'send_cups_browse()' - Send new browsing information using the CUPS
3115 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3117 int i
; /* Looping var */
3118 cups_ptype_t type
; /* Printer type */
3119 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3120 int bytes
; /* Length of packet */
3121 char packet
[1453], /* Browse data packet */
3122 uri
[1024], /* Printer URI */
3123 location
[1024], /* printer-location */
3124 info
[1024], /* printer-info */
3126 /* printer-make-and-model */
3127 cupsd_netif_t
*iface
; /* Network interface */
3131 * Figure out the printer type value...
3134 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3137 type
|= CUPS_PRINTER_REJECTING
;
3139 if (p
== DefaultPrinter
)
3140 type
|= CUPS_PRINTER_DEFAULT
;
3143 * Remove quotes from printer-info, printer-location, and
3144 * printer-make-and-model attributes...
3147 dequote(location
, p
->location
, sizeof(location
));
3148 dequote(info
, p
->info
, sizeof(info
));
3151 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3152 else if (p
->type
& CUPS_PRINTER_CLASS
)
3154 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3155 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3157 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3160 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3162 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3165 * Send a packet to each browse address...
3168 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3172 * Send the browse packet to one or more interfaces...
3175 if (!strcmp(b
->iface
, "*"))
3178 * Send to all local interfaces...
3183 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3185 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3188 * Only send to local, IPv4 interfaces...
3191 if (!iface
->is_local
|| !iface
->port
||
3192 iface
->address
.addr
.sa_family
!= AF_INET
)
3195 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3196 iface
->hostname
, iface
->port
,
3197 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3200 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3201 type
, p
->state
, uri
, location
, info
, make_model
,
3202 p
->browse_attrs
? p
->browse_attrs
: "");
3204 bytes
= strlen(packet
);
3206 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3207 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3208 iface
->name
, packet
);
3210 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3212 sendto(BrowseSocket
, packet
, bytes
, 0,
3213 (struct sockaddr
*)&(iface
->broadcast
),
3214 httpAddrLength(&(iface
->broadcast
)));
3217 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3220 * Send to the named interface using the IPv4 address...
3224 if (strcmp(b
->iface
, iface
->name
))
3229 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3232 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3236 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3237 iface
->hostname
, iface
->port
,
3238 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3241 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3242 type
, p
->state
, uri
, location
, info
, make_model
,
3243 p
->browse_attrs
? p
->browse_attrs
: "");
3245 bytes
= strlen(packet
);
3247 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3248 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3249 iface
->name
, packet
);
3251 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3253 sendto(BrowseSocket
, packet
, bytes
, 0,
3254 (struct sockaddr
*)&(iface
->broadcast
),
3255 httpAddrLength(&(iface
->broadcast
)));
3262 * Send the browse packet to the indicated address using
3263 * the default server name...
3266 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3267 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3268 p
->browse_attrs
? p
->browse_attrs
: "");
3270 bytes
= strlen(packet
);
3271 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3272 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3274 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3275 (struct sockaddr
*)&(b
->to
),
3276 httpAddrLength(&(b
->to
))) <= 0)
3279 * Unable to send browse packet, so remove this address from the
3283 cupsdLogMessage(CUPSD_LOG_ERROR
,
3284 "cupsdSendBrowseList: sendto failed for browser "
3286 (int)(b
- Browsers
+ 1), strerror(errno
));
3289 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3298 #ifdef HAVE_OPENLDAP
3300 * 'send_ldap_browse()' - Send LDAP printer registrations.
3304 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3306 int i
; /* Looping var... */
3307 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3308 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3309 LDAPMessage
*res
; /* Search result token */
3310 char *cn_value
[2], /* Change records */
3316 typestring
[255], /* String to hold printer-type */
3317 filter
[256], /* Search filter for possible UPDATEs */
3318 dn
[1024]; /* DN of the printer we are adding */
3319 int rc
; /* LDAP status */
3320 static const char * const objectClass_values
[] =
3321 { /* The 3 objectClass's we use in */
3322 "top", /* our LDAP entries */
3328 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
3331 * Everything in ldap is ** so we fudge around it...
3334 sprintf(typestring
, "%u", p
->type
);
3336 cn_value
[0] = p
->name
;
3338 info
[0] = p
->info
? p
->info
: "Unknown";
3340 location
[0] = p
->location
? p
->location
: "Unknown";
3342 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3343 make_model
[1] = NULL
;
3344 type
[0] = typestring
;
3349 snprintf(filter
, sizeof(filter
),
3350 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3352 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3353 filter
, (char **)ldap_attrs
, 0, &res
);
3354 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3357 mods
[0].mod_type
= "cn";
3358 mods
[0].mod_values
= cn_value
;
3359 mods
[1].mod_type
= "printerDescription";
3360 mods
[1].mod_values
= info
;
3361 mods
[2].mod_type
= "printerURI";
3362 mods
[2].mod_values
= uri
;
3363 mods
[3].mod_type
= "printerLocation";
3364 mods
[3].mod_values
= location
;
3365 mods
[4].mod_type
= "printerMakeAndModel";
3366 mods
[4].mod_values
= make_model
;
3367 mods
[5].mod_type
= "printerType";
3368 mods
[5].mod_values
= type
;
3369 mods
[6].mod_type
= "objectClass";
3370 mods
[6].mod_values
= (char **)objectClass_values
;
3372 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3373 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3375 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3378 * Printer has already been registered, modify the current
3382 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3383 "send_ldap_browse: Replacing entry...");
3385 for (i
= 0; i
< 7; i
++)
3387 pmods
[i
] = mods
+ i
;
3388 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3392 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3393 cupsdLogMessage(CUPSD_LOG_ERROR
,
3394 "LDAP modify for %s failed with status %d: %s",
3395 p
->name
, rc
, ldap_err2string(rc
));
3400 * Printer has never been registered, add the current
3404 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3405 "send_ldap_browse: Adding entry...");
3407 for (i
= 0; i
< 7; i
++)
3409 pmods
[i
] = mods
+ i
;
3410 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3414 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3415 cupsdLogMessage(CUPSD_LOG_ERROR
,
3416 "LDAP add for %s failed with status %d: %s",
3417 p
->name
, rc
, ldap_err2string(rc
));
3420 #endif /* HAVE_OPENLDAP */
3425 * 'send_slp_browse()' - Register the specified printer with SLP.
3429 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3431 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3432 attrs
[8192], /* Printer attributes */
3433 finishings
[1024], /* Finishings to support */
3434 make_model
[IPP_MAX_NAME
* 2],
3435 /* Make and model, quoted */
3436 location
[IPP_MAX_NAME
* 2],
3437 /* Location, quoted */
3438 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3439 *src
, /* Pointer to original string */
3440 *dst
; /* Pointer to destination string */
3441 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3442 SLPError error
; /* SLP error, if any */
3445 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3449 * Make the SLP service URL that conforms to the IANA
3450 * 'printer:' template.
3453 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3455 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3458 * Figure out the finishings string...
3461 if (p
->type
& CUPS_PRINTER_STAPLE
)
3462 strcpy(finishings
, "staple");
3464 finishings
[0] = '\0';
3466 if (p
->type
& CUPS_PRINTER_BIND
)
3469 strlcat(finishings
, ",bind", sizeof(finishings
));
3471 strcpy(finishings
, "bind");
3474 if (p
->type
& CUPS_PRINTER_PUNCH
)
3477 strlcat(finishings
, ",punch", sizeof(finishings
));
3479 strcpy(finishings
, "punch");
3482 if (p
->type
& CUPS_PRINTER_COVER
)
3485 strlcat(finishings
, ",cover", sizeof(finishings
));
3487 strcpy(finishings
, "cover");
3490 if (p
->type
& CUPS_PRINTER_SORT
)
3493 strlcat(finishings
, ",sort", sizeof(finishings
));
3495 strcpy(finishings
, "sort");
3499 strcpy(finishings
, "none");
3502 * Quote any commas in the make and model, location, and info strings...
3505 for (src
= p
->make_model
, dst
= make_model
;
3506 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3508 if (*src
== ',' || *src
== '\\' || *src
== ')')
3517 strcpy(make_model
, "Unknown");
3519 for (src
= p
->location
, dst
= location
;
3520 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3522 if (*src
== ',' || *src
== '\\' || *src
== ')')
3531 strcpy(location
, "Unknown");
3533 for (src
= p
->info
, dst
= info
;
3534 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3536 if (*src
== ',' || *src
== '\\' || *src
== ')')
3545 strcpy(info
, "Unknown");
3548 * Get the authentication value...
3551 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3555 * Make the SLP attribute string list that conforms to
3556 * the IANA 'printer:' template.
3559 snprintf(attrs
, sizeof(attrs
),
3560 "(printer-uri-supported=%s),"
3561 "(uri-authentication-supported=%s>),"
3563 "(uri-security-supported=tls>),"
3565 "(uri-security-supported=none>),"
3566 #endif /* HAVE_SSL */
3567 "(printer-name=%s),"
3568 "(printer-location=%s),"
3569 "(printer-info=%s),"
3570 "(printer-more-info=%s),"
3571 "(printer-make-and-model=%s),"
3572 "(printer-type=%d),"
3573 "(charset-supported=utf-8),"
3574 "(natural-language-configured=%s),"
3575 "(natural-language-supported=de,en,es,fr,it),"
3576 "(color-supported=%s),"
3577 "(finishings-supported=%s),"
3578 "(sides-supported=one-sided%s),"
3579 "(multiple-document-jobs-supported=true)"
3580 "(ipp-versions-supported=1.0,1.1)",
3581 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3582 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3583 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3585 p
->type
& CUPS_PRINTER_DUPLEX
?
3586 ",two-sided-long-edge,two-sided-short-edge" : "");
3588 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3591 * Register the printer with the SLP server...
3594 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3595 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3597 if (error
!= SLP_OK
)
3598 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3604 * 'slp_attr_callback()' - SLP attribute callback
3607 static SLPBoolean
/* O - SLP_TRUE for success */
3609 SLPHandle hslp
, /* I - SLP handle */
3610 const char *attrlist
, /* I - Attribute list */
3611 SLPError errcode
, /* I - Parsing status for this attr */
3612 void *cookie
) /* I - Current printer */
3614 char *tmp
= 0; /* Temporary string */
3615 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3616 /* Current printer */
3619 (void)hslp
; /* anti-compiler-warning-code */
3622 * Bail if there was an error
3625 if (errcode
!= SLP_OK
)
3629 * Parse the attrlist to obtain things needed to build CUPS browse packet
3632 memset(p
, 0, sizeof(cupsd_printer_t
));
3634 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3636 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3638 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3640 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3641 p
->type
= atoi(tmp
);
3643 p
->type
= CUPS_PRINTER_REMOTE
;
3645 cupsdClearString(&tmp
);
3652 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3656 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3658 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3661 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3663 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3666 * Make the SLP service URL that conforms to the IANA
3667 * 'printer:' template.
3670 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3673 * Deregister the printer...
3676 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3682 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3685 static int /* O - 0 on success */
3686 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3687 const char *tag
, /* I - Name of attribute */
3688 char **valbuf
) /* O - Value */
3690 char *ptr1
, /* Pointer into string */
3694 cupsdClearString(valbuf
);
3696 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3698 ptr1
+= strlen(tag
);
3700 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3706 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3707 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3710 * Dequote the value...
3713 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3714 if (*ptr1
== '\\' && ptr1
[1])
3715 _cups_strcpy(ptr1
, ptr1
+ 1);
3726 * 'slp_reg_callback()' - Empty SLPRegReport.
3730 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3731 SLPError errcode
, /* I - Error code, if any */
3732 void *cookie
) /* I - App data */
3743 * 'slp_url_callback()' - SLP service url callback
3746 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3748 SLPHandle hslp
, /* I - SLP handle */
3749 const char *srvurl
, /* I - URL of service */
3750 unsigned short lifetime
, /* I - Life of service */
3751 SLPError errcode
, /* I - Existing error code */
3752 void *cookie
) /* I - Pointer to service list */
3754 slpsrvurl_t
*s
, /* New service entry */
3755 **head
; /* Pointer to head of entry */
3759 * Let the compiler know we won't be using these vars...
3766 * Bail if there was an error
3769 if (errcode
!= SLP_OK
)
3773 * Grab the head of the list...
3776 head
= (slpsrvurl_t
**)cookie
;
3779 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3782 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3786 * Copy the SLP service URL...
3789 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3792 * Link the SLP service URL into the head of the list
3802 #endif /* HAVE_LIBSLP */
3806 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3810 update_cups_browse(void)
3812 int i
; /* Looping var */
3813 int auth
; /* Authorization status */
3814 int len
; /* Length of name string */
3815 int bytes
; /* Number of bytes left */
3816 char packet
[1541], /* Broadcast packet */
3817 *pptr
; /* Pointer into packet */
3818 socklen_t srclen
; /* Length of source address */
3819 http_addr_t srcaddr
; /* Source address */
3820 char srcname
[1024]; /* Source hostname */
3821 unsigned address
[4]; /* Source address */
3822 unsigned type
; /* Printer type */
3823 unsigned state
; /* Printer state */
3824 char uri
[HTTP_MAX_URI
], /* Printer URI */
3825 host
[HTTP_MAX_URI
], /* Host portion of URI */
3826 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3827 info
[IPP_MAX_NAME
], /* Information string */
3828 location
[IPP_MAX_NAME
], /* Location string */
3829 make_model
[IPP_MAX_NAME
];/* Make and model string */
3830 int num_attrs
; /* Number of attributes */
3831 cups_option_t
*attrs
; /* Attributes */
3835 * Read a packet from the browse socket...
3838 srclen
= sizeof(srcaddr
);
3839 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3840 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3843 * "Connection refused" is returned under Linux if the destination port
3844 * or address is unreachable from a previous sendto(); check for the
3845 * error here and ignore it for now...
3848 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3850 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3852 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
3855 closesocket(BrowseSocket
);
3857 close(BrowseSocket
);
3860 cupsdRemoveSelect(BrowseSocket
);
3863 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
3864 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
3870 packet
[bytes
] = '\0';
3873 * If we're about to sleep, ignore incoming browse packets.
3880 * Figure out where it came from...
3884 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3886 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3887 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3888 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3889 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3892 #endif /* AF_INET6 */
3897 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3900 if (HostNameLookups
)
3901 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3903 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3905 len
= strlen(srcname
);
3913 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3916 * Access from localhost (127.0.0.1) is always allowed...
3919 auth
= CUPSD_AUTH_ALLOW
;
3924 * Do authorization checks on the domain/address...
3927 switch (BrowseACL
->order_type
)
3930 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
3933 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
3934 auth
= CUPSD_AUTH_ALLOW
;
3936 if (cupsdCheckAuth(address
, srcname
, len
,
3937 BrowseACL
->num_deny
, BrowseACL
->deny
))
3938 auth
= CUPSD_AUTH_DENY
;
3940 if (cupsdCheckAuth(address
, srcname
, len
,
3941 BrowseACL
->num_allow
, BrowseACL
->allow
))
3942 auth
= CUPSD_AUTH_ALLOW
;
3945 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
3946 auth
= CUPSD_AUTH_DENY
;
3948 if (cupsdCheckAuth(address
, srcname
, len
,
3949 BrowseACL
->num_allow
, BrowseACL
->allow
))
3950 auth
= CUPSD_AUTH_ALLOW
;
3952 if (cupsdCheckAuth(address
, srcname
, len
,
3953 BrowseACL
->num_deny
, BrowseACL
->deny
))
3954 auth
= CUPSD_AUTH_DENY
;
3960 auth
= CUPSD_AUTH_ALLOW
;
3962 if (auth
== CUPSD_AUTH_DENY
)
3964 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3965 "update_cups_browse: Refused %d bytes from %s", bytes
,
3970 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3971 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3978 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3980 cupsdLogMessage(CUPSD_LOG_WARN
,
3981 "update_cups_browse: Garbled browse packet - %s", packet
);
3985 strcpy(location
, "Location Unknown");
3986 strcpy(info
, "No Information Available");
3987 make_model
[0] = '\0';
3991 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3994 * Have extended information; can't use sscanf for it because not all
3995 * sscanf's allow empty strings with %[^\"]...
3998 for (i
= 0, pptr
++;
3999 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4001 location
[i
] = *pptr
;
4009 while (*pptr
&& isspace(*pptr
& 255))
4014 for (i
= 0, pptr
++;
4015 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
4024 while (*pptr
&& isspace(*pptr
& 255))
4029 for (i
= 0, pptr
++;
4030 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
4032 make_model
[i
] = *pptr
;
4037 make_model
[i
] = '\0';
4040 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
4046 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
4047 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
4048 type
, state
, uri
, location
, info
, make_model
));
4051 * Pull the URI apart to see if this is a local or remote printer...
4054 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
4056 cupsFreeOptions(num_attrs
, attrs
);
4064 for (i
= 0; i
< NumRelays
; i
++)
4065 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
4066 if (sendto(BrowseSocket
, packet
, bytes
, 0,
4067 (struct sockaddr
*)&(Relays
[i
].to
),
4068 httpAddrLength(&(Relays
[i
].to
))) <= 0)
4070 cupsdLogMessage(CUPSD_LOG_ERROR
,
4071 "update_cups_browse: sendto failed for relay %d - %s.",
4072 i
+ 1, strerror(errno
));
4073 cupsFreeOptions(num_attrs
, attrs
);
4078 * Process the browse data...
4081 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
4082 (ipp_pstate_t
)state
, location
, info
, make_model
,
4088 * 'update_lpd()' - Update the LPD configuration as needed.
4092 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
4099 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
4100 * setting for backwards-compatibility.
4103 if (onoff
&& !get_hostconfig("CUPS_LPD"))
4105 #endif /* __APPLE__ */
4107 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
4110 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
4113 char newfile
[1024]; /* New cups-lpd.N file */
4114 cups_file_t
*ofp
, /* Original file pointer */
4115 *nfp
; /* New file pointer */
4116 char line
[1024]; /* Line from file */
4119 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
4121 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
4123 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
4124 LPDConfigFile
+ 9, strerror(errno
));
4128 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4130 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4131 newfile
, strerror(errno
));
4137 * Copy all of the lines from the cups-lpd file...
4140 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4144 cupsFilePrintf(nfp
, "%s\n", line
);
4145 snprintf(line
, sizeof(line
), "\tdisable = %s",
4146 onoff
? "no" : "yes");
4148 else if (!strstr(line
, "disable ="))
4149 cupsFilePrintf(nfp
, "%s\n", line
);
4154 rename(newfile
, LPDConfigFile
+ 9);
4157 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
4160 * Enable/disable LPD via the launchctl command...
4163 char *argv
[5], /* Arguments for command */
4164 *envp
[MAX_ENV
]; /* Environment for command */
4165 int pid
; /* Process ID */
4168 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4169 argv
[0] = (char *)"launchctl";
4170 argv
[1] = (char *)(onoff
? "load" : "unload");
4171 argv
[2] = (char *)"-w";
4172 argv
[3] = LPDConfigFile
+ 10;
4175 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
4178 #endif /* __APPLE__ */
4180 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
4185 * 'update_polling()' - Read status messages from the poll daemons.
4189 update_polling(void)
4191 char *ptr
, /* Pointer to end of line in buffer */
4192 message
[1024]; /* Pointer to message text */
4193 int loglevel
; /* Log level for message */
4196 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
4197 message
, sizeof(message
))) != NULL
)
4198 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
4201 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
4204 * All polling processes have died; stop polling...
4207 cupsdLogMessage(CUPSD_LOG_ERROR
,
4208 "update_polling: all polling processes have exited!");
4215 * 'update_smb()' - Update the SMB configuration as needed.
4219 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
4224 if (!strncmp(SMBConfigFile
, "samba:///", 9))
4227 * Enable/disable SMB via the specified smb.conf config file...
4230 char newfile
[1024]; /* New smb.conf.N file */
4231 cups_file_t
*ofp
, /* Original file pointer */
4232 *nfp
; /* New file pointer */
4233 char line
[1024]; /* Line from file */
4234 int in_printers
; /* In [printers] section? */
4237 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
4239 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
4241 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
4242 SMBConfigFile
+ 8, strerror(errno
));
4246 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4248 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4249 newfile
, strerror(errno
));
4255 * Copy all of the lines from the smb.conf file...
4260 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4262 if (in_printers
&& strstr(line
, "printable ="))
4263 snprintf(line
, sizeof(line
), " printable = %s",
4264 onoff
? "yes" : "no");
4266 cupsFilePrintf(nfp
, "%s\n", line
);
4269 in_printers
= !strcmp(line
, "[printers]");
4274 rename(newfile
, SMBConfigFile
+ 8);
4277 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
4282 * End of "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $".