2 * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * cupsdStartBrowsing() - Start sending and receiving broadcast
28 * cupsdStartPolling() - Start polling servers as needed.
29 * cupsdStopBrowsing() - Stop sending and receiving broadcast
31 * cupsdStopPolling() - Stop polling servers as needed.
32 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
33 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
34 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
35 * dequote() - Remote quotes from a string.
36 * is_local_queue() - Determine whether the URI points at a local
38 * process_browse_data() - Process new browse data.
39 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
40 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
42 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
44 * dnssdRegisterCallback() - DNSServiceRegister callback.
45 * dnssdRegisterPrinter() - Start sending broadcast information for a
46 * printer or update the broadcast contents.
47 * process_implicit_classes() - Create/update implicit classes as needed.
48 * send_cups_browse() - Send new browsing information using the CUPS
50 * send_ldap_browse() - Send LDAP printer registrations.
51 * send_slp_browse() - Register the specified printer with SLP.
52 * slp_attr_callback() - SLP attribute callback
53 * slp_dereg_printer() - SLPDereg() the specified printer
54 * slp_get_attr() - Get an attribute from an SLP registration.
55 * slp_reg_callback() - Empty SLPRegReport.
56 * slp_url_callback() - SLP service url callback
57 * update_cups_browse() - Update the browse lists using the CUPS
59 * update_lpd() - Update the LPD configuration as needed.
60 * update_polling() - Read status messages from the poll daemons.
61 * update_smb() - Update the SMB configuration as needed.
65 * Include necessary headers...
75 # ifdef HAVE_COREFOUNDATION
76 # include <CoreFoundation/CoreFoundation.h>
77 # endif /* HAVE_COREFOUNDATION */
78 # ifdef HAVE_SYSTEMCONFIGURATION
79 # include <SystemConfiguration/SystemConfiguration.h>
80 # endif /* HAVE_SYSTEMCONFIGURATION */
81 # endif /* __APPLE__ */
82 #endif /* HAVE_DNSSD */
89 static char *dequote(char *d
, const char *s
, int dlen
);
90 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
91 char *resource
, int resourcelen
);
92 static void process_browse_data(const char *uri
, const char *host
,
93 const char *resource
, cups_ptype_t type
,
94 ipp_pstate_t state
, const char *location
,
95 const char *info
, const char *make_model
,
96 int num_attrs
, cups_option_t
*attrs
);
97 static void process_implicit_classes(int *write_printcap
);
98 static void send_cups_browse(cupsd_printer_t
*p
);
100 static void send_ldap_browse(cupsd_printer_t
*p
);
101 #endif /* HAVE_LDAP */
103 static void send_slp_browse(cupsd_printer_t
*p
);
104 #endif /* HAVE_LIBSLP */
105 static void update_cups_browse(void);
106 static void update_lpd(int onoff
);
107 static void update_polling(void);
108 static void update_smb(int onoff
);
112 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
114 "printerDescription",
116 "printerMakeAndModel",
121 #endif /* HAVE_OPENLDAP */
129 * SLP service name for CUPS...
132 # define SLP_CUPS_SRVTYPE "service:printer"
133 # define SLP_CUPS_SRVLEN 15
137 * Printer service URL structure
140 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
142 struct _slpsrvurl_s
*next
; /* Next URL in list */
143 char url
[HTTP_MAX_URI
];
152 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
153 SLPError errcode
, void *cookie
);
154 static void slp_dereg_printer(cupsd_printer_t
*p
);
155 static int slp_get_attr(const char *attrlist
, const char *tag
,
157 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
159 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
160 unsigned short lifetime
,
161 SLPError errcode
, void *cookie
);
162 #endif /* HAVE_LIBSLP */
166 * For IPP register using a subtype of 'cups' so that shared printer browsing
167 * only finds other CUPS servers (not all IPP based printers).
169 static char dnssdIPPRegType
[] = "_ipp._tcp,_cups";
170 static char dnssdIPPFaxRegType
[] = "_fax-ipp._tcp";
172 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
);
173 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
174 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
176 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
177 DNSServiceFlags flags
,
178 DNSServiceErrorType errorCode
,
179 const char *name
, const char *regtype
,
180 const char *domain
, void *context
);
181 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
182 #endif /* HAVE_DNSSD */
186 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
187 * local printer and remove any pending
188 * references to remote printers.
192 cupsdDeregisterPrinter(
193 cupsd_printer_t
*p
, /* I - Printer to register */
194 int removeit
) /* I - Printer being permanently removed */
197 * Only deregister if browsing is enabled and it's a local printers...
200 if (!Browsing
|| !p
->shared
||
201 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
205 * Announce the deletion...
208 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
210 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
212 p
->type
|= CUPS_PRINTER_DELETE
;
220 if (BrowseLocalProtocols
& BROWSE_SLP
)
221 slp_dereg_printer(p
);
222 #endif /* HAVE_LIBSLP */
225 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
))
226 dnssdDeregisterPrinter(p
);
227 #endif /* HAVE_DNSSD */
232 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
236 cupsdLoadRemoteCache(void)
238 cups_file_t
*fp
; /* remote.cache file */
239 int linenum
; /* Current line number */
240 char line
[1024], /* Line from file */
241 *value
, /* Pointer to value */
242 *valueptr
, /* Pointer into value */
243 scheme
[32], /* Scheme portion of URI */
244 username
[64], /* Username portion of URI */
246 /* Hostname portion of URI */
247 resource
[HTTP_MAX_URI
];
248 /* Resource portion of URI */
249 int port
; /* Port number */
250 cupsd_printer_t
*p
; /* Current printer */
251 time_t now
; /* Current time */
255 * Don't load the cache if the CUPS remote protocol is disabled...
258 if (!Browsing
|| !(BrowseRemoteProtocols
& BROWSE_CUPS
))
260 cupsdLogMessage(CUPSD_LOG_DEBUG
,
261 "cupsdLoadRemoteCache: Not loading remote cache.");
266 * Open the remote.cache file...
269 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
270 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
274 * Read printer configurations until we hit EOF...
281 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
284 * Decode the directive...
287 if (!strcasecmp(line
, "<Printer") ||
288 !strcasecmp(line
, "<DefaultPrinter"))
291 * <Printer name> or <DefaultPrinter name>
294 if (p
== NULL
&& value
)
297 * Add the printer and a base file type...
300 cupsdLogMessage(CUPSD_LOG_DEBUG
,
301 "cupsdLoadRemoteCache: Loading printer %s...", value
);
303 if ((p
= cupsdFindDest(value
)) != NULL
)
305 if (p
->type
& CUPS_PRINTER_CLASS
)
307 cupsdLogMessage(CUPSD_LOG_WARN
,
308 "Cached remote printer \"%s\" conflicts with "
316 p
= cupsdAddPrinter(value
);
319 p
->state
= IPP_PRINTER_IDLE
;
320 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
321 p
->browse_time
= now
;
322 p
->browse_expire
= now
+ BrowseTimeout
;
325 * Set the default printer as needed...
328 if (!strcasecmp(line
, "<DefaultPrinter"))
333 cupsdLogMessage(CUPSD_LOG_ERROR
,
334 "Syntax error on line %d of remote.cache.", linenum
);
338 else if (!strcasecmp(line
, "<Class") ||
339 !strcasecmp(line
, "<DefaultClass"))
342 * <Class name> or <DefaultClass name>
345 if (p
== NULL
&& value
)
348 * Add the printer and a base file type...
351 cupsdLogMessage(CUPSD_LOG_DEBUG
,
352 "cupsdLoadRemoteCache: Loading class %s...", value
);
354 if ((p
= cupsdFindDest(value
)) != NULL
)
355 p
->type
= CUPS_PRINTER_CLASS
;
357 p
= cupsdAddClass(value
);
360 p
->state
= IPP_PRINTER_IDLE
;
361 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
362 p
->browse_time
= now
;
363 p
->browse_expire
= now
+ BrowseTimeout
;
366 * Set the default printer as needed...
369 if (!strcasecmp(line
, "<DefaultClass"))
374 cupsdLogMessage(CUPSD_LOG_ERROR
,
375 "Syntax error on line %d of remote.cache.", linenum
);
379 else if (!strcasecmp(line
, "</Printer>") ||
380 !strcasecmp(line
, "</Class>"))
385 * Close out the current printer...
388 cupsdSetPrinterAttrs(p
);
394 cupsdLogMessage(CUPSD_LOG_ERROR
,
395 "Syntax error on line %d of remote.cache.", linenum
);
401 cupsdLogMessage(CUPSD_LOG_ERROR
,
402 "Syntax error on line %d of remote.cache.", linenum
);
405 else if (!strcasecmp(line
, "Info"))
408 cupsdSetString(&p
->info
, value
);
410 else if (!strcasecmp(line
, "MakeModel"))
413 cupsdSetString(&p
->make_model
, value
);
415 else if (!strcasecmp(line
, "Location"))
418 cupsdSetString(&p
->location
, value
);
420 else if (!strcasecmp(line
, "DeviceURI"))
424 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
425 username
, sizeof(username
), host
, sizeof(host
), &port
,
426 resource
, sizeof(resource
));
428 cupsdSetString(&p
->hostname
, host
);
429 cupsdSetString(&p
->uri
, value
);
430 cupsdSetString(&p
->device_uri
, value
);
434 cupsdLogMessage(CUPSD_LOG_ERROR
,
435 "Syntax error on line %d of remote.cache.", linenum
);
439 else if (!strcasecmp(line
, "Option") && value
)
445 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
448 cupsdLogMessage(CUPSD_LOG_ERROR
,
449 "Syntax error on line %d of remote.cache.", linenum
);
452 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
454 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
458 else if (!strcasecmp(line
, "State"))
461 * Set the initial queue state...
464 if (value
&& !strcasecmp(value
, "idle"))
465 p
->state
= IPP_PRINTER_IDLE
;
466 else if (value
&& !strcasecmp(value
, "stopped"))
467 p
->state
= IPP_PRINTER_STOPPED
;
470 cupsdLogMessage(CUPSD_LOG_ERROR
,
471 "Syntax error on line %d of remote.cache.", linenum
);
475 else if (!strcasecmp(line
, "StateMessage"))
478 * Set the initial queue state message...
482 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
484 else if (!strcasecmp(line
, "Accepting"))
487 * Set the initial accepting state...
491 (!strcasecmp(value
, "yes") ||
492 !strcasecmp(value
, "on") ||
493 !strcasecmp(value
, "true")))
496 (!strcasecmp(value
, "no") ||
497 !strcasecmp(value
, "off") ||
498 !strcasecmp(value
, "false")))
502 cupsdLogMessage(CUPSD_LOG_ERROR
,
503 "Syntax error on line %d of remote.cache.", linenum
);
507 else if (!strcasecmp(line
, "Type"))
510 p
->type
= atoi(value
);
513 cupsdLogMessage(CUPSD_LOG_ERROR
,
514 "Syntax error on line %d of remote.cache.", linenum
);
518 else if (!strcasecmp(line
, "BrowseTime"))
522 time_t t
= atoi(value
);
524 if (t
> p
->browse_expire
)
525 p
->browse_expire
= t
;
529 cupsdLogMessage(CUPSD_LOG_ERROR
,
530 "Syntax error on line %d of remote.cache.", linenum
);
534 else if (!strcasecmp(line
, "JobSheets"))
537 * Set the initial job sheets...
542 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
547 cupsdSetString(&p
->job_sheets
[0], value
);
549 while (isspace(*valueptr
& 255))
554 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
559 cupsdSetString(&p
->job_sheets
[1], value
);
564 cupsdLogMessage(CUPSD_LOG_ERROR
,
565 "Syntax error on line %d of remote.cache.", linenum
);
569 else if (!strcasecmp(line
, "AllowUser"))
574 cupsdAddPrinterUser(p
, value
);
578 cupsdLogMessage(CUPSD_LOG_ERROR
,
579 "Syntax error on line %d of remote.cache.", linenum
);
583 else if (!strcasecmp(line
, "DenyUser"))
588 cupsdAddPrinterUser(p
, value
);
592 cupsdLogMessage(CUPSD_LOG_ERROR
,
593 "Syntax error on line %d of remote.cache.", linenum
);
600 * Something else we don't understand...
603 cupsdLogMessage(CUPSD_LOG_ERROR
,
604 "Unknown configuration directive %s on line %d of remote.cache.",
612 * Do auto-classing if needed...
615 process_implicit_classes(NULL
);
620 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
621 * printer or update the broadcast contents.
625 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
627 if (!Browsing
|| !BrowseLocalProtocols
|| !BrowseInterval
|| !NumBrowsers
||
628 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
632 /* if (BrowseLocalProtocols & BROWSE_SLP)
633 slpRegisterPrinter(p); */
634 #endif /* HAVE_LIBSLP */
637 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
638 dnssdRegisterPrinter(p
);
639 #endif /* HAVE_DNSSD */
644 * 'cupsdRestartPolling()' - Restart polling servers as needed.
648 cupsdRestartPolling(void)
650 int i
; /* Looping var */
651 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
654 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
656 kill(pollp
->pid
, SIGHUP
);
661 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
665 cupsdSaveRemoteCache(void)
667 int i
; /* Looping var */
668 cups_file_t
*fp
; /* printers.conf file */
669 char temp
[1024]; /* Temporary string */
670 cupsd_printer_t
*printer
; /* Current printer class */
671 time_t curtime
; /* Current time */
672 struct tm
*curdate
; /* Current date */
673 cups_option_t
*option
; /* Current option */
677 * Create the remote.cache file...
680 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
682 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
684 cupsdLogMessage(CUPSD_LOG_ERROR
,
685 "Unable to save remote.cache - %s", strerror(errno
));
689 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
692 * Restrict access to the file...
695 fchown(cupsFileNumber(fp
), getuid(), Group
);
696 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
699 * Write a small header to the file...
702 curtime
= time(NULL
);
703 curdate
= localtime(&curtime
);
704 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
706 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
707 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
710 * Write each local printer known to the system...
713 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
715 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
718 * Skip local destinations...
721 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
725 * Write printers as needed...
728 if (printer
== DefaultPrinter
)
729 cupsFilePuts(fp
, "<Default");
731 cupsFilePutChar(fp
, '<');
733 if (printer
->type
& CUPS_PRINTER_CLASS
)
734 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
736 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
738 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
740 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
743 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
745 if (printer
->make_model
)
746 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
748 if (printer
->location
)
749 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
751 if (printer
->device_uri
)
752 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
754 if (printer
->state
== IPP_PRINTER_STOPPED
)
756 cupsFilePuts(fp
, "State Stopped\n");
757 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
760 cupsFilePuts(fp
, "State Idle\n");
762 if (printer
->accepting
)
763 cupsFilePuts(fp
, "Accepting Yes\n");
765 cupsFilePuts(fp
, "Accepting No\n");
767 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
768 printer
->job_sheets
[1]);
770 for (i
= 0; i
< printer
->num_users
; i
++)
771 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
774 for (i
= printer
->num_options
, option
= printer
->options
;
777 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
779 if (printer
->type
& CUPS_PRINTER_CLASS
)
780 cupsFilePuts(fp
, "</Class>\n");
782 cupsFilePuts(fp
, "</Printer>\n");
790 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
794 cupsdSendBrowseList(void)
796 int count
; /* Number of dests to update */
797 cupsd_printer_t
*p
; /* Current printer */
798 time_t ut
, /* Minimum update time */
799 to
; /* Timeout time */
800 int write_printcap
; /* Write the printcap file? */
803 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
807 * Compute the update and timeout times...
811 ut
= to
- BrowseInterval
;
814 * Figure out how many printers need an update...
817 if (BrowseInterval
> 0)
819 int max_count
; /* Maximum number to update */
823 * Throttle the number of printers we'll be updating this time
824 * around based on the number of queues that need updating and
825 * the maximum number of queues to update each second...
828 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
830 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
831 count
< max_count
&& p
!= NULL
;
832 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
833 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
834 p
->shared
&& p
->browse_time
< ut
)
838 * Loop through all of the printers and send local updates as needed...
842 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
844 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
848 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
851 * Check for wraparound...
855 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
859 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
862 else if (p
->browse_time
< ut
)
865 * Need to send an update...
870 p
->browse_time
= time(NULL
);
872 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
876 if (BrowseLocalProtocols
& BROWSE_SLP
)
878 #endif /* HAVE_LIBSLP */
881 if (BrowseLocalProtocols
& BROWSE_LDAP
)
883 #endif /* HAVE_LDAP */
888 * Save where we left off so that all printers get updated...
895 * Loop through all of the printers and send local updates as needed...
898 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), write_printcap
= 0;
900 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
903 * If this is a remote queue, see if it needs to be timed out...
906 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
908 if (p
->browse_expire
< to
)
910 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
911 "%s \'%s\' deleted by directory services (timeout).",
912 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
915 cupsdLogMessage(CUPSD_LOG_DEBUG
,
916 "Remote destination \"%s\" has timed out; "
920 cupsArraySave(Printers
);
921 cupsdDeletePrinter(p
, 1);
922 cupsArrayRestore(Printers
);
929 cupsdWritePrintcap();
934 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
938 cupsdStartBrowsing(void)
940 int val
; /* Socket option value */
941 struct sockaddr_in addr
; /* Broadcast address */
942 cupsd_printer_t
*p
; /* Current printer */
947 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
950 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
952 if (BrowseSocket
< 0)
955 * Create the broadcast socket...
958 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
960 cupsdLogMessage(CUPSD_LOG_ERROR
,
961 "Unable to create broadcast socket - %s.",
963 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
964 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
969 * Bind the socket to browse port...
972 memset(&addr
, 0, sizeof(addr
));
973 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
974 addr
.sin_family
= AF_INET
;
975 addr
.sin_port
= htons(BrowsePort
);
977 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
979 cupsdLogMessage(CUPSD_LOG_ERROR
,
980 "Unable to bind broadcast socket - %s.",
984 closesocket(BrowseSocket
);
990 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
991 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
997 * Set the "broadcast" flag...
1001 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1003 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1007 closesocket(BrowseSocket
);
1009 close(BrowseSocket
);
1013 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1014 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1019 * Close the socket on exec...
1022 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1025 * Finally, add the socket to the input selection set as needed...
1028 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1031 * We only listen if we want remote printers...
1034 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1042 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1045 * Open SLP handle...
1048 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1050 cupsdLogMessage(CUPSD_LOG_ERROR
,
1051 "Unable to open an SLP handle; disabling SLP browsing!");
1052 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1053 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1056 BrowseSLPRefresh
= 0;
1059 BrowseSLPHandle
= NULL
;
1060 #endif /* HAVE_LIBSLP */
1062 #ifdef HAVE_OPENLDAP
1063 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1067 cupsdLogMessage(CUPSD_LOG_ERROR
,
1068 "Need to set BrowseLDAPDN to use LDAP browsing!");
1069 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1070 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1075 * Open LDAP handle...
1078 int rc
; /* LDAP API status */
1079 int version
= 3; /* LDAP version */
1080 struct berval bv
= {0, ""}; /* SASL bind value */
1084 * Set the certificate file to use for encrypted LDAP sessions...
1087 if (BrowseLDAPCACertFile
)
1089 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1090 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1091 BrowseLDAPCACertFile
);
1093 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1094 (void *)BrowseLDAPCACertFile
))
1096 cupsdLogMessage(CUPSD_LOG_ERROR
,
1097 "Unable to set CA certificate file for LDAP "
1098 "connections: %d - %s", rc
, ldap_err2string(rc
));
1102 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1105 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1106 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1108 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1110 if (rc
!= LDAP_SUCCESS
)
1112 cupsdLogMessage(CUPSD_LOG_ERROR
,
1113 "Unable to initialize LDAP; disabling LDAP browsing!");
1114 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1115 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1117 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1118 (const void *)&version
) != LDAP_SUCCESS
)
1120 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1121 BrowseLDAPHandle
= NULL
;
1122 cupsdLogMessage(CUPSD_LOG_ERROR
,
1123 "Unable to set LDAP protocol version; "
1124 "disabling LDAP browsing!");
1125 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1126 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1130 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1131 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1134 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1135 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1137 if (rc
!= LDAP_SUCCESS
)
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "Unable to bind to LDAP server; "
1141 "disabling LDAP browsing!");
1142 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1143 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1144 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1149 BrowseLDAPRefresh
= 0;
1151 #endif /* HAVE_OPENLDAP */
1154 * Enable LPD and SMB printer sharing as needed through external programs...
1157 if (BrowseLocalProtocols
& BROWSE_LPD
)
1160 if (BrowseLocalProtocols
& BROWSE_SMB
)
1164 * Register the individual printers
1167 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1169 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1170 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1171 cupsdRegisterPrinter(p
);
1176 * 'cupsdStartPolling()' - Start polling servers as needed.
1180 cupsdStartPolling(void)
1182 int i
; /* Looping var */
1183 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1184 char polld
[1024]; /* Poll daemon path */
1185 char sport
[255]; /* Server port */
1186 char bport
[255]; /* Browser port */
1187 char interval
[255]; /* Poll interval */
1188 int statusfds
[2]; /* Status pipe */
1189 char *argv
[6]; /* Arguments */
1190 char *envp
[100]; /* Environment */
1194 * Don't do anything if we aren't polling...
1197 if (NumPolled
== 0 || BrowseSocket
< 0)
1200 PollStatusBuffer
= NULL
;
1205 * Setup string arguments for polld, port and interval options.
1208 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1210 sprintf(bport
, "%d", BrowsePort
);
1213 sprintf(interval
, "%d", BrowseInterval
);
1215 strcpy(interval
, "30");
1217 argv
[0] = "cups-polld";
1223 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1226 * Create a pipe that receives the status messages from each
1230 if (cupsdOpenPipe(statusfds
))
1232 cupsdLogMessage(CUPSD_LOG_ERROR
,
1233 "Unable to create polling status pipes - %s.",
1236 PollStatusBuffer
= NULL
;
1240 PollPipe
= statusfds
[0];
1241 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1244 * Run each polling daemon, redirecting stderr to the polling pipe...
1247 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1249 sprintf(sport
, "%d", pollp
->port
);
1251 argv
[1] = pollp
->hostname
;
1253 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1254 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1256 cupsdLogMessage(CUPSD_LOG_ERROR
,
1257 "cupsdStartPolling: Unable to fork polling daemon - %s",
1263 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1264 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1265 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1268 close(statusfds
[1]);
1271 * Finally, add the pipe to the input selection set...
1274 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1279 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1283 cupsdStopBrowsing(void)
1285 cupsd_printer_t
*p
; /* Current printer */
1288 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1292 * De-register the individual printers
1295 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1297 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1298 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1299 cupsdDeregisterPrinter(p
, 1);
1302 * Shut down browsing sockets...
1305 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1309 * Close the socket and remove it from the input selection set.
1313 closesocket(BrowseSocket
);
1315 close(BrowseSocket
);
1318 cupsdRemoveSelect(BrowseSocket
);
1323 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1327 * Close SLP handle...
1330 SLPClose(BrowseSLPHandle
);
1331 BrowseSLPHandle
= NULL
;
1333 #endif /* HAVE_LIBSLP */
1335 #ifdef HAVE_OPENLDAP
1336 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1339 ldap_unbind(BrowseLDAPHandle
);
1340 BrowseLDAPHandle
= NULL
;
1342 #endif /* HAVE_OPENLDAP */
1345 * Disable LPD and SMB printer sharing as needed through external programs...
1348 if (BrowseLocalProtocols
& BROWSE_LPD
)
1351 if (BrowseLocalProtocols
& BROWSE_SMB
)
1357 * 'cupsdStopPolling()' - Stop polling servers as needed.
1361 cupsdStopPolling(void)
1363 int i
; /* Looping var */
1364 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1369 cupsdStatBufDelete(PollStatusBuffer
);
1372 cupsdRemoveSelect(PollPipe
);
1375 PollStatusBuffer
= NULL
;
1378 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1380 cupsdEndProcess(pollp
->pid
, 0);
1386 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1390 cupsdUpdateDNSSDBrowse(
1391 cupsd_printer_t
*p
) /* I - Printer being queried */
1393 DNSServiceErrorType sdErr
; /* Service discovery error */
1396 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1397 != kDNSServiceErr_NoError
)
1399 cupsdLogMessage(CUPSD_LOG_ERROR
,
1400 "DNS Service Discovery registration error %d for \"%s\"!",
1402 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1403 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1405 p
->dnssd_ipp_ref
= NULL
;
1406 p
->dnssd_ipp_fd
= -1;
1409 #endif /* HAVE_DNSSD */
1412 #ifdef HAVE_OPENLDAP
1414 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1418 cupsdUpdateLDAPBrowse(void)
1420 char uri
[HTTP_MAX_URI
], /* Printer URI */
1421 host
[HTTP_MAX_URI
], /* Hostname */
1422 resource
[HTTP_MAX_URI
], /* Resource path */
1423 location
[1024], /* Printer location */
1424 info
[1024], /* Printer information */
1425 make_model
[1024], /* Printer make and model */
1426 **value
; /* Holds the returned data from LDAP */
1427 int type
; /* Printer type */
1428 int rc
; /* LDAP status */
1429 int limit
; /* Size limit */
1430 LDAPMessage
*res
, /* LDAP search results */
1431 *e
; /* Current entry from search */
1435 * Search for printers...
1438 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1440 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1442 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1443 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1444 if (rc
!= LDAP_SUCCESS
)
1446 cupsdLogMessage(CUPSD_LOG_ERROR
,
1447 "LDAP search returned error %d: %s", rc
,
1448 ldap_err2string(rc
));
1452 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1453 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1458 * Loop through the available printers...
1461 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1463 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1466 * Get the required values from this entry...
1469 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1470 "printerDescription")) == NULL
)
1473 strlcpy(info
, *value
, sizeof(info
));
1474 ldap_value_free(value
);
1476 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1477 "printerLocation")) == NULL
)
1480 strlcpy(location
, *value
, sizeof(location
));
1481 ldap_value_free(value
);
1483 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1484 "printerMakeAndModel")) == NULL
)
1487 strlcpy(make_model
, *value
, sizeof(make_model
));
1488 ldap_value_free(value
);
1490 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1491 "printerType")) == NULL
)
1494 type
= atoi(*value
);
1495 ldap_value_free(value
);
1497 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1498 "printerURI")) == NULL
)
1501 strlcpy(uri
, *value
, sizeof(uri
));
1502 ldap_value_free(value
);
1505 * Process the entry as browse data...
1508 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1509 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1510 location
, info
, make_model
, 0, NULL
);
1514 #endif /* HAVE_OPENLDAP */
1519 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1523 cupsdUpdateSLPBrowse(void)
1525 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1526 *next
; /* Next service in list */
1527 cupsd_printer_t p
; /* Printer information */
1528 const char *uri
; /* Pointer to printer URI */
1529 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1530 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1534 * Reset the refresh time...
1537 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1540 * Poll for remote printers using SLP...
1545 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1546 slp_url_callback
, &s
);
1549 * Loop through the list of available printers...
1555 * Save the "next" pointer...
1561 * Load a cupsd_printer_t structure with the SLP service attributes...
1564 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1567 * Process this printer entry...
1570 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1572 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1575 * Pull the URI apart to see if this is a local or remote printer...
1578 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1579 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1580 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1584 * Free this listing...
1587 cupsdClearString(&p
.info
);
1588 cupsdClearString(&p
.location
);
1589 cupsdClearString(&p
.make_model
);
1594 #endif /* HAVE_LIBSLP */
1598 * 'dequote()' - Remote quotes from a string.
1601 static char * /* O - Dequoted string */
1602 dequote(char *d
, /* I - Destination string */
1603 const char *s
, /* I - Source string */
1604 int dlen
) /* I - Destination length */
1606 char *dptr
; /* Pointer into destination */
1611 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1628 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1631 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1632 is_local_queue(const char *uri
, /* I - Printer URI */
1633 char *host
, /* O - Host string */
1634 int hostlen
, /* I - Length of host buffer */
1635 char *resource
, /* O - Resource string */
1636 int resourcelen
) /* I - Length of resource buffer */
1638 char scheme
[32], /* Scheme portion of URI */
1639 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1640 int port
; /* Port portion of URI */
1641 cupsd_netif_t
*iface
; /* Network interface */
1645 * Pull the URI apart to see if this is a local or remote printer...
1648 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1649 username
, sizeof(username
), host
, hostlen
, &port
,
1650 resource
, resourcelen
) < HTTP_URI_OK
)
1653 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1656 * Check for local server addresses...
1659 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1664 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1666 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1667 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1671 * If we get here, the printer is remote...
1679 * 'process_browse_data()' - Process new browse data.
1683 process_browse_data(
1684 const char *uri
, /* I - URI of printer/class */
1685 const char *host
, /* I - Hostname */
1686 const char *resource
, /* I - Resource path */
1687 cups_ptype_t type
, /* I - Printer type */
1688 ipp_pstate_t state
, /* I - Printer state */
1689 const char *location
, /* I - Printer location */
1690 const char *info
, /* I - Printer information */
1691 const char *make_model
, /* I - Printer make and model */
1692 int num_attrs
, /* I - Number of attributes */
1693 cups_option_t
*attrs
) /* I - Attributes */
1695 int i
; /* Looping var */
1696 int update
, /* Update printer attributes? */
1697 write_printcap
; /* Write the printcap file? */
1698 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1699 name
[IPP_MAX_NAME
], /* Name of printer */
1700 newname
[IPP_MAX_NAME
], /* New name of printer */
1701 *hptr
, /* Pointer into hostname */
1702 *sptr
; /* Pointer into ServerName */
1703 char local_make_model
[IPP_MAX_NAME
];
1704 /* Local make and model */
1705 cupsd_printer_t
*p
; /* Printer information */
1706 const char *ipp_options
, /* ipp-options value */
1707 *lease_duration
; /* lease-duration value */
1711 * Determine if the URI contains any illegal characters in it...
1714 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1715 (strncmp(resource
, "/printers/", 10) &&
1716 strncmp(resource
, "/classes/", 9)))
1718 cupsdLogMessage(CUPSD_LOG_ERROR
,
1719 "process_browse_data: Bad printer URI in browse data: %s",
1724 if (strchr(resource
, '?') ||
1725 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1726 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1728 cupsdLogMessage(CUPSD_LOG_ERROR
,
1729 "process_browse_data: Bad resource in browse data: %s",
1735 * OK, this isn't a local printer; add any remote options...
1738 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1740 if (BrowseRemoteOptions
)
1742 if (BrowseRemoteOptions
[0] == '?')
1745 * Override server-supplied options...
1748 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1750 else if (ipp_options
)
1753 * Combine the server and local options...
1756 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1757 BrowseRemoteOptions
);
1762 * Just use the local options...
1765 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1770 else if (ipp_options
)
1773 * Just use the server-supplied options...
1776 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1781 * See if we already have it listed in the Printers list, and add it if not...
1784 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
1785 type
&= ~CUPS_PRINTER_IMPLICIT
;
1788 hptr
= strchr(host
, '.');
1789 sptr
= strchr(ServerName
, '.');
1791 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1794 * Strip the common domain name components...
1797 while (hptr
!= NULL
)
1799 if (!strcasecmp(hptr
, sptr
))
1805 hptr
= strchr(hptr
+ 1, '.');
1809 if (type
& CUPS_PRINTER_CLASS
)
1812 * Remote destination is a class...
1815 if (!strncmp(resource
, "/classes/", 9))
1816 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1821 *hptr
= '.'; /* Resource FQDN */
1823 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
1825 if ((p
= cupsdFindDest(resource
+ 9)) != NULL
)
1827 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1830 * Nope, this isn't the same host; if the hostname isn't the local host,
1831 * add it to the other class and then find a class using the full host
1835 if (p
->type
& CUPS_PRINTER_REMOTE
)
1837 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1838 "Renamed remote class \"%s\" to \"%s@%s\"...",
1839 p
->name
, p
->name
, p
->hostname
);
1840 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1841 "Class \'%s\' deleted by directory services.",
1844 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1845 cupsdRenamePrinter(p
, newname
);
1847 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1848 "Class \'%s\' added by directory services.",
1854 else if (!p
->hostname
)
1857 * Hostname not set, so this must be a cached remote printer
1858 * that was created for a pending print job...
1861 cupsdSetString(&p
->hostname
, host
);
1862 cupsdSetString(&p
->uri
, uri
);
1863 cupsdSetString(&p
->device_uri
, uri
);
1870 * Use the short name for this shared class.
1873 strlcpy(name
, resource
+ 9, sizeof(name
));
1876 else if (p
&& !p
->hostname
)
1879 * Hostname not set, so this must be a cached remote printer
1880 * that was created for a pending print job...
1883 cupsdSetString(&p
->hostname
, host
);
1884 cupsdSetString(&p
->uri
, uri
);
1885 cupsdSetString(&p
->device_uri
, uri
);
1892 * Class doesn't exist; add it...
1895 p
= cupsdAddClass(name
);
1897 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1899 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1900 "Class \'%s\' added by directory services.", name
);
1903 * Force the URI to point to the real server...
1906 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1908 cupsdSetString(&p
->uri
, uri
);
1909 cupsdSetString(&p
->device_uri
, uri
);
1910 cupsdSetString(&p
->hostname
, host
);
1919 * Remote destination is a printer...
1922 if (!strncmp(resource
, "/printers/", 10))
1923 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1928 *hptr
= '.'; /* Resource FQDN */
1930 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
1932 if ((p
= cupsdFindDest(resource
+ 10)) != NULL
)
1934 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1937 * Nope, this isn't the same host; if the hostname isn't the local host,
1938 * add it to the other printer and then find a printer using the full host
1942 if (p
->type
& CUPS_PRINTER_REMOTE
)
1944 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1945 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1946 p
->name
, p
->name
, p
->hostname
);
1947 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1948 "Printer \'%s\' deleted by directory services.",
1951 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1952 cupsdRenamePrinter(p
, newname
);
1954 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1955 "Printer \'%s\' added by directory services.",
1961 else if (!p
->hostname
)
1964 * Hostname not set, so this must be a cached remote printer
1965 * that was created for a pending print job...
1968 cupsdSetString(&p
->hostname
, host
);
1969 cupsdSetString(&p
->uri
, uri
);
1970 cupsdSetString(&p
->device_uri
, uri
);
1977 * Use the short name for this shared printer.
1980 strlcpy(name
, resource
+ 10, sizeof(name
));
1983 else if (p
&& !p
->hostname
)
1986 * Hostname not set, so this must be a cached remote printer
1987 * that was created for a pending print job...
1990 cupsdSetString(&p
->hostname
, host
);
1991 cupsdSetString(&p
->uri
, uri
);
1992 cupsdSetString(&p
->device_uri
, uri
);
1999 * Printer doesn't exist; add it...
2002 p
= cupsdAddPrinter(name
);
2004 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2005 "Printer \'%s\' added by directory services.", name
);
2007 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2010 * Force the URI to point to the real server...
2013 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2015 cupsdSetString(&p
->hostname
, host
);
2016 cupsdSetString(&p
->uri
, uri
);
2017 cupsdSetString(&p
->device_uri
, uri
);
2025 * Update the state...
2029 p
->browse_time
= time(NULL
);
2031 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2035 * Grab the lease-duration for the browse data; anything less then 1
2036 * second or more than 1 week gets the default BrowseTimeout...
2039 i
= atoi(lease_duration
);
2040 if (i
< 1 || i
> 604800)
2043 p
->browse_expire
= p
->browse_time
+ i
;
2046 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2048 if (type
& CUPS_PRINTER_REJECTING
)
2050 type
&= ~CUPS_PRINTER_REJECTING
;
2058 else if (!p
->accepting
)
2064 if (p
->type
!= type
)
2070 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2072 cupsdSetString(&p
->location
, location
);
2076 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2078 cupsdSetString(&p
->info
, info
);
2083 if (!make_model
|| !make_model
[0])
2085 if (type
& CUPS_PRINTER_CLASS
)
2086 snprintf(local_make_model
, sizeof(local_make_model
),
2087 "Remote Class on %s", host
);
2089 snprintf(local_make_model
, sizeof(local_make_model
),
2090 "Remote Printer on %s", host
);
2093 snprintf(local_make_model
, sizeof(local_make_model
),
2094 "%s on %s", make_model
, host
);
2096 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2098 cupsdSetString(&p
->make_model
, local_make_model
);
2104 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2107 * See if we need to update the attributes...
2110 if (p
->num_options
!= num_attrs
)
2114 for (i
= 0; i
< num_attrs
; i
++)
2115 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2116 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2117 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2126 * Free the old options...
2129 cupsFreeOptions(p
->num_options
, p
->options
);
2132 p
->num_options
= num_attrs
;
2135 if (type
& CUPS_PRINTER_DELETE
)
2137 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2138 "%s \'%s\' deleted by directory services.",
2139 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2141 cupsdExpireSubscriptions(p
, NULL
);
2143 cupsdDeletePrinter(p
, 1);
2144 cupsdUpdateImplicitClasses();
2149 cupsdSetPrinterAttrs(p
);
2150 cupsdUpdateImplicitClasses();
2154 * See if we have a default printer... If not, make the first network
2155 * default printer the default.
2158 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2161 * Find the first network default printer and use it...
2164 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2166 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2167 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2176 * Do auto-classing if needed...
2179 process_implicit_classes(&write_printcap
);
2182 * Update the printcap file...
2186 cupsdWritePrintcap();
2192 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2195 static char * /* O - TXT record */
2196 dnssdBuildTxtRecord(
2197 int *txt_len
, /* O - TXT record length */
2198 cupsd_printer_t
*p
) /* I - Printer information */
2200 int i
, j
; /* Looping vars */
2201 char type_str
[32], /* Type to string buffer */
2202 state_str
[32], /* State to string buffer */
2203 rp_str
[1024], /* Queue name string buffer */
2204 air_str
[1024], /* auth-info-required string buffer */
2205 *keyvalue
[32][2]; /* Table of key/value pairs */
2209 * Load up the key value pairs...
2214 keyvalue
[i
][0] = "txtvers";
2215 keyvalue
[i
++][1] = "1";
2217 keyvalue
[i
][0] = "qtotal";
2218 keyvalue
[i
++][1] = "1";
2220 keyvalue
[i
][0] = "rp";
2221 keyvalue
[i
++][1] = rp_str
;
2222 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2223 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2225 keyvalue
[i
][0] = "ty";
2226 keyvalue
[i
++][1] = p
->make_model
;
2228 if (p
->location
&& *p
->location
!= '\0')
2230 keyvalue
[i
][0] = "note";
2231 keyvalue
[i
++][1] = p
->location
;
2234 keyvalue
[i
][0] = "product";
2235 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2237 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2238 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2240 keyvalue
[i
][0] = "printer-state";
2241 keyvalue
[i
++][1] = state_str
;
2243 keyvalue
[i
][0] = "printer-type";
2244 keyvalue
[i
++][1] = type_str
;
2246 keyvalue
[i
][0] = "Transparent";
2247 keyvalue
[i
++][1] = "T";
2249 keyvalue
[i
][0] = "Binary";
2250 keyvalue
[i
++][1] = "T";
2252 if ((p
->type
& CUPS_PRINTER_FAX
))
2254 keyvalue
[i
][0] = "Fax";
2255 keyvalue
[i
++][1] = "T";
2258 if ((p
->type
& CUPS_PRINTER_COLOR
))
2260 keyvalue
[i
][0] = "Color";
2261 keyvalue
[i
++][1] = "T";
2264 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2266 keyvalue
[i
][0] = "Duplex";
2267 keyvalue
[i
++][1] = "T";
2270 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2272 keyvalue
[i
][0] = "Staple";
2273 keyvalue
[i
++][1] = "T";
2276 if ((p
->type
& CUPS_PRINTER_COPIES
))
2278 keyvalue
[i
][0] = "Copies";
2279 keyvalue
[i
++][1] = "T";
2282 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2284 keyvalue
[i
][0] = "Collate";
2285 keyvalue
[i
++][1] = "T";
2288 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2290 keyvalue
[i
][0] = "Punch";
2291 keyvalue
[i
++][1] = "T";
2294 if ((p
->type
& CUPS_PRINTER_BIND
))
2296 keyvalue
[i
][0] = "Bind";
2297 keyvalue
[i
++][1] = "T";
2300 if ((p
->type
& CUPS_PRINTER_SORT
))
2302 keyvalue
[i
][0] = "Sort";
2303 keyvalue
[i
++][1] = "T";
2306 keyvalue
[i
][0] = "pdl";
2307 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2309 if (p
->num_auth_info_required
)
2311 char *air
= air_str
; /* Pointer into string */
2314 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2316 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2322 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2326 keyvalue
[i
][0] = "air";
2327 keyvalue
[i
++][1] = air
;
2331 * Then pack them into a proper txt record...
2334 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2339 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2344 dnssdDeregisterPrinter(
2345 cupsd_printer_t
*p
) /* I - Printer */
2347 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2350 * Closing the socket deregisters the service
2353 if (p
->dnssd_ipp_ref
)
2355 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2356 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2357 p
->dnssd_ipp_ref
= NULL
;
2358 p
->dnssd_ipp_fd
= -1;
2361 cupsdClearString(&p
->reg_name
);
2366 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2369 free(p
->txt_record
);
2370 p
->txt_record
= NULL
;
2376 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2377 * TXT record format.
2380 static char * /* O - TXT record */
2381 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2382 char *keyvalue
[][2], /* I - Table of key value pairs */
2383 int count
) /* I - Items in table */
2385 int i
; /* Looping var */
2386 int length
; /* Length of TXT record */
2387 int length2
; /* Length of value */
2388 char *txtRecord
; /* TXT record buffer */
2389 char *cursor
; /* Looping pointer */
2393 * Calculate the buffer size
2396 for (length
= i
= 0; i
< count
; i
++)
2397 length
+= 1 + strlen(keyvalue
[i
][0]) +
2398 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2401 * Allocate and fill it
2404 txtRecord
= malloc(length
);
2409 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2412 * Drop in the p-string style length byte followed by the data
2415 length
= strlen(keyvalue
[i
][0]);
2416 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2418 *cursor
++ = (unsigned char)(length
+ length2
);
2420 memcpy(cursor
, keyvalue
[i
][0], length
);
2427 memcpy(cursor
, keyvalue
[i
][1], length2
);
2438 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2442 dnssdRegisterCallback(
2443 DNSServiceRef sdRef
, /* I - DNS Service reference */
2444 DNSServiceFlags flags
, /* I - Reserved for future use */
2445 DNSServiceErrorType errorCode
, /* I - Error code */
2446 const char *name
, /* I - Service name */
2447 const char *regtype
, /* I - Service type */
2448 const char *domain
, /* I - Domain. ".local" for now */
2449 void *context
) /* I - User-defined context */
2453 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2454 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2458 cupsdLogMessage(CUPSD_LOG_ERROR
,
2459 "DNSServiceRegister failed with error %d", (int)errorCode
);
2466 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2467 * or update the broadcast contents.
2471 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2473 DNSServiceErrorType se
; /* dnssd errors */
2474 cupsd_listener_t
*lis
; /* Current listening socket */
2475 char *txt_record
, /* TXT record buffer */
2476 *name
; /* Service name */
2477 int txt_len
, /* TXT record length */
2478 port
; /* IPP port number */
2479 char resource
[1024], /* Resource path for printer */
2481 /* C-string buffer */
2482 const char *computerName
; /* Computer name c-string ptr */
2483 const char *regtype
; /* Registration type */
2484 const char *domain
; /* Registration domain */
2485 cupsd_location_t
*location
, /* Printer location */
2486 *policy
; /* Operation policy for Print-Job */
2487 unsigned address
[4]; /* INADDR_ANY address */
2488 #ifdef HAVE_COREFOUNDATION_H
2489 CFStringRef computerNameRef
;/* Computer name CFString */
2490 CFStringEncoding nameEncoding
; /* Computer name encoding */
2491 CFMutableStringRef shortNameRef
; /* Mutable name string */
2492 CFIndex nameLength
; /* Name string length */
2494 int nameLength
; /* Name string length */
2495 #endif /* HAVE_COREFOUNDATION_H */
2498 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2499 !p
->dnssd_ipp_ref
? "new" : "update");
2502 * If per-printer sharing was just disabled make sure we're not
2503 * registered before returning.
2508 dnssdDeregisterPrinter(p
);
2513 * Get the computer name as a c-string...
2516 #ifdef HAVE_COREFOUNDATION_H
2517 computerName
= NULL
;
2518 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2519 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2520 kCFStringEncodingUTF8
)) == NULL
)
2521 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2522 kCFStringEncodingUTF8
))
2523 computerName
= str_buffer
;
2525 computerName
= ServerName
;
2526 #endif /* HAVE_COREFOUNDATION_H */
2529 * The registered name takes the form of "<printer-info> @ <computer name>"...
2534 cupsdSetStringf(&name
, "%s @ %s",
2535 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2538 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2540 #ifdef HAVE_COREFOUNDATION_H
2541 if (computerNameRef
)
2542 CFRelease(computerNameRef
);
2543 #endif /* HAVE_COREFOUNDATION_H */
2546 * If an existing printer was renamed, unregister it and start over...
2549 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2550 dnssdDeregisterPrinter(p
);
2552 txt_len
= 0; /* anti-compiler-warning-code */
2553 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2555 if (!p
->dnssd_ipp_ref
)
2558 * Initial registration...
2561 cupsdSetString(&p
->reg_name
, name
);
2565 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2567 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2569 if (lis
->address
.addr
.sa_family
== AF_INET
)
2571 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2574 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2576 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2582 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2583 * let dnssd decide on the domain, otherwise restrict it to ".local".
2586 if (p
->type
& CUPS_PRINTER_CLASS
)
2587 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2589 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2591 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2592 location
= cupsdFindBest(resource
, HTTP_POST
);
2593 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2595 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2596 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2602 * Use the _fax subtype for fax queues...
2605 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2609 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2610 "dnssdRegisterPrinter(%s) type, domain is \"%s\", \"%s\"",
2611 p
->name
, regtype
, domain
? domain
: "(null)");
2613 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2614 domain
, NULL
, htons(port
), txt_len
, txt_record
,
2615 dnssdRegisterCallback
, p
);
2618 * In case the name is too long, try shortening the string one character
2622 if (se
== kDNSServiceErr_BadParam
)
2624 #ifdef HAVE_COREFOUNDATION_H
2625 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2627 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2628 nameLength
= CFStringGetLength(shortNameRef
);
2630 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2632 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2633 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2634 kCFStringEncodingUTF8
))
2636 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2637 regtype
, NULL
, NULL
, htons(port
),
2638 txt_len
, txt_record
,
2639 dnssdRegisterCallback
, p
);
2643 CFRelease(shortNameRef
);
2646 nameLength
= strlen(name
);
2647 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2649 name
[--nameLength
] = '\0';
2650 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2651 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2652 dnssdRegisterCallback
, p
);
2654 #endif /* HAVE_COREFOUNDATION_H */
2657 if (se
== kDNSServiceErr_NoError
)
2659 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2660 p
->txt_record
= txt_record
;
2661 p
->txt_len
= txt_len
;
2664 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2668 cupsdLogMessage(CUPSD_LOG_WARN
,
2669 "DNS-SD registration of \"%s\" failed with %d",
2672 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2675 * Update the existing registration...
2678 /* A TTL of 0 means use record's original value (Radar 3176248) */
2679 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2680 txt_len
, txt_record
, 0);
2683 free(p
->txt_record
);
2685 p
->txt_record
= txt_record
;
2686 p
->txt_len
= txt_len
;
2693 cupsdClearString(&name
);
2695 #endif /* HAVE_DNSSD */
2699 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2703 process_implicit_classes(
2704 int *write_printcap
) /* O - Write printcap file? */
2706 int i
; /* Looping var */
2707 int update
; /* Update printer attributes? */
2708 char name
[IPP_MAX_NAME
], /* Name of printer */
2709 *hptr
; /* Pointer into hostname */
2710 cupsd_printer_t
*p
, /* Printer information */
2711 *pclass
, /* Printer class */
2712 *first
; /* First printer in class */
2713 int offset
, /* Offset of name */
2714 len
; /* Length of name */
2717 if (!ImplicitClasses
|| !Printers
)
2721 * Loop through all available printers and create classes as needed...
2724 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2725 update
= 0, pclass
= NULL
, first
= NULL
;
2727 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2730 * Skip implicit classes...
2733 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2740 * If len == 0, get the length of this printer name up to the "@"
2744 cupsArraySave(Printers
);
2747 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2748 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2751 * We have more than one printer with the same name; see if
2752 * we have a class, and if this printer is a member...
2755 if (pclass
&& strcasecmp(pclass
->name
, name
))
2758 cupsdSetPrinterAttrs(pclass
);
2764 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2767 * Need to add the class...
2770 pclass
= cupsdAddPrinter(name
);
2771 cupsArrayAdd(ImplicitPrinters
, pclass
);
2773 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2774 pclass
->accepting
= 1;
2775 pclass
->state
= IPP_PRINTER_IDLE
;
2777 cupsdSetString(&pclass
->location
, p
->location
);
2778 cupsdSetString(&pclass
->info
, p
->info
);
2780 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2781 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2786 *write_printcap
= 1;
2788 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2790 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2791 "Implicit class \'%s\' added by directory services.",
2797 for (i
= 0; i
< pclass
->num_printers
; i
++)
2798 if (pclass
->printers
[i
] == first
)
2801 if (i
>= pclass
->num_printers
)
2803 first
->in_implicit_class
= 1;
2804 cupsdAddPrinterToClass(pclass
, first
);
2810 for (i
= 0; i
< pclass
->num_printers
; i
++)
2811 if (pclass
->printers
[i
] == p
)
2814 if (i
>= pclass
->num_printers
)
2816 p
->in_implicit_class
= 1;
2817 cupsdAddPrinterToClass(pclass
, p
);
2824 * First time around; just get name length and mark it as first
2828 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2829 len
= hptr
- p
->name
;
2831 len
= strlen(p
->name
);
2833 strncpy(name
, p
->name
, len
);
2837 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2838 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2841 * Can't use same name as a local printer; add "Any" to the
2842 * front of the name, unless we have explicitly disabled
2843 * the "ImplicitAnyClasses"...
2846 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2849 * Add "Any" to the class name...
2852 strcpy(name
, "Any");
2853 strncpy(name
+ 3, p
->name
, len
);
2854 name
[len
+ 3] = '\0';
2860 * Don't create an implicit class if we have a local printer
2861 * with the same name...
2865 cupsArrayRestore(Printers
);
2873 cupsArrayRestore(Printers
);
2877 * Update the last printer class as needed...
2880 if (pclass
&& update
)
2881 cupsdSetPrinterAttrs(pclass
);
2886 * 'send_cups_browse()' - Send new browsing information using the CUPS
2891 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2893 int i
; /* Looping var */
2894 cups_ptype_t type
; /* Printer type */
2895 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2896 int bytes
; /* Length of packet */
2897 char packet
[1453], /* Browse data packet */
2898 uri
[1024], /* Printer URI */
2899 location
[1024], /* printer-location */
2900 info
[1024], /* printer-info */
2902 /* printer-make-and-model */
2903 cupsd_netif_t
*iface
; /* Network interface */
2907 * Figure out the printer type value...
2910 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2913 type
|= CUPS_PRINTER_REJECTING
;
2915 if (p
== DefaultPrinter
)
2916 type
|= CUPS_PRINTER_DEFAULT
;
2919 * Remove quotes from printer-info, printer-location, and
2920 * printer-make-and-model attributes...
2923 dequote(location
, p
->location
, sizeof(location
));
2924 dequote(info
, p
->info
, sizeof(info
));
2927 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2928 else if (p
->type
& CUPS_PRINTER_CLASS
)
2930 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2931 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2933 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2936 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2938 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2941 * Send a packet to each browse address...
2944 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2948 * Send the browse packet to one or more interfaces...
2951 if (!strcmp(b
->iface
, "*"))
2954 * Send to all local interfaces...
2959 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2961 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2964 * Only send to local, IPv4 interfaces...
2967 if (!iface
->is_local
|| !iface
->port
||
2968 iface
->address
.addr
.sa_family
!= AF_INET
)
2971 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2972 iface
->hostname
, iface
->port
,
2973 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2976 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2977 type
, p
->state
, uri
, location
, info
, make_model
,
2978 p
->browse_attrs
? p
->browse_attrs
: "");
2980 bytes
= strlen(packet
);
2982 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2983 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2984 iface
->name
, packet
);
2986 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2988 sendto(BrowseSocket
, packet
, bytes
, 0,
2989 (struct sockaddr
*)&(iface
->broadcast
),
2990 httpAddrLength(&(iface
->broadcast
)));
2993 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2996 * Send to the named interface using the IPv4 address...
3000 if (strcmp(b
->iface
, iface
->name
))
3005 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3008 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3012 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3013 iface
->hostname
, iface
->port
,
3014 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3017 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3018 type
, p
->state
, uri
, location
, info
, make_model
,
3019 p
->browse_attrs
? p
->browse_attrs
: "");
3021 bytes
= strlen(packet
);
3023 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3024 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3025 iface
->name
, packet
);
3027 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3029 sendto(BrowseSocket
, packet
, bytes
, 0,
3030 (struct sockaddr
*)&(iface
->broadcast
),
3031 httpAddrLength(&(iface
->broadcast
)));
3038 * Send the browse packet to the indicated address using
3039 * the default server name...
3042 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3043 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3044 p
->browse_attrs
? p
->browse_attrs
: "");
3046 bytes
= strlen(packet
);
3047 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3048 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3050 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3051 (struct sockaddr
*)&(b
->to
),
3052 httpAddrLength(&(b
->to
))) <= 0)
3055 * Unable to send browse packet, so remove this address from the
3059 cupsdLogMessage(CUPSD_LOG_ERROR
,
3060 "cupsdSendBrowseList: sendto failed for browser "
3062 (int)(b
- Browsers
+ 1), strerror(errno
));
3065 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3074 #ifdef HAVE_OPENLDAP
3076 * 'send_ldap_browse()' - Send LDAP printer registrations.
3080 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3082 int i
; /* Looping var... */
3083 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3084 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3085 LDAPMessage
*res
; /* Search result token */
3086 char *cn_value
[2], /* Change records */
3092 typestring
[255], /* String to hold printer-type */
3093 filter
[256], /* Search filter for possible UPDATEs */
3094 dn
[1024]; /* DN of the printer we are adding */
3095 int rc
; /* LDAP status */
3096 static const char * const objectClass_values
[] =
3097 { /* The 3 objectClass's we use in */
3098 "top", /* our LDAP entries */
3104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3107 * Everything in ldap is ** so we fudge around it...
3110 sprintf(typestring
, "%u", p
->type
);
3112 cn_value
[0] = p
->name
;
3114 info
[0] = p
->info
? p
->info
: "Unknown";
3116 location
[0] = p
->location
? p
->location
: "Unknown";
3118 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3119 make_model
[1] = NULL
;
3120 type
[0] = typestring
;
3125 snprintf(filter
, sizeof(filter
),
3126 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3128 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3129 filter
, (char **)ldap_attrs
, 0, &res
);
3130 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3133 mods
[0].mod_type
= "cn";
3134 mods
[0].mod_values
= cn_value
;
3135 mods
[1].mod_type
= "printerDescription";
3136 mods
[1].mod_values
= info
;
3137 mods
[2].mod_type
= "printerURI";
3138 mods
[2].mod_values
= uri
;
3139 mods
[3].mod_type
= "printerLocation";
3140 mods
[3].mod_values
= location
;
3141 mods
[4].mod_type
= "printerMakeAndModel";
3142 mods
[4].mod_values
= make_model
;
3143 mods
[5].mod_type
= "printerType";
3144 mods
[5].mod_values
= type
;
3145 mods
[6].mod_type
= "objectClass";
3146 mods
[6].mod_values
= (char **)objectClass_values
;
3148 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3149 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3151 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3154 * Printer has already been registered, modify the current
3158 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3159 "send_ldap_browse: Replacing entry...");
3161 for (i
= 0; i
< 7; i
++)
3163 pmods
[i
] = mods
+ i
;
3164 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3168 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3169 cupsdLogMessage(CUPSD_LOG_ERROR
,
3170 "LDAP modify for %s failed with status %d: %s",
3171 p
->name
, rc
, ldap_err2string(rc
));
3176 * Printer has never been registered, add the current
3180 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3181 "send_ldap_browse: Adding entry...");
3183 for (i
= 0; i
< 7; i
++)
3185 pmods
[i
] = mods
+ i
;
3186 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3190 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3191 cupsdLogMessage(CUPSD_LOG_ERROR
,
3192 "LDAP add for %s failed with status %d: %s",
3193 p
->name
, rc
, ldap_err2string(rc
));
3196 #endif /* HAVE_OPENLDAP */
3201 * 'send_slp_browse()' - Register the specified printer with SLP.
3205 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3207 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3208 attrs
[8192], /* Printer attributes */
3209 finishings
[1024], /* Finishings to support */
3210 make_model
[IPP_MAX_NAME
* 2],
3211 /* Make and model, quoted */
3212 location
[IPP_MAX_NAME
* 2],
3213 /* Location, quoted */
3214 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3215 *src
, /* Pointer to original string */
3216 *dst
; /* Pointer to destination string */
3217 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3218 SLPError error
; /* SLP error, if any */
3221 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3225 * Make the SLP service URL that conforms to the IANA
3226 * 'printer:' template.
3229 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3231 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3234 * Figure out the finishings string...
3237 if (p
->type
& CUPS_PRINTER_STAPLE
)
3238 strcpy(finishings
, "staple");
3240 finishings
[0] = '\0';
3242 if (p
->type
& CUPS_PRINTER_BIND
)
3245 strlcat(finishings
, ",bind", sizeof(finishings
));
3247 strcpy(finishings
, "bind");
3250 if (p
->type
& CUPS_PRINTER_PUNCH
)
3253 strlcat(finishings
, ",punch", sizeof(finishings
));
3255 strcpy(finishings
, "punch");
3258 if (p
->type
& CUPS_PRINTER_COVER
)
3261 strlcat(finishings
, ",cover", sizeof(finishings
));
3263 strcpy(finishings
, "cover");
3266 if (p
->type
& CUPS_PRINTER_SORT
)
3269 strlcat(finishings
, ",sort", sizeof(finishings
));
3271 strcpy(finishings
, "sort");
3275 strcpy(finishings
, "none");
3278 * Quote any commas in the make and model, location, and info strings...
3281 for (src
= p
->make_model
, dst
= make_model
;
3282 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3284 if (*src
== ',' || *src
== '\\' || *src
== ')')
3293 strcpy(make_model
, "Unknown");
3295 for (src
= p
->location
, dst
= location
;
3296 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3298 if (*src
== ',' || *src
== '\\' || *src
== ')')
3307 strcpy(location
, "Unknown");
3309 for (src
= p
->info
, dst
= info
;
3310 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3312 if (*src
== ',' || *src
== '\\' || *src
== ')')
3321 strcpy(info
, "Unknown");
3324 * Get the authentication value...
3327 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3331 * Make the SLP attribute string list that conforms to
3332 * the IANA 'printer:' template.
3335 snprintf(attrs
, sizeof(attrs
),
3336 "(printer-uri-supported=%s),"
3337 "(uri-authentication-supported=%s>),"
3339 "(uri-security-supported=tls>),"
3341 "(uri-security-supported=none>),"
3342 #endif /* HAVE_SSL */
3343 "(printer-name=%s),"
3344 "(printer-location=%s),"
3345 "(printer-info=%s),"
3346 "(printer-more-info=%s),"
3347 "(printer-make-and-model=%s),"
3348 "(printer-type=%d),"
3349 "(charset-supported=utf-8),"
3350 "(natural-language-configured=%s),"
3351 "(natural-language-supported=de,en,es,fr,it),"
3352 "(color-supported=%s),"
3353 "(finishings-supported=%s),"
3354 "(sides-supported=one-sided%s),"
3355 "(multiple-document-jobs-supported=true)"
3356 "(ipp-versions-supported=1.0,1.1)",
3357 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3358 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3359 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3361 p
->type
& CUPS_PRINTER_DUPLEX
?
3362 ",two-sided-long-edge,two-sided-short-edge" : "");
3364 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3367 * Register the printer with the SLP server...
3370 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3371 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3373 if (error
!= SLP_OK
)
3374 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3380 * 'slp_attr_callback()' - SLP attribute callback
3383 static SLPBoolean
/* O - SLP_TRUE for success */
3385 SLPHandle hslp
, /* I - SLP handle */
3386 const char *attrlist
, /* I - Attribute list */
3387 SLPError errcode
, /* I - Parsing status for this attr */
3388 void *cookie
) /* I - Current printer */
3390 char *tmp
= 0; /* Temporary string */
3391 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3392 /* Current printer */
3395 (void)hslp
; /* anti-compiler-warning-code */
3398 * Bail if there was an error
3401 if (errcode
!= SLP_OK
)
3405 * Parse the attrlist to obtain things needed to build CUPS browse packet
3408 memset(p
, 0, sizeof(cupsd_printer_t
));
3410 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3412 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3414 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3416 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3417 p
->type
= atoi(tmp
);
3419 p
->type
= CUPS_PRINTER_REMOTE
;
3421 cupsdClearString(&tmp
);
3428 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3432 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3434 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3437 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3439 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3442 * Make the SLP service URL that conforms to the IANA
3443 * 'printer:' template.
3446 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3449 * Deregister the printer...
3452 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3458 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3461 static int /* O - 0 on success */
3462 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3463 const char *tag
, /* I - Name of attribute */
3464 char **valbuf
) /* O - Value */
3466 char *ptr1
, /* Pointer into string */
3470 cupsdClearString(valbuf
);
3472 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3474 ptr1
+= strlen(tag
);
3476 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3482 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3483 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3486 * Dequote the value...
3489 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3490 if (*ptr1
== '\\' && ptr1
[1])
3491 _cups_strcpy(ptr1
, ptr1
+ 1);
3502 * 'slp_reg_callback()' - Empty SLPRegReport.
3506 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3507 SLPError errcode
, /* I - Error code, if any */
3508 void *cookie
) /* I - App data */
3519 * 'slp_url_callback()' - SLP service url callback
3522 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3524 SLPHandle hslp
, /* I - SLP handle */
3525 const char *srvurl
, /* I - URL of service */
3526 unsigned short lifetime
, /* I - Life of service */
3527 SLPError errcode
, /* I - Existing error code */
3528 void *cookie
) /* I - Pointer to service list */
3530 slpsrvurl_t
*s
, /* New service entry */
3531 **head
; /* Pointer to head of entry */
3535 * Let the compiler know we won't be using these vars...
3542 * Bail if there was an error
3545 if (errcode
!= SLP_OK
)
3549 * Grab the head of the list...
3552 head
= (slpsrvurl_t
**)cookie
;
3555 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3558 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3562 * Copy the SLP service URL...
3565 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3568 * Link the SLP service URL into the head of the list
3578 #endif /* HAVE_LIBSLP */
3582 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3586 update_cups_browse(void)
3588 int i
; /* Looping var */
3589 int auth
; /* Authorization status */
3590 int len
; /* Length of name string */
3591 int bytes
; /* Number of bytes left */
3592 char packet
[1541], /* Broadcast packet */
3593 *pptr
; /* Pointer into packet */
3594 socklen_t srclen
; /* Length of source address */
3595 http_addr_t srcaddr
; /* Source address */
3596 char srcname
[1024]; /* Source hostname */
3597 unsigned address
[4]; /* Source address */
3598 unsigned type
; /* Printer type */
3599 unsigned state
; /* Printer state */
3600 char uri
[HTTP_MAX_URI
], /* Printer URI */
3601 host
[HTTP_MAX_URI
], /* Host portion of URI */
3602 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3603 info
[IPP_MAX_NAME
], /* Information string */
3604 location
[IPP_MAX_NAME
], /* Location string */
3605 make_model
[IPP_MAX_NAME
];/* Make and model string */
3606 int num_attrs
; /* Number of attributes */
3607 cups_option_t
*attrs
; /* Attributes */
3611 * Read a packet from the browse socket...
3614 srclen
= sizeof(srcaddr
);
3615 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3616 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3619 * "Connection refused" is returned under Linux if the destination port
3620 * or address is unreachable from a previous sendto(); check for the
3621 * error here and ignore it for now...
3624 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3626 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3628 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
3630 cupsdStopBrowsing();
3637 packet
[bytes
] = '\0';
3640 * If we're about to sleep, ignore incoming browse packets.
3647 * Figure out where it came from...
3651 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3653 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3654 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3655 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3656 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3659 #endif /* AF_INET6 */
3664 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3667 if (HostNameLookups
)
3668 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3670 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3672 len
= strlen(srcname
);
3680 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3683 * Access from localhost (127.0.0.1) is always allowed...
3691 * Do authorization checks on the domain/address...
3694 switch (BrowseACL
->order_type
)
3697 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
3700 case AUTH_ALLOW
: /* Order Deny,Allow */
3703 if (cupsdCheckAuth(address
, srcname
, len
,
3704 BrowseACL
->num_deny
, BrowseACL
->deny
))
3707 if (cupsdCheckAuth(address
, srcname
, len
,
3708 BrowseACL
->num_allow
, BrowseACL
->allow
))
3712 case AUTH_DENY
: /* Order Allow,Deny */
3715 if (cupsdCheckAuth(address
, srcname
, len
,
3716 BrowseACL
->num_allow
, BrowseACL
->allow
))
3719 if (cupsdCheckAuth(address
, srcname
, len
,
3720 BrowseACL
->num_deny
, BrowseACL
->deny
))
3729 if (auth
== AUTH_DENY
)
3731 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3732 "update_cups_browse: Refused %d bytes from %s", bytes
,
3737 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3738 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3745 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3747 cupsdLogMessage(CUPSD_LOG_WARN
,
3748 "update_cups_browse: Garbled browse packet - %s", packet
);
3752 strcpy(location
, "Location Unknown");
3753 strcpy(info
, "No Information Available");
3754 make_model
[0] = '\0';
3758 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3761 * Have extended information; can't use sscanf for it because not all
3762 * sscanf's allow empty strings with %[^\"]...
3765 for (i
= 0, pptr
++;
3766 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3768 location
[i
] = *pptr
;
3776 while (*pptr
&& isspace(*pptr
& 255))
3781 for (i
= 0, pptr
++;
3782 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3791 while (*pptr
&& isspace(*pptr
& 255))
3796 for (i
= 0, pptr
++;
3797 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3799 make_model
[i
] = *pptr
;
3804 make_model
[i
] = '\0';
3807 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3813 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3814 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3815 type
, state
, uri
, location
, info
, make_model
));
3818 * Pull the URI apart to see if this is a local or remote printer...
3821 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
3823 cupsFreeOptions(num_attrs
, attrs
);
3831 for (i
= 0; i
< NumRelays
; i
++)
3832 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
3833 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3834 (struct sockaddr
*)&(Relays
[i
].to
),
3835 httpAddrLength(&(Relays
[i
].to
))) <= 0)
3837 cupsdLogMessage(CUPSD_LOG_ERROR
,
3838 "update_cups_browse: sendto failed for relay %d - %s.",
3839 i
+ 1, strerror(errno
));
3840 cupsFreeOptions(num_attrs
, attrs
);
3845 * Process the browse data...
3848 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
3849 (ipp_pstate_t
)state
, location
, info
, make_model
,
3855 * 'update_lpd()' - Update the LPD configuration as needed.
3859 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
3864 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
3867 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
3870 char newfile
[1024]; /* New cups-lpd.N file */
3871 cups_file_t
*ofp
, /* Original file pointer */
3872 *nfp
; /* New file pointer */
3873 char line
[1024]; /* Line from file */
3876 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
3878 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
3880 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
3881 LPDConfigFile
+ 9, strerror(errno
));
3885 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
3887 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
3888 newfile
, strerror(errno
));
3894 * Copy all of the lines from the cups-lpd file...
3897 while (cupsFileGets(ofp
, line
, sizeof(line
)))
3901 cupsFilePrintf(nfp
, "%s\n", line
);
3902 snprintf(line
, sizeof(line
), "\tdisable = %s",
3903 onoff
? "no" : "yes");
3905 else if (strstr(line
, "disable ="))
3908 cupsFilePrintf(nfp
, "%s\n", line
);
3913 rename(newfile
, LPDConfigFile
+ 9);
3915 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
3918 * Enable/disable LPD via the launchctl command...
3921 char *argv
[5], /* Arguments for command */
3922 *envp
[MAX_ENV
]; /* Environment for command */
3923 int pid
; /* Process ID */
3926 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3927 argv
[0] = (char *)"launchctl";
3928 argv
[1] = (char *)(onoff
? "load" : "unload");
3929 argv
[2] = (char *)"-w";
3930 argv
[3] = LPDConfigFile
+ 10;
3933 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
3940 * 'update_polling()' - Read status messages from the poll daemons.
3944 update_polling(void)
3946 char *ptr
, /* Pointer to end of line in buffer */
3947 message
[1024]; /* Pointer to message text */
3948 int loglevel
; /* Log level for message */
3951 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
3952 message
, sizeof(message
))) != NULL
)
3953 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
3956 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
3959 * All polling processes have died; stop polling...
3962 cupsdLogMessage(CUPSD_LOG_ERROR
,
3963 "update_polling: all polling processes have exited!");
3970 * 'update_smb()' - Update the SMB configuration as needed.
3974 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
3979 if (!strncmp(SMBConfigFile
, "samba:///", 9))
3982 * Enable/disable SMB via the specified smb.conf config file...
3985 char newfile
[1024]; /* New smb.conf.N file */
3986 cups_file_t
*ofp
, /* Original file pointer */
3987 *nfp
; /* New file pointer */
3988 char line
[1024]; /* Line from file */
3989 int in_printers
; /* In [printers] section? */
3992 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
3994 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
3996 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
3997 SMBConfigFile
+ 8, strerror(errno
));
4001 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4003 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4004 newfile
, strerror(errno
));
4010 * Copy all of the lines from the smb.conf file...
4015 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4017 if (in_printers
&& strstr(line
, "printable ="))
4018 snprintf(line
, sizeof(line
), " printable = %s",
4019 onoff
? "yes" : "no");
4021 cupsFilePrintf(nfp
, "%s\n", line
);
4024 in_printers
= !strcmp(line
, "[printers]");
4029 rename(newfile
, SMBConfigFile
+ 8);
4031 else if (!strncmp(SMBConfigFile
, "launchd:///", 11))
4034 * Enable/disable SMB via the launchctl command...
4037 char *argv
[5], /* Arguments for command */
4038 *envp
[MAX_ENV
]; /* Environment for command */
4039 int pid
; /* Process ID */
4042 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4043 argv
[0] = (char *)"launchctl";
4044 argv
[1] = (char *)(onoff
? "load" : "unload");
4045 argv
[2] = (char *)"-w";
4046 argv
[3] = SMBConfigFile
+ 10;
4049 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
4056 * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".