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-2009 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 cupsdLogMessage(CUPSD_LOG_DEBUG
,
242 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
245 if (!Browsing
|| !p
->shared
||
246 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
250 * Announce the deletion...
253 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
255 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
257 p
->type
|= CUPS_PRINTER_DELETE
;
265 if (BrowseLocalProtocols
& BROWSE_SLP
)
266 slp_dereg_printer(p
);
267 #endif /* HAVE_LIBSLP */
270 if (BrowseLocalProtocols
& BROWSE_LDAP
)
271 ldap_dereg_printer(p
);
272 #endif /* HAVE_LDAP */
275 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
276 dnssdDeregisterPrinter(p
);
277 #endif /* HAVE_DNSSD */
282 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
286 cupsdLoadRemoteCache(void)
288 int i
; /* Looping var */
289 cups_file_t
*fp
; /* remote.cache file */
290 int linenum
; /* Current line number */
291 char line
[4096], /* Line from file */
292 *value
, /* Pointer to value */
293 *valueptr
, /* Pointer into value */
294 scheme
[32], /* Scheme portion of URI */
295 username
[64], /* Username portion of URI */
297 /* Hostname portion of URI */
298 resource
[HTTP_MAX_URI
];
299 /* Resource portion of URI */
300 int port
; /* Port number */
301 cupsd_printer_t
*p
; /* Current printer */
302 time_t now
; /* Current time */
306 * Don't load the cache if the remote protocols are disabled...
311 cupsdLogMessage(CUPSD_LOG_DEBUG
,
312 "cupsdLoadRemoteCache: Not loading remote cache.");
317 * Open the remote.cache file...
320 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
321 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
325 * Read printer configurations until we hit EOF...
332 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
335 * Decode the directive...
338 if (!strcasecmp(line
, "<Printer") ||
339 !strcasecmp(line
, "<DefaultPrinter"))
342 * <Printer name> or <DefaultPrinter name>
345 if (p
== NULL
&& value
)
348 * Add the printer and a base file type...
351 cupsdLogMessage(CUPSD_LOG_DEBUG
,
352 "cupsdLoadRemoteCache: Loading printer %s...", value
);
354 if ((p
= cupsdFindDest(value
)) != NULL
)
356 if (p
->type
& CUPS_PRINTER_CLASS
)
358 cupsdLogMessage(CUPSD_LOG_WARN
,
359 "Cached remote printer \"%s\" conflicts with "
367 p
= cupsdAddPrinter(value
);
370 p
->state
= IPP_PRINTER_IDLE
;
371 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
372 p
->browse_time
= now
;
373 p
->browse_expire
= now
+ BrowseTimeout
;
376 * Set the default printer as needed...
379 if (!strcasecmp(line
, "<DefaultPrinter"))
384 cupsdLogMessage(CUPSD_LOG_ERROR
,
385 "Syntax error on line %d of remote.cache.", linenum
);
389 else if (!strcasecmp(line
, "<Class") ||
390 !strcasecmp(line
, "<DefaultClass"))
393 * <Class name> or <DefaultClass name>
396 if (p
== NULL
&& value
)
399 * Add the printer and a base file type...
402 cupsdLogMessage(CUPSD_LOG_DEBUG
,
403 "cupsdLoadRemoteCache: Loading class %s...", value
);
405 if ((p
= cupsdFindDest(value
)) != NULL
)
406 p
->type
= CUPS_PRINTER_CLASS
;
408 p
= cupsdAddClass(value
);
411 p
->state
= IPP_PRINTER_IDLE
;
412 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
413 p
->browse_time
= now
;
414 p
->browse_expire
= now
+ BrowseTimeout
;
417 * Set the default printer as needed...
420 if (!strcasecmp(line
, "<DefaultClass"))
425 cupsdLogMessage(CUPSD_LOG_ERROR
,
426 "Syntax error on line %d of remote.cache.", linenum
);
430 else if (!strcasecmp(line
, "</Printer>") ||
431 !strcasecmp(line
, "</Class>"))
436 * Close out the current printer...
439 cupsdSetPrinterAttrs(p
);
444 cupsdLogMessage(CUPSD_LOG_ERROR
,
445 "Syntax error on line %d of remote.cache.", linenum
);
449 cupsdLogMessage(CUPSD_LOG_ERROR
,
450 "Syntax error on line %d of remote.cache.", linenum
);
452 else if (!strcasecmp(line
, "Info"))
455 cupsdSetString(&p
->info
, value
);
457 else if (!strcasecmp(line
, "MakeModel"))
460 cupsdSetString(&p
->make_model
, value
);
462 else if (!strcasecmp(line
, "Location"))
465 cupsdSetString(&p
->location
, value
);
467 else if (!strcasecmp(line
, "DeviceURI"))
471 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
472 username
, sizeof(username
), host
, sizeof(host
), &port
,
473 resource
, sizeof(resource
));
475 cupsdSetString(&p
->hostname
, host
);
476 cupsdSetString(&p
->uri
, value
);
477 cupsdSetDeviceURI(p
, value
);
480 cupsdLogMessage(CUPSD_LOG_ERROR
,
481 "Syntax error on line %d of remote.cache.", linenum
);
483 else if (!strcasecmp(line
, "Option") && value
)
489 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
492 cupsdLogMessage(CUPSD_LOG_ERROR
,
493 "Syntax error on line %d of remote.cache.", linenum
);
496 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
498 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
502 else if (!strcasecmp(line
, "Reason"))
506 for (i
= 0 ; i
< p
->num_reasons
; i
++)
507 if (!strcmp(value
, p
->reasons
[i
]))
510 if (i
>= p
->num_reasons
&&
511 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
513 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
518 cupsdLogMessage(CUPSD_LOG_ERROR
,
519 "Syntax error on line %d of remote.cache.", linenum
);
521 else if (!strcasecmp(line
, "State"))
524 * Set the initial queue state...
527 if (value
&& !strcasecmp(value
, "idle"))
528 p
->state
= IPP_PRINTER_IDLE
;
529 else if (value
&& !strcasecmp(value
, "stopped"))
531 p
->state
= IPP_PRINTER_STOPPED
;
532 cupsdSetPrinterReasons(p
, "+paused");
535 cupsdLogMessage(CUPSD_LOG_ERROR
,
536 "Syntax error on line %d of remote.cache.", linenum
);
538 else if (!strcasecmp(line
, "StateMessage"))
541 * Set the initial queue state message...
545 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
547 else if (!strcasecmp(line
, "Accepting"))
550 * Set the initial accepting state...
554 (!strcasecmp(value
, "yes") ||
555 !strcasecmp(value
, "on") ||
556 !strcasecmp(value
, "true")))
559 (!strcasecmp(value
, "no") ||
560 !strcasecmp(value
, "off") ||
561 !strcasecmp(value
, "false")))
564 cupsdLogMessage(CUPSD_LOG_ERROR
,
565 "Syntax error on line %d of remote.cache.", linenum
);
567 else if (!strcasecmp(line
, "Type"))
570 p
->type
= atoi(value
);
572 cupsdLogMessage(CUPSD_LOG_ERROR
,
573 "Syntax error on line %d of remote.cache.", linenum
);
575 else if (!strcasecmp(line
, "BrowseTime"))
579 time_t t
= atoi(value
);
581 if (t
> p
->browse_expire
)
582 p
->browse_expire
= t
;
585 cupsdLogMessage(CUPSD_LOG_ERROR
,
586 "Syntax error on line %d of remote.cache.", linenum
);
588 else if (!strcasecmp(line
, "JobSheets"))
591 * Set the initial job sheets...
596 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
601 cupsdSetString(&p
->job_sheets
[0], value
);
603 while (isspace(*valueptr
& 255))
608 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
613 cupsdSetString(&p
->job_sheets
[1], value
);
617 cupsdLogMessage(CUPSD_LOG_ERROR
,
618 "Syntax error on line %d of remote.cache.", linenum
);
620 else if (!strcasecmp(line
, "AllowUser"))
625 cupsdAddPrinterUser(p
, value
);
628 cupsdLogMessage(CUPSD_LOG_ERROR
,
629 "Syntax error on line %d of remote.cache.", linenum
);
631 else if (!strcasecmp(line
, "DenyUser"))
636 cupsdAddPrinterUser(p
, value
);
639 cupsdLogMessage(CUPSD_LOG_ERROR
,
640 "Syntax error on line %d of remote.cache.", linenum
);
645 * Something else we don't understand...
648 cupsdLogMessage(CUPSD_LOG_ERROR
,
649 "Unknown configuration directive %s on line %d of remote.cache.",
657 * Do auto-classing if needed...
660 process_implicit_classes();
665 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
666 * printer or update the broadcast contents.
670 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
672 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
675 if (!Browsing
|| !BrowseLocalProtocols
||
676 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
680 /* if (BrowseLocalProtocols & BROWSE_SLP)
681 slpRegisterPrinter(p); */
682 #endif /* HAVE_LIBSLP */
685 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
686 dnssdRegisterPrinter(p
);
687 #endif /* HAVE_DNSSD */
692 * 'cupsdRestartPolling()' - Restart polling servers as needed.
696 cupsdRestartPolling(void)
698 int i
; /* Looping var */
699 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
702 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
704 kill(pollp
->pid
, SIGHUP
);
709 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
713 cupsdSaveRemoteCache(void)
715 int i
; /* Looping var */
716 cups_file_t
*fp
; /* printers.conf file */
717 char temp
[1024], /* Temporary string */
718 value
[2048]; /* Value string */
719 cupsd_printer_t
*printer
; /* Current printer class */
720 time_t curtime
; /* Current time */
721 struct tm
*curdate
; /* Current date */
722 cups_option_t
*option
; /* Current option */
726 * Create the remote.cache file...
729 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
731 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
733 cupsdLogMessage(CUPSD_LOG_ERROR
,
734 "Unable to save remote.cache - %s", strerror(errno
));
738 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
741 * Restrict access to the file...
744 fchown(cupsFileNumber(fp
), getuid(), Group
);
745 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
748 * Write a small header to the file...
751 curtime
= time(NULL
);
752 curdate
= localtime(&curtime
);
753 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
755 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
756 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
759 * Write each local printer known to the system...
762 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
764 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
767 * Skip local destinations...
770 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
774 * Write printers as needed...
777 if (printer
== DefaultPrinter
)
778 cupsFilePuts(fp
, "<Default");
780 cupsFilePutChar(fp
, '<');
782 if (printer
->type
& CUPS_PRINTER_CLASS
)
783 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
785 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
787 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
790 cupsFilePutConf(fp
, "Info", printer
->info
);
792 if (printer
->location
)
793 cupsFilePutConf(fp
, "Location", printer
->location
);
795 if (printer
->make_model
)
796 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
798 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
800 if (printer
->state
== IPP_PRINTER_STOPPED
)
801 cupsFilePuts(fp
, "State Stopped\n");
803 cupsFilePuts(fp
, "State Idle\n");
805 for (i
= 0; i
< printer
->num_reasons
; i
++)
806 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
808 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
810 if (printer
->accepting
)
811 cupsFilePuts(fp
, "Accepting Yes\n");
813 cupsFilePuts(fp
, "Accepting No\n");
815 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
816 printer
->job_sheets
[1]);
817 cupsFilePutConf(fp
, "JobSheets", value
);
819 for (i
= 0; i
< printer
->num_users
; i
++)
820 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
823 for (i
= printer
->num_options
, option
= printer
->options
;
827 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
828 cupsFilePutConf(fp
, "Option", value
);
831 if (printer
->type
& CUPS_PRINTER_CLASS
)
832 cupsFilePuts(fp
, "</Class>\n");
834 cupsFilePuts(fp
, "</Printer>\n");
842 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
846 cupsdSendBrowseList(void)
848 int count
; /* Number of dests to update */
849 cupsd_printer_t
*p
; /* Current printer */
850 time_t ut
, /* Minimum update time */
851 to
; /* Timeout time */
854 if (!Browsing
|| !Printers
)
858 * Compute the update and timeout times...
862 ut
= to
- BrowseInterval
;
865 * Figure out how many printers need an update...
868 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
870 int max_count
; /* Maximum number to update */
874 * Throttle the number of printers we'll be updating this time
875 * around based on the number of queues that need updating and
876 * the maximum number of queues to update each second...
879 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
881 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
882 count
< max_count
&& p
!= NULL
;
883 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
884 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
885 p
->shared
&& p
->browse_time
< ut
)
889 * Loop through all of the printers and send local updates as needed...
893 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
895 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
899 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
902 * Check for wraparound...
906 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
910 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
913 else if (p
->browse_time
< ut
)
916 * Need to send an update...
921 p
->browse_time
= time(NULL
);
923 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
927 if (BrowseLocalProtocols
& BROWSE_SLP
)
929 #endif /* HAVE_LIBSLP */
932 if (BrowseLocalProtocols
& BROWSE_LDAP
)
934 #endif /* HAVE_LDAP */
939 * Save where we left off so that all printers get updated...
946 * Loop through all of the printers and timeout old printers as needed...
949 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
951 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
954 * If this is a remote queue, see if it needs to be timed out...
957 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
958 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
959 p
->browse_expire
< to
)
961 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
962 "%s \'%s\' deleted by directory services (timeout).",
963 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
966 cupsdLogMessage(CUPSD_LOG_DEBUG
,
967 "Remote destination \"%s\" has timed out; "
971 cupsArraySave(Printers
);
972 cupsdDeletePrinter(p
, 1);
973 cupsArrayRestore(Printers
);
974 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
980 #ifdef HAVE_LDAP_REBIND_PROC
981 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
983 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
987 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
988 LDAP_CONST
char *refsp
,
996 * Bind to new LDAP server...
999 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1000 "ldap_rebind_proc: Rebind to %s", refsp
);
1002 # if LDAP_API_VERSION > 3000
1004 bval
.bv_val
= BrowseLDAPPassword
;
1005 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1007 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1009 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
1010 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1011 # endif /* LDAP_API_VERSION > 3000 */
1016 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1019 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1023 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1035 * Free current values...
1038 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1039 "ldap_rebind_proc: Free values...");
1041 if ( dnp
&& *dnp
) {
1044 if ( passwdp
&& *passwdp
) {
1052 * Return credentials for LDAP referal...
1055 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1056 "ldap_rebind_proc: Return necessary values...");
1058 *dnp
= strdup(BrowseLDAPBindDN
);
1059 *passwdp
= strdup(BrowseLDAPPassword
);
1060 *authmethodp
= LDAP_AUTH_SIMPLE
;
1066 * Should never happen...
1069 cupsdLogMessage(CUPSD_LOG_ERROR
,
1070 "LDAP rebind has been called with wrong freeit value!");
1075 return (LDAP_SUCCESS
);
1077 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1078 #endif /* HAVE_LDAP_REBIND_PROC */
1083 * 'ldap_connect()' - Start new LDAP connection
1090 * Open LDAP handle...
1093 int rc
; /* LDAP API status */
1094 int version
= 3; /* LDAP version */
1095 struct berval bv
= {0, ""}; /* SASL bind value */
1096 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1097 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1098 int ldap_ssl
= 0; /* LDAP SSL indicator */
1099 int ssl_err
= 0; /* LDAP SSL error value */
1100 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1102 # ifdef HAVE_OPENLDAP
1103 # ifdef HAVE_LDAP_SSL
1106 * Set the certificate file to use for encrypted LDAP sessions...
1109 if (BrowseLDAPCACertFile
)
1111 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1112 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1113 BrowseLDAPCACertFile
);
1115 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1116 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1117 cupsdLogMessage(CUPSD_LOG_ERROR
,
1118 "Unable to set CA certificate file for LDAP "
1119 "connections: %d - %s", rc
, ldap_err2string(rc
));
1122 # endif /* HAVE_LDAP_SSL */
1124 * Initialize OPENLDAP connection...
1125 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1128 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1129 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1131 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1133 # else /* HAVE_OPENLDAP */
1135 int ldap_port
= 0; /* LDAP port */
1136 char ldap_protocol
[11], /* LDAP protocol */
1137 ldap_host
[255]; /* LDAP host */
1140 * Split LDAP URI into its components...
1143 if (! BrowseLDAPServer
)
1145 cupsdLogMessage(CUPSD_LOG_ERROR
,
1146 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1147 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1148 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1152 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1154 if (strcmp(ldap_protocol
, "ldap") == 0) {
1156 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1159 cupsdLogMessage(CUPSD_LOG_ERROR
,
1160 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1161 cupsdLogMessage(CUPSD_LOG_ERROR
,
1162 "Disable LDAP browsing!");
1163 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1164 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1171 ldap_port
= LDAPS_PORT
;
1173 ldap_port
= LDAP_PORT
;
1176 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1177 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1178 ldap_protocol
, ldap_host
, ldap_port
);
1181 * Initialize LDAP connection...
1186 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1187 rc
= LDAP_OPERATIONS_ERROR
;
1191 # ifdef HAVE_LDAP_SSL
1197 * Initialize SSL LDAP connection...
1199 if (BrowseLDAPCACertFile
)
1201 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1202 if (rc
!= LDAP_SUCCESS
) {
1203 cupsdLogMessage(CUPSD_LOG_ERROR
,
1204 "Failed to initialize LDAP SSL client!");
1205 rc
= LDAP_OPERATIONS_ERROR
;
1207 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1208 rc
= LDAP_OPERATIONS_ERROR
;
1215 cupsdLogMessage(CUPSD_LOG_ERROR
,
1216 "LDAP SSL certificate file/database not configured!");
1217 rc
= LDAP_OPERATIONS_ERROR
;
1220 # else /* HAVE_LDAP_SSL */
1223 * Return error, because client libraries doesn't support SSL
1226 cupsdLogMessage(CUPSD_LOG_ERROR
,
1227 "LDAP client libraries does not support TLS");
1228 rc
= LDAP_OPERATIONS_ERROR
;
1230 # endif /* HAVE_LDAP_SSL */
1232 # endif /* HAVE_OPENLDAP */
1235 * Check return code from LDAP initialize...
1238 if (rc
!= LDAP_SUCCESS
)
1240 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1242 cupsdLogMessage(CUPSD_LOG_ERROR
,
1243 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1247 cupsdLogMessage(CUPSD_LOG_ERROR
,
1248 "Unable to initialize LDAP! Disable LDAP browsing!");
1249 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1250 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1253 ldap_disconnect(TempBrowseLDAPHandle
);
1254 TempBrowseLDAPHandle
= NULL
;
1258 * Upgrade LDAP version...
1261 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1262 (const void *)&version
) != LDAP_SUCCESS
)
1264 cupsdLogMessage(CUPSD_LOG_ERROR
,
1265 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1267 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1268 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1269 ldap_disconnect(TempBrowseLDAPHandle
);
1270 TempBrowseLDAPHandle
= NULL
;
1276 * Register LDAP rebind procedure...
1279 # ifdef HAVE_LDAP_REBIND_PROC
1280 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1282 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1283 if ( rc
!= LDAP_SUCCESS
)
1284 cupsdLogMessage(CUPSD_LOG_ERROR
,
1285 "Setting LDAP rebind function failed with status %d: %s",
1286 rc
, ldap_err2string(rc
));
1290 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1292 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1293 # endif /* HAVE_LDAP_REBIND_PROC */
1296 * Start LDAP bind...
1299 # if LDAP_API_VERSION > 3000
1301 bval
.bv_val
= BrowseLDAPPassword
;
1302 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1304 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1305 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1308 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1310 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1311 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1312 # endif /* LDAP_API_VERSION > 3000 */
1314 if (rc
!= LDAP_SUCCESS
)
1316 cupsdLogMessage(CUPSD_LOG_ERROR
,
1317 "LDAP bind failed with error %d: %s",
1318 rc
, ldap_err2string(rc
));
1319 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1320 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1322 ssl_err
= PORT_GetError();
1324 cupsdLogMessage(CUPSD_LOG_ERROR
,
1325 "LDAP SSL error %d: %s",
1326 ssl_err
, ldapssl_err2string(ssl_err
));
1328 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1329 ldap_disconnect(TempBrowseLDAPHandle
);
1330 TempBrowseLDAPHandle
= NULL
;
1334 cupsdLogMessage(CUPSD_LOG_INFO
,
1335 "LDAP connection established");
1339 return (TempBrowseLDAPHandle
);
1344 * 'ldap_reconnect()' - Reconnect to LDAP Server
1348 ldap_reconnect(void)
1350 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1352 cupsdLogMessage(CUPSD_LOG_INFO
,
1353 "Try LDAP reconnect...");
1356 * Get a new LDAP Handle and replace the global Handle
1357 * if the new connection was successful
1360 TempBrowseLDAPHandle
= ldap_connect();
1362 if (TempBrowseLDAPHandle
!= NULL
)
1364 if (BrowseLDAPHandle
!= NULL
)
1366 ldap_disconnect(BrowseLDAPHandle
);
1368 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1374 * 'ldap_disconnect()' - Disconnect from LDAP Server
1378 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1380 int rc
; /* return code */
1383 * Close LDAP handle...
1386 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1387 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1389 rc
= ldap_unbind_s(ld
);
1390 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1391 if (rc
!= LDAP_SUCCESS
)
1392 cupsdLogMessage(CUPSD_LOG_ERROR
,
1393 "Unbind from LDAP server failed with status %d: %s",
1394 rc
, ldap_err2string(rc
));
1396 #endif /* HAVE_LDAP */
1400 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1404 cupsdStartBrowsing(void)
1406 int val
; /* Socket option value */
1407 struct sockaddr_in addr
; /* Broadcast address */
1408 cupsd_printer_t
*p
; /* Current printer */
1413 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1416 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1418 if (BrowseSocket
< 0)
1421 * Create the broadcast socket...
1424 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1426 cupsdLogMessage(CUPSD_LOG_ERROR
,
1427 "Unable to create broadcast socket - %s.",
1429 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1430 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1432 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1433 cupsdEndProcess(getpid(), 0);
1437 if (BrowseSocket
>= 0)
1440 * Bind the socket to browse port...
1443 memset(&addr
, 0, sizeof(addr
));
1444 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1445 addr
.sin_family
= AF_INET
;
1446 addr
.sin_port
= htons(BrowsePort
);
1448 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1450 cupsdLogMessage(CUPSD_LOG_ERROR
,
1451 "Unable to bind broadcast socket - %s.",
1455 closesocket(BrowseSocket
);
1457 close(BrowseSocket
);
1461 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1462 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1464 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1465 cupsdEndProcess(getpid(), 0);
1469 if (BrowseSocket
>= 0)
1472 * Set the "broadcast" flag...
1476 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1478 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1482 closesocket(BrowseSocket
);
1484 close(BrowseSocket
);
1488 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1489 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1491 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1492 cupsdEndProcess(getpid(), 0);
1496 if (BrowseSocket
>= 0)
1499 * Close the socket on exec...
1502 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1505 * Finally, add the socket to the input selection set as needed...
1508 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1511 * We only listen if we want remote printers...
1514 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1523 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1525 DNSServiceErrorType error
; /* Error from service creation */
1526 cupsd_listener_t
*lis
; /* Current listening socket */
1530 * First create a "master" connection for all registrations...
1533 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1534 != kDNSServiceErr_NoError
)
1536 cupsdLogMessage(CUPSD_LOG_ERROR
,
1537 "Unable to create master DNS-SD reference: %d", error
);
1539 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1540 cupsdEndProcess(getpid(), 0);
1545 * Add the master connection to the select list...
1548 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1549 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1552 * Then get the port we use for registrations. If we are not listening
1553 * on any non-local ports, there is no sense sharing local printers via
1559 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1561 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1563 if (httpAddrLocalhost(&(lis
->address
)))
1566 if (lis
->address
.addr
.sa_family
== AF_INET
)
1568 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1571 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1573 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1579 * Create an array to track the printers we share...
1582 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1583 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1587 * Set the computer name and register the web interface...
1590 cupsdUpdateDNSSDName();
1593 #endif /* HAVE_DNSSD */
1596 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1599 * Open SLP handle...
1602 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1604 cupsdLogMessage(CUPSD_LOG_ERROR
,
1605 "Unable to open an SLP handle; disabling SLP browsing!");
1606 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1607 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1608 BrowseSLPHandle
= NULL
;
1610 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1611 cupsdEndProcess(getpid(), 0);
1614 BrowseSLPRefresh
= 0;
1617 BrowseSLPHandle
= NULL
;
1618 #endif /* HAVE_LIBSLP */
1621 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1625 cupsdLogMessage(CUPSD_LOG_ERROR
,
1626 "Need to set BrowseLDAPDN to use LDAP browsing!");
1627 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1628 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1630 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1631 cupsdEndProcess(getpid(), 0);
1636 * Open LDAP handle...
1639 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1640 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1641 cupsdEndProcess(getpid(), 0);
1644 BrowseLDAPRefresh
= 0;
1646 #endif /* HAVE_LDAP */
1649 * Enable LPD and SMB printer sharing as needed through external programs...
1652 if (BrowseLocalProtocols
& BROWSE_LPD
)
1655 if (BrowseLocalProtocols
& BROWSE_SMB
)
1659 * Register the individual printers
1662 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1664 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1665 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1666 cupsdRegisterPrinter(p
);
1671 * 'cupsdStartPolling()' - Start polling servers as needed.
1675 cupsdStartPolling(void)
1677 int i
; /* Looping var */
1678 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1679 char polld
[1024]; /* Poll daemon path */
1680 char sport
[255]; /* Server port */
1681 char bport
[255]; /* Browser port */
1682 char interval
[255]; /* Poll interval */
1683 int statusfds
[2]; /* Status pipe */
1684 char *argv
[6]; /* Arguments */
1685 char *envp
[100]; /* Environment */
1689 * Don't do anything if we aren't polling...
1692 if (NumPolled
== 0 || BrowseSocket
< 0)
1695 PollStatusBuffer
= NULL
;
1700 * Setup string arguments for polld, port and interval options.
1703 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1705 sprintf(bport
, "%d", BrowsePort
);
1708 sprintf(interval
, "%d", BrowseInterval
);
1710 strcpy(interval
, "30");
1712 argv
[0] = "cups-polld";
1718 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1721 * Create a pipe that receives the status messages from each
1725 if (cupsdOpenPipe(statusfds
))
1727 cupsdLogMessage(CUPSD_LOG_ERROR
,
1728 "Unable to create polling status pipes - %s.",
1731 PollStatusBuffer
= NULL
;
1735 PollPipe
= statusfds
[0];
1736 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1739 * Run each polling daemon, redirecting stderr to the polling pipe...
1742 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1744 sprintf(sport
, "%d", pollp
->port
);
1746 argv
[1] = pollp
->hostname
;
1748 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1749 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1751 cupsdLogMessage(CUPSD_LOG_ERROR
,
1752 "cupsdStartPolling: Unable to fork polling daemon - %s",
1758 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1759 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1760 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1763 close(statusfds
[1]);
1766 * Finally, add the pipe to the input selection set...
1769 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1774 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1778 cupsdStopBrowsing(void)
1780 cupsd_printer_t
*p
; /* Current printer */
1783 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1787 * De-register the individual printers
1790 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1792 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1793 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1794 cupsdDeregisterPrinter(p
, 1);
1797 * Shut down browsing sockets...
1800 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1804 * Close the socket and remove it from the input selection set.
1808 closesocket(BrowseSocket
);
1810 close(BrowseSocket
);
1813 cupsdRemoveSelect(BrowseSocket
);
1818 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1822 DNSServiceRefDeallocate(WebIFRef
);
1828 DNSServiceRefDeallocate(RemoteRef
);
1832 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1834 DNSServiceRefDeallocate(DNSSDRef
);
1837 cupsArrayDelete(DNSSDPrinters
);
1838 DNSSDPrinters
= NULL
;
1842 #endif /* HAVE_DNSSD */
1845 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1849 * Close SLP handle...
1852 SLPClose(BrowseSLPHandle
);
1853 BrowseSLPHandle
= NULL
;
1855 #endif /* HAVE_LIBSLP */
1858 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1861 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1862 ldap_disconnect(BrowseLDAPHandle
);
1863 BrowseLDAPHandle
= NULL
;
1865 #endif /* HAVE_OPENLDAP */
1868 * Disable LPD and SMB printer sharing as needed through external programs...
1871 if (BrowseLocalProtocols
& BROWSE_LPD
)
1874 if (BrowseLocalProtocols
& BROWSE_SMB
)
1880 * 'cupsdStopPolling()' - Stop polling servers as needed.
1884 cupsdStopPolling(void)
1886 int i
; /* Looping var */
1887 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1892 cupsdStatBufDelete(PollStatusBuffer
);
1895 cupsdRemoveSelect(PollPipe
);
1898 PollStatusBuffer
= NULL
;
1901 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1903 cupsdEndProcess(pollp
->pid
, 0);
1909 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1913 cupsdUpdateDNSSDName(void)
1915 DNSServiceErrorType error
; /* Error from service creation */
1916 char webif
[1024]; /* Web interface share name */
1917 #ifdef HAVE_COREFOUNDATION_H
1918 CFStringRef nameRef
; /* Computer name CFString */
1919 char nameBuffer
[1024]; /* C-string buffer */
1920 CFStringEncoding nameEncoding
; /* Computer name encoding */
1921 #endif /* HAVE_COREFOUNDATION_H */
1925 * Only share the web interface and printers when non-local listening is
1933 * Get the computer name as a c-string...
1936 #ifdef HAVE_COREFOUNDATION_H
1937 cupsdClearString(&DNSSDName
);
1939 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1940 &nameEncoding
)) != NULL
)
1942 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1943 kCFStringEncodingUTF8
))
1944 cupsdSetString(&DNSSDName
, nameBuffer
);
1950 cupsdSetString(&DNSSDName
, ServerName
);
1951 #endif /* HAVE_COREFOUNDATION_H */
1954 * Then (re)register the web interface if enabled...
1960 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1962 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1965 DNSServiceRefDeallocate(WebIFRef
);
1967 WebIFRef
= DNSSDRef
;
1968 if ((error
= DNSServiceRegister(&WebIFRef
,
1969 kDNSServiceFlagsShareConnection
,
1970 0, webif
, "_http._tcp", NULL
,
1971 NULL
, htons(DNSSDPort
), 7,
1972 "\006path=/", dnssdRegisterCallback
,
1973 NULL
)) != kDNSServiceErr_NoError
)
1974 cupsdLogMessage(CUPSD_LOG_ERROR
,
1975 "DNS-SD web interface registration failed: %d", error
);
1978 #endif /* HAVE_DNSSD */
1983 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1987 cupsdUpdateLDAPBrowse(void)
1989 char uri
[HTTP_MAX_URI
], /* Printer URI */
1990 host
[HTTP_MAX_URI
], /* Hostname */
1991 resource
[HTTP_MAX_URI
], /* Resource path */
1992 location
[1024], /* Printer location */
1993 info
[1024], /* Printer information */
1994 make_model
[1024], /* Printer make and model */
1995 type_num
[30]; /* Printer type number */
1996 int type
; /* Printer type */
1997 int rc
; /* LDAP status */
1998 int limit
; /* Size limit */
1999 LDAPMessage
*res
, /* LDAP search results */
2000 *e
; /* Current entry from search */
2002 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2004 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2007 * Reconnect if LDAP Handle is invalid...
2010 if (! BrowseLDAPHandle
)
2017 * Search for cups printers in LDAP directory...
2020 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2021 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2024 * If ldap search was successfull then exit function
2025 * and temporary disable LDAP updates...
2028 if (rc
!= LDAP_SUCCESS
)
2030 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2032 BrowseLDAPUpdate
= FALSE
;
2033 cupsdLogMessage(CUPSD_LOG_INFO
,
2034 "LDAP update temporary disabled");
2040 * If LDAP updates were disabled, we will reenable them...
2043 if (! BrowseLDAPUpdate
)
2045 BrowseLDAPUpdate
= TRUE
;
2046 cupsdLogMessage(CUPSD_LOG_INFO
,
2047 "LDAP update enabled");
2051 * Count LDAP entries and return if no entry exist...
2054 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2055 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2063 * Loop through the available printers...
2066 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2068 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2071 * Get the required values from this entry...
2074 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2075 "printerDescription", info
, sizeof(info
)) == -1)
2078 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2079 "printerLocation", location
, sizeof(location
)) == -1)
2082 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2083 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2086 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2087 "printerType", type_num
, sizeof(type_num
)) == -1)
2090 type
= atoi(type_num
);
2092 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2093 "printerURI", uri
, sizeof(uri
)) == -1)
2097 * Process the entry as browse data...
2100 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2101 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2102 location
, info
, make_model
, 0, NULL
);
2108 #endif /* HAVE_LDAP */
2113 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2117 cupsdUpdateSLPBrowse(void)
2119 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2120 *next
; /* Next service in list */
2121 cupsd_printer_t p
; /* Printer information */
2122 const char *uri
; /* Pointer to printer URI */
2123 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2124 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2128 * Reset the refresh time...
2131 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2134 * Poll for remote printers using SLP...
2139 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2140 slp_url_callback
, &s
);
2143 * Loop through the list of available printers...
2149 * Save the "next" pointer...
2155 * Load a cupsd_printer_t structure with the SLP service attributes...
2158 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2161 * Process this printer entry...
2164 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2166 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2169 * Pull the URI apart to see if this is a local or remote printer...
2172 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2173 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2174 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2178 * Free this listing...
2181 cupsdClearString(&p
.info
);
2182 cupsdClearString(&p
.location
);
2183 cupsdClearString(&p
.make_model
);
2188 #endif /* HAVE_LIBSLP */
2192 * 'dequote()' - Remote quotes from a string.
2195 static char * /* O - Dequoted string */
2196 dequote(char *d
, /* I - Destination string */
2197 const char *s
, /* I - Source string */
2198 int dlen
) /* I - Destination length */
2200 char *dptr
; /* Pointer into destination */
2205 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2223 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2226 static char * /* O - TXT record */
2227 dnssdBuildTxtRecord(
2228 int *txt_len
, /* O - TXT record length */
2229 cupsd_printer_t
*p
, /* I - Printer information */
2230 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2232 int i
, j
; /* Looping vars */
2233 char type_str
[32], /* Type to string buffer */
2234 state_str
[32], /* State to string buffer */
2235 rp_str
[1024], /* Queue name string buffer */
2236 air_str
[1024], /* auth-info-required string buffer */
2237 *keyvalue
[32][2]; /* Table of key/value pairs */
2238 ipp_attribute_t
*air_attr
; /* auth-info-required attribute */
2242 * Load up the key value pairs...
2247 keyvalue
[i
][0] = "txtvers";
2248 keyvalue
[i
++][1] = "1";
2250 keyvalue
[i
][0] = "qtotal";
2251 keyvalue
[i
++][1] = "1";
2253 keyvalue
[i
][0] = "rp";
2254 keyvalue
[i
++][1] = rp_str
;
2256 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2258 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2259 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2261 keyvalue
[i
][0] = "ty";
2262 keyvalue
[i
++][1] = p
->make_model
;
2264 if (p
->location
&& *p
->location
!= '\0')
2266 keyvalue
[i
][0] = "note";
2267 keyvalue
[i
++][1] = p
->location
;
2270 keyvalue
[i
][0] = "priority";
2271 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2273 keyvalue
[i
][0] = "product";
2274 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2276 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2277 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2279 keyvalue
[i
][0] = "printer-state";
2280 keyvalue
[i
++][1] = state_str
;
2282 keyvalue
[i
][0] = "printer-type";
2283 keyvalue
[i
++][1] = type_str
;
2285 keyvalue
[i
][0] = "Transparent";
2286 keyvalue
[i
++][1] = "T";
2288 keyvalue
[i
][0] = "Binary";
2289 keyvalue
[i
++][1] = "T";
2291 if ((p
->type
& CUPS_PRINTER_FAX
))
2293 keyvalue
[i
][0] = "Fax";
2294 keyvalue
[i
++][1] = "T";
2297 if ((p
->type
& CUPS_PRINTER_COLOR
))
2299 keyvalue
[i
][0] = "Color";
2300 keyvalue
[i
++][1] = "T";
2303 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2305 keyvalue
[i
][0] = "Duplex";
2306 keyvalue
[i
++][1] = "T";
2309 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2311 keyvalue
[i
][0] = "Staple";
2312 keyvalue
[i
++][1] = "T";
2315 if ((p
->type
& CUPS_PRINTER_COPIES
))
2317 keyvalue
[i
][0] = "Copies";
2318 keyvalue
[i
++][1] = "T";
2321 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2323 keyvalue
[i
][0] = "Collate";
2324 keyvalue
[i
++][1] = "T";
2327 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2329 keyvalue
[i
][0] = "Punch";
2330 keyvalue
[i
++][1] = "T";
2333 if ((p
->type
& CUPS_PRINTER_BIND
))
2335 keyvalue
[i
][0] = "Bind";
2336 keyvalue
[i
++][1] = "T";
2339 if ((p
->type
& CUPS_PRINTER_SORT
))
2341 keyvalue
[i
][0] = "Sort";
2342 keyvalue
[i
++][1] = "T";
2345 keyvalue
[i
][0] = "pdl";
2346 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2348 if ((air_attr
= ippFindAttribute(p
->attrs
, "auth-info-required",
2349 IPP_TAG_KEYWORD
)) != NULL
&&
2350 strcmp(air_attr
->values
[0].string
.text
, "none"))
2352 char *air
= air_str
; /* Pointer into string */
2355 for (j
= 0; j
< air_attr
->num_values
; j
++)
2357 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2363 strlcpy(air
, air_attr
->values
[j
].string
.text
,
2364 sizeof(air_str
) - (air
- air_str
));
2368 keyvalue
[i
][0] = "air";
2369 keyvalue
[i
++][1] = air_str
;
2373 * Then pack them into a proper txt record...
2376 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2381 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2384 static int /* O - Result of comparison */
2385 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2386 cupsd_printer_t
*b
)/* I - Second printer */
2388 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2393 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2398 dnssdDeregisterPrinter(
2399 cupsd_printer_t
*p
) /* I - Printer */
2401 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2404 * Closing the socket deregisters the service
2409 DNSServiceRefDeallocate(p
->ipp_ref
);
2416 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2425 DNSServiceRefDeallocate(p
->printer_ref
);
2426 p
->printer_ref
= NULL
;
2432 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2435 free(p
->printer_txt
);
2436 p
->printer_txt
= NULL
;
2440 * Remove the printer from the array of DNS-SD printers, then clear the
2441 * registered name...
2444 cupsArrayRemove(DNSSDPrinters
, p
);
2445 cupsdClearString(&p
->reg_name
);
2450 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2451 * TXT record format.
2454 static char * /* O - TXT record */
2455 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2456 char *keyvalue
[][2], /* I - Table of key value pairs */
2457 int count
) /* I - Items in table */
2459 int i
; /* Looping var */
2460 int length
; /* Length of TXT record */
2461 int length2
; /* Length of value */
2462 char *txtRecord
; /* TXT record buffer */
2463 char *cursor
; /* Looping pointer */
2467 * Calculate the buffer size
2470 for (length
= i
= 0; i
< count
; i
++)
2471 length
+= 1 + strlen(keyvalue
[i
][0]) +
2472 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2475 * Allocate and fill it
2478 txtRecord
= malloc(length
);
2483 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2486 * Drop in the p-string style length byte followed by the data
2489 length
= strlen(keyvalue
[i
][0]);
2490 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2492 *cursor
++ = (unsigned char)(length
+ length2
);
2494 memcpy(cursor
, keyvalue
[i
][0], length
);
2501 memcpy(cursor
, keyvalue
[i
][1], length2
);
2512 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2516 dnssdRegisterCallback(
2517 DNSServiceRef sdRef
, /* I - DNS Service reference */
2518 DNSServiceFlags flags
, /* I - Reserved for future use */
2519 DNSServiceErrorType errorCode
, /* I - Error code */
2520 const char *name
, /* I - Service name */
2521 const char *regtype
, /* I - Service type */
2522 const char *domain
, /* I - Domain. ".local" for now */
2523 void *context
) /* I - User-defined context */
2525 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2526 /* Current printer */
2529 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2530 name
, regtype
, p
? p
->name
: "Web Interface",
2531 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2535 cupsdLogMessage(CUPSD_LOG_ERROR
,
2536 "DNSServiceRegister failed with error %d", (int)errorCode
);
2539 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2541 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2544 cupsArrayRemove(DNSSDPrinters
, p
);
2545 cupsdSetString(&p
->reg_name
, name
);
2546 cupsArrayAdd(DNSSDPrinters
, p
);
2548 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2554 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2555 * or update the broadcast contents.
2559 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2561 DNSServiceErrorType se
; /* dnssd errors */
2562 char *ipp_txt
, /* IPP TXT record buffer */
2563 *printer_txt
, /* LPD TXT record buffer */
2564 name
[1024], /* Service name */
2565 *nameptr
; /* Pointer into name */
2566 int ipp_len
, /* IPP TXT record length */
2567 printer_len
; /* LPD TXT record length */
2568 char resource
[1024]; /* Resource path for printer */
2569 const char *regtype
; /* Registration type */
2570 const char *domain
; /* Registration domain */
2571 cupsd_location_t
*location
, /* Printer location */
2572 *policy
; /* Operation policy for Print-Job */
2573 unsigned address
[4]; /* INADDR_ANY address */
2576 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2577 !p
->ipp_ref
? "new" : "update");
2580 * If per-printer sharing was just disabled make sure we're not
2581 * registered before returning.
2586 dnssdDeregisterPrinter(p
);
2591 * The registered name takes the form of "<printer-info> @ <computer name>"...
2594 if (p
->info
&& strlen(p
->info
) > 0)
2597 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2599 strlcpy(name
, p
->info
, sizeof(name
));
2602 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2604 strlcpy(name
, p
->name
, sizeof(name
));
2607 * If an existing printer was renamed, unregister it and start over...
2610 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2611 dnssdDeregisterPrinter(p
);
2615 cupsdSetString(&p
->reg_name
, name
);
2616 cupsArrayAdd(DNSSDPrinters
, p
);
2620 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2621 * let dnssd decide on the domain, otherwise restrict it to ".local".
2624 if (p
->type
& CUPS_PRINTER_CLASS
)
2625 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2627 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2629 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2630 location
= cupsdFindBest(resource
, HTTP_POST
);
2631 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2633 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2634 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2640 * Register IPP and (optionally) LPD...
2643 ipp_len
= 0; /* anti-compiler-warning-code */
2644 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2647 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2650 * Update the existing registration...
2653 /* A TTL of 0 means use record's original value (Radar 3176248) */
2654 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2655 0)) == kDNSServiceErr_NoError
)
2660 p
->ipp_txt
= ipp_txt
;
2661 p
->ipp_len
= ipp_len
;
2667 * Failed to update record, lets close this reference and move on...
2670 cupsdLogMessage(CUPSD_LOG_ERROR
,
2671 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2674 DNSServiceRefDeallocate(p
->ipp_ref
);
2682 * Initial registration. Use the _fax subtype for fax queues...
2685 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2688 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2689 "Registering DNS-SD printer %s with name \"%s\", "
2690 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2691 domain
? domain
: "(null)");
2694 * Register the queue, dropping characters as needed until we succeed...
2697 nameptr
= name
+ strlen(name
);
2701 p
->ipp_ref
= DNSSDRef
;
2702 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2703 0, name
, regtype
, domain
, NULL
,
2704 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2705 dnssdRegisterCallback
,
2706 p
)) == kDNSServiceErr_BadParam
)
2709 * Name is too long, drop trailing characters, taking into account
2715 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2722 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2724 if (se
== kDNSServiceErr_NoError
)
2726 p
->ipp_txt
= ipp_txt
;
2727 p
->ipp_len
= ipp_len
;
2731 cupsdLogMessage(CUPSD_LOG_WARN
,
2732 "DNS-SD IPP registration of \"%s\" failed: %d",
2739 if (BrowseLocalProtocols
& BROWSE_LPD
)
2741 printer_len
= 0; /* anti-compiler-warning-code */
2742 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2744 if (p
->printer_ref
&&
2745 (printer_len
!= p
->printer_len
||
2746 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2749 * Update the existing registration...
2752 /* A TTL of 0 means use record's original value (Radar 3176248) */
2753 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2755 0)) == kDNSServiceErr_NoError
)
2758 free(p
->printer_txt
);
2760 p
->printer_txt
= printer_txt
;
2761 p
->printer_len
= printer_len
;
2767 * Failed to update record, lets close this reference and move on...
2770 cupsdLogMessage(CUPSD_LOG_ERROR
,
2771 "Unable to update LPD DNS-SD record for %s - %d",
2774 DNSServiceRefDeallocate(p
->printer_ref
);
2775 p
->printer_ref
= NULL
;
2779 if (!p
->printer_ref
)
2782 * Initial registration...
2785 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2786 "Registering DNS-SD printer %s with name \"%s\", "
2787 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2788 name
, domain
? domain
: "(null)");
2790 p
->printer_ref
= DNSSDRef
;
2791 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2792 kDNSServiceFlagsShareConnection
,
2793 0, name
, "_printer._tcp", domain
, NULL
,
2794 htons(515), printer_len
, printer_txt
,
2795 dnssdRegisterCallback
,
2796 p
)) == kDNSServiceErr_NoError
)
2798 p
->printer_txt
= printer_txt
;
2799 p
->printer_len
= printer_len
;
2803 cupsdLogMessage(CUPSD_LOG_WARN
,
2804 "DNS-SD LPD registration of \"%s\" failed: %d",
2815 * 'dnssdUpdate()' - Handle DNS-SD queries.
2821 DNSServiceErrorType sdErr
; /* Service discovery error */
2824 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2825 cupsdLogMessage(CUPSD_LOG_ERROR
,
2826 "DNS Service Discovery registration error %d!",
2829 #endif /* HAVE_DNSSD */
2834 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2837 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2838 get_hostconfig(const char *name
) /* I - Name of service */
2840 cups_file_t
*fp
; /* Hostconfig file */
2841 char line
[1024], /* Line from file */
2842 *ptr
; /* Pointer to value */
2843 int state
= 1; /* State of service */
2847 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2848 * the service is enabled/auto.
2851 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2854 * Read lines from the file until we find the service...
2857 while (cupsFileGets(fp
, line
, sizeof(line
)))
2859 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2864 if (!strcasecmp(line
, name
))
2867 * Found the service, see if it is set to "-NO-"...
2870 if (!strncasecmp(ptr
, "-NO-", 4))
2881 #endif /* __APPLE__ */
2885 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2888 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2889 is_local_queue(const char *uri
, /* I - Printer URI */
2890 char *host
, /* O - Host string */
2891 int hostlen
, /* I - Length of host buffer */
2892 char *resource
, /* O - Resource string */
2893 int resourcelen
) /* I - Length of resource buffer */
2895 char scheme
[32], /* Scheme portion of URI */
2896 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2897 int port
; /* Port portion of URI */
2898 cupsd_netif_t
*iface
; /* Network interface */
2902 * Pull the URI apart to see if this is a local or remote printer...
2905 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2906 username
, sizeof(username
), host
, hostlen
, &port
,
2907 resource
, resourcelen
) < HTTP_URI_OK
)
2910 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2913 * Check for local server addresses...
2916 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2921 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2923 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2924 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2928 * If we get here, the printer is remote...
2936 * 'process_browse_data()' - Process new browse data.
2940 process_browse_data(
2941 const char *uri
, /* I - URI of printer/class */
2942 const char *host
, /* I - Hostname */
2943 const char *resource
, /* I - Resource path */
2944 cups_ptype_t type
, /* I - Printer type */
2945 ipp_pstate_t state
, /* I - Printer state */
2946 const char *location
, /* I - Printer location */
2947 const char *info
, /* I - Printer information */
2948 const char *make_model
, /* I - Printer make and model */
2949 int num_attrs
, /* I - Number of attributes */
2950 cups_option_t
*attrs
) /* I - Attributes */
2952 int i
; /* Looping var */
2953 int update
; /* Update printer attributes? */
2954 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2955 name
[IPP_MAX_NAME
], /* Name of printer */
2956 newname
[IPP_MAX_NAME
], /* New name of printer */
2957 *hptr
, /* Pointer into hostname */
2958 *sptr
; /* Pointer into ServerName */
2959 const char *shortname
; /* Short queue name (queue) */
2960 char local_make_model
[IPP_MAX_NAME
];
2961 /* Local make and model */
2962 cupsd_printer_t
*p
; /* Printer information */
2963 const char *ipp_options
, /* ipp-options value */
2964 *lease_duration
; /* lease-duration value */
2965 int is_class
; /* Is this queue a class? */
2968 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2969 "process_browse_data(uri=\"%s\", host=\"%s\", "
2970 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2971 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2972 uri
, host
, resource
, type
, state
,
2973 location
? location
: "(nil)", info
? info
: "(nil)",
2974 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2977 * Determine if the URI contains any illegal characters in it...
2980 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2981 (strncmp(resource
, "/printers/", 10) &&
2982 strncmp(resource
, "/classes/", 9)))
2984 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2988 if (strchr(resource
, '?') ||
2989 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2990 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2992 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2998 * OK, this isn't a local printer; add any remote options...
3001 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3003 if (BrowseRemoteOptions
)
3005 if (BrowseRemoteOptions
[0] == '?')
3008 * Override server-supplied options...
3011 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3013 else if (ipp_options
)
3016 * Combine the server and local options...
3019 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3020 BrowseRemoteOptions
);
3025 * Just use the local options...
3028 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3033 else if (ipp_options
)
3036 * Just use the server-supplied options...
3039 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3044 * See if we already have it listed in the Printers list, and add it if not...
3047 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3048 type
&= ~CUPS_PRINTER_IMPLICIT
;
3050 hptr
= strchr(host
, '.');
3051 sptr
= strchr(ServerName
, '.');
3052 is_class
= type
& CUPS_PRINTER_CLASS
;
3054 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3057 * Strip the common domain name components...
3060 while (hptr
!= NULL
)
3062 if (!strcasecmp(hptr
, sptr
))
3068 hptr
= strchr(hptr
+ 1, '.');
3075 * Remote destination is a class...
3078 if (!strncmp(resource
, "/classes/", 9))
3079 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3083 shortname
= resource
+ 9;
3088 * Remote destination is a printer...
3091 if (!strncmp(resource
, "/printers/", 10))
3092 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3096 shortname
= resource
+ 10;
3100 *hptr
= '.'; /* Resource FQDN */
3102 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3105 * Long name doesn't exist, try short name...
3108 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3111 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3114 * Short name doesn't exist, use it for this shared queue.
3117 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3119 strlcpy(name
, shortname
, sizeof(name
));
3124 * Short name exists...
3127 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3128 "process_browse_data: %s found, type=%x, hostname=%s...",
3129 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3131 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3132 p
= NULL
; /* Don't replace implicit classes */
3133 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3136 * Short name exists but is for a different host. If this is a remote
3137 * queue, rename it and use the long name...
3140 if (p
->type
& CUPS_PRINTER_REMOTE
)
3142 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3143 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3144 is_class
? "class" : "printer", p
->name
, p
->name
,
3146 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3147 "%s \'%s\' deleted by directory services.",
3148 is_class
? "Class" : "Printer", p
->name
);
3150 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3151 cupsdRenamePrinter(p
, newname
);
3153 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3154 "%s \'%s\' added by directory services.",
3155 is_class
? "Class" : "Printer", p
->name
);
3159 * Force creation with long name...
3167 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3168 "process_browse_data: %s found, type=%x, hostname=%s...",
3169 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3174 * Queue doesn't exist; add it...
3178 p
= cupsdAddClass(name
);
3180 p
= cupsdAddPrinter(name
);
3185 cupsdClearString(&(p
->hostname
));
3187 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3188 is_class
? "class" : "printer", name
);
3190 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3191 "%s \'%s\' added by directory services.",
3192 is_class
? "Class" : "Printer", name
);
3195 * Force the URI to point to the real server...
3198 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3201 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3207 * Hostname not set, so this must be a cached remote printer
3208 * that was created for a pending print job...
3211 cupsdSetString(&p
->hostname
, host
);
3212 cupsdSetString(&p
->uri
, uri
);
3213 cupsdSetString(&p
->device_uri
, uri
);
3216 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3220 * Update the state...
3224 p
->browse_time
= time(NULL
);
3226 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3230 * Grab the lease-duration for the browse data; anything less then 1
3231 * second or more than 1 week gets the default BrowseTimeout...
3234 i
= atoi(lease_duration
);
3235 if (i
< 1 || i
> 604800)
3238 p
->browse_expire
= p
->browse_time
+ i
;
3241 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3243 if (type
& CUPS_PRINTER_REJECTING
)
3245 type
&= ~CUPS_PRINTER_REJECTING
;
3253 else if (!p
->accepting
)
3259 if (p
->type
!= type
)
3265 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3267 cupsdSetString(&p
->location
, location
);
3271 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3273 cupsdSetString(&p
->info
, info
);
3276 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3279 if (!make_model
|| !make_model
[0])
3282 snprintf(local_make_model
, sizeof(local_make_model
),
3283 "Remote Class on %s", host
);
3285 snprintf(local_make_model
, sizeof(local_make_model
),
3286 "Remote Printer on %s", host
);
3289 snprintf(local_make_model
, sizeof(local_make_model
),
3290 "%s on %s", make_model
, host
);
3292 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3294 cupsdSetString(&p
->make_model
, local_make_model
);
3300 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3303 * See if we need to update the attributes...
3306 if (p
->num_options
!= num_attrs
)
3310 for (i
= 0; i
< num_attrs
; i
++)
3311 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3312 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3313 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3322 * Free the old options...
3325 cupsFreeOptions(p
->num_options
, p
->options
);
3328 p
->num_options
= num_attrs
;
3331 if (type
& CUPS_PRINTER_DELETE
)
3333 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3334 "%s \'%s\' deleted by directory services.",
3335 is_class
? "Class" : "Printer", p
->name
);
3337 cupsdExpireSubscriptions(p
, NULL
);
3339 cupsdDeletePrinter(p
, 1);
3340 cupsdUpdateImplicitClasses();
3341 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3345 cupsdSetPrinterAttrs(p
);
3346 cupsdUpdateImplicitClasses();
3350 * See if we have a default printer... If not, make the first network
3351 * default printer the default.
3354 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3357 * Find the first network default printer and use it...
3360 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3362 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3363 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3366 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3372 * Do auto-classing if needed...
3375 process_implicit_classes();
3380 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3384 process_implicit_classes(void)
3386 int i
; /* Looping var */
3387 int update
; /* Update printer attributes? */
3388 char name
[IPP_MAX_NAME
], /* Name of printer */
3389 *hptr
; /* Pointer into hostname */
3390 cupsd_printer_t
*p
, /* Printer information */
3391 *pclass
, /* Printer class */
3392 *first
; /* First printer in class */
3393 int offset
, /* Offset of name */
3394 len
; /* Length of name */
3397 if (!ImplicitClasses
|| !Printers
)
3401 * Loop through all available printers and create classes as needed...
3404 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3405 update
= 0, pclass
= NULL
, first
= NULL
;
3407 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3410 * Skip implicit classes...
3413 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3420 * If len == 0, get the length of this printer name up to the "@"
3424 cupsArraySave(Printers
);
3427 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3428 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3431 * We have more than one printer with the same name; see if
3432 * we have a class, and if this printer is a member...
3435 if (pclass
&& strcasecmp(pclass
->name
, name
))
3438 cupsdSetPrinterAttrs(pclass
);
3444 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3447 * Need to add the class...
3450 pclass
= cupsdAddPrinter(name
);
3451 cupsArrayAdd(ImplicitPrinters
, pclass
);
3453 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3454 pclass
->accepting
= 1;
3455 pclass
->state
= IPP_PRINTER_IDLE
;
3457 cupsdSetString(&pclass
->location
, p
->location
);
3458 cupsdSetString(&pclass
->info
, p
->info
);
3460 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3461 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3465 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3467 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3469 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3470 "Implicit class \'%s\' added by directory services.",
3476 for (i
= 0; i
< pclass
->num_printers
; i
++)
3477 if (pclass
->printers
[i
] == first
)
3480 if (i
>= pclass
->num_printers
)
3482 first
->in_implicit_class
= 1;
3483 cupsdAddPrinterToClass(pclass
, first
);
3489 for (i
= 0; i
< pclass
->num_printers
; i
++)
3490 if (pclass
->printers
[i
] == p
)
3493 if (i
>= pclass
->num_printers
)
3495 p
->in_implicit_class
= 1;
3496 cupsdAddPrinterToClass(pclass
, p
);
3503 * First time around; just get name length and mark it as first
3507 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3508 len
= hptr
- p
->name
;
3510 len
= strlen(p
->name
);
3512 strncpy(name
, p
->name
, len
);
3516 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3517 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3520 * Can't use same name as a local printer; add "Any" to the
3521 * front of the name, unless we have explicitly disabled
3522 * the "ImplicitAnyClasses"...
3525 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3528 * Add "Any" to the class name...
3531 strcpy(name
, "Any");
3532 strncpy(name
+ 3, p
->name
, len
);
3533 name
[len
+ 3] = '\0';
3539 * Don't create an implicit class if we have a local printer
3540 * with the same name...
3544 cupsArrayRestore(Printers
);
3552 cupsArrayRestore(Printers
);
3556 * Update the last printer class as needed...
3559 if (pclass
&& update
)
3560 cupsdSetPrinterAttrs(pclass
);
3565 * 'send_cups_browse()' - Send new browsing information using the CUPS
3570 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3572 int i
; /* Looping var */
3573 cups_ptype_t type
; /* Printer type */
3574 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3575 int bytes
; /* Length of packet */
3576 char packet
[1453], /* Browse data packet */
3577 uri
[1024], /* Printer URI */
3578 location
[1024], /* printer-location */
3579 info
[1024], /* printer-info */
3581 /* printer-make-and-model */
3582 cupsd_netif_t
*iface
; /* Network interface */
3586 * Figure out the printer type value...
3589 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3592 type
|= CUPS_PRINTER_REJECTING
;
3594 if (p
== DefaultPrinter
)
3595 type
|= CUPS_PRINTER_DEFAULT
;
3598 * Remove quotes from printer-info, printer-location, and
3599 * printer-make-and-model attributes...
3602 dequote(location
, p
->location
, sizeof(location
));
3603 dequote(info
, p
->info
, sizeof(info
));
3606 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3607 else if (p
->type
& CUPS_PRINTER_CLASS
)
3609 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3610 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3612 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3615 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3617 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3620 * Send a packet to each browse address...
3623 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3627 * Send the browse packet to one or more interfaces...
3630 if (!strcmp(b
->iface
, "*"))
3633 * Send to all local interfaces...
3638 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3640 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3643 * Only send to local, IPv4 interfaces...
3646 if (!iface
->is_local
|| !iface
->port
||
3647 iface
->address
.addr
.sa_family
!= AF_INET
)
3650 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3651 iface
->hostname
, iface
->port
,
3652 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3655 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3656 type
, p
->state
, uri
, location
, info
, make_model
,
3657 p
->browse_attrs
? p
->browse_attrs
: "");
3659 bytes
= strlen(packet
);
3661 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3662 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3663 iface
->name
, packet
);
3665 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3667 sendto(BrowseSocket
, packet
, bytes
, 0,
3668 (struct sockaddr
*)&(iface
->broadcast
),
3669 httpAddrLength(&(iface
->broadcast
)));
3672 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3675 * Send to the named interface using the IPv4 address...
3679 if (strcmp(b
->iface
, iface
->name
))
3684 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3687 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3691 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3692 iface
->hostname
, iface
->port
,
3693 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3696 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3697 type
, p
->state
, uri
, location
, info
, make_model
,
3698 p
->browse_attrs
? p
->browse_attrs
: "");
3700 bytes
= strlen(packet
);
3702 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3703 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3704 iface
->name
, packet
);
3706 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3708 sendto(BrowseSocket
, packet
, bytes
, 0,
3709 (struct sockaddr
*)&(iface
->broadcast
),
3710 httpAddrLength(&(iface
->broadcast
)));
3717 * Send the browse packet to the indicated address using
3718 * the default server name...
3721 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3722 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3723 p
->browse_attrs
? p
->browse_attrs
: "");
3725 bytes
= strlen(packet
);
3726 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3727 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3729 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3730 (struct sockaddr
*)&(b
->to
),
3731 httpAddrLength(&(b
->to
))) <= 0)
3734 * Unable to send browse packet, so remove this address from the
3738 cupsdLogMessage(CUPSD_LOG_ERROR
,
3739 "cupsdSendBrowseList: sendto failed for browser "
3741 (int)(b
- Browsers
+ 1), strerror(errno
));
3744 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3755 * 'ldap_search_rec()' - LDAP Search with reconnect
3759 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3760 char *base
, /* I - Base dn */
3761 int scope
, /* I - LDAP search scope */
3762 char *filter
, /* I - Filter string */
3763 char *attrs
[], /* I - Requested attributes */
3764 int attrsonly
, /* I - Return only attributes? */
3765 LDAPMessage
**res
) /* I - LDAP handler */
3767 int rc
; /* Return code */
3770 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3771 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3772 NULL
, LDAP_NO_LIMIT
, res
);
3774 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3775 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3778 * If we have a connection problem try again...
3781 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3783 cupsdLogMessage(CUPSD_LOG_ERROR
,
3784 "LDAP search failed with status %d: %s",
3785 rc
, ldap_err2string(rc
));
3786 cupsdLogMessage(CUPSD_LOG_INFO
,
3787 "We try the LDAP search once again after reconnecting to "
3792 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3793 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3794 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3796 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3797 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3800 if (rc
== LDAP_NO_SUCH_OBJECT
)
3801 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3802 "ldap_search_rec: LDAP entry/object not found");
3803 else if (rc
!= LDAP_SUCCESS
)
3804 cupsdLogMessage(CUPSD_LOG_ERROR
,
3805 "ldap_search_rec: LDAP search failed with status %d: %s",
3806 rc
, ldap_err2string(rc
));
3808 if (rc
!= LDAP_SUCCESS
)
3816 * 'ldap_freeres()' - Free LDAPMessage
3820 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3822 int rc
; /* Return value */
3825 rc
= ldap_msgfree(entry
);
3827 cupsdLogMessage(CUPSD_LOG_WARN
,
3828 "Can't free LDAPMessage!");
3830 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3831 "Freeing LDAPMessage was unnecessary");
3836 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3840 ldap_getval_firststring(
3841 LDAP
*ld
, /* I - LDAP handler */
3842 LDAPMessage
*entry
, /* I - LDAP message or search result */
3843 char *attr
, /* I - the wanted attribute */
3844 char *retval
, /* O - String to return */
3845 unsigned long maxsize
) /* I - Max string size */
3847 char *dn
; /* LDAP DN */
3848 int rc
= 0; /* Return code */
3849 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3850 struct berval
**bval
; /* LDAP value array */
3851 unsigned long size
; /* String size */
3855 * Get value from LDAPMessage...
3858 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3861 dn
= ldap_get_dn(ld
, entry
);
3862 cupsdLogMessage(CUPSD_LOG_WARN
,
3863 "Failed to get LDAP value %s for %s!",
3871 * Check size and copy value into our string...
3875 if (size
< bval
[0]->bv_len
)
3878 dn
= ldap_get_dn(ld
, entry
);
3879 cupsdLogMessage(CUPSD_LOG_WARN
,
3880 "Attribute %s is too big! (dn: %s)",
3885 size
= bval
[0]->bv_len
;
3887 strlcpy(retval
, bval
[0]->bv_val
, size
);
3888 ldap_value_free_len(bval
);
3891 char **value
; /* LDAP value */
3894 * Get value from LDAPMessage...
3897 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3900 dn
= ldap_get_dn(ld
, entry
);
3901 cupsdLogMessage(CUPSD_LOG_WARN
,
3902 "Failed to get LDAP value %s for %s!",
3908 strlcpy(retval
, *value
, maxsize
);
3909 ldap_value_free(value
);
3911 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3918 * 'send_ldap_ou()' - Send LDAP ou registrations.
3922 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3923 char *basedn
, /* I - Our base dn */
3924 char *descstring
) /* I - Description for ou */
3926 int i
; /* Looping var... */
3927 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3928 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3929 LDAPMessage
*res
, /* Search result token */
3930 *e
; /* Current entry from search */
3931 int rc
; /* LDAP status */
3932 char dn
[1024], /* DN of the organizational unit we are adding */
3933 *desc
[2], /* Change records */
3935 char old_desc
[1024]; /* Old description */
3936 static const char * const objectClass_values
[] =
3937 { /* The 2 objectClass's we use in */
3938 "top", /* our LDAP entries */
3939 "organizationalUnit",
3942 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3949 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3952 * Reconnect if LDAP Handle is invalid...
3955 if (! BrowseLDAPHandle
)
3957 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3958 "send_ldap_ou: LDAP Handle is invalid. Try "
3965 * Prepare ldap search...
3968 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3969 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3973 desc
[0] = descstring
;
3976 mods
[0].mod_type
= "ou";
3977 mods
[0].mod_values
= ou_value
;
3978 mods
[1].mod_type
= "description";
3979 mods
[1].mod_values
= desc
;
3980 mods
[2].mod_type
= "objectClass";
3981 mods
[2].mod_values
= (char **)objectClass_values
;
3983 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3984 (char **)ou_attrs
, 0, &res
);
3987 * If ldap search was not successfull then exit function...
3990 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3994 * Check if we need to insert or update the LDAP entry...
3997 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3998 rc
!= LDAP_NO_SUCH_OBJECT
)
4001 * Printserver has already been registered, check if
4002 * modification is required...
4005 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4008 * Get the required values from this entry...
4011 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4012 sizeof(old_desc
)) == -1)
4016 * Check if modification is required...
4019 if ( strcmp(desc
[0], old_desc
) == 0 )
4022 * LDAP entry for the printer exists.
4023 * Printer has already been registered,
4024 * no modifications required...
4026 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4027 "send_ldap_ou: No updates required for %s", ou
);
4032 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4033 "send_ldap_ou: Replace entry for %s", ou
);
4035 for (i
= 0; i
< 3; i
++)
4037 pmods
[i
] = mods
+ i
;
4038 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4042 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4043 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4044 NULL
)) != LDAP_SUCCESS
)
4046 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4047 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4049 cupsdLogMessage(CUPSD_LOG_ERROR
,
4050 "LDAP modify for %s failed with status %d: %s",
4051 ou
, rc
, ldap_err2string(rc
));
4052 if ( LDAP_SERVER_DOWN
== rc
)
4060 * Printserver has never been registered,
4061 * add registration...
4064 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4065 "send_ldap_ou: Add entry for %s", ou
);
4067 for (i
= 0; i
< 3; i
++)
4069 pmods
[i
] = mods
+ i
;
4070 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4074 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4075 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4076 NULL
)) != LDAP_SUCCESS
)
4078 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4079 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4081 cupsdLogMessage(CUPSD_LOG_ERROR
,
4082 "LDAP add for %s failed with status %d: %s",
4083 ou
, rc
, ldap_err2string(rc
));
4084 if ( LDAP_SERVER_DOWN
== rc
)
4094 * 'send_ldap_browse()' - Send LDAP printer registrations.
4098 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4100 int i
; /* Looping var... */
4101 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4102 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4103 LDAPMessage
*res
, /* Search result token */
4104 *e
; /* Current entry from search */
4105 char *cn_value
[2], /* Change records */
4111 typestring
[255], /* String to hold printer-type */
4112 dn
[1024]; /* DN of the printer we are adding */
4113 int rc
; /* LDAP status */
4114 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4115 old_location
[1024], /* Printer location */
4116 old_info
[1024], /* Printer information */
4117 old_make_model
[1024], /* Printer make and model */
4118 old_type_string
[30]; /* Temporary type number */
4119 int old_type
; /* Printer type */
4120 static const char * const objectClass_values
[] =
4121 { /* The 3 objectClass's we use in */
4122 "top", /* our LDAP entries */
4129 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4132 * Exit function if LDAP updates has been disabled...
4135 if (!BrowseLDAPUpdate
)
4137 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4138 "send_ldap_browse: Updates temporary disabled; "
4144 * Reconnect if LDAP Handle is invalid...
4147 if (!BrowseLDAPHandle
)
4149 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4150 "send_ldap_browse: LDAP Handle is invalid. Try "
4157 * Everything in ldap is ** so we fudge around it...
4160 sprintf(typestring
, "%u", p
->type
);
4162 cn_value
[0] = p
->name
;
4164 info
[0] = p
->info
? p
->info
: "Unknown";
4166 location
[0] = p
->location
? p
->location
: "Unknown";
4168 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4169 make_model
[1] = NULL
;
4170 type
[0] = typestring
;
4176 * Get ldap entry for printer ...
4179 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4181 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4183 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4184 (char **)ldap_attrs
, 0, &res
);
4187 * If ldap search was not successfull then exit function
4188 * and temporary disable LDAP updates...
4191 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4193 if (BrowseLDAPUpdate
&&
4194 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4196 BrowseLDAPUpdate
= FALSE
;
4197 cupsdLogMessage(CUPSD_LOG_INFO
,
4198 "LDAP update temporary disabled");
4205 * Fill modification array...
4208 mods
[0].mod_type
= "cn";
4209 mods
[0].mod_values
= cn_value
;
4210 mods
[1].mod_type
= "printerDescription";
4211 mods
[1].mod_values
= info
;
4212 mods
[2].mod_type
= "printerURI";
4213 mods
[2].mod_values
= uri
;
4214 mods
[3].mod_type
= "printerLocation";
4215 mods
[3].mod_values
= location
;
4216 mods
[4].mod_type
= "printerMakeAndModel";
4217 mods
[4].mod_values
= make_model
;
4218 mods
[5].mod_type
= "printerType";
4219 mods
[5].mod_values
= type
;
4220 mods
[6].mod_type
= "objectClass";
4221 mods
[6].mod_values
= (char **)objectClass_values
;
4224 * Check if we need to insert or update the LDAP entry...
4227 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4228 rc
!= LDAP_NO_SUCH_OBJECT
)
4231 * Printer has already been registered, check if
4232 * modification is required...
4235 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4238 * Get the required values from this entry...
4241 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4242 old_info
, sizeof(old_info
)) == -1)
4245 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4246 old_location
, sizeof(old_location
)) == -1)
4249 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4250 old_make_model
, sizeof(old_make_model
)) == -1)
4253 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4254 old_type_string
, sizeof(old_type_string
)) == -1)
4257 old_type
= atoi(old_type_string
);
4259 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4260 sizeof(old_uri
)) == -1)
4264 * Check if modification is required...
4267 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4268 !strcmp(location
[0], old_location
) &&
4269 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4272 * LDAP entry for the printer exists. Printer has already been registered,
4273 * no modifications required...
4276 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4277 "send_ldap_browse: No updates required for %s", p
->name
);
4282 * LDAP entry for the printer exists. Printer has already been registered,
4283 * modify the current registration...
4286 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4287 "send_ldap_browse: Replace entry for %s", p
->name
);
4289 for (i
= 0; i
< 7; i
++)
4291 pmods
[i
] = mods
+ i
;
4292 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4296 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4297 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4298 NULL
)) != LDAP_SUCCESS
)
4300 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4301 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4303 cupsdLogMessage(CUPSD_LOG_ERROR
,
4304 "LDAP modify for %s failed with status %d: %s",
4305 p
->name
, rc
, ldap_err2string(rc
));
4306 if (rc
== LDAP_SERVER_DOWN
)
4314 * No LDAP entry exists for the printer. Printer has never been registered,
4315 * add the current registration...
4318 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4320 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4321 "send_ldap_browse: Add entry for %s", p
->name
);
4323 for (i
= 0; i
< 7; i
++)
4325 pmods
[i
] = mods
+ i
;
4326 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4330 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4331 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4332 NULL
)) != LDAP_SUCCESS
)
4334 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4335 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4337 cupsdLogMessage(CUPSD_LOG_ERROR
,
4338 "LDAP add for %s failed with status %d: %s",
4339 p
->name
, rc
, ldap_err2string(rc
));
4340 if (rc
== LDAP_SERVER_DOWN
)
4350 * 'ldap_dereg_printer()' - Delete printer from directory
4354 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4356 char dn
[1024]; /* DN of the printer */
4357 int rc
; /* LDAP status */
4360 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4364 * Reconnect if LDAP Handle is invalid...
4367 if (!BrowseLDAPHandle
)
4374 * Get dn for printer and delete LDAP entry...
4377 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4379 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4381 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4382 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4383 NULL
)) != LDAP_SUCCESS
)
4385 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4386 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4388 cupsdLogMessage(CUPSD_LOG_WARN
,
4389 "LDAP delete for %s failed with status %d: %s",
4390 p
->name
, rc
, ldap_err2string(rc
));
4393 * If we had a connection problem (connection timed out, etc.)
4394 * we should reconnect and try again to delete the entry...
4397 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4399 cupsdLogMessage(CUPSD_LOG_INFO
,
4400 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4403 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4404 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4405 NULL
)) != LDAP_SUCCESS
)
4407 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4408 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4409 cupsdLogMessage(CUPSD_LOG_WARN
,
4410 "LDAP delete for %s failed with status %d: %s",
4411 p
->name
, rc
, ldap_err2string(rc
));
4418 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4419 char *basedn
) /* I - Dase dn */
4421 char dn
[1024]; /* DN of the printer */
4422 int rc
; /* LDAP status */
4425 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4428 * Reconnect if LDAP Handle is invalid...
4431 if (!BrowseLDAPHandle
)
4438 * Get dn for printer and delete LDAP entry...
4441 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4442 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4444 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4445 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4446 NULL
)) != LDAP_SUCCESS
)
4448 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4449 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4451 cupsdLogMessage(CUPSD_LOG_WARN
,
4452 "LDAP delete for %s failed with status %d: %s",
4453 ou
, rc
, ldap_err2string(rc
));
4456 * If we had a connection problem (connection timed out, etc.)
4457 * we should reconnect and try again to delete the entry...
4460 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4462 cupsdLogMessage(CUPSD_LOG_INFO
,
4463 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4465 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4466 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4467 NULL
)) != LDAP_SUCCESS
)
4469 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4470 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4471 cupsdLogMessage(CUPSD_LOG_WARN
,
4472 "LDAP delete for %s failed with status %d: %s",
4473 ou
, rc
, ldap_err2string(rc
));
4478 #endif /* HAVE_LDAP */
4483 * 'send_slp_browse()' - Register the specified printer with SLP.
4487 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4489 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4490 attrs
[8192], /* Printer attributes */
4491 finishings
[1024], /* Finishings to support */
4492 make_model
[IPP_MAX_NAME
* 2],
4493 /* Make and model, quoted */
4494 location
[IPP_MAX_NAME
* 2],
4495 /* Location, quoted */
4496 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4497 *src
, /* Pointer to original string */
4498 *dst
; /* Pointer to destination string */
4499 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4500 SLPError error
; /* SLP error, if any */
4503 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4507 * Make the SLP service URL that conforms to the IANA
4508 * 'printer:' template.
4511 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4513 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4516 * Figure out the finishings string...
4519 if (p
->type
& CUPS_PRINTER_STAPLE
)
4520 strcpy(finishings
, "staple");
4522 finishings
[0] = '\0';
4524 if (p
->type
& CUPS_PRINTER_BIND
)
4527 strlcat(finishings
, ",bind", sizeof(finishings
));
4529 strcpy(finishings
, "bind");
4532 if (p
->type
& CUPS_PRINTER_PUNCH
)
4535 strlcat(finishings
, ",punch", sizeof(finishings
));
4537 strcpy(finishings
, "punch");
4540 if (p
->type
& CUPS_PRINTER_COVER
)
4543 strlcat(finishings
, ",cover", sizeof(finishings
));
4545 strcpy(finishings
, "cover");
4548 if (p
->type
& CUPS_PRINTER_SORT
)
4551 strlcat(finishings
, ",sort", sizeof(finishings
));
4553 strcpy(finishings
, "sort");
4557 strcpy(finishings
, "none");
4560 * Quote any commas in the make and model, location, and info strings...
4563 for (src
= p
->make_model
, dst
= make_model
;
4564 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4566 if (*src
== ',' || *src
== '\\' || *src
== ')')
4575 strcpy(make_model
, "Unknown");
4577 for (src
= p
->location
, dst
= location
;
4578 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4580 if (*src
== ',' || *src
== '\\' || *src
== ')')
4589 strcpy(location
, "Unknown");
4591 for (src
= p
->info
, dst
= info
;
4592 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4594 if (*src
== ',' || *src
== '\\' || *src
== ')')
4603 strcpy(info
, "Unknown");
4606 * Get the authentication value...
4609 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4613 * Make the SLP attribute string list that conforms to
4614 * the IANA 'printer:' template.
4617 snprintf(attrs
, sizeof(attrs
),
4618 "(printer-uri-supported=%s),"
4619 "(uri-authentication-supported=%s>),"
4621 "(uri-security-supported=tls>),"
4623 "(uri-security-supported=none>),"
4624 #endif /* HAVE_SSL */
4625 "(printer-name=%s),"
4626 "(printer-location=%s),"
4627 "(printer-info=%s),"
4628 "(printer-more-info=%s),"
4629 "(printer-make-and-model=%s),"
4630 "(printer-type=%d),"
4631 "(charset-supported=utf-8),"
4632 "(natural-language-configured=%s),"
4633 "(natural-language-supported=de,en,es,fr,it),"
4634 "(color-supported=%s),"
4635 "(finishings-supported=%s),"
4636 "(sides-supported=one-sided%s),"
4637 "(multiple-document-jobs-supported=true)"
4638 "(ipp-versions-supported=1.0,1.1)",
4639 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4640 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4641 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4643 p
->type
& CUPS_PRINTER_DUPLEX
?
4644 ",two-sided-long-edge,two-sided-short-edge" : "");
4646 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4649 * Register the printer with the SLP server...
4652 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4653 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4655 if (error
!= SLP_OK
)
4656 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4662 * 'slp_attr_callback()' - SLP attribute callback
4665 static SLPBoolean
/* O - SLP_TRUE for success */
4667 SLPHandle hslp
, /* I - SLP handle */
4668 const char *attrlist
, /* I - Attribute list */
4669 SLPError errcode
, /* I - Parsing status for this attr */
4670 void *cookie
) /* I - Current printer */
4672 char *tmp
= 0; /* Temporary string */
4673 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4674 /* Current printer */
4677 (void)hslp
; /* anti-compiler-warning-code */
4680 * Bail if there was an error
4683 if (errcode
!= SLP_OK
)
4687 * Parse the attrlist to obtain things needed to build CUPS browse packet
4690 memset(p
, 0, sizeof(cupsd_printer_t
));
4692 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4694 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4696 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4698 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4699 p
->type
= atoi(tmp
);
4701 p
->type
= CUPS_PRINTER_REMOTE
;
4703 cupsdClearString(&tmp
);
4710 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4714 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4716 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4719 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4721 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4724 * Make the SLP service URL that conforms to the IANA
4725 * 'printer:' template.
4728 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4731 * Deregister the printer...
4734 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4740 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4743 static int /* O - 0 on success */
4744 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4745 const char *tag
, /* I - Name of attribute */
4746 char **valbuf
) /* O - Value */
4748 char *ptr1
, /* Pointer into string */
4752 cupsdClearString(valbuf
);
4754 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4756 ptr1
+= strlen(tag
);
4758 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4764 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4765 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4768 * Dequote the value...
4771 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4772 if (*ptr1
== '\\' && ptr1
[1])
4773 _cups_strcpy(ptr1
, ptr1
+ 1);
4784 * 'slp_reg_callback()' - Empty SLPRegReport.
4788 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4789 SLPError errcode
, /* I - Error code, if any */
4790 void *cookie
) /* I - App data */
4801 * 'slp_url_callback()' - SLP service url callback
4804 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4806 SLPHandle hslp
, /* I - SLP handle */
4807 const char *srvurl
, /* I - URL of service */
4808 unsigned short lifetime
, /* I - Life of service */
4809 SLPError errcode
, /* I - Existing error code */
4810 void *cookie
) /* I - Pointer to service list */
4812 slpsrvurl_t
*s
, /* New service entry */
4813 **head
; /* Pointer to head of entry */
4817 * Let the compiler know we won't be using these vars...
4824 * Bail if there was an error
4827 if (errcode
!= SLP_OK
)
4831 * Grab the head of the list...
4834 head
= (slpsrvurl_t
**)cookie
;
4837 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4840 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4844 * Copy the SLP service URL...
4847 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4850 * Link the SLP service URL into the head of the list
4860 #endif /* HAVE_LIBSLP */
4864 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4868 update_cups_browse(void)
4870 int i
; /* Looping var */
4871 int auth
; /* Authorization status */
4872 int len
; /* Length of name string */
4873 int bytes
; /* Number of bytes left */
4874 char packet
[1541], /* Broadcast packet */
4875 *pptr
; /* Pointer into packet */
4876 socklen_t srclen
; /* Length of source address */
4877 http_addr_t srcaddr
; /* Source address */
4878 char srcname
[1024]; /* Source hostname */
4879 unsigned address
[4]; /* Source address */
4880 unsigned type
; /* Printer type */
4881 unsigned state
; /* Printer state */
4882 char uri
[HTTP_MAX_URI
], /* Printer URI */
4883 host
[HTTP_MAX_URI
], /* Host portion of URI */
4884 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4885 info
[IPP_MAX_NAME
], /* Information string */
4886 location
[IPP_MAX_NAME
], /* Location string */
4887 make_model
[IPP_MAX_NAME
];/* Make and model string */
4888 int num_attrs
; /* Number of attributes */
4889 cups_option_t
*attrs
; /* Attributes */
4893 * Read a packet from the browse socket...
4896 srclen
= sizeof(srcaddr
);
4897 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4898 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4901 * "Connection refused" is returned under Linux if the destination port
4902 * or address is unreachable from a previous sendto(); check for the
4903 * error here and ignore it for now...
4906 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4908 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4910 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4913 closesocket(BrowseSocket
);
4915 close(BrowseSocket
);
4918 cupsdRemoveSelect(BrowseSocket
);
4921 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4922 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4928 packet
[bytes
] = '\0';
4931 * If we're about to sleep, ignore incoming browse packets.
4938 * Figure out where it came from...
4942 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4944 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4945 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4946 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4947 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4950 #endif /* AF_INET6 */
4955 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4958 if (HostNameLookups
)
4959 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4961 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4963 len
= strlen(srcname
);
4971 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4974 * Access from localhost (127.0.0.1) is always allowed...
4977 auth
= CUPSD_AUTH_ALLOW
;
4982 * Do authorization checks on the domain/address...
4985 switch (BrowseACL
->order_type
)
4988 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4991 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4992 auth
= CUPSD_AUTH_ALLOW
;
4994 if (cupsdCheckAuth(address
, srcname
, len
,
4995 BrowseACL
->num_deny
, BrowseACL
->deny
))
4996 auth
= CUPSD_AUTH_DENY
;
4998 if (cupsdCheckAuth(address
, srcname
, len
,
4999 BrowseACL
->num_allow
, BrowseACL
->allow
))
5000 auth
= CUPSD_AUTH_ALLOW
;
5003 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5004 auth
= CUPSD_AUTH_DENY
;
5006 if (cupsdCheckAuth(address
, srcname
, len
,
5007 BrowseACL
->num_allow
, BrowseACL
->allow
))
5008 auth
= CUPSD_AUTH_ALLOW
;
5010 if (cupsdCheckAuth(address
, srcname
, len
,
5011 BrowseACL
->num_deny
, BrowseACL
->deny
))
5012 auth
= CUPSD_AUTH_DENY
;
5018 auth
= CUPSD_AUTH_ALLOW
;
5020 if (auth
== CUPSD_AUTH_DENY
)
5022 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5023 "update_cups_browse: Refused %d bytes from %s", bytes
,
5028 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5029 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5036 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5038 cupsdLogMessage(CUPSD_LOG_WARN
,
5039 "update_cups_browse: Garbled browse packet - %s", packet
);
5043 strcpy(location
, "Location Unknown");
5044 strcpy(info
, "No Information Available");
5045 make_model
[0] = '\0';
5049 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5052 * Have extended information; can't use sscanf for it because not all
5053 * sscanf's allow empty strings with %[^\"]...
5056 for (i
= 0, pptr
++;
5057 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5059 location
[i
] = *pptr
;
5067 while (*pptr
&& isspace(*pptr
& 255))
5072 for (i
= 0, pptr
++;
5073 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5082 while (*pptr
&& isspace(*pptr
& 255))
5087 for (i
= 0, pptr
++;
5088 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5090 make_model
[i
] = *pptr
;
5095 make_model
[i
] = '\0';
5098 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5104 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5105 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5106 type
, state
, uri
, location
, info
, make_model
));
5109 * Pull the URI apart to see if this is a local or remote printer...
5112 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5114 cupsFreeOptions(num_attrs
, attrs
);
5122 for (i
= 0; i
< NumRelays
; i
++)
5123 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5124 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5125 (struct sockaddr
*)&(Relays
[i
].to
),
5126 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5128 cupsdLogMessage(CUPSD_LOG_ERROR
,
5129 "update_cups_browse: sendto failed for relay %d - %s.",
5130 i
+ 1, strerror(errno
));
5131 cupsFreeOptions(num_attrs
, attrs
);
5136 * Process the browse data...
5139 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5140 (ipp_pstate_t
)state
, location
, info
, make_model
,
5146 * 'update_lpd()' - Update the LPD configuration as needed.
5150 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5157 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5158 * setting for backwards-compatibility.
5161 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5163 #endif /* __APPLE__ */
5165 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5168 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5171 char newfile
[1024]; /* New cups-lpd.N file */
5172 cups_file_t
*ofp
, /* Original file pointer */
5173 *nfp
; /* New file pointer */
5174 char line
[1024]; /* Line from file */
5177 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5179 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5181 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5182 LPDConfigFile
+ 9, strerror(errno
));
5186 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5188 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5189 newfile
, strerror(errno
));
5195 * Copy all of the lines from the cups-lpd file...
5198 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5202 cupsFilePrintf(nfp
, "%s\n", line
);
5203 snprintf(line
, sizeof(line
), "\tdisable = %s",
5204 onoff
? "no" : "yes");
5206 else if (!strstr(line
, "disable ="))
5207 cupsFilePrintf(nfp
, "%s\n", line
);
5212 rename(newfile
, LPDConfigFile
+ 9);
5215 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5218 * Enable/disable LPD via the launchctl command...
5221 char *argv
[5], /* Arguments for command */
5222 *envp
[MAX_ENV
]; /* Environment for command */
5223 int pid
; /* Process ID */
5226 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5227 argv
[0] = (char *)"launchctl";
5228 argv
[1] = (char *)(onoff
? "load" : "unload");
5229 argv
[2] = (char *)"-w";
5230 argv
[3] = LPDConfigFile
+ 10;
5233 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5236 #endif /* __APPLE__ */
5238 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5243 * 'update_polling()' - Read status messages from the poll daemons.
5247 update_polling(void)
5249 char *ptr
, /* Pointer to end of line in buffer */
5250 message
[1024]; /* Pointer to message text */
5251 int loglevel
; /* Log level for message */
5254 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5255 message
, sizeof(message
))) != NULL
)
5256 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5259 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5262 * All polling processes have died; stop polling...
5265 cupsdLogMessage(CUPSD_LOG_ERROR
,
5266 "update_polling: all polling processes have exited!");
5273 * 'update_smb()' - Update the SMB configuration as needed.
5277 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5282 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5285 * Enable/disable SMB via the specified smb.conf config file...
5288 char newfile
[1024]; /* New smb.conf.N file */
5289 cups_file_t
*ofp
, /* Original file pointer */
5290 *nfp
; /* New file pointer */
5291 char line
[1024]; /* Line from file */
5292 int in_printers
; /* In [printers] section? */
5295 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5297 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5299 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5300 SMBConfigFile
+ 8, strerror(errno
));
5304 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5306 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5307 newfile
, strerror(errno
));
5313 * Copy all of the lines from the smb.conf file...
5318 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5320 if (in_printers
&& strstr(line
, "printable ="))
5321 snprintf(line
, sizeof(line
), " printable = %s",
5322 onoff
? "yes" : "no");
5324 cupsFilePrintf(nfp
, "%s\n", line
);
5327 in_printers
= !strcmp(line
, "[printers]");
5332 rename(newfile
, SMBConfigFile
+ 8);
5335 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5340 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".