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 * dnssdStop() - Stop all DNS-SD registrations.
51 * dnssdUpdate() - Handle DNS-SD queries.
52 * get_hostconfig() - Get an /etc/hostconfig service setting.
53 * is_local_queue() - Determine whether the URI points at a local
55 * process_browse_data() - Process new browse data.
56 * process_implicit_classes() - Create/update implicit classes as needed.
57 * send_cups_browse() - Send new browsing information using the CUPS
59 * ldap_search_rec() - LDAP Search with reconnect
60 * ldap_freeres() - Free LDAPMessage
61 * ldap_getval_char() - Get first LDAP value and convert to string
62 * send_ldap_ou() - Send LDAP ou registrations.
63 * send_ldap_browse() - Send LDAP printer registrations.
64 * ldap_dereg_printer() - Delete printer from directory
65 * send_slp_browse() - Register the specified printer with SLP.
66 * slp_attr_callback() - SLP attribute callback
67 * slp_dereg_printer() - SLPDereg() the specified printer
68 * slp_get_attr() - Get an attribute from an SLP registration.
69 * slp_reg_callback() - Empty SLPRegReport.
70 * slp_url_callback() - SLP service url callback
71 * update_cups_browse() - Update the browse lists using the CUPS
73 * update_lpd() - Update the LPD configuration as needed.
74 * update_polling() - Read status messages from the poll daemons.
75 * update_smb() - Update the SMB configuration as needed.
79 * Include necessary headers...
89 # ifdef HAVE_COREFOUNDATION
90 # include <CoreFoundation/CoreFoundation.h>
91 # endif /* HAVE_COREFOUNDATION */
92 # ifdef HAVE_SYSTEMCONFIGURATION
93 # include <SystemConfiguration/SystemConfiguration.h>
94 # endif /* HAVE_SYSTEMCONFIGURATION */
95 # endif /* __APPLE__ */
96 #endif /* HAVE_DNSSD */
103 static char *dequote(char *d
, const char *s
, int dlen
);
105 static int get_hostconfig(const char *name
);
106 #endif /* __APPLE__ */
107 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
108 char *resource
, int resourcelen
);
109 static void process_browse_data(const char *uri
, const char *host
,
110 const char *resource
, cups_ptype_t type
,
111 ipp_pstate_t state
, const char *location
,
112 const char *info
, const char *make_model
,
113 int num_attrs
, cups_option_t
*attrs
);
114 static void process_implicit_classes(void);
115 static void send_cups_browse(cupsd_printer_t
*p
);
117 static LDAP
*ldap_connect(void);
118 static void ldap_reconnect(void);
119 static void ldap_disconnect(LDAP
*ld
);
120 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
121 char *filter
, char *attrs
[],
122 int attrsonly
, LDAPMessage
**res
);
123 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
124 char *attr
, char *retval
,
125 unsigned long maxsize
);
126 static void ldap_freeres(LDAPMessage
*entry
);
127 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
128 static void send_ldap_browse(cupsd_printer_t
*p
);
129 static void ldap_dereg_printer(cupsd_printer_t
*p
);
130 static void ldap_dereg_ou(char *ou
, char *basedn
);
131 # ifdef HAVE_LDAP_REBIND_PROC
132 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
133 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
134 LDAP_CONST
char *refsp
,
139 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
145 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
146 # endif /* HAVE_LDAP_REBIND_PROC */
147 #endif /* HAVE_LDAP */
149 static void send_slp_browse(cupsd_printer_t
*p
);
150 #endif /* HAVE_LIBSLP */
151 static void update_cups_browse(void);
152 static void update_lpd(int onoff
);
153 static void update_polling(void);
154 static void update_smb(int onoff
);
158 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
160 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
161 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
162 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
164 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
165 DNSServiceFlags flags
,
166 DNSServiceErrorType errorCode
,
167 const char *name
, const char *regtype
,
168 const char *domain
, void *context
);
169 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
170 static void dnssdStop(void);
171 static void dnssdUpdate(void);
172 #endif /* HAVE_DNSSD */
175 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
177 "printerDescription",
179 "printerMakeAndModel",
184 #endif /* HAVE_LDAP */
192 * SLP service name for CUPS...
195 # define SLP_CUPS_SRVTYPE "service:printer"
196 # define SLP_CUPS_SRVLEN 15
200 * Printer service URL structure
203 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
205 struct _slpsrvurl_s
*next
; /* Next URL in list */
206 char url
[HTTP_MAX_URI
];
215 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
216 SLPError errcode
, void *cookie
);
217 static void slp_dereg_printer(cupsd_printer_t
*p
);
218 static int slp_get_attr(const char *attrlist
, const char *tag
,
220 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
222 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
223 unsigned short lifetime
,
224 SLPError errcode
, void *cookie
);
225 #endif /* HAVE_LIBSLP */
229 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
230 * local printer and remove any pending
231 * references to remote printers.
235 cupsdDeregisterPrinter(
236 cupsd_printer_t
*p
, /* I - Printer to register */
237 int removeit
) /* I - Printer being permanently removed */
240 * Only deregister if browsing is enabled and it's a local printer...
243 cupsdLogMessage(CUPSD_LOG_DEBUG
,
244 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
247 if (!Browsing
|| !p
->shared
||
248 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
252 * Announce the deletion...
255 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
257 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
259 p
->type
|= CUPS_PRINTER_DELETE
;
267 if (BrowseLocalProtocols
& BROWSE_SLP
)
268 slp_dereg_printer(p
);
269 #endif /* HAVE_LIBSLP */
272 if (BrowseLocalProtocols
& BROWSE_LDAP
)
273 ldap_dereg_printer(p
);
274 #endif /* HAVE_LDAP */
277 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
278 dnssdDeregisterPrinter(p
);
279 #endif /* HAVE_DNSSD */
284 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
288 cupsdLoadRemoteCache(void)
290 int i
; /* Looping var */
291 cups_file_t
*fp
; /* remote.cache file */
292 int linenum
; /* Current line number */
293 char line
[4096], /* Line from file */
294 *value
, /* Pointer to value */
295 *valueptr
, /* Pointer into value */
296 scheme
[32], /* Scheme portion of URI */
297 username
[64], /* Username portion of URI */
299 /* Hostname portion of URI */
300 resource
[HTTP_MAX_URI
];
301 /* Resource portion of URI */
302 int port
; /* Port number */
303 cupsd_printer_t
*p
; /* Current printer */
304 time_t now
; /* Current time */
308 * Don't load the cache if the remote protocols are disabled...
313 cupsdLogMessage(CUPSD_LOG_DEBUG
,
314 "cupsdLoadRemoteCache: Not loading remote cache.");
319 * Open the remote.cache file...
322 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
323 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
327 * Read printer configurations until we hit EOF...
334 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
337 * Decode the directive...
340 if (!strcasecmp(line
, "<Printer") ||
341 !strcasecmp(line
, "<DefaultPrinter"))
344 * <Printer name> or <DefaultPrinter name>
347 if (p
== NULL
&& value
)
350 * Add the printer and a base file type...
353 cupsdLogMessage(CUPSD_LOG_DEBUG
,
354 "cupsdLoadRemoteCache: Loading printer %s...", value
);
356 if ((p
= cupsdFindDest(value
)) != NULL
)
358 if (p
->type
& CUPS_PRINTER_CLASS
)
360 cupsdLogMessage(CUPSD_LOG_WARN
,
361 "Cached remote printer \"%s\" conflicts with "
369 p
= cupsdAddPrinter(value
);
372 p
->state
= IPP_PRINTER_IDLE
;
373 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
374 p
->browse_time
= now
;
375 p
->browse_expire
= now
+ BrowseTimeout
;
378 * Set the default printer as needed...
381 if (!strcasecmp(line
, "<DefaultPrinter"))
386 cupsdLogMessage(CUPSD_LOG_ERROR
,
387 "Syntax error on line %d of remote.cache.", linenum
);
391 else if (!strcasecmp(line
, "<Class") ||
392 !strcasecmp(line
, "<DefaultClass"))
395 * <Class name> or <DefaultClass name>
398 if (p
== NULL
&& value
)
401 * Add the printer and a base file type...
404 cupsdLogMessage(CUPSD_LOG_DEBUG
,
405 "cupsdLoadRemoteCache: Loading class %s...", value
);
407 if ((p
= cupsdFindDest(value
)) != NULL
)
408 p
->type
= CUPS_PRINTER_CLASS
;
410 p
= cupsdAddClass(value
);
413 p
->state
= IPP_PRINTER_IDLE
;
414 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
415 p
->browse_time
= now
;
416 p
->browse_expire
= now
+ BrowseTimeout
;
419 * Set the default printer as needed...
422 if (!strcasecmp(line
, "<DefaultClass"))
427 cupsdLogMessage(CUPSD_LOG_ERROR
,
428 "Syntax error on line %d of remote.cache.", linenum
);
432 else if (!strcasecmp(line
, "</Printer>") ||
433 !strcasecmp(line
, "</Class>"))
438 * Close out the current printer...
441 cupsdSetPrinterAttrs(p
);
446 cupsdLogMessage(CUPSD_LOG_ERROR
,
447 "Syntax error on line %d of remote.cache.", linenum
);
451 cupsdLogMessage(CUPSD_LOG_ERROR
,
452 "Syntax error on line %d of remote.cache.", linenum
);
454 else if (!strcasecmp(line
, "Info"))
457 cupsdSetString(&p
->info
, value
);
459 else if (!strcasecmp(line
, "MakeModel"))
462 cupsdSetString(&p
->make_model
, value
);
464 else if (!strcasecmp(line
, "Location"))
467 cupsdSetString(&p
->location
, value
);
469 else if (!strcasecmp(line
, "DeviceURI"))
473 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
474 username
, sizeof(username
), host
, sizeof(host
), &port
,
475 resource
, sizeof(resource
));
477 cupsdSetString(&p
->hostname
, host
);
478 cupsdSetString(&p
->uri
, value
);
479 cupsdSetDeviceURI(p
, value
);
482 cupsdLogMessage(CUPSD_LOG_ERROR
,
483 "Syntax error on line %d of remote.cache.", linenum
);
485 else if (!strcasecmp(line
, "Option") && value
)
491 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
494 cupsdLogMessage(CUPSD_LOG_ERROR
,
495 "Syntax error on line %d of remote.cache.", linenum
);
498 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
500 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
504 else if (!strcasecmp(line
, "Reason"))
508 for (i
= 0 ; i
< p
->num_reasons
; i
++)
509 if (!strcmp(value
, p
->reasons
[i
]))
512 if (i
>= p
->num_reasons
&&
513 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
515 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
520 cupsdLogMessage(CUPSD_LOG_ERROR
,
521 "Syntax error on line %d of remote.cache.", linenum
);
523 else if (!strcasecmp(line
, "State"))
526 * Set the initial queue state...
529 if (value
&& !strcasecmp(value
, "idle"))
530 p
->state
= IPP_PRINTER_IDLE
;
531 else if (value
&& !strcasecmp(value
, "stopped"))
533 p
->state
= IPP_PRINTER_STOPPED
;
534 cupsdSetPrinterReasons(p
, "+paused");
537 cupsdLogMessage(CUPSD_LOG_ERROR
,
538 "Syntax error on line %d of remote.cache.", linenum
);
540 else if (!strcasecmp(line
, "StateMessage"))
543 * Set the initial queue state message...
547 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
549 else if (!strcasecmp(line
, "Accepting"))
552 * Set the initial accepting state...
556 (!strcasecmp(value
, "yes") ||
557 !strcasecmp(value
, "on") ||
558 !strcasecmp(value
, "true")))
561 (!strcasecmp(value
, "no") ||
562 !strcasecmp(value
, "off") ||
563 !strcasecmp(value
, "false")))
566 cupsdLogMessage(CUPSD_LOG_ERROR
,
567 "Syntax error on line %d of remote.cache.", linenum
);
569 else if (!strcasecmp(line
, "Type"))
572 p
->type
= atoi(value
);
574 cupsdLogMessage(CUPSD_LOG_ERROR
,
575 "Syntax error on line %d of remote.cache.", linenum
);
577 else if (!strcasecmp(line
, "BrowseTime"))
581 time_t t
= atoi(value
);
583 if (t
> p
->browse_expire
)
584 p
->browse_expire
= t
;
587 cupsdLogMessage(CUPSD_LOG_ERROR
,
588 "Syntax error on line %d of remote.cache.", linenum
);
590 else if (!strcasecmp(line
, "JobSheets"))
593 * Set the initial job sheets...
598 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
603 cupsdSetString(&p
->job_sheets
[0], value
);
605 while (isspace(*valueptr
& 255))
610 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
615 cupsdSetString(&p
->job_sheets
[1], value
);
619 cupsdLogMessage(CUPSD_LOG_ERROR
,
620 "Syntax error on line %d of remote.cache.", linenum
);
622 else if (!strcasecmp(line
, "AllowUser"))
627 cupsdAddPrinterUser(p
, value
);
630 cupsdLogMessage(CUPSD_LOG_ERROR
,
631 "Syntax error on line %d of remote.cache.", linenum
);
633 else if (!strcasecmp(line
, "DenyUser"))
638 cupsdAddPrinterUser(p
, value
);
641 cupsdLogMessage(CUPSD_LOG_ERROR
,
642 "Syntax error on line %d of remote.cache.", linenum
);
647 * Something else we don't understand...
650 cupsdLogMessage(CUPSD_LOG_ERROR
,
651 "Unknown configuration directive %s on line %d of remote.cache.",
659 * Do auto-classing if needed...
662 process_implicit_classes();
667 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
668 * printer or update the broadcast contents.
672 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
674 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
677 if (!Browsing
|| !BrowseLocalProtocols
||
678 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
682 /* if (BrowseLocalProtocols & BROWSE_SLP)
683 slpRegisterPrinter(p); */
684 #endif /* HAVE_LIBSLP */
687 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
688 dnssdRegisterPrinter(p
);
689 #endif /* HAVE_DNSSD */
694 * 'cupsdRestartPolling()' - Restart polling servers as needed.
698 cupsdRestartPolling(void)
700 int i
; /* Looping var */
701 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
704 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
706 kill(pollp
->pid
, SIGHUP
);
711 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
715 cupsdSaveRemoteCache(void)
717 int i
; /* Looping var */
718 cups_file_t
*fp
; /* printers.conf file */
719 char temp
[1024], /* Temporary string */
720 value
[2048]; /* Value string */
721 cupsd_printer_t
*printer
; /* Current printer class */
722 time_t curtime
; /* Current time */
723 struct tm
*curdate
; /* Current date */
724 cups_option_t
*option
; /* Current option */
728 * Create the remote.cache file...
731 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
733 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
735 cupsdLogMessage(CUPSD_LOG_ERROR
,
736 "Unable to save remote.cache - %s", strerror(errno
));
740 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
743 * Restrict access to the file...
746 fchown(cupsFileNumber(fp
), getuid(), Group
);
747 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
750 * Write a small header to the file...
753 curtime
= time(NULL
);
754 curdate
= localtime(&curtime
);
755 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
757 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
758 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
761 * Write each local printer known to the system...
764 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
766 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
769 * Skip local destinations...
772 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
776 * Write printers as needed...
779 if (printer
== DefaultPrinter
)
780 cupsFilePuts(fp
, "<Default");
782 cupsFilePutChar(fp
, '<');
784 if (printer
->type
& CUPS_PRINTER_CLASS
)
785 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
787 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
789 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
792 cupsFilePutConf(fp
, "Info", printer
->info
);
794 if (printer
->location
)
795 cupsFilePutConf(fp
, "Location", printer
->location
);
797 if (printer
->make_model
)
798 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
800 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
802 if (printer
->state
== IPP_PRINTER_STOPPED
)
803 cupsFilePuts(fp
, "State Stopped\n");
805 cupsFilePuts(fp
, "State Idle\n");
807 for (i
= 0; i
< printer
->num_reasons
; i
++)
808 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
810 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
812 if (printer
->accepting
)
813 cupsFilePuts(fp
, "Accepting Yes\n");
815 cupsFilePuts(fp
, "Accepting No\n");
817 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
818 printer
->job_sheets
[1]);
819 cupsFilePutConf(fp
, "JobSheets", value
);
821 for (i
= 0; i
< printer
->num_users
; i
++)
822 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
825 for (i
= printer
->num_options
, option
= printer
->options
;
829 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
830 cupsFilePutConf(fp
, "Option", value
);
833 if (printer
->type
& CUPS_PRINTER_CLASS
)
834 cupsFilePuts(fp
, "</Class>\n");
836 cupsFilePuts(fp
, "</Printer>\n");
844 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
848 cupsdSendBrowseList(void)
850 int count
; /* Number of dests to update */
851 cupsd_printer_t
*p
; /* Current printer */
852 time_t ut
, /* Minimum update time */
853 to
; /* Timeout time */
856 if (!Browsing
|| !Printers
)
860 * Compute the update and timeout times...
864 ut
= to
- BrowseInterval
;
867 * Figure out how many printers need an update...
870 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
872 int max_count
; /* Maximum number to update */
876 * Throttle the number of printers we'll be updating this time
877 * around based on the number of queues that need updating and
878 * the maximum number of queues to update each second...
881 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
883 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
884 count
< max_count
&& p
!= NULL
;
885 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
886 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
887 p
->shared
&& p
->browse_time
< ut
)
891 * Loop through all of the printers and send local updates as needed...
895 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
897 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
901 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
904 * Check for wraparound...
908 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
912 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
915 else if (p
->browse_time
< ut
)
918 * Need to send an update...
923 p
->browse_time
= time(NULL
);
925 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
929 if (BrowseLocalProtocols
& BROWSE_SLP
)
931 #endif /* HAVE_LIBSLP */
934 if (BrowseLocalProtocols
& BROWSE_LDAP
)
936 #endif /* HAVE_LDAP */
941 * Save where we left off so that all printers get updated...
948 * Loop through all of the printers and timeout old printers as needed...
951 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
953 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
956 * If this is a remote queue, see if it needs to be timed out...
959 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
960 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
961 p
->browse_expire
< to
)
963 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
964 "%s \'%s\' deleted by directory services (timeout).",
965 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
968 cupsdLogMessage(CUPSD_LOG_DEBUG
,
969 "Remote destination \"%s\" has timed out; "
973 cupsArraySave(Printers
);
974 cupsdDeletePrinter(p
, 1);
975 cupsArrayRestore(Printers
);
976 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
982 #ifdef HAVE_LDAP_REBIND_PROC
983 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
985 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
989 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
990 LDAP_CONST
char *refsp
,
998 * Bind to new LDAP server...
1001 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1002 "ldap_rebind_proc: Rebind to %s", refsp
);
1004 # if LDAP_API_VERSION > 3000
1006 bval
.bv_val
= BrowseLDAPPassword
;
1007 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1009 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1011 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
1012 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1013 # endif /* LDAP_API_VERSION > 3000 */
1018 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1021 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1025 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1037 * Free current values...
1040 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1041 "ldap_rebind_proc: Free values...");
1043 if ( dnp
&& *dnp
) {
1046 if ( passwdp
&& *passwdp
) {
1054 * Return credentials for LDAP referal...
1057 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1058 "ldap_rebind_proc: Return necessary values...");
1060 *dnp
= strdup(BrowseLDAPBindDN
);
1061 *passwdp
= strdup(BrowseLDAPPassword
);
1062 *authmethodp
= LDAP_AUTH_SIMPLE
;
1068 * Should never happen...
1071 cupsdLogMessage(CUPSD_LOG_ERROR
,
1072 "LDAP rebind has been called with wrong freeit value!");
1077 return (LDAP_SUCCESS
);
1079 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1080 #endif /* HAVE_LDAP_REBIND_PROC */
1085 * 'ldap_connect()' - Start new LDAP connection
1092 * Open LDAP handle...
1095 int rc
; /* LDAP API status */
1096 int version
= 3; /* LDAP version */
1097 struct berval bv
= {0, ""}; /* SASL bind value */
1098 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1099 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1100 int ldap_ssl
= 0; /* LDAP SSL indicator */
1101 int ssl_err
= 0; /* LDAP SSL error value */
1102 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1104 # ifdef HAVE_OPENLDAP
1105 # ifdef HAVE_LDAP_SSL
1108 * Set the certificate file to use for encrypted LDAP sessions...
1111 if (BrowseLDAPCACertFile
)
1113 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1114 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1115 BrowseLDAPCACertFile
);
1117 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1118 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1119 cupsdLogMessage(CUPSD_LOG_ERROR
,
1120 "Unable to set CA certificate file for LDAP "
1121 "connections: %d - %s", rc
, ldap_err2string(rc
));
1124 # endif /* HAVE_LDAP_SSL */
1126 * Initialize OPENLDAP connection...
1127 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1130 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1131 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1133 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1135 # else /* HAVE_OPENLDAP */
1137 int ldap_port
= 0; /* LDAP port */
1138 char ldap_protocol
[11], /* LDAP protocol */
1139 ldap_host
[255]; /* LDAP host */
1142 * Split LDAP URI into its components...
1145 if (! BrowseLDAPServer
)
1147 cupsdLogMessage(CUPSD_LOG_ERROR
,
1148 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1149 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1150 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1154 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1156 if (strcmp(ldap_protocol
, "ldap") == 0) {
1158 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1161 cupsdLogMessage(CUPSD_LOG_ERROR
,
1162 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1163 cupsdLogMessage(CUPSD_LOG_ERROR
,
1164 "Disable LDAP browsing!");
1165 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1166 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1173 ldap_port
= LDAPS_PORT
;
1175 ldap_port
= LDAP_PORT
;
1178 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1179 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1180 ldap_protocol
, ldap_host
, ldap_port
);
1183 * Initialize LDAP connection...
1188 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1189 rc
= LDAP_OPERATIONS_ERROR
;
1193 # ifdef HAVE_LDAP_SSL
1199 * Initialize SSL LDAP connection...
1201 if (BrowseLDAPCACertFile
)
1203 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1204 if (rc
!= LDAP_SUCCESS
) {
1205 cupsdLogMessage(CUPSD_LOG_ERROR
,
1206 "Failed to initialize LDAP SSL client!");
1207 rc
= LDAP_OPERATIONS_ERROR
;
1209 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1210 rc
= LDAP_OPERATIONS_ERROR
;
1217 cupsdLogMessage(CUPSD_LOG_ERROR
,
1218 "LDAP SSL certificate file/database not configured!");
1219 rc
= LDAP_OPERATIONS_ERROR
;
1222 # else /* HAVE_LDAP_SSL */
1225 * Return error, because client libraries doesn't support SSL
1228 cupsdLogMessage(CUPSD_LOG_ERROR
,
1229 "LDAP client libraries does not support TLS");
1230 rc
= LDAP_OPERATIONS_ERROR
;
1232 # endif /* HAVE_LDAP_SSL */
1234 # endif /* HAVE_OPENLDAP */
1237 * Check return code from LDAP initialize...
1240 if (rc
!= LDAP_SUCCESS
)
1242 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1244 cupsdLogMessage(CUPSD_LOG_ERROR
,
1245 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1249 cupsdLogMessage(CUPSD_LOG_ERROR
,
1250 "Unable to initialize LDAP! Disable LDAP browsing!");
1251 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1252 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1255 ldap_disconnect(TempBrowseLDAPHandle
);
1256 TempBrowseLDAPHandle
= NULL
;
1260 * Upgrade LDAP version...
1263 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1264 (const void *)&version
) != LDAP_SUCCESS
)
1266 cupsdLogMessage(CUPSD_LOG_ERROR
,
1267 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1269 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1270 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1271 ldap_disconnect(TempBrowseLDAPHandle
);
1272 TempBrowseLDAPHandle
= NULL
;
1278 * Register LDAP rebind procedure...
1281 # ifdef HAVE_LDAP_REBIND_PROC
1282 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1284 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1285 if ( rc
!= LDAP_SUCCESS
)
1286 cupsdLogMessage(CUPSD_LOG_ERROR
,
1287 "Setting LDAP rebind function failed with status %d: %s",
1288 rc
, ldap_err2string(rc
));
1292 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1294 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1295 # endif /* HAVE_LDAP_REBIND_PROC */
1298 * Start LDAP bind...
1301 # if LDAP_API_VERSION > 3000
1303 bval
.bv_val
= BrowseLDAPPassword
;
1304 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1306 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1307 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1310 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1312 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1313 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1314 # endif /* LDAP_API_VERSION > 3000 */
1316 if (rc
!= LDAP_SUCCESS
)
1318 cupsdLogMessage(CUPSD_LOG_ERROR
,
1319 "LDAP bind failed with error %d: %s",
1320 rc
, ldap_err2string(rc
));
1321 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1322 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1324 ssl_err
= PORT_GetError();
1326 cupsdLogMessage(CUPSD_LOG_ERROR
,
1327 "LDAP SSL error %d: %s",
1328 ssl_err
, ldapssl_err2string(ssl_err
));
1330 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1331 ldap_disconnect(TempBrowseLDAPHandle
);
1332 TempBrowseLDAPHandle
= NULL
;
1336 cupsdLogMessage(CUPSD_LOG_INFO
,
1337 "LDAP connection established");
1341 return (TempBrowseLDAPHandle
);
1346 * 'ldap_reconnect()' - Reconnect to LDAP Server
1350 ldap_reconnect(void)
1352 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1354 cupsdLogMessage(CUPSD_LOG_INFO
,
1355 "Try LDAP reconnect...");
1358 * Get a new LDAP Handle and replace the global Handle
1359 * if the new connection was successful
1362 TempBrowseLDAPHandle
= ldap_connect();
1364 if (TempBrowseLDAPHandle
!= NULL
)
1366 if (BrowseLDAPHandle
!= NULL
)
1368 ldap_disconnect(BrowseLDAPHandle
);
1370 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1376 * 'ldap_disconnect()' - Disconnect from LDAP Server
1380 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1382 int rc
; /* return code */
1385 * Close LDAP handle...
1388 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1389 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1391 rc
= ldap_unbind_s(ld
);
1392 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1393 if (rc
!= LDAP_SUCCESS
)
1394 cupsdLogMessage(CUPSD_LOG_ERROR
,
1395 "Unbind from LDAP server failed with status %d: %s",
1396 rc
, ldap_err2string(rc
));
1398 #endif /* HAVE_LDAP */
1402 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1406 cupsdStartBrowsing(void)
1408 int val
; /* Socket option value */
1409 struct sockaddr_in addr
; /* Broadcast address */
1410 cupsd_printer_t
*p
; /* Current printer */
1415 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1418 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1420 if (BrowseSocket
< 0)
1423 * Create the broadcast socket...
1426 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1428 cupsdLogMessage(CUPSD_LOG_ERROR
,
1429 "Unable to create broadcast socket - %s.",
1431 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1432 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1434 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1435 cupsdEndProcess(getpid(), 0);
1439 if (BrowseSocket
>= 0)
1442 * Bind the socket to browse port...
1445 memset(&addr
, 0, sizeof(addr
));
1446 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1447 addr
.sin_family
= AF_INET
;
1448 addr
.sin_port
= htons(BrowsePort
);
1450 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1452 cupsdLogMessage(CUPSD_LOG_ERROR
,
1453 "Unable to bind broadcast socket - %s.",
1457 closesocket(BrowseSocket
);
1459 close(BrowseSocket
);
1463 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1464 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1466 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1467 cupsdEndProcess(getpid(), 0);
1471 if (BrowseSocket
>= 0)
1474 * Set the "broadcast" flag...
1478 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1480 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1484 closesocket(BrowseSocket
);
1486 close(BrowseSocket
);
1490 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1491 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1493 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1494 cupsdEndProcess(getpid(), 0);
1498 if (BrowseSocket
>= 0)
1501 * Close the socket on exec...
1504 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1507 * Finally, add the socket to the input selection set as needed...
1510 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1513 * We only listen if we want remote printers...
1516 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1525 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1527 DNSServiceErrorType error
; /* Error from service creation */
1528 cupsd_listener_t
*lis
; /* Current listening socket */
1532 * First create a "master" connection for all registrations...
1535 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1536 != kDNSServiceErr_NoError
)
1538 cupsdLogMessage(CUPSD_LOG_ERROR
,
1539 "Unable to create master DNS-SD reference: %d", error
);
1541 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1542 cupsdEndProcess(getpid(), 0);
1547 * Add the master connection to the select list...
1550 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1551 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1554 * Then get the port we use for registrations. If we are not listening
1555 * on any non-local ports, there is no sense sharing local printers via
1561 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1563 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1565 if (httpAddrLocalhost(&(lis
->address
)))
1568 if (lis
->address
.addr
.sa_family
== AF_INET
)
1570 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1573 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1575 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1581 * Create an array to track the printers we share...
1584 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1585 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1589 * Set the computer name and register the web interface...
1592 cupsdUpdateDNSSDName();
1595 #endif /* HAVE_DNSSD */
1598 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1601 * Open SLP handle...
1604 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1606 cupsdLogMessage(CUPSD_LOG_ERROR
,
1607 "Unable to open an SLP handle; disabling SLP browsing!");
1608 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1609 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1610 BrowseSLPHandle
= NULL
;
1612 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1613 cupsdEndProcess(getpid(), 0);
1616 BrowseSLPRefresh
= 0;
1619 BrowseSLPHandle
= NULL
;
1620 #endif /* HAVE_LIBSLP */
1623 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1627 cupsdLogMessage(CUPSD_LOG_ERROR
,
1628 "Need to set BrowseLDAPDN to use LDAP browsing!");
1629 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1630 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1632 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1633 cupsdEndProcess(getpid(), 0);
1638 * Open LDAP handle...
1641 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1642 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1643 cupsdEndProcess(getpid(), 0);
1646 BrowseLDAPRefresh
= 0;
1648 #endif /* HAVE_LDAP */
1651 * Enable LPD and SMB printer sharing as needed through external programs...
1654 if (BrowseLocalProtocols
& BROWSE_LPD
)
1657 if (BrowseLocalProtocols
& BROWSE_SMB
)
1661 * Register the individual printers
1664 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1666 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1667 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1668 cupsdRegisterPrinter(p
);
1673 * 'cupsdStartPolling()' - Start polling servers as needed.
1677 cupsdStartPolling(void)
1679 int i
; /* Looping var */
1680 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1681 char polld
[1024]; /* Poll daemon path */
1682 char sport
[255]; /* Server port */
1683 char bport
[255]; /* Browser port */
1684 char interval
[255]; /* Poll interval */
1685 int statusfds
[2]; /* Status pipe */
1686 char *argv
[6]; /* Arguments */
1687 char *envp
[100]; /* Environment */
1691 * Don't do anything if we aren't polling...
1694 if (NumPolled
== 0 || BrowseSocket
< 0)
1697 PollStatusBuffer
= NULL
;
1702 * Setup string arguments for polld, port and interval options.
1705 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1707 sprintf(bport
, "%d", BrowsePort
);
1710 sprintf(interval
, "%d", BrowseInterval
);
1712 strcpy(interval
, "30");
1714 argv
[0] = "cups-polld";
1720 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1723 * Create a pipe that receives the status messages from each
1727 if (cupsdOpenPipe(statusfds
))
1729 cupsdLogMessage(CUPSD_LOG_ERROR
,
1730 "Unable to create polling status pipes - %s.",
1733 PollStatusBuffer
= NULL
;
1737 PollPipe
= statusfds
[0];
1738 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1741 * Run each polling daemon, redirecting stderr to the polling pipe...
1744 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1746 sprintf(sport
, "%d", pollp
->port
);
1748 argv
[1] = pollp
->hostname
;
1750 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1751 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1753 cupsdLogMessage(CUPSD_LOG_ERROR
,
1754 "cupsdStartPolling: Unable to fork polling daemon - %s",
1760 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1761 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1762 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1765 close(statusfds
[1]);
1768 * Finally, add the pipe to the input selection set...
1771 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1776 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1780 cupsdStopBrowsing(void)
1782 cupsd_printer_t
*p
; /* Current printer */
1785 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1789 * De-register the individual printers
1792 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1794 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1795 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1796 cupsdDeregisterPrinter(p
, 1);
1799 * Shut down browsing sockets...
1802 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1806 * Close the socket and remove it from the input selection set.
1810 closesocket(BrowseSocket
);
1812 close(BrowseSocket
);
1815 cupsdRemoveSelect(BrowseSocket
);
1820 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1822 #endif /* HAVE_DNSSD */
1825 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1829 * Close SLP handle...
1832 SLPClose(BrowseSLPHandle
);
1833 BrowseSLPHandle
= NULL
;
1835 #endif /* HAVE_LIBSLP */
1838 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1841 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1842 ldap_disconnect(BrowseLDAPHandle
);
1843 BrowseLDAPHandle
= NULL
;
1845 #endif /* HAVE_OPENLDAP */
1848 * Disable LPD and SMB printer sharing as needed through external programs...
1851 if (BrowseLocalProtocols
& BROWSE_LPD
)
1854 if (BrowseLocalProtocols
& BROWSE_SMB
)
1860 * 'cupsdStopPolling()' - Stop polling servers as needed.
1864 cupsdStopPolling(void)
1866 int i
; /* Looping var */
1867 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1872 cupsdStatBufDelete(PollStatusBuffer
);
1875 cupsdRemoveSelect(PollPipe
);
1878 PollStatusBuffer
= NULL
;
1881 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1883 cupsdEndProcess(pollp
->pid
, 0);
1889 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1893 cupsdUpdateDNSSDName(void)
1895 DNSServiceErrorType error
; /* Error from service creation */
1896 char webif
[1024]; /* Web interface share name */
1897 #ifdef HAVE_COREFOUNDATION_H
1898 CFStringRef nameRef
; /* Computer name CFString */
1899 char nameBuffer
[1024]; /* C-string buffer */
1900 CFStringEncoding nameEncoding
; /* Computer name encoding */
1901 #endif /* HAVE_COREFOUNDATION_H */
1905 * Only share the web interface and printers when non-local listening is
1913 * Get the computer name as a c-string...
1916 #ifdef HAVE_COREFOUNDATION_H
1917 cupsdClearString(&DNSSDName
);
1919 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1920 &nameEncoding
)) != NULL
)
1922 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1923 kCFStringEncodingUTF8
))
1924 cupsdSetString(&DNSSDName
, nameBuffer
);
1930 cupsdSetString(&DNSSDName
, ServerName
);
1931 #endif /* HAVE_COREFOUNDATION_H */
1934 * Then (re)register the web interface if enabled...
1940 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1942 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1945 DNSServiceRefDeallocate(WebIFRef
);
1947 WebIFRef
= DNSSDRef
;
1948 if ((error
= DNSServiceRegister(&WebIFRef
,
1949 kDNSServiceFlagsShareConnection
,
1950 0, webif
, "_http._tcp", NULL
,
1951 NULL
, htons(DNSSDPort
), 7,
1952 "\006path=/", dnssdRegisterCallback
,
1953 NULL
)) != kDNSServiceErr_NoError
)
1954 cupsdLogMessage(CUPSD_LOG_ERROR
,
1955 "DNS-SD web interface registration failed: %d", error
);
1958 #endif /* HAVE_DNSSD */
1963 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1967 cupsdUpdateLDAPBrowse(void)
1969 char uri
[HTTP_MAX_URI
], /* Printer URI */
1970 host
[HTTP_MAX_URI
], /* Hostname */
1971 resource
[HTTP_MAX_URI
], /* Resource path */
1972 location
[1024], /* Printer location */
1973 info
[1024], /* Printer information */
1974 make_model
[1024], /* Printer make and model */
1975 type_num
[30]; /* Printer type number */
1976 int type
; /* Printer type */
1977 int rc
; /* LDAP status */
1978 int limit
; /* Size limit */
1979 LDAPMessage
*res
, /* LDAP search results */
1980 *e
; /* Current entry from search */
1982 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1984 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1987 * Reconnect if LDAP Handle is invalid...
1990 if (! BrowseLDAPHandle
)
1997 * Search for cups printers in LDAP directory...
2000 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2001 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2004 * If ldap search was successfull then exit function
2005 * and temporary disable LDAP updates...
2008 if (rc
!= LDAP_SUCCESS
)
2010 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2012 BrowseLDAPUpdate
= FALSE
;
2013 cupsdLogMessage(CUPSD_LOG_INFO
,
2014 "LDAP update temporary disabled");
2020 * If LDAP updates were disabled, we will reenable them...
2023 if (! BrowseLDAPUpdate
)
2025 BrowseLDAPUpdate
= TRUE
;
2026 cupsdLogMessage(CUPSD_LOG_INFO
,
2027 "LDAP update enabled");
2031 * Count LDAP entries and return if no entry exist...
2034 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2035 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2043 * Loop through the available printers...
2046 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2048 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2051 * Get the required values from this entry...
2054 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2055 "printerDescription", info
, sizeof(info
)) == -1)
2058 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2059 "printerLocation", location
, sizeof(location
)) == -1)
2062 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2063 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2066 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2067 "printerType", type_num
, sizeof(type_num
)) == -1)
2070 type
= atoi(type_num
);
2072 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2073 "printerURI", uri
, sizeof(uri
)) == -1)
2077 * Process the entry as browse data...
2080 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2081 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2082 location
, info
, make_model
, 0, NULL
);
2088 #endif /* HAVE_LDAP */
2093 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2097 cupsdUpdateSLPBrowse(void)
2099 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2100 *next
; /* Next service in list */
2101 cupsd_printer_t p
; /* Printer information */
2102 const char *uri
; /* Pointer to printer URI */
2103 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2104 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2108 * Reset the refresh time...
2111 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2114 * Poll for remote printers using SLP...
2119 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2120 slp_url_callback
, &s
);
2123 * Loop through the list of available printers...
2129 * Save the "next" pointer...
2135 * Load a cupsd_printer_t structure with the SLP service attributes...
2138 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2141 * Process this printer entry...
2144 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2146 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2149 * Pull the URI apart to see if this is a local or remote printer...
2152 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2153 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2154 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2158 * Free this listing...
2161 cupsdClearString(&p
.info
);
2162 cupsdClearString(&p
.location
);
2163 cupsdClearString(&p
.make_model
);
2168 #endif /* HAVE_LIBSLP */
2172 * 'dequote()' - Remote quotes from a string.
2175 static char * /* O - Dequoted string */
2176 dequote(char *d
, /* I - Destination string */
2177 const char *s
, /* I - Source string */
2178 int dlen
) /* I - Destination length */
2180 char *dptr
; /* Pointer into destination */
2185 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2203 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2206 static char * /* O - TXT record */
2207 dnssdBuildTxtRecord(
2208 int *txt_len
, /* O - TXT record length */
2209 cupsd_printer_t
*p
, /* I - Printer information */
2210 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2212 int i
, j
; /* Looping vars */
2213 char type_str
[32], /* Type to string buffer */
2214 state_str
[32], /* State to string buffer */
2215 rp_str
[1024], /* Queue name string buffer */
2216 air_str
[1024], /* auth-info-required string buffer */
2217 *keyvalue
[32][2]; /* Table of key/value pairs */
2218 ipp_attribute_t
*air_attr
; /* auth-info-required attribute */
2222 * Load up the key value pairs...
2227 keyvalue
[i
][0] = "txtvers";
2228 keyvalue
[i
++][1] = "1";
2230 keyvalue
[i
][0] = "qtotal";
2231 keyvalue
[i
++][1] = "1";
2233 keyvalue
[i
][0] = "rp";
2234 keyvalue
[i
++][1] = rp_str
;
2236 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2238 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2239 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2241 keyvalue
[i
][0] = "ty";
2242 keyvalue
[i
++][1] = p
->make_model
;
2244 if (p
->location
&& *p
->location
!= '\0')
2246 keyvalue
[i
][0] = "note";
2247 keyvalue
[i
++][1] = p
->location
;
2250 keyvalue
[i
][0] = "priority";
2251 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2253 keyvalue
[i
][0] = "product";
2254 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2256 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2257 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2259 keyvalue
[i
][0] = "printer-state";
2260 keyvalue
[i
++][1] = state_str
;
2262 keyvalue
[i
][0] = "printer-type";
2263 keyvalue
[i
++][1] = type_str
;
2265 keyvalue
[i
][0] = "Transparent";
2266 keyvalue
[i
++][1] = "T";
2268 keyvalue
[i
][0] = "Binary";
2269 keyvalue
[i
++][1] = "T";
2271 if ((p
->type
& CUPS_PRINTER_FAX
))
2273 keyvalue
[i
][0] = "Fax";
2274 keyvalue
[i
++][1] = "T";
2277 if ((p
->type
& CUPS_PRINTER_COLOR
))
2279 keyvalue
[i
][0] = "Color";
2280 keyvalue
[i
++][1] = "T";
2283 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2285 keyvalue
[i
][0] = "Duplex";
2286 keyvalue
[i
++][1] = "T";
2289 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2291 keyvalue
[i
][0] = "Staple";
2292 keyvalue
[i
++][1] = "T";
2295 if ((p
->type
& CUPS_PRINTER_COPIES
))
2297 keyvalue
[i
][0] = "Copies";
2298 keyvalue
[i
++][1] = "T";
2301 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2303 keyvalue
[i
][0] = "Collate";
2304 keyvalue
[i
++][1] = "T";
2307 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2309 keyvalue
[i
][0] = "Punch";
2310 keyvalue
[i
++][1] = "T";
2313 if ((p
->type
& CUPS_PRINTER_BIND
))
2315 keyvalue
[i
][0] = "Bind";
2316 keyvalue
[i
++][1] = "T";
2319 if ((p
->type
& CUPS_PRINTER_SORT
))
2321 keyvalue
[i
][0] = "Sort";
2322 keyvalue
[i
++][1] = "T";
2325 keyvalue
[i
][0] = "pdl";
2326 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2328 if ((air_attr
= ippFindAttribute(p
->attrs
, "auth-info-required",
2329 IPP_TAG_KEYWORD
)) != NULL
&&
2330 strcmp(air_attr
->values
[0].string
.text
, "none"))
2332 char *air
= air_str
; /* Pointer into string */
2335 for (j
= 0; j
< air_attr
->num_values
; j
++)
2337 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2343 strlcpy(air
, air_attr
->values
[j
].string
.text
,
2344 sizeof(air_str
) - (air
- air_str
));
2348 keyvalue
[i
][0] = "air";
2349 keyvalue
[i
++][1] = air_str
;
2353 * Then pack them into a proper txt record...
2356 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2361 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2364 static int /* O - Result of comparison */
2365 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2366 cupsd_printer_t
*b
)/* I - Second printer */
2368 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2373 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2378 dnssdDeregisterPrinter(
2379 cupsd_printer_t
*p
) /* I - Printer */
2381 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2384 * Closing the socket deregisters the service
2389 DNSServiceRefDeallocate(p
->ipp_ref
);
2396 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2405 DNSServiceRefDeallocate(p
->printer_ref
);
2406 p
->printer_ref
= NULL
;
2412 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2415 free(p
->printer_txt
);
2416 p
->printer_txt
= NULL
;
2420 * Remove the printer from the array of DNS-SD printers, then clear the
2421 * registered name...
2424 cupsArrayRemove(DNSSDPrinters
, p
);
2425 cupsdClearString(&p
->reg_name
);
2430 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2431 * TXT record format.
2434 static char * /* O - TXT record */
2435 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2436 char *keyvalue
[][2], /* I - Table of key value pairs */
2437 int count
) /* I - Items in table */
2439 int i
; /* Looping var */
2440 int length
; /* Length of TXT record */
2441 int length2
; /* Length of value */
2442 char *txtRecord
; /* TXT record buffer */
2443 char *cursor
; /* Looping pointer */
2447 * Calculate the buffer size
2450 for (length
= i
= 0; i
< count
; i
++)
2451 length
+= 1 + strlen(keyvalue
[i
][0]) +
2452 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2455 * Allocate and fill it
2458 txtRecord
= malloc(length
);
2463 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2466 * Drop in the p-string style length byte followed by the data
2469 length
= strlen(keyvalue
[i
][0]);
2470 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2472 *cursor
++ = (unsigned char)(length
+ length2
);
2474 memcpy(cursor
, keyvalue
[i
][0], length
);
2481 memcpy(cursor
, keyvalue
[i
][1], length2
);
2492 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2496 dnssdRegisterCallback(
2497 DNSServiceRef sdRef
, /* I - DNS Service reference */
2498 DNSServiceFlags flags
, /* I - Reserved for future use */
2499 DNSServiceErrorType errorCode
, /* I - Error code */
2500 const char *name
, /* I - Service name */
2501 const char *regtype
, /* I - Service type */
2502 const char *domain
, /* I - Domain. ".local" for now */
2503 void *context
) /* I - User-defined context */
2505 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2506 /* Current printer */
2509 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2510 name
, regtype
, p
? p
->name
: "Web Interface",
2511 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2515 cupsdLogMessage(CUPSD_LOG_ERROR
,
2516 "DNSServiceRegister failed with error %d", (int)errorCode
);
2519 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2521 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2524 cupsArrayRemove(DNSSDPrinters
, p
);
2525 cupsdSetString(&p
->reg_name
, name
);
2526 cupsArrayAdd(DNSSDPrinters
, p
);
2528 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2534 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2535 * or update the broadcast contents.
2539 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2541 DNSServiceErrorType se
; /* dnssd errors */
2542 char *ipp_txt
, /* IPP TXT record buffer */
2543 *printer_txt
, /* LPD TXT record buffer */
2544 name
[1024], /* Service name */
2545 *nameptr
; /* Pointer into name */
2546 int ipp_len
, /* IPP TXT record length */
2547 printer_len
; /* LPD TXT record length */
2548 char resource
[1024]; /* Resource path for printer */
2549 const char *regtype
; /* Registration type */
2550 const char *domain
; /* Registration domain */
2551 cupsd_location_t
*location
, /* Printer location */
2552 *policy
; /* Operation policy for Print-Job */
2553 unsigned address
[4]; /* INADDR_ANY address */
2556 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2557 !p
->ipp_ref
? "new" : "update");
2560 * If per-printer sharing was just disabled make sure we're not
2561 * registered before returning.
2566 dnssdDeregisterPrinter(p
);
2571 * The registered name takes the form of "<printer-info> @ <computer name>"...
2574 if (p
->info
&& strlen(p
->info
) > 0)
2577 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2579 strlcpy(name
, p
->info
, sizeof(name
));
2582 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2584 strlcpy(name
, p
->name
, sizeof(name
));
2587 * If an existing printer was renamed, unregister it and start over...
2590 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2591 dnssdDeregisterPrinter(p
);
2595 cupsdSetString(&p
->reg_name
, name
);
2596 cupsArrayAdd(DNSSDPrinters
, p
);
2600 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2601 * let dnssd decide on the domain, otherwise restrict it to ".local".
2604 if (p
->type
& CUPS_PRINTER_CLASS
)
2605 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2607 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2609 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2610 location
= cupsdFindBest(resource
, HTTP_POST
);
2611 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2613 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2614 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2620 * Register IPP and (optionally) LPD...
2623 ipp_len
= 0; /* anti-compiler-warning-code */
2624 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2627 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2630 * Update the existing registration...
2633 /* A TTL of 0 means use record's original value (Radar 3176248) */
2634 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2635 0)) == kDNSServiceErr_NoError
)
2640 p
->ipp_txt
= ipp_txt
;
2641 p
->ipp_len
= ipp_len
;
2647 * Failed to update record, lets close this reference and move on...
2650 cupsdLogMessage(CUPSD_LOG_ERROR
,
2651 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2654 DNSServiceRefDeallocate(p
->ipp_ref
);
2662 * Initial registration. Use the _fax subtype for fax queues...
2665 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2668 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2669 "Registering DNS-SD printer %s with name \"%s\", "
2670 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2671 domain
? domain
: "(null)");
2674 * Register the queue, dropping characters as needed until we succeed...
2677 nameptr
= name
+ strlen(name
);
2681 p
->ipp_ref
= DNSSDRef
;
2682 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2683 0, name
, regtype
, domain
, NULL
,
2684 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2685 dnssdRegisterCallback
,
2686 p
)) == kDNSServiceErr_BadParam
)
2689 * Name is too long, drop trailing characters, taking into account
2695 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2702 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2704 if (se
== kDNSServiceErr_NoError
)
2706 p
->ipp_txt
= ipp_txt
;
2707 p
->ipp_len
= ipp_len
;
2711 cupsdLogMessage(CUPSD_LOG_WARN
,
2712 "DNS-SD IPP registration of \"%s\" failed: %d",
2719 if (BrowseLocalProtocols
& BROWSE_LPD
)
2721 printer_len
= 0; /* anti-compiler-warning-code */
2722 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2724 if (p
->printer_ref
&&
2725 (printer_len
!= p
->printer_len
||
2726 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2729 * Update the existing registration...
2732 /* A TTL of 0 means use record's original value (Radar 3176248) */
2733 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2735 0)) == kDNSServiceErr_NoError
)
2738 free(p
->printer_txt
);
2740 p
->printer_txt
= printer_txt
;
2741 p
->printer_len
= printer_len
;
2747 * Failed to update record, lets close this reference and move on...
2750 cupsdLogMessage(CUPSD_LOG_ERROR
,
2751 "Unable to update LPD DNS-SD record for %s - %d",
2754 DNSServiceRefDeallocate(p
->printer_ref
);
2755 p
->printer_ref
= NULL
;
2759 if (!p
->printer_ref
)
2762 * Initial registration...
2765 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2766 "Registering DNS-SD printer %s with name \"%s\", "
2767 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2768 name
, domain
? domain
: "(null)");
2770 p
->printer_ref
= DNSSDRef
;
2771 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2772 kDNSServiceFlagsShareConnection
,
2773 0, name
, "_printer._tcp", domain
, NULL
,
2774 htons(515), printer_len
, printer_txt
,
2775 dnssdRegisterCallback
,
2776 p
)) == kDNSServiceErr_NoError
)
2778 p
->printer_txt
= printer_txt
;
2779 p
->printer_len
= printer_len
;
2783 cupsdLogMessage(CUPSD_LOG_WARN
,
2784 "DNS-SD LPD registration of \"%s\" failed: %d",
2795 * 'dnssdStop()' - Stop all DNS-SD registrations.
2801 cupsd_printer_t
*p
; /* Current printer */
2805 * De-register the individual printers
2808 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2810 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2811 dnssdDeregisterPrinter(p
);
2814 * Shutdown the rest of the service refs...
2819 DNSServiceRefDeallocate(WebIFRef
);
2825 DNSServiceRefDeallocate(RemoteRef
);
2829 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2831 DNSServiceRefDeallocate(DNSSDRef
);
2834 cupsArrayDelete(DNSSDPrinters
);
2835 DNSSDPrinters
= NULL
;
2842 * 'dnssdUpdate()' - Handle DNS-SD queries.
2848 DNSServiceErrorType sdErr
; /* Service discovery error */
2851 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2853 cupsdLogMessage(CUPSD_LOG_ERROR
,
2854 "DNS Service Discovery registration error %d!",
2859 #endif /* HAVE_DNSSD */
2864 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2867 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2868 get_hostconfig(const char *name
) /* I - Name of service */
2870 cups_file_t
*fp
; /* Hostconfig file */
2871 char line
[1024], /* Line from file */
2872 *ptr
; /* Pointer to value */
2873 int state
= 1; /* State of service */
2877 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2878 * the service is enabled/auto.
2881 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2884 * Read lines from the file until we find the service...
2887 while (cupsFileGets(fp
, line
, sizeof(line
)))
2889 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2894 if (!strcasecmp(line
, name
))
2897 * Found the service, see if it is set to "-NO-"...
2900 if (!strncasecmp(ptr
, "-NO-", 4))
2911 #endif /* __APPLE__ */
2915 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2918 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2919 is_local_queue(const char *uri
, /* I - Printer URI */
2920 char *host
, /* O - Host string */
2921 int hostlen
, /* I - Length of host buffer */
2922 char *resource
, /* O - Resource string */
2923 int resourcelen
) /* I - Length of resource buffer */
2925 char scheme
[32], /* Scheme portion of URI */
2926 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2927 int port
; /* Port portion of URI */
2928 cupsd_netif_t
*iface
; /* Network interface */
2932 * Pull the URI apart to see if this is a local or remote printer...
2935 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2936 username
, sizeof(username
), host
, hostlen
, &port
,
2937 resource
, resourcelen
) < HTTP_URI_OK
)
2940 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2943 * Check for local server addresses...
2946 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2951 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2953 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2954 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2958 * If we get here, the printer is remote...
2966 * 'process_browse_data()' - Process new browse data.
2970 process_browse_data(
2971 const char *uri
, /* I - URI of printer/class */
2972 const char *host
, /* I - Hostname */
2973 const char *resource
, /* I - Resource path */
2974 cups_ptype_t type
, /* I - Printer type */
2975 ipp_pstate_t state
, /* I - Printer state */
2976 const char *location
, /* I - Printer location */
2977 const char *info
, /* I - Printer information */
2978 const char *make_model
, /* I - Printer make and model */
2979 int num_attrs
, /* I - Number of attributes */
2980 cups_option_t
*attrs
) /* I - Attributes */
2982 int i
; /* Looping var */
2983 int update
; /* Update printer attributes? */
2984 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2985 name
[IPP_MAX_NAME
], /* Name of printer */
2986 newname
[IPP_MAX_NAME
], /* New name of printer */
2987 *hptr
, /* Pointer into hostname */
2988 *sptr
; /* Pointer into ServerName */
2989 const char *shortname
; /* Short queue name (queue) */
2990 char local_make_model
[IPP_MAX_NAME
];
2991 /* Local make and model */
2992 cupsd_printer_t
*p
; /* Printer information */
2993 const char *ipp_options
, /* ipp-options value */
2994 *lease_duration
; /* lease-duration value */
2995 int is_class
; /* Is this queue a class? */
2998 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2999 "process_browse_data(uri=\"%s\", host=\"%s\", "
3000 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3001 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3002 uri
, host
, resource
, type
, state
,
3003 location
? location
: "(nil)", info
? info
: "(nil)",
3004 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3007 * Determine if the URI contains any illegal characters in it...
3010 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3011 (strncmp(resource
, "/printers/", 10) &&
3012 strncmp(resource
, "/classes/", 9)))
3014 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3018 if (strchr(resource
, '?') ||
3019 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3020 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3022 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3028 * OK, this isn't a local printer; add any remote options...
3031 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3033 if (BrowseRemoteOptions
)
3035 if (BrowseRemoteOptions
[0] == '?')
3038 * Override server-supplied options...
3041 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3043 else if (ipp_options
)
3046 * Combine the server and local options...
3049 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3050 BrowseRemoteOptions
);
3055 * Just use the local options...
3058 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3063 else if (ipp_options
)
3066 * Just use the server-supplied options...
3069 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3074 * See if we already have it listed in the Printers list, and add it if not...
3077 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3078 type
&= ~CUPS_PRINTER_IMPLICIT
;
3080 hptr
= strchr(host
, '.');
3081 sptr
= strchr(ServerName
, '.');
3082 is_class
= type
& CUPS_PRINTER_CLASS
;
3084 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3087 * Strip the common domain name components...
3090 while (hptr
!= NULL
)
3092 if (!strcasecmp(hptr
, sptr
))
3098 hptr
= strchr(hptr
+ 1, '.');
3105 * Remote destination is a class...
3108 if (!strncmp(resource
, "/classes/", 9))
3109 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3113 shortname
= resource
+ 9;
3118 * Remote destination is a printer...
3121 if (!strncmp(resource
, "/printers/", 10))
3122 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3126 shortname
= resource
+ 10;
3130 *hptr
= '.'; /* Resource FQDN */
3132 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3135 * Long name doesn't exist, try short name...
3138 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3141 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3144 * Short name doesn't exist, use it for this shared queue.
3147 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3149 strlcpy(name
, shortname
, sizeof(name
));
3154 * Short name exists...
3157 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3158 "process_browse_data: %s found, type=%x, hostname=%s...",
3159 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3161 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3162 p
= NULL
; /* Don't replace implicit classes */
3163 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3166 * Short name exists but is for a different host. If this is a remote
3167 * queue, rename it and use the long name...
3170 if (p
->type
& CUPS_PRINTER_REMOTE
)
3172 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3173 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3174 is_class
? "class" : "printer", p
->name
, p
->name
,
3176 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3177 "%s \'%s\' deleted by directory services.",
3178 is_class
? "Class" : "Printer", p
->name
);
3180 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3181 cupsdRenamePrinter(p
, newname
);
3183 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3184 "%s \'%s\' added by directory services.",
3185 is_class
? "Class" : "Printer", p
->name
);
3189 * Force creation with long name...
3197 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3198 "process_browse_data: %s found, type=%x, hostname=%s...",
3199 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3204 * Queue doesn't exist; add it...
3208 p
= cupsdAddClass(name
);
3210 p
= cupsdAddPrinter(name
);
3215 cupsdClearString(&(p
->hostname
));
3217 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3218 is_class
? "class" : "printer", name
);
3220 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3221 "%s \'%s\' added by directory services.",
3222 is_class
? "Class" : "Printer", name
);
3225 * Force the URI to point to the real server...
3228 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3231 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3237 * Hostname not set, so this must be a cached remote printer
3238 * that was created for a pending print job...
3241 cupsdSetString(&p
->hostname
, host
);
3242 cupsdSetString(&p
->uri
, uri
);
3243 cupsdSetString(&p
->device_uri
, uri
);
3246 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3250 * Update the state...
3254 p
->browse_time
= time(NULL
);
3256 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3260 * Grab the lease-duration for the browse data; anything less then 1
3261 * second or more than 1 week gets the default BrowseTimeout...
3264 i
= atoi(lease_duration
);
3265 if (i
< 1 || i
> 604800)
3268 p
->browse_expire
= p
->browse_time
+ i
;
3271 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3273 if (type
& CUPS_PRINTER_REJECTING
)
3275 type
&= ~CUPS_PRINTER_REJECTING
;
3283 else if (!p
->accepting
)
3289 if (p
->type
!= type
)
3295 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3297 cupsdSetString(&p
->location
, location
);
3301 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3303 cupsdSetString(&p
->info
, info
);
3306 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3309 if (!make_model
|| !make_model
[0])
3312 snprintf(local_make_model
, sizeof(local_make_model
),
3313 "Remote Class on %s", host
);
3315 snprintf(local_make_model
, sizeof(local_make_model
),
3316 "Remote Printer on %s", host
);
3319 snprintf(local_make_model
, sizeof(local_make_model
),
3320 "%s on %s", make_model
, host
);
3322 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3324 cupsdSetString(&p
->make_model
, local_make_model
);
3330 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3333 * See if we need to update the attributes...
3336 if (p
->num_options
!= num_attrs
)
3340 for (i
= 0; i
< num_attrs
; i
++)
3341 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3342 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3343 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3352 * Free the old options...
3355 cupsFreeOptions(p
->num_options
, p
->options
);
3358 p
->num_options
= num_attrs
;
3361 if (type
& CUPS_PRINTER_DELETE
)
3363 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3364 "%s \'%s\' deleted by directory services.",
3365 is_class
? "Class" : "Printer", p
->name
);
3367 cupsdExpireSubscriptions(p
, NULL
);
3369 cupsdDeletePrinter(p
, 1);
3370 cupsdUpdateImplicitClasses();
3371 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3375 cupsdSetPrinterAttrs(p
);
3376 cupsdUpdateImplicitClasses();
3380 * See if we have a default printer... If not, make the first network
3381 * default printer the default.
3384 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3387 * Find the first network default printer and use it...
3390 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3392 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3393 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3396 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3402 * Do auto-classing if needed...
3405 process_implicit_classes();
3410 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3414 process_implicit_classes(void)
3416 int i
; /* Looping var */
3417 int update
; /* Update printer attributes? */
3418 char name
[IPP_MAX_NAME
], /* Name of printer */
3419 *hptr
; /* Pointer into hostname */
3420 cupsd_printer_t
*p
, /* Printer information */
3421 *pclass
, /* Printer class */
3422 *first
; /* First printer in class */
3423 int offset
, /* Offset of name */
3424 len
; /* Length of name */
3427 if (!ImplicitClasses
|| !Printers
)
3431 * Loop through all available printers and create classes as needed...
3434 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3435 update
= 0, pclass
= NULL
, first
= NULL
;
3437 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3440 * Skip implicit classes...
3443 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3450 * If len == 0, get the length of this printer name up to the "@"
3454 cupsArraySave(Printers
);
3457 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3458 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3461 * We have more than one printer with the same name; see if
3462 * we have a class, and if this printer is a member...
3465 if (pclass
&& strcasecmp(pclass
->name
, name
))
3468 cupsdSetPrinterAttrs(pclass
);
3474 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3477 * Need to add the class...
3480 pclass
= cupsdAddPrinter(name
);
3481 cupsArrayAdd(ImplicitPrinters
, pclass
);
3483 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3484 pclass
->accepting
= 1;
3485 pclass
->state
= IPP_PRINTER_IDLE
;
3487 cupsdSetString(&pclass
->location
, p
->location
);
3488 cupsdSetString(&pclass
->info
, p
->info
);
3490 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3491 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3495 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3497 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3499 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3500 "Implicit class \'%s\' added by directory services.",
3506 for (i
= 0; i
< pclass
->num_printers
; i
++)
3507 if (pclass
->printers
[i
] == first
)
3510 if (i
>= pclass
->num_printers
)
3512 first
->in_implicit_class
= 1;
3513 cupsdAddPrinterToClass(pclass
, first
);
3519 for (i
= 0; i
< pclass
->num_printers
; i
++)
3520 if (pclass
->printers
[i
] == p
)
3523 if (i
>= pclass
->num_printers
)
3525 p
->in_implicit_class
= 1;
3526 cupsdAddPrinterToClass(pclass
, p
);
3533 * First time around; just get name length and mark it as first
3537 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3538 len
= hptr
- p
->name
;
3540 len
= strlen(p
->name
);
3542 if (len
>= sizeof(name
))
3545 * If the printer name length somehow is greater than we normally allow,
3546 * skip this printer...
3550 cupsArrayRestore(Printers
);
3554 strncpy(name
, p
->name
, len
);
3558 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3559 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3562 * Can't use same name as a local printer; add "Any" to the
3563 * front of the name, unless we have explicitly disabled
3564 * the "ImplicitAnyClasses"...
3567 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3570 * Add "Any" to the class name...
3573 strcpy(name
, "Any");
3574 strncpy(name
+ 3, p
->name
, len
);
3575 name
[len
+ 3] = '\0';
3581 * Don't create an implicit class if we have a local printer
3582 * with the same name...
3586 cupsArrayRestore(Printers
);
3594 cupsArrayRestore(Printers
);
3598 * Update the last printer class as needed...
3601 if (pclass
&& update
)
3602 cupsdSetPrinterAttrs(pclass
);
3607 * 'send_cups_browse()' - Send new browsing information using the CUPS
3612 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3614 int i
; /* Looping var */
3615 cups_ptype_t type
; /* Printer type */
3616 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3617 int bytes
; /* Length of packet */
3618 char packet
[1453], /* Browse data packet */
3619 uri
[1024], /* Printer URI */
3620 location
[1024], /* printer-location */
3621 info
[1024], /* printer-info */
3623 /* printer-make-and-model */
3624 cupsd_netif_t
*iface
; /* Network interface */
3628 * Figure out the printer type value...
3631 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3634 type
|= CUPS_PRINTER_REJECTING
;
3636 if (p
== DefaultPrinter
)
3637 type
|= CUPS_PRINTER_DEFAULT
;
3640 * Remove quotes from printer-info, printer-location, and
3641 * printer-make-and-model attributes...
3644 dequote(location
, p
->location
, sizeof(location
));
3645 dequote(info
, p
->info
, sizeof(info
));
3648 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3649 else if (p
->type
& CUPS_PRINTER_CLASS
)
3651 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3652 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3654 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3657 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3659 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3662 * Send a packet to each browse address...
3665 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3669 * Send the browse packet to one or more interfaces...
3672 if (!strcmp(b
->iface
, "*"))
3675 * Send to all local interfaces...
3680 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3682 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3685 * Only send to local, IPv4 interfaces...
3688 if (!iface
->is_local
|| !iface
->port
||
3689 iface
->address
.addr
.sa_family
!= AF_INET
)
3692 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3693 iface
->hostname
, iface
->port
,
3694 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3697 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3698 type
, p
->state
, uri
, location
, info
, make_model
,
3699 p
->browse_attrs
? p
->browse_attrs
: "");
3701 bytes
= strlen(packet
);
3703 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3704 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3705 iface
->name
, packet
);
3707 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3709 sendto(BrowseSocket
, packet
, bytes
, 0,
3710 (struct sockaddr
*)&(iface
->broadcast
),
3711 httpAddrLength(&(iface
->broadcast
)));
3714 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3717 * Send to the named interface using the IPv4 address...
3721 if (strcmp(b
->iface
, iface
->name
))
3726 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3729 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3733 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3734 iface
->hostname
, iface
->port
,
3735 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3738 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3739 type
, p
->state
, uri
, location
, info
, make_model
,
3740 p
->browse_attrs
? p
->browse_attrs
: "");
3742 bytes
= strlen(packet
);
3744 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3745 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3746 iface
->name
, packet
);
3748 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3750 sendto(BrowseSocket
, packet
, bytes
, 0,
3751 (struct sockaddr
*)&(iface
->broadcast
),
3752 httpAddrLength(&(iface
->broadcast
)));
3759 * Send the browse packet to the indicated address using
3760 * the default server name...
3763 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3764 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3765 p
->browse_attrs
? p
->browse_attrs
: "");
3767 bytes
= strlen(packet
);
3768 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3769 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3771 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3772 (struct sockaddr
*)&(b
->to
),
3773 httpAddrLength(&(b
->to
))) <= 0)
3776 * Unable to send browse packet, so remove this address from the
3780 cupsdLogMessage(CUPSD_LOG_ERROR
,
3781 "cupsdSendBrowseList: sendto failed for browser "
3783 (int)(b
- Browsers
+ 1), strerror(errno
));
3786 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3797 * 'ldap_search_rec()' - LDAP Search with reconnect
3801 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3802 char *base
, /* I - Base dn */
3803 int scope
, /* I - LDAP search scope */
3804 char *filter
, /* I - Filter string */
3805 char *attrs
[], /* I - Requested attributes */
3806 int attrsonly
, /* I - Return only attributes? */
3807 LDAPMessage
**res
) /* I - LDAP handler */
3809 int rc
; /* Return code */
3812 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3813 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3814 NULL
, LDAP_NO_LIMIT
, res
);
3816 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3817 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3820 * If we have a connection problem try again...
3823 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3825 cupsdLogMessage(CUPSD_LOG_ERROR
,
3826 "LDAP search failed with status %d: %s",
3827 rc
, ldap_err2string(rc
));
3828 cupsdLogMessage(CUPSD_LOG_INFO
,
3829 "We try the LDAP search once again after reconnecting to "
3834 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3835 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3836 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3838 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3839 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3842 if (rc
== LDAP_NO_SUCH_OBJECT
)
3843 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3844 "ldap_search_rec: LDAP entry/object not found");
3845 else if (rc
!= LDAP_SUCCESS
)
3846 cupsdLogMessage(CUPSD_LOG_ERROR
,
3847 "ldap_search_rec: LDAP search failed with status %d: %s",
3848 rc
, ldap_err2string(rc
));
3850 if (rc
!= LDAP_SUCCESS
)
3858 * 'ldap_freeres()' - Free LDAPMessage
3862 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3864 int rc
; /* Return value */
3867 rc
= ldap_msgfree(entry
);
3869 cupsdLogMessage(CUPSD_LOG_WARN
,
3870 "Can't free LDAPMessage!");
3872 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3873 "Freeing LDAPMessage was unnecessary");
3878 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3882 ldap_getval_firststring(
3883 LDAP
*ld
, /* I - LDAP handler */
3884 LDAPMessage
*entry
, /* I - LDAP message or search result */
3885 char *attr
, /* I - the wanted attribute */
3886 char *retval
, /* O - String to return */
3887 unsigned long maxsize
) /* I - Max string size */
3889 char *dn
; /* LDAP DN */
3890 int rc
= 0; /* Return code */
3891 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3892 struct berval
**bval
; /* LDAP value array */
3893 unsigned long size
; /* String size */
3897 * Get value from LDAPMessage...
3900 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3903 dn
= ldap_get_dn(ld
, entry
);
3904 cupsdLogMessage(CUPSD_LOG_WARN
,
3905 "Failed to get LDAP value %s for %s!",
3913 * Check size and copy value into our string...
3917 if (size
< bval
[0]->bv_len
)
3920 dn
= ldap_get_dn(ld
, entry
);
3921 cupsdLogMessage(CUPSD_LOG_WARN
,
3922 "Attribute %s is too big! (dn: %s)",
3927 size
= bval
[0]->bv_len
;
3929 strlcpy(retval
, bval
[0]->bv_val
, size
);
3930 ldap_value_free_len(bval
);
3933 char **value
; /* LDAP value */
3936 * Get value from LDAPMessage...
3939 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3942 dn
= ldap_get_dn(ld
, entry
);
3943 cupsdLogMessage(CUPSD_LOG_WARN
,
3944 "Failed to get LDAP value %s for %s!",
3950 strlcpy(retval
, *value
, maxsize
);
3951 ldap_value_free(value
);
3953 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3960 * 'send_ldap_ou()' - Send LDAP ou registrations.
3964 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3965 char *basedn
, /* I - Our base dn */
3966 char *descstring
) /* I - Description for ou */
3968 int i
; /* Looping var... */
3969 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3970 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3971 LDAPMessage
*res
, /* Search result token */
3972 *e
; /* Current entry from search */
3973 int rc
; /* LDAP status */
3974 char dn
[1024], /* DN of the organizational unit we are adding */
3975 *desc
[2], /* Change records */
3977 char old_desc
[1024]; /* Old description */
3978 static const char * const objectClass_values
[] =
3979 { /* The 2 objectClass's we use in */
3980 "top", /* our LDAP entries */
3981 "organizationalUnit",
3984 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3991 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3994 * Reconnect if LDAP Handle is invalid...
3997 if (! BrowseLDAPHandle
)
3999 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4000 "send_ldap_ou: LDAP Handle is invalid. Try "
4007 * Prepare ldap search...
4010 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4011 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4015 desc
[0] = descstring
;
4018 mods
[0].mod_type
= "ou";
4019 mods
[0].mod_values
= ou_value
;
4020 mods
[1].mod_type
= "description";
4021 mods
[1].mod_values
= desc
;
4022 mods
[2].mod_type
= "objectClass";
4023 mods
[2].mod_values
= (char **)objectClass_values
;
4025 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4026 (char **)ou_attrs
, 0, &res
);
4029 * If ldap search was not successfull then exit function...
4032 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4036 * Check if we need to insert or update the LDAP entry...
4039 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4040 rc
!= LDAP_NO_SUCH_OBJECT
)
4043 * Printserver has already been registered, check if
4044 * modification is required...
4047 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4050 * Get the required values from this entry...
4053 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4054 sizeof(old_desc
)) == -1)
4058 * Check if modification is required...
4061 if ( strcmp(desc
[0], old_desc
) == 0 )
4064 * LDAP entry for the printer exists.
4065 * Printer has already been registered,
4066 * no modifications required...
4068 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4069 "send_ldap_ou: No updates required for %s", ou
);
4074 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4075 "send_ldap_ou: Replace entry for %s", ou
);
4077 for (i
= 0; i
< 3; i
++)
4079 pmods
[i
] = mods
+ i
;
4080 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4084 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4085 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4086 NULL
)) != LDAP_SUCCESS
)
4088 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4089 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4091 cupsdLogMessage(CUPSD_LOG_ERROR
,
4092 "LDAP modify for %s failed with status %d: %s",
4093 ou
, rc
, ldap_err2string(rc
));
4094 if ( LDAP_SERVER_DOWN
== rc
)
4102 * Printserver has never been registered,
4103 * add registration...
4106 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4107 "send_ldap_ou: Add entry for %s", ou
);
4109 for (i
= 0; i
< 3; i
++)
4111 pmods
[i
] = mods
+ i
;
4112 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4116 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4117 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4118 NULL
)) != LDAP_SUCCESS
)
4120 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4121 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4123 cupsdLogMessage(CUPSD_LOG_ERROR
,
4124 "LDAP add for %s failed with status %d: %s",
4125 ou
, rc
, ldap_err2string(rc
));
4126 if ( LDAP_SERVER_DOWN
== rc
)
4136 * 'send_ldap_browse()' - Send LDAP printer registrations.
4140 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4142 int i
; /* Looping var... */
4143 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4144 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4145 LDAPMessage
*res
, /* Search result token */
4146 *e
; /* Current entry from search */
4147 char *cn_value
[2], /* Change records */
4153 typestring
[255], /* String to hold printer-type */
4154 dn
[1024]; /* DN of the printer we are adding */
4155 int rc
; /* LDAP status */
4156 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4157 old_location
[1024], /* Printer location */
4158 old_info
[1024], /* Printer information */
4159 old_make_model
[1024], /* Printer make and model */
4160 old_type_string
[30]; /* Temporary type number */
4161 int old_type
; /* Printer type */
4162 static const char * const objectClass_values
[] =
4163 { /* The 3 objectClass's we use in */
4164 "top", /* our LDAP entries */
4171 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4174 * Exit function if LDAP updates has been disabled...
4177 if (!BrowseLDAPUpdate
)
4179 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4180 "send_ldap_browse: Updates temporary disabled; "
4186 * Reconnect if LDAP Handle is invalid...
4189 if (!BrowseLDAPHandle
)
4191 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4192 "send_ldap_browse: LDAP Handle is invalid. Try "
4199 * Everything in ldap is ** so we fudge around it...
4202 sprintf(typestring
, "%u", p
->type
);
4204 cn_value
[0] = p
->name
;
4206 info
[0] = p
->info
? p
->info
: "Unknown";
4208 location
[0] = p
->location
? p
->location
: "Unknown";
4210 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4211 make_model
[1] = NULL
;
4212 type
[0] = typestring
;
4218 * Get ldap entry for printer ...
4221 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4223 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4225 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4226 (char **)ldap_attrs
, 0, &res
);
4229 * If ldap search was not successfull then exit function
4230 * and temporary disable LDAP updates...
4233 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4235 if (BrowseLDAPUpdate
&&
4236 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4238 BrowseLDAPUpdate
= FALSE
;
4239 cupsdLogMessage(CUPSD_LOG_INFO
,
4240 "LDAP update temporary disabled");
4247 * Fill modification array...
4250 mods
[0].mod_type
= "cn";
4251 mods
[0].mod_values
= cn_value
;
4252 mods
[1].mod_type
= "printerDescription";
4253 mods
[1].mod_values
= info
;
4254 mods
[2].mod_type
= "printerURI";
4255 mods
[2].mod_values
= uri
;
4256 mods
[3].mod_type
= "printerLocation";
4257 mods
[3].mod_values
= location
;
4258 mods
[4].mod_type
= "printerMakeAndModel";
4259 mods
[4].mod_values
= make_model
;
4260 mods
[5].mod_type
= "printerType";
4261 mods
[5].mod_values
= type
;
4262 mods
[6].mod_type
= "objectClass";
4263 mods
[6].mod_values
= (char **)objectClass_values
;
4266 * Check if we need to insert or update the LDAP entry...
4269 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4270 rc
!= LDAP_NO_SUCH_OBJECT
)
4273 * Printer has already been registered, check if
4274 * modification is required...
4277 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4280 * Get the required values from this entry...
4283 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4284 old_info
, sizeof(old_info
)) == -1)
4287 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4288 old_location
, sizeof(old_location
)) == -1)
4291 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4292 old_make_model
, sizeof(old_make_model
)) == -1)
4295 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4296 old_type_string
, sizeof(old_type_string
)) == -1)
4299 old_type
= atoi(old_type_string
);
4301 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4302 sizeof(old_uri
)) == -1)
4306 * Check if modification is required...
4309 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4310 !strcmp(location
[0], old_location
) &&
4311 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4314 * LDAP entry for the printer exists. Printer has already been registered,
4315 * no modifications required...
4318 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4319 "send_ldap_browse: No updates required for %s", p
->name
);
4324 * LDAP entry for the printer exists. Printer has already been registered,
4325 * modify the current registration...
4328 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4329 "send_ldap_browse: Replace entry for %s", p
->name
);
4331 for (i
= 0; i
< 7; i
++)
4333 pmods
[i
] = mods
+ i
;
4334 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4338 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4339 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4340 NULL
)) != LDAP_SUCCESS
)
4342 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4343 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4345 cupsdLogMessage(CUPSD_LOG_ERROR
,
4346 "LDAP modify for %s failed with status %d: %s",
4347 p
->name
, rc
, ldap_err2string(rc
));
4348 if (rc
== LDAP_SERVER_DOWN
)
4356 * No LDAP entry exists for the printer. Printer has never been registered,
4357 * add the current registration...
4360 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4362 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4363 "send_ldap_browse: Add entry for %s", p
->name
);
4365 for (i
= 0; i
< 7; i
++)
4367 pmods
[i
] = mods
+ i
;
4368 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4372 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4373 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4374 NULL
)) != LDAP_SUCCESS
)
4376 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4377 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4379 cupsdLogMessage(CUPSD_LOG_ERROR
,
4380 "LDAP add for %s failed with status %d: %s",
4381 p
->name
, rc
, ldap_err2string(rc
));
4382 if (rc
== LDAP_SERVER_DOWN
)
4392 * 'ldap_dereg_printer()' - Delete printer from directory
4396 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4398 char dn
[1024]; /* DN of the printer */
4399 int rc
; /* LDAP status */
4402 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4406 * Reconnect if LDAP Handle is invalid...
4409 if (!BrowseLDAPHandle
)
4416 * Get dn for printer and delete LDAP entry...
4419 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4421 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4423 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4424 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4425 NULL
)) != LDAP_SUCCESS
)
4427 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4428 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4430 cupsdLogMessage(CUPSD_LOG_WARN
,
4431 "LDAP delete for %s failed with status %d: %s",
4432 p
->name
, rc
, ldap_err2string(rc
));
4435 * If we had a connection problem (connection timed out, etc.)
4436 * we should reconnect and try again to delete the entry...
4439 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4441 cupsdLogMessage(CUPSD_LOG_INFO
,
4442 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4445 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4446 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4447 NULL
)) != LDAP_SUCCESS
)
4449 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4450 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4451 cupsdLogMessage(CUPSD_LOG_WARN
,
4452 "LDAP delete for %s failed with status %d: %s",
4453 p
->name
, rc
, ldap_err2string(rc
));
4460 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4461 char *basedn
) /* I - Dase dn */
4463 char dn
[1024]; /* DN of the printer */
4464 int rc
; /* LDAP status */
4467 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4470 * Reconnect if LDAP Handle is invalid...
4473 if (!BrowseLDAPHandle
)
4480 * Get dn for printer and delete LDAP entry...
4483 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4484 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4486 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4487 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4488 NULL
)) != LDAP_SUCCESS
)
4490 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4491 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4493 cupsdLogMessage(CUPSD_LOG_WARN
,
4494 "LDAP delete for %s failed with status %d: %s",
4495 ou
, rc
, ldap_err2string(rc
));
4498 * If we had a connection problem (connection timed out, etc.)
4499 * we should reconnect and try again to delete the entry...
4502 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4504 cupsdLogMessage(CUPSD_LOG_INFO
,
4505 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4507 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4508 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4509 NULL
)) != LDAP_SUCCESS
)
4511 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4512 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4513 cupsdLogMessage(CUPSD_LOG_WARN
,
4514 "LDAP delete for %s failed with status %d: %s",
4515 ou
, rc
, ldap_err2string(rc
));
4520 #endif /* HAVE_LDAP */
4525 * 'send_slp_browse()' - Register the specified printer with SLP.
4529 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4531 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4532 attrs
[8192], /* Printer attributes */
4533 finishings
[1024], /* Finishings to support */
4534 make_model
[IPP_MAX_NAME
* 2],
4535 /* Make and model, quoted */
4536 location
[IPP_MAX_NAME
* 2],
4537 /* Location, quoted */
4538 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4539 *src
, /* Pointer to original string */
4540 *dst
; /* Pointer to destination string */
4541 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4542 SLPError error
; /* SLP error, if any */
4545 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4549 * Make the SLP service URL that conforms to the IANA
4550 * 'printer:' template.
4553 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4555 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4558 * Figure out the finishings string...
4561 if (p
->type
& CUPS_PRINTER_STAPLE
)
4562 strcpy(finishings
, "staple");
4564 finishings
[0] = '\0';
4566 if (p
->type
& CUPS_PRINTER_BIND
)
4569 strlcat(finishings
, ",bind", sizeof(finishings
));
4571 strcpy(finishings
, "bind");
4574 if (p
->type
& CUPS_PRINTER_PUNCH
)
4577 strlcat(finishings
, ",punch", sizeof(finishings
));
4579 strcpy(finishings
, "punch");
4582 if (p
->type
& CUPS_PRINTER_COVER
)
4585 strlcat(finishings
, ",cover", sizeof(finishings
));
4587 strcpy(finishings
, "cover");
4590 if (p
->type
& CUPS_PRINTER_SORT
)
4593 strlcat(finishings
, ",sort", sizeof(finishings
));
4595 strcpy(finishings
, "sort");
4599 strcpy(finishings
, "none");
4602 * Quote any commas in the make and model, location, and info strings...
4605 for (src
= p
->make_model
, dst
= make_model
;
4606 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4608 if (*src
== ',' || *src
== '\\' || *src
== ')')
4617 strcpy(make_model
, "Unknown");
4619 for (src
= p
->location
, dst
= location
;
4620 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4622 if (*src
== ',' || *src
== '\\' || *src
== ')')
4631 strcpy(location
, "Unknown");
4633 for (src
= p
->info
, dst
= info
;
4634 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4636 if (*src
== ',' || *src
== '\\' || *src
== ')')
4645 strcpy(info
, "Unknown");
4648 * Get the authentication value...
4651 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4655 * Make the SLP attribute string list that conforms to
4656 * the IANA 'printer:' template.
4659 snprintf(attrs
, sizeof(attrs
),
4660 "(printer-uri-supported=%s),"
4661 "(uri-authentication-supported=%s>),"
4663 "(uri-security-supported=tls>),"
4665 "(uri-security-supported=none>),"
4666 #endif /* HAVE_SSL */
4667 "(printer-name=%s),"
4668 "(printer-location=%s),"
4669 "(printer-info=%s),"
4670 "(printer-more-info=%s),"
4671 "(printer-make-and-model=%s),"
4672 "(printer-type=%d),"
4673 "(charset-supported=utf-8),"
4674 "(natural-language-configured=%s),"
4675 "(natural-language-supported=de,en,es,fr,it),"
4676 "(color-supported=%s),"
4677 "(finishings-supported=%s),"
4678 "(sides-supported=one-sided%s),"
4679 "(multiple-document-jobs-supported=true)"
4680 "(ipp-versions-supported=1.0,1.1)",
4681 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4682 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4683 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4685 p
->type
& CUPS_PRINTER_DUPLEX
?
4686 ",two-sided-long-edge,two-sided-short-edge" : "");
4688 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4691 * Register the printer with the SLP server...
4694 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4695 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4697 if (error
!= SLP_OK
)
4698 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4704 * 'slp_attr_callback()' - SLP attribute callback
4707 static SLPBoolean
/* O - SLP_TRUE for success */
4709 SLPHandle hslp
, /* I - SLP handle */
4710 const char *attrlist
, /* I - Attribute list */
4711 SLPError errcode
, /* I - Parsing status for this attr */
4712 void *cookie
) /* I - Current printer */
4714 char *tmp
= 0; /* Temporary string */
4715 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4716 /* Current printer */
4719 (void)hslp
; /* anti-compiler-warning-code */
4722 * Bail if there was an error
4725 if (errcode
!= SLP_OK
)
4729 * Parse the attrlist to obtain things needed to build CUPS browse packet
4732 memset(p
, 0, sizeof(cupsd_printer_t
));
4734 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4736 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4738 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4740 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4741 p
->type
= atoi(tmp
);
4743 p
->type
= CUPS_PRINTER_REMOTE
;
4745 cupsdClearString(&tmp
);
4752 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4756 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4758 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4761 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4763 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4766 * Make the SLP service URL that conforms to the IANA
4767 * 'printer:' template.
4770 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4773 * Deregister the printer...
4776 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4782 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4785 static int /* O - 0 on success */
4786 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4787 const char *tag
, /* I - Name of attribute */
4788 char **valbuf
) /* O - Value */
4790 char *ptr1
, /* Pointer into string */
4794 cupsdClearString(valbuf
);
4796 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4798 ptr1
+= strlen(tag
);
4800 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4806 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4807 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4810 * Dequote the value...
4813 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4814 if (*ptr1
== '\\' && ptr1
[1])
4815 _cups_strcpy(ptr1
, ptr1
+ 1);
4826 * 'slp_reg_callback()' - Empty SLPRegReport.
4830 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4831 SLPError errcode
, /* I - Error code, if any */
4832 void *cookie
) /* I - App data */
4843 * 'slp_url_callback()' - SLP service url callback
4846 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4848 SLPHandle hslp
, /* I - SLP handle */
4849 const char *srvurl
, /* I - URL of service */
4850 unsigned short lifetime
, /* I - Life of service */
4851 SLPError errcode
, /* I - Existing error code */
4852 void *cookie
) /* I - Pointer to service list */
4854 slpsrvurl_t
*s
, /* New service entry */
4855 **head
; /* Pointer to head of entry */
4859 * Let the compiler know we won't be using these vars...
4866 * Bail if there was an error
4869 if (errcode
!= SLP_OK
)
4873 * Grab the head of the list...
4876 head
= (slpsrvurl_t
**)cookie
;
4879 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4882 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4886 * Copy the SLP service URL...
4889 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4892 * Link the SLP service URL into the head of the list
4902 #endif /* HAVE_LIBSLP */
4906 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4910 update_cups_browse(void)
4912 int i
; /* Looping var */
4913 int auth
; /* Authorization status */
4914 int len
; /* Length of name string */
4915 int bytes
; /* Number of bytes left */
4916 char packet
[1541], /* Broadcast packet */
4917 *pptr
; /* Pointer into packet */
4918 socklen_t srclen
; /* Length of source address */
4919 http_addr_t srcaddr
; /* Source address */
4920 char srcname
[1024]; /* Source hostname */
4921 unsigned address
[4]; /* Source address */
4922 unsigned type
; /* Printer type */
4923 unsigned state
; /* Printer state */
4924 char uri
[HTTP_MAX_URI
], /* Printer URI */
4925 host
[HTTP_MAX_URI
], /* Host portion of URI */
4926 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4927 info
[IPP_MAX_NAME
], /* Information string */
4928 location
[IPP_MAX_NAME
], /* Location string */
4929 make_model
[IPP_MAX_NAME
];/* Make and model string */
4930 int num_attrs
; /* Number of attributes */
4931 cups_option_t
*attrs
; /* Attributes */
4935 * Read a packet from the browse socket...
4938 srclen
= sizeof(srcaddr
);
4939 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4940 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4943 * "Connection refused" is returned under Linux if the destination port
4944 * or address is unreachable from a previous sendto(); check for the
4945 * error here and ignore it for now...
4948 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4950 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4952 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4955 closesocket(BrowseSocket
);
4957 close(BrowseSocket
);
4960 cupsdRemoveSelect(BrowseSocket
);
4963 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4964 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4970 packet
[bytes
] = '\0';
4973 * If we're about to sleep, ignore incoming browse packets.
4980 * Figure out where it came from...
4984 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4986 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4987 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4988 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4989 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4992 #endif /* AF_INET6 */
4997 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5000 if (HostNameLookups
)
5001 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5003 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5005 len
= strlen(srcname
);
5013 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5016 * Access from localhost (127.0.0.1) is always allowed...
5019 auth
= CUPSD_AUTH_ALLOW
;
5024 * Do authorization checks on the domain/address...
5027 switch (BrowseACL
->order_type
)
5030 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5033 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5034 auth
= CUPSD_AUTH_ALLOW
;
5036 if (cupsdCheckAuth(address
, srcname
, len
,
5037 BrowseACL
->num_deny
, BrowseACL
->deny
))
5038 auth
= CUPSD_AUTH_DENY
;
5040 if (cupsdCheckAuth(address
, srcname
, len
,
5041 BrowseACL
->num_allow
, BrowseACL
->allow
))
5042 auth
= CUPSD_AUTH_ALLOW
;
5045 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5046 auth
= CUPSD_AUTH_DENY
;
5048 if (cupsdCheckAuth(address
, srcname
, len
,
5049 BrowseACL
->num_allow
, BrowseACL
->allow
))
5050 auth
= CUPSD_AUTH_ALLOW
;
5052 if (cupsdCheckAuth(address
, srcname
, len
,
5053 BrowseACL
->num_deny
, BrowseACL
->deny
))
5054 auth
= CUPSD_AUTH_DENY
;
5060 auth
= CUPSD_AUTH_ALLOW
;
5062 if (auth
== CUPSD_AUTH_DENY
)
5064 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5065 "update_cups_browse: Refused %d bytes from %s", bytes
,
5070 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5071 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5078 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5080 cupsdLogMessage(CUPSD_LOG_WARN
,
5081 "update_cups_browse: Garbled browse packet - %s", packet
);
5085 strcpy(location
, "Location Unknown");
5086 strcpy(info
, "No Information Available");
5087 make_model
[0] = '\0';
5091 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5094 * Have extended information; can't use sscanf for it because not all
5095 * sscanf's allow empty strings with %[^\"]...
5098 for (i
= 0, pptr
++;
5099 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5101 location
[i
] = *pptr
;
5109 while (*pptr
&& isspace(*pptr
& 255))
5114 for (i
= 0, pptr
++;
5115 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5124 while (*pptr
&& isspace(*pptr
& 255))
5129 for (i
= 0, pptr
++;
5130 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5132 make_model
[i
] = *pptr
;
5137 make_model
[i
] = '\0';
5140 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5146 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5147 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5148 type
, state
, uri
, location
, info
, make_model
));
5151 * Pull the URI apart to see if this is a local or remote printer...
5154 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5156 cupsFreeOptions(num_attrs
, attrs
);
5164 for (i
= 0; i
< NumRelays
; i
++)
5165 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5166 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5167 (struct sockaddr
*)&(Relays
[i
].to
),
5168 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5170 cupsdLogMessage(CUPSD_LOG_ERROR
,
5171 "update_cups_browse: sendto failed for relay %d - %s.",
5172 i
+ 1, strerror(errno
));
5173 cupsFreeOptions(num_attrs
, attrs
);
5178 * Process the browse data...
5181 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5182 (ipp_pstate_t
)state
, location
, info
, make_model
,
5188 * 'update_lpd()' - Update the LPD configuration as needed.
5192 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5199 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5200 * setting for backwards-compatibility.
5203 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5205 #endif /* __APPLE__ */
5207 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5210 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5213 char newfile
[1024]; /* New cups-lpd.N file */
5214 cups_file_t
*ofp
, /* Original file pointer */
5215 *nfp
; /* New file pointer */
5216 char line
[1024]; /* Line from file */
5219 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5221 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5223 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5224 LPDConfigFile
+ 9, strerror(errno
));
5228 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5230 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5231 newfile
, strerror(errno
));
5237 * Copy all of the lines from the cups-lpd file...
5240 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5244 cupsFilePrintf(nfp
, "%s\n", line
);
5245 snprintf(line
, sizeof(line
), "\tdisable = %s",
5246 onoff
? "no" : "yes");
5248 else if (!strstr(line
, "disable ="))
5249 cupsFilePrintf(nfp
, "%s\n", line
);
5254 rename(newfile
, LPDConfigFile
+ 9);
5257 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5260 * Enable/disable LPD via the launchctl command...
5263 char *argv
[5], /* Arguments for command */
5264 *envp
[MAX_ENV
]; /* Environment for command */
5265 int pid
; /* Process ID */
5268 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5269 argv
[0] = (char *)"launchctl";
5270 argv
[1] = (char *)(onoff
? "load" : "unload");
5271 argv
[2] = (char *)"-w";
5272 argv
[3] = LPDConfigFile
+ 10;
5275 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5278 #endif /* __APPLE__ */
5280 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5285 * 'update_polling()' - Read status messages from the poll daemons.
5289 update_polling(void)
5291 char *ptr
, /* Pointer to end of line in buffer */
5292 message
[1024]; /* Pointer to message text */
5293 int loglevel
; /* Log level for message */
5296 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5297 message
, sizeof(message
))) != NULL
)
5299 if (loglevel
== CUPSD_LOG_INFO
)
5300 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5302 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5306 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5309 * All polling processes have died; stop polling...
5312 cupsdLogMessage(CUPSD_LOG_ERROR
,
5313 "update_polling: all polling processes have exited!");
5320 * 'update_smb()' - Update the SMB configuration as needed.
5324 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5329 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5332 * Enable/disable SMB via the specified smb.conf config file...
5335 char newfile
[1024]; /* New smb.conf.N file */
5336 cups_file_t
*ofp
, /* Original file pointer */
5337 *nfp
; /* New file pointer */
5338 char line
[1024]; /* Line from file */
5339 int in_printers
; /* In [printers] section? */
5342 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5344 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5346 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5347 SMBConfigFile
+ 8, strerror(errno
));
5351 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5353 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5354 newfile
, strerror(errno
));
5360 * Copy all of the lines from the smb.conf file...
5365 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5367 if (in_printers
&& strstr(line
, "printable ="))
5368 snprintf(line
, sizeof(line
), " printable = %s",
5369 onoff
? "yes" : "no");
5371 cupsFilePrintf(nfp
, "%s\n", line
);
5374 in_printers
= !strcmp(line
, "[printers]");
5379 rename(newfile
, SMBConfigFile
+ 8);
5382 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5387 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".