2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z 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 if enabled...
1954 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1956 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1959 DNSServiceRefDeallocate(WebIFRef
);
1961 WebIFRef
= DNSSDRef
;
1962 if ((error
= DNSServiceRegister(&WebIFRef
,
1963 kDNSServiceFlagsShareConnection
,
1964 0, webif
, "_http._tcp", NULL
,
1965 NULL
, htons(DNSSDPort
), 7,
1966 "\006path=/", dnssdRegisterCallback
,
1967 NULL
)) != kDNSServiceErr_NoError
)
1968 cupsdLogMessage(CUPSD_LOG_ERROR
,
1969 "DNS-SD web interface registration failed: %d", error
);
1972 #endif /* HAVE_DNSSD */
1977 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1981 cupsdUpdateLDAPBrowse(void)
1983 char uri
[HTTP_MAX_URI
], /* Printer URI */
1984 host
[HTTP_MAX_URI
], /* Hostname */
1985 resource
[HTTP_MAX_URI
], /* Resource path */
1986 location
[1024], /* Printer location */
1987 info
[1024], /* Printer information */
1988 make_model
[1024], /* Printer make and model */
1989 type_num
[30]; /* Printer type number */
1990 int type
; /* Printer type */
1991 int rc
; /* LDAP status */
1992 int limit
; /* Size limit */
1993 LDAPMessage
*res
, /* LDAP search results */
1994 *e
; /* Current entry from search */
1996 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1998 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2001 * Reconnect if LDAP Handle is invalid...
2004 if (! BrowseLDAPHandle
)
2011 * Search for cups printers in LDAP directory...
2014 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2015 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2018 * If ldap search was successfull then exit function
2019 * and temporary disable LDAP updates...
2022 if (rc
!= LDAP_SUCCESS
)
2024 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2026 BrowseLDAPUpdate
= FALSE
;
2027 cupsdLogMessage(CUPSD_LOG_INFO
,
2028 "LDAP update temporary disabled");
2034 * If LDAP updates were disabled, we will reenable them...
2037 if (! BrowseLDAPUpdate
)
2039 BrowseLDAPUpdate
= TRUE
;
2040 cupsdLogMessage(CUPSD_LOG_INFO
,
2041 "LDAP update enabled");
2045 * Count LDAP entries and return if no entry exist...
2048 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2049 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2057 * Loop through the available printers...
2060 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2062 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2065 * Get the required values from this entry...
2068 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2069 "printerDescription", info
, sizeof(info
)) == -1)
2072 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2073 "printerLocation", location
, sizeof(location
)) == -1)
2076 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2077 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2080 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2081 "printerType", type_num
, sizeof(type_num
)) == -1)
2084 type
= atoi(type_num
);
2086 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2087 "printerURI", uri
, sizeof(uri
)) == -1)
2091 * Process the entry as browse data...
2094 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2095 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2096 location
, info
, make_model
, 0, NULL
);
2102 #endif /* HAVE_LDAP */
2107 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2111 cupsdUpdateSLPBrowse(void)
2113 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2114 *next
; /* Next service in list */
2115 cupsd_printer_t p
; /* Printer information */
2116 const char *uri
; /* Pointer to printer URI */
2117 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2118 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2122 * Reset the refresh time...
2125 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2128 * Poll for remote printers using SLP...
2133 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2134 slp_url_callback
, &s
);
2137 * Loop through the list of available printers...
2143 * Save the "next" pointer...
2149 * Load a cupsd_printer_t structure with the SLP service attributes...
2152 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2155 * Process this printer entry...
2158 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2160 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2163 * Pull the URI apart to see if this is a local or remote printer...
2166 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2167 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2168 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2172 * Free this listing...
2175 cupsdClearString(&p
.info
);
2176 cupsdClearString(&p
.location
);
2177 cupsdClearString(&p
.make_model
);
2182 #endif /* HAVE_LIBSLP */
2186 * 'dequote()' - Remote quotes from a string.
2189 static char * /* O - Dequoted string */
2190 dequote(char *d
, /* I - Destination string */
2191 const char *s
, /* I - Source string */
2192 int dlen
) /* I - Destination length */
2194 char *dptr
; /* Pointer into destination */
2199 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2217 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2220 static char * /* O - TXT record */
2221 dnssdBuildTxtRecord(
2222 int *txt_len
, /* O - TXT record length */
2223 cupsd_printer_t
*p
, /* I - Printer information */
2224 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2226 int i
, j
; /* Looping vars */
2227 char type_str
[32], /* Type to string buffer */
2228 state_str
[32], /* State to string buffer */
2229 rp_str
[1024], /* Queue name string buffer */
2230 air_str
[1024], /* auth-info-required string buffer */
2231 *keyvalue
[32][2]; /* Table of key/value pairs */
2235 * Load up the key value pairs...
2240 keyvalue
[i
][0] = "txtvers";
2241 keyvalue
[i
++][1] = "1";
2243 keyvalue
[i
][0] = "qtotal";
2244 keyvalue
[i
++][1] = "1";
2246 keyvalue
[i
][0] = "rp";
2247 keyvalue
[i
++][1] = rp_str
;
2249 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2251 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2252 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2254 keyvalue
[i
][0] = "ty";
2255 keyvalue
[i
++][1] = p
->make_model
;
2257 if (p
->location
&& *p
->location
!= '\0')
2259 keyvalue
[i
][0] = "note";
2260 keyvalue
[i
++][1] = p
->location
;
2263 keyvalue
[i
][0] = "priority";
2264 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2266 keyvalue
[i
][0] = "product";
2267 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2269 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2270 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2272 keyvalue
[i
][0] = "printer-state";
2273 keyvalue
[i
++][1] = state_str
;
2275 keyvalue
[i
][0] = "printer-type";
2276 keyvalue
[i
++][1] = type_str
;
2278 keyvalue
[i
][0] = "Transparent";
2279 keyvalue
[i
++][1] = "T";
2281 keyvalue
[i
][0] = "Binary";
2282 keyvalue
[i
++][1] = "T";
2284 if ((p
->type
& CUPS_PRINTER_FAX
))
2286 keyvalue
[i
][0] = "Fax";
2287 keyvalue
[i
++][1] = "T";
2290 if ((p
->type
& CUPS_PRINTER_COLOR
))
2292 keyvalue
[i
][0] = "Color";
2293 keyvalue
[i
++][1] = "T";
2296 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2298 keyvalue
[i
][0] = "Duplex";
2299 keyvalue
[i
++][1] = "T";
2302 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2304 keyvalue
[i
][0] = "Staple";
2305 keyvalue
[i
++][1] = "T";
2308 if ((p
->type
& CUPS_PRINTER_COPIES
))
2310 keyvalue
[i
][0] = "Copies";
2311 keyvalue
[i
++][1] = "T";
2314 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2316 keyvalue
[i
][0] = "Collate";
2317 keyvalue
[i
++][1] = "T";
2320 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2322 keyvalue
[i
][0] = "Punch";
2323 keyvalue
[i
++][1] = "T";
2326 if ((p
->type
& CUPS_PRINTER_BIND
))
2328 keyvalue
[i
][0] = "Bind";
2329 keyvalue
[i
++][1] = "T";
2332 if ((p
->type
& CUPS_PRINTER_SORT
))
2334 keyvalue
[i
][0] = "Sort";
2335 keyvalue
[i
++][1] = "T";
2338 keyvalue
[i
][0] = "pdl";
2339 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2341 if (p
->num_auth_info_required
)
2343 char *air
= air_str
; /* Pointer into string */
2346 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2348 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2354 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2358 keyvalue
[i
][0] = "air";
2359 keyvalue
[i
++][1] = air_str
;
2363 * Then pack them into a proper txt record...
2366 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2371 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2374 static int /* O - Result of comparison */
2375 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2376 cupsd_printer_t
*b
)/* I - Second printer */
2378 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2383 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2388 dnssdDeregisterPrinter(
2389 cupsd_printer_t
*p
) /* I - Printer */
2391 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2394 * Closing the socket deregisters the service
2399 DNSServiceRefDeallocate(p
->ipp_ref
);
2403 cupsArrayRemove(DNSSDPrinters
, p
);
2404 cupsdClearString(&p
->reg_name
);
2409 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2419 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2420 * TXT record format.
2423 static char * /* O - TXT record */
2424 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2425 char *keyvalue
[][2], /* I - Table of key value pairs */
2426 int count
) /* I - Items in table */
2428 int i
; /* Looping var */
2429 int length
; /* Length of TXT record */
2430 int length2
; /* Length of value */
2431 char *txtRecord
; /* TXT record buffer */
2432 char *cursor
; /* Looping pointer */
2436 * Calculate the buffer size
2439 for (length
= i
= 0; i
< count
; i
++)
2440 length
+= 1 + strlen(keyvalue
[i
][0]) +
2441 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2444 * Allocate and fill it
2447 txtRecord
= malloc(length
);
2452 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2455 * Drop in the p-string style length byte followed by the data
2458 length
= strlen(keyvalue
[i
][0]);
2459 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2461 *cursor
++ = (unsigned char)(length
+ length2
);
2463 memcpy(cursor
, keyvalue
[i
][0], length
);
2470 memcpy(cursor
, keyvalue
[i
][1], length2
);
2481 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2485 dnssdRegisterCallback(
2486 DNSServiceRef sdRef
, /* I - DNS Service reference */
2487 DNSServiceFlags flags
, /* I - Reserved for future use */
2488 DNSServiceErrorType errorCode
, /* I - Error code */
2489 const char *name
, /* I - Service name */
2490 const char *regtype
, /* I - Service type */
2491 const char *domain
, /* I - Domain. ".local" for now */
2492 void *context
) /* I - User-defined context */
2494 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2495 /* Current printer */
2498 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2499 name
, regtype
, p
? p
->name
: "Web Interface");
2503 cupsdLogMessage(CUPSD_LOG_ERROR
,
2504 "DNSServiceRegister failed with error %d", (int)errorCode
);
2507 else if (p
&& strcasecmp(name
, p
->reg_name
))
2509 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2512 cupsArrayRemove(DNSSDPrinters
, p
);
2513 cupsdSetString(&p
->reg_name
, name
);
2514 cupsArrayAdd(DNSSDPrinters
, p
);
2516 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2522 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2523 * or update the broadcast contents.
2527 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2529 DNSServiceErrorType se
; /* dnssd errors */
2530 char *ipp_txt
, /* IPP TXT record buffer */
2531 *printer_txt
, /* LPD TXT record buffer */
2532 name
[1024], /* Service name */
2533 *nameptr
; /* Pointer into name */
2534 int ipp_len
, /* IPP TXT record length */
2535 printer_len
; /* LPD TXT record length */
2536 char resource
[1024]; /* Resource path for printer */
2537 const char *regtype
; /* Registration type */
2538 const char *domain
; /* Registration domain */
2539 cupsd_location_t
*location
, /* Printer location */
2540 *policy
; /* Operation policy for Print-Job */
2541 unsigned address
[4]; /* INADDR_ANY address */
2544 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2545 !p
->ipp_ref
? "new" : "update");
2548 * If per-printer sharing was just disabled make sure we're not
2549 * registered before returning.
2554 dnssdDeregisterPrinter(p
);
2559 * The registered name takes the form of "<printer-info> @ <computer name>"...
2562 if (p
->info
&& strlen(p
->info
) > 0)
2565 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2567 strlcpy(name
, p
->info
, sizeof(name
));
2570 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2572 strlcpy(name
, p
->name
, sizeof(name
));
2575 * If an existing printer was renamed, unregister it and start over...
2578 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2579 dnssdDeregisterPrinter(p
);
2583 cupsdSetString(&p
->reg_name
, name
);
2584 cupsArrayAdd(DNSSDPrinters
, p
);
2588 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2589 * let dnssd decide on the domain, otherwise restrict it to ".local".
2592 if (p
->type
& CUPS_PRINTER_CLASS
)
2593 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2595 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2597 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2598 location
= cupsdFindBest(resource
, HTTP_POST
);
2599 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2601 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2602 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2608 * Register IPP and (optionally) LPD...
2611 ipp_len
= 0; /* anti-compiler-warning-code */
2612 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2617 * Initial registration. Use the _fax subtype for fax queues...
2620 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2623 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2624 "Registering DNS-SD printer %s with name \"%s\", "
2625 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2626 domain
? domain
: "(null)");
2629 * Register the queue, dropping characters as needed until we succeed...
2632 nameptr
= name
+ strlen(name
);
2636 p
->ipp_ref
= DNSSDRef
;
2637 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2638 0, name
, regtype
, domain
, NULL
,
2639 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2640 dnssdRegisterCallback
,
2641 p
)) == kDNSServiceErr_BadParam
)
2644 * Name is too long, drop trailing characters, taking into account
2650 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2657 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2659 if (se
== kDNSServiceErr_NoError
)
2661 p
->ipp_txt
= ipp_txt
;
2662 p
->ipp_len
= ipp_len
;
2666 cupsdLogMessage(CUPSD_LOG_WARN
,
2667 "DNS-SD IPP registration of \"%s\" failed: %d",
2670 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2673 * Update the existing registration...
2676 /* A TTL of 0 means use record's original value (Radar 3176248) */
2677 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2682 p
->ipp_txt
= ipp_txt
;
2683 p
->ipp_len
= ipp_len
;
2690 if (BrowseLocalProtocols
& BROWSE_LPD
)
2692 printer_len
= 0; /* anti-compiler-warning-code */
2693 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2695 if (!p
->printer_ref
)
2698 * Initial registration...
2701 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2702 "Registering DNS-SD printer %s with name \"%s\", "
2703 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2704 name
, domain
? domain
: "(null)");
2706 p
->printer_ref
= DNSSDRef
;
2707 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2708 kDNSServiceFlagsShareConnection
,
2709 0, name
, "_printer._tcp", domain
, NULL
,
2710 htons(515), printer_len
, printer_txt
,
2711 dnssdRegisterCallback
,
2712 p
)) == kDNSServiceErr_NoError
)
2714 p
->printer_txt
= printer_txt
;
2715 p
->printer_len
= printer_len
;
2719 cupsdLogMessage(CUPSD_LOG_WARN
,
2720 "DNS-SD LPD registration of \"%s\" failed: %d",
2723 else if (printer_len
!= p
->printer_len
||
2724 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2727 * Update the existing registration...
2730 /* A TTL of 0 means use record's original value (Radar 3176248) */
2731 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2735 free(p
->printer_txt
);
2737 p
->printer_txt
= printer_txt
;
2738 p
->printer_len
= printer_len
;
2749 * 'dnssdUpdate()' - Handle DNS-SD queries.
2755 DNSServiceErrorType sdErr
; /* Service discovery error */
2758 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2759 cupsdLogMessage(CUPSD_LOG_ERROR
,
2760 "DNS Service Discovery registration error %d!",
2763 #endif /* HAVE_DNSSD */
2768 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2771 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2772 get_hostconfig(const char *name
) /* I - Name of service */
2774 cups_file_t
*fp
; /* Hostconfig file */
2775 char line
[1024], /* Line from file */
2776 *ptr
; /* Pointer to value */
2777 int state
= 1; /* State of service */
2781 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2782 * the service is enabled/auto.
2785 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2788 * Read lines from the file until we find the service...
2791 while (cupsFileGets(fp
, line
, sizeof(line
)))
2793 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2798 if (!strcasecmp(line
, name
))
2801 * Found the service, see if it is set to "-NO-"...
2804 if (!strncasecmp(ptr
, "-NO-", 4))
2815 #endif /* __APPLE__ */
2819 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2822 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2823 is_local_queue(const char *uri
, /* I - Printer URI */
2824 char *host
, /* O - Host string */
2825 int hostlen
, /* I - Length of host buffer */
2826 char *resource
, /* O - Resource string */
2827 int resourcelen
) /* I - Length of resource buffer */
2829 char scheme
[32], /* Scheme portion of URI */
2830 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2831 int port
; /* Port portion of URI */
2832 cupsd_netif_t
*iface
; /* Network interface */
2836 * Pull the URI apart to see if this is a local or remote printer...
2839 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2840 username
, sizeof(username
), host
, hostlen
, &port
,
2841 resource
, resourcelen
) < HTTP_URI_OK
)
2844 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2847 * Check for local server addresses...
2850 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2855 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2857 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2858 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2862 * If we get here, the printer is remote...
2870 * 'process_browse_data()' - Process new browse data.
2874 process_browse_data(
2875 const char *uri
, /* I - URI of printer/class */
2876 const char *host
, /* I - Hostname */
2877 const char *resource
, /* I - Resource path */
2878 cups_ptype_t type
, /* I - Printer type */
2879 ipp_pstate_t state
, /* I - Printer state */
2880 const char *location
, /* I - Printer location */
2881 const char *info
, /* I - Printer information */
2882 const char *make_model
, /* I - Printer make and model */
2883 int num_attrs
, /* I - Number of attributes */
2884 cups_option_t
*attrs
) /* I - Attributes */
2886 int i
; /* Looping var */
2887 int update
; /* Update printer attributes? */
2888 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2889 name
[IPP_MAX_NAME
], /* Name of printer */
2890 newname
[IPP_MAX_NAME
], /* New name of printer */
2891 *hptr
, /* Pointer into hostname */
2892 *sptr
; /* Pointer into ServerName */
2893 const char *shortname
; /* Short queue name (queue) */
2894 char local_make_model
[IPP_MAX_NAME
];
2895 /* Local make and model */
2896 cupsd_printer_t
*p
; /* Printer information */
2897 const char *ipp_options
, /* ipp-options value */
2898 *lease_duration
; /* lease-duration value */
2899 int is_class
; /* Is this queue a class? */
2902 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2903 "process_browse_data(uri=\"%s\", host=\"%s\", "
2904 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2905 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2906 uri
, host
, resource
, type
, state
,
2907 location
? location
: "(nil)", info
? info
: "(nil)",
2908 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2911 * Determine if the URI contains any illegal characters in it...
2914 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2915 (strncmp(resource
, "/printers/", 10) &&
2916 strncmp(resource
, "/classes/", 9)))
2918 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2922 if (strchr(resource
, '?') ||
2923 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2924 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2926 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2932 * OK, this isn't a local printer; add any remote options...
2935 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2937 if (BrowseRemoteOptions
)
2939 if (BrowseRemoteOptions
[0] == '?')
2942 * Override server-supplied options...
2945 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2947 else if (ipp_options
)
2950 * Combine the server and local options...
2953 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2954 BrowseRemoteOptions
);
2959 * Just use the local options...
2962 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2967 else if (ipp_options
)
2970 * Just use the server-supplied options...
2973 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2978 * See if we already have it listed in the Printers list, and add it if not...
2981 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2982 type
&= ~CUPS_PRINTER_IMPLICIT
;
2984 hptr
= strchr(host
, '.');
2985 sptr
= strchr(ServerName
, '.');
2986 is_class
= type
& CUPS_PRINTER_CLASS
;
2988 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2991 * Strip the common domain name components...
2994 while (hptr
!= NULL
)
2996 if (!strcasecmp(hptr
, sptr
))
3002 hptr
= strchr(hptr
+ 1, '.');
3009 * Remote destination is a class...
3012 if (!strncmp(resource
, "/classes/", 9))
3013 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3017 shortname
= resource
+ 9;
3022 * Remote destination is a printer...
3025 if (!strncmp(resource
, "/printers/", 10))
3026 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3030 shortname
= resource
+ 10;
3034 *hptr
= '.'; /* Resource FQDN */
3036 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3039 * Long name doesn't exist, try short name...
3042 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3045 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3048 * Short name doesn't exist, use it for this shared queue.
3051 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3053 strlcpy(name
, shortname
, sizeof(name
));
3058 * Short name exists...
3061 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3062 "process_browse_data: %s found, type=%x, hostname=%s...",
3063 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3065 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3066 p
= NULL
; /* Don't replace implicit classes */
3067 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3070 * Short name exists but is for a different host. If this is a remote
3071 * queue, rename it and use the long name...
3074 if (p
->type
& CUPS_PRINTER_REMOTE
)
3076 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3077 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3078 is_class
? "class" : "printer", p
->name
, p
->name
,
3080 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3081 "%s \'%s\' deleted by directory services.",
3082 is_class
? "Class" : "Printer", p
->name
);
3084 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3085 cupsdRenamePrinter(p
, newname
);
3087 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3088 "%s \'%s\' added by directory services.",
3089 is_class
? "Class" : "Printer", p
->name
);
3093 * Force creation with long name...
3101 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3102 "process_browse_data: %s found, type=%x, hostname=%s...",
3103 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3108 * Queue doesn't exist; add it...
3112 p
= cupsdAddClass(name
);
3114 p
= cupsdAddPrinter(name
);
3116 cupsdClearString(&(p
->hostname
));
3118 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3119 is_class
? "class" : "printer", name
);
3121 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3122 "%s \'%s\' added by directory services.",
3123 is_class
? "Class" : "Printer", name
);
3126 * Force the URI to point to the real server...
3129 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3132 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3141 * Hostname not set, so this must be a cached remote printer
3142 * that was created for a pending print job...
3145 cupsdSetString(&p
->hostname
, host
);
3146 cupsdSetString(&p
->uri
, uri
);
3147 cupsdSetString(&p
->device_uri
, uri
);
3150 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3154 * Update the state...
3158 p
->browse_time
= time(NULL
);
3160 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3164 * Grab the lease-duration for the browse data; anything less then 1
3165 * second or more than 1 week gets the default BrowseTimeout...
3168 i
= atoi(lease_duration
);
3169 if (i
< 1 || i
> 604800)
3172 p
->browse_expire
= p
->browse_time
+ i
;
3175 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3177 if (type
& CUPS_PRINTER_REJECTING
)
3179 type
&= ~CUPS_PRINTER_REJECTING
;
3187 else if (!p
->accepting
)
3193 if (p
->type
!= type
)
3199 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3201 cupsdSetString(&p
->location
, location
);
3205 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3207 cupsdSetString(&p
->info
, info
);
3210 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3213 if (!make_model
|| !make_model
[0])
3216 snprintf(local_make_model
, sizeof(local_make_model
),
3217 "Remote Class on %s", host
);
3219 snprintf(local_make_model
, sizeof(local_make_model
),
3220 "Remote Printer on %s", host
);
3223 snprintf(local_make_model
, sizeof(local_make_model
),
3224 "%s on %s", make_model
, host
);
3226 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3228 cupsdSetString(&p
->make_model
, local_make_model
);
3234 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3237 * See if we need to update the attributes...
3240 if (p
->num_options
!= num_attrs
)
3244 for (i
= 0; i
< num_attrs
; i
++)
3245 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3246 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3247 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3256 * Free the old options...
3259 cupsFreeOptions(p
->num_options
, p
->options
);
3262 p
->num_options
= num_attrs
;
3265 if (type
& CUPS_PRINTER_DELETE
)
3267 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3268 "%s \'%s\' deleted by directory services.",
3269 is_class
? "Class" : "Printer", p
->name
);
3271 cupsdExpireSubscriptions(p
, NULL
);
3273 cupsdDeletePrinter(p
, 1);
3274 cupsdUpdateImplicitClasses();
3275 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3279 cupsdSetPrinterAttrs(p
);
3280 cupsdUpdateImplicitClasses();
3284 * See if we have a default printer... If not, make the first network
3285 * default printer the default.
3288 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3291 * Find the first network default printer and use it...
3294 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3296 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3297 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3300 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3306 * Do auto-classing if needed...
3309 process_implicit_classes();
3314 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3318 process_implicit_classes(void)
3320 int i
; /* Looping var */
3321 int update
; /* Update printer attributes? */
3322 char name
[IPP_MAX_NAME
], /* Name of printer */
3323 *hptr
; /* Pointer into hostname */
3324 cupsd_printer_t
*p
, /* Printer information */
3325 *pclass
, /* Printer class */
3326 *first
; /* First printer in class */
3327 int offset
, /* Offset of name */
3328 len
; /* Length of name */
3331 if (!ImplicitClasses
|| !Printers
)
3335 * Loop through all available printers and create classes as needed...
3338 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3339 update
= 0, pclass
= NULL
, first
= NULL
;
3341 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3344 * Skip implicit classes...
3347 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3354 * If len == 0, get the length of this printer name up to the "@"
3358 cupsArraySave(Printers
);
3361 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3362 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3365 * We have more than one printer with the same name; see if
3366 * we have a class, and if this printer is a member...
3369 if (pclass
&& strcasecmp(pclass
->name
, name
))
3372 cupsdSetPrinterAttrs(pclass
);
3378 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3381 * Need to add the class...
3384 pclass
= cupsdAddPrinter(name
);
3385 cupsArrayAdd(ImplicitPrinters
, pclass
);
3387 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3388 pclass
->accepting
= 1;
3389 pclass
->state
= IPP_PRINTER_IDLE
;
3391 cupsdSetString(&pclass
->location
, p
->location
);
3392 cupsdSetString(&pclass
->info
, p
->info
);
3394 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3395 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3399 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3401 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3403 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3404 "Implicit class \'%s\' added by directory services.",
3410 for (i
= 0; i
< pclass
->num_printers
; i
++)
3411 if (pclass
->printers
[i
] == first
)
3414 if (i
>= pclass
->num_printers
)
3416 first
->in_implicit_class
= 1;
3417 cupsdAddPrinterToClass(pclass
, first
);
3423 for (i
= 0; i
< pclass
->num_printers
; i
++)
3424 if (pclass
->printers
[i
] == p
)
3427 if (i
>= pclass
->num_printers
)
3429 p
->in_implicit_class
= 1;
3430 cupsdAddPrinterToClass(pclass
, p
);
3437 * First time around; just get name length and mark it as first
3441 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3442 len
= hptr
- p
->name
;
3444 len
= strlen(p
->name
);
3446 strncpy(name
, p
->name
, len
);
3450 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3451 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3454 * Can't use same name as a local printer; add "Any" to the
3455 * front of the name, unless we have explicitly disabled
3456 * the "ImplicitAnyClasses"...
3459 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3462 * Add "Any" to the class name...
3465 strcpy(name
, "Any");
3466 strncpy(name
+ 3, p
->name
, len
);
3467 name
[len
+ 3] = '\0';
3473 * Don't create an implicit class if we have a local printer
3474 * with the same name...
3478 cupsArrayRestore(Printers
);
3486 cupsArrayRestore(Printers
);
3490 * Update the last printer class as needed...
3493 if (pclass
&& update
)
3494 cupsdSetPrinterAttrs(pclass
);
3499 * 'send_cups_browse()' - Send new browsing information using the CUPS
3504 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3506 int i
; /* Looping var */
3507 cups_ptype_t type
; /* Printer type */
3508 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3509 int bytes
; /* Length of packet */
3510 char packet
[1453], /* Browse data packet */
3511 uri
[1024], /* Printer URI */
3512 location
[1024], /* printer-location */
3513 info
[1024], /* printer-info */
3515 /* printer-make-and-model */
3516 cupsd_netif_t
*iface
; /* Network interface */
3520 * Figure out the printer type value...
3523 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3526 type
|= CUPS_PRINTER_REJECTING
;
3528 if (p
== DefaultPrinter
)
3529 type
|= CUPS_PRINTER_DEFAULT
;
3532 * Remove quotes from printer-info, printer-location, and
3533 * printer-make-and-model attributes...
3536 dequote(location
, p
->location
, sizeof(location
));
3537 dequote(info
, p
->info
, sizeof(info
));
3540 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3541 else if (p
->type
& CUPS_PRINTER_CLASS
)
3543 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3544 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3546 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3549 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3551 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3554 * Send a packet to each browse address...
3557 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3561 * Send the browse packet to one or more interfaces...
3564 if (!strcmp(b
->iface
, "*"))
3567 * Send to all local interfaces...
3572 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3574 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3577 * Only send to local, IPv4 interfaces...
3580 if (!iface
->is_local
|| !iface
->port
||
3581 iface
->address
.addr
.sa_family
!= AF_INET
)
3584 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3585 iface
->hostname
, iface
->port
,
3586 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3589 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3590 type
, p
->state
, uri
, location
, info
, make_model
,
3591 p
->browse_attrs
? p
->browse_attrs
: "");
3593 bytes
= strlen(packet
);
3595 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3596 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3597 iface
->name
, packet
);
3599 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3601 sendto(BrowseSocket
, packet
, bytes
, 0,
3602 (struct sockaddr
*)&(iface
->broadcast
),
3603 httpAddrLength(&(iface
->broadcast
)));
3606 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3609 * Send to the named interface using the IPv4 address...
3613 if (strcmp(b
->iface
, iface
->name
))
3618 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3621 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3625 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3626 iface
->hostname
, iface
->port
,
3627 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3630 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3631 type
, p
->state
, uri
, location
, info
, make_model
,
3632 p
->browse_attrs
? p
->browse_attrs
: "");
3634 bytes
= strlen(packet
);
3636 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3637 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3638 iface
->name
, packet
);
3640 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3642 sendto(BrowseSocket
, packet
, bytes
, 0,
3643 (struct sockaddr
*)&(iface
->broadcast
),
3644 httpAddrLength(&(iface
->broadcast
)));
3651 * Send the browse packet to the indicated address using
3652 * the default server name...
3655 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3656 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3657 p
->browse_attrs
? p
->browse_attrs
: "");
3659 bytes
= strlen(packet
);
3660 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3661 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3663 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3664 (struct sockaddr
*)&(b
->to
),
3665 httpAddrLength(&(b
->to
))) <= 0)
3668 * Unable to send browse packet, so remove this address from the
3672 cupsdLogMessage(CUPSD_LOG_ERROR
,
3673 "cupsdSendBrowseList: sendto failed for browser "
3675 (int)(b
- Browsers
+ 1), strerror(errno
));
3678 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3689 * 'ldap_search_rec()' - LDAP Search with reconnect
3693 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3694 char *base
, /* I - Base dn */
3695 int scope
, /* I - LDAP search scope */
3696 char *filter
, /* I - Filter string */
3697 char *attrs
[], /* I - Requested attributes */
3698 int attrsonly
, /* I - Return only attributes? */
3699 LDAPMessage
**res
) /* I - LDAP handler */
3701 int rc
; /* Return code */
3704 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3705 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3706 NULL
, LDAP_NO_LIMIT
, res
);
3708 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3709 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3712 * If we have a connection problem try again...
3715 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3717 cupsdLogMessage(CUPSD_LOG_ERROR
,
3718 "LDAP search failed with status %d: %s",
3719 rc
, ldap_err2string(rc
));
3720 cupsdLogMessage(CUPSD_LOG_INFO
,
3721 "We try the LDAP search once again after reconnecting to "
3726 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3727 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3728 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3730 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3731 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3734 if (rc
== LDAP_NO_SUCH_OBJECT
)
3735 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3736 "ldap_search_rec: LDAP entry/object not found");
3737 else if (rc
!= LDAP_SUCCESS
)
3738 cupsdLogMessage(CUPSD_LOG_ERROR
,
3739 "ldap_search_rec: LDAP search failed with status %d: %s",
3740 rc
, ldap_err2string(rc
));
3742 if (rc
!= LDAP_SUCCESS
)
3750 * 'ldap_freeres()' - Free LDAPMessage
3754 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3756 int rc
; /* Return value */
3759 rc
= ldap_msgfree(entry
);
3761 cupsdLogMessage(CUPSD_LOG_WARN
,
3762 "Can't free LDAPMessage!");
3764 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3765 "Freeing LDAPMessage was unnecessary");
3770 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3774 ldap_getval_firststring(
3775 LDAP
*ld
, /* I - LDAP handler */
3776 LDAPMessage
*entry
, /* I - LDAP message or search result */
3777 char *attr
, /* I - the wanted attribute */
3778 char *retval
, /* O - String to return */
3779 unsigned long maxsize
) /* I - Max string size */
3781 char *dn
; /* LDAP DN */
3782 int rc
= 0; /* Return code */
3783 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3784 struct berval
**bval
; /* LDAP value array */
3785 unsigned long size
; /* String size */
3789 * Get value from LDAPMessage...
3792 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3795 dn
= ldap_get_dn(ld
, entry
);
3796 cupsdLogMessage(CUPSD_LOG_WARN
,
3797 "Failed to get LDAP value %s for %s!",
3805 * Check size and copy value into our string...
3809 if (size
< bval
[0]->bv_len
)
3812 dn
= ldap_get_dn(ld
, entry
);
3813 cupsdLogMessage(CUPSD_LOG_WARN
,
3814 "Attribute %s is too big! (dn: %s)",
3819 size
= bval
[0]->bv_len
;
3821 strlcpy(retval
, bval
[0]->bv_val
, size
);
3822 ldap_value_free_len(bval
);
3825 char **value
; /* LDAP value */
3828 * Get value from LDAPMessage...
3831 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3834 dn
= ldap_get_dn(ld
, entry
);
3835 cupsdLogMessage(CUPSD_LOG_WARN
,
3836 "Failed to get LDAP value %s for %s!",
3842 strlcpy(retval
, *value
, maxsize
);
3843 ldap_value_free(value
);
3845 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3852 * 'send_ldap_ou()' - Send LDAP ou registrations.
3856 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3857 char *basedn
, /* I - Our base dn */
3858 char *descstring
) /* I - Description for ou */
3860 int i
; /* Looping var... */
3861 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3862 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3863 LDAPMessage
*res
, /* Search result token */
3864 *e
; /* Current entry from search */
3865 int rc
; /* LDAP status */
3866 char dn
[1024], /* DN of the organizational unit we are adding */
3867 *desc
[2], /* Change records */
3869 char old_desc
[1024]; /* Old description */
3870 static const char * const objectClass_values
[] =
3871 { /* The 2 objectClass's we use in */
3872 "top", /* our LDAP entries */
3873 "organizationalUnit",
3876 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3883 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3886 * Reconnect if LDAP Handle is invalid...
3889 if (! BrowseLDAPHandle
)
3891 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3892 "send_ldap_ou: LDAP Handle is invalid. Try "
3899 * Prepare ldap search...
3902 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3903 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3907 desc
[0] = descstring
;
3910 mods
[0].mod_type
= "ou";
3911 mods
[0].mod_values
= ou_value
;
3912 mods
[1].mod_type
= "description";
3913 mods
[1].mod_values
= desc
;
3914 mods
[2].mod_type
= "objectClass";
3915 mods
[2].mod_values
= (char **)objectClass_values
;
3917 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3918 (char **)ou_attrs
, 0, &res
);
3921 * If ldap search was not successfull then exit function...
3924 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3928 * Check if we need to insert or update the LDAP entry...
3931 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3932 rc
!= LDAP_NO_SUCH_OBJECT
)
3935 * Printserver has already been registered, check if
3936 * modification is required...
3939 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
3942 * Get the required values from this entry...
3945 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
3946 sizeof(old_desc
)) == -1)
3950 * Check if modification is required...
3953 if ( strcmp(desc
[0], old_desc
) == 0 )
3956 * LDAP entry for the printer exists.
3957 * Printer has already been registered,
3958 * no modifications required...
3960 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3961 "send_ldap_ou: No updates required for %s", ou
);
3966 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3967 "send_ldap_ou: Replace entry for %s", ou
);
3969 for (i
= 0; i
< 3; i
++)
3971 pmods
[i
] = mods
+ i
;
3972 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3976 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3977 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3978 NULL
)) != LDAP_SUCCESS
)
3980 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3981 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3983 cupsdLogMessage(CUPSD_LOG_ERROR
,
3984 "LDAP modify for %s failed with status %d: %s",
3985 ou
, rc
, ldap_err2string(rc
));
3986 if ( LDAP_SERVER_DOWN
== rc
)
3994 * Printserver has never been registered,
3995 * add registration...
3998 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3999 "send_ldap_ou: Add entry for %s", ou
);
4001 for (i
= 0; i
< 3; i
++)
4003 pmods
[i
] = mods
+ i
;
4004 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4008 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4009 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4010 NULL
)) != LDAP_SUCCESS
)
4012 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4013 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4015 cupsdLogMessage(CUPSD_LOG_ERROR
,
4016 "LDAP add for %s failed with status %d: %s",
4017 ou
, rc
, ldap_err2string(rc
));
4018 if ( LDAP_SERVER_DOWN
== rc
)
4028 * 'send_ldap_browse()' - Send LDAP printer registrations.
4032 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4034 int i
; /* Looping var... */
4035 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4036 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4037 LDAPMessage
*res
, /* Search result token */
4038 *e
; /* Current entry from search */
4039 char *cn_value
[2], /* Change records */
4045 typestring
[255], /* String to hold printer-type */
4046 dn
[1024]; /* DN of the printer we are adding */
4047 int rc
; /* LDAP status */
4048 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4049 old_location
[1024], /* Printer location */
4050 old_info
[1024], /* Printer information */
4051 old_make_model
[1024], /* Printer make and model */
4052 old_type_string
[30]; /* Temporary type number */
4053 int old_type
; /* Printer type */
4054 static const char * const objectClass_values
[] =
4055 { /* The 3 objectClass's we use in */
4056 "top", /* our LDAP entries */
4063 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4066 * Exit function if LDAP updates has been disabled...
4069 if (!BrowseLDAPUpdate
)
4071 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4072 "send_ldap_browse: Updates temporary disabled; "
4078 * Reconnect if LDAP Handle is invalid...
4081 if (!BrowseLDAPHandle
)
4083 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4084 "send_ldap_browse: LDAP Handle is invalid. Try "
4091 * Everything in ldap is ** so we fudge around it...
4094 sprintf(typestring
, "%u", p
->type
);
4096 cn_value
[0] = p
->name
;
4098 info
[0] = p
->info
? p
->info
: "Unknown";
4100 location
[0] = p
->location
? p
->location
: "Unknown";
4102 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4103 make_model
[1] = NULL
;
4104 type
[0] = typestring
;
4110 * Get ldap entry for printer ...
4113 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4115 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4117 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4118 (char **)ldap_attrs
, 0, &res
);
4121 * If ldap search was not successfull then exit function
4122 * and temporary disable LDAP updates...
4125 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4127 if (BrowseLDAPUpdate
&&
4128 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4130 BrowseLDAPUpdate
= FALSE
;
4131 cupsdLogMessage(CUPSD_LOG_INFO
,
4132 "LDAP update temporary disabled");
4139 * Fill modification array...
4142 mods
[0].mod_type
= "cn";
4143 mods
[0].mod_values
= cn_value
;
4144 mods
[1].mod_type
= "printerDescription";
4145 mods
[1].mod_values
= info
;
4146 mods
[2].mod_type
= "printerURI";
4147 mods
[2].mod_values
= uri
;
4148 mods
[3].mod_type
= "printerLocation";
4149 mods
[3].mod_values
= location
;
4150 mods
[4].mod_type
= "printerMakeAndModel";
4151 mods
[4].mod_values
= make_model
;
4152 mods
[5].mod_type
= "printerType";
4153 mods
[5].mod_values
= type
;
4154 mods
[6].mod_type
= "objectClass";
4155 mods
[6].mod_values
= (char **)objectClass_values
;
4158 * Check if we need to insert or update the LDAP entry...
4161 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4162 rc
!= LDAP_NO_SUCH_OBJECT
)
4165 * Printer has already been registered, check if
4166 * modification is required...
4169 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4172 * Get the required values from this entry...
4175 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4176 old_info
, sizeof(old_info
)) == -1)
4179 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4180 old_location
, sizeof(old_location
)) == -1)
4183 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4184 old_make_model
, sizeof(old_make_model
)) == -1)
4187 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4188 old_type_string
, sizeof(old_type_string
)) == -1)
4191 old_type
= atoi(old_type_string
);
4193 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4194 sizeof(old_uri
)) == -1)
4198 * Check if modification is required...
4201 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4202 !strcmp(location
[0], old_location
) &&
4203 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4206 * LDAP entry for the printer exists. Printer has already been registered,
4207 * no modifications required...
4210 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4211 "send_ldap_browse: No updates required for %s", p
->name
);
4216 * LDAP entry for the printer exists. Printer has already been registered,
4217 * modify the current registration...
4220 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4221 "send_ldap_browse: Replace entry for %s", p
->name
);
4223 for (i
= 0; i
< 7; i
++)
4225 pmods
[i
] = mods
+ i
;
4226 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4230 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4231 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4232 NULL
)) != LDAP_SUCCESS
)
4234 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4235 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4237 cupsdLogMessage(CUPSD_LOG_ERROR
,
4238 "LDAP modify for %s failed with status %d: %s",
4239 p
->name
, rc
, ldap_err2string(rc
));
4240 if (rc
== LDAP_SERVER_DOWN
)
4248 * No LDAP entry exists for the printer. Printer has never been registered,
4249 * add the current registration...
4252 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4254 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4255 "send_ldap_browse: Add entry for %s", p
->name
);
4257 for (i
= 0; i
< 7; i
++)
4259 pmods
[i
] = mods
+ i
;
4260 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4264 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4265 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4266 NULL
)) != LDAP_SUCCESS
)
4268 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4269 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4271 cupsdLogMessage(CUPSD_LOG_ERROR
,
4272 "LDAP add for %s failed with status %d: %s",
4273 p
->name
, rc
, ldap_err2string(rc
));
4274 if (rc
== LDAP_SERVER_DOWN
)
4284 * 'ldap_dereg_printer()' - Delete printer from directory
4288 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4290 char dn
[1024]; /* DN of the printer */
4291 int rc
; /* LDAP status */
4294 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4298 * Reconnect if LDAP Handle is invalid...
4301 if (!BrowseLDAPHandle
)
4308 * Get dn for printer and delete LDAP entry...
4311 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4313 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4315 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4316 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4317 NULL
)) != LDAP_SUCCESS
)
4319 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4320 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4322 cupsdLogMessage(CUPSD_LOG_WARN
,
4323 "LDAP delete for %s failed with status %d: %s",
4324 p
->name
, rc
, ldap_err2string(rc
));
4327 * If we had a connection problem (connection timed out, etc.)
4328 * we should reconnect and try again to delete the entry...
4331 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4333 cupsdLogMessage(CUPSD_LOG_INFO
,
4334 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4337 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4338 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4339 NULL
)) != LDAP_SUCCESS
)
4341 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4342 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4343 cupsdLogMessage(CUPSD_LOG_WARN
,
4344 "LDAP delete for %s failed with status %d: %s",
4345 p
->name
, rc
, ldap_err2string(rc
));
4352 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4353 char *basedn
) /* I - Dase dn */
4355 char dn
[1024]; /* DN of the printer */
4356 int rc
; /* LDAP status */
4359 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4362 * Reconnect if LDAP Handle is invalid...
4365 if (!BrowseLDAPHandle
)
4372 * Get dn for printer and delete LDAP entry...
4375 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4376 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4378 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4379 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4380 NULL
)) != LDAP_SUCCESS
)
4382 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4383 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4385 cupsdLogMessage(CUPSD_LOG_WARN
,
4386 "LDAP delete for %s failed with status %d: %s",
4387 ou
, rc
, ldap_err2string(rc
));
4390 * If we had a connection problem (connection timed out, etc.)
4391 * we should reconnect and try again to delete the entry...
4394 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4396 cupsdLogMessage(CUPSD_LOG_INFO
,
4397 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4399 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4400 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4401 NULL
)) != LDAP_SUCCESS
)
4403 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4404 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4405 cupsdLogMessage(CUPSD_LOG_WARN
,
4406 "LDAP delete for %s failed with status %d: %s",
4407 ou
, rc
, ldap_err2string(rc
));
4412 #endif /* HAVE_LDAP */
4417 * 'send_slp_browse()' - Register the specified printer with SLP.
4421 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4423 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4424 attrs
[8192], /* Printer attributes */
4425 finishings
[1024], /* Finishings to support */
4426 make_model
[IPP_MAX_NAME
* 2],
4427 /* Make and model, quoted */
4428 location
[IPP_MAX_NAME
* 2],
4429 /* Location, quoted */
4430 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4431 *src
, /* Pointer to original string */
4432 *dst
; /* Pointer to destination string */
4433 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4434 SLPError error
; /* SLP error, if any */
4437 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4441 * Make the SLP service URL that conforms to the IANA
4442 * 'printer:' template.
4445 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4447 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4450 * Figure out the finishings string...
4453 if (p
->type
& CUPS_PRINTER_STAPLE
)
4454 strcpy(finishings
, "staple");
4456 finishings
[0] = '\0';
4458 if (p
->type
& CUPS_PRINTER_BIND
)
4461 strlcat(finishings
, ",bind", sizeof(finishings
));
4463 strcpy(finishings
, "bind");
4466 if (p
->type
& CUPS_PRINTER_PUNCH
)
4469 strlcat(finishings
, ",punch", sizeof(finishings
));
4471 strcpy(finishings
, "punch");
4474 if (p
->type
& CUPS_PRINTER_COVER
)
4477 strlcat(finishings
, ",cover", sizeof(finishings
));
4479 strcpy(finishings
, "cover");
4482 if (p
->type
& CUPS_PRINTER_SORT
)
4485 strlcat(finishings
, ",sort", sizeof(finishings
));
4487 strcpy(finishings
, "sort");
4491 strcpy(finishings
, "none");
4494 * Quote any commas in the make and model, location, and info strings...
4497 for (src
= p
->make_model
, dst
= make_model
;
4498 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4500 if (*src
== ',' || *src
== '\\' || *src
== ')')
4509 strcpy(make_model
, "Unknown");
4511 for (src
= p
->location
, dst
= location
;
4512 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4514 if (*src
== ',' || *src
== '\\' || *src
== ')')
4523 strcpy(location
, "Unknown");
4525 for (src
= p
->info
, dst
= info
;
4526 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4528 if (*src
== ',' || *src
== '\\' || *src
== ')')
4537 strcpy(info
, "Unknown");
4540 * Get the authentication value...
4543 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4547 * Make the SLP attribute string list that conforms to
4548 * the IANA 'printer:' template.
4551 snprintf(attrs
, sizeof(attrs
),
4552 "(printer-uri-supported=%s),"
4553 "(uri-authentication-supported=%s>),"
4555 "(uri-security-supported=tls>),"
4557 "(uri-security-supported=none>),"
4558 #endif /* HAVE_SSL */
4559 "(printer-name=%s),"
4560 "(printer-location=%s),"
4561 "(printer-info=%s),"
4562 "(printer-more-info=%s),"
4563 "(printer-make-and-model=%s),"
4564 "(printer-type=%d),"
4565 "(charset-supported=utf-8),"
4566 "(natural-language-configured=%s),"
4567 "(natural-language-supported=de,en,es,fr,it),"
4568 "(color-supported=%s),"
4569 "(finishings-supported=%s),"
4570 "(sides-supported=one-sided%s),"
4571 "(multiple-document-jobs-supported=true)"
4572 "(ipp-versions-supported=1.0,1.1)",
4573 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4574 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4575 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4577 p
->type
& CUPS_PRINTER_DUPLEX
?
4578 ",two-sided-long-edge,two-sided-short-edge" : "");
4580 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4583 * Register the printer with the SLP server...
4586 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4587 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4589 if (error
!= SLP_OK
)
4590 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4596 * 'slp_attr_callback()' - SLP attribute callback
4599 static SLPBoolean
/* O - SLP_TRUE for success */
4601 SLPHandle hslp
, /* I - SLP handle */
4602 const char *attrlist
, /* I - Attribute list */
4603 SLPError errcode
, /* I - Parsing status for this attr */
4604 void *cookie
) /* I - Current printer */
4606 char *tmp
= 0; /* Temporary string */
4607 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4608 /* Current printer */
4611 (void)hslp
; /* anti-compiler-warning-code */
4614 * Bail if there was an error
4617 if (errcode
!= SLP_OK
)
4621 * Parse the attrlist to obtain things needed to build CUPS browse packet
4624 memset(p
, 0, sizeof(cupsd_printer_t
));
4626 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4628 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4630 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4632 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4633 p
->type
= atoi(tmp
);
4635 p
->type
= CUPS_PRINTER_REMOTE
;
4637 cupsdClearString(&tmp
);
4644 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4648 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4650 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4653 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4655 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4658 * Make the SLP service URL that conforms to the IANA
4659 * 'printer:' template.
4662 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4665 * Deregister the printer...
4668 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4674 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4677 static int /* O - 0 on success */
4678 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4679 const char *tag
, /* I - Name of attribute */
4680 char **valbuf
) /* O - Value */
4682 char *ptr1
, /* Pointer into string */
4686 cupsdClearString(valbuf
);
4688 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4690 ptr1
+= strlen(tag
);
4692 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4698 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4699 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4702 * Dequote the value...
4705 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4706 if (*ptr1
== '\\' && ptr1
[1])
4707 _cups_strcpy(ptr1
, ptr1
+ 1);
4718 * 'slp_reg_callback()' - Empty SLPRegReport.
4722 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4723 SLPError errcode
, /* I - Error code, if any */
4724 void *cookie
) /* I - App data */
4735 * 'slp_url_callback()' - SLP service url callback
4738 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4740 SLPHandle hslp
, /* I - SLP handle */
4741 const char *srvurl
, /* I - URL of service */
4742 unsigned short lifetime
, /* I - Life of service */
4743 SLPError errcode
, /* I - Existing error code */
4744 void *cookie
) /* I - Pointer to service list */
4746 slpsrvurl_t
*s
, /* New service entry */
4747 **head
; /* Pointer to head of entry */
4751 * Let the compiler know we won't be using these vars...
4758 * Bail if there was an error
4761 if (errcode
!= SLP_OK
)
4765 * Grab the head of the list...
4768 head
= (slpsrvurl_t
**)cookie
;
4771 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4774 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4778 * Copy the SLP service URL...
4781 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4784 * Link the SLP service URL into the head of the list
4794 #endif /* HAVE_LIBSLP */
4798 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4802 update_cups_browse(void)
4804 int i
; /* Looping var */
4805 int auth
; /* Authorization status */
4806 int len
; /* Length of name string */
4807 int bytes
; /* Number of bytes left */
4808 char packet
[1541], /* Broadcast packet */
4809 *pptr
; /* Pointer into packet */
4810 socklen_t srclen
; /* Length of source address */
4811 http_addr_t srcaddr
; /* Source address */
4812 char srcname
[1024]; /* Source hostname */
4813 unsigned address
[4]; /* Source address */
4814 unsigned type
; /* Printer type */
4815 unsigned state
; /* Printer state */
4816 char uri
[HTTP_MAX_URI
], /* Printer URI */
4817 host
[HTTP_MAX_URI
], /* Host portion of URI */
4818 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4819 info
[IPP_MAX_NAME
], /* Information string */
4820 location
[IPP_MAX_NAME
], /* Location string */
4821 make_model
[IPP_MAX_NAME
];/* Make and model string */
4822 int num_attrs
; /* Number of attributes */
4823 cups_option_t
*attrs
; /* Attributes */
4827 * Read a packet from the browse socket...
4830 srclen
= sizeof(srcaddr
);
4831 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4832 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4835 * "Connection refused" is returned under Linux if the destination port
4836 * or address is unreachable from a previous sendto(); check for the
4837 * error here and ignore it for now...
4840 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4842 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4844 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4847 closesocket(BrowseSocket
);
4849 close(BrowseSocket
);
4852 cupsdRemoveSelect(BrowseSocket
);
4855 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4856 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4862 packet
[bytes
] = '\0';
4865 * If we're about to sleep, ignore incoming browse packets.
4872 * Figure out where it came from...
4876 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4878 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4879 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4880 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4881 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4884 #endif /* AF_INET6 */
4889 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4892 if (HostNameLookups
)
4893 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4895 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4897 len
= strlen(srcname
);
4905 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4908 * Access from localhost (127.0.0.1) is always allowed...
4911 auth
= CUPSD_AUTH_ALLOW
;
4916 * Do authorization checks on the domain/address...
4919 switch (BrowseACL
->order_type
)
4922 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4925 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4926 auth
= CUPSD_AUTH_ALLOW
;
4928 if (cupsdCheckAuth(address
, srcname
, len
,
4929 BrowseACL
->num_deny
, BrowseACL
->deny
))
4930 auth
= CUPSD_AUTH_DENY
;
4932 if (cupsdCheckAuth(address
, srcname
, len
,
4933 BrowseACL
->num_allow
, BrowseACL
->allow
))
4934 auth
= CUPSD_AUTH_ALLOW
;
4937 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
4938 auth
= CUPSD_AUTH_DENY
;
4940 if (cupsdCheckAuth(address
, srcname
, len
,
4941 BrowseACL
->num_allow
, BrowseACL
->allow
))
4942 auth
= CUPSD_AUTH_ALLOW
;
4944 if (cupsdCheckAuth(address
, srcname
, len
,
4945 BrowseACL
->num_deny
, BrowseACL
->deny
))
4946 auth
= CUPSD_AUTH_DENY
;
4952 auth
= CUPSD_AUTH_ALLOW
;
4954 if (auth
== CUPSD_AUTH_DENY
)
4956 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4957 "update_cups_browse: Refused %d bytes from %s", bytes
,
4962 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4963 "update_cups_browse: (%d bytes from %s) %s", bytes
,
4970 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
4972 cupsdLogMessage(CUPSD_LOG_WARN
,
4973 "update_cups_browse: Garbled browse packet - %s", packet
);
4977 strcpy(location
, "Location Unknown");
4978 strcpy(info
, "No Information Available");
4979 make_model
[0] = '\0';
4983 if ((pptr
= strchr(packet
, '\"')) != NULL
)
4986 * Have extended information; can't use sscanf for it because not all
4987 * sscanf's allow empty strings with %[^\"]...
4990 for (i
= 0, pptr
++;
4991 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4993 location
[i
] = *pptr
;
5001 while (*pptr
&& isspace(*pptr
& 255))
5006 for (i
= 0, pptr
++;
5007 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5016 while (*pptr
&& isspace(*pptr
& 255))
5021 for (i
= 0, pptr
++;
5022 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5024 make_model
[i
] = *pptr
;
5029 make_model
[i
] = '\0';
5032 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5038 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5039 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5040 type
, state
, uri
, location
, info
, make_model
));
5043 * Pull the URI apart to see if this is a local or remote printer...
5046 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5048 cupsFreeOptions(num_attrs
, attrs
);
5056 for (i
= 0; i
< NumRelays
; i
++)
5057 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5058 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5059 (struct sockaddr
*)&(Relays
[i
].to
),
5060 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5062 cupsdLogMessage(CUPSD_LOG_ERROR
,
5063 "update_cups_browse: sendto failed for relay %d - %s.",
5064 i
+ 1, strerror(errno
));
5065 cupsFreeOptions(num_attrs
, attrs
);
5070 * Process the browse data...
5073 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5074 (ipp_pstate_t
)state
, location
, info
, make_model
,
5080 * 'update_lpd()' - Update the LPD configuration as needed.
5084 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5091 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5092 * setting for backwards-compatibility.
5095 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5097 #endif /* __APPLE__ */
5099 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5102 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5105 char newfile
[1024]; /* New cups-lpd.N file */
5106 cups_file_t
*ofp
, /* Original file pointer */
5107 *nfp
; /* New file pointer */
5108 char line
[1024]; /* Line from file */
5111 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5113 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5115 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5116 LPDConfigFile
+ 9, strerror(errno
));
5120 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5122 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5123 newfile
, strerror(errno
));
5129 * Copy all of the lines from the cups-lpd file...
5132 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5136 cupsFilePrintf(nfp
, "%s\n", line
);
5137 snprintf(line
, sizeof(line
), "\tdisable = %s",
5138 onoff
? "no" : "yes");
5140 else if (!strstr(line
, "disable ="))
5141 cupsFilePrintf(nfp
, "%s\n", line
);
5146 rename(newfile
, LPDConfigFile
+ 9);
5149 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5152 * Enable/disable LPD via the launchctl command...
5155 char *argv
[5], /* Arguments for command */
5156 *envp
[MAX_ENV
]; /* Environment for command */
5157 int pid
; /* Process ID */
5160 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5161 argv
[0] = (char *)"launchctl";
5162 argv
[1] = (char *)(onoff
? "load" : "unload");
5163 argv
[2] = (char *)"-w";
5164 argv
[3] = LPDConfigFile
+ 10;
5167 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5170 #endif /* __APPLE__ */
5172 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5177 * 'update_polling()' - Read status messages from the poll daemons.
5181 update_polling(void)
5183 char *ptr
, /* Pointer to end of line in buffer */
5184 message
[1024]; /* Pointer to message text */
5185 int loglevel
; /* Log level for message */
5188 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5189 message
, sizeof(message
))) != NULL
)
5190 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5193 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5196 * All polling processes have died; stop polling...
5199 cupsdLogMessage(CUPSD_LOG_ERROR
,
5200 "update_polling: all polling processes have exited!");
5207 * 'update_smb()' - Update the SMB configuration as needed.
5211 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5216 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5219 * Enable/disable SMB via the specified smb.conf config file...
5222 char newfile
[1024]; /* New smb.conf.N file */
5223 cups_file_t
*ofp
, /* Original file pointer */
5224 *nfp
; /* New file pointer */
5225 char line
[1024]; /* Line from file */
5226 int in_printers
; /* In [printers] section? */
5229 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5231 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5233 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5234 SMBConfigFile
+ 8, strerror(errno
));
5238 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5240 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5241 newfile
, strerror(errno
));
5247 * Copy all of the lines from the smb.conf file...
5252 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5254 if (in_printers
&& strstr(line
, "printable ="))
5255 snprintf(line
, sizeof(line
), " printable = %s",
5256 onoff
? "yes" : "no");
5258 cupsFilePrintf(nfp
, "%s\n", line
);
5261 in_printers
= !strcmp(line
, "[printers]");
5266 rename(newfile
, SMBConfigFile
+ 8);
5269 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5274 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".