2 * "$Id: dirsvc.c 6590 2007-06-21 18:22:22Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdDeregisterPrinter() - Stop sending broadcast information for a
27 * local printer and remove any pending
28 * references to remote printers.
29 * cupsdLoadRemoteCache() - Load the remote printer cache.
30 * cupsdRegisterPrinter() - Start sending broadcast information for a
31 * printer update the broadcast contents.
32 * cupsdSaveRemoteCache() - Save the remote printer cache.
33 * cupsdSendBrowseList() - Send new browsing information as necessary.
34 * cupsdStartBrowsing() - Start sending and receiving broadcast
36 * cupsdStartPolling() - Start polling servers as needed.
37 * cupsdStopBrowsing() - Stop sending and receiving broadcast
39 * cupsdStopPolling() - Stop polling servers as needed.
40 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
41 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
42 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
43 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
44 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
46 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the
48 * dnssdRegisterCallback() - DNSServiceRegister callback.
49 * dnssdRegisterPrinter() - Start sending broadcast information for a
50 * printer or update the broadcast contents.
51 * dequote() - Remote quotes from a string.
52 * process_browse_data() - Process new browse data.
53 * process_implicit_classes() - Create/update implicit classes as needed.
54 * send_cups_browse() - Send new browsing information using the
56 * send_ldap_browse() - Send LDAP printer registrations.
57 * send_slp_browse() - Register the specified printer with SLP.
58 * slp_attr_callback() - SLP attribute callback
59 * slp_dereg_printer() - SLPDereg() the specified printer
60 * slp_get_attr() - Get an attribute from an SLP registration.
61 * slp_reg_callback() - Empty SLPRegReport.
62 * slp_url_callback() - SLP service url callback
63 * update_cups_browse() - Update the browse lists using the CUPS
65 * update_polling() - Read status messages from the poll daemons.
69 * Include necessary headers...
79 # ifdef HAVE_COREFOUNDATION
80 # include <CoreFoundation/CoreFoundation.h>
81 # endif /* HAVE_COREFOUNDATION */
82 # ifdef HAVE_SYSTEMCONFIGURATION
83 # include <SystemConfiguration/SystemConfiguration.h>
84 # endif /* HAVE_SYSTEMCONFIGURATION */
85 #endif /* HAVE_DNSSD */
92 static char *dequote(char *d
, const char *s
, int dlen
);
93 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
94 char *resource
, int resourcelen
);
95 static void process_browse_data(const char *uri
, const char *host
,
96 const char *resource
, cups_ptype_t type
,
97 ipp_pstate_t state
, const char *location
,
98 const char *info
, const char *make_model
,
99 int num_attrs
, cups_option_t
*attrs
);
100 static void process_implicit_classes(void);
101 static void send_cups_browse(cupsd_printer_t
*p
);
103 static void send_ldap_browse(cupsd_printer_t
*p
);
104 #endif /* HAVE_LDAP */
106 static void send_slp_browse(cupsd_printer_t
*p
);
107 #endif /* HAVE_LIBSLP */
108 static void update_cups_browse(void);
109 static void update_polling(void);
113 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
115 "printerDescription",
117 "printerMakeAndModel",
122 #endif /* HAVE_OPENLDAP */
130 * SLP service name for CUPS...
133 # define SLP_CUPS_SRVTYPE "service:printer"
134 # define SLP_CUPS_SRVLEN 15
138 * Printer service URL structure
141 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
143 struct _slpsrvurl_s
*next
; /* Next URL in list */
144 char url
[HTTP_MAX_URI
];
153 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
154 SLPError errcode
, void *cookie
);
155 static void slp_dereg_printer(cupsd_printer_t
*p
);
156 static int slp_get_attr(const char *attrlist
, const char *tag
,
158 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
160 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
161 unsigned short lifetime
,
162 SLPError errcode
, void *cookie
);
163 #endif /* HAVE_LIBSLP */
167 * For IPP register using a subtype of 'cups' so that shared printer browsing
168 * only finds other CUPS servers (not all IPP based printers).
170 static char dnssdIPPRegType
[] = "_ipp._tcp,_cups";
171 static char dnssdIPPFaxRegType
[] = "_fax-ipp._tcp";
173 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
);
174 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
175 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
177 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
178 DNSServiceFlags flags
,
179 DNSServiceErrorType errorCode
,
180 const char *name
, const char *regtype
,
181 const char *domain
, void *context
);
182 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
183 #endif /* HAVE_DNSSD */
187 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
188 * local printer and remove any pending
189 * references to remote printers.
193 cupsdDeregisterPrinter(
194 cupsd_printer_t
*p
, /* I - Printer to register */
195 int removeit
) /* I - Printer being permanently removed */
198 * Only deregister if browsing is enabled and it's a local printers...
201 if (!Browsing
|| !p
->shared
||
202 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
206 * Announce the deletion...
209 if ((BrowseLocalProtocols
& BROWSE_CUPS
))
211 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
213 p
->type
|= CUPS_PRINTER_DELETE
;
221 if (BrowseLocalProtocols
& BROWSE_SLP
)
222 slp_dereg_printer(p
);
223 #endif /* HAVE_LIBSLP */
226 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
))
227 dnssdDeregisterPrinter(p
);
228 #endif /* HAVE_DNSSD */
233 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
237 cupsdLoadRemoteCache(void)
239 cups_file_t
*fp
; /* remote.cache file */
240 int linenum
; /* Current line number */
241 char line
[1024], /* Line from file */
242 *value
, /* Pointer to value */
243 *valueptr
, /* Pointer into value */
244 scheme
[32], /* Scheme portion of URI */
245 username
[64], /* Username portion of URI */
247 /* Hostname portion of URI */
248 resource
[HTTP_MAX_URI
];
249 /* Resource portion of URI */
250 int port
; /* Port number */
251 cupsd_printer_t
*p
; /* Current printer */
252 time_t now
; /* Current time */
256 * Don't load the cache if the CUPS remote protocol is disabled...
259 if (!Browsing
|| !(BrowseRemoteProtocols
& BROWSE_CUPS
))
261 cupsdLogMessage(CUPSD_LOG_DEBUG
,
262 "cupsdLoadRemoteCache: Not loading remote cache.");
267 * Open the remote.cache file...
270 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
271 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
275 * Read printer configurations until we hit EOF...
282 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
285 * Decode the directive...
288 if (!strcasecmp(line
, "<Printer") ||
289 !strcasecmp(line
, "<DefaultPrinter"))
292 * <Printer name> or <DefaultPrinter name>
295 if (p
== NULL
&& value
)
298 * Add the printer and a base file type...
301 cupsdLogMessage(CUPSD_LOG_DEBUG
,
302 "cupsdLoadRemoteCache: Loading printer %s...", value
);
304 if ((p
= cupsdFindDest(value
)) != NULL
)
306 if (p
->type
& CUPS_PRINTER_CLASS
)
308 cupsdLogMessage(CUPSD_LOG_WARN
,
309 "Cached remote printer \"%s\" conflicts with "
317 p
= cupsdAddPrinter(value
);
320 p
->state
= IPP_PRINTER_IDLE
;
321 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
322 p
->browse_time
= now
;
323 p
->browse_expire
= now
+ BrowseTimeout
;
326 * Set the default printer as needed...
329 if (!strcasecmp(line
, "<DefaultPrinter"))
334 cupsdLogMessage(CUPSD_LOG_ERROR
,
335 "Syntax error on line %d of remote.cache.", linenum
);
339 else if (!strcasecmp(line
, "<Class") ||
340 !strcasecmp(line
, "<DefaultClass"))
343 * <Class name> or <DefaultClass name>
346 if (p
== NULL
&& value
)
349 * Add the printer and a base file type...
352 cupsdLogMessage(CUPSD_LOG_DEBUG
,
353 "cupsdLoadRemoteCache: Loading class %s...", value
);
355 if ((p
= cupsdFindDest(value
)) != NULL
)
356 p
->type
= CUPS_PRINTER_CLASS
;
358 p
= cupsdAddClass(value
);
361 p
->state
= IPP_PRINTER_IDLE
;
362 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
363 p
->browse_time
= now
;
364 p
->browse_expire
= now
+ BrowseTimeout
;
367 * Set the default printer as needed...
370 if (!strcasecmp(line
, "<DefaultClass"))
375 cupsdLogMessage(CUPSD_LOG_ERROR
,
376 "Syntax error on line %d of remote.cache.", linenum
);
380 else if (!strcasecmp(line
, "</Printer>") ||
381 !strcasecmp(line
, "</Class>"))
386 * Close out the current printer...
389 cupsdSetPrinterAttrs(p
);
395 cupsdLogMessage(CUPSD_LOG_ERROR
,
396 "Syntax error on line %d of remote.cache.", linenum
);
402 cupsdLogMessage(CUPSD_LOG_ERROR
,
403 "Syntax error on line %d of remote.cache.", linenum
);
406 else if (!strcasecmp(line
, "Info"))
409 cupsdSetString(&p
->info
, value
);
411 else if (!strcasecmp(line
, "MakeModel"))
414 cupsdSetString(&p
->make_model
, value
);
416 else if (!strcasecmp(line
, "Location"))
419 cupsdSetString(&p
->location
, value
);
421 else if (!strcasecmp(line
, "DeviceURI"))
425 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
426 username
, sizeof(username
), host
, sizeof(host
), &port
,
427 resource
, sizeof(resource
));
429 cupsdSetString(&p
->hostname
, host
);
430 cupsdSetString(&p
->uri
, value
);
431 cupsdSetString(&p
->device_uri
, value
);
435 cupsdLogMessage(CUPSD_LOG_ERROR
,
436 "Syntax error on line %d of remote.cache.", linenum
);
440 else if (!strcasecmp(line
, "Option") && value
)
446 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
449 cupsdLogMessage(CUPSD_LOG_ERROR
,
450 "Syntax error on line %d of remote.cache.", linenum
);
453 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
455 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
459 else if (!strcasecmp(line
, "State"))
462 * Set the initial queue state...
465 if (value
&& !strcasecmp(value
, "idle"))
466 p
->state
= IPP_PRINTER_IDLE
;
467 else if (value
&& !strcasecmp(value
, "stopped"))
468 p
->state
= IPP_PRINTER_STOPPED
;
471 cupsdLogMessage(CUPSD_LOG_ERROR
,
472 "Syntax error on line %d of remote.cache.", linenum
);
476 else if (!strcasecmp(line
, "StateMessage"))
479 * Set the initial queue state message...
483 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
485 else if (!strcasecmp(line
, "Accepting"))
488 * Set the initial accepting state...
492 (!strcasecmp(value
, "yes") ||
493 !strcasecmp(value
, "on") ||
494 !strcasecmp(value
, "true")))
497 (!strcasecmp(value
, "no") ||
498 !strcasecmp(value
, "off") ||
499 !strcasecmp(value
, "false")))
503 cupsdLogMessage(CUPSD_LOG_ERROR
,
504 "Syntax error on line %d of remote.cache.", linenum
);
508 else if (!strcasecmp(line
, "Type"))
511 p
->type
= atoi(value
);
514 cupsdLogMessage(CUPSD_LOG_ERROR
,
515 "Syntax error on line %d of remote.cache.", linenum
);
519 else if (!strcasecmp(line
, "BrowseTime"))
523 time_t t
= atoi(value
);
525 if (t
> p
->browse_expire
)
526 p
->browse_expire
= t
;
530 cupsdLogMessage(CUPSD_LOG_ERROR
,
531 "Syntax error on line %d of remote.cache.", linenum
);
535 else if (!strcasecmp(line
, "JobSheets"))
538 * Set the initial job sheets...
543 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
548 cupsdSetString(&p
->job_sheets
[0], value
);
550 while (isspace(*valueptr
& 255))
555 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
560 cupsdSetString(&p
->job_sheets
[1], value
);
565 cupsdLogMessage(CUPSD_LOG_ERROR
,
566 "Syntax error on line %d of remote.cache.", linenum
);
570 else if (!strcasecmp(line
, "AllowUser"))
575 cupsdAddPrinterUser(p
, value
);
579 cupsdLogMessage(CUPSD_LOG_ERROR
,
580 "Syntax error on line %d of remote.cache.", linenum
);
584 else if (!strcasecmp(line
, "DenyUser"))
589 cupsdAddPrinterUser(p
, value
);
593 cupsdLogMessage(CUPSD_LOG_ERROR
,
594 "Syntax error on line %d of remote.cache.", linenum
);
601 * Something else we don't understand...
604 cupsdLogMessage(CUPSD_LOG_ERROR
,
605 "Unknown configuration directive %s on line %d of remote.cache.",
613 * Do auto-classing if needed...
616 process_implicit_classes();
621 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
622 * printer or update the broadcast contents.
626 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
628 if (!Browsing
|| !BrowseLocalProtocols
|| !BrowseInterval
|| !NumBrowsers
||
629 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
633 /* if (BrowseLocalProtocols & BROWSE_SLP)
634 slpRegisterPrinter(p); */
635 #endif /* HAVE_LIBSLP */
638 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
639 dnssdRegisterPrinter(p
);
640 #endif /* HAVE_DNSSD */
645 * 'cupsdRestartPolling()' - Restart polling servers as needed.
649 cupsdRestartPolling(void)
651 int i
; /* Looping var */
652 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
655 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
657 kill(pollp
->pid
, SIGHUP
);
662 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
666 cupsdSaveRemoteCache(void)
668 int i
; /* Looping var */
669 cups_file_t
*fp
; /* printers.conf file */
670 char temp
[1024]; /* Temporary string */
671 cupsd_printer_t
*printer
; /* Current printer class */
672 time_t curtime
; /* Current time */
673 struct tm
*curdate
; /* Current date */
674 cups_option_t
*option
; /* Current option */
678 * Create the remote.cache file...
681 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
683 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
685 cupsdLogMessage(CUPSD_LOG_ERROR
,
686 "Unable to save remote.cache - %s", strerror(errno
));
690 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
693 * Restrict access to the file...
696 fchown(cupsFileNumber(fp
), getuid(), Group
);
697 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
700 * Write a small header to the file...
703 curtime
= time(NULL
);
704 curdate
= localtime(&curtime
);
705 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
707 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
708 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
711 * Write each local printer known to the system...
714 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
716 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
719 * Skip local destinations...
722 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
726 * Write printers as needed...
729 if (printer
== DefaultPrinter
)
730 cupsFilePuts(fp
, "<Default");
732 cupsFilePutChar(fp
, '<');
734 if (printer
->type
& CUPS_PRINTER_CLASS
)
735 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
737 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
739 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
741 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
744 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
746 if (printer
->make_model
)
747 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
749 if (printer
->location
)
750 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
752 if (printer
->device_uri
)
753 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
755 if (printer
->state
== IPP_PRINTER_STOPPED
)
757 cupsFilePuts(fp
, "State Stopped\n");
758 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
761 cupsFilePuts(fp
, "State Idle\n");
763 if (printer
->accepting
)
764 cupsFilePuts(fp
, "Accepting Yes\n");
766 cupsFilePuts(fp
, "Accepting No\n");
768 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
769 printer
->job_sheets
[1]);
771 for (i
= 0; i
< printer
->num_users
; i
++)
772 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
775 for (i
= printer
->num_options
, option
= printer
->options
;
778 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
780 if (printer
->type
& CUPS_PRINTER_CLASS
)
781 cupsFilePuts(fp
, "</Class>\n");
783 cupsFilePuts(fp
, "</Printer>\n");
791 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
795 cupsdSendBrowseList(void)
797 int count
; /* Number of dests to update */
798 cupsd_printer_t
*p
; /* Current printer */
799 time_t ut
, /* Minimum update time */
800 to
; /* Timeout time */
803 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
807 * Compute the update and timeout times...
811 ut
= to
- BrowseInterval
;
814 * Figure out how many printers need an update...
817 if (BrowseInterval
> 0)
819 int max_count
; /* Maximum number to update */
823 * Throttle the number of printers we'll be updating this time
824 * around based on the number of queues that need updating and
825 * the maximum number of queues to update each second...
828 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
830 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
831 count
< max_count
&& p
!= NULL
;
832 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
833 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
834 p
->shared
&& p
->browse_time
< ut
)
838 * Loop through all of the printers and send local updates as needed...
842 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
844 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
848 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
851 * Check for wraparound...
855 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
859 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
862 else if (p
->browse_time
< ut
)
865 * Need to send an update...
870 p
->browse_time
= time(NULL
);
872 if (BrowseLocalProtocols
& BROWSE_CUPS
)
876 if (BrowseLocalProtocols
& BROWSE_SLP
)
878 #endif /* HAVE_LIBSLP */
881 if (BrowseLocalProtocols
& BROWSE_LDAP
)
883 #endif /* HAVE_LDAP */
888 * Save where we left off so that all printers get updated...
895 * Loop through all of the printers and send local updates as needed...
898 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
900 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
903 * If this is a remote queue, see if it needs to be timed out...
906 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
908 if (p
->browse_expire
< to
)
910 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
911 "%s \'%s\' deleted by directory services (timeout).",
912 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
915 cupsdLogMessage(CUPSD_LOG_DEBUG
,
916 "Remote destination \"%s\" has timed out; "
920 cupsArraySave(Printers
);
921 cupsdDeletePrinter(p
, 1);
922 cupsArrayRestore(Printers
);
930 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
934 cupsdStartBrowsing(void)
936 int val
; /* Socket option value */
937 struct sockaddr_in addr
; /* Broadcast address */
938 cupsd_printer_t
*p
; /* Current printer */
943 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
946 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
948 if (BrowseSocket
< 0)
951 * Create the broadcast socket...
954 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
956 cupsdLogMessage(CUPSD_LOG_ERROR
,
957 "cupsdStartBrowsing: Unable to create broadcast "
958 "socket - %s.", strerror(errno
));
959 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
960 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
965 * Bind the socket to browse port...
968 memset(&addr
, 0, sizeof(addr
));
969 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
970 addr
.sin_family
= AF_INET
;
971 addr
.sin_port
= htons(BrowsePort
);
973 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
975 cupsdLogMessage(CUPSD_LOG_ERROR
,
976 "cupsdStartBrowsing: Unable to bind broadcast "
977 "socket - %s.", strerror(errno
));
980 closesocket(BrowseSocket
);
986 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
987 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
993 * Set the "broadcast" flag...
997 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
999 cupsdLogMessage(CUPSD_LOG_ERROR
,
1000 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1004 closesocket(BrowseSocket
);
1006 close(BrowseSocket
);
1010 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1011 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1016 * Close the socket on exec...
1019 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1022 * Finally, add the socket to the input selection set as needed...
1025 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1028 * We only listen if we want remote printers...
1031 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1039 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1042 * Open SLP handle...
1045 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1047 cupsdLogMessage(CUPSD_LOG_ERROR
,
1048 "Unable to open an SLP handle; disabling SLP browsing!");
1049 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1050 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1053 BrowseSLPRefresh
= 0;
1056 BrowseSLPHandle
= NULL
;
1057 #endif /* HAVE_LIBSLP */
1059 #ifdef HAVE_OPENLDAP
1060 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1064 cupsdLogMessage(CUPSD_LOG_ERROR
,
1065 "Need to set BrowseLDAPDN to use LDAP browsing!");
1066 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1067 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1072 * Open LDAP handle...
1075 int rc
; /* LDAP API status */
1076 int version
= 3; /* LDAP version */
1077 struct berval bv
= {0, ""}; /* SASL bind value */
1081 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1084 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1085 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1087 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1089 if (rc
!= LDAP_SUCCESS
)
1091 cupsdLogMessage(CUPSD_LOG_ERROR
,
1092 "Unable to initialize LDAP; disabling LDAP browsing!");
1093 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1094 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1096 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1097 (const void *)&version
) != LDAP_SUCCESS
)
1099 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1100 BrowseLDAPHandle
= NULL
;
1101 cupsdLogMessage(CUPSD_LOG_ERROR
,
1102 "Unable to set LDAP protocol version; "
1103 "disabling LDAP browsing!");
1104 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1105 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1109 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1110 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1113 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1114 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1116 if (rc
!= LDAP_SUCCESS
)
1118 cupsdLogMessage(CUPSD_LOG_ERROR
,
1119 "Unable to bind to LDAP server; "
1120 "disabling LDAP browsing!");
1121 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1122 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1123 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1128 BrowseLDAPRefresh
= 0;
1130 #endif /* HAVE_OPENLDAP */
1133 * Register the individual printers
1136 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1138 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1139 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1140 cupsdRegisterPrinter(p
);
1145 * 'cupsdStartPolling()' - Start polling servers as needed.
1149 cupsdStartPolling(void)
1151 int i
; /* Looping var */
1152 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1153 char polld
[1024]; /* Poll daemon path */
1154 char sport
[255]; /* Server port */
1155 char bport
[255]; /* Browser port */
1156 char interval
[255]; /* Poll interval */
1157 int statusfds
[2]; /* Status pipe */
1158 char *argv
[6]; /* Arguments */
1159 char *envp
[100]; /* Environment */
1163 * Don't do anything if we aren't polling...
1166 if (NumPolled
== 0 || BrowseSocket
< 0)
1169 PollStatusBuffer
= NULL
;
1174 * Setup string arguments for polld, port and interval options.
1177 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1179 sprintf(bport
, "%d", BrowsePort
);
1182 sprintf(interval
, "%d", BrowseInterval
);
1184 strcpy(interval
, "30");
1186 argv
[0] = "cups-polld";
1192 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1195 * Create a pipe that receives the status messages from each
1199 if (cupsdOpenPipe(statusfds
))
1201 cupsdLogMessage(CUPSD_LOG_ERROR
,
1202 "Unable to create polling status pipes - %s.",
1205 PollStatusBuffer
= NULL
;
1209 PollPipe
= statusfds
[0];
1210 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1213 * Run each polling daemon, redirecting stderr to the polling pipe...
1216 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1218 sprintf(sport
, "%d", pollp
->port
);
1220 argv
[1] = pollp
->hostname
;
1222 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1223 0, &(pollp
->pid
)) < 0)
1225 cupsdLogMessage(CUPSD_LOG_ERROR
,
1226 "cupsdStartPolling: Unable to fork polling daemon - %s",
1232 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1233 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1234 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1237 close(statusfds
[1]);
1240 * Finally, add the pipe to the input selection set...
1243 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1248 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1252 cupsdStopBrowsing(void)
1254 cupsd_printer_t
*p
; /* Current printer */
1257 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1261 * De-register the individual printers
1264 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1266 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1267 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1268 cupsdDeregisterPrinter(p
, 1);
1271 * Shut down browsing sockets...
1274 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1278 * Close the socket and remove it from the input selection set.
1282 closesocket(BrowseSocket
);
1284 close(BrowseSocket
);
1287 cupsdRemoveSelect(BrowseSocket
);
1292 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1296 * Close SLP handle...
1299 SLPClose(BrowseSLPHandle
);
1300 BrowseSLPHandle
= NULL
;
1302 #endif /* HAVE_LIBSLP */
1304 #ifdef HAVE_OPENLDAP
1305 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1308 ldap_unbind(BrowseLDAPHandle
);
1309 BrowseLDAPHandle
= NULL
;
1311 #endif /* HAVE_OPENLDAP */
1316 * 'cupsdStopPolling()' - Stop polling servers as needed.
1320 cupsdStopPolling(void)
1322 int i
; /* Looping var */
1323 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1328 cupsdStatBufDelete(PollStatusBuffer
);
1331 cupsdRemoveSelect(PollPipe
);
1334 PollStatusBuffer
= NULL
;
1337 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1339 cupsdEndProcess(pollp
->pid
, 0);
1345 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1349 cupsdUpdateDNSSDBrowse(
1350 cupsd_printer_t
*p
) /* I - Printer being queried */
1352 DNSServiceErrorType sdErr
; /* Service discovery error */
1355 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1356 != kDNSServiceErr_NoError
)
1358 cupsdLogMessage(CUPSD_LOG_ERROR
,
1359 "DNS Service Discovery registration error %d for \"%s\"!",
1361 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1362 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1364 p
->dnssd_ipp_ref
= NULL
;
1365 p
->dnssd_ipp_fd
= -1;
1368 #endif /* HAVE_DNSSD */
1371 #ifdef HAVE_OPENLDAP
1373 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1377 cupsdUpdateLDAPBrowse(void)
1379 char uri
[HTTP_MAX_URI
], /* Printer URI */
1380 host
[HTTP_MAX_URI
], /* Hostname */
1381 resource
[HTTP_MAX_URI
], /* Resource path */
1382 location
[1024], /* Printer location */
1383 info
[1024], /* Printer information */
1384 make_model
[1024], /* Printer make and model */
1385 **value
; /* Holds the returned data from LDAP */
1386 int type
; /* Printer type */
1387 int rc
; /* LDAP status */
1388 int limit
; /* Size limit */
1389 LDAPMessage
*res
, /* LDAP search results */
1390 *e
; /* Current entry from search */
1394 * Search for printers...
1397 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1399 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1401 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1402 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1403 if (rc
!= LDAP_SUCCESS
)
1405 cupsdLogMessage(CUPSD_LOG_ERROR
,
1406 "LDAP search returned error %d: %s", rc
,
1407 ldap_err2string(rc
));
1411 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1412 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1417 * Loop through the available printers...
1420 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1422 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1425 * Get the required values from this entry...
1428 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1429 "printerDescription")) == NULL
)
1432 strlcpy(info
, *value
, sizeof(info
));
1433 ldap_value_free(value
);
1435 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1436 "printerLocation")) == NULL
)
1439 strlcpy(location
, *value
, sizeof(location
));
1440 ldap_value_free(value
);
1442 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1443 "printerMakeAndModel")) == NULL
)
1446 strlcpy(make_model
, *value
, sizeof(make_model
));
1447 ldap_value_free(value
);
1449 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1450 "printerType")) == NULL
)
1453 type
= atoi(*value
);
1454 ldap_value_free(value
);
1456 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1457 "printerURI")) == NULL
)
1460 strlcpy(uri
, *value
, sizeof(uri
));
1461 ldap_value_free(value
);
1464 * Process the entry as browse data...
1467 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1468 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1469 location
, info
, make_model
, 0, NULL
);
1473 #endif /* HAVE_OPENLDAP */
1478 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1482 cupsdUpdateSLPBrowse(void)
1484 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1485 *next
; /* Next service in list */
1486 cupsd_printer_t p
; /* Printer information */
1487 const char *uri
; /* Pointer to printer URI */
1488 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1489 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1493 * Reset the refresh time...
1496 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1499 * Poll for remote printers using SLP...
1504 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1505 slp_url_callback
, &s
);
1508 * Loop through the list of available printers...
1514 * Save the "next" pointer...
1520 * Load a cupsd_printer_t structure with the SLP service attributes...
1523 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1526 * Process this printer entry...
1529 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1531 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1534 * Pull the URI apart to see if this is a local or remote printer...
1537 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1538 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1539 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1543 * Free this listing...
1546 cupsdClearString(&p
.info
);
1547 cupsdClearString(&p
.location
);
1548 cupsdClearString(&p
.make_model
);
1553 #endif /* HAVE_LIBSLP */
1557 * 'dequote()' - Remote quotes from a string.
1560 static char * /* O - Dequoted string */
1561 dequote(char *d
, /* I - Destination string */
1562 const char *s
, /* I - Source string */
1563 int dlen
) /* I - Destination length */
1565 char *dptr
; /* Pointer into destination */
1570 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1587 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1590 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1591 is_local_queue(const char *uri
, /* I - Printer URI */
1592 char *host
, /* O - Host string */
1593 int hostlen
, /* I - Length of host buffer */
1594 char *resource
, /* O - Resource string */
1595 int resourcelen
) /* I - Length of resource buffer */
1597 char scheme
[32], /* Scheme portion of URI */
1598 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1599 int port
; /* Port portion of URI */
1600 cupsd_netif_t
*iface
; /* Network interface */
1604 * Pull the URI apart to see if this is a local or remote printer...
1607 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1608 username
, sizeof(username
), host
, hostlen
, &port
,
1609 resource
, resourcelen
) < HTTP_URI_OK
)
1612 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1615 * Check for local server addresses...
1618 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1623 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1625 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1626 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1630 * If we get here, the printer is remote...
1638 * 'process_browse_data()' - Process new browse data.
1642 process_browse_data(
1643 const char *uri
, /* I - URI of printer/class */
1644 const char *host
, /* I - Hostname */
1645 const char *resource
, /* I - Resource path */
1646 cups_ptype_t type
, /* I - Printer type */
1647 ipp_pstate_t state
, /* I - Printer state */
1648 const char *location
, /* I - Printer location */
1649 const char *info
, /* I - Printer information */
1650 const char *make_model
, /* I - Printer make and model */
1651 int num_attrs
, /* I - Number of attributes */
1652 cups_option_t
*attrs
) /* I - Attributes */
1654 int i
; /* Looping var */
1655 int update
; /* Update printer attributes? */
1656 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1657 name
[IPP_MAX_NAME
], /* Name of printer */
1658 newname
[IPP_MAX_NAME
], /* New name of printer */
1659 *hptr
, /* Pointer into hostname */
1660 *sptr
; /* Pointer into ServerName */
1661 char local_make_model
[IPP_MAX_NAME
];
1662 /* Local make and model */
1663 cupsd_printer_t
*p
; /* Printer information */
1664 const char *ipp_options
, /* ipp-options value */
1665 *lease_duration
; /* lease-duration value */
1669 * Determine if the URI contains any illegal characters in it...
1672 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1673 (strncmp(resource
, "/printers/", 10) &&
1674 strncmp(resource
, "/classes/", 9)))
1676 cupsdLogMessage(CUPSD_LOG_ERROR
,
1677 "process_browse_data: Bad printer URI in browse data: %s",
1682 if (strchr(resource
, '?') ||
1683 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1684 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1686 cupsdLogMessage(CUPSD_LOG_ERROR
,
1687 "process_browse_data: Bad resource in browse data: %s",
1693 * OK, this isn't a local printer; add any remote options...
1696 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1698 if (BrowseRemoteOptions
)
1700 if (BrowseRemoteOptions
[0] == '?')
1703 * Override server-supplied options...
1706 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1708 else if (ipp_options
)
1711 * Combine the server and local options...
1714 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1715 BrowseRemoteOptions
);
1720 * Just use the local options...
1723 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1728 else if (ipp_options
)
1731 * Just use the server-supplied options...
1734 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1739 * See if we already have it listed in the Printers list, and add it if not...
1742 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
1743 type
&= ~CUPS_PRINTER_IMPLICIT
;
1745 hptr
= strchr(host
, '.');
1746 sptr
= strchr(ServerName
, '.');
1748 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1751 * Strip the common domain name components...
1754 while (hptr
!= NULL
)
1756 if (!strcasecmp(hptr
, sptr
))
1762 hptr
= strchr(hptr
+ 1, '.');
1766 if (type
& CUPS_PRINTER_CLASS
)
1769 * Remote destination is a class...
1772 if (!strncmp(resource
, "/classes/", 9))
1773 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1778 *hptr
= '.'; /* Resource FQDN */
1780 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1782 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1784 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1787 * Nope, this isn't the same host; if the hostname isn't the local host,
1788 * add it to the other class and then find a class using the full host
1792 if (p
->type
& CUPS_PRINTER_REMOTE
)
1794 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1795 "Renamed remote class \"%s\" to \"%s@%s\"...",
1796 p
->name
, p
->name
, p
->hostname
);
1797 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1798 "Class \'%s\' deleted by directory services.",
1801 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1802 cupsdRenamePrinter(p
, newname
);
1804 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1805 "Class \'%s\' added by directory services.",
1811 else if (!p
->hostname
)
1814 * Hostname not set, so this must be a cached remote printer
1815 * that was created for a pending print job...
1818 cupsdSetString(&p
->hostname
, host
);
1819 cupsdSetString(&p
->uri
, uri
);
1820 cupsdSetString(&p
->device_uri
, uri
);
1827 * Use the short name for this shared class.
1830 strlcpy(name
, resource
+ 9, sizeof(name
));
1833 else if (p
&& !p
->hostname
)
1836 * Hostname not set, so this must be a cached remote printer
1837 * that was created for a pending print job...
1840 cupsdSetString(&p
->hostname
, host
);
1841 cupsdSetString(&p
->uri
, uri
);
1842 cupsdSetString(&p
->device_uri
, uri
);
1849 * Class doesn't exist; add it...
1852 p
= cupsdAddClass(name
);
1854 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1856 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1857 "Class \'%s\' added by directory services.", name
);
1860 * Force the URI to point to the real server...
1863 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1865 cupsdSetString(&p
->uri
, uri
);
1866 cupsdSetString(&p
->device_uri
, uri
);
1867 cupsdSetString(&p
->hostname
, host
);
1875 * Remote destination is a printer...
1878 if (!strncmp(resource
, "/printers/", 10))
1879 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1884 *hptr
= '.'; /* Resource FQDN */
1886 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
1888 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
1890 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1893 * Nope, this isn't the same host; if the hostname isn't the local host,
1894 * add it to the other printer and then find a printer using the full host
1898 if (p
->type
& CUPS_PRINTER_REMOTE
)
1900 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1901 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1902 p
->name
, p
->name
, p
->hostname
);
1903 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1904 "Printer \'%s\' deleted by directory services.",
1907 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1908 cupsdRenamePrinter(p
, newname
);
1910 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1911 "Printer \'%s\' added by directory services.",
1917 else if (!p
->hostname
)
1920 * Hostname not set, so this must be a cached remote printer
1921 * that was created for a pending print job...
1924 cupsdSetString(&p
->hostname
, host
);
1925 cupsdSetString(&p
->uri
, uri
);
1926 cupsdSetString(&p
->device_uri
, uri
);
1933 * Use the short name for this shared printer.
1936 strlcpy(name
, resource
+ 10, sizeof(name
));
1939 else if (p
&& !p
->hostname
)
1942 * Hostname not set, so this must be a cached remote printer
1943 * that was created for a pending print job...
1946 cupsdSetString(&p
->hostname
, host
);
1947 cupsdSetString(&p
->uri
, uri
);
1948 cupsdSetString(&p
->device_uri
, uri
);
1955 * Printer doesn't exist; add it...
1958 p
= cupsdAddPrinter(name
);
1960 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1961 "Printer \'%s\' added by directory services.", name
);
1963 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
1966 * Force the URI to point to the real server...
1969 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1971 cupsdSetString(&p
->hostname
, host
);
1972 cupsdSetString(&p
->uri
, uri
);
1973 cupsdSetString(&p
->device_uri
, uri
);
1980 * Update the state...
1984 p
->browse_time
= time(NULL
);
1986 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
1990 * Grab the lease-duration for the browse data; anything less then 1
1991 * second or more than 1 week gets the default BrowseTimeout...
1994 i
= atoi(lease_duration
);
1995 if (i
< 1 || i
> 604800)
1998 p
->browse_expire
= p
->browse_time
+ i
;
2001 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2003 if (type
& CUPS_PRINTER_REJECTING
)
2005 type
&= ~CUPS_PRINTER_REJECTING
;
2013 else if (!p
->accepting
)
2019 if (p
->type
!= type
)
2025 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2027 cupsdSetString(&p
->location
, location
);
2031 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2033 cupsdSetString(&p
->info
, info
);
2037 if (!make_model
|| !make_model
[0])
2039 if (type
& CUPS_PRINTER_CLASS
)
2040 snprintf(local_make_model
, sizeof(local_make_model
),
2041 "Remote Class on %s", host
);
2043 snprintf(local_make_model
, sizeof(local_make_model
),
2044 "Remote Printer on %s", host
);
2047 snprintf(local_make_model
, sizeof(local_make_model
),
2048 "%s on %s", make_model
, host
);
2050 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2052 cupsdSetString(&p
->make_model
, local_make_model
);
2058 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2061 * See if we need to update the attributes...
2064 if (p
->num_options
!= num_attrs
)
2068 for (i
= 0; i
< num_attrs
; i
++)
2069 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2070 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2071 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2080 * Free the old options...
2083 cupsFreeOptions(p
->num_options
, p
->options
);
2086 p
->num_options
= num_attrs
;
2089 if (type
& CUPS_PRINTER_DELETE
)
2091 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2092 "%s \'%s\' deleted by directory services.",
2093 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2095 cupsdExpireSubscriptions(p
, NULL
);
2097 cupsdDeletePrinter(p
, 1);
2098 cupsdUpdateImplicitClasses();
2102 cupsdSetPrinterAttrs(p
);
2103 cupsdUpdateImplicitClasses();
2107 * See if we have a default printer... If not, make the first network
2108 * default printer the default.
2111 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2114 * Find the first network default printer and use it...
2117 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2119 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2120 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2128 * Do auto-classing if needed...
2131 process_implicit_classes();
2134 * Update the printcap file...
2137 cupsdWritePrintcap();
2143 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2146 static char * /* O - TXT record */
2147 dnssdBuildTxtRecord(
2148 int *txt_len
, /* O - TXT record length */
2149 cupsd_printer_t
*p
) /* I - Printer information */
2151 int i
, j
; /* Looping vars */
2152 char type_str
[32], /* Type to string buffer */
2153 state_str
[32], /* State to string buffer */
2154 rp_str
[1024], /* Queue name string buffer */
2155 air_str
[1024], /* auth-info-required string buffer */
2156 *keyvalue
[32][2]; /* Table of key/value pairs */
2160 * Load up the key value pairs...
2165 keyvalue
[i
][0] = "txtvers";
2166 keyvalue
[i
++][1] = "1";
2168 keyvalue
[i
][0] = "qtotal";
2169 keyvalue
[i
++][1] = "1";
2171 keyvalue
[i
][0] = "rp";
2172 keyvalue
[i
++][1] = rp_str
;
2173 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2174 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2176 keyvalue
[i
][0] = "ty";
2177 keyvalue
[i
++][1] = p
->make_model
;
2179 if (p
->location
&& *p
->location
!= '\0')
2181 keyvalue
[i
][0] = "note";
2182 keyvalue
[i
++][1] = p
->location
;
2185 keyvalue
[i
][0] = "product";
2186 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2188 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2189 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2191 keyvalue
[i
][0] = "printer-state";
2192 keyvalue
[i
++][1] = state_str
;
2194 keyvalue
[i
][0] = "printer-type";
2195 keyvalue
[i
++][1] = type_str
;
2197 keyvalue
[i
][0] = "Transparent";
2198 keyvalue
[i
++][1] = "T";
2200 keyvalue
[i
][0] = "Binary";
2201 keyvalue
[i
++][1] = "T";
2203 if ((p
->type
& CUPS_PRINTER_FAX
))
2205 keyvalue
[i
][0] = "Fax";
2206 keyvalue
[i
++][1] = "T";
2209 if ((p
->type
& CUPS_PRINTER_COLOR
))
2211 keyvalue
[i
][0] = "Color";
2212 keyvalue
[i
++][1] = "T";
2215 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2217 keyvalue
[i
][0] = "Duplex";
2218 keyvalue
[i
++][1] = "T";
2221 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2223 keyvalue
[i
][0] = "Staple";
2224 keyvalue
[i
++][1] = "T";
2227 if ((p
->type
& CUPS_PRINTER_COPIES
))
2229 keyvalue
[i
][0] = "Copies";
2230 keyvalue
[i
++][1] = "T";
2233 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2235 keyvalue
[i
][0] = "Collate";
2236 keyvalue
[i
++][1] = "T";
2239 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2241 keyvalue
[i
][0] = "Punch";
2242 keyvalue
[i
++][1] = "T";
2245 if ((p
->type
& CUPS_PRINTER_BIND
))
2247 keyvalue
[i
][0] = "Bind";
2248 keyvalue
[i
++][1] = "T";
2251 if ((p
->type
& CUPS_PRINTER_SORT
))
2253 keyvalue
[i
][0] = "Sort";
2254 keyvalue
[i
++][1] = "T";
2257 keyvalue
[i
][0] = "pdl";
2258 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2260 if (p
->num_auth_info_required
)
2262 char *air
= air_str
; /* Pointer into string */
2265 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2267 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2273 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2277 keyvalue
[i
][0] = "air";
2278 keyvalue
[i
++][1] = air
;
2282 * Then pack them into a proper txt record...
2285 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2290 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2295 dnssdDeregisterPrinter(
2296 cupsd_printer_t
*p
) /* I - Printer */
2298 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2301 * Closing the socket deregisters the service
2304 if (p
->dnssd_ipp_ref
)
2306 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2307 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2308 p
->dnssd_ipp_ref
= NULL
;
2309 p
->dnssd_ipp_fd
= -1;
2312 cupsdClearString(&p
->reg_name
);
2317 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2320 free(p
->txt_record
);
2321 p
->txt_record
= NULL
;
2327 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2328 * TXT record format.
2331 static char * /* O - TXT record */
2332 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2333 char *keyvalue
[][2], /* I - Table of key value pairs */
2334 int count
) /* I - Items in table */
2336 int i
; /* Looping var */
2337 int length
; /* Length of TXT record */
2338 int length2
; /* Length of value */
2339 char *txtRecord
; /* TXT record buffer */
2340 char *cursor
; /* Looping pointer */
2344 * Calculate the buffer size
2347 for (length
= i
= 0; i
< count
; i
++)
2348 length
+= 1 + strlen(keyvalue
[i
][0]) +
2349 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2352 * Allocate and fill it
2355 txtRecord
= malloc(length
);
2360 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2363 * Drop in the p-string style length byte followed by the data
2366 length
= strlen(keyvalue
[i
][0]);
2367 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2369 *cursor
++ = (unsigned char)(length
+ length2
);
2371 memcpy(cursor
, keyvalue
[i
][0], length
);
2378 memcpy(cursor
, keyvalue
[i
][1], length2
);
2389 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2393 dnssdRegisterCallback(
2394 DNSServiceRef sdRef
, /* I - DNS Service reference */
2395 DNSServiceFlags flags
, /* I - Reserved for future use */
2396 DNSServiceErrorType errorCode
, /* I - Error code */
2397 const char *name
, /* I - Service name */
2398 const char *regtype
, /* I - Service type */
2399 const char *domain
, /* I - Domain. ".local" for now */
2400 void *context
) /* I - User-defined context */
2404 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2405 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2409 cupsdLogMessage(CUPSD_LOG_ERROR
,
2410 "DNSServiceRegister failed with error %d", (int)errorCode
);
2417 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2418 * or update the broadcast contents.
2422 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2424 DNSServiceErrorType se
; /* dnssd errors */
2425 cupsd_listener_t
*lis
; /* Current listening socket */
2426 char *txt_record
, /* TXT record buffer */
2427 *name
; /* Service name */
2428 int txt_len
, /* TXT record length */
2429 port
; /* IPP port number */
2430 char str_buffer
[1024];
2431 /* C-string buffer */
2432 const char *computerName
; /* Computer name c-string ptr */
2433 const char *regtype
; /* Registration type */
2434 #ifdef HAVE_COREFOUNDATION_H
2435 CFStringRef computerNameRef
;/* Computer name CFString */
2436 CFStringEncoding nameEncoding
; /* Computer name encoding */
2437 CFMutableStringRef shortNameRef
; /* Mutable name string */
2438 CFIndex nameLength
; /* Name string length */
2440 int nameLength
; /* Name string length */
2441 #endif /* HAVE_COREFOUNDATION_H */
2444 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2445 !p
->dnssd_ipp_ref
? "new" : "update");
2448 * If per-printer sharing was just disabled make sure we're not
2449 * registered before returning.
2454 dnssdDeregisterPrinter(p
);
2459 * Get the computer name as a c-string...
2462 #ifdef HAVE_COREFOUNDATION_H
2463 computerName
= NULL
;
2464 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2465 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2466 kCFStringEncodingUTF8
)) == NULL
)
2467 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2468 kCFStringEncodingUTF8
))
2469 computerName
= str_buffer
;
2471 computerName
= ServerName
;
2472 #endif /* HAVE_COREFOUNDATION_H */
2475 * The registered name takes the form of "<printer-info> @ <computer name>"...
2480 cupsdSetStringf(&name
, "%s @ %s",
2481 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2484 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2486 #ifdef HAVE_COREFOUNDATION_H
2487 if (computerNameRef
)
2488 CFRelease(computerNameRef
);
2489 #endif /* HAVE_COREFOUNDATION_H */
2492 * If an existing printer was renamed, unregister it and start over...
2495 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2496 dnssdDeregisterPrinter(p
);
2498 txt_len
= 0; /* anti-compiler-warning-code */
2499 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2501 if (!p
->dnssd_ipp_ref
)
2504 * Initial registration...
2507 cupsdSetString(&p
->reg_name
, name
);
2511 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2513 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2515 if (lis
->address
.addr
.sa_family
== AF_INET
)
2517 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2520 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2522 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2528 * Use the _fax subtype for fax queues...
2531 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2534 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) type is \"%s\"",
2537 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2538 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2539 dnssdRegisterCallback
, p
);
2542 * In case the name is too long, try shortening the string one character
2546 if (se
== kDNSServiceErr_BadParam
)
2548 #ifdef HAVE_COREFOUNDATION_H
2549 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2551 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2552 nameLength
= CFStringGetLength(shortNameRef
);
2554 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2556 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2557 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2558 kCFStringEncodingUTF8
))
2560 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2561 regtype
, NULL
, NULL
, htons(port
),
2562 txt_len
, txt_record
,
2563 dnssdRegisterCallback
, p
);
2567 CFRelease(shortNameRef
);
2570 nameLength
= strlen(name
);
2571 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2573 name
[--nameLength
] = '\0';
2574 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2575 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2576 dnssdRegisterCallback
, p
);
2578 #endif /* HAVE_COREFOUNDATION_H */
2581 if (se
== kDNSServiceErr_NoError
)
2583 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2584 p
->txt_record
= txt_record
;
2585 p
->txt_len
= txt_len
;
2588 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2592 cupsdLogMessage(CUPSD_LOG_WARN
,
2593 "DNS-SD registration of \"%s\" failed with %d",
2596 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2599 * Update the existing registration...
2602 /* A TTL of 0 means use record's original value (Radar 3176248) */
2603 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2604 txt_len
, txt_record
, 0);
2607 free(p
->txt_record
);
2609 p
->txt_record
= txt_record
;
2610 p
->txt_len
= txt_len
;
2617 cupsdClearString(&name
);
2619 #endif /* HAVE_DNSSD */
2623 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2627 process_implicit_classes(void)
2629 int i
; /* Looping var */
2630 int update
; /* Update printer attributes? */
2631 char name
[IPP_MAX_NAME
], /* Name of printer */
2632 *hptr
; /* Pointer into hostname */
2633 cupsd_printer_t
*p
, /* Printer information */
2634 *pclass
, /* Printer class */
2635 *first
; /* First printer in class */
2636 int offset
, /* Offset of name */
2637 len
; /* Length of name */
2640 if (!ImplicitClasses
|| !Printers
)
2644 * Loop through all available printers and create classes as needed...
2647 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2648 update
= 0, pclass
= NULL
, first
= NULL
;
2650 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2653 * Skip implicit classes...
2656 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2663 * If len == 0, get the length of this printer name up to the "@"
2667 cupsArraySave(Printers
);
2670 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2671 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2674 * We have more than one printer with the same name; see if
2675 * we have a class, and if this printer is a member...
2678 if (pclass
&& strcasecmp(pclass
->name
, name
))
2681 cupsdSetPrinterAttrs(pclass
);
2687 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2690 * Need to add the class...
2693 pclass
= cupsdAddPrinter(name
);
2694 cupsArrayAdd(ImplicitPrinters
, pclass
);
2696 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2697 pclass
->accepting
= 1;
2698 pclass
->state
= IPP_PRINTER_IDLE
;
2700 cupsdSetString(&pclass
->location
, p
->location
);
2701 cupsdSetString(&pclass
->info
, p
->info
);
2703 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2704 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2708 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2710 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2711 "Implicit class \'%s\' added by directory services.",
2717 for (i
= 0; i
< pclass
->num_printers
; i
++)
2718 if (pclass
->printers
[i
] == first
)
2721 if (i
>= pclass
->num_printers
)
2723 first
->in_implicit_class
= 1;
2724 cupsdAddPrinterToClass(pclass
, first
);
2730 for (i
= 0; i
< pclass
->num_printers
; i
++)
2731 if (pclass
->printers
[i
] == p
)
2734 if (i
>= pclass
->num_printers
)
2736 p
->in_implicit_class
= 1;
2737 cupsdAddPrinterToClass(pclass
, p
);
2744 * First time around; just get name length and mark it as first
2748 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2749 len
= hptr
- p
->name
;
2751 len
= strlen(p
->name
);
2753 strncpy(name
, p
->name
, len
);
2757 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2758 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2761 * Can't use same name as a local printer; add "Any" to the
2762 * front of the name, unless we have explicitly disabled
2763 * the "ImplicitAnyClasses"...
2766 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2769 * Add "Any" to the class name...
2772 strcpy(name
, "Any");
2773 strncpy(name
+ 3, p
->name
, len
);
2774 name
[len
+ 3] = '\0';
2780 * Don't create an implicit class if we have a local printer
2781 * with the same name...
2785 cupsArrayRestore(Printers
);
2793 cupsArrayRestore(Printers
);
2797 * Update the last printer class as needed...
2800 if (pclass
&& update
)
2801 cupsdSetPrinterAttrs(pclass
);
2806 * 'send_cups_browse()' - Send new browsing information using the CUPS
2811 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2813 int i
; /* Looping var */
2814 cups_ptype_t type
; /* Printer type */
2815 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2816 int bytes
; /* Length of packet */
2817 char packet
[1453], /* Browse data packet */
2818 uri
[1024], /* Printer URI */
2819 location
[1024], /* printer-location */
2820 info
[1024], /* printer-info */
2822 /* printer-make-and-model */
2823 cupsd_netif_t
*iface
; /* Network interface */
2827 * Figure out the printer type value...
2830 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2833 type
|= CUPS_PRINTER_REJECTING
;
2835 if (p
== DefaultPrinter
)
2836 type
|= CUPS_PRINTER_DEFAULT
;
2839 * Remove quotes from printer-info, printer-location, and
2840 * printer-make-and-model attributes...
2843 dequote(location
, p
->location
, sizeof(location
));
2844 dequote(info
, p
->info
, sizeof(info
));
2847 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2848 else if (p
->type
& CUPS_PRINTER_CLASS
)
2850 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2851 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2853 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2856 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2858 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2861 * Send a packet to each browse address...
2864 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2868 * Send the browse packet to one or more interfaces...
2871 if (!strcmp(b
->iface
, "*"))
2874 * Send to all local interfaces...
2879 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2881 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2884 * Only send to local, IPv4 interfaces...
2887 if (!iface
->is_local
|| !iface
->port
||
2888 iface
->address
.addr
.sa_family
!= AF_INET
)
2891 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2892 iface
->hostname
, iface
->port
,
2893 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2896 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2897 type
, p
->state
, uri
, location
, info
, make_model
,
2898 p
->browse_attrs
? p
->browse_attrs
: "");
2900 bytes
= strlen(packet
);
2902 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2903 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2904 iface
->name
, packet
);
2906 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2908 sendto(BrowseSocket
, packet
, bytes
, 0,
2909 (struct sockaddr
*)&(iface
->broadcast
),
2910 httpAddrLength(&(iface
->broadcast
)));
2913 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2916 * Send to the named interface using the IPv4 address...
2920 if (strcmp(b
->iface
, iface
->name
))
2925 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2928 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2932 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2933 iface
->hostname
, iface
->port
,
2934 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2937 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2938 type
, p
->state
, uri
, location
, info
, make_model
,
2939 p
->browse_attrs
? p
->browse_attrs
: "");
2941 bytes
= strlen(packet
);
2943 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2944 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2945 iface
->name
, packet
);
2947 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2949 sendto(BrowseSocket
, packet
, bytes
, 0,
2950 (struct sockaddr
*)&(iface
->broadcast
),
2951 httpAddrLength(&(iface
->broadcast
)));
2958 * Send the browse packet to the indicated address using
2959 * the default server name...
2962 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2963 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2964 p
->browse_attrs
? p
->browse_attrs
: "");
2966 bytes
= strlen(packet
);
2967 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2968 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2970 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2971 (struct sockaddr
*)&(b
->to
),
2972 httpAddrLength(&(b
->to
))) <= 0)
2975 * Unable to send browse packet, so remove this address from the
2979 cupsdLogMessage(CUPSD_LOG_ERROR
,
2980 "cupsdSendBrowseList: sendto failed for browser "
2982 (int)(b
- Browsers
+ 1), strerror(errno
));
2985 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2994 #ifdef HAVE_OPENLDAP
2996 * 'send_ldap_browse()' - Send LDAP printer registrations.
3000 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3002 int i
; /* Looping var... */
3003 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3004 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3005 LDAPMessage
*res
; /* Search result token */
3006 char *cn_value
[2], /* Change records */
3012 typestring
[255], /* String to hold printer-type */
3013 filter
[256], /* Search filter for possible UPDATEs */
3014 dn
[1024]; /* DN of the printer we are adding */
3015 int rc
; /* LDAP status */
3016 static const char * const objectClass_values
[] =
3017 { /* The 3 objectClass's we use in */
3018 "top", /* our LDAP entries */
3024 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3027 * Everything in ldap is ** so we fudge around it...
3030 sprintf(typestring
, "%u", p
->type
);
3032 cn_value
[0] = p
->name
;
3034 info
[0] = p
->info
? p
->info
: "Unknown";
3036 location
[0] = p
->location
? p
->location
: "Unknown";
3038 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3039 make_model
[1] = NULL
;
3040 type
[0] = typestring
;
3045 snprintf(filter
, sizeof(filter
),
3046 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3048 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3049 filter
, (char **)ldap_attrs
, 0, &res
);
3050 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3053 mods
[0].mod_type
= "cn";
3054 mods
[0].mod_values
= cn_value
;
3055 mods
[1].mod_type
= "printerDescription";
3056 mods
[1].mod_values
= info
;
3057 mods
[2].mod_type
= "printerURI";
3058 mods
[2].mod_values
= uri
;
3059 mods
[3].mod_type
= "printerLocation";
3060 mods
[3].mod_values
= location
;
3061 mods
[4].mod_type
= "printerMakeAndModel";
3062 mods
[4].mod_values
= make_model
;
3063 mods
[5].mod_type
= "printerType";
3064 mods
[5].mod_values
= type
;
3065 mods
[6].mod_type
= "objectClass";
3066 mods
[6].mod_values
= (char **)objectClass_values
;
3068 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3069 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3071 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3074 * Printer has already been registered, modify the current
3078 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3079 "send_ldap_browse: Replacing entry...");
3081 for (i
= 0; i
< 7; i
++)
3083 pmods
[i
] = mods
+ i
;
3084 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3088 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3089 cupsdLogMessage(CUPSD_LOG_ERROR
,
3090 "LDAP modify for %s failed with status %d: %s",
3091 p
->name
, rc
, ldap_err2string(rc
));
3096 * Printer has never been registered, add the current
3100 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3101 "send_ldap_browse: Adding entry...");
3103 for (i
= 0; i
< 7; i
++)
3105 pmods
[i
] = mods
+ i
;
3106 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3110 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3111 cupsdLogMessage(CUPSD_LOG_ERROR
,
3112 "LDAP add for %s failed with status %d: %s",
3113 p
->name
, rc
, ldap_err2string(rc
));
3116 #endif /* HAVE_OPENLDAP */
3121 * 'send_slp_browse()' - Register the specified printer with SLP.
3125 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3127 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3128 attrs
[8192], /* Printer attributes */
3129 finishings
[1024], /* Finishings to support */
3130 make_model
[IPP_MAX_NAME
* 2],
3131 /* Make and model, quoted */
3132 location
[IPP_MAX_NAME
* 2],
3133 /* Location, quoted */
3134 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3135 *src
, /* Pointer to original string */
3136 *dst
; /* Pointer to destination string */
3137 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3138 SLPError error
; /* SLP error, if any */
3141 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3145 * Make the SLP service URL that conforms to the IANA
3146 * 'printer:' template.
3149 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3151 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3154 * Figure out the finishings string...
3157 if (p
->type
& CUPS_PRINTER_STAPLE
)
3158 strcpy(finishings
, "staple");
3160 finishings
[0] = '\0';
3162 if (p
->type
& CUPS_PRINTER_BIND
)
3165 strlcat(finishings
, ",bind", sizeof(finishings
));
3167 strcpy(finishings
, "bind");
3170 if (p
->type
& CUPS_PRINTER_PUNCH
)
3173 strlcat(finishings
, ",punch", sizeof(finishings
));
3175 strcpy(finishings
, "punch");
3178 if (p
->type
& CUPS_PRINTER_COVER
)
3181 strlcat(finishings
, ",cover", sizeof(finishings
));
3183 strcpy(finishings
, "cover");
3186 if (p
->type
& CUPS_PRINTER_SORT
)
3189 strlcat(finishings
, ",sort", sizeof(finishings
));
3191 strcpy(finishings
, "sort");
3195 strcpy(finishings
, "none");
3198 * Quote any commas in the make and model, location, and info strings...
3201 for (src
= p
->make_model
, dst
= make_model
;
3202 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3204 if (*src
== ',' || *src
== '\\' || *src
== ')')
3213 strcpy(make_model
, "Unknown");
3215 for (src
= p
->location
, dst
= location
;
3216 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3218 if (*src
== ',' || *src
== '\\' || *src
== ')')
3227 strcpy(location
, "Unknown");
3229 for (src
= p
->info
, dst
= info
;
3230 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3232 if (*src
== ',' || *src
== '\\' || *src
== ')')
3241 strcpy(info
, "Unknown");
3244 * Get the authentication value...
3247 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3251 * Make the SLP attribute string list that conforms to
3252 * the IANA 'printer:' template.
3255 snprintf(attrs
, sizeof(attrs
),
3256 "(printer-uri-supported=%s),"
3257 "(uri-authentication-supported=%s>),"
3259 "(uri-security-supported=tls>),"
3261 "(uri-security-supported=none>),"
3262 #endif /* HAVE_SSL */
3263 "(printer-name=%s),"
3264 "(printer-location=%s),"
3265 "(printer-info=%s),"
3266 "(printer-more-info=%s),"
3267 "(printer-make-and-model=%s),"
3268 "(printer-type=%d),"
3269 "(charset-supported=utf-8),"
3270 "(natural-language-configured=%s),"
3271 "(natural-language-supported=de,en,es,fr,it),"
3272 "(color-supported=%s),"
3273 "(finishings-supported=%s),"
3274 "(sides-supported=one-sided%s),"
3275 "(multiple-document-jobs-supported=true)"
3276 "(ipp-versions-supported=1.0,1.1)",
3277 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3278 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3279 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3281 p
->type
& CUPS_PRINTER_DUPLEX
?
3282 ",two-sided-long-edge,two-sided-short-edge" : "");
3284 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3287 * Register the printer with the SLP server...
3290 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3291 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3293 if (error
!= SLP_OK
)
3294 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3300 * 'slp_attr_callback()' - SLP attribute callback
3303 static SLPBoolean
/* O - SLP_TRUE for success */
3305 SLPHandle hslp
, /* I - SLP handle */
3306 const char *attrlist
, /* I - Attribute list */
3307 SLPError errcode
, /* I - Parsing status for this attr */
3308 void *cookie
) /* I - Current printer */
3310 char *tmp
= 0; /* Temporary string */
3311 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3312 /* Current printer */
3315 (void)hslp
; /* anti-compiler-warning-code */
3318 * Bail if there was an error
3321 if (errcode
!= SLP_OK
)
3325 * Parse the attrlist to obtain things needed to build CUPS browse packet
3328 memset(p
, 0, sizeof(cupsd_printer_t
));
3330 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3332 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3334 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3336 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3337 p
->type
= atoi(tmp
);
3339 p
->type
= CUPS_PRINTER_REMOTE
;
3341 cupsdClearString(&tmp
);
3348 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3352 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3354 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3357 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3359 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3362 * Make the SLP service URL that conforms to the IANA
3363 * 'printer:' template.
3366 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3369 * Deregister the printer...
3372 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3378 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3381 static int /* O - 0 on success */
3382 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3383 const char *tag
, /* I - Name of attribute */
3384 char **valbuf
) /* O - Value */
3386 char *ptr1
, /* Pointer into string */
3390 cupsdClearString(valbuf
);
3392 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3394 ptr1
+= strlen(tag
);
3396 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3402 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3403 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3406 * Dequote the value...
3409 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3410 if (*ptr1
== '\\' && ptr1
[1])
3411 _cups_strcpy(ptr1
, ptr1
+ 1);
3422 * 'slp_reg_callback()' - Empty SLPRegReport.
3426 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3427 SLPError errcode
, /* I - Error code, if any */
3428 void *cookie
) /* I - App data */
3439 * 'slp_url_callback()' - SLP service url callback
3442 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3444 SLPHandle hslp
, /* I - SLP handle */
3445 const char *srvurl
, /* I - URL of service */
3446 unsigned short lifetime
, /* I - Life of service */
3447 SLPError errcode
, /* I - Existing error code */
3448 void *cookie
) /* I - Pointer to service list */
3450 slpsrvurl_t
*s
, /* New service entry */
3451 **head
; /* Pointer to head of entry */
3455 * Let the compiler know we won't be using these vars...
3462 * Bail if there was an error
3465 if (errcode
!= SLP_OK
)
3469 * Grab the head of the list...
3472 head
= (slpsrvurl_t
**)cookie
;
3475 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3478 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3482 * Copy the SLP service URL...
3485 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3488 * Link the SLP service URL into the head of the list
3498 #endif /* HAVE_LIBSLP */
3502 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3506 update_cups_browse(void)
3508 int i
; /* Looping var */
3509 int auth
; /* Authorization status */
3510 int len
; /* Length of name string */
3511 int bytes
; /* Number of bytes left */
3512 char packet
[1541], /* Broadcast packet */
3513 *pptr
; /* Pointer into packet */
3514 socklen_t srclen
; /* Length of source address */
3515 http_addr_t srcaddr
; /* Source address */
3516 char srcname
[1024]; /* Source hostname */
3517 unsigned address
[4]; /* Source address */
3518 unsigned type
; /* Printer type */
3519 unsigned state
; /* Printer state */
3520 char uri
[HTTP_MAX_URI
], /* Printer URI */
3521 host
[HTTP_MAX_URI
], /* Host portion of URI */
3522 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3523 info
[IPP_MAX_NAME
], /* Information string */
3524 location
[IPP_MAX_NAME
], /* Location string */
3525 make_model
[IPP_MAX_NAME
];/* Make and model string */
3526 int num_attrs
; /* Number of attributes */
3527 cups_option_t
*attrs
; /* Attributes */
3531 * Read a packet from the browse socket...
3534 srclen
= sizeof(srcaddr
);
3535 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3536 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3539 * "Connection refused" is returned under Linux if the destination port
3540 * or address is unreachable from a previous sendto(); check for the
3541 * error here and ignore it for now...
3544 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3546 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3548 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
3550 cupsdStopBrowsing();
3557 packet
[bytes
] = '\0';
3560 * If we're about to sleep, ignore incoming browse packets.
3567 * Figure out where it came from...
3571 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3573 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3574 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3575 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3576 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3579 #endif /* AF_INET6 */
3584 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3587 if (HostNameLookups
)
3588 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3590 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3592 len
= strlen(srcname
);
3600 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3603 * Access from localhost (127.0.0.1) is always allowed...
3611 * Do authorization checks on the domain/address...
3614 switch (BrowseACL
->order_type
)
3617 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
3620 case AUTH_ALLOW
: /* Order Deny,Allow */
3623 if (cupsdCheckAuth(address
, srcname
, len
,
3624 BrowseACL
->num_deny
, BrowseACL
->deny
))
3627 if (cupsdCheckAuth(address
, srcname
, len
,
3628 BrowseACL
->num_allow
, BrowseACL
->allow
))
3632 case AUTH_DENY
: /* Order Allow,Deny */
3635 if (cupsdCheckAuth(address
, srcname
, len
,
3636 BrowseACL
->num_allow
, BrowseACL
->allow
))
3639 if (cupsdCheckAuth(address
, srcname
, len
,
3640 BrowseACL
->num_deny
, BrowseACL
->deny
))
3649 if (auth
== AUTH_DENY
)
3651 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3652 "update_cups_browse: Refused %d bytes from %s", bytes
,
3657 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3658 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3665 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3667 cupsdLogMessage(CUPSD_LOG_WARN
,
3668 "update_cups_browse: Garbled browse packet - %s", packet
);
3672 strcpy(location
, "Location Unknown");
3673 strcpy(info
, "No Information Available");
3674 make_model
[0] = '\0';
3678 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3681 * Have extended information; can't use sscanf for it because not all
3682 * sscanf's allow empty strings with %[^\"]...
3685 for (i
= 0, pptr
++;
3686 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3688 location
[i
] = *pptr
;
3696 while (*pptr
&& isspace(*pptr
& 255))
3701 for (i
= 0, pptr
++;
3702 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3711 while (*pptr
&& isspace(*pptr
& 255))
3716 for (i
= 0, pptr
++;
3717 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3719 make_model
[i
] = *pptr
;
3724 make_model
[i
] = '\0';
3727 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3733 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3734 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3735 type
, state
, uri
, location
, info
, make_model
));
3738 * Pull the URI apart to see if this is a local or remote printer...
3741 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
3743 cupsFreeOptions(num_attrs
, attrs
);
3751 for (i
= 0; i
< NumRelays
; i
++)
3752 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
3753 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3754 (struct sockaddr
*)&(Relays
[i
].to
),
3755 httpAddrLength(&(Relays
[i
].to
))) <= 0)
3757 cupsdLogMessage(CUPSD_LOG_ERROR
,
3758 "update_cups_browse: sendto failed for relay %d - %s.",
3759 i
+ 1, strerror(errno
));
3760 cupsFreeOptions(num_attrs
, attrs
);
3765 * Process the browse data...
3768 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
3769 (ipp_pstate_t
)state
, location
, info
, make_model
,
3775 * 'update_polling()' - Read status messages from the poll daemons.
3779 update_polling(void)
3781 char *ptr
, /* Pointer to end of line in buffer */
3782 message
[1024]; /* Pointer to message text */
3783 int loglevel
; /* Log level for message */
3786 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
3787 message
, sizeof(message
))) != NULL
)
3788 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
3791 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
3794 * All polling processes have died; stop polling...
3797 cupsdLogMessage(CUPSD_LOG_ERROR
,
3798 "update_polling: all polling processes have exited!");
3805 * End of "$Id: dirsvc.c 6590 2007-06-21 18:22:22Z mike $".