2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
4 * Directory services routines for the CUPS scheduler.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * ldap_rebind_proc() - Callback function for LDAP rebind
27 * ldap_connect() - Start new LDAP connection
28 * ldap_reconnect() - Reconnect to LDAP Server
29 * ldap_disconnect() - Disconnect from LDAP Server
30 * cupsdStartBrowsing() - Start sending and receiving broadcast
32 * cupsdStartPolling() - Start polling servers as needed.
33 * cupsdStopBrowsing() - Stop sending and receiving broadcast
35 * cupsdStopPolling() - Stop polling servers as needed.
36 * cupsdUpdateDNSSDName() - Update the computer name we use for
38 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
39 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
40 * dequote() - Remote quotes from a string.
41 * dnssdAddAlias() - Add a DNS-SD alias name.
42 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
43 * dnssdComparePrinters() - Compare the registered names of two printers.
44 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
46 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
48 * dnssdRegisterCallback() - DNSServiceRegister callback.
49 * dnssdRegisterPrinter() - Start sending broadcast information for a
50 * printer or update the broadcast contents.
51 * dnssdStop() - Stop all DNS-SD registrations.
52 * dnssdUpdate() - Handle DNS-SD queries.
53 * get_auth_info_required() - Get the auth-info-required value to advertise.
54 * get_hostconfig() - Get an /etc/hostconfig service setting.
55 * is_local_queue() - Determine whether the URI points at a local
57 * process_browse_data() - Process new browse data.
58 * process_implicit_classes() - Create/update implicit classes as needed.
59 * send_cups_browse() - Send new browsing information using the CUPS
61 * ldap_search_rec() - LDAP Search with reconnect
62 * ldap_freeres() - Free LDAPMessage
63 * ldap_getval_char() - Get first LDAP value and convert to string
64 * send_ldap_ou() - Send LDAP ou registrations.
65 * send_ldap_browse() - Send LDAP printer registrations.
66 * ldap_dereg_printer() - Delete printer from directory
67 * ldap_dereg_ou() - Remove the organizational unit.
68 * send_slp_browse() - Register the specified printer with SLP.
69 * slp_attr_callback() - SLP attribute callback
70 * slp_dereg_printer() - SLPDereg() the specified printer
71 * slp_get_attr() - Get an attribute from an SLP registration.
72 * slp_reg_callback() - Empty SLPRegReport.
73 * slp_url_callback() - SLP service url callback
74 * update_cups_browse() - Update the browse lists using the CUPS
76 * update_lpd() - Update the LPD configuration as needed.
77 * update_polling() - Read status messages from the poll daemons.
78 * update_smb() - Update the SMB configuration as needed.
82 * Include necessary headers...
92 # ifdef HAVE_COREFOUNDATION
93 # include <CoreFoundation/CoreFoundation.h>
94 # endif /* HAVE_COREFOUNDATION */
95 # ifdef HAVE_SYSTEMCONFIGURATION
96 # include <SystemConfiguration/SystemConfiguration.h>
97 # endif /* HAVE_SYSTEMCONFIGURATION */
98 # endif /* __APPLE__ */
99 #endif /* HAVE_DNSSD */
106 static char *dequote(char *d
, const char *s
, int dlen
);
107 static char *get_auth_info_required(cupsd_printer_t
*p
, char *buffer
,
110 static int get_hostconfig(const char *name
);
111 #endif /* __APPLE__ */
112 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
113 char *resource
, int resourcelen
);
114 static void process_browse_data(const char *uri
, const char *host
,
115 const char *resource
, cups_ptype_t type
,
116 ipp_pstate_t state
, const char *location
,
117 const char *info
, const char *make_model
,
118 int num_attrs
, cups_option_t
*attrs
);
119 static void process_implicit_classes(void);
120 static void send_cups_browse(cupsd_printer_t
*p
);
122 static LDAP
*ldap_connect(void);
123 static LDAP
*ldap_reconnect(void);
124 static void ldap_disconnect(LDAP
*ld
);
125 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
126 char *filter
, char *attrs
[],
127 int attrsonly
, LDAPMessage
**res
);
128 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
129 char *attr
, char *retval
,
130 unsigned long maxsize
);
131 static void ldap_freeres(LDAPMessage
*entry
);
132 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
133 static void send_ldap_browse(cupsd_printer_t
*p
);
134 static void ldap_dereg_printer(cupsd_printer_t
*p
);
135 static void ldap_dereg_ou(char *ou
, char *basedn
);
136 # ifdef HAVE_LDAP_REBIND_PROC
137 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
138 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
139 LDAP_CONST
char *refsp
,
144 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
150 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
151 # endif /* HAVE_LDAP_REBIND_PROC */
152 #endif /* HAVE_LDAP */
154 static void send_slp_browse(cupsd_printer_t
*p
);
155 #endif /* HAVE_LIBSLP */
156 static void update_cups_browse(void);
157 static void update_lpd(int onoff
);
158 static void update_polling(void);
159 static void update_smb(int onoff
);
163 # ifdef HAVE_COREFOUNDATION
164 static void dnssdAddAlias(const void *key
, const void *value
,
166 # endif /* HAVE_COREFOUNDATION */
167 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
169 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
170 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
171 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
173 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
174 DNSServiceFlags flags
,
175 DNSServiceErrorType errorCode
,
176 const char *name
, const char *regtype
,
177 const char *domain
, void *context
);
178 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
179 static void dnssdStop(void);
180 static void dnssdUpdate(void);
181 #endif /* HAVE_DNSSD */
184 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
186 "printerDescription",
188 "printerMakeAndModel",
193 #endif /* HAVE_LDAP */
201 * SLP service name for CUPS...
204 # define SLP_CUPS_SRVTYPE "service:printer"
205 # define SLP_CUPS_SRVLEN 15
209 * Printer service URL structure
212 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
214 struct _slpsrvurl_s
*next
; /* Next URL in list */
215 char url
[HTTP_MAX_URI
];
224 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
225 SLPError errcode
, void *cookie
);
226 static void slp_dereg_printer(cupsd_printer_t
*p
);
227 static int slp_get_attr(const char *attrlist
, const char *tag
,
229 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
231 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
232 unsigned short lifetime
,
233 SLPError errcode
, void *cookie
);
234 #endif /* HAVE_LIBSLP */
238 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
239 * local printer and remove any pending
240 * references to remote printers.
244 cupsdDeregisterPrinter(
245 cupsd_printer_t
*p
, /* I - Printer to register */
246 int removeit
) /* I - Printer being permanently removed */
249 * Only deregister if browsing is enabled and it's a local printer...
252 cupsdLogMessage(CUPSD_LOG_DEBUG
,
253 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
256 if (!Browsing
|| !p
->shared
||
257 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
258 CUPS_PRINTER_SCANNER
)))
262 * Announce the deletion...
265 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
267 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
269 p
->type
|= CUPS_PRINTER_DELETE
;
277 if (BrowseLocalProtocols
& BROWSE_SLP
)
278 slp_dereg_printer(p
);
279 #endif /* HAVE_LIBSLP */
282 if (BrowseLocalProtocols
& BROWSE_LDAP
)
283 ldap_dereg_printer(p
);
284 #endif /* HAVE_LDAP */
287 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
288 dnssdDeregisterPrinter(p
);
289 #endif /* HAVE_DNSSD */
294 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
298 cupsdLoadRemoteCache(void)
300 int i
; /* Looping var */
301 cups_file_t
*fp
; /* remote.cache file */
302 int linenum
; /* Current line number */
303 char line
[4096], /* Line from file */
304 *value
, /* Pointer to value */
305 *valueptr
, /* Pointer into value */
306 scheme
[32], /* Scheme portion of URI */
307 username
[64], /* Username portion of URI */
309 /* Hostname portion of URI */
310 resource
[HTTP_MAX_URI
];
311 /* Resource portion of URI */
312 int port
; /* Port number */
313 cupsd_printer_t
*p
; /* Current printer */
314 time_t now
; /* Current time */
318 * Don't load the cache if the remote protocols are disabled...
323 cupsdLogMessage(CUPSD_LOG_DEBUG
,
324 "cupsdLoadRemoteCache: Not loading remote cache.");
329 * Open the remote.cache file...
332 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
333 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
337 * Read printer configurations until we hit EOF...
344 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
347 * Decode the directive...
350 if (!strcasecmp(line
, "<Printer") ||
351 !strcasecmp(line
, "<DefaultPrinter"))
354 * <Printer name> or <DefaultPrinter name>
357 if (p
== NULL
&& value
)
360 * Add the printer and a base file type...
363 cupsdLogMessage(CUPSD_LOG_DEBUG
,
364 "cupsdLoadRemoteCache: Loading printer %s...", value
);
366 if ((p
= cupsdFindDest(value
)) != NULL
)
368 if (p
->type
& CUPS_PRINTER_CLASS
)
370 cupsdLogMessage(CUPSD_LOG_WARN
,
371 "Cached remote printer \"%s\" conflicts with "
379 p
= cupsdAddPrinter(value
);
382 p
->state
= IPP_PRINTER_IDLE
;
383 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
384 p
->browse_time
= now
;
385 p
->browse_expire
= now
+ BrowseTimeout
;
388 * Set the default printer as needed...
391 if (!strcasecmp(line
, "<DefaultPrinter"))
396 cupsdLogMessage(CUPSD_LOG_ERROR
,
397 "Syntax error on line %d of remote.cache.", linenum
);
401 else if (!strcasecmp(line
, "<Class") ||
402 !strcasecmp(line
, "<DefaultClass"))
405 * <Class name> or <DefaultClass name>
408 if (p
== NULL
&& value
)
411 * Add the printer and a base file type...
414 cupsdLogMessage(CUPSD_LOG_DEBUG
,
415 "cupsdLoadRemoteCache: Loading class %s...", value
);
417 if ((p
= cupsdFindDest(value
)) != NULL
)
418 p
->type
= CUPS_PRINTER_CLASS
;
420 p
= cupsdAddClass(value
);
423 p
->state
= IPP_PRINTER_IDLE
;
424 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
425 p
->browse_time
= now
;
426 p
->browse_expire
= now
+ BrowseTimeout
;
429 * Set the default printer as needed...
432 if (!strcasecmp(line
, "<DefaultClass"))
437 cupsdLogMessage(CUPSD_LOG_ERROR
,
438 "Syntax error on line %d of remote.cache.", linenum
);
442 else if (!strcasecmp(line
, "</Printer>") ||
443 !strcasecmp(line
, "</Class>"))
448 * Close out the current printer...
451 cupsdSetPrinterAttrs(p
);
456 cupsdLogMessage(CUPSD_LOG_ERROR
,
457 "Syntax error on line %d of remote.cache.", linenum
);
461 cupsdLogMessage(CUPSD_LOG_ERROR
,
462 "Syntax error on line %d of remote.cache.", linenum
);
464 else if (!strcasecmp(line
, "UUID"))
466 if (value
&& !strncmp(value
, "urn:uuid:", 9))
467 cupsdSetString(&(p
->uuid
), value
);
469 cupsdLogMessage(CUPSD_LOG_ERROR
,
470 "Bad UUID on line %d of remote.cache.", linenum
);
472 else if (!strcasecmp(line
, "Info"))
475 cupsdSetString(&p
->info
, value
);
477 else if (!strcasecmp(line
, "MakeModel"))
480 cupsdSetString(&p
->make_model
, value
);
482 else if (!strcasecmp(line
, "Location"))
485 cupsdSetString(&p
->location
, value
);
487 else if (!strcasecmp(line
, "DeviceURI"))
491 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
492 username
, sizeof(username
), host
, sizeof(host
), &port
,
493 resource
, sizeof(resource
));
495 cupsdSetString(&p
->hostname
, host
);
496 cupsdSetString(&p
->uri
, value
);
497 cupsdSetDeviceURI(p
, value
);
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Syntax error on line %d of remote.cache.", linenum
);
503 else if (!strcasecmp(line
, "Option") && value
)
509 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
512 cupsdLogMessage(CUPSD_LOG_ERROR
,
513 "Syntax error on line %d of remote.cache.", linenum
);
516 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
518 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
522 else if (!strcasecmp(line
, "Reason"))
526 for (i
= 0 ; i
< p
->num_reasons
; i
++)
527 if (!strcmp(value
, p
->reasons
[i
]))
530 if (i
>= p
->num_reasons
&&
531 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
533 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
538 cupsdLogMessage(CUPSD_LOG_ERROR
,
539 "Syntax error on line %d of remote.cache.", linenum
);
541 else if (!strcasecmp(line
, "State"))
544 * Set the initial queue state...
547 if (value
&& !strcasecmp(value
, "idle"))
548 p
->state
= IPP_PRINTER_IDLE
;
549 else if (value
&& !strcasecmp(value
, "stopped"))
551 p
->state
= IPP_PRINTER_STOPPED
;
552 cupsdSetPrinterReasons(p
, "+paused");
555 cupsdLogMessage(CUPSD_LOG_ERROR
,
556 "Syntax error on line %d of remote.cache.", linenum
);
558 else if (!strcasecmp(line
, "StateMessage"))
561 * Set the initial queue state message...
565 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
567 else if (!strcasecmp(line
, "Accepting"))
570 * Set the initial accepting state...
574 (!strcasecmp(value
, "yes") ||
575 !strcasecmp(value
, "on") ||
576 !strcasecmp(value
, "true")))
579 (!strcasecmp(value
, "no") ||
580 !strcasecmp(value
, "off") ||
581 !strcasecmp(value
, "false")))
584 cupsdLogMessage(CUPSD_LOG_ERROR
,
585 "Syntax error on line %d of remote.cache.", linenum
);
587 else if (!strcasecmp(line
, "Type"))
590 p
->type
= atoi(value
);
592 cupsdLogMessage(CUPSD_LOG_ERROR
,
593 "Syntax error on line %d of remote.cache.", linenum
);
595 else if (!strcasecmp(line
, "BrowseTime"))
599 time_t t
= atoi(value
);
601 if (t
> p
->browse_expire
)
602 p
->browse_expire
= t
;
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "Syntax error on line %d of remote.cache.", linenum
);
608 else if (!strcasecmp(line
, "JobSheets"))
611 * Set the initial job sheets...
616 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
621 cupsdSetString(&p
->job_sheets
[0], value
);
623 while (isspace(*valueptr
& 255))
628 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
633 cupsdSetString(&p
->job_sheets
[1], value
);
637 cupsdLogMessage(CUPSD_LOG_ERROR
,
638 "Syntax error on line %d of remote.cache.", linenum
);
640 else if (!strcasecmp(line
, "AllowUser"))
645 cupsdAddString(&(p
->users
), value
);
648 cupsdLogMessage(CUPSD_LOG_ERROR
,
649 "Syntax error on line %d of remote.cache.", linenum
);
651 else if (!strcasecmp(line
, "DenyUser"))
656 cupsdAddString(&(p
->users
), value
);
659 cupsdLogMessage(CUPSD_LOG_ERROR
,
660 "Syntax error on line %d of remote.cache.", linenum
);
665 * Something else we don't understand...
668 cupsdLogMessage(CUPSD_LOG_ERROR
,
669 "Unknown configuration directive %s on line %d of remote.cache.",
677 * Do auto-classing if needed...
680 process_implicit_classes();
685 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
686 * printer or update the broadcast contents.
690 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
692 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
695 if (!Browsing
|| !BrowseLocalProtocols
||
696 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
697 CUPS_PRINTER_SCANNER
)))
701 /* if (BrowseLocalProtocols & BROWSE_SLP)
702 slpRegisterPrinter(p); */
703 #endif /* HAVE_LIBSLP */
706 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
707 dnssdRegisterPrinter(p
);
708 #endif /* HAVE_DNSSD */
713 * 'cupsdRestartPolling()' - Restart polling servers as needed.
717 cupsdRestartPolling(void)
719 int i
; /* Looping var */
720 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
723 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
725 kill(pollp
->pid
, SIGHUP
);
730 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
734 cupsdSaveRemoteCache(void)
736 int i
; /* Looping var */
737 cups_file_t
*fp
; /* printers.conf file */
738 char temp
[1024], /* Temporary string */
739 value
[2048], /* Value string */
740 *name
; /* Current user name */
741 cupsd_printer_t
*printer
; /* Current printer class */
742 time_t curtime
; /* Current time */
743 struct tm
*curdate
; /* Current date */
744 cups_option_t
*option
; /* Current option */
748 * Create the remote.cache file...
751 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
753 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
755 cupsdLogMessage(CUPSD_LOG_ERROR
,
756 "Unable to save remote.cache - %s", strerror(errno
));
760 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
763 * Restrict access to the file...
766 fchown(cupsFileNumber(fp
), getuid(), Group
);
767 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
770 * Write a small header to the file...
773 curtime
= time(NULL
);
774 curdate
= localtime(&curtime
);
775 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
777 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
778 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
781 * Write each local printer known to the system...
784 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
786 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
789 * Skip local destinations...
792 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
796 * Write printers as needed...
799 if (printer
== DefaultPrinter
)
800 cupsFilePuts(fp
, "<Default");
802 cupsFilePutChar(fp
, '<');
804 if (printer
->type
& CUPS_PRINTER_CLASS
)
805 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
807 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
809 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
811 cupsFilePrintf(fp
, "UUID %s\n", printer
->uuid
);
814 cupsFilePutConf(fp
, "Info", printer
->info
);
816 if (printer
->location
)
817 cupsFilePutConf(fp
, "Location", printer
->location
);
819 if (printer
->make_model
)
820 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
822 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
824 if (printer
->state
== IPP_PRINTER_STOPPED
)
825 cupsFilePuts(fp
, "State Stopped\n");
827 cupsFilePuts(fp
, "State Idle\n");
829 for (i
= 0; i
< printer
->num_reasons
; i
++)
830 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
832 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
834 if (printer
->accepting
)
835 cupsFilePuts(fp
, "Accepting Yes\n");
837 cupsFilePuts(fp
, "Accepting No\n");
839 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
840 printer
->job_sheets
[1]);
841 cupsFilePutConf(fp
, "JobSheets", value
);
843 for (name
= (char *)cupsArrayFirst(printer
->users
);
845 name
= (char *)cupsArrayNext(printer
->users
))
846 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser", name
);
848 for (i
= printer
->num_options
, option
= printer
->options
;
852 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
853 cupsFilePutConf(fp
, "Option", value
);
856 if (printer
->type
& CUPS_PRINTER_CLASS
)
857 cupsFilePuts(fp
, "</Class>\n");
859 cupsFilePuts(fp
, "</Printer>\n");
867 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
871 cupsdSendBrowseList(void)
873 int count
; /* Number of dests to update */
874 cupsd_printer_t
*p
; /* Current printer */
875 time_t ut
, /* Minimum update time */
876 to
; /* Timeout time */
879 if (!Browsing
|| !Printers
)
883 * Compute the update and timeout times...
887 ut
= to
- BrowseInterval
;
890 * Figure out how many printers need an update...
893 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
895 int max_count
; /* Maximum number to update */
899 * Throttle the number of printers we'll be updating this time
900 * around based on the number of queues that need updating and
901 * the maximum number of queues to update each second...
904 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
906 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
907 count
< max_count
&& p
!= NULL
;
908 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
909 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
910 CUPS_PRINTER_SCANNER
)) &&
911 p
->shared
&& p
->browse_time
< ut
)
915 * Loop through all of the printers and send local updates as needed...
919 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
921 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
925 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
928 * Check for wraparound...
932 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
936 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
937 CUPS_PRINTER_SCANNER
)) ||
940 else if (p
->browse_time
< ut
)
943 * Need to send an update...
948 p
->browse_time
= time(NULL
);
950 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
954 if (BrowseLocalProtocols
& BROWSE_SLP
)
956 #endif /* HAVE_LIBSLP */
959 if (BrowseLocalProtocols
& BROWSE_LDAP
)
961 #endif /* HAVE_LDAP */
966 * Save where we left off so that all printers get updated...
973 * Loop through all of the printers and timeout old printers as needed...
976 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
978 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
981 * If this is a remote queue, see if it needs to be timed out...
984 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
985 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
986 p
->browse_expire
< to
)
988 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
989 "%s \'%s\' deleted by directory services (timeout).",
990 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
993 cupsdLogMessage(CUPSD_LOG_DEBUG
,
994 "Remote destination \"%s\" has timed out; "
998 cupsArraySave(Printers
);
999 cupsdDeletePrinter(p
, 1);
1000 cupsArrayRestore(Printers
);
1001 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
1007 #ifdef HAVE_LDAP_REBIND_PROC
1008 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1010 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1013 static int /* O - Result code */
1015 LDAP
*RebindLDAPHandle
, /* I - LDAP handle */
1016 LDAP_CONST
char *refsp
, /* I - ??? */
1017 ber_tag_t request
, /* I - ??? */
1018 ber_int_t msgid
, /* I - ??? */
1019 void *params
) /* I - ??? */
1021 int rc
; /* Result code */
1022 # if LDAP_API_VERSION > 3000
1023 struct berval bval
; /* Bind value */
1024 # endif /* LDAP_API_VERSION > 3000 */
1027 * Bind to new LDAP server...
1030 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_rebind_proc: Rebind to %s", refsp
);
1032 # if LDAP_API_VERSION > 3000
1033 bval
.bv_val
= BrowseLDAPPassword
;
1034 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1036 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
,
1037 &bval
, NULL
, NULL
, NULL
);
1039 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, BrowseLDAPPassword
,
1041 # endif /* LDAP_API_VERSION > 3000 */
1047 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1049 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1052 static int /* O - Result code */
1054 LDAP
*RebindLDAPHandle
, /* I - LDAP handle */
1055 char **dnp
, /* I - ??? */
1056 char **passwdp
, /* I - ??? */
1057 int *authmethodp
, /* I - ??? */
1058 int freeit
, /* I - ??? */
1059 void *arg
) /* I - ??? */
1065 * Free current values...
1068 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_rebind_proc: Free values...");
1073 if (passwdp
&& *passwdp
)
1079 * Return credentials for LDAP referal...
1082 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1083 "ldap_rebind_proc: Return necessary values...");
1085 *dnp
= strdup(BrowseLDAPBindDN
);
1086 *passwdp
= strdup(BrowseLDAPPassword
);
1087 *authmethodp
= LDAP_AUTH_SIMPLE
;
1092 * Should never happen...
1095 cupsdLogMessage(CUPSD_LOG_ERROR
,
1096 "LDAP rebind has been called with wrong freeit value!");
1100 return (LDAP_SUCCESS
);
1102 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1103 #endif /* HAVE_LDAP_REBIND_PROC */
1108 * 'ldap_connect()' - Start new LDAP connection
1111 static LDAP
* /* O - LDAP handle */
1114 int rc
; /* LDAP API status */
1115 int version
= 3; /* LDAP version */
1116 struct berval bv
= {0, ""}; /* SASL bind value */
1117 LDAP
*TempBrowseLDAPHandle
=NULL
;
1118 /* Temporary LDAP Handle */
1119 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1120 int ldap_ssl
= 0; /* LDAP SSL indicator */
1121 int ssl_err
= 0; /* LDAP SSL error value */
1122 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1125 # ifdef HAVE_OPENLDAP
1126 # ifdef HAVE_LDAP_SSL
1128 * Set the certificate file to use for encrypted LDAP sessions...
1131 if (BrowseLDAPCACertFile
)
1133 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1134 "ldap_connect: Setting CA certificate file \"%s\"",
1135 BrowseLDAPCACertFile
);
1137 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1138 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "Unable to set CA certificate file for LDAP "
1141 "connections: %d - %s", rc
, ldap_err2string(rc
));
1143 # endif /* HAVE_LDAP_SSL */
1146 * Initialize OPENLDAP connection...
1147 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1150 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1151 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1153 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1155 # else /* HAVE_OPENLDAP */
1157 int ldap_port
= 0; /* LDAP port */
1158 char ldap_protocol
[11], /* LDAP protocol */
1159 ldap_host
[255]; /* LDAP host */
1162 * Split LDAP URI into its components...
1165 if (!BrowseLDAPServer
)
1167 cupsdLogMessage(CUPSD_LOG_ERROR
, "BrowseLDAPServer not configured!");
1168 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1169 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1170 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1174 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
,
1177 if (!strcmp(ldap_protocol
, "ldap"))
1179 else if (!strcmp(ldap_protocol
, "ldaps"))
1183 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unrecognized LDAP protocol (%s)!",
1185 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1186 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1187 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1194 ldap_port
= LDAPS_PORT
;
1196 ldap_port
= LDAP_PORT
;
1199 cupsdLogMessage(CUPSD_LOG_DEBUG
, "ldap_connect: PROT:%s HOST:%s PORT:%d",
1200 ldap_protocol
, ldap_host
, ldap_port
);
1203 * Initialize LDAP connection...
1208 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1209 rc
= LDAP_OPERATIONS_ERROR
;
1213 # ifdef HAVE_LDAP_SSL
1218 * Initialize SSL LDAP connection...
1221 if (BrowseLDAPCACertFile
)
1223 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1224 if (rc
!= LDAP_SUCCESS
)
1226 cupsdLogMessage(CUPSD_LOG_ERROR
,
1227 "Failed to initialize LDAP SSL client!");
1228 rc
= LDAP_OPERATIONS_ERROR
;
1232 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
,
1234 rc
= LDAP_OPERATIONS_ERROR
;
1241 cupsdLogMessage(CUPSD_LOG_ERROR
,
1242 "LDAP SSL certificate file/database not configured!");
1243 rc
= LDAP_OPERATIONS_ERROR
;
1246 # else /* HAVE_LDAP_SSL */
1249 * Return error, because client libraries doesn't support SSL
1252 cupsdLogMessage(CUPSD_LOG_ERROR
,
1253 "LDAP client libraries do not support SSL");
1254 rc
= LDAP_OPERATIONS_ERROR
;
1256 # endif /* HAVE_LDAP_SSL */
1258 # endif /* HAVE_OPENLDAP */
1261 * Check return code from LDAP initialize...
1264 if (rc
!= LDAP_SUCCESS
)
1266 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to initialize LDAP!");
1268 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
1269 cupsdLogMessage(CUPSD_LOG_ERROR
, "Temporarily disabling LDAP browsing...");
1272 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1274 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1275 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1278 ldap_disconnect(TempBrowseLDAPHandle
);
1284 * Upgrade LDAP version...
1287 if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1288 (const void *)&version
) != LDAP_SUCCESS
)
1290 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set LDAP protocol version %d!",
1292 cupsdLogMessage(CUPSD_LOG_ERROR
, "Disabling LDAP browsing!");
1294 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1295 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1296 ldap_disconnect(TempBrowseLDAPHandle
);
1302 * Register LDAP rebind procedure...
1305 # ifdef HAVE_LDAP_REBIND_PROC
1306 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1308 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
,
1310 if (rc
!= LDAP_SUCCESS
)
1311 cupsdLogMessage(CUPSD_LOG_ERROR
,
1312 "Setting LDAP rebind function failed with status %d: %s",
1313 rc
, ldap_err2string(rc
));
1317 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1319 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1320 # endif /* HAVE_LDAP_REBIND_PROC */
1323 * Start LDAP bind...
1326 # if LDAP_API_VERSION > 3000
1328 bval
.bv_val
= BrowseLDAPPassword
;
1329 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1331 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1332 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1335 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1338 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1339 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1340 # endif /* LDAP_API_VERSION > 3000 */
1342 if (rc
!= LDAP_SUCCESS
)
1344 cupsdLogMessage(CUPSD_LOG_ERROR
, "LDAP bind failed with error %d: %s",
1345 rc
, ldap_err2string(rc
));
1347 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1348 if (ldap_ssl
&& (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
1350 ssl_err
= PORT_GetError();
1352 cupsdLogMessage(CUPSD_LOG_ERROR
, "LDAP SSL error %d: %s", ssl_err
,
1353 ldapssl_err2string(ssl_err
));
1355 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1357 ldap_disconnect(TempBrowseLDAPHandle
);
1362 cupsdLogMessage(CUPSD_LOG_INFO
, "LDAP connection established");
1364 return (TempBrowseLDAPHandle
);
1369 * 'ldap_reconnect()' - Reconnect to LDAP Server
1372 static LDAP
* /* O - New LDAP handle */
1373 ldap_reconnect(void)
1375 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1379 * Get a new LDAP Handle and replace the global Handle
1380 * if the new connection was successful.
1383 cupsdLogMessage(CUPSD_LOG_INFO
, "Try LDAP reconnect...");
1385 TempBrowseLDAPHandle
= ldap_connect();
1387 if (TempBrowseLDAPHandle
!= NULL
)
1389 if (BrowseLDAPHandle
!= NULL
)
1390 ldap_disconnect(BrowseLDAPHandle
);
1392 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1395 return (BrowseLDAPHandle
);
1400 * 'ldap_disconnect()' - Disconnect from LDAP Server
1404 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1406 int rc
; /* Return code */
1410 * Close LDAP handle...
1413 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1414 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1416 rc
= ldap_unbind_s(ld
);
1417 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1419 if (rc
!= LDAP_SUCCESS
)
1420 cupsdLogMessage(CUPSD_LOG_ERROR
,
1421 "Unbind from LDAP server failed with status %d: %s",
1422 rc
, ldap_err2string(rc
));
1424 #endif /* HAVE_LDAP */
1428 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1432 cupsdStartBrowsing(void)
1434 int val
; /* Socket option value */
1435 struct sockaddr_in addr
; /* Broadcast address */
1436 cupsd_printer_t
*p
; /* Current printer */
1441 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1444 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1446 if (BrowseSocket
< 0)
1449 * Create the broadcast socket...
1452 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1454 cupsdLogMessage(CUPSD_LOG_ERROR
,
1455 "Unable to create broadcast socket - %s.",
1457 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1458 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1460 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1461 cupsdEndProcess(getpid(), 0);
1465 if (BrowseSocket
>= 0)
1468 * Bind the socket to browse port...
1471 memset(&addr
, 0, sizeof(addr
));
1472 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1473 addr
.sin_family
= AF_INET
;
1474 addr
.sin_port
= htons(BrowsePort
);
1476 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1478 cupsdLogMessage(CUPSD_LOG_ERROR
,
1479 "Unable to bind broadcast socket - %s.",
1483 closesocket(BrowseSocket
);
1485 close(BrowseSocket
);
1489 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1490 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1492 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1493 cupsdEndProcess(getpid(), 0);
1497 if (BrowseSocket
>= 0)
1500 * Set the "broadcast" flag...
1504 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1506 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1510 closesocket(BrowseSocket
);
1512 close(BrowseSocket
);
1516 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1517 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1519 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1520 cupsdEndProcess(getpid(), 0);
1524 if (BrowseSocket
>= 0)
1527 * Close the socket on exec...
1530 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1533 * Finally, add the socket to the input selection set as needed...
1536 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1539 * We only listen if we want remote printers...
1542 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1551 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1553 DNSServiceErrorType error
; /* Error from service creation */
1554 cupsd_listener_t
*lis
; /* Current listening socket */
1558 * First create a "master" connection for all registrations...
1561 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1562 != kDNSServiceErr_NoError
)
1564 cupsdLogMessage(CUPSD_LOG_ERROR
,
1565 "Unable to create master DNS-SD reference: %d", error
);
1567 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1568 cupsdEndProcess(getpid(), 0);
1573 * Add the master connection to the select list...
1576 int fd
= DNSServiceRefSockFD(DNSSDRef
);
1578 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
1580 cupsdAddSelect(fd
, (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1583 * Then get the port we use for registrations. If we are not listening
1584 * on any non-local ports, there is no sense sharing local printers via
1590 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1592 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1594 if (httpAddrLocalhost(&(lis
->address
)))
1597 if (lis
->address
.addr
.sa_family
== AF_INET
)
1599 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1602 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1604 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1610 * Create an array to track the printers we share...
1613 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1614 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1618 * Set the computer name and register the web interface...
1621 cupsdUpdateDNSSDName();
1624 #endif /* HAVE_DNSSD */
1627 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1630 * Open SLP handle...
1633 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1635 cupsdLogMessage(CUPSD_LOG_ERROR
,
1636 "Unable to open an SLP handle; disabling SLP browsing!");
1637 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1638 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1639 BrowseSLPHandle
= NULL
;
1641 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1642 cupsdEndProcess(getpid(), 0);
1645 BrowseSLPRefresh
= 0;
1648 BrowseSLPHandle
= NULL
;
1649 #endif /* HAVE_LIBSLP */
1652 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1656 cupsdLogMessage(CUPSD_LOG_ERROR
,
1657 "Need to set BrowseLDAPDN to use LDAP browsing!");
1658 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1659 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1661 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1662 cupsdEndProcess(getpid(), 0);
1667 * Open LDAP handle...
1670 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1671 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1672 cupsdEndProcess(getpid(), 0);
1675 BrowseLDAPRefresh
= 0;
1677 #endif /* HAVE_LDAP */
1680 * Enable LPD and SMB printer sharing as needed through external programs...
1683 if (BrowseLocalProtocols
& BROWSE_LPD
)
1686 if (BrowseLocalProtocols
& BROWSE_SMB
)
1690 * Register the individual printers
1693 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1695 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1696 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1697 CUPS_PRINTER_SCANNER
)))
1698 cupsdRegisterPrinter(p
);
1703 * 'cupsdStartPolling()' - Start polling servers as needed.
1707 cupsdStartPolling(void)
1709 int i
; /* Looping var */
1710 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1711 char polld
[1024]; /* Poll daemon path */
1712 char sport
[255]; /* Server port */
1713 char bport
[255]; /* Browser port */
1714 char interval
[255]; /* Poll interval */
1715 int statusfds
[2]; /* Status pipe */
1716 char *argv
[6]; /* Arguments */
1717 char *envp
[100]; /* Environment */
1721 * Don't do anything if we aren't polling...
1724 if (NumPolled
== 0 || BrowseSocket
< 0)
1727 PollStatusBuffer
= NULL
;
1732 * Setup string arguments for polld, port and interval options.
1735 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1737 sprintf(bport
, "%d", BrowsePort
);
1740 sprintf(interval
, "%d", BrowseInterval
);
1742 strcpy(interval
, "30");
1744 argv
[0] = "cups-polld";
1750 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1753 * Create a pipe that receives the status messages from each
1757 if (cupsdOpenPipe(statusfds
))
1759 cupsdLogMessage(CUPSD_LOG_ERROR
,
1760 "Unable to create polling status pipes - %s.",
1763 PollStatusBuffer
= NULL
;
1767 PollPipe
= statusfds
[0];
1768 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1771 * Run each polling daemon, redirecting stderr to the polling pipe...
1774 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1776 sprintf(sport
, "%d", pollp
->port
);
1778 argv
[1] = pollp
->hostname
;
1780 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1781 0, DefaultProfile
, NULL
, &(pollp
->pid
)) < 0)
1783 cupsdLogMessage(CUPSD_LOG_ERROR
,
1784 "cupsdStartPolling: Unable to fork polling daemon - %s",
1790 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1791 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1792 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1795 close(statusfds
[1]);
1798 * Finally, add the pipe to the input selection set...
1801 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1806 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1810 cupsdStopBrowsing(void)
1812 cupsd_printer_t
*p
; /* Current printer */
1815 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1819 * De-register the individual printers
1822 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1824 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1825 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1826 CUPS_PRINTER_SCANNER
)))
1827 cupsdDeregisterPrinter(p
, 1);
1830 * Shut down browsing sockets...
1833 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1837 * Close the socket and remove it from the input selection set.
1841 closesocket(BrowseSocket
);
1843 close(BrowseSocket
);
1846 cupsdRemoveSelect(BrowseSocket
);
1851 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1853 #endif /* HAVE_DNSSD */
1856 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1860 * Close SLP handle...
1863 SLPClose(BrowseSLPHandle
);
1864 BrowseSLPHandle
= NULL
;
1866 #endif /* HAVE_LIBSLP */
1869 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1872 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1873 ldap_disconnect(BrowseLDAPHandle
);
1874 BrowseLDAPHandle
= NULL
;
1876 #endif /* HAVE_OPENLDAP */
1879 * Disable LPD and SMB printer sharing as needed through external programs...
1882 if (BrowseLocalProtocols
& BROWSE_LPD
)
1885 if (BrowseLocalProtocols
& BROWSE_SMB
)
1891 * 'cupsdStopPolling()' - Stop polling servers as needed.
1895 cupsdStopPolling(void)
1897 int i
; /* Looping var */
1898 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1903 cupsdStatBufDelete(PollStatusBuffer
);
1906 cupsdRemoveSelect(PollPipe
);
1909 PollStatusBuffer
= NULL
;
1912 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1914 cupsdEndProcess(pollp
->pid
, 0);
1920 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1924 cupsdUpdateDNSSDName(void)
1926 DNSServiceErrorType error
; /* Error from service creation */
1927 char webif
[1024]; /* Web interface share name */
1928 # ifdef HAVE_SYSTEMCONFIGURATION
1929 SCDynamicStoreRef sc
; /* Context for dynamic store */
1930 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1931 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1932 CFStringRef nameRef
; /* Host name CFString */
1933 char nameBuffer
[1024]; /* C-string buffer */
1934 # endif /* HAVE_SYSTEMCONFIGURATION */
1938 * Only share the web interface and printers when non-local listening is
1947 * Get the computer name as a c-string...
1950 # ifdef HAVE_SYSTEMCONFIGURATION
1951 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1956 * Get the computer name from the dynamic store...
1959 cupsdClearString(&DNSSDComputerName
);
1961 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1963 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1964 kCFStringEncodingUTF8
))
1966 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1967 "Dynamic store computer name is \"%s\".", nameBuffer
);
1968 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1974 if (!DNSSDComputerName
)
1977 * Use the ServerName instead...
1980 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1981 "Using ServerName \"%s\" as computer name.", ServerName
);
1982 cupsdSetString(&DNSSDComputerName
, ServerName
);
1986 * Get the local hostname from the dynamic store...
1989 cupsdClearString(&DNSSDHostName
);
1991 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1993 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1994 kCFStringEncodingUTF8
))
1996 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1997 "Dynamic store host name is \"%s\".", nameBuffer
);
1998 cupsdSetString(&DNSSDHostName
, nameBuffer
);
2007 * Use the ServerName instead...
2010 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2011 "Using ServerName \"%s\" as host name.", ServerName
);
2012 cupsdSetString(&DNSSDHostName
, ServerName
);
2016 * Get any Back-to-My-Mac domains and add them as aliases...
2019 cupsdFreeAliases(DNSSDAlias
);
2022 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
2023 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
2025 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
2026 (int)CFDictionaryGetCount(btmm
));
2027 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
2030 cupsdLogMessage(CUPSD_LOG_ERROR
,
2031 "Bad Back to My Mac data in dynamic store!");
2033 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
2041 # endif /* HAVE_SYSTEMCONFIGURATION */
2043 cupsdSetString(&DNSSDComputerName
, ServerName
);
2044 cupsdSetString(&DNSSDHostName
, ServerName
);
2048 * Then (re)register the web interface if enabled...
2053 if (DNSSDComputerName
)
2054 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
2056 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2059 DNSServiceRefDeallocate(WebIFRef
);
2061 WebIFRef
= DNSSDRef
;
2062 if ((error
= DNSServiceRegister(&WebIFRef
,
2063 kDNSServiceFlagsShareConnection
,
2064 0, webif
, "_http._tcp", NULL
,
2065 NULL
, htons(DNSSDPort
), 7,
2066 "\006path=/", dnssdRegisterCallback
,
2067 NULL
)) != kDNSServiceErr_NoError
)
2068 cupsdLogMessage(CUPSD_LOG_ERROR
,
2069 "DNS-SD web interface registration failed: %d", error
);
2072 #endif /* HAVE_DNSSD */
2077 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2081 cupsdUpdateLDAPBrowse(void)
2083 char uri
[HTTP_MAX_URI
], /* Printer URI */
2084 host
[HTTP_MAX_URI
], /* Hostname */
2085 resource
[HTTP_MAX_URI
], /* Resource path */
2086 location
[1024], /* Printer location */
2087 info
[1024], /* Printer information */
2088 make_model
[1024], /* Printer make and model */
2089 type_num
[30]; /* Printer type number */
2090 int type
; /* Printer type */
2091 int rc
; /* LDAP status */
2092 int limit
; /* Size limit */
2093 LDAPMessage
*res
, /* LDAP search results */
2094 *e
; /* Current entry from search */
2096 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2098 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2101 * Reconnect if LDAP Handle is invalid...
2104 if (! BrowseLDAPHandle
)
2111 * Search for cups printers in LDAP directory...
2114 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2115 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2118 * If ldap search was successfull then exit function
2119 * and temporary disable LDAP updates...
2122 if (rc
!= LDAP_SUCCESS
)
2124 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2126 BrowseLDAPUpdate
= FALSE
;
2127 cupsdLogMessage(CUPSD_LOG_INFO
,
2128 "LDAP update temporary disabled");
2134 * If LDAP updates were disabled, we will reenable them...
2137 if (! BrowseLDAPUpdate
)
2139 BrowseLDAPUpdate
= TRUE
;
2140 cupsdLogMessage(CUPSD_LOG_INFO
,
2141 "LDAP update enabled");
2145 * Count LDAP entries and return if no entry exist...
2148 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2149 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2157 * Loop through the available printers...
2160 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2162 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2165 * Get the required values from this entry...
2168 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2169 "printerDescription", info
, sizeof(info
)) == -1)
2172 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2173 "printerLocation", location
, sizeof(location
)) == -1)
2176 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2177 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2180 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2181 "printerType", type_num
, sizeof(type_num
)) == -1)
2184 type
= atoi(type_num
);
2186 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2187 "printerURI", uri
, sizeof(uri
)) == -1)
2191 * Process the entry as browse data...
2194 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2195 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2196 location
, info
, make_model
, 0, NULL
);
2202 #endif /* HAVE_LDAP */
2207 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2211 cupsdUpdateSLPBrowse(void)
2213 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2214 *next
; /* Next service in list */
2215 cupsd_printer_t p
; /* Printer information */
2216 const char *uri
; /* Pointer to printer URI */
2217 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2218 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2222 * Reset the refresh time...
2225 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2228 * Poll for remote printers using SLP...
2233 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2234 slp_url_callback
, &s
);
2237 * Loop through the list of available printers...
2243 * Save the "next" pointer...
2249 * Load a cupsd_printer_t structure with the SLP service attributes...
2252 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2255 * Process this printer entry...
2258 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2260 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2263 * Pull the URI apart to see if this is a local or remote printer...
2266 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2267 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2268 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2272 * Free this listing...
2275 cupsdClearString(&p
.info
);
2276 cupsdClearString(&p
.location
);
2277 cupsdClearString(&p
.make_model
);
2282 #endif /* HAVE_LIBSLP */
2286 * 'dequote()' - Remote quotes from a string.
2289 static char * /* O - Dequoted string */
2290 dequote(char *d
, /* I - Destination string */
2291 const char *s
, /* I - Source string */
2292 int dlen
) /* I - Destination length */
2294 char *dptr
; /* Pointer into destination */
2299 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2316 # ifdef HAVE_COREFOUNDATION
2318 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2322 dnssdAddAlias(const void *key
, /* I - Key */
2323 const void *value
, /* I - Value (domain) */
2324 void *context
) /* I - Unused */
2326 char valueStr
[1024], /* Domain string */
2327 hostname
[1024]; /* Complete hostname */
2332 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2333 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2334 kCFStringEncodingUTF8
))
2336 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
2338 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2340 cupsdAddAlias(DNSSDAlias
, hostname
);
2341 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2345 cupsdLogMessage(CUPSD_LOG_ERROR
,
2346 "Bad Back to My Mac domain in dynamic store!");
2348 # endif /* HAVE_COREFOUNDATION */
2352 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2355 static char * /* O - TXT record */
2356 dnssdBuildTxtRecord(
2357 int *txt_len
, /* O - TXT record length */
2358 cupsd_printer_t
*p
, /* I - Printer information */
2359 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2361 int i
; /* Looping var */
2362 char admin_hostname
[256], /* .local hostname for admin page */
2363 adminurl_str
[256], /* URL for the admin page */
2364 type_str
[32], /* Type to string buffer */
2365 state_str
[32], /* State to string buffer */
2366 rp_str
[1024], /* Queue name string buffer */
2367 air_str
[1024], /* auth-info-required string buffer */
2368 *keyvalue
[32][2]; /* Table of key/value pairs */
2372 * Load up the key value pairs...
2377 keyvalue
[i
][0] = "txtvers";
2378 keyvalue
[i
++][1] = "1";
2380 keyvalue
[i
][0] = "qtotal";
2381 keyvalue
[i
++][1] = "1";
2383 keyvalue
[i
][0] = "rp";
2384 keyvalue
[i
++][1] = rp_str
;
2386 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2388 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2389 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2391 keyvalue
[i
][0] = "ty";
2392 keyvalue
[i
++][1] = p
->make_model
? p
->make_model
: "Unknown";
2394 snprintf(admin_hostname
, sizeof(admin_hostname
), "%s.local.", DNSSDHostName
);
2395 httpAssembleURIf(HTTP_URI_CODING_ALL
, adminurl_str
, sizeof(adminurl_str
),
2396 "http", NULL
, admin_hostname
, DNSSDPort
, "/%s/%s",
2397 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers",
2399 keyvalue
[i
][0] = "adminurl";
2400 keyvalue
[i
++][1] = adminurl_str
;
2402 keyvalue
[i
][0] = "note";
2403 keyvalue
[i
++][1] = p
->location
? p
->location
: "";
2405 keyvalue
[i
][0] = "priority";
2406 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2408 keyvalue
[i
][0] = "product";
2409 keyvalue
[i
++][1] = p
->pc
&& p
->pc
->product
? p
->pc
->product
: "Unknown";
2411 keyvalue
[i
][0] = "pdl";
2412 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2414 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
2416 keyvalue
[i
][0] = "air";
2417 keyvalue
[i
++][1] = air_str
;
2420 keyvalue
[i
][0] = "UUID";
2421 keyvalue
[i
++][1] = p
->uuid
+ 9;
2424 keyvalue
[i
][0] = "TLS";
2425 keyvalue
[i
++][1] = "1.2";
2426 #endif /* HAVE_SSL */
2428 keyvalue
[i
][0] = "Transparent";
2429 keyvalue
[i
++][1] = "F";
2431 keyvalue
[i
][0] = "Binary";
2432 keyvalue
[i
++][1] = "F";
2434 keyvalue
[i
][0] = "Fax";
2435 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_FAX
) ? "T" : "F";
2437 keyvalue
[i
][0] = "Color";
2438 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_COLOR
) ? "T" : "F";
2440 keyvalue
[i
][0] = "Duplex";
2441 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_DUPLEX
) ? "T" : "F";
2443 keyvalue
[i
][0] = "Staple";
2444 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_STAPLE
) ? "T" : "F";
2446 keyvalue
[i
][0] = "Copies";
2447 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_COPIES
) ? "T" : "F";
2449 keyvalue
[i
][0] = "Collate";
2450 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_COLLATE
) ? "T" : "F";
2452 keyvalue
[i
][0] = "Punch";
2453 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_PUNCH
) ? "T" : "F";
2455 keyvalue
[i
][0] = "Bind";
2456 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_BIND
) ? "T" : "F";
2458 keyvalue
[i
][0] = "Sort";
2459 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_SORT
) ? "T" : "F";
2461 keyvalue
[i
][0] = "Scan";
2462 keyvalue
[i
++][1] = (p
->type
& CUPS_PRINTER_MFP
) ? "T" : "F";
2464 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2465 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2467 keyvalue
[i
][0] = "printer-state";
2468 keyvalue
[i
++][1] = state_str
;
2470 keyvalue
[i
][0] = "printer-type";
2471 keyvalue
[i
++][1] = type_str
;
2474 * Then pack them into a proper txt record...
2477 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2482 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2485 static int /* O - Result of comparison */
2486 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2487 cupsd_printer_t
*b
)/* I - Second printer */
2489 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2494 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2499 dnssdDeregisterPrinter(
2500 cupsd_printer_t
*p
) /* I - Printer */
2502 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2505 * Closing the socket deregisters the service
2510 DNSServiceRefDeallocate(p
->ipp_ref
);
2517 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2526 DNSServiceRefDeallocate(p
->printer_ref
);
2527 p
->printer_ref
= NULL
;
2533 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2536 free(p
->printer_txt
);
2537 p
->printer_txt
= NULL
;
2541 * Remove the printer from the array of DNS-SD printers, then clear the
2542 * registered name...
2545 cupsArrayRemove(DNSSDPrinters
, p
);
2546 cupsdClearString(&p
->reg_name
);
2551 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2552 * TXT record format.
2555 static char * /* O - TXT record */
2556 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2557 char *keyvalue
[][2], /* I - Table of key value pairs */
2558 int count
) /* I - Items in table */
2560 int i
; /* Looping var */
2561 int length
; /* Length of TXT record */
2562 int length2
; /* Length of value */
2563 char *txtRecord
; /* TXT record buffer */
2564 char *cursor
; /* Looping pointer */
2568 * Calculate the buffer size
2574 for (length
= i
= 0; i
< count
; i
++)
2575 length
+= 1 + strlen(keyvalue
[i
][0]) +
2576 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2579 * Allocate and fill it
2582 txtRecord
= malloc(length
);
2587 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2590 * Drop in the p-string style length byte followed by the data
2593 length
= strlen(keyvalue
[i
][0]);
2594 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2596 *cursor
++ = (unsigned char)(length
+ length2
);
2598 memcpy(cursor
, keyvalue
[i
][0], length
);
2605 memcpy(cursor
, keyvalue
[i
][1], length2
);
2616 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2620 dnssdRegisterCallback(
2621 DNSServiceRef sdRef
, /* I - DNS Service reference */
2622 DNSServiceFlags flags
, /* I - Reserved for future use */
2623 DNSServiceErrorType errorCode
, /* I - Error code */
2624 const char *name
, /* I - Service name */
2625 const char *regtype
, /* I - Service type */
2626 const char *domain
, /* I - Domain. ".local" for now */
2627 void *context
) /* I - User-defined context */
2629 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2630 /* Current printer */
2633 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2634 name
, regtype
, p
? p
->name
: "Web Interface",
2635 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2639 cupsdLogMessage(CUPSD_LOG_ERROR
,
2640 "DNSServiceRegister failed with error %d", (int)errorCode
);
2643 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2645 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2648 cupsArrayRemove(DNSSDPrinters
, p
);
2649 cupsdSetString(&p
->reg_name
, name
);
2650 cupsArrayAdd(DNSSDPrinters
, p
);
2652 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2658 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2659 * or update the broadcast contents.
2663 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2665 DNSServiceErrorType se
; /* dnssd errors */
2666 char *ipp_txt
, /* IPP TXT record buffer */
2667 *printer_txt
, /* LPD TXT record buffer */
2668 name
[1024], /* Service name */
2669 *nameptr
; /* Pointer into name */
2670 int ipp_len
, /* IPP TXT record length */
2671 printer_len
, /* LPD TXT record length */
2672 printer_port
; /* LPD port number */
2673 const char *regtype
; /* Registration type */
2676 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2677 !p
->ipp_ref
? "new" : "update");
2680 * If per-printer sharing was just disabled make sure we're not
2681 * registered before returning.
2686 dnssdDeregisterPrinter(p
);
2691 * The registered name takes the form of "<printer-info> @ <computer name>"...
2694 if (p
->info
&& strlen(p
->info
) > 0)
2696 if (DNSSDComputerName
)
2697 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
2699 strlcpy(name
, p
->info
, sizeof(name
));
2701 else if (DNSSDComputerName
)
2702 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
2704 strlcpy(name
, p
->name
, sizeof(name
));
2707 * If an existing printer was renamed, unregister it and start over...
2710 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2711 dnssdDeregisterPrinter(p
);
2715 cupsdSetString(&p
->reg_name
, name
);
2716 cupsArrayAdd(DNSSDPrinters
, p
);
2720 * Register IPP and (optionally) LPD...
2723 ipp_len
= 0; /* anti-compiler-warning-code */
2724 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2727 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2730 * Update the existing registration...
2733 /* A TTL of 0 means use record's original value (Radar 3176248) */
2734 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2735 0)) == kDNSServiceErr_NoError
)
2740 p
->ipp_txt
= ipp_txt
;
2741 p
->ipp_len
= ipp_len
;
2747 * Failed to update record, lets close this reference and move on...
2750 cupsdLogMessage(CUPSD_LOG_ERROR
,
2751 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2754 DNSServiceRefDeallocate(p
->ipp_ref
);
2762 * Initial registration. Use the _fax-ipp regtype for fax queues...
2765 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" : DNSSDRegType
;
2767 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2768 "Registering DNS-SD printer %s with name \"%s\" and "
2769 "type \"%s\"", p
->name
, name
, regtype
);
2772 * Register the queue, dropping characters as needed until we succeed...
2775 nameptr
= name
+ strlen(name
);
2779 p
->ipp_ref
= DNSSDRef
;
2780 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2781 0, name
, regtype
, NULL
, NULL
,
2782 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2783 dnssdRegisterCallback
,
2784 p
)) == kDNSServiceErr_BadParam
)
2787 * Name is too long, drop trailing characters, taking into account
2793 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2800 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2802 if (se
== kDNSServiceErr_NoError
)
2804 p
->ipp_txt
= ipp_txt
;
2805 p
->ipp_len
= ipp_len
;
2809 cupsdLogMessage(CUPSD_LOG_WARN
,
2810 "DNS-SD IPP registration of \"%s\" failed: %d",
2817 if (BrowseLocalProtocols
& BROWSE_LPD
)
2819 printer_len
= 0; /* anti-compiler-warning-code */
2821 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2830 if (p
->printer_ref
&&
2831 (printer_len
!= p
->printer_len
||
2832 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2835 * Update the existing registration...
2838 /* A TTL of 0 means use record's original value (Radar 3176248) */
2839 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2841 0)) == kDNSServiceErr_NoError
)
2844 free(p
->printer_txt
);
2846 p
->printer_txt
= printer_txt
;
2847 p
->printer_len
= printer_len
;
2853 * Failed to update record, lets close this reference and move on...
2856 cupsdLogMessage(CUPSD_LOG_ERROR
,
2857 "Unable to update LPD DNS-SD record for %s - %d",
2860 DNSServiceRefDeallocate(p
->printer_ref
);
2861 p
->printer_ref
= NULL
;
2865 if (!p
->printer_ref
)
2868 * Initial registration...
2871 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2872 "Registering DNS-SD printer %s with name \"%s\" and "
2873 "type \"_printer._tcp\"", p
->name
, name
);
2875 p
->printer_ref
= DNSSDRef
;
2876 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2877 kDNSServiceFlagsShareConnection
,
2878 0, name
, "_printer._tcp", NULL
, NULL
,
2879 htons(printer_port
), printer_len
, printer_txt
,
2880 dnssdRegisterCallback
,
2881 p
)) == kDNSServiceErr_NoError
)
2883 p
->printer_txt
= printer_txt
;
2884 p
->printer_len
= printer_len
;
2888 cupsdLogMessage(CUPSD_LOG_WARN
,
2889 "DNS-SD LPD registration of \"%s\" failed: %d",
2899 * 'dnssdStop()' - Stop all DNS-SD registrations.
2905 cupsd_printer_t
*p
; /* Current printer */
2909 * De-register the individual printers
2912 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2914 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2915 dnssdDeregisterPrinter(p
);
2918 * Shutdown the rest of the service refs...
2923 DNSServiceRefDeallocate(WebIFRef
);
2929 DNSServiceRefDeallocate(RemoteRef
);
2933 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2935 DNSServiceRefDeallocate(DNSSDRef
);
2938 cupsArrayDelete(DNSSDPrinters
);
2939 DNSSDPrinters
= NULL
;
2946 * 'dnssdUpdate()' - Handle DNS-SD queries.
2952 DNSServiceErrorType sdErr
; /* Service discovery error */
2955 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2957 cupsdLogMessage(CUPSD_LOG_ERROR
,
2958 "DNS Service Discovery registration error %d!",
2963 #endif /* HAVE_DNSSD */
2967 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2970 static char * /* O - String or NULL if none */
2971 get_auth_info_required(
2972 cupsd_printer_t
*p
, /* I - Printer */
2973 char *buffer
, /* I - Value buffer */
2974 size_t bufsize
) /* I - Size of value buffer */
2976 cupsd_location_t
*auth
; /* Pointer to authentication element */
2977 char resource
[1024]; /* Printer/class resource path */
2981 * If auth-info-required is set for this printer, return that...
2984 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2986 int i
; /* Looping var */
2987 char *bufptr
; /* Pointer into buffer */
2989 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
2991 if (bufptr
>= (buffer
+ bufsize
- 2))
2997 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
2998 bufptr
+= strlen(bufptr
);
3005 * Figure out the authentication data requirements to advertise...
3008 if (p
->type
& CUPS_PRINTER_CLASS
)
3009 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
3011 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
3013 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
3014 auth
->type
== CUPSD_AUTH_NONE
)
3015 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
3019 int auth_type
; /* Authentication type */
3021 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
3022 auth_type
= DefaultAuthType
;
3026 case CUPSD_AUTH_NONE
:
3029 case CUPSD_AUTH_NEGOTIATE
:
3030 strlcpy(buffer
, "negotiate", bufsize
);
3034 strlcpy(buffer
, "username,password", bufsize
);
3047 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
3050 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
3051 get_hostconfig(const char *name
) /* I - Name of service */
3053 cups_file_t
*fp
; /* Hostconfig file */
3054 char line
[1024], /* Line from file */
3055 *ptr
; /* Pointer to value */
3056 int state
= 1; /* State of service */
3060 * Try opening the /etc/hostconfig file; if we can't open it, assume that
3061 * the service is enabled/auto.
3064 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
3067 * Read lines from the file until we find the service...
3070 while (cupsFileGets(fp
, line
, sizeof(line
)))
3072 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
3077 if (!strcasecmp(line
, name
))
3080 * Found the service, see if it is set to "-NO-"...
3083 if (!strncasecmp(ptr
, "-NO-", 4))
3094 #endif /* __APPLE__ */
3098 * 'is_local_queue()' - Determine whether the URI points at a local queue.
3101 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
3102 is_local_queue(const char *uri
, /* I - Printer URI */
3103 char *host
, /* O - Host string */
3104 int hostlen
, /* I - Length of host buffer */
3105 char *resource
, /* O - Resource string */
3106 int resourcelen
) /* I - Length of resource buffer */
3108 char scheme
[32], /* Scheme portion of URI */
3109 username
[HTTP_MAX_URI
]; /* Username portion of URI */
3110 int port
; /* Port portion of URI */
3111 cupsd_netif_t
*iface
; /* Network interface */
3115 * Pull the URI apart to see if this is a local or remote printer...
3118 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3119 username
, sizeof(username
), host
, hostlen
, &port
,
3120 resource
, resourcelen
) < HTTP_URI_OK
)
3123 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3126 * Check for local server addresses...
3129 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3134 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3136 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3137 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3141 * If we get here, the printer is remote...
3149 * 'process_browse_data()' - Process new browse data.
3153 process_browse_data(
3154 const char *uri
, /* I - URI of printer/class */
3155 const char *host
, /* I - Hostname */
3156 const char *resource
, /* I - Resource path */
3157 cups_ptype_t type
, /* I - Printer type */
3158 ipp_pstate_t state
, /* I - Printer state */
3159 const char *location
, /* I - Printer location */
3160 const char *info
, /* I - Printer information */
3161 const char *make_model
, /* I - Printer make and model */
3162 int num_attrs
, /* I - Number of attributes */
3163 cups_option_t
*attrs
) /* I - Attributes */
3165 int i
; /* Looping var */
3166 int update
; /* Update printer attributes? */
3167 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3168 name
[IPP_MAX_NAME
], /* Name of printer */
3169 newname
[IPP_MAX_NAME
], /* New name of printer */
3170 *hptr
, /* Pointer into hostname */
3171 *sptr
; /* Pointer into ServerName */
3172 const char *shortname
; /* Short queue name (queue) */
3173 char local_make_model
[IPP_MAX_NAME
];
3174 /* Local make and model */
3175 cupsd_printer_t
*p
; /* Printer information */
3176 const char *ipp_options
, /* ipp-options value */
3177 *lease_duration
, /* lease-duration value */
3178 *uuid
; /* uuid value */
3179 int is_class
; /* Is this queue a class? */
3182 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3183 "process_browse_data(uri=\"%s\", host=\"%s\", "
3184 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3185 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3186 uri
, host
, resource
, type
, state
,
3187 location
? location
: "(nil)", info
? info
: "(nil)",
3188 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3191 * Determine if the URI contains any illegal characters in it...
3194 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3195 (strncmp(resource
, "/printers/", 10) &&
3196 strncmp(resource
, "/classes/", 9)))
3198 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3202 if (strchr(resource
, '?') ||
3203 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3204 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3206 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3212 * OK, this isn't a local printer; add any remote options...
3215 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3217 if (BrowseRemoteOptions
)
3219 if (BrowseRemoteOptions
[0] == '?')
3222 * Override server-supplied options...
3225 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3227 else if (ipp_options
)
3230 * Combine the server and local options...
3233 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3234 BrowseRemoteOptions
);
3239 * Just use the local options...
3242 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3247 else if (ipp_options
)
3250 * Just use the server-supplied options...
3253 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3258 * See if we already have it listed in the Printers list, and add it if not...
3261 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3262 type
&= ~CUPS_PRINTER_IMPLICIT
;
3264 hptr
= strchr(host
, '.');
3265 sptr
= strchr(ServerName
, '.');
3266 is_class
= type
& CUPS_PRINTER_CLASS
;
3267 uuid
= cupsGetOption("uuid", num_attrs
, attrs
);
3269 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3272 * Strip the common domain name components...
3275 while (hptr
!= NULL
)
3277 if (!strcasecmp(hptr
, sptr
))
3283 hptr
= strchr(hptr
+ 1, '.');
3290 * Remote destination is a class...
3293 if (!strncmp(resource
, "/classes/", 9))
3294 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3298 shortname
= resource
+ 9;
3303 * Remote destination is a printer...
3306 if (!strncmp(resource
, "/printers/", 10))
3307 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3311 shortname
= resource
+ 10;
3315 *hptr
= '.'; /* Resource FQDN */
3317 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3320 * Long name doesn't exist, try short name...
3323 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3326 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3329 * Short name doesn't exist, use it for this shared queue.
3332 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3334 strlcpy(name
, shortname
, sizeof(name
));
3339 * Short name exists...
3342 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3343 "process_browse_data: %s found, type=%x, hostname=%s...",
3344 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3346 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3347 p
= NULL
; /* Don't replace implicit classes */
3348 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3351 * Short name exists but is for a different host. If this is a remote
3352 * queue, rename it and use the long name...
3355 if (p
->type
& CUPS_PRINTER_REMOTE
)
3357 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3358 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3359 is_class
? "class" : "printer", p
->name
, p
->name
,
3361 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3362 "%s \'%s\' deleted by directory services.",
3363 is_class
? "Class" : "Printer", p
->name
);
3365 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3366 cupsdRenamePrinter(p
, newname
);
3368 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3369 "%s \'%s\' added by directory services.",
3370 is_class
? "Class" : "Printer", p
->name
);
3374 * Force creation with long name...
3382 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3383 "process_browse_data: %s found, type=%x, hostname=%s...",
3384 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3389 * Queue doesn't exist; add it...
3393 p
= cupsdAddClass(name
);
3395 p
= cupsdAddPrinter(name
);
3400 cupsdClearString(&(p
->hostname
));
3402 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3403 is_class
? "class" : "printer", name
);
3405 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3406 "%s \'%s\' added by directory services.",
3407 is_class
? "Class" : "Printer", name
);
3410 * Force the URI to point to the real server...
3413 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3416 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3422 * Hostname not set, so this must be a cached remote printer
3423 * that was created for a pending print job...
3426 cupsdSetString(&p
->hostname
, host
);
3427 cupsdSetString(&p
->uri
, uri
);
3428 cupsdSetString(&p
->device_uri
, uri
);
3431 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3435 * Update the state...
3439 p
->browse_time
= time(NULL
);
3441 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3445 * Grab the lease-duration for the browse data; anything less then 1
3446 * second or more than 1 week gets the default BrowseTimeout...
3449 i
= atoi(lease_duration
);
3450 if (i
< 1 || i
> 604800)
3453 p
->browse_expire
= p
->browse_time
+ i
;
3456 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3458 if (type
& CUPS_PRINTER_REJECTING
)
3460 type
&= ~CUPS_PRINTER_REJECTING
;
3468 else if (!p
->accepting
)
3474 if (p
->type
!= type
)
3480 if (uuid
&& strcmp(p
->uuid
, uuid
))
3482 cupsdSetString(&p
->uuid
, uuid
);
3486 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3488 cupsdSetString(&p
->location
, location
);
3492 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3494 cupsdSetString(&p
->info
, info
);
3497 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3500 if (!make_model
|| !make_model
[0])
3503 snprintf(local_make_model
, sizeof(local_make_model
),
3504 "Remote Class on %s", host
);
3506 snprintf(local_make_model
, sizeof(local_make_model
),
3507 "Remote Printer on %s", host
);
3510 snprintf(local_make_model
, sizeof(local_make_model
),
3511 "%s on %s", make_model
, host
);
3513 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3515 cupsdSetString(&p
->make_model
, local_make_model
);
3521 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3524 * See if we need to update the attributes...
3527 if (p
->num_options
!= num_attrs
)
3531 for (i
= 0; i
< num_attrs
; i
++)
3532 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3533 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3534 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3543 * Free the old options...
3546 cupsFreeOptions(p
->num_options
, p
->options
);
3549 p
->num_options
= num_attrs
;
3552 if (type
& CUPS_PRINTER_DELETE
)
3554 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3555 "%s \'%s\' deleted by directory services.",
3556 is_class
? "Class" : "Printer", p
->name
);
3558 cupsdExpireSubscriptions(p
, NULL
);
3560 cupsdDeletePrinter(p
, 1);
3561 cupsdUpdateImplicitClasses();
3562 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3566 cupsdSetPrinterAttrs(p
);
3567 cupsdUpdateImplicitClasses();
3571 * See if we have a default printer... If not, make the first network
3572 * default printer the default.
3575 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3578 * Find the first network default printer and use it...
3581 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3583 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3584 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3587 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3593 * Do auto-classing if needed...
3596 process_implicit_classes();
3601 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3605 process_implicit_classes(void)
3607 int i
; /* Looping var */
3608 int update
; /* Update printer attributes? */
3609 char name
[IPP_MAX_NAME
], /* Name of printer */
3610 *hptr
; /* Pointer into hostname */
3611 cupsd_printer_t
*p
, /* Printer information */
3612 *pclass
, /* Printer class */
3613 *first
; /* First printer in class */
3614 int offset
, /* Offset of name */
3615 len
; /* Length of name */
3618 if (!ImplicitClasses
|| !Printers
)
3622 * Loop through all available printers and create classes as needed...
3625 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3626 update
= 0, pclass
= NULL
, first
= NULL
;
3628 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3631 * Skip implicit classes...
3634 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3641 * If len == 0, get the length of this printer name up to the "@"
3645 cupsArraySave(Printers
);
3648 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3649 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3652 * We have more than one printer with the same name; see if
3653 * we have a class, and if this printer is a member...
3656 if (pclass
&& strcasecmp(pclass
->name
, name
))
3659 cupsdSetPrinterAttrs(pclass
);
3665 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3668 * Need to add the class...
3671 pclass
= cupsdAddPrinter(name
);
3672 cupsArrayAdd(ImplicitPrinters
, pclass
);
3674 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3675 pclass
->accepting
= 1;
3676 pclass
->state
= IPP_PRINTER_IDLE
;
3678 cupsdSetString(&pclass
->location
, p
->location
);
3679 cupsdSetString(&pclass
->info
, p
->info
);
3681 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3682 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3686 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3688 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3690 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3691 "Implicit class \'%s\' added by directory services.",
3697 for (i
= 0; i
< pclass
->num_printers
; i
++)
3698 if (pclass
->printers
[i
] == first
)
3701 if (i
>= pclass
->num_printers
)
3703 first
->in_implicit_class
= 1;
3704 cupsdAddPrinterToClass(pclass
, first
);
3710 for (i
= 0; i
< pclass
->num_printers
; i
++)
3711 if (pclass
->printers
[i
] == p
)
3714 if (i
>= pclass
->num_printers
)
3716 p
->in_implicit_class
= 1;
3717 cupsdAddPrinterToClass(pclass
, p
);
3724 * First time around; just get name length and mark it as first
3728 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3729 len
= hptr
- p
->name
;
3731 len
= strlen(p
->name
);
3733 if (len
>= sizeof(name
))
3736 * If the printer name length somehow is greater than we normally allow,
3737 * skip this printer...
3741 cupsArrayRestore(Printers
);
3745 strncpy(name
, p
->name
, len
);
3749 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3750 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3753 * Can't use same name as a local printer; add "Any" to the
3754 * front of the name, unless we have explicitly disabled
3755 * the "ImplicitAnyClasses"...
3758 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3761 * Add "Any" to the class name...
3764 strcpy(name
, "Any");
3765 strncpy(name
+ 3, p
->name
, len
);
3766 name
[len
+ 3] = '\0';
3772 * Don't create an implicit class if we have a local printer
3773 * with the same name...
3777 cupsArrayRestore(Printers
);
3785 cupsArrayRestore(Printers
);
3789 * Update the last printer class as needed...
3792 if (pclass
&& update
)
3793 cupsdSetPrinterAttrs(pclass
);
3798 * 'send_cups_browse()' - Send new browsing information using the CUPS
3803 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3805 int i
; /* Looping var */
3806 cups_ptype_t type
; /* Printer type */
3807 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3808 int bytes
; /* Length of packet */
3809 char packet
[1453], /* Browse data packet */
3810 uri
[1024], /* Printer URI */
3811 location
[1024], /* printer-location */
3812 info
[1024], /* printer-info */
3814 /* printer-make-and-model */
3815 air
[1024]; /* auth-info-required */
3816 cupsd_netif_t
*iface
; /* Network interface */
3820 * Figure out the printer type value...
3823 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3826 type
|= CUPS_PRINTER_REJECTING
;
3828 if (p
== DefaultPrinter
)
3829 type
|= CUPS_PRINTER_DEFAULT
;
3832 * Remove quotes from printer-info, printer-location, and
3833 * printer-make-and-model attributes...
3836 dequote(location
, p
->location
, sizeof(location
));
3837 dequote(info
, p
->info
, sizeof(info
));
3840 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3841 else if (p
->type
& CUPS_PRINTER_CLASS
)
3843 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3844 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3846 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3849 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3851 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3853 if (get_auth_info_required(p
, packet
, sizeof(packet
)))
3854 snprintf(air
, sizeof(air
), " auth-info-required=%s", packet
);
3859 * Send a packet to each browse address...
3862 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3866 * Send the browse packet to one or more interfaces...
3869 if (!strcmp(b
->iface
, "*"))
3872 * Send to all local interfaces...
3877 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3879 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3882 * Only send to local, IPv4 interfaces...
3885 if (!iface
->is_local
|| !iface
->port
||
3886 iface
->address
.addr
.sa_family
!= AF_INET
)
3889 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3890 iface
->hostname
, iface
->port
,
3891 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3894 snprintf(packet
, sizeof(packet
),
3895 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
3896 type
, p
->state
, uri
, location
, info
, make_model
,
3897 p
->browse_attrs
? p
->browse_attrs
: "", air
, p
->uuid
);
3899 bytes
= strlen(packet
);
3901 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3902 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3903 iface
->name
, packet
);
3905 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3907 sendto(BrowseSocket
, packet
, bytes
, 0,
3908 (struct sockaddr
*)&(iface
->broadcast
),
3909 httpAddrLength(&(iface
->broadcast
)));
3912 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3915 * Send to the named interface using the IPv4 address...
3919 if (strcmp(b
->iface
, iface
->name
))
3924 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3927 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3931 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3932 iface
->hostname
, iface
->port
,
3933 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3936 snprintf(packet
, sizeof(packet
),
3937 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
3938 type
, p
->state
, uri
, location
, info
, make_model
,
3939 p
->browse_attrs
? p
->browse_attrs
: "", air
, p
->uuid
);
3941 bytes
= strlen(packet
);
3943 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3944 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3945 iface
->name
, packet
);
3947 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3949 sendto(BrowseSocket
, packet
, bytes
, 0,
3950 (struct sockaddr
*)&(iface
->broadcast
),
3951 httpAddrLength(&(iface
->broadcast
)));
3958 * Send the browse packet to the indicated address using
3959 * the default server name...
3962 snprintf(packet
, sizeof(packet
),
3963 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
3964 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3965 p
->browse_attrs
? p
->browse_attrs
: "", air
, p
->uuid
);
3967 bytes
= strlen(packet
);
3968 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3969 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3971 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3972 (struct sockaddr
*)&(b
->to
),
3973 httpAddrLength(&(b
->to
))) <= 0)
3976 * Unable to send browse packet, so remove this address from the
3980 cupsdLogMessage(CUPSD_LOG_ERROR
,
3981 "cupsdSendBrowseList: sendto failed for browser "
3983 (int)(b
- Browsers
+ 1), strerror(errno
));
3986 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3997 * 'ldap_search_rec()' - LDAP Search with reconnect
4000 static int /* O - Return code */
4001 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
4002 char *base
, /* I - Base dn */
4003 int scope
, /* I - LDAP search scope */
4004 char *filter
, /* I - Filter string */
4005 char *attrs
[], /* I - Requested attributes */
4006 int attrsonly
, /* I - Return only attributes? */
4007 LDAPMessage
**res
) /* I - LDAP handler */
4009 int rc
; /* Return code */
4010 LDAP
*ldr
; /* LDAP handler after reconnect */
4013 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4014 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
4015 NULL
, LDAP_NO_LIMIT
, res
);
4017 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4018 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4021 * If we have a connection problem try again...
4024 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4026 cupsdLogMessage(CUPSD_LOG_ERROR
,
4027 "LDAP search failed with status %d: %s",
4028 rc
, ldap_err2string(rc
));
4029 cupsdLogMessage(CUPSD_LOG_INFO
,
4030 "We try the LDAP search once again after reconnecting to "
4033 ldr
= ldap_reconnect();
4035 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4036 rc
= ldap_search_ext_s(ldr
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
4037 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
4039 rc
= ldap_search_s(ldr
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4040 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4043 if (rc
== LDAP_NO_SUCH_OBJECT
)
4044 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4045 "ldap_search_rec: LDAP entry/object not found");
4046 else if (rc
!= LDAP_SUCCESS
)
4047 cupsdLogMessage(CUPSD_LOG_ERROR
,
4048 "ldap_search_rec: LDAP search failed with status %d: %s",
4049 rc
, ldap_err2string(rc
));
4051 if (rc
!= LDAP_SUCCESS
)
4059 * 'ldap_freeres()' - Free LDAPMessage
4063 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
4065 int rc
; /* Return value */
4068 rc
= ldap_msgfree(entry
);
4070 cupsdLogMessage(CUPSD_LOG_WARN
, "Can't free LDAPMessage!");
4072 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Freeing LDAPMessage was unnecessary");
4077 * 'ldap_getval_char()' - Get first LDAP value and convert to string
4080 static int /* O - Return code */
4081 ldap_getval_firststring(
4082 LDAP
*ld
, /* I - LDAP handler */
4083 LDAPMessage
*entry
, /* I - LDAP message or search result */
4084 char *attr
, /* I - the wanted attribute */
4085 char *retval
, /* O - String to return */
4086 unsigned long maxsize
) /* I - Max string size */
4088 char *dn
; /* LDAP DN */
4089 int rc
= 0; /* Return code */
4090 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4091 struct berval
**bval
; /* LDAP value array */
4092 unsigned long size
; /* String size */
4096 * Get value from LDAPMessage...
4099 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
4102 dn
= ldap_get_dn(ld
, entry
);
4103 cupsdLogMessage(CUPSD_LOG_WARN
,
4104 "Failed to get LDAP value %s for %s!",
4111 * Check size and copy value into our string...
4115 if (size
< (bval
[0]->bv_len
+ 1))
4118 dn
= ldap_get_dn(ld
, entry
);
4119 cupsdLogMessage(CUPSD_LOG_WARN
,
4120 "Attribute %s is too big! (dn: %s)",
4125 size
= bval
[0]->bv_len
+ 1;
4127 strlcpy(retval
, bval
[0]->bv_val
, size
);
4128 ldap_value_free_len(bval
);
4131 char **value
; /* LDAP value */
4134 * Get value from LDAPMessage...
4137 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4140 dn
= ldap_get_dn(ld
, entry
);
4141 cupsdLogMessage(CUPSD_LOG_WARN
, "Failed to get LDAP value %s for %s!",
4147 strlcpy(retval
, *value
, maxsize
);
4148 ldap_value_free(value
);
4150 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4157 * 'send_ldap_ou()' - Send LDAP ou registrations.
4161 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4162 char *basedn
, /* I - Our base dn */
4163 char *descstring
) /* I - Description for ou */
4165 int i
; /* Looping var... */
4166 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4167 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4168 LDAPMessage
*res
, /* Search result token */
4169 *e
; /* Current entry from search */
4170 int rc
; /* LDAP status */
4171 int rcmod
; /* LDAP status for modifications */
4172 char dn
[1024], /* DN of the organizational unit we are adding */
4173 *desc
[2], /* Change records */
4175 char old_desc
[1024]; /* Old description */
4176 static const char * const objectClass_values
[] =
4177 { /* The 2 objectClass's we use in */
4178 "top", /* our LDAP entries */
4179 "organizationalUnit",
4182 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4189 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4192 * Reconnect if LDAP Handle is invalid...
4195 if (!BrowseLDAPHandle
)
4197 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4198 "send_ldap_ou: LDAP Handle is invalid. Try reconnecting...");
4204 * Prepare ldap search...
4207 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4208 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4212 desc
[0] = descstring
;
4215 mods
[0].mod_type
= "ou";
4216 mods
[0].mod_values
= ou_value
;
4217 mods
[1].mod_type
= "description";
4218 mods
[1].mod_values
= desc
;
4219 mods
[2].mod_type
= "objectClass";
4220 mods
[2].mod_values
= (char **)objectClass_values
;
4222 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4223 (char **)ou_attrs
, 0, &res
);
4226 * If ldap search was not successfull then exit function...
4229 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4233 * Check if we need to insert or update the LDAP entry...
4236 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4237 rc
!= LDAP_NO_SUCH_OBJECT
)
4240 * Printserver has already been registered, check if
4241 * modification is required...
4244 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4247 * Get the required values from this entry...
4250 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4251 sizeof(old_desc
)) == -1)
4255 * Check if modification is required...
4258 if ( strcmp(desc
[0], old_desc
) == 0 )
4261 * LDAP entry for the printer exists.
4262 * Printer has already been registered,
4263 * no modifications required...
4265 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4266 "send_ldap_ou: No updates required for %s", ou
);
4271 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4272 "send_ldap_ou: Replace entry for %s", ou
);
4274 for (i
= 0; i
< 3; i
++)
4276 pmods
[i
] = mods
+ i
;
4277 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4281 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4282 if ((rcmod
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4283 NULL
)) != LDAP_SUCCESS
)
4285 if ((rcmod
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4286 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4288 cupsdLogMessage(CUPSD_LOG_ERROR
,
4289 "LDAP modify for %s failed with status %d: %s",
4290 ou
, rcmod
, ldap_err2string(rcmod
));
4291 if (rcmod
== LDAP_SERVER_DOWN
)
4299 * Printserver has never been registered,
4300 * add registration...
4303 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4304 "send_ldap_ou: Add entry for %s", ou
);
4306 for (i
= 0; i
< 3; i
++)
4308 pmods
[i
] = mods
+ i
;
4309 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4313 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4314 if ((rcmod
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4315 NULL
)) != LDAP_SUCCESS
)
4317 if ((rcmod
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4318 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4320 cupsdLogMessage(CUPSD_LOG_ERROR
,
4321 "LDAP add for %s failed with status %d: %s",
4322 ou
, rcmod
, ldap_err2string(rcmod
));
4323 if (rcmod
== LDAP_SERVER_DOWN
)
4328 if (rc
== LDAP_SUCCESS
)
4334 * 'send_ldap_browse()' - Send LDAP printer registrations.
4338 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4340 int i
; /* Looping var... */
4341 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4342 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4343 LDAPMessage
*res
, /* Search result token */
4344 *e
; /* Current entry from search */
4345 char *cn_value
[2], /* Change records */
4351 typestring
[255], /* String to hold printer-type */
4352 dn
[1024]; /* DN of the printer we are adding */
4353 int rc
; /* LDAP status */
4354 int rcmod
; /* LDAP status for modifications */
4355 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4356 old_location
[1024], /* Printer location */
4357 old_info
[1024], /* Printer information */
4358 old_make_model
[1024], /* Printer make and model */
4359 old_type_string
[30]; /* Temporary type number */
4360 int old_type
; /* Printer type */
4361 static const char * const objectClass_values
[] =
4362 { /* The 3 objectClass's we use in */
4363 "top", /* our LDAP entries */
4370 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4373 * Exit function if LDAP updates has been disabled...
4376 if (!BrowseLDAPUpdate
)
4378 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4379 "send_ldap_browse: Updates temporary disabled; "
4385 * Reconnect if LDAP Handle is invalid...
4388 if (!BrowseLDAPHandle
)
4390 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4391 "send_ldap_browse: LDAP Handle is invalid. Try "
4398 * Everything in ldap is ** so we fudge around it...
4401 sprintf(typestring
, "%u", p
->type
);
4403 cn_value
[0] = p
->name
;
4405 info
[0] = p
->info
? p
->info
: "Unknown";
4407 location
[0] = p
->location
? p
->location
: "Unknown";
4409 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4410 make_model
[1] = NULL
;
4411 type
[0] = typestring
;
4417 * Get ldap entry for printer ...
4420 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4422 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4424 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4425 (char **)ldap_attrs
, 0, &res
);
4428 * If ldap search was not successfull then exit function
4429 * and temporary disable LDAP updates...
4432 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4434 if (BrowseLDAPUpdate
&&
4435 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4437 BrowseLDAPUpdate
= FALSE
;
4438 cupsdLogMessage(CUPSD_LOG_INFO
,
4439 "LDAP update temporary disabled");
4446 * Fill modification array...
4449 mods
[0].mod_type
= "cn";
4450 mods
[0].mod_values
= cn_value
;
4451 mods
[1].mod_type
= "printerDescription";
4452 mods
[1].mod_values
= info
;
4453 mods
[2].mod_type
= "printerURI";
4454 mods
[2].mod_values
= uri
;
4455 mods
[3].mod_type
= "printerLocation";
4456 mods
[3].mod_values
= location
;
4457 mods
[4].mod_type
= "printerMakeAndModel";
4458 mods
[4].mod_values
= make_model
;
4459 mods
[5].mod_type
= "printerType";
4460 mods
[5].mod_values
= type
;
4461 mods
[6].mod_type
= "objectClass";
4462 mods
[6].mod_values
= (char **)objectClass_values
;
4465 * Check if we need to insert or update the LDAP entry...
4468 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4469 rc
!= LDAP_NO_SUCH_OBJECT
)
4472 * Printer has already been registered, check if
4473 * modification is required...
4476 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4479 * Get the required values from this entry...
4482 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4483 old_info
, sizeof(old_info
)) == -1)
4486 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4487 old_location
, sizeof(old_location
)) == -1)
4490 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4491 old_make_model
, sizeof(old_make_model
)) == -1)
4494 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4495 old_type_string
, sizeof(old_type_string
)) == -1)
4498 old_type
= atoi(old_type_string
);
4500 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4501 sizeof(old_uri
)) == -1)
4505 * Check if modification is required...
4508 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4509 !strcmp(location
[0], old_location
) &&
4510 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4513 * LDAP entry for the printer exists. Printer has already been registered,
4514 * no modifications required...
4517 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4518 "send_ldap_browse: No updates required for %s", p
->name
);
4523 * LDAP entry for the printer exists. Printer has already been registered,
4524 * modify the current registration...
4527 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4528 "send_ldap_browse: Replace entry for %s", p
->name
);
4530 for (i
= 0; i
< 7; i
++)
4532 pmods
[i
] = mods
+ i
;
4533 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4537 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4538 if ((rcmod
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4539 NULL
)) != LDAP_SUCCESS
)
4541 if ((rcmod
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4542 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4544 cupsdLogMessage(CUPSD_LOG_ERROR
,
4545 "LDAP modify for %s failed with status %d: %s",
4546 p
->name
, rcmod
, ldap_err2string(rcmod
));
4547 if (rcmod
== LDAP_SERVER_DOWN
)
4555 * No LDAP entry exists for the printer. Printer has never been registered,
4556 * add the current registration...
4559 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4561 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4562 "send_ldap_browse: Add entry for %s", p
->name
);
4564 for (i
= 0; i
< 7; i
++)
4566 pmods
[i
] = mods
+ i
;
4567 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4571 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4572 if ((rcmod
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4573 NULL
)) != LDAP_SUCCESS
)
4575 if ((rcmod
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4576 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4578 cupsdLogMessage(CUPSD_LOG_ERROR
,
4579 "LDAP add for %s failed with status %d: %s",
4580 p
->name
, rcmod
, ldap_err2string(rcmod
));
4581 if (rcmod
== LDAP_SERVER_DOWN
)
4586 if (rc
== LDAP_SUCCESS
)
4592 * 'ldap_dereg_printer()' - Delete printer from directory
4596 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4598 char dn
[1024]; /* DN of the printer */
4599 int rc
; /* LDAP status */
4602 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4606 * Reconnect if LDAP Handle is invalid...
4609 if (!BrowseLDAPHandle
)
4616 * Get dn for printer and delete LDAP entry...
4619 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4621 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4623 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4624 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4625 NULL
)) != LDAP_SUCCESS
)
4627 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4628 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4630 cupsdLogMessage(CUPSD_LOG_WARN
,
4631 "LDAP delete for %s failed with status %d: %s",
4632 p
->name
, rc
, ldap_err2string(rc
));
4635 * If we had a connection problem (connection timed out, etc.)
4636 * we should reconnect and try again to delete the entry...
4639 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4641 cupsdLogMessage(CUPSD_LOG_INFO
,
4642 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4645 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4646 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4647 NULL
)) != LDAP_SUCCESS
)
4649 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4650 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4651 cupsdLogMessage(CUPSD_LOG_WARN
,
4652 "LDAP delete for %s failed with status %d: %s",
4653 p
->name
, rc
, ldap_err2string(rc
));
4660 * 'ldap_dereg_ou()' - Remove the organizational unit.
4664 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4665 char *basedn
) /* I - Dase dn */
4667 char dn
[1024]; /* DN of the printer */
4668 int rc
; /* LDAP status */
4671 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4674 * Reconnect if LDAP Handle is invalid...
4677 if (!BrowseLDAPHandle
)
4684 * Get dn for printer and delete LDAP entry...
4687 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4688 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4690 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4691 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4692 NULL
)) != LDAP_SUCCESS
)
4694 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4695 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4697 cupsdLogMessage(CUPSD_LOG_WARN
,
4698 "LDAP delete for %s failed with status %d: %s",
4699 ou
, rc
, ldap_err2string(rc
));
4702 * If we had a connection problem (connection timed out, etc.)
4703 * we should reconnect and try again to delete the entry...
4706 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4708 cupsdLogMessage(CUPSD_LOG_INFO
,
4709 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4711 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4712 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4713 NULL
)) != LDAP_SUCCESS
)
4715 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4716 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4717 cupsdLogMessage(CUPSD_LOG_WARN
,
4718 "LDAP delete for %s failed with status %d: %s",
4719 ou
, rc
, ldap_err2string(rc
));
4723 #endif /* HAVE_LDAP */
4728 * 'send_slp_browse()' - Register the specified printer with SLP.
4732 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4734 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4735 attrs
[8192], /* Printer attributes */
4736 finishings
[1024], /* Finishings to support */
4737 make_model
[IPP_MAX_NAME
* 2],
4738 /* Make and model, quoted */
4739 location
[IPP_MAX_NAME
* 2],
4740 /* Location, quoted */
4741 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4742 *src
, /* Pointer to original string */
4743 *dst
; /* Pointer to destination string */
4744 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4745 SLPError error
; /* SLP error, if any */
4748 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4752 * Make the SLP service URL that conforms to the IANA
4753 * 'printer:' template.
4756 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4758 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4761 * Figure out the finishings string...
4764 if (p
->type
& CUPS_PRINTER_STAPLE
)
4765 strcpy(finishings
, "staple");
4767 finishings
[0] = '\0';
4769 if (p
->type
& CUPS_PRINTER_BIND
)
4772 strlcat(finishings
, ",bind", sizeof(finishings
));
4774 strcpy(finishings
, "bind");
4777 if (p
->type
& CUPS_PRINTER_PUNCH
)
4780 strlcat(finishings
, ",punch", sizeof(finishings
));
4782 strcpy(finishings
, "punch");
4785 if (p
->type
& CUPS_PRINTER_COVER
)
4788 strlcat(finishings
, ",cover", sizeof(finishings
));
4790 strcpy(finishings
, "cover");
4793 if (p
->type
& CUPS_PRINTER_SORT
)
4796 strlcat(finishings
, ",sort", sizeof(finishings
));
4798 strcpy(finishings
, "sort");
4802 strcpy(finishings
, "none");
4805 * Quote any commas in the make and model, location, and info strings...
4808 for (src
= p
->make_model
, dst
= make_model
;
4809 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4811 if (*src
== ',' || *src
== '\\' || *src
== ')')
4820 strcpy(make_model
, "Unknown");
4822 for (src
= p
->location
, dst
= location
;
4823 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4825 if (*src
== ',' || *src
== '\\' || *src
== ')')
4834 strcpy(location
, "Unknown");
4836 for (src
= p
->info
, dst
= info
;
4837 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4839 if (*src
== ',' || *src
== '\\' || *src
== ')')
4848 strcpy(info
, "Unknown");
4851 * Get the authentication value...
4854 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4858 * Make the SLP attribute string list that conforms to
4859 * the IANA 'printer:' template.
4862 snprintf(attrs
, sizeof(attrs
),
4863 "(printer-uri-supported=%s),"
4864 "(uri-authentication-supported=%s>),"
4866 "(uri-security-supported=tls>),"
4868 "(uri-security-supported=none>),"
4869 #endif /* HAVE_SSL */
4870 "(printer-name=%s),"
4871 "(printer-location=%s),"
4872 "(printer-info=%s),"
4873 "(printer-more-info=%s),"
4874 "(printer-make-and-model=%s),"
4875 "(printer-type=%d),"
4876 "(charset-supported=utf-8),"
4877 "(natural-language-configured=%s),"
4878 "(natural-language-supported=de,en,es,fr,it),"
4879 "(color-supported=%s),"
4880 "(finishings-supported=%s),"
4881 "(sides-supported=one-sided%s),"
4882 "(multiple-document-jobs-supported=true)"
4883 "(ipp-versions-supported=1.0,1.1)",
4884 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4885 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4886 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4888 p
->type
& CUPS_PRINTER_DUPLEX
?
4889 ",two-sided-long-edge,two-sided-short-edge" : "");
4891 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4894 * Register the printer with the SLP server...
4897 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4898 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4900 if (error
!= SLP_OK
)
4901 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4907 * 'slp_attr_callback()' - SLP attribute callback
4910 static SLPBoolean
/* O - SLP_TRUE for success */
4912 SLPHandle hslp
, /* I - SLP handle */
4913 const char *attrlist
, /* I - Attribute list */
4914 SLPError errcode
, /* I - Parsing status for this attr */
4915 void *cookie
) /* I - Current printer */
4917 char *tmp
= 0; /* Temporary string */
4918 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4919 /* Current printer */
4922 (void)hslp
; /* anti-compiler-warning-code */
4925 * Bail if there was an error
4928 if (errcode
!= SLP_OK
)
4932 * Parse the attrlist to obtain things needed to build CUPS browse packet
4935 memset(p
, 0, sizeof(cupsd_printer_t
));
4937 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4939 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4941 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4943 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4944 p
->type
= atoi(tmp
);
4946 p
->type
= CUPS_PRINTER_REMOTE
;
4948 cupsdClearString(&tmp
);
4955 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4959 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4961 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4964 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4966 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4969 * Make the SLP service URL that conforms to the IANA
4970 * 'printer:' template.
4973 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4976 * Deregister the printer...
4979 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4985 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4988 static int /* O - 0 on success */
4989 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4990 const char *tag
, /* I - Name of attribute */
4991 char **valbuf
) /* O - Value */
4993 char *ptr1
, /* Pointer into string */
4997 cupsdClearString(valbuf
);
4999 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
5001 ptr1
+= strlen(tag
);
5003 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
5009 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
5010 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
5013 * Dequote the value...
5016 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
5017 if (*ptr1
== '\\' && ptr1
[1])
5018 _cups_strcpy(ptr1
, ptr1
+ 1);
5029 * 'slp_reg_callback()' - Empty SLPRegReport.
5033 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
5034 SLPError errcode
, /* I - Error code, if any */
5035 void *cookie
) /* I - App data */
5046 * 'slp_url_callback()' - SLP service url callback
5049 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
5051 SLPHandle hslp
, /* I - SLP handle */
5052 const char *srvurl
, /* I - URL of service */
5053 unsigned short lifetime
, /* I - Life of service */
5054 SLPError errcode
, /* I - Existing error code */
5055 void *cookie
) /* I - Pointer to service list */
5057 slpsrvurl_t
*s
, /* New service entry */
5058 **head
; /* Pointer to head of entry */
5062 * Let the compiler know we won't be using these vars...
5069 * Bail if there was an error
5072 if (errcode
!= SLP_OK
)
5076 * Grab the head of the list...
5079 head
= (slpsrvurl_t
**)cookie
;
5082 * Allocate a *temporary* slpsrvurl_t to hold this entry.
5085 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
5089 * Copy the SLP service URL...
5092 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
5095 * Link the SLP service URL into the head of the list
5105 #endif /* HAVE_LIBSLP */
5109 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
5113 update_cups_browse(void)
5115 int i
; /* Looping var */
5116 int auth
; /* Authorization status */
5117 int len
; /* Length of name string */
5118 int bytes
; /* Number of bytes left */
5119 char packet
[1541], /* Broadcast packet */
5120 *pptr
; /* Pointer into packet */
5121 socklen_t srclen
; /* Length of source address */
5122 http_addr_t srcaddr
; /* Source address */
5123 char srcname
[1024]; /* Source hostname */
5124 unsigned address
[4]; /* Source address */
5125 unsigned type
; /* Printer type */
5126 unsigned state
; /* Printer state */
5127 char uri
[HTTP_MAX_URI
], /* Printer URI */
5128 host
[HTTP_MAX_URI
], /* Host portion of URI */
5129 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
5130 info
[IPP_MAX_NAME
], /* Information string */
5131 location
[IPP_MAX_NAME
], /* Location string */
5132 make_model
[IPP_MAX_NAME
];/* Make and model string */
5133 int num_attrs
; /* Number of attributes */
5134 cups_option_t
*attrs
; /* Attributes */
5138 * Read a packet from the browse socket...
5141 srclen
= sizeof(srcaddr
);
5142 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5143 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5146 * "Connection refused" is returned under Linux if the destination port
5147 * or address is unreachable from a previous sendto(); check for the
5148 * error here and ignore it for now...
5151 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5153 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5155 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5158 closesocket(BrowseSocket
);
5160 close(BrowseSocket
);
5163 cupsdRemoveSelect(BrowseSocket
);
5166 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5167 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5173 packet
[bytes
] = '\0';
5176 * If we're about to sleep, ignore incoming browse packets.
5183 * Figure out where it came from...
5187 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5189 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5190 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5191 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5192 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5195 #endif /* AF_INET6 */
5200 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5203 if (HostNameLookups
)
5204 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5206 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5208 len
= strlen(srcname
);
5216 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5219 * Access from localhost (127.0.0.1) is always allowed...
5222 auth
= CUPSD_AUTH_ALLOW
;
5227 * Do authorization checks on the domain/address...
5230 switch (BrowseACL
->order_type
)
5233 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5236 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5237 auth
= CUPSD_AUTH_ALLOW
;
5239 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->deny
))
5240 auth
= CUPSD_AUTH_DENY
;
5242 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->allow
))
5243 auth
= CUPSD_AUTH_ALLOW
;
5246 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5247 auth
= CUPSD_AUTH_DENY
;
5249 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->allow
))
5250 auth
= CUPSD_AUTH_ALLOW
;
5252 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->deny
))
5253 auth
= CUPSD_AUTH_DENY
;
5259 auth
= CUPSD_AUTH_ALLOW
;
5261 if (auth
== CUPSD_AUTH_DENY
)
5263 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5264 "update_cups_browse: Refused %d bytes from %s", bytes
,
5269 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5270 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5277 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5279 cupsdLogMessage(CUPSD_LOG_WARN
,
5280 "update_cups_browse: Garbled browse packet - %s", packet
);
5284 strcpy(location
, "Location Unknown");
5285 strcpy(info
, "No Information Available");
5286 make_model
[0] = '\0';
5290 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5293 * Have extended information; can't use sscanf for it because not all
5294 * sscanf's allow empty strings with %[^\"]...
5297 for (i
= 0, pptr
++;
5298 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5300 location
[i
] = *pptr
;
5308 while (*pptr
&& isspace(*pptr
& 255))
5313 for (i
= 0, pptr
++;
5314 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5323 while (*pptr
&& isspace(*pptr
& 255))
5328 for (i
= 0, pptr
++;
5329 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5331 make_model
[i
] = *pptr
;
5336 make_model
[i
] = '\0';
5339 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5345 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5346 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5347 type
, state
, uri
, location
, info
, make_model
));
5350 * Pull the URI apart to see if this is a local or remote printer...
5353 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5355 cupsFreeOptions(num_attrs
, attrs
);
5363 for (i
= 0; i
< NumRelays
; i
++)
5364 if (cupsdCheckAuth(address
, srcname
, len
, Relays
[i
].from
))
5365 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5366 (struct sockaddr
*)&(Relays
[i
].to
),
5367 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5369 cupsdLogMessage(CUPSD_LOG_ERROR
,
5370 "update_cups_browse: sendto failed for relay %d - %s.",
5371 i
+ 1, strerror(errno
));
5372 cupsFreeOptions(num_attrs
, attrs
);
5377 * Process the browse data...
5380 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5381 (ipp_pstate_t
)state
, location
, info
, make_model
,
5387 * 'update_lpd()' - Update the LPD configuration as needed.
5391 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5398 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5399 * setting for backwards-compatibility.
5402 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5404 #endif /* __APPLE__ */
5406 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5409 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5412 char newfile
[1024]; /* New cups-lpd.N file */
5413 cups_file_t
*ofp
, /* Original file pointer */
5414 *nfp
; /* New file pointer */
5415 char line
[1024]; /* Line from file */
5418 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5420 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5422 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5423 LPDConfigFile
+ 9, strerror(errno
));
5427 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5429 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5430 newfile
, strerror(errno
));
5436 * Copy all of the lines from the cups-lpd file...
5439 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5443 cupsFilePrintf(nfp
, "%s\n", line
);
5444 snprintf(line
, sizeof(line
), "\tdisable = %s",
5445 onoff
? "no" : "yes");
5447 else if (!strstr(line
, "disable ="))
5448 cupsFilePrintf(nfp
, "%s\n", line
);
5453 rename(newfile
, LPDConfigFile
+ 9);
5456 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5459 * Enable/disable LPD via the launchctl command...
5462 char *argv
[5], /* Arguments for command */
5463 *envp
[MAX_ENV
]; /* Environment for command */
5464 int pid
; /* Process ID */
5467 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5468 argv
[0] = (char *)"launchctl";
5469 argv
[1] = (char *)(onoff
? "load" : "unload");
5470 argv
[2] = (char *)"-w";
5471 argv
[3] = LPDConfigFile
+ 10;
5474 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5477 #endif /* __APPLE__ */
5479 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5484 * 'update_polling()' - Read status messages from the poll daemons.
5488 update_polling(void)
5490 char *ptr
, /* Pointer to end of line in buffer */
5491 message
[1024]; /* Pointer to message text */
5492 int loglevel
; /* Log level for message */
5495 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5496 message
, sizeof(message
))) != NULL
)
5498 if (loglevel
== CUPSD_LOG_INFO
)
5499 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5501 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5505 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5508 * All polling processes have died; stop polling...
5511 cupsdLogMessage(CUPSD_LOG_ERROR
,
5512 "update_polling: all polling processes have exited!");
5519 * 'update_smb()' - Update the SMB configuration as needed.
5523 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5528 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5531 * Enable/disable SMB via the specified smb.conf config file...
5534 char newfile
[1024]; /* New smb.conf.N file */
5535 cups_file_t
*ofp
, /* Original file pointer */
5536 *nfp
; /* New file pointer */
5537 char line
[1024]; /* Line from file */
5538 int in_printers
; /* In [printers] section? */
5541 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5543 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5545 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5546 SMBConfigFile
+ 8, strerror(errno
));
5550 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5552 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5553 newfile
, strerror(errno
));
5559 * Copy all of the lines from the smb.conf file...
5564 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5566 if (in_printers
&& strstr(line
, "printable ="))
5567 snprintf(line
, sizeof(line
), " printable = %s",
5568 onoff
? "yes" : "no");
5570 cupsFilePrintf(nfp
, "%s\n", line
);
5573 in_printers
= !strcmp(line
, "[printers]");
5578 rename(newfile
, SMBConfigFile
+ 8);
5581 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5586 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".