2 * "$Id: dirsvc.c 6649 2007-07-11 21:46:42Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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
18 * local printer and remove any pending
19 * references to remote printers.
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer update the broadcast contents.
23 * cupsdSaveRemoteCache() - Save the remote printer cache.
24 * cupsdSendBrowseList() - Send new browsing information as necessary.
25 * cupsdStartBrowsing() - Start sending and receiving broadcast
27 * cupsdStartPolling() - Start polling servers as needed.
28 * cupsdStopBrowsing() - Stop sending and receiving broadcast
30 * cupsdStopPolling() - Stop polling servers as needed.
31 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
32 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
33 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
34 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
35 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
37 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the
39 * dnssdRegisterCallback() - DNSServiceRegister callback.
40 * dnssdRegisterPrinter() - Start sending broadcast information for a
41 * printer or update the broadcast contents.
42 * dequote() - Remote quotes from a string.
43 * process_browse_data() - Process new browse data.
44 * process_implicit_classes() - Create/update implicit classes as needed.
45 * send_cups_browse() - Send new browsing information using the
47 * send_ldap_browse() - Send LDAP printer registrations.
48 * send_slp_browse() - Register the specified printer with SLP.
49 * slp_attr_callback() - SLP attribute callback
50 * slp_dereg_printer() - SLPDereg() the specified printer
51 * slp_get_attr() - Get an attribute from an SLP registration.
52 * slp_reg_callback() - Empty SLPRegReport.
53 * slp_url_callback() - SLP service url callback
54 * update_cups_browse() - Update the browse lists using the CUPS
56 * update_polling() - Read status messages from the poll daemons.
60 * Include necessary headers...
70 # ifdef HAVE_COREFOUNDATION
71 # include <CoreFoundation/CoreFoundation.h>
72 # endif /* HAVE_COREFOUNDATION */
73 # ifdef HAVE_SYSTEMCONFIGURATION
74 # include <SystemConfiguration/SystemConfiguration.h>
75 # endif /* HAVE_SYSTEMCONFIGURATION */
76 #endif /* HAVE_DNSSD */
83 static char *dequote(char *d
, const char *s
, int dlen
);
84 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
85 char *resource
, int resourcelen
);
86 static void process_browse_data(const char *uri
, const char *host
,
87 const char *resource
, cups_ptype_t type
,
88 ipp_pstate_t state
, const char *location
,
89 const char *info
, const char *make_model
,
90 int num_attrs
, cups_option_t
*attrs
);
91 static void process_implicit_classes(void);
92 static void send_cups_browse(cupsd_printer_t
*p
);
94 static void send_ldap_browse(cupsd_printer_t
*p
);
95 #endif /* HAVE_LDAP */
97 static void send_slp_browse(cupsd_printer_t
*p
);
98 #endif /* HAVE_LIBSLP */
99 static void update_cups_browse(void);
100 static void update_polling(void);
104 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
106 "printerDescription",
108 "printerMakeAndModel",
113 #endif /* HAVE_OPENLDAP */
121 * SLP service name for CUPS...
124 # define SLP_CUPS_SRVTYPE "service:printer"
125 # define SLP_CUPS_SRVLEN 15
129 * Printer service URL structure
132 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
134 struct _slpsrvurl_s
*next
; /* Next URL in list */
135 char url
[HTTP_MAX_URI
];
144 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
145 SLPError errcode
, void *cookie
);
146 static void slp_dereg_printer(cupsd_printer_t
*p
);
147 static int slp_get_attr(const char *attrlist
, const char *tag
,
149 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
151 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
152 unsigned short lifetime
,
153 SLPError errcode
, void *cookie
);
154 #endif /* HAVE_LIBSLP */
158 * For IPP register using a subtype of 'cups' so that shared printer browsing
159 * only finds other CUPS servers (not all IPP based printers).
161 static char dnssdIPPRegType
[] = "_ipp._tcp,_cups";
162 static char dnssdIPPFaxRegType
[] = "_fax-ipp._tcp";
164 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
);
165 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
166 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
168 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
169 DNSServiceFlags flags
,
170 DNSServiceErrorType errorCode
,
171 const char *name
, const char *regtype
,
172 const char *domain
, void *context
);
173 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
174 #endif /* HAVE_DNSSD */
178 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
179 * local printer and remove any pending
180 * references to remote printers.
184 cupsdDeregisterPrinter(
185 cupsd_printer_t
*p
, /* I - Printer to register */
186 int removeit
) /* I - Printer being permanently removed */
189 * Only deregister if browsing is enabled and it's a local printers...
192 if (!Browsing
|| !p
->shared
||
193 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
197 * Announce the deletion...
200 if ((BrowseLocalProtocols
& BROWSE_CUPS
))
202 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
204 p
->type
|= CUPS_PRINTER_DELETE
;
212 if (BrowseLocalProtocols
& BROWSE_SLP
)
213 slp_dereg_printer(p
);
214 #endif /* HAVE_LIBSLP */
217 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
))
218 dnssdDeregisterPrinter(p
);
219 #endif /* HAVE_DNSSD */
224 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
228 cupsdLoadRemoteCache(void)
230 cups_file_t
*fp
; /* remote.cache file */
231 int linenum
; /* Current line number */
232 char line
[1024], /* Line from file */
233 *value
, /* Pointer to value */
234 *valueptr
, /* Pointer into value */
235 scheme
[32], /* Scheme portion of URI */
236 username
[64], /* Username portion of URI */
238 /* Hostname portion of URI */
239 resource
[HTTP_MAX_URI
];
240 /* Resource portion of URI */
241 int port
; /* Port number */
242 cupsd_printer_t
*p
; /* Current printer */
243 time_t now
; /* Current time */
247 * Don't load the cache if the CUPS remote protocol is disabled...
250 if (!Browsing
|| !(BrowseRemoteProtocols
& BROWSE_CUPS
))
252 cupsdLogMessage(CUPSD_LOG_DEBUG
,
253 "cupsdLoadRemoteCache: Not loading remote cache.");
258 * Open the remote.cache file...
261 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
262 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
266 * Read printer configurations until we hit EOF...
273 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
276 * Decode the directive...
279 if (!strcasecmp(line
, "<Printer") ||
280 !strcasecmp(line
, "<DefaultPrinter"))
283 * <Printer name> or <DefaultPrinter name>
286 if (p
== NULL
&& value
)
289 * Add the printer and a base file type...
292 cupsdLogMessage(CUPSD_LOG_DEBUG
,
293 "cupsdLoadRemoteCache: Loading printer %s...", value
);
295 if ((p
= cupsdFindDest(value
)) != NULL
)
297 if (p
->type
& CUPS_PRINTER_CLASS
)
299 cupsdLogMessage(CUPSD_LOG_WARN
,
300 "Cached remote printer \"%s\" conflicts with "
308 p
= cupsdAddPrinter(value
);
311 p
->state
= IPP_PRINTER_IDLE
;
312 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
313 p
->browse_time
= now
;
314 p
->browse_expire
= now
+ BrowseTimeout
;
317 * Set the default printer as needed...
320 if (!strcasecmp(line
, "<DefaultPrinter"))
325 cupsdLogMessage(CUPSD_LOG_ERROR
,
326 "Syntax error on line %d of remote.cache.", linenum
);
330 else if (!strcasecmp(line
, "<Class") ||
331 !strcasecmp(line
, "<DefaultClass"))
334 * <Class name> or <DefaultClass name>
337 if (p
== NULL
&& value
)
340 * Add the printer and a base file type...
343 cupsdLogMessage(CUPSD_LOG_DEBUG
,
344 "cupsdLoadRemoteCache: Loading class %s...", value
);
346 if ((p
= cupsdFindDest(value
)) != NULL
)
347 p
->type
= CUPS_PRINTER_CLASS
;
349 p
= cupsdAddClass(value
);
352 p
->state
= IPP_PRINTER_IDLE
;
353 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
354 p
->browse_time
= now
;
355 p
->browse_expire
= now
+ BrowseTimeout
;
358 * Set the default printer as needed...
361 if (!strcasecmp(line
, "<DefaultClass"))
366 cupsdLogMessage(CUPSD_LOG_ERROR
,
367 "Syntax error on line %d of remote.cache.", linenum
);
371 else if (!strcasecmp(line
, "</Printer>") ||
372 !strcasecmp(line
, "</Class>"))
377 * Close out the current printer...
380 cupsdSetPrinterAttrs(p
);
386 cupsdLogMessage(CUPSD_LOG_ERROR
,
387 "Syntax error on line %d of remote.cache.", linenum
);
393 cupsdLogMessage(CUPSD_LOG_ERROR
,
394 "Syntax error on line %d of remote.cache.", linenum
);
397 else if (!strcasecmp(line
, "Info"))
400 cupsdSetString(&p
->info
, value
);
402 else if (!strcasecmp(line
, "MakeModel"))
405 cupsdSetString(&p
->make_model
, value
);
407 else if (!strcasecmp(line
, "Location"))
410 cupsdSetString(&p
->location
, value
);
412 else if (!strcasecmp(line
, "DeviceURI"))
416 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
417 username
, sizeof(username
), host
, sizeof(host
), &port
,
418 resource
, sizeof(resource
));
420 cupsdSetString(&p
->hostname
, host
);
421 cupsdSetString(&p
->uri
, value
);
422 cupsdSetString(&p
->device_uri
, value
);
426 cupsdLogMessage(CUPSD_LOG_ERROR
,
427 "Syntax error on line %d of remote.cache.", linenum
);
431 else if (!strcasecmp(line
, "Option") && value
)
437 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
440 cupsdLogMessage(CUPSD_LOG_ERROR
,
441 "Syntax error on line %d of remote.cache.", linenum
);
444 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
446 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
450 else if (!strcasecmp(line
, "State"))
453 * Set the initial queue state...
456 if (value
&& !strcasecmp(value
, "idle"))
457 p
->state
= IPP_PRINTER_IDLE
;
458 else if (value
&& !strcasecmp(value
, "stopped"))
459 p
->state
= IPP_PRINTER_STOPPED
;
462 cupsdLogMessage(CUPSD_LOG_ERROR
,
463 "Syntax error on line %d of remote.cache.", linenum
);
467 else if (!strcasecmp(line
, "StateMessage"))
470 * Set the initial queue state message...
474 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
476 else if (!strcasecmp(line
, "Accepting"))
479 * Set the initial accepting state...
483 (!strcasecmp(value
, "yes") ||
484 !strcasecmp(value
, "on") ||
485 !strcasecmp(value
, "true")))
488 (!strcasecmp(value
, "no") ||
489 !strcasecmp(value
, "off") ||
490 !strcasecmp(value
, "false")))
494 cupsdLogMessage(CUPSD_LOG_ERROR
,
495 "Syntax error on line %d of remote.cache.", linenum
);
499 else if (!strcasecmp(line
, "Type"))
502 p
->type
= atoi(value
);
505 cupsdLogMessage(CUPSD_LOG_ERROR
,
506 "Syntax error on line %d of remote.cache.", linenum
);
510 else if (!strcasecmp(line
, "BrowseTime"))
514 time_t t
= atoi(value
);
516 if (t
> p
->browse_expire
)
517 p
->browse_expire
= t
;
521 cupsdLogMessage(CUPSD_LOG_ERROR
,
522 "Syntax error on line %d of remote.cache.", linenum
);
526 else if (!strcasecmp(line
, "JobSheets"))
529 * Set the initial job sheets...
534 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
539 cupsdSetString(&p
->job_sheets
[0], value
);
541 while (isspace(*valueptr
& 255))
546 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
551 cupsdSetString(&p
->job_sheets
[1], value
);
556 cupsdLogMessage(CUPSD_LOG_ERROR
,
557 "Syntax error on line %d of remote.cache.", linenum
);
561 else if (!strcasecmp(line
, "AllowUser"))
566 cupsdAddPrinterUser(p
, value
);
570 cupsdLogMessage(CUPSD_LOG_ERROR
,
571 "Syntax error on line %d of remote.cache.", linenum
);
575 else if (!strcasecmp(line
, "DenyUser"))
580 cupsdAddPrinterUser(p
, value
);
584 cupsdLogMessage(CUPSD_LOG_ERROR
,
585 "Syntax error on line %d of remote.cache.", linenum
);
592 * Something else we don't understand...
595 cupsdLogMessage(CUPSD_LOG_ERROR
,
596 "Unknown configuration directive %s on line %d of remote.cache.",
604 * Do auto-classing if needed...
607 process_implicit_classes();
612 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
613 * printer or update the broadcast contents.
617 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
619 if (!Browsing
|| !BrowseLocalProtocols
|| !BrowseInterval
|| !NumBrowsers
||
620 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
624 /* if (BrowseLocalProtocols & BROWSE_SLP)
625 slpRegisterPrinter(p); */
626 #endif /* HAVE_LIBSLP */
629 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
630 dnssdRegisterPrinter(p
);
631 #endif /* HAVE_DNSSD */
636 * 'cupsdRestartPolling()' - Restart polling servers as needed.
640 cupsdRestartPolling(void)
642 int i
; /* Looping var */
643 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
646 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
648 kill(pollp
->pid
, SIGHUP
);
653 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
657 cupsdSaveRemoteCache(void)
659 int i
; /* Looping var */
660 cups_file_t
*fp
; /* printers.conf file */
661 char temp
[1024]; /* Temporary string */
662 cupsd_printer_t
*printer
; /* Current printer class */
663 time_t curtime
; /* Current time */
664 struct tm
*curdate
; /* Current date */
665 cups_option_t
*option
; /* Current option */
669 * Create the remote.cache file...
672 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
674 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
676 cupsdLogMessage(CUPSD_LOG_ERROR
,
677 "Unable to save remote.cache - %s", strerror(errno
));
681 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
684 * Restrict access to the file...
687 fchown(cupsFileNumber(fp
), getuid(), Group
);
688 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
691 * Write a small header to the file...
694 curtime
= time(NULL
);
695 curdate
= localtime(&curtime
);
696 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
698 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
699 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
702 * Write each local printer known to the system...
705 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
707 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
710 * Skip local destinations...
713 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
717 * Write printers as needed...
720 if (printer
== DefaultPrinter
)
721 cupsFilePuts(fp
, "<Default");
723 cupsFilePutChar(fp
, '<');
725 if (printer
->type
& CUPS_PRINTER_CLASS
)
726 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
728 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
730 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
732 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
735 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
737 if (printer
->make_model
)
738 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
740 if (printer
->location
)
741 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
743 if (printer
->device_uri
)
744 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
746 if (printer
->state
== IPP_PRINTER_STOPPED
)
748 cupsFilePuts(fp
, "State Stopped\n");
749 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
752 cupsFilePuts(fp
, "State Idle\n");
754 if (printer
->accepting
)
755 cupsFilePuts(fp
, "Accepting Yes\n");
757 cupsFilePuts(fp
, "Accepting No\n");
759 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
760 printer
->job_sheets
[1]);
762 for (i
= 0; i
< printer
->num_users
; i
++)
763 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
766 for (i
= printer
->num_options
, option
= printer
->options
;
769 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
771 if (printer
->type
& CUPS_PRINTER_CLASS
)
772 cupsFilePuts(fp
, "</Class>\n");
774 cupsFilePuts(fp
, "</Printer>\n");
782 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
786 cupsdSendBrowseList(void)
788 int count
; /* Number of dests to update */
789 cupsd_printer_t
*p
; /* Current printer */
790 time_t ut
, /* Minimum update time */
791 to
; /* Timeout time */
794 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
798 * Compute the update and timeout times...
802 ut
= to
- BrowseInterval
;
805 * Figure out how many printers need an update...
808 if (BrowseInterval
> 0)
810 int max_count
; /* Maximum number to update */
814 * Throttle the number of printers we'll be updating this time
815 * around based on the number of queues that need updating and
816 * the maximum number of queues to update each second...
819 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
821 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
822 count
< max_count
&& p
!= NULL
;
823 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
824 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
825 p
->shared
&& p
->browse_time
< ut
)
829 * Loop through all of the printers and send local updates as needed...
833 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
835 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
839 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
842 * Check for wraparound...
846 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
850 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
853 else if (p
->browse_time
< ut
)
856 * Need to send an update...
861 p
->browse_time
= time(NULL
);
863 if (BrowseLocalProtocols
& BROWSE_CUPS
)
867 if (BrowseLocalProtocols
& BROWSE_SLP
)
869 #endif /* HAVE_LIBSLP */
872 if (BrowseLocalProtocols
& BROWSE_LDAP
)
874 #endif /* HAVE_LDAP */
879 * Save where we left off so that all printers get updated...
886 * Loop through all of the printers and send local updates as needed...
889 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
891 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
894 * If this is a remote queue, see if it needs to be timed out...
897 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
899 if (p
->browse_expire
< to
)
901 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
902 "%s \'%s\' deleted by directory services (timeout).",
903 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
906 cupsdLogMessage(CUPSD_LOG_DEBUG
,
907 "Remote destination \"%s\" has timed out; "
911 cupsArraySave(Printers
);
912 cupsdDeletePrinter(p
, 1);
913 cupsArrayRestore(Printers
);
921 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
925 cupsdStartBrowsing(void)
927 int val
; /* Socket option value */
928 struct sockaddr_in addr
; /* Broadcast address */
929 cupsd_printer_t
*p
; /* Current printer */
934 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
937 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
939 if (BrowseSocket
< 0)
942 * Create the broadcast socket...
945 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
947 cupsdLogMessage(CUPSD_LOG_ERROR
,
948 "Unable to create broadcast socket - %s.",
950 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
951 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
956 * Bind the socket to browse port...
959 memset(&addr
, 0, sizeof(addr
));
960 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
961 addr
.sin_family
= AF_INET
;
962 addr
.sin_port
= htons(BrowsePort
);
964 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
966 cupsdLogMessage(CUPSD_LOG_ERROR
,
967 "Unable to bind broadcast socket - %s.",
971 closesocket(BrowseSocket
);
977 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
978 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
984 * Set the "broadcast" flag...
988 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
990 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
994 closesocket(BrowseSocket
);
1000 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1001 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1006 * Close the socket on exec...
1009 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1012 * Finally, add the socket to the input selection set as needed...
1015 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1018 * We only listen if we want remote printers...
1021 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1029 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1032 * Open SLP handle...
1035 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1037 cupsdLogMessage(CUPSD_LOG_ERROR
,
1038 "Unable to open an SLP handle; disabling SLP browsing!");
1039 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1040 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1043 BrowseSLPRefresh
= 0;
1046 BrowseSLPHandle
= NULL
;
1047 #endif /* HAVE_LIBSLP */
1049 #ifdef HAVE_OPENLDAP
1050 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1054 cupsdLogMessage(CUPSD_LOG_ERROR
,
1055 "Need to set BrowseLDAPDN to use LDAP browsing!");
1056 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1057 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1062 * Open LDAP handle...
1065 int rc
; /* LDAP API status */
1066 int version
= 3; /* LDAP version */
1067 struct berval bv
= {0, ""}; /* SASL bind value */
1071 * Set the certificate file to use for encrypted LDAP sessions...
1074 if (BrowseLDAPCACertFile
)
1076 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1077 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1078 BrowseLDAPCACertFile
);
1080 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1081 (void *)BrowseLDAPCACertFile
))
1083 cupsdLogMessage(CUPSD_LOG_ERROR
,
1084 "Unable to set CA certificate file for LDAP "
1085 "connections: %d - %s", rc
, ldap_err2string(rc
));
1089 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1092 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1093 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1095 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1097 if (rc
!= LDAP_SUCCESS
)
1099 cupsdLogMessage(CUPSD_LOG_ERROR
,
1100 "Unable to initialize LDAP; disabling LDAP browsing!");
1101 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1102 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1104 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1105 (const void *)&version
) != LDAP_SUCCESS
)
1107 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1108 BrowseLDAPHandle
= NULL
;
1109 cupsdLogMessage(CUPSD_LOG_ERROR
,
1110 "Unable to set LDAP protocol version; "
1111 "disabling LDAP browsing!");
1112 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1113 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1117 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1118 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1121 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1122 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1124 if (rc
!= LDAP_SUCCESS
)
1126 cupsdLogMessage(CUPSD_LOG_ERROR
,
1127 "Unable to bind to LDAP server; "
1128 "disabling LDAP browsing!");
1129 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1130 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1131 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1136 BrowseLDAPRefresh
= 0;
1138 #endif /* HAVE_OPENLDAP */
1141 * Register the individual printers
1144 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1146 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1147 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1148 cupsdRegisterPrinter(p
);
1153 * 'cupsdStartPolling()' - Start polling servers as needed.
1157 cupsdStartPolling(void)
1159 int i
; /* Looping var */
1160 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1161 char polld
[1024]; /* Poll daemon path */
1162 char sport
[255]; /* Server port */
1163 char bport
[255]; /* Browser port */
1164 char interval
[255]; /* Poll interval */
1165 int statusfds
[2]; /* Status pipe */
1166 char *argv
[6]; /* Arguments */
1167 char *envp
[100]; /* Environment */
1171 * Don't do anything if we aren't polling...
1174 if (NumPolled
== 0 || BrowseSocket
< 0)
1177 PollStatusBuffer
= NULL
;
1182 * Setup string arguments for polld, port and interval options.
1185 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1187 sprintf(bport
, "%d", BrowsePort
);
1190 sprintf(interval
, "%d", BrowseInterval
);
1192 strcpy(interval
, "30");
1194 argv
[0] = "cups-polld";
1200 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1203 * Create a pipe that receives the status messages from each
1207 if (cupsdOpenPipe(statusfds
))
1209 cupsdLogMessage(CUPSD_LOG_ERROR
,
1210 "Unable to create polling status pipes - %s.",
1213 PollStatusBuffer
= NULL
;
1217 PollPipe
= statusfds
[0];
1218 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1221 * Run each polling daemon, redirecting stderr to the polling pipe...
1224 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1226 sprintf(sport
, "%d", pollp
->port
);
1228 argv
[1] = pollp
->hostname
;
1230 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1231 0, &(pollp
->pid
)) < 0)
1233 cupsdLogMessage(CUPSD_LOG_ERROR
,
1234 "cupsdStartPolling: Unable to fork polling daemon - %s",
1240 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1241 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1242 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1245 close(statusfds
[1]);
1248 * Finally, add the pipe to the input selection set...
1251 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1256 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1260 cupsdStopBrowsing(void)
1262 cupsd_printer_t
*p
; /* Current printer */
1265 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1269 * De-register the individual printers
1272 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1274 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1275 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1276 cupsdDeregisterPrinter(p
, 1);
1279 * Shut down browsing sockets...
1282 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1286 * Close the socket and remove it from the input selection set.
1290 closesocket(BrowseSocket
);
1292 close(BrowseSocket
);
1295 cupsdRemoveSelect(BrowseSocket
);
1300 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1304 * Close SLP handle...
1307 SLPClose(BrowseSLPHandle
);
1308 BrowseSLPHandle
= NULL
;
1310 #endif /* HAVE_LIBSLP */
1312 #ifdef HAVE_OPENLDAP
1313 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1316 ldap_unbind(BrowseLDAPHandle
);
1317 BrowseLDAPHandle
= NULL
;
1319 #endif /* HAVE_OPENLDAP */
1324 * 'cupsdStopPolling()' - Stop polling servers as needed.
1328 cupsdStopPolling(void)
1330 int i
; /* Looping var */
1331 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1336 cupsdStatBufDelete(PollStatusBuffer
);
1339 cupsdRemoveSelect(PollPipe
);
1342 PollStatusBuffer
= NULL
;
1345 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1347 cupsdEndProcess(pollp
->pid
, 0);
1353 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1357 cupsdUpdateDNSSDBrowse(
1358 cupsd_printer_t
*p
) /* I - Printer being queried */
1360 DNSServiceErrorType sdErr
; /* Service discovery error */
1363 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1364 != kDNSServiceErr_NoError
)
1366 cupsdLogMessage(CUPSD_LOG_ERROR
,
1367 "DNS Service Discovery registration error %d for \"%s\"!",
1369 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1370 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1372 p
->dnssd_ipp_ref
= NULL
;
1373 p
->dnssd_ipp_fd
= -1;
1376 #endif /* HAVE_DNSSD */
1379 #ifdef HAVE_OPENLDAP
1381 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1385 cupsdUpdateLDAPBrowse(void)
1387 char uri
[HTTP_MAX_URI
], /* Printer URI */
1388 host
[HTTP_MAX_URI
], /* Hostname */
1389 resource
[HTTP_MAX_URI
], /* Resource path */
1390 location
[1024], /* Printer location */
1391 info
[1024], /* Printer information */
1392 make_model
[1024], /* Printer make and model */
1393 **value
; /* Holds the returned data from LDAP */
1394 int type
; /* Printer type */
1395 int rc
; /* LDAP status */
1396 int limit
; /* Size limit */
1397 LDAPMessage
*res
, /* LDAP search results */
1398 *e
; /* Current entry from search */
1402 * Search for printers...
1405 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1407 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1409 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1410 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1411 if (rc
!= LDAP_SUCCESS
)
1413 cupsdLogMessage(CUPSD_LOG_ERROR
,
1414 "LDAP search returned error %d: %s", rc
,
1415 ldap_err2string(rc
));
1419 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1420 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1425 * Loop through the available printers...
1428 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1430 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1433 * Get the required values from this entry...
1436 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1437 "printerDescription")) == NULL
)
1440 strlcpy(info
, *value
, sizeof(info
));
1441 ldap_value_free(value
);
1443 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1444 "printerLocation")) == NULL
)
1447 strlcpy(location
, *value
, sizeof(location
));
1448 ldap_value_free(value
);
1450 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1451 "printerMakeAndModel")) == NULL
)
1454 strlcpy(make_model
, *value
, sizeof(make_model
));
1455 ldap_value_free(value
);
1457 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1458 "printerType")) == NULL
)
1461 type
= atoi(*value
);
1462 ldap_value_free(value
);
1464 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1465 "printerURI")) == NULL
)
1468 strlcpy(uri
, *value
, sizeof(uri
));
1469 ldap_value_free(value
);
1472 * Process the entry as browse data...
1475 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1476 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1477 location
, info
, make_model
, 0, NULL
);
1481 #endif /* HAVE_OPENLDAP */
1486 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1490 cupsdUpdateSLPBrowse(void)
1492 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1493 *next
; /* Next service in list */
1494 cupsd_printer_t p
; /* Printer information */
1495 const char *uri
; /* Pointer to printer URI */
1496 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1497 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1501 * Reset the refresh time...
1504 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1507 * Poll for remote printers using SLP...
1512 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1513 slp_url_callback
, &s
);
1516 * Loop through the list of available printers...
1522 * Save the "next" pointer...
1528 * Load a cupsd_printer_t structure with the SLP service attributes...
1531 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1534 * Process this printer entry...
1537 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1539 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1542 * Pull the URI apart to see if this is a local or remote printer...
1545 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1546 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1547 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1551 * Free this listing...
1554 cupsdClearString(&p
.info
);
1555 cupsdClearString(&p
.location
);
1556 cupsdClearString(&p
.make_model
);
1561 #endif /* HAVE_LIBSLP */
1565 * 'dequote()' - Remote quotes from a string.
1568 static char * /* O - Dequoted string */
1569 dequote(char *d
, /* I - Destination string */
1570 const char *s
, /* I - Source string */
1571 int dlen
) /* I - Destination length */
1573 char *dptr
; /* Pointer into destination */
1578 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1595 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1598 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1599 is_local_queue(const char *uri
, /* I - Printer URI */
1600 char *host
, /* O - Host string */
1601 int hostlen
, /* I - Length of host buffer */
1602 char *resource
, /* O - Resource string */
1603 int resourcelen
) /* I - Length of resource buffer */
1605 char scheme
[32], /* Scheme portion of URI */
1606 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1607 int port
; /* Port portion of URI */
1608 cupsd_netif_t
*iface
; /* Network interface */
1612 * Pull the URI apart to see if this is a local or remote printer...
1615 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1616 username
, sizeof(username
), host
, hostlen
, &port
,
1617 resource
, resourcelen
) < HTTP_URI_OK
)
1620 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1623 * Check for local server addresses...
1626 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1631 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1633 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1634 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1638 * If we get here, the printer is remote...
1646 * 'process_browse_data()' - Process new browse data.
1650 process_browse_data(
1651 const char *uri
, /* I - URI of printer/class */
1652 const char *host
, /* I - Hostname */
1653 const char *resource
, /* I - Resource path */
1654 cups_ptype_t type
, /* I - Printer type */
1655 ipp_pstate_t state
, /* I - Printer state */
1656 const char *location
, /* I - Printer location */
1657 const char *info
, /* I - Printer information */
1658 const char *make_model
, /* I - Printer make and model */
1659 int num_attrs
, /* I - Number of attributes */
1660 cups_option_t
*attrs
) /* I - Attributes */
1662 int i
; /* Looping var */
1663 int update
; /* Update printer attributes? */
1664 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1665 name
[IPP_MAX_NAME
], /* Name of printer */
1666 newname
[IPP_MAX_NAME
], /* New name of printer */
1667 *hptr
, /* Pointer into hostname */
1668 *sptr
; /* Pointer into ServerName */
1669 char local_make_model
[IPP_MAX_NAME
];
1670 /* Local make and model */
1671 cupsd_printer_t
*p
; /* Printer information */
1672 const char *ipp_options
, /* ipp-options value */
1673 *lease_duration
; /* lease-duration value */
1677 * Determine if the URI contains any illegal characters in it...
1680 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1681 (strncmp(resource
, "/printers/", 10) &&
1682 strncmp(resource
, "/classes/", 9)))
1684 cupsdLogMessage(CUPSD_LOG_ERROR
,
1685 "process_browse_data: Bad printer URI in browse data: %s",
1690 if (strchr(resource
, '?') ||
1691 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1692 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1694 cupsdLogMessage(CUPSD_LOG_ERROR
,
1695 "process_browse_data: Bad resource in browse data: %s",
1701 * OK, this isn't a local printer; add any remote options...
1704 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1706 if (BrowseRemoteOptions
)
1708 if (BrowseRemoteOptions
[0] == '?')
1711 * Override server-supplied options...
1714 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1716 else if (ipp_options
)
1719 * Combine the server and local options...
1722 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1723 BrowseRemoteOptions
);
1728 * Just use the local options...
1731 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1736 else if (ipp_options
)
1739 * Just use the server-supplied options...
1742 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1747 * See if we already have it listed in the Printers list, and add it if not...
1750 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
1751 type
&= ~CUPS_PRINTER_IMPLICIT
;
1753 hptr
= strchr(host
, '.');
1754 sptr
= strchr(ServerName
, '.');
1756 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1759 * Strip the common domain name components...
1762 while (hptr
!= NULL
)
1764 if (!strcasecmp(hptr
, sptr
))
1770 hptr
= strchr(hptr
+ 1, '.');
1774 if (type
& CUPS_PRINTER_CLASS
)
1777 * Remote destination is a class...
1780 if (!strncmp(resource
, "/classes/", 9))
1781 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1786 *hptr
= '.'; /* Resource FQDN */
1788 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1790 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1792 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1795 * Nope, this isn't the same host; if the hostname isn't the local host,
1796 * add it to the other class and then find a class using the full host
1800 if (p
->type
& CUPS_PRINTER_REMOTE
)
1802 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1803 "Renamed remote class \"%s\" to \"%s@%s\"...",
1804 p
->name
, p
->name
, p
->hostname
);
1805 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1806 "Class \'%s\' deleted by directory services.",
1809 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1810 cupsdRenamePrinter(p
, newname
);
1812 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1813 "Class \'%s\' added by directory services.",
1819 else if (!p
->hostname
)
1822 * Hostname not set, so this must be a cached remote printer
1823 * that was created for a pending print job...
1826 cupsdSetString(&p
->hostname
, host
);
1827 cupsdSetString(&p
->uri
, uri
);
1828 cupsdSetString(&p
->device_uri
, uri
);
1835 * Use the short name for this shared class.
1838 strlcpy(name
, resource
+ 9, sizeof(name
));
1841 else if (p
&& !p
->hostname
)
1844 * Hostname not set, so this must be a cached remote printer
1845 * that was created for a pending print job...
1848 cupsdSetString(&p
->hostname
, host
);
1849 cupsdSetString(&p
->uri
, uri
);
1850 cupsdSetString(&p
->device_uri
, uri
);
1857 * Class doesn't exist; add it...
1860 p
= cupsdAddClass(name
);
1862 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1864 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1865 "Class \'%s\' added by directory services.", name
);
1868 * Force the URI to point to the real server...
1871 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1873 cupsdSetString(&p
->uri
, uri
);
1874 cupsdSetString(&p
->device_uri
, uri
);
1875 cupsdSetString(&p
->hostname
, host
);
1883 * Remote destination is a printer...
1886 if (!strncmp(resource
, "/printers/", 10))
1887 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1892 *hptr
= '.'; /* Resource FQDN */
1894 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
1896 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
1898 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1901 * Nope, this isn't the same host; if the hostname isn't the local host,
1902 * add it to the other printer and then find a printer using the full host
1906 if (p
->type
& CUPS_PRINTER_REMOTE
)
1908 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1909 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1910 p
->name
, p
->name
, p
->hostname
);
1911 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1912 "Printer \'%s\' deleted by directory services.",
1915 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1916 cupsdRenamePrinter(p
, newname
);
1918 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1919 "Printer \'%s\' added by directory services.",
1925 else if (!p
->hostname
)
1928 * Hostname not set, so this must be a cached remote printer
1929 * that was created for a pending print job...
1932 cupsdSetString(&p
->hostname
, host
);
1933 cupsdSetString(&p
->uri
, uri
);
1934 cupsdSetString(&p
->device_uri
, uri
);
1941 * Use the short name for this shared printer.
1944 strlcpy(name
, resource
+ 10, sizeof(name
));
1947 else if (p
&& !p
->hostname
)
1950 * Hostname not set, so this must be a cached remote printer
1951 * that was created for a pending print job...
1954 cupsdSetString(&p
->hostname
, host
);
1955 cupsdSetString(&p
->uri
, uri
);
1956 cupsdSetString(&p
->device_uri
, uri
);
1963 * Printer doesn't exist; add it...
1966 p
= cupsdAddPrinter(name
);
1968 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1969 "Printer \'%s\' added by directory services.", name
);
1971 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
1974 * Force the URI to point to the real server...
1977 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1979 cupsdSetString(&p
->hostname
, host
);
1980 cupsdSetString(&p
->uri
, uri
);
1981 cupsdSetString(&p
->device_uri
, uri
);
1988 * Update the state...
1992 p
->browse_time
= time(NULL
);
1994 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
1998 * Grab the lease-duration for the browse data; anything less then 1
1999 * second or more than 1 week gets the default BrowseTimeout...
2002 i
= atoi(lease_duration
);
2003 if (i
< 1 || i
> 604800)
2006 p
->browse_expire
= p
->browse_time
+ i
;
2009 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2011 if (type
& CUPS_PRINTER_REJECTING
)
2013 type
&= ~CUPS_PRINTER_REJECTING
;
2021 else if (!p
->accepting
)
2027 if (p
->type
!= type
)
2033 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2035 cupsdSetString(&p
->location
, location
);
2039 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2041 cupsdSetString(&p
->info
, info
);
2045 if (!make_model
|| !make_model
[0])
2047 if (type
& CUPS_PRINTER_CLASS
)
2048 snprintf(local_make_model
, sizeof(local_make_model
),
2049 "Remote Class on %s", host
);
2051 snprintf(local_make_model
, sizeof(local_make_model
),
2052 "Remote Printer on %s", host
);
2055 snprintf(local_make_model
, sizeof(local_make_model
),
2056 "%s on %s", make_model
, host
);
2058 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2060 cupsdSetString(&p
->make_model
, local_make_model
);
2066 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2069 * See if we need to update the attributes...
2072 if (p
->num_options
!= num_attrs
)
2076 for (i
= 0; i
< num_attrs
; i
++)
2077 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2078 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2079 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2088 * Free the old options...
2091 cupsFreeOptions(p
->num_options
, p
->options
);
2094 p
->num_options
= num_attrs
;
2097 if (type
& CUPS_PRINTER_DELETE
)
2099 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2100 "%s \'%s\' deleted by directory services.",
2101 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2103 cupsdExpireSubscriptions(p
, NULL
);
2105 cupsdDeletePrinter(p
, 1);
2106 cupsdUpdateImplicitClasses();
2110 cupsdSetPrinterAttrs(p
);
2111 cupsdUpdateImplicitClasses();
2115 * See if we have a default printer... If not, make the first network
2116 * default printer the default.
2119 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2122 * Find the first network default printer and use it...
2125 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2127 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2128 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2136 * Do auto-classing if needed...
2139 process_implicit_classes();
2142 * Update the printcap file...
2145 cupsdWritePrintcap();
2151 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2154 static char * /* O - TXT record */
2155 dnssdBuildTxtRecord(
2156 int *txt_len
, /* O - TXT record length */
2157 cupsd_printer_t
*p
) /* I - Printer information */
2159 int i
, j
; /* Looping vars */
2160 char type_str
[32], /* Type to string buffer */
2161 state_str
[32], /* State to string buffer */
2162 rp_str
[1024], /* Queue name string buffer */
2163 air_str
[1024], /* auth-info-required string buffer */
2164 *keyvalue
[32][2]; /* Table of key/value pairs */
2168 * Load up the key value pairs...
2173 keyvalue
[i
][0] = "txtvers";
2174 keyvalue
[i
++][1] = "1";
2176 keyvalue
[i
][0] = "qtotal";
2177 keyvalue
[i
++][1] = "1";
2179 keyvalue
[i
][0] = "rp";
2180 keyvalue
[i
++][1] = rp_str
;
2181 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2182 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2184 keyvalue
[i
][0] = "ty";
2185 keyvalue
[i
++][1] = p
->make_model
;
2187 if (p
->location
&& *p
->location
!= '\0')
2189 keyvalue
[i
][0] = "note";
2190 keyvalue
[i
++][1] = p
->location
;
2193 keyvalue
[i
][0] = "product";
2194 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2196 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2197 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2199 keyvalue
[i
][0] = "printer-state";
2200 keyvalue
[i
++][1] = state_str
;
2202 keyvalue
[i
][0] = "printer-type";
2203 keyvalue
[i
++][1] = type_str
;
2205 keyvalue
[i
][0] = "Transparent";
2206 keyvalue
[i
++][1] = "T";
2208 keyvalue
[i
][0] = "Binary";
2209 keyvalue
[i
++][1] = "T";
2211 if ((p
->type
& CUPS_PRINTER_FAX
))
2213 keyvalue
[i
][0] = "Fax";
2214 keyvalue
[i
++][1] = "T";
2217 if ((p
->type
& CUPS_PRINTER_COLOR
))
2219 keyvalue
[i
][0] = "Color";
2220 keyvalue
[i
++][1] = "T";
2223 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2225 keyvalue
[i
][0] = "Duplex";
2226 keyvalue
[i
++][1] = "T";
2229 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2231 keyvalue
[i
][0] = "Staple";
2232 keyvalue
[i
++][1] = "T";
2235 if ((p
->type
& CUPS_PRINTER_COPIES
))
2237 keyvalue
[i
][0] = "Copies";
2238 keyvalue
[i
++][1] = "T";
2241 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2243 keyvalue
[i
][0] = "Collate";
2244 keyvalue
[i
++][1] = "T";
2247 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2249 keyvalue
[i
][0] = "Punch";
2250 keyvalue
[i
++][1] = "T";
2253 if ((p
->type
& CUPS_PRINTER_BIND
))
2255 keyvalue
[i
][0] = "Bind";
2256 keyvalue
[i
++][1] = "T";
2259 if ((p
->type
& CUPS_PRINTER_SORT
))
2261 keyvalue
[i
][0] = "Sort";
2262 keyvalue
[i
++][1] = "T";
2265 keyvalue
[i
][0] = "pdl";
2266 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2268 if (p
->num_auth_info_required
)
2270 char *air
= air_str
; /* Pointer into string */
2273 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2275 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2281 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2285 keyvalue
[i
][0] = "air";
2286 keyvalue
[i
++][1] = air
;
2290 * Then pack them into a proper txt record...
2293 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2298 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2303 dnssdDeregisterPrinter(
2304 cupsd_printer_t
*p
) /* I - Printer */
2306 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2309 * Closing the socket deregisters the service
2312 if (p
->dnssd_ipp_ref
)
2314 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2315 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2316 p
->dnssd_ipp_ref
= NULL
;
2317 p
->dnssd_ipp_fd
= -1;
2320 cupsdClearString(&p
->reg_name
);
2325 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2328 free(p
->txt_record
);
2329 p
->txt_record
= NULL
;
2335 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2336 * TXT record format.
2339 static char * /* O - TXT record */
2340 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2341 char *keyvalue
[][2], /* I - Table of key value pairs */
2342 int count
) /* I - Items in table */
2344 int i
; /* Looping var */
2345 int length
; /* Length of TXT record */
2346 int length2
; /* Length of value */
2347 char *txtRecord
; /* TXT record buffer */
2348 char *cursor
; /* Looping pointer */
2352 * Calculate the buffer size
2355 for (length
= i
= 0; i
< count
; i
++)
2356 length
+= 1 + strlen(keyvalue
[i
][0]) +
2357 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2360 * Allocate and fill it
2363 txtRecord
= malloc(length
);
2368 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2371 * Drop in the p-string style length byte followed by the data
2374 length
= strlen(keyvalue
[i
][0]);
2375 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2377 *cursor
++ = (unsigned char)(length
+ length2
);
2379 memcpy(cursor
, keyvalue
[i
][0], length
);
2386 memcpy(cursor
, keyvalue
[i
][1], length2
);
2397 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2401 dnssdRegisterCallback(
2402 DNSServiceRef sdRef
, /* I - DNS Service reference */
2403 DNSServiceFlags flags
, /* I - Reserved for future use */
2404 DNSServiceErrorType errorCode
, /* I - Error code */
2405 const char *name
, /* I - Service name */
2406 const char *regtype
, /* I - Service type */
2407 const char *domain
, /* I - Domain. ".local" for now */
2408 void *context
) /* I - User-defined context */
2412 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2413 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2417 cupsdLogMessage(CUPSD_LOG_ERROR
,
2418 "DNSServiceRegister failed with error %d", (int)errorCode
);
2425 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2426 * or update the broadcast contents.
2430 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2432 DNSServiceErrorType se
; /* dnssd errors */
2433 cupsd_listener_t
*lis
; /* Current listening socket */
2434 char *txt_record
, /* TXT record buffer */
2435 *name
; /* Service name */
2436 int txt_len
, /* TXT record length */
2437 port
; /* IPP port number */
2438 char str_buffer
[1024];
2439 /* C-string buffer */
2440 const char *computerName
; /* Computer name c-string ptr */
2441 const char *regtype
; /* Registration type */
2442 #ifdef HAVE_COREFOUNDATION_H
2443 CFStringRef computerNameRef
;/* Computer name CFString */
2444 CFStringEncoding nameEncoding
; /* Computer name encoding */
2445 CFMutableStringRef shortNameRef
; /* Mutable name string */
2446 CFIndex nameLength
; /* Name string length */
2448 int nameLength
; /* Name string length */
2449 #endif /* HAVE_COREFOUNDATION_H */
2452 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2453 !p
->dnssd_ipp_ref
? "new" : "update");
2456 * If per-printer sharing was just disabled make sure we're not
2457 * registered before returning.
2462 dnssdDeregisterPrinter(p
);
2467 * Get the computer name as a c-string...
2470 #ifdef HAVE_COREFOUNDATION_H
2471 computerName
= NULL
;
2472 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2473 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2474 kCFStringEncodingUTF8
)) == NULL
)
2475 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2476 kCFStringEncodingUTF8
))
2477 computerName
= str_buffer
;
2479 computerName
= ServerName
;
2480 #endif /* HAVE_COREFOUNDATION_H */
2483 * The registered name takes the form of "<printer-info> @ <computer name>"...
2488 cupsdSetStringf(&name
, "%s @ %s",
2489 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2492 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2494 #ifdef HAVE_COREFOUNDATION_H
2495 if (computerNameRef
)
2496 CFRelease(computerNameRef
);
2497 #endif /* HAVE_COREFOUNDATION_H */
2500 * If an existing printer was renamed, unregister it and start over...
2503 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2504 dnssdDeregisterPrinter(p
);
2506 txt_len
= 0; /* anti-compiler-warning-code */
2507 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2509 if (!p
->dnssd_ipp_ref
)
2512 * Initial registration...
2515 cupsdSetString(&p
->reg_name
, name
);
2519 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2521 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2523 if (lis
->address
.addr
.sa_family
== AF_INET
)
2525 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2528 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2530 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2536 * Use the _fax subtype for fax queues...
2539 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2542 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) type is \"%s\"",
2545 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2546 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2547 dnssdRegisterCallback
, p
);
2550 * In case the name is too long, try shortening the string one character
2554 if (se
== kDNSServiceErr_BadParam
)
2556 #ifdef HAVE_COREFOUNDATION_H
2557 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2559 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2560 nameLength
= CFStringGetLength(shortNameRef
);
2562 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2564 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2565 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2566 kCFStringEncodingUTF8
))
2568 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2569 regtype
, NULL
, NULL
, htons(port
),
2570 txt_len
, txt_record
,
2571 dnssdRegisterCallback
, p
);
2575 CFRelease(shortNameRef
);
2578 nameLength
= strlen(name
);
2579 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2581 name
[--nameLength
] = '\0';
2582 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2583 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2584 dnssdRegisterCallback
, p
);
2586 #endif /* HAVE_COREFOUNDATION_H */
2589 if (se
== kDNSServiceErr_NoError
)
2591 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2592 p
->txt_record
= txt_record
;
2593 p
->txt_len
= txt_len
;
2596 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2600 cupsdLogMessage(CUPSD_LOG_WARN
,
2601 "DNS-SD registration of \"%s\" failed with %d",
2604 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2607 * Update the existing registration...
2610 /* A TTL of 0 means use record's original value (Radar 3176248) */
2611 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2612 txt_len
, txt_record
, 0);
2615 free(p
->txt_record
);
2617 p
->txt_record
= txt_record
;
2618 p
->txt_len
= txt_len
;
2625 cupsdClearString(&name
);
2627 #endif /* HAVE_DNSSD */
2631 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2635 process_implicit_classes(void)
2637 int i
; /* Looping var */
2638 int update
; /* Update printer attributes? */
2639 char name
[IPP_MAX_NAME
], /* Name of printer */
2640 *hptr
; /* Pointer into hostname */
2641 cupsd_printer_t
*p
, /* Printer information */
2642 *pclass
, /* Printer class */
2643 *first
; /* First printer in class */
2644 int offset
, /* Offset of name */
2645 len
; /* Length of name */
2648 if (!ImplicitClasses
|| !Printers
)
2652 * Loop through all available printers and create classes as needed...
2655 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2656 update
= 0, pclass
= NULL
, first
= NULL
;
2658 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2661 * Skip implicit classes...
2664 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2671 * If len == 0, get the length of this printer name up to the "@"
2675 cupsArraySave(Printers
);
2678 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2679 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2682 * We have more than one printer with the same name; see if
2683 * we have a class, and if this printer is a member...
2686 if (pclass
&& strcasecmp(pclass
->name
, name
))
2689 cupsdSetPrinterAttrs(pclass
);
2695 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2698 * Need to add the class...
2701 pclass
= cupsdAddPrinter(name
);
2702 cupsArrayAdd(ImplicitPrinters
, pclass
);
2704 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2705 pclass
->accepting
= 1;
2706 pclass
->state
= IPP_PRINTER_IDLE
;
2708 cupsdSetString(&pclass
->location
, p
->location
);
2709 cupsdSetString(&pclass
->info
, p
->info
);
2711 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2712 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2716 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2718 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2719 "Implicit class \'%s\' added by directory services.",
2725 for (i
= 0; i
< pclass
->num_printers
; i
++)
2726 if (pclass
->printers
[i
] == first
)
2729 if (i
>= pclass
->num_printers
)
2731 first
->in_implicit_class
= 1;
2732 cupsdAddPrinterToClass(pclass
, first
);
2738 for (i
= 0; i
< pclass
->num_printers
; i
++)
2739 if (pclass
->printers
[i
] == p
)
2742 if (i
>= pclass
->num_printers
)
2744 p
->in_implicit_class
= 1;
2745 cupsdAddPrinterToClass(pclass
, p
);
2752 * First time around; just get name length and mark it as first
2756 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2757 len
= hptr
- p
->name
;
2759 len
= strlen(p
->name
);
2761 strncpy(name
, p
->name
, len
);
2765 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2766 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2769 * Can't use same name as a local printer; add "Any" to the
2770 * front of the name, unless we have explicitly disabled
2771 * the "ImplicitAnyClasses"...
2774 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2777 * Add "Any" to the class name...
2780 strcpy(name
, "Any");
2781 strncpy(name
+ 3, p
->name
, len
);
2782 name
[len
+ 3] = '\0';
2788 * Don't create an implicit class if we have a local printer
2789 * with the same name...
2793 cupsArrayRestore(Printers
);
2801 cupsArrayRestore(Printers
);
2805 * Update the last printer class as needed...
2808 if (pclass
&& update
)
2809 cupsdSetPrinterAttrs(pclass
);
2814 * 'send_cups_browse()' - Send new browsing information using the CUPS
2819 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2821 int i
; /* Looping var */
2822 cups_ptype_t type
; /* Printer type */
2823 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2824 int bytes
; /* Length of packet */
2825 char packet
[1453], /* Browse data packet */
2826 uri
[1024], /* Printer URI */
2827 location
[1024], /* printer-location */
2828 info
[1024], /* printer-info */
2830 /* printer-make-and-model */
2831 cupsd_netif_t
*iface
; /* Network interface */
2835 * Figure out the printer type value...
2838 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2841 type
|= CUPS_PRINTER_REJECTING
;
2843 if (p
== DefaultPrinter
)
2844 type
|= CUPS_PRINTER_DEFAULT
;
2847 * Remove quotes from printer-info, printer-location, and
2848 * printer-make-and-model attributes...
2851 dequote(location
, p
->location
, sizeof(location
));
2852 dequote(info
, p
->info
, sizeof(info
));
2855 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2856 else if (p
->type
& CUPS_PRINTER_CLASS
)
2858 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2859 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2861 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2864 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2866 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2869 * Send a packet to each browse address...
2872 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2876 * Send the browse packet to one or more interfaces...
2879 if (!strcmp(b
->iface
, "*"))
2882 * Send to all local interfaces...
2887 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2889 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2892 * Only send to local, IPv4 interfaces...
2895 if (!iface
->is_local
|| !iface
->port
||
2896 iface
->address
.addr
.sa_family
!= AF_INET
)
2899 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2900 iface
->hostname
, iface
->port
,
2901 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2904 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2905 type
, p
->state
, uri
, location
, info
, make_model
,
2906 p
->browse_attrs
? p
->browse_attrs
: "");
2908 bytes
= strlen(packet
);
2910 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2911 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2912 iface
->name
, packet
);
2914 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2916 sendto(BrowseSocket
, packet
, bytes
, 0,
2917 (struct sockaddr
*)&(iface
->broadcast
),
2918 httpAddrLength(&(iface
->broadcast
)));
2921 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2924 * Send to the named interface using the IPv4 address...
2928 if (strcmp(b
->iface
, iface
->name
))
2933 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2936 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2940 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2941 iface
->hostname
, iface
->port
,
2942 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2945 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2946 type
, p
->state
, uri
, location
, info
, make_model
,
2947 p
->browse_attrs
? p
->browse_attrs
: "");
2949 bytes
= strlen(packet
);
2951 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2952 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2953 iface
->name
, packet
);
2955 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2957 sendto(BrowseSocket
, packet
, bytes
, 0,
2958 (struct sockaddr
*)&(iface
->broadcast
),
2959 httpAddrLength(&(iface
->broadcast
)));
2966 * Send the browse packet to the indicated address using
2967 * the default server name...
2970 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2971 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2972 p
->browse_attrs
? p
->browse_attrs
: "");
2974 bytes
= strlen(packet
);
2975 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2976 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2978 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2979 (struct sockaddr
*)&(b
->to
),
2980 httpAddrLength(&(b
->to
))) <= 0)
2983 * Unable to send browse packet, so remove this address from the
2987 cupsdLogMessage(CUPSD_LOG_ERROR
,
2988 "cupsdSendBrowseList: sendto failed for browser "
2990 (int)(b
- Browsers
+ 1), strerror(errno
));
2993 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3002 #ifdef HAVE_OPENLDAP
3004 * 'send_ldap_browse()' - Send LDAP printer registrations.
3008 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3010 int i
; /* Looping var... */
3011 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3012 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3013 LDAPMessage
*res
; /* Search result token */
3014 char *cn_value
[2], /* Change records */
3020 typestring
[255], /* String to hold printer-type */
3021 filter
[256], /* Search filter for possible UPDATEs */
3022 dn
[1024]; /* DN of the printer we are adding */
3023 int rc
; /* LDAP status */
3024 static const char * const objectClass_values
[] =
3025 { /* The 3 objectClass's we use in */
3026 "top", /* our LDAP entries */
3032 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3035 * Everything in ldap is ** so we fudge around it...
3038 sprintf(typestring
, "%u", p
->type
);
3040 cn_value
[0] = p
->name
;
3042 info
[0] = p
->info
? p
->info
: "Unknown";
3044 location
[0] = p
->location
? p
->location
: "Unknown";
3046 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3047 make_model
[1] = NULL
;
3048 type
[0] = typestring
;
3053 snprintf(filter
, sizeof(filter
),
3054 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3056 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3057 filter
, (char **)ldap_attrs
, 0, &res
);
3058 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3061 mods
[0].mod_type
= "cn";
3062 mods
[0].mod_values
= cn_value
;
3063 mods
[1].mod_type
= "printerDescription";
3064 mods
[1].mod_values
= info
;
3065 mods
[2].mod_type
= "printerURI";
3066 mods
[2].mod_values
= uri
;
3067 mods
[3].mod_type
= "printerLocation";
3068 mods
[3].mod_values
= location
;
3069 mods
[4].mod_type
= "printerMakeAndModel";
3070 mods
[4].mod_values
= make_model
;
3071 mods
[5].mod_type
= "printerType";
3072 mods
[5].mod_values
= type
;
3073 mods
[6].mod_type
= "objectClass";
3074 mods
[6].mod_values
= (char **)objectClass_values
;
3076 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3077 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3079 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3082 * Printer has already been registered, modify the current
3086 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3087 "send_ldap_browse: Replacing entry...");
3089 for (i
= 0; i
< 7; i
++)
3091 pmods
[i
] = mods
+ i
;
3092 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3096 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3097 cupsdLogMessage(CUPSD_LOG_ERROR
,
3098 "LDAP modify for %s failed with status %d: %s",
3099 p
->name
, rc
, ldap_err2string(rc
));
3104 * Printer has never been registered, add the current
3108 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3109 "send_ldap_browse: Adding entry...");
3111 for (i
= 0; i
< 7; i
++)
3113 pmods
[i
] = mods
+ i
;
3114 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3118 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3119 cupsdLogMessage(CUPSD_LOG_ERROR
,
3120 "LDAP add for %s failed with status %d: %s",
3121 p
->name
, rc
, ldap_err2string(rc
));
3124 #endif /* HAVE_OPENLDAP */
3129 * 'send_slp_browse()' - Register the specified printer with SLP.
3133 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3135 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3136 attrs
[8192], /* Printer attributes */
3137 finishings
[1024], /* Finishings to support */
3138 make_model
[IPP_MAX_NAME
* 2],
3139 /* Make and model, quoted */
3140 location
[IPP_MAX_NAME
* 2],
3141 /* Location, quoted */
3142 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3143 *src
, /* Pointer to original string */
3144 *dst
; /* Pointer to destination string */
3145 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3146 SLPError error
; /* SLP error, if any */
3149 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3153 * Make the SLP service URL that conforms to the IANA
3154 * 'printer:' template.
3157 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3159 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3162 * Figure out the finishings string...
3165 if (p
->type
& CUPS_PRINTER_STAPLE
)
3166 strcpy(finishings
, "staple");
3168 finishings
[0] = '\0';
3170 if (p
->type
& CUPS_PRINTER_BIND
)
3173 strlcat(finishings
, ",bind", sizeof(finishings
));
3175 strcpy(finishings
, "bind");
3178 if (p
->type
& CUPS_PRINTER_PUNCH
)
3181 strlcat(finishings
, ",punch", sizeof(finishings
));
3183 strcpy(finishings
, "punch");
3186 if (p
->type
& CUPS_PRINTER_COVER
)
3189 strlcat(finishings
, ",cover", sizeof(finishings
));
3191 strcpy(finishings
, "cover");
3194 if (p
->type
& CUPS_PRINTER_SORT
)
3197 strlcat(finishings
, ",sort", sizeof(finishings
));
3199 strcpy(finishings
, "sort");
3203 strcpy(finishings
, "none");
3206 * Quote any commas in the make and model, location, and info strings...
3209 for (src
= p
->make_model
, dst
= make_model
;
3210 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3212 if (*src
== ',' || *src
== '\\' || *src
== ')')
3221 strcpy(make_model
, "Unknown");
3223 for (src
= p
->location
, dst
= location
;
3224 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3226 if (*src
== ',' || *src
== '\\' || *src
== ')')
3235 strcpy(location
, "Unknown");
3237 for (src
= p
->info
, dst
= info
;
3238 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3240 if (*src
== ',' || *src
== '\\' || *src
== ')')
3249 strcpy(info
, "Unknown");
3252 * Get the authentication value...
3255 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3259 * Make the SLP attribute string list that conforms to
3260 * the IANA 'printer:' template.
3263 snprintf(attrs
, sizeof(attrs
),
3264 "(printer-uri-supported=%s),"
3265 "(uri-authentication-supported=%s>),"
3267 "(uri-security-supported=tls>),"
3269 "(uri-security-supported=none>),"
3270 #endif /* HAVE_SSL */
3271 "(printer-name=%s),"
3272 "(printer-location=%s),"
3273 "(printer-info=%s),"
3274 "(printer-more-info=%s),"
3275 "(printer-make-and-model=%s),"
3276 "(printer-type=%d),"
3277 "(charset-supported=utf-8),"
3278 "(natural-language-configured=%s),"
3279 "(natural-language-supported=de,en,es,fr,it),"
3280 "(color-supported=%s),"
3281 "(finishings-supported=%s),"
3282 "(sides-supported=one-sided%s),"
3283 "(multiple-document-jobs-supported=true)"
3284 "(ipp-versions-supported=1.0,1.1)",
3285 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3286 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3287 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3289 p
->type
& CUPS_PRINTER_DUPLEX
?
3290 ",two-sided-long-edge,two-sided-short-edge" : "");
3292 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3295 * Register the printer with the SLP server...
3298 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3299 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3301 if (error
!= SLP_OK
)
3302 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3308 * 'slp_attr_callback()' - SLP attribute callback
3311 static SLPBoolean
/* O - SLP_TRUE for success */
3313 SLPHandle hslp
, /* I - SLP handle */
3314 const char *attrlist
, /* I - Attribute list */
3315 SLPError errcode
, /* I - Parsing status for this attr */
3316 void *cookie
) /* I - Current printer */
3318 char *tmp
= 0; /* Temporary string */
3319 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3320 /* Current printer */
3323 (void)hslp
; /* anti-compiler-warning-code */
3326 * Bail if there was an error
3329 if (errcode
!= SLP_OK
)
3333 * Parse the attrlist to obtain things needed to build CUPS browse packet
3336 memset(p
, 0, sizeof(cupsd_printer_t
));
3338 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3340 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3342 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3344 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3345 p
->type
= atoi(tmp
);
3347 p
->type
= CUPS_PRINTER_REMOTE
;
3349 cupsdClearString(&tmp
);
3356 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3360 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3362 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3365 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3367 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3370 * Make the SLP service URL that conforms to the IANA
3371 * 'printer:' template.
3374 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3377 * Deregister the printer...
3380 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3386 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3389 static int /* O - 0 on success */
3390 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3391 const char *tag
, /* I - Name of attribute */
3392 char **valbuf
) /* O - Value */
3394 char *ptr1
, /* Pointer into string */
3398 cupsdClearString(valbuf
);
3400 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3402 ptr1
+= strlen(tag
);
3404 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3410 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3411 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3414 * Dequote the value...
3417 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3418 if (*ptr1
== '\\' && ptr1
[1])
3419 _cups_strcpy(ptr1
, ptr1
+ 1);
3430 * 'slp_reg_callback()' - Empty SLPRegReport.
3434 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3435 SLPError errcode
, /* I - Error code, if any */
3436 void *cookie
) /* I - App data */
3447 * 'slp_url_callback()' - SLP service url callback
3450 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3452 SLPHandle hslp
, /* I - SLP handle */
3453 const char *srvurl
, /* I - URL of service */
3454 unsigned short lifetime
, /* I - Life of service */
3455 SLPError errcode
, /* I - Existing error code */
3456 void *cookie
) /* I - Pointer to service list */
3458 slpsrvurl_t
*s
, /* New service entry */
3459 **head
; /* Pointer to head of entry */
3463 * Let the compiler know we won't be using these vars...
3470 * Bail if there was an error
3473 if (errcode
!= SLP_OK
)
3477 * Grab the head of the list...
3480 head
= (slpsrvurl_t
**)cookie
;
3483 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3486 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3490 * Copy the SLP service URL...
3493 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3496 * Link the SLP service URL into the head of the list
3506 #endif /* HAVE_LIBSLP */
3510 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3514 update_cups_browse(void)
3516 int i
; /* Looping var */
3517 int auth
; /* Authorization status */
3518 int len
; /* Length of name string */
3519 int bytes
; /* Number of bytes left */
3520 char packet
[1541], /* Broadcast packet */
3521 *pptr
; /* Pointer into packet */
3522 socklen_t srclen
; /* Length of source address */
3523 http_addr_t srcaddr
; /* Source address */
3524 char srcname
[1024]; /* Source hostname */
3525 unsigned address
[4]; /* Source address */
3526 unsigned type
; /* Printer type */
3527 unsigned state
; /* Printer state */
3528 char uri
[HTTP_MAX_URI
], /* Printer URI */
3529 host
[HTTP_MAX_URI
], /* Host portion of URI */
3530 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3531 info
[IPP_MAX_NAME
], /* Information string */
3532 location
[IPP_MAX_NAME
], /* Location string */
3533 make_model
[IPP_MAX_NAME
];/* Make and model string */
3534 int num_attrs
; /* Number of attributes */
3535 cups_option_t
*attrs
; /* Attributes */
3539 * Read a packet from the browse socket...
3542 srclen
= sizeof(srcaddr
);
3543 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3544 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3547 * "Connection refused" is returned under Linux if the destination port
3548 * or address is unreachable from a previous sendto(); check for the
3549 * error here and ignore it for now...
3552 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3554 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3556 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
3558 cupsdStopBrowsing();
3565 packet
[bytes
] = '\0';
3568 * If we're about to sleep, ignore incoming browse packets.
3575 * Figure out where it came from...
3579 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3581 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3582 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3583 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3584 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3587 #endif /* AF_INET6 */
3592 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3595 if (HostNameLookups
)
3596 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3598 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3600 len
= strlen(srcname
);
3608 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3611 * Access from localhost (127.0.0.1) is always allowed...
3619 * Do authorization checks on the domain/address...
3622 switch (BrowseACL
->order_type
)
3625 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
3628 case AUTH_ALLOW
: /* Order Deny,Allow */
3631 if (cupsdCheckAuth(address
, srcname
, len
,
3632 BrowseACL
->num_deny
, BrowseACL
->deny
))
3635 if (cupsdCheckAuth(address
, srcname
, len
,
3636 BrowseACL
->num_allow
, BrowseACL
->allow
))
3640 case AUTH_DENY
: /* Order Allow,Deny */
3643 if (cupsdCheckAuth(address
, srcname
, len
,
3644 BrowseACL
->num_allow
, BrowseACL
->allow
))
3647 if (cupsdCheckAuth(address
, srcname
, len
,
3648 BrowseACL
->num_deny
, BrowseACL
->deny
))
3657 if (auth
== AUTH_DENY
)
3659 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3660 "update_cups_browse: Refused %d bytes from %s", bytes
,
3665 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3666 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3673 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3675 cupsdLogMessage(CUPSD_LOG_WARN
,
3676 "update_cups_browse: Garbled browse packet - %s", packet
);
3680 strcpy(location
, "Location Unknown");
3681 strcpy(info
, "No Information Available");
3682 make_model
[0] = '\0';
3686 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3689 * Have extended information; can't use sscanf for it because not all
3690 * sscanf's allow empty strings with %[^\"]...
3693 for (i
= 0, pptr
++;
3694 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3696 location
[i
] = *pptr
;
3704 while (*pptr
&& isspace(*pptr
& 255))
3709 for (i
= 0, pptr
++;
3710 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3719 while (*pptr
&& isspace(*pptr
& 255))
3724 for (i
= 0, pptr
++;
3725 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3727 make_model
[i
] = *pptr
;
3732 make_model
[i
] = '\0';
3735 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3741 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3742 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3743 type
, state
, uri
, location
, info
, make_model
));
3746 * Pull the URI apart to see if this is a local or remote printer...
3749 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
3751 cupsFreeOptions(num_attrs
, attrs
);
3759 for (i
= 0; i
< NumRelays
; i
++)
3760 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
3761 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3762 (struct sockaddr
*)&(Relays
[i
].to
),
3763 httpAddrLength(&(Relays
[i
].to
))) <= 0)
3765 cupsdLogMessage(CUPSD_LOG_ERROR
,
3766 "update_cups_browse: sendto failed for relay %d - %s.",
3767 i
+ 1, strerror(errno
));
3768 cupsFreeOptions(num_attrs
, attrs
);
3773 * Process the browse data...
3776 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
3777 (ipp_pstate_t
)state
, location
, info
, make_model
,
3783 * 'update_polling()' - Read status messages from the poll daemons.
3787 update_polling(void)
3789 char *ptr
, /* Pointer to end of line in buffer */
3790 message
[1024]; /* Pointer to message text */
3791 int loglevel
; /* Log level for message */
3794 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
3795 message
, sizeof(message
))) != NULL
)
3796 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
3799 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
3802 * All polling processes have died; stop polling...
3805 cupsdLogMessage(CUPSD_LOG_ERROR
,
3806 "update_polling: all polling processes have exited!");
3813 * End of "$Id: dirsvc.c 6649 2007-07-11 21:46:42Z mike $".