2 * "$Id: dirsvc.c 6691 2007-07-19 19:09:46Z 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 /* __APPLE__ */
77 #endif /* HAVE_DNSSD */
84 static char *dequote(char *d
, const char *s
, int dlen
);
85 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
86 char *resource
, int resourcelen
);
87 static void process_browse_data(const char *uri
, const char *host
,
88 const char *resource
, cups_ptype_t type
,
89 ipp_pstate_t state
, const char *location
,
90 const char *info
, const char *make_model
,
91 int num_attrs
, cups_option_t
*attrs
);
92 static void process_implicit_classes(void);
93 static void send_cups_browse(cupsd_printer_t
*p
);
95 static void send_ldap_browse(cupsd_printer_t
*p
);
96 #endif /* HAVE_LDAP */
98 static void send_slp_browse(cupsd_printer_t
*p
);
99 #endif /* HAVE_LIBSLP */
100 static void update_cups_browse(void);
101 static void update_polling(void);
105 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
107 "printerDescription",
109 "printerMakeAndModel",
114 #endif /* HAVE_OPENLDAP */
122 * SLP service name for CUPS...
125 # define SLP_CUPS_SRVTYPE "service:printer"
126 # define SLP_CUPS_SRVLEN 15
130 * Printer service URL structure
133 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
135 struct _slpsrvurl_s
*next
; /* Next URL in list */
136 char url
[HTTP_MAX_URI
];
145 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
146 SLPError errcode
, void *cookie
);
147 static void slp_dereg_printer(cupsd_printer_t
*p
);
148 static int slp_get_attr(const char *attrlist
, const char *tag
,
150 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
152 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
153 unsigned short lifetime
,
154 SLPError errcode
, void *cookie
);
155 #endif /* HAVE_LIBSLP */
159 * For IPP register using a subtype of 'cups' so that shared printer browsing
160 * only finds other CUPS servers (not all IPP based printers).
162 static char dnssdIPPRegType
[] = "_ipp._tcp,_cups";
163 static char dnssdIPPFaxRegType
[] = "_fax-ipp._tcp";
165 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
);
166 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
167 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
169 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
170 DNSServiceFlags flags
,
171 DNSServiceErrorType errorCode
,
172 const char *name
, const char *regtype
,
173 const char *domain
, void *context
);
174 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
175 #endif /* HAVE_DNSSD */
179 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
180 * local printer and remove any pending
181 * references to remote printers.
185 cupsdDeregisterPrinter(
186 cupsd_printer_t
*p
, /* I - Printer to register */
187 int removeit
) /* I - Printer being permanently removed */
190 * Only deregister if browsing is enabled and it's a local printers...
193 if (!Browsing
|| !p
->shared
||
194 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
198 * Announce the deletion...
201 if ((BrowseLocalProtocols
& BROWSE_CUPS
))
203 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
205 p
->type
|= CUPS_PRINTER_DELETE
;
213 if (BrowseLocalProtocols
& BROWSE_SLP
)
214 slp_dereg_printer(p
);
215 #endif /* HAVE_LIBSLP */
218 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
))
219 dnssdDeregisterPrinter(p
);
220 #endif /* HAVE_DNSSD */
225 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
229 cupsdLoadRemoteCache(void)
231 cups_file_t
*fp
; /* remote.cache file */
232 int linenum
; /* Current line number */
233 char line
[1024], /* Line from file */
234 *value
, /* Pointer to value */
235 *valueptr
, /* Pointer into value */
236 scheme
[32], /* Scheme portion of URI */
237 username
[64], /* Username portion of URI */
239 /* Hostname portion of URI */
240 resource
[HTTP_MAX_URI
];
241 /* Resource portion of URI */
242 int port
; /* Port number */
243 cupsd_printer_t
*p
; /* Current printer */
244 time_t now
; /* Current time */
248 * Don't load the cache if the CUPS remote protocol is disabled...
251 if (!Browsing
|| !(BrowseRemoteProtocols
& BROWSE_CUPS
))
253 cupsdLogMessage(CUPSD_LOG_DEBUG
,
254 "cupsdLoadRemoteCache: Not loading remote cache.");
259 * Open the remote.cache file...
262 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
263 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
267 * Read printer configurations until we hit EOF...
274 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
277 * Decode the directive...
280 if (!strcasecmp(line
, "<Printer") ||
281 !strcasecmp(line
, "<DefaultPrinter"))
284 * <Printer name> or <DefaultPrinter name>
287 if (p
== NULL
&& value
)
290 * Add the printer and a base file type...
293 cupsdLogMessage(CUPSD_LOG_DEBUG
,
294 "cupsdLoadRemoteCache: Loading printer %s...", value
);
296 if ((p
= cupsdFindDest(value
)) != NULL
)
298 if (p
->type
& CUPS_PRINTER_CLASS
)
300 cupsdLogMessage(CUPSD_LOG_WARN
,
301 "Cached remote printer \"%s\" conflicts with "
309 p
= cupsdAddPrinter(value
);
312 p
->state
= IPP_PRINTER_IDLE
;
313 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
314 p
->browse_time
= now
;
315 p
->browse_expire
= now
+ BrowseTimeout
;
318 * Set the default printer as needed...
321 if (!strcasecmp(line
, "<DefaultPrinter"))
326 cupsdLogMessage(CUPSD_LOG_ERROR
,
327 "Syntax error on line %d of remote.cache.", linenum
);
331 else if (!strcasecmp(line
, "<Class") ||
332 !strcasecmp(line
, "<DefaultClass"))
335 * <Class name> or <DefaultClass name>
338 if (p
== NULL
&& value
)
341 * Add the printer and a base file type...
344 cupsdLogMessage(CUPSD_LOG_DEBUG
,
345 "cupsdLoadRemoteCache: Loading class %s...", value
);
347 if ((p
= cupsdFindDest(value
)) != NULL
)
348 p
->type
= CUPS_PRINTER_CLASS
;
350 p
= cupsdAddClass(value
);
353 p
->state
= IPP_PRINTER_IDLE
;
354 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
355 p
->browse_time
= now
;
356 p
->browse_expire
= now
+ BrowseTimeout
;
359 * Set the default printer as needed...
362 if (!strcasecmp(line
, "<DefaultClass"))
367 cupsdLogMessage(CUPSD_LOG_ERROR
,
368 "Syntax error on line %d of remote.cache.", linenum
);
372 else if (!strcasecmp(line
, "</Printer>") ||
373 !strcasecmp(line
, "</Class>"))
378 * Close out the current printer...
381 cupsdSetPrinterAttrs(p
);
387 cupsdLogMessage(CUPSD_LOG_ERROR
,
388 "Syntax error on line %d of remote.cache.", linenum
);
394 cupsdLogMessage(CUPSD_LOG_ERROR
,
395 "Syntax error on line %d of remote.cache.", linenum
);
398 else if (!strcasecmp(line
, "Info"))
401 cupsdSetString(&p
->info
, value
);
403 else if (!strcasecmp(line
, "MakeModel"))
406 cupsdSetString(&p
->make_model
, value
);
408 else if (!strcasecmp(line
, "Location"))
411 cupsdSetString(&p
->location
, value
);
413 else if (!strcasecmp(line
, "DeviceURI"))
417 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
418 username
, sizeof(username
), host
, sizeof(host
), &port
,
419 resource
, sizeof(resource
));
421 cupsdSetString(&p
->hostname
, host
);
422 cupsdSetString(&p
->uri
, value
);
423 cupsdSetString(&p
->device_uri
, value
);
427 cupsdLogMessage(CUPSD_LOG_ERROR
,
428 "Syntax error on line %d of remote.cache.", linenum
);
432 else if (!strcasecmp(line
, "Option") && value
)
438 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
441 cupsdLogMessage(CUPSD_LOG_ERROR
,
442 "Syntax error on line %d of remote.cache.", linenum
);
445 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
447 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
451 else if (!strcasecmp(line
, "State"))
454 * Set the initial queue state...
457 if (value
&& !strcasecmp(value
, "idle"))
458 p
->state
= IPP_PRINTER_IDLE
;
459 else if (value
&& !strcasecmp(value
, "stopped"))
460 p
->state
= IPP_PRINTER_STOPPED
;
463 cupsdLogMessage(CUPSD_LOG_ERROR
,
464 "Syntax error on line %d of remote.cache.", linenum
);
468 else if (!strcasecmp(line
, "StateMessage"))
471 * Set the initial queue state message...
475 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
477 else if (!strcasecmp(line
, "Accepting"))
480 * Set the initial accepting state...
484 (!strcasecmp(value
, "yes") ||
485 !strcasecmp(value
, "on") ||
486 !strcasecmp(value
, "true")))
489 (!strcasecmp(value
, "no") ||
490 !strcasecmp(value
, "off") ||
491 !strcasecmp(value
, "false")))
495 cupsdLogMessage(CUPSD_LOG_ERROR
,
496 "Syntax error on line %d of remote.cache.", linenum
);
500 else if (!strcasecmp(line
, "Type"))
503 p
->type
= atoi(value
);
506 cupsdLogMessage(CUPSD_LOG_ERROR
,
507 "Syntax error on line %d of remote.cache.", linenum
);
511 else if (!strcasecmp(line
, "BrowseTime"))
515 time_t t
= atoi(value
);
517 if (t
> p
->browse_expire
)
518 p
->browse_expire
= t
;
522 cupsdLogMessage(CUPSD_LOG_ERROR
,
523 "Syntax error on line %d of remote.cache.", linenum
);
527 else if (!strcasecmp(line
, "JobSheets"))
530 * Set the initial job sheets...
535 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
540 cupsdSetString(&p
->job_sheets
[0], value
);
542 while (isspace(*valueptr
& 255))
547 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
552 cupsdSetString(&p
->job_sheets
[1], value
);
557 cupsdLogMessage(CUPSD_LOG_ERROR
,
558 "Syntax error on line %d of remote.cache.", linenum
);
562 else if (!strcasecmp(line
, "AllowUser"))
567 cupsdAddPrinterUser(p
, value
);
571 cupsdLogMessage(CUPSD_LOG_ERROR
,
572 "Syntax error on line %d of remote.cache.", linenum
);
576 else if (!strcasecmp(line
, "DenyUser"))
581 cupsdAddPrinterUser(p
, value
);
585 cupsdLogMessage(CUPSD_LOG_ERROR
,
586 "Syntax error on line %d of remote.cache.", linenum
);
593 * Something else we don't understand...
596 cupsdLogMessage(CUPSD_LOG_ERROR
,
597 "Unknown configuration directive %s on line %d of remote.cache.",
605 * Do auto-classing if needed...
608 process_implicit_classes();
613 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
614 * printer or update the broadcast contents.
618 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
620 if (!Browsing
|| !BrowseLocalProtocols
|| !BrowseInterval
|| !NumBrowsers
||
621 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
625 /* if (BrowseLocalProtocols & BROWSE_SLP)
626 slpRegisterPrinter(p); */
627 #endif /* HAVE_LIBSLP */
630 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
631 dnssdRegisterPrinter(p
);
632 #endif /* HAVE_DNSSD */
637 * 'cupsdRestartPolling()' - Restart polling servers as needed.
641 cupsdRestartPolling(void)
643 int i
; /* Looping var */
644 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
647 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
649 kill(pollp
->pid
, SIGHUP
);
654 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
658 cupsdSaveRemoteCache(void)
660 int i
; /* Looping var */
661 cups_file_t
*fp
; /* printers.conf file */
662 char temp
[1024]; /* Temporary string */
663 cupsd_printer_t
*printer
; /* Current printer class */
664 time_t curtime
; /* Current time */
665 struct tm
*curdate
; /* Current date */
666 cups_option_t
*option
; /* Current option */
670 * Create the remote.cache file...
673 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
675 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
677 cupsdLogMessage(CUPSD_LOG_ERROR
,
678 "Unable to save remote.cache - %s", strerror(errno
));
682 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
685 * Restrict access to the file...
688 fchown(cupsFileNumber(fp
), getuid(), Group
);
689 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
692 * Write a small header to the file...
695 curtime
= time(NULL
);
696 curdate
= localtime(&curtime
);
697 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
699 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
700 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
703 * Write each local printer known to the system...
706 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
708 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
711 * Skip local destinations...
714 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
718 * Write printers as needed...
721 if (printer
== DefaultPrinter
)
722 cupsFilePuts(fp
, "<Default");
724 cupsFilePutChar(fp
, '<');
726 if (printer
->type
& CUPS_PRINTER_CLASS
)
727 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
729 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
731 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
733 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
736 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
738 if (printer
->make_model
)
739 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
741 if (printer
->location
)
742 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
744 if (printer
->device_uri
)
745 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
747 if (printer
->state
== IPP_PRINTER_STOPPED
)
749 cupsFilePuts(fp
, "State Stopped\n");
750 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
753 cupsFilePuts(fp
, "State Idle\n");
755 if (printer
->accepting
)
756 cupsFilePuts(fp
, "Accepting Yes\n");
758 cupsFilePuts(fp
, "Accepting No\n");
760 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
761 printer
->job_sheets
[1]);
763 for (i
= 0; i
< printer
->num_users
; i
++)
764 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
767 for (i
= printer
->num_options
, option
= printer
->options
;
770 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
772 if (printer
->type
& CUPS_PRINTER_CLASS
)
773 cupsFilePuts(fp
, "</Class>\n");
775 cupsFilePuts(fp
, "</Printer>\n");
783 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
787 cupsdSendBrowseList(void)
789 int count
; /* Number of dests to update */
790 cupsd_printer_t
*p
; /* Current printer */
791 time_t ut
, /* Minimum update time */
792 to
; /* Timeout time */
795 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
799 * Compute the update and timeout times...
803 ut
= to
- BrowseInterval
;
806 * Figure out how many printers need an update...
809 if (BrowseInterval
> 0)
811 int max_count
; /* Maximum number to update */
815 * Throttle the number of printers we'll be updating this time
816 * around based on the number of queues that need updating and
817 * the maximum number of queues to update each second...
820 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
822 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
823 count
< max_count
&& p
!= NULL
;
824 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
825 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
826 p
->shared
&& p
->browse_time
< ut
)
830 * Loop through all of the printers and send local updates as needed...
834 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
836 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
840 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
843 * Check for wraparound...
847 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
851 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
854 else if (p
->browse_time
< ut
)
857 * Need to send an update...
862 p
->browse_time
= time(NULL
);
864 if (BrowseLocalProtocols
& BROWSE_CUPS
)
868 if (BrowseLocalProtocols
& BROWSE_SLP
)
870 #endif /* HAVE_LIBSLP */
873 if (BrowseLocalProtocols
& BROWSE_LDAP
)
875 #endif /* HAVE_LDAP */
880 * Save where we left off so that all printers get updated...
887 * Loop through all of the printers and send local updates as needed...
890 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
892 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
895 * If this is a remote queue, see if it needs to be timed out...
898 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
900 if (p
->browse_expire
< to
)
902 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
903 "%s \'%s\' deleted by directory services (timeout).",
904 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
907 cupsdLogMessage(CUPSD_LOG_DEBUG
,
908 "Remote destination \"%s\" has timed out; "
912 cupsArraySave(Printers
);
913 cupsdDeletePrinter(p
, 1);
914 cupsArrayRestore(Printers
);
922 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
926 cupsdStartBrowsing(void)
928 int val
; /* Socket option value */
929 struct sockaddr_in addr
; /* Broadcast address */
930 cupsd_printer_t
*p
; /* Current printer */
935 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
938 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
940 if (BrowseSocket
< 0)
943 * Create the broadcast socket...
946 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
948 cupsdLogMessage(CUPSD_LOG_ERROR
,
949 "Unable to create broadcast socket - %s.",
951 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
952 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
957 * Bind the socket to browse port...
960 memset(&addr
, 0, sizeof(addr
));
961 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
962 addr
.sin_family
= AF_INET
;
963 addr
.sin_port
= htons(BrowsePort
);
965 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
967 cupsdLogMessage(CUPSD_LOG_ERROR
,
968 "Unable to bind broadcast socket - %s.",
972 closesocket(BrowseSocket
);
978 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
979 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
985 * Set the "broadcast" flag...
989 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
991 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
995 closesocket(BrowseSocket
);
1001 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1002 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1007 * Close the socket on exec...
1010 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1013 * Finally, add the socket to the input selection set as needed...
1016 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1019 * We only listen if we want remote printers...
1022 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1030 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1033 * Open SLP handle...
1036 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1038 cupsdLogMessage(CUPSD_LOG_ERROR
,
1039 "Unable to open an SLP handle; disabling SLP browsing!");
1040 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1041 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1044 BrowseSLPRefresh
= 0;
1047 BrowseSLPHandle
= NULL
;
1048 #endif /* HAVE_LIBSLP */
1050 #ifdef HAVE_OPENLDAP
1051 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1055 cupsdLogMessage(CUPSD_LOG_ERROR
,
1056 "Need to set BrowseLDAPDN to use LDAP browsing!");
1057 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1058 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1063 * Open LDAP handle...
1066 int rc
; /* LDAP API status */
1067 int version
= 3; /* LDAP version */
1068 struct berval bv
= {0, ""}; /* SASL bind value */
1072 * Set the certificate file to use for encrypted LDAP sessions...
1075 if (BrowseLDAPCACertFile
)
1077 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1078 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1079 BrowseLDAPCACertFile
);
1081 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1082 (void *)BrowseLDAPCACertFile
))
1084 cupsdLogMessage(CUPSD_LOG_ERROR
,
1085 "Unable to set CA certificate file for LDAP "
1086 "connections: %d - %s", rc
, ldap_err2string(rc
));
1090 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1093 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1094 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1096 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1098 if (rc
!= LDAP_SUCCESS
)
1100 cupsdLogMessage(CUPSD_LOG_ERROR
,
1101 "Unable to initialize LDAP; disabling LDAP browsing!");
1102 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1103 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1105 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1106 (const void *)&version
) != LDAP_SUCCESS
)
1108 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1109 BrowseLDAPHandle
= NULL
;
1110 cupsdLogMessage(CUPSD_LOG_ERROR
,
1111 "Unable to set LDAP protocol version; "
1112 "disabling LDAP browsing!");
1113 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1114 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1118 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1119 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1122 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1123 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1125 if (rc
!= LDAP_SUCCESS
)
1127 cupsdLogMessage(CUPSD_LOG_ERROR
,
1128 "Unable to bind to LDAP server; "
1129 "disabling LDAP browsing!");
1130 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1131 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1132 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1137 BrowseLDAPRefresh
= 0;
1139 #endif /* HAVE_OPENLDAP */
1142 * Register the individual printers
1145 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1147 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1148 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1149 cupsdRegisterPrinter(p
);
1154 * 'cupsdStartPolling()' - Start polling servers as needed.
1158 cupsdStartPolling(void)
1160 int i
; /* Looping var */
1161 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1162 char polld
[1024]; /* Poll daemon path */
1163 char sport
[255]; /* Server port */
1164 char bport
[255]; /* Browser port */
1165 char interval
[255]; /* Poll interval */
1166 int statusfds
[2]; /* Status pipe */
1167 char *argv
[6]; /* Arguments */
1168 char *envp
[100]; /* Environment */
1172 * Don't do anything if we aren't polling...
1175 if (NumPolled
== 0 || BrowseSocket
< 0)
1178 PollStatusBuffer
= NULL
;
1183 * Setup string arguments for polld, port and interval options.
1186 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1188 sprintf(bport
, "%d", BrowsePort
);
1191 sprintf(interval
, "%d", BrowseInterval
);
1193 strcpy(interval
, "30");
1195 argv
[0] = "cups-polld";
1201 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1204 * Create a pipe that receives the status messages from each
1208 if (cupsdOpenPipe(statusfds
))
1210 cupsdLogMessage(CUPSD_LOG_ERROR
,
1211 "Unable to create polling status pipes - %s.",
1214 PollStatusBuffer
= NULL
;
1218 PollPipe
= statusfds
[0];
1219 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1222 * Run each polling daemon, redirecting stderr to the polling pipe...
1225 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1227 sprintf(sport
, "%d", pollp
->port
);
1229 argv
[1] = pollp
->hostname
;
1231 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1232 0, &(pollp
->pid
)) < 0)
1234 cupsdLogMessage(CUPSD_LOG_ERROR
,
1235 "cupsdStartPolling: Unable to fork polling daemon - %s",
1241 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1242 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1243 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1246 close(statusfds
[1]);
1249 * Finally, add the pipe to the input selection set...
1252 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1257 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1261 cupsdStopBrowsing(void)
1263 cupsd_printer_t
*p
; /* Current printer */
1266 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1270 * De-register the individual printers
1273 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1275 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1276 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1277 cupsdDeregisterPrinter(p
, 1);
1280 * Shut down browsing sockets...
1283 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1287 * Close the socket and remove it from the input selection set.
1291 closesocket(BrowseSocket
);
1293 close(BrowseSocket
);
1296 cupsdRemoveSelect(BrowseSocket
);
1301 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1305 * Close SLP handle...
1308 SLPClose(BrowseSLPHandle
);
1309 BrowseSLPHandle
= NULL
;
1311 #endif /* HAVE_LIBSLP */
1313 #ifdef HAVE_OPENLDAP
1314 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1317 ldap_unbind(BrowseLDAPHandle
);
1318 BrowseLDAPHandle
= NULL
;
1320 #endif /* HAVE_OPENLDAP */
1325 * 'cupsdStopPolling()' - Stop polling servers as needed.
1329 cupsdStopPolling(void)
1331 int i
; /* Looping var */
1332 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1337 cupsdStatBufDelete(PollStatusBuffer
);
1340 cupsdRemoveSelect(PollPipe
);
1343 PollStatusBuffer
= NULL
;
1346 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1348 cupsdEndProcess(pollp
->pid
, 0);
1354 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1358 cupsdUpdateDNSSDBrowse(
1359 cupsd_printer_t
*p
) /* I - Printer being queried */
1361 DNSServiceErrorType sdErr
; /* Service discovery error */
1364 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1365 != kDNSServiceErr_NoError
)
1367 cupsdLogMessage(CUPSD_LOG_ERROR
,
1368 "DNS Service Discovery registration error %d for \"%s\"!",
1370 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1371 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1373 p
->dnssd_ipp_ref
= NULL
;
1374 p
->dnssd_ipp_fd
= -1;
1377 #endif /* HAVE_DNSSD */
1380 #ifdef HAVE_OPENLDAP
1382 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1386 cupsdUpdateLDAPBrowse(void)
1388 char uri
[HTTP_MAX_URI
], /* Printer URI */
1389 host
[HTTP_MAX_URI
], /* Hostname */
1390 resource
[HTTP_MAX_URI
], /* Resource path */
1391 location
[1024], /* Printer location */
1392 info
[1024], /* Printer information */
1393 make_model
[1024], /* Printer make and model */
1394 **value
; /* Holds the returned data from LDAP */
1395 int type
; /* Printer type */
1396 int rc
; /* LDAP status */
1397 int limit
; /* Size limit */
1398 LDAPMessage
*res
, /* LDAP search results */
1399 *e
; /* Current entry from search */
1403 * Search for printers...
1406 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1408 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1410 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1411 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1412 if (rc
!= LDAP_SUCCESS
)
1414 cupsdLogMessage(CUPSD_LOG_ERROR
,
1415 "LDAP search returned error %d: %s", rc
,
1416 ldap_err2string(rc
));
1420 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1421 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1426 * Loop through the available printers...
1429 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1431 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1434 * Get the required values from this entry...
1437 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1438 "printerDescription")) == NULL
)
1441 strlcpy(info
, *value
, sizeof(info
));
1442 ldap_value_free(value
);
1444 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1445 "printerLocation")) == NULL
)
1448 strlcpy(location
, *value
, sizeof(location
));
1449 ldap_value_free(value
);
1451 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1452 "printerMakeAndModel")) == NULL
)
1455 strlcpy(make_model
, *value
, sizeof(make_model
));
1456 ldap_value_free(value
);
1458 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1459 "printerType")) == NULL
)
1462 type
= atoi(*value
);
1463 ldap_value_free(value
);
1465 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1466 "printerURI")) == NULL
)
1469 strlcpy(uri
, *value
, sizeof(uri
));
1470 ldap_value_free(value
);
1473 * Process the entry as browse data...
1476 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1477 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1478 location
, info
, make_model
, 0, NULL
);
1482 #endif /* HAVE_OPENLDAP */
1487 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1491 cupsdUpdateSLPBrowse(void)
1493 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1494 *next
; /* Next service in list */
1495 cupsd_printer_t p
; /* Printer information */
1496 const char *uri
; /* Pointer to printer URI */
1497 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1498 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1502 * Reset the refresh time...
1505 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1508 * Poll for remote printers using SLP...
1513 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1514 slp_url_callback
, &s
);
1517 * Loop through the list of available printers...
1523 * Save the "next" pointer...
1529 * Load a cupsd_printer_t structure with the SLP service attributes...
1532 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1535 * Process this printer entry...
1538 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1540 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1543 * Pull the URI apart to see if this is a local or remote printer...
1546 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1547 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1548 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1552 * Free this listing...
1555 cupsdClearString(&p
.info
);
1556 cupsdClearString(&p
.location
);
1557 cupsdClearString(&p
.make_model
);
1562 #endif /* HAVE_LIBSLP */
1566 * 'dequote()' - Remote quotes from a string.
1569 static char * /* O - Dequoted string */
1570 dequote(char *d
, /* I - Destination string */
1571 const char *s
, /* I - Source string */
1572 int dlen
) /* I - Destination length */
1574 char *dptr
; /* Pointer into destination */
1579 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1596 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1599 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1600 is_local_queue(const char *uri
, /* I - Printer URI */
1601 char *host
, /* O - Host string */
1602 int hostlen
, /* I - Length of host buffer */
1603 char *resource
, /* O - Resource string */
1604 int resourcelen
) /* I - Length of resource buffer */
1606 char scheme
[32], /* Scheme portion of URI */
1607 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1608 int port
; /* Port portion of URI */
1609 cupsd_netif_t
*iface
; /* Network interface */
1613 * Pull the URI apart to see if this is a local or remote printer...
1616 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1617 username
, sizeof(username
), host
, hostlen
, &port
,
1618 resource
, resourcelen
) < HTTP_URI_OK
)
1621 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1624 * Check for local server addresses...
1627 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1632 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1634 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1635 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1639 * If we get here, the printer is remote...
1647 * 'process_browse_data()' - Process new browse data.
1651 process_browse_data(
1652 const char *uri
, /* I - URI of printer/class */
1653 const char *host
, /* I - Hostname */
1654 const char *resource
, /* I - Resource path */
1655 cups_ptype_t type
, /* I - Printer type */
1656 ipp_pstate_t state
, /* I - Printer state */
1657 const char *location
, /* I - Printer location */
1658 const char *info
, /* I - Printer information */
1659 const char *make_model
, /* I - Printer make and model */
1660 int num_attrs
, /* I - Number of attributes */
1661 cups_option_t
*attrs
) /* I - Attributes */
1663 int i
; /* Looping var */
1664 int update
; /* Update printer attributes? */
1665 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1666 name
[IPP_MAX_NAME
], /* Name of printer */
1667 newname
[IPP_MAX_NAME
], /* New name of printer */
1668 *hptr
, /* Pointer into hostname */
1669 *sptr
; /* Pointer into ServerName */
1670 char local_make_model
[IPP_MAX_NAME
];
1671 /* Local make and model */
1672 cupsd_printer_t
*p
; /* Printer information */
1673 const char *ipp_options
, /* ipp-options value */
1674 *lease_duration
; /* lease-duration value */
1678 * Determine if the URI contains any illegal characters in it...
1681 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1682 (strncmp(resource
, "/printers/", 10) &&
1683 strncmp(resource
, "/classes/", 9)))
1685 cupsdLogMessage(CUPSD_LOG_ERROR
,
1686 "process_browse_data: Bad printer URI in browse data: %s",
1691 if (strchr(resource
, '?') ||
1692 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1693 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1695 cupsdLogMessage(CUPSD_LOG_ERROR
,
1696 "process_browse_data: Bad resource in browse data: %s",
1702 * OK, this isn't a local printer; add any remote options...
1705 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1707 if (BrowseRemoteOptions
)
1709 if (BrowseRemoteOptions
[0] == '?')
1712 * Override server-supplied options...
1715 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1717 else if (ipp_options
)
1720 * Combine the server and local options...
1723 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1724 BrowseRemoteOptions
);
1729 * Just use the local options...
1732 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1737 else if (ipp_options
)
1740 * Just use the server-supplied options...
1743 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1748 * See if we already have it listed in the Printers list, and add it if not...
1751 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
1752 type
&= ~CUPS_PRINTER_IMPLICIT
;
1754 hptr
= strchr(host
, '.');
1755 sptr
= strchr(ServerName
, '.');
1757 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1760 * Strip the common domain name components...
1763 while (hptr
!= NULL
)
1765 if (!strcasecmp(hptr
, sptr
))
1771 hptr
= strchr(hptr
+ 1, '.');
1775 if (type
& CUPS_PRINTER_CLASS
)
1778 * Remote destination is a class...
1781 if (!strncmp(resource
, "/classes/", 9))
1782 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1787 *hptr
= '.'; /* Resource FQDN */
1789 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1791 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1793 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1796 * Nope, this isn't the same host; if the hostname isn't the local host,
1797 * add it to the other class and then find a class using the full host
1801 if (p
->type
& CUPS_PRINTER_REMOTE
)
1803 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1804 "Renamed remote class \"%s\" to \"%s@%s\"...",
1805 p
->name
, p
->name
, p
->hostname
);
1806 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1807 "Class \'%s\' deleted by directory services.",
1810 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1811 cupsdRenamePrinter(p
, newname
);
1813 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1814 "Class \'%s\' added by directory services.",
1820 else if (!p
->hostname
)
1823 * Hostname not set, so this must be a cached remote printer
1824 * that was created for a pending print job...
1827 cupsdSetString(&p
->hostname
, host
);
1828 cupsdSetString(&p
->uri
, uri
);
1829 cupsdSetString(&p
->device_uri
, uri
);
1836 * Use the short name for this shared class.
1839 strlcpy(name
, resource
+ 9, sizeof(name
));
1842 else if (p
&& !p
->hostname
)
1845 * Hostname not set, so this must be a cached remote printer
1846 * that was created for a pending print job...
1849 cupsdSetString(&p
->hostname
, host
);
1850 cupsdSetString(&p
->uri
, uri
);
1851 cupsdSetString(&p
->device_uri
, uri
);
1858 * Class doesn't exist; add it...
1861 p
= cupsdAddClass(name
);
1863 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1865 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1866 "Class \'%s\' added by directory services.", name
);
1869 * Force the URI to point to the real server...
1872 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1874 cupsdSetString(&p
->uri
, uri
);
1875 cupsdSetString(&p
->device_uri
, uri
);
1876 cupsdSetString(&p
->hostname
, host
);
1884 * Remote destination is a printer...
1887 if (!strncmp(resource
, "/printers/", 10))
1888 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1893 *hptr
= '.'; /* Resource FQDN */
1895 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
1897 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
1899 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1902 * Nope, this isn't the same host; if the hostname isn't the local host,
1903 * add it to the other printer and then find a printer using the full host
1907 if (p
->type
& CUPS_PRINTER_REMOTE
)
1909 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1910 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1911 p
->name
, p
->name
, p
->hostname
);
1912 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1913 "Printer \'%s\' deleted by directory services.",
1916 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1917 cupsdRenamePrinter(p
, newname
);
1919 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1920 "Printer \'%s\' added by directory services.",
1926 else if (!p
->hostname
)
1929 * Hostname not set, so this must be a cached remote printer
1930 * that was created for a pending print job...
1933 cupsdSetString(&p
->hostname
, host
);
1934 cupsdSetString(&p
->uri
, uri
);
1935 cupsdSetString(&p
->device_uri
, uri
);
1942 * Use the short name for this shared printer.
1945 strlcpy(name
, resource
+ 10, sizeof(name
));
1948 else if (p
&& !p
->hostname
)
1951 * Hostname not set, so this must be a cached remote printer
1952 * that was created for a pending print job...
1955 cupsdSetString(&p
->hostname
, host
);
1956 cupsdSetString(&p
->uri
, uri
);
1957 cupsdSetString(&p
->device_uri
, uri
);
1964 * Printer doesn't exist; add it...
1967 p
= cupsdAddPrinter(name
);
1969 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1970 "Printer \'%s\' added by directory services.", name
);
1972 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
1975 * Force the URI to point to the real server...
1978 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1980 cupsdSetString(&p
->hostname
, host
);
1981 cupsdSetString(&p
->uri
, uri
);
1982 cupsdSetString(&p
->device_uri
, uri
);
1989 * Update the state...
1993 p
->browse_time
= time(NULL
);
1995 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
1999 * Grab the lease-duration for the browse data; anything less then 1
2000 * second or more than 1 week gets the default BrowseTimeout...
2003 i
= atoi(lease_duration
);
2004 if (i
< 1 || i
> 604800)
2007 p
->browse_expire
= p
->browse_time
+ i
;
2010 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2012 if (type
& CUPS_PRINTER_REJECTING
)
2014 type
&= ~CUPS_PRINTER_REJECTING
;
2022 else if (!p
->accepting
)
2028 if (p
->type
!= type
)
2034 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2036 cupsdSetString(&p
->location
, location
);
2040 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2042 cupsdSetString(&p
->info
, info
);
2046 if (!make_model
|| !make_model
[0])
2048 if (type
& CUPS_PRINTER_CLASS
)
2049 snprintf(local_make_model
, sizeof(local_make_model
),
2050 "Remote Class on %s", host
);
2052 snprintf(local_make_model
, sizeof(local_make_model
),
2053 "Remote Printer on %s", host
);
2056 snprintf(local_make_model
, sizeof(local_make_model
),
2057 "%s on %s", make_model
, host
);
2059 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2061 cupsdSetString(&p
->make_model
, local_make_model
);
2067 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2070 * See if we need to update the attributes...
2073 if (p
->num_options
!= num_attrs
)
2077 for (i
= 0; i
< num_attrs
; i
++)
2078 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2079 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2080 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2089 * Free the old options...
2092 cupsFreeOptions(p
->num_options
, p
->options
);
2095 p
->num_options
= num_attrs
;
2098 if (type
& CUPS_PRINTER_DELETE
)
2100 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2101 "%s \'%s\' deleted by directory services.",
2102 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2104 cupsdExpireSubscriptions(p
, NULL
);
2106 cupsdDeletePrinter(p
, 1);
2107 cupsdUpdateImplicitClasses();
2111 cupsdSetPrinterAttrs(p
);
2112 cupsdUpdateImplicitClasses();
2116 * See if we have a default printer... If not, make the first network
2117 * default printer the default.
2120 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2123 * Find the first network default printer and use it...
2126 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2128 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2129 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2137 * Do auto-classing if needed...
2140 process_implicit_classes();
2143 * Update the printcap file...
2146 cupsdWritePrintcap();
2152 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2155 static char * /* O - TXT record */
2156 dnssdBuildTxtRecord(
2157 int *txt_len
, /* O - TXT record length */
2158 cupsd_printer_t
*p
) /* I - Printer information */
2160 int i
, j
; /* Looping vars */
2161 char type_str
[32], /* Type to string buffer */
2162 state_str
[32], /* State to string buffer */
2163 rp_str
[1024], /* Queue name string buffer */
2164 air_str
[1024], /* auth-info-required string buffer */
2165 *keyvalue
[32][2]; /* Table of key/value pairs */
2169 * Load up the key value pairs...
2174 keyvalue
[i
][0] = "txtvers";
2175 keyvalue
[i
++][1] = "1";
2177 keyvalue
[i
][0] = "qtotal";
2178 keyvalue
[i
++][1] = "1";
2180 keyvalue
[i
][0] = "rp";
2181 keyvalue
[i
++][1] = rp_str
;
2182 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2183 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2185 keyvalue
[i
][0] = "ty";
2186 keyvalue
[i
++][1] = p
->make_model
;
2188 if (p
->location
&& *p
->location
!= '\0')
2190 keyvalue
[i
][0] = "note";
2191 keyvalue
[i
++][1] = p
->location
;
2194 keyvalue
[i
][0] = "product";
2195 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2197 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2198 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2200 keyvalue
[i
][0] = "printer-state";
2201 keyvalue
[i
++][1] = state_str
;
2203 keyvalue
[i
][0] = "printer-type";
2204 keyvalue
[i
++][1] = type_str
;
2206 keyvalue
[i
][0] = "Transparent";
2207 keyvalue
[i
++][1] = "T";
2209 keyvalue
[i
][0] = "Binary";
2210 keyvalue
[i
++][1] = "T";
2212 if ((p
->type
& CUPS_PRINTER_FAX
))
2214 keyvalue
[i
][0] = "Fax";
2215 keyvalue
[i
++][1] = "T";
2218 if ((p
->type
& CUPS_PRINTER_COLOR
))
2220 keyvalue
[i
][0] = "Color";
2221 keyvalue
[i
++][1] = "T";
2224 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2226 keyvalue
[i
][0] = "Duplex";
2227 keyvalue
[i
++][1] = "T";
2230 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2232 keyvalue
[i
][0] = "Staple";
2233 keyvalue
[i
++][1] = "T";
2236 if ((p
->type
& CUPS_PRINTER_COPIES
))
2238 keyvalue
[i
][0] = "Copies";
2239 keyvalue
[i
++][1] = "T";
2242 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2244 keyvalue
[i
][0] = "Collate";
2245 keyvalue
[i
++][1] = "T";
2248 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2250 keyvalue
[i
][0] = "Punch";
2251 keyvalue
[i
++][1] = "T";
2254 if ((p
->type
& CUPS_PRINTER_BIND
))
2256 keyvalue
[i
][0] = "Bind";
2257 keyvalue
[i
++][1] = "T";
2260 if ((p
->type
& CUPS_PRINTER_SORT
))
2262 keyvalue
[i
][0] = "Sort";
2263 keyvalue
[i
++][1] = "T";
2266 keyvalue
[i
][0] = "pdl";
2267 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2269 if (p
->num_auth_info_required
)
2271 char *air
= air_str
; /* Pointer into string */
2274 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2276 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2282 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2286 keyvalue
[i
][0] = "air";
2287 keyvalue
[i
++][1] = air
;
2291 * Then pack them into a proper txt record...
2294 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2299 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2304 dnssdDeregisterPrinter(
2305 cupsd_printer_t
*p
) /* I - Printer */
2307 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2310 * Closing the socket deregisters the service
2313 if (p
->dnssd_ipp_ref
)
2315 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2316 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2317 p
->dnssd_ipp_ref
= NULL
;
2318 p
->dnssd_ipp_fd
= -1;
2321 cupsdClearString(&p
->reg_name
);
2326 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2329 free(p
->txt_record
);
2330 p
->txt_record
= NULL
;
2336 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2337 * TXT record format.
2340 static char * /* O - TXT record */
2341 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2342 char *keyvalue
[][2], /* I - Table of key value pairs */
2343 int count
) /* I - Items in table */
2345 int i
; /* Looping var */
2346 int length
; /* Length of TXT record */
2347 int length2
; /* Length of value */
2348 char *txtRecord
; /* TXT record buffer */
2349 char *cursor
; /* Looping pointer */
2353 * Calculate the buffer size
2356 for (length
= i
= 0; i
< count
; i
++)
2357 length
+= 1 + strlen(keyvalue
[i
][0]) +
2358 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2361 * Allocate and fill it
2364 txtRecord
= malloc(length
);
2369 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2372 * Drop in the p-string style length byte followed by the data
2375 length
= strlen(keyvalue
[i
][0]);
2376 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2378 *cursor
++ = (unsigned char)(length
+ length2
);
2380 memcpy(cursor
, keyvalue
[i
][0], length
);
2387 memcpy(cursor
, keyvalue
[i
][1], length2
);
2398 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2402 dnssdRegisterCallback(
2403 DNSServiceRef sdRef
, /* I - DNS Service reference */
2404 DNSServiceFlags flags
, /* I - Reserved for future use */
2405 DNSServiceErrorType errorCode
, /* I - Error code */
2406 const char *name
, /* I - Service name */
2407 const char *regtype
, /* I - Service type */
2408 const char *domain
, /* I - Domain. ".local" for now */
2409 void *context
) /* I - User-defined context */
2413 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2414 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2418 cupsdLogMessage(CUPSD_LOG_ERROR
,
2419 "DNSServiceRegister failed with error %d", (int)errorCode
);
2426 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2427 * or update the broadcast contents.
2431 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2433 DNSServiceErrorType se
; /* dnssd errors */
2434 cupsd_listener_t
*lis
; /* Current listening socket */
2435 char *txt_record
, /* TXT record buffer */
2436 *name
; /* Service name */
2437 int txt_len
, /* TXT record length */
2438 port
; /* IPP port number */
2439 char str_buffer
[1024];
2440 /* C-string buffer */
2441 const char *computerName
; /* Computer name c-string ptr */
2442 const char *regtype
; /* Registration type */
2443 #ifdef HAVE_COREFOUNDATION_H
2444 CFStringRef computerNameRef
;/* Computer name CFString */
2445 CFStringEncoding nameEncoding
; /* Computer name encoding */
2446 CFMutableStringRef shortNameRef
; /* Mutable name string */
2447 CFIndex nameLength
; /* Name string length */
2449 int nameLength
; /* Name string length */
2450 #endif /* HAVE_COREFOUNDATION_H */
2453 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2454 !p
->dnssd_ipp_ref
? "new" : "update");
2457 * If per-printer sharing was just disabled make sure we're not
2458 * registered before returning.
2463 dnssdDeregisterPrinter(p
);
2468 * Get the computer name as a c-string...
2471 #ifdef HAVE_COREFOUNDATION_H
2472 computerName
= NULL
;
2473 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2474 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2475 kCFStringEncodingUTF8
)) == NULL
)
2476 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2477 kCFStringEncodingUTF8
))
2478 computerName
= str_buffer
;
2480 computerName
= ServerName
;
2481 #endif /* HAVE_COREFOUNDATION_H */
2484 * The registered name takes the form of "<printer-info> @ <computer name>"...
2489 cupsdSetStringf(&name
, "%s @ %s",
2490 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2493 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2495 #ifdef HAVE_COREFOUNDATION_H
2496 if (computerNameRef
)
2497 CFRelease(computerNameRef
);
2498 #endif /* HAVE_COREFOUNDATION_H */
2501 * If an existing printer was renamed, unregister it and start over...
2504 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2505 dnssdDeregisterPrinter(p
);
2507 txt_len
= 0; /* anti-compiler-warning-code */
2508 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2510 if (!p
->dnssd_ipp_ref
)
2513 * Initial registration...
2516 cupsdSetString(&p
->reg_name
, name
);
2520 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2522 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2524 if (lis
->address
.addr
.sa_family
== AF_INET
)
2526 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2529 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2531 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2537 * Use the _fax subtype for fax queues...
2540 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2543 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) type is \"%s\"",
2546 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2547 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2548 dnssdRegisterCallback
, p
);
2551 * In case the name is too long, try shortening the string one character
2555 if (se
== kDNSServiceErr_BadParam
)
2557 #ifdef HAVE_COREFOUNDATION_H
2558 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2560 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2561 nameLength
= CFStringGetLength(shortNameRef
);
2563 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2565 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2566 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2567 kCFStringEncodingUTF8
))
2569 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2570 regtype
, NULL
, NULL
, htons(port
),
2571 txt_len
, txt_record
,
2572 dnssdRegisterCallback
, p
);
2576 CFRelease(shortNameRef
);
2579 nameLength
= strlen(name
);
2580 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2582 name
[--nameLength
] = '\0';
2583 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2584 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2585 dnssdRegisterCallback
, p
);
2587 #endif /* HAVE_COREFOUNDATION_H */
2590 if (se
== kDNSServiceErr_NoError
)
2592 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2593 p
->txt_record
= txt_record
;
2594 p
->txt_len
= txt_len
;
2597 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2601 cupsdLogMessage(CUPSD_LOG_WARN
,
2602 "DNS-SD registration of \"%s\" failed with %d",
2605 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2608 * Update the existing registration...
2611 /* A TTL of 0 means use record's original value (Radar 3176248) */
2612 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2613 txt_len
, txt_record
, 0);
2616 free(p
->txt_record
);
2618 p
->txt_record
= txt_record
;
2619 p
->txt_len
= txt_len
;
2626 cupsdClearString(&name
);
2628 #endif /* HAVE_DNSSD */
2632 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2636 process_implicit_classes(void)
2638 int i
; /* Looping var */
2639 int update
; /* Update printer attributes? */
2640 char name
[IPP_MAX_NAME
], /* Name of printer */
2641 *hptr
; /* Pointer into hostname */
2642 cupsd_printer_t
*p
, /* Printer information */
2643 *pclass
, /* Printer class */
2644 *first
; /* First printer in class */
2645 int offset
, /* Offset of name */
2646 len
; /* Length of name */
2649 if (!ImplicitClasses
|| !Printers
)
2653 * Loop through all available printers and create classes as needed...
2656 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2657 update
= 0, pclass
= NULL
, first
= NULL
;
2659 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2662 * Skip implicit classes...
2665 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2672 * If len == 0, get the length of this printer name up to the "@"
2676 cupsArraySave(Printers
);
2679 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2680 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2683 * We have more than one printer with the same name; see if
2684 * we have a class, and if this printer is a member...
2687 if (pclass
&& strcasecmp(pclass
->name
, name
))
2690 cupsdSetPrinterAttrs(pclass
);
2696 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2699 * Need to add the class...
2702 pclass
= cupsdAddPrinter(name
);
2703 cupsArrayAdd(ImplicitPrinters
, pclass
);
2705 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2706 pclass
->accepting
= 1;
2707 pclass
->state
= IPP_PRINTER_IDLE
;
2709 cupsdSetString(&pclass
->location
, p
->location
);
2710 cupsdSetString(&pclass
->info
, p
->info
);
2712 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2713 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2717 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2719 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2720 "Implicit class \'%s\' added by directory services.",
2726 for (i
= 0; i
< pclass
->num_printers
; i
++)
2727 if (pclass
->printers
[i
] == first
)
2730 if (i
>= pclass
->num_printers
)
2732 first
->in_implicit_class
= 1;
2733 cupsdAddPrinterToClass(pclass
, first
);
2739 for (i
= 0; i
< pclass
->num_printers
; i
++)
2740 if (pclass
->printers
[i
] == p
)
2743 if (i
>= pclass
->num_printers
)
2745 p
->in_implicit_class
= 1;
2746 cupsdAddPrinterToClass(pclass
, p
);
2753 * First time around; just get name length and mark it as first
2757 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2758 len
= hptr
- p
->name
;
2760 len
= strlen(p
->name
);
2762 strncpy(name
, p
->name
, len
);
2766 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2767 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2770 * Can't use same name as a local printer; add "Any" to the
2771 * front of the name, unless we have explicitly disabled
2772 * the "ImplicitAnyClasses"...
2775 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2778 * Add "Any" to the class name...
2781 strcpy(name
, "Any");
2782 strncpy(name
+ 3, p
->name
, len
);
2783 name
[len
+ 3] = '\0';
2789 * Don't create an implicit class if we have a local printer
2790 * with the same name...
2794 cupsArrayRestore(Printers
);
2802 cupsArrayRestore(Printers
);
2806 * Update the last printer class as needed...
2809 if (pclass
&& update
)
2810 cupsdSetPrinterAttrs(pclass
);
2815 * 'send_cups_browse()' - Send new browsing information using the CUPS
2820 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2822 int i
; /* Looping var */
2823 cups_ptype_t type
; /* Printer type */
2824 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2825 int bytes
; /* Length of packet */
2826 char packet
[1453], /* Browse data packet */
2827 uri
[1024], /* Printer URI */
2828 location
[1024], /* printer-location */
2829 info
[1024], /* printer-info */
2831 /* printer-make-and-model */
2832 cupsd_netif_t
*iface
; /* Network interface */
2836 * Figure out the printer type value...
2839 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2842 type
|= CUPS_PRINTER_REJECTING
;
2844 if (p
== DefaultPrinter
)
2845 type
|= CUPS_PRINTER_DEFAULT
;
2848 * Remove quotes from printer-info, printer-location, and
2849 * printer-make-and-model attributes...
2852 dequote(location
, p
->location
, sizeof(location
));
2853 dequote(info
, p
->info
, sizeof(info
));
2856 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2857 else if (p
->type
& CUPS_PRINTER_CLASS
)
2859 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2860 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2862 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2865 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2867 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2870 * Send a packet to each browse address...
2873 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2877 * Send the browse packet to one or more interfaces...
2880 if (!strcmp(b
->iface
, "*"))
2883 * Send to all local interfaces...
2888 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2890 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2893 * Only send to local, IPv4 interfaces...
2896 if (!iface
->is_local
|| !iface
->port
||
2897 iface
->address
.addr
.sa_family
!= AF_INET
)
2900 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2901 iface
->hostname
, iface
->port
,
2902 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2905 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2906 type
, p
->state
, uri
, location
, info
, make_model
,
2907 p
->browse_attrs
? p
->browse_attrs
: "");
2909 bytes
= strlen(packet
);
2911 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2912 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2913 iface
->name
, packet
);
2915 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2917 sendto(BrowseSocket
, packet
, bytes
, 0,
2918 (struct sockaddr
*)&(iface
->broadcast
),
2919 httpAddrLength(&(iface
->broadcast
)));
2922 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2925 * Send to the named interface using the IPv4 address...
2929 if (strcmp(b
->iface
, iface
->name
))
2934 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2937 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2941 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2942 iface
->hostname
, iface
->port
,
2943 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2946 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2947 type
, p
->state
, uri
, location
, info
, make_model
,
2948 p
->browse_attrs
? p
->browse_attrs
: "");
2950 bytes
= strlen(packet
);
2952 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2953 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2954 iface
->name
, packet
);
2956 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2958 sendto(BrowseSocket
, packet
, bytes
, 0,
2959 (struct sockaddr
*)&(iface
->broadcast
),
2960 httpAddrLength(&(iface
->broadcast
)));
2967 * Send the browse packet to the indicated address using
2968 * the default server name...
2971 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2972 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2973 p
->browse_attrs
? p
->browse_attrs
: "");
2975 bytes
= strlen(packet
);
2976 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2977 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2979 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2980 (struct sockaddr
*)&(b
->to
),
2981 httpAddrLength(&(b
->to
))) <= 0)
2984 * Unable to send browse packet, so remove this address from the
2988 cupsdLogMessage(CUPSD_LOG_ERROR
,
2989 "cupsdSendBrowseList: sendto failed for browser "
2991 (int)(b
- Browsers
+ 1), strerror(errno
));
2994 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3003 #ifdef HAVE_OPENLDAP
3005 * 'send_ldap_browse()' - Send LDAP printer registrations.
3009 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3011 int i
; /* Looping var... */
3012 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3013 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3014 LDAPMessage
*res
; /* Search result token */
3015 char *cn_value
[2], /* Change records */
3021 typestring
[255], /* String to hold printer-type */
3022 filter
[256], /* Search filter for possible UPDATEs */
3023 dn
[1024]; /* DN of the printer we are adding */
3024 int rc
; /* LDAP status */
3025 static const char * const objectClass_values
[] =
3026 { /* The 3 objectClass's we use in */
3027 "top", /* our LDAP entries */
3033 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3036 * Everything in ldap is ** so we fudge around it...
3039 sprintf(typestring
, "%u", p
->type
);
3041 cn_value
[0] = p
->name
;
3043 info
[0] = p
->info
? p
->info
: "Unknown";
3045 location
[0] = p
->location
? p
->location
: "Unknown";
3047 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3048 make_model
[1] = NULL
;
3049 type
[0] = typestring
;
3054 snprintf(filter
, sizeof(filter
),
3055 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3057 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3058 filter
, (char **)ldap_attrs
, 0, &res
);
3059 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3062 mods
[0].mod_type
= "cn";
3063 mods
[0].mod_values
= cn_value
;
3064 mods
[1].mod_type
= "printerDescription";
3065 mods
[1].mod_values
= info
;
3066 mods
[2].mod_type
= "printerURI";
3067 mods
[2].mod_values
= uri
;
3068 mods
[3].mod_type
= "printerLocation";
3069 mods
[3].mod_values
= location
;
3070 mods
[4].mod_type
= "printerMakeAndModel";
3071 mods
[4].mod_values
= make_model
;
3072 mods
[5].mod_type
= "printerType";
3073 mods
[5].mod_values
= type
;
3074 mods
[6].mod_type
= "objectClass";
3075 mods
[6].mod_values
= (char **)objectClass_values
;
3077 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3078 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3080 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3083 * Printer has already been registered, modify the current
3087 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3088 "send_ldap_browse: Replacing entry...");
3090 for (i
= 0; i
< 7; i
++)
3092 pmods
[i
] = mods
+ i
;
3093 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3097 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3098 cupsdLogMessage(CUPSD_LOG_ERROR
,
3099 "LDAP modify for %s failed with status %d: %s",
3100 p
->name
, rc
, ldap_err2string(rc
));
3105 * Printer has never been registered, add the current
3109 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3110 "send_ldap_browse: Adding entry...");
3112 for (i
= 0; i
< 7; i
++)
3114 pmods
[i
] = mods
+ i
;
3115 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3119 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3120 cupsdLogMessage(CUPSD_LOG_ERROR
,
3121 "LDAP add for %s failed with status %d: %s",
3122 p
->name
, rc
, ldap_err2string(rc
));
3125 #endif /* HAVE_OPENLDAP */
3130 * 'send_slp_browse()' - Register the specified printer with SLP.
3134 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3136 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3137 attrs
[8192], /* Printer attributes */
3138 finishings
[1024], /* Finishings to support */
3139 make_model
[IPP_MAX_NAME
* 2],
3140 /* Make and model, quoted */
3141 location
[IPP_MAX_NAME
* 2],
3142 /* Location, quoted */
3143 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3144 *src
, /* Pointer to original string */
3145 *dst
; /* Pointer to destination string */
3146 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3147 SLPError error
; /* SLP error, if any */
3150 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3154 * Make the SLP service URL that conforms to the IANA
3155 * 'printer:' template.
3158 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3160 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3163 * Figure out the finishings string...
3166 if (p
->type
& CUPS_PRINTER_STAPLE
)
3167 strcpy(finishings
, "staple");
3169 finishings
[0] = '\0';
3171 if (p
->type
& CUPS_PRINTER_BIND
)
3174 strlcat(finishings
, ",bind", sizeof(finishings
));
3176 strcpy(finishings
, "bind");
3179 if (p
->type
& CUPS_PRINTER_PUNCH
)
3182 strlcat(finishings
, ",punch", sizeof(finishings
));
3184 strcpy(finishings
, "punch");
3187 if (p
->type
& CUPS_PRINTER_COVER
)
3190 strlcat(finishings
, ",cover", sizeof(finishings
));
3192 strcpy(finishings
, "cover");
3195 if (p
->type
& CUPS_PRINTER_SORT
)
3198 strlcat(finishings
, ",sort", sizeof(finishings
));
3200 strcpy(finishings
, "sort");
3204 strcpy(finishings
, "none");
3207 * Quote any commas in the make and model, location, and info strings...
3210 for (src
= p
->make_model
, dst
= make_model
;
3211 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3213 if (*src
== ',' || *src
== '\\' || *src
== ')')
3222 strcpy(make_model
, "Unknown");
3224 for (src
= p
->location
, dst
= location
;
3225 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3227 if (*src
== ',' || *src
== '\\' || *src
== ')')
3236 strcpy(location
, "Unknown");
3238 for (src
= p
->info
, dst
= info
;
3239 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3241 if (*src
== ',' || *src
== '\\' || *src
== ')')
3250 strcpy(info
, "Unknown");
3253 * Get the authentication value...
3256 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3260 * Make the SLP attribute string list that conforms to
3261 * the IANA 'printer:' template.
3264 snprintf(attrs
, sizeof(attrs
),
3265 "(printer-uri-supported=%s),"
3266 "(uri-authentication-supported=%s>),"
3268 "(uri-security-supported=tls>),"
3270 "(uri-security-supported=none>),"
3271 #endif /* HAVE_SSL */
3272 "(printer-name=%s),"
3273 "(printer-location=%s),"
3274 "(printer-info=%s),"
3275 "(printer-more-info=%s),"
3276 "(printer-make-and-model=%s),"
3277 "(printer-type=%d),"
3278 "(charset-supported=utf-8),"
3279 "(natural-language-configured=%s),"
3280 "(natural-language-supported=de,en,es,fr,it),"
3281 "(color-supported=%s),"
3282 "(finishings-supported=%s),"
3283 "(sides-supported=one-sided%s),"
3284 "(multiple-document-jobs-supported=true)"
3285 "(ipp-versions-supported=1.0,1.1)",
3286 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3287 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3288 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3290 p
->type
& CUPS_PRINTER_DUPLEX
?
3291 ",two-sided-long-edge,two-sided-short-edge" : "");
3293 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3296 * Register the printer with the SLP server...
3299 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3300 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3302 if (error
!= SLP_OK
)
3303 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3309 * 'slp_attr_callback()' - SLP attribute callback
3312 static SLPBoolean
/* O - SLP_TRUE for success */
3314 SLPHandle hslp
, /* I - SLP handle */
3315 const char *attrlist
, /* I - Attribute list */
3316 SLPError errcode
, /* I - Parsing status for this attr */
3317 void *cookie
) /* I - Current printer */
3319 char *tmp
= 0; /* Temporary string */
3320 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3321 /* Current printer */
3324 (void)hslp
; /* anti-compiler-warning-code */
3327 * Bail if there was an error
3330 if (errcode
!= SLP_OK
)
3334 * Parse the attrlist to obtain things needed to build CUPS browse packet
3337 memset(p
, 0, sizeof(cupsd_printer_t
));
3339 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3341 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3343 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3345 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3346 p
->type
= atoi(tmp
);
3348 p
->type
= CUPS_PRINTER_REMOTE
;
3350 cupsdClearString(&tmp
);
3357 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3361 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3363 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3366 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3368 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3371 * Make the SLP service URL that conforms to the IANA
3372 * 'printer:' template.
3375 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3378 * Deregister the printer...
3381 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3387 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3390 static int /* O - 0 on success */
3391 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3392 const char *tag
, /* I - Name of attribute */
3393 char **valbuf
) /* O - Value */
3395 char *ptr1
, /* Pointer into string */
3399 cupsdClearString(valbuf
);
3401 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3403 ptr1
+= strlen(tag
);
3405 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3411 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3412 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3415 * Dequote the value...
3418 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3419 if (*ptr1
== '\\' && ptr1
[1])
3420 _cups_strcpy(ptr1
, ptr1
+ 1);
3431 * 'slp_reg_callback()' - Empty SLPRegReport.
3435 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3436 SLPError errcode
, /* I - Error code, if any */
3437 void *cookie
) /* I - App data */
3448 * 'slp_url_callback()' - SLP service url callback
3451 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3453 SLPHandle hslp
, /* I - SLP handle */
3454 const char *srvurl
, /* I - URL of service */
3455 unsigned short lifetime
, /* I - Life of service */
3456 SLPError errcode
, /* I - Existing error code */
3457 void *cookie
) /* I - Pointer to service list */
3459 slpsrvurl_t
*s
, /* New service entry */
3460 **head
; /* Pointer to head of entry */
3464 * Let the compiler know we won't be using these vars...
3471 * Bail if there was an error
3474 if (errcode
!= SLP_OK
)
3478 * Grab the head of the list...
3481 head
= (slpsrvurl_t
**)cookie
;
3484 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3487 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3491 * Copy the SLP service URL...
3494 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3497 * Link the SLP service URL into the head of the list
3507 #endif /* HAVE_LIBSLP */
3511 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3515 update_cups_browse(void)
3517 int i
; /* Looping var */
3518 int auth
; /* Authorization status */
3519 int len
; /* Length of name string */
3520 int bytes
; /* Number of bytes left */
3521 char packet
[1541], /* Broadcast packet */
3522 *pptr
; /* Pointer into packet */
3523 socklen_t srclen
; /* Length of source address */
3524 http_addr_t srcaddr
; /* Source address */
3525 char srcname
[1024]; /* Source hostname */
3526 unsigned address
[4]; /* Source address */
3527 unsigned type
; /* Printer type */
3528 unsigned state
; /* Printer state */
3529 char uri
[HTTP_MAX_URI
], /* Printer URI */
3530 host
[HTTP_MAX_URI
], /* Host portion of URI */
3531 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3532 info
[IPP_MAX_NAME
], /* Information string */
3533 location
[IPP_MAX_NAME
], /* Location string */
3534 make_model
[IPP_MAX_NAME
];/* Make and model string */
3535 int num_attrs
; /* Number of attributes */
3536 cups_option_t
*attrs
; /* Attributes */
3540 * Read a packet from the browse socket...
3543 srclen
= sizeof(srcaddr
);
3544 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3545 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3548 * "Connection refused" is returned under Linux if the destination port
3549 * or address is unreachable from a previous sendto(); check for the
3550 * error here and ignore it for now...
3553 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3555 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3557 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
3559 cupsdStopBrowsing();
3566 packet
[bytes
] = '\0';
3569 * If we're about to sleep, ignore incoming browse packets.
3576 * Figure out where it came from...
3580 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3582 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3583 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3584 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3585 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3588 #endif /* AF_INET6 */
3593 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3596 if (HostNameLookups
)
3597 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3599 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3601 len
= strlen(srcname
);
3609 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3612 * Access from localhost (127.0.0.1) is always allowed...
3620 * Do authorization checks on the domain/address...
3623 switch (BrowseACL
->order_type
)
3626 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
3629 case AUTH_ALLOW
: /* Order Deny,Allow */
3632 if (cupsdCheckAuth(address
, srcname
, len
,
3633 BrowseACL
->num_deny
, BrowseACL
->deny
))
3636 if (cupsdCheckAuth(address
, srcname
, len
,
3637 BrowseACL
->num_allow
, BrowseACL
->allow
))
3641 case AUTH_DENY
: /* Order Allow,Deny */
3644 if (cupsdCheckAuth(address
, srcname
, len
,
3645 BrowseACL
->num_allow
, BrowseACL
->allow
))
3648 if (cupsdCheckAuth(address
, srcname
, len
,
3649 BrowseACL
->num_deny
, BrowseACL
->deny
))
3658 if (auth
== AUTH_DENY
)
3660 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3661 "update_cups_browse: Refused %d bytes from %s", bytes
,
3666 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3667 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3674 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3676 cupsdLogMessage(CUPSD_LOG_WARN
,
3677 "update_cups_browse: Garbled browse packet - %s", packet
);
3681 strcpy(location
, "Location Unknown");
3682 strcpy(info
, "No Information Available");
3683 make_model
[0] = '\0';
3687 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3690 * Have extended information; can't use sscanf for it because not all
3691 * sscanf's allow empty strings with %[^\"]...
3694 for (i
= 0, pptr
++;
3695 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3697 location
[i
] = *pptr
;
3705 while (*pptr
&& isspace(*pptr
& 255))
3710 for (i
= 0, pptr
++;
3711 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3720 while (*pptr
&& isspace(*pptr
& 255))
3725 for (i
= 0, pptr
++;
3726 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3728 make_model
[i
] = *pptr
;
3733 make_model
[i
] = '\0';
3736 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3742 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3743 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3744 type
, state
, uri
, location
, info
, make_model
));
3747 * Pull the URI apart to see if this is a local or remote printer...
3750 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
3752 cupsFreeOptions(num_attrs
, attrs
);
3760 for (i
= 0; i
< NumRelays
; i
++)
3761 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
3762 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3763 (struct sockaddr
*)&(Relays
[i
].to
),
3764 httpAddrLength(&(Relays
[i
].to
))) <= 0)
3766 cupsdLogMessage(CUPSD_LOG_ERROR
,
3767 "update_cups_browse: sendto failed for relay %d - %s.",
3768 i
+ 1, strerror(errno
));
3769 cupsFreeOptions(num_attrs
, attrs
);
3774 * Process the browse data...
3777 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
3778 (ipp_pstate_t
)state
, location
, info
, make_model
,
3784 * 'update_polling()' - Read status messages from the poll daemons.
3788 update_polling(void)
3790 char *ptr
, /* Pointer to end of line in buffer */
3791 message
[1024]; /* Pointer to message text */
3792 int loglevel
; /* Log level for message */
3795 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
3796 message
, sizeof(message
))) != NULL
)
3797 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
3800 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
3803 * All polling processes have died; stop polling...
3806 cupsdLogMessage(CUPSD_LOG_ERROR
,
3807 "update_polling: all polling processes have exited!");
3814 * End of "$Id: dirsvc.c 6691 2007-07-19 19:09:46Z mike $".