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 value
[2048]; /* Value string */
701 cupsd_printer_t
*printer
; /* Current printer class */
702 time_t curtime
; /* Current time */
703 struct tm
*curdate
; /* Current date */
704 cups_option_t
*option
; /* Current option */
708 * Create the remote.cache file...
711 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
713 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
715 cupsdLogMessage(CUPSD_LOG_ERROR
,
716 "Unable to save remote.cache - %s", strerror(errno
));
720 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
723 * Restrict access to the file...
726 fchown(cupsFileNumber(fp
), getuid(), Group
);
727 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
730 * Write a small header to the file...
733 curtime
= time(NULL
);
734 curdate
= localtime(&curtime
);
735 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
737 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
738 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
741 * Write each local printer known to the system...
744 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
746 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
749 * Skip local destinations...
752 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
756 * Write printers as needed...
759 if (printer
== DefaultPrinter
)
760 cupsFilePuts(fp
, "<Default");
762 cupsFilePutChar(fp
, '<');
764 if (printer
->type
& CUPS_PRINTER_CLASS
)
765 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
767 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
769 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
772 cupsFilePutConf(fp
, "Info", printer
->info
);
774 if (printer
->location
)
775 cupsFilePutConf(fp
, "Location", printer
->location
);
777 if (printer
->make_model
)
778 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
780 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
782 if (printer
->state
== IPP_PRINTER_STOPPED
)
783 cupsFilePuts(fp
, "State Stopped\n");
785 cupsFilePuts(fp
, "State Idle\n");
787 for (i
= 0; i
< printer
->num_reasons
; i
++)
788 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
790 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
792 if (printer
->accepting
)
793 cupsFilePuts(fp
, "Accepting Yes\n");
795 cupsFilePuts(fp
, "Accepting No\n");
797 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
798 printer
->job_sheets
[1]);
799 cupsFilePutConf(fp
, "JobSheets", value
);
801 for (i
= 0; i
< printer
->num_users
; i
++)
802 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
805 for (i
= printer
->num_options
, option
= printer
->options
;
809 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
810 cupsFilePutConf(fp
, "Option", value
);
813 if (printer
->type
& CUPS_PRINTER_CLASS
)
814 cupsFilePuts(fp
, "</Class>\n");
816 cupsFilePuts(fp
, "</Printer>\n");
824 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
828 cupsdSendBrowseList(void)
830 int count
; /* Number of dests to update */
831 cupsd_printer_t
*p
; /* Current printer */
832 time_t ut
, /* Minimum update time */
833 to
; /* Timeout time */
836 if (!Browsing
|| !Printers
)
840 * Compute the update and timeout times...
844 ut
= to
- BrowseInterval
;
847 * Figure out how many printers need an update...
850 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
852 int max_count
; /* Maximum number to update */
856 * Throttle the number of printers we'll be updating this time
857 * around based on the number of queues that need updating and
858 * the maximum number of queues to update each second...
861 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
863 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
864 count
< max_count
&& p
!= NULL
;
865 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
866 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
867 p
->shared
&& p
->browse_time
< ut
)
871 * Loop through all of the printers and send local updates as needed...
875 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
877 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
881 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
884 * Check for wraparound...
888 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
892 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
895 else if (p
->browse_time
< ut
)
898 * Need to send an update...
903 p
->browse_time
= time(NULL
);
905 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
909 if (BrowseLocalProtocols
& BROWSE_SLP
)
911 #endif /* HAVE_LIBSLP */
914 if (BrowseLocalProtocols
& BROWSE_LDAP
)
916 #endif /* HAVE_LDAP */
921 * Save where we left off so that all printers get updated...
928 * Loop through all of the printers and timeout old printers as needed...
931 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
933 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
936 * If this is a remote queue, see if it needs to be timed out...
939 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
940 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
941 p
->browse_expire
< to
)
943 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
944 "%s \'%s\' deleted by directory services (timeout).",
945 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
948 cupsdLogMessage(CUPSD_LOG_DEBUG
,
949 "Remote destination \"%s\" has timed out; "
953 cupsArraySave(Printers
);
954 cupsdDeletePrinter(p
, 1);
955 cupsArrayRestore(Printers
);
956 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
962 #ifdef HAVE_LDAP_REBIND_PROC
963 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
965 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
969 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
970 LDAP_CONST
char *refsp
,
978 * Bind to new LDAP server...
981 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
982 "ldap_rebind_proc: Rebind to %s", refsp
);
984 # if LDAP_API_VERSION > 3000
986 bval
.bv_val
= BrowseLDAPPassword
;
987 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
989 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
991 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
992 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
993 # endif /* LDAP_API_VERSION > 3000 */
998 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1001 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1005 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1017 * Free current values...
1020 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1021 "ldap_rebind_proc: Free values...");
1023 if ( dnp
&& *dnp
) {
1026 if ( passwdp
&& *passwdp
) {
1034 * Return credentials for LDAP referal...
1037 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1038 "ldap_rebind_proc: Return necessary values...");
1040 *dnp
= strdup(BrowseLDAPBindDN
);
1041 *passwdp
= strdup(BrowseLDAPPassword
);
1042 *authmethodp
= LDAP_AUTH_SIMPLE
;
1048 * Should never happen...
1051 cupsdLogMessage(CUPSD_LOG_ERROR
,
1052 "LDAP rebind has been called with wrong freeit value!");
1057 return (LDAP_SUCCESS
);
1059 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1060 #endif /* HAVE_LDAP_REBIND_PROC */
1065 * 'ldap_connect()' - Start new LDAP connection
1072 * Open LDAP handle...
1075 int rc
; /* LDAP API status */
1076 int version
= 3; /* LDAP version */
1077 struct berval bv
= {0, ""}; /* SASL bind value */
1078 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1079 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1080 int ldap_ssl
= 0; /* LDAP SSL indicator */
1081 int ssl_err
= 0; /* LDAP SSL error value */
1082 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1084 # ifdef HAVE_OPENLDAP
1085 # ifdef HAVE_LDAP_SSL
1088 * Set the certificate file to use for encrypted LDAP sessions...
1091 if (BrowseLDAPCACertFile
)
1093 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1094 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1095 BrowseLDAPCACertFile
);
1097 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1098 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1099 cupsdLogMessage(CUPSD_LOG_ERROR
,
1100 "Unable to set CA certificate file for LDAP "
1101 "connections: %d - %s", rc
, ldap_err2string(rc
));
1104 # endif /* HAVE_LDAP_SSL */
1106 * Initialize OPENLDAP connection...
1107 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1110 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1111 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1113 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1115 # else /* HAVE_OPENLDAP */
1117 int ldap_port
= 0; /* LDAP port */
1118 char ldap_protocol
[11], /* LDAP protocol */
1119 ldap_host
[255]; /* LDAP host */
1122 * Split LDAP URI into its components...
1125 if (! BrowseLDAPServer
)
1127 cupsdLogMessage(CUPSD_LOG_ERROR
,
1128 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1129 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1130 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1134 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1136 if (strcmp(ldap_protocol
, "ldap") == 0) {
1138 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1141 cupsdLogMessage(CUPSD_LOG_ERROR
,
1142 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1143 cupsdLogMessage(CUPSD_LOG_ERROR
,
1144 "Disable LDAP browsing!");
1145 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1146 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1153 ldap_port
= LDAPS_PORT
;
1155 ldap_port
= LDAP_PORT
;
1158 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1159 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1160 ldap_protocol
, ldap_host
, ldap_port
);
1163 * Initialize LDAP connection...
1168 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1169 rc
= LDAP_OPERATIONS_ERROR
;
1173 # ifdef HAVE_LDAP_SSL
1179 * Initialize SSL LDAP connection...
1181 if (BrowseLDAPCACertFile
)
1183 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1184 if (rc
!= LDAP_SUCCESS
) {
1185 cupsdLogMessage(CUPSD_LOG_ERROR
,
1186 "Failed to initialize LDAP SSL client!");
1187 rc
= LDAP_OPERATIONS_ERROR
;
1189 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1190 rc
= LDAP_OPERATIONS_ERROR
;
1197 cupsdLogMessage(CUPSD_LOG_ERROR
,
1198 "LDAP SSL certificate file/database not configured!");
1199 rc
= LDAP_OPERATIONS_ERROR
;
1202 # else /* HAVE_LDAP_SSL */
1205 * Return error, because client libraries doesn't support SSL
1208 cupsdLogMessage(CUPSD_LOG_ERROR
,
1209 "LDAP client libraries does not support TLS");
1210 rc
= LDAP_OPERATIONS_ERROR
;
1212 # endif /* HAVE_LDAP_SSL */
1214 # endif /* HAVE_OPENLDAP */
1217 * Check return code from LDAP initialize...
1220 if (rc
!= LDAP_SUCCESS
)
1222 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1224 cupsdLogMessage(CUPSD_LOG_ERROR
,
1225 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1229 cupsdLogMessage(CUPSD_LOG_ERROR
,
1230 "Unable to initialize LDAP! Disable LDAP browsing!");
1231 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1232 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1235 ldap_disconnect(TempBrowseLDAPHandle
);
1236 TempBrowseLDAPHandle
= NULL
;
1240 * Upgrade LDAP version...
1243 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1244 (const void *)&version
) != LDAP_SUCCESS
)
1246 cupsdLogMessage(CUPSD_LOG_ERROR
,
1247 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1249 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1250 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1251 ldap_disconnect(TempBrowseLDAPHandle
);
1252 TempBrowseLDAPHandle
= NULL
;
1258 * Register LDAP rebind procedure...
1261 # ifdef HAVE_LDAP_REBIND_PROC
1262 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1264 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1265 if ( rc
!= LDAP_SUCCESS
)
1266 cupsdLogMessage(CUPSD_LOG_ERROR
,
1267 "Setting LDAP rebind function failed with status %d: %s",
1268 rc
, ldap_err2string(rc
));
1272 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1274 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1275 # endif /* HAVE_LDAP_REBIND_PROC */
1278 * Start LDAP bind...
1281 # if LDAP_API_VERSION > 3000
1283 bval
.bv_val
= BrowseLDAPPassword
;
1284 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1286 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1287 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1290 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1292 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1293 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1294 # endif /* LDAP_API_VERSION > 3000 */
1296 if (rc
!= LDAP_SUCCESS
)
1298 cupsdLogMessage(CUPSD_LOG_ERROR
,
1299 "LDAP bind failed with error %d: %s",
1300 rc
, ldap_err2string(rc
));
1301 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1302 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1304 ssl_err
= PORT_GetError();
1306 cupsdLogMessage(CUPSD_LOG_ERROR
,
1307 "LDAP SSL error %d: %s",
1308 ssl_err
, ldapssl_err2string(ssl_err
));
1310 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1311 ldap_disconnect(TempBrowseLDAPHandle
);
1312 TempBrowseLDAPHandle
= NULL
;
1316 cupsdLogMessage(CUPSD_LOG_INFO
,
1317 "LDAP connection established");
1321 return (TempBrowseLDAPHandle
);
1326 * 'ldap_reconnect()' - Reconnect to LDAP Server
1330 ldap_reconnect(void)
1332 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1334 cupsdLogMessage(CUPSD_LOG_INFO
,
1335 "Try LDAP reconnect...");
1338 * Get a new LDAP Handle and replace the global Handle
1339 * if the new connection was successful
1342 TempBrowseLDAPHandle
= ldap_connect();
1344 if (TempBrowseLDAPHandle
!= NULL
)
1346 if (BrowseLDAPHandle
!= NULL
)
1348 ldap_disconnect(BrowseLDAPHandle
);
1350 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1356 * 'ldap_disconnect()' - Disconnect from LDAP Server
1360 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1362 int rc
; /* return code */
1365 * Close LDAP handle...
1368 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1369 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1371 rc
= ldap_unbind_s(ld
);
1372 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1373 if (rc
!= LDAP_SUCCESS
)
1374 cupsdLogMessage(CUPSD_LOG_ERROR
,
1375 "Unbind from LDAP server failed with status %d: %s",
1376 rc
, ldap_err2string(rc
));
1378 #endif /* HAVE_LDAP */
1382 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1386 cupsdStartBrowsing(void)
1388 int val
; /* Socket option value */
1389 struct sockaddr_in addr
; /* Broadcast address */
1390 cupsd_printer_t
*p
; /* Current printer */
1395 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1398 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1400 if (BrowseSocket
< 0)
1403 * Create the broadcast socket...
1406 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1408 cupsdLogMessage(CUPSD_LOG_ERROR
,
1409 "Unable to create broadcast socket - %s.",
1411 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1412 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1414 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1415 cupsdEndProcess(getpid(), 0);
1419 if (BrowseSocket
>= 0)
1422 * Bind the socket to browse port...
1425 memset(&addr
, 0, sizeof(addr
));
1426 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1427 addr
.sin_family
= AF_INET
;
1428 addr
.sin_port
= htons(BrowsePort
);
1430 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1432 cupsdLogMessage(CUPSD_LOG_ERROR
,
1433 "Unable to bind broadcast socket - %s.",
1437 closesocket(BrowseSocket
);
1439 close(BrowseSocket
);
1443 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1444 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1446 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1447 cupsdEndProcess(getpid(), 0);
1451 if (BrowseSocket
>= 0)
1454 * Set the "broadcast" flag...
1458 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1460 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1464 closesocket(BrowseSocket
);
1466 close(BrowseSocket
);
1470 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1471 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1473 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1474 cupsdEndProcess(getpid(), 0);
1478 if (BrowseSocket
>= 0)
1481 * Close the socket on exec...
1484 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1487 * Finally, add the socket to the input selection set as needed...
1490 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1493 * We only listen if we want remote printers...
1496 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1505 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1507 DNSServiceErrorType error
; /* Error from service creation */
1508 cupsd_listener_t
*lis
; /* Current listening socket */
1512 * First create a "master" connection for all registrations...
1515 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1516 != kDNSServiceErr_NoError
)
1518 cupsdLogMessage(CUPSD_LOG_ERROR
,
1519 "Unable to create master DNS-SD reference: %d", error
);
1521 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1522 cupsdEndProcess(getpid(), 0);
1527 * Add the master connection to the select list...
1530 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1531 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1534 * Then get the port we use for registrations. If we are not listening
1535 * on any non-local ports, there is no sense sharing local printers via
1541 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1543 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1545 if (httpAddrLocalhost(&(lis
->address
)))
1548 if (lis
->address
.addr
.sa_family
== AF_INET
)
1550 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1553 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1555 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1561 * Create an array to track the printers we share...
1564 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1565 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1569 * Set the computer name and register the web interface...
1572 cupsdUpdateDNSSDName();
1575 #endif /* HAVE_DNSSD */
1578 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1581 * Open SLP handle...
1584 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1586 cupsdLogMessage(CUPSD_LOG_ERROR
,
1587 "Unable to open an SLP handle; disabling SLP browsing!");
1588 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1589 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1590 BrowseSLPHandle
= NULL
;
1592 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1593 cupsdEndProcess(getpid(), 0);
1596 BrowseSLPRefresh
= 0;
1599 BrowseSLPHandle
= NULL
;
1600 #endif /* HAVE_LIBSLP */
1603 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1607 cupsdLogMessage(CUPSD_LOG_ERROR
,
1608 "Need to set BrowseLDAPDN to use LDAP browsing!");
1609 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1610 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1612 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1613 cupsdEndProcess(getpid(), 0);
1618 * Open LDAP handle...
1621 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1622 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1623 cupsdEndProcess(getpid(), 0);
1626 BrowseLDAPRefresh
= 0;
1628 #endif /* HAVE_LDAP */
1631 * Enable LPD and SMB printer sharing as needed through external programs...
1634 if (BrowseLocalProtocols
& BROWSE_LPD
)
1637 if (BrowseLocalProtocols
& BROWSE_SMB
)
1641 * Register the individual printers
1644 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1646 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1647 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1648 cupsdRegisterPrinter(p
);
1653 * 'cupsdStartPolling()' - Start polling servers as needed.
1657 cupsdStartPolling(void)
1659 int i
; /* Looping var */
1660 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1661 char polld
[1024]; /* Poll daemon path */
1662 char sport
[255]; /* Server port */
1663 char bport
[255]; /* Browser port */
1664 char interval
[255]; /* Poll interval */
1665 int statusfds
[2]; /* Status pipe */
1666 char *argv
[6]; /* Arguments */
1667 char *envp
[100]; /* Environment */
1671 * Don't do anything if we aren't polling...
1674 if (NumPolled
== 0 || BrowseSocket
< 0)
1677 PollStatusBuffer
= NULL
;
1682 * Setup string arguments for polld, port and interval options.
1685 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1687 sprintf(bport
, "%d", BrowsePort
);
1690 sprintf(interval
, "%d", BrowseInterval
);
1692 strcpy(interval
, "30");
1694 argv
[0] = "cups-polld";
1700 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1703 * Create a pipe that receives the status messages from each
1707 if (cupsdOpenPipe(statusfds
))
1709 cupsdLogMessage(CUPSD_LOG_ERROR
,
1710 "Unable to create polling status pipes - %s.",
1713 PollStatusBuffer
= NULL
;
1717 PollPipe
= statusfds
[0];
1718 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1721 * Run each polling daemon, redirecting stderr to the polling pipe...
1724 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1726 sprintf(sport
, "%d", pollp
->port
);
1728 argv
[1] = pollp
->hostname
;
1730 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1731 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1733 cupsdLogMessage(CUPSD_LOG_ERROR
,
1734 "cupsdStartPolling: Unable to fork polling daemon - %s",
1740 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1741 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1742 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1745 close(statusfds
[1]);
1748 * Finally, add the pipe to the input selection set...
1751 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1756 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1760 cupsdStopBrowsing(void)
1762 cupsd_printer_t
*p
; /* Current printer */
1765 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1769 * De-register the individual printers
1772 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1774 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1775 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1776 cupsdDeregisterPrinter(p
, 1);
1779 * Shut down browsing sockets...
1782 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1786 * Close the socket and remove it from the input selection set.
1790 closesocket(BrowseSocket
);
1792 close(BrowseSocket
);
1795 cupsdRemoveSelect(BrowseSocket
);
1800 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1804 DNSServiceRefDeallocate(WebIFRef
);
1810 DNSServiceRefDeallocate(RemoteRef
);
1814 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1816 DNSServiceRefDeallocate(DNSSDRef
);
1819 cupsArrayDelete(DNSSDPrinters
);
1820 DNSSDPrinters
= NULL
;
1824 #endif /* HAVE_DNSSD */
1827 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1831 * Close SLP handle...
1834 SLPClose(BrowseSLPHandle
);
1835 BrowseSLPHandle
= NULL
;
1837 #endif /* HAVE_LIBSLP */
1840 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1843 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1844 ldap_disconnect(BrowseLDAPHandle
);
1845 BrowseLDAPHandle
= NULL
;
1847 #endif /* HAVE_OPENLDAP */
1850 * Disable LPD and SMB printer sharing as needed through external programs...
1853 if (BrowseLocalProtocols
& BROWSE_LPD
)
1856 if (BrowseLocalProtocols
& BROWSE_SMB
)
1862 * 'cupsdStopPolling()' - Stop polling servers as needed.
1866 cupsdStopPolling(void)
1868 int i
; /* Looping var */
1869 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1874 cupsdStatBufDelete(PollStatusBuffer
);
1877 cupsdRemoveSelect(PollPipe
);
1880 PollStatusBuffer
= NULL
;
1883 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1885 cupsdEndProcess(pollp
->pid
, 0);
1891 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1895 cupsdUpdateDNSSDName(void)
1897 DNSServiceErrorType error
; /* Error from service creation */
1898 char webif
[1024]; /* Web interface share name */
1899 #ifdef HAVE_COREFOUNDATION_H
1900 CFStringRef nameRef
; /* Computer name CFString */
1901 char nameBuffer
[1024]; /* C-string buffer */
1902 CFStringEncoding nameEncoding
; /* Computer name encoding */
1903 #endif /* HAVE_COREFOUNDATION_H */
1907 * Only share the web interface and printers when non-local listening is
1915 * Get the computer name as a c-string...
1918 #ifdef HAVE_COREFOUNDATION_H
1919 cupsdClearString(&DNSSDName
);
1921 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1922 &nameEncoding
)) != NULL
)
1924 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1925 kCFStringEncodingUTF8
))
1926 cupsdSetString(&DNSSDName
, nameBuffer
);
1932 cupsdSetString(&DNSSDName
, ServerName
);
1933 #endif /* HAVE_COREFOUNDATION_H */
1936 * Then (re)register the web interface if enabled...
1942 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1944 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1947 DNSServiceRefDeallocate(WebIFRef
);
1949 WebIFRef
= DNSSDRef
;
1950 if ((error
= DNSServiceRegister(&WebIFRef
,
1951 kDNSServiceFlagsShareConnection
,
1952 0, webif
, "_http._tcp", NULL
,
1953 NULL
, htons(DNSSDPort
), 7,
1954 "\006path=/", dnssdRegisterCallback
,
1955 NULL
)) != kDNSServiceErr_NoError
)
1956 cupsdLogMessage(CUPSD_LOG_ERROR
,
1957 "DNS-SD web interface registration failed: %d", error
);
1960 #endif /* HAVE_DNSSD */
1965 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1969 cupsdUpdateLDAPBrowse(void)
1971 char uri
[HTTP_MAX_URI
], /* Printer URI */
1972 host
[HTTP_MAX_URI
], /* Hostname */
1973 resource
[HTTP_MAX_URI
], /* Resource path */
1974 location
[1024], /* Printer location */
1975 info
[1024], /* Printer information */
1976 make_model
[1024], /* Printer make and model */
1977 type_num
[30]; /* Printer type number */
1978 int type
; /* Printer type */
1979 int rc
; /* LDAP status */
1980 int limit
; /* Size limit */
1981 LDAPMessage
*res
, /* LDAP search results */
1982 *e
; /* Current entry from search */
1984 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1986 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1989 * Reconnect if LDAP Handle is invalid...
1992 if (! BrowseLDAPHandle
)
1999 * Search for cups printers in LDAP directory...
2002 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2003 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2006 * If ldap search was successfull then exit function
2007 * and temporary disable LDAP updates...
2010 if (rc
!= LDAP_SUCCESS
)
2012 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2014 BrowseLDAPUpdate
= FALSE
;
2015 cupsdLogMessage(CUPSD_LOG_INFO
,
2016 "LDAP update temporary disabled");
2022 * If LDAP updates were disabled, we will reenable them...
2025 if (! BrowseLDAPUpdate
)
2027 BrowseLDAPUpdate
= TRUE
;
2028 cupsdLogMessage(CUPSD_LOG_INFO
,
2029 "LDAP update enabled");
2033 * Count LDAP entries and return if no entry exist...
2036 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2037 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2045 * Loop through the available printers...
2048 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2050 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2053 * Get the required values from this entry...
2056 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2057 "printerDescription", info
, sizeof(info
)) == -1)
2060 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2061 "printerLocation", location
, sizeof(location
)) == -1)
2064 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2065 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2068 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2069 "printerType", type_num
, sizeof(type_num
)) == -1)
2072 type
= atoi(type_num
);
2074 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2075 "printerURI", uri
, sizeof(uri
)) == -1)
2079 * Process the entry as browse data...
2082 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2083 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2084 location
, info
, make_model
, 0, NULL
);
2090 #endif /* HAVE_LDAP */
2095 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2099 cupsdUpdateSLPBrowse(void)
2101 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2102 *next
; /* Next service in list */
2103 cupsd_printer_t p
; /* Printer information */
2104 const char *uri
; /* Pointer to printer URI */
2105 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2106 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2110 * Reset the refresh time...
2113 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2116 * Poll for remote printers using SLP...
2121 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2122 slp_url_callback
, &s
);
2125 * Loop through the list of available printers...
2131 * Save the "next" pointer...
2137 * Load a cupsd_printer_t structure with the SLP service attributes...
2140 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2143 * Process this printer entry...
2146 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2148 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2151 * Pull the URI apart to see if this is a local or remote printer...
2154 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2155 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2156 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2160 * Free this listing...
2163 cupsdClearString(&p
.info
);
2164 cupsdClearString(&p
.location
);
2165 cupsdClearString(&p
.make_model
);
2170 #endif /* HAVE_LIBSLP */
2174 * 'dequote()' - Remote quotes from a string.
2177 static char * /* O - Dequoted string */
2178 dequote(char *d
, /* I - Destination string */
2179 const char *s
, /* I - Source string */
2180 int dlen
) /* I - Destination length */
2182 char *dptr
; /* Pointer into destination */
2187 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2205 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2208 static char * /* O - TXT record */
2209 dnssdBuildTxtRecord(
2210 int *txt_len
, /* O - TXT record length */
2211 cupsd_printer_t
*p
, /* I - Printer information */
2212 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2214 int i
, j
; /* Looping vars */
2215 char type_str
[32], /* Type to string buffer */
2216 state_str
[32], /* State to string buffer */
2217 rp_str
[1024], /* Queue name string buffer */
2218 air_str
[1024], /* auth-info-required string buffer */
2219 *keyvalue
[32][2]; /* Table of key/value pairs */
2223 * Load up the key value pairs...
2228 keyvalue
[i
][0] = "txtvers";
2229 keyvalue
[i
++][1] = "1";
2231 keyvalue
[i
][0] = "qtotal";
2232 keyvalue
[i
++][1] = "1";
2234 keyvalue
[i
][0] = "rp";
2235 keyvalue
[i
++][1] = rp_str
;
2237 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2239 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2240 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2242 keyvalue
[i
][0] = "ty";
2243 keyvalue
[i
++][1] = p
->make_model
;
2245 if (p
->location
&& *p
->location
!= '\0')
2247 keyvalue
[i
][0] = "note";
2248 keyvalue
[i
++][1] = p
->location
;
2251 keyvalue
[i
][0] = "priority";
2252 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2254 keyvalue
[i
][0] = "product";
2255 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2257 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2258 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2260 keyvalue
[i
][0] = "printer-state";
2261 keyvalue
[i
++][1] = state_str
;
2263 keyvalue
[i
][0] = "printer-type";
2264 keyvalue
[i
++][1] = type_str
;
2266 keyvalue
[i
][0] = "Transparent";
2267 keyvalue
[i
++][1] = "T";
2269 keyvalue
[i
][0] = "Binary";
2270 keyvalue
[i
++][1] = "T";
2272 if ((p
->type
& CUPS_PRINTER_FAX
))
2274 keyvalue
[i
][0] = "Fax";
2275 keyvalue
[i
++][1] = "T";
2278 if ((p
->type
& CUPS_PRINTER_COLOR
))
2280 keyvalue
[i
][0] = "Color";
2281 keyvalue
[i
++][1] = "T";
2284 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2286 keyvalue
[i
][0] = "Duplex";
2287 keyvalue
[i
++][1] = "T";
2290 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2292 keyvalue
[i
][0] = "Staple";
2293 keyvalue
[i
++][1] = "T";
2296 if ((p
->type
& CUPS_PRINTER_COPIES
))
2298 keyvalue
[i
][0] = "Copies";
2299 keyvalue
[i
++][1] = "T";
2302 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2304 keyvalue
[i
][0] = "Collate";
2305 keyvalue
[i
++][1] = "T";
2308 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2310 keyvalue
[i
][0] = "Punch";
2311 keyvalue
[i
++][1] = "T";
2314 if ((p
->type
& CUPS_PRINTER_BIND
))
2316 keyvalue
[i
][0] = "Bind";
2317 keyvalue
[i
++][1] = "T";
2320 if ((p
->type
& CUPS_PRINTER_SORT
))
2322 keyvalue
[i
][0] = "Sort";
2323 keyvalue
[i
++][1] = "T";
2326 keyvalue
[i
][0] = "pdl";
2327 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2329 if (p
->num_auth_info_required
)
2331 char *air
= air_str
; /* Pointer into string */
2334 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2336 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2342 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2346 keyvalue
[i
][0] = "air";
2347 keyvalue
[i
++][1] = air_str
;
2351 * Then pack them into a proper txt record...
2354 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2359 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2362 static int /* O - Result of comparison */
2363 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2364 cupsd_printer_t
*b
)/* I - Second printer */
2366 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2371 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2376 dnssdDeregisterPrinter(
2377 cupsd_printer_t
*p
) /* I - Printer */
2379 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2382 * Closing the socket deregisters the service
2387 DNSServiceRefDeallocate(p
->ipp_ref
);
2391 cupsArrayRemove(DNSSDPrinters
, p
);
2392 cupsdClearString(&p
->reg_name
);
2397 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2407 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2408 * TXT record format.
2411 static char * /* O - TXT record */
2412 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2413 char *keyvalue
[][2], /* I - Table of key value pairs */
2414 int count
) /* I - Items in table */
2416 int i
; /* Looping var */
2417 int length
; /* Length of TXT record */
2418 int length2
; /* Length of value */
2419 char *txtRecord
; /* TXT record buffer */
2420 char *cursor
; /* Looping pointer */
2424 * Calculate the buffer size
2427 for (length
= i
= 0; i
< count
; i
++)
2428 length
+= 1 + strlen(keyvalue
[i
][0]) +
2429 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2432 * Allocate and fill it
2435 txtRecord
= malloc(length
);
2440 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2443 * Drop in the p-string style length byte followed by the data
2446 length
= strlen(keyvalue
[i
][0]);
2447 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2449 *cursor
++ = (unsigned char)(length
+ length2
);
2451 memcpy(cursor
, keyvalue
[i
][0], length
);
2458 memcpy(cursor
, keyvalue
[i
][1], length2
);
2469 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2473 dnssdRegisterCallback(
2474 DNSServiceRef sdRef
, /* I - DNS Service reference */
2475 DNSServiceFlags flags
, /* I - Reserved for future use */
2476 DNSServiceErrorType errorCode
, /* I - Error code */
2477 const char *name
, /* I - Service name */
2478 const char *regtype
, /* I - Service type */
2479 const char *domain
, /* I - Domain. ".local" for now */
2480 void *context
) /* I - User-defined context */
2482 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2483 /* Current printer */
2486 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2487 name
, regtype
, p
? p
->name
: "Web Interface");
2491 cupsdLogMessage(CUPSD_LOG_ERROR
,
2492 "DNSServiceRegister failed with error %d", (int)errorCode
);
2495 else if (p
&& strcasecmp(name
, p
->reg_name
))
2497 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2500 cupsArrayRemove(DNSSDPrinters
, p
);
2501 cupsdSetString(&p
->reg_name
, name
);
2502 cupsArrayAdd(DNSSDPrinters
, p
);
2504 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2510 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2511 * or update the broadcast contents.
2515 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2517 DNSServiceErrorType se
; /* dnssd errors */
2518 char *ipp_txt
, /* IPP TXT record buffer */
2519 *printer_txt
, /* LPD TXT record buffer */
2520 name
[1024], /* Service name */
2521 *nameptr
; /* Pointer into name */
2522 int ipp_len
, /* IPP TXT record length */
2523 printer_len
; /* LPD TXT record length */
2524 char resource
[1024]; /* Resource path for printer */
2525 const char *regtype
; /* Registration type */
2526 const char *domain
; /* Registration domain */
2527 cupsd_location_t
*location
, /* Printer location */
2528 *policy
; /* Operation policy for Print-Job */
2529 unsigned address
[4]; /* INADDR_ANY address */
2532 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2533 !p
->ipp_ref
? "new" : "update");
2536 * If per-printer sharing was just disabled make sure we're not
2537 * registered before returning.
2542 dnssdDeregisterPrinter(p
);
2547 * The registered name takes the form of "<printer-info> @ <computer name>"...
2550 if (p
->info
&& strlen(p
->info
) > 0)
2553 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2555 strlcpy(name
, p
->info
, sizeof(name
));
2558 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2560 strlcpy(name
, p
->name
, sizeof(name
));
2563 * If an existing printer was renamed, unregister it and start over...
2566 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2567 dnssdDeregisterPrinter(p
);
2571 cupsdSetString(&p
->reg_name
, name
);
2572 cupsArrayAdd(DNSSDPrinters
, p
);
2576 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2577 * let dnssd decide on the domain, otherwise restrict it to ".local".
2580 if (p
->type
& CUPS_PRINTER_CLASS
)
2581 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2583 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2585 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2586 location
= cupsdFindBest(resource
, HTTP_POST
);
2587 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2589 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2590 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2596 * Register IPP and (optionally) LPD...
2599 ipp_len
= 0; /* anti-compiler-warning-code */
2600 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2605 * Initial registration. Use the _fax subtype for fax queues...
2608 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2611 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2612 "Registering DNS-SD printer %s with name \"%s\", "
2613 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2614 domain
? domain
: "(null)");
2617 * Register the queue, dropping characters as needed until we succeed...
2620 nameptr
= name
+ strlen(name
);
2624 p
->ipp_ref
= DNSSDRef
;
2625 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2626 0, name
, regtype
, domain
, NULL
,
2627 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2628 dnssdRegisterCallback
,
2629 p
)) == kDNSServiceErr_BadParam
)
2632 * Name is too long, drop trailing characters, taking into account
2638 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2645 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2647 if (se
== kDNSServiceErr_NoError
)
2649 p
->ipp_txt
= ipp_txt
;
2650 p
->ipp_len
= ipp_len
;
2654 cupsdLogMessage(CUPSD_LOG_WARN
,
2655 "DNS-SD IPP registration of \"%s\" failed: %d",
2658 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2661 * Update the existing registration...
2664 /* A TTL of 0 means use record's original value (Radar 3176248) */
2665 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2670 p
->ipp_txt
= ipp_txt
;
2671 p
->ipp_len
= ipp_len
;
2678 if (BrowseLocalProtocols
& BROWSE_LPD
)
2680 printer_len
= 0; /* anti-compiler-warning-code */
2681 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2683 if (!p
->printer_ref
)
2686 * Initial registration...
2689 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2690 "Registering DNS-SD printer %s with name \"%s\", "
2691 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2692 name
, domain
? domain
: "(null)");
2694 p
->printer_ref
= DNSSDRef
;
2695 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2696 kDNSServiceFlagsShareConnection
,
2697 0, name
, "_printer._tcp", domain
, NULL
,
2698 htons(515), printer_len
, printer_txt
,
2699 dnssdRegisterCallback
,
2700 p
)) == kDNSServiceErr_NoError
)
2702 p
->printer_txt
= printer_txt
;
2703 p
->printer_len
= printer_len
;
2707 cupsdLogMessage(CUPSD_LOG_WARN
,
2708 "DNS-SD LPD registration of \"%s\" failed: %d",
2711 else if (printer_len
!= p
->printer_len
||
2712 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2715 * Update the existing registration...
2718 /* A TTL of 0 means use record's original value (Radar 3176248) */
2719 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2723 free(p
->printer_txt
);
2725 p
->printer_txt
= printer_txt
;
2726 p
->printer_len
= printer_len
;
2737 * 'dnssdUpdate()' - Handle DNS-SD queries.
2743 DNSServiceErrorType sdErr
; /* Service discovery error */
2746 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2747 cupsdLogMessage(CUPSD_LOG_ERROR
,
2748 "DNS Service Discovery registration error %d!",
2751 #endif /* HAVE_DNSSD */
2756 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2759 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2760 get_hostconfig(const char *name
) /* I - Name of service */
2762 cups_file_t
*fp
; /* Hostconfig file */
2763 char line
[1024], /* Line from file */
2764 *ptr
; /* Pointer to value */
2765 int state
= 1; /* State of service */
2769 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2770 * the service is enabled/auto.
2773 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2776 * Read lines from the file until we find the service...
2779 while (cupsFileGets(fp
, line
, sizeof(line
)))
2781 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2786 if (!strcasecmp(line
, name
))
2789 * Found the service, see if it is set to "-NO-"...
2792 if (!strncasecmp(ptr
, "-NO-", 4))
2803 #endif /* __APPLE__ */
2807 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2810 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2811 is_local_queue(const char *uri
, /* I - Printer URI */
2812 char *host
, /* O - Host string */
2813 int hostlen
, /* I - Length of host buffer */
2814 char *resource
, /* O - Resource string */
2815 int resourcelen
) /* I - Length of resource buffer */
2817 char scheme
[32], /* Scheme portion of URI */
2818 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2819 int port
; /* Port portion of URI */
2820 cupsd_netif_t
*iface
; /* Network interface */
2824 * Pull the URI apart to see if this is a local or remote printer...
2827 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2828 username
, sizeof(username
), host
, hostlen
, &port
,
2829 resource
, resourcelen
) < HTTP_URI_OK
)
2832 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2835 * Check for local server addresses...
2838 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2843 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2845 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2846 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2850 * If we get here, the printer is remote...
2858 * 'process_browse_data()' - Process new browse data.
2862 process_browse_data(
2863 const char *uri
, /* I - URI of printer/class */
2864 const char *host
, /* I - Hostname */
2865 const char *resource
, /* I - Resource path */
2866 cups_ptype_t type
, /* I - Printer type */
2867 ipp_pstate_t state
, /* I - Printer state */
2868 const char *location
, /* I - Printer location */
2869 const char *info
, /* I - Printer information */
2870 const char *make_model
, /* I - Printer make and model */
2871 int num_attrs
, /* I - Number of attributes */
2872 cups_option_t
*attrs
) /* I - Attributes */
2874 int i
; /* Looping var */
2875 int update
; /* Update printer attributes? */
2876 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2877 name
[IPP_MAX_NAME
], /* Name of printer */
2878 newname
[IPP_MAX_NAME
], /* New name of printer */
2879 *hptr
, /* Pointer into hostname */
2880 *sptr
; /* Pointer into ServerName */
2881 const char *shortname
; /* Short queue name (queue) */
2882 char local_make_model
[IPP_MAX_NAME
];
2883 /* Local make and model */
2884 cupsd_printer_t
*p
; /* Printer information */
2885 const char *ipp_options
, /* ipp-options value */
2886 *lease_duration
; /* lease-duration value */
2887 int is_class
; /* Is this queue a class? */
2890 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2891 "process_browse_data(uri=\"%s\", host=\"%s\", "
2892 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2893 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2894 uri
, host
, resource
, type
, state
,
2895 location
? location
: "(nil)", info
? info
: "(nil)",
2896 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2899 * Determine if the URI contains any illegal characters in it...
2902 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2903 (strncmp(resource
, "/printers/", 10) &&
2904 strncmp(resource
, "/classes/", 9)))
2906 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2910 if (strchr(resource
, '?') ||
2911 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2912 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2914 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2920 * OK, this isn't a local printer; add any remote options...
2923 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2925 if (BrowseRemoteOptions
)
2927 if (BrowseRemoteOptions
[0] == '?')
2930 * Override server-supplied options...
2933 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2935 else if (ipp_options
)
2938 * Combine the server and local options...
2941 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2942 BrowseRemoteOptions
);
2947 * Just use the local options...
2950 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2955 else if (ipp_options
)
2958 * Just use the server-supplied options...
2961 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2966 * See if we already have it listed in the Printers list, and add it if not...
2969 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2970 type
&= ~CUPS_PRINTER_IMPLICIT
;
2972 hptr
= strchr(host
, '.');
2973 sptr
= strchr(ServerName
, '.');
2974 is_class
= type
& CUPS_PRINTER_CLASS
;
2976 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2979 * Strip the common domain name components...
2982 while (hptr
!= NULL
)
2984 if (!strcasecmp(hptr
, sptr
))
2990 hptr
= strchr(hptr
+ 1, '.');
2997 * Remote destination is a class...
3000 if (!strncmp(resource
, "/classes/", 9))
3001 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3005 shortname
= resource
+ 9;
3010 * Remote destination is a printer...
3013 if (!strncmp(resource
, "/printers/", 10))
3014 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3018 shortname
= resource
+ 10;
3022 *hptr
= '.'; /* Resource FQDN */
3024 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3027 * Long name doesn't exist, try short name...
3030 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3033 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3036 * Short name doesn't exist, use it for this shared queue.
3039 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3041 strlcpy(name
, shortname
, sizeof(name
));
3046 * Short name exists...
3049 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3050 "process_browse_data: %s found, type=%x, hostname=%s...",
3051 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3053 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3054 p
= NULL
; /* Don't replace implicit classes */
3055 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3058 * Short name exists but is for a different host. If this is a remote
3059 * queue, rename it and use the long name...
3062 if (p
->type
& CUPS_PRINTER_REMOTE
)
3064 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3065 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3066 is_class
? "class" : "printer", p
->name
, p
->name
,
3068 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3069 "%s \'%s\' deleted by directory services.",
3070 is_class
? "Class" : "Printer", p
->name
);
3072 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3073 cupsdRenamePrinter(p
, newname
);
3075 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3076 "%s \'%s\' added by directory services.",
3077 is_class
? "Class" : "Printer", p
->name
);
3081 * Force creation with long name...
3089 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3090 "process_browse_data: %s found, type=%x, hostname=%s...",
3091 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3096 * Queue doesn't exist; add it...
3100 p
= cupsdAddClass(name
);
3102 p
= cupsdAddPrinter(name
);
3107 cupsdClearString(&(p
->hostname
));
3109 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3110 is_class
? "class" : "printer", name
);
3112 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3113 "%s \'%s\' added by directory services.",
3114 is_class
? "Class" : "Printer", name
);
3117 * Force the URI to point to the real server...
3120 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3123 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3129 * Hostname not set, so this must be a cached remote printer
3130 * that was created for a pending print job...
3133 cupsdSetString(&p
->hostname
, host
);
3134 cupsdSetString(&p
->uri
, uri
);
3135 cupsdSetString(&p
->device_uri
, uri
);
3138 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3142 * Update the state...
3146 p
->browse_time
= time(NULL
);
3148 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3152 * Grab the lease-duration for the browse data; anything less then 1
3153 * second or more than 1 week gets the default BrowseTimeout...
3156 i
= atoi(lease_duration
);
3157 if (i
< 1 || i
> 604800)
3160 p
->browse_expire
= p
->browse_time
+ i
;
3163 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3165 if (type
& CUPS_PRINTER_REJECTING
)
3167 type
&= ~CUPS_PRINTER_REJECTING
;
3175 else if (!p
->accepting
)
3181 if (p
->type
!= type
)
3187 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3189 cupsdSetString(&p
->location
, location
);
3193 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3195 cupsdSetString(&p
->info
, info
);
3198 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3201 if (!make_model
|| !make_model
[0])
3204 snprintf(local_make_model
, sizeof(local_make_model
),
3205 "Remote Class on %s", host
);
3207 snprintf(local_make_model
, sizeof(local_make_model
),
3208 "Remote Printer on %s", host
);
3211 snprintf(local_make_model
, sizeof(local_make_model
),
3212 "%s on %s", make_model
, host
);
3214 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3216 cupsdSetString(&p
->make_model
, local_make_model
);
3222 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3225 * See if we need to update the attributes...
3228 if (p
->num_options
!= num_attrs
)
3232 for (i
= 0; i
< num_attrs
; i
++)
3233 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3234 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3235 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3244 * Free the old options...
3247 cupsFreeOptions(p
->num_options
, p
->options
);
3250 p
->num_options
= num_attrs
;
3253 if (type
& CUPS_PRINTER_DELETE
)
3255 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3256 "%s \'%s\' deleted by directory services.",
3257 is_class
? "Class" : "Printer", p
->name
);
3259 cupsdExpireSubscriptions(p
, NULL
);
3261 cupsdDeletePrinter(p
, 1);
3262 cupsdUpdateImplicitClasses();
3263 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3267 cupsdSetPrinterAttrs(p
);
3268 cupsdUpdateImplicitClasses();
3272 * See if we have a default printer... If not, make the first network
3273 * default printer the default.
3276 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3279 * Find the first network default printer and use it...
3282 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3284 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3285 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3288 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3294 * Do auto-classing if needed...
3297 process_implicit_classes();
3302 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3306 process_implicit_classes(void)
3308 int i
; /* Looping var */
3309 int update
; /* Update printer attributes? */
3310 char name
[IPP_MAX_NAME
], /* Name of printer */
3311 *hptr
; /* Pointer into hostname */
3312 cupsd_printer_t
*p
, /* Printer information */
3313 *pclass
, /* Printer class */
3314 *first
; /* First printer in class */
3315 int offset
, /* Offset of name */
3316 len
; /* Length of name */
3319 if (!ImplicitClasses
|| !Printers
)
3323 * Loop through all available printers and create classes as needed...
3326 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3327 update
= 0, pclass
= NULL
, first
= NULL
;
3329 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3332 * Skip implicit classes...
3335 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3342 * If len == 0, get the length of this printer name up to the "@"
3346 cupsArraySave(Printers
);
3349 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3350 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3353 * We have more than one printer with the same name; see if
3354 * we have a class, and if this printer is a member...
3357 if (pclass
&& strcasecmp(pclass
->name
, name
))
3360 cupsdSetPrinterAttrs(pclass
);
3366 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3369 * Need to add the class...
3372 pclass
= cupsdAddPrinter(name
);
3373 cupsArrayAdd(ImplicitPrinters
, pclass
);
3375 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3376 pclass
->accepting
= 1;
3377 pclass
->state
= IPP_PRINTER_IDLE
;
3379 cupsdSetString(&pclass
->location
, p
->location
);
3380 cupsdSetString(&pclass
->info
, p
->info
);
3382 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3383 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3387 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3389 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3391 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3392 "Implicit class \'%s\' added by directory services.",
3398 for (i
= 0; i
< pclass
->num_printers
; i
++)
3399 if (pclass
->printers
[i
] == first
)
3402 if (i
>= pclass
->num_printers
)
3404 first
->in_implicit_class
= 1;
3405 cupsdAddPrinterToClass(pclass
, first
);
3411 for (i
= 0; i
< pclass
->num_printers
; i
++)
3412 if (pclass
->printers
[i
] == p
)
3415 if (i
>= pclass
->num_printers
)
3417 p
->in_implicit_class
= 1;
3418 cupsdAddPrinterToClass(pclass
, p
);
3425 * First time around; just get name length and mark it as first
3429 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3430 len
= hptr
- p
->name
;
3432 len
= strlen(p
->name
);
3434 strncpy(name
, p
->name
, len
);
3438 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3439 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3442 * Can't use same name as a local printer; add "Any" to the
3443 * front of the name, unless we have explicitly disabled
3444 * the "ImplicitAnyClasses"...
3447 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3450 * Add "Any" to the class name...
3453 strcpy(name
, "Any");
3454 strncpy(name
+ 3, p
->name
, len
);
3455 name
[len
+ 3] = '\0';
3461 * Don't create an implicit class if we have a local printer
3462 * with the same name...
3466 cupsArrayRestore(Printers
);
3474 cupsArrayRestore(Printers
);
3478 * Update the last printer class as needed...
3481 if (pclass
&& update
)
3482 cupsdSetPrinterAttrs(pclass
);
3487 * 'send_cups_browse()' - Send new browsing information using the CUPS
3492 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3494 int i
; /* Looping var */
3495 cups_ptype_t type
; /* Printer type */
3496 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3497 int bytes
; /* Length of packet */
3498 char packet
[1453], /* Browse data packet */
3499 uri
[1024], /* Printer URI */
3500 location
[1024], /* printer-location */
3501 info
[1024], /* printer-info */
3503 /* printer-make-and-model */
3504 cupsd_netif_t
*iface
; /* Network interface */
3508 * Figure out the printer type value...
3511 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3514 type
|= CUPS_PRINTER_REJECTING
;
3516 if (p
== DefaultPrinter
)
3517 type
|= CUPS_PRINTER_DEFAULT
;
3520 * Remove quotes from printer-info, printer-location, and
3521 * printer-make-and-model attributes...
3524 dequote(location
, p
->location
, sizeof(location
));
3525 dequote(info
, p
->info
, sizeof(info
));
3528 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3529 else if (p
->type
& CUPS_PRINTER_CLASS
)
3531 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3532 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3534 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3537 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3539 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3542 * Send a packet to each browse address...
3545 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3549 * Send the browse packet to one or more interfaces...
3552 if (!strcmp(b
->iface
, "*"))
3555 * Send to all local interfaces...
3560 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3562 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3565 * Only send to local, IPv4 interfaces...
3568 if (!iface
->is_local
|| !iface
->port
||
3569 iface
->address
.addr
.sa_family
!= AF_INET
)
3572 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3573 iface
->hostname
, iface
->port
,
3574 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3577 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3578 type
, p
->state
, uri
, location
, info
, make_model
,
3579 p
->browse_attrs
? p
->browse_attrs
: "");
3581 bytes
= strlen(packet
);
3583 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3584 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3585 iface
->name
, packet
);
3587 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3589 sendto(BrowseSocket
, packet
, bytes
, 0,
3590 (struct sockaddr
*)&(iface
->broadcast
),
3591 httpAddrLength(&(iface
->broadcast
)));
3594 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3597 * Send to the named interface using the IPv4 address...
3601 if (strcmp(b
->iface
, iface
->name
))
3606 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3609 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3613 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3614 iface
->hostname
, iface
->port
,
3615 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3618 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3619 type
, p
->state
, uri
, location
, info
, make_model
,
3620 p
->browse_attrs
? p
->browse_attrs
: "");
3622 bytes
= strlen(packet
);
3624 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3625 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3626 iface
->name
, packet
);
3628 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3630 sendto(BrowseSocket
, packet
, bytes
, 0,
3631 (struct sockaddr
*)&(iface
->broadcast
),
3632 httpAddrLength(&(iface
->broadcast
)));
3639 * Send the browse packet to the indicated address using
3640 * the default server name...
3643 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3644 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3645 p
->browse_attrs
? p
->browse_attrs
: "");
3647 bytes
= strlen(packet
);
3648 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3649 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3651 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3652 (struct sockaddr
*)&(b
->to
),
3653 httpAddrLength(&(b
->to
))) <= 0)
3656 * Unable to send browse packet, so remove this address from the
3660 cupsdLogMessage(CUPSD_LOG_ERROR
,
3661 "cupsdSendBrowseList: sendto failed for browser "
3663 (int)(b
- Browsers
+ 1), strerror(errno
));
3666 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3677 * 'ldap_search_rec()' - LDAP Search with reconnect
3681 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3682 char *base
, /* I - Base dn */
3683 int scope
, /* I - LDAP search scope */
3684 char *filter
, /* I - Filter string */
3685 char *attrs
[], /* I - Requested attributes */
3686 int attrsonly
, /* I - Return only attributes? */
3687 LDAPMessage
**res
) /* I - LDAP handler */
3689 int rc
; /* Return code */
3692 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3693 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3694 NULL
, LDAP_NO_LIMIT
, res
);
3696 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3697 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3700 * If we have a connection problem try again...
3703 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3705 cupsdLogMessage(CUPSD_LOG_ERROR
,
3706 "LDAP search failed with status %d: %s",
3707 rc
, ldap_err2string(rc
));
3708 cupsdLogMessage(CUPSD_LOG_INFO
,
3709 "We try the LDAP search once again after reconnecting to "
3714 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3715 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3716 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3718 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3719 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3722 if (rc
== LDAP_NO_SUCH_OBJECT
)
3723 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3724 "ldap_search_rec: LDAP entry/object not found");
3725 else if (rc
!= LDAP_SUCCESS
)
3726 cupsdLogMessage(CUPSD_LOG_ERROR
,
3727 "ldap_search_rec: LDAP search failed with status %d: %s",
3728 rc
, ldap_err2string(rc
));
3730 if (rc
!= LDAP_SUCCESS
)
3738 * 'ldap_freeres()' - Free LDAPMessage
3742 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3744 int rc
; /* Return value */
3747 rc
= ldap_msgfree(entry
);
3749 cupsdLogMessage(CUPSD_LOG_WARN
,
3750 "Can't free LDAPMessage!");
3752 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3753 "Freeing LDAPMessage was unnecessary");
3758 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3762 ldap_getval_firststring(
3763 LDAP
*ld
, /* I - LDAP handler */
3764 LDAPMessage
*entry
, /* I - LDAP message or search result */
3765 char *attr
, /* I - the wanted attribute */
3766 char *retval
, /* O - String to return */
3767 unsigned long maxsize
) /* I - Max string size */
3769 char *dn
; /* LDAP DN */
3770 int rc
= 0; /* Return code */
3771 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3772 struct berval
**bval
; /* LDAP value array */
3773 unsigned long size
; /* String size */
3777 * Get value from LDAPMessage...
3780 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3783 dn
= ldap_get_dn(ld
, entry
);
3784 cupsdLogMessage(CUPSD_LOG_WARN
,
3785 "Failed to get LDAP value %s for %s!",
3793 * Check size and copy value into our string...
3797 if (size
< bval
[0]->bv_len
)
3800 dn
= ldap_get_dn(ld
, entry
);
3801 cupsdLogMessage(CUPSD_LOG_WARN
,
3802 "Attribute %s is too big! (dn: %s)",
3807 size
= bval
[0]->bv_len
;
3809 strlcpy(retval
, bval
[0]->bv_val
, size
);
3810 ldap_value_free_len(bval
);
3813 char **value
; /* LDAP value */
3816 * Get value from LDAPMessage...
3819 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3822 dn
= ldap_get_dn(ld
, entry
);
3823 cupsdLogMessage(CUPSD_LOG_WARN
,
3824 "Failed to get LDAP value %s for %s!",
3830 strlcpy(retval
, *value
, maxsize
);
3831 ldap_value_free(value
);
3833 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3840 * 'send_ldap_ou()' - Send LDAP ou registrations.
3844 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3845 char *basedn
, /* I - Our base dn */
3846 char *descstring
) /* I - Description for ou */
3848 int i
; /* Looping var... */
3849 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3850 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3851 LDAPMessage
*res
, /* Search result token */
3852 *e
; /* Current entry from search */
3853 int rc
; /* LDAP status */
3854 char dn
[1024], /* DN of the organizational unit we are adding */
3855 *desc
[2], /* Change records */
3857 char old_desc
[1024]; /* Old description */
3858 static const char * const objectClass_values
[] =
3859 { /* The 2 objectClass's we use in */
3860 "top", /* our LDAP entries */
3861 "organizationalUnit",
3864 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3871 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3874 * Reconnect if LDAP Handle is invalid...
3877 if (! BrowseLDAPHandle
)
3879 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3880 "send_ldap_ou: LDAP Handle is invalid. Try "
3887 * Prepare ldap search...
3890 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3891 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3895 desc
[0] = descstring
;
3898 mods
[0].mod_type
= "ou";
3899 mods
[0].mod_values
= ou_value
;
3900 mods
[1].mod_type
= "description";
3901 mods
[1].mod_values
= desc
;
3902 mods
[2].mod_type
= "objectClass";
3903 mods
[2].mod_values
= (char **)objectClass_values
;
3905 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3906 (char **)ou_attrs
, 0, &res
);
3909 * If ldap search was not successfull then exit function...
3912 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3916 * Check if we need to insert or update the LDAP entry...
3919 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3920 rc
!= LDAP_NO_SUCH_OBJECT
)
3923 * Printserver has already been registered, check if
3924 * modification is required...
3927 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
3930 * Get the required values from this entry...
3933 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
3934 sizeof(old_desc
)) == -1)
3938 * Check if modification is required...
3941 if ( strcmp(desc
[0], old_desc
) == 0 )
3944 * LDAP entry for the printer exists.
3945 * Printer has already been registered,
3946 * no modifications required...
3948 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3949 "send_ldap_ou: No updates required for %s", ou
);
3954 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3955 "send_ldap_ou: Replace entry for %s", ou
);
3957 for (i
= 0; i
< 3; i
++)
3959 pmods
[i
] = mods
+ i
;
3960 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3964 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3965 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3966 NULL
)) != LDAP_SUCCESS
)
3968 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3969 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3971 cupsdLogMessage(CUPSD_LOG_ERROR
,
3972 "LDAP modify for %s failed with status %d: %s",
3973 ou
, rc
, ldap_err2string(rc
));
3974 if ( LDAP_SERVER_DOWN
== rc
)
3982 * Printserver has never been registered,
3983 * add registration...
3986 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3987 "send_ldap_ou: Add entry for %s", ou
);
3989 for (i
= 0; i
< 3; i
++)
3991 pmods
[i
] = mods
+ i
;
3992 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3996 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3997 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3998 NULL
)) != LDAP_SUCCESS
)
4000 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4001 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4003 cupsdLogMessage(CUPSD_LOG_ERROR
,
4004 "LDAP add for %s failed with status %d: %s",
4005 ou
, rc
, ldap_err2string(rc
));
4006 if ( LDAP_SERVER_DOWN
== rc
)
4016 * 'send_ldap_browse()' - Send LDAP printer registrations.
4020 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4022 int i
; /* Looping var... */
4023 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4024 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4025 LDAPMessage
*res
, /* Search result token */
4026 *e
; /* Current entry from search */
4027 char *cn_value
[2], /* Change records */
4033 typestring
[255], /* String to hold printer-type */
4034 dn
[1024]; /* DN of the printer we are adding */
4035 int rc
; /* LDAP status */
4036 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4037 old_location
[1024], /* Printer location */
4038 old_info
[1024], /* Printer information */
4039 old_make_model
[1024], /* Printer make and model */
4040 old_type_string
[30]; /* Temporary type number */
4041 int old_type
; /* Printer type */
4042 static const char * const objectClass_values
[] =
4043 { /* The 3 objectClass's we use in */
4044 "top", /* our LDAP entries */
4051 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4054 * Exit function if LDAP updates has been disabled...
4057 if (!BrowseLDAPUpdate
)
4059 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4060 "send_ldap_browse: Updates temporary disabled; "
4066 * Reconnect if LDAP Handle is invalid...
4069 if (!BrowseLDAPHandle
)
4071 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4072 "send_ldap_browse: LDAP Handle is invalid. Try "
4079 * Everything in ldap is ** so we fudge around it...
4082 sprintf(typestring
, "%u", p
->type
);
4084 cn_value
[0] = p
->name
;
4086 info
[0] = p
->info
? p
->info
: "Unknown";
4088 location
[0] = p
->location
? p
->location
: "Unknown";
4090 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4091 make_model
[1] = NULL
;
4092 type
[0] = typestring
;
4098 * Get ldap entry for printer ...
4101 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4103 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4105 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4106 (char **)ldap_attrs
, 0, &res
);
4109 * If ldap search was not successfull then exit function
4110 * and temporary disable LDAP updates...
4113 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4115 if (BrowseLDAPUpdate
&&
4116 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4118 BrowseLDAPUpdate
= FALSE
;
4119 cupsdLogMessage(CUPSD_LOG_INFO
,
4120 "LDAP update temporary disabled");
4127 * Fill modification array...
4130 mods
[0].mod_type
= "cn";
4131 mods
[0].mod_values
= cn_value
;
4132 mods
[1].mod_type
= "printerDescription";
4133 mods
[1].mod_values
= info
;
4134 mods
[2].mod_type
= "printerURI";
4135 mods
[2].mod_values
= uri
;
4136 mods
[3].mod_type
= "printerLocation";
4137 mods
[3].mod_values
= location
;
4138 mods
[4].mod_type
= "printerMakeAndModel";
4139 mods
[4].mod_values
= make_model
;
4140 mods
[5].mod_type
= "printerType";
4141 mods
[5].mod_values
= type
;
4142 mods
[6].mod_type
= "objectClass";
4143 mods
[6].mod_values
= (char **)objectClass_values
;
4146 * Check if we need to insert or update the LDAP entry...
4149 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4150 rc
!= LDAP_NO_SUCH_OBJECT
)
4153 * Printer has already been registered, check if
4154 * modification is required...
4157 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4160 * Get the required values from this entry...
4163 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4164 old_info
, sizeof(old_info
)) == -1)
4167 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4168 old_location
, sizeof(old_location
)) == -1)
4171 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4172 old_make_model
, sizeof(old_make_model
)) == -1)
4175 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4176 old_type_string
, sizeof(old_type_string
)) == -1)
4179 old_type
= atoi(old_type_string
);
4181 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4182 sizeof(old_uri
)) == -1)
4186 * Check if modification is required...
4189 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4190 !strcmp(location
[0], old_location
) &&
4191 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4194 * LDAP entry for the printer exists. Printer has already been registered,
4195 * no modifications required...
4198 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4199 "send_ldap_browse: No updates required for %s", p
->name
);
4204 * LDAP entry for the printer exists. Printer has already been registered,
4205 * modify the current registration...
4208 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4209 "send_ldap_browse: Replace entry for %s", p
->name
);
4211 for (i
= 0; i
< 7; i
++)
4213 pmods
[i
] = mods
+ i
;
4214 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4218 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4219 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4220 NULL
)) != LDAP_SUCCESS
)
4222 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4223 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4225 cupsdLogMessage(CUPSD_LOG_ERROR
,
4226 "LDAP modify for %s failed with status %d: %s",
4227 p
->name
, rc
, ldap_err2string(rc
));
4228 if (rc
== LDAP_SERVER_DOWN
)
4236 * No LDAP entry exists for the printer. Printer has never been registered,
4237 * add the current registration...
4240 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4242 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4243 "send_ldap_browse: Add entry for %s", p
->name
);
4245 for (i
= 0; i
< 7; i
++)
4247 pmods
[i
] = mods
+ i
;
4248 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4252 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4253 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4254 NULL
)) != LDAP_SUCCESS
)
4256 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4257 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4259 cupsdLogMessage(CUPSD_LOG_ERROR
,
4260 "LDAP add for %s failed with status %d: %s",
4261 p
->name
, rc
, ldap_err2string(rc
));
4262 if (rc
== LDAP_SERVER_DOWN
)
4272 * 'ldap_dereg_printer()' - Delete printer from directory
4276 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4278 char dn
[1024]; /* DN of the printer */
4279 int rc
; /* LDAP status */
4282 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4286 * Reconnect if LDAP Handle is invalid...
4289 if (!BrowseLDAPHandle
)
4296 * Get dn for printer and delete LDAP entry...
4299 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4301 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4303 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4304 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4305 NULL
)) != LDAP_SUCCESS
)
4307 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4308 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4310 cupsdLogMessage(CUPSD_LOG_WARN
,
4311 "LDAP delete for %s failed with status %d: %s",
4312 p
->name
, rc
, ldap_err2string(rc
));
4315 * If we had a connection problem (connection timed out, etc.)
4316 * we should reconnect and try again to delete the entry...
4319 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4321 cupsdLogMessage(CUPSD_LOG_INFO
,
4322 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4325 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4326 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4327 NULL
)) != LDAP_SUCCESS
)
4329 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4330 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4331 cupsdLogMessage(CUPSD_LOG_WARN
,
4332 "LDAP delete for %s failed with status %d: %s",
4333 p
->name
, rc
, ldap_err2string(rc
));
4340 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4341 char *basedn
) /* I - Dase dn */
4343 char dn
[1024]; /* DN of the printer */
4344 int rc
; /* LDAP status */
4347 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4350 * Reconnect if LDAP Handle is invalid...
4353 if (!BrowseLDAPHandle
)
4360 * Get dn for printer and delete LDAP entry...
4363 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4364 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4366 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4367 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4368 NULL
)) != LDAP_SUCCESS
)
4370 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4371 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4373 cupsdLogMessage(CUPSD_LOG_WARN
,
4374 "LDAP delete for %s failed with status %d: %s",
4375 ou
, rc
, ldap_err2string(rc
));
4378 * If we had a connection problem (connection timed out, etc.)
4379 * we should reconnect and try again to delete the entry...
4382 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4384 cupsdLogMessage(CUPSD_LOG_INFO
,
4385 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4387 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4388 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4389 NULL
)) != LDAP_SUCCESS
)
4391 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4392 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4393 cupsdLogMessage(CUPSD_LOG_WARN
,
4394 "LDAP delete for %s failed with status %d: %s",
4395 ou
, rc
, ldap_err2string(rc
));
4400 #endif /* HAVE_LDAP */
4405 * 'send_slp_browse()' - Register the specified printer with SLP.
4409 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4411 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4412 attrs
[8192], /* Printer attributes */
4413 finishings
[1024], /* Finishings to support */
4414 make_model
[IPP_MAX_NAME
* 2],
4415 /* Make and model, quoted */
4416 location
[IPP_MAX_NAME
* 2],
4417 /* Location, quoted */
4418 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4419 *src
, /* Pointer to original string */
4420 *dst
; /* Pointer to destination string */
4421 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4422 SLPError error
; /* SLP error, if any */
4425 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4429 * Make the SLP service URL that conforms to the IANA
4430 * 'printer:' template.
4433 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4438 * Figure out the finishings string...
4441 if (p
->type
& CUPS_PRINTER_STAPLE
)
4442 strcpy(finishings
, "staple");
4444 finishings
[0] = '\0';
4446 if (p
->type
& CUPS_PRINTER_BIND
)
4449 strlcat(finishings
, ",bind", sizeof(finishings
));
4451 strcpy(finishings
, "bind");
4454 if (p
->type
& CUPS_PRINTER_PUNCH
)
4457 strlcat(finishings
, ",punch", sizeof(finishings
));
4459 strcpy(finishings
, "punch");
4462 if (p
->type
& CUPS_PRINTER_COVER
)
4465 strlcat(finishings
, ",cover", sizeof(finishings
));
4467 strcpy(finishings
, "cover");
4470 if (p
->type
& CUPS_PRINTER_SORT
)
4473 strlcat(finishings
, ",sort", sizeof(finishings
));
4475 strcpy(finishings
, "sort");
4479 strcpy(finishings
, "none");
4482 * Quote any commas in the make and model, location, and info strings...
4485 for (src
= p
->make_model
, dst
= make_model
;
4486 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4488 if (*src
== ',' || *src
== '\\' || *src
== ')')
4497 strcpy(make_model
, "Unknown");
4499 for (src
= p
->location
, dst
= location
;
4500 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4502 if (*src
== ',' || *src
== '\\' || *src
== ')')
4511 strcpy(location
, "Unknown");
4513 for (src
= p
->info
, dst
= info
;
4514 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4516 if (*src
== ',' || *src
== '\\' || *src
== ')')
4525 strcpy(info
, "Unknown");
4528 * Get the authentication value...
4531 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4535 * Make the SLP attribute string list that conforms to
4536 * the IANA 'printer:' template.
4539 snprintf(attrs
, sizeof(attrs
),
4540 "(printer-uri-supported=%s),"
4541 "(uri-authentication-supported=%s>),"
4543 "(uri-security-supported=tls>),"
4545 "(uri-security-supported=none>),"
4546 #endif /* HAVE_SSL */
4547 "(printer-name=%s),"
4548 "(printer-location=%s),"
4549 "(printer-info=%s),"
4550 "(printer-more-info=%s),"
4551 "(printer-make-and-model=%s),"
4552 "(printer-type=%d),"
4553 "(charset-supported=utf-8),"
4554 "(natural-language-configured=%s),"
4555 "(natural-language-supported=de,en,es,fr,it),"
4556 "(color-supported=%s),"
4557 "(finishings-supported=%s),"
4558 "(sides-supported=one-sided%s),"
4559 "(multiple-document-jobs-supported=true)"
4560 "(ipp-versions-supported=1.0,1.1)",
4561 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4562 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4563 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4565 p
->type
& CUPS_PRINTER_DUPLEX
?
4566 ",two-sided-long-edge,two-sided-short-edge" : "");
4568 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4571 * Register the printer with the SLP server...
4574 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4575 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4577 if (error
!= SLP_OK
)
4578 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4584 * 'slp_attr_callback()' - SLP attribute callback
4587 static SLPBoolean
/* O - SLP_TRUE for success */
4589 SLPHandle hslp
, /* I - SLP handle */
4590 const char *attrlist
, /* I - Attribute list */
4591 SLPError errcode
, /* I - Parsing status for this attr */
4592 void *cookie
) /* I - Current printer */
4594 char *tmp
= 0; /* Temporary string */
4595 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4596 /* Current printer */
4599 (void)hslp
; /* anti-compiler-warning-code */
4602 * Bail if there was an error
4605 if (errcode
!= SLP_OK
)
4609 * Parse the attrlist to obtain things needed to build CUPS browse packet
4612 memset(p
, 0, sizeof(cupsd_printer_t
));
4614 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4616 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4618 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4620 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4621 p
->type
= atoi(tmp
);
4623 p
->type
= CUPS_PRINTER_REMOTE
;
4625 cupsdClearString(&tmp
);
4632 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4636 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4638 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4641 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4643 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4646 * Make the SLP service URL that conforms to the IANA
4647 * 'printer:' template.
4650 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4653 * Deregister the printer...
4656 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4662 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4665 static int /* O - 0 on success */
4666 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4667 const char *tag
, /* I - Name of attribute */
4668 char **valbuf
) /* O - Value */
4670 char *ptr1
, /* Pointer into string */
4674 cupsdClearString(valbuf
);
4676 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4678 ptr1
+= strlen(tag
);
4680 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4686 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4687 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4690 * Dequote the value...
4693 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4694 if (*ptr1
== '\\' && ptr1
[1])
4695 _cups_strcpy(ptr1
, ptr1
+ 1);
4706 * 'slp_reg_callback()' - Empty SLPRegReport.
4710 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4711 SLPError errcode
, /* I - Error code, if any */
4712 void *cookie
) /* I - App data */
4723 * 'slp_url_callback()' - SLP service url callback
4726 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4728 SLPHandle hslp
, /* I - SLP handle */
4729 const char *srvurl
, /* I - URL of service */
4730 unsigned short lifetime
, /* I - Life of service */
4731 SLPError errcode
, /* I - Existing error code */
4732 void *cookie
) /* I - Pointer to service list */
4734 slpsrvurl_t
*s
, /* New service entry */
4735 **head
; /* Pointer to head of entry */
4739 * Let the compiler know we won't be using these vars...
4746 * Bail if there was an error
4749 if (errcode
!= SLP_OK
)
4753 * Grab the head of the list...
4756 head
= (slpsrvurl_t
**)cookie
;
4759 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4762 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4766 * Copy the SLP service URL...
4769 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4772 * Link the SLP service URL into the head of the list
4782 #endif /* HAVE_LIBSLP */
4786 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4790 update_cups_browse(void)
4792 int i
; /* Looping var */
4793 int auth
; /* Authorization status */
4794 int len
; /* Length of name string */
4795 int bytes
; /* Number of bytes left */
4796 char packet
[1541], /* Broadcast packet */
4797 *pptr
; /* Pointer into packet */
4798 socklen_t srclen
; /* Length of source address */
4799 http_addr_t srcaddr
; /* Source address */
4800 char srcname
[1024]; /* Source hostname */
4801 unsigned address
[4]; /* Source address */
4802 unsigned type
; /* Printer type */
4803 unsigned state
; /* Printer state */
4804 char uri
[HTTP_MAX_URI
], /* Printer URI */
4805 host
[HTTP_MAX_URI
], /* Host portion of URI */
4806 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4807 info
[IPP_MAX_NAME
], /* Information string */
4808 location
[IPP_MAX_NAME
], /* Location string */
4809 make_model
[IPP_MAX_NAME
];/* Make and model string */
4810 int num_attrs
; /* Number of attributes */
4811 cups_option_t
*attrs
; /* Attributes */
4815 * Read a packet from the browse socket...
4818 srclen
= sizeof(srcaddr
);
4819 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4820 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4823 * "Connection refused" is returned under Linux if the destination port
4824 * or address is unreachable from a previous sendto(); check for the
4825 * error here and ignore it for now...
4828 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4830 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4832 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4835 closesocket(BrowseSocket
);
4837 close(BrowseSocket
);
4840 cupsdRemoveSelect(BrowseSocket
);
4843 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4844 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4850 packet
[bytes
] = '\0';
4853 * If we're about to sleep, ignore incoming browse packets.
4860 * Figure out where it came from...
4864 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4866 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4867 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4868 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4869 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4872 #endif /* AF_INET6 */
4877 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4880 if (HostNameLookups
)
4881 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4883 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4885 len
= strlen(srcname
);
4893 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4896 * Access from localhost (127.0.0.1) is always allowed...
4899 auth
= CUPSD_AUTH_ALLOW
;
4904 * Do authorization checks on the domain/address...
4907 switch (BrowseACL
->order_type
)
4910 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4913 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4914 auth
= CUPSD_AUTH_ALLOW
;
4916 if (cupsdCheckAuth(address
, srcname
, len
,
4917 BrowseACL
->num_deny
, BrowseACL
->deny
))
4918 auth
= CUPSD_AUTH_DENY
;
4920 if (cupsdCheckAuth(address
, srcname
, len
,
4921 BrowseACL
->num_allow
, BrowseACL
->allow
))
4922 auth
= CUPSD_AUTH_ALLOW
;
4925 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
4926 auth
= CUPSD_AUTH_DENY
;
4928 if (cupsdCheckAuth(address
, srcname
, len
,
4929 BrowseACL
->num_allow
, BrowseACL
->allow
))
4930 auth
= CUPSD_AUTH_ALLOW
;
4932 if (cupsdCheckAuth(address
, srcname
, len
,
4933 BrowseACL
->num_deny
, BrowseACL
->deny
))
4934 auth
= CUPSD_AUTH_DENY
;
4940 auth
= CUPSD_AUTH_ALLOW
;
4942 if (auth
== CUPSD_AUTH_DENY
)
4944 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4945 "update_cups_browse: Refused %d bytes from %s", bytes
,
4950 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4951 "update_cups_browse: (%d bytes from %s) %s", bytes
,
4958 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
4960 cupsdLogMessage(CUPSD_LOG_WARN
,
4961 "update_cups_browse: Garbled browse packet - %s", packet
);
4965 strcpy(location
, "Location Unknown");
4966 strcpy(info
, "No Information Available");
4967 make_model
[0] = '\0';
4971 if ((pptr
= strchr(packet
, '\"')) != NULL
)
4974 * Have extended information; can't use sscanf for it because not all
4975 * sscanf's allow empty strings with %[^\"]...
4978 for (i
= 0, pptr
++;
4979 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4981 location
[i
] = *pptr
;
4989 while (*pptr
&& isspace(*pptr
& 255))
4994 for (i
= 0, pptr
++;
4995 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5004 while (*pptr
&& isspace(*pptr
& 255))
5009 for (i
= 0, pptr
++;
5010 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5012 make_model
[i
] = *pptr
;
5017 make_model
[i
] = '\0';
5020 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5026 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5027 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5028 type
, state
, uri
, location
, info
, make_model
));
5031 * Pull the URI apart to see if this is a local or remote printer...
5034 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5036 cupsFreeOptions(num_attrs
, attrs
);
5044 for (i
= 0; i
< NumRelays
; i
++)
5045 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5046 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5047 (struct sockaddr
*)&(Relays
[i
].to
),
5048 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5050 cupsdLogMessage(CUPSD_LOG_ERROR
,
5051 "update_cups_browse: sendto failed for relay %d - %s.",
5052 i
+ 1, strerror(errno
));
5053 cupsFreeOptions(num_attrs
, attrs
);
5058 * Process the browse data...
5061 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5062 (ipp_pstate_t
)state
, location
, info
, make_model
,
5068 * 'update_lpd()' - Update the LPD configuration as needed.
5072 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5079 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5080 * setting for backwards-compatibility.
5083 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5085 #endif /* __APPLE__ */
5087 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5090 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5093 char newfile
[1024]; /* New cups-lpd.N file */
5094 cups_file_t
*ofp
, /* Original file pointer */
5095 *nfp
; /* New file pointer */
5096 char line
[1024]; /* Line from file */
5099 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5101 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5103 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5104 LPDConfigFile
+ 9, strerror(errno
));
5108 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5110 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5111 newfile
, strerror(errno
));
5117 * Copy all of the lines from the cups-lpd file...
5120 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5124 cupsFilePrintf(nfp
, "%s\n", line
);
5125 snprintf(line
, sizeof(line
), "\tdisable = %s",
5126 onoff
? "no" : "yes");
5128 else if (!strstr(line
, "disable ="))
5129 cupsFilePrintf(nfp
, "%s\n", line
);
5134 rename(newfile
, LPDConfigFile
+ 9);
5137 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5140 * Enable/disable LPD via the launchctl command...
5143 char *argv
[5], /* Arguments for command */
5144 *envp
[MAX_ENV
]; /* Environment for command */
5145 int pid
; /* Process ID */
5148 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5149 argv
[0] = (char *)"launchctl";
5150 argv
[1] = (char *)(onoff
? "load" : "unload");
5151 argv
[2] = (char *)"-w";
5152 argv
[3] = LPDConfigFile
+ 10;
5155 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5158 #endif /* __APPLE__ */
5160 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5165 * 'update_polling()' - Read status messages from the poll daemons.
5169 update_polling(void)
5171 char *ptr
, /* Pointer to end of line in buffer */
5172 message
[1024]; /* Pointer to message text */
5173 int loglevel
; /* Log level for message */
5176 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5177 message
, sizeof(message
))) != NULL
)
5178 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5181 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5184 * All polling processes have died; stop polling...
5187 cupsdLogMessage(CUPSD_LOG_ERROR
,
5188 "update_polling: all polling processes have exited!");
5195 * 'update_smb()' - Update the SMB configuration as needed.
5199 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5204 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5207 * Enable/disable SMB via the specified smb.conf config file...
5210 char newfile
[1024]; /* New smb.conf.N file */
5211 cups_file_t
*ofp
, /* Original file pointer */
5212 *nfp
; /* New file pointer */
5213 char line
[1024]; /* Line from file */
5214 int in_printers
; /* In [printers] section? */
5217 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5219 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5221 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5222 SMBConfigFile
+ 8, strerror(errno
));
5226 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5228 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5229 newfile
, strerror(errno
));
5235 * Copy all of the lines from the smb.conf file...
5240 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5242 if (in_printers
&& strstr(line
, "printable ="))
5243 snprintf(line
, sizeof(line
), " printable = %s",
5244 onoff
? "yes" : "no");
5246 cupsFilePrintf(nfp
, "%s\n", line
);
5249 in_printers
= !strcmp(line
, "[printers]");
5254 rename(newfile
, SMBConfigFile
+ 8);
5257 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5262 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".