2 * "$Id: dirsvc.c 6354 2007-03-19 06:16:32Z 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 * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
42 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
43 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
44 * cupsdUpdatePolling() - Read status messages from the poll daemons.
45 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
46 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
47 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
49 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the
51 * dnssdRegisterCallback() - DNSServiceRegister callback.
52 * dnssdRegisterPrinter() - Start sending broadcast information for a
53 * printer or update the broadcast contents.
54 * dequote() - Remote quotes from a string.
55 * process_browse_data() - Process new browse data.
56 * process_implicit_classes() - Create/update implicit classes as needed.
57 * send_cups_browse() - Send new browsing information using the
59 * send_ldap_browse() - Send LDAP printer registrations.
60 * send_slp_browse() - Register the specified printer with SLP.
61 * slp_attr_callback() - SLP attribute callback
62 * slp_dereg_printer() - SLPDereg() the specified printer
63 * slp_get_attr() - Get an attribute from an SLP registration.
64 * slp_reg_callback() - Empty SLPRegReport.
65 * slp_url_callback() - SLP service url callback
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 */
110 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
112 "printerDescription",
114 "printerMakeAndModel",
119 #endif /* HAVE_OPENLDAP */
127 * SLP service name for CUPS...
130 # define SLP_CUPS_SRVTYPE "service:printer"
131 # define SLP_CUPS_SRVLEN 15
135 * Printer service URL structure
138 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
140 struct _slpsrvurl_s
*next
; /* Next URL in list */
141 char url
[HTTP_MAX_URI
];
150 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
151 SLPError errcode
, void *cookie
);
152 static void slp_dereg_printer(cupsd_printer_t
*p
);
153 static int slp_get_attr(const char *attrlist
, const char *tag
,
155 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
157 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
158 unsigned short lifetime
,
159 SLPError errcode
, void *cookie
);
160 #endif /* HAVE_LIBSLP */
164 * For IPP register using a subtype of 'cups' so that shared printer browsing
165 * only finds other CUPS servers (not all IPP based printers).
167 static char dnssdIPPRegType
[] = "_ipp._tcp,_cups";
168 static char dnssdIPPFaxRegType
[] = "_fax-ipp._tcp";
170 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
);
171 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
172 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
174 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
175 DNSServiceFlags flags
,
176 DNSServiceErrorType errorCode
,
177 const char *name
, const char *regtype
,
178 const char *domain
, void *context
);
179 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
180 #endif /* HAVE_DNSSD */
184 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
185 * local printer and remove any pending
186 * references to remote printers.
190 cupsdDeregisterPrinter(
191 cupsd_printer_t
*p
, /* I - Printer to register */
192 int removeit
) /* I - Printer being permanently removed */
195 * Only deregister if browsing is enabled and it's a local printers...
198 if (!Browsing
|| !p
->shared
||
199 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
203 * Announce the deletion...
206 if ((BrowseLocalProtocols
& BROWSE_CUPS
))
208 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
210 p
->type
|= CUPS_PRINTER_DELETE
;
218 if (BrowseLocalProtocols
& BROWSE_SLP
)
219 slp_dereg_printer(p
);
220 #endif /* HAVE_LIBSLP */
223 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
))
224 dnssdDeregisterPrinter(p
);
225 #endif /* HAVE_DNSSD */
230 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
234 cupsdLoadRemoteCache(void)
236 cups_file_t
*fp
; /* remote.cache file */
237 int linenum
; /* Current line number */
238 char line
[1024], /* Line from file */
239 *value
, /* Pointer to value */
240 *valueptr
, /* Pointer into value */
241 scheme
[32], /* Scheme portion of URI */
242 username
[64], /* Username portion of URI */
244 /* Hostname portion of URI */
245 resource
[HTTP_MAX_URI
];
246 /* Resource portion of URI */
247 int port
; /* Port number */
248 cupsd_printer_t
*p
; /* Current printer */
249 time_t now
; /* Current time */
253 * Don't load the cache if the CUPS remote protocol is disabled...
256 if (!Browsing
|| !(BrowseRemoteProtocols
& BROWSE_CUPS
))
258 cupsdLogMessage(CUPSD_LOG_DEBUG
,
259 "cupsdLoadRemoteCache: Not loading remote cache.");
264 * Open the remote.cache file...
267 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
268 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
272 * Read printer configurations until we hit EOF...
279 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
282 * Decode the directive...
285 if (!strcasecmp(line
, "<Printer") ||
286 !strcasecmp(line
, "<DefaultPrinter"))
289 * <Printer name> or <DefaultPrinter name>
292 if (p
== NULL
&& value
)
295 * Add the printer and a base file type...
298 cupsdLogMessage(CUPSD_LOG_DEBUG
,
299 "cupsdLoadRemoteCache: Loading printer %s...", value
);
301 if ((p
= cupsdFindDest(value
)) != NULL
)
303 if (p
->type
& CUPS_PRINTER_CLASS
)
305 cupsdLogMessage(CUPSD_LOG_WARN
,
306 "Cached remote printer \"%s\" conflicts with "
314 p
= cupsdAddPrinter(value
);
317 p
->state
= IPP_PRINTER_IDLE
;
318 p
->type
|= CUPS_PRINTER_REMOTE
;
319 p
->browse_time
= now
;
320 p
->browse_expire
= now
+ BrowseTimeout
;
323 * Set the default printer as needed...
326 if (!strcasecmp(line
, "<DefaultPrinter"))
331 cupsdLogMessage(CUPSD_LOG_ERROR
,
332 "Syntax error on line %d of remote.cache.", linenum
);
336 else if (!strcasecmp(line
, "<Class") ||
337 !strcasecmp(line
, "<DefaultClass"))
340 * <Class name> or <DefaultClass name>
343 if (p
== NULL
&& value
)
346 * Add the printer and a base file type...
349 cupsdLogMessage(CUPSD_LOG_DEBUG
,
350 "cupsdLoadRemoteCache: Loading class %s...", value
);
352 if ((p
= cupsdFindDest(value
)) != NULL
)
353 p
->type
= CUPS_PRINTER_CLASS
;
355 p
= cupsdAddClass(value
);
358 p
->state
= IPP_PRINTER_IDLE
;
359 p
->type
|= CUPS_PRINTER_REMOTE
;
360 p
->browse_time
= now
;
361 p
->browse_expire
= now
+ BrowseTimeout
;
364 * Set the default printer as needed...
367 if (!strcasecmp(line
, "<DefaultClass"))
372 cupsdLogMessage(CUPSD_LOG_ERROR
,
373 "Syntax error on line %d of remote.cache.", linenum
);
377 else if (!strcasecmp(line
, "</Printer>") ||
378 !strcasecmp(line
, "</Class>"))
383 * Close out the current printer...
386 cupsdSetPrinterAttrs(p
);
392 cupsdLogMessage(CUPSD_LOG_ERROR
,
393 "Syntax error on line %d of remote.cache.", linenum
);
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Syntax error on line %d of remote.cache.", linenum
);
403 else if (!strcasecmp(line
, "Info"))
406 cupsdSetString(&p
->info
, value
);
408 else if (!strcasecmp(line
, "MakeModel"))
411 cupsdSetString(&p
->make_model
, value
);
413 else if (!strcasecmp(line
, "Location"))
416 cupsdSetString(&p
->location
, value
);
418 else if (!strcasecmp(line
, "DeviceURI"))
422 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
423 username
, sizeof(username
), host
, sizeof(host
), &port
,
424 resource
, sizeof(resource
));
426 cupsdSetString(&p
->hostname
, host
);
427 cupsdSetString(&p
->uri
, value
);
428 cupsdSetString(&p
->device_uri
, value
);
432 cupsdLogMessage(CUPSD_LOG_ERROR
,
433 "Syntax error on line %d of remote.cache.", linenum
);
437 else if (!strcasecmp(line
, "Option") && value
)
443 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
446 cupsdLogMessage(CUPSD_LOG_ERROR
,
447 "Syntax error on line %d of remote.cache.", linenum
);
450 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
452 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
456 else if (!strcasecmp(line
, "State"))
459 * Set the initial queue state...
462 if (value
&& !strcasecmp(value
, "idle"))
463 p
->state
= IPP_PRINTER_IDLE
;
464 else if (value
&& !strcasecmp(value
, "stopped"))
465 p
->state
= IPP_PRINTER_STOPPED
;
468 cupsdLogMessage(CUPSD_LOG_ERROR
,
469 "Syntax error on line %d of remote.cache.", linenum
);
473 else if (!strcasecmp(line
, "StateMessage"))
476 * Set the initial queue state message...
480 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
482 else if (!strcasecmp(line
, "Accepting"))
485 * Set the initial accepting state...
489 (!strcasecmp(value
, "yes") ||
490 !strcasecmp(value
, "on") ||
491 !strcasecmp(value
, "true")))
494 (!strcasecmp(value
, "no") ||
495 !strcasecmp(value
, "off") ||
496 !strcasecmp(value
, "false")))
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Syntax error on line %d of remote.cache.", linenum
);
505 else if (!strcasecmp(line
, "Type"))
508 p
->type
= atoi(value
);
511 cupsdLogMessage(CUPSD_LOG_ERROR
,
512 "Syntax error on line %d of remote.cache.", linenum
);
516 else if (!strcasecmp(line
, "BrowseTime"))
520 time_t t
= atoi(value
);
522 if (t
> p
->browse_expire
)
523 p
->browse_expire
= t
;
527 cupsdLogMessage(CUPSD_LOG_ERROR
,
528 "Syntax error on line %d of remote.cache.", linenum
);
532 else if (!strcasecmp(line
, "JobSheets"))
535 * Set the initial job sheets...
540 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
545 cupsdSetString(&p
->job_sheets
[0], value
);
547 while (isspace(*valueptr
& 255))
552 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
557 cupsdSetString(&p
->job_sheets
[1], value
);
562 cupsdLogMessage(CUPSD_LOG_ERROR
,
563 "Syntax error on line %d of remote.cache.", linenum
);
567 else if (!strcasecmp(line
, "AllowUser"))
572 cupsdAddPrinterUser(p
, value
);
576 cupsdLogMessage(CUPSD_LOG_ERROR
,
577 "Syntax error on line %d of remote.cache.", linenum
);
581 else if (!strcasecmp(line
, "DenyUser"))
586 cupsdAddPrinterUser(p
, value
);
590 cupsdLogMessage(CUPSD_LOG_ERROR
,
591 "Syntax error on line %d of remote.cache.", linenum
);
598 * Something else we don't understand...
601 cupsdLogMessage(CUPSD_LOG_ERROR
,
602 "Unknown configuration directive %s on line %d of remote.cache.",
610 * Do auto-classing if needed...
613 process_implicit_classes();
618 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
619 * printer or update the broadcast contents.
623 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
625 if (!Browsing
|| !BrowseLocalProtocols
|| !BrowseInterval
|| !NumBrowsers
||
626 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
630 /* if (BrowseLocalProtocols & BROWSE_SLP)
631 slpRegisterPrinter(p); */
632 #endif /* HAVE_LIBSLP */
635 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
636 dnssdRegisterPrinter(p
);
637 #endif /* HAVE_DNSSD */
642 * 'cupsdRestartPolling()' - Restart polling servers as needed.
646 cupsdRestartPolling(void)
648 int i
; /* Looping var */
649 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
652 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
654 kill(pollp
->pid
, SIGHUP
);
659 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
663 cupsdSaveRemoteCache(void)
665 int i
; /* Looping var */
666 cups_file_t
*fp
; /* printers.conf file */
667 char temp
[1024]; /* Temporary string */
668 cupsd_printer_t
*printer
; /* Current printer class */
669 time_t curtime
; /* Current time */
670 struct tm
*curdate
; /* Current date */
671 cups_option_t
*option
; /* Current option */
675 * Create the remote.cache file...
678 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
680 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
682 cupsdLogMessage(CUPSD_LOG_ERROR
,
683 "Unable to save remote.cache - %s", strerror(errno
));
687 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
690 * Restrict access to the file...
693 fchown(cupsFileNumber(fp
), getuid(), Group
);
694 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
697 * Write a small header to the file...
700 curtime
= time(NULL
);
701 curdate
= localtime(&curtime
);
702 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
704 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
705 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
708 * Write each local printer known to the system...
711 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
713 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
716 * Skip local destinations...
719 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
723 * Write printers as needed...
726 if (printer
== DefaultPrinter
)
727 cupsFilePuts(fp
, "<Default");
729 cupsFilePutChar(fp
, '<');
731 if (printer
->type
& CUPS_PRINTER_CLASS
)
732 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
734 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
736 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
738 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
741 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
743 if (printer
->make_model
)
744 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
746 if (printer
->location
)
747 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
749 if (printer
->device_uri
)
750 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
752 if (printer
->state
== IPP_PRINTER_STOPPED
)
754 cupsFilePuts(fp
, "State Stopped\n");
755 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
758 cupsFilePuts(fp
, "State Idle\n");
760 if (printer
->accepting
)
761 cupsFilePuts(fp
, "Accepting Yes\n");
763 cupsFilePuts(fp
, "Accepting No\n");
765 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
766 printer
->job_sheets
[1]);
768 for (i
= 0; i
< printer
->num_users
; i
++)
769 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
772 for (i
= printer
->num_options
, option
= printer
->options
;
775 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
777 if (printer
->type
& CUPS_PRINTER_CLASS
)
778 cupsFilePuts(fp
, "</Class>\n");
780 cupsFilePuts(fp
, "</Printer>\n");
788 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
792 cupsdSendBrowseList(void)
794 int count
; /* Number of dests to update */
795 cupsd_printer_t
*p
; /* Current printer */
796 time_t ut
, /* Minimum update time */
797 to
; /* Timeout time */
800 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
804 * Compute the update and timeout times...
808 ut
= to
- BrowseInterval
;
811 * Figure out how many printers need an update...
814 if (BrowseInterval
> 0)
816 int max_count
; /* Maximum number to update */
820 * Throttle the number of printers we'll be updating this time
821 * around based on the number of queues that need updating and
822 * the maximum number of queues to update each second...
825 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
827 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
828 count
< max_count
&& p
!= NULL
;
829 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
830 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
831 p
->shared
&& p
->browse_time
< ut
)
835 * Loop through all of the printers and send local updates as needed...
839 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
841 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
845 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
848 * Check for wraparound...
852 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
856 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
859 else if (p
->browse_time
< ut
)
862 * Need to send an update...
867 p
->browse_time
= time(NULL
);
869 if (BrowseLocalProtocols
& BROWSE_CUPS
)
873 if (BrowseLocalProtocols
& BROWSE_SLP
)
875 #endif /* HAVE_LIBSLP */
878 if (BrowseLocalProtocols
& BROWSE_LDAP
)
880 #endif /* HAVE_LDAP */
885 * Save where we left off so that all printers get updated...
892 * Loop through all of the printers and send local updates as needed...
895 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
897 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
900 * If this is a remote queue, see if it needs to be timed out...
903 if (p
->type
& CUPS_PRINTER_REMOTE
)
905 if (p
->browse_expire
< to
)
907 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
908 "%s \'%s\' deleted by directory services (timeout).",
909 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
912 cupsdLogMessage(CUPSD_LOG_DEBUG
,
913 "Remote destination \"%s\" has timed out; "
917 cupsArraySave(Printers
);
918 cupsdDeletePrinter(p
, 1);
919 cupsArrayRestore(Printers
);
927 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
931 cupsdStartBrowsing(void)
933 int val
; /* Socket option value */
934 struct sockaddr_in addr
; /* Broadcast address */
935 cupsd_printer_t
*p
; /* Current printer */
940 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
943 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
945 if (BrowseSocket
< 0)
948 * Create the broadcast socket...
951 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
953 cupsdLogMessage(CUPSD_LOG_ERROR
,
954 "cupsdStartBrowsing: Unable to create broadcast "
955 "socket - %s.", strerror(errno
));
956 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
957 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
962 * Bind the socket to browse port...
965 memset(&addr
, 0, sizeof(addr
));
966 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
967 addr
.sin_family
= AF_INET
;
968 addr
.sin_port
= htons(BrowsePort
);
970 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
972 cupsdLogMessage(CUPSD_LOG_ERROR
,
973 "cupsdStartBrowsing: Unable to bind broadcast "
974 "socket - %s.", strerror(errno
));
977 closesocket(BrowseSocket
);
983 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
984 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
990 * Set the "broadcast" flag...
994 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
996 cupsdLogMessage(CUPSD_LOG_ERROR
,
997 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
1001 closesocket(BrowseSocket
);
1003 close(BrowseSocket
);
1007 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1008 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1013 * Close the socket on exec...
1016 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1019 * Finally, add the socket to the input selection set as needed...
1022 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1025 * We only listen if we want remote printers...
1028 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)cupsdUpdateCUPSBrowse
,
1036 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1039 * Open SLP handle...
1042 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "Unable to open an SLP handle; disabling SLP browsing!");
1046 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1047 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1050 BrowseSLPRefresh
= 0;
1053 BrowseSLPHandle
= NULL
;
1054 #endif /* HAVE_LIBSLP */
1056 #ifdef HAVE_OPENLDAP
1057 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1061 cupsdLogMessage(CUPSD_LOG_ERROR
,
1062 "Need to set BrowseLDAPDN to use LDAP browsing!");
1063 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1064 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1069 * Open LDAP handle...
1072 int rc
; /* LDAP API status */
1073 int version
= 3; /* LDAP version */
1074 struct berval bv
= {0, ""}; /* SASL bind value */
1078 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1081 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1082 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1084 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1086 if (rc
!= LDAP_SUCCESS
)
1088 cupsdLogMessage(CUPSD_LOG_ERROR
,
1089 "Unable to initialize LDAP; disabling LDAP browsing!");
1090 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1091 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1093 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1094 (const void *)&version
) != LDAP_SUCCESS
)
1096 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1097 BrowseLDAPHandle
= NULL
;
1098 cupsdLogMessage(CUPSD_LOG_ERROR
,
1099 "Unable to set LDAP protocol version; "
1100 "disabling LDAP browsing!");
1101 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1102 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1106 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1107 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1110 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1111 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1113 if (rc
!= LDAP_SUCCESS
)
1115 cupsdLogMessage(CUPSD_LOG_ERROR
,
1116 "Unable to bind to LDAP server; "
1117 "disabling LDAP browsing!");
1118 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1119 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1120 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1125 BrowseLDAPRefresh
= 0;
1127 #endif /* HAVE_OPENLDAP */
1130 * Register the individual printers
1133 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1135 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1136 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1137 cupsdRegisterPrinter(p
);
1142 * 'cupsdStartPolling()' - Start polling servers as needed.
1146 cupsdStartPolling(void)
1148 int i
; /* Looping var */
1149 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1150 char polld
[1024]; /* Poll daemon path */
1151 char sport
[255]; /* Server port */
1152 char bport
[255]; /* Browser port */
1153 char interval
[255]; /* Poll interval */
1154 int statusfds
[2]; /* Status pipe */
1155 char *argv
[6]; /* Arguments */
1156 char *envp
[100]; /* Environment */
1160 * Don't do anything if we aren't polling...
1163 if (NumPolled
== 0 || BrowseSocket
< 0)
1166 PollStatusBuffer
= NULL
;
1171 * Setup string arguments for polld, port and interval options.
1174 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1176 sprintf(bport
, "%d", BrowsePort
);
1179 sprintf(interval
, "%d", BrowseInterval
);
1181 strcpy(interval
, "30");
1183 argv
[0] = "cups-polld";
1189 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1192 * Create a pipe that receives the status messages from each
1196 if (cupsdOpenPipe(statusfds
))
1198 cupsdLogMessage(CUPSD_LOG_ERROR
,
1199 "Unable to create polling status pipes - %s.",
1202 PollStatusBuffer
= NULL
;
1206 PollPipe
= statusfds
[0];
1207 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1210 * Run each polling daemon, redirecting stderr to the polling pipe...
1213 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1215 sprintf(sport
, "%d", pollp
->port
);
1217 argv
[1] = pollp
->hostname
;
1219 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1220 0, &(pollp
->pid
)) < 0)
1222 cupsdLogMessage(CUPSD_LOG_ERROR
,
1223 "cupsdStartPolling: Unable to fork polling daemon - %s",
1229 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1230 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1231 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1234 close(statusfds
[1]);
1237 * Finally, add the pipe to the input selection set...
1240 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)cupsdUpdatePolling
, NULL
, NULL
);
1245 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1249 cupsdStopBrowsing(void)
1251 cupsd_printer_t
*p
; /* Current printer */
1254 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1258 * De-register the individual printers
1261 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1263 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1264 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1265 cupsdDeregisterPrinter(p
, 1);
1268 * Shut down browsing sockets...
1271 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1275 * Close the socket and remove it from the input selection set.
1279 closesocket(BrowseSocket
);
1281 close(BrowseSocket
);
1284 cupsdRemoveSelect(BrowseSocket
);
1289 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1293 * Close SLP handle...
1296 SLPClose(BrowseSLPHandle
);
1297 BrowseSLPHandle
= NULL
;
1299 #endif /* HAVE_LIBSLP */
1301 #ifdef HAVE_OPENLDAP
1302 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1305 ldap_unbind(BrowseLDAPHandle
);
1306 BrowseLDAPHandle
= NULL
;
1308 #endif /* HAVE_OPENLDAP */
1313 * 'cupsdStopPolling()' - Stop polling servers as needed.
1317 cupsdStopPolling(void)
1319 int i
; /* Looping var */
1320 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1325 cupsdStatBufDelete(PollStatusBuffer
);
1328 cupsdRemoveSelect(PollPipe
);
1331 PollStatusBuffer
= NULL
;
1334 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1336 cupsdEndProcess(pollp
->pid
, 0);
1341 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1345 cupsdUpdateCUPSBrowse(void)
1347 int i
; /* Looping var */
1348 int auth
; /* Authorization status */
1349 int len
; /* Length of name string */
1350 int bytes
; /* Number of bytes left */
1351 char packet
[1541], /* Broadcast packet */
1352 *pptr
; /* Pointer into packet */
1353 socklen_t srclen
; /* Length of source address */
1354 http_addr_t srcaddr
; /* Source address */
1355 char srcname
[1024]; /* Source hostname */
1356 unsigned address
[4]; /* Source address */
1357 unsigned type
; /* Printer type */
1358 unsigned state
; /* Printer state */
1359 char uri
[HTTP_MAX_URI
], /* Printer URI */
1360 host
[HTTP_MAX_URI
], /* Host portion of URI */
1361 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1362 info
[IPP_MAX_NAME
], /* Information string */
1363 location
[IPP_MAX_NAME
], /* Location string */
1364 make_model
[IPP_MAX_NAME
];/* Make and model string */
1365 int num_attrs
; /* Number of attributes */
1366 cups_option_t
*attrs
; /* Attributes */
1370 * Read a packet from the browse socket...
1373 srclen
= sizeof(srcaddr
);
1374 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1375 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1378 * "Connection refused" is returned under Linux if the destination port
1379 * or address is unreachable from a previous sendto(); check for the
1380 * error here and ignore it for now...
1383 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1385 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1387 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1389 cupsdStopBrowsing();
1396 packet
[bytes
] = '\0';
1399 * If we're about to sleep, ignore incoming browse packets.
1406 * Figure out where it came from...
1410 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1412 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1413 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1414 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1415 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1418 #endif /* AF_INET6 */
1423 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1426 if (HostNameLookups
)
1427 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1429 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1431 len
= strlen(srcname
);
1439 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1442 * Access from localhost (127.0.0.1) is always allowed...
1450 * Do authorization checks on the domain/address...
1453 switch (BrowseACL
->order_type
)
1456 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1459 case AUTH_ALLOW
: /* Order Deny,Allow */
1462 if (cupsdCheckAuth(address
, srcname
, len
,
1463 BrowseACL
->num_deny
, BrowseACL
->deny
))
1466 if (cupsdCheckAuth(address
, srcname
, len
,
1467 BrowseACL
->num_allow
, BrowseACL
->allow
))
1471 case AUTH_DENY
: /* Order Allow,Deny */
1474 if (cupsdCheckAuth(address
, srcname
, len
,
1475 BrowseACL
->num_allow
, BrowseACL
->allow
))
1478 if (cupsdCheckAuth(address
, srcname
, len
,
1479 BrowseACL
->num_deny
, BrowseACL
->deny
))
1488 if (auth
== AUTH_DENY
)
1490 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1491 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1496 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1497 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1504 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1506 cupsdLogMessage(CUPSD_LOG_WARN
,
1507 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1511 strcpy(location
, "Location Unknown");
1512 strcpy(info
, "No Information Available");
1513 make_model
[0] = '\0';
1517 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1520 * Have extended information; can't use sscanf for it because not all
1521 * sscanf's allow empty strings with %[^\"]...
1524 for (i
= 0, pptr
++;
1525 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1527 location
[i
] = *pptr
;
1535 while (*pptr
&& isspace(*pptr
& 255))
1540 for (i
= 0, pptr
++;
1541 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1550 while (*pptr
&& isspace(*pptr
& 255))
1555 for (i
= 0, pptr
++;
1556 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1558 make_model
[i
] = *pptr
;
1563 make_model
[i
] = '\0';
1566 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1572 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1573 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1574 type
, state
, uri
, location
, info
, make_model
));
1577 * Pull the URI apart to see if this is a local or remote printer...
1580 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1582 cupsFreeOptions(num_attrs
, attrs
);
1590 for (i
= 0; i
< NumRelays
; i
++)
1591 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1592 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1593 (struct sockaddr
*)&(Relays
[i
].to
),
1594 httpAddrLength(&(Relays
[i
].to
))) <= 0)
1596 cupsdLogMessage(CUPSD_LOG_ERROR
,
1597 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1598 i
+ 1, strerror(errno
));
1599 cupsFreeOptions(num_attrs
, attrs
);
1604 * Process the browse data...
1607 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
1608 (ipp_pstate_t
)state
, location
, info
, make_model
,
1615 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1619 cupsdUpdateDNSSDBrowse(
1620 cupsd_printer_t
*p
) /* I - Printer being queried */
1622 DNSServiceErrorType sdErr
; /* Service discovery error */
1625 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1626 != kDNSServiceErr_NoError
)
1628 cupsdLogMessage(CUPSD_LOG_ERROR
,
1629 "DNS Service Discovery registration error %d for \"%s\"!",
1631 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1632 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1634 p
->dnssd_ipp_ref
= NULL
;
1635 p
->dnssd_ipp_fd
= -1;
1638 #endif /* HAVE_DNSSD */
1641 #ifdef HAVE_OPENLDAP
1643 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1647 cupsdUpdateLDAPBrowse(void)
1649 char uri
[HTTP_MAX_URI
], /* Printer URI */
1650 host
[HTTP_MAX_URI
], /* Hostname */
1651 resource
[HTTP_MAX_URI
], /* Resource path */
1652 location
[1024], /* Printer location */
1653 info
[1024], /* Printer information */
1654 make_model
[1024], /* Printer make and model */
1655 **value
; /* Holds the returned data from LDAP */
1656 int type
; /* Printer type */
1657 int rc
; /* LDAP status */
1658 int limit
; /* Size limit */
1659 LDAPMessage
*res
, /* LDAP search results */
1660 *e
; /* Current entry from search */
1664 * Search for printers...
1667 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1669 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1671 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1672 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1673 if (rc
!= LDAP_SUCCESS
)
1675 cupsdLogMessage(CUPSD_LOG_ERROR
,
1676 "LDAP search returned error %d: %s", rc
,
1677 ldap_err2string(rc
));
1681 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1682 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1687 * Loop through the available printers...
1690 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1692 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1695 * Get the required values from this entry...
1698 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1699 "printerDescription")) == NULL
)
1702 strlcpy(info
, *value
, sizeof(info
));
1703 ldap_value_free(value
);
1705 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1706 "printerLocation")) == NULL
)
1709 strlcpy(location
, *value
, sizeof(location
));
1710 ldap_value_free(value
);
1712 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1713 "printerMakeAndModel")) == NULL
)
1716 strlcpy(make_model
, *value
, sizeof(make_model
));
1717 ldap_value_free(value
);
1719 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1720 "printerType")) == NULL
)
1723 type
= atoi(*value
);
1724 ldap_value_free(value
);
1726 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1727 "printerURI")) == NULL
)
1730 strlcpy(uri
, *value
, sizeof(uri
));
1731 ldap_value_free(value
);
1734 * Process the entry as browse data...
1737 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1738 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1739 location
, info
, make_model
, 0, NULL
);
1743 #endif /* HAVE_OPENLDAP */
1747 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1751 cupsdUpdatePolling(void)
1753 char *ptr
, /* Pointer to end of line in buffer */
1754 message
[1024]; /* Pointer to message text */
1755 int loglevel
; /* Log level for message */
1758 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1759 message
, sizeof(message
))) != NULL
)
1760 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1763 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
1766 * All polling processes have died; stop polling...
1769 cupsdLogMessage(CUPSD_LOG_ERROR
,
1770 "cupsdUpdatePolling: all polling processes have exited!");
1778 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1782 cupsdUpdateSLPBrowse(void)
1784 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1785 *next
; /* Next service in list */
1786 cupsd_printer_t p
; /* Printer information */
1787 const char *uri
; /* Pointer to printer URI */
1788 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1789 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1793 * Reset the refresh time...
1796 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1799 * Poll for remote printers using SLP...
1804 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1805 slp_url_callback
, &s
);
1808 * Loop through the list of available printers...
1814 * Save the "next" pointer...
1820 * Load a cupsd_printer_t structure with the SLP service attributes...
1823 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1826 * Process this printer entry...
1829 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1831 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1834 * Pull the URI apart to see if this is a local or remote printer...
1837 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1838 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1839 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1843 * Free this listing...
1846 cupsdClearString(&p
.info
);
1847 cupsdClearString(&p
.location
);
1848 cupsdClearString(&p
.make_model
);
1853 #endif /* HAVE_LIBSLP */
1857 * 'dequote()' - Remote quotes from a string.
1860 static char * /* O - Dequoted string */
1861 dequote(char *d
, /* I - Destination string */
1862 const char *s
, /* I - Source string */
1863 int dlen
) /* I - Destination length */
1865 char *dptr
; /* Pointer into destination */
1870 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1887 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1890 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1891 is_local_queue(const char *uri
, /* I - Printer URI */
1892 char *host
, /* O - Host string */
1893 int hostlen
, /* I - Length of host buffer */
1894 char *resource
, /* O - Resource string */
1895 int resourcelen
) /* I - Length of resource buffer */
1897 char scheme
[32], /* Scheme portion of URI */
1898 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1899 int port
; /* Port portion of URI */
1900 cupsd_netif_t
*iface
; /* Network interface */
1904 * Pull the URI apart to see if this is a local or remote printer...
1907 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1908 username
, sizeof(username
), host
, hostlen
, &port
,
1909 resource
, resourcelen
) < HTTP_URI_OK
)
1912 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1915 * Check for local server addresses...
1918 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1923 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1925 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1926 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1930 * If we get here, the printer is remote...
1938 * 'process_browse_data()' - Process new browse data.
1942 process_browse_data(
1943 const char *uri
, /* I - URI of printer/class */
1944 const char *host
, /* I - Hostname */
1945 const char *resource
, /* I - Resource path */
1946 cups_ptype_t type
, /* I - Printer type */
1947 ipp_pstate_t state
, /* I - Printer state */
1948 const char *location
, /* I - Printer location */
1949 const char *info
, /* I - Printer information */
1950 const char *make_model
, /* I - Printer make and model */
1951 int num_attrs
, /* I - Number of attributes */
1952 cups_option_t
*attrs
) /* I - Attributes */
1954 int i
; /* Looping var */
1955 int update
; /* Update printer attributes? */
1956 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1957 name
[IPP_MAX_NAME
], /* Name of printer */
1958 newname
[IPP_MAX_NAME
], /* New name of printer */
1959 *hptr
, /* Pointer into hostname */
1960 *sptr
; /* Pointer into ServerName */
1961 char local_make_model
[IPP_MAX_NAME
];
1962 /* Local make and model */
1963 cupsd_printer_t
*p
; /* Printer information */
1964 const char *ipp_options
, /* ipp-options value */
1965 *lease_duration
; /* lease-duration value */
1969 * Determine if the URI contains any illegal characters in it...
1972 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1973 (strncmp(resource
, "/printers/", 10) &&
1974 strncmp(resource
, "/classes/", 9)))
1976 cupsdLogMessage(CUPSD_LOG_ERROR
,
1977 "process_browse_data: Bad printer URI in browse data: %s",
1982 if (strchr(resource
, '?') ||
1983 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1984 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1986 cupsdLogMessage(CUPSD_LOG_ERROR
,
1987 "process_browse_data: Bad resource in browse data: %s",
1993 * OK, this isn't a local printer; add any remote options...
1996 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1998 if (BrowseRemoteOptions
)
2000 if (BrowseRemoteOptions
[0] == '?')
2003 * Override server-supplied options...
2006 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2008 else if (ipp_options
)
2011 * Combine the server and local options...
2014 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2015 BrowseRemoteOptions
);
2020 * Just use the local options...
2023 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2028 else if (ipp_options
)
2031 * Just use the server-supplied options...
2034 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2039 * See if we already have it listed in the Printers list, and add it if not...
2042 type
|= CUPS_PRINTER_REMOTE
;
2043 type
&= ~CUPS_PRINTER_IMPLICIT
;
2045 hptr
= strchr(host
, '.');
2046 sptr
= strchr(ServerName
, '.');
2048 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2051 * Strip the common domain name components...
2054 while (hptr
!= NULL
)
2056 if (!strcasecmp(hptr
, sptr
))
2062 hptr
= strchr(hptr
+ 1, '.');
2066 if (type
& CUPS_PRINTER_CLASS
)
2069 * Remote destination is a class...
2072 if (!strncmp(resource
, "/classes/", 9))
2073 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2078 *hptr
= '.'; /* Resource FQDN */
2080 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
2082 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
2084 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2087 * Nope, this isn't the same host; if the hostname isn't the local host,
2088 * add it to the other class and then find a class using the full host
2092 if (p
->type
& CUPS_PRINTER_REMOTE
)
2094 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2095 "Renamed remote class \"%s\" to \"%s@%s\"...",
2096 p
->name
, p
->name
, p
->hostname
);
2097 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2098 "Class \'%s\' deleted by directory services.",
2101 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2102 cupsdRenamePrinter(p
, newname
);
2104 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2105 "Class \'%s\' added by directory services.",
2111 else if (!p
->hostname
)
2114 * Hostname not set, so this must be a cached remote printer
2115 * that was created for a pending print job...
2118 cupsdSetString(&p
->hostname
, host
);
2119 cupsdSetString(&p
->uri
, uri
);
2120 cupsdSetString(&p
->device_uri
, uri
);
2127 * Use the short name for this shared class.
2130 strlcpy(name
, resource
+ 9, sizeof(name
));
2133 else if (p
&& !p
->hostname
)
2136 * Hostname not set, so this must be a cached remote printer
2137 * that was created for a pending print job...
2140 cupsdSetString(&p
->hostname
, host
);
2141 cupsdSetString(&p
->uri
, uri
);
2142 cupsdSetString(&p
->device_uri
, uri
);
2149 * Class doesn't exist; add it...
2152 p
= cupsdAddClass(name
);
2154 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2156 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2157 "Class \'%s\' added by directory services.", name
);
2160 * Force the URI to point to the real server...
2163 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2165 cupsdSetString(&p
->uri
, uri
);
2166 cupsdSetString(&p
->device_uri
, uri
);
2167 cupsdSetString(&p
->hostname
, host
);
2175 * Remote destination is a printer...
2178 if (!strncmp(resource
, "/printers/", 10))
2179 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2184 *hptr
= '.'; /* Resource FQDN */
2186 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2188 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2190 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2193 * Nope, this isn't the same host; if the hostname isn't the local host,
2194 * add it to the other printer and then find a printer using the full host
2198 if (p
->type
& CUPS_PRINTER_REMOTE
)
2200 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2201 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2202 p
->name
, p
->name
, p
->hostname
);
2203 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2204 "Printer \'%s\' deleted by directory services.",
2207 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2208 cupsdRenamePrinter(p
, newname
);
2210 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2211 "Printer \'%s\' added by directory services.",
2217 else if (!p
->hostname
)
2220 * Hostname not set, so this must be a cached remote printer
2221 * that was created for a pending print job...
2224 cupsdSetString(&p
->hostname
, host
);
2225 cupsdSetString(&p
->uri
, uri
);
2226 cupsdSetString(&p
->device_uri
, uri
);
2233 * Use the short name for this shared printer.
2236 strlcpy(name
, resource
+ 10, sizeof(name
));
2239 else if (p
&& !p
->hostname
)
2242 * Hostname not set, so this must be a cached remote printer
2243 * that was created for a pending print job...
2246 cupsdSetString(&p
->hostname
, host
);
2247 cupsdSetString(&p
->uri
, uri
);
2248 cupsdSetString(&p
->device_uri
, uri
);
2255 * Printer doesn't exist; add it...
2258 p
= cupsdAddPrinter(name
);
2260 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2261 "Printer \'%s\' added by directory services.", name
);
2263 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2266 * Force the URI to point to the real server...
2269 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2271 cupsdSetString(&p
->hostname
, host
);
2272 cupsdSetString(&p
->uri
, uri
);
2273 cupsdSetString(&p
->device_uri
, uri
);
2280 * Update the state...
2284 p
->browse_time
= time(NULL
);
2286 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2290 * Grab the lease-duration for the browse data; anything less then 1
2291 * second or more than 1 week gets the default BrowseTimeout...
2294 i
= atoi(lease_duration
);
2295 if (i
< 1 || i
> 604800)
2298 p
->browse_expire
= p
->browse_time
+ i
;
2301 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2303 if (type
& CUPS_PRINTER_REJECTING
)
2305 type
&= ~CUPS_PRINTER_REJECTING
;
2313 else if (!p
->accepting
)
2319 if (p
->type
!= type
)
2325 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2327 cupsdSetString(&p
->location
, location
);
2331 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2333 cupsdSetString(&p
->info
, info
);
2337 if (!make_model
|| !make_model
[0])
2339 if (type
& CUPS_PRINTER_CLASS
)
2340 snprintf(local_make_model
, sizeof(local_make_model
),
2341 "Remote Class on %s", host
);
2343 snprintf(local_make_model
, sizeof(local_make_model
),
2344 "Remote Printer on %s", host
);
2347 snprintf(local_make_model
, sizeof(local_make_model
),
2348 "%s on %s", make_model
, host
);
2350 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2352 cupsdSetString(&p
->make_model
, local_make_model
);
2358 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2361 * See if we need to update the attributes...
2364 if (p
->num_options
!= num_attrs
)
2368 for (i
= 0; i
< num_attrs
; i
++)
2369 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2370 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2371 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2380 * Free the old options...
2383 cupsFreeOptions(p
->num_options
, p
->options
);
2386 p
->num_options
= num_attrs
;
2389 if (type
& CUPS_PRINTER_DELETE
)
2391 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2392 "%s \'%s\' deleted by directory services.",
2393 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2395 cupsdExpireSubscriptions(p
, NULL
);
2397 cupsdDeletePrinter(p
, 1);
2398 cupsdUpdateImplicitClasses();
2402 cupsdSetPrinterAttrs(p
);
2403 cupsdUpdateImplicitClasses();
2407 * See if we have a default printer... If not, make the first network
2408 * default printer the default.
2411 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2414 * Find the first network default printer and use it...
2417 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2419 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2420 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2428 * Do auto-classing if needed...
2431 process_implicit_classes();
2434 * Update the printcap file...
2437 cupsdWritePrintcap();
2443 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2446 static char * /* O - TXT record */
2447 dnssdBuildTxtRecord(
2448 int *txt_len
, /* O - TXT record length */
2449 cupsd_printer_t
*p
) /* I - Printer information */
2451 int i
, j
; /* Looping vars */
2452 char type_str
[32], /* Type to string buffer */
2453 state_str
[32], /* State to string buffer */
2454 rp_str
[1024], /* Queue name string buffer */
2455 air_str
[1024], /* auth-info-required string buffer */
2456 *keyvalue
[32][2]; /* Table of key/value pairs */
2460 * Load up the key value pairs...
2465 keyvalue
[i
][0] = "txtvers";
2466 keyvalue
[i
++][1] = "1";
2468 keyvalue
[i
][0] = "qtotal";
2469 keyvalue
[i
++][1] = "1";
2471 keyvalue
[i
][0] = "rp";
2472 keyvalue
[i
++][1] = rp_str
;
2473 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2474 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2476 keyvalue
[i
][0] = "ty";
2477 keyvalue
[i
++][1] = p
->make_model
;
2479 if (p
->location
&& *p
->location
!= '\0')
2481 keyvalue
[i
][0] = "note";
2482 keyvalue
[i
++][1] = p
->location
;
2485 keyvalue
[i
][0] = "product";
2486 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2488 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2489 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2491 keyvalue
[i
][0] = "printer-state";
2492 keyvalue
[i
++][1] = state_str
;
2494 keyvalue
[i
][0] = "printer-type";
2495 keyvalue
[i
++][1] = type_str
;
2497 keyvalue
[i
][0] = "Transparent";
2498 keyvalue
[i
++][1] = "T";
2500 keyvalue
[i
][0] = "Binary";
2501 keyvalue
[i
++][1] = "T";
2503 if ((p
->type
& CUPS_PRINTER_FAX
))
2505 keyvalue
[i
][0] = "Fax";
2506 keyvalue
[i
++][1] = "T";
2509 if ((p
->type
& CUPS_PRINTER_COLOR
))
2511 keyvalue
[i
][0] = "Color";
2512 keyvalue
[i
++][1] = "T";
2515 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2517 keyvalue
[i
][0] = "Duplex";
2518 keyvalue
[i
++][1] = "T";
2521 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2523 keyvalue
[i
][0] = "Staple";
2524 keyvalue
[i
++][1] = "T";
2527 if ((p
->type
& CUPS_PRINTER_COPIES
))
2529 keyvalue
[i
][0] = "Copies";
2530 keyvalue
[i
++][1] = "T";
2533 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2535 keyvalue
[i
][0] = "Collate";
2536 keyvalue
[i
++][1] = "T";
2539 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2541 keyvalue
[i
][0] = "Punch";
2542 keyvalue
[i
++][1] = "T";
2545 if ((p
->type
& CUPS_PRINTER_BIND
))
2547 keyvalue
[i
][0] = "Bind";
2548 keyvalue
[i
++][1] = "T";
2551 if ((p
->type
& CUPS_PRINTER_SORT
))
2553 keyvalue
[i
][0] = "Sort";
2554 keyvalue
[i
++][1] = "T";
2557 keyvalue
[i
][0] = "pdl";
2558 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2560 if (p
->num_auth_info_required
)
2562 char *air
= air_str
; /* Pointer into string */
2565 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2567 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2573 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2577 keyvalue
[i
][0] = "air";
2578 keyvalue
[i
++][1] = air
;
2582 * Then pack them into a proper txt record...
2585 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2590 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2595 dnssdDeregisterPrinter(
2596 cupsd_printer_t
*p
) /* I - Printer */
2598 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2601 * Closing the socket deregisters the service
2604 if (p
->dnssd_ipp_ref
)
2606 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2607 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2608 p
->dnssd_ipp_ref
= NULL
;
2609 p
->dnssd_ipp_fd
= -1;
2612 cupsdClearString(&p
->reg_name
);
2617 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2620 free(p
->txt_record
);
2621 p
->txt_record
= NULL
;
2627 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2628 * TXT record format.
2631 static char * /* O - TXT record */
2632 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2633 char *keyvalue
[][2], /* I - Table of key value pairs */
2634 int count
) /* I - Items in table */
2636 int i
; /* Looping var */
2637 int length
; /* Length of TXT record */
2638 int length2
; /* Length of value */
2639 char *txtRecord
; /* TXT record buffer */
2640 char *cursor
; /* Looping pointer */
2644 * Calculate the buffer size
2647 for (length
= i
= 0; i
< count
; i
++)
2648 length
+= 1 + strlen(keyvalue
[i
][0]) +
2649 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2652 * Allocate and fill it
2655 txtRecord
= malloc(length
);
2660 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2663 * Drop in the p-string style length byte followed by the data
2666 length
= strlen(keyvalue
[i
][0]);
2667 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2669 *cursor
++ = (unsigned char)(length
+ length2
);
2671 memcpy(cursor
, keyvalue
[i
][0], length
);
2678 memcpy(cursor
, keyvalue
[i
][1], length2
);
2689 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2693 dnssdRegisterCallback(
2694 DNSServiceRef sdRef
, /* I - DNS Service reference */
2695 DNSServiceFlags flags
, /* I - Reserved for future use */
2696 DNSServiceErrorType errorCode
, /* I - Error code */
2697 const char *name
, /* I - Service name */
2698 const char *regtype
, /* I - Service type */
2699 const char *domain
, /* I - Domain. ".local" for now */
2700 void *context
) /* I - User-defined context */
2704 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2705 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2709 cupsdLogMessage(CUPSD_LOG_ERROR
,
2710 "DNSServiceRegister failed with error %d", (int)errorCode
);
2717 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2718 * or update the broadcast contents.
2722 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2724 DNSServiceErrorType se
; /* dnssd errors */
2725 cupsd_listener_t
*lis
; /* Current listening socket */
2726 char *txt_record
, /* TXT record buffer */
2727 *name
; /* Service name */
2728 int txt_len
, /* TXT record length */
2729 port
; /* IPP port number */
2730 char str_buffer
[1024];
2731 /* C-string buffer */
2732 const char *computerName
; /* Computer name c-string ptr */
2733 const char *regtype
; /* Registration type */
2734 #ifdef HAVE_COREFOUNDATION_H
2735 CFStringRef computerNameRef
;/* Computer name CFString */
2736 CFStringEncoding nameEncoding
; /* Computer name encoding */
2737 CFMutableStringRef shortNameRef
; /* Mutable name string */
2738 CFIndex nameLength
; /* Name string length */
2740 int nameLength
; /* Name string length */
2741 #endif /* HAVE_COREFOUNDATION_H */
2744 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2745 !p
->dnssd_ipp_ref
? "new" : "update");
2748 * If per-printer sharing was just disabled make sure we're not
2749 * registered before returning.
2754 dnssdDeregisterPrinter(p
);
2759 * Get the computer name as a c-string...
2762 #ifdef HAVE_COREFOUNDATION_H
2763 computerName
= NULL
;
2764 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2765 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2766 kCFStringEncodingUTF8
)) == NULL
)
2767 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2768 kCFStringEncodingUTF8
))
2769 computerName
= str_buffer
;
2771 computerName
= ServerName
;
2772 #endif /* HAVE_COREFOUNDATION_H */
2775 * The registered name takes the form of "<printer-info> @ <computer name>"...
2780 cupsdSetStringf(&name
, "%s @ %s",
2781 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2784 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2786 #ifdef HAVE_COREFOUNDATION_H
2787 if (computerNameRef
)
2788 CFRelease(computerNameRef
);
2789 #endif /* HAVE_COREFOUNDATION_H */
2792 * If an existing printer was renamed, unregister it and start over...
2795 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2796 dnssdDeregisterPrinter(p
);
2798 txt_len
= 0; /* anti-compiler-warning-code */
2799 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2801 if (!p
->dnssd_ipp_ref
)
2804 * Initial registration...
2807 cupsdSetString(&p
->reg_name
, name
);
2811 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2813 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2815 if (lis
->address
.addr
.sa_family
== AF_INET
)
2817 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2820 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2822 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2828 * Use the _fax subtype for fax queues...
2831 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2834 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) type is \"%s\"",
2837 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2838 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2839 dnssdRegisterCallback
, p
);
2842 * In case the name is too long, try shortening the string one character
2846 if (se
== kDNSServiceErr_BadParam
)
2848 #ifdef HAVE_COREFOUNDATION_H
2849 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2851 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2852 nameLength
= CFStringGetLength(shortNameRef
);
2854 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2856 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2857 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2858 kCFStringEncodingUTF8
))
2860 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2861 regtype
, NULL
, NULL
, htons(port
),
2862 txt_len
, txt_record
,
2863 dnssdRegisterCallback
, p
);
2867 CFRelease(shortNameRef
);
2870 nameLength
= strlen(name
);
2871 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2873 name
[--nameLength
] = '\0';
2874 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2875 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2876 dnssdRegisterCallback
, p
);
2878 #endif /* HAVE_COREFOUNDATION_H */
2881 if (se
== kDNSServiceErr_NoError
)
2883 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2884 p
->txt_record
= txt_record
;
2885 p
->txt_len
= txt_len
;
2888 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2892 cupsdLogMessage(CUPSD_LOG_WARN
,
2893 "DNS-SD registration of \"%s\" failed with %d",
2896 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2899 * Update the existing registration...
2902 /* A TTL of 0 means use record's original value (Radar 3176248) */
2903 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2904 txt_len
, txt_record
, 0);
2907 free(p
->txt_record
);
2909 p
->txt_record
= txt_record
;
2910 p
->txt_len
= txt_len
;
2917 cupsdClearString(&name
);
2919 #endif /* HAVE_DNSSD */
2923 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2927 process_implicit_classes(void)
2929 int i
; /* Looping var */
2930 int update
; /* Update printer attributes? */
2931 char name
[IPP_MAX_NAME
], /* Name of printer */
2932 *hptr
; /* Pointer into hostname */
2933 cupsd_printer_t
*p
, /* Printer information */
2934 *pclass
, /* Printer class */
2935 *first
; /* First printer in class */
2936 int offset
, /* Offset of name */
2937 len
; /* Length of name */
2940 if (!ImplicitClasses
|| !Printers
)
2944 * Loop through all available printers and create classes as needed...
2947 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2948 update
= 0, pclass
= NULL
, first
= NULL
;
2950 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2953 * Skip implicit classes...
2956 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2963 * If len == 0, get the length of this printer name up to the "@"
2967 cupsArraySave(Printers
);
2970 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2971 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2974 * We have more than one printer with the same name; see if
2975 * we have a class, and if this printer is a member...
2978 if (pclass
&& strcasecmp(pclass
->name
, name
))
2981 cupsdSetPrinterAttrs(pclass
);
2987 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2990 * Need to add the class...
2993 pclass
= cupsdAddPrinter(name
);
2994 cupsArrayAdd(ImplicitPrinters
, pclass
);
2996 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2997 pclass
->accepting
= 1;
2998 pclass
->state
= IPP_PRINTER_IDLE
;
3000 cupsdSetString(&pclass
->location
, p
->location
);
3001 cupsdSetString(&pclass
->info
, p
->info
);
3003 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3004 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3008 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3010 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3011 "Implicit class \'%s\' added by directory services.",
3017 for (i
= 0; i
< pclass
->num_printers
; i
++)
3018 if (pclass
->printers
[i
] == first
)
3021 if (i
>= pclass
->num_printers
)
3023 first
->in_implicit_class
= 1;
3024 cupsdAddPrinterToClass(pclass
, first
);
3030 for (i
= 0; i
< pclass
->num_printers
; i
++)
3031 if (pclass
->printers
[i
] == p
)
3034 if (i
>= pclass
->num_printers
)
3036 p
->in_implicit_class
= 1;
3037 cupsdAddPrinterToClass(pclass
, p
);
3044 * First time around; just get name length and mark it as first
3048 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3049 len
= hptr
- p
->name
;
3051 len
= strlen(p
->name
);
3053 strncpy(name
, p
->name
, len
);
3057 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3058 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3061 * Can't use same name as a local printer; add "Any" to the
3062 * front of the name, unless we have explicitly disabled
3063 * the "ImplicitAnyClasses"...
3066 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3069 * Add "Any" to the class name...
3072 strcpy(name
, "Any");
3073 strncpy(name
+ 3, p
->name
, len
);
3074 name
[len
+ 3] = '\0';
3080 * Don't create an implicit class if we have a local printer
3081 * with the same name...
3085 cupsArrayRestore(Printers
);
3093 cupsArrayRestore(Printers
);
3097 * Update the last printer class as needed...
3100 if (pclass
&& update
)
3101 cupsdSetPrinterAttrs(pclass
);
3106 * 'send_cups_browse()' - Send new browsing information using the CUPS
3111 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3113 int i
; /* Looping var */
3114 cups_ptype_t type
; /* Printer type */
3115 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3116 int bytes
; /* Length of packet */
3117 char packet
[1453], /* Browse data packet */
3118 uri
[1024], /* Printer URI */
3119 location
[1024], /* printer-location */
3120 info
[1024], /* printer-info */
3122 /* printer-make-and-model */
3123 cupsd_netif_t
*iface
; /* Network interface */
3127 * Figure out the printer type value...
3130 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3133 type
|= CUPS_PRINTER_REJECTING
;
3135 if (p
== DefaultPrinter
)
3136 type
|= CUPS_PRINTER_DEFAULT
;
3139 * Remove quotes from printer-info, printer-location, and
3140 * printer-make-and-model attributes...
3143 dequote(location
, p
->location
, sizeof(location
));
3144 dequote(info
, p
->info
, sizeof(info
));
3147 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3148 else if (p
->type
& CUPS_PRINTER_CLASS
)
3150 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3151 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3153 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3156 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3158 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3161 * Send a packet to each browse address...
3164 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3168 * Send the browse packet to one or more interfaces...
3171 if (!strcmp(b
->iface
, "*"))
3174 * Send to all local interfaces...
3179 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3181 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3184 * Only send to local, IPv4 interfaces...
3187 if (!iface
->is_local
|| !iface
->port
||
3188 iface
->address
.addr
.sa_family
!= AF_INET
)
3191 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3192 iface
->hostname
, iface
->port
,
3193 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3196 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3197 type
, p
->state
, uri
, location
, info
, make_model
,
3198 p
->browse_attrs
? p
->browse_attrs
: "");
3200 bytes
= strlen(packet
);
3202 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3203 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3204 iface
->name
, packet
);
3206 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3208 sendto(BrowseSocket
, packet
, bytes
, 0,
3209 (struct sockaddr
*)&(iface
->broadcast
),
3210 httpAddrLength(&(iface
->broadcast
)));
3213 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3216 * Send to the named interface using the IPv4 address...
3220 if (strcmp(b
->iface
, iface
->name
))
3225 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3228 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3232 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3233 iface
->hostname
, iface
->port
,
3234 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
3237 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3238 type
, p
->state
, uri
, location
, info
, make_model
,
3239 p
->browse_attrs
? p
->browse_attrs
: "");
3241 bytes
= strlen(packet
);
3243 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3244 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3245 iface
->name
, packet
);
3247 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3249 sendto(BrowseSocket
, packet
, bytes
, 0,
3250 (struct sockaddr
*)&(iface
->broadcast
),
3251 httpAddrLength(&(iface
->broadcast
)));
3258 * Send the browse packet to the indicated address using
3259 * the default server name...
3262 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3263 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3264 p
->browse_attrs
? p
->browse_attrs
: "");
3266 bytes
= strlen(packet
);
3267 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3268 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3270 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3271 (struct sockaddr
*)&(b
->to
),
3272 httpAddrLength(&(b
->to
))) <= 0)
3275 * Unable to send browse packet, so remove this address from the
3279 cupsdLogMessage(CUPSD_LOG_ERROR
,
3280 "cupsdSendBrowseList: sendto failed for browser "
3282 (int)(b
- Browsers
+ 1), strerror(errno
));
3285 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3294 #ifdef HAVE_OPENLDAP
3296 * 'send_ldap_browse()' - Send LDAP printer registrations.
3300 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3302 int i
; /* Looping var... */
3303 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3304 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3305 LDAPMessage
*res
; /* Search result token */
3306 char *cn_value
[2], /* Change records */
3312 typestring
[255], /* String to hold printer-type */
3313 filter
[256], /* Search filter for possible UPDATEs */
3314 dn
[1024]; /* DN of the printer we are adding */
3315 int rc
; /* LDAP status */
3316 static const char * const objectClass_values
[] =
3317 { /* The 3 objectClass's we use in */
3318 "top", /* our LDAP entries */
3324 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3327 * Everything in ldap is ** so we fudge around it...
3330 sprintf(typestring
, "%u", p
->type
);
3332 cn_value
[0] = p
->name
;
3334 info
[0] = p
->info
? p
->info
: "Unknown";
3336 location
[0] = p
->location
? p
->location
: "Unknown";
3338 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3339 make_model
[1] = NULL
;
3340 type
[0] = typestring
;
3345 snprintf(filter
, sizeof(filter
),
3346 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3348 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3349 filter
, (char **)ldap_attrs
, 0, &res
);
3350 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3353 mods
[0].mod_type
= "cn";
3354 mods
[0].mod_values
= cn_value
;
3355 mods
[1].mod_type
= "printerDescription";
3356 mods
[1].mod_values
= info
;
3357 mods
[2].mod_type
= "printerURI";
3358 mods
[2].mod_values
= uri
;
3359 mods
[3].mod_type
= "printerLocation";
3360 mods
[3].mod_values
= location
;
3361 mods
[4].mod_type
= "printerMakeAndModel";
3362 mods
[4].mod_values
= make_model
;
3363 mods
[5].mod_type
= "printerType";
3364 mods
[5].mod_values
= type
;
3365 mods
[6].mod_type
= "objectClass";
3366 mods
[6].mod_values
= (char **)objectClass_values
;
3368 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3369 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3371 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3374 * Printer has already been registered, modify the current
3378 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3379 "send_ldap_browse: Replacing entry...");
3381 for (i
= 0; i
< 7; i
++)
3383 pmods
[i
] = mods
+ i
;
3384 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3388 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3389 cupsdLogMessage(CUPSD_LOG_ERROR
,
3390 "LDAP modify for %s failed with status %d: %s",
3391 p
->name
, rc
, ldap_err2string(rc
));
3396 * Printer has never been registered, add the current
3400 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3401 "send_ldap_browse: Adding entry...");
3403 for (i
= 0; i
< 7; i
++)
3405 pmods
[i
] = mods
+ i
;
3406 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3410 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3411 cupsdLogMessage(CUPSD_LOG_ERROR
,
3412 "LDAP add for %s failed with status %d: %s",
3413 p
->name
, rc
, ldap_err2string(rc
));
3416 #endif /* HAVE_OPENLDAP */
3421 * 'send_slp_browse()' - Register the specified printer with SLP.
3425 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3427 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3428 attrs
[8192], /* Printer attributes */
3429 finishings
[1024], /* Finishings to support */
3430 make_model
[IPP_MAX_NAME
* 2],
3431 /* Make and model, quoted */
3432 location
[IPP_MAX_NAME
* 2],
3433 /* Location, quoted */
3434 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3435 *src
, /* Pointer to original string */
3436 *dst
; /* Pointer to destination string */
3437 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3438 SLPError error
; /* SLP error, if any */
3441 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3445 * Make the SLP service URL that conforms to the IANA
3446 * 'printer:' template.
3449 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3451 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3454 * Figure out the finishings string...
3457 if (p
->type
& CUPS_PRINTER_STAPLE
)
3458 strcpy(finishings
, "staple");
3460 finishings
[0] = '\0';
3462 if (p
->type
& CUPS_PRINTER_BIND
)
3465 strlcat(finishings
, ",bind", sizeof(finishings
));
3467 strcpy(finishings
, "bind");
3470 if (p
->type
& CUPS_PRINTER_PUNCH
)
3473 strlcat(finishings
, ",punch", sizeof(finishings
));
3475 strcpy(finishings
, "punch");
3478 if (p
->type
& CUPS_PRINTER_COVER
)
3481 strlcat(finishings
, ",cover", sizeof(finishings
));
3483 strcpy(finishings
, "cover");
3486 if (p
->type
& CUPS_PRINTER_SORT
)
3489 strlcat(finishings
, ",sort", sizeof(finishings
));
3491 strcpy(finishings
, "sort");
3495 strcpy(finishings
, "none");
3498 * Quote any commas in the make and model, location, and info strings...
3501 for (src
= p
->make_model
, dst
= make_model
;
3502 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3504 if (*src
== ',' || *src
== '\\' || *src
== ')')
3513 strcpy(make_model
, "Unknown");
3515 for (src
= p
->location
, dst
= location
;
3516 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3518 if (*src
== ',' || *src
== '\\' || *src
== ')')
3527 strcpy(location
, "Unknown");
3529 for (src
= p
->info
, dst
= info
;
3530 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3532 if (*src
== ',' || *src
== '\\' || *src
== ')')
3541 strcpy(info
, "Unknown");
3544 * Get the authentication value...
3547 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3551 * Make the SLP attribute string list that conforms to
3552 * the IANA 'printer:' template.
3555 snprintf(attrs
, sizeof(attrs
),
3556 "(printer-uri-supported=%s),"
3557 "(uri-authentication-supported=%s>),"
3559 "(uri-security-supported=tls>),"
3561 "(uri-security-supported=none>),"
3562 #endif /* HAVE_SSL */
3563 "(printer-name=%s),"
3564 "(printer-location=%s),"
3565 "(printer-info=%s),"
3566 "(printer-more-info=%s),"
3567 "(printer-make-and-model=%s),"
3568 "(printer-type=%d),"
3569 "(charset-supported=utf-8),"
3570 "(natural-language-configured=%s),"
3571 "(natural-language-supported=de,en,es,fr,it),"
3572 "(color-supported=%s),"
3573 "(finishings-supported=%s),"
3574 "(sides-supported=one-sided%s),"
3575 "(multiple-document-jobs-supported=true)"
3576 "(ipp-versions-supported=1.0,1.1)",
3577 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3578 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3579 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3581 p
->type
& CUPS_PRINTER_DUPLEX
?
3582 ",two-sided-long-edge,two-sided-short-edge" : "");
3584 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3587 * Register the printer with the SLP server...
3590 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3591 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3593 if (error
!= SLP_OK
)
3594 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3600 * 'slp_attr_callback()' - SLP attribute callback
3603 static SLPBoolean
/* O - SLP_TRUE for success */
3605 SLPHandle hslp
, /* I - SLP handle */
3606 const char *attrlist
, /* I - Attribute list */
3607 SLPError errcode
, /* I - Parsing status for this attr */
3608 void *cookie
) /* I - Current printer */
3610 char *tmp
= 0; /* Temporary string */
3611 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3612 /* Current printer */
3615 (void)hslp
; /* anti-compiler-warning-code */
3618 * Bail if there was an error
3621 if (errcode
!= SLP_OK
)
3625 * Parse the attrlist to obtain things needed to build CUPS browse packet
3628 memset(p
, 0, sizeof(cupsd_printer_t
));
3630 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3632 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3634 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3636 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3637 p
->type
= atoi(tmp
);
3639 p
->type
= CUPS_PRINTER_REMOTE
;
3641 cupsdClearString(&tmp
);
3648 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3652 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3654 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3657 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3659 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3662 * Make the SLP service URL that conforms to the IANA
3663 * 'printer:' template.
3666 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3669 * Deregister the printer...
3672 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3678 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3681 static int /* O - 0 on success */
3682 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3683 const char *tag
, /* I - Name of attribute */
3684 char **valbuf
) /* O - Value */
3686 char *ptr1
, /* Pointer into string */
3690 cupsdClearString(valbuf
);
3692 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3694 ptr1
+= strlen(tag
);
3696 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3702 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3703 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3706 * Dequote the value...
3709 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3710 if (*ptr1
== '\\' && ptr1
[1])
3711 _cups_strcpy(ptr1
, ptr1
+ 1);
3722 * 'slp_reg_callback()' - Empty SLPRegReport.
3726 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3727 SLPError errcode
, /* I - Error code, if any */
3728 void *cookie
) /* I - App data */
3739 * 'slp_url_callback()' - SLP service url callback
3742 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3744 SLPHandle hslp
, /* I - SLP handle */
3745 const char *srvurl
, /* I - URL of service */
3746 unsigned short lifetime
, /* I - Life of service */
3747 SLPError errcode
, /* I - Existing error code */
3748 void *cookie
) /* I - Pointer to service list */
3750 slpsrvurl_t
*s
, /* New service entry */
3751 **head
; /* Pointer to head of entry */
3755 * Let the compiler know we won't be using these vars...
3762 * Bail if there was an error
3765 if (errcode
!= SLP_OK
)
3769 * Grab the head of the list...
3772 head
= (slpsrvurl_t
**)cookie
;
3775 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3778 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3782 * Copy the SLP service URL...
3785 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3788 * Link the SLP service URL into the head of the list
3798 #endif /* HAVE_LIBSLP */
3802 * End of "$Id: dirsvc.c 6354 2007-03-19 06:16:32Z mike $".