2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * ldap_rebind_proc() - Callback function for LDAP rebind
27 * ldap_connect() - Start new LDAP connection
28 * ldap_reconnect() - Reconnect to LDAP Server
29 * ldap_disconnect() - Disconnect from LDAP Server
30 * cupsdStartBrowsing() - Start sending and receiving broadcast
32 * cupsdStartPolling() - Start polling servers as needed.
33 * cupsdStopBrowsing() - Stop sending and receiving broadcast
35 * cupsdStopPolling() - Stop polling servers as needed.
36 * cupsdUpdateDNSSDName() - Update the computer name we use for
38 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
39 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
40 * dequote() - Remote quotes from a string.
41 * 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_hostconfig() - Get an /etc/hostconfig service setting.
54 * is_local_queue() - Determine whether the URI points at a local
56 * process_browse_data() - Process new browse data.
57 * process_implicit_classes() - Create/update implicit classes as needed.
58 * send_cups_browse() - Send new browsing information using the CUPS
60 * ldap_search_rec() - LDAP Search with reconnect
61 * ldap_freeres() - Free LDAPMessage
62 * ldap_getval_char() - Get first LDAP value and convert to string
63 * send_ldap_ou() - Send LDAP ou registrations.
64 * send_ldap_browse() - Send LDAP printer registrations.
65 * ldap_dereg_printer() - Delete printer from directory
66 * send_slp_browse() - Register the specified printer with SLP.
67 * slp_attr_callback() - SLP attribute callback
68 * slp_dereg_printer() - SLPDereg() the specified printer
69 * slp_get_attr() - Get an attribute from an SLP registration.
70 * slp_reg_callback() - Empty SLPRegReport.
71 * slp_url_callback() - SLP service url callback
72 * update_cups_browse() - Update the browse lists using the CUPS
74 * update_lpd() - Update the LPD configuration as needed.
75 * update_polling() - Read status messages from the poll daemons.
76 * update_smb() - Update the SMB configuration as needed.
80 * Include necessary headers...
90 # ifdef HAVE_COREFOUNDATION
91 # include <CoreFoundation/CoreFoundation.h>
92 # endif /* HAVE_COREFOUNDATION */
93 # ifdef HAVE_SYSTEMCONFIGURATION
94 # include <SystemConfiguration/SystemConfiguration.h>
95 # endif /* HAVE_SYSTEMCONFIGURATION */
96 # endif /* __APPLE__ */
97 #endif /* HAVE_DNSSD */
104 static char *dequote(char *d
, const char *s
, int dlen
);
106 static int get_hostconfig(const char *name
);
107 #endif /* __APPLE__ */
108 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
109 char *resource
, int resourcelen
);
110 static void process_browse_data(const char *uri
, const char *host
,
111 const char *resource
, cups_ptype_t type
,
112 ipp_pstate_t state
, const char *location
,
113 const char *info
, const char *make_model
,
114 int num_attrs
, cups_option_t
*attrs
);
115 static void process_implicit_classes(void);
116 static void send_cups_browse(cupsd_printer_t
*p
);
118 static LDAP
*ldap_connect(void);
119 static void ldap_reconnect(void);
120 static void ldap_disconnect(LDAP
*ld
);
121 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
122 char *filter
, char *attrs
[],
123 int attrsonly
, LDAPMessage
**res
);
124 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
125 char *attr
, char *retval
,
126 unsigned long maxsize
);
127 static void ldap_freeres(LDAPMessage
*entry
);
128 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
129 static void send_ldap_browse(cupsd_printer_t
*p
);
130 static void ldap_dereg_printer(cupsd_printer_t
*p
);
131 static void ldap_dereg_ou(char *ou
, char *basedn
);
132 # ifdef HAVE_LDAP_REBIND_PROC
133 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
134 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
135 LDAP_CONST
char *refsp
,
140 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
146 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
147 # endif /* HAVE_LDAP_REBIND_PROC */
148 #endif /* HAVE_LDAP */
150 static void send_slp_browse(cupsd_printer_t
*p
);
151 #endif /* HAVE_LIBSLP */
152 static void update_cups_browse(void);
153 static void update_lpd(int onoff
);
154 static void update_polling(void);
155 static void update_smb(int onoff
);
159 # ifdef HAVE_COREFOUNDATION
160 static void dnssdAddAlias(const void *key
, const void *value
,
162 # endif /* HAVE_COREFOUNDATION */
163 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
165 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
166 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
167 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
169 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
170 DNSServiceFlags flags
,
171 DNSServiceErrorType errorCode
,
172 const char *name
, const char *regtype
,
173 const char *domain
, void *context
);
174 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
175 static void dnssdStop(void);
176 static void dnssdUpdate(void);
177 #endif /* HAVE_DNSSD */
180 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
182 "printerDescription",
184 "printerMakeAndModel",
189 #endif /* HAVE_LDAP */
197 * SLP service name for CUPS...
200 # define SLP_CUPS_SRVTYPE "service:printer"
201 # define SLP_CUPS_SRVLEN 15
205 * Printer service URL structure
208 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
210 struct _slpsrvurl_s
*next
; /* Next URL in list */
211 char url
[HTTP_MAX_URI
];
220 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
221 SLPError errcode
, void *cookie
);
222 static void slp_dereg_printer(cupsd_printer_t
*p
);
223 static int slp_get_attr(const char *attrlist
, const char *tag
,
225 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
227 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
228 unsigned short lifetime
,
229 SLPError errcode
, void *cookie
);
230 #endif /* HAVE_LIBSLP */
234 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
235 * local printer and remove any pending
236 * references to remote printers.
240 cupsdDeregisterPrinter(
241 cupsd_printer_t
*p
, /* I - Printer to register */
242 int removeit
) /* I - Printer being permanently removed */
245 * Only deregister if browsing is enabled and it's a local printer...
248 cupsdLogMessage(CUPSD_LOG_DEBUG
,
249 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
252 if (!Browsing
|| !p
->shared
||
253 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
257 * Announce the deletion...
260 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
262 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
264 p
->type
|= CUPS_PRINTER_DELETE
;
272 if (BrowseLocalProtocols
& BROWSE_SLP
)
273 slp_dereg_printer(p
);
274 #endif /* HAVE_LIBSLP */
277 if (BrowseLocalProtocols
& BROWSE_LDAP
)
278 ldap_dereg_printer(p
);
279 #endif /* HAVE_LDAP */
282 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
283 dnssdDeregisterPrinter(p
);
284 #endif /* HAVE_DNSSD */
289 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
293 cupsdLoadRemoteCache(void)
295 int i
; /* Looping var */
296 cups_file_t
*fp
; /* remote.cache file */
297 int linenum
; /* Current line number */
298 char line
[4096], /* Line from file */
299 *value
, /* Pointer to value */
300 *valueptr
, /* Pointer into value */
301 scheme
[32], /* Scheme portion of URI */
302 username
[64], /* Username portion of URI */
304 /* Hostname portion of URI */
305 resource
[HTTP_MAX_URI
];
306 /* Resource portion of URI */
307 int port
; /* Port number */
308 cupsd_printer_t
*p
; /* Current printer */
309 time_t now
; /* Current time */
313 * Don't load the cache if the remote protocols are disabled...
318 cupsdLogMessage(CUPSD_LOG_DEBUG
,
319 "cupsdLoadRemoteCache: Not loading remote cache.");
324 * Open the remote.cache file...
327 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
328 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
332 * Read printer configurations until we hit EOF...
339 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
342 * Decode the directive...
345 if (!strcasecmp(line
, "<Printer") ||
346 !strcasecmp(line
, "<DefaultPrinter"))
349 * <Printer name> or <DefaultPrinter name>
352 if (p
== NULL
&& value
)
355 * Add the printer and a base file type...
358 cupsdLogMessage(CUPSD_LOG_DEBUG
,
359 "cupsdLoadRemoteCache: Loading printer %s...", value
);
361 if ((p
= cupsdFindDest(value
)) != NULL
)
363 if (p
->type
& CUPS_PRINTER_CLASS
)
365 cupsdLogMessage(CUPSD_LOG_WARN
,
366 "Cached remote printer \"%s\" conflicts with "
374 p
= cupsdAddPrinter(value
);
377 p
->state
= IPP_PRINTER_IDLE
;
378 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
379 p
->browse_time
= now
;
380 p
->browse_expire
= now
+ BrowseTimeout
;
383 * Set the default printer as needed...
386 if (!strcasecmp(line
, "<DefaultPrinter"))
391 cupsdLogMessage(CUPSD_LOG_ERROR
,
392 "Syntax error on line %d of remote.cache.", linenum
);
396 else if (!strcasecmp(line
, "<Class") ||
397 !strcasecmp(line
, "<DefaultClass"))
400 * <Class name> or <DefaultClass name>
403 if (p
== NULL
&& value
)
406 * Add the printer and a base file type...
409 cupsdLogMessage(CUPSD_LOG_DEBUG
,
410 "cupsdLoadRemoteCache: Loading class %s...", value
);
412 if ((p
= cupsdFindDest(value
)) != NULL
)
413 p
->type
= CUPS_PRINTER_CLASS
;
415 p
= cupsdAddClass(value
);
418 p
->state
= IPP_PRINTER_IDLE
;
419 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
420 p
->browse_time
= now
;
421 p
->browse_expire
= now
+ BrowseTimeout
;
424 * Set the default printer as needed...
427 if (!strcasecmp(line
, "<DefaultClass"))
432 cupsdLogMessage(CUPSD_LOG_ERROR
,
433 "Syntax error on line %d of remote.cache.", linenum
);
437 else if (!strcasecmp(line
, "</Printer>") ||
438 !strcasecmp(line
, "</Class>"))
443 * Close out the current printer...
446 cupsdSetPrinterAttrs(p
);
451 cupsdLogMessage(CUPSD_LOG_ERROR
,
452 "Syntax error on line %d of remote.cache.", linenum
);
456 cupsdLogMessage(CUPSD_LOG_ERROR
,
457 "Syntax error on line %d of remote.cache.", linenum
);
459 else if (!strcasecmp(line
, "Info"))
462 cupsdSetString(&p
->info
, value
);
464 else if (!strcasecmp(line
, "MakeModel"))
467 cupsdSetString(&p
->make_model
, value
);
469 else if (!strcasecmp(line
, "Location"))
472 cupsdSetString(&p
->location
, value
);
474 else if (!strcasecmp(line
, "DeviceURI"))
478 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
479 username
, sizeof(username
), host
, sizeof(host
), &port
,
480 resource
, sizeof(resource
));
482 cupsdSetString(&p
->hostname
, host
);
483 cupsdSetString(&p
->uri
, value
);
484 cupsdSetDeviceURI(p
, value
);
487 cupsdLogMessage(CUPSD_LOG_ERROR
,
488 "Syntax error on line %d of remote.cache.", linenum
);
490 else if (!strcasecmp(line
, "Option") && value
)
496 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
499 cupsdLogMessage(CUPSD_LOG_ERROR
,
500 "Syntax error on line %d of remote.cache.", linenum
);
503 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
505 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
509 else if (!strcasecmp(line
, "Reason"))
513 for (i
= 0 ; i
< p
->num_reasons
; i
++)
514 if (!strcmp(value
, p
->reasons
[i
]))
517 if (i
>= p
->num_reasons
&&
518 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
520 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
525 cupsdLogMessage(CUPSD_LOG_ERROR
,
526 "Syntax error on line %d of remote.cache.", linenum
);
528 else if (!strcasecmp(line
, "State"))
531 * Set the initial queue state...
534 if (value
&& !strcasecmp(value
, "idle"))
535 p
->state
= IPP_PRINTER_IDLE
;
536 else if (value
&& !strcasecmp(value
, "stopped"))
538 p
->state
= IPP_PRINTER_STOPPED
;
539 cupsdSetPrinterReasons(p
, "+paused");
542 cupsdLogMessage(CUPSD_LOG_ERROR
,
543 "Syntax error on line %d of remote.cache.", linenum
);
545 else if (!strcasecmp(line
, "StateMessage"))
548 * Set the initial queue state message...
552 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
554 else if (!strcasecmp(line
, "Accepting"))
557 * Set the initial accepting state...
561 (!strcasecmp(value
, "yes") ||
562 !strcasecmp(value
, "on") ||
563 !strcasecmp(value
, "true")))
566 (!strcasecmp(value
, "no") ||
567 !strcasecmp(value
, "off") ||
568 !strcasecmp(value
, "false")))
571 cupsdLogMessage(CUPSD_LOG_ERROR
,
572 "Syntax error on line %d of remote.cache.", linenum
);
574 else if (!strcasecmp(line
, "Type"))
577 p
->type
= atoi(value
);
579 cupsdLogMessage(CUPSD_LOG_ERROR
,
580 "Syntax error on line %d of remote.cache.", linenum
);
582 else if (!strcasecmp(line
, "BrowseTime"))
586 time_t t
= atoi(value
);
588 if (t
> p
->browse_expire
)
589 p
->browse_expire
= t
;
592 cupsdLogMessage(CUPSD_LOG_ERROR
,
593 "Syntax error on line %d of remote.cache.", linenum
);
595 else if (!strcasecmp(line
, "JobSheets"))
598 * Set the initial job sheets...
603 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
608 cupsdSetString(&p
->job_sheets
[0], value
);
610 while (isspace(*valueptr
& 255))
615 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
620 cupsdSetString(&p
->job_sheets
[1], value
);
624 cupsdLogMessage(CUPSD_LOG_ERROR
,
625 "Syntax error on line %d of remote.cache.", linenum
);
627 else if (!strcasecmp(line
, "AllowUser"))
632 cupsdAddPrinterUser(p
, value
);
635 cupsdLogMessage(CUPSD_LOG_ERROR
,
636 "Syntax error on line %d of remote.cache.", linenum
);
638 else if (!strcasecmp(line
, "DenyUser"))
643 cupsdAddPrinterUser(p
, value
);
646 cupsdLogMessage(CUPSD_LOG_ERROR
,
647 "Syntax error on line %d of remote.cache.", linenum
);
652 * Something else we don't understand...
655 cupsdLogMessage(CUPSD_LOG_ERROR
,
656 "Unknown configuration directive %s on line %d of remote.cache.",
664 * Do auto-classing if needed...
667 process_implicit_classes();
672 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
673 * printer or update the broadcast contents.
677 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
679 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
682 if (!Browsing
|| !BrowseLocalProtocols
||
683 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
687 /* if (BrowseLocalProtocols & BROWSE_SLP)
688 slpRegisterPrinter(p); */
689 #endif /* HAVE_LIBSLP */
692 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
693 dnssdRegisterPrinter(p
);
694 #endif /* HAVE_DNSSD */
699 * 'cupsdRestartPolling()' - Restart polling servers as needed.
703 cupsdRestartPolling(void)
705 int i
; /* Looping var */
706 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
709 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
711 kill(pollp
->pid
, SIGHUP
);
716 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
720 cupsdSaveRemoteCache(void)
722 int i
; /* Looping var */
723 cups_file_t
*fp
; /* printers.conf file */
724 char temp
[1024], /* Temporary string */
725 value
[2048]; /* Value string */
726 cupsd_printer_t
*printer
; /* Current printer class */
727 time_t curtime
; /* Current time */
728 struct tm
*curdate
; /* Current date */
729 cups_option_t
*option
; /* Current option */
733 * Create the remote.cache file...
736 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
738 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
740 cupsdLogMessage(CUPSD_LOG_ERROR
,
741 "Unable to save remote.cache - %s", strerror(errno
));
745 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
748 * Restrict access to the file...
751 fchown(cupsFileNumber(fp
), getuid(), Group
);
752 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
755 * Write a small header to the file...
758 curtime
= time(NULL
);
759 curdate
= localtime(&curtime
);
760 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
762 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
763 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
766 * Write each local printer known to the system...
769 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
771 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
774 * Skip local destinations...
777 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
781 * Write printers as needed...
784 if (printer
== DefaultPrinter
)
785 cupsFilePuts(fp
, "<Default");
787 cupsFilePutChar(fp
, '<');
789 if (printer
->type
& CUPS_PRINTER_CLASS
)
790 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
792 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
794 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
797 cupsFilePutConf(fp
, "Info", printer
->info
);
799 if (printer
->location
)
800 cupsFilePutConf(fp
, "Location", printer
->location
);
802 if (printer
->make_model
)
803 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
805 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
807 if (printer
->state
== IPP_PRINTER_STOPPED
)
808 cupsFilePuts(fp
, "State Stopped\n");
810 cupsFilePuts(fp
, "State Idle\n");
812 for (i
= 0; i
< printer
->num_reasons
; i
++)
813 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
815 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
817 if (printer
->accepting
)
818 cupsFilePuts(fp
, "Accepting Yes\n");
820 cupsFilePuts(fp
, "Accepting No\n");
822 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
823 printer
->job_sheets
[1]);
824 cupsFilePutConf(fp
, "JobSheets", value
);
826 for (i
= 0; i
< printer
->num_users
; i
++)
827 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
830 for (i
= printer
->num_options
, option
= printer
->options
;
834 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
835 cupsFilePutConf(fp
, "Option", value
);
838 if (printer
->type
& CUPS_PRINTER_CLASS
)
839 cupsFilePuts(fp
, "</Class>\n");
841 cupsFilePuts(fp
, "</Printer>\n");
849 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
853 cupsdSendBrowseList(void)
855 int count
; /* Number of dests to update */
856 cupsd_printer_t
*p
; /* Current printer */
857 time_t ut
, /* Minimum update time */
858 to
; /* Timeout time */
861 if (!Browsing
|| !Printers
)
865 * Compute the update and timeout times...
869 ut
= to
- BrowseInterval
;
872 * Figure out how many printers need an update...
875 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
877 int max_count
; /* Maximum number to update */
881 * Throttle the number of printers we'll be updating this time
882 * around based on the number of queues that need updating and
883 * the maximum number of queues to update each second...
886 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
888 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
889 count
< max_count
&& p
!= NULL
;
890 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
891 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
892 p
->shared
&& p
->browse_time
< ut
)
896 * Loop through all of the printers and send local updates as needed...
900 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
902 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
906 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
909 * Check for wraparound...
913 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
917 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
920 else if (p
->browse_time
< ut
)
923 * Need to send an update...
928 p
->browse_time
= time(NULL
);
930 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
934 if (BrowseLocalProtocols
& BROWSE_SLP
)
936 #endif /* HAVE_LIBSLP */
939 if (BrowseLocalProtocols
& BROWSE_LDAP
)
941 #endif /* HAVE_LDAP */
946 * Save where we left off so that all printers get updated...
953 * Loop through all of the printers and timeout old printers as needed...
956 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
958 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
961 * If this is a remote queue, see if it needs to be timed out...
964 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
965 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
966 p
->browse_expire
< to
)
968 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
969 "%s \'%s\' deleted by directory services (timeout).",
970 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
973 cupsdLogMessage(CUPSD_LOG_DEBUG
,
974 "Remote destination \"%s\" has timed out; "
978 cupsArraySave(Printers
);
979 cupsdDeletePrinter(p
, 1);
980 cupsArrayRestore(Printers
);
981 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
987 #ifdef HAVE_LDAP_REBIND_PROC
988 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
990 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
994 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
995 LDAP_CONST
char *refsp
,
1003 * Bind to new LDAP server...
1006 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1007 "ldap_rebind_proc: Rebind to %s", refsp
);
1009 # if LDAP_API_VERSION > 3000
1011 bval
.bv_val
= BrowseLDAPPassword
;
1012 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1014 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1016 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
1017 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1018 # endif /* LDAP_API_VERSION > 3000 */
1023 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1026 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1030 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1042 * Free current values...
1045 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1046 "ldap_rebind_proc: Free values...");
1048 if ( dnp
&& *dnp
) {
1051 if ( passwdp
&& *passwdp
) {
1059 * Return credentials for LDAP referal...
1062 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1063 "ldap_rebind_proc: Return necessary values...");
1065 *dnp
= strdup(BrowseLDAPBindDN
);
1066 *passwdp
= strdup(BrowseLDAPPassword
);
1067 *authmethodp
= LDAP_AUTH_SIMPLE
;
1073 * Should never happen...
1076 cupsdLogMessage(CUPSD_LOG_ERROR
,
1077 "LDAP rebind has been called with wrong freeit value!");
1082 return (LDAP_SUCCESS
);
1084 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1085 #endif /* HAVE_LDAP_REBIND_PROC */
1090 * 'ldap_connect()' - Start new LDAP connection
1097 * Open LDAP handle...
1100 int rc
; /* LDAP API status */
1101 int version
= 3; /* LDAP version */
1102 struct berval bv
= {0, ""}; /* SASL bind value */
1103 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1104 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1105 int ldap_ssl
= 0; /* LDAP SSL indicator */
1106 int ssl_err
= 0; /* LDAP SSL error value */
1107 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1109 # ifdef HAVE_OPENLDAP
1110 # ifdef HAVE_LDAP_SSL
1113 * Set the certificate file to use for encrypted LDAP sessions...
1116 if (BrowseLDAPCACertFile
)
1118 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1119 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1120 BrowseLDAPCACertFile
);
1122 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1123 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1124 cupsdLogMessage(CUPSD_LOG_ERROR
,
1125 "Unable to set CA certificate file for LDAP "
1126 "connections: %d - %s", rc
, ldap_err2string(rc
));
1129 # endif /* HAVE_LDAP_SSL */
1131 * Initialize OPENLDAP connection...
1132 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1135 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1136 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1138 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1140 # else /* HAVE_OPENLDAP */
1142 int ldap_port
= 0; /* LDAP port */
1143 char ldap_protocol
[11], /* LDAP protocol */
1144 ldap_host
[255]; /* LDAP host */
1147 * Split LDAP URI into its components...
1150 if (! BrowseLDAPServer
)
1152 cupsdLogMessage(CUPSD_LOG_ERROR
,
1153 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1154 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1155 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1159 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1161 if (strcmp(ldap_protocol
, "ldap") == 0) {
1163 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1166 cupsdLogMessage(CUPSD_LOG_ERROR
,
1167 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1168 cupsdLogMessage(CUPSD_LOG_ERROR
,
1169 "Disable LDAP browsing!");
1170 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1171 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1178 ldap_port
= LDAPS_PORT
;
1180 ldap_port
= LDAP_PORT
;
1183 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1184 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1185 ldap_protocol
, ldap_host
, ldap_port
);
1188 * Initialize LDAP connection...
1193 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1194 rc
= LDAP_OPERATIONS_ERROR
;
1198 # ifdef HAVE_LDAP_SSL
1204 * Initialize SSL LDAP connection...
1206 if (BrowseLDAPCACertFile
)
1208 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1209 if (rc
!= LDAP_SUCCESS
) {
1210 cupsdLogMessage(CUPSD_LOG_ERROR
,
1211 "Failed to initialize LDAP SSL client!");
1212 rc
= LDAP_OPERATIONS_ERROR
;
1214 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1215 rc
= LDAP_OPERATIONS_ERROR
;
1222 cupsdLogMessage(CUPSD_LOG_ERROR
,
1223 "LDAP SSL certificate file/database not configured!");
1224 rc
= LDAP_OPERATIONS_ERROR
;
1227 # else /* HAVE_LDAP_SSL */
1230 * Return error, because client libraries doesn't support SSL
1233 cupsdLogMessage(CUPSD_LOG_ERROR
,
1234 "LDAP client libraries does not support TLS");
1235 rc
= LDAP_OPERATIONS_ERROR
;
1237 # endif /* HAVE_LDAP_SSL */
1239 # endif /* HAVE_OPENLDAP */
1242 * Check return code from LDAP initialize...
1245 if (rc
!= LDAP_SUCCESS
)
1247 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1249 cupsdLogMessage(CUPSD_LOG_ERROR
,
1250 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1254 cupsdLogMessage(CUPSD_LOG_ERROR
,
1255 "Unable to initialize LDAP! Disable LDAP browsing!");
1256 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1257 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1260 ldap_disconnect(TempBrowseLDAPHandle
);
1261 TempBrowseLDAPHandle
= NULL
;
1265 * Upgrade LDAP version...
1268 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1269 (const void *)&version
) != LDAP_SUCCESS
)
1271 cupsdLogMessage(CUPSD_LOG_ERROR
,
1272 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1274 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1275 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1276 ldap_disconnect(TempBrowseLDAPHandle
);
1277 TempBrowseLDAPHandle
= NULL
;
1283 * Register LDAP rebind procedure...
1286 # ifdef HAVE_LDAP_REBIND_PROC
1287 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1289 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1290 if ( rc
!= LDAP_SUCCESS
)
1291 cupsdLogMessage(CUPSD_LOG_ERROR
,
1292 "Setting LDAP rebind function failed with status %d: %s",
1293 rc
, ldap_err2string(rc
));
1297 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1299 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1300 # endif /* HAVE_LDAP_REBIND_PROC */
1303 * Start LDAP bind...
1306 # if LDAP_API_VERSION > 3000
1308 bval
.bv_val
= BrowseLDAPPassword
;
1309 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1311 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1312 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1315 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1317 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1318 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1319 # endif /* LDAP_API_VERSION > 3000 */
1321 if (rc
!= LDAP_SUCCESS
)
1323 cupsdLogMessage(CUPSD_LOG_ERROR
,
1324 "LDAP bind failed with error %d: %s",
1325 rc
, ldap_err2string(rc
));
1326 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1327 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1329 ssl_err
= PORT_GetError();
1331 cupsdLogMessage(CUPSD_LOG_ERROR
,
1332 "LDAP SSL error %d: %s",
1333 ssl_err
, ldapssl_err2string(ssl_err
));
1335 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1336 ldap_disconnect(TempBrowseLDAPHandle
);
1337 TempBrowseLDAPHandle
= NULL
;
1341 cupsdLogMessage(CUPSD_LOG_INFO
,
1342 "LDAP connection established");
1346 return (TempBrowseLDAPHandle
);
1351 * 'ldap_reconnect()' - Reconnect to LDAP Server
1355 ldap_reconnect(void)
1357 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1359 cupsdLogMessage(CUPSD_LOG_INFO
,
1360 "Try LDAP reconnect...");
1363 * Get a new LDAP Handle and replace the global Handle
1364 * if the new connection was successful
1367 TempBrowseLDAPHandle
= ldap_connect();
1369 if (TempBrowseLDAPHandle
!= NULL
)
1371 if (BrowseLDAPHandle
!= NULL
)
1373 ldap_disconnect(BrowseLDAPHandle
);
1375 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1381 * 'ldap_disconnect()' - Disconnect from LDAP Server
1385 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1387 int rc
; /* return code */
1390 * Close LDAP handle...
1393 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1394 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1396 rc
= ldap_unbind_s(ld
);
1397 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1398 if (rc
!= LDAP_SUCCESS
)
1399 cupsdLogMessage(CUPSD_LOG_ERROR
,
1400 "Unbind from LDAP server failed with status %d: %s",
1401 rc
, ldap_err2string(rc
));
1403 #endif /* HAVE_LDAP */
1407 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1411 cupsdStartBrowsing(void)
1413 int val
; /* Socket option value */
1414 struct sockaddr_in addr
; /* Broadcast address */
1415 cupsd_printer_t
*p
; /* Current printer */
1420 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1423 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1425 if (BrowseSocket
< 0)
1428 * Create the broadcast socket...
1431 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1433 cupsdLogMessage(CUPSD_LOG_ERROR
,
1434 "Unable to create broadcast socket - %s.",
1436 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1437 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1439 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1440 cupsdEndProcess(getpid(), 0);
1444 if (BrowseSocket
>= 0)
1447 * Bind the socket to browse port...
1450 memset(&addr
, 0, sizeof(addr
));
1451 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1452 addr
.sin_family
= AF_INET
;
1453 addr
.sin_port
= htons(BrowsePort
);
1455 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1457 cupsdLogMessage(CUPSD_LOG_ERROR
,
1458 "Unable to bind broadcast socket - %s.",
1462 closesocket(BrowseSocket
);
1464 close(BrowseSocket
);
1468 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1469 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1471 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1472 cupsdEndProcess(getpid(), 0);
1476 if (BrowseSocket
>= 0)
1479 * Set the "broadcast" flag...
1483 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1485 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1489 closesocket(BrowseSocket
);
1491 close(BrowseSocket
);
1495 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1496 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1498 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1499 cupsdEndProcess(getpid(), 0);
1503 if (BrowseSocket
>= 0)
1506 * Close the socket on exec...
1509 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1512 * Finally, add the socket to the input selection set as needed...
1515 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1518 * We only listen if we want remote printers...
1521 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1530 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1532 DNSServiceErrorType error
; /* Error from service creation */
1533 cupsd_listener_t
*lis
; /* Current listening socket */
1537 * First create a "master" connection for all registrations...
1540 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1541 != kDNSServiceErr_NoError
)
1543 cupsdLogMessage(CUPSD_LOG_ERROR
,
1544 "Unable to create master DNS-SD reference: %d", error
);
1546 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1547 cupsdEndProcess(getpid(), 0);
1552 * Add the master connection to the select list...
1555 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1556 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1559 * Then get the port we use for registrations. If we are not listening
1560 * on any non-local ports, there is no sense sharing local printers via
1566 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1568 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1570 if (httpAddrLocalhost(&(lis
->address
)))
1573 if (lis
->address
.addr
.sa_family
== AF_INET
)
1575 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1578 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1580 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1586 * Create an array to track the printers we share...
1589 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1590 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1594 * Set the computer name and register the web interface...
1597 cupsdUpdateDNSSDName();
1600 #endif /* HAVE_DNSSD */
1603 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1606 * Open SLP handle...
1609 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1611 cupsdLogMessage(CUPSD_LOG_ERROR
,
1612 "Unable to open an SLP handle; disabling SLP browsing!");
1613 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1614 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1615 BrowseSLPHandle
= NULL
;
1617 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1618 cupsdEndProcess(getpid(), 0);
1621 BrowseSLPRefresh
= 0;
1624 BrowseSLPHandle
= NULL
;
1625 #endif /* HAVE_LIBSLP */
1628 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1632 cupsdLogMessage(CUPSD_LOG_ERROR
,
1633 "Need to set BrowseLDAPDN to use LDAP browsing!");
1634 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1635 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1637 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1638 cupsdEndProcess(getpid(), 0);
1643 * Open LDAP handle...
1646 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1647 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1648 cupsdEndProcess(getpid(), 0);
1651 BrowseLDAPRefresh
= 0;
1653 #endif /* HAVE_LDAP */
1656 * Enable LPD and SMB printer sharing as needed through external programs...
1659 if (BrowseLocalProtocols
& BROWSE_LPD
)
1662 if (BrowseLocalProtocols
& BROWSE_SMB
)
1666 * Register the individual printers
1669 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1671 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1672 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1673 cupsdRegisterPrinter(p
);
1678 * 'cupsdStartPolling()' - Start polling servers as needed.
1682 cupsdStartPolling(void)
1684 int i
; /* Looping var */
1685 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1686 char polld
[1024]; /* Poll daemon path */
1687 char sport
[255]; /* Server port */
1688 char bport
[255]; /* Browser port */
1689 char interval
[255]; /* Poll interval */
1690 int statusfds
[2]; /* Status pipe */
1691 char *argv
[6]; /* Arguments */
1692 char *envp
[100]; /* Environment */
1696 * Don't do anything if we aren't polling...
1699 if (NumPolled
== 0 || BrowseSocket
< 0)
1702 PollStatusBuffer
= NULL
;
1707 * Setup string arguments for polld, port and interval options.
1710 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1712 sprintf(bport
, "%d", BrowsePort
);
1715 sprintf(interval
, "%d", BrowseInterval
);
1717 strcpy(interval
, "30");
1719 argv
[0] = "cups-polld";
1725 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1728 * Create a pipe that receives the status messages from each
1732 if (cupsdOpenPipe(statusfds
))
1734 cupsdLogMessage(CUPSD_LOG_ERROR
,
1735 "Unable to create polling status pipes - %s.",
1738 PollStatusBuffer
= NULL
;
1742 PollPipe
= statusfds
[0];
1743 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1746 * Run each polling daemon, redirecting stderr to the polling pipe...
1749 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1751 sprintf(sport
, "%d", pollp
->port
);
1753 argv
[1] = pollp
->hostname
;
1755 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1756 0, DefaultProfile
, NULL
, &(pollp
->pid
)) < 0)
1758 cupsdLogMessage(CUPSD_LOG_ERROR
,
1759 "cupsdStartPolling: Unable to fork polling daemon - %s",
1765 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1766 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1767 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1770 close(statusfds
[1]);
1773 * Finally, add the pipe to the input selection set...
1776 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1781 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1785 cupsdStopBrowsing(void)
1787 cupsd_printer_t
*p
; /* Current printer */
1790 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1794 * De-register the individual printers
1797 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1799 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1800 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1801 cupsdDeregisterPrinter(p
, 1);
1804 * Shut down browsing sockets...
1807 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1811 * Close the socket and remove it from the input selection set.
1815 closesocket(BrowseSocket
);
1817 close(BrowseSocket
);
1820 cupsdRemoveSelect(BrowseSocket
);
1825 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1827 #endif /* HAVE_DNSSD */
1830 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1834 * Close SLP handle...
1837 SLPClose(BrowseSLPHandle
);
1838 BrowseSLPHandle
= NULL
;
1840 #endif /* HAVE_LIBSLP */
1843 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1846 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1847 ldap_disconnect(BrowseLDAPHandle
);
1848 BrowseLDAPHandle
= NULL
;
1850 #endif /* HAVE_OPENLDAP */
1853 * Disable LPD and SMB printer sharing as needed through external programs...
1856 if (BrowseLocalProtocols
& BROWSE_LPD
)
1859 if (BrowseLocalProtocols
& BROWSE_SMB
)
1865 * 'cupsdStopPolling()' - Stop polling servers as needed.
1869 cupsdStopPolling(void)
1871 int i
; /* Looping var */
1872 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1877 cupsdStatBufDelete(PollStatusBuffer
);
1880 cupsdRemoveSelect(PollPipe
);
1883 PollStatusBuffer
= NULL
;
1886 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1888 cupsdEndProcess(pollp
->pid
, 0);
1894 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1898 cupsdUpdateDNSSDName(void)
1900 DNSServiceErrorType error
; /* Error from service creation */
1901 char webif
[1024]; /* Web interface share name */
1902 #ifdef HAVE_COREFOUNDATION_H
1903 SCDynamicStoreRef sc
; /* Context for dynamic store */
1904 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1905 CFStringRef nameRef
; /* Computer name CFString */
1906 char nameBuffer
[1024]; /* C-string buffer */
1907 CFStringEncoding nameEncoding
; /* Computer name encoding */
1908 #endif /* HAVE_COREFOUNDATION_H */
1912 * Only share the web interface and printers when non-local listening is
1921 * Get the computer name as a c-string...
1924 #ifdef HAVE_COREFOUNDATION_H
1925 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1930 * Get the computer name from the dynamic store...
1933 cupsdClearString(&DNSSDName
);
1935 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
,
1936 &nameEncoding
)) != NULL
)
1938 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1939 kCFStringEncodingUTF8
))
1941 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1942 "Dynamic store computer name is \"%s\".", nameBuffer
);
1943 cupsdSetString(&DNSSDName
, nameBuffer
);
1952 * Use the ServerName instead...
1955 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1956 "Using ServerName \"%s\" as computer name.", ServerName
);
1957 cupsdSetString(&DNSSDName
, ServerName
);
1961 * Get any Back-to-My-Mac domains and add them as aliases...
1964 cupsdFreeAliases(DNSSDAlias
);
1967 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
1968 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
1970 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
1971 (int)CFDictionaryGetCount(btmm
));
1972 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
1975 cupsdLogMessage(CUPSD_LOG_ERROR
,
1976 "Bad Back to My Mac data in dynamic store!");
1978 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
1986 #endif /* HAVE_COREFOUNDATION_H */
1987 cupsdSetString(&DNSSDName
, ServerName
);
1990 * Then (re)register the web interface if enabled...
1996 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1998 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2001 DNSServiceRefDeallocate(WebIFRef
);
2003 WebIFRef
= DNSSDRef
;
2004 if ((error
= DNSServiceRegister(&WebIFRef
,
2005 kDNSServiceFlagsShareConnection
,
2006 0, webif
, "_http._tcp", NULL
,
2007 NULL
, htons(DNSSDPort
), 7,
2008 "\006path=/", dnssdRegisterCallback
,
2009 NULL
)) != kDNSServiceErr_NoError
)
2010 cupsdLogMessage(CUPSD_LOG_ERROR
,
2011 "DNS-SD web interface registration failed: %d", error
);
2014 #endif /* HAVE_DNSSD */
2019 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2023 cupsdUpdateLDAPBrowse(void)
2025 char uri
[HTTP_MAX_URI
], /* Printer URI */
2026 host
[HTTP_MAX_URI
], /* Hostname */
2027 resource
[HTTP_MAX_URI
], /* Resource path */
2028 location
[1024], /* Printer location */
2029 info
[1024], /* Printer information */
2030 make_model
[1024], /* Printer make and model */
2031 type_num
[30]; /* Printer type number */
2032 int type
; /* Printer type */
2033 int rc
; /* LDAP status */
2034 int limit
; /* Size limit */
2035 LDAPMessage
*res
, /* LDAP search results */
2036 *e
; /* Current entry from search */
2038 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2040 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2043 * Reconnect if LDAP Handle is invalid...
2046 if (! BrowseLDAPHandle
)
2053 * Search for cups printers in LDAP directory...
2056 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2057 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2060 * If ldap search was successfull then exit function
2061 * and temporary disable LDAP updates...
2064 if (rc
!= LDAP_SUCCESS
)
2066 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2068 BrowseLDAPUpdate
= FALSE
;
2069 cupsdLogMessage(CUPSD_LOG_INFO
,
2070 "LDAP update temporary disabled");
2076 * If LDAP updates were disabled, we will reenable them...
2079 if (! BrowseLDAPUpdate
)
2081 BrowseLDAPUpdate
= TRUE
;
2082 cupsdLogMessage(CUPSD_LOG_INFO
,
2083 "LDAP update enabled");
2087 * Count LDAP entries and return if no entry exist...
2090 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2091 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2099 * Loop through the available printers...
2102 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2104 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2107 * Get the required values from this entry...
2110 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2111 "printerDescription", info
, sizeof(info
)) == -1)
2114 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2115 "printerLocation", location
, sizeof(location
)) == -1)
2118 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2119 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2122 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2123 "printerType", type_num
, sizeof(type_num
)) == -1)
2126 type
= atoi(type_num
);
2128 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2129 "printerURI", uri
, sizeof(uri
)) == -1)
2133 * Process the entry as browse data...
2136 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2137 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2138 location
, info
, make_model
, 0, NULL
);
2144 #endif /* HAVE_LDAP */
2149 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2153 cupsdUpdateSLPBrowse(void)
2155 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2156 *next
; /* Next service in list */
2157 cupsd_printer_t p
; /* Printer information */
2158 const char *uri
; /* Pointer to printer URI */
2159 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2160 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2164 * Reset the refresh time...
2167 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2170 * Poll for remote printers using SLP...
2175 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2176 slp_url_callback
, &s
);
2179 * Loop through the list of available printers...
2185 * Save the "next" pointer...
2191 * Load a cupsd_printer_t structure with the SLP service attributes...
2194 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2197 * Process this printer entry...
2200 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2202 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2205 * Pull the URI apart to see if this is a local or remote printer...
2208 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2209 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2210 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2214 * Free this listing...
2217 cupsdClearString(&p
.info
);
2218 cupsdClearString(&p
.location
);
2219 cupsdClearString(&p
.make_model
);
2224 #endif /* HAVE_LIBSLP */
2228 * 'dequote()' - Remote quotes from a string.
2231 static char * /* O - Dequoted string */
2232 dequote(char *d
, /* I - Destination string */
2233 const char *s
, /* I - Source string */
2234 int dlen
) /* I - Destination length */
2236 char *dptr
; /* Pointer into destination */
2241 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2258 # ifdef HAVE_COREFOUNDATION
2260 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2264 dnssdAddAlias(const void *key
, /* I - Key */
2265 const void *value
, /* I - Value (domain) */
2266 void *context
) /* I - Unused */
2268 char valueStr
[1024], /* Domain string */
2269 hostname
[1024]; /* Complete hostname */
2274 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2275 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2276 kCFStringEncodingUTF8
))
2278 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDName
, valueStr
);
2280 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2282 cupsdAddAlias(DNSSDAlias
, hostname
);
2283 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2287 cupsdLogMessage(CUPSD_LOG_ERROR
,
2288 "Bad Back to My Mac domain in dynamic store!");
2290 # endif /* HAVE_COREFOUNDATION */
2294 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2297 static char * /* O - TXT record */
2298 dnssdBuildTxtRecord(
2299 int *txt_len
, /* O - TXT record length */
2300 cupsd_printer_t
*p
, /* I - Printer information */
2301 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2303 int i
, j
; /* Looping vars */
2304 char type_str
[32], /* Type to string buffer */
2305 state_str
[32], /* State to string buffer */
2306 rp_str
[1024], /* Queue name string buffer */
2307 air_str
[1024], /* auth-info-required string buffer */
2308 *keyvalue
[32][2]; /* Table of key/value pairs */
2309 ipp_attribute_t
*air_attr
; /* auth-info-required attribute */
2313 * Load up the key value pairs...
2318 keyvalue
[i
][0] = "txtvers";
2319 keyvalue
[i
++][1] = "1";
2321 keyvalue
[i
][0] = "qtotal";
2322 keyvalue
[i
++][1] = "1";
2324 keyvalue
[i
][0] = "rp";
2325 keyvalue
[i
++][1] = rp_str
;
2327 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2329 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2330 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2332 keyvalue
[i
][0] = "ty";
2333 keyvalue
[i
++][1] = p
->make_model
;
2335 if (p
->location
&& *p
->location
!= '\0')
2337 keyvalue
[i
][0] = "note";
2338 keyvalue
[i
++][1] = p
->location
;
2341 keyvalue
[i
][0] = "priority";
2342 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2344 keyvalue
[i
][0] = "product";
2345 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2347 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2348 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2350 keyvalue
[i
][0] = "printer-state";
2351 keyvalue
[i
++][1] = state_str
;
2353 keyvalue
[i
][0] = "printer-type";
2354 keyvalue
[i
++][1] = type_str
;
2356 keyvalue
[i
][0] = "Transparent";
2357 keyvalue
[i
++][1] = "T";
2359 keyvalue
[i
][0] = "Binary";
2360 keyvalue
[i
++][1] = "T";
2362 if ((p
->type
& CUPS_PRINTER_FAX
))
2364 keyvalue
[i
][0] = "Fax";
2365 keyvalue
[i
++][1] = "T";
2368 if ((p
->type
& CUPS_PRINTER_COLOR
))
2370 keyvalue
[i
][0] = "Color";
2371 keyvalue
[i
++][1] = "T";
2374 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2376 keyvalue
[i
][0] = "Duplex";
2377 keyvalue
[i
++][1] = "T";
2380 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2382 keyvalue
[i
][0] = "Staple";
2383 keyvalue
[i
++][1] = "T";
2386 if ((p
->type
& CUPS_PRINTER_COPIES
))
2388 keyvalue
[i
][0] = "Copies";
2389 keyvalue
[i
++][1] = "T";
2392 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2394 keyvalue
[i
][0] = "Collate";
2395 keyvalue
[i
++][1] = "T";
2398 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2400 keyvalue
[i
][0] = "Punch";
2401 keyvalue
[i
++][1] = "T";
2404 if ((p
->type
& CUPS_PRINTER_BIND
))
2406 keyvalue
[i
][0] = "Bind";
2407 keyvalue
[i
++][1] = "T";
2410 if ((p
->type
& CUPS_PRINTER_SORT
))
2412 keyvalue
[i
][0] = "Sort";
2413 keyvalue
[i
++][1] = "T";
2416 keyvalue
[i
][0] = "pdl";
2417 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2419 if ((air_attr
= ippFindAttribute(p
->attrs
, "auth-info-required",
2420 IPP_TAG_KEYWORD
)) != NULL
&&
2421 strcmp(air_attr
->values
[0].string
.text
, "none"))
2423 char *air
= air_str
; /* Pointer into string */
2426 for (j
= 0; j
< air_attr
->num_values
; j
++)
2428 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2434 strlcpy(air
, air_attr
->values
[j
].string
.text
,
2435 sizeof(air_str
) - (air
- air_str
));
2439 keyvalue
[i
][0] = "air";
2440 keyvalue
[i
++][1] = air_str
;
2444 * Then pack them into a proper txt record...
2447 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2452 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2455 static int /* O - Result of comparison */
2456 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2457 cupsd_printer_t
*b
)/* I - Second printer */
2459 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2464 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2469 dnssdDeregisterPrinter(
2470 cupsd_printer_t
*p
) /* I - Printer */
2472 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2475 * Closing the socket deregisters the service
2480 DNSServiceRefDeallocate(p
->ipp_ref
);
2487 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2496 DNSServiceRefDeallocate(p
->printer_ref
);
2497 p
->printer_ref
= NULL
;
2503 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2506 free(p
->printer_txt
);
2507 p
->printer_txt
= NULL
;
2511 * Remove the printer from the array of DNS-SD printers, then clear the
2512 * registered name...
2515 cupsArrayRemove(DNSSDPrinters
, p
);
2516 cupsdClearString(&p
->reg_name
);
2521 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2522 * TXT record format.
2525 static char * /* O - TXT record */
2526 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2527 char *keyvalue
[][2], /* I - Table of key value pairs */
2528 int count
) /* I - Items in table */
2530 int i
; /* Looping var */
2531 int length
; /* Length of TXT record */
2532 int length2
; /* Length of value */
2533 char *txtRecord
; /* TXT record buffer */
2534 char *cursor
; /* Looping pointer */
2538 * Calculate the buffer size
2541 for (length
= i
= 0; i
< count
; i
++)
2542 length
+= 1 + strlen(keyvalue
[i
][0]) +
2543 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2546 * Allocate and fill it
2549 txtRecord
= malloc(length
);
2554 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2557 * Drop in the p-string style length byte followed by the data
2560 length
= strlen(keyvalue
[i
][0]);
2561 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2563 *cursor
++ = (unsigned char)(length
+ length2
);
2565 memcpy(cursor
, keyvalue
[i
][0], length
);
2572 memcpy(cursor
, keyvalue
[i
][1], length2
);
2583 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2587 dnssdRegisterCallback(
2588 DNSServiceRef sdRef
, /* I - DNS Service reference */
2589 DNSServiceFlags flags
, /* I - Reserved for future use */
2590 DNSServiceErrorType errorCode
, /* I - Error code */
2591 const char *name
, /* I - Service name */
2592 const char *regtype
, /* I - Service type */
2593 const char *domain
, /* I - Domain. ".local" for now */
2594 void *context
) /* I - User-defined context */
2596 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2597 /* Current printer */
2600 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2601 name
, regtype
, p
? p
->name
: "Web Interface",
2602 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2606 cupsdLogMessage(CUPSD_LOG_ERROR
,
2607 "DNSServiceRegister failed with error %d", (int)errorCode
);
2610 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2612 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2615 cupsArrayRemove(DNSSDPrinters
, p
);
2616 cupsdSetString(&p
->reg_name
, name
);
2617 cupsArrayAdd(DNSSDPrinters
, p
);
2619 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2625 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2626 * or update the broadcast contents.
2630 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2632 DNSServiceErrorType se
; /* dnssd errors */
2633 char *ipp_txt
, /* IPP TXT record buffer */
2634 *printer_txt
, /* LPD TXT record buffer */
2635 name
[1024], /* Service name */
2636 *nameptr
; /* Pointer into name */
2637 int ipp_len
, /* IPP TXT record length */
2638 printer_len
; /* LPD TXT record length */
2639 const char *regtype
; /* Registration type */
2642 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2643 !p
->ipp_ref
? "new" : "update");
2646 * If per-printer sharing was just disabled make sure we're not
2647 * registered before returning.
2652 dnssdDeregisterPrinter(p
);
2657 * The registered name takes the form of "<printer-info> @ <computer name>"...
2660 if (p
->info
&& strlen(p
->info
) > 0)
2663 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2665 strlcpy(name
, p
->info
, sizeof(name
));
2668 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2670 strlcpy(name
, p
->name
, sizeof(name
));
2673 * If an existing printer was renamed, unregister it and start over...
2676 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2677 dnssdDeregisterPrinter(p
);
2681 cupsdSetString(&p
->reg_name
, name
);
2682 cupsArrayAdd(DNSSDPrinters
, p
);
2686 * Register IPP and (optionally) LPD...
2689 ipp_len
= 0; /* anti-compiler-warning-code */
2690 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2693 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2696 * Update the existing registration...
2699 /* A TTL of 0 means use record's original value (Radar 3176248) */
2700 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2701 0)) == kDNSServiceErr_NoError
)
2706 p
->ipp_txt
= ipp_txt
;
2707 p
->ipp_len
= ipp_len
;
2713 * Failed to update record, lets close this reference and move on...
2716 cupsdLogMessage(CUPSD_LOG_ERROR
,
2717 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2720 DNSServiceRefDeallocate(p
->ipp_ref
);
2728 * Initial registration. Use the _fax subtype for fax queues...
2731 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2734 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2735 "Registering DNS-SD printer %s with name \"%s\" and "
2736 "type \"%s\"", p
->name
, name
, regtype
);
2739 * Register the queue, dropping characters as needed until we succeed...
2742 nameptr
= name
+ strlen(name
);
2746 p
->ipp_ref
= DNSSDRef
;
2747 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2748 0, name
, regtype
, NULL
, NULL
,
2749 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2750 dnssdRegisterCallback
,
2751 p
)) == kDNSServiceErr_BadParam
)
2754 * Name is too long, drop trailing characters, taking into account
2760 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2767 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2769 if (se
== kDNSServiceErr_NoError
)
2771 p
->ipp_txt
= ipp_txt
;
2772 p
->ipp_len
= ipp_len
;
2776 cupsdLogMessage(CUPSD_LOG_WARN
,
2777 "DNS-SD IPP registration of \"%s\" failed: %d",
2784 if (BrowseLocalProtocols
& BROWSE_LPD
)
2786 printer_len
= 0; /* anti-compiler-warning-code */
2787 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2789 if (p
->printer_ref
&&
2790 (printer_len
!= p
->printer_len
||
2791 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2794 * Update the existing registration...
2797 /* A TTL of 0 means use record's original value (Radar 3176248) */
2798 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2800 0)) == kDNSServiceErr_NoError
)
2803 free(p
->printer_txt
);
2805 p
->printer_txt
= printer_txt
;
2806 p
->printer_len
= printer_len
;
2812 * Failed to update record, lets close this reference and move on...
2815 cupsdLogMessage(CUPSD_LOG_ERROR
,
2816 "Unable to update LPD DNS-SD record for %s - %d",
2819 DNSServiceRefDeallocate(p
->printer_ref
);
2820 p
->printer_ref
= NULL
;
2824 if (!p
->printer_ref
)
2827 * Initial registration...
2830 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2831 "Registering DNS-SD printer %s with name \"%s\" and "
2832 "type \"_printer._tcp\"", p
->name
, name
);
2834 p
->printer_ref
= DNSSDRef
;
2835 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2836 kDNSServiceFlagsShareConnection
,
2837 0, name
, "_printer._tcp", NULL
, NULL
,
2838 htons(515), printer_len
, printer_txt
,
2839 dnssdRegisterCallback
,
2840 p
)) == kDNSServiceErr_NoError
)
2842 p
->printer_txt
= printer_txt
;
2843 p
->printer_len
= printer_len
;
2847 cupsdLogMessage(CUPSD_LOG_WARN
,
2848 "DNS-SD LPD registration of \"%s\" failed: %d",
2859 * 'dnssdStop()' - Stop all DNS-SD registrations.
2865 cupsd_printer_t
*p
; /* Current printer */
2869 * De-register the individual printers
2872 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2874 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2875 dnssdDeregisterPrinter(p
);
2878 * Shutdown the rest of the service refs...
2883 DNSServiceRefDeallocate(WebIFRef
);
2889 DNSServiceRefDeallocate(RemoteRef
);
2893 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2895 DNSServiceRefDeallocate(DNSSDRef
);
2898 cupsArrayDelete(DNSSDPrinters
);
2899 DNSSDPrinters
= NULL
;
2906 * 'dnssdUpdate()' - Handle DNS-SD queries.
2912 DNSServiceErrorType sdErr
; /* Service discovery error */
2915 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2917 cupsdLogMessage(CUPSD_LOG_ERROR
,
2918 "DNS Service Discovery registration error %d!",
2923 #endif /* HAVE_DNSSD */
2928 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2931 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2932 get_hostconfig(const char *name
) /* I - Name of service */
2934 cups_file_t
*fp
; /* Hostconfig file */
2935 char line
[1024], /* Line from file */
2936 *ptr
; /* Pointer to value */
2937 int state
= 1; /* State of service */
2941 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2942 * the service is enabled/auto.
2945 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2948 * Read lines from the file until we find the service...
2951 while (cupsFileGets(fp
, line
, sizeof(line
)))
2953 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2958 if (!strcasecmp(line
, name
))
2961 * Found the service, see if it is set to "-NO-"...
2964 if (!strncasecmp(ptr
, "-NO-", 4))
2975 #endif /* __APPLE__ */
2979 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2982 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2983 is_local_queue(const char *uri
, /* I - Printer URI */
2984 char *host
, /* O - Host string */
2985 int hostlen
, /* I - Length of host buffer */
2986 char *resource
, /* O - Resource string */
2987 int resourcelen
) /* I - Length of resource buffer */
2989 char scheme
[32], /* Scheme portion of URI */
2990 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2991 int port
; /* Port portion of URI */
2992 cupsd_netif_t
*iface
; /* Network interface */
2996 * Pull the URI apart to see if this is a local or remote printer...
2999 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3000 username
, sizeof(username
), host
, hostlen
, &port
,
3001 resource
, resourcelen
) < HTTP_URI_OK
)
3004 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3007 * Check for local server addresses...
3010 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3015 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3017 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3018 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3022 * If we get here, the printer is remote...
3030 * 'process_browse_data()' - Process new browse data.
3034 process_browse_data(
3035 const char *uri
, /* I - URI of printer/class */
3036 const char *host
, /* I - Hostname */
3037 const char *resource
, /* I - Resource path */
3038 cups_ptype_t type
, /* I - Printer type */
3039 ipp_pstate_t state
, /* I - Printer state */
3040 const char *location
, /* I - Printer location */
3041 const char *info
, /* I - Printer information */
3042 const char *make_model
, /* I - Printer make and model */
3043 int num_attrs
, /* I - Number of attributes */
3044 cups_option_t
*attrs
) /* I - Attributes */
3046 int i
; /* Looping var */
3047 int update
; /* Update printer attributes? */
3048 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3049 name
[IPP_MAX_NAME
], /* Name of printer */
3050 newname
[IPP_MAX_NAME
], /* New name of printer */
3051 *hptr
, /* Pointer into hostname */
3052 *sptr
; /* Pointer into ServerName */
3053 const char *shortname
; /* Short queue name (queue) */
3054 char local_make_model
[IPP_MAX_NAME
];
3055 /* Local make and model */
3056 cupsd_printer_t
*p
; /* Printer information */
3057 const char *ipp_options
, /* ipp-options value */
3058 *lease_duration
; /* lease-duration value */
3059 int is_class
; /* Is this queue a class? */
3062 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3063 "process_browse_data(uri=\"%s\", host=\"%s\", "
3064 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3065 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3066 uri
, host
, resource
, type
, state
,
3067 location
? location
: "(nil)", info
? info
: "(nil)",
3068 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3071 * Determine if the URI contains any illegal characters in it...
3074 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3075 (strncmp(resource
, "/printers/", 10) &&
3076 strncmp(resource
, "/classes/", 9)))
3078 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3082 if (strchr(resource
, '?') ||
3083 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3084 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3086 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3092 * OK, this isn't a local printer; add any remote options...
3095 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3097 if (BrowseRemoteOptions
)
3099 if (BrowseRemoteOptions
[0] == '?')
3102 * Override server-supplied options...
3105 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3107 else if (ipp_options
)
3110 * Combine the server and local options...
3113 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3114 BrowseRemoteOptions
);
3119 * Just use the local options...
3122 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3127 else if (ipp_options
)
3130 * Just use the server-supplied options...
3133 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3138 * See if we already have it listed in the Printers list, and add it if not...
3141 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3142 type
&= ~CUPS_PRINTER_IMPLICIT
;
3144 hptr
= strchr(host
, '.');
3145 sptr
= strchr(ServerName
, '.');
3146 is_class
= type
& CUPS_PRINTER_CLASS
;
3148 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3151 * Strip the common domain name components...
3154 while (hptr
!= NULL
)
3156 if (!strcasecmp(hptr
, sptr
))
3162 hptr
= strchr(hptr
+ 1, '.');
3169 * Remote destination is a class...
3172 if (!strncmp(resource
, "/classes/", 9))
3173 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3177 shortname
= resource
+ 9;
3182 * Remote destination is a printer...
3185 if (!strncmp(resource
, "/printers/", 10))
3186 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3190 shortname
= resource
+ 10;
3194 *hptr
= '.'; /* Resource FQDN */
3196 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3199 * Long name doesn't exist, try short name...
3202 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3205 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3208 * Short name doesn't exist, use it for this shared queue.
3211 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3213 strlcpy(name
, shortname
, sizeof(name
));
3218 * Short name exists...
3221 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3222 "process_browse_data: %s found, type=%x, hostname=%s...",
3223 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3225 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3226 p
= NULL
; /* Don't replace implicit classes */
3227 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3230 * Short name exists but is for a different host. If this is a remote
3231 * queue, rename it and use the long name...
3234 if (p
->type
& CUPS_PRINTER_REMOTE
)
3236 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3237 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3238 is_class
? "class" : "printer", p
->name
, p
->name
,
3240 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3241 "%s \'%s\' deleted by directory services.",
3242 is_class
? "Class" : "Printer", p
->name
);
3244 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3245 cupsdRenamePrinter(p
, newname
);
3247 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3248 "%s \'%s\' added by directory services.",
3249 is_class
? "Class" : "Printer", p
->name
);
3253 * Force creation with long name...
3261 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3262 "process_browse_data: %s found, type=%x, hostname=%s...",
3263 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3268 * Queue doesn't exist; add it...
3272 p
= cupsdAddClass(name
);
3274 p
= cupsdAddPrinter(name
);
3279 cupsdClearString(&(p
->hostname
));
3281 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3282 is_class
? "class" : "printer", name
);
3284 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3285 "%s \'%s\' added by directory services.",
3286 is_class
? "Class" : "Printer", name
);
3289 * Force the URI to point to the real server...
3292 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3295 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3301 * Hostname not set, so this must be a cached remote printer
3302 * that was created for a pending print job...
3305 cupsdSetString(&p
->hostname
, host
);
3306 cupsdSetString(&p
->uri
, uri
);
3307 cupsdSetString(&p
->device_uri
, uri
);
3310 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3314 * Update the state...
3318 p
->browse_time
= time(NULL
);
3320 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3324 * Grab the lease-duration for the browse data; anything less then 1
3325 * second or more than 1 week gets the default BrowseTimeout...
3328 i
= atoi(lease_duration
);
3329 if (i
< 1 || i
> 604800)
3332 p
->browse_expire
= p
->browse_time
+ i
;
3335 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3337 if (type
& CUPS_PRINTER_REJECTING
)
3339 type
&= ~CUPS_PRINTER_REJECTING
;
3347 else if (!p
->accepting
)
3353 if (p
->type
!= type
)
3359 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3361 cupsdSetString(&p
->location
, location
);
3365 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3367 cupsdSetString(&p
->info
, info
);
3370 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3373 if (!make_model
|| !make_model
[0])
3376 snprintf(local_make_model
, sizeof(local_make_model
),
3377 "Remote Class on %s", host
);
3379 snprintf(local_make_model
, sizeof(local_make_model
),
3380 "Remote Printer on %s", host
);
3383 snprintf(local_make_model
, sizeof(local_make_model
),
3384 "%s on %s", make_model
, host
);
3386 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3388 cupsdSetString(&p
->make_model
, local_make_model
);
3394 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3397 * See if we need to update the attributes...
3400 if (p
->num_options
!= num_attrs
)
3404 for (i
= 0; i
< num_attrs
; i
++)
3405 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3406 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3407 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3416 * Free the old options...
3419 cupsFreeOptions(p
->num_options
, p
->options
);
3422 p
->num_options
= num_attrs
;
3425 if (type
& CUPS_PRINTER_DELETE
)
3427 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3428 "%s \'%s\' deleted by directory services.",
3429 is_class
? "Class" : "Printer", p
->name
);
3431 cupsdExpireSubscriptions(p
, NULL
);
3433 cupsdDeletePrinter(p
, 1);
3434 cupsdUpdateImplicitClasses();
3435 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3439 cupsdSetPrinterAttrs(p
);
3440 cupsdUpdateImplicitClasses();
3444 * See if we have a default printer... If not, make the first network
3445 * default printer the default.
3448 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3451 * Find the first network default printer and use it...
3454 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3456 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3457 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3460 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3466 * Do auto-classing if needed...
3469 process_implicit_classes();
3474 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3478 process_implicit_classes(void)
3480 int i
; /* Looping var */
3481 int update
; /* Update printer attributes? */
3482 char name
[IPP_MAX_NAME
], /* Name of printer */
3483 *hptr
; /* Pointer into hostname */
3484 cupsd_printer_t
*p
, /* Printer information */
3485 *pclass
, /* Printer class */
3486 *first
; /* First printer in class */
3487 int offset
, /* Offset of name */
3488 len
; /* Length of name */
3491 if (!ImplicitClasses
|| !Printers
)
3495 * Loop through all available printers and create classes as needed...
3498 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3499 update
= 0, pclass
= NULL
, first
= NULL
;
3501 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3504 * Skip implicit classes...
3507 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3514 * If len == 0, get the length of this printer name up to the "@"
3518 cupsArraySave(Printers
);
3521 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3522 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3525 * We have more than one printer with the same name; see if
3526 * we have a class, and if this printer is a member...
3529 if (pclass
&& strcasecmp(pclass
->name
, name
))
3532 cupsdSetPrinterAttrs(pclass
);
3538 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3541 * Need to add the class...
3544 pclass
= cupsdAddPrinter(name
);
3545 cupsArrayAdd(ImplicitPrinters
, pclass
);
3547 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3548 pclass
->accepting
= 1;
3549 pclass
->state
= IPP_PRINTER_IDLE
;
3551 cupsdSetString(&pclass
->location
, p
->location
);
3552 cupsdSetString(&pclass
->info
, p
->info
);
3554 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3555 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3559 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3561 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3563 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3564 "Implicit class \'%s\' added by directory services.",
3570 for (i
= 0; i
< pclass
->num_printers
; i
++)
3571 if (pclass
->printers
[i
] == first
)
3574 if (i
>= pclass
->num_printers
)
3576 first
->in_implicit_class
= 1;
3577 cupsdAddPrinterToClass(pclass
, first
);
3583 for (i
= 0; i
< pclass
->num_printers
; i
++)
3584 if (pclass
->printers
[i
] == p
)
3587 if (i
>= pclass
->num_printers
)
3589 p
->in_implicit_class
= 1;
3590 cupsdAddPrinterToClass(pclass
, p
);
3597 * First time around; just get name length and mark it as first
3601 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3602 len
= hptr
- p
->name
;
3604 len
= strlen(p
->name
);
3606 if (len
>= sizeof(name
))
3609 * If the printer name length somehow is greater than we normally allow,
3610 * skip this printer...
3614 cupsArrayRestore(Printers
);
3618 strncpy(name
, p
->name
, len
);
3622 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3623 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3626 * Can't use same name as a local printer; add "Any" to the
3627 * front of the name, unless we have explicitly disabled
3628 * the "ImplicitAnyClasses"...
3631 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3634 * Add "Any" to the class name...
3637 strcpy(name
, "Any");
3638 strncpy(name
+ 3, p
->name
, len
);
3639 name
[len
+ 3] = '\0';
3645 * Don't create an implicit class if we have a local printer
3646 * with the same name...
3650 cupsArrayRestore(Printers
);
3658 cupsArrayRestore(Printers
);
3662 * Update the last printer class as needed...
3665 if (pclass
&& update
)
3666 cupsdSetPrinterAttrs(pclass
);
3671 * 'send_cups_browse()' - Send new browsing information using the CUPS
3676 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3678 int i
; /* Looping var */
3679 cups_ptype_t type
; /* Printer type */
3680 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3681 int bytes
; /* Length of packet */
3682 char packet
[1453], /* Browse data packet */
3683 uri
[1024], /* Printer URI */
3684 location
[1024], /* printer-location */
3685 info
[1024], /* printer-info */
3687 /* printer-make-and-model */
3688 cupsd_netif_t
*iface
; /* Network interface */
3692 * Figure out the printer type value...
3695 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3698 type
|= CUPS_PRINTER_REJECTING
;
3700 if (p
== DefaultPrinter
)
3701 type
|= CUPS_PRINTER_DEFAULT
;
3704 * Remove quotes from printer-info, printer-location, and
3705 * printer-make-and-model attributes...
3708 dequote(location
, p
->location
, sizeof(location
));
3709 dequote(info
, p
->info
, sizeof(info
));
3712 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3713 else if (p
->type
& CUPS_PRINTER_CLASS
)
3715 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3716 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3718 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3721 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3723 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3726 * Send a packet to each browse address...
3729 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3733 * Send the browse packet to one or more interfaces...
3736 if (!strcmp(b
->iface
, "*"))
3739 * Send to all local interfaces...
3744 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3746 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3749 * Only send to local, IPv4 interfaces...
3752 if (!iface
->is_local
|| !iface
->port
||
3753 iface
->address
.addr
.sa_family
!= AF_INET
)
3756 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3757 iface
->hostname
, iface
->port
,
3758 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3761 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3762 type
, p
->state
, uri
, location
, info
, make_model
,
3763 p
->browse_attrs
? p
->browse_attrs
: "");
3765 bytes
= strlen(packet
);
3767 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3768 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3769 iface
->name
, packet
);
3771 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3773 sendto(BrowseSocket
, packet
, bytes
, 0,
3774 (struct sockaddr
*)&(iface
->broadcast
),
3775 httpAddrLength(&(iface
->broadcast
)));
3778 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3781 * Send to the named interface using the IPv4 address...
3785 if (strcmp(b
->iface
, iface
->name
))
3790 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3793 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3797 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3798 iface
->hostname
, iface
->port
,
3799 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3802 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3803 type
, p
->state
, uri
, location
, info
, make_model
,
3804 p
->browse_attrs
? p
->browse_attrs
: "");
3806 bytes
= strlen(packet
);
3808 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3809 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3810 iface
->name
, packet
);
3812 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3814 sendto(BrowseSocket
, packet
, bytes
, 0,
3815 (struct sockaddr
*)&(iface
->broadcast
),
3816 httpAddrLength(&(iface
->broadcast
)));
3823 * Send the browse packet to the indicated address using
3824 * the default server name...
3827 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3828 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3829 p
->browse_attrs
? p
->browse_attrs
: "");
3831 bytes
= strlen(packet
);
3832 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3833 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3835 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3836 (struct sockaddr
*)&(b
->to
),
3837 httpAddrLength(&(b
->to
))) <= 0)
3840 * Unable to send browse packet, so remove this address from the
3844 cupsdLogMessage(CUPSD_LOG_ERROR
,
3845 "cupsdSendBrowseList: sendto failed for browser "
3847 (int)(b
- Browsers
+ 1), strerror(errno
));
3850 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3861 * 'ldap_search_rec()' - LDAP Search with reconnect
3865 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3866 char *base
, /* I - Base dn */
3867 int scope
, /* I - LDAP search scope */
3868 char *filter
, /* I - Filter string */
3869 char *attrs
[], /* I - Requested attributes */
3870 int attrsonly
, /* I - Return only attributes? */
3871 LDAPMessage
**res
) /* I - LDAP handler */
3873 int rc
; /* Return code */
3876 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3877 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3878 NULL
, LDAP_NO_LIMIT
, res
);
3880 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3881 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3884 * If we have a connection problem try again...
3887 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3889 cupsdLogMessage(CUPSD_LOG_ERROR
,
3890 "LDAP search failed with status %d: %s",
3891 rc
, ldap_err2string(rc
));
3892 cupsdLogMessage(CUPSD_LOG_INFO
,
3893 "We try the LDAP search once again after reconnecting to "
3898 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3899 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3900 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3902 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3903 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3906 if (rc
== LDAP_NO_SUCH_OBJECT
)
3907 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3908 "ldap_search_rec: LDAP entry/object not found");
3909 else if (rc
!= LDAP_SUCCESS
)
3910 cupsdLogMessage(CUPSD_LOG_ERROR
,
3911 "ldap_search_rec: LDAP search failed with status %d: %s",
3912 rc
, ldap_err2string(rc
));
3914 if (rc
!= LDAP_SUCCESS
)
3922 * 'ldap_freeres()' - Free LDAPMessage
3926 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3928 int rc
; /* Return value */
3931 rc
= ldap_msgfree(entry
);
3933 cupsdLogMessage(CUPSD_LOG_WARN
,
3934 "Can't free LDAPMessage!");
3936 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3937 "Freeing LDAPMessage was unnecessary");
3942 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3946 ldap_getval_firststring(
3947 LDAP
*ld
, /* I - LDAP handler */
3948 LDAPMessage
*entry
, /* I - LDAP message or search result */
3949 char *attr
, /* I - the wanted attribute */
3950 char *retval
, /* O - String to return */
3951 unsigned long maxsize
) /* I - Max string size */
3953 char *dn
; /* LDAP DN */
3954 int rc
= 0; /* Return code */
3955 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3956 struct berval
**bval
; /* LDAP value array */
3957 unsigned long size
; /* String size */
3961 * Get value from LDAPMessage...
3964 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3967 dn
= ldap_get_dn(ld
, entry
);
3968 cupsdLogMessage(CUPSD_LOG_WARN
,
3969 "Failed to get LDAP value %s for %s!",
3977 * Check size and copy value into our string...
3981 if (size
< bval
[0]->bv_len
)
3984 dn
= ldap_get_dn(ld
, entry
);
3985 cupsdLogMessage(CUPSD_LOG_WARN
,
3986 "Attribute %s is too big! (dn: %s)",
3991 size
= bval
[0]->bv_len
;
3993 strlcpy(retval
, bval
[0]->bv_val
, size
);
3994 ldap_value_free_len(bval
);
3997 char **value
; /* LDAP value */
4000 * Get value from LDAPMessage...
4003 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4006 dn
= ldap_get_dn(ld
, entry
);
4007 cupsdLogMessage(CUPSD_LOG_WARN
,
4008 "Failed to get LDAP value %s for %s!",
4014 strlcpy(retval
, *value
, maxsize
);
4015 ldap_value_free(value
);
4017 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4024 * 'send_ldap_ou()' - Send LDAP ou registrations.
4028 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4029 char *basedn
, /* I - Our base dn */
4030 char *descstring
) /* I - Description for ou */
4032 int i
; /* Looping var... */
4033 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4034 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4035 LDAPMessage
*res
, /* Search result token */
4036 *e
; /* Current entry from search */
4037 int rc
; /* LDAP status */
4038 char dn
[1024], /* DN of the organizational unit we are adding */
4039 *desc
[2], /* Change records */
4041 char old_desc
[1024]; /* Old description */
4042 static const char * const objectClass_values
[] =
4043 { /* The 2 objectClass's we use in */
4044 "top", /* our LDAP entries */
4045 "organizationalUnit",
4048 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4055 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4058 * Reconnect if LDAP Handle is invalid...
4061 if (! BrowseLDAPHandle
)
4063 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4064 "send_ldap_ou: LDAP Handle is invalid. Try "
4071 * Prepare ldap search...
4074 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4075 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4079 desc
[0] = descstring
;
4082 mods
[0].mod_type
= "ou";
4083 mods
[0].mod_values
= ou_value
;
4084 mods
[1].mod_type
= "description";
4085 mods
[1].mod_values
= desc
;
4086 mods
[2].mod_type
= "objectClass";
4087 mods
[2].mod_values
= (char **)objectClass_values
;
4089 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4090 (char **)ou_attrs
, 0, &res
);
4093 * If ldap search was not successfull then exit function...
4096 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4100 * Check if we need to insert or update the LDAP entry...
4103 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4104 rc
!= LDAP_NO_SUCH_OBJECT
)
4107 * Printserver has already been registered, check if
4108 * modification is required...
4111 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4114 * Get the required values from this entry...
4117 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4118 sizeof(old_desc
)) == -1)
4122 * Check if modification is required...
4125 if ( strcmp(desc
[0], old_desc
) == 0 )
4128 * LDAP entry for the printer exists.
4129 * Printer has already been registered,
4130 * no modifications required...
4132 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4133 "send_ldap_ou: No updates required for %s", ou
);
4138 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4139 "send_ldap_ou: Replace entry for %s", ou
);
4141 for (i
= 0; i
< 3; i
++)
4143 pmods
[i
] = mods
+ i
;
4144 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4148 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4149 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4150 NULL
)) != LDAP_SUCCESS
)
4152 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4153 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4155 cupsdLogMessage(CUPSD_LOG_ERROR
,
4156 "LDAP modify for %s failed with status %d: %s",
4157 ou
, rc
, ldap_err2string(rc
));
4158 if ( LDAP_SERVER_DOWN
== rc
)
4166 * Printserver has never been registered,
4167 * add registration...
4170 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4171 "send_ldap_ou: Add entry for %s", ou
);
4173 for (i
= 0; i
< 3; i
++)
4175 pmods
[i
] = mods
+ i
;
4176 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4180 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4181 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4182 NULL
)) != LDAP_SUCCESS
)
4184 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4185 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4187 cupsdLogMessage(CUPSD_LOG_ERROR
,
4188 "LDAP add for %s failed with status %d: %s",
4189 ou
, rc
, ldap_err2string(rc
));
4190 if ( LDAP_SERVER_DOWN
== rc
)
4200 * 'send_ldap_browse()' - Send LDAP printer registrations.
4204 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4206 int i
; /* Looping var... */
4207 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4208 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4209 LDAPMessage
*res
, /* Search result token */
4210 *e
; /* Current entry from search */
4211 char *cn_value
[2], /* Change records */
4217 typestring
[255], /* String to hold printer-type */
4218 dn
[1024]; /* DN of the printer we are adding */
4219 int rc
; /* LDAP status */
4220 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4221 old_location
[1024], /* Printer location */
4222 old_info
[1024], /* Printer information */
4223 old_make_model
[1024], /* Printer make and model */
4224 old_type_string
[30]; /* Temporary type number */
4225 int old_type
; /* Printer type */
4226 static const char * const objectClass_values
[] =
4227 { /* The 3 objectClass's we use in */
4228 "top", /* our LDAP entries */
4235 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4238 * Exit function if LDAP updates has been disabled...
4241 if (!BrowseLDAPUpdate
)
4243 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4244 "send_ldap_browse: Updates temporary disabled; "
4250 * Reconnect if LDAP Handle is invalid...
4253 if (!BrowseLDAPHandle
)
4255 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4256 "send_ldap_browse: LDAP Handle is invalid. Try "
4263 * Everything in ldap is ** so we fudge around it...
4266 sprintf(typestring
, "%u", p
->type
);
4268 cn_value
[0] = p
->name
;
4270 info
[0] = p
->info
? p
->info
: "Unknown";
4272 location
[0] = p
->location
? p
->location
: "Unknown";
4274 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4275 make_model
[1] = NULL
;
4276 type
[0] = typestring
;
4282 * Get ldap entry for printer ...
4285 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4287 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4289 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4290 (char **)ldap_attrs
, 0, &res
);
4293 * If ldap search was not successfull then exit function
4294 * and temporary disable LDAP updates...
4297 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4299 if (BrowseLDAPUpdate
&&
4300 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4302 BrowseLDAPUpdate
= FALSE
;
4303 cupsdLogMessage(CUPSD_LOG_INFO
,
4304 "LDAP update temporary disabled");
4311 * Fill modification array...
4314 mods
[0].mod_type
= "cn";
4315 mods
[0].mod_values
= cn_value
;
4316 mods
[1].mod_type
= "printerDescription";
4317 mods
[1].mod_values
= info
;
4318 mods
[2].mod_type
= "printerURI";
4319 mods
[2].mod_values
= uri
;
4320 mods
[3].mod_type
= "printerLocation";
4321 mods
[3].mod_values
= location
;
4322 mods
[4].mod_type
= "printerMakeAndModel";
4323 mods
[4].mod_values
= make_model
;
4324 mods
[5].mod_type
= "printerType";
4325 mods
[5].mod_values
= type
;
4326 mods
[6].mod_type
= "objectClass";
4327 mods
[6].mod_values
= (char **)objectClass_values
;
4330 * Check if we need to insert or update the LDAP entry...
4333 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4334 rc
!= LDAP_NO_SUCH_OBJECT
)
4337 * Printer has already been registered, check if
4338 * modification is required...
4341 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4344 * Get the required values from this entry...
4347 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4348 old_info
, sizeof(old_info
)) == -1)
4351 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4352 old_location
, sizeof(old_location
)) == -1)
4355 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4356 old_make_model
, sizeof(old_make_model
)) == -1)
4359 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4360 old_type_string
, sizeof(old_type_string
)) == -1)
4363 old_type
= atoi(old_type_string
);
4365 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4366 sizeof(old_uri
)) == -1)
4370 * Check if modification is required...
4373 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4374 !strcmp(location
[0], old_location
) &&
4375 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4378 * LDAP entry for the printer exists. Printer has already been registered,
4379 * no modifications required...
4382 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4383 "send_ldap_browse: No updates required for %s", p
->name
);
4388 * LDAP entry for the printer exists. Printer has already been registered,
4389 * modify the current registration...
4392 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4393 "send_ldap_browse: Replace entry for %s", p
->name
);
4395 for (i
= 0; i
< 7; i
++)
4397 pmods
[i
] = mods
+ i
;
4398 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4402 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4403 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4404 NULL
)) != LDAP_SUCCESS
)
4406 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4407 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4409 cupsdLogMessage(CUPSD_LOG_ERROR
,
4410 "LDAP modify for %s failed with status %d: %s",
4411 p
->name
, rc
, ldap_err2string(rc
));
4412 if (rc
== LDAP_SERVER_DOWN
)
4420 * No LDAP entry exists for the printer. Printer has never been registered,
4421 * add the current registration...
4424 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4426 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4427 "send_ldap_browse: Add entry for %s", p
->name
);
4429 for (i
= 0; i
< 7; i
++)
4431 pmods
[i
] = mods
+ i
;
4432 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4436 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4437 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4438 NULL
)) != LDAP_SUCCESS
)
4440 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4441 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4443 cupsdLogMessage(CUPSD_LOG_ERROR
,
4444 "LDAP add for %s failed with status %d: %s",
4445 p
->name
, rc
, ldap_err2string(rc
));
4446 if (rc
== LDAP_SERVER_DOWN
)
4456 * 'ldap_dereg_printer()' - Delete printer from directory
4460 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4462 char dn
[1024]; /* DN of the printer */
4463 int rc
; /* LDAP status */
4466 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4470 * Reconnect if LDAP Handle is invalid...
4473 if (!BrowseLDAPHandle
)
4480 * Get dn for printer and delete LDAP entry...
4483 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4485 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4487 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4488 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4489 NULL
)) != LDAP_SUCCESS
)
4491 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4492 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4494 cupsdLogMessage(CUPSD_LOG_WARN
,
4495 "LDAP delete for %s failed with status %d: %s",
4496 p
->name
, rc
, ldap_err2string(rc
));
4499 * If we had a connection problem (connection timed out, etc.)
4500 * we should reconnect and try again to delete the entry...
4503 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4505 cupsdLogMessage(CUPSD_LOG_INFO
,
4506 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4509 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4510 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4511 NULL
)) != LDAP_SUCCESS
)
4513 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4514 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4515 cupsdLogMessage(CUPSD_LOG_WARN
,
4516 "LDAP delete for %s failed with status %d: %s",
4517 p
->name
, rc
, ldap_err2string(rc
));
4524 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4525 char *basedn
) /* I - Dase dn */
4527 char dn
[1024]; /* DN of the printer */
4528 int rc
; /* LDAP status */
4531 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4534 * Reconnect if LDAP Handle is invalid...
4537 if (!BrowseLDAPHandle
)
4544 * Get dn for printer and delete LDAP entry...
4547 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4548 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4550 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4551 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4552 NULL
)) != LDAP_SUCCESS
)
4554 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4555 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4557 cupsdLogMessage(CUPSD_LOG_WARN
,
4558 "LDAP delete for %s failed with status %d: %s",
4559 ou
, rc
, ldap_err2string(rc
));
4562 * If we had a connection problem (connection timed out, etc.)
4563 * we should reconnect and try again to delete the entry...
4566 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4568 cupsdLogMessage(CUPSD_LOG_INFO
,
4569 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4571 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4572 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4573 NULL
)) != LDAP_SUCCESS
)
4575 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4576 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4577 cupsdLogMessage(CUPSD_LOG_WARN
,
4578 "LDAP delete for %s failed with status %d: %s",
4579 ou
, rc
, ldap_err2string(rc
));
4584 #endif /* HAVE_LDAP */
4589 * 'send_slp_browse()' - Register the specified printer with SLP.
4593 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4595 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4596 attrs
[8192], /* Printer attributes */
4597 finishings
[1024], /* Finishings to support */
4598 make_model
[IPP_MAX_NAME
* 2],
4599 /* Make and model, quoted */
4600 location
[IPP_MAX_NAME
* 2],
4601 /* Location, quoted */
4602 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4603 *src
, /* Pointer to original string */
4604 *dst
; /* Pointer to destination string */
4605 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4606 SLPError error
; /* SLP error, if any */
4609 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4613 * Make the SLP service URL that conforms to the IANA
4614 * 'printer:' template.
4617 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4619 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4622 * Figure out the finishings string...
4625 if (p
->type
& CUPS_PRINTER_STAPLE
)
4626 strcpy(finishings
, "staple");
4628 finishings
[0] = '\0';
4630 if (p
->type
& CUPS_PRINTER_BIND
)
4633 strlcat(finishings
, ",bind", sizeof(finishings
));
4635 strcpy(finishings
, "bind");
4638 if (p
->type
& CUPS_PRINTER_PUNCH
)
4641 strlcat(finishings
, ",punch", sizeof(finishings
));
4643 strcpy(finishings
, "punch");
4646 if (p
->type
& CUPS_PRINTER_COVER
)
4649 strlcat(finishings
, ",cover", sizeof(finishings
));
4651 strcpy(finishings
, "cover");
4654 if (p
->type
& CUPS_PRINTER_SORT
)
4657 strlcat(finishings
, ",sort", sizeof(finishings
));
4659 strcpy(finishings
, "sort");
4663 strcpy(finishings
, "none");
4666 * Quote any commas in the make and model, location, and info strings...
4669 for (src
= p
->make_model
, dst
= make_model
;
4670 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4672 if (*src
== ',' || *src
== '\\' || *src
== ')')
4681 strcpy(make_model
, "Unknown");
4683 for (src
= p
->location
, dst
= location
;
4684 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4686 if (*src
== ',' || *src
== '\\' || *src
== ')')
4695 strcpy(location
, "Unknown");
4697 for (src
= p
->info
, dst
= info
;
4698 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4700 if (*src
== ',' || *src
== '\\' || *src
== ')')
4709 strcpy(info
, "Unknown");
4712 * Get the authentication value...
4715 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4719 * Make the SLP attribute string list that conforms to
4720 * the IANA 'printer:' template.
4723 snprintf(attrs
, sizeof(attrs
),
4724 "(printer-uri-supported=%s),"
4725 "(uri-authentication-supported=%s>),"
4727 "(uri-security-supported=tls>),"
4729 "(uri-security-supported=none>),"
4730 #endif /* HAVE_SSL */
4731 "(printer-name=%s),"
4732 "(printer-location=%s),"
4733 "(printer-info=%s),"
4734 "(printer-more-info=%s),"
4735 "(printer-make-and-model=%s),"
4736 "(printer-type=%d),"
4737 "(charset-supported=utf-8),"
4738 "(natural-language-configured=%s),"
4739 "(natural-language-supported=de,en,es,fr,it),"
4740 "(color-supported=%s),"
4741 "(finishings-supported=%s),"
4742 "(sides-supported=one-sided%s),"
4743 "(multiple-document-jobs-supported=true)"
4744 "(ipp-versions-supported=1.0,1.1)",
4745 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4746 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4747 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4749 p
->type
& CUPS_PRINTER_DUPLEX
?
4750 ",two-sided-long-edge,two-sided-short-edge" : "");
4752 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4755 * Register the printer with the SLP server...
4758 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4759 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4761 if (error
!= SLP_OK
)
4762 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4768 * 'slp_attr_callback()' - SLP attribute callback
4771 static SLPBoolean
/* O - SLP_TRUE for success */
4773 SLPHandle hslp
, /* I - SLP handle */
4774 const char *attrlist
, /* I - Attribute list */
4775 SLPError errcode
, /* I - Parsing status for this attr */
4776 void *cookie
) /* I - Current printer */
4778 char *tmp
= 0; /* Temporary string */
4779 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4780 /* Current printer */
4783 (void)hslp
; /* anti-compiler-warning-code */
4786 * Bail if there was an error
4789 if (errcode
!= SLP_OK
)
4793 * Parse the attrlist to obtain things needed to build CUPS browse packet
4796 memset(p
, 0, sizeof(cupsd_printer_t
));
4798 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4800 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4802 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4804 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4805 p
->type
= atoi(tmp
);
4807 p
->type
= CUPS_PRINTER_REMOTE
;
4809 cupsdClearString(&tmp
);
4816 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4820 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4822 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4825 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4827 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4830 * Make the SLP service URL that conforms to the IANA
4831 * 'printer:' template.
4834 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4837 * Deregister the printer...
4840 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4846 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4849 static int /* O - 0 on success */
4850 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4851 const char *tag
, /* I - Name of attribute */
4852 char **valbuf
) /* O - Value */
4854 char *ptr1
, /* Pointer into string */
4858 cupsdClearString(valbuf
);
4860 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4862 ptr1
+= strlen(tag
);
4864 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4870 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4871 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4874 * Dequote the value...
4877 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4878 if (*ptr1
== '\\' && ptr1
[1])
4879 _cups_strcpy(ptr1
, ptr1
+ 1);
4890 * 'slp_reg_callback()' - Empty SLPRegReport.
4894 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4895 SLPError errcode
, /* I - Error code, if any */
4896 void *cookie
) /* I - App data */
4907 * 'slp_url_callback()' - SLP service url callback
4910 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4912 SLPHandle hslp
, /* I - SLP handle */
4913 const char *srvurl
, /* I - URL of service */
4914 unsigned short lifetime
, /* I - Life of service */
4915 SLPError errcode
, /* I - Existing error code */
4916 void *cookie
) /* I - Pointer to service list */
4918 slpsrvurl_t
*s
, /* New service entry */
4919 **head
; /* Pointer to head of entry */
4923 * Let the compiler know we won't be using these vars...
4930 * Bail if there was an error
4933 if (errcode
!= SLP_OK
)
4937 * Grab the head of the list...
4940 head
= (slpsrvurl_t
**)cookie
;
4943 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4946 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4950 * Copy the SLP service URL...
4953 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4956 * Link the SLP service URL into the head of the list
4966 #endif /* HAVE_LIBSLP */
4970 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4974 update_cups_browse(void)
4976 int i
; /* Looping var */
4977 int auth
; /* Authorization status */
4978 int len
; /* Length of name string */
4979 int bytes
; /* Number of bytes left */
4980 char packet
[1541], /* Broadcast packet */
4981 *pptr
; /* Pointer into packet */
4982 socklen_t srclen
; /* Length of source address */
4983 http_addr_t srcaddr
; /* Source address */
4984 char srcname
[1024]; /* Source hostname */
4985 unsigned address
[4]; /* Source address */
4986 unsigned type
; /* Printer type */
4987 unsigned state
; /* Printer state */
4988 char uri
[HTTP_MAX_URI
], /* Printer URI */
4989 host
[HTTP_MAX_URI
], /* Host portion of URI */
4990 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4991 info
[IPP_MAX_NAME
], /* Information string */
4992 location
[IPP_MAX_NAME
], /* Location string */
4993 make_model
[IPP_MAX_NAME
];/* Make and model string */
4994 int num_attrs
; /* Number of attributes */
4995 cups_option_t
*attrs
; /* Attributes */
4999 * Read a packet from the browse socket...
5002 srclen
= sizeof(srcaddr
);
5003 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5004 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5007 * "Connection refused" is returned under Linux if the destination port
5008 * or address is unreachable from a previous sendto(); check for the
5009 * error here and ignore it for now...
5012 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5014 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5016 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5019 closesocket(BrowseSocket
);
5021 close(BrowseSocket
);
5024 cupsdRemoveSelect(BrowseSocket
);
5027 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5028 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5034 packet
[bytes
] = '\0';
5037 * If we're about to sleep, ignore incoming browse packets.
5044 * Figure out where it came from...
5048 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5050 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5051 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5052 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5053 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5056 #endif /* AF_INET6 */
5061 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5064 if (HostNameLookups
)
5065 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5067 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5069 len
= strlen(srcname
);
5077 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5080 * Access from localhost (127.0.0.1) is always allowed...
5083 auth
= CUPSD_AUTH_ALLOW
;
5088 * Do authorization checks on the domain/address...
5091 switch (BrowseACL
->order_type
)
5094 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5097 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5098 auth
= CUPSD_AUTH_ALLOW
;
5100 if (cupsdCheckAuth(address
, srcname
, len
,
5101 BrowseACL
->num_deny
, BrowseACL
->deny
))
5102 auth
= CUPSD_AUTH_DENY
;
5104 if (cupsdCheckAuth(address
, srcname
, len
,
5105 BrowseACL
->num_allow
, BrowseACL
->allow
))
5106 auth
= CUPSD_AUTH_ALLOW
;
5109 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5110 auth
= CUPSD_AUTH_DENY
;
5112 if (cupsdCheckAuth(address
, srcname
, len
,
5113 BrowseACL
->num_allow
, BrowseACL
->allow
))
5114 auth
= CUPSD_AUTH_ALLOW
;
5116 if (cupsdCheckAuth(address
, srcname
, len
,
5117 BrowseACL
->num_deny
, BrowseACL
->deny
))
5118 auth
= CUPSD_AUTH_DENY
;
5124 auth
= CUPSD_AUTH_ALLOW
;
5126 if (auth
== CUPSD_AUTH_DENY
)
5128 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5129 "update_cups_browse: Refused %d bytes from %s", bytes
,
5134 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5135 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5142 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5144 cupsdLogMessage(CUPSD_LOG_WARN
,
5145 "update_cups_browse: Garbled browse packet - %s", packet
);
5149 strcpy(location
, "Location Unknown");
5150 strcpy(info
, "No Information Available");
5151 make_model
[0] = '\0';
5155 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5158 * Have extended information; can't use sscanf for it because not all
5159 * sscanf's allow empty strings with %[^\"]...
5162 for (i
= 0, pptr
++;
5163 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5165 location
[i
] = *pptr
;
5173 while (*pptr
&& isspace(*pptr
& 255))
5178 for (i
= 0, pptr
++;
5179 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5188 while (*pptr
&& isspace(*pptr
& 255))
5193 for (i
= 0, pptr
++;
5194 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5196 make_model
[i
] = *pptr
;
5201 make_model
[i
] = '\0';
5204 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5210 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5211 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5212 type
, state
, uri
, location
, info
, make_model
));
5215 * Pull the URI apart to see if this is a local or remote printer...
5218 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5220 cupsFreeOptions(num_attrs
, attrs
);
5228 for (i
= 0; i
< NumRelays
; i
++)
5229 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5230 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5231 (struct sockaddr
*)&(Relays
[i
].to
),
5232 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5234 cupsdLogMessage(CUPSD_LOG_ERROR
,
5235 "update_cups_browse: sendto failed for relay %d - %s.",
5236 i
+ 1, strerror(errno
));
5237 cupsFreeOptions(num_attrs
, attrs
);
5242 * Process the browse data...
5245 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5246 (ipp_pstate_t
)state
, location
, info
, make_model
,
5252 * 'update_lpd()' - Update the LPD configuration as needed.
5256 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5263 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5264 * setting for backwards-compatibility.
5267 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5269 #endif /* __APPLE__ */
5271 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5274 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5277 char newfile
[1024]; /* New cups-lpd.N file */
5278 cups_file_t
*ofp
, /* Original file pointer */
5279 *nfp
; /* New file pointer */
5280 char line
[1024]; /* Line from file */
5283 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5285 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5287 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5288 LPDConfigFile
+ 9, strerror(errno
));
5292 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5294 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5295 newfile
, strerror(errno
));
5301 * Copy all of the lines from the cups-lpd file...
5304 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5308 cupsFilePrintf(nfp
, "%s\n", line
);
5309 snprintf(line
, sizeof(line
), "\tdisable = %s",
5310 onoff
? "no" : "yes");
5312 else if (!strstr(line
, "disable ="))
5313 cupsFilePrintf(nfp
, "%s\n", line
);
5318 rename(newfile
, LPDConfigFile
+ 9);
5321 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5324 * Enable/disable LPD via the launchctl command...
5327 char *argv
[5], /* Arguments for command */
5328 *envp
[MAX_ENV
]; /* Environment for command */
5329 int pid
; /* Process ID */
5332 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5333 argv
[0] = (char *)"launchctl";
5334 argv
[1] = (char *)(onoff
? "load" : "unload");
5335 argv
[2] = (char *)"-w";
5336 argv
[3] = LPDConfigFile
+ 10;
5339 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5342 #endif /* __APPLE__ */
5344 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5349 * 'update_polling()' - Read status messages from the poll daemons.
5353 update_polling(void)
5355 char *ptr
, /* Pointer to end of line in buffer */
5356 message
[1024]; /* Pointer to message text */
5357 int loglevel
; /* Log level for message */
5360 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5361 message
, sizeof(message
))) != NULL
)
5363 if (loglevel
== CUPSD_LOG_INFO
)
5364 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5366 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5370 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5373 * All polling processes have died; stop polling...
5376 cupsdLogMessage(CUPSD_LOG_ERROR
,
5377 "update_polling: all polling processes have exited!");
5384 * 'update_smb()' - Update the SMB configuration as needed.
5388 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5393 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5396 * Enable/disable SMB via the specified smb.conf config file...
5399 char newfile
[1024]; /* New smb.conf.N file */
5400 cups_file_t
*ofp
, /* Original file pointer */
5401 *nfp
; /* New file pointer */
5402 char line
[1024]; /* Line from file */
5403 int in_printers
; /* In [printers] section? */
5406 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5408 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5410 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5411 SMBConfigFile
+ 8, strerror(errno
));
5415 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5417 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5418 newfile
, strerror(errno
));
5424 * Copy all of the lines from the smb.conf file...
5429 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5431 if (in_printers
&& strstr(line
, "printable ="))
5432 snprintf(line
, sizeof(line
), " printable = %s",
5433 onoff
? "yes" : "no");
5435 cupsFilePrintf(nfp
, "%s\n", line
);
5438 in_printers
= !strcmp(line
, "[printers]");
5443 rename(newfile
, SMBConfigFile
+ 8);
5446 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5451 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".