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
;
1426 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1427 cupsdEndProcess(getpid(), 0);
1431 if (BrowseSocket
>= 0)
1434 * Bind the socket to browse port...
1437 memset(&addr
, 0, sizeof(addr
));
1438 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1439 addr
.sin_family
= AF_INET
;
1440 addr
.sin_port
= htons(BrowsePort
);
1442 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1444 cupsdLogMessage(CUPSD_LOG_ERROR
,
1445 "Unable to bind broadcast socket - %s.",
1449 closesocket(BrowseSocket
);
1451 close(BrowseSocket
);
1455 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1456 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1458 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1459 cupsdEndProcess(getpid(), 0);
1463 if (BrowseSocket
>= 0)
1466 * Set the "broadcast" flag...
1470 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1472 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1476 closesocket(BrowseSocket
);
1478 close(BrowseSocket
);
1482 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1483 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1485 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1486 cupsdEndProcess(getpid(), 0);
1490 if (BrowseSocket
>= 0)
1493 * Close the socket on exec...
1496 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1499 * Finally, add the socket to the input selection set as needed...
1502 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1505 * We only listen if we want remote printers...
1508 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1517 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1519 DNSServiceErrorType error
; /* Error from service creation */
1520 cupsd_listener_t
*lis
; /* Current listening socket */
1524 * First create a "master" connection for all registrations...
1527 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1528 != kDNSServiceErr_NoError
)
1530 cupsdLogMessage(CUPSD_LOG_ERROR
,
1531 "Unable to create master DNS-SD reference: %d", error
);
1533 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1534 cupsdEndProcess(getpid(), 0);
1539 * Add the master connection to the select list...
1542 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1543 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1546 * Then get the port we use for registrations. If we are not listening
1547 * on any non-local ports, there is no sense sharing local printers via
1553 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1555 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1557 if (httpAddrLocalhost(&(lis
->address
)))
1560 if (lis
->address
.addr
.sa_family
== AF_INET
)
1562 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1565 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1567 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1573 * Create an array to track the printers we share...
1576 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1577 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1581 * Set the computer name and register the web interface...
1584 cupsdUpdateDNSSDName();
1587 #endif /* HAVE_DNSSD */
1590 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1593 * Open SLP handle...
1596 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1598 cupsdLogMessage(CUPSD_LOG_ERROR
,
1599 "Unable to open an SLP handle; disabling SLP browsing!");
1600 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1601 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1602 BrowseSLPHandle
= NULL
;
1604 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1605 cupsdEndProcess(getpid(), 0);
1608 BrowseSLPRefresh
= 0;
1611 BrowseSLPHandle
= NULL
;
1612 #endif /* HAVE_LIBSLP */
1615 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1619 cupsdLogMessage(CUPSD_LOG_ERROR
,
1620 "Need to set BrowseLDAPDN to use LDAP browsing!");
1621 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1622 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1624 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1625 cupsdEndProcess(getpid(), 0);
1630 * Open LDAP handle...
1633 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1634 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1635 cupsdEndProcess(getpid(), 0);
1638 BrowseLDAPRefresh
= 0;
1640 #endif /* HAVE_LDAP */
1643 * Enable LPD and SMB printer sharing as needed through external programs...
1646 if (BrowseLocalProtocols
& BROWSE_LPD
)
1649 if (BrowseLocalProtocols
& BROWSE_SMB
)
1653 * Register the individual printers
1656 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1658 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1659 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1660 cupsdRegisterPrinter(p
);
1665 * 'cupsdStartPolling()' - Start polling servers as needed.
1669 cupsdStartPolling(void)
1671 int i
; /* Looping var */
1672 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1673 char polld
[1024]; /* Poll daemon path */
1674 char sport
[255]; /* Server port */
1675 char bport
[255]; /* Browser port */
1676 char interval
[255]; /* Poll interval */
1677 int statusfds
[2]; /* Status pipe */
1678 char *argv
[6]; /* Arguments */
1679 char *envp
[100]; /* Environment */
1683 * Don't do anything if we aren't polling...
1686 if (NumPolled
== 0 || BrowseSocket
< 0)
1689 PollStatusBuffer
= NULL
;
1694 * Setup string arguments for polld, port and interval options.
1697 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1699 sprintf(bport
, "%d", BrowsePort
);
1702 sprintf(interval
, "%d", BrowseInterval
);
1704 strcpy(interval
, "30");
1706 argv
[0] = "cups-polld";
1712 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1715 * Create a pipe that receives the status messages from each
1719 if (cupsdOpenPipe(statusfds
))
1721 cupsdLogMessage(CUPSD_LOG_ERROR
,
1722 "Unable to create polling status pipes - %s.",
1725 PollStatusBuffer
= NULL
;
1729 PollPipe
= statusfds
[0];
1730 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1733 * Run each polling daemon, redirecting stderr to the polling pipe...
1736 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1738 sprintf(sport
, "%d", pollp
->port
);
1740 argv
[1] = pollp
->hostname
;
1742 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1743 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1745 cupsdLogMessage(CUPSD_LOG_ERROR
,
1746 "cupsdStartPolling: Unable to fork polling daemon - %s",
1752 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1753 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1754 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1757 close(statusfds
[1]);
1760 * Finally, add the pipe to the input selection set...
1763 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1768 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1772 cupsdStopBrowsing(void)
1774 cupsd_printer_t
*p
; /* Current printer */
1777 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1781 * De-register the individual printers
1784 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1786 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1787 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1788 cupsdDeregisterPrinter(p
, 1);
1791 * Shut down browsing sockets...
1794 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1798 * Close the socket and remove it from the input selection set.
1802 closesocket(BrowseSocket
);
1804 close(BrowseSocket
);
1807 cupsdRemoveSelect(BrowseSocket
);
1812 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1816 DNSServiceRefDeallocate(WebIFRef
);
1822 DNSServiceRefDeallocate(RemoteRef
);
1826 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1828 DNSServiceRefDeallocate(DNSSDRef
);
1831 cupsArrayDelete(DNSSDPrinters
);
1832 DNSSDPrinters
= NULL
;
1836 #endif /* HAVE_DNSSD */
1839 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1843 * Close SLP handle...
1846 SLPClose(BrowseSLPHandle
);
1847 BrowseSLPHandle
= NULL
;
1849 #endif /* HAVE_LIBSLP */
1852 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1855 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1856 ldap_disconnect(BrowseLDAPHandle
);
1857 BrowseLDAPHandle
= NULL
;
1859 #endif /* HAVE_OPENLDAP */
1862 * Disable LPD and SMB printer sharing as needed through external programs...
1865 if (BrowseLocalProtocols
& BROWSE_LPD
)
1868 if (BrowseLocalProtocols
& BROWSE_SMB
)
1874 * 'cupsdStopPolling()' - Stop polling servers as needed.
1878 cupsdStopPolling(void)
1880 int i
; /* Looping var */
1881 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1886 cupsdStatBufDelete(PollStatusBuffer
);
1889 cupsdRemoveSelect(PollPipe
);
1892 PollStatusBuffer
= NULL
;
1895 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1897 cupsdEndProcess(pollp
->pid
, 0);
1903 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1907 cupsdUpdateDNSSDName(void)
1909 DNSServiceErrorType error
; /* Error from service creation */
1910 char webif
[1024]; /* Web interface share name */
1911 #ifdef HAVE_COREFOUNDATION_H
1912 CFStringRef nameRef
; /* Computer name CFString */
1913 char nameBuffer
[1024]; /* C-string buffer */
1914 CFStringEncoding nameEncoding
; /* Computer name encoding */
1915 #endif /* HAVE_COREFOUNDATION_H */
1919 * Only share the web interface and printers when non-local listening is
1927 * Get the computer name as a c-string...
1930 #ifdef HAVE_COREFOUNDATION_H
1931 cupsdClearString(&DNSSDName
);
1933 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1934 &nameEncoding
)) != NULL
)
1936 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1937 kCFStringEncodingUTF8
))
1938 cupsdSetString(&DNSSDName
, nameBuffer
);
1944 cupsdSetString(&DNSSDName
, ServerName
);
1945 #endif /* HAVE_COREFOUNDATION_H */
1948 * Then (re)register the web interface...
1952 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1954 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1957 DNSServiceRefDeallocate(WebIFRef
);
1959 WebIFRef
= DNSSDRef
;
1960 if ((error
= DNSServiceRegister(&WebIFRef
,
1961 kDNSServiceFlagsShareConnection
,
1962 0, webif
, "_http._tcp", NULL
,
1963 NULL
, htons(DNSSDPort
), 7,
1964 "\006path=/", dnssdRegisterCallback
,
1965 NULL
)) != kDNSServiceErr_NoError
)
1966 cupsdLogMessage(CUPSD_LOG_ERROR
,
1967 "DNS-SD web interface registration failed: %d", error
);
1969 #endif /* HAVE_DNSSD */
1974 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1978 cupsdUpdateLDAPBrowse(void)
1980 char uri
[HTTP_MAX_URI
], /* Printer URI */
1981 host
[HTTP_MAX_URI
], /* Hostname */
1982 resource
[HTTP_MAX_URI
], /* Resource path */
1983 location
[1024], /* Printer location */
1984 info
[1024], /* Printer information */
1985 make_model
[1024], /* Printer make and model */
1986 type_num
[30]; /* Printer type number */
1987 int type
; /* Printer type */
1988 int rc
; /* LDAP status */
1989 int limit
; /* Size limit */
1990 LDAPMessage
*res
, /* LDAP search results */
1991 *e
; /* Current entry from search */
1993 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1995 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1998 * Reconnect if LDAP Handle is invalid...
2001 if (! BrowseLDAPHandle
)
2008 * Search for cups printers in LDAP directory...
2011 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2012 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2015 * If ldap search was successfull then exit function
2016 * and temporary disable LDAP updates...
2019 if (rc
!= LDAP_SUCCESS
)
2021 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2023 BrowseLDAPUpdate
= FALSE
;
2024 cupsdLogMessage(CUPSD_LOG_INFO
,
2025 "LDAP update temporary disabled");
2031 * If LDAP updates were disabled, we will reenable them...
2034 if (! BrowseLDAPUpdate
)
2036 BrowseLDAPUpdate
= TRUE
;
2037 cupsdLogMessage(CUPSD_LOG_INFO
,
2038 "LDAP update enabled");
2042 * Count LDAP entries and return if no entry exist...
2045 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2046 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2054 * Loop through the available printers...
2057 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2059 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2062 * Get the required values from this entry...
2065 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2066 "printerDescription", info
, sizeof(info
)) == -1)
2069 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2070 "printerLocation", location
, sizeof(location
)) == -1)
2073 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2074 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2077 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2078 "printerType", type_num
, sizeof(type_num
)) == -1)
2081 type
= atoi(type_num
);
2083 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2084 "printerURI", uri
, sizeof(uri
)) == -1)
2088 * Process the entry as browse data...
2091 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2092 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2093 location
, info
, make_model
, 0, NULL
);
2099 #endif /* HAVE_LDAP */
2104 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2108 cupsdUpdateSLPBrowse(void)
2110 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2111 *next
; /* Next service in list */
2112 cupsd_printer_t p
; /* Printer information */
2113 const char *uri
; /* Pointer to printer URI */
2114 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2115 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2119 * Reset the refresh time...
2122 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2125 * Poll for remote printers using SLP...
2130 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2131 slp_url_callback
, &s
);
2134 * Loop through the list of available printers...
2140 * Save the "next" pointer...
2146 * Load a cupsd_printer_t structure with the SLP service attributes...
2149 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2152 * Process this printer entry...
2155 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2157 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2160 * Pull the URI apart to see if this is a local or remote printer...
2163 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2164 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2165 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2169 * Free this listing...
2172 cupsdClearString(&p
.info
);
2173 cupsdClearString(&p
.location
);
2174 cupsdClearString(&p
.make_model
);
2179 #endif /* HAVE_LIBSLP */
2183 * 'dequote()' - Remote quotes from a string.
2186 static char * /* O - Dequoted string */
2187 dequote(char *d
, /* I - Destination string */
2188 const char *s
, /* I - Source string */
2189 int dlen
) /* I - Destination length */
2191 char *dptr
; /* Pointer into destination */
2196 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2214 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2217 static char * /* O - TXT record */
2218 dnssdBuildTxtRecord(
2219 int *txt_len
, /* O - TXT record length */
2220 cupsd_printer_t
*p
, /* I - Printer information */
2221 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2223 int i
, j
; /* Looping vars */
2224 char type_str
[32], /* Type to string buffer */
2225 state_str
[32], /* State to string buffer */
2226 rp_str
[1024], /* Queue name string buffer */
2227 air_str
[1024], /* auth-info-required string buffer */
2228 *keyvalue
[32][2]; /* Table of key/value pairs */
2232 * Load up the key value pairs...
2237 keyvalue
[i
][0] = "txtvers";
2238 keyvalue
[i
++][1] = "1";
2240 keyvalue
[i
][0] = "qtotal";
2241 keyvalue
[i
++][1] = "1";
2243 keyvalue
[i
][0] = "rp";
2244 keyvalue
[i
++][1] = rp_str
;
2246 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2248 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2249 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2251 keyvalue
[i
][0] = "ty";
2252 keyvalue
[i
++][1] = p
->make_model
;
2254 if (p
->location
&& *p
->location
!= '\0')
2256 keyvalue
[i
][0] = "note";
2257 keyvalue
[i
++][1] = p
->location
;
2260 keyvalue
[i
][0] = "priority";
2261 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2263 keyvalue
[i
][0] = "product";
2264 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2266 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2267 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2269 keyvalue
[i
][0] = "printer-state";
2270 keyvalue
[i
++][1] = state_str
;
2272 keyvalue
[i
][0] = "printer-type";
2273 keyvalue
[i
++][1] = type_str
;
2275 keyvalue
[i
][0] = "Transparent";
2276 keyvalue
[i
++][1] = "T";
2278 keyvalue
[i
][0] = "Binary";
2279 keyvalue
[i
++][1] = "T";
2281 if ((p
->type
& CUPS_PRINTER_FAX
))
2283 keyvalue
[i
][0] = "Fax";
2284 keyvalue
[i
++][1] = "T";
2287 if ((p
->type
& CUPS_PRINTER_COLOR
))
2289 keyvalue
[i
][0] = "Color";
2290 keyvalue
[i
++][1] = "T";
2293 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2295 keyvalue
[i
][0] = "Duplex";
2296 keyvalue
[i
++][1] = "T";
2299 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2301 keyvalue
[i
][0] = "Staple";
2302 keyvalue
[i
++][1] = "T";
2305 if ((p
->type
& CUPS_PRINTER_COPIES
))
2307 keyvalue
[i
][0] = "Copies";
2308 keyvalue
[i
++][1] = "T";
2311 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2313 keyvalue
[i
][0] = "Collate";
2314 keyvalue
[i
++][1] = "T";
2317 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2319 keyvalue
[i
][0] = "Punch";
2320 keyvalue
[i
++][1] = "T";
2323 if ((p
->type
& CUPS_PRINTER_BIND
))
2325 keyvalue
[i
][0] = "Bind";
2326 keyvalue
[i
++][1] = "T";
2329 if ((p
->type
& CUPS_PRINTER_SORT
))
2331 keyvalue
[i
][0] = "Sort";
2332 keyvalue
[i
++][1] = "T";
2335 keyvalue
[i
][0] = "pdl";
2336 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2338 if (p
->num_auth_info_required
)
2340 char *air
= air_str
; /* Pointer into string */
2343 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2345 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2351 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2355 keyvalue
[i
][0] = "air";
2356 keyvalue
[i
++][1] = air_str
;
2360 * Then pack them into a proper txt record...
2363 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2368 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2371 static int /* O - Result of comparison */
2372 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2373 cupsd_printer_t
*b
)/* I - Second printer */
2375 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2380 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2385 dnssdDeregisterPrinter(
2386 cupsd_printer_t
*p
) /* I - Printer */
2388 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2391 * Closing the socket deregisters the service
2396 DNSServiceRefDeallocate(p
->ipp_ref
);
2400 cupsArrayRemove(DNSSDPrinters
, p
);
2401 cupsdClearString(&p
->reg_name
);
2406 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2416 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2417 * TXT record format.
2420 static char * /* O - TXT record */
2421 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2422 char *keyvalue
[][2], /* I - Table of key value pairs */
2423 int count
) /* I - Items in table */
2425 int i
; /* Looping var */
2426 int length
; /* Length of TXT record */
2427 int length2
; /* Length of value */
2428 char *txtRecord
; /* TXT record buffer */
2429 char *cursor
; /* Looping pointer */
2433 * Calculate the buffer size
2436 for (length
= i
= 0; i
< count
; i
++)
2437 length
+= 1 + strlen(keyvalue
[i
][0]) +
2438 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2441 * Allocate and fill it
2444 txtRecord
= malloc(length
);
2449 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2452 * Drop in the p-string style length byte followed by the data
2455 length
= strlen(keyvalue
[i
][0]);
2456 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2458 *cursor
++ = (unsigned char)(length
+ length2
);
2460 memcpy(cursor
, keyvalue
[i
][0], length
);
2467 memcpy(cursor
, keyvalue
[i
][1], length2
);
2478 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2482 dnssdRegisterCallback(
2483 DNSServiceRef sdRef
, /* I - DNS Service reference */
2484 DNSServiceFlags flags
, /* I - Reserved for future use */
2485 DNSServiceErrorType errorCode
, /* I - Error code */
2486 const char *name
, /* I - Service name */
2487 const char *regtype
, /* I - Service type */
2488 const char *domain
, /* I - Domain. ".local" for now */
2489 void *context
) /* I - User-defined context */
2491 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2492 /* Current printer */
2495 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2496 name
, regtype
, p
? p
->name
: "Web Interface");
2500 cupsdLogMessage(CUPSD_LOG_ERROR
,
2501 "DNSServiceRegister failed with error %d", (int)errorCode
);
2504 else if (p
&& strcasecmp(name
, p
->reg_name
))
2506 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2509 cupsArrayRemove(DNSSDPrinters
, p
);
2510 cupsdSetString(&p
->reg_name
, name
);
2511 cupsArrayAdd(DNSSDPrinters
, p
);
2513 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2519 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2520 * or update the broadcast contents.
2524 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2526 DNSServiceErrorType se
; /* dnssd errors */
2527 char *ipp_txt
, /* IPP TXT record buffer */
2528 *printer_txt
, /* LPD TXT record buffer */
2529 name
[1024], /* Service name */
2530 *nameptr
; /* Pointer into name */
2531 int ipp_len
, /* IPP TXT record length */
2532 printer_len
; /* LPD TXT record length */
2533 char resource
[1024]; /* Resource path for printer */
2534 const char *regtype
; /* Registration type */
2535 const char *domain
; /* Registration domain */
2536 cupsd_location_t
*location
, /* Printer location */
2537 *policy
; /* Operation policy for Print-Job */
2538 unsigned address
[4]; /* INADDR_ANY address */
2541 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2542 !p
->ipp_ref
? "new" : "update");
2545 * If per-printer sharing was just disabled make sure we're not
2546 * registered before returning.
2551 dnssdDeregisterPrinter(p
);
2556 * The registered name takes the form of "<printer-info> @ <computer name>"...
2559 if (p
->info
&& strlen(p
->info
) > 0)
2562 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2564 strlcpy(name
, p
->info
, sizeof(name
));
2567 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2569 strlcpy(name
, p
->name
, sizeof(name
));
2572 * If an existing printer was renamed, unregister it and start over...
2575 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2576 dnssdDeregisterPrinter(p
);
2580 cupsdSetString(&p
->reg_name
, name
);
2581 cupsArrayAdd(DNSSDPrinters
, p
);
2585 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2586 * let dnssd decide on the domain, otherwise restrict it to ".local".
2589 if (p
->type
& CUPS_PRINTER_CLASS
)
2590 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2592 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2594 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2595 location
= cupsdFindBest(resource
, HTTP_POST
);
2596 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2598 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2599 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2605 * Register IPP and (optionally) LPD...
2608 ipp_len
= 0; /* anti-compiler-warning-code */
2609 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2614 * Initial registration. Use the _fax subtype for fax queues...
2617 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2620 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2621 "Registering DNS-SD printer %s with name \"%s\", "
2622 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2623 domain
? domain
: "(null)");
2626 * Register the queue, dropping characters as needed until we succeed...
2629 nameptr
= name
+ strlen(name
);
2633 p
->ipp_ref
= DNSSDRef
;
2634 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2635 0, name
, regtype
, domain
, NULL
,
2636 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2637 dnssdRegisterCallback
,
2638 p
)) == kDNSServiceErr_BadParam
)
2641 * Name is too long, drop trailing characters, taking into account
2647 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2654 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2656 if (se
== kDNSServiceErr_NoError
)
2658 p
->ipp_txt
= ipp_txt
;
2659 p
->ipp_len
= ipp_len
;
2663 cupsdLogMessage(CUPSD_LOG_WARN
,
2664 "DNS-SD IPP registration of \"%s\" failed: %d",
2667 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2670 * Update the existing registration...
2673 /* A TTL of 0 means use record's original value (Radar 3176248) */
2674 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2679 p
->ipp_txt
= ipp_txt
;
2680 p
->ipp_len
= ipp_len
;
2687 if (BrowseLocalProtocols
& BROWSE_LPD
)
2689 printer_len
= 0; /* anti-compiler-warning-code */
2690 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2692 if (!p
->printer_ref
)
2695 * Initial registration...
2698 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2699 "Registering DNS-SD printer %s with name \"%s\", "
2700 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2701 name
, domain
? domain
: "(null)");
2703 p
->printer_ref
= DNSSDRef
;
2704 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2705 kDNSServiceFlagsShareConnection
,
2706 0, name
, "_printer._tcp", domain
, NULL
,
2707 htons(515), printer_len
, printer_txt
,
2708 dnssdRegisterCallback
,
2709 p
)) == kDNSServiceErr_NoError
)
2711 p
->printer_txt
= printer_txt
;
2712 p
->printer_len
= printer_len
;
2716 cupsdLogMessage(CUPSD_LOG_WARN
,
2717 "DNS-SD LPD registration of \"%s\" failed: %d",
2720 else if (printer_len
!= p
->printer_len
||
2721 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2724 * Update the existing registration...
2727 /* A TTL of 0 means use record's original value (Radar 3176248) */
2728 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2732 free(p
->printer_txt
);
2734 p
->printer_txt
= printer_txt
;
2735 p
->printer_len
= printer_len
;
2746 * 'dnssdUpdate()' - Handle DNS-SD queries.
2752 DNSServiceErrorType sdErr
; /* Service discovery error */
2755 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2756 cupsdLogMessage(CUPSD_LOG_ERROR
,
2757 "DNS Service Discovery registration error %d!",
2760 #endif /* HAVE_DNSSD */
2765 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2768 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2769 get_hostconfig(const char *name
) /* I - Name of service */
2771 cups_file_t
*fp
; /* Hostconfig file */
2772 char line
[1024], /* Line from file */
2773 *ptr
; /* Pointer to value */
2774 int state
= 1; /* State of service */
2778 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2779 * the service is enabled/auto.
2782 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2785 * Read lines from the file until we find the service...
2788 while (cupsFileGets(fp
, line
, sizeof(line
)))
2790 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2795 if (!strcasecmp(line
, name
))
2798 * Found the service, see if it is set to "-NO-"...
2801 if (!strncasecmp(ptr
, "-NO-", 4))
2812 #endif /* __APPLE__ */
2816 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2819 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2820 is_local_queue(const char *uri
, /* I - Printer URI */
2821 char *host
, /* O - Host string */
2822 int hostlen
, /* I - Length of host buffer */
2823 char *resource
, /* O - Resource string */
2824 int resourcelen
) /* I - Length of resource buffer */
2826 char scheme
[32], /* Scheme portion of URI */
2827 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2828 int port
; /* Port portion of URI */
2829 cupsd_netif_t
*iface
; /* Network interface */
2833 * Pull the URI apart to see if this is a local or remote printer...
2836 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2837 username
, sizeof(username
), host
, hostlen
, &port
,
2838 resource
, resourcelen
) < HTTP_URI_OK
)
2841 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2844 * Check for local server addresses...
2847 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2852 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2854 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2855 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2859 * If we get here, the printer is remote...
2867 * 'process_browse_data()' - Process new browse data.
2871 process_browse_data(
2872 const char *uri
, /* I - URI of printer/class */
2873 const char *host
, /* I - Hostname */
2874 const char *resource
, /* I - Resource path */
2875 cups_ptype_t type
, /* I - Printer type */
2876 ipp_pstate_t state
, /* I - Printer state */
2877 const char *location
, /* I - Printer location */
2878 const char *info
, /* I - Printer information */
2879 const char *make_model
, /* I - Printer make and model */
2880 int num_attrs
, /* I - Number of attributes */
2881 cups_option_t
*attrs
) /* I - Attributes */
2883 int i
; /* Looping var */
2884 int update
; /* Update printer attributes? */
2885 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2886 name
[IPP_MAX_NAME
], /* Name of printer */
2887 newname
[IPP_MAX_NAME
], /* New name of printer */
2888 *hptr
, /* Pointer into hostname */
2889 *sptr
; /* Pointer into ServerName */
2890 const char *shortname
; /* Short queue name (queue) */
2891 char local_make_model
[IPP_MAX_NAME
];
2892 /* Local make and model */
2893 cupsd_printer_t
*p
; /* Printer information */
2894 const char *ipp_options
, /* ipp-options value */
2895 *lease_duration
; /* lease-duration value */
2896 int is_class
; /* Is this queue a class? */
2899 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2900 "process_browse_data(uri=\"%s\", host=\"%s\", "
2901 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2902 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2903 uri
, host
, resource
, type
, state
,
2904 location
? location
: "(nil)", info
? info
: "(nil)",
2905 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2908 * Determine if the URI contains any illegal characters in it...
2911 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2912 (strncmp(resource
, "/printers/", 10) &&
2913 strncmp(resource
, "/classes/", 9)))
2915 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2919 if (strchr(resource
, '?') ||
2920 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2921 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2923 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2929 * OK, this isn't a local printer; add any remote options...
2932 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2934 if (BrowseRemoteOptions
)
2936 if (BrowseRemoteOptions
[0] == '?')
2939 * Override server-supplied options...
2942 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2944 else if (ipp_options
)
2947 * Combine the server and local options...
2950 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2951 BrowseRemoteOptions
);
2956 * Just use the local options...
2959 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2964 else if (ipp_options
)
2967 * Just use the server-supplied options...
2970 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2975 * See if we already have it listed in the Printers list, and add it if not...
2978 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2979 type
&= ~CUPS_PRINTER_IMPLICIT
;
2981 hptr
= strchr(host
, '.');
2982 sptr
= strchr(ServerName
, '.');
2983 is_class
= type
& CUPS_PRINTER_CLASS
;
2985 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2988 * Strip the common domain name components...
2991 while (hptr
!= NULL
)
2993 if (!strcasecmp(hptr
, sptr
))
2999 hptr
= strchr(hptr
+ 1, '.');
3006 * Remote destination is a class...
3009 if (!strncmp(resource
, "/classes/", 9))
3010 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3014 shortname
= resource
+ 9;
3019 * Remote destination is a printer...
3022 if (!strncmp(resource
, "/printers/", 10))
3023 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3027 shortname
= resource
+ 10;
3031 *hptr
= '.'; /* Resource FQDN */
3033 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3036 * Long name doesn't exist, try short name...
3039 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3042 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3045 * Short name doesn't exist, use it for this shared queue.
3048 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3050 strlcpy(name
, shortname
, sizeof(name
));
3055 * Short name exists...
3058 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3059 "process_browse_data: %s found, type=%x, hostname=%s...",
3060 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3062 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3063 p
= NULL
; /* Don't replace implicit classes */
3064 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3067 * Short name exists but is for a different host. If this is a remote
3068 * queue, rename it and use the long name...
3071 if (p
->type
& CUPS_PRINTER_REMOTE
)
3073 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3074 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3075 is_class
? "class" : "printer", p
->name
, p
->name
,
3077 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3078 "%s \'%s\' deleted by directory services.",
3079 is_class
? "Class" : "Printer", p
->name
);
3081 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3082 cupsdRenamePrinter(p
, newname
);
3084 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3085 "%s \'%s\' added by directory services.",
3086 is_class
? "Class" : "Printer", p
->name
);
3090 * Force creation with long name...
3098 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3099 "process_browse_data: %s found, type=%x, hostname=%s...",
3100 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3105 * Queue doesn't exist; add it...
3109 p
= cupsdAddClass(name
);
3111 p
= cupsdAddPrinter(name
);
3113 cupsdClearString(&(p
->hostname
));
3115 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3116 is_class
? "class" : "printer", name
);
3118 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3119 "%s \'%s\' added by directory services.",
3120 is_class
? "Class" : "Printer", name
);
3123 * Force the URI to point to the real server...
3126 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3129 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3138 * Hostname not set, so this must be a cached remote printer
3139 * that was created for a pending print job...
3142 cupsdSetString(&p
->hostname
, host
);
3143 cupsdSetString(&p
->uri
, uri
);
3144 cupsdSetString(&p
->device_uri
, uri
);
3147 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3151 * Update the state...
3155 p
->browse_time
= time(NULL
);
3157 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3161 * Grab the lease-duration for the browse data; anything less then 1
3162 * second or more than 1 week gets the default BrowseTimeout...
3165 i
= atoi(lease_duration
);
3166 if (i
< 1 || i
> 604800)
3169 p
->browse_expire
= p
->browse_time
+ i
;
3172 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3174 if (type
& CUPS_PRINTER_REJECTING
)
3176 type
&= ~CUPS_PRINTER_REJECTING
;
3184 else if (!p
->accepting
)
3190 if (p
->type
!= type
)
3196 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3198 cupsdSetString(&p
->location
, location
);
3202 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3204 cupsdSetString(&p
->info
, info
);
3207 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3210 if (!make_model
|| !make_model
[0])
3213 snprintf(local_make_model
, sizeof(local_make_model
),
3214 "Remote Class on %s", host
);
3216 snprintf(local_make_model
, sizeof(local_make_model
),
3217 "Remote Printer on %s", host
);
3220 snprintf(local_make_model
, sizeof(local_make_model
),
3221 "%s on %s", make_model
, host
);
3223 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3225 cupsdSetString(&p
->make_model
, local_make_model
);
3231 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3234 * See if we need to update the attributes...
3237 if (p
->num_options
!= num_attrs
)
3241 for (i
= 0; i
< num_attrs
; i
++)
3242 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3243 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3244 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3253 * Free the old options...
3256 cupsFreeOptions(p
->num_options
, p
->options
);
3259 p
->num_options
= num_attrs
;
3262 if (type
& CUPS_PRINTER_DELETE
)
3264 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3265 "%s \'%s\' deleted by directory services.",
3266 is_class
? "Class" : "Printer", p
->name
);
3268 cupsdExpireSubscriptions(p
, NULL
);
3270 cupsdDeletePrinter(p
, 1);
3271 cupsdUpdateImplicitClasses();
3272 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3276 cupsdSetPrinterAttrs(p
);
3277 cupsdUpdateImplicitClasses();
3281 * See if we have a default printer... If not, make the first network
3282 * default printer the default.
3285 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3288 * Find the first network default printer and use it...
3291 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3293 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3294 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3297 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3303 * Do auto-classing if needed...
3306 process_implicit_classes();
3311 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3315 process_implicit_classes(void)
3317 int i
; /* Looping var */
3318 int update
; /* Update printer attributes? */
3319 char name
[IPP_MAX_NAME
], /* Name of printer */
3320 *hptr
; /* Pointer into hostname */
3321 cupsd_printer_t
*p
, /* Printer information */
3322 *pclass
, /* Printer class */
3323 *first
; /* First printer in class */
3324 int offset
, /* Offset of name */
3325 len
; /* Length of name */
3328 if (!ImplicitClasses
|| !Printers
)
3332 * Loop through all available printers and create classes as needed...
3335 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3336 update
= 0, pclass
= NULL
, first
= NULL
;
3338 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3341 * Skip implicit classes...
3344 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3351 * If len == 0, get the length of this printer name up to the "@"
3355 cupsArraySave(Printers
);
3358 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3359 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3362 * We have more than one printer with the same name; see if
3363 * we have a class, and if this printer is a member...
3366 if (pclass
&& strcasecmp(pclass
->name
, name
))
3369 cupsdSetPrinterAttrs(pclass
);
3375 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3378 * Need to add the class...
3381 pclass
= cupsdAddPrinter(name
);
3382 cupsArrayAdd(ImplicitPrinters
, pclass
);
3384 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3385 pclass
->accepting
= 1;
3386 pclass
->state
= IPP_PRINTER_IDLE
;
3388 cupsdSetString(&pclass
->location
, p
->location
);
3389 cupsdSetString(&pclass
->info
, p
->info
);
3391 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3392 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3396 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3398 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3400 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3401 "Implicit class \'%s\' added by directory services.",
3407 for (i
= 0; i
< pclass
->num_printers
; i
++)
3408 if (pclass
->printers
[i
] == first
)
3411 if (i
>= pclass
->num_printers
)
3413 first
->in_implicit_class
= 1;
3414 cupsdAddPrinterToClass(pclass
, first
);
3420 for (i
= 0; i
< pclass
->num_printers
; i
++)
3421 if (pclass
->printers
[i
] == p
)
3424 if (i
>= pclass
->num_printers
)
3426 p
->in_implicit_class
= 1;
3427 cupsdAddPrinterToClass(pclass
, p
);
3434 * First time around; just get name length and mark it as first
3438 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3439 len
= hptr
- p
->name
;
3441 len
= strlen(p
->name
);
3443 strncpy(name
, p
->name
, len
);
3447 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3448 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3451 * Can't use same name as a local printer; add "Any" to the
3452 * front of the name, unless we have explicitly disabled
3453 * the "ImplicitAnyClasses"...
3456 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3459 * Add "Any" to the class name...
3462 strcpy(name
, "Any");
3463 strncpy(name
+ 3, p
->name
, len
);
3464 name
[len
+ 3] = '\0';
3470 * Don't create an implicit class if we have a local printer
3471 * with the same name...
3475 cupsArrayRestore(Printers
);
3483 cupsArrayRestore(Printers
);
3487 * Update the last printer class as needed...
3490 if (pclass
&& update
)
3491 cupsdSetPrinterAttrs(pclass
);
3496 * 'send_cups_browse()' - Send new browsing information using the CUPS
3501 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3503 int i
; /* Looping var */
3504 cups_ptype_t type
; /* Printer type */
3505 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3506 int bytes
; /* Length of packet */
3507 char packet
[1453], /* Browse data packet */
3508 uri
[1024], /* Printer URI */
3509 location
[1024], /* printer-location */
3510 info
[1024], /* printer-info */
3512 /* printer-make-and-model */
3513 cupsd_netif_t
*iface
; /* Network interface */
3517 * Figure out the printer type value...
3520 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3523 type
|= CUPS_PRINTER_REJECTING
;
3525 if (p
== DefaultPrinter
)
3526 type
|= CUPS_PRINTER_DEFAULT
;
3529 * Remove quotes from printer-info, printer-location, and
3530 * printer-make-and-model attributes...
3533 dequote(location
, p
->location
, sizeof(location
));
3534 dequote(info
, p
->info
, sizeof(info
));
3537 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3538 else if (p
->type
& CUPS_PRINTER_CLASS
)
3540 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3541 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3543 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3546 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3548 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3551 * Send a packet to each browse address...
3554 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3558 * Send the browse packet to one or more interfaces...
3561 if (!strcmp(b
->iface
, "*"))
3564 * Send to all local interfaces...
3569 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3571 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3574 * Only send to local, IPv4 interfaces...
3577 if (!iface
->is_local
|| !iface
->port
||
3578 iface
->address
.addr
.sa_family
!= AF_INET
)
3581 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3582 iface
->hostname
, iface
->port
,
3583 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3586 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3587 type
, p
->state
, uri
, location
, info
, make_model
,
3588 p
->browse_attrs
? p
->browse_attrs
: "");
3590 bytes
= strlen(packet
);
3592 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3593 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3594 iface
->name
, packet
);
3596 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3598 sendto(BrowseSocket
, packet
, bytes
, 0,
3599 (struct sockaddr
*)&(iface
->broadcast
),
3600 httpAddrLength(&(iface
->broadcast
)));
3603 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3606 * Send to the named interface using the IPv4 address...
3610 if (strcmp(b
->iface
, iface
->name
))
3615 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3618 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3622 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3623 iface
->hostname
, iface
->port
,
3624 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3627 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3628 type
, p
->state
, uri
, location
, info
, make_model
,
3629 p
->browse_attrs
? p
->browse_attrs
: "");
3631 bytes
= strlen(packet
);
3633 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3634 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3635 iface
->name
, packet
);
3637 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3639 sendto(BrowseSocket
, packet
, bytes
, 0,
3640 (struct sockaddr
*)&(iface
->broadcast
),
3641 httpAddrLength(&(iface
->broadcast
)));
3648 * Send the browse packet to the indicated address using
3649 * the default server name...
3652 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3653 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3654 p
->browse_attrs
? p
->browse_attrs
: "");
3656 bytes
= strlen(packet
);
3657 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3658 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3660 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3661 (struct sockaddr
*)&(b
->to
),
3662 httpAddrLength(&(b
->to
))) <= 0)
3665 * Unable to send browse packet, so remove this address from the
3669 cupsdLogMessage(CUPSD_LOG_ERROR
,
3670 "cupsdSendBrowseList: sendto failed for browser "
3672 (int)(b
- Browsers
+ 1), strerror(errno
));
3675 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3686 * 'ldap_search_rec()' - LDAP Search with reconnect
3690 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3691 char *base
, /* I - Base dn */
3692 int scope
, /* I - LDAP search scope */
3693 char *filter
, /* I - Filter string */
3694 char *attrs
[], /* I - Requested attributes */
3695 int attrsonly
, /* I - Return only attributes? */
3696 LDAPMessage
**res
) /* I - LDAP handler */
3698 int rc
; /* Return code */
3701 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3702 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3703 NULL
, LDAP_NO_LIMIT
, res
);
3705 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3706 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3709 * If we have a connection problem try again...
3712 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3714 cupsdLogMessage(CUPSD_LOG_ERROR
,
3715 "LDAP search failed with status %d: %s",
3716 rc
, ldap_err2string(rc
));
3717 cupsdLogMessage(CUPSD_LOG_INFO
,
3718 "We try the LDAP search once again after reconnecting to "
3723 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3724 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3725 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3727 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3728 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3731 if (rc
== LDAP_NO_SUCH_OBJECT
)
3732 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3733 "ldap_search_rec: LDAP entry/object not found");
3734 else if (rc
!= LDAP_SUCCESS
)
3735 cupsdLogMessage(CUPSD_LOG_ERROR
,
3736 "ldap_search_rec: LDAP search failed with status %d: %s",
3737 rc
, ldap_err2string(rc
));
3739 if (rc
!= LDAP_SUCCESS
)
3747 * 'ldap_freeres()' - Free LDAPMessage
3751 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3753 int rc
; /* Return value */
3756 rc
= ldap_msgfree(entry
);
3758 cupsdLogMessage(CUPSD_LOG_WARN
,
3759 "Can't free LDAPMessage!");
3761 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3762 "Freeing LDAPMessage was unnecessary");
3767 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3771 ldap_getval_firststring(
3772 LDAP
*ld
, /* I - LDAP handler */
3773 LDAPMessage
*entry
, /* I - LDAP message or search result */
3774 char *attr
, /* I - the wanted attribute */
3775 char *retval
, /* O - String to return */
3776 unsigned long maxsize
) /* I - Max string size */
3778 char *dn
; /* LDAP DN */
3779 int rc
= 0; /* Return code */
3780 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3781 struct berval
**bval
; /* LDAP value array */
3782 unsigned long size
; /* String size */
3786 * Get value from LDAPMessage...
3789 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3792 dn
= ldap_get_dn(ld
, entry
);
3793 cupsdLogMessage(CUPSD_LOG_WARN
,
3794 "Failed to get LDAP value %s for %s!",
3802 * Check size and copy value into our string...
3806 if (size
< bval
[0]->bv_len
)
3809 dn
= ldap_get_dn(ld
, entry
);
3810 cupsdLogMessage(CUPSD_LOG_WARN
,
3811 "Attribute %s is too big! (dn: %s)",
3816 size
= bval
[0]->bv_len
;
3818 strlcpy(retval
, bval
[0]->bv_val
, size
);
3819 ldap_value_free_len(bval
);
3822 char **value
; /* LDAP value */
3825 * Get value from LDAPMessage...
3828 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3831 dn
= ldap_get_dn(ld
, entry
);
3832 cupsdLogMessage(CUPSD_LOG_WARN
,
3833 "Failed to get LDAP value %s for %s!",
3839 strlcpy(retval
, *value
, maxsize
);
3840 ldap_value_free(value
);
3842 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3849 * 'send_ldap_ou()' - Send LDAP ou registrations.
3853 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3854 char *basedn
, /* I - Our base dn */
3855 char *descstring
) /* I - Description for ou */
3857 int i
; /* Looping var... */
3858 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3859 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3860 LDAPMessage
*res
, /* Search result token */
3861 *e
; /* Current entry from search */
3862 int rc
; /* LDAP status */
3863 char dn
[1024], /* DN of the organizational unit we are adding */
3864 *desc
[2], /* Change records */
3866 char old_desc
[1024]; /* Old description */
3867 static const char * const objectClass_values
[] =
3868 { /* The 2 objectClass's we use in */
3869 "top", /* our LDAP entries */
3870 "organizationalUnit",
3873 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3880 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3883 * Reconnect if LDAP Handle is invalid...
3886 if (! BrowseLDAPHandle
)
3888 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3889 "send_ldap_ou: LDAP Handle is invalid. Try "
3896 * Prepare ldap search...
3899 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3900 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3904 desc
[0] = descstring
;
3907 mods
[0].mod_type
= "ou";
3908 mods
[0].mod_values
= ou_value
;
3909 mods
[1].mod_type
= "description";
3910 mods
[1].mod_values
= desc
;
3911 mods
[2].mod_type
= "objectClass";
3912 mods
[2].mod_values
= (char **)objectClass_values
;
3914 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3915 (char **)ou_attrs
, 0, &res
);
3918 * If ldap search was not successfull then exit function...
3921 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3925 * Check if we need to insert or update the LDAP entry...
3928 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3929 rc
!= LDAP_NO_SUCH_OBJECT
)
3932 * Printserver has already been registered, check if
3933 * modification is required...
3936 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
3939 * Get the required values from this entry...
3942 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
3943 sizeof(old_desc
)) == -1)
3947 * Check if modification is required...
3950 if ( strcmp(desc
[0], old_desc
) == 0 )
3953 * LDAP entry for the printer exists.
3954 * Printer has already been registered,
3955 * no modifications required...
3957 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3958 "send_ldap_ou: No updates required for %s", ou
);
3963 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3964 "send_ldap_ou: Replace entry for %s", ou
);
3966 for (i
= 0; i
< 3; i
++)
3968 pmods
[i
] = mods
+ i
;
3969 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3973 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3974 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3975 NULL
)) != LDAP_SUCCESS
)
3977 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3978 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3980 cupsdLogMessage(CUPSD_LOG_ERROR
,
3981 "LDAP modify for %s failed with status %d: %s",
3982 ou
, rc
, ldap_err2string(rc
));
3983 if ( LDAP_SERVER_DOWN
== rc
)
3991 * Printserver has never been registered,
3992 * add registration...
3995 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3996 "send_ldap_ou: Add entry for %s", ou
);
3998 for (i
= 0; i
< 3; i
++)
4000 pmods
[i
] = mods
+ i
;
4001 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4005 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4006 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4007 NULL
)) != LDAP_SUCCESS
)
4009 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4010 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4012 cupsdLogMessage(CUPSD_LOG_ERROR
,
4013 "LDAP add for %s failed with status %d: %s",
4014 ou
, rc
, ldap_err2string(rc
));
4015 if ( LDAP_SERVER_DOWN
== rc
)
4025 * 'send_ldap_browse()' - Send LDAP printer registrations.
4029 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4031 int i
; /* Looping var... */
4032 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4033 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4034 LDAPMessage
*res
, /* Search result token */
4035 *e
; /* Current entry from search */
4036 char *cn_value
[2], /* Change records */
4042 typestring
[255], /* String to hold printer-type */
4043 dn
[1024]; /* DN of the printer we are adding */
4044 int rc
; /* LDAP status */
4045 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4046 old_location
[1024], /* Printer location */
4047 old_info
[1024], /* Printer information */
4048 old_make_model
[1024], /* Printer make and model */
4049 old_type_string
[30]; /* Temporary type number */
4050 int old_type
; /* Printer type */
4051 static const char * const objectClass_values
[] =
4052 { /* The 3 objectClass's we use in */
4053 "top", /* our LDAP entries */
4060 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4063 * Exit function if LDAP updates has been disabled...
4066 if (!BrowseLDAPUpdate
)
4068 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4069 "send_ldap_browse: Updates temporary disabled; "
4075 * Reconnect if LDAP Handle is invalid...
4078 if (!BrowseLDAPHandle
)
4080 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4081 "send_ldap_browse: LDAP Handle is invalid. Try "
4088 * Everything in ldap is ** so we fudge around it...
4091 sprintf(typestring
, "%u", p
->type
);
4093 cn_value
[0] = p
->name
;
4095 info
[0] = p
->info
? p
->info
: "Unknown";
4097 location
[0] = p
->location
? p
->location
: "Unknown";
4099 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4100 make_model
[1] = NULL
;
4101 type
[0] = typestring
;
4107 * Get ldap entry for printer ...
4110 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4112 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4114 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4115 (char **)ldap_attrs
, 0, &res
);
4118 * If ldap search was not successfull then exit function
4119 * and temporary disable LDAP updates...
4122 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4124 if (BrowseLDAPUpdate
&&
4125 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4127 BrowseLDAPUpdate
= FALSE
;
4128 cupsdLogMessage(CUPSD_LOG_INFO
,
4129 "LDAP update temporary disabled");
4136 * Fill modification array...
4139 mods
[0].mod_type
= "cn";
4140 mods
[0].mod_values
= cn_value
;
4141 mods
[1].mod_type
= "printerDescription";
4142 mods
[1].mod_values
= info
;
4143 mods
[2].mod_type
= "printerURI";
4144 mods
[2].mod_values
= uri
;
4145 mods
[3].mod_type
= "printerLocation";
4146 mods
[3].mod_values
= location
;
4147 mods
[4].mod_type
= "printerMakeAndModel";
4148 mods
[4].mod_values
= make_model
;
4149 mods
[5].mod_type
= "printerType";
4150 mods
[5].mod_values
= type
;
4151 mods
[6].mod_type
= "objectClass";
4152 mods
[6].mod_values
= (char **)objectClass_values
;
4155 * Check if we need to insert or update the LDAP entry...
4158 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4159 rc
!= LDAP_NO_SUCH_OBJECT
)
4162 * Printer has already been registered, check if
4163 * modification is required...
4166 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4169 * Get the required values from this entry...
4172 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4173 old_info
, sizeof(old_info
)) == -1)
4176 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4177 old_location
, sizeof(old_location
)) == -1)
4180 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4181 old_make_model
, sizeof(old_make_model
)) == -1)
4184 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4185 old_type_string
, sizeof(old_type_string
)) == -1)
4188 old_type
= atoi(old_type_string
);
4190 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4191 sizeof(old_uri
)) == -1)
4195 * Check if modification is required...
4198 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4199 !strcmp(location
[0], old_location
) &&
4200 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4203 * LDAP entry for the printer exists. Printer has already been registered,
4204 * no modifications required...
4207 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4208 "send_ldap_browse: No updates required for %s", p
->name
);
4213 * LDAP entry for the printer exists. Printer has already been registered,
4214 * modify the current registration...
4217 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4218 "send_ldap_browse: Replace entry for %s", p
->name
);
4220 for (i
= 0; i
< 7; i
++)
4222 pmods
[i
] = mods
+ i
;
4223 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4227 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4228 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4229 NULL
)) != LDAP_SUCCESS
)
4231 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4232 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4234 cupsdLogMessage(CUPSD_LOG_ERROR
,
4235 "LDAP modify for %s failed with status %d: %s",
4236 p
->name
, rc
, ldap_err2string(rc
));
4237 if (rc
== LDAP_SERVER_DOWN
)
4245 * No LDAP entry exists for the printer. Printer has never been registered,
4246 * add the current registration...
4249 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4251 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4252 "send_ldap_browse: Add entry for %s", p
->name
);
4254 for (i
= 0; i
< 7; i
++)
4256 pmods
[i
] = mods
+ i
;
4257 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4261 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4262 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4263 NULL
)) != LDAP_SUCCESS
)
4265 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4266 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4268 cupsdLogMessage(CUPSD_LOG_ERROR
,
4269 "LDAP add for %s failed with status %d: %s",
4270 p
->name
, rc
, ldap_err2string(rc
));
4271 if (rc
== LDAP_SERVER_DOWN
)
4281 * 'ldap_dereg_printer()' - Delete printer from directory
4285 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4287 char dn
[1024]; /* DN of the printer */
4288 int rc
; /* LDAP status */
4291 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4295 * Reconnect if LDAP Handle is invalid...
4298 if (!BrowseLDAPHandle
)
4305 * Get dn for printer and delete LDAP entry...
4308 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4310 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4312 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4313 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4314 NULL
)) != LDAP_SUCCESS
)
4316 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4317 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4319 cupsdLogMessage(CUPSD_LOG_WARN
,
4320 "LDAP delete for %s failed with status %d: %s",
4321 p
->name
, rc
, ldap_err2string(rc
));
4324 * If we had a connection problem (connection timed out, etc.)
4325 * we should reconnect and try again to delete the entry...
4328 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4330 cupsdLogMessage(CUPSD_LOG_INFO
,
4331 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4334 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4335 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4336 NULL
)) != LDAP_SUCCESS
)
4338 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4339 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4340 cupsdLogMessage(CUPSD_LOG_WARN
,
4341 "LDAP delete for %s failed with status %d: %s",
4342 p
->name
, rc
, ldap_err2string(rc
));
4349 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4350 char *basedn
) /* I - Dase dn */
4352 char dn
[1024]; /* DN of the printer */
4353 int rc
; /* LDAP status */
4356 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4359 * Reconnect if LDAP Handle is invalid...
4362 if (!BrowseLDAPHandle
)
4369 * Get dn for printer and delete LDAP entry...
4372 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4373 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4375 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4376 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4377 NULL
)) != LDAP_SUCCESS
)
4379 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4380 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4382 cupsdLogMessage(CUPSD_LOG_WARN
,
4383 "LDAP delete for %s failed with status %d: %s",
4384 ou
, rc
, ldap_err2string(rc
));
4387 * If we had a connection problem (connection timed out, etc.)
4388 * we should reconnect and try again to delete the entry...
4391 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4393 cupsdLogMessage(CUPSD_LOG_INFO
,
4394 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4396 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4397 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4398 NULL
)) != LDAP_SUCCESS
)
4400 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4401 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4402 cupsdLogMessage(CUPSD_LOG_WARN
,
4403 "LDAP delete for %s failed with status %d: %s",
4404 ou
, rc
, ldap_err2string(rc
));
4409 #endif /* HAVE_LDAP */
4414 * 'send_slp_browse()' - Register the specified printer with SLP.
4418 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4420 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4421 attrs
[8192], /* Printer attributes */
4422 finishings
[1024], /* Finishings to support */
4423 make_model
[IPP_MAX_NAME
* 2],
4424 /* Make and model, quoted */
4425 location
[IPP_MAX_NAME
* 2],
4426 /* Location, quoted */
4427 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4428 *src
, /* Pointer to original string */
4429 *dst
; /* Pointer to destination string */
4430 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4431 SLPError error
; /* SLP error, if any */
4434 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4438 * Make the SLP service URL that conforms to the IANA
4439 * 'printer:' template.
4442 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4444 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4447 * Figure out the finishings string...
4450 if (p
->type
& CUPS_PRINTER_STAPLE
)
4451 strcpy(finishings
, "staple");
4453 finishings
[0] = '\0';
4455 if (p
->type
& CUPS_PRINTER_BIND
)
4458 strlcat(finishings
, ",bind", sizeof(finishings
));
4460 strcpy(finishings
, "bind");
4463 if (p
->type
& CUPS_PRINTER_PUNCH
)
4466 strlcat(finishings
, ",punch", sizeof(finishings
));
4468 strcpy(finishings
, "punch");
4471 if (p
->type
& CUPS_PRINTER_COVER
)
4474 strlcat(finishings
, ",cover", sizeof(finishings
));
4476 strcpy(finishings
, "cover");
4479 if (p
->type
& CUPS_PRINTER_SORT
)
4482 strlcat(finishings
, ",sort", sizeof(finishings
));
4484 strcpy(finishings
, "sort");
4488 strcpy(finishings
, "none");
4491 * Quote any commas in the make and model, location, and info strings...
4494 for (src
= p
->make_model
, dst
= make_model
;
4495 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4497 if (*src
== ',' || *src
== '\\' || *src
== ')')
4506 strcpy(make_model
, "Unknown");
4508 for (src
= p
->location
, dst
= location
;
4509 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4511 if (*src
== ',' || *src
== '\\' || *src
== ')')
4520 strcpy(location
, "Unknown");
4522 for (src
= p
->info
, dst
= info
;
4523 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4525 if (*src
== ',' || *src
== '\\' || *src
== ')')
4534 strcpy(info
, "Unknown");
4537 * Get the authentication value...
4540 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4544 * Make the SLP attribute string list that conforms to
4545 * the IANA 'printer:' template.
4548 snprintf(attrs
, sizeof(attrs
),
4549 "(printer-uri-supported=%s),"
4550 "(uri-authentication-supported=%s>),"
4552 "(uri-security-supported=tls>),"
4554 "(uri-security-supported=none>),"
4555 #endif /* HAVE_SSL */
4556 "(printer-name=%s),"
4557 "(printer-location=%s),"
4558 "(printer-info=%s),"
4559 "(printer-more-info=%s),"
4560 "(printer-make-and-model=%s),"
4561 "(printer-type=%d),"
4562 "(charset-supported=utf-8),"
4563 "(natural-language-configured=%s),"
4564 "(natural-language-supported=de,en,es,fr,it),"
4565 "(color-supported=%s),"
4566 "(finishings-supported=%s),"
4567 "(sides-supported=one-sided%s),"
4568 "(multiple-document-jobs-supported=true)"
4569 "(ipp-versions-supported=1.0,1.1)",
4570 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4571 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4572 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4574 p
->type
& CUPS_PRINTER_DUPLEX
?
4575 ",two-sided-long-edge,two-sided-short-edge" : "");
4577 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4580 * Register the printer with the SLP server...
4583 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4584 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4586 if (error
!= SLP_OK
)
4587 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4593 * 'slp_attr_callback()' - SLP attribute callback
4596 static SLPBoolean
/* O - SLP_TRUE for success */
4598 SLPHandle hslp
, /* I - SLP handle */
4599 const char *attrlist
, /* I - Attribute list */
4600 SLPError errcode
, /* I - Parsing status for this attr */
4601 void *cookie
) /* I - Current printer */
4603 char *tmp
= 0; /* Temporary string */
4604 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4605 /* Current printer */
4608 (void)hslp
; /* anti-compiler-warning-code */
4611 * Bail if there was an error
4614 if (errcode
!= SLP_OK
)
4618 * Parse the attrlist to obtain things needed to build CUPS browse packet
4621 memset(p
, 0, sizeof(cupsd_printer_t
));
4623 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4625 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4627 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4629 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4630 p
->type
= atoi(tmp
);
4632 p
->type
= CUPS_PRINTER_REMOTE
;
4634 cupsdClearString(&tmp
);
4641 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4645 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4647 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4650 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4652 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4655 * Make the SLP service URL that conforms to the IANA
4656 * 'printer:' template.
4659 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4662 * Deregister the printer...
4665 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4671 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4674 static int /* O - 0 on success */
4675 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4676 const char *tag
, /* I - Name of attribute */
4677 char **valbuf
) /* O - Value */
4679 char *ptr1
, /* Pointer into string */
4683 cupsdClearString(valbuf
);
4685 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4687 ptr1
+= strlen(tag
);
4689 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4695 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4696 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4699 * Dequote the value...
4702 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4703 if (*ptr1
== '\\' && ptr1
[1])
4704 _cups_strcpy(ptr1
, ptr1
+ 1);
4715 * 'slp_reg_callback()' - Empty SLPRegReport.
4719 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4720 SLPError errcode
, /* I - Error code, if any */
4721 void *cookie
) /* I - App data */
4732 * 'slp_url_callback()' - SLP service url callback
4735 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4737 SLPHandle hslp
, /* I - SLP handle */
4738 const char *srvurl
, /* I - URL of service */
4739 unsigned short lifetime
, /* I - Life of service */
4740 SLPError errcode
, /* I - Existing error code */
4741 void *cookie
) /* I - Pointer to service list */
4743 slpsrvurl_t
*s
, /* New service entry */
4744 **head
; /* Pointer to head of entry */
4748 * Let the compiler know we won't be using these vars...
4755 * Bail if there was an error
4758 if (errcode
!= SLP_OK
)
4762 * Grab the head of the list...
4765 head
= (slpsrvurl_t
**)cookie
;
4768 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4771 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4775 * Copy the SLP service URL...
4778 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4781 * Link the SLP service URL into the head of the list
4791 #endif /* HAVE_LIBSLP */
4795 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4799 update_cups_browse(void)
4801 int i
; /* Looping var */
4802 int auth
; /* Authorization status */
4803 int len
; /* Length of name string */
4804 int bytes
; /* Number of bytes left */
4805 char packet
[1541], /* Broadcast packet */
4806 *pptr
; /* Pointer into packet */
4807 socklen_t srclen
; /* Length of source address */
4808 http_addr_t srcaddr
; /* Source address */
4809 char srcname
[1024]; /* Source hostname */
4810 unsigned address
[4]; /* Source address */
4811 unsigned type
; /* Printer type */
4812 unsigned state
; /* Printer state */
4813 char uri
[HTTP_MAX_URI
], /* Printer URI */
4814 host
[HTTP_MAX_URI
], /* Host portion of URI */
4815 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4816 info
[IPP_MAX_NAME
], /* Information string */
4817 location
[IPP_MAX_NAME
], /* Location string */
4818 make_model
[IPP_MAX_NAME
];/* Make and model string */
4819 int num_attrs
; /* Number of attributes */
4820 cups_option_t
*attrs
; /* Attributes */
4824 * Read a packet from the browse socket...
4827 srclen
= sizeof(srcaddr
);
4828 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4829 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4832 * "Connection refused" is returned under Linux if the destination port
4833 * or address is unreachable from a previous sendto(); check for the
4834 * error here and ignore it for now...
4837 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4839 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4841 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4844 closesocket(BrowseSocket
);
4846 close(BrowseSocket
);
4849 cupsdRemoveSelect(BrowseSocket
);
4852 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4853 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4859 packet
[bytes
] = '\0';
4862 * If we're about to sleep, ignore incoming browse packets.
4869 * Figure out where it came from...
4873 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4875 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4876 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4877 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4878 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4881 #endif /* AF_INET6 */
4886 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4889 if (HostNameLookups
)
4890 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4892 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4894 len
= strlen(srcname
);
4902 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4905 * Access from localhost (127.0.0.1) is always allowed...
4908 auth
= CUPSD_AUTH_ALLOW
;
4913 * Do authorization checks on the domain/address...
4916 switch (BrowseACL
->order_type
)
4919 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4922 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4923 auth
= CUPSD_AUTH_ALLOW
;
4925 if (cupsdCheckAuth(address
, srcname
, len
,
4926 BrowseACL
->num_deny
, BrowseACL
->deny
))
4927 auth
= CUPSD_AUTH_DENY
;
4929 if (cupsdCheckAuth(address
, srcname
, len
,
4930 BrowseACL
->num_allow
, BrowseACL
->allow
))
4931 auth
= CUPSD_AUTH_ALLOW
;
4934 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
4935 auth
= CUPSD_AUTH_DENY
;
4937 if (cupsdCheckAuth(address
, srcname
, len
,
4938 BrowseACL
->num_allow
, BrowseACL
->allow
))
4939 auth
= CUPSD_AUTH_ALLOW
;
4941 if (cupsdCheckAuth(address
, srcname
, len
,
4942 BrowseACL
->num_deny
, BrowseACL
->deny
))
4943 auth
= CUPSD_AUTH_DENY
;
4949 auth
= CUPSD_AUTH_ALLOW
;
4951 if (auth
== CUPSD_AUTH_DENY
)
4953 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4954 "update_cups_browse: Refused %d bytes from %s", bytes
,
4959 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4960 "update_cups_browse: (%d bytes from %s) %s", bytes
,
4967 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
4969 cupsdLogMessage(CUPSD_LOG_WARN
,
4970 "update_cups_browse: Garbled browse packet - %s", packet
);
4974 strcpy(location
, "Location Unknown");
4975 strcpy(info
, "No Information Available");
4976 make_model
[0] = '\0';
4980 if ((pptr
= strchr(packet
, '\"')) != NULL
)
4983 * Have extended information; can't use sscanf for it because not all
4984 * sscanf's allow empty strings with %[^\"]...
4987 for (i
= 0, pptr
++;
4988 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4990 location
[i
] = *pptr
;
4998 while (*pptr
&& isspace(*pptr
& 255))
5003 for (i
= 0, pptr
++;
5004 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5013 while (*pptr
&& isspace(*pptr
& 255))
5018 for (i
= 0, pptr
++;
5019 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5021 make_model
[i
] = *pptr
;
5026 make_model
[i
] = '\0';
5029 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5035 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5036 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5037 type
, state
, uri
, location
, info
, make_model
));
5040 * Pull the URI apart to see if this is a local or remote printer...
5043 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5045 cupsFreeOptions(num_attrs
, attrs
);
5053 for (i
= 0; i
< NumRelays
; i
++)
5054 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5055 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5056 (struct sockaddr
*)&(Relays
[i
].to
),
5057 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5059 cupsdLogMessage(CUPSD_LOG_ERROR
,
5060 "update_cups_browse: sendto failed for relay %d - %s.",
5061 i
+ 1, strerror(errno
));
5062 cupsFreeOptions(num_attrs
, attrs
);
5067 * Process the browse data...
5070 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5071 (ipp_pstate_t
)state
, location
, info
, make_model
,
5077 * 'update_lpd()' - Update the LPD configuration as needed.
5081 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5088 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5089 * setting for backwards-compatibility.
5092 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5094 #endif /* __APPLE__ */
5096 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5099 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5102 char newfile
[1024]; /* New cups-lpd.N file */
5103 cups_file_t
*ofp
, /* Original file pointer */
5104 *nfp
; /* New file pointer */
5105 char line
[1024]; /* Line from file */
5108 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5110 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5112 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5113 LPDConfigFile
+ 9, strerror(errno
));
5117 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5119 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5120 newfile
, strerror(errno
));
5126 * Copy all of the lines from the cups-lpd file...
5129 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5133 cupsFilePrintf(nfp
, "%s\n", line
);
5134 snprintf(line
, sizeof(line
), "\tdisable = %s",
5135 onoff
? "no" : "yes");
5137 else if (!strstr(line
, "disable ="))
5138 cupsFilePrintf(nfp
, "%s\n", line
);
5143 rename(newfile
, LPDConfigFile
+ 9);
5146 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5149 * Enable/disable LPD via the launchctl command...
5152 char *argv
[5], /* Arguments for command */
5153 *envp
[MAX_ENV
]; /* Environment for command */
5154 int pid
; /* Process ID */
5157 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5158 argv
[0] = (char *)"launchctl";
5159 argv
[1] = (char *)(onoff
? "load" : "unload");
5160 argv
[2] = (char *)"-w";
5161 argv
[3] = LPDConfigFile
+ 10;
5164 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5167 #endif /* __APPLE__ */
5169 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5174 * 'update_polling()' - Read status messages from the poll daemons.
5178 update_polling(void)
5180 char *ptr
, /* Pointer to end of line in buffer */
5181 message
[1024]; /* Pointer to message text */
5182 int loglevel
; /* Log level for message */
5185 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5186 message
, sizeof(message
))) != NULL
)
5187 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5190 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5193 * All polling processes have died; stop polling...
5196 cupsdLogMessage(CUPSD_LOG_ERROR
,
5197 "update_polling: all polling processes have exited!");
5204 * 'update_smb()' - Update the SMB configuration as needed.
5208 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5213 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5216 * Enable/disable SMB via the specified smb.conf config file...
5219 char newfile
[1024]; /* New smb.conf.N file */
5220 cups_file_t
*ofp
, /* Original file pointer */
5221 *nfp
; /* New file pointer */
5222 char line
[1024]; /* Line from file */
5223 int in_printers
; /* In [printers] section? */
5226 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5228 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5230 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5231 SMBConfigFile
+ 8, strerror(errno
));
5235 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5237 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5238 newfile
, strerror(errno
));
5244 * Copy all of the lines from the smb.conf file...
5249 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5251 if (in_printers
&& strstr(line
, "printable ="))
5252 snprintf(line
, sizeof(line
), " printable = %s",
5253 onoff
? "yes" : "no");
5255 cupsFilePrintf(nfp
, "%s\n", line
);
5258 in_printers
= !strcmp(line
, "[printers]");
5263 rename(newfile
, SMBConfigFile
+ 8);
5266 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5271 * End of "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $".