2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
4 * Directory services routines for the CUPS scheduler.
6 * Copyright 2007-2011 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 * dnssdAddAlias() - Add a DNS-SD alias name.
42 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
43 * dnssdComparePrinters() - Compare the registered names of two printers.
44 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
46 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
48 * dnssdRegisterCallback() - DNSServiceRegister callback.
49 * dnssdRegisterPrinter() - Start sending broadcast information for a
50 * printer or update the broadcast contents.
51 * dnssdStop() - Stop all DNS-SD registrations.
52 * dnssdUpdate() - Handle DNS-SD queries.
53 * get_auth_info_required() - Get the auth-info-required value to advertise.
54 * get_hostconfig() - Get an /etc/hostconfig service setting.
55 * is_local_queue() - Determine whether the URI points at a local
57 * process_browse_data() - Process new browse data.
58 * process_implicit_classes() - Create/update implicit classes as needed.
59 * send_cups_browse() - Send new browsing information using the CUPS
61 * ldap_search_rec() - LDAP Search with reconnect
62 * ldap_freeres() - Free LDAPMessage
63 * ldap_getval_char() - Get first LDAP value and convert to string
64 * send_ldap_ou() - Send LDAP ou registrations.
65 * send_ldap_browse() - Send LDAP printer registrations.
66 * ldap_dereg_printer() - Delete printer from directory
67 * ldap_dereg_ou() - Remove the organizational unit.
68 * send_slp_browse() - Register the specified printer with SLP.
69 * slp_attr_callback() - SLP attribute callback
70 * slp_dereg_printer() - SLPDereg() the specified printer
71 * slp_get_attr() - Get an attribute from an SLP registration.
72 * slp_reg_callback() - Empty SLPRegReport.
73 * slp_url_callback() - SLP service url callback
74 * update_cups_browse() - Update the browse lists using the CUPS
76 * update_lpd() - Update the LPD configuration as needed.
77 * update_polling() - Read status messages from the poll daemons.
78 * update_smb() - Update the SMB configuration as needed.
82 * Include necessary headers...
92 # ifdef HAVE_COREFOUNDATION
93 # include <CoreFoundation/CoreFoundation.h>
94 # endif /* HAVE_COREFOUNDATION */
95 # ifdef HAVE_SYSTEMCONFIGURATION
96 # include <SystemConfiguration/SystemConfiguration.h>
97 # endif /* HAVE_SYSTEMCONFIGURATION */
98 # endif /* __APPLE__ */
99 #endif /* HAVE_DNSSD */
106 static char *dequote(char *d
, const char *s
, int dlen
);
107 static char *get_auth_info_required(cupsd_printer_t
*p
, char *buffer
,
110 static int get_hostconfig(const char *name
);
111 #endif /* __APPLE__ */
112 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
113 char *resource
, int resourcelen
);
114 static void process_browse_data(const char *uri
, const char *host
,
115 const char *resource
, cups_ptype_t type
,
116 ipp_pstate_t state
, const char *location
,
117 const char *info
, const char *make_model
,
118 int num_attrs
, cups_option_t
*attrs
);
119 static void process_implicit_classes(void);
120 static void send_cups_browse(cupsd_printer_t
*p
);
122 static LDAP
*ldap_connect(void);
123 static LDAP
*ldap_reconnect(void);
124 static void ldap_disconnect(LDAP
*ld
);
125 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
126 char *filter
, char *attrs
[],
127 int attrsonly
, LDAPMessage
**res
);
128 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
129 char *attr
, char *retval
,
130 unsigned long maxsize
);
131 static void ldap_freeres(LDAPMessage
*entry
);
132 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
133 static void send_ldap_browse(cupsd_printer_t
*p
);
134 static void ldap_dereg_printer(cupsd_printer_t
*p
);
135 static void ldap_dereg_ou(char *ou
, char *basedn
);
136 # ifdef HAVE_LDAP_REBIND_PROC
137 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
138 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
139 LDAP_CONST
char *refsp
,
144 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
150 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
151 # endif /* HAVE_LDAP_REBIND_PROC */
152 #endif /* HAVE_LDAP */
154 static void send_slp_browse(cupsd_printer_t
*p
);
155 #endif /* HAVE_LIBSLP */
156 static void update_cups_browse(void);
157 static void update_lpd(int onoff
);
158 static void update_polling(void);
159 static void update_smb(int onoff
);
163 # ifdef HAVE_COREFOUNDATION
164 static void dnssdAddAlias(const void *key
, const void *value
,
166 # endif /* HAVE_COREFOUNDATION */
167 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
169 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
170 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
171 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
173 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
174 DNSServiceFlags flags
,
175 DNSServiceErrorType errorCode
,
176 const char *name
, const char *regtype
,
177 const char *domain
, void *context
);
178 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
179 static void dnssdStop(void);
180 static void dnssdUpdate(void);
181 #endif /* HAVE_DNSSD */
184 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
186 "printerDescription",
188 "printerMakeAndModel",
193 #endif /* HAVE_LDAP */
201 * SLP service name for CUPS...
204 # define SLP_CUPS_SRVTYPE "service:printer"
205 # define SLP_CUPS_SRVLEN 15
209 * Printer service URL structure
212 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
214 struct _slpsrvurl_s
*next
; /* Next URL in list */
215 char url
[HTTP_MAX_URI
];
224 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
225 SLPError errcode
, void *cookie
);
226 static void slp_dereg_printer(cupsd_printer_t
*p
);
227 static int slp_get_attr(const char *attrlist
, const char *tag
,
229 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
231 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
232 unsigned short lifetime
,
233 SLPError errcode
, void *cookie
);
234 #endif /* HAVE_LIBSLP */
238 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
239 * local printer and remove any pending
240 * references to remote printers.
244 cupsdDeregisterPrinter(
245 cupsd_printer_t
*p
, /* I - Printer to register */
246 int removeit
) /* I - Printer being permanently removed */
249 * Only deregister if browsing is enabled and it's a local printer...
252 cupsdLogMessage(CUPSD_LOG_DEBUG
,
253 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
256 if (!Browsing
|| !p
->shared
||
257 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
258 CUPS_PRINTER_SCANNER
)))
262 * Announce the deletion...
265 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
267 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
269 p
->type
|= CUPS_PRINTER_DELETE
;
277 if (BrowseLocalProtocols
& BROWSE_SLP
)
278 slp_dereg_printer(p
);
279 #endif /* HAVE_LIBSLP */
282 if (BrowseLocalProtocols
& BROWSE_LDAP
)
283 ldap_dereg_printer(p
);
284 #endif /* HAVE_LDAP */
287 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
288 dnssdDeregisterPrinter(p
);
289 #endif /* HAVE_DNSSD */
294 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
298 cupsdLoadRemoteCache(void)
300 int i
; /* Looping var */
301 cups_file_t
*fp
; /* remote.cache file */
302 int linenum
; /* Current line number */
303 char line
[4096], /* Line from file */
304 *value
, /* Pointer to value */
305 *valueptr
, /* Pointer into value */
306 scheme
[32], /* Scheme portion of URI */
307 username
[64], /* Username portion of URI */
309 /* Hostname portion of URI */
310 resource
[HTTP_MAX_URI
];
311 /* Resource portion of URI */
312 int port
; /* Port number */
313 cupsd_printer_t
*p
; /* Current printer */
314 time_t now
; /* Current time */
318 * Don't load the cache if the remote protocols are disabled...
323 cupsdLogMessage(CUPSD_LOG_DEBUG
,
324 "cupsdLoadRemoteCache: Not loading remote cache.");
329 * Open the remote.cache file...
332 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
333 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
337 * Read printer configurations until we hit EOF...
344 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
347 * Decode the directive...
350 if (!strcasecmp(line
, "<Printer") ||
351 !strcasecmp(line
, "<DefaultPrinter"))
354 * <Printer name> or <DefaultPrinter name>
357 if (p
== NULL
&& value
)
360 * Add the printer and a base file type...
363 cupsdLogMessage(CUPSD_LOG_DEBUG
,
364 "cupsdLoadRemoteCache: Loading printer %s...", value
);
366 if ((p
= cupsdFindDest(value
)) != NULL
)
368 if (p
->type
& CUPS_PRINTER_CLASS
)
370 cupsdLogMessage(CUPSD_LOG_WARN
,
371 "Cached remote printer \"%s\" conflicts with "
379 p
= cupsdAddPrinter(value
);
382 p
->state
= IPP_PRINTER_IDLE
;
383 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
384 p
->browse_time
= now
;
385 p
->browse_expire
= now
+ BrowseTimeout
;
388 * Set the default printer as needed...
391 if (!strcasecmp(line
, "<DefaultPrinter"))
396 cupsdLogMessage(CUPSD_LOG_ERROR
,
397 "Syntax error on line %d of remote.cache.", linenum
);
401 else if (!strcasecmp(line
, "<Class") ||
402 !strcasecmp(line
, "<DefaultClass"))
405 * <Class name> or <DefaultClass name>
408 if (p
== NULL
&& value
)
411 * Add the printer and a base file type...
414 cupsdLogMessage(CUPSD_LOG_DEBUG
,
415 "cupsdLoadRemoteCache: Loading class %s...", value
);
417 if ((p
= cupsdFindDest(value
)) != NULL
)
418 p
->type
= CUPS_PRINTER_CLASS
;
420 p
= cupsdAddClass(value
);
423 p
->state
= IPP_PRINTER_IDLE
;
424 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
425 p
->browse_time
= now
;
426 p
->browse_expire
= now
+ BrowseTimeout
;
429 * Set the default printer as needed...
432 if (!strcasecmp(line
, "<DefaultClass"))
437 cupsdLogMessage(CUPSD_LOG_ERROR
,
438 "Syntax error on line %d of remote.cache.", linenum
);
442 else if (!strcasecmp(line
, "</Printer>") ||
443 !strcasecmp(line
, "</Class>"))
448 * Close out the current printer...
451 cupsdSetPrinterAttrs(p
);
456 cupsdLogMessage(CUPSD_LOG_ERROR
,
457 "Syntax error on line %d of remote.cache.", linenum
);
461 cupsdLogMessage(CUPSD_LOG_ERROR
,
462 "Syntax error on line %d of remote.cache.", linenum
);
464 else if (!strcasecmp(line
, "Info"))
467 cupsdSetString(&p
->info
, value
);
469 else if (!strcasecmp(line
, "MakeModel"))
472 cupsdSetString(&p
->make_model
, value
);
474 else if (!strcasecmp(line
, "Location"))
477 cupsdSetString(&p
->location
, value
);
479 else if (!strcasecmp(line
, "DeviceURI"))
483 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
484 username
, sizeof(username
), host
, sizeof(host
), &port
,
485 resource
, sizeof(resource
));
487 cupsdSetString(&p
->hostname
, host
);
488 cupsdSetString(&p
->uri
, value
);
489 cupsdSetDeviceURI(p
, value
);
492 cupsdLogMessage(CUPSD_LOG_ERROR
,
493 "Syntax error on line %d of remote.cache.", linenum
);
495 else if (!strcasecmp(line
, "Option") && value
)
501 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
504 cupsdLogMessage(CUPSD_LOG_ERROR
,
505 "Syntax error on line %d of remote.cache.", linenum
);
508 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
510 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
514 else if (!strcasecmp(line
, "Reason"))
518 for (i
= 0 ; i
< p
->num_reasons
; i
++)
519 if (!strcmp(value
, p
->reasons
[i
]))
522 if (i
>= p
->num_reasons
&&
523 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
525 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
530 cupsdLogMessage(CUPSD_LOG_ERROR
,
531 "Syntax error on line %d of remote.cache.", linenum
);
533 else if (!strcasecmp(line
, "State"))
536 * Set the initial queue state...
539 if (value
&& !strcasecmp(value
, "idle"))
540 p
->state
= IPP_PRINTER_IDLE
;
541 else if (value
&& !strcasecmp(value
, "stopped"))
543 p
->state
= IPP_PRINTER_STOPPED
;
544 cupsdSetPrinterReasons(p
, "+paused");
547 cupsdLogMessage(CUPSD_LOG_ERROR
,
548 "Syntax error on line %d of remote.cache.", linenum
);
550 else if (!strcasecmp(line
, "StateMessage"))
553 * Set the initial queue state message...
557 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
559 else if (!strcasecmp(line
, "Accepting"))
562 * Set the initial accepting state...
566 (!strcasecmp(value
, "yes") ||
567 !strcasecmp(value
, "on") ||
568 !strcasecmp(value
, "true")))
571 (!strcasecmp(value
, "no") ||
572 !strcasecmp(value
, "off") ||
573 !strcasecmp(value
, "false")))
576 cupsdLogMessage(CUPSD_LOG_ERROR
,
577 "Syntax error on line %d of remote.cache.", linenum
);
579 else if (!strcasecmp(line
, "Type"))
582 p
->type
= atoi(value
);
584 cupsdLogMessage(CUPSD_LOG_ERROR
,
585 "Syntax error on line %d of remote.cache.", linenum
);
587 else if (!strcasecmp(line
, "BrowseTime"))
591 time_t t
= atoi(value
);
593 if (t
> p
->browse_expire
)
594 p
->browse_expire
= t
;
597 cupsdLogMessage(CUPSD_LOG_ERROR
,
598 "Syntax error on line %d of remote.cache.", linenum
);
600 else if (!strcasecmp(line
, "JobSheets"))
603 * Set the initial job sheets...
608 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
613 cupsdSetString(&p
->job_sheets
[0], value
);
615 while (isspace(*valueptr
& 255))
620 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
625 cupsdSetString(&p
->job_sheets
[1], value
);
629 cupsdLogMessage(CUPSD_LOG_ERROR
,
630 "Syntax error on line %d of remote.cache.", linenum
);
632 else if (!strcasecmp(line
, "AllowUser"))
637 cupsdAddString(&(p
->users
), value
);
640 cupsdLogMessage(CUPSD_LOG_ERROR
,
641 "Syntax error on line %d of remote.cache.", linenum
);
643 else if (!strcasecmp(line
, "DenyUser"))
648 cupsdAddString(&(p
->users
), value
);
651 cupsdLogMessage(CUPSD_LOG_ERROR
,
652 "Syntax error on line %d of remote.cache.", linenum
);
657 * Something else we don't understand...
660 cupsdLogMessage(CUPSD_LOG_ERROR
,
661 "Unknown configuration directive %s on line %d of remote.cache.",
669 * Do auto-classing if needed...
672 process_implicit_classes();
677 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
678 * printer or update the broadcast contents.
682 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
684 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
687 if (!Browsing
|| !BrowseLocalProtocols
||
688 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
689 CUPS_PRINTER_SCANNER
)))
693 /* if (BrowseLocalProtocols & BROWSE_SLP)
694 slpRegisterPrinter(p); */
695 #endif /* HAVE_LIBSLP */
698 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
699 dnssdRegisterPrinter(p
);
700 #endif /* HAVE_DNSSD */
705 * 'cupsdRestartPolling()' - Restart polling servers as needed.
709 cupsdRestartPolling(void)
711 int i
; /* Looping var */
712 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
715 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
717 kill(pollp
->pid
, SIGHUP
);
722 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
726 cupsdSaveRemoteCache(void)
728 int i
; /* Looping var */
729 cups_file_t
*fp
; /* printers.conf file */
730 char temp
[1024], /* Temporary string */
731 value
[2048], /* Value string */
732 *name
; /* Current user name */
733 cupsd_printer_t
*printer
; /* Current printer class */
734 time_t curtime
; /* Current time */
735 struct tm
*curdate
; /* Current date */
736 cups_option_t
*option
; /* Current option */
740 * Create the remote.cache file...
743 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
745 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
747 cupsdLogMessage(CUPSD_LOG_ERROR
,
748 "Unable to save remote.cache - %s", strerror(errno
));
752 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
755 * Restrict access to the file...
758 fchown(cupsFileNumber(fp
), getuid(), Group
);
759 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
762 * Write a small header to the file...
765 curtime
= time(NULL
);
766 curdate
= localtime(&curtime
);
767 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
769 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
770 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
773 * Write each local printer known to the system...
776 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
778 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
781 * Skip local destinations...
784 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
788 * Write printers as needed...
791 if (printer
== DefaultPrinter
)
792 cupsFilePuts(fp
, "<Default");
794 cupsFilePutChar(fp
, '<');
796 if (printer
->type
& CUPS_PRINTER_CLASS
)
797 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
799 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
801 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
804 cupsFilePutConf(fp
, "Info", printer
->info
);
806 if (printer
->location
)
807 cupsFilePutConf(fp
, "Location", printer
->location
);
809 if (printer
->make_model
)
810 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
812 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
814 if (printer
->state
== IPP_PRINTER_STOPPED
)
815 cupsFilePuts(fp
, "State Stopped\n");
817 cupsFilePuts(fp
, "State Idle\n");
819 for (i
= 0; i
< printer
->num_reasons
; i
++)
820 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
822 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
824 if (printer
->accepting
)
825 cupsFilePuts(fp
, "Accepting Yes\n");
827 cupsFilePuts(fp
, "Accepting No\n");
829 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
830 printer
->job_sheets
[1]);
831 cupsFilePutConf(fp
, "JobSheets", value
);
833 for (name
= (char *)cupsArrayFirst(printer
->users
);
835 name
= (char *)cupsArrayNext(printer
->users
))
836 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser", name
);
838 for (i
= printer
->num_options
, option
= printer
->options
;
842 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
843 cupsFilePutConf(fp
, "Option", value
);
846 if (printer
->type
& CUPS_PRINTER_CLASS
)
847 cupsFilePuts(fp
, "</Class>\n");
849 cupsFilePuts(fp
, "</Printer>\n");
857 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
861 cupsdSendBrowseList(void)
863 int count
; /* Number of dests to update */
864 cupsd_printer_t
*p
; /* Current printer */
865 time_t ut
, /* Minimum update time */
866 to
; /* Timeout time */
869 if (!Browsing
|| !Printers
)
873 * Compute the update and timeout times...
877 ut
= to
- BrowseInterval
;
880 * Figure out how many printers need an update...
883 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
885 int max_count
; /* Maximum number to update */
889 * Throttle the number of printers we'll be updating this time
890 * around based on the number of queues that need updating and
891 * the maximum number of queues to update each second...
894 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
896 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
897 count
< max_count
&& p
!= NULL
;
898 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
899 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
900 CUPS_PRINTER_SCANNER
)) &&
901 p
->shared
&& p
->browse_time
< ut
)
905 * Loop through all of the printers and send local updates as needed...
909 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
911 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
915 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
918 * Check for wraparound...
922 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
926 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
927 CUPS_PRINTER_SCANNER
)) ||
930 else if (p
->browse_time
< ut
)
933 * Need to send an update...
938 p
->browse_time
= time(NULL
);
940 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
944 if (BrowseLocalProtocols
& BROWSE_SLP
)
946 #endif /* HAVE_LIBSLP */
949 if (BrowseLocalProtocols
& BROWSE_LDAP
)
951 #endif /* HAVE_LDAP */
956 * Save where we left off so that all printers get updated...
963 * Loop through all of the printers and timeout old printers as needed...
966 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
968 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
971 * If this is a remote queue, see if it needs to be timed out...
974 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
975 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
976 p
->browse_expire
< to
)
978 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
979 "%s \'%s\' deleted by directory services (timeout).",
980 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
983 cupsdLogMessage(CUPSD_LOG_DEBUG
,
984 "Remote destination \"%s\" has timed out; "
988 cupsArraySave(Printers
);
989 cupsdDeletePrinter(p
, 1);
990 cupsArrayRestore(Printers
);
991 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
997 #ifdef HAVE_LDAP_REBIND_PROC
998 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1000 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1003 static int /* O - Result code */
1005 LDAP
*RebindLDAPHandle
, /* I - LDAP handle */
1006 LDAP_CONST
char *refsp
, /* I - ??? */
1007 ber_tag_t request
, /* I - ??? */
1008 ber_int_t msgid
, /* I - ??? */
1009 void *params
) /* I - ??? */
1011 int rc
; /* Result code */
1012 # if LDAP_API_VERSION > 3000
1013 struct berval bval
; /* Bind value */
1014 # endif /* LDAP_API_VERSION > 3000 */
1017 * Bind to new LDAP server...
1020 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_rebind_proc: Rebind to %s", refsp
);
1022 # if LDAP_API_VERSION > 3000
1023 bval
.bv_val
= BrowseLDAPPassword
;
1024 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1026 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
,
1027 &bval
, NULL
, NULL
, NULL
);
1029 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, BrowseLDAPPassword
,
1031 # endif /* LDAP_API_VERSION > 3000 */
1037 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1039 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1042 static int /* O - Result code */
1044 LDAP
*RebindLDAPHandle
, /* I - LDAP handle */
1045 char **dnp
, /* I - ??? */
1046 char **passwdp
, /* I - ??? */
1047 int *authmethodp
, /* I - ??? */
1048 int freeit
, /* I - ??? */
1049 void *arg
) /* I - ??? */
1055 * Free current values...
1058 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_rebind_proc: Free values...");
1063 if (passwdp
&& *passwdp
)
1069 * Return credentials for LDAP referal...
1072 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1073 "ldap_rebind_proc: Return necessary values...");
1075 *dnp
= strdup(BrowseLDAPBindDN
);
1076 *passwdp
= strdup(BrowseLDAPPassword
);
1077 *authmethodp
= LDAP_AUTH_SIMPLE
;
1082 * Should never happen...
1085 cupsdLogMessage(CUPSD_LOG_ERROR
,
1086 "LDAP rebind has been called with wrong freeit value!");
1090 return (LDAP_SUCCESS
);
1092 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1093 #endif /* HAVE_LDAP_REBIND_PROC */
1098 * 'ldap_connect()' - Start new LDAP connection
1101 static LDAP
* /* O - LDAP handle */
1104 int rc
; /* LDAP API status */
1105 int version
= 3; /* LDAP version */
1106 struct berval bv
= {0, ""}; /* SASL bind value */
1107 LDAP
*TempBrowseLDAPHandle
=NULL
;
1108 /* Temporary LDAP Handle */
1109 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1110 int ldap_ssl
= 0; /* LDAP SSL indicator */
1111 int ssl_err
= 0; /* LDAP SSL error value */
1112 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1115 # ifdef HAVE_OPENLDAP
1116 # ifdef HAVE_LDAP_SSL
1118 * Set the certificate file to use for encrypted LDAP sessions...
1121 if (BrowseLDAPCACertFile
)
1123 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1124 "ldap_connect: Setting CA certificate file \"%s\"",
1125 BrowseLDAPCACertFile
);
1127 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1128 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1129 cupsdLogMessage(CUPSD_LOG_ERROR
,
1130 "Unable to set CA certificate file for LDAP "
1131 "connections: %d - %s", rc
, ldap_err2string(rc
));
1133 # endif /* HAVE_LDAP_SSL */
1136 * Initialize OPENLDAP connection...
1137 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1140 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1141 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1143 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1145 # else /* HAVE_OPENLDAP */
1147 int ldap_port
= 0; /* LDAP port */
1148 char ldap_protocol
[11], /* LDAP protocol */
1149 ldap_host
[255]; /* LDAP host */
1152 * Split LDAP URI into its components...
1155 if (!BrowseLDAPServer
)
1157 cupsdLogMessage(CUPSD_LOG_ERROR
, "BrowseLDAPServer not configured!");
1158 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1159 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1160 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1164 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
,
1167 if (!strcmp(ldap_protocol
, "ldap"))
1169 else if (!strcmp(ldap_protocol
, "ldaps"))
1173 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unrecognized LDAP protocol (%s)!",
1175 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1176 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1177 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1184 ldap_port
= LDAPS_PORT
;
1186 ldap_port
= LDAP_PORT
;
1189 cupsdLogMessage(CUPSD_LOG_DEBUG
, "ldap_connect: PROT:%s HOST:%s PORT:%d",
1190 ldap_protocol
, ldap_host
, ldap_port
);
1193 * Initialize LDAP connection...
1198 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1199 rc
= LDAP_OPERATIONS_ERROR
;
1203 # ifdef HAVE_LDAP_SSL
1208 * Initialize SSL LDAP connection...
1211 if (BrowseLDAPCACertFile
)
1213 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1214 if (rc
!= LDAP_SUCCESS
)
1216 cupsdLogMessage(CUPSD_LOG_ERROR
,
1217 "Failed to initialize LDAP SSL client!");
1218 rc
= LDAP_OPERATIONS_ERROR
;
1222 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
,
1224 rc
= LDAP_OPERATIONS_ERROR
;
1231 cupsdLogMessage(CUPSD_LOG_ERROR
,
1232 "LDAP SSL certificate file/database not configured!");
1233 rc
= LDAP_OPERATIONS_ERROR
;
1236 # else /* HAVE_LDAP_SSL */
1239 * Return error, because client libraries doesn't support SSL
1242 cupsdLogMessage(CUPSD_LOG_ERROR
,
1243 "LDAP client libraries do not support SSL");
1244 rc
= LDAP_OPERATIONS_ERROR
;
1246 # endif /* HAVE_LDAP_SSL */
1248 # endif /* HAVE_OPENLDAP */
1251 * Check return code from LDAP initialize...
1254 if (rc
!= LDAP_SUCCESS
)
1256 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize LDAP!");
1258 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
1259 cupsdLogMessage(CUPSD_LOG_ERROR
, "Temporarily disabling LDAP browsing...");
1262 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1264 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1265 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1268 ldap_disconnect(TempBrowseLDAPHandle
);
1274 * Upgrade LDAP version...
1277 if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1278 (const void *)&version
) != LDAP_SUCCESS
)
1280 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set LDAP protocol version %d!",
1282 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1284 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1285 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1286 ldap_disconnect(TempBrowseLDAPHandle
);
1292 * Register LDAP rebind procedure...
1295 # ifdef HAVE_LDAP_REBIND_PROC
1296 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1298 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
,
1300 if (rc
!= LDAP_SUCCESS
)
1301 cupsdLogMessage(CUPSD_LOG_ERROR
,
1302 "Setting LDAP rebind function failed with status %d: %s",
1303 rc
, ldap_err2string(rc
));
1307 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1309 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1310 # endif /* HAVE_LDAP_REBIND_PROC */
1313 * Start LDAP bind...
1316 # if LDAP_API_VERSION > 3000
1318 bval
.bv_val
= BrowseLDAPPassword
;
1319 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1321 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1322 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1325 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1328 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1329 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1330 # endif /* LDAP_API_VERSION > 3000 */
1332 if (rc
!= LDAP_SUCCESS
)
1334 cupsdLogMessage(CUPSD_LOG_ERROR
, "LDAP bind failed with error %d: %s",
1335 rc
, ldap_err2string(rc
));
1337 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1338 if (ldap_ssl
&& (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
1340 ssl_err
= PORT_GetError();
1342 cupsdLogMessage(CUPSD_LOG_ERROR
, "LDAP SSL error %d: %s", ssl_err
,
1343 ldapssl_err2string(ssl_err
));
1345 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1347 ldap_disconnect(TempBrowseLDAPHandle
);
1352 cupsdLogMessage(CUPSD_LOG_INFO
, "LDAP connection established");
1354 return (TempBrowseLDAPHandle
);
1359 * 'ldap_reconnect()' - Reconnect to LDAP Server
1362 static LDAP
* /* O - New LDAP handle */
1363 ldap_reconnect(void)
1365 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1369 * Get a new LDAP Handle and replace the global Handle
1370 * if the new connection was successful.
1373 cupsdLogMessage(CUPSD_LOG_INFO
, "Try LDAP reconnect...");
1375 TempBrowseLDAPHandle
= ldap_connect();
1377 if (TempBrowseLDAPHandle
!= NULL
)
1379 if (BrowseLDAPHandle
!= NULL
)
1380 ldap_disconnect(BrowseLDAPHandle
);
1382 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1385 return (BrowseLDAPHandle
);
1390 * 'ldap_disconnect()' - Disconnect from LDAP Server
1394 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1396 int rc
; /* Return code */
1400 * Close LDAP handle...
1403 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1404 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1406 rc
= ldap_unbind_s(ld
);
1407 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1409 if (rc
!= LDAP_SUCCESS
)
1410 cupsdLogMessage(CUPSD_LOG_ERROR
,
1411 "Unbind from LDAP server failed with status %d: %s",
1412 rc
, ldap_err2string(rc
));
1414 #endif /* HAVE_LDAP */
1418 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1422 cupsdStartBrowsing(void)
1424 int val
; /* Socket option value */
1425 struct sockaddr_in addr
; /* Broadcast address */
1426 cupsd_printer_t
*p
; /* Current printer */
1431 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1434 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1436 if (BrowseSocket
< 0)
1439 * Create the broadcast socket...
1442 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1444 cupsdLogMessage(CUPSD_LOG_ERROR
,
1445 "Unable to create broadcast socket - %s.",
1447 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1448 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1450 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1451 cupsdEndProcess(getpid(), 0);
1455 if (BrowseSocket
>= 0)
1458 * Bind the socket to browse port...
1461 memset(&addr
, 0, sizeof(addr
));
1462 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1463 addr
.sin_family
= AF_INET
;
1464 addr
.sin_port
= htons(BrowsePort
);
1466 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1468 cupsdLogMessage(CUPSD_LOG_ERROR
,
1469 "Unable to bind broadcast socket - %s.",
1473 closesocket(BrowseSocket
);
1475 close(BrowseSocket
);
1479 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1480 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1482 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1483 cupsdEndProcess(getpid(), 0);
1487 if (BrowseSocket
>= 0)
1490 * Set the "broadcast" flag...
1494 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1496 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1500 closesocket(BrowseSocket
);
1502 close(BrowseSocket
);
1506 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1507 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1509 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1510 cupsdEndProcess(getpid(), 0);
1514 if (BrowseSocket
>= 0)
1517 * Close the socket on exec...
1520 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1523 * Finally, add the socket to the input selection set as needed...
1526 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1529 * We only listen if we want remote printers...
1532 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1541 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1543 DNSServiceErrorType error
; /* Error from service creation */
1544 cupsd_listener_t
*lis
; /* Current listening socket */
1548 * First create a "master" connection for all registrations...
1551 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1552 != kDNSServiceErr_NoError
)
1554 cupsdLogMessage(CUPSD_LOG_ERROR
,
1555 "Unable to create master DNS-SD reference: %d", error
);
1557 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1558 cupsdEndProcess(getpid(), 0);
1563 * Add the master connection to the select list...
1566 int fd
= DNSServiceRefSockFD(DNSSDRef
);
1568 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
1570 cupsdAddSelect(fd
, (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1573 * Then get the port we use for registrations. If we are not listening
1574 * on any non-local ports, there is no sense sharing local printers via
1580 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1582 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1584 if (httpAddrLocalhost(&(lis
->address
)))
1587 if (lis
->address
.addr
.sa_family
== AF_INET
)
1589 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1592 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1594 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1600 * Create an array to track the printers we share...
1603 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1604 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1608 * Set the computer name and register the web interface...
1611 cupsdUpdateDNSSDName();
1614 #endif /* HAVE_DNSSD */
1617 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1620 * Open SLP handle...
1623 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1625 cupsdLogMessage(CUPSD_LOG_ERROR
,
1626 "Unable to open an SLP handle; disabling SLP browsing!");
1627 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1628 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1629 BrowseSLPHandle
= NULL
;
1631 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1632 cupsdEndProcess(getpid(), 0);
1635 BrowseSLPRefresh
= 0;
1638 BrowseSLPHandle
= NULL
;
1639 #endif /* HAVE_LIBSLP */
1642 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1646 cupsdLogMessage(CUPSD_LOG_ERROR
,
1647 "Need to set BrowseLDAPDN to use LDAP browsing!");
1648 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1649 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1651 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1652 cupsdEndProcess(getpid(), 0);
1657 * Open LDAP handle...
1660 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1661 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1662 cupsdEndProcess(getpid(), 0);
1665 BrowseLDAPRefresh
= 0;
1667 #endif /* HAVE_LDAP */
1670 * Enable LPD and SMB printer sharing as needed through external programs...
1673 if (BrowseLocalProtocols
& BROWSE_LPD
)
1676 if (BrowseLocalProtocols
& BROWSE_SMB
)
1680 * Register the individual printers
1683 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1685 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1686 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1687 CUPS_PRINTER_SCANNER
)))
1688 cupsdRegisterPrinter(p
);
1693 * 'cupsdStartPolling()' - Start polling servers as needed.
1697 cupsdStartPolling(void)
1699 int i
; /* Looping var */
1700 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1701 char polld
[1024]; /* Poll daemon path */
1702 char sport
[255]; /* Server port */
1703 char bport
[255]; /* Browser port */
1704 char interval
[255]; /* Poll interval */
1705 int statusfds
[2]; /* Status pipe */
1706 char *argv
[6]; /* Arguments */
1707 char *envp
[100]; /* Environment */
1711 * Don't do anything if we aren't polling...
1714 if (NumPolled
== 0 || BrowseSocket
< 0)
1717 PollStatusBuffer
= NULL
;
1722 * Setup string arguments for polld, port and interval options.
1725 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1727 sprintf(bport
, "%d", BrowsePort
);
1730 sprintf(interval
, "%d", BrowseInterval
);
1732 strcpy(interval
, "30");
1734 argv
[0] = "cups-polld";
1740 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1743 * Create a pipe that receives the status messages from each
1747 if (cupsdOpenPipe(statusfds
))
1749 cupsdLogMessage(CUPSD_LOG_ERROR
,
1750 "Unable to create polling status pipes - %s.",
1753 PollStatusBuffer
= NULL
;
1757 PollPipe
= statusfds
[0];
1758 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1761 * Run each polling daemon, redirecting stderr to the polling pipe...
1764 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1766 sprintf(sport
, "%d", pollp
->port
);
1768 argv
[1] = pollp
->hostname
;
1770 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1771 0, DefaultProfile
, NULL
, &(pollp
->pid
)) < 0)
1773 cupsdLogMessage(CUPSD_LOG_ERROR
,
1774 "cupsdStartPolling: Unable to fork polling daemon - %s",
1780 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1781 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1782 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1785 close(statusfds
[1]);
1788 * Finally, add the pipe to the input selection set...
1791 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1796 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1800 cupsdStopBrowsing(void)
1802 cupsd_printer_t
*p
; /* Current printer */
1805 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1809 * De-register the individual printers
1812 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1814 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1815 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1816 CUPS_PRINTER_SCANNER
)))
1817 cupsdDeregisterPrinter(p
, 1);
1820 * Shut down browsing sockets...
1823 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1827 * Close the socket and remove it from the input selection set.
1831 closesocket(BrowseSocket
);
1833 close(BrowseSocket
);
1836 cupsdRemoveSelect(BrowseSocket
);
1841 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1843 #endif /* HAVE_DNSSD */
1846 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1850 * Close SLP handle...
1853 SLPClose(BrowseSLPHandle
);
1854 BrowseSLPHandle
= NULL
;
1856 #endif /* HAVE_LIBSLP */
1859 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1862 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1863 ldap_disconnect(BrowseLDAPHandle
);
1864 BrowseLDAPHandle
= NULL
;
1866 #endif /* HAVE_OPENLDAP */
1869 * Disable LPD and SMB printer sharing as needed through external programs...
1872 if (BrowseLocalProtocols
& BROWSE_LPD
)
1875 if (BrowseLocalProtocols
& BROWSE_SMB
)
1881 * 'cupsdStopPolling()' - Stop polling servers as needed.
1885 cupsdStopPolling(void)
1887 int i
; /* Looping var */
1888 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1893 cupsdStatBufDelete(PollStatusBuffer
);
1896 cupsdRemoveSelect(PollPipe
);
1899 PollStatusBuffer
= NULL
;
1902 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1904 cupsdEndProcess(pollp
->pid
, 0);
1910 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1914 cupsdUpdateDNSSDName(void)
1916 DNSServiceErrorType error
; /* Error from service creation */
1917 char webif
[1024]; /* Web interface share name */
1918 # ifdef HAVE_SYSTEMCONFIGURATION
1919 SCDynamicStoreRef sc
; /* Context for dynamic store */
1920 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1921 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1922 CFStringRef nameRef
; /* Host name CFString */
1923 char nameBuffer
[1024]; /* C-string buffer */
1924 # endif /* HAVE_SYSTEMCONFIGURATION */
1928 * Only share the web interface and printers when non-local listening is
1937 * Get the computer name as a c-string...
1940 # ifdef HAVE_SYSTEMCONFIGURATION
1941 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1946 * Get the computer name from the dynamic store...
1949 cupsdClearString(&DNSSDComputerName
);
1951 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1953 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1954 kCFStringEncodingUTF8
))
1956 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1957 "Dynamic store computer name is \"%s\".", nameBuffer
);
1958 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1964 if (!DNSSDComputerName
)
1967 * Use the ServerName instead...
1970 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1971 "Using ServerName \"%s\" as computer name.", ServerName
);
1972 cupsdSetString(&DNSSDComputerName
, ServerName
);
1976 * Get the local hostname from the dynamic store...
1979 cupsdClearString(&DNSSDHostName
);
1981 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1983 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1984 kCFStringEncodingUTF8
))
1986 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1987 "Dynamic store host name is \"%s\".", nameBuffer
);
1988 cupsdSetString(&DNSSDHostName
, nameBuffer
);
1997 * Use the ServerName instead...
2000 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2001 "Using ServerName \"%s\" as host name.", ServerName
);
2002 cupsdSetString(&DNSSDHostName
, ServerName
);
2006 * Get any Back-to-My-Mac domains and add them as aliases...
2009 cupsdFreeAliases(DNSSDAlias
);
2012 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
2013 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
2015 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
2016 (int)CFDictionaryGetCount(btmm
));
2017 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
2020 cupsdLogMessage(CUPSD_LOG_ERROR
,
2021 "Bad Back to My Mac data in dynamic store!");
2023 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
2031 # endif /* HAVE_SYSTEMCONFIGURATION */
2033 cupsdSetString(&DNSSDComputerName
, ServerName
);
2034 cupsdSetString(&DNSSDHostName
, ServerName
);
2038 * Then (re)register the web interface if enabled...
2043 if (DNSSDComputerName
)
2044 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
2046 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2049 DNSServiceRefDeallocate(WebIFRef
);
2051 WebIFRef
= DNSSDRef
;
2052 if ((error
= DNSServiceRegister(&WebIFRef
,
2053 kDNSServiceFlagsShareConnection
,
2054 0, webif
, "_http._tcp", NULL
,
2055 NULL
, htons(DNSSDPort
), 7,
2056 "\006path=/", dnssdRegisterCallback
,
2057 NULL
)) != kDNSServiceErr_NoError
)
2058 cupsdLogMessage(CUPSD_LOG_ERROR
,
2059 "DNS-SD web interface registration failed: %d", error
);
2062 #endif /* HAVE_DNSSD */
2067 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2071 cupsdUpdateLDAPBrowse(void)
2073 char uri
[HTTP_MAX_URI
], /* Printer URI */
2074 host
[HTTP_MAX_URI
], /* Hostname */
2075 resource
[HTTP_MAX_URI
], /* Resource path */
2076 location
[1024], /* Printer location */
2077 info
[1024], /* Printer information */
2078 make_model
[1024], /* Printer make and model */
2079 type_num
[30]; /* Printer type number */
2080 int type
; /* Printer type */
2081 int rc
; /* LDAP status */
2082 int limit
; /* Size limit */
2083 LDAPMessage
*res
, /* LDAP search results */
2084 *e
; /* Current entry from search */
2086 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2088 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2091 * Reconnect if LDAP Handle is invalid...
2094 if (! BrowseLDAPHandle
)
2101 * Search for cups printers in LDAP directory...
2104 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2105 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2108 * If ldap search was successfull then exit function
2109 * and temporary disable LDAP updates...
2112 if (rc
!= LDAP_SUCCESS
)
2114 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2116 BrowseLDAPUpdate
= FALSE
;
2117 cupsdLogMessage(CUPSD_LOG_INFO
,
2118 "LDAP update temporary disabled");
2124 * If LDAP updates were disabled, we will reenable them...
2127 if (! BrowseLDAPUpdate
)
2129 BrowseLDAPUpdate
= TRUE
;
2130 cupsdLogMessage(CUPSD_LOG_INFO
,
2131 "LDAP update enabled");
2135 * Count LDAP entries and return if no entry exist...
2138 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2139 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2147 * Loop through the available printers...
2150 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2152 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2155 * Get the required values from this entry...
2158 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2159 "printerDescription", info
, sizeof(info
)) == -1)
2162 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2163 "printerLocation", location
, sizeof(location
)) == -1)
2166 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2167 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2170 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2171 "printerType", type_num
, sizeof(type_num
)) == -1)
2174 type
= atoi(type_num
);
2176 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2177 "printerURI", uri
, sizeof(uri
)) == -1)
2181 * Process the entry as browse data...
2184 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2185 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2186 location
, info
, make_model
, 0, NULL
);
2192 #endif /* HAVE_LDAP */
2197 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2201 cupsdUpdateSLPBrowse(void)
2203 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2204 *next
; /* Next service in list */
2205 cupsd_printer_t p
; /* Printer information */
2206 const char *uri
; /* Pointer to printer URI */
2207 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2208 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2212 * Reset the refresh time...
2215 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2218 * Poll for remote printers using SLP...
2223 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2224 slp_url_callback
, &s
);
2227 * Loop through the list of available printers...
2233 * Save the "next" pointer...
2239 * Load a cupsd_printer_t structure with the SLP service attributes...
2242 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2245 * Process this printer entry...
2248 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2250 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2253 * Pull the URI apart to see if this is a local or remote printer...
2256 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2257 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2258 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2262 * Free this listing...
2265 cupsdClearString(&p
.info
);
2266 cupsdClearString(&p
.location
);
2267 cupsdClearString(&p
.make_model
);
2272 #endif /* HAVE_LIBSLP */
2276 * 'dequote()' - Remote quotes from a string.
2279 static char * /* O - Dequoted string */
2280 dequote(char *d
, /* I - Destination string */
2281 const char *s
, /* I - Source string */
2282 int dlen
) /* I - Destination length */
2284 char *dptr
; /* Pointer into destination */
2289 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2306 # ifdef HAVE_COREFOUNDATION
2308 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2312 dnssdAddAlias(const void *key
, /* I - Key */
2313 const void *value
, /* I - Value (domain) */
2314 void *context
) /* I - Unused */
2316 char valueStr
[1024], /* Domain string */
2317 hostname
[1024]; /* Complete hostname */
2322 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2323 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2324 kCFStringEncodingUTF8
))
2326 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
2328 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2330 cupsdAddAlias(DNSSDAlias
, hostname
);
2331 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2335 cupsdLogMessage(CUPSD_LOG_ERROR
,
2336 "Bad Back to My Mac domain in dynamic store!");
2338 # endif /* HAVE_COREFOUNDATION */
2342 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2345 static char * /* O - TXT record */
2346 dnssdBuildTxtRecord(
2347 int *txt_len
, /* O - TXT record length */
2348 cupsd_printer_t
*p
, /* I - Printer information */
2349 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2351 int i
; /* Looping var */
2352 char admin_hostname
[256], /* .local hostname for admin page */
2353 adminurl_str
[256], /* URL for the admin page */
2354 type_str
[32], /* Type to string buffer */
2355 state_str
[32], /* State to string buffer */
2356 rp_str
[1024], /* Queue name string buffer */
2357 air_str
[1024], /* auth-info-required string buffer */
2358 *keyvalue
[32][2]; /* Table of key/value pairs */
2362 * Load up the key value pairs...
2367 keyvalue
[i
][0] = "txtvers";
2368 keyvalue
[i
++][1] = "1";
2370 keyvalue
[i
][0] = "qtotal";
2371 keyvalue
[i
++][1] = "1";
2373 keyvalue
[i
][0] = "rp";
2374 keyvalue
[i
++][1] = rp_str
;
2376 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2378 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2379 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2381 keyvalue
[i
][0] = "ty";
2382 keyvalue
[i
++][1] = p
->make_model
? p
->make_model
: "Unknown";
2384 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.local", DNSSDHostName
);
2385 httpAssembleURIf(HTTP_URI_CODING_ALL
, adminurl_str
, sizeof(adminurl_str
),
2386 "http", NULL
, admin_hostname
, DNSSDPort
, "/%s/%s",
2387 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
2389 keyvalue
[i
][0] = "adminurl";
2390 keyvalue
[i
++][1] = adminurl_str
;
2392 keyvalue
[i
][0] = "note";
2393 keyvalue
[i
++][1] = p
->location
? p
->location
: "";
2395 keyvalue
[i
][0] = "priority";
2396 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2398 keyvalue
[i
][0] = "product";
2399 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2401 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2402 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2404 keyvalue
[i
][0] = "printer-state";
2405 keyvalue
[i
++][1] = state_str
;
2407 keyvalue
[i
][0] = "printer-type";
2408 keyvalue
[i
++][1] = type_str
;
2410 keyvalue
[i
][0] = "Transparent";
2411 keyvalue
[i
++][1] = "T";
2413 keyvalue
[i
][0] = "Binary";
2414 keyvalue
[i
++][1] = "T";
2416 keyvalue
[i
][0] = "Fax";
2417 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_FAX
) ? "T" : "F";
2419 keyvalue
[i
][0] = "Color";
2420 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_COLOR
) ? "T" : "F";
2422 keyvalue
[i
][0] = "Duplex";
2423 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_DUPLEX
) ? "T" : "F";
2425 keyvalue
[i
][0] = "Staple";
2426 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_STAPLE
) ? "T" : "F";
2428 keyvalue
[i
][0] = "Copies";
2429 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_COPIES
) ? "T" : "F";
2431 keyvalue
[i
][0] = "Collate";
2432 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_COLLATE
) ? "T" : "F";
2434 keyvalue
[i
][0] = "Punch";
2435 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_PUNCH
) ? "T" : "F";
2437 keyvalue
[i
][0] = "Bind";
2438 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_BIND
) ? "T" : "F";
2440 keyvalue
[i
][0] = "Sort";
2441 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_SORT
) ? "T" : "F";
2443 keyvalue
[i
][0] = "Scan";
2444 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_MFP
) ? "T" : "F";
2446 keyvalue
[i
][0] = "pdl";
2447 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2449 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
2451 keyvalue
[i
][0] = "air";
2452 keyvalue
[i
++][1] = air_str
;
2456 * Then pack them into a proper txt record...
2459 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2464 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2467 static int /* O - Result of comparison */
2468 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2469 cupsd_printer_t
*b
)/* I - Second printer */
2471 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2476 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2481 dnssdDeregisterPrinter(
2482 cupsd_printer_t
*p
) /* I - Printer */
2484 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2487 * Closing the socket deregisters the service
2492 DNSServiceRefDeallocate(p
->ipp_ref
);
2499 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2508 DNSServiceRefDeallocate(p
->printer_ref
);
2509 p
->printer_ref
= NULL
;
2515 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2518 free(p
->printer_txt
);
2519 p
->printer_txt
= NULL
;
2523 * Remove the printer from the array of DNS-SD printers, then clear the
2524 * registered name...
2527 cupsArrayRemove(DNSSDPrinters
, p
);
2528 cupsdClearString(&p
->reg_name
);
2533 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2534 * TXT record format.
2537 static char * /* O - TXT record */
2538 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2539 char *keyvalue
[][2], /* I - Table of key value pairs */
2540 int count
) /* I - Items in table */
2542 int i
; /* Looping var */
2543 int length
; /* Length of TXT record */
2544 int length2
; /* Length of value */
2545 char *txtRecord
; /* TXT record buffer */
2546 char *cursor
; /* Looping pointer */
2550 * Calculate the buffer size
2556 for (length
= i
= 0; i
< count
; i
++)
2557 length
+= 1 + strlen(keyvalue
[i
][0]) +
2558 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2561 * Allocate and fill it
2564 txtRecord
= malloc(length
);
2569 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2572 * Drop in the p-string style length byte followed by the data
2575 length
= strlen(keyvalue
[i
][0]);
2576 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2578 *cursor
++ = (unsigned char)(length
+ length2
);
2580 memcpy(cursor
, keyvalue
[i
][0], length
);
2587 memcpy(cursor
, keyvalue
[i
][1], length2
);
2598 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2602 dnssdRegisterCallback(
2603 DNSServiceRef sdRef
, /* I - DNS Service reference */
2604 DNSServiceFlags flags
, /* I - Reserved for future use */
2605 DNSServiceErrorType errorCode
, /* I - Error code */
2606 const char *name
, /* I - Service name */
2607 const char *regtype
, /* I - Service type */
2608 const char *domain
, /* I - Domain. ".local" for now */
2609 void *context
) /* I - User-defined context */
2611 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2612 /* Current printer */
2615 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2616 name
, regtype
, p
? p
->name
: "Web Interface",
2617 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2621 cupsdLogMessage(CUPSD_LOG_ERROR
,
2622 "DNSServiceRegister failed with error %d", (int)errorCode
);
2625 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2627 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2630 cupsArrayRemove(DNSSDPrinters
, p
);
2631 cupsdSetString(&p
->reg_name
, name
);
2632 cupsArrayAdd(DNSSDPrinters
, p
);
2634 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2640 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2641 * or update the broadcast contents.
2645 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2647 DNSServiceErrorType se
; /* dnssd errors */
2648 char *ipp_txt
, /* IPP TXT record buffer */
2649 *printer_txt
, /* LPD TXT record buffer */
2650 name
[1024], /* Service name */
2651 *nameptr
; /* Pointer into name */
2652 int ipp_len
, /* IPP TXT record length */
2653 printer_len
, /* LPD TXT record length */
2654 printer_port
; /* LPD port number */
2655 const char *regtype
; /* Registration type */
2658 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2659 !p
->ipp_ref
? "new" : "update");
2662 * If per-printer sharing was just disabled make sure we're not
2663 * registered before returning.
2668 dnssdDeregisterPrinter(p
);
2673 * The registered name takes the form of "<printer-info> @ <computer name>"...
2676 if (p
->info
&& strlen(p
->info
) > 0)
2678 if (DNSSDComputerName
)
2679 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
2681 strlcpy(name
, p
->info
, sizeof(name
));
2683 else if (DNSSDComputerName
)
2684 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
2686 strlcpy(name
, p
->name
, sizeof(name
));
2689 * If an existing printer was renamed, unregister it and start over...
2692 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2693 dnssdDeregisterPrinter(p
);
2697 cupsdSetString(&p
->reg_name
, name
);
2698 cupsArrayAdd(DNSSDPrinters
, p
);
2702 * Register IPP and (optionally) LPD...
2705 ipp_len
= 0; /* anti-compiler-warning-code */
2706 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2709 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2712 * Update the existing registration...
2715 /* A TTL of 0 means use record's original value (Radar 3176248) */
2716 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2717 0)) == kDNSServiceErr_NoError
)
2722 p
->ipp_txt
= ipp_txt
;
2723 p
->ipp_len
= ipp_len
;
2729 * Failed to update record, lets close this reference and move on...
2732 cupsdLogMessage(CUPSD_LOG_ERROR
,
2733 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2736 DNSServiceRefDeallocate(p
->ipp_ref
);
2744 * Initial registration. Use the _fax subtype for fax queues...
2747 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2750 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2751 "Registering DNS-SD printer %s with name \"%s\" and "
2752 "type \"%s\"", p
->name
, name
, regtype
);
2755 * Register the queue, dropping characters as needed until we succeed...
2758 nameptr
= name
+ strlen(name
);
2762 p
->ipp_ref
= DNSSDRef
;
2763 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2764 0, name
, regtype
, NULL
, NULL
,
2765 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2766 dnssdRegisterCallback
,
2767 p
)) == kDNSServiceErr_BadParam
)
2770 * Name is too long, drop trailing characters, taking into account
2776 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2783 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2785 if (se
== kDNSServiceErr_NoError
)
2787 p
->ipp_txt
= ipp_txt
;
2788 p
->ipp_len
= ipp_len
;
2792 cupsdLogMessage(CUPSD_LOG_WARN
,
2793 "DNS-SD IPP registration of \"%s\" failed: %d",
2800 if (BrowseLocalProtocols
& BROWSE_LPD
)
2802 printer_len
= 0; /* anti-compiler-warning-code */
2804 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2813 if (p
->printer_ref
&&
2814 (printer_len
!= p
->printer_len
||
2815 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2818 * Update the existing registration...
2821 /* A TTL of 0 means use record's original value (Radar 3176248) */
2822 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2824 0)) == kDNSServiceErr_NoError
)
2827 free(p
->printer_txt
);
2829 p
->printer_txt
= printer_txt
;
2830 p
->printer_len
= printer_len
;
2836 * Failed to update record, lets close this reference and move on...
2839 cupsdLogMessage(CUPSD_LOG_ERROR
,
2840 "Unable to update LPD DNS-SD record for %s - %d",
2843 DNSServiceRefDeallocate(p
->printer_ref
);
2844 p
->printer_ref
= NULL
;
2848 if (!p
->printer_ref
)
2851 * Initial registration...
2854 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2855 "Registering DNS-SD printer %s with name \"%s\" and "
2856 "type \"_printer._tcp\"", p
->name
, name
);
2858 p
->printer_ref
= DNSSDRef
;
2859 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2860 kDNSServiceFlagsShareConnection
,
2861 0, name
, "_printer._tcp", NULL
, NULL
,
2862 htons(printer_port
), printer_len
, printer_txt
,
2863 dnssdRegisterCallback
,
2864 p
)) == kDNSServiceErr_NoError
)
2866 p
->printer_txt
= printer_txt
;
2867 p
->printer_len
= printer_len
;
2871 cupsdLogMessage(CUPSD_LOG_WARN
,
2872 "DNS-SD LPD registration of \"%s\" failed: %d",
2882 * 'dnssdStop()' - Stop all DNS-SD registrations.
2888 cupsd_printer_t
*p
; /* Current printer */
2892 * De-register the individual printers
2895 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2897 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2898 dnssdDeregisterPrinter(p
);
2901 * Shutdown the rest of the service refs...
2906 DNSServiceRefDeallocate(WebIFRef
);
2912 DNSServiceRefDeallocate(RemoteRef
);
2916 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2918 DNSServiceRefDeallocate(DNSSDRef
);
2921 cupsArrayDelete(DNSSDPrinters
);
2922 DNSSDPrinters
= NULL
;
2929 * 'dnssdUpdate()' - Handle DNS-SD queries.
2935 DNSServiceErrorType sdErr
; /* Service discovery error */
2938 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2940 cupsdLogMessage(CUPSD_LOG_ERROR
,
2941 "DNS Service Discovery registration error %d!",
2946 #endif /* HAVE_DNSSD */
2950 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2953 static char * /* O - String or NULL if none */
2954 get_auth_info_required(
2955 cupsd_printer_t
*p
, /* I - Printer */
2956 char *buffer
, /* I - Value buffer */
2957 size_t bufsize
) /* I - Size of value buffer */
2959 cupsd_location_t
*auth
; /* Pointer to authentication element */
2960 char resource
[1024]; /* Printer/class resource path */
2964 * If auth-info-required is set for this printer, return that...
2967 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2969 int i
; /* Looping var */
2970 char *bufptr
; /* Pointer into buffer */
2972 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
2974 if (bufptr
>= (buffer
+ bufsize
- 2))
2980 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
2981 bufptr
+= strlen(bufptr
);
2988 * Figure out the authentication data requirements to advertise...
2991 if (p
->type
& CUPS_PRINTER_CLASS
)
2992 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2994 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2996 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2997 auth
->type
== CUPSD_AUTH_NONE
)
2998 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
3002 int auth_type
; /* Authentication type */
3004 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
3005 auth_type
= DefaultAuthType
;
3009 case CUPSD_AUTH_NONE
:
3012 case CUPSD_AUTH_NEGOTIATE
:
3013 strlcpy(buffer
, "negotiate", bufsize
);
3017 strlcpy(buffer
, "username,password", bufsize
);
3030 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
3033 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
3034 get_hostconfig(const char *name
) /* I - Name of service */
3036 cups_file_t
*fp
; /* Hostconfig file */
3037 char line
[1024], /* Line from file */
3038 *ptr
; /* Pointer to value */
3039 int state
= 1; /* State of service */
3043 * Try opening the /etc/hostconfig file; if we can't open it, assume that
3044 * the service is enabled/auto.
3047 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
3050 * Read lines from the file until we find the service...
3053 while (cupsFileGets(fp
, line
, sizeof(line
)))
3055 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
3060 if (!strcasecmp(line
, name
))
3063 * Found the service, see if it is set to "-NO-"...
3066 if (!strncasecmp(ptr
, "-NO-", 4))
3077 #endif /* __APPLE__ */
3081 * 'is_local_queue()' - Determine whether the URI points at a local queue.
3084 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
3085 is_local_queue(const char *uri
, /* I - Printer URI */
3086 char *host
, /* O - Host string */
3087 int hostlen
, /* I - Length of host buffer */
3088 char *resource
, /* O - Resource string */
3089 int resourcelen
) /* I - Length of resource buffer */
3091 char scheme
[32], /* Scheme portion of URI */
3092 username
[HTTP_MAX_URI
]; /* Username portion of URI */
3093 int port
; /* Port portion of URI */
3094 cupsd_netif_t
*iface
; /* Network interface */
3098 * Pull the URI apart to see if this is a local or remote printer...
3101 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3102 username
, sizeof(username
), host
, hostlen
, &port
,
3103 resource
, resourcelen
) < HTTP_URI_OK
)
3106 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3109 * Check for local server addresses...
3112 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3117 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3119 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3120 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3124 * If we get here, the printer is remote...
3132 * 'process_browse_data()' - Process new browse data.
3136 process_browse_data(
3137 const char *uri
, /* I - URI of printer/class */
3138 const char *host
, /* I - Hostname */
3139 const char *resource
, /* I - Resource path */
3140 cups_ptype_t type
, /* I - Printer type */
3141 ipp_pstate_t state
, /* I - Printer state */
3142 const char *location
, /* I - Printer location */
3143 const char *info
, /* I - Printer information */
3144 const char *make_model
, /* I - Printer make and model */
3145 int num_attrs
, /* I - Number of attributes */
3146 cups_option_t
*attrs
) /* I - Attributes */
3148 int i
; /* Looping var */
3149 int update
; /* Update printer attributes? */
3150 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3151 name
[IPP_MAX_NAME
], /* Name of printer */
3152 newname
[IPP_MAX_NAME
], /* New name of printer */
3153 *hptr
, /* Pointer into hostname */
3154 *sptr
; /* Pointer into ServerName */
3155 const char *shortname
; /* Short queue name (queue) */
3156 char local_make_model
[IPP_MAX_NAME
];
3157 /* Local make and model */
3158 cupsd_printer_t
*p
; /* Printer information */
3159 const char *ipp_options
, /* ipp-options value */
3160 *lease_duration
; /* lease-duration value */
3161 int is_class
; /* Is this queue a class? */
3164 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3165 "process_browse_data(uri=\"%s\", host=\"%s\", "
3166 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3167 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3168 uri
, host
, resource
, type
, state
,
3169 location
? location
: "(nil)", info
? info
: "(nil)",
3170 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3173 * Determine if the URI contains any illegal characters in it...
3176 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3177 (strncmp(resource
, "/printers/", 10) &&
3178 strncmp(resource
, "/classes/", 9)))
3180 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3184 if (strchr(resource
, '?') ||
3185 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3186 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3188 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3194 * OK, this isn't a local printer; add any remote options...
3197 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3199 if (BrowseRemoteOptions
)
3201 if (BrowseRemoteOptions
[0] == '?')
3204 * Override server-supplied options...
3207 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3209 else if (ipp_options
)
3212 * Combine the server and local options...
3215 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3216 BrowseRemoteOptions
);
3221 * Just use the local options...
3224 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3229 else if (ipp_options
)
3232 * Just use the server-supplied options...
3235 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3240 * See if we already have it listed in the Printers list, and add it if not...
3243 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3244 type
&= ~CUPS_PRINTER_IMPLICIT
;
3246 hptr
= strchr(host
, '.');
3247 sptr
= strchr(ServerName
, '.');
3248 is_class
= type
& CUPS_PRINTER_CLASS
;
3250 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3253 * Strip the common domain name components...
3256 while (hptr
!= NULL
)
3258 if (!strcasecmp(hptr
, sptr
))
3264 hptr
= strchr(hptr
+ 1, '.');
3271 * Remote destination is a class...
3274 if (!strncmp(resource
, "/classes/", 9))
3275 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3279 shortname
= resource
+ 9;
3284 * Remote destination is a printer...
3287 if (!strncmp(resource
, "/printers/", 10))
3288 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3292 shortname
= resource
+ 10;
3296 *hptr
= '.'; /* Resource FQDN */
3298 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3301 * Long name doesn't exist, try short name...
3304 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3307 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3310 * Short name doesn't exist, use it for this shared queue.
3313 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3315 strlcpy(name
, shortname
, sizeof(name
));
3320 * Short name exists...
3323 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3324 "process_browse_data: %s found, type=%x, hostname=%s...",
3325 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3327 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3328 p
= NULL
; /* Don't replace implicit classes */
3329 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3332 * Short name exists but is for a different host. If this is a remote
3333 * queue, rename it and use the long name...
3336 if (p
->type
& CUPS_PRINTER_REMOTE
)
3338 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3339 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3340 is_class
? "class" : "printer", p
->name
, p
->name
,
3342 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3343 "%s \'%s\' deleted by directory services.",
3344 is_class
? "Class" : "Printer", p
->name
);
3346 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3347 cupsdRenamePrinter(p
, newname
);
3349 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3350 "%s \'%s\' added by directory services.",
3351 is_class
? "Class" : "Printer", p
->name
);
3355 * Force creation with long name...
3363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3364 "process_browse_data: %s found, type=%x, hostname=%s...",
3365 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3370 * Queue doesn't exist; add it...
3374 p
= cupsdAddClass(name
);
3376 p
= cupsdAddPrinter(name
);
3381 cupsdClearString(&(p
->hostname
));
3383 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3384 is_class
? "class" : "printer", name
);
3386 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3387 "%s \'%s\' added by directory services.",
3388 is_class
? "Class" : "Printer", name
);
3391 * Force the URI to point to the real server...
3394 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3397 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3403 * Hostname not set, so this must be a cached remote printer
3404 * that was created for a pending print job...
3407 cupsdSetString(&p
->hostname
, host
);
3408 cupsdSetString(&p
->uri
, uri
);
3409 cupsdSetString(&p
->device_uri
, uri
);
3412 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3416 * Update the state...
3420 p
->browse_time
= time(NULL
);
3422 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3426 * Grab the lease-duration for the browse data; anything less then 1
3427 * second or more than 1 week gets the default BrowseTimeout...
3430 i
= atoi(lease_duration
);
3431 if (i
< 1 || i
> 604800)
3434 p
->browse_expire
= p
->browse_time
+ i
;
3437 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3439 if (type
& CUPS_PRINTER_REJECTING
)
3441 type
&= ~CUPS_PRINTER_REJECTING
;
3449 else if (!p
->accepting
)
3455 if (p
->type
!= type
)
3461 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3463 cupsdSetString(&p
->location
, location
);
3467 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3469 cupsdSetString(&p
->info
, info
);
3472 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3475 if (!make_model
|| !make_model
[0])
3478 snprintf(local_make_model
, sizeof(local_make_model
),
3479 "Remote Class on %s", host
);
3481 snprintf(local_make_model
, sizeof(local_make_model
),
3482 "Remote Printer on %s", host
);
3485 snprintf(local_make_model
, sizeof(local_make_model
),
3486 "%s on %s", make_model
, host
);
3488 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3490 cupsdSetString(&p
->make_model
, local_make_model
);
3496 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3499 * See if we need to update the attributes...
3502 if (p
->num_options
!= num_attrs
)
3506 for (i
= 0; i
< num_attrs
; i
++)
3507 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3508 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3509 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3518 * Free the old options...
3521 cupsFreeOptions(p
->num_options
, p
->options
);
3524 p
->num_options
= num_attrs
;
3527 if (type
& CUPS_PRINTER_DELETE
)
3529 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3530 "%s \'%s\' deleted by directory services.",
3531 is_class
? "Class" : "Printer", p
->name
);
3533 cupsdExpireSubscriptions(p
, NULL
);
3535 cupsdDeletePrinter(p
, 1);
3536 cupsdUpdateImplicitClasses();
3537 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3541 cupsdSetPrinterAttrs(p
);
3542 cupsdUpdateImplicitClasses();
3546 * See if we have a default printer... If not, make the first network
3547 * default printer the default.
3550 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3553 * Find the first network default printer and use it...
3556 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3558 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3559 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3562 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3568 * Do auto-classing if needed...
3571 process_implicit_classes();
3576 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3580 process_implicit_classes(void)
3582 int i
; /* Looping var */
3583 int update
; /* Update printer attributes? */
3584 char name
[IPP_MAX_NAME
], /* Name of printer */
3585 *hptr
; /* Pointer into hostname */
3586 cupsd_printer_t
*p
, /* Printer information */
3587 *pclass
, /* Printer class */
3588 *first
; /* First printer in class */
3589 int offset
, /* Offset of name */
3590 len
; /* Length of name */
3593 if (!ImplicitClasses
|| !Printers
)
3597 * Loop through all available printers and create classes as needed...
3600 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3601 update
= 0, pclass
= NULL
, first
= NULL
;
3603 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3606 * Skip implicit classes...
3609 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3616 * If len == 0, get the length of this printer name up to the "@"
3620 cupsArraySave(Printers
);
3623 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3624 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3627 * We have more than one printer with the same name; see if
3628 * we have a class, and if this printer is a member...
3631 if (pclass
&& strcasecmp(pclass
->name
, name
))
3634 cupsdSetPrinterAttrs(pclass
);
3640 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3643 * Need to add the class...
3646 pclass
= cupsdAddPrinter(name
);
3647 cupsArrayAdd(ImplicitPrinters
, pclass
);
3649 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3650 pclass
->accepting
= 1;
3651 pclass
->state
= IPP_PRINTER_IDLE
;
3653 cupsdSetString(&pclass
->location
, p
->location
);
3654 cupsdSetString(&pclass
->info
, p
->info
);
3656 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3657 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3661 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3663 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3665 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3666 "Implicit class \'%s\' added by directory services.",
3672 for (i
= 0; i
< pclass
->num_printers
; i
++)
3673 if (pclass
->printers
[i
] == first
)
3676 if (i
>= pclass
->num_printers
)
3678 first
->in_implicit_class
= 1;
3679 cupsdAddPrinterToClass(pclass
, first
);
3685 for (i
= 0; i
< pclass
->num_printers
; i
++)
3686 if (pclass
->printers
[i
] == p
)
3689 if (i
>= pclass
->num_printers
)
3691 p
->in_implicit_class
= 1;
3692 cupsdAddPrinterToClass(pclass
, p
);
3699 * First time around; just get name length and mark it as first
3703 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3704 len
= hptr
- p
->name
;
3706 len
= strlen(p
->name
);
3708 if (len
>= sizeof(name
))
3711 * If the printer name length somehow is greater than we normally allow,
3712 * skip this printer...
3716 cupsArrayRestore(Printers
);
3720 strncpy(name
, p
->name
, len
);
3724 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3725 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3728 * Can't use same name as a local printer; add "Any" to the
3729 * front of the name, unless we have explicitly disabled
3730 * the "ImplicitAnyClasses"...
3733 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3736 * Add "Any" to the class name...
3739 strcpy(name
, "Any");
3740 strncpy(name
+ 3, p
->name
, len
);
3741 name
[len
+ 3] = '\0';
3747 * Don't create an implicit class if we have a local printer
3748 * with the same name...
3752 cupsArrayRestore(Printers
);
3760 cupsArrayRestore(Printers
);
3764 * Update the last printer class as needed...
3767 if (pclass
&& update
)
3768 cupsdSetPrinterAttrs(pclass
);
3773 * 'send_cups_browse()' - Send new browsing information using the CUPS
3778 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3780 int i
; /* Looping var */
3781 cups_ptype_t type
; /* Printer type */
3782 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3783 int bytes
; /* Length of packet */
3784 char packet
[1453], /* Browse data packet */
3785 uri
[1024], /* Printer URI */
3786 location
[1024], /* printer-location */
3787 info
[1024], /* printer-info */
3789 /* printer-make-and-model */
3790 air
[1024]; /* auth-info-required */
3791 cupsd_netif_t
*iface
; /* Network interface */
3795 * Figure out the printer type value...
3798 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3801 type
|= CUPS_PRINTER_REJECTING
;
3803 if (p
== DefaultPrinter
)
3804 type
|= CUPS_PRINTER_DEFAULT
;
3807 * Remove quotes from printer-info, printer-location, and
3808 * printer-make-and-model attributes...
3811 dequote(location
, p
->location
, sizeof(location
));
3812 dequote(info
, p
->info
, sizeof(info
));
3815 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3816 else if (p
->type
& CUPS_PRINTER_CLASS
)
3818 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3819 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3821 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3824 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3826 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3828 if (get_auth_info_required(p
, packet
, sizeof(packet
)))
3829 snprintf(air
, sizeof(air
), " auth-info-required=%s", packet
);
3834 * Send a packet to each browse address...
3837 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3841 * Send the browse packet to one or more interfaces...
3844 if (!strcmp(b
->iface
, "*"))
3847 * Send to all local interfaces...
3852 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3854 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3857 * Only send to local, IPv4 interfaces...
3860 if (!iface
->is_local
|| !iface
->port
||
3861 iface
->address
.addr
.sa_family
!= AF_INET
)
3864 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3865 iface
->hostname
, iface
->port
,
3866 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3869 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3870 type
, p
->state
, uri
, location
, info
, make_model
,
3871 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3873 bytes
= strlen(packet
);
3875 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3876 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3877 iface
->name
, packet
);
3879 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3881 sendto(BrowseSocket
, packet
, bytes
, 0,
3882 (struct sockaddr
*)&(iface
->broadcast
),
3883 httpAddrLength(&(iface
->broadcast
)));
3886 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3889 * Send to the named interface using the IPv4 address...
3893 if (strcmp(b
->iface
, iface
->name
))
3898 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3901 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3905 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3906 iface
->hostname
, iface
->port
,
3907 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3910 snprintf(packet
, sizeof(packet
),
3911 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3912 type
, p
->state
, uri
, location
, info
, make_model
,
3913 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3915 bytes
= strlen(packet
);
3917 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3918 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3919 iface
->name
, packet
);
3921 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3923 sendto(BrowseSocket
, packet
, bytes
, 0,
3924 (struct sockaddr
*)&(iface
->broadcast
),
3925 httpAddrLength(&(iface
->broadcast
)));
3932 * Send the browse packet to the indicated address using
3933 * the default server name...
3936 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3937 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3938 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3940 bytes
= strlen(packet
);
3941 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3942 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3944 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3945 (struct sockaddr
*)&(b
->to
),
3946 httpAddrLength(&(b
->to
))) <= 0)
3949 * Unable to send browse packet, so remove this address from the
3953 cupsdLogMessage(CUPSD_LOG_ERROR
,
3954 "cupsdSendBrowseList: sendto failed for browser "
3956 (int)(b
- Browsers
+ 1), strerror(errno
));
3959 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3970 * 'ldap_search_rec()' - LDAP Search with reconnect
3973 static int /* O - Return code */
3974 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3975 char *base
, /* I - Base dn */
3976 int scope
, /* I - LDAP search scope */
3977 char *filter
, /* I - Filter string */
3978 char *attrs
[], /* I - Requested attributes */
3979 int attrsonly
, /* I - Return only attributes? */
3980 LDAPMessage
**res
) /* I - LDAP handler */
3982 int rc
; /* Return code */
3983 LDAP
*ldr
; /* LDAP handler after reconnect */
3986 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3987 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3988 NULL
, LDAP_NO_LIMIT
, res
);
3990 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3991 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3994 * If we have a connection problem try again...
3997 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3999 cupsdLogMessage(CUPSD_LOG_ERROR
,
4000 "LDAP search failed with status %d: %s",
4001 rc
, ldap_err2string(rc
));
4002 cupsdLogMessage(CUPSD_LOG_INFO
,
4003 "We try the LDAP search once again after reconnecting to "
4006 ldr
= ldap_reconnect();
4008 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4009 rc
= ldap_search_ext_s(ldr
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
4010 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
4012 rc
= ldap_search_s(ldr
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4013 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4016 if (rc
== LDAP_NO_SUCH_OBJECT
)
4017 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4018 "ldap_search_rec: LDAP entry/object not found");
4019 else if (rc
!= LDAP_SUCCESS
)
4020 cupsdLogMessage(CUPSD_LOG_ERROR
,
4021 "ldap_search_rec: LDAP search failed with status %d: %s",
4022 rc
, ldap_err2string(rc
));
4024 if (rc
!= LDAP_SUCCESS
)
4032 * 'ldap_freeres()' - Free LDAPMessage
4036 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
4038 int rc
; /* Return value */
4041 rc
= ldap_msgfree(entry
);
4043 cupsdLogMessage(CUPSD_LOG_WARN
, "Can't free LDAPMessage!");
4045 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Freeing LDAPMessage was unnecessary");
4050 * 'ldap_getval_char()' - Get first LDAP value and convert to string
4053 static int /* O - Return code */
4054 ldap_getval_firststring(
4055 LDAP
*ld
, /* I - LDAP handler */
4056 LDAPMessage
*entry
, /* I - LDAP message or search result */
4057 char *attr
, /* I - the wanted attribute */
4058 char *retval
, /* O - String to return */
4059 unsigned long maxsize
) /* I - Max string size */
4061 char *dn
; /* LDAP DN */
4062 int rc
= 0; /* Return code */
4063 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4064 struct berval
**bval
; /* LDAP value array */
4065 unsigned long size
; /* String size */
4069 * Get value from LDAPMessage...
4072 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
4075 dn
= ldap_get_dn(ld
, entry
);
4076 cupsdLogMessage(CUPSD_LOG_WARN
,
4077 "Failed to get LDAP value %s for %s!",
4084 * Check size and copy value into our string...
4088 if (size
< (bval
[0]->bv_len
+ 1))
4091 dn
= ldap_get_dn(ld
, entry
);
4092 cupsdLogMessage(CUPSD_LOG_WARN
,
4093 "Attribute %s is too big! (dn: %s)",
4098 size
= bval
[0]->bv_len
+ 1;
4100 strlcpy(retval
, bval
[0]->bv_val
, size
);
4101 ldap_value_free_len(bval
);
4104 char **value
; /* LDAP value */
4107 * Get value from LDAPMessage...
4110 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4113 dn
= ldap_get_dn(ld
, entry
);
4114 cupsdLogMessage(CUPSD_LOG_WARN
, "Failed to get LDAP value %s for %s!",
4120 strlcpy(retval
, *value
, maxsize
);
4121 ldap_value_free(value
);
4123 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4130 * 'send_ldap_ou()' - Send LDAP ou registrations.
4134 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4135 char *basedn
, /* I - Our base dn */
4136 char *descstring
) /* I - Description for ou */
4138 int i
; /* Looping var... */
4139 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4140 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4141 LDAPMessage
*res
, /* Search result token */
4142 *e
; /* Current entry from search */
4143 int rc
; /* LDAP status */
4144 int rcmod
; /* LDAP status for modifications */
4145 char dn
[1024], /* DN of the organizational unit we are adding */
4146 *desc
[2], /* Change records */
4148 char old_desc
[1024]; /* Old description */
4149 static const char * const objectClass_values
[] =
4150 { /* The 2 objectClass's we use in */
4151 "top", /* our LDAP entries */
4152 "organizationalUnit",
4155 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4162 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4165 * Reconnect if LDAP Handle is invalid...
4168 if (!BrowseLDAPHandle
)
4170 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4171 "send_ldap_ou: LDAP Handle is invalid. Try reconnecting...");
4177 * Prepare ldap search...
4180 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4181 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4185 desc
[0] = descstring
;
4188 mods
[0].mod_type
= "ou";
4189 mods
[0].mod_values
= ou_value
;
4190 mods
[1].mod_type
= "description";
4191 mods
[1].mod_values
= desc
;
4192 mods
[2].mod_type
= "objectClass";
4193 mods
[2].mod_values
= (char **)objectClass_values
;
4195 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4196 (char **)ou_attrs
, 0, &res
);
4199 * If ldap search was not successfull then exit function...
4202 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4206 * Check if we need to insert or update the LDAP entry...
4209 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4210 rc
!= LDAP_NO_SUCH_OBJECT
)
4213 * Printserver has already been registered, check if
4214 * modification is required...
4217 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4220 * Get the required values from this entry...
4223 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4224 sizeof(old_desc
)) == -1)
4228 * Check if modification is required...
4231 if ( strcmp(desc
[0], old_desc
) == 0 )
4234 * LDAP entry for the printer exists.
4235 * Printer has already been registered,
4236 * no modifications required...
4238 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4239 "send_ldap_ou: No updates required for %s", ou
);
4244 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4245 "send_ldap_ou: Replace entry for %s", ou
);
4247 for (i
= 0; i
< 3; i
++)
4249 pmods
[i
] = mods
+ i
;
4250 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4254 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4255 if ((rcmod
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4256 NULL
)) != LDAP_SUCCESS
)
4258 if ((rcmod
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4259 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4261 cupsdLogMessage(CUPSD_LOG_ERROR
,
4262 "LDAP modify for %s failed with status %d: %s",
4263 ou
, rcmod
, ldap_err2string(rcmod
));
4264 if (rcmod
== LDAP_SERVER_DOWN
)
4272 * Printserver has never been registered,
4273 * add registration...
4276 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4277 "send_ldap_ou: Add entry for %s", ou
);
4279 for (i
= 0; i
< 3; i
++)
4281 pmods
[i
] = mods
+ i
;
4282 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4286 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4287 if ((rcmod
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4288 NULL
)) != LDAP_SUCCESS
)
4290 if ((rcmod
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4291 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4293 cupsdLogMessage(CUPSD_LOG_ERROR
,
4294 "LDAP add for %s failed with status %d: %s",
4295 ou
, rcmod
, ldap_err2string(rcmod
));
4296 if (rcmod
== LDAP_SERVER_DOWN
)
4301 if (rc
== LDAP_SUCCESS
)
4307 * 'send_ldap_browse()' - Send LDAP printer registrations.
4311 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4313 int i
; /* Looping var... */
4314 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4315 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4316 LDAPMessage
*res
, /* Search result token */
4317 *e
; /* Current entry from search */
4318 char *cn_value
[2], /* Change records */
4324 typestring
[255], /* String to hold printer-type */
4325 dn
[1024]; /* DN of the printer we are adding */
4326 int rc
; /* LDAP status */
4327 int rcmod
; /* LDAP status for modifications */
4328 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4329 old_location
[1024], /* Printer location */
4330 old_info
[1024], /* Printer information */
4331 old_make_model
[1024], /* Printer make and model */
4332 old_type_string
[30]; /* Temporary type number */
4333 int old_type
; /* Printer type */
4334 static const char * const objectClass_values
[] =
4335 { /* The 3 objectClass's we use in */
4336 "top", /* our LDAP entries */
4343 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4346 * Exit function if LDAP updates has been disabled...
4349 if (!BrowseLDAPUpdate
)
4351 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4352 "send_ldap_browse: Updates temporary disabled; "
4358 * Reconnect if LDAP Handle is invalid...
4361 if (!BrowseLDAPHandle
)
4363 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4364 "send_ldap_browse: LDAP Handle is invalid. Try "
4371 * Everything in ldap is ** so we fudge around it...
4374 sprintf(typestring
, "%u", p
->type
);
4376 cn_value
[0] = p
->name
;
4378 info
[0] = p
->info
? p
->info
: "Unknown";
4380 location
[0] = p
->location
? p
->location
: "Unknown";
4382 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4383 make_model
[1] = NULL
;
4384 type
[0] = typestring
;
4390 * Get ldap entry for printer ...
4393 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4395 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4397 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4398 (char **)ldap_attrs
, 0, &res
);
4401 * If ldap search was not successfull then exit function
4402 * and temporary disable LDAP updates...
4405 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4407 if (BrowseLDAPUpdate
&&
4408 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4410 BrowseLDAPUpdate
= FALSE
;
4411 cupsdLogMessage(CUPSD_LOG_INFO
,
4412 "LDAP update temporary disabled");
4419 * Fill modification array...
4422 mods
[0].mod_type
= "cn";
4423 mods
[0].mod_values
= cn_value
;
4424 mods
[1].mod_type
= "printerDescription";
4425 mods
[1].mod_values
= info
;
4426 mods
[2].mod_type
= "printerURI";
4427 mods
[2].mod_values
= uri
;
4428 mods
[3].mod_type
= "printerLocation";
4429 mods
[3].mod_values
= location
;
4430 mods
[4].mod_type
= "printerMakeAndModel";
4431 mods
[4].mod_values
= make_model
;
4432 mods
[5].mod_type
= "printerType";
4433 mods
[5].mod_values
= type
;
4434 mods
[6].mod_type
= "objectClass";
4435 mods
[6].mod_values
= (char **)objectClass_values
;
4438 * Check if we need to insert or update the LDAP entry...
4441 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4442 rc
!= LDAP_NO_SUCH_OBJECT
)
4445 * Printer has already been registered, check if
4446 * modification is required...
4449 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4452 * Get the required values from this entry...
4455 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4456 old_info
, sizeof(old_info
)) == -1)
4459 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4460 old_location
, sizeof(old_location
)) == -1)
4463 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4464 old_make_model
, sizeof(old_make_model
)) == -1)
4467 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4468 old_type_string
, sizeof(old_type_string
)) == -1)
4471 old_type
= atoi(old_type_string
);
4473 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4474 sizeof(old_uri
)) == -1)
4478 * Check if modification is required...
4481 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4482 !strcmp(location
[0], old_location
) &&
4483 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4486 * LDAP entry for the printer exists. Printer has already been registered,
4487 * no modifications required...
4490 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4491 "send_ldap_browse: No updates required for %s", p
->name
);
4496 * LDAP entry for the printer exists. Printer has already been registered,
4497 * modify the current registration...
4500 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4501 "send_ldap_browse: Replace entry for %s", p
->name
);
4503 for (i
= 0; i
< 7; i
++)
4505 pmods
[i
] = mods
+ i
;
4506 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4510 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4511 if ((rcmod
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4512 NULL
)) != LDAP_SUCCESS
)
4514 if ((rcmod
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4515 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4517 cupsdLogMessage(CUPSD_LOG_ERROR
,
4518 "LDAP modify for %s failed with status %d: %s",
4519 p
->name
, rcmod
, ldap_err2string(rcmod
));
4520 if (rcmod
== LDAP_SERVER_DOWN
)
4528 * No LDAP entry exists for the printer. Printer has never been registered,
4529 * add the current registration...
4532 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4534 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4535 "send_ldap_browse: Add entry for %s", p
->name
);
4537 for (i
= 0; i
< 7; i
++)
4539 pmods
[i
] = mods
+ i
;
4540 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4544 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4545 if ((rcmod
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4546 NULL
)) != LDAP_SUCCESS
)
4548 if ((rcmod
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4549 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4551 cupsdLogMessage(CUPSD_LOG_ERROR
,
4552 "LDAP add for %s failed with status %d: %s",
4553 p
->name
, rcmod
, ldap_err2string(rcmod
));
4554 if (rcmod
== LDAP_SERVER_DOWN
)
4559 if (rc
== LDAP_SUCCESS
)
4565 * 'ldap_dereg_printer()' - Delete printer from directory
4569 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4571 char dn
[1024]; /* DN of the printer */
4572 int rc
; /* LDAP status */
4575 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4579 * Reconnect if LDAP Handle is invalid...
4582 if (!BrowseLDAPHandle
)
4589 * Get dn for printer and delete LDAP entry...
4592 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4594 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4596 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4597 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4598 NULL
)) != LDAP_SUCCESS
)
4600 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4601 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4603 cupsdLogMessage(CUPSD_LOG_WARN
,
4604 "LDAP delete for %s failed with status %d: %s",
4605 p
->name
, rc
, ldap_err2string(rc
));
4608 * If we had a connection problem (connection timed out, etc.)
4609 * we should reconnect and try again to delete the entry...
4612 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4614 cupsdLogMessage(CUPSD_LOG_INFO
,
4615 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4618 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4619 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4620 NULL
)) != LDAP_SUCCESS
)
4622 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4623 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4624 cupsdLogMessage(CUPSD_LOG_WARN
,
4625 "LDAP delete for %s failed with status %d: %s",
4626 p
->name
, rc
, ldap_err2string(rc
));
4633 * 'ldap_dereg_ou()' - Remove the organizational unit.
4637 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4638 char *basedn
) /* I - Dase dn */
4640 char dn
[1024]; /* DN of the printer */
4641 int rc
; /* LDAP status */
4644 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4647 * Reconnect if LDAP Handle is invalid...
4650 if (!BrowseLDAPHandle
)
4657 * Get dn for printer and delete LDAP entry...
4660 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4661 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4663 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4664 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4665 NULL
)) != LDAP_SUCCESS
)
4667 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4668 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4670 cupsdLogMessage(CUPSD_LOG_WARN
,
4671 "LDAP delete for %s failed with status %d: %s",
4672 ou
, rc
, ldap_err2string(rc
));
4675 * If we had a connection problem (connection timed out, etc.)
4676 * we should reconnect and try again to delete the entry...
4679 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4681 cupsdLogMessage(CUPSD_LOG_INFO
,
4682 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4684 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4685 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4686 NULL
)) != LDAP_SUCCESS
)
4688 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4689 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4690 cupsdLogMessage(CUPSD_LOG_WARN
,
4691 "LDAP delete for %s failed with status %d: %s",
4692 ou
, rc
, ldap_err2string(rc
));
4696 #endif /* HAVE_LDAP */
4701 * 'send_slp_browse()' - Register the specified printer with SLP.
4705 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4707 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4708 attrs
[8192], /* Printer attributes */
4709 finishings
[1024], /* Finishings to support */
4710 make_model
[IPP_MAX_NAME
* 2],
4711 /* Make and model, quoted */
4712 location
[IPP_MAX_NAME
* 2],
4713 /* Location, quoted */
4714 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4715 *src
, /* Pointer to original string */
4716 *dst
; /* Pointer to destination string */
4717 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4718 SLPError error
; /* SLP error, if any */
4721 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4725 * Make the SLP service URL that conforms to the IANA
4726 * 'printer:' template.
4729 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4731 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4734 * Figure out the finishings string...
4737 if (p
->type
& CUPS_PRINTER_STAPLE
)
4738 strcpy(finishings
, "staple");
4740 finishings
[0] = '\0';
4742 if (p
->type
& CUPS_PRINTER_BIND
)
4745 strlcat(finishings
, ",bind", sizeof(finishings
));
4747 strcpy(finishings
, "bind");
4750 if (p
->type
& CUPS_PRINTER_PUNCH
)
4753 strlcat(finishings
, ",punch", sizeof(finishings
));
4755 strcpy(finishings
, "punch");
4758 if (p
->type
& CUPS_PRINTER_COVER
)
4761 strlcat(finishings
, ",cover", sizeof(finishings
));
4763 strcpy(finishings
, "cover");
4766 if (p
->type
& CUPS_PRINTER_SORT
)
4769 strlcat(finishings
, ",sort", sizeof(finishings
));
4771 strcpy(finishings
, "sort");
4775 strcpy(finishings
, "none");
4778 * Quote any commas in the make and model, location, and info strings...
4781 for (src
= p
->make_model
, dst
= make_model
;
4782 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4784 if (*src
== ',' || *src
== '\\' || *src
== ')')
4793 strcpy(make_model
, "Unknown");
4795 for (src
= p
->location
, dst
= location
;
4796 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4798 if (*src
== ',' || *src
== '\\' || *src
== ')')
4807 strcpy(location
, "Unknown");
4809 for (src
= p
->info
, dst
= info
;
4810 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4812 if (*src
== ',' || *src
== '\\' || *src
== ')')
4821 strcpy(info
, "Unknown");
4824 * Get the authentication value...
4827 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4831 * Make the SLP attribute string list that conforms to
4832 * the IANA 'printer:' template.
4835 snprintf(attrs
, sizeof(attrs
),
4836 "(printer-uri-supported=%s),"
4837 "(uri-authentication-supported=%s>),"
4839 "(uri-security-supported=tls>),"
4841 "(uri-security-supported=none>),"
4842 #endif /* HAVE_SSL */
4843 "(printer-name=%s),"
4844 "(printer-location=%s),"
4845 "(printer-info=%s),"
4846 "(printer-more-info=%s),"
4847 "(printer-make-and-model=%s),"
4848 "(printer-type=%d),"
4849 "(charset-supported=utf-8),"
4850 "(natural-language-configured=%s),"
4851 "(natural-language-supported=de,en,es,fr,it),"
4852 "(color-supported=%s),"
4853 "(finishings-supported=%s),"
4854 "(sides-supported=one-sided%s),"
4855 "(multiple-document-jobs-supported=true)"
4856 "(ipp-versions-supported=1.0,1.1)",
4857 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4858 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4859 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4861 p
->type
& CUPS_PRINTER_DUPLEX
?
4862 ",two-sided-long-edge,two-sided-short-edge" : "");
4864 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4867 * Register the printer with the SLP server...
4870 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4871 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4873 if (error
!= SLP_OK
)
4874 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4880 * 'slp_attr_callback()' - SLP attribute callback
4883 static SLPBoolean
/* O - SLP_TRUE for success */
4885 SLPHandle hslp
, /* I - SLP handle */
4886 const char *attrlist
, /* I - Attribute list */
4887 SLPError errcode
, /* I - Parsing status for this attr */
4888 void *cookie
) /* I - Current printer */
4890 char *tmp
= 0; /* Temporary string */
4891 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4892 /* Current printer */
4895 (void)hslp
; /* anti-compiler-warning-code */
4898 * Bail if there was an error
4901 if (errcode
!= SLP_OK
)
4905 * Parse the attrlist to obtain things needed to build CUPS browse packet
4908 memset(p
, 0, sizeof(cupsd_printer_t
));
4910 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4912 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4914 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4916 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4917 p
->type
= atoi(tmp
);
4919 p
->type
= CUPS_PRINTER_REMOTE
;
4921 cupsdClearString(&tmp
);
4928 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4932 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4934 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4937 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4939 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4942 * Make the SLP service URL that conforms to the IANA
4943 * 'printer:' template.
4946 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4949 * Deregister the printer...
4952 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4958 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4961 static int /* O - 0 on success */
4962 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4963 const char *tag
, /* I - Name of attribute */
4964 char **valbuf
) /* O - Value */
4966 char *ptr1
, /* Pointer into string */
4970 cupsdClearString(valbuf
);
4972 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4974 ptr1
+= strlen(tag
);
4976 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4982 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4983 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4986 * Dequote the value...
4989 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4990 if (*ptr1
== '\\' && ptr1
[1])
4991 _cups_strcpy(ptr1
, ptr1
+ 1);
5002 * 'slp_reg_callback()' - Empty SLPRegReport.
5006 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
5007 SLPError errcode
, /* I - Error code, if any */
5008 void *cookie
) /* I - App data */
5019 * 'slp_url_callback()' - SLP service url callback
5022 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
5024 SLPHandle hslp
, /* I - SLP handle */
5025 const char *srvurl
, /* I - URL of service */
5026 unsigned short lifetime
, /* I - Life of service */
5027 SLPError errcode
, /* I - Existing error code */
5028 void *cookie
) /* I - Pointer to service list */
5030 slpsrvurl_t
*s
, /* New service entry */
5031 **head
; /* Pointer to head of entry */
5035 * Let the compiler know we won't be using these vars...
5042 * Bail if there was an error
5045 if (errcode
!= SLP_OK
)
5049 * Grab the head of the list...
5052 head
= (slpsrvurl_t
**)cookie
;
5055 * Allocate a *temporary* slpsrvurl_t to hold this entry.
5058 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
5062 * Copy the SLP service URL...
5065 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
5068 * Link the SLP service URL into the head of the list
5078 #endif /* HAVE_LIBSLP */
5082 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
5086 update_cups_browse(void)
5088 int i
; /* Looping var */
5089 int auth
; /* Authorization status */
5090 int len
; /* Length of name string */
5091 int bytes
; /* Number of bytes left */
5092 char packet
[1541], /* Broadcast packet */
5093 *pptr
; /* Pointer into packet */
5094 socklen_t srclen
; /* Length of source address */
5095 http_addr_t srcaddr
; /* Source address */
5096 char srcname
[1024]; /* Source hostname */
5097 unsigned address
[4]; /* Source address */
5098 unsigned type
; /* Printer type */
5099 unsigned state
; /* Printer state */
5100 char uri
[HTTP_MAX_URI
], /* Printer URI */
5101 host
[HTTP_MAX_URI
], /* Host portion of URI */
5102 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
5103 info
[IPP_MAX_NAME
], /* Information string */
5104 location
[IPP_MAX_NAME
], /* Location string */
5105 make_model
[IPP_MAX_NAME
];/* Make and model string */
5106 int num_attrs
; /* Number of attributes */
5107 cups_option_t
*attrs
; /* Attributes */
5111 * Read a packet from the browse socket...
5114 srclen
= sizeof(srcaddr
);
5115 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5116 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5119 * "Connection refused" is returned under Linux if the destination port
5120 * or address is unreachable from a previous sendto(); check for the
5121 * error here and ignore it for now...
5124 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5126 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5128 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5131 closesocket(BrowseSocket
);
5133 close(BrowseSocket
);
5136 cupsdRemoveSelect(BrowseSocket
);
5139 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5140 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5146 packet
[bytes
] = '\0';
5149 * If we're about to sleep, ignore incoming browse packets.
5156 * Figure out where it came from...
5160 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5162 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5163 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5164 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5165 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5168 #endif /* AF_INET6 */
5173 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5176 if (HostNameLookups
)
5177 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5179 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5181 len
= strlen(srcname
);
5189 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5192 * Access from localhost (127.0.0.1) is always allowed...
5195 auth
= CUPSD_AUTH_ALLOW
;
5200 * Do authorization checks on the domain/address...
5203 switch (BrowseACL
->order_type
)
5206 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5209 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5210 auth
= CUPSD_AUTH_ALLOW
;
5212 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->deny
))
5213 auth
= CUPSD_AUTH_DENY
;
5215 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->allow
))
5216 auth
= CUPSD_AUTH_ALLOW
;
5219 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5220 auth
= CUPSD_AUTH_DENY
;
5222 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->allow
))
5223 auth
= CUPSD_AUTH_ALLOW
;
5225 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->deny
))
5226 auth
= CUPSD_AUTH_DENY
;
5232 auth
= CUPSD_AUTH_ALLOW
;
5234 if (auth
== CUPSD_AUTH_DENY
)
5236 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5237 "update_cups_browse: Refused %d bytes from %s", bytes
,
5242 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5243 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5250 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5252 cupsdLogMessage(CUPSD_LOG_WARN
,
5253 "update_cups_browse: Garbled browse packet - %s", packet
);
5257 strcpy(location
, "Location Unknown");
5258 strcpy(info
, "No Information Available");
5259 make_model
[0] = '\0';
5263 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5266 * Have extended information; can't use sscanf for it because not all
5267 * sscanf's allow empty strings with %[^\"]...
5270 for (i
= 0, pptr
++;
5271 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5273 location
[i
] = *pptr
;
5281 while (*pptr
&& isspace(*pptr
& 255))
5286 for (i
= 0, pptr
++;
5287 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5296 while (*pptr
&& isspace(*pptr
& 255))
5301 for (i
= 0, pptr
++;
5302 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5304 make_model
[i
] = *pptr
;
5309 make_model
[i
] = '\0';
5312 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5318 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5319 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5320 type
, state
, uri
, location
, info
, make_model
));
5323 * Pull the URI apart to see if this is a local or remote printer...
5326 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5328 cupsFreeOptions(num_attrs
, attrs
);
5336 for (i
= 0; i
< NumRelays
; i
++)
5337 if (cupsdCheckAuth(address
, srcname
, len
, Relays
[i
].from
))
5338 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5339 (struct sockaddr
*)&(Relays
[i
].to
),
5340 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5342 cupsdLogMessage(CUPSD_LOG_ERROR
,
5343 "update_cups_browse: sendto failed for relay %d - %s.",
5344 i
+ 1, strerror(errno
));
5345 cupsFreeOptions(num_attrs
, attrs
);
5350 * Process the browse data...
5353 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5354 (ipp_pstate_t
)state
, location
, info
, make_model
,
5360 * 'update_lpd()' - Update the LPD configuration as needed.
5364 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5371 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5372 * setting for backwards-compatibility.
5375 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5377 #endif /* __APPLE__ */
5379 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5382 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5385 char newfile
[1024]; /* New cups-lpd.N file */
5386 cups_file_t
*ofp
, /* Original file pointer */
5387 *nfp
; /* New file pointer */
5388 char line
[1024]; /* Line from file */
5391 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5393 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5395 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5396 LPDConfigFile
+ 9, strerror(errno
));
5400 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5402 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5403 newfile
, strerror(errno
));
5409 * Copy all of the lines from the cups-lpd file...
5412 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5416 cupsFilePrintf(nfp
, "%s\n", line
);
5417 snprintf(line
, sizeof(line
), "\tdisable = %s",
5418 onoff
? "no" : "yes");
5420 else if (!strstr(line
, "disable ="))
5421 cupsFilePrintf(nfp
, "%s\n", line
);
5426 rename(newfile
, LPDConfigFile
+ 9);
5429 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5432 * Enable/disable LPD via the launchctl command...
5435 char *argv
[5], /* Arguments for command */
5436 *envp
[MAX_ENV
]; /* Environment for command */
5437 int pid
; /* Process ID */
5440 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5441 argv
[0] = (char *)"launchctl";
5442 argv
[1] = (char *)(onoff
? "load" : "unload");
5443 argv
[2] = (char *)"-w";
5444 argv
[3] = LPDConfigFile
+ 10;
5447 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5450 #endif /* __APPLE__ */
5452 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5457 * 'update_polling()' - Read status messages from the poll daemons.
5461 update_polling(void)
5463 char *ptr
, /* Pointer to end of line in buffer */
5464 message
[1024]; /* Pointer to message text */
5465 int loglevel
; /* Log level for message */
5468 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5469 message
, sizeof(message
))) != NULL
)
5471 if (loglevel
== CUPSD_LOG_INFO
)
5472 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5474 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5478 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5481 * All polling processes have died; stop polling...
5484 cupsdLogMessage(CUPSD_LOG_ERROR
,
5485 "update_polling: all polling processes have exited!");
5492 * 'update_smb()' - Update the SMB configuration as needed.
5496 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5501 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5504 * Enable/disable SMB via the specified smb.conf config file...
5507 char newfile
[1024]; /* New smb.conf.N file */
5508 cups_file_t
*ofp
, /* Original file pointer */
5509 *nfp
; /* New file pointer */
5510 char line
[1024]; /* Line from file */
5511 int in_printers
; /* In [printers] section? */
5514 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5516 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5518 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5519 SMBConfigFile
+ 8, strerror(errno
));
5523 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5525 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5526 newfile
, strerror(errno
));
5532 * Copy all of the lines from the smb.conf file...
5537 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5539 if (in_printers
&& strstr(line
, "printable ="))
5540 snprintf(line
, sizeof(line
), " printable = %s",
5541 onoff
? "yes" : "no");
5543 cupsFilePrintf(nfp
, "%s\n", line
);
5546 in_printers
= !strcmp(line
, "[printers]");
5551 rename(newfile
, SMBConfigFile
+ 8);
5554 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5559 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".