2 * "$Id: dirsvc.c 6538 2007-05-17 18:26:05Z 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
->external_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
;
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
;
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
->external_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_REMOTE
))
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
->external_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
->external_type
& (CUPS_PRINTER_REMOTE
|
860 CUPS_PRINTER_IMPLICIT
)) ||
863 else if (p
->browse_time
< ut
)
866 * Need to send an update...
871 p
->browse_time
= time(NULL
);
873 if (BrowseLocalProtocols
& BROWSE_CUPS
)
877 if (BrowseLocalProtocols
& BROWSE_SLP
)
879 #endif /* HAVE_LIBSLP */
882 if (BrowseLocalProtocols
& BROWSE_LDAP
)
884 #endif /* HAVE_LDAP */
889 * Save where we left off so that all printers get updated...
896 * Loop through all of the printers and send local updates as needed...
899 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
901 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
904 * If this is a remote queue, see if it needs to be timed out...
907 if (p
->type
& CUPS_PRINTER_REMOTE
)
909 if (p
->browse_expire
< to
)
911 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
912 "%s \'%s\' deleted by directory services (timeout).",
913 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
916 cupsdLogMessage(CUPSD_LOG_DEBUG
,
917 "Remote destination \"%s\" has timed out; "
921 cupsArraySave(Printers
);
922 cupsdDeletePrinter(p
, 1);
923 cupsArrayRestore(Printers
);
931 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
935 cupsdStartBrowsing(void)
937 int val
; /* Socket option value */
938 struct sockaddr_in addr
; /* Broadcast address */
939 cupsd_printer_t
*p
; /* Current printer */
944 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
947 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
949 if (BrowseSocket
< 0)
952 * Create the broadcast socket...
955 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
957 cupsdLogMessage(CUPSD_LOG_ERROR
,
958 "cupsdStartBrowsing: Unable to create broadcast "
959 "socket - %s.", strerror(errno
));
960 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
961 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
966 * Bind the socket to browse port...
969 memset(&addr
, 0, sizeof(addr
));
970 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
971 addr
.sin_family
= AF_INET
;
972 addr
.sin_port
= htons(BrowsePort
);
974 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
976 cupsdLogMessage(CUPSD_LOG_ERROR
,
977 "cupsdStartBrowsing: Unable to bind broadcast "
978 "socket - %s.", strerror(errno
));
981 closesocket(BrowseSocket
);
987 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
988 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
994 * Set the "broadcast" flag...
998 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1000 cupsdLogMessage(CUPSD_LOG_ERROR
,
1001 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1005 closesocket(BrowseSocket
);
1007 close(BrowseSocket
);
1011 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1012 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1017 * Close the socket on exec...
1020 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1023 * Finally, add the socket to the input selection set as needed...
1026 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1029 * We only listen if we want remote printers...
1032 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1040 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1043 * Open SLP handle...
1046 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1048 cupsdLogMessage(CUPSD_LOG_ERROR
,
1049 "Unable to open an SLP handle; disabling SLP browsing!");
1050 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1051 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1054 BrowseSLPRefresh
= 0;
1057 BrowseSLPHandle
= NULL
;
1058 #endif /* HAVE_LIBSLP */
1060 #ifdef HAVE_OPENLDAP
1061 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1065 cupsdLogMessage(CUPSD_LOG_ERROR
,
1066 "Need to set BrowseLDAPDN to use LDAP browsing!");
1067 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1068 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1073 * Open LDAP handle...
1076 int rc
; /* LDAP API status */
1077 int version
= 3; /* LDAP version */
1078 struct berval bv
= {0, ""}; /* SASL bind value */
1082 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1085 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1086 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1088 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1090 if (rc
!= LDAP_SUCCESS
)
1092 cupsdLogMessage(CUPSD_LOG_ERROR
,
1093 "Unable to initialize LDAP; disabling LDAP browsing!");
1094 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1095 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1097 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1098 (const void *)&version
) != LDAP_SUCCESS
)
1100 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1101 BrowseLDAPHandle
= NULL
;
1102 cupsdLogMessage(CUPSD_LOG_ERROR
,
1103 "Unable to set LDAP protocol version; "
1104 "disabling LDAP browsing!");
1105 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1106 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1110 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1111 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1114 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1115 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1117 if (rc
!= LDAP_SUCCESS
)
1119 cupsdLogMessage(CUPSD_LOG_ERROR
,
1120 "Unable to bind to LDAP server; "
1121 "disabling LDAP browsing!");
1122 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1123 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1124 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1129 BrowseLDAPRefresh
= 0;
1131 #endif /* HAVE_OPENLDAP */
1134 * Register the individual printers
1137 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1139 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1140 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1141 cupsdRegisterPrinter(p
);
1146 * 'cupsdStartPolling()' - Start polling servers as needed.
1150 cupsdStartPolling(void)
1152 int i
; /* Looping var */
1153 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1154 char polld
[1024]; /* Poll daemon path */
1155 char sport
[255]; /* Server port */
1156 char bport
[255]; /* Browser port */
1157 char interval
[255]; /* Poll interval */
1158 int statusfds
[2]; /* Status pipe */
1159 char *argv
[6]; /* Arguments */
1160 char *envp
[100]; /* Environment */
1164 * Don't do anything if we aren't polling...
1167 if (NumPolled
== 0 || BrowseSocket
< 0)
1170 PollStatusBuffer
= NULL
;
1175 * Setup string arguments for polld, port and interval options.
1178 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1180 sprintf(bport
, "%d", BrowsePort
);
1183 sprintf(interval
, "%d", BrowseInterval
);
1185 strcpy(interval
, "30");
1187 argv
[0] = "cups-polld";
1193 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1196 * Create a pipe that receives the status messages from each
1200 if (cupsdOpenPipe(statusfds
))
1202 cupsdLogMessage(CUPSD_LOG_ERROR
,
1203 "Unable to create polling status pipes - %s.",
1206 PollStatusBuffer
= NULL
;
1210 PollPipe
= statusfds
[0];
1211 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1214 * Run each polling daemon, redirecting stderr to the polling pipe...
1217 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1219 sprintf(sport
, "%d", pollp
->port
);
1221 argv
[1] = pollp
->hostname
;
1223 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1224 0, &(pollp
->pid
)) < 0)
1226 cupsdLogMessage(CUPSD_LOG_ERROR
,
1227 "cupsdStartPolling: Unable to fork polling daemon - %s",
1233 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1234 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1235 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1238 close(statusfds
[1]);
1241 * Finally, add the pipe to the input selection set...
1244 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1249 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1253 cupsdStopBrowsing(void)
1255 cupsd_printer_t
*p
; /* Current printer */
1258 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1262 * De-register the individual printers
1265 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1267 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1268 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1269 cupsdDeregisterPrinter(p
, 1);
1272 * Shut down browsing sockets...
1275 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1279 * Close the socket and remove it from the input selection set.
1283 closesocket(BrowseSocket
);
1285 close(BrowseSocket
);
1288 cupsdRemoveSelect(BrowseSocket
);
1293 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1297 * Close SLP handle...
1300 SLPClose(BrowseSLPHandle
);
1301 BrowseSLPHandle
= NULL
;
1303 #endif /* HAVE_LIBSLP */
1305 #ifdef HAVE_OPENLDAP
1306 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1309 ldap_unbind(BrowseLDAPHandle
);
1310 BrowseLDAPHandle
= NULL
;
1312 #endif /* HAVE_OPENLDAP */
1317 * 'cupsdStopPolling()' - Stop polling servers as needed.
1321 cupsdStopPolling(void)
1323 int i
; /* Looping var */
1324 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1329 cupsdStatBufDelete(PollStatusBuffer
);
1332 cupsdRemoveSelect(PollPipe
);
1335 PollStatusBuffer
= NULL
;
1338 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1340 cupsdEndProcess(pollp
->pid
, 0);
1346 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1350 cupsdUpdateDNSSDBrowse(
1351 cupsd_printer_t
*p
) /* I - Printer being queried */
1353 DNSServiceErrorType sdErr
; /* Service discovery error */
1356 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1357 != kDNSServiceErr_NoError
)
1359 cupsdLogMessage(CUPSD_LOG_ERROR
,
1360 "DNS Service Discovery registration error %d for \"%s\"!",
1362 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1363 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1365 p
->dnssd_ipp_ref
= NULL
;
1366 p
->dnssd_ipp_fd
= -1;
1369 #endif /* HAVE_DNSSD */
1372 #ifdef HAVE_OPENLDAP
1374 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1378 cupsdUpdateLDAPBrowse(void)
1380 char uri
[HTTP_MAX_URI
], /* Printer URI */
1381 host
[HTTP_MAX_URI
], /* Hostname */
1382 resource
[HTTP_MAX_URI
], /* Resource path */
1383 location
[1024], /* Printer location */
1384 info
[1024], /* Printer information */
1385 make_model
[1024], /* Printer make and model */
1386 **value
; /* Holds the returned data from LDAP */
1387 int type
; /* Printer type */
1388 int rc
; /* LDAP status */
1389 int limit
; /* Size limit */
1390 LDAPMessage
*res
, /* LDAP search results */
1391 *e
; /* Current entry from search */
1395 * Search for printers...
1398 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1400 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1402 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1403 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1404 if (rc
!= LDAP_SUCCESS
)
1406 cupsdLogMessage(CUPSD_LOG_ERROR
,
1407 "LDAP search returned error %d: %s", rc
,
1408 ldap_err2string(rc
));
1412 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1413 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1418 * Loop through the available printers...
1421 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1423 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1426 * Get the required values from this entry...
1429 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1430 "printerDescription")) == NULL
)
1433 strlcpy(info
, *value
, sizeof(info
));
1434 ldap_value_free(value
);
1436 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1437 "printerLocation")) == NULL
)
1440 strlcpy(location
, *value
, sizeof(location
));
1441 ldap_value_free(value
);
1443 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1444 "printerMakeAndModel")) == NULL
)
1447 strlcpy(make_model
, *value
, sizeof(make_model
));
1448 ldap_value_free(value
);
1450 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1451 "printerType")) == NULL
)
1454 type
= atoi(*value
);
1455 ldap_value_free(value
);
1457 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1458 "printerURI")) == NULL
)
1461 strlcpy(uri
, *value
, sizeof(uri
));
1462 ldap_value_free(value
);
1465 * Process the entry as browse data...
1468 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1469 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1470 location
, info
, make_model
, 0, NULL
);
1474 #endif /* HAVE_OPENLDAP */
1479 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1483 cupsdUpdateSLPBrowse(void)
1485 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1486 *next
; /* Next service in list */
1487 cupsd_printer_t p
; /* Printer information */
1488 const char *uri
; /* Pointer to printer URI */
1489 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1490 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1494 * Reset the refresh time...
1497 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1500 * Poll for remote printers using SLP...
1505 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1506 slp_url_callback
, &s
);
1509 * Loop through the list of available printers...
1515 * Save the "next" pointer...
1521 * Load a cupsd_printer_t structure with the SLP service attributes...
1524 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1527 * Process this printer entry...
1530 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1532 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1535 * Pull the URI apart to see if this is a local or remote printer...
1538 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1539 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1540 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1544 * Free this listing...
1547 cupsdClearString(&p
.info
);
1548 cupsdClearString(&p
.location
);
1549 cupsdClearString(&p
.make_model
);
1554 #endif /* HAVE_LIBSLP */
1558 * 'dequote()' - Remote quotes from a string.
1561 static char * /* O - Dequoted string */
1562 dequote(char *d
, /* I - Destination string */
1563 const char *s
, /* I - Source string */
1564 int dlen
) /* I - Destination length */
1566 char *dptr
; /* Pointer into destination */
1571 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1588 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1591 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1592 is_local_queue(const char *uri
, /* I - Printer URI */
1593 char *host
, /* O - Host string */
1594 int hostlen
, /* I - Length of host buffer */
1595 char *resource
, /* O - Resource string */
1596 int resourcelen
) /* I - Length of resource buffer */
1598 char scheme
[32], /* Scheme portion of URI */
1599 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1600 int port
; /* Port portion of URI */
1601 cupsd_netif_t
*iface
; /* Network interface */
1605 * Pull the URI apart to see if this is a local or remote printer...
1608 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1609 username
, sizeof(username
), host
, hostlen
, &port
,
1610 resource
, resourcelen
) < HTTP_URI_OK
)
1613 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1616 * Check for local server addresses...
1619 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1624 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1626 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1627 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1631 * If we get here, the printer is remote...
1639 * 'process_browse_data()' - Process new browse data.
1643 process_browse_data(
1644 const char *uri
, /* I - URI of printer/class */
1645 const char *host
, /* I - Hostname */
1646 const char *resource
, /* I - Resource path */
1647 cups_ptype_t type
, /* I - Printer type */
1648 ipp_pstate_t state
, /* I - Printer state */
1649 const char *location
, /* I - Printer location */
1650 const char *info
, /* I - Printer information */
1651 const char *make_model
, /* I - Printer make and model */
1652 int num_attrs
, /* I - Number of attributes */
1653 cups_option_t
*attrs
) /* I - Attributes */
1655 int i
; /* Looping var */
1656 int update
; /* Update printer attributes? */
1657 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1658 name
[IPP_MAX_NAME
], /* Name of printer */
1659 newname
[IPP_MAX_NAME
], /* New name of printer */
1660 *hptr
, /* Pointer into hostname */
1661 *sptr
; /* Pointer into ServerName */
1662 char local_make_model
[IPP_MAX_NAME
];
1663 /* Local make and model */
1664 cupsd_printer_t
*p
; /* Printer information */
1665 const char *ipp_options
, /* ipp-options value */
1666 *lease_duration
; /* lease-duration value */
1670 * Determine if the URI contains any illegal characters in it...
1673 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1674 (strncmp(resource
, "/printers/", 10) &&
1675 strncmp(resource
, "/classes/", 9)))
1677 cupsdLogMessage(CUPSD_LOG_ERROR
,
1678 "process_browse_data: Bad printer URI in browse data: %s",
1683 if (strchr(resource
, '?') ||
1684 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1685 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1687 cupsdLogMessage(CUPSD_LOG_ERROR
,
1688 "process_browse_data: Bad resource in browse data: %s",
1694 * OK, this isn't a local printer; add any remote options...
1697 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1699 if (BrowseRemoteOptions
)
1701 if (BrowseRemoteOptions
[0] == '?')
1704 * Override server-supplied options...
1707 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1709 else if (ipp_options
)
1712 * Combine the server and local options...
1715 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1716 BrowseRemoteOptions
);
1721 * Just use the local options...
1724 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1729 else if (ipp_options
)
1732 * Just use the server-supplied options...
1735 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1740 * See if we already have it listed in the Printers list, and add it if not...
1743 type
|= CUPS_PRINTER_REMOTE
;
1744 type
&= ~CUPS_PRINTER_IMPLICIT
;
1746 hptr
= strchr(host
, '.');
1747 sptr
= strchr(ServerName
, '.');
1749 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1752 * Strip the common domain name components...
1755 while (hptr
!= NULL
)
1757 if (!strcasecmp(hptr
, sptr
))
1763 hptr
= strchr(hptr
+ 1, '.');
1767 if (type
& CUPS_PRINTER_CLASS
)
1770 * Remote destination is a class...
1773 if (!strncmp(resource
, "/classes/", 9))
1774 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1779 *hptr
= '.'; /* Resource FQDN */
1781 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1783 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1785 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1788 * Nope, this isn't the same host; if the hostname isn't the local host,
1789 * add it to the other class and then find a class using the full host
1793 if (p
->type
& CUPS_PRINTER_REMOTE
)
1795 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1796 "Renamed remote class \"%s\" to \"%s@%s\"...",
1797 p
->name
, p
->name
, p
->hostname
);
1798 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1799 "Class \'%s\' deleted by directory services.",
1802 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1803 cupsdRenamePrinter(p
, newname
);
1805 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1806 "Class \'%s\' added by directory services.",
1812 else if (!p
->hostname
)
1815 * Hostname not set, so this must be a cached remote printer
1816 * that was created for a pending print job...
1819 cupsdSetString(&p
->hostname
, host
);
1820 cupsdSetString(&p
->uri
, uri
);
1821 cupsdSetString(&p
->device_uri
, uri
);
1828 * Use the short name for this shared class.
1831 strlcpy(name
, resource
+ 9, sizeof(name
));
1834 else if (p
&& !p
->hostname
)
1837 * Hostname not set, so this must be a cached remote printer
1838 * that was created for a pending print job...
1841 cupsdSetString(&p
->hostname
, host
);
1842 cupsdSetString(&p
->uri
, uri
);
1843 cupsdSetString(&p
->device_uri
, uri
);
1850 * Class doesn't exist; add it...
1853 p
= cupsdAddClass(name
);
1855 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1857 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1858 "Class \'%s\' added by directory services.", name
);
1861 * Force the URI to point to the real server...
1864 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1866 cupsdSetString(&p
->uri
, uri
);
1867 cupsdSetString(&p
->device_uri
, uri
);
1868 cupsdSetString(&p
->hostname
, host
);
1876 * Remote destination is a printer...
1879 if (!strncmp(resource
, "/printers/", 10))
1880 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1885 *hptr
= '.'; /* Resource FQDN */
1887 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
1889 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
1891 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1894 * Nope, this isn't the same host; if the hostname isn't the local host,
1895 * add it to the other printer and then find a printer using the full host
1899 if (p
->type
& CUPS_PRINTER_REMOTE
)
1901 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1902 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1903 p
->name
, p
->name
, p
->hostname
);
1904 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1905 "Printer \'%s\' deleted by directory services.",
1908 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1909 cupsdRenamePrinter(p
, newname
);
1911 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1912 "Printer \'%s\' added by directory services.",
1918 else if (!p
->hostname
)
1921 * Hostname not set, so this must be a cached remote printer
1922 * that was created for a pending print job...
1925 cupsdSetString(&p
->hostname
, host
);
1926 cupsdSetString(&p
->uri
, uri
);
1927 cupsdSetString(&p
->device_uri
, uri
);
1934 * Use the short name for this shared printer.
1937 strlcpy(name
, resource
+ 10, sizeof(name
));
1940 else if (p
&& !p
->hostname
)
1943 * Hostname not set, so this must be a cached remote printer
1944 * that was created for a pending print job...
1947 cupsdSetString(&p
->hostname
, host
);
1948 cupsdSetString(&p
->uri
, uri
);
1949 cupsdSetString(&p
->device_uri
, uri
);
1956 * Printer doesn't exist; add it...
1959 p
= cupsdAddPrinter(name
);
1961 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1962 "Printer \'%s\' added by directory services.", name
);
1964 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
1967 * Force the URI to point to the real server...
1970 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1972 cupsdSetString(&p
->hostname
, host
);
1973 cupsdSetString(&p
->uri
, uri
);
1974 cupsdSetString(&p
->device_uri
, uri
);
1981 * Update the state...
1985 p
->browse_time
= time(NULL
);
1987 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
1991 * Grab the lease-duration for the browse data; anything less then 1
1992 * second or more than 1 week gets the default BrowseTimeout...
1995 i
= atoi(lease_duration
);
1996 if (i
< 1 || i
> 604800)
1999 p
->browse_expire
= p
->browse_time
+ i
;
2002 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2004 if (type
& CUPS_PRINTER_REJECTING
)
2006 type
&= ~CUPS_PRINTER_REJECTING
;
2014 else if (!p
->accepting
)
2020 if (p
->type
!= type
)
2026 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2028 cupsdSetString(&p
->location
, location
);
2032 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2034 cupsdSetString(&p
->info
, info
);
2038 if (!make_model
|| !make_model
[0])
2040 if (type
& CUPS_PRINTER_CLASS
)
2041 snprintf(local_make_model
, sizeof(local_make_model
),
2042 "Remote Class on %s", host
);
2044 snprintf(local_make_model
, sizeof(local_make_model
),
2045 "Remote Printer on %s", host
);
2048 snprintf(local_make_model
, sizeof(local_make_model
),
2049 "%s on %s", make_model
, host
);
2051 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2053 cupsdSetString(&p
->make_model
, local_make_model
);
2059 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2062 * See if we need to update the attributes...
2065 if (p
->num_options
!= num_attrs
)
2069 for (i
= 0; i
< num_attrs
; i
++)
2070 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2071 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2072 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2081 * Free the old options...
2084 cupsFreeOptions(p
->num_options
, p
->options
);
2087 p
->num_options
= num_attrs
;
2090 if (type
& CUPS_PRINTER_DELETE
)
2092 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2093 "%s \'%s\' deleted by directory services.",
2094 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2096 cupsdExpireSubscriptions(p
, NULL
);
2098 cupsdDeletePrinter(p
, 1);
2099 cupsdUpdateImplicitClasses();
2103 cupsdSetPrinterAttrs(p
);
2104 cupsdUpdateImplicitClasses();
2108 * See if we have a default printer... If not, make the first network
2109 * default printer the default.
2112 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2115 * Find the first network default printer and use it...
2118 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2120 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2121 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2129 * Do auto-classing if needed...
2132 process_implicit_classes();
2135 * Update the printcap file...
2138 cupsdWritePrintcap();
2144 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2147 static char * /* O - TXT record */
2148 dnssdBuildTxtRecord(
2149 int *txt_len
, /* O - TXT record length */
2150 cupsd_printer_t
*p
) /* I - Printer information */
2152 int i
, j
; /* Looping vars */
2153 char type_str
[32], /* Type to string buffer */
2154 state_str
[32], /* State to string buffer */
2155 rp_str
[1024], /* Queue name string buffer */
2156 air_str
[1024], /* auth-info-required string buffer */
2157 *keyvalue
[32][2]; /* Table of key/value pairs */
2161 * Load up the key value pairs...
2166 keyvalue
[i
][0] = "txtvers";
2167 keyvalue
[i
++][1] = "1";
2169 keyvalue
[i
][0] = "qtotal";
2170 keyvalue
[i
++][1] = "1";
2172 keyvalue
[i
][0] = "rp";
2173 keyvalue
[i
++][1] = rp_str
;
2174 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2175 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2177 keyvalue
[i
][0] = "ty";
2178 keyvalue
[i
++][1] = p
->make_model
;
2180 if (p
->location
&& *p
->location
!= '\0')
2182 keyvalue
[i
][0] = "note";
2183 keyvalue
[i
++][1] = p
->location
;
2186 keyvalue
[i
][0] = "product";
2187 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2189 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2190 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2192 keyvalue
[i
][0] = "printer-state";
2193 keyvalue
[i
++][1] = state_str
;
2195 keyvalue
[i
][0] = "printer-type";
2196 keyvalue
[i
++][1] = type_str
;
2198 keyvalue
[i
][0] = "Transparent";
2199 keyvalue
[i
++][1] = "T";
2201 keyvalue
[i
][0] = "Binary";
2202 keyvalue
[i
++][1] = "T";
2204 if ((p
->type
& CUPS_PRINTER_FAX
))
2206 keyvalue
[i
][0] = "Fax";
2207 keyvalue
[i
++][1] = "T";
2210 if ((p
->type
& CUPS_PRINTER_COLOR
))
2212 keyvalue
[i
][0] = "Color";
2213 keyvalue
[i
++][1] = "T";
2216 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2218 keyvalue
[i
][0] = "Duplex";
2219 keyvalue
[i
++][1] = "T";
2222 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2224 keyvalue
[i
][0] = "Staple";
2225 keyvalue
[i
++][1] = "T";
2228 if ((p
->type
& CUPS_PRINTER_COPIES
))
2230 keyvalue
[i
][0] = "Copies";
2231 keyvalue
[i
++][1] = "T";
2234 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2236 keyvalue
[i
][0] = "Collate";
2237 keyvalue
[i
++][1] = "T";
2240 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2242 keyvalue
[i
][0] = "Punch";
2243 keyvalue
[i
++][1] = "T";
2246 if ((p
->type
& CUPS_PRINTER_BIND
))
2248 keyvalue
[i
][0] = "Bind";
2249 keyvalue
[i
++][1] = "T";
2252 if ((p
->type
& CUPS_PRINTER_SORT
))
2254 keyvalue
[i
][0] = "Sort";
2255 keyvalue
[i
++][1] = "T";
2258 keyvalue
[i
][0] = "pdl";
2259 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2261 if (p
->num_auth_info_required
)
2263 char *air
= air_str
; /* Pointer into string */
2266 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2268 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2274 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2278 keyvalue
[i
][0] = "air";
2279 keyvalue
[i
++][1] = air
;
2283 * Then pack them into a proper txt record...
2286 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2291 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2296 dnssdDeregisterPrinter(
2297 cupsd_printer_t
*p
) /* I - Printer */
2299 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2302 * Closing the socket deregisters the service
2305 if (p
->dnssd_ipp_ref
)
2307 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2308 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2309 p
->dnssd_ipp_ref
= NULL
;
2310 p
->dnssd_ipp_fd
= -1;
2313 cupsdClearString(&p
->reg_name
);
2318 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2321 free(p
->txt_record
);
2322 p
->txt_record
= NULL
;
2328 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2329 * TXT record format.
2332 static char * /* O - TXT record */
2333 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2334 char *keyvalue
[][2], /* I - Table of key value pairs */
2335 int count
) /* I - Items in table */
2337 int i
; /* Looping var */
2338 int length
; /* Length of TXT record */
2339 int length2
; /* Length of value */
2340 char *txtRecord
; /* TXT record buffer */
2341 char *cursor
; /* Looping pointer */
2345 * Calculate the buffer size
2348 for (length
= i
= 0; i
< count
; i
++)
2349 length
+= 1 + strlen(keyvalue
[i
][0]) +
2350 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2353 * Allocate and fill it
2356 txtRecord
= malloc(length
);
2361 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2364 * Drop in the p-string style length byte followed by the data
2367 length
= strlen(keyvalue
[i
][0]);
2368 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2370 *cursor
++ = (unsigned char)(length
+ length2
);
2372 memcpy(cursor
, keyvalue
[i
][0], length
);
2379 memcpy(cursor
, keyvalue
[i
][1], length2
);
2390 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2394 dnssdRegisterCallback(
2395 DNSServiceRef sdRef
, /* I - DNS Service reference */
2396 DNSServiceFlags flags
, /* I - Reserved for future use */
2397 DNSServiceErrorType errorCode
, /* I - Error code */
2398 const char *name
, /* I - Service name */
2399 const char *regtype
, /* I - Service type */
2400 const char *domain
, /* I - Domain. ".local" for now */
2401 void *context
) /* I - User-defined context */
2405 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2406 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2410 cupsdLogMessage(CUPSD_LOG_ERROR
,
2411 "DNSServiceRegister failed with error %d", (int)errorCode
);
2418 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2419 * or update the broadcast contents.
2423 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2425 DNSServiceErrorType se
; /* dnssd errors */
2426 cupsd_listener_t
*lis
; /* Current listening socket */
2427 char *txt_record
, /* TXT record buffer */
2428 *name
; /* Service name */
2429 int txt_len
, /* TXT record length */
2430 port
; /* IPP port number */
2431 char str_buffer
[1024];
2432 /* C-string buffer */
2433 const char *computerName
; /* Computer name c-string ptr */
2434 const char *regtype
; /* Registration type */
2435 #ifdef HAVE_COREFOUNDATION_H
2436 CFStringRef computerNameRef
;/* Computer name CFString */
2437 CFStringEncoding nameEncoding
; /* Computer name encoding */
2438 CFMutableStringRef shortNameRef
; /* Mutable name string */
2439 CFIndex nameLength
; /* Name string length */
2441 int nameLength
; /* Name string length */
2442 #endif /* HAVE_COREFOUNDATION_H */
2445 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2446 !p
->dnssd_ipp_ref
? "new" : "update");
2449 * If per-printer sharing was just disabled make sure we're not
2450 * registered before returning.
2455 dnssdDeregisterPrinter(p
);
2460 * Get the computer name as a c-string...
2463 #ifdef HAVE_COREFOUNDATION_H
2464 computerName
= NULL
;
2465 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2466 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2467 kCFStringEncodingUTF8
)) == NULL
)
2468 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2469 kCFStringEncodingUTF8
))
2470 computerName
= str_buffer
;
2472 computerName
= ServerName
;
2473 #endif /* HAVE_COREFOUNDATION_H */
2476 * The registered name takes the form of "<printer-info> @ <computer name>"...
2481 cupsdSetStringf(&name
, "%s @ %s",
2482 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2485 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2487 #ifdef HAVE_COREFOUNDATION_H
2488 if (computerNameRef
)
2489 CFRelease(computerNameRef
);
2490 #endif /* HAVE_COREFOUNDATION_H */
2493 * If an existing printer was renamed, unregister it and start over...
2496 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2497 dnssdDeregisterPrinter(p
);
2499 txt_len
= 0; /* anti-compiler-warning-code */
2500 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2502 if (!p
->dnssd_ipp_ref
)
2505 * Initial registration...
2508 cupsdSetString(&p
->reg_name
, name
);
2512 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2514 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2516 if (lis
->address
.addr
.sa_family
== AF_INET
)
2518 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2521 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2523 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2529 * Use the _fax subtype for fax queues...
2532 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) type is \"%s\"",
2538 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2539 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2540 dnssdRegisterCallback
, p
);
2543 * In case the name is too long, try shortening the string one character
2547 if (se
== kDNSServiceErr_BadParam
)
2549 #ifdef HAVE_COREFOUNDATION_H
2550 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2552 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2553 nameLength
= CFStringGetLength(shortNameRef
);
2555 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2557 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2558 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2559 kCFStringEncodingUTF8
))
2561 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2562 regtype
, NULL
, NULL
, htons(port
),
2563 txt_len
, txt_record
,
2564 dnssdRegisterCallback
, p
);
2568 CFRelease(shortNameRef
);
2571 nameLength
= strlen(name
);
2572 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2574 name
[--nameLength
] = '\0';
2575 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2576 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2577 dnssdRegisterCallback
, p
);
2579 #endif /* HAVE_COREFOUNDATION_H */
2582 if (se
== kDNSServiceErr_NoError
)
2584 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2585 p
->txt_record
= txt_record
;
2586 p
->txt_len
= txt_len
;
2589 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2593 cupsdLogMessage(CUPSD_LOG_WARN
,
2594 "DNS-SD registration of \"%s\" failed with %d",
2597 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2600 * Update the existing registration...
2603 /* A TTL of 0 means use record's original value (Radar 3176248) */
2604 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2605 txt_len
, txt_record
, 0);
2608 free(p
->txt_record
);
2610 p
->txt_record
= txt_record
;
2611 p
->txt_len
= txt_len
;
2618 cupsdClearString(&name
);
2620 #endif /* HAVE_DNSSD */
2624 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2628 process_implicit_classes(void)
2630 int i
; /* Looping var */
2631 int update
; /* Update printer attributes? */
2632 char name
[IPP_MAX_NAME
], /* Name of printer */
2633 *hptr
; /* Pointer into hostname */
2634 cupsd_printer_t
*p
, /* Printer information */
2635 *pclass
, /* Printer class */
2636 *first
; /* First printer in class */
2637 int offset
, /* Offset of name */
2638 len
; /* Length of name */
2641 if (!ImplicitClasses
|| !Printers
)
2645 * Loop through all available printers and create classes as needed...
2648 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2649 update
= 0, pclass
= NULL
, first
= NULL
;
2651 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2654 * Skip implicit classes...
2657 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2664 * If len == 0, get the length of this printer name up to the "@"
2668 cupsArraySave(Printers
);
2671 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2672 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2675 * We have more than one printer with the same name; see if
2676 * we have a class, and if this printer is a member...
2679 if (pclass
&& strcasecmp(pclass
->name
, name
))
2682 cupsdSetPrinterAttrs(pclass
);
2688 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2691 * Need to add the class...
2694 pclass
= cupsdAddPrinter(name
);
2695 cupsArrayAdd(ImplicitPrinters
, pclass
);
2697 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2698 pclass
->accepting
= 1;
2699 pclass
->state
= IPP_PRINTER_IDLE
;
2701 cupsdSetString(&pclass
->location
, p
->location
);
2702 cupsdSetString(&pclass
->info
, p
->info
);
2704 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2705 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2709 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2711 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2712 "Implicit class \'%s\' added by directory services.",
2718 for (i
= 0; i
< pclass
->num_printers
; i
++)
2719 if (pclass
->printers
[i
] == first
)
2722 if (i
>= pclass
->num_printers
)
2724 first
->in_implicit_class
= 1;
2725 cupsdAddPrinterToClass(pclass
, first
);
2731 for (i
= 0; i
< pclass
->num_printers
; i
++)
2732 if (pclass
->printers
[i
] == p
)
2735 if (i
>= pclass
->num_printers
)
2737 p
->in_implicit_class
= 1;
2738 cupsdAddPrinterToClass(pclass
, p
);
2745 * First time around; just get name length and mark it as first
2749 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2750 len
= hptr
- p
->name
;
2752 len
= strlen(p
->name
);
2754 strncpy(name
, p
->name
, len
);
2758 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2759 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2762 * Can't use same name as a local printer; add "Any" to the
2763 * front of the name, unless we have explicitly disabled
2764 * the "ImplicitAnyClasses"...
2767 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2770 * Add "Any" to the class name...
2773 strcpy(name
, "Any");
2774 strncpy(name
+ 3, p
->name
, len
);
2775 name
[len
+ 3] = '\0';
2781 * Don't create an implicit class if we have a local printer
2782 * with the same name...
2786 cupsArrayRestore(Printers
);
2794 cupsArrayRestore(Printers
);
2798 * Update the last printer class as needed...
2801 if (pclass
&& update
)
2802 cupsdSetPrinterAttrs(pclass
);
2807 * 'send_cups_browse()' - Send new browsing information using the CUPS
2812 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2814 int i
; /* Looping var */
2815 cups_ptype_t type
; /* Printer type */
2816 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2817 int bytes
; /* Length of packet */
2818 char packet
[1453], /* Browse data packet */
2819 uri
[1024], /* Printer URI */
2820 location
[1024], /* printer-location */
2821 info
[1024], /* printer-info */
2823 /* printer-make-and-model */
2824 cupsd_netif_t
*iface
; /* Network interface */
2828 * Figure out the printer type value...
2831 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2834 type
|= CUPS_PRINTER_REJECTING
;
2836 if (p
== DefaultPrinter
)
2837 type
|= CUPS_PRINTER_DEFAULT
;
2840 * Remove quotes from printer-info, printer-location, and
2841 * printer-make-and-model attributes...
2844 dequote(location
, p
->location
, sizeof(location
));
2845 dequote(info
, p
->info
, sizeof(info
));
2848 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2849 else if (p
->type
& CUPS_PRINTER_CLASS
)
2851 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2852 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2854 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2857 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2859 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2862 * Send a packet to each browse address...
2865 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2869 * Send the browse packet to one or more interfaces...
2872 if (!strcmp(b
->iface
, "*"))
2875 * Send to all local interfaces...
2880 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2882 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2885 * Only send to local, IPv4 interfaces...
2888 if (!iface
->is_local
|| !iface
->port
||
2889 iface
->address
.addr
.sa_family
!= AF_INET
)
2892 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2893 iface
->hostname
, iface
->port
,
2894 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2897 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2898 type
, p
->state
, uri
, location
, info
, make_model
,
2899 p
->browse_attrs
? p
->browse_attrs
: "");
2901 bytes
= strlen(packet
);
2903 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2904 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2905 iface
->name
, packet
);
2907 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2909 sendto(BrowseSocket
, packet
, bytes
, 0,
2910 (struct sockaddr
*)&(iface
->broadcast
),
2911 httpAddrLength(&(iface
->broadcast
)));
2914 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2917 * Send to the named interface using the IPv4 address...
2921 if (strcmp(b
->iface
, iface
->name
))
2926 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2929 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2933 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2934 iface
->hostname
, iface
->port
,
2935 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2938 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2939 type
, p
->state
, uri
, location
, info
, make_model
,
2940 p
->browse_attrs
? p
->browse_attrs
: "");
2942 bytes
= strlen(packet
);
2944 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2945 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2946 iface
->name
, packet
);
2948 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2950 sendto(BrowseSocket
, packet
, bytes
, 0,
2951 (struct sockaddr
*)&(iface
->broadcast
),
2952 httpAddrLength(&(iface
->broadcast
)));
2959 * Send the browse packet to the indicated address using
2960 * the default server name...
2963 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2964 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2965 p
->browse_attrs
? p
->browse_attrs
: "");
2967 bytes
= strlen(packet
);
2968 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2969 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2971 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2972 (struct sockaddr
*)&(b
->to
),
2973 httpAddrLength(&(b
->to
))) <= 0)
2976 * Unable to send browse packet, so remove this address from the
2980 cupsdLogMessage(CUPSD_LOG_ERROR
,
2981 "cupsdSendBrowseList: sendto failed for browser "
2983 (int)(b
- Browsers
+ 1), strerror(errno
));
2986 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2995 #ifdef HAVE_OPENLDAP
2997 * 'send_ldap_browse()' - Send LDAP printer registrations.
3001 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3003 int i
; /* Looping var... */
3004 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3005 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3006 LDAPMessage
*res
; /* Search result token */
3007 char *cn_value
[2], /* Change records */
3013 typestring
[255], /* String to hold printer-type */
3014 filter
[256], /* Search filter for possible UPDATEs */
3015 dn
[1024]; /* DN of the printer we are adding */
3016 int rc
; /* LDAP status */
3017 static const char * const objectClass_values
[] =
3018 { /* The 3 objectClass's we use in */
3019 "top", /* our LDAP entries */
3025 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3028 * Everything in ldap is ** so we fudge around it...
3031 sprintf(typestring
, "%u", p
->type
);
3033 cn_value
[0] = p
->name
;
3035 info
[0] = p
->info
? p
->info
: "Unknown";
3037 location
[0] = p
->location
? p
->location
: "Unknown";
3039 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3040 make_model
[1] = NULL
;
3041 type
[0] = typestring
;
3046 snprintf(filter
, sizeof(filter
),
3047 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3049 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3050 filter
, (char **)ldap_attrs
, 0, &res
);
3051 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3054 mods
[0].mod_type
= "cn";
3055 mods
[0].mod_values
= cn_value
;
3056 mods
[1].mod_type
= "printerDescription";
3057 mods
[1].mod_values
= info
;
3058 mods
[2].mod_type
= "printerURI";
3059 mods
[2].mod_values
= uri
;
3060 mods
[3].mod_type
= "printerLocation";
3061 mods
[3].mod_values
= location
;
3062 mods
[4].mod_type
= "printerMakeAndModel";
3063 mods
[4].mod_values
= make_model
;
3064 mods
[5].mod_type
= "printerType";
3065 mods
[5].mod_values
= type
;
3066 mods
[6].mod_type
= "objectClass";
3067 mods
[6].mod_values
= (char **)objectClass_values
;
3069 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3070 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3072 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3075 * Printer has already been registered, modify the current
3079 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3080 "send_ldap_browse: Replacing entry...");
3082 for (i
= 0; i
< 7; i
++)
3084 pmods
[i
] = mods
+ i
;
3085 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3089 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3090 cupsdLogMessage(CUPSD_LOG_ERROR
,
3091 "LDAP modify for %s failed with status %d: %s",
3092 p
->name
, rc
, ldap_err2string(rc
));
3097 * Printer has never been registered, add the current
3101 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3102 "send_ldap_browse: Adding entry...");
3104 for (i
= 0; i
< 7; i
++)
3106 pmods
[i
] = mods
+ i
;
3107 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3111 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3112 cupsdLogMessage(CUPSD_LOG_ERROR
,
3113 "LDAP add for %s failed with status %d: %s",
3114 p
->name
, rc
, ldap_err2string(rc
));
3117 #endif /* HAVE_OPENLDAP */
3122 * 'send_slp_browse()' - Register the specified printer with SLP.
3126 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3128 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3129 attrs
[8192], /* Printer attributes */
3130 finishings
[1024], /* Finishings to support */
3131 make_model
[IPP_MAX_NAME
* 2],
3132 /* Make and model, quoted */
3133 location
[IPP_MAX_NAME
* 2],
3134 /* Location, quoted */
3135 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3136 *src
, /* Pointer to original string */
3137 *dst
; /* Pointer to destination string */
3138 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3139 SLPError error
; /* SLP error, if any */
3142 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3146 * Make the SLP service URL that conforms to the IANA
3147 * 'printer:' template.
3150 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3152 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3155 * Figure out the finishings string...
3158 if (p
->type
& CUPS_PRINTER_STAPLE
)
3159 strcpy(finishings
, "staple");
3161 finishings
[0] = '\0';
3163 if (p
->type
& CUPS_PRINTER_BIND
)
3166 strlcat(finishings
, ",bind", sizeof(finishings
));
3168 strcpy(finishings
, "bind");
3171 if (p
->type
& CUPS_PRINTER_PUNCH
)
3174 strlcat(finishings
, ",punch", sizeof(finishings
));
3176 strcpy(finishings
, "punch");
3179 if (p
->type
& CUPS_PRINTER_COVER
)
3182 strlcat(finishings
, ",cover", sizeof(finishings
));
3184 strcpy(finishings
, "cover");
3187 if (p
->type
& CUPS_PRINTER_SORT
)
3190 strlcat(finishings
, ",sort", sizeof(finishings
));
3192 strcpy(finishings
, "sort");
3196 strcpy(finishings
, "none");
3199 * Quote any commas in the make and model, location, and info strings...
3202 for (src
= p
->make_model
, dst
= make_model
;
3203 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3205 if (*src
== ',' || *src
== '\\' || *src
== ')')
3214 strcpy(make_model
, "Unknown");
3216 for (src
= p
->location
, dst
= location
;
3217 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3219 if (*src
== ',' || *src
== '\\' || *src
== ')')
3228 strcpy(location
, "Unknown");
3230 for (src
= p
->info
, dst
= info
;
3231 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3233 if (*src
== ',' || *src
== '\\' || *src
== ')')
3242 strcpy(info
, "Unknown");
3245 * Get the authentication value...
3248 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3252 * Make the SLP attribute string list that conforms to
3253 * the IANA 'printer:' template.
3256 snprintf(attrs
, sizeof(attrs
),
3257 "(printer-uri-supported=%s),"
3258 "(uri-authentication-supported=%s>),"
3260 "(uri-security-supported=tls>),"
3262 "(uri-security-supported=none>),"
3263 #endif /* HAVE_SSL */
3264 "(printer-name=%s),"
3265 "(printer-location=%s),"
3266 "(printer-info=%s),"
3267 "(printer-more-info=%s),"
3268 "(printer-make-and-model=%s),"
3269 "(printer-type=%d),"
3270 "(charset-supported=utf-8),"
3271 "(natural-language-configured=%s),"
3272 "(natural-language-supported=de,en,es,fr,it),"
3273 "(color-supported=%s),"
3274 "(finishings-supported=%s),"
3275 "(sides-supported=one-sided%s),"
3276 "(multiple-document-jobs-supported=true)"
3277 "(ipp-versions-supported=1.0,1.1)",
3278 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3279 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3280 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3282 p
->type
& CUPS_PRINTER_DUPLEX
?
3283 ",two-sided-long-edge,two-sided-short-edge" : "");
3285 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3288 * Register the printer with the SLP server...
3291 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3292 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3294 if (error
!= SLP_OK
)
3295 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3301 * 'slp_attr_callback()' - SLP attribute callback
3304 static SLPBoolean
/* O - SLP_TRUE for success */
3306 SLPHandle hslp
, /* I - SLP handle */
3307 const char *attrlist
, /* I - Attribute list */
3308 SLPError errcode
, /* I - Parsing status for this attr */
3309 void *cookie
) /* I - Current printer */
3311 char *tmp
= 0; /* Temporary string */
3312 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3313 /* Current printer */
3316 (void)hslp
; /* anti-compiler-warning-code */
3319 * Bail if there was an error
3322 if (errcode
!= SLP_OK
)
3326 * Parse the attrlist to obtain things needed to build CUPS browse packet
3329 memset(p
, 0, sizeof(cupsd_printer_t
));
3331 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3333 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3335 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3337 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3338 p
->type
= atoi(tmp
);
3340 p
->type
= CUPS_PRINTER_REMOTE
;
3342 cupsdClearString(&tmp
);
3349 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3353 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3355 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3358 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3360 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3363 * Make the SLP service URL that conforms to the IANA
3364 * 'printer:' template.
3367 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3370 * Deregister the printer...
3373 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3379 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3382 static int /* O - 0 on success */
3383 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3384 const char *tag
, /* I - Name of attribute */
3385 char **valbuf
) /* O - Value */
3387 char *ptr1
, /* Pointer into string */
3391 cupsdClearString(valbuf
);
3393 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3395 ptr1
+= strlen(tag
);
3397 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3403 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3404 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3407 * Dequote the value...
3410 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3411 if (*ptr1
== '\\' && ptr1
[1])
3412 _cups_strcpy(ptr1
, ptr1
+ 1);
3423 * 'slp_reg_callback()' - Empty SLPRegReport.
3427 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3428 SLPError errcode
, /* I - Error code, if any */
3429 void *cookie
) /* I - App data */
3440 * 'slp_url_callback()' - SLP service url callback
3443 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3445 SLPHandle hslp
, /* I - SLP handle */
3446 const char *srvurl
, /* I - URL of service */
3447 unsigned short lifetime
, /* I - Life of service */
3448 SLPError errcode
, /* I - Existing error code */
3449 void *cookie
) /* I - Pointer to service list */
3451 slpsrvurl_t
*s
, /* New service entry */
3452 **head
; /* Pointer to head of entry */
3456 * Let the compiler know we won't be using these vars...
3463 * Bail if there was an error
3466 if (errcode
!= SLP_OK
)
3470 * Grab the head of the list...
3473 head
= (slpsrvurl_t
**)cookie
;
3476 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3479 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3483 * Copy the SLP service URL...
3486 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3489 * Link the SLP service URL into the head of the list
3499 #endif /* HAVE_LIBSLP */
3503 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3507 update_cups_browse(void)
3509 int i
; /* Looping var */
3510 int auth
; /* Authorization status */
3511 int len
; /* Length of name string */
3512 int bytes
; /* Number of bytes left */
3513 char packet
[1541], /* Broadcast packet */
3514 *pptr
; /* Pointer into packet */
3515 socklen_t srclen
; /* Length of source address */
3516 http_addr_t srcaddr
; /* Source address */
3517 char srcname
[1024]; /* Source hostname */
3518 unsigned address
[4]; /* Source address */
3519 unsigned type
; /* Printer type */
3520 unsigned state
; /* Printer state */
3521 char uri
[HTTP_MAX_URI
], /* Printer URI */
3522 host
[HTTP_MAX_URI
], /* Host portion of URI */
3523 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3524 info
[IPP_MAX_NAME
], /* Information string */
3525 location
[IPP_MAX_NAME
], /* Location string */
3526 make_model
[IPP_MAX_NAME
];/* Make and model string */
3527 int num_attrs
; /* Number of attributes */
3528 cups_option_t
*attrs
; /* Attributes */
3532 * Read a packet from the browse socket...
3535 srclen
= sizeof(srcaddr
);
3536 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3537 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3540 * "Connection refused" is returned under Linux if the destination port
3541 * or address is unreachable from a previous sendto(); check for the
3542 * error here and ignore it for now...
3545 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3547 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3549 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
3551 cupsdStopBrowsing();
3558 packet
[bytes
] = '\0';
3561 * If we're about to sleep, ignore incoming browse packets.
3568 * Figure out where it came from...
3572 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3574 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3575 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3576 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3577 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3580 #endif /* AF_INET6 */
3585 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3588 if (HostNameLookups
)
3589 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3591 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3593 len
= strlen(srcname
);
3601 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3604 * Access from localhost (127.0.0.1) is always allowed...
3612 * Do authorization checks on the domain/address...
3615 switch (BrowseACL
->order_type
)
3618 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
3621 case AUTH_ALLOW
: /* Order Deny,Allow */
3624 if (cupsdCheckAuth(address
, srcname
, len
,
3625 BrowseACL
->num_deny
, BrowseACL
->deny
))
3628 if (cupsdCheckAuth(address
, srcname
, len
,
3629 BrowseACL
->num_allow
, BrowseACL
->allow
))
3633 case AUTH_DENY
: /* Order Allow,Deny */
3636 if (cupsdCheckAuth(address
, srcname
, len
,
3637 BrowseACL
->num_allow
, BrowseACL
->allow
))
3640 if (cupsdCheckAuth(address
, srcname
, len
,
3641 BrowseACL
->num_deny
, BrowseACL
->deny
))
3650 if (auth
== AUTH_DENY
)
3652 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3653 "update_cups_browse: Refused %d bytes from %s", bytes
,
3658 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3659 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3666 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3668 cupsdLogMessage(CUPSD_LOG_WARN
,
3669 "update_cups_browse: Garbled browse packet - %s", packet
);
3673 strcpy(location
, "Location Unknown");
3674 strcpy(info
, "No Information Available");
3675 make_model
[0] = '\0';
3679 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3682 * Have extended information; can't use sscanf for it because not all
3683 * sscanf's allow empty strings with %[^\"]...
3686 for (i
= 0, pptr
++;
3687 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3689 location
[i
] = *pptr
;
3697 while (*pptr
&& isspace(*pptr
& 255))
3702 for (i
= 0, pptr
++;
3703 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3712 while (*pptr
&& isspace(*pptr
& 255))
3717 for (i
= 0, pptr
++;
3718 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3720 make_model
[i
] = *pptr
;
3725 make_model
[i
] = '\0';
3728 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3734 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3735 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3736 type
, state
, uri
, location
, info
, make_model
));
3739 * Pull the URI apart to see if this is a local or remote printer...
3742 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
3744 cupsFreeOptions(num_attrs
, attrs
);
3752 for (i
= 0; i
< NumRelays
; i
++)
3753 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
3754 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3755 (struct sockaddr
*)&(Relays
[i
].to
),
3756 httpAddrLength(&(Relays
[i
].to
))) <= 0)
3758 cupsdLogMessage(CUPSD_LOG_ERROR
,
3759 "update_cups_browse: sendto failed for relay %d - %s.",
3760 i
+ 1, strerror(errno
));
3761 cupsFreeOptions(num_attrs
, attrs
);
3766 * Process the browse data...
3769 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
3770 (ipp_pstate_t
)state
, location
, info
, make_model
,
3776 * 'update_polling()' - Read status messages from the poll daemons.
3780 update_polling(void)
3782 char *ptr
, /* Pointer to end of line in buffer */
3783 message
[1024]; /* Pointer to message text */
3784 int loglevel
; /* Log level for message */
3787 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
3788 message
, sizeof(message
))) != NULL
)
3789 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
3792 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
3795 * All polling processes have died; stop polling...
3798 cupsdLogMessage(CUPSD_LOG_ERROR
,
3799 "update_polling: all polling processes have exited!");
3806 * End of "$Id: dirsvc.c 6538 2007-05-17 18:26:05Z mike $".