2 * "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z 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 * ldap_rebind_proc() - Callback function for LDAP rebind
27 * ldap_connect() - Start new LDAP connection
28 * ldap_reconnect() - Reconnect to LDAP Server
29 * ldap_disconnect() - Disconnect from LDAP Server
30 * cupsdStartBrowsing() - Start sending and receiving broadcast
32 * cupsdStartPolling() - Start polling servers as needed.
33 * cupsdStopBrowsing() - Stop sending and receiving broadcast
35 * cupsdStopPolling() - Stop polling servers as needed.
36 * cupsdUpdateDNSSDName() - Update the computer name we use for
38 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
39 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
40 * dequote() - Remote quotes from a string.
41 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
42 * dnssdComparePrinters() - Compare the registered names of two printers.
43 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
45 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
47 * dnssdRegisterCallback() - DNSServiceRegister callback.
48 * dnssdRegisterPrinter() - Start sending broadcast information for a
49 * printer or update the broadcast contents.
50 * dnssdUpdate() - Handle DNS-SD queries.
51 * get_hostconfig() - Get an /etc/hostconfig service setting.
52 * is_local_queue() - Determine whether the URI points at a local
54 * process_browse_data() - Process new browse data.
55 * process_implicit_classes() - Create/update implicit classes as needed.
56 * send_cups_browse() - Send new browsing information using the CUPS
58 * ldap_search_rec() - LDAP Search with reconnect
59 * ldap_freeres() - Free LDAPMessage
60 * ldap_getval_char() - Get first LDAP value and convert to string
61 * send_ldap_ou() - Send LDAP ou registrations.
62 * send_ldap_browse() - Send LDAP printer registrations.
63 * ldap_dereg_printer() - Delete printer from directory
64 * send_slp_browse() - Register the specified printer with SLP.
65 * slp_attr_callback() - SLP attribute callback
66 * slp_dereg_printer() - SLPDereg() the specified printer
67 * slp_get_attr() - Get an attribute from an SLP registration.
68 * slp_reg_callback() - Empty SLPRegReport.
69 * slp_url_callback() - SLP service url callback
70 * update_cups_browse() - Update the browse lists using the CUPS
72 * update_lpd() - Update the LPD configuration as needed.
73 * update_polling() - Read status messages from the poll daemons.
74 * update_smb() - Update the SMB configuration as needed.
78 * Include necessary headers...
88 # ifdef HAVE_COREFOUNDATION
89 # include <CoreFoundation/CoreFoundation.h>
90 # endif /* HAVE_COREFOUNDATION */
91 # ifdef HAVE_SYSTEMCONFIGURATION
92 # include <SystemConfiguration/SystemConfiguration.h>
93 # endif /* HAVE_SYSTEMCONFIGURATION */
94 # endif /* __APPLE__ */
95 #endif /* HAVE_DNSSD */
102 static char *dequote(char *d
, const char *s
, int dlen
);
104 static int get_hostconfig(const char *name
);
105 #endif /* __APPLE__ */
106 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
107 char *resource
, int resourcelen
);
108 static void process_browse_data(const char *uri
, const char *host
,
109 const char *resource
, cups_ptype_t type
,
110 ipp_pstate_t state
, const char *location
,
111 const char *info
, const char *make_model
,
112 int num_attrs
, cups_option_t
*attrs
);
113 static void process_implicit_classes(void);
114 static void send_cups_browse(cupsd_printer_t
*p
);
116 static LDAP
*ldap_connect(void);
117 static void ldap_reconnect(void);
118 static void ldap_disconnect(LDAP
*ld
);
119 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
120 char *filter
, char *attrs
[],
121 int attrsonly
, LDAPMessage
**res
);
122 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
123 char *attr
, char *retval
,
124 unsigned long maxsize
);
125 static void ldap_freeres(LDAPMessage
*entry
);
126 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
127 static void send_ldap_browse(cupsd_printer_t
*p
);
128 static void ldap_dereg_printer(cupsd_printer_t
*p
);
129 static void ldap_dereg_ou(char *ou
, char *basedn
);
130 # ifdef HAVE_LDAP_REBIND_PROC
131 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
132 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
133 LDAP_CONST
char *refsp
,
138 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
144 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
145 # endif /* HAVE_LDAP_REBIND_PROC */
146 #endif /* HAVE_LDAP */
148 static void send_slp_browse(cupsd_printer_t
*p
);
149 #endif /* HAVE_LIBSLP */
150 static void update_cups_browse(void);
151 static void update_lpd(int onoff
);
152 static void update_polling(void);
153 static void update_smb(int onoff
);
157 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
159 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
160 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
161 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
163 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
164 DNSServiceFlags flags
,
165 DNSServiceErrorType errorCode
,
166 const char *name
, const char *regtype
,
167 const char *domain
, void *context
);
168 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
169 static void dnssdUpdate(void);
170 #endif /* HAVE_DNSSD */
173 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
175 "printerDescription",
177 "printerMakeAndModel",
182 #endif /* HAVE_LDAP */
190 * SLP service name for CUPS...
193 # define SLP_CUPS_SRVTYPE "service:printer"
194 # define SLP_CUPS_SRVLEN 15
198 * Printer service URL structure
201 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
203 struct _slpsrvurl_s
*next
; /* Next URL in list */
204 char url
[HTTP_MAX_URI
];
213 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
214 SLPError errcode
, void *cookie
);
215 static void slp_dereg_printer(cupsd_printer_t
*p
);
216 static int slp_get_attr(const char *attrlist
, const char *tag
,
218 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
220 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
221 unsigned short lifetime
,
222 SLPError errcode
, void *cookie
);
223 #endif /* HAVE_LIBSLP */
227 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
228 * local printer and remove any pending
229 * references to remote printers.
233 cupsdDeregisterPrinter(
234 cupsd_printer_t
*p
, /* I - Printer to register */
235 int removeit
) /* I - Printer being permanently removed */
238 * Only deregister if browsing is enabled and it's a local printer...
241 if (!Browsing
|| !p
->shared
||
242 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
246 * Announce the deletion...
249 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
251 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
253 p
->type
|= CUPS_PRINTER_DELETE
;
261 if (BrowseLocalProtocols
& BROWSE_SLP
)
262 slp_dereg_printer(p
);
263 #endif /* HAVE_LIBSLP */
266 if (BrowseLocalProtocols
& BROWSE_LDAP
)
267 ldap_dereg_printer(p
);
268 #endif /* HAVE_LDAP */
271 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
272 dnssdDeregisterPrinter(p
);
273 #endif /* HAVE_DNSSD */
278 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
282 cupsdLoadRemoteCache(void)
284 cups_file_t
*fp
; /* remote.cache file */
285 int linenum
; /* Current line number */
286 char line
[1024], /* Line from file */
287 *value
, /* Pointer to value */
288 *valueptr
, /* Pointer into value */
289 scheme
[32], /* Scheme portion of URI */
290 username
[64], /* Username portion of URI */
292 /* Hostname portion of URI */
293 resource
[HTTP_MAX_URI
];
294 /* Resource portion of URI */
295 int port
; /* Port number */
296 cupsd_printer_t
*p
; /* Current printer */
297 time_t now
; /* Current time */
301 * Don't load the cache if the remote protocols are disabled...
306 cupsdLogMessage(CUPSD_LOG_DEBUG
,
307 "cupsdLoadRemoteCache: Not loading remote cache.");
312 * Open the remote.cache file...
315 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
316 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
320 * Read printer configurations until we hit EOF...
327 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
330 * Decode the directive...
333 if (!strcasecmp(line
, "<Printer") ||
334 !strcasecmp(line
, "<DefaultPrinter"))
337 * <Printer name> or <DefaultPrinter name>
340 if (p
== NULL
&& value
)
343 * Add the printer and a base file type...
346 cupsdLogMessage(CUPSD_LOG_DEBUG
,
347 "cupsdLoadRemoteCache: Loading printer %s...", value
);
349 if ((p
= cupsdFindDest(value
)) != NULL
)
351 if (p
->type
& CUPS_PRINTER_CLASS
)
353 cupsdLogMessage(CUPSD_LOG_WARN
,
354 "Cached remote printer \"%s\" conflicts with "
362 p
= cupsdAddPrinter(value
);
365 p
->state
= IPP_PRINTER_IDLE
;
366 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
367 p
->browse_time
= now
;
368 p
->browse_expire
= now
+ BrowseTimeout
;
371 * Set the default printer as needed...
374 if (!strcasecmp(line
, "<DefaultPrinter"))
379 cupsdLogMessage(CUPSD_LOG_ERROR
,
380 "Syntax error on line %d of remote.cache.", linenum
);
384 else if (!strcasecmp(line
, "<Class") ||
385 !strcasecmp(line
, "<DefaultClass"))
388 * <Class name> or <DefaultClass name>
391 if (p
== NULL
&& value
)
394 * Add the printer and a base file type...
397 cupsdLogMessage(CUPSD_LOG_DEBUG
,
398 "cupsdLoadRemoteCache: Loading class %s...", value
);
400 if ((p
= cupsdFindDest(value
)) != NULL
)
401 p
->type
= CUPS_PRINTER_CLASS
;
403 p
= cupsdAddClass(value
);
406 p
->state
= IPP_PRINTER_IDLE
;
407 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
408 p
->browse_time
= now
;
409 p
->browse_expire
= now
+ BrowseTimeout
;
412 * Set the default printer as needed...
415 if (!strcasecmp(line
, "<DefaultClass"))
420 cupsdLogMessage(CUPSD_LOG_ERROR
,
421 "Syntax error on line %d of remote.cache.", linenum
);
425 else if (!strcasecmp(line
, "</Printer>") ||
426 !strcasecmp(line
, "</Class>"))
431 * Close out the current printer...
434 cupsdSetPrinterAttrs(p
);
440 cupsdLogMessage(CUPSD_LOG_ERROR
,
441 "Syntax error on line %d of remote.cache.", linenum
);
447 cupsdLogMessage(CUPSD_LOG_ERROR
,
448 "Syntax error on line %d of remote.cache.", linenum
);
451 else if (!strcasecmp(line
, "Info"))
454 cupsdSetString(&p
->info
, value
);
456 else if (!strcasecmp(line
, "MakeModel"))
459 cupsdSetString(&p
->make_model
, value
);
461 else if (!strcasecmp(line
, "Location"))
464 cupsdSetString(&p
->location
, value
);
466 else if (!strcasecmp(line
, "DeviceURI"))
470 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
471 username
, sizeof(username
), host
, sizeof(host
), &port
,
472 resource
, sizeof(resource
));
474 cupsdSetString(&p
->hostname
, host
);
475 cupsdSetString(&p
->uri
, value
);
476 cupsdSetString(&p
->device_uri
, value
);
480 cupsdLogMessage(CUPSD_LOG_ERROR
,
481 "Syntax error on line %d of remote.cache.", linenum
);
485 else if (!strcasecmp(line
, "Option") && value
)
491 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
494 cupsdLogMessage(CUPSD_LOG_ERROR
,
495 "Syntax error on line %d of remote.cache.", linenum
);
498 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
500 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
504 else if (!strcasecmp(line
, "State"))
507 * Set the initial queue state...
510 if (value
&& !strcasecmp(value
, "idle"))
511 p
->state
= IPP_PRINTER_IDLE
;
512 else if (value
&& !strcasecmp(value
, "stopped"))
513 p
->state
= IPP_PRINTER_STOPPED
;
516 cupsdLogMessage(CUPSD_LOG_ERROR
,
517 "Syntax error on line %d of remote.cache.", linenum
);
521 else if (!strcasecmp(line
, "StateMessage"))
524 * Set the initial queue state message...
528 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
530 else if (!strcasecmp(line
, "Accepting"))
533 * Set the initial accepting state...
537 (!strcasecmp(value
, "yes") ||
538 !strcasecmp(value
, "on") ||
539 !strcasecmp(value
, "true")))
542 (!strcasecmp(value
, "no") ||
543 !strcasecmp(value
, "off") ||
544 !strcasecmp(value
, "false")))
548 cupsdLogMessage(CUPSD_LOG_ERROR
,
549 "Syntax error on line %d of remote.cache.", linenum
);
553 else if (!strcasecmp(line
, "Type"))
556 p
->type
= atoi(value
);
559 cupsdLogMessage(CUPSD_LOG_ERROR
,
560 "Syntax error on line %d of remote.cache.", linenum
);
564 else if (!strcasecmp(line
, "BrowseTime"))
568 time_t t
= atoi(value
);
570 if (t
> p
->browse_expire
)
571 p
->browse_expire
= t
;
575 cupsdLogMessage(CUPSD_LOG_ERROR
,
576 "Syntax error on line %d of remote.cache.", linenum
);
580 else if (!strcasecmp(line
, "JobSheets"))
583 * Set the initial job sheets...
588 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
593 cupsdSetString(&p
->job_sheets
[0], value
);
595 while (isspace(*valueptr
& 255))
600 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
605 cupsdSetString(&p
->job_sheets
[1], value
);
610 cupsdLogMessage(CUPSD_LOG_ERROR
,
611 "Syntax error on line %d of remote.cache.", linenum
);
615 else if (!strcasecmp(line
, "AllowUser"))
620 cupsdAddPrinterUser(p
, value
);
624 cupsdLogMessage(CUPSD_LOG_ERROR
,
625 "Syntax error on line %d of remote.cache.", linenum
);
629 else if (!strcasecmp(line
, "DenyUser"))
634 cupsdAddPrinterUser(p
, value
);
638 cupsdLogMessage(CUPSD_LOG_ERROR
,
639 "Syntax error on line %d of remote.cache.", linenum
);
646 * Something else we don't understand...
649 cupsdLogMessage(CUPSD_LOG_ERROR
,
650 "Unknown configuration directive %s on line %d of remote.cache.",
658 * Do auto-classing if needed...
661 process_implicit_classes();
666 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
667 * printer or update the broadcast contents.
671 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
673 if (!Browsing
|| !BrowseLocalProtocols
||
674 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
678 /* if (BrowseLocalProtocols & BROWSE_SLP)
679 slpRegisterPrinter(p); */
680 #endif /* HAVE_LIBSLP */
683 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
684 dnssdRegisterPrinter(p
);
685 #endif /* HAVE_DNSSD */
690 * 'cupsdRestartPolling()' - Restart polling servers as needed.
694 cupsdRestartPolling(void)
696 int i
; /* Looping var */
697 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
700 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
702 kill(pollp
->pid
, SIGHUP
);
707 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
711 cupsdSaveRemoteCache(void)
713 int i
; /* Looping var */
714 cups_file_t
*fp
; /* printers.conf file */
715 char temp
[1024]; /* Temporary string */
716 cupsd_printer_t
*printer
; /* Current printer class */
717 time_t curtime
; /* Current time */
718 struct tm
*curdate
; /* Current date */
719 cups_option_t
*option
; /* Current option */
723 * Create the remote.cache file...
726 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
728 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
730 cupsdLogMessage(CUPSD_LOG_ERROR
,
731 "Unable to save remote.cache - %s", strerror(errno
));
735 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
738 * Restrict access to the file...
741 fchown(cupsFileNumber(fp
), getuid(), Group
);
742 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
745 * Write a small header to the file...
748 curtime
= time(NULL
);
749 curdate
= localtime(&curtime
);
750 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
752 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
753 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
756 * Write each local printer known to the system...
759 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
761 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
764 * Skip local destinations...
767 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
771 * Write printers as needed...
774 if (printer
== DefaultPrinter
)
775 cupsFilePuts(fp
, "<Default");
777 cupsFilePutChar(fp
, '<');
779 if (printer
->type
& CUPS_PRINTER_CLASS
)
780 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
782 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
784 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
786 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
789 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
791 if (printer
->make_model
)
792 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
794 if (printer
->location
)
795 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
797 if (printer
->device_uri
)
798 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
800 if (printer
->state
== IPP_PRINTER_STOPPED
)
802 cupsFilePuts(fp
, "State Stopped\n");
803 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
806 cupsFilePuts(fp
, "State Idle\n");
808 if (printer
->accepting
)
809 cupsFilePuts(fp
, "Accepting Yes\n");
811 cupsFilePuts(fp
, "Accepting No\n");
813 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
814 printer
->job_sheets
[1]);
816 for (i
= 0; i
< printer
->num_users
; i
++)
817 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
820 for (i
= printer
->num_options
, option
= printer
->options
;
823 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
825 if (printer
->type
& CUPS_PRINTER_CLASS
)
826 cupsFilePuts(fp
, "</Class>\n");
828 cupsFilePuts(fp
, "</Printer>\n");
836 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
840 cupsdSendBrowseList(void)
842 int count
; /* Number of dests to update */
843 cupsd_printer_t
*p
; /* Current printer */
844 time_t ut
, /* Minimum update time */
845 to
; /* Timeout time */
848 if (!Browsing
|| !Printers
)
852 * Compute the update and timeout times...
856 ut
= to
- BrowseInterval
;
859 * Figure out how many printers need an update...
862 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
864 int max_count
; /* Maximum number to update */
868 * Throttle the number of printers we'll be updating this time
869 * around based on the number of queues that need updating and
870 * the maximum number of queues to update each second...
873 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
875 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
876 count
< max_count
&& p
!= NULL
;
877 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
878 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
879 p
->shared
&& p
->browse_time
< ut
)
883 * Loop through all of the printers and send local updates as needed...
887 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
889 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
893 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
896 * Check for wraparound...
900 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
904 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
907 else if (p
->browse_time
< ut
)
910 * Need to send an update...
915 p
->browse_time
= time(NULL
);
917 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
921 if (BrowseLocalProtocols
& BROWSE_SLP
)
923 #endif /* HAVE_LIBSLP */
926 if (BrowseLocalProtocols
& BROWSE_LDAP
)
928 #endif /* HAVE_LDAP */
933 * Save where we left off so that all printers get updated...
940 * Loop through all of the printers and timeout old printers as needed...
943 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
945 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
948 * If this is a remote queue, see if it needs to be timed out...
951 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
952 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
953 p
->browse_expire
< to
)
955 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
956 "%s \'%s\' deleted by directory services (timeout).",
957 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
960 cupsdLogMessage(CUPSD_LOG_DEBUG
,
961 "Remote destination \"%s\" has timed out; "
965 cupsArraySave(Printers
);
966 cupsdDeletePrinter(p
, 1);
967 cupsArrayRestore(Printers
);
968 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
974 #ifdef HAVE_LDAP_REBIND_PROC
975 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
977 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
981 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
982 LDAP_CONST
char *refsp
,
990 * Bind to new LDAP server...
993 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
994 "ldap_rebind_proc: Rebind to %s", refsp
);
996 # if LDAP_API_VERSION > 3000
998 bval
.bv_val
= BrowseLDAPPassword
;
999 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1001 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1003 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
1004 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1005 # endif /* LDAP_API_VERSION > 3000 */
1010 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1013 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1017 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1029 * Free current values...
1032 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1033 "ldap_rebind_proc: Free values...");
1035 if ( dnp
&& *dnp
) {
1038 if ( passwdp
&& *passwdp
) {
1046 * Return credentials for LDAP referal...
1049 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1050 "ldap_rebind_proc: Return necessary values...");
1052 *dnp
= strdup(BrowseLDAPBindDN
);
1053 *passwdp
= strdup(BrowseLDAPPassword
);
1054 *authmethodp
= LDAP_AUTH_SIMPLE
;
1060 * Should never happen...
1063 cupsdLogMessage(CUPSD_LOG_ERROR
,
1064 "LDAP rebind has been called with wrong freeit value!");
1069 return (LDAP_SUCCESS
);
1071 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1072 #endif /* HAVE_LDAP_REBIND_PROC */
1077 * 'ldap_connect()' - Start new LDAP connection
1084 * Open LDAP handle...
1087 int rc
; /* LDAP API status */
1088 int version
= 3; /* LDAP version */
1089 struct berval bv
= {0, ""}; /* SASL bind value */
1090 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1091 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1092 int ldap_ssl
= 0; /* LDAP SSL indicator */
1093 int ssl_err
= 0; /* LDAP SSL error value */
1094 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1096 # ifdef HAVE_OPENLDAP
1097 # ifdef HAVE_LDAP_SSL
1100 * Set the certificate file to use for encrypted LDAP sessions...
1103 if (BrowseLDAPCACertFile
)
1105 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1106 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1107 BrowseLDAPCACertFile
);
1109 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1110 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1111 cupsdLogMessage(CUPSD_LOG_ERROR
,
1112 "Unable to set CA certificate file for LDAP "
1113 "connections: %d - %s", rc
, ldap_err2string(rc
));
1116 # endif /* HAVE_LDAP_SSL */
1118 * Initialize OPENLDAP connection...
1119 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1122 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1123 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1125 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1127 # else /* HAVE_OPENLDAP */
1129 int ldap_port
= 0; /* LDAP port */
1130 char ldap_protocol
[11], /* LDAP protocol */
1131 ldap_host
[255]; /* LDAP host */
1134 * Split LDAP URI into its components...
1137 if (! BrowseLDAPServer
)
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1141 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1142 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1146 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1148 if (strcmp(ldap_protocol
, "ldap") == 0) {
1150 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1153 cupsdLogMessage(CUPSD_LOG_ERROR
,
1154 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1155 cupsdLogMessage(CUPSD_LOG_ERROR
,
1156 "Disable LDAP browsing!");
1157 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1158 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1165 ldap_port
= LDAPS_PORT
;
1167 ldap_port
= LDAP_PORT
;
1170 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1171 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1172 ldap_protocol
, ldap_host
, ldap_port
);
1175 * Initialize LDAP connection...
1180 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1181 rc
= LDAP_OPERATIONS_ERROR
;
1185 # ifdef HAVE_LDAP_SSL
1191 * Initialize SSL LDAP connection...
1193 if (BrowseLDAPCACertFile
)
1195 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1196 if (rc
!= LDAP_SUCCESS
) {
1197 cupsdLogMessage(CUPSD_LOG_ERROR
,
1198 "Failed to initialize LDAP SSL client!");
1199 rc
= LDAP_OPERATIONS_ERROR
;
1201 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1202 rc
= LDAP_OPERATIONS_ERROR
;
1209 cupsdLogMessage(CUPSD_LOG_ERROR
,
1210 "LDAP SSL certificate file/database not configured!");
1211 rc
= LDAP_OPERATIONS_ERROR
;
1214 # else /* HAVE_LDAP_SSL */
1217 * Return error, because client libraries doesn't support SSL
1220 cupsdLogMessage(CUPSD_LOG_ERROR
,
1221 "LDAP client libraries does not support TLS");
1222 rc
= LDAP_OPERATIONS_ERROR
;
1224 # endif /* HAVE_LDAP_SSL */
1226 # endif /* HAVE_OPENLDAP */
1229 * Check return code from LDAP initialize...
1232 if (rc
!= LDAP_SUCCESS
)
1234 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1236 cupsdLogMessage(CUPSD_LOG_ERROR
,
1237 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1241 cupsdLogMessage(CUPSD_LOG_ERROR
,
1242 "Unable to initialize LDAP! Disable LDAP browsing!");
1243 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1244 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1247 ldap_disconnect(TempBrowseLDAPHandle
);
1248 TempBrowseLDAPHandle
= NULL
;
1252 * Upgrade LDAP version...
1255 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1256 (const void *)&version
) != LDAP_SUCCESS
)
1258 cupsdLogMessage(CUPSD_LOG_ERROR
,
1259 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1261 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1262 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1263 ldap_disconnect(TempBrowseLDAPHandle
);
1264 TempBrowseLDAPHandle
= NULL
;
1270 * Register LDAP rebind procedure...
1273 # ifdef HAVE_LDAP_REBIND_PROC
1274 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1276 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1277 if ( rc
!= LDAP_SUCCESS
)
1278 cupsdLogMessage(CUPSD_LOG_ERROR
,
1279 "Setting LDAP rebind function failed with status %d: %s",
1280 rc
, ldap_err2string(rc
));
1284 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1286 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1287 # endif /* HAVE_LDAP_REBIND_PROC */
1290 * Start LDAP bind...
1293 # if LDAP_API_VERSION > 3000
1295 bval
.bv_val
= BrowseLDAPPassword
;
1296 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1298 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1299 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1302 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1304 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1305 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1306 # endif /* LDAP_API_VERSION > 3000 */
1308 if (rc
!= LDAP_SUCCESS
)
1310 cupsdLogMessage(CUPSD_LOG_ERROR
,
1311 "LDAP bind failed with error %d: %s",
1312 rc
, ldap_err2string(rc
));
1313 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1314 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1316 ssl_err
= PORT_GetError();
1318 cupsdLogMessage(CUPSD_LOG_ERROR
,
1319 "LDAP SSL error %d: %s",
1320 ssl_err
, ldapssl_err2string(ssl_err
));
1322 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1323 ldap_disconnect(TempBrowseLDAPHandle
);
1324 TempBrowseLDAPHandle
= NULL
;
1328 cupsdLogMessage(CUPSD_LOG_INFO
,
1329 "LDAP connection established");
1333 return (TempBrowseLDAPHandle
);
1338 * 'ldap_reconnect()' - Reconnect to LDAP Server
1342 ldap_reconnect(void)
1344 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1346 cupsdLogMessage(CUPSD_LOG_INFO
,
1347 "Try LDAP reconnect...");
1350 * Get a new LDAP Handle and replace the global Handle
1351 * if the new connection was successful
1354 TempBrowseLDAPHandle
= ldap_connect();
1356 if (TempBrowseLDAPHandle
!= NULL
)
1358 if (BrowseLDAPHandle
!= NULL
)
1360 ldap_disconnect(BrowseLDAPHandle
);
1362 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1368 * 'ldap_disconnect()' - Disconnect from LDAP Server
1372 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1374 int rc
; /* return code */
1377 * Close LDAP handle...
1380 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1381 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1383 rc
= ldap_unbind_s(ld
);
1384 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1385 if (rc
!= LDAP_SUCCESS
)
1386 cupsdLogMessage(CUPSD_LOG_ERROR
,
1387 "Unbind from LDAP server failed with status %d: %s",
1388 rc
, ldap_err2string(rc
));
1390 #endif /* HAVE_LDAP */
1394 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1398 cupsdStartBrowsing(void)
1400 int val
; /* Socket option value */
1401 struct sockaddr_in addr
; /* Broadcast address */
1402 cupsd_printer_t
*p
; /* Current printer */
1407 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1410 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1412 if (BrowseSocket
< 0)
1415 * Create the broadcast socket...
1418 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1420 cupsdLogMessage(CUPSD_LOG_ERROR
,
1421 "Unable to create broadcast socket - %s.",
1423 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1424 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1429 * Bind the socket to browse port...
1432 memset(&addr
, 0, sizeof(addr
));
1433 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1434 addr
.sin_family
= AF_INET
;
1435 addr
.sin_port
= htons(BrowsePort
);
1437 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1439 cupsdLogMessage(CUPSD_LOG_ERROR
,
1440 "Unable to bind broadcast socket - %s.",
1444 closesocket(BrowseSocket
);
1446 close(BrowseSocket
);
1450 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1451 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1457 * Set the "broadcast" flag...
1461 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1463 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1467 closesocket(BrowseSocket
);
1469 close(BrowseSocket
);
1473 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1474 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1479 * Close the socket on exec...
1482 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1485 * Finally, add the socket to the input selection set as needed...
1488 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1491 * We only listen if we want remote printers...
1494 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1502 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1504 DNSServiceErrorType error
; /* Error from service creation */
1505 cupsd_listener_t
*lis
; /* Current listening socket */
1509 * First create a "master" connection for all registrations...
1512 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1513 != kDNSServiceErr_NoError
)
1514 cupsdLogMessage(CUPSD_LOG_ERROR
,
1515 "Unable to create master DNS-SD reference: %d", error
);
1519 * Add the master connection to the select list...
1522 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1523 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1526 * Then get the port we use for registrations. If we are not listening
1527 * on any non-local ports, there is no sense sharing local printers via
1533 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1535 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1537 if (httpAddrLocalhost(&(lis
->address
)))
1540 if (lis
->address
.addr
.sa_family
== AF_INET
)
1542 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1545 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1547 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1553 * Create an array to track the printers we share...
1556 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1557 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1561 * Set the computer name and register the web interface...
1564 cupsdUpdateDNSSDName();
1567 #endif /* HAVE_DNSSD */
1570 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1573 * Open SLP handle...
1576 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1578 cupsdLogMessage(CUPSD_LOG_ERROR
,
1579 "Unable to open an SLP handle; disabling SLP browsing!");
1580 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1581 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1584 BrowseSLPRefresh
= 0;
1587 BrowseSLPHandle
= NULL
;
1588 #endif /* HAVE_LIBSLP */
1591 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1595 cupsdLogMessage(CUPSD_LOG_ERROR
,
1596 "Need to set BrowseLDAPDN to use LDAP browsing!");
1597 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1598 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1602 /* Open LDAP handle... */
1603 BrowseLDAPHandle
= ldap_connect();
1606 BrowseLDAPRefresh
= 0;
1608 #endif /* HAVE_LDAP */
1611 * Enable LPD and SMB printer sharing as needed through external programs...
1614 if (BrowseLocalProtocols
& BROWSE_LPD
)
1617 if (BrowseLocalProtocols
& BROWSE_SMB
)
1621 * Register the individual printers
1624 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1626 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1627 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1628 cupsdRegisterPrinter(p
);
1633 * 'cupsdStartPolling()' - Start polling servers as needed.
1637 cupsdStartPolling(void)
1639 int i
; /* Looping var */
1640 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1641 char polld
[1024]; /* Poll daemon path */
1642 char sport
[255]; /* Server port */
1643 char bport
[255]; /* Browser port */
1644 char interval
[255]; /* Poll interval */
1645 int statusfds
[2]; /* Status pipe */
1646 char *argv
[6]; /* Arguments */
1647 char *envp
[100]; /* Environment */
1651 * Don't do anything if we aren't polling...
1654 if (NumPolled
== 0 || BrowseSocket
< 0)
1657 PollStatusBuffer
= NULL
;
1662 * Setup string arguments for polld, port and interval options.
1665 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1667 sprintf(bport
, "%d", BrowsePort
);
1670 sprintf(interval
, "%d", BrowseInterval
);
1672 strcpy(interval
, "30");
1674 argv
[0] = "cups-polld";
1680 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1683 * Create a pipe that receives the status messages from each
1687 if (cupsdOpenPipe(statusfds
))
1689 cupsdLogMessage(CUPSD_LOG_ERROR
,
1690 "Unable to create polling status pipes - %s.",
1693 PollStatusBuffer
= NULL
;
1697 PollPipe
= statusfds
[0];
1698 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1701 * Run each polling daemon, redirecting stderr to the polling pipe...
1704 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1706 sprintf(sport
, "%d", pollp
->port
);
1708 argv
[1] = pollp
->hostname
;
1710 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1711 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1713 cupsdLogMessage(CUPSD_LOG_ERROR
,
1714 "cupsdStartPolling: Unable to fork polling daemon - %s",
1720 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1721 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1722 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1725 close(statusfds
[1]);
1728 * Finally, add the pipe to the input selection set...
1731 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1736 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1740 cupsdStopBrowsing(void)
1742 cupsd_printer_t
*p
; /* Current printer */
1745 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1749 * De-register the individual printers
1752 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1754 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1755 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1756 cupsdDeregisterPrinter(p
, 1);
1759 * Shut down browsing sockets...
1762 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1766 * Close the socket and remove it from the input selection set.
1770 closesocket(BrowseSocket
);
1772 close(BrowseSocket
);
1775 cupsdRemoveSelect(BrowseSocket
);
1780 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1784 DNSServiceRefDeallocate(WebIFRef
);
1790 DNSServiceRefDeallocate(RemoteRef
);
1794 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1796 DNSServiceRefDeallocate(DNSSDRef
);
1799 cupsArrayDelete(DNSSDPrinters
);
1800 DNSSDPrinters
= NULL
;
1804 #endif /* HAVE_DNSSD */
1807 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1811 * Close SLP handle...
1814 SLPClose(BrowseSLPHandle
);
1815 BrowseSLPHandle
= NULL
;
1817 #endif /* HAVE_LIBSLP */
1820 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1823 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1824 ldap_disconnect(BrowseLDAPHandle
);
1825 BrowseLDAPHandle
= NULL
;
1827 #endif /* HAVE_OPENLDAP */
1830 * Disable LPD and SMB printer sharing as needed through external programs...
1833 if (BrowseLocalProtocols
& BROWSE_LPD
)
1836 if (BrowseLocalProtocols
& BROWSE_SMB
)
1842 * 'cupsdStopPolling()' - Stop polling servers as needed.
1846 cupsdStopPolling(void)
1848 int i
; /* Looping var */
1849 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1854 cupsdStatBufDelete(PollStatusBuffer
);
1857 cupsdRemoveSelect(PollPipe
);
1860 PollStatusBuffer
= NULL
;
1863 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1865 cupsdEndProcess(pollp
->pid
, 0);
1871 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1875 cupsdUpdateDNSSDName(void)
1877 DNSServiceErrorType error
; /* Error from service creation */
1878 char webif
[1024]; /* Web interface share name */
1879 #ifdef HAVE_COREFOUNDATION_H
1880 CFStringRef nameRef
; /* Computer name CFString */
1881 char nameBuffer
[1024]; /* C-string buffer */
1882 CFStringEncoding nameEncoding
; /* Computer name encoding */
1883 #endif /* HAVE_COREFOUNDATION_H */
1887 * Only share the web interface and printers when non-local listening is
1895 * Get the computer name as a c-string...
1898 #ifdef HAVE_COREFOUNDATION_H
1899 cupsdClearString(&DNSSDName
);
1901 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1902 &nameEncoding
)) != NULL
)
1904 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1905 kCFStringEncodingUTF8
))
1906 cupsdSetString(&DNSSDName
, nameBuffer
);
1912 cupsdSetString(&DNSSDName
, ServerName
);
1913 #endif /* HAVE_COREFOUNDATION_H */
1916 * Then (re)register the web interface...
1920 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1922 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1925 DNSServiceRefDeallocate(WebIFRef
);
1927 WebIFRef
= DNSSDRef
;
1928 if ((error
= DNSServiceRegister(&WebIFRef
,
1929 kDNSServiceFlagsShareConnection
,
1930 0, webif
, "_http._tcp", NULL
,
1931 NULL
, htons(DNSSDPort
), 7,
1932 "\006path=/", dnssdRegisterCallback
,
1933 NULL
)) != kDNSServiceErr_NoError
)
1934 cupsdLogMessage(CUPSD_LOG_ERROR
,
1935 "DNS-SD web interface registration failed: %d", error
);
1937 #endif /* HAVE_DNSSD */
1942 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1946 cupsdUpdateLDAPBrowse(void)
1948 char uri
[HTTP_MAX_URI
], /* Printer URI */
1949 host
[HTTP_MAX_URI
], /* Hostname */
1950 resource
[HTTP_MAX_URI
], /* Resource path */
1951 location
[1024], /* Printer location */
1952 info
[1024], /* Printer information */
1953 make_model
[1024], /* Printer make and model */
1954 type_num
[30]; /* Printer type number */
1955 int type
; /* Printer type */
1956 int rc
; /* LDAP status */
1957 int limit
; /* Size limit */
1958 LDAPMessage
*res
, /* LDAP search results */
1959 *e
; /* Current entry from search */
1961 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1963 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1966 * Reconnect if LDAP Handle is invalid...
1969 if (! BrowseLDAPHandle
)
1976 * Search for cups printers in LDAP directory...
1979 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1980 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1983 * If ldap search was successfull then exit function
1984 * and temporary disable LDAP updates...
1987 if (rc
!= LDAP_SUCCESS
)
1989 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1991 BrowseLDAPUpdate
= FALSE
;
1992 cupsdLogMessage(CUPSD_LOG_INFO
,
1993 "LDAP update temporary disabled");
1999 * If LDAP updates were disabled, we will reenable them...
2002 if (! BrowseLDAPUpdate
)
2004 BrowseLDAPUpdate
= TRUE
;
2005 cupsdLogMessage(CUPSD_LOG_INFO
,
2006 "LDAP update enabled");
2010 * Count LDAP entries and return if no entry exist...
2013 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2014 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2022 * Loop through the available printers...
2025 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2027 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2030 * Get the required values from this entry...
2033 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2034 "printerDescription", info
, sizeof(info
)) == -1)
2037 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2038 "printerLocation", location
, sizeof(location
)) == -1)
2041 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2042 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2045 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2046 "printerType", type_num
, sizeof(type_num
)) == -1)
2049 type
= atoi(type_num
);
2051 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2052 "printerURI", uri
, sizeof(uri
)) == -1)
2056 * Process the entry as browse data...
2059 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2060 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2061 location
, info
, make_model
, 0, NULL
);
2067 #endif /* HAVE_LDAP */
2072 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2076 cupsdUpdateSLPBrowse(void)
2078 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2079 *next
; /* Next service in list */
2080 cupsd_printer_t p
; /* Printer information */
2081 const char *uri
; /* Pointer to printer URI */
2082 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2083 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2087 * Reset the refresh time...
2090 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2093 * Poll for remote printers using SLP...
2098 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2099 slp_url_callback
, &s
);
2102 * Loop through the list of available printers...
2108 * Save the "next" pointer...
2114 * Load a cupsd_printer_t structure with the SLP service attributes...
2117 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2120 * Process this printer entry...
2123 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2125 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2128 * Pull the URI apart to see if this is a local or remote printer...
2131 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2132 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2133 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2137 * Free this listing...
2140 cupsdClearString(&p
.info
);
2141 cupsdClearString(&p
.location
);
2142 cupsdClearString(&p
.make_model
);
2147 #endif /* HAVE_LIBSLP */
2151 * 'dequote()' - Remote quotes from a string.
2154 static char * /* O - Dequoted string */
2155 dequote(char *d
, /* I - Destination string */
2156 const char *s
, /* I - Source string */
2157 int dlen
) /* I - Destination length */
2159 char *dptr
; /* Pointer into destination */
2164 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2182 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2185 static char * /* O - TXT record */
2186 dnssdBuildTxtRecord(
2187 int *txt_len
, /* O - TXT record length */
2188 cupsd_printer_t
*p
, /* I - Printer information */
2189 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2191 int i
, j
; /* Looping vars */
2192 char type_str
[32], /* Type to string buffer */
2193 state_str
[32], /* State to string buffer */
2194 rp_str
[1024], /* Queue name string buffer */
2195 air_str
[1024], /* auth-info-required string buffer */
2196 *keyvalue
[32][2]; /* Table of key/value pairs */
2200 * Load up the key value pairs...
2205 keyvalue
[i
][0] = "txtvers";
2206 keyvalue
[i
++][1] = "1";
2208 keyvalue
[i
][0] = "qtotal";
2209 keyvalue
[i
++][1] = "1";
2211 keyvalue
[i
][0] = "rp";
2212 keyvalue
[i
++][1] = rp_str
;
2214 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2216 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2217 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2219 keyvalue
[i
][0] = "ty";
2220 keyvalue
[i
++][1] = p
->make_model
;
2222 if (p
->location
&& *p
->location
!= '\0')
2224 keyvalue
[i
][0] = "note";
2225 keyvalue
[i
++][1] = p
->location
;
2228 keyvalue
[i
][0] = "priority";
2229 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2231 keyvalue
[i
][0] = "product";
2232 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2234 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2235 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2237 keyvalue
[i
][0] = "printer-state";
2238 keyvalue
[i
++][1] = state_str
;
2240 keyvalue
[i
][0] = "printer-type";
2241 keyvalue
[i
++][1] = type_str
;
2243 keyvalue
[i
][0] = "Transparent";
2244 keyvalue
[i
++][1] = "T";
2246 keyvalue
[i
][0] = "Binary";
2247 keyvalue
[i
++][1] = "T";
2249 if ((p
->type
& CUPS_PRINTER_FAX
))
2251 keyvalue
[i
][0] = "Fax";
2252 keyvalue
[i
++][1] = "T";
2255 if ((p
->type
& CUPS_PRINTER_COLOR
))
2257 keyvalue
[i
][0] = "Color";
2258 keyvalue
[i
++][1] = "T";
2261 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2263 keyvalue
[i
][0] = "Duplex";
2264 keyvalue
[i
++][1] = "T";
2267 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2269 keyvalue
[i
][0] = "Staple";
2270 keyvalue
[i
++][1] = "T";
2273 if ((p
->type
& CUPS_PRINTER_COPIES
))
2275 keyvalue
[i
][0] = "Copies";
2276 keyvalue
[i
++][1] = "T";
2279 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2281 keyvalue
[i
][0] = "Collate";
2282 keyvalue
[i
++][1] = "T";
2285 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2287 keyvalue
[i
][0] = "Punch";
2288 keyvalue
[i
++][1] = "T";
2291 if ((p
->type
& CUPS_PRINTER_BIND
))
2293 keyvalue
[i
][0] = "Bind";
2294 keyvalue
[i
++][1] = "T";
2297 if ((p
->type
& CUPS_PRINTER_SORT
))
2299 keyvalue
[i
][0] = "Sort";
2300 keyvalue
[i
++][1] = "T";
2303 keyvalue
[i
][0] = "pdl";
2304 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2306 if (p
->num_auth_info_required
)
2308 char *air
= air_str
; /* Pointer into string */
2311 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2313 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2319 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2323 keyvalue
[i
][0] = "air";
2324 keyvalue
[i
++][1] = air
;
2328 * Then pack them into a proper txt record...
2331 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2336 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2339 static int /* O - Result of comparison */
2340 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2341 cupsd_printer_t
*b
)/* I - Second printer */
2343 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2348 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2353 dnssdDeregisterPrinter(
2354 cupsd_printer_t
*p
) /* I - Printer */
2356 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2359 * Closing the socket deregisters the service
2364 DNSServiceRefDeallocate(p
->ipp_ref
);
2368 cupsArrayRemove(DNSSDPrinters
, p
);
2369 cupsdClearString(&p
->reg_name
);
2374 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2384 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2385 * TXT record format.
2388 static char * /* O - TXT record */
2389 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2390 char *keyvalue
[][2], /* I - Table of key value pairs */
2391 int count
) /* I - Items in table */
2393 int i
; /* Looping var */
2394 int length
; /* Length of TXT record */
2395 int length2
; /* Length of value */
2396 char *txtRecord
; /* TXT record buffer */
2397 char *cursor
; /* Looping pointer */
2401 * Calculate the buffer size
2404 for (length
= i
= 0; i
< count
; i
++)
2405 length
+= 1 + strlen(keyvalue
[i
][0]) +
2406 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2409 * Allocate and fill it
2412 txtRecord
= malloc(length
);
2417 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2420 * Drop in the p-string style length byte followed by the data
2423 length
= strlen(keyvalue
[i
][0]);
2424 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2426 *cursor
++ = (unsigned char)(length
+ length2
);
2428 memcpy(cursor
, keyvalue
[i
][0], length
);
2435 memcpy(cursor
, keyvalue
[i
][1], length2
);
2446 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2450 dnssdRegisterCallback(
2451 DNSServiceRef sdRef
, /* I - DNS Service reference */
2452 DNSServiceFlags flags
, /* I - Reserved for future use */
2453 DNSServiceErrorType errorCode
, /* I - Error code */
2454 const char *name
, /* I - Service name */
2455 const char *regtype
, /* I - Service type */
2456 const char *domain
, /* I - Domain. ".local" for now */
2457 void *context
) /* I - User-defined context */
2459 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2460 /* Current printer */
2463 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2464 name
, regtype
, p
? p
->name
: "Web Interface");
2468 cupsdLogMessage(CUPSD_LOG_ERROR
,
2469 "DNSServiceRegister failed with error %d", (int)errorCode
);
2472 else if (p
&& strcasecmp(name
, p
->reg_name
))
2474 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2477 cupsArrayRemove(DNSSDPrinters
, p
);
2478 cupsdSetString(&p
->reg_name
, name
);
2479 cupsArrayAdd(DNSSDPrinters
, p
);
2481 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2487 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2488 * or update the broadcast contents.
2492 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2494 DNSServiceErrorType se
; /* dnssd errors */
2495 char *ipp_txt
, /* IPP TXT record buffer */
2496 *printer_txt
, /* LPD TXT record buffer */
2497 name
[1024], /* Service name */
2498 *nameptr
; /* Pointer into name */
2499 int ipp_len
, /* IPP TXT record length */
2500 printer_len
; /* LPD TXT record length */
2501 char resource
[1024]; /* Resource path for printer */
2502 const char *regtype
; /* Registration type */
2503 const char *domain
; /* Registration domain */
2504 cupsd_location_t
*location
, /* Printer location */
2505 *policy
; /* Operation policy for Print-Job */
2506 unsigned address
[4]; /* INADDR_ANY address */
2509 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2510 !p
->ipp_ref
? "new" : "update");
2513 * If per-printer sharing was just disabled make sure we're not
2514 * registered before returning.
2519 dnssdDeregisterPrinter(p
);
2524 * The registered name takes the form of "<printer-info> @ <computer name>"...
2527 if (p
->info
&& strlen(p
->info
) > 0)
2530 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2532 strlcpy(name
, p
->info
, sizeof(name
));
2535 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2537 strlcpy(name
, p
->name
, sizeof(name
));
2540 * If an existing printer was renamed, unregister it and start over...
2543 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2544 dnssdDeregisterPrinter(p
);
2548 cupsdSetString(&p
->reg_name
, name
);
2549 cupsArrayAdd(DNSSDPrinters
, p
);
2553 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2554 * let dnssd decide on the domain, otherwise restrict it to ".local".
2557 if (p
->type
& CUPS_PRINTER_CLASS
)
2558 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2560 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2562 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2563 location
= cupsdFindBest(resource
, HTTP_POST
);
2564 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2566 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2567 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2573 * Register IPP and (optionally) LPD...
2576 ipp_len
= 0; /* anti-compiler-warning-code */
2577 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2582 * Initial registration. Use the _fax subtype for fax queues...
2585 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2588 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2589 "Registering DNS-SD printer %s with name \"%s\", "
2590 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2591 domain
? domain
: "(null)");
2594 * Register the queue, dropping characters as needed until we succeed...
2597 nameptr
= name
+ strlen(name
);
2601 p
->ipp_ref
= DNSSDRef
;
2602 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2603 0, name
, regtype
, domain
, NULL
,
2604 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2605 dnssdRegisterCallback
,
2606 p
)) == kDNSServiceErr_BadParam
)
2609 * Name is too long, drop trailing characters, taking into account
2615 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2622 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2624 if (se
== kDNSServiceErr_NoError
)
2626 p
->ipp_txt
= ipp_txt
;
2627 p
->ipp_len
= ipp_len
;
2631 cupsdLogMessage(CUPSD_LOG_WARN
,
2632 "DNS-SD IPP registration of \"%s\" failed: %d",
2635 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2638 * Update the existing registration...
2641 /* A TTL of 0 means use record's original value (Radar 3176248) */
2642 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2647 p
->ipp_txt
= ipp_txt
;
2648 p
->ipp_len
= ipp_len
;
2655 if (BrowseLocalProtocols
& BROWSE_LPD
)
2657 printer_len
= 0; /* anti-compiler-warning-code */
2658 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2660 if (!p
->printer_ref
)
2663 * Initial registration...
2666 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2667 "Registering DNS-SD printer %s with name \"%s\", "
2668 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2669 name
, domain
? domain
: "(null)");
2671 p
->printer_ref
= DNSSDRef
;
2672 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2673 kDNSServiceFlagsShareConnection
,
2674 0, name
, "_printer._tcp", domain
, NULL
,
2675 htons(515), printer_len
, printer_txt
,
2676 dnssdRegisterCallback
,
2677 p
)) == kDNSServiceErr_NoError
)
2679 p
->printer_txt
= printer_txt
;
2680 p
->printer_len
= printer_len
;
2684 cupsdLogMessage(CUPSD_LOG_WARN
,
2685 "DNS-SD LPD registration of \"%s\" failed: %d",
2688 else if (printer_len
!= p
->printer_len
||
2689 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2692 * Update the existing registration...
2695 /* A TTL of 0 means use record's original value (Radar 3176248) */
2696 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2700 free(p
->printer_txt
);
2702 p
->printer_txt
= printer_txt
;
2703 p
->printer_len
= printer_len
;
2714 * 'dnssdUpdate()' - Handle DNS-SD queries.
2720 DNSServiceErrorType sdErr
; /* Service discovery error */
2723 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2724 cupsdLogMessage(CUPSD_LOG_ERROR
,
2725 "DNS Service Discovery registration error %d!",
2728 #endif /* HAVE_DNSSD */
2733 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2736 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2737 get_hostconfig(const char *name
) /* I - Name of service */
2739 cups_file_t
*fp
; /* Hostconfig file */
2740 char line
[1024], /* Line from file */
2741 *ptr
; /* Pointer to value */
2742 int state
= 1; /* State of service */
2746 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2747 * the service is enabled/auto.
2750 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2753 * Read lines from the file until we find the service...
2756 while (cupsFileGets(fp
, line
, sizeof(line
)))
2758 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2763 if (!strcasecmp(line
, name
))
2766 * Found the service, see if it is set to "-NO-"...
2769 if (!strncasecmp(ptr
, "-NO-", 4))
2780 #endif /* __APPLE__ */
2784 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2787 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2788 is_local_queue(const char *uri
, /* I - Printer URI */
2789 char *host
, /* O - Host string */
2790 int hostlen
, /* I - Length of host buffer */
2791 char *resource
, /* O - Resource string */
2792 int resourcelen
) /* I - Length of resource buffer */
2794 char scheme
[32], /* Scheme portion of URI */
2795 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2796 int port
; /* Port portion of URI */
2797 cupsd_netif_t
*iface
; /* Network interface */
2801 * Pull the URI apart to see if this is a local or remote printer...
2804 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2805 username
, sizeof(username
), host
, hostlen
, &port
,
2806 resource
, resourcelen
) < HTTP_URI_OK
)
2809 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2812 * Check for local server addresses...
2815 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2820 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2822 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2823 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2827 * If we get here, the printer is remote...
2835 * 'process_browse_data()' - Process new browse data.
2839 process_browse_data(
2840 const char *uri
, /* I - URI of printer/class */
2841 const char *host
, /* I - Hostname */
2842 const char *resource
, /* I - Resource path */
2843 cups_ptype_t type
, /* I - Printer type */
2844 ipp_pstate_t state
, /* I - Printer state */
2845 const char *location
, /* I - Printer location */
2846 const char *info
, /* I - Printer information */
2847 const char *make_model
, /* I - Printer make and model */
2848 int num_attrs
, /* I - Number of attributes */
2849 cups_option_t
*attrs
) /* I - Attributes */
2851 int i
; /* Looping var */
2852 int update
; /* Update printer attributes? */
2853 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2854 name
[IPP_MAX_NAME
], /* Name of printer */
2855 newname
[IPP_MAX_NAME
], /* New name of printer */
2856 *hptr
, /* Pointer into hostname */
2857 *sptr
; /* Pointer into ServerName */
2858 const char *shortname
; /* Short queue name (queue) */
2859 char local_make_model
[IPP_MAX_NAME
];
2860 /* Local make and model */
2861 cupsd_printer_t
*p
; /* Printer information */
2862 const char *ipp_options
, /* ipp-options value */
2863 *lease_duration
; /* lease-duration value */
2864 int is_class
; /* Is this queue a class? */
2867 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2868 "process_browse_data(uri=\"%s\", host=\"%s\", "
2869 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2870 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2871 uri
, host
, resource
, type
, state
,
2872 location
? location
: "(nil)", info
? info
: "(nil)",
2873 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2876 * Determine if the URI contains any illegal characters in it...
2879 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2880 (strncmp(resource
, "/printers/", 10) &&
2881 strncmp(resource
, "/classes/", 9)))
2883 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2887 if (strchr(resource
, '?') ||
2888 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2889 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2891 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2897 * OK, this isn't a local printer; add any remote options...
2900 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2902 if (BrowseRemoteOptions
)
2904 if (BrowseRemoteOptions
[0] == '?')
2907 * Override server-supplied options...
2910 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2912 else if (ipp_options
)
2915 * Combine the server and local options...
2918 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2919 BrowseRemoteOptions
);
2924 * Just use the local options...
2927 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2932 else if (ipp_options
)
2935 * Just use the server-supplied options...
2938 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2943 * See if we already have it listed in the Printers list, and add it if not...
2946 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2947 type
&= ~CUPS_PRINTER_IMPLICIT
;
2949 hptr
= strchr(host
, '.');
2950 sptr
= strchr(ServerName
, '.');
2951 is_class
= type
& CUPS_PRINTER_CLASS
;
2953 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2956 * Strip the common domain name components...
2959 while (hptr
!= NULL
)
2961 if (!strcasecmp(hptr
, sptr
))
2967 hptr
= strchr(hptr
+ 1, '.');
2974 * Remote destination is a class...
2977 if (!strncmp(resource
, "/classes/", 9))
2978 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2982 shortname
= resource
+ 9;
2987 * Remote destination is a printer...
2990 if (!strncmp(resource
, "/printers/", 10))
2991 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2995 shortname
= resource
+ 10;
2999 *hptr
= '.'; /* Resource FQDN */
3001 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3004 * Long name doesn't exist, try short name...
3007 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3010 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3013 * Short name doesn't exist, use it for this shared queue.
3016 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3018 strlcpy(name
, shortname
, sizeof(name
));
3023 * Short name exists...
3026 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3027 "process_browse_data: %s found, type=%x, hostname=%s...",
3028 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3030 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3031 p
= NULL
; /* Don't replace implicit classes */
3032 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3035 * Short name exists but is for a different host. If this is a remote
3036 * queue, rename it and use the long name...
3039 if (p
->type
& CUPS_PRINTER_REMOTE
)
3041 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3042 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3043 is_class
? "class" : "printer", p
->name
, p
->name
,
3045 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3046 "%s \'%s\' deleted by directory services.",
3047 is_class
? "Class" : "Printer", p
->name
);
3049 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3050 cupsdRenamePrinter(p
, newname
);
3052 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3053 "%s \'%s\' added by directory services.",
3054 is_class
? "Class" : "Printer", p
->name
);
3058 * Force creation with long name...
3066 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3067 "process_browse_data: %s found, type=%x, hostname=%s...",
3068 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3073 * Queue doesn't exist; add it...
3077 p
= cupsdAddClass(name
);
3079 p
= cupsdAddPrinter(name
);
3081 cupsdClearString(&(p
->hostname
));
3083 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3084 is_class
? "class" : "printer", name
);
3086 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3087 "%s \'%s\' added by directory services.",
3088 is_class
? "Class" : "Printer", name
);
3091 * Force the URI to point to the real server...
3094 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3097 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3106 * Hostname not set, so this must be a cached remote printer
3107 * that was created for a pending print job...
3110 cupsdSetString(&p
->hostname
, host
);
3111 cupsdSetString(&p
->uri
, uri
);
3112 cupsdSetString(&p
->device_uri
, uri
);
3115 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3119 * Update the state...
3123 p
->browse_time
= time(NULL
);
3125 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3129 * Grab the lease-duration for the browse data; anything less then 1
3130 * second or more than 1 week gets the default BrowseTimeout...
3133 i
= atoi(lease_duration
);
3134 if (i
< 1 || i
> 604800)
3137 p
->browse_expire
= p
->browse_time
+ i
;
3140 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3142 if (type
& CUPS_PRINTER_REJECTING
)
3144 type
&= ~CUPS_PRINTER_REJECTING
;
3152 else if (!p
->accepting
)
3158 if (p
->type
!= type
)
3164 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3166 cupsdSetString(&p
->location
, location
);
3170 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3172 cupsdSetString(&p
->info
, info
);
3175 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3178 if (!make_model
|| !make_model
[0])
3181 snprintf(local_make_model
, sizeof(local_make_model
),
3182 "Remote Class on %s", host
);
3184 snprintf(local_make_model
, sizeof(local_make_model
),
3185 "Remote Printer on %s", host
);
3188 snprintf(local_make_model
, sizeof(local_make_model
),
3189 "%s on %s", make_model
, host
);
3191 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3193 cupsdSetString(&p
->make_model
, local_make_model
);
3199 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3202 * See if we need to update the attributes...
3205 if (p
->num_options
!= num_attrs
)
3209 for (i
= 0; i
< num_attrs
; i
++)
3210 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3211 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3212 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3221 * Free the old options...
3224 cupsFreeOptions(p
->num_options
, p
->options
);
3227 p
->num_options
= num_attrs
;
3230 if (type
& CUPS_PRINTER_DELETE
)
3232 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3233 "%s \'%s\' deleted by directory services.",
3234 is_class
? "Class" : "Printer", p
->name
);
3236 cupsdExpireSubscriptions(p
, NULL
);
3238 cupsdDeletePrinter(p
, 1);
3239 cupsdUpdateImplicitClasses();
3240 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3244 cupsdSetPrinterAttrs(p
);
3245 cupsdUpdateImplicitClasses();
3249 * See if we have a default printer... If not, make the first network
3250 * default printer the default.
3253 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3256 * Find the first network default printer and use it...
3259 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3261 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3262 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3265 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3271 * Do auto-classing if needed...
3274 process_implicit_classes();
3279 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3283 process_implicit_classes(void)
3285 int i
; /* Looping var */
3286 int update
; /* Update printer attributes? */
3287 char name
[IPP_MAX_NAME
], /* Name of printer */
3288 *hptr
; /* Pointer into hostname */
3289 cupsd_printer_t
*p
, /* Printer information */
3290 *pclass
, /* Printer class */
3291 *first
; /* First printer in class */
3292 int offset
, /* Offset of name */
3293 len
; /* Length of name */
3296 if (!ImplicitClasses
|| !Printers
)
3300 * Loop through all available printers and create classes as needed...
3303 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3304 update
= 0, pclass
= NULL
, first
= NULL
;
3306 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3309 * Skip implicit classes...
3312 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3319 * If len == 0, get the length of this printer name up to the "@"
3323 cupsArraySave(Printers
);
3326 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3327 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3330 * We have more than one printer with the same name; see if
3331 * we have a class, and if this printer is a member...
3334 if (pclass
&& strcasecmp(pclass
->name
, name
))
3337 cupsdSetPrinterAttrs(pclass
);
3343 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3346 * Need to add the class...
3349 pclass
= cupsdAddPrinter(name
);
3350 cupsArrayAdd(ImplicitPrinters
, pclass
);
3352 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3353 pclass
->accepting
= 1;
3354 pclass
->state
= IPP_PRINTER_IDLE
;
3356 cupsdSetString(&pclass
->location
, p
->location
);
3357 cupsdSetString(&pclass
->info
, p
->info
);
3359 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3360 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3364 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3366 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3368 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3369 "Implicit class \'%s\' added by directory services.",
3375 for (i
= 0; i
< pclass
->num_printers
; i
++)
3376 if (pclass
->printers
[i
] == first
)
3379 if (i
>= pclass
->num_printers
)
3381 first
->in_implicit_class
= 1;
3382 cupsdAddPrinterToClass(pclass
, first
);
3388 for (i
= 0; i
< pclass
->num_printers
; i
++)
3389 if (pclass
->printers
[i
] == p
)
3392 if (i
>= pclass
->num_printers
)
3394 p
->in_implicit_class
= 1;
3395 cupsdAddPrinterToClass(pclass
, p
);
3402 * First time around; just get name length and mark it as first
3406 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3407 len
= hptr
- p
->name
;
3409 len
= strlen(p
->name
);
3411 strncpy(name
, p
->name
, len
);
3415 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3416 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3419 * Can't use same name as a local printer; add "Any" to the
3420 * front of the name, unless we have explicitly disabled
3421 * the "ImplicitAnyClasses"...
3424 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3427 * Add "Any" to the class name...
3430 strcpy(name
, "Any");
3431 strncpy(name
+ 3, p
->name
, len
);
3432 name
[len
+ 3] = '\0';
3438 * Don't create an implicit class if we have a local printer
3439 * with the same name...
3443 cupsArrayRestore(Printers
);
3451 cupsArrayRestore(Printers
);
3455 * Update the last printer class as needed...
3458 if (pclass
&& update
)
3459 cupsdSetPrinterAttrs(pclass
);
3464 * 'send_cups_browse()' - Send new browsing information using the CUPS
3469 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3471 int i
; /* Looping var */
3472 cups_ptype_t type
; /* Printer type */
3473 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3474 int bytes
; /* Length of packet */
3475 char packet
[1453], /* Browse data packet */
3476 uri
[1024], /* Printer URI */
3477 location
[1024], /* printer-location */
3478 info
[1024], /* printer-info */
3480 /* printer-make-and-model */
3481 cupsd_netif_t
*iface
; /* Network interface */
3485 * Figure out the printer type value...
3488 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3491 type
|= CUPS_PRINTER_REJECTING
;
3493 if (p
== DefaultPrinter
)
3494 type
|= CUPS_PRINTER_DEFAULT
;
3497 * Remove quotes from printer-info, printer-location, and
3498 * printer-make-and-model attributes...
3501 dequote(location
, p
->location
, sizeof(location
));
3502 dequote(info
, p
->info
, sizeof(info
));
3505 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3506 else if (p
->type
& CUPS_PRINTER_CLASS
)
3508 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3509 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3511 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3514 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3516 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3519 * Send a packet to each browse address...
3522 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3526 * Send the browse packet to one or more interfaces...
3529 if (!strcmp(b
->iface
, "*"))
3532 * Send to all local interfaces...
3537 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3539 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3542 * Only send to local, IPv4 interfaces...
3545 if (!iface
->is_local
|| !iface
->port
||
3546 iface
->address
.addr
.sa_family
!= AF_INET
)
3549 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3550 iface
->hostname
, iface
->port
,
3551 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3554 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3555 type
, p
->state
, uri
, location
, info
, make_model
,
3556 p
->browse_attrs
? p
->browse_attrs
: "");
3558 bytes
= strlen(packet
);
3560 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3561 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3562 iface
->name
, packet
);
3564 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3566 sendto(BrowseSocket
, packet
, bytes
, 0,
3567 (struct sockaddr
*)&(iface
->broadcast
),
3568 httpAddrLength(&(iface
->broadcast
)));
3571 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3574 * Send to the named interface using the IPv4 address...
3578 if (strcmp(b
->iface
, iface
->name
))
3583 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3586 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3590 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3591 iface
->hostname
, iface
->port
,
3592 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3595 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3596 type
, p
->state
, uri
, location
, info
, make_model
,
3597 p
->browse_attrs
? p
->browse_attrs
: "");
3599 bytes
= strlen(packet
);
3601 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3602 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3603 iface
->name
, packet
);
3605 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3607 sendto(BrowseSocket
, packet
, bytes
, 0,
3608 (struct sockaddr
*)&(iface
->broadcast
),
3609 httpAddrLength(&(iface
->broadcast
)));
3616 * Send the browse packet to the indicated address using
3617 * the default server name...
3620 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3621 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3622 p
->browse_attrs
? p
->browse_attrs
: "");
3624 bytes
= strlen(packet
);
3625 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3626 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3628 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3629 (struct sockaddr
*)&(b
->to
),
3630 httpAddrLength(&(b
->to
))) <= 0)
3633 * Unable to send browse packet, so remove this address from the
3637 cupsdLogMessage(CUPSD_LOG_ERROR
,
3638 "cupsdSendBrowseList: sendto failed for browser "
3640 (int)(b
- Browsers
+ 1), strerror(errno
));
3643 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3654 * 'ldap_search_rec()' - LDAP Search with reconnect
3658 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3659 char *base
, /* I - Base dn */
3660 int scope
, /* I - LDAP search scope */
3661 char *filter
, /* I - Filter string */
3662 char *attrs
[], /* I - Requested attributes */
3663 int attrsonly
, /* I - Return only attributes? */
3664 LDAPMessage
**res
) /* I - LDAP handler */
3666 int rc
; /* Return code */
3669 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3670 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3671 NULL
, LDAP_NO_LIMIT
, res
);
3673 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3674 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3677 * If we have a connection problem try again...
3680 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3682 cupsdLogMessage(CUPSD_LOG_ERROR
,
3683 "LDAP search failed with status %d: %s",
3684 rc
, ldap_err2string(rc
));
3685 cupsdLogMessage(CUPSD_LOG_INFO
,
3686 "We try the LDAP search once again after reconnecting to "
3691 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3692 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3693 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3695 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3696 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3699 if (rc
== LDAP_NO_SUCH_OBJECT
)
3700 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3701 "ldap_search_rec: LDAP entry/object not found");
3702 else if (rc
!= LDAP_SUCCESS
)
3703 cupsdLogMessage(CUPSD_LOG_ERROR
,
3704 "ldap_search_rec: LDAP search failed with status %d: %s",
3705 rc
, ldap_err2string(rc
));
3707 if (rc
!= LDAP_SUCCESS
)
3715 * 'ldap_freeres()' - Free LDAPMessage
3719 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3721 int rc
; /* Return value */
3724 rc
= ldap_msgfree(entry
);
3726 cupsdLogMessage(CUPSD_LOG_WARN
,
3727 "Can't free LDAPMessage!");
3729 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3730 "Freeing LDAPMessage was unnecessary");
3735 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3739 ldap_getval_firststring(
3740 LDAP
*ld
, /* I - LDAP handler */
3741 LDAPMessage
*entry
, /* I - LDAP message or search result */
3742 char *attr
, /* I - the wanted attribute */
3743 char *retval
, /* O - String to return */
3744 unsigned long maxsize
) /* I - Max string size */
3746 char *dn
; /* LDAP DN */
3747 int rc
= 0; /* Return code */
3748 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3749 struct berval
**bval
; /* LDAP value array */
3750 unsigned long size
; /* String size */
3754 * Get value from LDAPMessage...
3757 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3760 dn
= ldap_get_dn(ld
, entry
);
3761 cupsdLogMessage(CUPSD_LOG_WARN
,
3762 "Failed to get LDAP value %s for %s!",
3770 * Check size and copy value into our string...
3774 if (size
< bval
[0]->bv_len
)
3777 dn
= ldap_get_dn(ld
, entry
);
3778 cupsdLogMessage(CUPSD_LOG_WARN
,
3779 "Attribute %s is too big! (dn: %s)",
3784 size
= bval
[0]->bv_len
;
3786 strlcpy(retval
, bval
[0]->bv_val
, size
);
3787 ldap_value_free_len(bval
);
3790 char **value
; /* LDAP value */
3793 * Get value from LDAPMessage...
3796 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3799 dn
= ldap_get_dn(ld
, entry
);
3800 cupsdLogMessage(CUPSD_LOG_WARN
,
3801 "Failed to get LDAP value %s for %s!",
3807 strlcpy(retval
, *value
, maxsize
);
3808 ldap_value_free(value
);
3810 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3817 * 'send_ldap_ou()' - Send LDAP ou registrations.
3821 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3822 char *basedn
, /* I - Our base dn */
3823 char *descstring
) /* I - Description for ou */
3825 int i
; /* Looping var... */
3826 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3827 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3828 LDAPMessage
*res
, /* Search result token */
3829 *e
; /* Current entry from search */
3830 int rc
; /* LDAP status */
3831 char dn
[1024], /* DN of the organizational unit we are adding */
3832 *desc
[2], /* Change records */
3834 char old_desc
[1024]; /* Old description */
3835 static const char * const objectClass_values
[] =
3836 { /* The 2 objectClass's we use in */
3837 "top", /* our LDAP entries */
3838 "organizationalUnit",
3841 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3848 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3851 * Reconnect if LDAP Handle is invalid...
3854 if (! BrowseLDAPHandle
)
3856 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3857 "send_ldap_ou: LDAP Handle is invalid. Try "
3864 * Prepare ldap search...
3867 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3868 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3872 desc
[0] = descstring
;
3875 mods
[0].mod_type
= "ou";
3876 mods
[0].mod_values
= ou_value
;
3877 mods
[1].mod_type
= "description";
3878 mods
[1].mod_values
= desc
;
3879 mods
[2].mod_type
= "objectClass";
3880 mods
[2].mod_values
= (char **)objectClass_values
;
3882 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3883 (char **)ou_attrs
, 0, &res
);
3886 * If ldap search was not successfull then exit function...
3889 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3893 * Check if we need to insert or update the LDAP entry...
3896 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3897 rc
!= LDAP_NO_SUCH_OBJECT
)
3900 * Printserver has already been registered, check if
3901 * modification is required...
3904 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
3907 * Get the required values from this entry...
3910 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
3911 sizeof(old_desc
)) == -1)
3915 * Check if modification is required...
3918 if ( strcmp(desc
[0], old_desc
) == 0 )
3921 * LDAP entry for the printer exists.
3922 * Printer has already been registered,
3923 * no modifications required...
3925 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3926 "send_ldap_ou: No updates required for %s", ou
);
3931 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3932 "send_ldap_ou: Replace entry for %s", ou
);
3934 for (i
= 0; i
< 3; i
++)
3936 pmods
[i
] = mods
+ i
;
3937 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3941 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3942 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3943 NULL
)) != LDAP_SUCCESS
)
3945 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3946 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3948 cupsdLogMessage(CUPSD_LOG_ERROR
,
3949 "LDAP modify for %s failed with status %d: %s",
3950 ou
, rc
, ldap_err2string(rc
));
3951 if ( LDAP_SERVER_DOWN
== rc
)
3959 * Printserver has never been registered,
3960 * add registration...
3963 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3964 "send_ldap_ou: Add entry for %s", ou
);
3966 for (i
= 0; i
< 3; i
++)
3968 pmods
[i
] = mods
+ i
;
3969 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3973 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3974 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3975 NULL
)) != LDAP_SUCCESS
)
3977 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3978 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3980 cupsdLogMessage(CUPSD_LOG_ERROR
,
3981 "LDAP add for %s failed with status %d: %s",
3982 ou
, rc
, ldap_err2string(rc
));
3983 if ( LDAP_SERVER_DOWN
== rc
)
3993 * 'send_ldap_browse()' - Send LDAP printer registrations.
3997 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3999 int i
; /* Looping var... */
4000 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4001 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4002 LDAPMessage
*res
, /* Search result token */
4003 *e
; /* Current entry from search */
4004 char *cn_value
[2], /* Change records */
4010 typestring
[255], /* String to hold printer-type */
4011 dn
[1024]; /* DN of the printer we are adding */
4012 int rc
; /* LDAP status */
4013 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4014 old_location
[1024], /* Printer location */
4015 old_info
[1024], /* Printer information */
4016 old_make_model
[1024], /* Printer make and model */
4017 old_type_string
[30]; /* Temporary type number */
4018 int old_type
; /* Printer type */
4019 static const char * const objectClass_values
[] =
4020 { /* The 3 objectClass's we use in */
4021 "top", /* our LDAP entries */
4028 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4031 * Exit function if LDAP updates has been disabled...
4034 if (!BrowseLDAPUpdate
)
4036 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4037 "send_ldap_browse: Updates temporary disabled; "
4043 * Reconnect if LDAP Handle is invalid...
4046 if (!BrowseLDAPHandle
)
4048 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4049 "send_ldap_browse: LDAP Handle is invalid. Try "
4056 * Everything in ldap is ** so we fudge around it...
4059 sprintf(typestring
, "%u", p
->type
);
4061 cn_value
[0] = p
->name
;
4063 info
[0] = p
->info
? p
->info
: "Unknown";
4065 location
[0] = p
->location
? p
->location
: "Unknown";
4067 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4068 make_model
[1] = NULL
;
4069 type
[0] = typestring
;
4075 * Get ldap entry for printer ...
4078 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4080 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4082 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4083 (char **)ldap_attrs
, 0, &res
);
4086 * If ldap search was not successfull then exit function
4087 * and temporary disable LDAP updates...
4090 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4092 if (BrowseLDAPUpdate
&&
4093 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4095 BrowseLDAPUpdate
= FALSE
;
4096 cupsdLogMessage(CUPSD_LOG_INFO
,
4097 "LDAP update temporary disabled");
4104 * Fill modification array...
4107 mods
[0].mod_type
= "cn";
4108 mods
[0].mod_values
= cn_value
;
4109 mods
[1].mod_type
= "printerDescription";
4110 mods
[1].mod_values
= info
;
4111 mods
[2].mod_type
= "printerURI";
4112 mods
[2].mod_values
= uri
;
4113 mods
[3].mod_type
= "printerLocation";
4114 mods
[3].mod_values
= location
;
4115 mods
[4].mod_type
= "printerMakeAndModel";
4116 mods
[4].mod_values
= make_model
;
4117 mods
[5].mod_type
= "printerType";
4118 mods
[5].mod_values
= type
;
4119 mods
[6].mod_type
= "objectClass";
4120 mods
[6].mod_values
= (char **)objectClass_values
;
4123 * Check if we need to insert or update the LDAP entry...
4126 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4127 rc
!= LDAP_NO_SUCH_OBJECT
)
4130 * Printer has already been registered, check if
4131 * modification is required...
4134 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4137 * Get the required values from this entry...
4140 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4141 old_info
, sizeof(old_info
)) == -1)
4144 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4145 old_location
, sizeof(old_location
)) == -1)
4148 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4149 old_make_model
, sizeof(old_make_model
)) == -1)
4152 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4153 old_type_string
, sizeof(old_type_string
)) == -1)
4156 old_type
= atoi(old_type_string
);
4158 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4159 sizeof(old_uri
)) == -1)
4163 * Check if modification is required...
4166 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4167 !strcmp(location
[0], old_location
) &&
4168 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4171 * LDAP entry for the printer exists. Printer has already been registered,
4172 * no modifications required...
4175 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4176 "send_ldap_browse: No updates required for %s", p
->name
);
4181 * LDAP entry for the printer exists. Printer has already been registered,
4182 * modify the current registration...
4185 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4186 "send_ldap_browse: Replace entry for %s", p
->name
);
4188 for (i
= 0; i
< 7; i
++)
4190 pmods
[i
] = mods
+ i
;
4191 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4195 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4196 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4197 NULL
)) != LDAP_SUCCESS
)
4199 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4200 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4202 cupsdLogMessage(CUPSD_LOG_ERROR
,
4203 "LDAP modify for %s failed with status %d: %s",
4204 p
->name
, rc
, ldap_err2string(rc
));
4205 if (rc
== LDAP_SERVER_DOWN
)
4213 * No LDAP entry exists for the printer. Printer has never been registered,
4214 * add the current registration...
4217 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4219 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4220 "send_ldap_browse: Add entry for %s", p
->name
);
4222 for (i
= 0; i
< 7; i
++)
4224 pmods
[i
] = mods
+ i
;
4225 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4229 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4230 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4231 NULL
)) != LDAP_SUCCESS
)
4233 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4234 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4236 cupsdLogMessage(CUPSD_LOG_ERROR
,
4237 "LDAP add for %s failed with status %d: %s",
4238 p
->name
, rc
, ldap_err2string(rc
));
4239 if (rc
== LDAP_SERVER_DOWN
)
4249 * 'ldap_dereg_printer()' - Delete printer from directory
4253 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4255 char dn
[1024]; /* DN of the printer */
4256 int rc
; /* LDAP status */
4259 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4263 * Reconnect if LDAP Handle is invalid...
4266 if (!BrowseLDAPHandle
)
4273 * Get dn for printer and delete LDAP entry...
4276 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4278 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4280 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4281 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4282 NULL
)) != LDAP_SUCCESS
)
4284 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4285 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4287 cupsdLogMessage(CUPSD_LOG_WARN
,
4288 "LDAP delete for %s failed with status %d: %s",
4289 p
->name
, rc
, ldap_err2string(rc
));
4292 * If we had a connection problem (connection timed out, etc.)
4293 * we should reconnect and try again to delete the entry...
4296 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4298 cupsdLogMessage(CUPSD_LOG_INFO
,
4299 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4302 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4303 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4304 NULL
)) != LDAP_SUCCESS
)
4306 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4307 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4308 cupsdLogMessage(CUPSD_LOG_WARN
,
4309 "LDAP delete for %s failed with status %d: %s",
4310 p
->name
, rc
, ldap_err2string(rc
));
4317 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4318 char *basedn
) /* I - Dase dn */
4320 char dn
[1024]; /* DN of the printer */
4321 int rc
; /* LDAP status */
4324 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4327 * Reconnect if LDAP Handle is invalid...
4330 if (!BrowseLDAPHandle
)
4337 * Get dn for printer and delete LDAP entry...
4340 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4341 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4343 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4344 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4345 NULL
)) != LDAP_SUCCESS
)
4347 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4348 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4350 cupsdLogMessage(CUPSD_LOG_WARN
,
4351 "LDAP delete for %s failed with status %d: %s",
4352 ou
, rc
, ldap_err2string(rc
));
4355 * If we had a connection problem (connection timed out, etc.)
4356 * we should reconnect and try again to delete the entry...
4359 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4361 cupsdLogMessage(CUPSD_LOG_INFO
,
4362 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4364 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4365 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4366 NULL
)) != LDAP_SUCCESS
)
4368 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4369 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4370 cupsdLogMessage(CUPSD_LOG_WARN
,
4371 "LDAP delete for %s failed with status %d: %s",
4372 ou
, rc
, ldap_err2string(rc
));
4377 #endif /* HAVE_LDAP */
4382 * 'send_slp_browse()' - Register the specified printer with SLP.
4386 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4388 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4389 attrs
[8192], /* Printer attributes */
4390 finishings
[1024], /* Finishings to support */
4391 make_model
[IPP_MAX_NAME
* 2],
4392 /* Make and model, quoted */
4393 location
[IPP_MAX_NAME
* 2],
4394 /* Location, quoted */
4395 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4396 *src
, /* Pointer to original string */
4397 *dst
; /* Pointer to destination string */
4398 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4399 SLPError error
; /* SLP error, if any */
4402 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4406 * Make the SLP service URL that conforms to the IANA
4407 * 'printer:' template.
4410 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4412 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4415 * Figure out the finishings string...
4418 if (p
->type
& CUPS_PRINTER_STAPLE
)
4419 strcpy(finishings
, "staple");
4421 finishings
[0] = '\0';
4423 if (p
->type
& CUPS_PRINTER_BIND
)
4426 strlcat(finishings
, ",bind", sizeof(finishings
));
4428 strcpy(finishings
, "bind");
4431 if (p
->type
& CUPS_PRINTER_PUNCH
)
4434 strlcat(finishings
, ",punch", sizeof(finishings
));
4436 strcpy(finishings
, "punch");
4439 if (p
->type
& CUPS_PRINTER_COVER
)
4442 strlcat(finishings
, ",cover", sizeof(finishings
));
4444 strcpy(finishings
, "cover");
4447 if (p
->type
& CUPS_PRINTER_SORT
)
4450 strlcat(finishings
, ",sort", sizeof(finishings
));
4452 strcpy(finishings
, "sort");
4456 strcpy(finishings
, "none");
4459 * Quote any commas in the make and model, location, and info strings...
4462 for (src
= p
->make_model
, dst
= make_model
;
4463 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4465 if (*src
== ',' || *src
== '\\' || *src
== ')')
4474 strcpy(make_model
, "Unknown");
4476 for (src
= p
->location
, dst
= location
;
4477 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4479 if (*src
== ',' || *src
== '\\' || *src
== ')')
4488 strcpy(location
, "Unknown");
4490 for (src
= p
->info
, dst
= info
;
4491 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4493 if (*src
== ',' || *src
== '\\' || *src
== ')')
4502 strcpy(info
, "Unknown");
4505 * Get the authentication value...
4508 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4512 * Make the SLP attribute string list that conforms to
4513 * the IANA 'printer:' template.
4516 snprintf(attrs
, sizeof(attrs
),
4517 "(printer-uri-supported=%s),"
4518 "(uri-authentication-supported=%s>),"
4520 "(uri-security-supported=tls>),"
4522 "(uri-security-supported=none>),"
4523 #endif /* HAVE_SSL */
4524 "(printer-name=%s),"
4525 "(printer-location=%s),"
4526 "(printer-info=%s),"
4527 "(printer-more-info=%s),"
4528 "(printer-make-and-model=%s),"
4529 "(printer-type=%d),"
4530 "(charset-supported=utf-8),"
4531 "(natural-language-configured=%s),"
4532 "(natural-language-supported=de,en,es,fr,it),"
4533 "(color-supported=%s),"
4534 "(finishings-supported=%s),"
4535 "(sides-supported=one-sided%s),"
4536 "(multiple-document-jobs-supported=true)"
4537 "(ipp-versions-supported=1.0,1.1)",
4538 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4539 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4540 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4542 p
->type
& CUPS_PRINTER_DUPLEX
?
4543 ",two-sided-long-edge,two-sided-short-edge" : "");
4545 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4548 * Register the printer with the SLP server...
4551 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4552 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4554 if (error
!= SLP_OK
)
4555 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4561 * 'slp_attr_callback()' - SLP attribute callback
4564 static SLPBoolean
/* O - SLP_TRUE for success */
4566 SLPHandle hslp
, /* I - SLP handle */
4567 const char *attrlist
, /* I - Attribute list */
4568 SLPError errcode
, /* I - Parsing status for this attr */
4569 void *cookie
) /* I - Current printer */
4571 char *tmp
= 0; /* Temporary string */
4572 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4573 /* Current printer */
4576 (void)hslp
; /* anti-compiler-warning-code */
4579 * Bail if there was an error
4582 if (errcode
!= SLP_OK
)
4586 * Parse the attrlist to obtain things needed to build CUPS browse packet
4589 memset(p
, 0, sizeof(cupsd_printer_t
));
4591 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4593 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4595 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4597 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4598 p
->type
= atoi(tmp
);
4600 p
->type
= CUPS_PRINTER_REMOTE
;
4602 cupsdClearString(&tmp
);
4609 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4613 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4615 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4618 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4620 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4623 * Make the SLP service URL that conforms to the IANA
4624 * 'printer:' template.
4627 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4630 * Deregister the printer...
4633 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4639 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4642 static int /* O - 0 on success */
4643 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4644 const char *tag
, /* I - Name of attribute */
4645 char **valbuf
) /* O - Value */
4647 char *ptr1
, /* Pointer into string */
4651 cupsdClearString(valbuf
);
4653 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4655 ptr1
+= strlen(tag
);
4657 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4663 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4664 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4667 * Dequote the value...
4670 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4671 if (*ptr1
== '\\' && ptr1
[1])
4672 _cups_strcpy(ptr1
, ptr1
+ 1);
4683 * 'slp_reg_callback()' - Empty SLPRegReport.
4687 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4688 SLPError errcode
, /* I - Error code, if any */
4689 void *cookie
) /* I - App data */
4700 * 'slp_url_callback()' - SLP service url callback
4703 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4705 SLPHandle hslp
, /* I - SLP handle */
4706 const char *srvurl
, /* I - URL of service */
4707 unsigned short lifetime
, /* I - Life of service */
4708 SLPError errcode
, /* I - Existing error code */
4709 void *cookie
) /* I - Pointer to service list */
4711 slpsrvurl_t
*s
, /* New service entry */
4712 **head
; /* Pointer to head of entry */
4716 * Let the compiler know we won't be using these vars...
4723 * Bail if there was an error
4726 if (errcode
!= SLP_OK
)
4730 * Grab the head of the list...
4733 head
= (slpsrvurl_t
**)cookie
;
4736 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4739 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4743 * Copy the SLP service URL...
4746 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4749 * Link the SLP service URL into the head of the list
4759 #endif /* HAVE_LIBSLP */
4763 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4767 update_cups_browse(void)
4769 int i
; /* Looping var */
4770 int auth
; /* Authorization status */
4771 int len
; /* Length of name string */
4772 int bytes
; /* Number of bytes left */
4773 char packet
[1541], /* Broadcast packet */
4774 *pptr
; /* Pointer into packet */
4775 socklen_t srclen
; /* Length of source address */
4776 http_addr_t srcaddr
; /* Source address */
4777 char srcname
[1024]; /* Source hostname */
4778 unsigned address
[4]; /* Source address */
4779 unsigned type
; /* Printer type */
4780 unsigned state
; /* Printer state */
4781 char uri
[HTTP_MAX_URI
], /* Printer URI */
4782 host
[HTTP_MAX_URI
], /* Host portion of URI */
4783 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4784 info
[IPP_MAX_NAME
], /* Information string */
4785 location
[IPP_MAX_NAME
], /* Location string */
4786 make_model
[IPP_MAX_NAME
];/* Make and model string */
4787 int num_attrs
; /* Number of attributes */
4788 cups_option_t
*attrs
; /* Attributes */
4792 * Read a packet from the browse socket...
4795 srclen
= sizeof(srcaddr
);
4796 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4797 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4800 * "Connection refused" is returned under Linux if the destination port
4801 * or address is unreachable from a previous sendto(); check for the
4802 * error here and ignore it for now...
4805 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4807 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4809 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4812 closesocket(BrowseSocket
);
4814 close(BrowseSocket
);
4817 cupsdRemoveSelect(BrowseSocket
);
4820 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4821 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4827 packet
[bytes
] = '\0';
4830 * If we're about to sleep, ignore incoming browse packets.
4837 * Figure out where it came from...
4841 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4843 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4844 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4845 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4846 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4849 #endif /* AF_INET6 */
4854 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4857 if (HostNameLookups
)
4858 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4860 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4862 len
= strlen(srcname
);
4870 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4873 * Access from localhost (127.0.0.1) is always allowed...
4876 auth
= CUPSD_AUTH_ALLOW
;
4881 * Do authorization checks on the domain/address...
4884 switch (BrowseACL
->order_type
)
4887 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4890 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4891 auth
= CUPSD_AUTH_ALLOW
;
4893 if (cupsdCheckAuth(address
, srcname
, len
,
4894 BrowseACL
->num_deny
, BrowseACL
->deny
))
4895 auth
= CUPSD_AUTH_DENY
;
4897 if (cupsdCheckAuth(address
, srcname
, len
,
4898 BrowseACL
->num_allow
, BrowseACL
->allow
))
4899 auth
= CUPSD_AUTH_ALLOW
;
4902 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
4903 auth
= CUPSD_AUTH_DENY
;
4905 if (cupsdCheckAuth(address
, srcname
, len
,
4906 BrowseACL
->num_allow
, BrowseACL
->allow
))
4907 auth
= CUPSD_AUTH_ALLOW
;
4909 if (cupsdCheckAuth(address
, srcname
, len
,
4910 BrowseACL
->num_deny
, BrowseACL
->deny
))
4911 auth
= CUPSD_AUTH_DENY
;
4917 auth
= CUPSD_AUTH_ALLOW
;
4919 if (auth
== CUPSD_AUTH_DENY
)
4921 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4922 "update_cups_browse: Refused %d bytes from %s", bytes
,
4927 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4928 "update_cups_browse: (%d bytes from %s) %s", bytes
,
4935 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
4937 cupsdLogMessage(CUPSD_LOG_WARN
,
4938 "update_cups_browse: Garbled browse packet - %s", packet
);
4942 strcpy(location
, "Location Unknown");
4943 strcpy(info
, "No Information Available");
4944 make_model
[0] = '\0';
4948 if ((pptr
= strchr(packet
, '\"')) != NULL
)
4951 * Have extended information; can't use sscanf for it because not all
4952 * sscanf's allow empty strings with %[^\"]...
4955 for (i
= 0, pptr
++;
4956 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4958 location
[i
] = *pptr
;
4966 while (*pptr
&& isspace(*pptr
& 255))
4971 for (i
= 0, pptr
++;
4972 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
4981 while (*pptr
&& isspace(*pptr
& 255))
4986 for (i
= 0, pptr
++;
4987 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
4989 make_model
[i
] = *pptr
;
4994 make_model
[i
] = '\0';
4997 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5003 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5004 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5005 type
, state
, uri
, location
, info
, make_model
));
5008 * Pull the URI apart to see if this is a local or remote printer...
5011 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5013 cupsFreeOptions(num_attrs
, attrs
);
5021 for (i
= 0; i
< NumRelays
; i
++)
5022 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5023 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5024 (struct sockaddr
*)&(Relays
[i
].to
),
5025 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5027 cupsdLogMessage(CUPSD_LOG_ERROR
,
5028 "update_cups_browse: sendto failed for relay %d - %s.",
5029 i
+ 1, strerror(errno
));
5030 cupsFreeOptions(num_attrs
, attrs
);
5035 * Process the browse data...
5038 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5039 (ipp_pstate_t
)state
, location
, info
, make_model
,
5045 * 'update_lpd()' - Update the LPD configuration as needed.
5049 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5056 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5057 * setting for backwards-compatibility.
5060 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5062 #endif /* __APPLE__ */
5064 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5067 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5070 char newfile
[1024]; /* New cups-lpd.N file */
5071 cups_file_t
*ofp
, /* Original file pointer */
5072 *nfp
; /* New file pointer */
5073 char line
[1024]; /* Line from file */
5076 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5078 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5080 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5081 LPDConfigFile
+ 9, strerror(errno
));
5085 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5087 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5088 newfile
, strerror(errno
));
5094 * Copy all of the lines from the cups-lpd file...
5097 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5101 cupsFilePrintf(nfp
, "%s\n", line
);
5102 snprintf(line
, sizeof(line
), "\tdisable = %s",
5103 onoff
? "no" : "yes");
5105 else if (!strstr(line
, "disable ="))
5106 cupsFilePrintf(nfp
, "%s\n", line
);
5111 rename(newfile
, LPDConfigFile
+ 9);
5114 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5117 * Enable/disable LPD via the launchctl command...
5120 char *argv
[5], /* Arguments for command */
5121 *envp
[MAX_ENV
]; /* Environment for command */
5122 int pid
; /* Process ID */
5125 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5126 argv
[0] = (char *)"launchctl";
5127 argv
[1] = (char *)(onoff
? "load" : "unload");
5128 argv
[2] = (char *)"-w";
5129 argv
[3] = LPDConfigFile
+ 10;
5132 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5135 #endif /* __APPLE__ */
5137 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5142 * 'update_polling()' - Read status messages from the poll daemons.
5146 update_polling(void)
5148 char *ptr
, /* Pointer to end of line in buffer */
5149 message
[1024]; /* Pointer to message text */
5150 int loglevel
; /* Log level for message */
5153 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5154 message
, sizeof(message
))) != NULL
)
5155 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5158 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5161 * All polling processes have died; stop polling...
5164 cupsdLogMessage(CUPSD_LOG_ERROR
,
5165 "update_polling: all polling processes have exited!");
5172 * 'update_smb()' - Update the SMB configuration as needed.
5176 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5181 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5184 * Enable/disable SMB via the specified smb.conf config file...
5187 char newfile
[1024]; /* New smb.conf.N file */
5188 cups_file_t
*ofp
, /* Original file pointer */
5189 *nfp
; /* New file pointer */
5190 char line
[1024]; /* Line from file */
5191 int in_printers
; /* In [printers] section? */
5194 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5196 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5198 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5199 SMBConfigFile
+ 8, strerror(errno
));
5203 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5205 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5206 newfile
, strerror(errno
));
5212 * Copy all of the lines from the smb.conf file...
5217 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5219 if (in_printers
&& strstr(line
, "printable ="))
5220 snprintf(line
, sizeof(line
), " printable = %s",
5221 onoff
? "yes" : "no");
5223 cupsFilePrintf(nfp
, "%s\n", line
);
5226 in_printers
= !strcmp(line
, "[printers]");
5231 rename(newfile
, SMBConfigFile
+ 8);
5234 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5239 * End of "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $".