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
|
254 CUPS_PRINTER_SCANNER
)))
258 * Announce the deletion...
261 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
263 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
265 p
->type
|= CUPS_PRINTER_DELETE
;
273 if (BrowseLocalProtocols
& BROWSE_SLP
)
274 slp_dereg_printer(p
);
275 #endif /* HAVE_LIBSLP */
278 if (BrowseLocalProtocols
& BROWSE_LDAP
)
279 ldap_dereg_printer(p
);
280 #endif /* HAVE_LDAP */
283 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
284 dnssdDeregisterPrinter(p
);
285 #endif /* HAVE_DNSSD */
290 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
294 cupsdLoadRemoteCache(void)
296 int i
; /* Looping var */
297 cups_file_t
*fp
; /* remote.cache file */
298 int linenum
; /* Current line number */
299 char line
[4096], /* Line from file */
300 *value
, /* Pointer to value */
301 *valueptr
, /* Pointer into value */
302 scheme
[32], /* Scheme portion of URI */
303 username
[64], /* Username portion of URI */
305 /* Hostname portion of URI */
306 resource
[HTTP_MAX_URI
];
307 /* Resource portion of URI */
308 int port
; /* Port number */
309 cupsd_printer_t
*p
; /* Current printer */
310 time_t now
; /* Current time */
314 * Don't load the cache if the remote protocols are disabled...
319 cupsdLogMessage(CUPSD_LOG_DEBUG
,
320 "cupsdLoadRemoteCache: Not loading remote cache.");
325 * Open the remote.cache file...
328 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
329 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
333 * Read printer configurations until we hit EOF...
340 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
343 * Decode the directive...
346 if (!strcasecmp(line
, "<Printer") ||
347 !strcasecmp(line
, "<DefaultPrinter"))
350 * <Printer name> or <DefaultPrinter name>
353 if (p
== NULL
&& value
)
356 * Add the printer and a base file type...
359 cupsdLogMessage(CUPSD_LOG_DEBUG
,
360 "cupsdLoadRemoteCache: Loading printer %s...", value
);
362 if ((p
= cupsdFindDest(value
)) != NULL
)
364 if (p
->type
& CUPS_PRINTER_CLASS
)
366 cupsdLogMessage(CUPSD_LOG_WARN
,
367 "Cached remote printer \"%s\" conflicts with "
375 p
= cupsdAddPrinter(value
);
378 p
->state
= IPP_PRINTER_IDLE
;
379 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
380 p
->browse_time
= now
;
381 p
->browse_expire
= now
+ BrowseTimeout
;
384 * Set the default printer as needed...
387 if (!strcasecmp(line
, "<DefaultPrinter"))
392 cupsdLogMessage(CUPSD_LOG_ERROR
,
393 "Syntax error on line %d of remote.cache.", linenum
);
397 else if (!strcasecmp(line
, "<Class") ||
398 !strcasecmp(line
, "<DefaultClass"))
401 * <Class name> or <DefaultClass name>
404 if (p
== NULL
&& value
)
407 * Add the printer and a base file type...
410 cupsdLogMessage(CUPSD_LOG_DEBUG
,
411 "cupsdLoadRemoteCache: Loading class %s...", value
);
413 if ((p
= cupsdFindDest(value
)) != NULL
)
414 p
->type
= CUPS_PRINTER_CLASS
;
416 p
= cupsdAddClass(value
);
419 p
->state
= IPP_PRINTER_IDLE
;
420 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
421 p
->browse_time
= now
;
422 p
->browse_expire
= now
+ BrowseTimeout
;
425 * Set the default printer as needed...
428 if (!strcasecmp(line
, "<DefaultClass"))
433 cupsdLogMessage(CUPSD_LOG_ERROR
,
434 "Syntax error on line %d of remote.cache.", linenum
);
438 else if (!strcasecmp(line
, "</Printer>") ||
439 !strcasecmp(line
, "</Class>"))
444 * Close out the current printer...
447 cupsdSetPrinterAttrs(p
);
452 cupsdLogMessage(CUPSD_LOG_ERROR
,
453 "Syntax error on line %d of remote.cache.", linenum
);
457 cupsdLogMessage(CUPSD_LOG_ERROR
,
458 "Syntax error on line %d of remote.cache.", linenum
);
460 else if (!strcasecmp(line
, "Info"))
463 cupsdSetString(&p
->info
, value
);
465 else if (!strcasecmp(line
, "MakeModel"))
468 cupsdSetString(&p
->make_model
, value
);
470 else if (!strcasecmp(line
, "Location"))
473 cupsdSetString(&p
->location
, value
);
475 else if (!strcasecmp(line
, "DeviceURI"))
479 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
480 username
, sizeof(username
), host
, sizeof(host
), &port
,
481 resource
, sizeof(resource
));
483 cupsdSetString(&p
->hostname
, host
);
484 cupsdSetString(&p
->uri
, value
);
485 cupsdSetDeviceURI(p
, value
);
488 cupsdLogMessage(CUPSD_LOG_ERROR
,
489 "Syntax error on line %d of remote.cache.", linenum
);
491 else if (!strcasecmp(line
, "Option") && value
)
497 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Syntax error on line %d of remote.cache.", linenum
);
504 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
506 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
510 else if (!strcasecmp(line
, "Reason"))
514 for (i
= 0 ; i
< p
->num_reasons
; i
++)
515 if (!strcmp(value
, p
->reasons
[i
]))
518 if (i
>= p
->num_reasons
&&
519 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
521 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
526 cupsdLogMessage(CUPSD_LOG_ERROR
,
527 "Syntax error on line %d of remote.cache.", linenum
);
529 else if (!strcasecmp(line
, "State"))
532 * Set the initial queue state...
535 if (value
&& !strcasecmp(value
, "idle"))
536 p
->state
= IPP_PRINTER_IDLE
;
537 else if (value
&& !strcasecmp(value
, "stopped"))
539 p
->state
= IPP_PRINTER_STOPPED
;
540 cupsdSetPrinterReasons(p
, "+paused");
543 cupsdLogMessage(CUPSD_LOG_ERROR
,
544 "Syntax error on line %d of remote.cache.", linenum
);
546 else if (!strcasecmp(line
, "StateMessage"))
549 * Set the initial queue state message...
553 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
555 else if (!strcasecmp(line
, "Accepting"))
558 * Set the initial accepting state...
562 (!strcasecmp(value
, "yes") ||
563 !strcasecmp(value
, "on") ||
564 !strcasecmp(value
, "true")))
567 (!strcasecmp(value
, "no") ||
568 !strcasecmp(value
, "off") ||
569 !strcasecmp(value
, "false")))
572 cupsdLogMessage(CUPSD_LOG_ERROR
,
573 "Syntax error on line %d of remote.cache.", linenum
);
575 else if (!strcasecmp(line
, "Type"))
578 p
->type
= atoi(value
);
580 cupsdLogMessage(CUPSD_LOG_ERROR
,
581 "Syntax error on line %d of remote.cache.", linenum
);
583 else if (!strcasecmp(line
, "BrowseTime"))
587 time_t t
= atoi(value
);
589 if (t
> p
->browse_expire
)
590 p
->browse_expire
= t
;
593 cupsdLogMessage(CUPSD_LOG_ERROR
,
594 "Syntax error on line %d of remote.cache.", linenum
);
596 else if (!strcasecmp(line
, "JobSheets"))
599 * Set the initial job sheets...
604 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
609 cupsdSetString(&p
->job_sheets
[0], value
);
611 while (isspace(*valueptr
& 255))
616 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
621 cupsdSetString(&p
->job_sheets
[1], value
);
625 cupsdLogMessage(CUPSD_LOG_ERROR
,
626 "Syntax error on line %d of remote.cache.", linenum
);
628 else if (!strcasecmp(line
, "AllowUser"))
633 cupsdAddPrinterUser(p
, value
);
636 cupsdLogMessage(CUPSD_LOG_ERROR
,
637 "Syntax error on line %d of remote.cache.", linenum
);
639 else if (!strcasecmp(line
, "DenyUser"))
644 cupsdAddPrinterUser(p
, value
);
647 cupsdLogMessage(CUPSD_LOG_ERROR
,
648 "Syntax error on line %d of remote.cache.", linenum
);
653 * Something else we don't understand...
656 cupsdLogMessage(CUPSD_LOG_ERROR
,
657 "Unknown configuration directive %s on line %d of remote.cache.",
665 * Do auto-classing if needed...
668 process_implicit_classes();
673 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
674 * printer or update the broadcast contents.
678 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
680 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
683 if (!Browsing
|| !BrowseLocalProtocols
||
684 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
685 CUPS_PRINTER_SCANNER
)))
689 /* if (BrowseLocalProtocols & BROWSE_SLP)
690 slpRegisterPrinter(p); */
691 #endif /* HAVE_LIBSLP */
694 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
695 dnssdRegisterPrinter(p
);
696 #endif /* HAVE_DNSSD */
701 * 'cupsdRestartPolling()' - Restart polling servers as needed.
705 cupsdRestartPolling(void)
707 int i
; /* Looping var */
708 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
711 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
713 kill(pollp
->pid
, SIGHUP
);
718 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
722 cupsdSaveRemoteCache(void)
724 int i
; /* Looping var */
725 cups_file_t
*fp
; /* printers.conf file */
726 char temp
[1024], /* Temporary string */
727 value
[2048]; /* Value string */
728 cupsd_printer_t
*printer
; /* Current printer class */
729 time_t curtime
; /* Current time */
730 struct tm
*curdate
; /* Current date */
731 cups_option_t
*option
; /* Current option */
735 * Create the remote.cache file...
738 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
740 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
742 cupsdLogMessage(CUPSD_LOG_ERROR
,
743 "Unable to save remote.cache - %s", strerror(errno
));
747 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
750 * Restrict access to the file...
753 fchown(cupsFileNumber(fp
), getuid(), Group
);
754 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
757 * Write a small header to the file...
760 curtime
= time(NULL
);
761 curdate
= localtime(&curtime
);
762 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
764 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
765 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
768 * Write each local printer known to the system...
771 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
773 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
776 * Skip local destinations...
779 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
783 * Write printers as needed...
786 if (printer
== DefaultPrinter
)
787 cupsFilePuts(fp
, "<Default");
789 cupsFilePutChar(fp
, '<');
791 if (printer
->type
& CUPS_PRINTER_CLASS
)
792 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
794 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
796 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
799 cupsFilePutConf(fp
, "Info", printer
->info
);
801 if (printer
->location
)
802 cupsFilePutConf(fp
, "Location", printer
->location
);
804 if (printer
->make_model
)
805 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
807 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
809 if (printer
->state
== IPP_PRINTER_STOPPED
)
810 cupsFilePuts(fp
, "State Stopped\n");
812 cupsFilePuts(fp
, "State Idle\n");
814 for (i
= 0; i
< printer
->num_reasons
; i
++)
815 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
817 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
819 if (printer
->accepting
)
820 cupsFilePuts(fp
, "Accepting Yes\n");
822 cupsFilePuts(fp
, "Accepting No\n");
824 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
825 printer
->job_sheets
[1]);
826 cupsFilePutConf(fp
, "JobSheets", value
);
828 for (i
= 0; i
< printer
->num_users
; i
++)
829 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
832 for (i
= printer
->num_options
, option
= printer
->options
;
836 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
837 cupsFilePutConf(fp
, "Option", value
);
840 if (printer
->type
& CUPS_PRINTER_CLASS
)
841 cupsFilePuts(fp
, "</Class>\n");
843 cupsFilePuts(fp
, "</Printer>\n");
851 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
855 cupsdSendBrowseList(void)
857 int count
; /* Number of dests to update */
858 cupsd_printer_t
*p
; /* Current printer */
859 time_t ut
, /* Minimum update time */
860 to
; /* Timeout time */
863 if (!Browsing
|| !Printers
)
867 * Compute the update and timeout times...
871 ut
= to
- BrowseInterval
;
874 * Figure out how many printers need an update...
877 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
879 int max_count
; /* Maximum number to update */
883 * Throttle the number of printers we'll be updating this time
884 * around based on the number of queues that need updating and
885 * the maximum number of queues to update each second...
888 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
890 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
891 count
< max_count
&& p
!= NULL
;
892 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
893 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
894 CUPS_PRINTER_SCANNER
)) &&
895 p
->shared
&& p
->browse_time
< ut
)
899 * Loop through all of the printers and send local updates as needed...
903 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
905 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
909 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
912 * Check for wraparound...
916 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
920 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
921 CUPS_PRINTER_SCANNER
)) ||
924 else if (p
->browse_time
< ut
)
927 * Need to send an update...
932 p
->browse_time
= time(NULL
);
934 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
938 if (BrowseLocalProtocols
& BROWSE_SLP
)
940 #endif /* HAVE_LIBSLP */
943 if (BrowseLocalProtocols
& BROWSE_LDAP
)
945 #endif /* HAVE_LDAP */
950 * Save where we left off so that all printers get updated...
957 * Loop through all of the printers and timeout old printers as needed...
960 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
962 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
965 * If this is a remote queue, see if it needs to be timed out...
968 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
969 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
970 p
->browse_expire
< to
)
972 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
973 "%s \'%s\' deleted by directory services (timeout).",
974 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
977 cupsdLogMessage(CUPSD_LOG_DEBUG
,
978 "Remote destination \"%s\" has timed out; "
982 cupsArraySave(Printers
);
983 cupsdDeletePrinter(p
, 1);
984 cupsArrayRestore(Printers
);
985 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
991 #ifdef HAVE_LDAP_REBIND_PROC
992 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
994 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
998 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
999 LDAP_CONST
char *refsp
,
1007 * Bind to new LDAP server...
1010 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1011 "ldap_rebind_proc: Rebind to %s", refsp
);
1013 # if LDAP_API_VERSION > 3000
1015 bval
.bv_val
= BrowseLDAPPassword
;
1016 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1018 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1020 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
1021 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1022 # endif /* LDAP_API_VERSION > 3000 */
1027 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1030 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1034 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1046 * Free current values...
1049 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1050 "ldap_rebind_proc: Free values...");
1052 if ( dnp
&& *dnp
) {
1055 if ( passwdp
&& *passwdp
) {
1063 * Return credentials for LDAP referal...
1066 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1067 "ldap_rebind_proc: Return necessary values...");
1069 *dnp
= strdup(BrowseLDAPBindDN
);
1070 *passwdp
= strdup(BrowseLDAPPassword
);
1071 *authmethodp
= LDAP_AUTH_SIMPLE
;
1077 * Should never happen...
1080 cupsdLogMessage(CUPSD_LOG_ERROR
,
1081 "LDAP rebind has been called with wrong freeit value!");
1086 return (LDAP_SUCCESS
);
1088 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1089 #endif /* HAVE_LDAP_REBIND_PROC */
1094 * 'ldap_connect()' - Start new LDAP connection
1101 * Open LDAP handle...
1104 int rc
; /* LDAP API status */
1105 int version
= 3; /* LDAP version */
1106 struct berval bv
= {0, ""}; /* SASL bind value */
1107 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1108 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1109 int ldap_ssl
= 0; /* LDAP SSL indicator */
1110 int ssl_err
= 0; /* LDAP SSL error value */
1111 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1113 # ifdef HAVE_OPENLDAP
1114 # ifdef HAVE_LDAP_SSL
1117 * Set the certificate file to use for encrypted LDAP sessions...
1120 if (BrowseLDAPCACertFile
)
1122 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1123 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1124 BrowseLDAPCACertFile
);
1126 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1127 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1128 cupsdLogMessage(CUPSD_LOG_ERROR
,
1129 "Unable to set CA certificate file for LDAP "
1130 "connections: %d - %s", rc
, ldap_err2string(rc
));
1133 # endif /* HAVE_LDAP_SSL */
1135 * Initialize OPENLDAP connection...
1136 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1139 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1140 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1142 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1144 # else /* HAVE_OPENLDAP */
1146 int ldap_port
= 0; /* LDAP port */
1147 char ldap_protocol
[11], /* LDAP protocol */
1148 ldap_host
[255]; /* LDAP host */
1151 * Split LDAP URI into its components...
1154 if (! BrowseLDAPServer
)
1156 cupsdLogMessage(CUPSD_LOG_ERROR
,
1157 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1158 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1159 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1163 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1165 if (strcmp(ldap_protocol
, "ldap") == 0) {
1167 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1170 cupsdLogMessage(CUPSD_LOG_ERROR
,
1171 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1172 cupsdLogMessage(CUPSD_LOG_ERROR
,
1173 "Disable LDAP browsing!");
1174 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1175 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1182 ldap_port
= LDAPS_PORT
;
1184 ldap_port
= LDAP_PORT
;
1187 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1188 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1189 ldap_protocol
, ldap_host
, ldap_port
);
1192 * Initialize LDAP connection...
1197 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1198 rc
= LDAP_OPERATIONS_ERROR
;
1202 # ifdef HAVE_LDAP_SSL
1208 * Initialize SSL LDAP connection...
1210 if (BrowseLDAPCACertFile
)
1212 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1213 if (rc
!= LDAP_SUCCESS
) {
1214 cupsdLogMessage(CUPSD_LOG_ERROR
,
1215 "Failed to initialize LDAP SSL client!");
1216 rc
= LDAP_OPERATIONS_ERROR
;
1218 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1219 rc
= LDAP_OPERATIONS_ERROR
;
1226 cupsdLogMessage(CUPSD_LOG_ERROR
,
1227 "LDAP SSL certificate file/database not configured!");
1228 rc
= LDAP_OPERATIONS_ERROR
;
1231 # else /* HAVE_LDAP_SSL */
1234 * Return error, because client libraries doesn't support SSL
1237 cupsdLogMessage(CUPSD_LOG_ERROR
,
1238 "LDAP client libraries does not support TLS");
1239 rc
= LDAP_OPERATIONS_ERROR
;
1241 # endif /* HAVE_LDAP_SSL */
1243 # endif /* HAVE_OPENLDAP */
1246 * Check return code from LDAP initialize...
1249 if (rc
!= LDAP_SUCCESS
)
1251 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1253 cupsdLogMessage(CUPSD_LOG_ERROR
,
1254 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1258 cupsdLogMessage(CUPSD_LOG_ERROR
,
1259 "Unable to initialize LDAP! Disable LDAP browsing!");
1260 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1261 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1264 ldap_disconnect(TempBrowseLDAPHandle
);
1265 TempBrowseLDAPHandle
= NULL
;
1269 * Upgrade LDAP version...
1272 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1273 (const void *)&version
) != LDAP_SUCCESS
)
1275 cupsdLogMessage(CUPSD_LOG_ERROR
,
1276 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1278 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1279 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1280 ldap_disconnect(TempBrowseLDAPHandle
);
1281 TempBrowseLDAPHandle
= NULL
;
1287 * Register LDAP rebind procedure...
1290 # ifdef HAVE_LDAP_REBIND_PROC
1291 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1293 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1294 if ( rc
!= LDAP_SUCCESS
)
1295 cupsdLogMessage(CUPSD_LOG_ERROR
,
1296 "Setting LDAP rebind function failed with status %d: %s",
1297 rc
, ldap_err2string(rc
));
1301 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1303 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1304 # endif /* HAVE_LDAP_REBIND_PROC */
1307 * Start LDAP bind...
1310 # if LDAP_API_VERSION > 3000
1312 bval
.bv_val
= BrowseLDAPPassword
;
1313 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1315 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1316 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1319 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1321 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1322 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1323 # endif /* LDAP_API_VERSION > 3000 */
1325 if (rc
!= LDAP_SUCCESS
)
1327 cupsdLogMessage(CUPSD_LOG_ERROR
,
1328 "LDAP bind failed with error %d: %s",
1329 rc
, ldap_err2string(rc
));
1330 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1331 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1333 ssl_err
= PORT_GetError();
1335 cupsdLogMessage(CUPSD_LOG_ERROR
,
1336 "LDAP SSL error %d: %s",
1337 ssl_err
, ldapssl_err2string(ssl_err
));
1339 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1340 ldap_disconnect(TempBrowseLDAPHandle
);
1341 TempBrowseLDAPHandle
= NULL
;
1345 cupsdLogMessage(CUPSD_LOG_INFO
,
1346 "LDAP connection established");
1350 return (TempBrowseLDAPHandle
);
1355 * 'ldap_reconnect()' - Reconnect to LDAP Server
1359 ldap_reconnect(void)
1361 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1363 cupsdLogMessage(CUPSD_LOG_INFO
,
1364 "Try LDAP reconnect...");
1367 * Get a new LDAP Handle and replace the global Handle
1368 * if the new connection was successful
1371 TempBrowseLDAPHandle
= ldap_connect();
1373 if (TempBrowseLDAPHandle
!= NULL
)
1375 if (BrowseLDAPHandle
!= NULL
)
1377 ldap_disconnect(BrowseLDAPHandle
);
1379 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1385 * 'ldap_disconnect()' - Disconnect from LDAP Server
1389 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1391 int rc
; /* return code */
1394 * Close LDAP handle...
1397 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1398 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1400 rc
= ldap_unbind_s(ld
);
1401 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1402 if (rc
!= LDAP_SUCCESS
)
1403 cupsdLogMessage(CUPSD_LOG_ERROR
,
1404 "Unbind from LDAP server failed with status %d: %s",
1405 rc
, ldap_err2string(rc
));
1407 #endif /* HAVE_LDAP */
1411 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1415 cupsdStartBrowsing(void)
1417 int val
; /* Socket option value */
1418 struct sockaddr_in addr
; /* Broadcast address */
1419 cupsd_printer_t
*p
; /* Current printer */
1424 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1427 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1429 if (BrowseSocket
< 0)
1432 * Create the broadcast socket...
1435 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1437 cupsdLogMessage(CUPSD_LOG_ERROR
,
1438 "Unable to create broadcast socket - %s.",
1440 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1441 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1443 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1444 cupsdEndProcess(getpid(), 0);
1448 if (BrowseSocket
>= 0)
1451 * Bind the socket to browse port...
1454 memset(&addr
, 0, sizeof(addr
));
1455 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1456 addr
.sin_family
= AF_INET
;
1457 addr
.sin_port
= htons(BrowsePort
);
1459 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1461 cupsdLogMessage(CUPSD_LOG_ERROR
,
1462 "Unable to bind broadcast socket - %s.",
1466 closesocket(BrowseSocket
);
1468 close(BrowseSocket
);
1472 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1473 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1475 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1476 cupsdEndProcess(getpid(), 0);
1480 if (BrowseSocket
>= 0)
1483 * Set the "broadcast" flag...
1487 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1489 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1493 closesocket(BrowseSocket
);
1495 close(BrowseSocket
);
1499 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1500 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1502 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1503 cupsdEndProcess(getpid(), 0);
1507 if (BrowseSocket
>= 0)
1510 * Close the socket on exec...
1513 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1516 * Finally, add the socket to the input selection set as needed...
1519 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1522 * We only listen if we want remote printers...
1525 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1534 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1536 DNSServiceErrorType error
; /* Error from service creation */
1537 cupsd_listener_t
*lis
; /* Current listening socket */
1541 * First create a "master" connection for all registrations...
1544 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1545 != kDNSServiceErr_NoError
)
1547 cupsdLogMessage(CUPSD_LOG_ERROR
,
1548 "Unable to create master DNS-SD reference: %d", error
);
1550 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1551 cupsdEndProcess(getpid(), 0);
1556 * Add the master connection to the select list...
1559 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1560 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1563 * Then get the port we use for registrations. If we are not listening
1564 * on any non-local ports, there is no sense sharing local printers via
1570 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1572 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1574 if (httpAddrLocalhost(&(lis
->address
)))
1577 if (lis
->address
.addr
.sa_family
== AF_INET
)
1579 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1582 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1584 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1590 * Create an array to track the printers we share...
1593 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1594 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1598 * Set the computer name and register the web interface...
1601 cupsdUpdateDNSSDName();
1604 #endif /* HAVE_DNSSD */
1607 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1610 * Open SLP handle...
1613 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1615 cupsdLogMessage(CUPSD_LOG_ERROR
,
1616 "Unable to open an SLP handle; disabling SLP browsing!");
1617 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1618 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1619 BrowseSLPHandle
= NULL
;
1621 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1622 cupsdEndProcess(getpid(), 0);
1625 BrowseSLPRefresh
= 0;
1628 BrowseSLPHandle
= NULL
;
1629 #endif /* HAVE_LIBSLP */
1632 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1636 cupsdLogMessage(CUPSD_LOG_ERROR
,
1637 "Need to set BrowseLDAPDN to use LDAP browsing!");
1638 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1639 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1641 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1642 cupsdEndProcess(getpid(), 0);
1647 * Open LDAP handle...
1650 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1651 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1652 cupsdEndProcess(getpid(), 0);
1655 BrowseLDAPRefresh
= 0;
1657 #endif /* HAVE_LDAP */
1660 * Enable LPD and SMB printer sharing as needed through external programs...
1663 if (BrowseLocalProtocols
& BROWSE_LPD
)
1666 if (BrowseLocalProtocols
& BROWSE_SMB
)
1670 * Register the individual printers
1673 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1675 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1676 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1677 CUPS_PRINTER_SCANNER
)))
1678 cupsdRegisterPrinter(p
);
1683 * 'cupsdStartPolling()' - Start polling servers as needed.
1687 cupsdStartPolling(void)
1689 int i
; /* Looping var */
1690 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1691 char polld
[1024]; /* Poll daemon path */
1692 char sport
[255]; /* Server port */
1693 char bport
[255]; /* Browser port */
1694 char interval
[255]; /* Poll interval */
1695 int statusfds
[2]; /* Status pipe */
1696 char *argv
[6]; /* Arguments */
1697 char *envp
[100]; /* Environment */
1701 * Don't do anything if we aren't polling...
1704 if (NumPolled
== 0 || BrowseSocket
< 0)
1707 PollStatusBuffer
= NULL
;
1712 * Setup string arguments for polld, port and interval options.
1715 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1717 sprintf(bport
, "%d", BrowsePort
);
1720 sprintf(interval
, "%d", BrowseInterval
);
1722 strcpy(interval
, "30");
1724 argv
[0] = "cups-polld";
1730 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1733 * Create a pipe that receives the status messages from each
1737 if (cupsdOpenPipe(statusfds
))
1739 cupsdLogMessage(CUPSD_LOG_ERROR
,
1740 "Unable to create polling status pipes - %s.",
1743 PollStatusBuffer
= NULL
;
1747 PollPipe
= statusfds
[0];
1748 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1751 * Run each polling daemon, redirecting stderr to the polling pipe...
1754 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1756 sprintf(sport
, "%d", pollp
->port
);
1758 argv
[1] = pollp
->hostname
;
1760 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1761 0, DefaultProfile
, NULL
, &(pollp
->pid
)) < 0)
1763 cupsdLogMessage(CUPSD_LOG_ERROR
,
1764 "cupsdStartPolling: Unable to fork polling daemon - %s",
1770 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1771 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1772 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1775 close(statusfds
[1]);
1778 * Finally, add the pipe to the input selection set...
1781 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1786 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1790 cupsdStopBrowsing(void)
1792 cupsd_printer_t
*p
; /* Current printer */
1795 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1799 * De-register the individual printers
1802 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1804 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1805 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1806 CUPS_PRINTER_SCANNER
)))
1807 cupsdDeregisterPrinter(p
, 1);
1810 * Shut down browsing sockets...
1813 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1817 * Close the socket and remove it from the input selection set.
1821 closesocket(BrowseSocket
);
1823 close(BrowseSocket
);
1826 cupsdRemoveSelect(BrowseSocket
);
1831 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1833 #endif /* HAVE_DNSSD */
1836 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1840 * Close SLP handle...
1843 SLPClose(BrowseSLPHandle
);
1844 BrowseSLPHandle
= NULL
;
1846 #endif /* HAVE_LIBSLP */
1849 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1852 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1853 ldap_disconnect(BrowseLDAPHandle
);
1854 BrowseLDAPHandle
= NULL
;
1856 #endif /* HAVE_OPENLDAP */
1859 * Disable LPD and SMB printer sharing as needed through external programs...
1862 if (BrowseLocalProtocols
& BROWSE_LPD
)
1865 if (BrowseLocalProtocols
& BROWSE_SMB
)
1871 * 'cupsdStopPolling()' - Stop polling servers as needed.
1875 cupsdStopPolling(void)
1877 int i
; /* Looping var */
1878 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1883 cupsdStatBufDelete(PollStatusBuffer
);
1886 cupsdRemoveSelect(PollPipe
);
1889 PollStatusBuffer
= NULL
;
1892 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1894 cupsdEndProcess(pollp
->pid
, 0);
1900 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1904 cupsdUpdateDNSSDName(void)
1906 DNSServiceErrorType error
; /* Error from service creation */
1907 char webif
[1024]; /* Web interface share name */
1908 #ifdef HAVE_COREFOUNDATION_H
1909 SCDynamicStoreRef sc
; /* Context for dynamic store */
1910 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1911 CFStringRef nameRef
; /* Host name CFString */
1912 char nameBuffer
[1024]; /* C-string buffer */
1913 #endif /* HAVE_COREFOUNDATION_H */
1917 * Only share the web interface and printers when non-local listening is
1926 * Get the computer name as a c-string...
1929 #ifdef HAVE_COREFOUNDATION_H
1930 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1935 * Get the computer name from the dynamic store...
1938 cupsdClearString(&DNSSDName
);
1940 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1942 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1943 kCFStringEncodingUTF8
))
1945 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1946 "Dynamic store host name is \"%s\".", nameBuffer
);
1947 cupsdSetString(&DNSSDName
, nameBuffer
);
1956 * Use the ServerName instead...
1959 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1960 "Using ServerName \"%s\" as host name.", ServerName
);
1961 cupsdSetString(&DNSSDName
, ServerName
);
1965 * Get any Back-to-My-Mac domains and add them as aliases...
1968 cupsdFreeAliases(DNSSDAlias
);
1971 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
1972 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
1974 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
1975 (int)CFDictionaryGetCount(btmm
));
1976 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
1979 cupsdLogMessage(CUPSD_LOG_ERROR
,
1980 "Bad Back to My Mac data in dynamic store!");
1982 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
1990 #endif /* HAVE_COREFOUNDATION_H */
1991 cupsdSetString(&DNSSDName
, ServerName
);
1994 * Then (re)register the web interface if enabled...
2000 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
2002 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2005 DNSServiceRefDeallocate(WebIFRef
);
2007 WebIFRef
= DNSSDRef
;
2008 if ((error
= DNSServiceRegister(&WebIFRef
,
2009 kDNSServiceFlagsShareConnection
,
2010 0, webif
, "_http._tcp", NULL
,
2011 NULL
, htons(DNSSDPort
), 7,
2012 "\006path=/", dnssdRegisterCallback
,
2013 NULL
)) != kDNSServiceErr_NoError
)
2014 cupsdLogMessage(CUPSD_LOG_ERROR
,
2015 "DNS-SD web interface registration failed: %d", error
);
2018 #endif /* HAVE_DNSSD */
2023 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2027 cupsdUpdateLDAPBrowse(void)
2029 char uri
[HTTP_MAX_URI
], /* Printer URI */
2030 host
[HTTP_MAX_URI
], /* Hostname */
2031 resource
[HTTP_MAX_URI
], /* Resource path */
2032 location
[1024], /* Printer location */
2033 info
[1024], /* Printer information */
2034 make_model
[1024], /* Printer make and model */
2035 type_num
[30]; /* Printer type number */
2036 int type
; /* Printer type */
2037 int rc
; /* LDAP status */
2038 int limit
; /* Size limit */
2039 LDAPMessage
*res
, /* LDAP search results */
2040 *e
; /* Current entry from search */
2042 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2044 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2047 * Reconnect if LDAP Handle is invalid...
2050 if (! BrowseLDAPHandle
)
2057 * Search for cups printers in LDAP directory...
2060 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2061 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2064 * If ldap search was successfull then exit function
2065 * and temporary disable LDAP updates...
2068 if (rc
!= LDAP_SUCCESS
)
2070 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2072 BrowseLDAPUpdate
= FALSE
;
2073 cupsdLogMessage(CUPSD_LOG_INFO
,
2074 "LDAP update temporary disabled");
2080 * If LDAP updates were disabled, we will reenable them...
2083 if (! BrowseLDAPUpdate
)
2085 BrowseLDAPUpdate
= TRUE
;
2086 cupsdLogMessage(CUPSD_LOG_INFO
,
2087 "LDAP update enabled");
2091 * Count LDAP entries and return if no entry exist...
2094 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2095 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2103 * Loop through the available printers...
2106 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2108 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2111 * Get the required values from this entry...
2114 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2115 "printerDescription", info
, sizeof(info
)) == -1)
2118 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2119 "printerLocation", location
, sizeof(location
)) == -1)
2122 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2123 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2126 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2127 "printerType", type_num
, sizeof(type_num
)) == -1)
2130 type
= atoi(type_num
);
2132 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2133 "printerURI", uri
, sizeof(uri
)) == -1)
2137 * Process the entry as browse data...
2140 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2141 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2142 location
, info
, make_model
, 0, NULL
);
2148 #endif /* HAVE_LDAP */
2153 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2157 cupsdUpdateSLPBrowse(void)
2159 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2160 *next
; /* Next service in list */
2161 cupsd_printer_t p
; /* Printer information */
2162 const char *uri
; /* Pointer to printer URI */
2163 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2164 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2168 * Reset the refresh time...
2171 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2174 * Poll for remote printers using SLP...
2179 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2180 slp_url_callback
, &s
);
2183 * Loop through the list of available printers...
2189 * Save the "next" pointer...
2195 * Load a cupsd_printer_t structure with the SLP service attributes...
2198 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2201 * Process this printer entry...
2204 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2206 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2209 * Pull the URI apart to see if this is a local or remote printer...
2212 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2213 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2214 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2218 * Free this listing...
2221 cupsdClearString(&p
.info
);
2222 cupsdClearString(&p
.location
);
2223 cupsdClearString(&p
.make_model
);
2228 #endif /* HAVE_LIBSLP */
2232 * 'dequote()' - Remote quotes from a string.
2235 static char * /* O - Dequoted string */
2236 dequote(char *d
, /* I - Destination string */
2237 const char *s
, /* I - Source string */
2238 int dlen
) /* I - Destination length */
2240 char *dptr
; /* Pointer into destination */
2245 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2262 # ifdef HAVE_COREFOUNDATION
2264 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2268 dnssdAddAlias(const void *key
, /* I - Key */
2269 const void *value
, /* I - Value (domain) */
2270 void *context
) /* I - Unused */
2272 char valueStr
[1024], /* Domain string */
2273 hostname
[1024]; /* Complete hostname */
2278 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2279 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2280 kCFStringEncodingUTF8
))
2282 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDName
, valueStr
);
2284 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2286 cupsdAddAlias(DNSSDAlias
, hostname
);
2287 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2291 cupsdLogMessage(CUPSD_LOG_ERROR
,
2292 "Bad Back to My Mac domain in dynamic store!");
2294 # endif /* HAVE_COREFOUNDATION */
2298 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2301 static char * /* O - TXT record */
2302 dnssdBuildTxtRecord(
2303 int *txt_len
, /* O - TXT record length */
2304 cupsd_printer_t
*p
, /* I - Printer information */
2305 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2307 int i
, j
; /* Looping vars */
2308 char type_str
[32], /* Type to string buffer */
2309 state_str
[32], /* State to string buffer */
2310 rp_str
[1024], /* Queue name string buffer */
2311 air_str
[1024], /* auth-info-required string buffer */
2312 *keyvalue
[32][2]; /* Table of key/value pairs */
2313 ipp_attribute_t
*air_attr
; /* auth-info-required attribute */
2317 * Load up the key value pairs...
2322 keyvalue
[i
][0] = "txtvers";
2323 keyvalue
[i
++][1] = "1";
2325 keyvalue
[i
][0] = "qtotal";
2326 keyvalue
[i
++][1] = "1";
2328 keyvalue
[i
][0] = "rp";
2329 keyvalue
[i
++][1] = rp_str
;
2331 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2333 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2334 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2336 keyvalue
[i
][0] = "ty";
2337 keyvalue
[i
++][1] = p
->make_model
;
2339 if (p
->location
&& *p
->location
!= '\0')
2341 keyvalue
[i
][0] = "note";
2342 keyvalue
[i
++][1] = p
->location
;
2345 keyvalue
[i
][0] = "priority";
2346 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2348 keyvalue
[i
][0] = "product";
2349 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2351 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2352 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2354 keyvalue
[i
][0] = "printer-state";
2355 keyvalue
[i
++][1] = state_str
;
2357 keyvalue
[i
][0] = "printer-type";
2358 keyvalue
[i
++][1] = type_str
;
2360 keyvalue
[i
][0] = "Transparent";
2361 keyvalue
[i
++][1] = "T";
2363 keyvalue
[i
][0] = "Binary";
2364 keyvalue
[i
++][1] = "T";
2366 if ((p
->type
& CUPS_PRINTER_FAX
))
2368 keyvalue
[i
][0] = "Fax";
2369 keyvalue
[i
++][1] = "T";
2372 if ((p
->type
& CUPS_PRINTER_COLOR
))
2374 keyvalue
[i
][0] = "Color";
2375 keyvalue
[i
++][1] = "T";
2378 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2380 keyvalue
[i
][0] = "Duplex";
2381 keyvalue
[i
++][1] = "T";
2384 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2386 keyvalue
[i
][0] = "Staple";
2387 keyvalue
[i
++][1] = "T";
2390 if ((p
->type
& CUPS_PRINTER_COPIES
))
2392 keyvalue
[i
][0] = "Copies";
2393 keyvalue
[i
++][1] = "T";
2396 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2398 keyvalue
[i
][0] = "Collate";
2399 keyvalue
[i
++][1] = "T";
2402 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2404 keyvalue
[i
][0] = "Punch";
2405 keyvalue
[i
++][1] = "T";
2408 if ((p
->type
& CUPS_PRINTER_BIND
))
2410 keyvalue
[i
][0] = "Bind";
2411 keyvalue
[i
++][1] = "T";
2414 if ((p
->type
& CUPS_PRINTER_SORT
))
2416 keyvalue
[i
][0] = "Sort";
2417 keyvalue
[i
++][1] = "T";
2420 keyvalue
[i
][0] = "pdl";
2421 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2423 if ((air_attr
= ippFindAttribute(p
->attrs
, "auth-info-required",
2424 IPP_TAG_KEYWORD
)) != NULL
&&
2425 strcmp(air_attr
->values
[0].string
.text
, "none"))
2427 char *air
= air_str
; /* Pointer into string */
2430 for (j
= 0; j
< air_attr
->num_values
; j
++)
2432 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2438 strlcpy(air
, air_attr
->values
[j
].string
.text
,
2439 sizeof(air_str
) - (air
- air_str
));
2443 keyvalue
[i
][0] = "air";
2444 keyvalue
[i
++][1] = air_str
;
2448 * Then pack them into a proper txt record...
2451 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2456 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2459 static int /* O - Result of comparison */
2460 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2461 cupsd_printer_t
*b
)/* I - Second printer */
2463 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2468 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2473 dnssdDeregisterPrinter(
2474 cupsd_printer_t
*p
) /* I - Printer */
2476 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2479 * Closing the socket deregisters the service
2484 DNSServiceRefDeallocate(p
->ipp_ref
);
2491 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2500 DNSServiceRefDeallocate(p
->printer_ref
);
2501 p
->printer_ref
= NULL
;
2507 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2510 free(p
->printer_txt
);
2511 p
->printer_txt
= NULL
;
2515 * Remove the printer from the array of DNS-SD printers, then clear the
2516 * registered name...
2519 cupsArrayRemove(DNSSDPrinters
, p
);
2520 cupsdClearString(&p
->reg_name
);
2525 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2526 * TXT record format.
2529 static char * /* O - TXT record */
2530 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2531 char *keyvalue
[][2], /* I - Table of key value pairs */
2532 int count
) /* I - Items in table */
2534 int i
; /* Looping var */
2535 int length
; /* Length of TXT record */
2536 int length2
; /* Length of value */
2537 char *txtRecord
; /* TXT record buffer */
2538 char *cursor
; /* Looping pointer */
2542 * Calculate the buffer size
2545 for (length
= i
= 0; i
< count
; i
++)
2546 length
+= 1 + strlen(keyvalue
[i
][0]) +
2547 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2550 * Allocate and fill it
2553 txtRecord
= malloc(length
);
2558 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2561 * Drop in the p-string style length byte followed by the data
2564 length
= strlen(keyvalue
[i
][0]);
2565 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2567 *cursor
++ = (unsigned char)(length
+ length2
);
2569 memcpy(cursor
, keyvalue
[i
][0], length
);
2576 memcpy(cursor
, keyvalue
[i
][1], length2
);
2587 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2591 dnssdRegisterCallback(
2592 DNSServiceRef sdRef
, /* I - DNS Service reference */
2593 DNSServiceFlags flags
, /* I - Reserved for future use */
2594 DNSServiceErrorType errorCode
, /* I - Error code */
2595 const char *name
, /* I - Service name */
2596 const char *regtype
, /* I - Service type */
2597 const char *domain
, /* I - Domain. ".local" for now */
2598 void *context
) /* I - User-defined context */
2600 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2601 /* Current printer */
2604 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2605 name
, regtype
, p
? p
->name
: "Web Interface",
2606 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2610 cupsdLogMessage(CUPSD_LOG_ERROR
,
2611 "DNSServiceRegister failed with error %d", (int)errorCode
);
2614 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2616 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2619 cupsArrayRemove(DNSSDPrinters
, p
);
2620 cupsdSetString(&p
->reg_name
, name
);
2621 cupsArrayAdd(DNSSDPrinters
, p
);
2623 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2629 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2630 * or update the broadcast contents.
2634 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2636 DNSServiceErrorType se
; /* dnssd errors */
2637 char *ipp_txt
, /* IPP TXT record buffer */
2638 *printer_txt
, /* LPD TXT record buffer */
2639 name
[1024], /* Service name */
2640 *nameptr
; /* Pointer into name */
2641 int ipp_len
, /* IPP TXT record length */
2642 printer_len
; /* LPD TXT record length */
2643 const char *regtype
; /* Registration type */
2646 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2647 !p
->ipp_ref
? "new" : "update");
2650 * If per-printer sharing was just disabled make sure we're not
2651 * registered before returning.
2656 dnssdDeregisterPrinter(p
);
2661 * The registered name takes the form of "<printer-info> @ <computer name>"...
2664 if (p
->info
&& strlen(p
->info
) > 0)
2667 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2669 strlcpy(name
, p
->info
, sizeof(name
));
2672 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2674 strlcpy(name
, p
->name
, sizeof(name
));
2677 * If an existing printer was renamed, unregister it and start over...
2680 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2681 dnssdDeregisterPrinter(p
);
2685 cupsdSetString(&p
->reg_name
, name
);
2686 cupsArrayAdd(DNSSDPrinters
, p
);
2690 * Register IPP and (optionally) LPD...
2693 ipp_len
= 0; /* anti-compiler-warning-code */
2694 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2697 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2700 * Update the existing registration...
2703 /* A TTL of 0 means use record's original value (Radar 3176248) */
2704 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2705 0)) == kDNSServiceErr_NoError
)
2710 p
->ipp_txt
= ipp_txt
;
2711 p
->ipp_len
= ipp_len
;
2717 * Failed to update record, lets close this reference and move on...
2720 cupsdLogMessage(CUPSD_LOG_ERROR
,
2721 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2724 DNSServiceRefDeallocate(p
->ipp_ref
);
2732 * Initial registration. Use the _fax subtype for fax queues...
2735 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2738 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2739 "Registering DNS-SD printer %s with name \"%s\" and "
2740 "type \"%s\"", p
->name
, name
, regtype
);
2743 * Register the queue, dropping characters as needed until we succeed...
2746 nameptr
= name
+ strlen(name
);
2750 p
->ipp_ref
= DNSSDRef
;
2751 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2752 0, name
, regtype
, NULL
, NULL
,
2753 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2754 dnssdRegisterCallback
,
2755 p
)) == kDNSServiceErr_BadParam
)
2758 * Name is too long, drop trailing characters, taking into account
2764 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2771 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2773 if (se
== kDNSServiceErr_NoError
)
2775 p
->ipp_txt
= ipp_txt
;
2776 p
->ipp_len
= ipp_len
;
2780 cupsdLogMessage(CUPSD_LOG_WARN
,
2781 "DNS-SD IPP registration of \"%s\" failed: %d",
2788 if (BrowseLocalProtocols
& BROWSE_LPD
)
2790 printer_len
= 0; /* anti-compiler-warning-code */
2791 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2793 if (p
->printer_ref
&&
2794 (printer_len
!= p
->printer_len
||
2795 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2798 * Update the existing registration...
2801 /* A TTL of 0 means use record's original value (Radar 3176248) */
2802 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2804 0)) == kDNSServiceErr_NoError
)
2807 free(p
->printer_txt
);
2809 p
->printer_txt
= printer_txt
;
2810 p
->printer_len
= printer_len
;
2816 * Failed to update record, lets close this reference and move on...
2819 cupsdLogMessage(CUPSD_LOG_ERROR
,
2820 "Unable to update LPD DNS-SD record for %s - %d",
2823 DNSServiceRefDeallocate(p
->printer_ref
);
2824 p
->printer_ref
= NULL
;
2828 if (!p
->printer_ref
)
2831 * Initial registration...
2834 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2835 "Registering DNS-SD printer %s with name \"%s\" and "
2836 "type \"_printer._tcp\"", p
->name
, name
);
2838 p
->printer_ref
= DNSSDRef
;
2839 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2840 kDNSServiceFlagsShareConnection
,
2841 0, name
, "_printer._tcp", NULL
, NULL
,
2842 htons(515), printer_len
, printer_txt
,
2843 dnssdRegisterCallback
,
2844 p
)) == kDNSServiceErr_NoError
)
2846 p
->printer_txt
= printer_txt
;
2847 p
->printer_len
= printer_len
;
2851 cupsdLogMessage(CUPSD_LOG_WARN
,
2852 "DNS-SD LPD registration of \"%s\" failed: %d",
2863 * 'dnssdStop()' - Stop all DNS-SD registrations.
2869 cupsd_printer_t
*p
; /* Current printer */
2873 * De-register the individual printers
2876 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2878 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2879 dnssdDeregisterPrinter(p
);
2882 * Shutdown the rest of the service refs...
2887 DNSServiceRefDeallocate(WebIFRef
);
2893 DNSServiceRefDeallocate(RemoteRef
);
2897 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2899 DNSServiceRefDeallocate(DNSSDRef
);
2902 cupsArrayDelete(DNSSDPrinters
);
2903 DNSSDPrinters
= NULL
;
2910 * 'dnssdUpdate()' - Handle DNS-SD queries.
2916 DNSServiceErrorType sdErr
; /* Service discovery error */
2919 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2921 cupsdLogMessage(CUPSD_LOG_ERROR
,
2922 "DNS Service Discovery registration error %d!",
2927 #endif /* HAVE_DNSSD */
2932 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2935 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2936 get_hostconfig(const char *name
) /* I - Name of service */
2938 cups_file_t
*fp
; /* Hostconfig file */
2939 char line
[1024], /* Line from file */
2940 *ptr
; /* Pointer to value */
2941 int state
= 1; /* State of service */
2945 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2946 * the service is enabled/auto.
2949 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2952 * Read lines from the file until we find the service...
2955 while (cupsFileGets(fp
, line
, sizeof(line
)))
2957 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2962 if (!strcasecmp(line
, name
))
2965 * Found the service, see if it is set to "-NO-"...
2968 if (!strncasecmp(ptr
, "-NO-", 4))
2979 #endif /* __APPLE__ */
2983 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2986 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2987 is_local_queue(const char *uri
, /* I - Printer URI */
2988 char *host
, /* O - Host string */
2989 int hostlen
, /* I - Length of host buffer */
2990 char *resource
, /* O - Resource string */
2991 int resourcelen
) /* I - Length of resource buffer */
2993 char scheme
[32], /* Scheme portion of URI */
2994 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2995 int port
; /* Port portion of URI */
2996 cupsd_netif_t
*iface
; /* Network interface */
3000 * Pull the URI apart to see if this is a local or remote printer...
3003 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3004 username
, sizeof(username
), host
, hostlen
, &port
,
3005 resource
, resourcelen
) < HTTP_URI_OK
)
3008 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3011 * Check for local server addresses...
3014 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3019 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3021 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3022 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3026 * If we get here, the printer is remote...
3034 * 'process_browse_data()' - Process new browse data.
3038 process_browse_data(
3039 const char *uri
, /* I - URI of printer/class */
3040 const char *host
, /* I - Hostname */
3041 const char *resource
, /* I - Resource path */
3042 cups_ptype_t type
, /* I - Printer type */
3043 ipp_pstate_t state
, /* I - Printer state */
3044 const char *location
, /* I - Printer location */
3045 const char *info
, /* I - Printer information */
3046 const char *make_model
, /* I - Printer make and model */
3047 int num_attrs
, /* I - Number of attributes */
3048 cups_option_t
*attrs
) /* I - Attributes */
3050 int i
; /* Looping var */
3051 int update
; /* Update printer attributes? */
3052 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3053 name
[IPP_MAX_NAME
], /* Name of printer */
3054 newname
[IPP_MAX_NAME
], /* New name of printer */
3055 *hptr
, /* Pointer into hostname */
3056 *sptr
; /* Pointer into ServerName */
3057 const char *shortname
; /* Short queue name (queue) */
3058 char local_make_model
[IPP_MAX_NAME
];
3059 /* Local make and model */
3060 cupsd_printer_t
*p
; /* Printer information */
3061 const char *ipp_options
, /* ipp-options value */
3062 *lease_duration
; /* lease-duration value */
3063 int is_class
; /* Is this queue a class? */
3066 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3067 "process_browse_data(uri=\"%s\", host=\"%s\", "
3068 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3069 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3070 uri
, host
, resource
, type
, state
,
3071 location
? location
: "(nil)", info
? info
: "(nil)",
3072 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3075 * Determine if the URI contains any illegal characters in it...
3078 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3079 (strncmp(resource
, "/printers/", 10) &&
3080 strncmp(resource
, "/classes/", 9)))
3082 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3086 if (strchr(resource
, '?') ||
3087 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3088 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3090 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3096 * OK, this isn't a local printer; add any remote options...
3099 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3101 if (BrowseRemoteOptions
)
3103 if (BrowseRemoteOptions
[0] == '?')
3106 * Override server-supplied options...
3109 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3111 else if (ipp_options
)
3114 * Combine the server and local options...
3117 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3118 BrowseRemoteOptions
);
3123 * Just use the local options...
3126 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3131 else if (ipp_options
)
3134 * Just use the server-supplied options...
3137 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3142 * See if we already have it listed in the Printers list, and add it if not...
3145 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3146 type
&= ~CUPS_PRINTER_IMPLICIT
;
3148 hptr
= strchr(host
, '.');
3149 sptr
= strchr(ServerName
, '.');
3150 is_class
= type
& CUPS_PRINTER_CLASS
;
3152 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3155 * Strip the common domain name components...
3158 while (hptr
!= NULL
)
3160 if (!strcasecmp(hptr
, sptr
))
3166 hptr
= strchr(hptr
+ 1, '.');
3173 * Remote destination is a class...
3176 if (!strncmp(resource
, "/classes/", 9))
3177 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3181 shortname
= resource
+ 9;
3186 * Remote destination is a printer...
3189 if (!strncmp(resource
, "/printers/", 10))
3190 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3194 shortname
= resource
+ 10;
3198 *hptr
= '.'; /* Resource FQDN */
3200 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3203 * Long name doesn't exist, try short name...
3206 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3209 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3212 * Short name doesn't exist, use it for this shared queue.
3215 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3217 strlcpy(name
, shortname
, sizeof(name
));
3222 * Short name exists...
3225 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3226 "process_browse_data: %s found, type=%x, hostname=%s...",
3227 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3229 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3230 p
= NULL
; /* Don't replace implicit classes */
3231 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3234 * Short name exists but is for a different host. If this is a remote
3235 * queue, rename it and use the long name...
3238 if (p
->type
& CUPS_PRINTER_REMOTE
)
3240 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3241 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3242 is_class
? "class" : "printer", p
->name
, p
->name
,
3244 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3245 "%s \'%s\' deleted by directory services.",
3246 is_class
? "Class" : "Printer", p
->name
);
3248 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3249 cupsdRenamePrinter(p
, newname
);
3251 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3252 "%s \'%s\' added by directory services.",
3253 is_class
? "Class" : "Printer", p
->name
);
3257 * Force creation with long name...
3265 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3266 "process_browse_data: %s found, type=%x, hostname=%s...",
3267 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3272 * Queue doesn't exist; add it...
3276 p
= cupsdAddClass(name
);
3278 p
= cupsdAddPrinter(name
);
3283 cupsdClearString(&(p
->hostname
));
3285 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3286 is_class
? "class" : "printer", name
);
3288 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3289 "%s \'%s\' added by directory services.",
3290 is_class
? "Class" : "Printer", name
);
3293 * Force the URI to point to the real server...
3296 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3299 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3305 * Hostname not set, so this must be a cached remote printer
3306 * that was created for a pending print job...
3309 cupsdSetString(&p
->hostname
, host
);
3310 cupsdSetString(&p
->uri
, uri
);
3311 cupsdSetString(&p
->device_uri
, uri
);
3314 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3318 * Update the state...
3322 p
->browse_time
= time(NULL
);
3324 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3328 * Grab the lease-duration for the browse data; anything less then 1
3329 * second or more than 1 week gets the default BrowseTimeout...
3332 i
= atoi(lease_duration
);
3333 if (i
< 1 || i
> 604800)
3336 p
->browse_expire
= p
->browse_time
+ i
;
3339 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3341 if (type
& CUPS_PRINTER_REJECTING
)
3343 type
&= ~CUPS_PRINTER_REJECTING
;
3351 else if (!p
->accepting
)
3357 if (p
->type
!= type
)
3363 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3365 cupsdSetString(&p
->location
, location
);
3369 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3371 cupsdSetString(&p
->info
, info
);
3374 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3377 if (!make_model
|| !make_model
[0])
3380 snprintf(local_make_model
, sizeof(local_make_model
),
3381 "Remote Class on %s", host
);
3383 snprintf(local_make_model
, sizeof(local_make_model
),
3384 "Remote Printer on %s", host
);
3387 snprintf(local_make_model
, sizeof(local_make_model
),
3388 "%s on %s", make_model
, host
);
3390 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3392 cupsdSetString(&p
->make_model
, local_make_model
);
3398 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3401 * See if we need to update the attributes...
3404 if (p
->num_options
!= num_attrs
)
3408 for (i
= 0; i
< num_attrs
; i
++)
3409 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3410 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3411 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3420 * Free the old options...
3423 cupsFreeOptions(p
->num_options
, p
->options
);
3426 p
->num_options
= num_attrs
;
3429 if (type
& CUPS_PRINTER_DELETE
)
3431 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3432 "%s \'%s\' deleted by directory services.",
3433 is_class
? "Class" : "Printer", p
->name
);
3435 cupsdExpireSubscriptions(p
, NULL
);
3437 cupsdDeletePrinter(p
, 1);
3438 cupsdUpdateImplicitClasses();
3439 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3443 cupsdSetPrinterAttrs(p
);
3444 cupsdUpdateImplicitClasses();
3448 * See if we have a default printer... If not, make the first network
3449 * default printer the default.
3452 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3455 * Find the first network default printer and use it...
3458 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3460 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3461 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3464 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3470 * Do auto-classing if needed...
3473 process_implicit_classes();
3478 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3482 process_implicit_classes(void)
3484 int i
; /* Looping var */
3485 int update
; /* Update printer attributes? */
3486 char name
[IPP_MAX_NAME
], /* Name of printer */
3487 *hptr
; /* Pointer into hostname */
3488 cupsd_printer_t
*p
, /* Printer information */
3489 *pclass
, /* Printer class */
3490 *first
; /* First printer in class */
3491 int offset
, /* Offset of name */
3492 len
; /* Length of name */
3495 if (!ImplicitClasses
|| !Printers
)
3499 * Loop through all available printers and create classes as needed...
3502 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3503 update
= 0, pclass
= NULL
, first
= NULL
;
3505 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3508 * Skip implicit classes...
3511 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3518 * If len == 0, get the length of this printer name up to the "@"
3522 cupsArraySave(Printers
);
3525 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3526 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3529 * We have more than one printer with the same name; see if
3530 * we have a class, and if this printer is a member...
3533 if (pclass
&& strcasecmp(pclass
->name
, name
))
3536 cupsdSetPrinterAttrs(pclass
);
3542 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3545 * Need to add the class...
3548 pclass
= cupsdAddPrinter(name
);
3549 cupsArrayAdd(ImplicitPrinters
, pclass
);
3551 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3552 pclass
->accepting
= 1;
3553 pclass
->state
= IPP_PRINTER_IDLE
;
3555 cupsdSetString(&pclass
->location
, p
->location
);
3556 cupsdSetString(&pclass
->info
, p
->info
);
3558 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3559 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3563 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3565 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3567 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3568 "Implicit class \'%s\' added by directory services.",
3574 for (i
= 0; i
< pclass
->num_printers
; i
++)
3575 if (pclass
->printers
[i
] == first
)
3578 if (i
>= pclass
->num_printers
)
3580 first
->in_implicit_class
= 1;
3581 cupsdAddPrinterToClass(pclass
, first
);
3587 for (i
= 0; i
< pclass
->num_printers
; i
++)
3588 if (pclass
->printers
[i
] == p
)
3591 if (i
>= pclass
->num_printers
)
3593 p
->in_implicit_class
= 1;
3594 cupsdAddPrinterToClass(pclass
, p
);
3601 * First time around; just get name length and mark it as first
3605 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3606 len
= hptr
- p
->name
;
3608 len
= strlen(p
->name
);
3610 if (len
>= sizeof(name
))
3613 * If the printer name length somehow is greater than we normally allow,
3614 * skip this printer...
3618 cupsArrayRestore(Printers
);
3622 strncpy(name
, p
->name
, len
);
3626 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3627 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3630 * Can't use same name as a local printer; add "Any" to the
3631 * front of the name, unless we have explicitly disabled
3632 * the "ImplicitAnyClasses"...
3635 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3638 * Add "Any" to the class name...
3641 strcpy(name
, "Any");
3642 strncpy(name
+ 3, p
->name
, len
);
3643 name
[len
+ 3] = '\0';
3649 * Don't create an implicit class if we have a local printer
3650 * with the same name...
3654 cupsArrayRestore(Printers
);
3662 cupsArrayRestore(Printers
);
3666 * Update the last printer class as needed...
3669 if (pclass
&& update
)
3670 cupsdSetPrinterAttrs(pclass
);
3675 * 'send_cups_browse()' - Send new browsing information using the CUPS
3680 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3682 int i
; /* Looping var */
3683 cups_ptype_t type
; /* Printer type */
3684 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3685 int bytes
; /* Length of packet */
3686 char packet
[1453], /* Browse data packet */
3687 uri
[1024], /* Printer URI */
3688 location
[1024], /* printer-location */
3689 info
[1024], /* printer-info */
3691 /* printer-make-and-model */
3692 cupsd_netif_t
*iface
; /* Network interface */
3696 * Figure out the printer type value...
3699 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3702 type
|= CUPS_PRINTER_REJECTING
;
3704 if (p
== DefaultPrinter
)
3705 type
|= CUPS_PRINTER_DEFAULT
;
3708 * Remove quotes from printer-info, printer-location, and
3709 * printer-make-and-model attributes...
3712 dequote(location
, p
->location
, sizeof(location
));
3713 dequote(info
, p
->info
, sizeof(info
));
3716 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3717 else if (p
->type
& CUPS_PRINTER_CLASS
)
3719 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3720 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3722 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3725 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3727 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3730 * Send a packet to each browse address...
3733 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3737 * Send the browse packet to one or more interfaces...
3740 if (!strcmp(b
->iface
, "*"))
3743 * Send to all local interfaces...
3748 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3750 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3753 * Only send to local, IPv4 interfaces...
3756 if (!iface
->is_local
|| !iface
->port
||
3757 iface
->address
.addr
.sa_family
!= AF_INET
)
3760 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3761 iface
->hostname
, iface
->port
,
3762 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3765 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3766 type
, p
->state
, uri
, location
, info
, make_model
,
3767 p
->browse_attrs
? p
->browse_attrs
: "");
3769 bytes
= strlen(packet
);
3771 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3772 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3773 iface
->name
, packet
);
3775 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3777 sendto(BrowseSocket
, packet
, bytes
, 0,
3778 (struct sockaddr
*)&(iface
->broadcast
),
3779 httpAddrLength(&(iface
->broadcast
)));
3782 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3785 * Send to the named interface using the IPv4 address...
3789 if (strcmp(b
->iface
, iface
->name
))
3794 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3797 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3801 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3802 iface
->hostname
, iface
->port
,
3803 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3806 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3807 type
, p
->state
, uri
, location
, info
, make_model
,
3808 p
->browse_attrs
? p
->browse_attrs
: "");
3810 bytes
= strlen(packet
);
3812 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3813 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3814 iface
->name
, packet
);
3816 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3818 sendto(BrowseSocket
, packet
, bytes
, 0,
3819 (struct sockaddr
*)&(iface
->broadcast
),
3820 httpAddrLength(&(iface
->broadcast
)));
3827 * Send the browse packet to the indicated address using
3828 * the default server name...
3831 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3832 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3833 p
->browse_attrs
? p
->browse_attrs
: "");
3835 bytes
= strlen(packet
);
3836 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3837 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3839 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3840 (struct sockaddr
*)&(b
->to
),
3841 httpAddrLength(&(b
->to
))) <= 0)
3844 * Unable to send browse packet, so remove this address from the
3848 cupsdLogMessage(CUPSD_LOG_ERROR
,
3849 "cupsdSendBrowseList: sendto failed for browser "
3851 (int)(b
- Browsers
+ 1), strerror(errno
));
3854 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3865 * 'ldap_search_rec()' - LDAP Search with reconnect
3869 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3870 char *base
, /* I - Base dn */
3871 int scope
, /* I - LDAP search scope */
3872 char *filter
, /* I - Filter string */
3873 char *attrs
[], /* I - Requested attributes */
3874 int attrsonly
, /* I - Return only attributes? */
3875 LDAPMessage
**res
) /* I - LDAP handler */
3877 int rc
; /* Return code */
3880 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3881 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3882 NULL
, LDAP_NO_LIMIT
, res
);
3884 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3885 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3888 * If we have a connection problem try again...
3891 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3893 cupsdLogMessage(CUPSD_LOG_ERROR
,
3894 "LDAP search failed with status %d: %s",
3895 rc
, ldap_err2string(rc
));
3896 cupsdLogMessage(CUPSD_LOG_INFO
,
3897 "We try the LDAP search once again after reconnecting to "
3902 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3903 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3904 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3906 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3907 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3910 if (rc
== LDAP_NO_SUCH_OBJECT
)
3911 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3912 "ldap_search_rec: LDAP entry/object not found");
3913 else if (rc
!= LDAP_SUCCESS
)
3914 cupsdLogMessage(CUPSD_LOG_ERROR
,
3915 "ldap_search_rec: LDAP search failed with status %d: %s",
3916 rc
, ldap_err2string(rc
));
3918 if (rc
!= LDAP_SUCCESS
)
3926 * 'ldap_freeres()' - Free LDAPMessage
3930 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3932 int rc
; /* Return value */
3935 rc
= ldap_msgfree(entry
);
3937 cupsdLogMessage(CUPSD_LOG_WARN
,
3938 "Can't free LDAPMessage!");
3940 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3941 "Freeing LDAPMessage was unnecessary");
3946 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3950 ldap_getval_firststring(
3951 LDAP
*ld
, /* I - LDAP handler */
3952 LDAPMessage
*entry
, /* I - LDAP message or search result */
3953 char *attr
, /* I - the wanted attribute */
3954 char *retval
, /* O - String to return */
3955 unsigned long maxsize
) /* I - Max string size */
3957 char *dn
; /* LDAP DN */
3958 int rc
= 0; /* Return code */
3959 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3960 struct berval
**bval
; /* LDAP value array */
3961 unsigned long size
; /* String size */
3965 * Get value from LDAPMessage...
3968 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3971 dn
= ldap_get_dn(ld
, entry
);
3972 cupsdLogMessage(CUPSD_LOG_WARN
,
3973 "Failed to get LDAP value %s for %s!",
3981 * Check size and copy value into our string...
3985 if (size
< bval
[0]->bv_len
)
3988 dn
= ldap_get_dn(ld
, entry
);
3989 cupsdLogMessage(CUPSD_LOG_WARN
,
3990 "Attribute %s is too big! (dn: %s)",
3995 size
= bval
[0]->bv_len
;
3997 strlcpy(retval
, bval
[0]->bv_val
, size
);
3998 ldap_value_free_len(bval
);
4001 char **value
; /* LDAP value */
4004 * Get value from LDAPMessage...
4007 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4010 dn
= ldap_get_dn(ld
, entry
);
4011 cupsdLogMessage(CUPSD_LOG_WARN
,
4012 "Failed to get LDAP value %s for %s!",
4018 strlcpy(retval
, *value
, maxsize
);
4019 ldap_value_free(value
);
4021 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4028 * 'send_ldap_ou()' - Send LDAP ou registrations.
4032 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4033 char *basedn
, /* I - Our base dn */
4034 char *descstring
) /* I - Description for ou */
4036 int i
; /* Looping var... */
4037 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4038 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4039 LDAPMessage
*res
, /* Search result token */
4040 *e
; /* Current entry from search */
4041 int rc
; /* LDAP status */
4042 char dn
[1024], /* DN of the organizational unit we are adding */
4043 *desc
[2], /* Change records */
4045 char old_desc
[1024]; /* Old description */
4046 static const char * const objectClass_values
[] =
4047 { /* The 2 objectClass's we use in */
4048 "top", /* our LDAP entries */
4049 "organizationalUnit",
4052 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4059 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4062 * Reconnect if LDAP Handle is invalid...
4065 if (! BrowseLDAPHandle
)
4067 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4068 "send_ldap_ou: LDAP Handle is invalid. Try "
4075 * Prepare ldap search...
4078 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4079 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4083 desc
[0] = descstring
;
4086 mods
[0].mod_type
= "ou";
4087 mods
[0].mod_values
= ou_value
;
4088 mods
[1].mod_type
= "description";
4089 mods
[1].mod_values
= desc
;
4090 mods
[2].mod_type
= "objectClass";
4091 mods
[2].mod_values
= (char **)objectClass_values
;
4093 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4094 (char **)ou_attrs
, 0, &res
);
4097 * If ldap search was not successfull then exit function...
4100 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4104 * Check if we need to insert or update the LDAP entry...
4107 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4108 rc
!= LDAP_NO_SUCH_OBJECT
)
4111 * Printserver has already been registered, check if
4112 * modification is required...
4115 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4118 * Get the required values from this entry...
4121 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4122 sizeof(old_desc
)) == -1)
4126 * Check if modification is required...
4129 if ( strcmp(desc
[0], old_desc
) == 0 )
4132 * LDAP entry for the printer exists.
4133 * Printer has already been registered,
4134 * no modifications required...
4136 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4137 "send_ldap_ou: No updates required for %s", ou
);
4142 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4143 "send_ldap_ou: Replace entry for %s", ou
);
4145 for (i
= 0; i
< 3; i
++)
4147 pmods
[i
] = mods
+ i
;
4148 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4152 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4153 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4154 NULL
)) != LDAP_SUCCESS
)
4156 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4157 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4159 cupsdLogMessage(CUPSD_LOG_ERROR
,
4160 "LDAP modify for %s failed with status %d: %s",
4161 ou
, rc
, ldap_err2string(rc
));
4162 if ( LDAP_SERVER_DOWN
== rc
)
4170 * Printserver has never been registered,
4171 * add registration...
4174 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4175 "send_ldap_ou: Add entry for %s", ou
);
4177 for (i
= 0; i
< 3; i
++)
4179 pmods
[i
] = mods
+ i
;
4180 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4184 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4185 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4186 NULL
)) != LDAP_SUCCESS
)
4188 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4189 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4191 cupsdLogMessage(CUPSD_LOG_ERROR
,
4192 "LDAP add for %s failed with status %d: %s",
4193 ou
, rc
, ldap_err2string(rc
));
4194 if ( LDAP_SERVER_DOWN
== rc
)
4204 * 'send_ldap_browse()' - Send LDAP printer registrations.
4208 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4210 int i
; /* Looping var... */
4211 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4212 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4213 LDAPMessage
*res
, /* Search result token */
4214 *e
; /* Current entry from search */
4215 char *cn_value
[2], /* Change records */
4221 typestring
[255], /* String to hold printer-type */
4222 dn
[1024]; /* DN of the printer we are adding */
4223 int rc
; /* LDAP status */
4224 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4225 old_location
[1024], /* Printer location */
4226 old_info
[1024], /* Printer information */
4227 old_make_model
[1024], /* Printer make and model */
4228 old_type_string
[30]; /* Temporary type number */
4229 int old_type
; /* Printer type */
4230 static const char * const objectClass_values
[] =
4231 { /* The 3 objectClass's we use in */
4232 "top", /* our LDAP entries */
4239 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4242 * Exit function if LDAP updates has been disabled...
4245 if (!BrowseLDAPUpdate
)
4247 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4248 "send_ldap_browse: Updates temporary disabled; "
4254 * Reconnect if LDAP Handle is invalid...
4257 if (!BrowseLDAPHandle
)
4259 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4260 "send_ldap_browse: LDAP Handle is invalid. Try "
4267 * Everything in ldap is ** so we fudge around it...
4270 sprintf(typestring
, "%u", p
->type
);
4272 cn_value
[0] = p
->name
;
4274 info
[0] = p
->info
? p
->info
: "Unknown";
4276 location
[0] = p
->location
? p
->location
: "Unknown";
4278 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4279 make_model
[1] = NULL
;
4280 type
[0] = typestring
;
4286 * Get ldap entry for printer ...
4289 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4291 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4293 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4294 (char **)ldap_attrs
, 0, &res
);
4297 * If ldap search was not successfull then exit function
4298 * and temporary disable LDAP updates...
4301 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4303 if (BrowseLDAPUpdate
&&
4304 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4306 BrowseLDAPUpdate
= FALSE
;
4307 cupsdLogMessage(CUPSD_LOG_INFO
,
4308 "LDAP update temporary disabled");
4315 * Fill modification array...
4318 mods
[0].mod_type
= "cn";
4319 mods
[0].mod_values
= cn_value
;
4320 mods
[1].mod_type
= "printerDescription";
4321 mods
[1].mod_values
= info
;
4322 mods
[2].mod_type
= "printerURI";
4323 mods
[2].mod_values
= uri
;
4324 mods
[3].mod_type
= "printerLocation";
4325 mods
[3].mod_values
= location
;
4326 mods
[4].mod_type
= "printerMakeAndModel";
4327 mods
[4].mod_values
= make_model
;
4328 mods
[5].mod_type
= "printerType";
4329 mods
[5].mod_values
= type
;
4330 mods
[6].mod_type
= "objectClass";
4331 mods
[6].mod_values
= (char **)objectClass_values
;
4334 * Check if we need to insert or update the LDAP entry...
4337 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4338 rc
!= LDAP_NO_SUCH_OBJECT
)
4341 * Printer has already been registered, check if
4342 * modification is required...
4345 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4348 * Get the required values from this entry...
4351 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4352 old_info
, sizeof(old_info
)) == -1)
4355 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4356 old_location
, sizeof(old_location
)) == -1)
4359 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4360 old_make_model
, sizeof(old_make_model
)) == -1)
4363 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4364 old_type_string
, sizeof(old_type_string
)) == -1)
4367 old_type
= atoi(old_type_string
);
4369 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4370 sizeof(old_uri
)) == -1)
4374 * Check if modification is required...
4377 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4378 !strcmp(location
[0], old_location
) &&
4379 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4382 * LDAP entry for the printer exists. Printer has already been registered,
4383 * no modifications required...
4386 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4387 "send_ldap_browse: No updates required for %s", p
->name
);
4392 * LDAP entry for the printer exists. Printer has already been registered,
4393 * modify the current registration...
4396 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4397 "send_ldap_browse: Replace entry for %s", p
->name
);
4399 for (i
= 0; i
< 7; i
++)
4401 pmods
[i
] = mods
+ i
;
4402 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4406 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4407 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4408 NULL
)) != LDAP_SUCCESS
)
4410 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4411 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4413 cupsdLogMessage(CUPSD_LOG_ERROR
,
4414 "LDAP modify for %s failed with status %d: %s",
4415 p
->name
, rc
, ldap_err2string(rc
));
4416 if (rc
== LDAP_SERVER_DOWN
)
4424 * No LDAP entry exists for the printer. Printer has never been registered,
4425 * add the current registration...
4428 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4430 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4431 "send_ldap_browse: Add entry for %s", p
->name
);
4433 for (i
= 0; i
< 7; i
++)
4435 pmods
[i
] = mods
+ i
;
4436 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4440 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4441 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4442 NULL
)) != LDAP_SUCCESS
)
4444 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4445 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4447 cupsdLogMessage(CUPSD_LOG_ERROR
,
4448 "LDAP add for %s failed with status %d: %s",
4449 p
->name
, rc
, ldap_err2string(rc
));
4450 if (rc
== LDAP_SERVER_DOWN
)
4460 * 'ldap_dereg_printer()' - Delete printer from directory
4464 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4466 char dn
[1024]; /* DN of the printer */
4467 int rc
; /* LDAP status */
4470 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4474 * Reconnect if LDAP Handle is invalid...
4477 if (!BrowseLDAPHandle
)
4484 * Get dn for printer and delete LDAP entry...
4487 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4489 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4491 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4492 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4493 NULL
)) != LDAP_SUCCESS
)
4495 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4496 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4498 cupsdLogMessage(CUPSD_LOG_WARN
,
4499 "LDAP delete for %s failed with status %d: %s",
4500 p
->name
, rc
, ldap_err2string(rc
));
4503 * If we had a connection problem (connection timed out, etc.)
4504 * we should reconnect and try again to delete the entry...
4507 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4509 cupsdLogMessage(CUPSD_LOG_INFO
,
4510 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4513 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4514 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4515 NULL
)) != LDAP_SUCCESS
)
4517 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4518 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4519 cupsdLogMessage(CUPSD_LOG_WARN
,
4520 "LDAP delete for %s failed with status %d: %s",
4521 p
->name
, rc
, ldap_err2string(rc
));
4528 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4529 char *basedn
) /* I - Dase dn */
4531 char dn
[1024]; /* DN of the printer */
4532 int rc
; /* LDAP status */
4535 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4538 * Reconnect if LDAP Handle is invalid...
4541 if (!BrowseLDAPHandle
)
4548 * Get dn for printer and delete LDAP entry...
4551 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4552 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4554 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4555 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4556 NULL
)) != LDAP_SUCCESS
)
4558 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4559 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4561 cupsdLogMessage(CUPSD_LOG_WARN
,
4562 "LDAP delete for %s failed with status %d: %s",
4563 ou
, rc
, ldap_err2string(rc
));
4566 * If we had a connection problem (connection timed out, etc.)
4567 * we should reconnect and try again to delete the entry...
4570 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4572 cupsdLogMessage(CUPSD_LOG_INFO
,
4573 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4575 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4576 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4577 NULL
)) != LDAP_SUCCESS
)
4579 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4580 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4581 cupsdLogMessage(CUPSD_LOG_WARN
,
4582 "LDAP delete for %s failed with status %d: %s",
4583 ou
, rc
, ldap_err2string(rc
));
4588 #endif /* HAVE_LDAP */
4593 * 'send_slp_browse()' - Register the specified printer with SLP.
4597 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4599 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4600 attrs
[8192], /* Printer attributes */
4601 finishings
[1024], /* Finishings to support */
4602 make_model
[IPP_MAX_NAME
* 2],
4603 /* Make and model, quoted */
4604 location
[IPP_MAX_NAME
* 2],
4605 /* Location, quoted */
4606 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4607 *src
, /* Pointer to original string */
4608 *dst
; /* Pointer to destination string */
4609 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4610 SLPError error
; /* SLP error, if any */
4613 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4617 * Make the SLP service URL that conforms to the IANA
4618 * 'printer:' template.
4621 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4623 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4626 * Figure out the finishings string...
4629 if (p
->type
& CUPS_PRINTER_STAPLE
)
4630 strcpy(finishings
, "staple");
4632 finishings
[0] = '\0';
4634 if (p
->type
& CUPS_PRINTER_BIND
)
4637 strlcat(finishings
, ",bind", sizeof(finishings
));
4639 strcpy(finishings
, "bind");
4642 if (p
->type
& CUPS_PRINTER_PUNCH
)
4645 strlcat(finishings
, ",punch", sizeof(finishings
));
4647 strcpy(finishings
, "punch");
4650 if (p
->type
& CUPS_PRINTER_COVER
)
4653 strlcat(finishings
, ",cover", sizeof(finishings
));
4655 strcpy(finishings
, "cover");
4658 if (p
->type
& CUPS_PRINTER_SORT
)
4661 strlcat(finishings
, ",sort", sizeof(finishings
));
4663 strcpy(finishings
, "sort");
4667 strcpy(finishings
, "none");
4670 * Quote any commas in the make and model, location, and info strings...
4673 for (src
= p
->make_model
, dst
= make_model
;
4674 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4676 if (*src
== ',' || *src
== '\\' || *src
== ')')
4685 strcpy(make_model
, "Unknown");
4687 for (src
= p
->location
, dst
= location
;
4688 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4690 if (*src
== ',' || *src
== '\\' || *src
== ')')
4699 strcpy(location
, "Unknown");
4701 for (src
= p
->info
, dst
= info
;
4702 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4704 if (*src
== ',' || *src
== '\\' || *src
== ')')
4713 strcpy(info
, "Unknown");
4716 * Get the authentication value...
4719 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4723 * Make the SLP attribute string list that conforms to
4724 * the IANA 'printer:' template.
4727 snprintf(attrs
, sizeof(attrs
),
4728 "(printer-uri-supported=%s),"
4729 "(uri-authentication-supported=%s>),"
4731 "(uri-security-supported=tls>),"
4733 "(uri-security-supported=none>),"
4734 #endif /* HAVE_SSL */
4735 "(printer-name=%s),"
4736 "(printer-location=%s),"
4737 "(printer-info=%s),"
4738 "(printer-more-info=%s),"
4739 "(printer-make-and-model=%s),"
4740 "(printer-type=%d),"
4741 "(charset-supported=utf-8),"
4742 "(natural-language-configured=%s),"
4743 "(natural-language-supported=de,en,es,fr,it),"
4744 "(color-supported=%s),"
4745 "(finishings-supported=%s),"
4746 "(sides-supported=one-sided%s),"
4747 "(multiple-document-jobs-supported=true)"
4748 "(ipp-versions-supported=1.0,1.1)",
4749 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4750 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4751 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4753 p
->type
& CUPS_PRINTER_DUPLEX
?
4754 ",two-sided-long-edge,two-sided-short-edge" : "");
4756 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4759 * Register the printer with the SLP server...
4762 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4763 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4765 if (error
!= SLP_OK
)
4766 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4772 * 'slp_attr_callback()' - SLP attribute callback
4775 static SLPBoolean
/* O - SLP_TRUE for success */
4777 SLPHandle hslp
, /* I - SLP handle */
4778 const char *attrlist
, /* I - Attribute list */
4779 SLPError errcode
, /* I - Parsing status for this attr */
4780 void *cookie
) /* I - Current printer */
4782 char *tmp
= 0; /* Temporary string */
4783 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4784 /* Current printer */
4787 (void)hslp
; /* anti-compiler-warning-code */
4790 * Bail if there was an error
4793 if (errcode
!= SLP_OK
)
4797 * Parse the attrlist to obtain things needed to build CUPS browse packet
4800 memset(p
, 0, sizeof(cupsd_printer_t
));
4802 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4804 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4806 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4808 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4809 p
->type
= atoi(tmp
);
4811 p
->type
= CUPS_PRINTER_REMOTE
;
4813 cupsdClearString(&tmp
);
4820 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4824 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4826 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4829 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4831 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4834 * Make the SLP service URL that conforms to the IANA
4835 * 'printer:' template.
4838 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4841 * Deregister the printer...
4844 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4850 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4853 static int /* O - 0 on success */
4854 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4855 const char *tag
, /* I - Name of attribute */
4856 char **valbuf
) /* O - Value */
4858 char *ptr1
, /* Pointer into string */
4862 cupsdClearString(valbuf
);
4864 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4866 ptr1
+= strlen(tag
);
4868 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4874 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4875 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4878 * Dequote the value...
4881 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4882 if (*ptr1
== '\\' && ptr1
[1])
4883 _cups_strcpy(ptr1
, ptr1
+ 1);
4894 * 'slp_reg_callback()' - Empty SLPRegReport.
4898 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4899 SLPError errcode
, /* I - Error code, if any */
4900 void *cookie
) /* I - App data */
4911 * 'slp_url_callback()' - SLP service url callback
4914 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4916 SLPHandle hslp
, /* I - SLP handle */
4917 const char *srvurl
, /* I - URL of service */
4918 unsigned short lifetime
, /* I - Life of service */
4919 SLPError errcode
, /* I - Existing error code */
4920 void *cookie
) /* I - Pointer to service list */
4922 slpsrvurl_t
*s
, /* New service entry */
4923 **head
; /* Pointer to head of entry */
4927 * Let the compiler know we won't be using these vars...
4934 * Bail if there was an error
4937 if (errcode
!= SLP_OK
)
4941 * Grab the head of the list...
4944 head
= (slpsrvurl_t
**)cookie
;
4947 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4950 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4954 * Copy the SLP service URL...
4957 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4960 * Link the SLP service URL into the head of the list
4970 #endif /* HAVE_LIBSLP */
4974 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4978 update_cups_browse(void)
4980 int i
; /* Looping var */
4981 int auth
; /* Authorization status */
4982 int len
; /* Length of name string */
4983 int bytes
; /* Number of bytes left */
4984 char packet
[1541], /* Broadcast packet */
4985 *pptr
; /* Pointer into packet */
4986 socklen_t srclen
; /* Length of source address */
4987 http_addr_t srcaddr
; /* Source address */
4988 char srcname
[1024]; /* Source hostname */
4989 unsigned address
[4]; /* Source address */
4990 unsigned type
; /* Printer type */
4991 unsigned state
; /* Printer state */
4992 char uri
[HTTP_MAX_URI
], /* Printer URI */
4993 host
[HTTP_MAX_URI
], /* Host portion of URI */
4994 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4995 info
[IPP_MAX_NAME
], /* Information string */
4996 location
[IPP_MAX_NAME
], /* Location string */
4997 make_model
[IPP_MAX_NAME
];/* Make and model string */
4998 int num_attrs
; /* Number of attributes */
4999 cups_option_t
*attrs
; /* Attributes */
5003 * Read a packet from the browse socket...
5006 srclen
= sizeof(srcaddr
);
5007 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5008 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5011 * "Connection refused" is returned under Linux if the destination port
5012 * or address is unreachable from a previous sendto(); check for the
5013 * error here and ignore it for now...
5016 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5018 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5020 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5023 closesocket(BrowseSocket
);
5025 close(BrowseSocket
);
5028 cupsdRemoveSelect(BrowseSocket
);
5031 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5032 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5038 packet
[bytes
] = '\0';
5041 * If we're about to sleep, ignore incoming browse packets.
5048 * Figure out where it came from...
5052 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5054 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5055 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5056 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5057 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5060 #endif /* AF_INET6 */
5065 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5068 if (HostNameLookups
)
5069 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5071 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5073 len
= strlen(srcname
);
5081 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5084 * Access from localhost (127.0.0.1) is always allowed...
5087 auth
= CUPSD_AUTH_ALLOW
;
5092 * Do authorization checks on the domain/address...
5095 switch (BrowseACL
->order_type
)
5098 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5101 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5102 auth
= CUPSD_AUTH_ALLOW
;
5104 if (cupsdCheckAuth(address
, srcname
, len
,
5105 BrowseACL
->num_deny
, BrowseACL
->deny
))
5106 auth
= CUPSD_AUTH_DENY
;
5108 if (cupsdCheckAuth(address
, srcname
, len
,
5109 BrowseACL
->num_allow
, BrowseACL
->allow
))
5110 auth
= CUPSD_AUTH_ALLOW
;
5113 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5114 auth
= CUPSD_AUTH_DENY
;
5116 if (cupsdCheckAuth(address
, srcname
, len
,
5117 BrowseACL
->num_allow
, BrowseACL
->allow
))
5118 auth
= CUPSD_AUTH_ALLOW
;
5120 if (cupsdCheckAuth(address
, srcname
, len
,
5121 BrowseACL
->num_deny
, BrowseACL
->deny
))
5122 auth
= CUPSD_AUTH_DENY
;
5128 auth
= CUPSD_AUTH_ALLOW
;
5130 if (auth
== CUPSD_AUTH_DENY
)
5132 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5133 "update_cups_browse: Refused %d bytes from %s", bytes
,
5138 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5139 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5146 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5148 cupsdLogMessage(CUPSD_LOG_WARN
,
5149 "update_cups_browse: Garbled browse packet - %s", packet
);
5153 strcpy(location
, "Location Unknown");
5154 strcpy(info
, "No Information Available");
5155 make_model
[0] = '\0';
5159 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5162 * Have extended information; can't use sscanf for it because not all
5163 * sscanf's allow empty strings with %[^\"]...
5166 for (i
= 0, pptr
++;
5167 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5169 location
[i
] = *pptr
;
5177 while (*pptr
&& isspace(*pptr
& 255))
5182 for (i
= 0, pptr
++;
5183 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5192 while (*pptr
&& isspace(*pptr
& 255))
5197 for (i
= 0, pptr
++;
5198 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5200 make_model
[i
] = *pptr
;
5205 make_model
[i
] = '\0';
5208 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5214 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5215 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5216 type
, state
, uri
, location
, info
, make_model
));
5219 * Pull the URI apart to see if this is a local or remote printer...
5222 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5224 cupsFreeOptions(num_attrs
, attrs
);
5232 for (i
= 0; i
< NumRelays
; i
++)
5233 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5234 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5235 (struct sockaddr
*)&(Relays
[i
].to
),
5236 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5238 cupsdLogMessage(CUPSD_LOG_ERROR
,
5239 "update_cups_browse: sendto failed for relay %d - %s.",
5240 i
+ 1, strerror(errno
));
5241 cupsFreeOptions(num_attrs
, attrs
);
5246 * Process the browse data...
5249 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5250 (ipp_pstate_t
)state
, location
, info
, make_model
,
5256 * 'update_lpd()' - Update the LPD configuration as needed.
5260 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5267 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5268 * setting for backwards-compatibility.
5271 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5273 #endif /* __APPLE__ */
5275 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5278 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5281 char newfile
[1024]; /* New cups-lpd.N file */
5282 cups_file_t
*ofp
, /* Original file pointer */
5283 *nfp
; /* New file pointer */
5284 char line
[1024]; /* Line from file */
5287 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5289 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5291 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5292 LPDConfigFile
+ 9, strerror(errno
));
5296 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5298 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5299 newfile
, strerror(errno
));
5305 * Copy all of the lines from the cups-lpd file...
5308 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5312 cupsFilePrintf(nfp
, "%s\n", line
);
5313 snprintf(line
, sizeof(line
), "\tdisable = %s",
5314 onoff
? "no" : "yes");
5316 else if (!strstr(line
, "disable ="))
5317 cupsFilePrintf(nfp
, "%s\n", line
);
5322 rename(newfile
, LPDConfigFile
+ 9);
5325 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5328 * Enable/disable LPD via the launchctl command...
5331 char *argv
[5], /* Arguments for command */
5332 *envp
[MAX_ENV
]; /* Environment for command */
5333 int pid
; /* Process ID */
5336 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5337 argv
[0] = (char *)"launchctl";
5338 argv
[1] = (char *)(onoff
? "load" : "unload");
5339 argv
[2] = (char *)"-w";
5340 argv
[3] = LPDConfigFile
+ 10;
5343 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5346 #endif /* __APPLE__ */
5348 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5353 * 'update_polling()' - Read status messages from the poll daemons.
5357 update_polling(void)
5359 char *ptr
, /* Pointer to end of line in buffer */
5360 message
[1024]; /* Pointer to message text */
5361 int loglevel
; /* Log level for message */
5364 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5365 message
, sizeof(message
))) != NULL
)
5367 if (loglevel
== CUPSD_LOG_INFO
)
5368 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5370 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5374 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5377 * All polling processes have died; stop polling...
5380 cupsdLogMessage(CUPSD_LOG_ERROR
,
5381 "update_polling: all polling processes have exited!");
5388 * 'update_smb()' - Update the SMB configuration as needed.
5392 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5397 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5400 * Enable/disable SMB via the specified smb.conf config file...
5403 char newfile
[1024]; /* New smb.conf.N file */
5404 cups_file_t
*ofp
, /* Original file pointer */
5405 *nfp
; /* New file pointer */
5406 char line
[1024]; /* Line from file */
5407 int in_printers
; /* In [printers] section? */
5410 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5412 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5414 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5415 SMBConfigFile
+ 8, strerror(errno
));
5419 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5421 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5422 newfile
, strerror(errno
));
5428 * Copy all of the lines from the smb.conf file...
5433 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5435 if (in_printers
&& strstr(line
, "printable ="))
5436 snprintf(line
, sizeof(line
), " printable = %s",
5437 onoff
? "yes" : "no");
5439 cupsFilePrintf(nfp
, "%s\n", line
);
5442 in_printers
= !strcmp(line
, "[printers]");
5447 rename(newfile
, SMBConfigFile
+ 8);
5450 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5455 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".