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
);
439 cupsdLogMessage(CUPSD_LOG_ERROR
,
440 "Syntax error on line %d of remote.cache.", linenum
);
444 cupsdLogMessage(CUPSD_LOG_ERROR
,
445 "Syntax error on line %d of remote.cache.", linenum
);
447 else if (!strcasecmp(line
, "Info"))
450 cupsdSetString(&p
->info
, value
);
452 else if (!strcasecmp(line
, "MakeModel"))
455 cupsdSetString(&p
->make_model
, value
);
457 else if (!strcasecmp(line
, "Location"))
460 cupsdSetString(&p
->location
, value
);
462 else if (!strcasecmp(line
, "DeviceURI"))
466 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
467 username
, sizeof(username
), host
, sizeof(host
), &port
,
468 resource
, sizeof(resource
));
470 cupsdSetString(&p
->hostname
, host
);
471 cupsdSetString(&p
->uri
, value
);
472 cupsdSetDeviceURI(p
, value
);
475 cupsdLogMessage(CUPSD_LOG_ERROR
,
476 "Syntax error on line %d of remote.cache.", linenum
);
478 else if (!strcasecmp(line
, "Option") && value
)
484 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
487 cupsdLogMessage(CUPSD_LOG_ERROR
,
488 "Syntax error on line %d of remote.cache.", linenum
);
491 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
493 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
497 else if (!strcasecmp(line
, "Reason"))
500 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
502 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
506 cupsdLogMessage(CUPSD_LOG_ERROR
,
507 "Syntax error on line %d of remote.cache.", linenum
);
509 else if (!strcasecmp(line
, "State"))
512 * Set the initial queue state...
515 if (value
&& !strcasecmp(value
, "idle"))
516 p
->state
= IPP_PRINTER_IDLE
;
517 else if (value
&& !strcasecmp(value
, "stopped"))
518 p
->state
= IPP_PRINTER_STOPPED
;
520 cupsdLogMessage(CUPSD_LOG_ERROR
,
521 "Syntax error on line %d of remote.cache.", linenum
);
523 else if (!strcasecmp(line
, "StateMessage"))
526 * Set the initial queue state message...
530 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
532 else if (!strcasecmp(line
, "Accepting"))
535 * Set the initial accepting state...
539 (!strcasecmp(value
, "yes") ||
540 !strcasecmp(value
, "on") ||
541 !strcasecmp(value
, "true")))
544 (!strcasecmp(value
, "no") ||
545 !strcasecmp(value
, "off") ||
546 !strcasecmp(value
, "false")))
549 cupsdLogMessage(CUPSD_LOG_ERROR
,
550 "Syntax error on line %d of remote.cache.", linenum
);
552 else if (!strcasecmp(line
, "Type"))
555 p
->type
= atoi(value
);
557 cupsdLogMessage(CUPSD_LOG_ERROR
,
558 "Syntax error on line %d of remote.cache.", linenum
);
560 else if (!strcasecmp(line
, "BrowseTime"))
564 time_t t
= atoi(value
);
566 if (t
> p
->browse_expire
)
567 p
->browse_expire
= t
;
570 cupsdLogMessage(CUPSD_LOG_ERROR
,
571 "Syntax error on line %d of remote.cache.", linenum
);
573 else if (!strcasecmp(line
, "JobSheets"))
576 * Set the initial job sheets...
581 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
586 cupsdSetString(&p
->job_sheets
[0], value
);
588 while (isspace(*valueptr
& 255))
593 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
598 cupsdSetString(&p
->job_sheets
[1], value
);
602 cupsdLogMessage(CUPSD_LOG_ERROR
,
603 "Syntax error on line %d of remote.cache.", linenum
);
605 else if (!strcasecmp(line
, "AllowUser"))
610 cupsdAddPrinterUser(p
, value
);
613 cupsdLogMessage(CUPSD_LOG_ERROR
,
614 "Syntax error on line %d of remote.cache.", linenum
);
616 else if (!strcasecmp(line
, "DenyUser"))
621 cupsdAddPrinterUser(p
, value
);
624 cupsdLogMessage(CUPSD_LOG_ERROR
,
625 "Syntax error on line %d of remote.cache.", linenum
);
630 * Something else we don't understand...
633 cupsdLogMessage(CUPSD_LOG_ERROR
,
634 "Unknown configuration directive %s on line %d of remote.cache.",
642 * Do auto-classing if needed...
645 process_implicit_classes();
650 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
651 * printer or update the broadcast contents.
655 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
657 if (!Browsing
|| !BrowseLocalProtocols
||
658 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
662 /* if (BrowseLocalProtocols & BROWSE_SLP)
663 slpRegisterPrinter(p); */
664 #endif /* HAVE_LIBSLP */
667 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
668 dnssdRegisterPrinter(p
);
669 #endif /* HAVE_DNSSD */
674 * 'cupsdRestartPolling()' - Restart polling servers as needed.
678 cupsdRestartPolling(void)
680 int i
; /* Looping var */
681 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
684 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
686 kill(pollp
->pid
, SIGHUP
);
691 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
695 cupsdSaveRemoteCache(void)
697 int i
; /* Looping var */
698 cups_file_t
*fp
; /* printers.conf file */
699 char temp
[1024]; /* Temporary string */
700 cupsd_printer_t
*printer
; /* Current printer class */
701 time_t curtime
; /* Current time */
702 struct tm
*curdate
; /* Current date */
703 cups_option_t
*option
; /* Current option */
707 * Create the remote.cache file...
710 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
712 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
714 cupsdLogMessage(CUPSD_LOG_ERROR
,
715 "Unable to save remote.cache - %s", strerror(errno
));
719 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
722 * Restrict access to the file...
725 fchown(cupsFileNumber(fp
), getuid(), Group
);
726 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
729 * Write a small header to the file...
732 curtime
= time(NULL
);
733 curdate
= localtime(&curtime
);
734 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
736 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
737 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
740 * Write each local printer known to the system...
743 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
745 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
748 * Skip local destinations...
751 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
755 * Write printers as needed...
758 if (printer
== DefaultPrinter
)
759 cupsFilePuts(fp
, "<Default");
761 cupsFilePutChar(fp
, '<');
763 if (printer
->type
& CUPS_PRINTER_CLASS
)
764 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
766 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
768 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
770 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
773 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
775 if (printer
->make_model
)
776 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
778 if (printer
->location
)
779 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
781 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
783 if (printer
->state
== IPP_PRINTER_STOPPED
)
785 cupsFilePuts(fp
, "State Stopped\n");
786 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
789 cupsFilePuts(fp
, "State Idle\n");
791 for (i
= 0; i
< printer
->num_reasons
; i
++)
792 cupsFilePrintf(fp
, "Reason %s\n", printer
->reasons
[i
]);
794 if (printer
->accepting
)
795 cupsFilePuts(fp
, "Accepting Yes\n");
797 cupsFilePuts(fp
, "Accepting No\n");
799 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
800 printer
->job_sheets
[1]);
802 for (i
= 0; i
< printer
->num_users
; i
++)
803 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
806 for (i
= printer
->num_options
, option
= printer
->options
;
809 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
811 if (printer
->type
& CUPS_PRINTER_CLASS
)
812 cupsFilePuts(fp
, "</Class>\n");
814 cupsFilePuts(fp
, "</Printer>\n");
822 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
826 cupsdSendBrowseList(void)
828 int count
; /* Number of dests to update */
829 cupsd_printer_t
*p
; /* Current printer */
830 time_t ut
, /* Minimum update time */
831 to
; /* Timeout time */
834 if (!Browsing
|| !Printers
)
838 * Compute the update and timeout times...
842 ut
= to
- BrowseInterval
;
845 * Figure out how many printers need an update...
848 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
850 int max_count
; /* Maximum number to update */
854 * Throttle the number of printers we'll be updating this time
855 * around based on the number of queues that need updating and
856 * the maximum number of queues to update each second...
859 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
861 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
862 count
< max_count
&& p
!= NULL
;
863 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
864 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
865 p
->shared
&& p
->browse_time
< ut
)
869 * Loop through all of the printers and send local updates as needed...
873 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
875 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
879 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
882 * Check for wraparound...
886 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
890 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
893 else if (p
->browse_time
< ut
)
896 * Need to send an update...
901 p
->browse_time
= time(NULL
);
903 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
907 if (BrowseLocalProtocols
& BROWSE_SLP
)
909 #endif /* HAVE_LIBSLP */
912 if (BrowseLocalProtocols
& BROWSE_LDAP
)
914 #endif /* HAVE_LDAP */
919 * Save where we left off so that all printers get updated...
926 * Loop through all of the printers and timeout old printers as needed...
929 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
931 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
934 * If this is a remote queue, see if it needs to be timed out...
937 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
938 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
939 p
->browse_expire
< to
)
941 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
942 "%s \'%s\' deleted by directory services (timeout).",
943 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
946 cupsdLogMessage(CUPSD_LOG_DEBUG
,
947 "Remote destination \"%s\" has timed out; "
951 cupsArraySave(Printers
);
952 cupsdDeletePrinter(p
, 1);
953 cupsArrayRestore(Printers
);
954 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
960 #ifdef HAVE_LDAP_REBIND_PROC
961 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
963 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
967 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
968 LDAP_CONST
char *refsp
,
976 * Bind to new LDAP server...
979 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
980 "ldap_rebind_proc: Rebind to %s", refsp
);
982 # if LDAP_API_VERSION > 3000
984 bval
.bv_val
= BrowseLDAPPassword
;
985 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
987 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
989 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
990 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
991 # endif /* LDAP_API_VERSION > 3000 */
996 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
999 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1003 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1015 * Free current values...
1018 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1019 "ldap_rebind_proc: Free values...");
1021 if ( dnp
&& *dnp
) {
1024 if ( passwdp
&& *passwdp
) {
1032 * Return credentials for LDAP referal...
1035 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1036 "ldap_rebind_proc: Return necessary values...");
1038 *dnp
= strdup(BrowseLDAPBindDN
);
1039 *passwdp
= strdup(BrowseLDAPPassword
);
1040 *authmethodp
= LDAP_AUTH_SIMPLE
;
1046 * Should never happen...
1049 cupsdLogMessage(CUPSD_LOG_ERROR
,
1050 "LDAP rebind has been called with wrong freeit value!");
1055 return (LDAP_SUCCESS
);
1057 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1058 #endif /* HAVE_LDAP_REBIND_PROC */
1063 * 'ldap_connect()' - Start new LDAP connection
1070 * Open LDAP handle...
1073 int rc
; /* LDAP API status */
1074 int version
= 3; /* LDAP version */
1075 struct berval bv
= {0, ""}; /* SASL bind value */
1076 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1077 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1078 int ldap_ssl
= 0; /* LDAP SSL indicator */
1079 int ssl_err
= 0; /* LDAP SSL error value */
1080 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1082 # ifdef HAVE_OPENLDAP
1083 # ifdef HAVE_LDAP_SSL
1086 * Set the certificate file to use for encrypted LDAP sessions...
1089 if (BrowseLDAPCACertFile
)
1091 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1092 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1093 BrowseLDAPCACertFile
);
1095 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1096 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1097 cupsdLogMessage(CUPSD_LOG_ERROR
,
1098 "Unable to set CA certificate file for LDAP "
1099 "connections: %d - %s", rc
, ldap_err2string(rc
));
1102 # endif /* HAVE_LDAP_SSL */
1104 * Initialize OPENLDAP connection...
1105 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1108 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1109 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1111 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1113 # else /* HAVE_OPENLDAP */
1115 int ldap_port
= 0; /* LDAP port */
1116 char ldap_protocol
[11], /* LDAP protocol */
1117 ldap_host
[255]; /* LDAP host */
1120 * Split LDAP URI into its components...
1123 if (! BrowseLDAPServer
)
1125 cupsdLogMessage(CUPSD_LOG_ERROR
,
1126 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1127 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1128 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1132 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1134 if (strcmp(ldap_protocol
, "ldap") == 0) {
1136 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1141 cupsdLogMessage(CUPSD_LOG_ERROR
,
1142 "Disable LDAP browsing!");
1143 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1144 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1151 ldap_port
= LDAPS_PORT
;
1153 ldap_port
= LDAP_PORT
;
1156 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1157 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1158 ldap_protocol
, ldap_host
, ldap_port
);
1161 * Initialize LDAP connection...
1166 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1167 rc
= LDAP_OPERATIONS_ERROR
;
1171 # ifdef HAVE_LDAP_SSL
1177 * Initialize SSL LDAP connection...
1179 if (BrowseLDAPCACertFile
)
1181 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1182 if (rc
!= LDAP_SUCCESS
) {
1183 cupsdLogMessage(CUPSD_LOG_ERROR
,
1184 "Failed to initialize LDAP SSL client!");
1185 rc
= LDAP_OPERATIONS_ERROR
;
1187 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1188 rc
= LDAP_OPERATIONS_ERROR
;
1195 cupsdLogMessage(CUPSD_LOG_ERROR
,
1196 "LDAP SSL certificate file/database not configured!");
1197 rc
= LDAP_OPERATIONS_ERROR
;
1200 # else /* HAVE_LDAP_SSL */
1203 * Return error, because client libraries doesn't support SSL
1206 cupsdLogMessage(CUPSD_LOG_ERROR
,
1207 "LDAP client libraries does not support TLS");
1208 rc
= LDAP_OPERATIONS_ERROR
;
1210 # endif /* HAVE_LDAP_SSL */
1212 # endif /* HAVE_OPENLDAP */
1215 * Check return code from LDAP initialize...
1218 if (rc
!= LDAP_SUCCESS
)
1220 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1222 cupsdLogMessage(CUPSD_LOG_ERROR
,
1223 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1227 cupsdLogMessage(CUPSD_LOG_ERROR
,
1228 "Unable to initialize LDAP! Disable LDAP browsing!");
1229 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1230 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1233 ldap_disconnect(TempBrowseLDAPHandle
);
1234 TempBrowseLDAPHandle
= NULL
;
1238 * Upgrade LDAP version...
1241 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1242 (const void *)&version
) != LDAP_SUCCESS
)
1244 cupsdLogMessage(CUPSD_LOG_ERROR
,
1245 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1247 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1248 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1249 ldap_disconnect(TempBrowseLDAPHandle
);
1250 TempBrowseLDAPHandle
= NULL
;
1256 * Register LDAP rebind procedure...
1259 # ifdef HAVE_LDAP_REBIND_PROC
1260 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1262 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1263 if ( rc
!= LDAP_SUCCESS
)
1264 cupsdLogMessage(CUPSD_LOG_ERROR
,
1265 "Setting LDAP rebind function failed with status %d: %s",
1266 rc
, ldap_err2string(rc
));
1270 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1272 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1273 # endif /* HAVE_LDAP_REBIND_PROC */
1276 * Start LDAP bind...
1279 # if LDAP_API_VERSION > 3000
1281 bval
.bv_val
= BrowseLDAPPassword
;
1282 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1284 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1285 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1288 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1290 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1291 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1292 # endif /* LDAP_API_VERSION > 3000 */
1294 if (rc
!= LDAP_SUCCESS
)
1296 cupsdLogMessage(CUPSD_LOG_ERROR
,
1297 "LDAP bind failed with error %d: %s",
1298 rc
, ldap_err2string(rc
));
1299 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1300 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1302 ssl_err
= PORT_GetError();
1304 cupsdLogMessage(CUPSD_LOG_ERROR
,
1305 "LDAP SSL error %d: %s",
1306 ssl_err
, ldapssl_err2string(ssl_err
));
1308 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1309 ldap_disconnect(TempBrowseLDAPHandle
);
1310 TempBrowseLDAPHandle
= NULL
;
1314 cupsdLogMessage(CUPSD_LOG_INFO
,
1315 "LDAP connection established");
1319 return (TempBrowseLDAPHandle
);
1324 * 'ldap_reconnect()' - Reconnect to LDAP Server
1328 ldap_reconnect(void)
1330 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1332 cupsdLogMessage(CUPSD_LOG_INFO
,
1333 "Try LDAP reconnect...");
1336 * Get a new LDAP Handle and replace the global Handle
1337 * if the new connection was successful
1340 TempBrowseLDAPHandle
= ldap_connect();
1342 if (TempBrowseLDAPHandle
!= NULL
)
1344 if (BrowseLDAPHandle
!= NULL
)
1346 ldap_disconnect(BrowseLDAPHandle
);
1348 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1354 * 'ldap_disconnect()' - Disconnect from LDAP Server
1358 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1360 int rc
; /* return code */
1363 * Close LDAP handle...
1366 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1367 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1369 rc
= ldap_unbind_s(ld
);
1370 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1371 if (rc
!= LDAP_SUCCESS
)
1372 cupsdLogMessage(CUPSD_LOG_ERROR
,
1373 "Unbind from LDAP server failed with status %d: %s",
1374 rc
, ldap_err2string(rc
));
1376 #endif /* HAVE_LDAP */
1380 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1384 cupsdStartBrowsing(void)
1386 int val
; /* Socket option value */
1387 struct sockaddr_in addr
; /* Broadcast address */
1388 cupsd_printer_t
*p
; /* Current printer */
1393 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1396 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1398 if (BrowseSocket
< 0)
1401 * Create the broadcast socket...
1404 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1406 cupsdLogMessage(CUPSD_LOG_ERROR
,
1407 "Unable to create broadcast socket - %s.",
1409 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1410 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1412 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1413 cupsdEndProcess(getpid(), 0);
1417 if (BrowseSocket
>= 0)
1420 * Bind the socket to browse port...
1423 memset(&addr
, 0, sizeof(addr
));
1424 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1425 addr
.sin_family
= AF_INET
;
1426 addr
.sin_port
= htons(BrowsePort
);
1428 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1430 cupsdLogMessage(CUPSD_LOG_ERROR
,
1431 "Unable to bind broadcast socket - %s.",
1435 closesocket(BrowseSocket
);
1437 close(BrowseSocket
);
1441 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1442 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1444 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1445 cupsdEndProcess(getpid(), 0);
1449 if (BrowseSocket
>= 0)
1452 * Set the "broadcast" flag...
1456 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1458 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1462 closesocket(BrowseSocket
);
1464 close(BrowseSocket
);
1468 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1469 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1471 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1472 cupsdEndProcess(getpid(), 0);
1476 if (BrowseSocket
>= 0)
1479 * Close the socket on exec...
1482 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1485 * Finally, add the socket to the input selection set as needed...
1488 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1491 * We only listen if we want remote printers...
1494 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1503 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1505 DNSServiceErrorType error
; /* Error from service creation */
1506 cupsd_listener_t
*lis
; /* Current listening socket */
1510 * First create a "master" connection for all registrations...
1513 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1514 != kDNSServiceErr_NoError
)
1516 cupsdLogMessage(CUPSD_LOG_ERROR
,
1517 "Unable to create master DNS-SD reference: %d", error
);
1519 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1520 cupsdEndProcess(getpid(), 0);
1525 * Add the master connection to the select list...
1528 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1529 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1532 * Then get the port we use for registrations. If we are not listening
1533 * on any non-local ports, there is no sense sharing local printers via
1539 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1541 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1543 if (httpAddrLocalhost(&(lis
->address
)))
1546 if (lis
->address
.addr
.sa_family
== AF_INET
)
1548 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1551 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1553 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1559 * Create an array to track the printers we share...
1562 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1563 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1567 * Set the computer name and register the web interface...
1570 cupsdUpdateDNSSDName();
1573 #endif /* HAVE_DNSSD */
1576 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1579 * Open SLP handle...
1582 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1584 cupsdLogMessage(CUPSD_LOG_ERROR
,
1585 "Unable to open an SLP handle; disabling SLP browsing!");
1586 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1587 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1588 BrowseSLPHandle
= NULL
;
1590 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1591 cupsdEndProcess(getpid(), 0);
1594 BrowseSLPRefresh
= 0;
1597 BrowseSLPHandle
= NULL
;
1598 #endif /* HAVE_LIBSLP */
1601 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1605 cupsdLogMessage(CUPSD_LOG_ERROR
,
1606 "Need to set BrowseLDAPDN to use LDAP browsing!");
1607 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1608 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1610 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1611 cupsdEndProcess(getpid(), 0);
1616 * Open LDAP handle...
1619 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1620 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1621 cupsdEndProcess(getpid(), 0);
1624 BrowseLDAPRefresh
= 0;
1626 #endif /* HAVE_LDAP */
1629 * Enable LPD and SMB printer sharing as needed through external programs...
1632 if (BrowseLocalProtocols
& BROWSE_LPD
)
1635 if (BrowseLocalProtocols
& BROWSE_SMB
)
1639 * Register the individual printers
1642 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1644 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1645 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1646 cupsdRegisterPrinter(p
);
1651 * 'cupsdStartPolling()' - Start polling servers as needed.
1655 cupsdStartPolling(void)
1657 int i
; /* Looping var */
1658 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1659 char polld
[1024]; /* Poll daemon path */
1660 char sport
[255]; /* Server port */
1661 char bport
[255]; /* Browser port */
1662 char interval
[255]; /* Poll interval */
1663 int statusfds
[2]; /* Status pipe */
1664 char *argv
[6]; /* Arguments */
1665 char *envp
[100]; /* Environment */
1669 * Don't do anything if we aren't polling...
1672 if (NumPolled
== 0 || BrowseSocket
< 0)
1675 PollStatusBuffer
= NULL
;
1680 * Setup string arguments for polld, port and interval options.
1683 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1685 sprintf(bport
, "%d", BrowsePort
);
1688 sprintf(interval
, "%d", BrowseInterval
);
1690 strcpy(interval
, "30");
1692 argv
[0] = "cups-polld";
1698 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1701 * Create a pipe that receives the status messages from each
1705 if (cupsdOpenPipe(statusfds
))
1707 cupsdLogMessage(CUPSD_LOG_ERROR
,
1708 "Unable to create polling status pipes - %s.",
1711 PollStatusBuffer
= NULL
;
1715 PollPipe
= statusfds
[0];
1716 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1719 * Run each polling daemon, redirecting stderr to the polling pipe...
1722 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1724 sprintf(sport
, "%d", pollp
->port
);
1726 argv
[1] = pollp
->hostname
;
1728 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1729 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1731 cupsdLogMessage(CUPSD_LOG_ERROR
,
1732 "cupsdStartPolling: Unable to fork polling daemon - %s",
1738 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1739 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1740 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1743 close(statusfds
[1]);
1746 * Finally, add the pipe to the input selection set...
1749 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1754 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1758 cupsdStopBrowsing(void)
1760 cupsd_printer_t
*p
; /* Current printer */
1763 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1767 * De-register the individual printers
1770 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1772 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1773 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1774 cupsdDeregisterPrinter(p
, 1);
1777 * Shut down browsing sockets...
1780 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1784 * Close the socket and remove it from the input selection set.
1788 closesocket(BrowseSocket
);
1790 close(BrowseSocket
);
1793 cupsdRemoveSelect(BrowseSocket
);
1798 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1802 DNSServiceRefDeallocate(WebIFRef
);
1808 DNSServiceRefDeallocate(RemoteRef
);
1812 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1814 DNSServiceRefDeallocate(DNSSDRef
);
1817 cupsArrayDelete(DNSSDPrinters
);
1818 DNSSDPrinters
= NULL
;
1822 #endif /* HAVE_DNSSD */
1825 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1829 * Close SLP handle...
1832 SLPClose(BrowseSLPHandle
);
1833 BrowseSLPHandle
= NULL
;
1835 #endif /* HAVE_LIBSLP */
1838 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1841 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1842 ldap_disconnect(BrowseLDAPHandle
);
1843 BrowseLDAPHandle
= NULL
;
1845 #endif /* HAVE_OPENLDAP */
1848 * Disable LPD and SMB printer sharing as needed through external programs...
1851 if (BrowseLocalProtocols
& BROWSE_LPD
)
1854 if (BrowseLocalProtocols
& BROWSE_SMB
)
1860 * 'cupsdStopPolling()' - Stop polling servers as needed.
1864 cupsdStopPolling(void)
1866 int i
; /* Looping var */
1867 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1872 cupsdStatBufDelete(PollStatusBuffer
);
1875 cupsdRemoveSelect(PollPipe
);
1878 PollStatusBuffer
= NULL
;
1881 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1883 cupsdEndProcess(pollp
->pid
, 0);
1889 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1893 cupsdUpdateDNSSDName(void)
1895 DNSServiceErrorType error
; /* Error from service creation */
1896 char webif
[1024]; /* Web interface share name */
1897 #ifdef HAVE_COREFOUNDATION_H
1898 CFStringRef nameRef
; /* Computer name CFString */
1899 char nameBuffer
[1024]; /* C-string buffer */
1900 CFStringEncoding nameEncoding
; /* Computer name encoding */
1901 #endif /* HAVE_COREFOUNDATION_H */
1905 * Only share the web interface and printers when non-local listening is
1913 * Get the computer name as a c-string...
1916 #ifdef HAVE_COREFOUNDATION_H
1917 cupsdClearString(&DNSSDName
);
1919 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1920 &nameEncoding
)) != NULL
)
1922 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1923 kCFStringEncodingUTF8
))
1924 cupsdSetString(&DNSSDName
, nameBuffer
);
1930 cupsdSetString(&DNSSDName
, ServerName
);
1931 #endif /* HAVE_COREFOUNDATION_H */
1934 * Then (re)register the web interface if enabled...
1940 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1942 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1945 DNSServiceRefDeallocate(WebIFRef
);
1947 WebIFRef
= DNSSDRef
;
1948 if ((error
= DNSServiceRegister(&WebIFRef
,
1949 kDNSServiceFlagsShareConnection
,
1950 0, webif
, "_http._tcp", NULL
,
1951 NULL
, htons(DNSSDPort
), 7,
1952 "\006path=/", dnssdRegisterCallback
,
1953 NULL
)) != kDNSServiceErr_NoError
)
1954 cupsdLogMessage(CUPSD_LOG_ERROR
,
1955 "DNS-SD web interface registration failed: %d", error
);
1958 #endif /* HAVE_DNSSD */
1963 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1967 cupsdUpdateLDAPBrowse(void)
1969 char uri
[HTTP_MAX_URI
], /* Printer URI */
1970 host
[HTTP_MAX_URI
], /* Hostname */
1971 resource
[HTTP_MAX_URI
], /* Resource path */
1972 location
[1024], /* Printer location */
1973 info
[1024], /* Printer information */
1974 make_model
[1024], /* Printer make and model */
1975 type_num
[30]; /* Printer type number */
1976 int type
; /* Printer type */
1977 int rc
; /* LDAP status */
1978 int limit
; /* Size limit */
1979 LDAPMessage
*res
, /* LDAP search results */
1980 *e
; /* Current entry from search */
1982 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1984 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1987 * Reconnect if LDAP Handle is invalid...
1990 if (! BrowseLDAPHandle
)
1997 * Search for cups printers in LDAP directory...
2000 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2001 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2004 * If ldap search was successfull then exit function
2005 * and temporary disable LDAP updates...
2008 if (rc
!= LDAP_SUCCESS
)
2010 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2012 BrowseLDAPUpdate
= FALSE
;
2013 cupsdLogMessage(CUPSD_LOG_INFO
,
2014 "LDAP update temporary disabled");
2020 * If LDAP updates were disabled, we will reenable them...
2023 if (! BrowseLDAPUpdate
)
2025 BrowseLDAPUpdate
= TRUE
;
2026 cupsdLogMessage(CUPSD_LOG_INFO
,
2027 "LDAP update enabled");
2031 * Count LDAP entries and return if no entry exist...
2034 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2035 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2043 * Loop through the available printers...
2046 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2048 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2051 * Get the required values from this entry...
2054 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2055 "printerDescription", info
, sizeof(info
)) == -1)
2058 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2059 "printerLocation", location
, sizeof(location
)) == -1)
2062 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2063 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2066 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2067 "printerType", type_num
, sizeof(type_num
)) == -1)
2070 type
= atoi(type_num
);
2072 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2073 "printerURI", uri
, sizeof(uri
)) == -1)
2077 * Process the entry as browse data...
2080 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2081 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2082 location
, info
, make_model
, 0, NULL
);
2088 #endif /* HAVE_LDAP */
2093 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2097 cupsdUpdateSLPBrowse(void)
2099 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2100 *next
; /* Next service in list */
2101 cupsd_printer_t p
; /* Printer information */
2102 const char *uri
; /* Pointer to printer URI */
2103 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2104 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2108 * Reset the refresh time...
2111 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2114 * Poll for remote printers using SLP...
2119 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2120 slp_url_callback
, &s
);
2123 * Loop through the list of available printers...
2129 * Save the "next" pointer...
2135 * Load a cupsd_printer_t structure with the SLP service attributes...
2138 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2141 * Process this printer entry...
2144 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2146 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2149 * Pull the URI apart to see if this is a local or remote printer...
2152 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2153 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2154 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2158 * Free this listing...
2161 cupsdClearString(&p
.info
);
2162 cupsdClearString(&p
.location
);
2163 cupsdClearString(&p
.make_model
);
2168 #endif /* HAVE_LIBSLP */
2172 * 'dequote()' - Remote quotes from a string.
2175 static char * /* O - Dequoted string */
2176 dequote(char *d
, /* I - Destination string */
2177 const char *s
, /* I - Source string */
2178 int dlen
) /* I - Destination length */
2180 char *dptr
; /* Pointer into destination */
2185 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2203 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2206 static char * /* O - TXT record */
2207 dnssdBuildTxtRecord(
2208 int *txt_len
, /* O - TXT record length */
2209 cupsd_printer_t
*p
, /* I - Printer information */
2210 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2212 int i
, j
; /* Looping vars */
2213 char type_str
[32], /* Type to string buffer */
2214 state_str
[32], /* State to string buffer */
2215 rp_str
[1024], /* Queue name string buffer */
2216 air_str
[1024], /* auth-info-required string buffer */
2217 *keyvalue
[32][2]; /* Table of key/value pairs */
2221 * Load up the key value pairs...
2226 keyvalue
[i
][0] = "txtvers";
2227 keyvalue
[i
++][1] = "1";
2229 keyvalue
[i
][0] = "qtotal";
2230 keyvalue
[i
++][1] = "1";
2232 keyvalue
[i
][0] = "rp";
2233 keyvalue
[i
++][1] = rp_str
;
2235 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2237 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2238 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2240 keyvalue
[i
][0] = "ty";
2241 keyvalue
[i
++][1] = p
->make_model
;
2243 if (p
->location
&& *p
->location
!= '\0')
2245 keyvalue
[i
][0] = "note";
2246 keyvalue
[i
++][1] = p
->location
;
2249 keyvalue
[i
][0] = "priority";
2250 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2252 keyvalue
[i
][0] = "product";
2253 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2255 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2256 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2258 keyvalue
[i
][0] = "printer-state";
2259 keyvalue
[i
++][1] = state_str
;
2261 keyvalue
[i
][0] = "printer-type";
2262 keyvalue
[i
++][1] = type_str
;
2264 keyvalue
[i
][0] = "Transparent";
2265 keyvalue
[i
++][1] = "T";
2267 keyvalue
[i
][0] = "Binary";
2268 keyvalue
[i
++][1] = "T";
2270 if ((p
->type
& CUPS_PRINTER_FAX
))
2272 keyvalue
[i
][0] = "Fax";
2273 keyvalue
[i
++][1] = "T";
2276 if ((p
->type
& CUPS_PRINTER_COLOR
))
2278 keyvalue
[i
][0] = "Color";
2279 keyvalue
[i
++][1] = "T";
2282 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2284 keyvalue
[i
][0] = "Duplex";
2285 keyvalue
[i
++][1] = "T";
2288 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2290 keyvalue
[i
][0] = "Staple";
2291 keyvalue
[i
++][1] = "T";
2294 if ((p
->type
& CUPS_PRINTER_COPIES
))
2296 keyvalue
[i
][0] = "Copies";
2297 keyvalue
[i
++][1] = "T";
2300 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2302 keyvalue
[i
][0] = "Collate";
2303 keyvalue
[i
++][1] = "T";
2306 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2308 keyvalue
[i
][0] = "Punch";
2309 keyvalue
[i
++][1] = "T";
2312 if ((p
->type
& CUPS_PRINTER_BIND
))
2314 keyvalue
[i
][0] = "Bind";
2315 keyvalue
[i
++][1] = "T";
2318 if ((p
->type
& CUPS_PRINTER_SORT
))
2320 keyvalue
[i
][0] = "Sort";
2321 keyvalue
[i
++][1] = "T";
2324 keyvalue
[i
][0] = "pdl";
2325 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2327 if (p
->num_auth_info_required
)
2329 char *air
= air_str
; /* Pointer into string */
2332 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2334 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2340 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2344 keyvalue
[i
][0] = "air";
2345 keyvalue
[i
++][1] = air_str
;
2349 * Then pack them into a proper txt record...
2352 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2357 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2360 static int /* O - Result of comparison */
2361 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2362 cupsd_printer_t
*b
)/* I - Second printer */
2364 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2369 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2374 dnssdDeregisterPrinter(
2375 cupsd_printer_t
*p
) /* I - Printer */
2377 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2380 * Closing the socket deregisters the service
2385 DNSServiceRefDeallocate(p
->ipp_ref
);
2389 cupsArrayRemove(DNSSDPrinters
, p
);
2390 cupsdClearString(&p
->reg_name
);
2395 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2405 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2406 * TXT record format.
2409 static char * /* O - TXT record */
2410 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2411 char *keyvalue
[][2], /* I - Table of key value pairs */
2412 int count
) /* I - Items in table */
2414 int i
; /* Looping var */
2415 int length
; /* Length of TXT record */
2416 int length2
; /* Length of value */
2417 char *txtRecord
; /* TXT record buffer */
2418 char *cursor
; /* Looping pointer */
2422 * Calculate the buffer size
2425 for (length
= i
= 0; i
< count
; i
++)
2426 length
+= 1 + strlen(keyvalue
[i
][0]) +
2427 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2430 * Allocate and fill it
2433 txtRecord
= malloc(length
);
2438 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2441 * Drop in the p-string style length byte followed by the data
2444 length
= strlen(keyvalue
[i
][0]);
2445 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2447 *cursor
++ = (unsigned char)(length
+ length2
);
2449 memcpy(cursor
, keyvalue
[i
][0], length
);
2456 memcpy(cursor
, keyvalue
[i
][1], length2
);
2467 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2471 dnssdRegisterCallback(
2472 DNSServiceRef sdRef
, /* I - DNS Service reference */
2473 DNSServiceFlags flags
, /* I - Reserved for future use */
2474 DNSServiceErrorType errorCode
, /* I - Error code */
2475 const char *name
, /* I - Service name */
2476 const char *regtype
, /* I - Service type */
2477 const char *domain
, /* I - Domain. ".local" for now */
2478 void *context
) /* I - User-defined context */
2480 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2481 /* Current printer */
2484 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2485 name
, regtype
, p
? p
->name
: "Web Interface");
2489 cupsdLogMessage(CUPSD_LOG_ERROR
,
2490 "DNSServiceRegister failed with error %d", (int)errorCode
);
2493 else if (p
&& strcasecmp(name
, p
->reg_name
))
2495 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2498 cupsArrayRemove(DNSSDPrinters
, p
);
2499 cupsdSetString(&p
->reg_name
, name
);
2500 cupsArrayAdd(DNSSDPrinters
, p
);
2502 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2508 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2509 * or update the broadcast contents.
2513 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2515 DNSServiceErrorType se
; /* dnssd errors */
2516 char *ipp_txt
, /* IPP TXT record buffer */
2517 *printer_txt
, /* LPD TXT record buffer */
2518 name
[1024], /* Service name */
2519 *nameptr
; /* Pointer into name */
2520 int ipp_len
, /* IPP TXT record length */
2521 printer_len
; /* LPD TXT record length */
2522 char resource
[1024]; /* Resource path for printer */
2523 const char *regtype
; /* Registration type */
2524 const char *domain
; /* Registration domain */
2525 cupsd_location_t
*location
, /* Printer location */
2526 *policy
; /* Operation policy for Print-Job */
2527 unsigned address
[4]; /* INADDR_ANY address */
2530 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2531 !p
->ipp_ref
? "new" : "update");
2534 * If per-printer sharing was just disabled make sure we're not
2535 * registered before returning.
2540 dnssdDeregisterPrinter(p
);
2545 * The registered name takes the form of "<printer-info> @ <computer name>"...
2548 if (p
->info
&& strlen(p
->info
) > 0)
2551 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2553 strlcpy(name
, p
->info
, sizeof(name
));
2556 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2558 strlcpy(name
, p
->name
, sizeof(name
));
2561 * If an existing printer was renamed, unregister it and start over...
2564 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2565 dnssdDeregisterPrinter(p
);
2569 cupsdSetString(&p
->reg_name
, name
);
2570 cupsArrayAdd(DNSSDPrinters
, p
);
2574 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2575 * let dnssd decide on the domain, otherwise restrict it to ".local".
2578 if (p
->type
& CUPS_PRINTER_CLASS
)
2579 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2581 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2583 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2584 location
= cupsdFindBest(resource
, HTTP_POST
);
2585 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2587 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2588 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2594 * Register IPP and (optionally) LPD...
2597 ipp_len
= 0; /* anti-compiler-warning-code */
2598 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2603 * Initial registration. Use the _fax subtype for fax queues...
2606 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2609 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2610 "Registering DNS-SD printer %s with name \"%s\", "
2611 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2612 domain
? domain
: "(null)");
2615 * Register the queue, dropping characters as needed until we succeed...
2618 nameptr
= name
+ strlen(name
);
2622 p
->ipp_ref
= DNSSDRef
;
2623 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2624 0, name
, regtype
, domain
, NULL
,
2625 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2626 dnssdRegisterCallback
,
2627 p
)) == kDNSServiceErr_BadParam
)
2630 * Name is too long, drop trailing characters, taking into account
2636 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2643 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2645 if (se
== kDNSServiceErr_NoError
)
2647 p
->ipp_txt
= ipp_txt
;
2648 p
->ipp_len
= ipp_len
;
2652 cupsdLogMessage(CUPSD_LOG_WARN
,
2653 "DNS-SD IPP registration of \"%s\" failed: %d",
2656 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2659 * Update the existing registration...
2662 /* A TTL of 0 means use record's original value (Radar 3176248) */
2663 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2668 p
->ipp_txt
= ipp_txt
;
2669 p
->ipp_len
= ipp_len
;
2676 if (BrowseLocalProtocols
& BROWSE_LPD
)
2678 printer_len
= 0; /* anti-compiler-warning-code */
2679 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2681 if (!p
->printer_ref
)
2684 * Initial registration...
2687 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2688 "Registering DNS-SD printer %s with name \"%s\", "
2689 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2690 name
, domain
? domain
: "(null)");
2692 p
->printer_ref
= DNSSDRef
;
2693 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2694 kDNSServiceFlagsShareConnection
,
2695 0, name
, "_printer._tcp", domain
, NULL
,
2696 htons(515), printer_len
, printer_txt
,
2697 dnssdRegisterCallback
,
2698 p
)) == kDNSServiceErr_NoError
)
2700 p
->printer_txt
= printer_txt
;
2701 p
->printer_len
= printer_len
;
2705 cupsdLogMessage(CUPSD_LOG_WARN
,
2706 "DNS-SD LPD registration of \"%s\" failed: %d",
2709 else if (printer_len
!= p
->printer_len
||
2710 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2713 * Update the existing registration...
2716 /* A TTL of 0 means use record's original value (Radar 3176248) */
2717 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2721 free(p
->printer_txt
);
2723 p
->printer_txt
= printer_txt
;
2724 p
->printer_len
= printer_len
;
2735 * 'dnssdUpdate()' - Handle DNS-SD queries.
2741 DNSServiceErrorType sdErr
; /* Service discovery error */
2744 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2745 cupsdLogMessage(CUPSD_LOG_ERROR
,
2746 "DNS Service Discovery registration error %d!",
2749 #endif /* HAVE_DNSSD */
2754 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2757 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2758 get_hostconfig(const char *name
) /* I - Name of service */
2760 cups_file_t
*fp
; /* Hostconfig file */
2761 char line
[1024], /* Line from file */
2762 *ptr
; /* Pointer to value */
2763 int state
= 1; /* State of service */
2767 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2768 * the service is enabled/auto.
2771 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2774 * Read lines from the file until we find the service...
2777 while (cupsFileGets(fp
, line
, sizeof(line
)))
2779 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2784 if (!strcasecmp(line
, name
))
2787 * Found the service, see if it is set to "-NO-"...
2790 if (!strncasecmp(ptr
, "-NO-", 4))
2801 #endif /* __APPLE__ */
2805 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2808 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2809 is_local_queue(const char *uri
, /* I - Printer URI */
2810 char *host
, /* O - Host string */
2811 int hostlen
, /* I - Length of host buffer */
2812 char *resource
, /* O - Resource string */
2813 int resourcelen
) /* I - Length of resource buffer */
2815 char scheme
[32], /* Scheme portion of URI */
2816 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2817 int port
; /* Port portion of URI */
2818 cupsd_netif_t
*iface
; /* Network interface */
2822 * Pull the URI apart to see if this is a local or remote printer...
2825 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2826 username
, sizeof(username
), host
, hostlen
, &port
,
2827 resource
, resourcelen
) < HTTP_URI_OK
)
2830 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2833 * Check for local server addresses...
2836 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2841 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2843 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2844 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2848 * If we get here, the printer is remote...
2856 * 'process_browse_data()' - Process new browse data.
2860 process_browse_data(
2861 const char *uri
, /* I - URI of printer/class */
2862 const char *host
, /* I - Hostname */
2863 const char *resource
, /* I - Resource path */
2864 cups_ptype_t type
, /* I - Printer type */
2865 ipp_pstate_t state
, /* I - Printer state */
2866 const char *location
, /* I - Printer location */
2867 const char *info
, /* I - Printer information */
2868 const char *make_model
, /* I - Printer make and model */
2869 int num_attrs
, /* I - Number of attributes */
2870 cups_option_t
*attrs
) /* I - Attributes */
2872 int i
; /* Looping var */
2873 int update
; /* Update printer attributes? */
2874 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2875 name
[IPP_MAX_NAME
], /* Name of printer */
2876 newname
[IPP_MAX_NAME
], /* New name of printer */
2877 *hptr
, /* Pointer into hostname */
2878 *sptr
; /* Pointer into ServerName */
2879 const char *shortname
; /* Short queue name (queue) */
2880 char local_make_model
[IPP_MAX_NAME
];
2881 /* Local make and model */
2882 cupsd_printer_t
*p
; /* Printer information */
2883 const char *ipp_options
, /* ipp-options value */
2884 *lease_duration
; /* lease-duration value */
2885 int is_class
; /* Is this queue a class? */
2888 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2889 "process_browse_data(uri=\"%s\", host=\"%s\", "
2890 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2891 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2892 uri
, host
, resource
, type
, state
,
2893 location
? location
: "(nil)", info
? info
: "(nil)",
2894 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2897 * Determine if the URI contains any illegal characters in it...
2900 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2901 (strncmp(resource
, "/printers/", 10) &&
2902 strncmp(resource
, "/classes/", 9)))
2904 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2908 if (strchr(resource
, '?') ||
2909 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2910 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2912 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2918 * OK, this isn't a local printer; add any remote options...
2921 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2923 if (BrowseRemoteOptions
)
2925 if (BrowseRemoteOptions
[0] == '?')
2928 * Override server-supplied options...
2931 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2933 else if (ipp_options
)
2936 * Combine the server and local options...
2939 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2940 BrowseRemoteOptions
);
2945 * Just use the local options...
2948 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2953 else if (ipp_options
)
2956 * Just use the server-supplied options...
2959 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2964 * See if we already have it listed in the Printers list, and add it if not...
2967 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2968 type
&= ~CUPS_PRINTER_IMPLICIT
;
2970 hptr
= strchr(host
, '.');
2971 sptr
= strchr(ServerName
, '.');
2972 is_class
= type
& CUPS_PRINTER_CLASS
;
2974 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2977 * Strip the common domain name components...
2980 while (hptr
!= NULL
)
2982 if (!strcasecmp(hptr
, sptr
))
2988 hptr
= strchr(hptr
+ 1, '.');
2995 * Remote destination is a class...
2998 if (!strncmp(resource
, "/classes/", 9))
2999 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3003 shortname
= resource
+ 9;
3008 * Remote destination is a printer...
3011 if (!strncmp(resource
, "/printers/", 10))
3012 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3016 shortname
= resource
+ 10;
3020 *hptr
= '.'; /* Resource FQDN */
3022 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3025 * Long name doesn't exist, try short name...
3028 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3031 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3034 * Short name doesn't exist, use it for this shared queue.
3037 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3039 strlcpy(name
, shortname
, sizeof(name
));
3044 * Short name exists...
3047 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3048 "process_browse_data: %s found, type=%x, hostname=%s...",
3049 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3051 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3052 p
= NULL
; /* Don't replace implicit classes */
3053 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3056 * Short name exists but is for a different host. If this is a remote
3057 * queue, rename it and use the long name...
3060 if (p
->type
& CUPS_PRINTER_REMOTE
)
3062 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3063 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3064 is_class
? "class" : "printer", p
->name
, p
->name
,
3066 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3067 "%s \'%s\' deleted by directory services.",
3068 is_class
? "Class" : "Printer", p
->name
);
3070 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3071 cupsdRenamePrinter(p
, newname
);
3073 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3074 "%s \'%s\' added by directory services.",
3075 is_class
? "Class" : "Printer", p
->name
);
3079 * Force creation with long name...
3087 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3088 "process_browse_data: %s found, type=%x, hostname=%s...",
3089 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3094 * Queue doesn't exist; add it...
3098 p
= cupsdAddClass(name
);
3100 p
= cupsdAddPrinter(name
);
3105 cupsdClearString(&(p
->hostname
));
3107 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3108 is_class
? "class" : "printer", name
);
3110 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3111 "%s \'%s\' added by directory services.",
3112 is_class
? "Class" : "Printer", name
);
3115 * Force the URI to point to the real server...
3118 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3121 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3127 * Hostname not set, so this must be a cached remote printer
3128 * that was created for a pending print job...
3131 cupsdSetString(&p
->hostname
, host
);
3132 cupsdSetString(&p
->uri
, uri
);
3133 cupsdSetString(&p
->device_uri
, uri
);
3136 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3140 * Update the state...
3144 p
->browse_time
= time(NULL
);
3146 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3150 * Grab the lease-duration for the browse data; anything less then 1
3151 * second or more than 1 week gets the default BrowseTimeout...
3154 i
= atoi(lease_duration
);
3155 if (i
< 1 || i
> 604800)
3158 p
->browse_expire
= p
->browse_time
+ i
;
3161 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3163 if (type
& CUPS_PRINTER_REJECTING
)
3165 type
&= ~CUPS_PRINTER_REJECTING
;
3173 else if (!p
->accepting
)
3179 if (p
->type
!= type
)
3185 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3187 cupsdSetString(&p
->location
, location
);
3191 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3193 cupsdSetString(&p
->info
, info
);
3196 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3199 if (!make_model
|| !make_model
[0])
3202 snprintf(local_make_model
, sizeof(local_make_model
),
3203 "Remote Class on %s", host
);
3205 snprintf(local_make_model
, sizeof(local_make_model
),
3206 "Remote Printer on %s", host
);
3209 snprintf(local_make_model
, sizeof(local_make_model
),
3210 "%s on %s", make_model
, host
);
3212 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3214 cupsdSetString(&p
->make_model
, local_make_model
);
3220 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3223 * See if we need to update the attributes...
3226 if (p
->num_options
!= num_attrs
)
3230 for (i
= 0; i
< num_attrs
; i
++)
3231 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3232 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3233 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3242 * Free the old options...
3245 cupsFreeOptions(p
->num_options
, p
->options
);
3248 p
->num_options
= num_attrs
;
3251 if (type
& CUPS_PRINTER_DELETE
)
3253 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3254 "%s \'%s\' deleted by directory services.",
3255 is_class
? "Class" : "Printer", p
->name
);
3257 cupsdExpireSubscriptions(p
, NULL
);
3259 cupsdDeletePrinter(p
, 1);
3260 cupsdUpdateImplicitClasses();
3261 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3265 cupsdSetPrinterAttrs(p
);
3266 cupsdUpdateImplicitClasses();
3270 * See if we have a default printer... If not, make the first network
3271 * default printer the default.
3274 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3277 * Find the first network default printer and use it...
3280 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3282 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3283 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3286 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3292 * Do auto-classing if needed...
3295 process_implicit_classes();
3300 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3304 process_implicit_classes(void)
3306 int i
; /* Looping var */
3307 int update
; /* Update printer attributes? */
3308 char name
[IPP_MAX_NAME
], /* Name of printer */
3309 *hptr
; /* Pointer into hostname */
3310 cupsd_printer_t
*p
, /* Printer information */
3311 *pclass
, /* Printer class */
3312 *first
; /* First printer in class */
3313 int offset
, /* Offset of name */
3314 len
; /* Length of name */
3317 if (!ImplicitClasses
|| !Printers
)
3321 * Loop through all available printers and create classes as needed...
3324 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3325 update
= 0, pclass
= NULL
, first
= NULL
;
3327 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3330 * Skip implicit classes...
3333 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3340 * If len == 0, get the length of this printer name up to the "@"
3344 cupsArraySave(Printers
);
3347 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3348 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3351 * We have more than one printer with the same name; see if
3352 * we have a class, and if this printer is a member...
3355 if (pclass
&& strcasecmp(pclass
->name
, name
))
3358 cupsdSetPrinterAttrs(pclass
);
3364 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3367 * Need to add the class...
3370 pclass
= cupsdAddPrinter(name
);
3371 cupsArrayAdd(ImplicitPrinters
, pclass
);
3373 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3374 pclass
->accepting
= 1;
3375 pclass
->state
= IPP_PRINTER_IDLE
;
3377 cupsdSetString(&pclass
->location
, p
->location
);
3378 cupsdSetString(&pclass
->info
, p
->info
);
3380 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3381 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3385 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3387 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3389 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3390 "Implicit class \'%s\' added by directory services.",
3396 for (i
= 0; i
< pclass
->num_printers
; i
++)
3397 if (pclass
->printers
[i
] == first
)
3400 if (i
>= pclass
->num_printers
)
3402 first
->in_implicit_class
= 1;
3403 cupsdAddPrinterToClass(pclass
, first
);
3409 for (i
= 0; i
< pclass
->num_printers
; i
++)
3410 if (pclass
->printers
[i
] == p
)
3413 if (i
>= pclass
->num_printers
)
3415 p
->in_implicit_class
= 1;
3416 cupsdAddPrinterToClass(pclass
, p
);
3423 * First time around; just get name length and mark it as first
3427 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3428 len
= hptr
- p
->name
;
3430 len
= strlen(p
->name
);
3432 strncpy(name
, p
->name
, len
);
3436 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3437 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3440 * Can't use same name as a local printer; add "Any" to the
3441 * front of the name, unless we have explicitly disabled
3442 * the "ImplicitAnyClasses"...
3445 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3448 * Add "Any" to the class name...
3451 strcpy(name
, "Any");
3452 strncpy(name
+ 3, p
->name
, len
);
3453 name
[len
+ 3] = '\0';
3459 * Don't create an implicit class if we have a local printer
3460 * with the same name...
3464 cupsArrayRestore(Printers
);
3472 cupsArrayRestore(Printers
);
3476 * Update the last printer class as needed...
3479 if (pclass
&& update
)
3480 cupsdSetPrinterAttrs(pclass
);
3485 * 'send_cups_browse()' - Send new browsing information using the CUPS
3490 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3492 int i
; /* Looping var */
3493 cups_ptype_t type
; /* Printer type */
3494 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3495 int bytes
; /* Length of packet */
3496 char packet
[1453], /* Browse data packet */
3497 uri
[1024], /* Printer URI */
3498 location
[1024], /* printer-location */
3499 info
[1024], /* printer-info */
3501 /* printer-make-and-model */
3502 cupsd_netif_t
*iface
; /* Network interface */
3506 * Figure out the printer type value...
3509 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3512 type
|= CUPS_PRINTER_REJECTING
;
3514 if (p
== DefaultPrinter
)
3515 type
|= CUPS_PRINTER_DEFAULT
;
3518 * Remove quotes from printer-info, printer-location, and
3519 * printer-make-and-model attributes...
3522 dequote(location
, p
->location
, sizeof(location
));
3523 dequote(info
, p
->info
, sizeof(info
));
3526 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3527 else if (p
->type
& CUPS_PRINTER_CLASS
)
3529 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3530 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3532 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3535 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3537 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3540 * Send a packet to each browse address...
3543 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3547 * Send the browse packet to one or more interfaces...
3550 if (!strcmp(b
->iface
, "*"))
3553 * Send to all local interfaces...
3558 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3560 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3563 * Only send to local, IPv4 interfaces...
3566 if (!iface
->is_local
|| !iface
->port
||
3567 iface
->address
.addr
.sa_family
!= AF_INET
)
3570 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3571 iface
->hostname
, iface
->port
,
3572 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3575 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3576 type
, p
->state
, uri
, location
, info
, make_model
,
3577 p
->browse_attrs
? p
->browse_attrs
: "");
3579 bytes
= strlen(packet
);
3581 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3582 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3583 iface
->name
, packet
);
3585 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3587 sendto(BrowseSocket
, packet
, bytes
, 0,
3588 (struct sockaddr
*)&(iface
->broadcast
),
3589 httpAddrLength(&(iface
->broadcast
)));
3592 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3595 * Send to the named interface using the IPv4 address...
3599 if (strcmp(b
->iface
, iface
->name
))
3604 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3607 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3611 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3612 iface
->hostname
, iface
->port
,
3613 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3616 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3617 type
, p
->state
, uri
, location
, info
, make_model
,
3618 p
->browse_attrs
? p
->browse_attrs
: "");
3620 bytes
= strlen(packet
);
3622 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3623 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3624 iface
->name
, packet
);
3626 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3628 sendto(BrowseSocket
, packet
, bytes
, 0,
3629 (struct sockaddr
*)&(iface
->broadcast
),
3630 httpAddrLength(&(iface
->broadcast
)));
3637 * Send the browse packet to the indicated address using
3638 * the default server name...
3641 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3642 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3643 p
->browse_attrs
? p
->browse_attrs
: "");
3645 bytes
= strlen(packet
);
3646 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3647 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3649 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3650 (struct sockaddr
*)&(b
->to
),
3651 httpAddrLength(&(b
->to
))) <= 0)
3654 * Unable to send browse packet, so remove this address from the
3658 cupsdLogMessage(CUPSD_LOG_ERROR
,
3659 "cupsdSendBrowseList: sendto failed for browser "
3661 (int)(b
- Browsers
+ 1), strerror(errno
));
3664 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3675 * 'ldap_search_rec()' - LDAP Search with reconnect
3679 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3680 char *base
, /* I - Base dn */
3681 int scope
, /* I - LDAP search scope */
3682 char *filter
, /* I - Filter string */
3683 char *attrs
[], /* I - Requested attributes */
3684 int attrsonly
, /* I - Return only attributes? */
3685 LDAPMessage
**res
) /* I - LDAP handler */
3687 int rc
; /* Return code */
3690 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3691 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3692 NULL
, LDAP_NO_LIMIT
, res
);
3694 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3695 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3698 * If we have a connection problem try again...
3701 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3703 cupsdLogMessage(CUPSD_LOG_ERROR
,
3704 "LDAP search failed with status %d: %s",
3705 rc
, ldap_err2string(rc
));
3706 cupsdLogMessage(CUPSD_LOG_INFO
,
3707 "We try the LDAP search once again after reconnecting to "
3712 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3713 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3714 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3716 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3717 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3720 if (rc
== LDAP_NO_SUCH_OBJECT
)
3721 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3722 "ldap_search_rec: LDAP entry/object not found");
3723 else if (rc
!= LDAP_SUCCESS
)
3724 cupsdLogMessage(CUPSD_LOG_ERROR
,
3725 "ldap_search_rec: LDAP search failed with status %d: %s",
3726 rc
, ldap_err2string(rc
));
3728 if (rc
!= LDAP_SUCCESS
)
3736 * 'ldap_freeres()' - Free LDAPMessage
3740 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3742 int rc
; /* Return value */
3745 rc
= ldap_msgfree(entry
);
3747 cupsdLogMessage(CUPSD_LOG_WARN
,
3748 "Can't free LDAPMessage!");
3750 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3751 "Freeing LDAPMessage was unnecessary");
3756 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3760 ldap_getval_firststring(
3761 LDAP
*ld
, /* I - LDAP handler */
3762 LDAPMessage
*entry
, /* I - LDAP message or search result */
3763 char *attr
, /* I - the wanted attribute */
3764 char *retval
, /* O - String to return */
3765 unsigned long maxsize
) /* I - Max string size */
3767 char *dn
; /* LDAP DN */
3768 int rc
= 0; /* Return code */
3769 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3770 struct berval
**bval
; /* LDAP value array */
3771 unsigned long size
; /* String size */
3775 * Get value from LDAPMessage...
3778 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3781 dn
= ldap_get_dn(ld
, entry
);
3782 cupsdLogMessage(CUPSD_LOG_WARN
,
3783 "Failed to get LDAP value %s for %s!",
3791 * Check size and copy value into our string...
3795 if (size
< bval
[0]->bv_len
)
3798 dn
= ldap_get_dn(ld
, entry
);
3799 cupsdLogMessage(CUPSD_LOG_WARN
,
3800 "Attribute %s is too big! (dn: %s)",
3805 size
= bval
[0]->bv_len
;
3807 strlcpy(retval
, bval
[0]->bv_val
, size
);
3808 ldap_value_free_len(bval
);
3811 char **value
; /* LDAP value */
3814 * Get value from LDAPMessage...
3817 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3820 dn
= ldap_get_dn(ld
, entry
);
3821 cupsdLogMessage(CUPSD_LOG_WARN
,
3822 "Failed to get LDAP value %s for %s!",
3828 strlcpy(retval
, *value
, maxsize
);
3829 ldap_value_free(value
);
3831 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3838 * 'send_ldap_ou()' - Send LDAP ou registrations.
3842 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3843 char *basedn
, /* I - Our base dn */
3844 char *descstring
) /* I - Description for ou */
3846 int i
; /* Looping var... */
3847 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3848 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3849 LDAPMessage
*res
, /* Search result token */
3850 *e
; /* Current entry from search */
3851 int rc
; /* LDAP status */
3852 char dn
[1024], /* DN of the organizational unit we are adding */
3853 *desc
[2], /* Change records */
3855 char old_desc
[1024]; /* Old description */
3856 static const char * const objectClass_values
[] =
3857 { /* The 2 objectClass's we use in */
3858 "top", /* our LDAP entries */
3859 "organizationalUnit",
3862 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3869 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3872 * Reconnect if LDAP Handle is invalid...
3875 if (! BrowseLDAPHandle
)
3877 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3878 "send_ldap_ou: LDAP Handle is invalid. Try "
3885 * Prepare ldap search...
3888 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3889 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3893 desc
[0] = descstring
;
3896 mods
[0].mod_type
= "ou";
3897 mods
[0].mod_values
= ou_value
;
3898 mods
[1].mod_type
= "description";
3899 mods
[1].mod_values
= desc
;
3900 mods
[2].mod_type
= "objectClass";
3901 mods
[2].mod_values
= (char **)objectClass_values
;
3903 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3904 (char **)ou_attrs
, 0, &res
);
3907 * If ldap search was not successfull then exit function...
3910 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3914 * Check if we need to insert or update the LDAP entry...
3917 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3918 rc
!= LDAP_NO_SUCH_OBJECT
)
3921 * Printserver has already been registered, check if
3922 * modification is required...
3925 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
3928 * Get the required values from this entry...
3931 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
3932 sizeof(old_desc
)) == -1)
3936 * Check if modification is required...
3939 if ( strcmp(desc
[0], old_desc
) == 0 )
3942 * LDAP entry for the printer exists.
3943 * Printer has already been registered,
3944 * no modifications required...
3946 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3947 "send_ldap_ou: No updates required for %s", ou
);
3952 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3953 "send_ldap_ou: Replace entry for %s", ou
);
3955 for (i
= 0; i
< 3; i
++)
3957 pmods
[i
] = mods
+ i
;
3958 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3962 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3963 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3964 NULL
)) != LDAP_SUCCESS
)
3966 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3967 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3969 cupsdLogMessage(CUPSD_LOG_ERROR
,
3970 "LDAP modify for %s failed with status %d: %s",
3971 ou
, rc
, ldap_err2string(rc
));
3972 if ( LDAP_SERVER_DOWN
== rc
)
3980 * Printserver has never been registered,
3981 * add registration...
3984 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3985 "send_ldap_ou: Add entry for %s", ou
);
3987 for (i
= 0; i
< 3; i
++)
3989 pmods
[i
] = mods
+ i
;
3990 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3994 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3995 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3996 NULL
)) != LDAP_SUCCESS
)
3998 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3999 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4001 cupsdLogMessage(CUPSD_LOG_ERROR
,
4002 "LDAP add for %s failed with status %d: %s",
4003 ou
, rc
, ldap_err2string(rc
));
4004 if ( LDAP_SERVER_DOWN
== rc
)
4014 * 'send_ldap_browse()' - Send LDAP printer registrations.
4018 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4020 int i
; /* Looping var... */
4021 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4022 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4023 LDAPMessage
*res
, /* Search result token */
4024 *e
; /* Current entry from search */
4025 char *cn_value
[2], /* Change records */
4031 typestring
[255], /* String to hold printer-type */
4032 dn
[1024]; /* DN of the printer we are adding */
4033 int rc
; /* LDAP status */
4034 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4035 old_location
[1024], /* Printer location */
4036 old_info
[1024], /* Printer information */
4037 old_make_model
[1024], /* Printer make and model */
4038 old_type_string
[30]; /* Temporary type number */
4039 int old_type
; /* Printer type */
4040 static const char * const objectClass_values
[] =
4041 { /* The 3 objectClass's we use in */
4042 "top", /* our LDAP entries */
4049 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4052 * Exit function if LDAP updates has been disabled...
4055 if (!BrowseLDAPUpdate
)
4057 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4058 "send_ldap_browse: Updates temporary disabled; "
4064 * Reconnect if LDAP Handle is invalid...
4067 if (!BrowseLDAPHandle
)
4069 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4070 "send_ldap_browse: LDAP Handle is invalid. Try "
4077 * Everything in ldap is ** so we fudge around it...
4080 sprintf(typestring
, "%u", p
->type
);
4082 cn_value
[0] = p
->name
;
4084 info
[0] = p
->info
? p
->info
: "Unknown";
4086 location
[0] = p
->location
? p
->location
: "Unknown";
4088 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4089 make_model
[1] = NULL
;
4090 type
[0] = typestring
;
4096 * Get ldap entry for printer ...
4099 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4101 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4103 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4104 (char **)ldap_attrs
, 0, &res
);
4107 * If ldap search was not successfull then exit function
4108 * and temporary disable LDAP updates...
4111 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4113 if (BrowseLDAPUpdate
&&
4114 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4116 BrowseLDAPUpdate
= FALSE
;
4117 cupsdLogMessage(CUPSD_LOG_INFO
,
4118 "LDAP update temporary disabled");
4125 * Fill modification array...
4128 mods
[0].mod_type
= "cn";
4129 mods
[0].mod_values
= cn_value
;
4130 mods
[1].mod_type
= "printerDescription";
4131 mods
[1].mod_values
= info
;
4132 mods
[2].mod_type
= "printerURI";
4133 mods
[2].mod_values
= uri
;
4134 mods
[3].mod_type
= "printerLocation";
4135 mods
[3].mod_values
= location
;
4136 mods
[4].mod_type
= "printerMakeAndModel";
4137 mods
[4].mod_values
= make_model
;
4138 mods
[5].mod_type
= "printerType";
4139 mods
[5].mod_values
= type
;
4140 mods
[6].mod_type
= "objectClass";
4141 mods
[6].mod_values
= (char **)objectClass_values
;
4144 * Check if we need to insert or update the LDAP entry...
4147 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4148 rc
!= LDAP_NO_SUCH_OBJECT
)
4151 * Printer has already been registered, check if
4152 * modification is required...
4155 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4158 * Get the required values from this entry...
4161 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4162 old_info
, sizeof(old_info
)) == -1)
4165 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4166 old_location
, sizeof(old_location
)) == -1)
4169 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4170 old_make_model
, sizeof(old_make_model
)) == -1)
4173 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4174 old_type_string
, sizeof(old_type_string
)) == -1)
4177 old_type
= atoi(old_type_string
);
4179 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4180 sizeof(old_uri
)) == -1)
4184 * Check if modification is required...
4187 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4188 !strcmp(location
[0], old_location
) &&
4189 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4192 * LDAP entry for the printer exists. Printer has already been registered,
4193 * no modifications required...
4196 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4197 "send_ldap_browse: No updates required for %s", p
->name
);
4202 * LDAP entry for the printer exists. Printer has already been registered,
4203 * modify the current registration...
4206 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4207 "send_ldap_browse: Replace entry for %s", p
->name
);
4209 for (i
= 0; i
< 7; i
++)
4211 pmods
[i
] = mods
+ i
;
4212 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4216 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4217 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4218 NULL
)) != LDAP_SUCCESS
)
4220 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4221 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4223 cupsdLogMessage(CUPSD_LOG_ERROR
,
4224 "LDAP modify for %s failed with status %d: %s",
4225 p
->name
, rc
, ldap_err2string(rc
));
4226 if (rc
== LDAP_SERVER_DOWN
)
4234 * No LDAP entry exists for the printer. Printer has never been registered,
4235 * add the current registration...
4238 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4240 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4241 "send_ldap_browse: Add entry for %s", p
->name
);
4243 for (i
= 0; i
< 7; i
++)
4245 pmods
[i
] = mods
+ i
;
4246 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4250 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4251 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4252 NULL
)) != LDAP_SUCCESS
)
4254 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4255 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4257 cupsdLogMessage(CUPSD_LOG_ERROR
,
4258 "LDAP add for %s failed with status %d: %s",
4259 p
->name
, rc
, ldap_err2string(rc
));
4260 if (rc
== LDAP_SERVER_DOWN
)
4270 * 'ldap_dereg_printer()' - Delete printer from directory
4274 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4276 char dn
[1024]; /* DN of the printer */
4277 int rc
; /* LDAP status */
4280 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4284 * Reconnect if LDAP Handle is invalid...
4287 if (!BrowseLDAPHandle
)
4294 * Get dn for printer and delete LDAP entry...
4297 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4299 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4301 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4302 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4303 NULL
)) != LDAP_SUCCESS
)
4305 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4306 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4308 cupsdLogMessage(CUPSD_LOG_WARN
,
4309 "LDAP delete for %s failed with status %d: %s",
4310 p
->name
, rc
, ldap_err2string(rc
));
4313 * If we had a connection problem (connection timed out, etc.)
4314 * we should reconnect and try again to delete the entry...
4317 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4319 cupsdLogMessage(CUPSD_LOG_INFO
,
4320 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4323 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4324 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4325 NULL
)) != LDAP_SUCCESS
)
4327 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4328 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4329 cupsdLogMessage(CUPSD_LOG_WARN
,
4330 "LDAP delete for %s failed with status %d: %s",
4331 p
->name
, rc
, ldap_err2string(rc
));
4338 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4339 char *basedn
) /* I - Dase dn */
4341 char dn
[1024]; /* DN of the printer */
4342 int rc
; /* LDAP status */
4345 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4348 * Reconnect if LDAP Handle is invalid...
4351 if (!BrowseLDAPHandle
)
4358 * Get dn for printer and delete LDAP entry...
4361 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4362 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4364 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4365 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4366 NULL
)) != LDAP_SUCCESS
)
4368 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4369 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4371 cupsdLogMessage(CUPSD_LOG_WARN
,
4372 "LDAP delete for %s failed with status %d: %s",
4373 ou
, rc
, ldap_err2string(rc
));
4376 * If we had a connection problem (connection timed out, etc.)
4377 * we should reconnect and try again to delete the entry...
4380 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4382 cupsdLogMessage(CUPSD_LOG_INFO
,
4383 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4385 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4386 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4387 NULL
)) != LDAP_SUCCESS
)
4389 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4390 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4391 cupsdLogMessage(CUPSD_LOG_WARN
,
4392 "LDAP delete for %s failed with status %d: %s",
4393 ou
, rc
, ldap_err2string(rc
));
4398 #endif /* HAVE_LDAP */
4403 * 'send_slp_browse()' - Register the specified printer with SLP.
4407 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4409 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4410 attrs
[8192], /* Printer attributes */
4411 finishings
[1024], /* Finishings to support */
4412 make_model
[IPP_MAX_NAME
* 2],
4413 /* Make and model, quoted */
4414 location
[IPP_MAX_NAME
* 2],
4415 /* Location, quoted */
4416 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4417 *src
, /* Pointer to original string */
4418 *dst
; /* Pointer to destination string */
4419 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4420 SLPError error
; /* SLP error, if any */
4423 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4427 * Make the SLP service URL that conforms to the IANA
4428 * 'printer:' template.
4431 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4433 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4436 * Figure out the finishings string...
4439 if (p
->type
& CUPS_PRINTER_STAPLE
)
4440 strcpy(finishings
, "staple");
4442 finishings
[0] = '\0';
4444 if (p
->type
& CUPS_PRINTER_BIND
)
4447 strlcat(finishings
, ",bind", sizeof(finishings
));
4449 strcpy(finishings
, "bind");
4452 if (p
->type
& CUPS_PRINTER_PUNCH
)
4455 strlcat(finishings
, ",punch", sizeof(finishings
));
4457 strcpy(finishings
, "punch");
4460 if (p
->type
& CUPS_PRINTER_COVER
)
4463 strlcat(finishings
, ",cover", sizeof(finishings
));
4465 strcpy(finishings
, "cover");
4468 if (p
->type
& CUPS_PRINTER_SORT
)
4471 strlcat(finishings
, ",sort", sizeof(finishings
));
4473 strcpy(finishings
, "sort");
4477 strcpy(finishings
, "none");
4480 * Quote any commas in the make and model, location, and info strings...
4483 for (src
= p
->make_model
, dst
= make_model
;
4484 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4486 if (*src
== ',' || *src
== '\\' || *src
== ')')
4495 strcpy(make_model
, "Unknown");
4497 for (src
= p
->location
, dst
= location
;
4498 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4500 if (*src
== ',' || *src
== '\\' || *src
== ')')
4509 strcpy(location
, "Unknown");
4511 for (src
= p
->info
, dst
= info
;
4512 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4514 if (*src
== ',' || *src
== '\\' || *src
== ')')
4523 strcpy(info
, "Unknown");
4526 * Get the authentication value...
4529 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4533 * Make the SLP attribute string list that conforms to
4534 * the IANA 'printer:' template.
4537 snprintf(attrs
, sizeof(attrs
),
4538 "(printer-uri-supported=%s),"
4539 "(uri-authentication-supported=%s>),"
4541 "(uri-security-supported=tls>),"
4543 "(uri-security-supported=none>),"
4544 #endif /* HAVE_SSL */
4545 "(printer-name=%s),"
4546 "(printer-location=%s),"
4547 "(printer-info=%s),"
4548 "(printer-more-info=%s),"
4549 "(printer-make-and-model=%s),"
4550 "(printer-type=%d),"
4551 "(charset-supported=utf-8),"
4552 "(natural-language-configured=%s),"
4553 "(natural-language-supported=de,en,es,fr,it),"
4554 "(color-supported=%s),"
4555 "(finishings-supported=%s),"
4556 "(sides-supported=one-sided%s),"
4557 "(multiple-document-jobs-supported=true)"
4558 "(ipp-versions-supported=1.0,1.1)",
4559 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4560 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4561 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4563 p
->type
& CUPS_PRINTER_DUPLEX
?
4564 ",two-sided-long-edge,two-sided-short-edge" : "");
4566 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4569 * Register the printer with the SLP server...
4572 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4573 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4575 if (error
!= SLP_OK
)
4576 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4582 * 'slp_attr_callback()' - SLP attribute callback
4585 static SLPBoolean
/* O - SLP_TRUE for success */
4587 SLPHandle hslp
, /* I - SLP handle */
4588 const char *attrlist
, /* I - Attribute list */
4589 SLPError errcode
, /* I - Parsing status for this attr */
4590 void *cookie
) /* I - Current printer */
4592 char *tmp
= 0; /* Temporary string */
4593 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4594 /* Current printer */
4597 (void)hslp
; /* anti-compiler-warning-code */
4600 * Bail if there was an error
4603 if (errcode
!= SLP_OK
)
4607 * Parse the attrlist to obtain things needed to build CUPS browse packet
4610 memset(p
, 0, sizeof(cupsd_printer_t
));
4612 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4614 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4616 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4618 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4619 p
->type
= atoi(tmp
);
4621 p
->type
= CUPS_PRINTER_REMOTE
;
4623 cupsdClearString(&tmp
);
4630 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4634 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4636 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4639 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4641 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4644 * Make the SLP service URL that conforms to the IANA
4645 * 'printer:' template.
4648 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4651 * Deregister the printer...
4654 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4660 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4663 static int /* O - 0 on success */
4664 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4665 const char *tag
, /* I - Name of attribute */
4666 char **valbuf
) /* O - Value */
4668 char *ptr1
, /* Pointer into string */
4672 cupsdClearString(valbuf
);
4674 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4676 ptr1
+= strlen(tag
);
4678 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4684 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4685 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4688 * Dequote the value...
4691 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4692 if (*ptr1
== '\\' && ptr1
[1])
4693 _cups_strcpy(ptr1
, ptr1
+ 1);
4704 * 'slp_reg_callback()' - Empty SLPRegReport.
4708 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4709 SLPError errcode
, /* I - Error code, if any */
4710 void *cookie
) /* I - App data */
4721 * 'slp_url_callback()' - SLP service url callback
4724 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4726 SLPHandle hslp
, /* I - SLP handle */
4727 const char *srvurl
, /* I - URL of service */
4728 unsigned short lifetime
, /* I - Life of service */
4729 SLPError errcode
, /* I - Existing error code */
4730 void *cookie
) /* I - Pointer to service list */
4732 slpsrvurl_t
*s
, /* New service entry */
4733 **head
; /* Pointer to head of entry */
4737 * Let the compiler know we won't be using these vars...
4744 * Bail if there was an error
4747 if (errcode
!= SLP_OK
)
4751 * Grab the head of the list...
4754 head
= (slpsrvurl_t
**)cookie
;
4757 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4760 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4764 * Copy the SLP service URL...
4767 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4770 * Link the SLP service URL into the head of the list
4780 #endif /* HAVE_LIBSLP */
4784 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4788 update_cups_browse(void)
4790 int i
; /* Looping var */
4791 int auth
; /* Authorization status */
4792 int len
; /* Length of name string */
4793 int bytes
; /* Number of bytes left */
4794 char packet
[1541], /* Broadcast packet */
4795 *pptr
; /* Pointer into packet */
4796 socklen_t srclen
; /* Length of source address */
4797 http_addr_t srcaddr
; /* Source address */
4798 char srcname
[1024]; /* Source hostname */
4799 unsigned address
[4]; /* Source address */
4800 unsigned type
; /* Printer type */
4801 unsigned state
; /* Printer state */
4802 char uri
[HTTP_MAX_URI
], /* Printer URI */
4803 host
[HTTP_MAX_URI
], /* Host portion of URI */
4804 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4805 info
[IPP_MAX_NAME
], /* Information string */
4806 location
[IPP_MAX_NAME
], /* Location string */
4807 make_model
[IPP_MAX_NAME
];/* Make and model string */
4808 int num_attrs
; /* Number of attributes */
4809 cups_option_t
*attrs
; /* Attributes */
4813 * Read a packet from the browse socket...
4816 srclen
= sizeof(srcaddr
);
4817 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4818 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4821 * "Connection refused" is returned under Linux if the destination port
4822 * or address is unreachable from a previous sendto(); check for the
4823 * error here and ignore it for now...
4826 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4828 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4830 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4833 closesocket(BrowseSocket
);
4835 close(BrowseSocket
);
4838 cupsdRemoveSelect(BrowseSocket
);
4841 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4842 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4848 packet
[bytes
] = '\0';
4851 * If we're about to sleep, ignore incoming browse packets.
4858 * Figure out where it came from...
4862 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4864 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4865 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4866 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4867 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4870 #endif /* AF_INET6 */
4875 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4878 if (HostNameLookups
)
4879 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4881 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4883 len
= strlen(srcname
);
4891 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4894 * Access from localhost (127.0.0.1) is always allowed...
4897 auth
= CUPSD_AUTH_ALLOW
;
4902 * Do authorization checks on the domain/address...
4905 switch (BrowseACL
->order_type
)
4908 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4911 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4912 auth
= CUPSD_AUTH_ALLOW
;
4914 if (cupsdCheckAuth(address
, srcname
, len
,
4915 BrowseACL
->num_deny
, BrowseACL
->deny
))
4916 auth
= CUPSD_AUTH_DENY
;
4918 if (cupsdCheckAuth(address
, srcname
, len
,
4919 BrowseACL
->num_allow
, BrowseACL
->allow
))
4920 auth
= CUPSD_AUTH_ALLOW
;
4923 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
4924 auth
= CUPSD_AUTH_DENY
;
4926 if (cupsdCheckAuth(address
, srcname
, len
,
4927 BrowseACL
->num_allow
, BrowseACL
->allow
))
4928 auth
= CUPSD_AUTH_ALLOW
;
4930 if (cupsdCheckAuth(address
, srcname
, len
,
4931 BrowseACL
->num_deny
, BrowseACL
->deny
))
4932 auth
= CUPSD_AUTH_DENY
;
4938 auth
= CUPSD_AUTH_ALLOW
;
4940 if (auth
== CUPSD_AUTH_DENY
)
4942 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4943 "update_cups_browse: Refused %d bytes from %s", bytes
,
4948 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4949 "update_cups_browse: (%d bytes from %s) %s", bytes
,
4956 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
4958 cupsdLogMessage(CUPSD_LOG_WARN
,
4959 "update_cups_browse: Garbled browse packet - %s", packet
);
4963 strcpy(location
, "Location Unknown");
4964 strcpy(info
, "No Information Available");
4965 make_model
[0] = '\0';
4969 if ((pptr
= strchr(packet
, '\"')) != NULL
)
4972 * Have extended information; can't use sscanf for it because not all
4973 * sscanf's allow empty strings with %[^\"]...
4976 for (i
= 0, pptr
++;
4977 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4979 location
[i
] = *pptr
;
4987 while (*pptr
&& isspace(*pptr
& 255))
4992 for (i
= 0, pptr
++;
4993 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5002 while (*pptr
&& isspace(*pptr
& 255))
5007 for (i
= 0, pptr
++;
5008 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5010 make_model
[i
] = *pptr
;
5015 make_model
[i
] = '\0';
5018 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5024 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5025 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5026 type
, state
, uri
, location
, info
, make_model
));
5029 * Pull the URI apart to see if this is a local or remote printer...
5032 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5034 cupsFreeOptions(num_attrs
, attrs
);
5042 for (i
= 0; i
< NumRelays
; i
++)
5043 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5044 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5045 (struct sockaddr
*)&(Relays
[i
].to
),
5046 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5048 cupsdLogMessage(CUPSD_LOG_ERROR
,
5049 "update_cups_browse: sendto failed for relay %d - %s.",
5050 i
+ 1, strerror(errno
));
5051 cupsFreeOptions(num_attrs
, attrs
);
5056 * Process the browse data...
5059 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5060 (ipp_pstate_t
)state
, location
, info
, make_model
,
5066 * 'update_lpd()' - Update the LPD configuration as needed.
5070 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5077 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5078 * setting for backwards-compatibility.
5081 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5083 #endif /* __APPLE__ */
5085 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5088 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5091 char newfile
[1024]; /* New cups-lpd.N file */
5092 cups_file_t
*ofp
, /* Original file pointer */
5093 *nfp
; /* New file pointer */
5094 char line
[1024]; /* Line from file */
5097 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5099 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5101 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5102 LPDConfigFile
+ 9, strerror(errno
));
5106 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5108 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5109 newfile
, strerror(errno
));
5115 * Copy all of the lines from the cups-lpd file...
5118 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5122 cupsFilePrintf(nfp
, "%s\n", line
);
5123 snprintf(line
, sizeof(line
), "\tdisable = %s",
5124 onoff
? "no" : "yes");
5126 else if (!strstr(line
, "disable ="))
5127 cupsFilePrintf(nfp
, "%s\n", line
);
5132 rename(newfile
, LPDConfigFile
+ 9);
5135 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5138 * Enable/disable LPD via the launchctl command...
5141 char *argv
[5], /* Arguments for command */
5142 *envp
[MAX_ENV
]; /* Environment for command */
5143 int pid
; /* Process ID */
5146 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5147 argv
[0] = (char *)"launchctl";
5148 argv
[1] = (char *)(onoff
? "load" : "unload");
5149 argv
[2] = (char *)"-w";
5150 argv
[3] = LPDConfigFile
+ 10;
5153 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5156 #endif /* __APPLE__ */
5158 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5163 * 'update_polling()' - Read status messages from the poll daemons.
5167 update_polling(void)
5169 char *ptr
, /* Pointer to end of line in buffer */
5170 message
[1024]; /* Pointer to message text */
5171 int loglevel
; /* Log level for message */
5174 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5175 message
, sizeof(message
))) != NULL
)
5176 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5179 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5182 * All polling processes have died; stop polling...
5185 cupsdLogMessage(CUPSD_LOG_ERROR
,
5186 "update_polling: all polling processes have exited!");
5193 * 'update_smb()' - Update the SMB configuration as needed.
5197 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5202 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5205 * Enable/disable SMB via the specified smb.conf config file...
5208 char newfile
[1024]; /* New smb.conf.N file */
5209 cups_file_t
*ofp
, /* Original file pointer */
5210 *nfp
; /* New file pointer */
5211 char line
[1024]; /* Line from file */
5212 int in_printers
; /* In [printers] section? */
5215 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5217 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5219 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5220 SMBConfigFile
+ 8, strerror(errno
));
5224 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5226 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5227 newfile
, strerror(errno
));
5233 * Copy all of the lines from the smb.conf file...
5238 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5240 if (in_printers
&& strstr(line
, "printable ="))
5241 snprintf(line
, sizeof(line
), " printable = %s",
5242 onoff
? "yes" : "no");
5244 cupsFilePrintf(nfp
, "%s\n", line
);
5247 in_printers
= !strcmp(line
, "[printers]");
5252 rename(newfile
, SMBConfigFile
+ 8);
5255 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5260 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".