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 subtype for fax queues...
2765 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2768 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2769 "Registering DNS-SD printer %s with name \"%s\" and "
2770 "type \"%s\"", p
->name
, name
, regtype
);
2773 * Register the queue, dropping characters as needed until we succeed...
2776 nameptr
= name
+ strlen(name
);
2780 p
->ipp_ref
= DNSSDRef
;
2781 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2782 0, name
, regtype
, NULL
, NULL
,
2783 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2784 dnssdRegisterCallback
,
2785 p
)) == kDNSServiceErr_BadParam
)
2788 * Name is too long, drop trailing characters, taking into account
2794 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2801 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2803 if (se
== kDNSServiceErr_NoError
)
2805 p
->ipp_txt
= ipp_txt
;
2806 p
->ipp_len
= ipp_len
;
2810 cupsdLogMessage(CUPSD_LOG_WARN
,
2811 "DNS-SD IPP registration of \"%s\" failed: %d",
2818 if (BrowseLocalProtocols
& BROWSE_LPD
)
2820 printer_len
= 0; /* anti-compiler-warning-code */
2822 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2831 if (p
->printer_ref
&&
2832 (printer_len
!= p
->printer_len
||
2833 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2836 * Update the existing registration...
2839 /* A TTL of 0 means use record's original value (Radar 3176248) */
2840 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2842 0)) == kDNSServiceErr_NoError
)
2845 free(p
->printer_txt
);
2847 p
->printer_txt
= printer_txt
;
2848 p
->printer_len
= printer_len
;
2854 * Failed to update record, lets close this reference and move on...
2857 cupsdLogMessage(CUPSD_LOG_ERROR
,
2858 "Unable to update LPD DNS-SD record for %s - %d",
2861 DNSServiceRefDeallocate(p
->printer_ref
);
2862 p
->printer_ref
= NULL
;
2866 if (!p
->printer_ref
)
2869 * Initial registration...
2872 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2873 "Registering DNS-SD printer %s with name \"%s\" and "
2874 "type \"_printer._tcp\"", p
->name
, name
);
2876 p
->printer_ref
= DNSSDRef
;
2877 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2878 kDNSServiceFlagsShareConnection
,
2879 0, name
, "_printer._tcp", NULL
, NULL
,
2880 htons(printer_port
), printer_len
, printer_txt
,
2881 dnssdRegisterCallback
,
2882 p
)) == kDNSServiceErr_NoError
)
2884 p
->printer_txt
= printer_txt
;
2885 p
->printer_len
= printer_len
;
2889 cupsdLogMessage(CUPSD_LOG_WARN
,
2890 "DNS-SD LPD registration of \"%s\" failed: %d",
2900 * 'dnssdStop()' - Stop all DNS-SD registrations.
2906 cupsd_printer_t
*p
; /* Current printer */
2910 * De-register the individual printers
2913 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2915 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2916 dnssdDeregisterPrinter(p
);
2919 * Shutdown the rest of the service refs...
2924 DNSServiceRefDeallocate(WebIFRef
);
2930 DNSServiceRefDeallocate(RemoteRef
);
2934 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2936 DNSServiceRefDeallocate(DNSSDRef
);
2939 cupsArrayDelete(DNSSDPrinters
);
2940 DNSSDPrinters
= NULL
;
2947 * 'dnssdUpdate()' - Handle DNS-SD queries.
2953 DNSServiceErrorType sdErr
; /* Service discovery error */
2956 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2958 cupsdLogMessage(CUPSD_LOG_ERROR
,
2959 "DNS Service Discovery registration error %d!",
2964 #endif /* HAVE_DNSSD */
2968 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2971 static char * /* O - String or NULL if none */
2972 get_auth_info_required(
2973 cupsd_printer_t
*p
, /* I - Printer */
2974 char *buffer
, /* I - Value buffer */
2975 size_t bufsize
) /* I - Size of value buffer */
2977 cupsd_location_t
*auth
; /* Pointer to authentication element */
2978 char resource
[1024]; /* Printer/class resource path */
2982 * If auth-info-required is set for this printer, return that...
2985 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2987 int i
; /* Looping var */
2988 char *bufptr
; /* Pointer into buffer */
2990 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
2992 if (bufptr
>= (buffer
+ bufsize
- 2))
2998 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
2999 bufptr
+= strlen(bufptr
);
3006 * Figure out the authentication data requirements to advertise...
3009 if (p
->type
& CUPS_PRINTER_CLASS
)
3010 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
3012 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
3014 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
3015 auth
->type
== CUPSD_AUTH_NONE
)
3016 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
3020 int auth_type
; /* Authentication type */
3022 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
3023 auth_type
= DefaultAuthType
;
3027 case CUPSD_AUTH_NONE
:
3030 case CUPSD_AUTH_NEGOTIATE
:
3031 strlcpy(buffer
, "negotiate", bufsize
);
3035 strlcpy(buffer
, "username,password", bufsize
);
3048 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
3051 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
3052 get_hostconfig(const char *name
) /* I - Name of service */
3054 cups_file_t
*fp
; /* Hostconfig file */
3055 char line
[1024], /* Line from file */
3056 *ptr
; /* Pointer to value */
3057 int state
= 1; /* State of service */
3061 * Try opening the /etc/hostconfig file; if we can't open it, assume that
3062 * the service is enabled/auto.
3065 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
3068 * Read lines from the file until we find the service...
3071 while (cupsFileGets(fp
, line
, sizeof(line
)))
3073 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
3078 if (!strcasecmp(line
, name
))
3081 * Found the service, see if it is set to "-NO-"...
3084 if (!strncasecmp(ptr
, "-NO-", 4))
3095 #endif /* __APPLE__ */
3099 * 'is_local_queue()' - Determine whether the URI points at a local queue.
3102 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
3103 is_local_queue(const char *uri
, /* I - Printer URI */
3104 char *host
, /* O - Host string */
3105 int hostlen
, /* I - Length of host buffer */
3106 char *resource
, /* O - Resource string */
3107 int resourcelen
) /* I - Length of resource buffer */
3109 char scheme
[32], /* Scheme portion of URI */
3110 username
[HTTP_MAX_URI
]; /* Username portion of URI */
3111 int port
; /* Port portion of URI */
3112 cupsd_netif_t
*iface
; /* Network interface */
3116 * Pull the URI apart to see if this is a local or remote printer...
3119 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3120 username
, sizeof(username
), host
, hostlen
, &port
,
3121 resource
, resourcelen
) < HTTP_URI_OK
)
3124 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3127 * Check for local server addresses...
3130 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3135 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3137 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3138 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3142 * If we get here, the printer is remote...
3150 * 'process_browse_data()' - Process new browse data.
3154 process_browse_data(
3155 const char *uri
, /* I - URI of printer/class */
3156 const char *host
, /* I - Hostname */
3157 const char *resource
, /* I - Resource path */
3158 cups_ptype_t type
, /* I - Printer type */
3159 ipp_pstate_t state
, /* I - Printer state */
3160 const char *location
, /* I - Printer location */
3161 const char *info
, /* I - Printer information */
3162 const char *make_model
, /* I - Printer make and model */
3163 int num_attrs
, /* I - Number of attributes */
3164 cups_option_t
*attrs
) /* I - Attributes */
3166 int i
; /* Looping var */
3167 int update
; /* Update printer attributes? */
3168 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3169 name
[IPP_MAX_NAME
], /* Name of printer */
3170 newname
[IPP_MAX_NAME
], /* New name of printer */
3171 *hptr
, /* Pointer into hostname */
3172 *sptr
; /* Pointer into ServerName */
3173 const char *shortname
; /* Short queue name (queue) */
3174 char local_make_model
[IPP_MAX_NAME
];
3175 /* Local make and model */
3176 cupsd_printer_t
*p
; /* Printer information */
3177 const char *ipp_options
, /* ipp-options value */
3178 *lease_duration
, /* lease-duration value */
3179 *uuid
; /* uuid value */
3180 int is_class
; /* Is this queue a class? */
3183 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3184 "process_browse_data(uri=\"%s\", host=\"%s\", "
3185 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3186 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3187 uri
, host
, resource
, type
, state
,
3188 location
? location
: "(nil)", info
? info
: "(nil)",
3189 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3192 * Determine if the URI contains any illegal characters in it...
3195 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3196 (strncmp(resource
, "/printers/", 10) &&
3197 strncmp(resource
, "/classes/", 9)))
3199 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3203 if (strchr(resource
, '?') ||
3204 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3205 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3207 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3213 * OK, this isn't a local printer; add any remote options...
3216 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3218 if (BrowseRemoteOptions
)
3220 if (BrowseRemoteOptions
[0] == '?')
3223 * Override server-supplied options...
3226 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3228 else if (ipp_options
)
3231 * Combine the server and local options...
3234 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3235 BrowseRemoteOptions
);
3240 * Just use the local options...
3243 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3248 else if (ipp_options
)
3251 * Just use the server-supplied options...
3254 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3259 * See if we already have it listed in the Printers list, and add it if not...
3262 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3263 type
&= ~CUPS_PRINTER_IMPLICIT
;
3265 hptr
= strchr(host
, '.');
3266 sptr
= strchr(ServerName
, '.');
3267 is_class
= type
& CUPS_PRINTER_CLASS
;
3268 uuid
= cupsGetOption("uuid", num_attrs
, attrs
);
3270 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3273 * Strip the common domain name components...
3276 while (hptr
!= NULL
)
3278 if (!strcasecmp(hptr
, sptr
))
3284 hptr
= strchr(hptr
+ 1, '.');
3291 * Remote destination is a class...
3294 if (!strncmp(resource
, "/classes/", 9))
3295 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3299 shortname
= resource
+ 9;
3304 * Remote destination is a printer...
3307 if (!strncmp(resource
, "/printers/", 10))
3308 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3312 shortname
= resource
+ 10;
3316 *hptr
= '.'; /* Resource FQDN */
3318 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3321 * Long name doesn't exist, try short name...
3324 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3327 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3330 * Short name doesn't exist, use it for this shared queue.
3333 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3335 strlcpy(name
, shortname
, sizeof(name
));
3340 * Short name exists...
3343 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3344 "process_browse_data: %s found, type=%x, hostname=%s...",
3345 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3347 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3348 p
= NULL
; /* Don't replace implicit classes */
3349 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3352 * Short name exists but is for a different host. If this is a remote
3353 * queue, rename it and use the long name...
3356 if (p
->type
& CUPS_PRINTER_REMOTE
)
3358 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3359 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3360 is_class
? "class" : "printer", p
->name
, p
->name
,
3362 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3363 "%s \'%s\' deleted by directory services.",
3364 is_class
? "Class" : "Printer", p
->name
);
3366 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3367 cupsdRenamePrinter(p
, newname
);
3369 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3370 "%s \'%s\' added by directory services.",
3371 is_class
? "Class" : "Printer", p
->name
);
3375 * Force creation with long name...
3383 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3384 "process_browse_data: %s found, type=%x, hostname=%s...",
3385 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3390 * Queue doesn't exist; add it...
3394 p
= cupsdAddClass(name
);
3396 p
= cupsdAddPrinter(name
);
3401 cupsdClearString(&(p
->hostname
));
3403 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3404 is_class
? "class" : "printer", name
);
3406 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3407 "%s \'%s\' added by directory services.",
3408 is_class
? "Class" : "Printer", name
);
3411 * Force the URI to point to the real server...
3414 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3417 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3423 * Hostname not set, so this must be a cached remote printer
3424 * that was created for a pending print job...
3427 cupsdSetString(&p
->hostname
, host
);
3428 cupsdSetString(&p
->uri
, uri
);
3429 cupsdSetString(&p
->device_uri
, uri
);
3432 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3436 * Update the state...
3440 p
->browse_time
= time(NULL
);
3442 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3446 * Grab the lease-duration for the browse data; anything less then 1
3447 * second or more than 1 week gets the default BrowseTimeout...
3450 i
= atoi(lease_duration
);
3451 if (i
< 1 || i
> 604800)
3454 p
->browse_expire
= p
->browse_time
+ i
;
3457 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3459 if (type
& CUPS_PRINTER_REJECTING
)
3461 type
&= ~CUPS_PRINTER_REJECTING
;
3469 else if (!p
->accepting
)
3475 if (p
->type
!= type
)
3481 if (uuid
&& strcmp(p
->uuid
, uuid
))
3483 cupsdSetString(&p
->uuid
, uuid
);
3487 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3489 cupsdSetString(&p
->location
, location
);
3493 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3495 cupsdSetString(&p
->info
, info
);
3498 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3501 if (!make_model
|| !make_model
[0])
3504 snprintf(local_make_model
, sizeof(local_make_model
),
3505 "Remote Class on %s", host
);
3507 snprintf(local_make_model
, sizeof(local_make_model
),
3508 "Remote Printer on %s", host
);
3511 snprintf(local_make_model
, sizeof(local_make_model
),
3512 "%s on %s", make_model
, host
);
3514 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3516 cupsdSetString(&p
->make_model
, local_make_model
);
3522 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3525 * See if we need to update the attributes...
3528 if (p
->num_options
!= num_attrs
)
3532 for (i
= 0; i
< num_attrs
; i
++)
3533 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3534 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3535 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3544 * Free the old options...
3547 cupsFreeOptions(p
->num_options
, p
->options
);
3550 p
->num_options
= num_attrs
;
3553 if (type
& CUPS_PRINTER_DELETE
)
3555 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3556 "%s \'%s\' deleted by directory services.",
3557 is_class
? "Class" : "Printer", p
->name
);
3559 cupsdExpireSubscriptions(p
, NULL
);
3561 cupsdDeletePrinter(p
, 1);
3562 cupsdUpdateImplicitClasses();
3563 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3567 cupsdSetPrinterAttrs(p
);
3568 cupsdUpdateImplicitClasses();
3572 * See if we have a default printer... If not, make the first network
3573 * default printer the default.
3576 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3579 * Find the first network default printer and use it...
3582 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3584 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3585 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3588 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3594 * Do auto-classing if needed...
3597 process_implicit_classes();
3602 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3606 process_implicit_classes(void)
3608 int i
; /* Looping var */
3609 int update
; /* Update printer attributes? */
3610 char name
[IPP_MAX_NAME
], /* Name of printer */
3611 *hptr
; /* Pointer into hostname */
3612 cupsd_printer_t
*p
, /* Printer information */
3613 *pclass
, /* Printer class */
3614 *first
; /* First printer in class */
3615 int offset
, /* Offset of name */
3616 len
; /* Length of name */
3619 if (!ImplicitClasses
|| !Printers
)
3623 * Loop through all available printers and create classes as needed...
3626 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3627 update
= 0, pclass
= NULL
, first
= NULL
;
3629 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3632 * Skip implicit classes...
3635 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3642 * If len == 0, get the length of this printer name up to the "@"
3646 cupsArraySave(Printers
);
3649 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3650 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3653 * We have more than one printer with the same name; see if
3654 * we have a class, and if this printer is a member...
3657 if (pclass
&& strcasecmp(pclass
->name
, name
))
3660 cupsdSetPrinterAttrs(pclass
);
3666 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3669 * Need to add the class...
3672 pclass
= cupsdAddPrinter(name
);
3673 cupsArrayAdd(ImplicitPrinters
, pclass
);
3675 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3676 pclass
->accepting
= 1;
3677 pclass
->state
= IPP_PRINTER_IDLE
;
3679 cupsdSetString(&pclass
->location
, p
->location
);
3680 cupsdSetString(&pclass
->info
, p
->info
);
3682 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3683 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3687 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3689 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3691 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3692 "Implicit class \'%s\' added by directory services.",
3698 for (i
= 0; i
< pclass
->num_printers
; i
++)
3699 if (pclass
->printers
[i
] == first
)
3702 if (i
>= pclass
->num_printers
)
3704 first
->in_implicit_class
= 1;
3705 cupsdAddPrinterToClass(pclass
, first
);
3711 for (i
= 0; i
< pclass
->num_printers
; i
++)
3712 if (pclass
->printers
[i
] == p
)
3715 if (i
>= pclass
->num_printers
)
3717 p
->in_implicit_class
= 1;
3718 cupsdAddPrinterToClass(pclass
, p
);
3725 * First time around; just get name length and mark it as first
3729 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3730 len
= hptr
- p
->name
;
3732 len
= strlen(p
->name
);
3734 if (len
>= sizeof(name
))
3737 * If the printer name length somehow is greater than we normally allow,
3738 * skip this printer...
3742 cupsArrayRestore(Printers
);
3746 strncpy(name
, p
->name
, len
);
3750 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3751 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3754 * Can't use same name as a local printer; add "Any" to the
3755 * front of the name, unless we have explicitly disabled
3756 * the "ImplicitAnyClasses"...
3759 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3762 * Add "Any" to the class name...
3765 strcpy(name
, "Any");
3766 strncpy(name
+ 3, p
->name
, len
);
3767 name
[len
+ 3] = '\0';
3773 * Don't create an implicit class if we have a local printer
3774 * with the same name...
3778 cupsArrayRestore(Printers
);
3786 cupsArrayRestore(Printers
);
3790 * Update the last printer class as needed...
3793 if (pclass
&& update
)
3794 cupsdSetPrinterAttrs(pclass
);
3799 * 'send_cups_browse()' - Send new browsing information using the CUPS
3804 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3806 int i
; /* Looping var */
3807 cups_ptype_t type
; /* Printer type */
3808 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3809 int bytes
; /* Length of packet */
3810 char packet
[1453], /* Browse data packet */
3811 uri
[1024], /* Printer URI */
3812 location
[1024], /* printer-location */
3813 info
[1024], /* printer-info */
3815 /* printer-make-and-model */
3816 air
[1024]; /* auth-info-required */
3817 cupsd_netif_t
*iface
; /* Network interface */
3821 * Figure out the printer type value...
3824 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3827 type
|= CUPS_PRINTER_REJECTING
;
3829 if (p
== DefaultPrinter
)
3830 type
|= CUPS_PRINTER_DEFAULT
;
3833 * Remove quotes from printer-info, printer-location, and
3834 * printer-make-and-model attributes...
3837 dequote(location
, p
->location
, sizeof(location
));
3838 dequote(info
, p
->info
, sizeof(info
));
3841 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3842 else if (p
->type
& CUPS_PRINTER_CLASS
)
3844 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3845 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3847 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3850 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3852 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3854 if (get_auth_info_required(p
, packet
, sizeof(packet
)))
3855 snprintf(air
, sizeof(air
), " auth-info-required=%s", packet
);
3860 * Send a packet to each browse address...
3863 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3867 * Send the browse packet to one or more interfaces...
3870 if (!strcmp(b
->iface
, "*"))
3873 * Send to all local interfaces...
3878 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3880 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3883 * Only send to local, IPv4 interfaces...
3886 if (!iface
->is_local
|| !iface
->port
||
3887 iface
->address
.addr
.sa_family
!= AF_INET
)
3890 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3891 iface
->hostname
, iface
->port
,
3892 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3895 snprintf(packet
, sizeof(packet
),
3896 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
3897 type
, p
->state
, uri
, location
, info
, make_model
,
3898 p
->browse_attrs
? p
->browse_attrs
: "", air
, p
->uuid
);
3900 bytes
= strlen(packet
);
3902 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3903 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3904 iface
->name
, packet
);
3906 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3908 sendto(BrowseSocket
, packet
, bytes
, 0,
3909 (struct sockaddr
*)&(iface
->broadcast
),
3910 httpAddrLength(&(iface
->broadcast
)));
3913 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3916 * Send to the named interface using the IPv4 address...
3920 if (strcmp(b
->iface
, iface
->name
))
3925 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3928 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3932 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3933 iface
->hostname
, iface
->port
,
3934 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3937 snprintf(packet
, sizeof(packet
),
3938 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
3939 type
, p
->state
, uri
, location
, info
, make_model
,
3940 p
->browse_attrs
? p
->browse_attrs
: "", air
, p
->uuid
);
3942 bytes
= strlen(packet
);
3944 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3945 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3946 iface
->name
, packet
);
3948 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3950 sendto(BrowseSocket
, packet
, bytes
, 0,
3951 (struct sockaddr
*)&(iface
->broadcast
),
3952 httpAddrLength(&(iface
->broadcast
)));
3959 * Send the browse packet to the indicated address using
3960 * the default server name...
3963 snprintf(packet
, sizeof(packet
),
3964 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
3965 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3966 p
->browse_attrs
? p
->browse_attrs
: "", air
, p
->uuid
);
3968 bytes
= strlen(packet
);
3969 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3970 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3972 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3973 (struct sockaddr
*)&(b
->to
),
3974 httpAddrLength(&(b
->to
))) <= 0)
3977 * Unable to send browse packet, so remove this address from the
3981 cupsdLogMessage(CUPSD_LOG_ERROR
,
3982 "cupsdSendBrowseList: sendto failed for browser "
3984 (int)(b
- Browsers
+ 1), strerror(errno
));
3987 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3998 * 'ldap_search_rec()' - LDAP Search with reconnect
4001 static int /* O - Return code */
4002 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
4003 char *base
, /* I - Base dn */
4004 int scope
, /* I - LDAP search scope */
4005 char *filter
, /* I - Filter string */
4006 char *attrs
[], /* I - Requested attributes */
4007 int attrsonly
, /* I - Return only attributes? */
4008 LDAPMessage
**res
) /* I - LDAP handler */
4010 int rc
; /* Return code */
4011 LDAP
*ldr
; /* LDAP handler after reconnect */
4014 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4015 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
4016 NULL
, LDAP_NO_LIMIT
, res
);
4018 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4019 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4022 * If we have a connection problem try again...
4025 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4027 cupsdLogMessage(CUPSD_LOG_ERROR
,
4028 "LDAP search failed with status %d: %s",
4029 rc
, ldap_err2string(rc
));
4030 cupsdLogMessage(CUPSD_LOG_INFO
,
4031 "We try the LDAP search once again after reconnecting to "
4034 ldr
= ldap_reconnect();
4036 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4037 rc
= ldap_search_ext_s(ldr
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
4038 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
4040 rc
= ldap_search_s(ldr
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4041 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4044 if (rc
== LDAP_NO_SUCH_OBJECT
)
4045 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4046 "ldap_search_rec: LDAP entry/object not found");
4047 else if (rc
!= LDAP_SUCCESS
)
4048 cupsdLogMessage(CUPSD_LOG_ERROR
,
4049 "ldap_search_rec: LDAP search failed with status %d: %s",
4050 rc
, ldap_err2string(rc
));
4052 if (rc
!= LDAP_SUCCESS
)
4060 * 'ldap_freeres()' - Free LDAPMessage
4064 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
4066 int rc
; /* Return value */
4069 rc
= ldap_msgfree(entry
);
4071 cupsdLogMessage(CUPSD_LOG_WARN
, "Can't free LDAPMessage!");
4073 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Freeing LDAPMessage was unnecessary");
4078 * 'ldap_getval_char()' - Get first LDAP value and convert to string
4081 static int /* O - Return code */
4082 ldap_getval_firststring(
4083 LDAP
*ld
, /* I - LDAP handler */
4084 LDAPMessage
*entry
, /* I - LDAP message or search result */
4085 char *attr
, /* I - the wanted attribute */
4086 char *retval
, /* O - String to return */
4087 unsigned long maxsize
) /* I - Max string size */
4089 char *dn
; /* LDAP DN */
4090 int rc
= 0; /* Return code */
4091 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4092 struct berval
**bval
; /* LDAP value array */
4093 unsigned long size
; /* String size */
4097 * Get value from LDAPMessage...
4100 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
4103 dn
= ldap_get_dn(ld
, entry
);
4104 cupsdLogMessage(CUPSD_LOG_WARN
,
4105 "Failed to get LDAP value %s for %s!",
4112 * Check size and copy value into our string...
4116 if (size
< (bval
[0]->bv_len
+ 1))
4119 dn
= ldap_get_dn(ld
, entry
);
4120 cupsdLogMessage(CUPSD_LOG_WARN
,
4121 "Attribute %s is too big! (dn: %s)",
4126 size
= bval
[0]->bv_len
+ 1;
4128 strlcpy(retval
, bval
[0]->bv_val
, size
);
4129 ldap_value_free_len(bval
);
4132 char **value
; /* LDAP value */
4135 * Get value from LDAPMessage...
4138 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4141 dn
= ldap_get_dn(ld
, entry
);
4142 cupsdLogMessage(CUPSD_LOG_WARN
, "Failed to get LDAP value %s for %s!",
4148 strlcpy(retval
, *value
, maxsize
);
4149 ldap_value_free(value
);
4151 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4158 * 'send_ldap_ou()' - Send LDAP ou registrations.
4162 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4163 char *basedn
, /* I - Our base dn */
4164 char *descstring
) /* I - Description for ou */
4166 int i
; /* Looping var... */
4167 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4168 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4169 LDAPMessage
*res
, /* Search result token */
4170 *e
; /* Current entry from search */
4171 int rc
; /* LDAP status */
4172 int rcmod
; /* LDAP status for modifications */
4173 char dn
[1024], /* DN of the organizational unit we are adding */
4174 *desc
[2], /* Change records */
4176 char old_desc
[1024]; /* Old description */
4177 static const char * const objectClass_values
[] =
4178 { /* The 2 objectClass's we use in */
4179 "top", /* our LDAP entries */
4180 "organizationalUnit",
4183 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4190 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4193 * Reconnect if LDAP Handle is invalid...
4196 if (!BrowseLDAPHandle
)
4198 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4199 "send_ldap_ou: LDAP Handle is invalid. Try reconnecting...");
4205 * Prepare ldap search...
4208 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4209 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4213 desc
[0] = descstring
;
4216 mods
[0].mod_type
= "ou";
4217 mods
[0].mod_values
= ou_value
;
4218 mods
[1].mod_type
= "description";
4219 mods
[1].mod_values
= desc
;
4220 mods
[2].mod_type
= "objectClass";
4221 mods
[2].mod_values
= (char **)objectClass_values
;
4223 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4224 (char **)ou_attrs
, 0, &res
);
4227 * If ldap search was not successfull then exit function...
4230 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4234 * Check if we need to insert or update the LDAP entry...
4237 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4238 rc
!= LDAP_NO_SUCH_OBJECT
)
4241 * Printserver has already been registered, check if
4242 * modification is required...
4245 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4248 * Get the required values from this entry...
4251 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4252 sizeof(old_desc
)) == -1)
4256 * Check if modification is required...
4259 if ( strcmp(desc
[0], old_desc
) == 0 )
4262 * LDAP entry for the printer exists.
4263 * Printer has already been registered,
4264 * no modifications required...
4266 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4267 "send_ldap_ou: No updates required for %s", ou
);
4272 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4273 "send_ldap_ou: Replace entry for %s", ou
);
4275 for (i
= 0; i
< 3; i
++)
4277 pmods
[i
] = mods
+ i
;
4278 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4282 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4283 if ((rcmod
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4284 NULL
)) != LDAP_SUCCESS
)
4286 if ((rcmod
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4287 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4289 cupsdLogMessage(CUPSD_LOG_ERROR
,
4290 "LDAP modify for %s failed with status %d: %s",
4291 ou
, rcmod
, ldap_err2string(rcmod
));
4292 if (rcmod
== LDAP_SERVER_DOWN
)
4300 * Printserver has never been registered,
4301 * add registration...
4304 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4305 "send_ldap_ou: Add entry for %s", ou
);
4307 for (i
= 0; i
< 3; i
++)
4309 pmods
[i
] = mods
+ i
;
4310 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4314 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4315 if ((rcmod
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4316 NULL
)) != LDAP_SUCCESS
)
4318 if ((rcmod
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4319 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4321 cupsdLogMessage(CUPSD_LOG_ERROR
,
4322 "LDAP add for %s failed with status %d: %s",
4323 ou
, rcmod
, ldap_err2string(rcmod
));
4324 if (rcmod
== LDAP_SERVER_DOWN
)
4329 if (rc
== LDAP_SUCCESS
)
4335 * 'send_ldap_browse()' - Send LDAP printer registrations.
4339 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4341 int i
; /* Looping var... */
4342 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4343 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4344 LDAPMessage
*res
, /* Search result token */
4345 *e
; /* Current entry from search */
4346 char *cn_value
[2], /* Change records */
4352 typestring
[255], /* String to hold printer-type */
4353 dn
[1024]; /* DN of the printer we are adding */
4354 int rc
; /* LDAP status */
4355 int rcmod
; /* LDAP status for modifications */
4356 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4357 old_location
[1024], /* Printer location */
4358 old_info
[1024], /* Printer information */
4359 old_make_model
[1024], /* Printer make and model */
4360 old_type_string
[30]; /* Temporary type number */
4361 int old_type
; /* Printer type */
4362 static const char * const objectClass_values
[] =
4363 { /* The 3 objectClass's we use in */
4364 "top", /* our LDAP entries */
4371 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4374 * Exit function if LDAP updates has been disabled...
4377 if (!BrowseLDAPUpdate
)
4379 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4380 "send_ldap_browse: Updates temporary disabled; "
4386 * Reconnect if LDAP Handle is invalid...
4389 if (!BrowseLDAPHandle
)
4391 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4392 "send_ldap_browse: LDAP Handle is invalid. Try "
4399 * Everything in ldap is ** so we fudge around it...
4402 sprintf(typestring
, "%u", p
->type
);
4404 cn_value
[0] = p
->name
;
4406 info
[0] = p
->info
? p
->info
: "Unknown";
4408 location
[0] = p
->location
? p
->location
: "Unknown";
4410 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4411 make_model
[1] = NULL
;
4412 type
[0] = typestring
;
4418 * Get ldap entry for printer ...
4421 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4423 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4425 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4426 (char **)ldap_attrs
, 0, &res
);
4429 * If ldap search was not successfull then exit function
4430 * and temporary disable LDAP updates...
4433 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4435 if (BrowseLDAPUpdate
&&
4436 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4438 BrowseLDAPUpdate
= FALSE
;
4439 cupsdLogMessage(CUPSD_LOG_INFO
,
4440 "LDAP update temporary disabled");
4447 * Fill modification array...
4450 mods
[0].mod_type
= "cn";
4451 mods
[0].mod_values
= cn_value
;
4452 mods
[1].mod_type
= "printerDescription";
4453 mods
[1].mod_values
= info
;
4454 mods
[2].mod_type
= "printerURI";
4455 mods
[2].mod_values
= uri
;
4456 mods
[3].mod_type
= "printerLocation";
4457 mods
[3].mod_values
= location
;
4458 mods
[4].mod_type
= "printerMakeAndModel";
4459 mods
[4].mod_values
= make_model
;
4460 mods
[5].mod_type
= "printerType";
4461 mods
[5].mod_values
= type
;
4462 mods
[6].mod_type
= "objectClass";
4463 mods
[6].mod_values
= (char **)objectClass_values
;
4466 * Check if we need to insert or update the LDAP entry...
4469 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4470 rc
!= LDAP_NO_SUCH_OBJECT
)
4473 * Printer has already been registered, check if
4474 * modification is required...
4477 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4480 * Get the required values from this entry...
4483 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4484 old_info
, sizeof(old_info
)) == -1)
4487 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4488 old_location
, sizeof(old_location
)) == -1)
4491 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4492 old_make_model
, sizeof(old_make_model
)) == -1)
4495 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4496 old_type_string
, sizeof(old_type_string
)) == -1)
4499 old_type
= atoi(old_type_string
);
4501 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4502 sizeof(old_uri
)) == -1)
4506 * Check if modification is required...
4509 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4510 !strcmp(location
[0], old_location
) &&
4511 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4514 * LDAP entry for the printer exists. Printer has already been registered,
4515 * no modifications required...
4518 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4519 "send_ldap_browse: No updates required for %s", p
->name
);
4524 * LDAP entry for the printer exists. Printer has already been registered,
4525 * modify the current registration...
4528 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4529 "send_ldap_browse: Replace entry for %s", p
->name
);
4531 for (i
= 0; i
< 7; i
++)
4533 pmods
[i
] = mods
+ i
;
4534 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4538 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4539 if ((rcmod
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4540 NULL
)) != LDAP_SUCCESS
)
4542 if ((rcmod
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4543 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4545 cupsdLogMessage(CUPSD_LOG_ERROR
,
4546 "LDAP modify for %s failed with status %d: %s",
4547 p
->name
, rcmod
, ldap_err2string(rcmod
));
4548 if (rcmod
== LDAP_SERVER_DOWN
)
4556 * No LDAP entry exists for the printer. Printer has never been registered,
4557 * add the current registration...
4560 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4562 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4563 "send_ldap_browse: Add entry for %s", p
->name
);
4565 for (i
= 0; i
< 7; i
++)
4567 pmods
[i
] = mods
+ i
;
4568 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4572 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4573 if ((rcmod
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4574 NULL
)) != LDAP_SUCCESS
)
4576 if ((rcmod
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4577 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4579 cupsdLogMessage(CUPSD_LOG_ERROR
,
4580 "LDAP add for %s failed with status %d: %s",
4581 p
->name
, rcmod
, ldap_err2string(rcmod
));
4582 if (rcmod
== LDAP_SERVER_DOWN
)
4587 if (rc
== LDAP_SUCCESS
)
4593 * 'ldap_dereg_printer()' - Delete printer from directory
4597 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4599 char dn
[1024]; /* DN of the printer */
4600 int rc
; /* LDAP status */
4603 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4607 * Reconnect if LDAP Handle is invalid...
4610 if (!BrowseLDAPHandle
)
4617 * Get dn for printer and delete LDAP entry...
4620 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4622 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4624 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4625 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4626 NULL
)) != LDAP_SUCCESS
)
4628 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4629 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4631 cupsdLogMessage(CUPSD_LOG_WARN
,
4632 "LDAP delete for %s failed with status %d: %s",
4633 p
->name
, rc
, ldap_err2string(rc
));
4636 * If we had a connection problem (connection timed out, etc.)
4637 * we should reconnect and try again to delete the entry...
4640 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4642 cupsdLogMessage(CUPSD_LOG_INFO
,
4643 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4646 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4647 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4648 NULL
)) != LDAP_SUCCESS
)
4650 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4651 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4652 cupsdLogMessage(CUPSD_LOG_WARN
,
4653 "LDAP delete for %s failed with status %d: %s",
4654 p
->name
, rc
, ldap_err2string(rc
));
4661 * 'ldap_dereg_ou()' - Remove the organizational unit.
4665 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4666 char *basedn
) /* I - Dase dn */
4668 char dn
[1024]; /* DN of the printer */
4669 int rc
; /* LDAP status */
4672 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4675 * Reconnect if LDAP Handle is invalid...
4678 if (!BrowseLDAPHandle
)
4685 * Get dn for printer and delete LDAP entry...
4688 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4689 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4691 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4692 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4693 NULL
)) != LDAP_SUCCESS
)
4695 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4696 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4698 cupsdLogMessage(CUPSD_LOG_WARN
,
4699 "LDAP delete for %s failed with status %d: %s",
4700 ou
, rc
, ldap_err2string(rc
));
4703 * If we had a connection problem (connection timed out, etc.)
4704 * we should reconnect and try again to delete the entry...
4707 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4709 cupsdLogMessage(CUPSD_LOG_INFO
,
4710 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4712 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4713 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4714 NULL
)) != LDAP_SUCCESS
)
4716 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4717 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4718 cupsdLogMessage(CUPSD_LOG_WARN
,
4719 "LDAP delete for %s failed with status %d: %s",
4720 ou
, rc
, ldap_err2string(rc
));
4724 #endif /* HAVE_LDAP */
4729 * 'send_slp_browse()' - Register the specified printer with SLP.
4733 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4735 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4736 attrs
[8192], /* Printer attributes */
4737 finishings
[1024], /* Finishings to support */
4738 make_model
[IPP_MAX_NAME
* 2],
4739 /* Make and model, quoted */
4740 location
[IPP_MAX_NAME
* 2],
4741 /* Location, quoted */
4742 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4743 *src
, /* Pointer to original string */
4744 *dst
; /* Pointer to destination string */
4745 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4746 SLPError error
; /* SLP error, if any */
4749 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4753 * Make the SLP service URL that conforms to the IANA
4754 * 'printer:' template.
4757 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4759 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4762 * Figure out the finishings string...
4765 if (p
->type
& CUPS_PRINTER_STAPLE
)
4766 strcpy(finishings
, "staple");
4768 finishings
[0] = '\0';
4770 if (p
->type
& CUPS_PRINTER_BIND
)
4773 strlcat(finishings
, ",bind", sizeof(finishings
));
4775 strcpy(finishings
, "bind");
4778 if (p
->type
& CUPS_PRINTER_PUNCH
)
4781 strlcat(finishings
, ",punch", sizeof(finishings
));
4783 strcpy(finishings
, "punch");
4786 if (p
->type
& CUPS_PRINTER_COVER
)
4789 strlcat(finishings
, ",cover", sizeof(finishings
));
4791 strcpy(finishings
, "cover");
4794 if (p
->type
& CUPS_PRINTER_SORT
)
4797 strlcat(finishings
, ",sort", sizeof(finishings
));
4799 strcpy(finishings
, "sort");
4803 strcpy(finishings
, "none");
4806 * Quote any commas in the make and model, location, and info strings...
4809 for (src
= p
->make_model
, dst
= make_model
;
4810 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4812 if (*src
== ',' || *src
== '\\' || *src
== ')')
4821 strcpy(make_model
, "Unknown");
4823 for (src
= p
->location
, dst
= location
;
4824 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4826 if (*src
== ',' || *src
== '\\' || *src
== ')')
4835 strcpy(location
, "Unknown");
4837 for (src
= p
->info
, dst
= info
;
4838 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4840 if (*src
== ',' || *src
== '\\' || *src
== ')')
4849 strcpy(info
, "Unknown");
4852 * Get the authentication value...
4855 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4859 * Make the SLP attribute string list that conforms to
4860 * the IANA 'printer:' template.
4863 snprintf(attrs
, sizeof(attrs
),
4864 "(printer-uri-supported=%s),"
4865 "(uri-authentication-supported=%s>),"
4867 "(uri-security-supported=tls>),"
4869 "(uri-security-supported=none>),"
4870 #endif /* HAVE_SSL */
4871 "(printer-name=%s),"
4872 "(printer-location=%s),"
4873 "(printer-info=%s),"
4874 "(printer-more-info=%s),"
4875 "(printer-make-and-model=%s),"
4876 "(printer-type=%d),"
4877 "(charset-supported=utf-8),"
4878 "(natural-language-configured=%s),"
4879 "(natural-language-supported=de,en,es,fr,it),"
4880 "(color-supported=%s),"
4881 "(finishings-supported=%s),"
4882 "(sides-supported=one-sided%s),"
4883 "(multiple-document-jobs-supported=true)"
4884 "(ipp-versions-supported=1.0,1.1)",
4885 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4886 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4887 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4889 p
->type
& CUPS_PRINTER_DUPLEX
?
4890 ",two-sided-long-edge,two-sided-short-edge" : "");
4892 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4895 * Register the printer with the SLP server...
4898 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4899 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4901 if (error
!= SLP_OK
)
4902 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4908 * 'slp_attr_callback()' - SLP attribute callback
4911 static SLPBoolean
/* O - SLP_TRUE for success */
4913 SLPHandle hslp
, /* I - SLP handle */
4914 const char *attrlist
, /* I - Attribute list */
4915 SLPError errcode
, /* I - Parsing status for this attr */
4916 void *cookie
) /* I - Current printer */
4918 char *tmp
= 0; /* Temporary string */
4919 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4920 /* Current printer */
4923 (void)hslp
; /* anti-compiler-warning-code */
4926 * Bail if there was an error
4929 if (errcode
!= SLP_OK
)
4933 * Parse the attrlist to obtain things needed to build CUPS browse packet
4936 memset(p
, 0, sizeof(cupsd_printer_t
));
4938 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4940 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4942 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4944 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4945 p
->type
= atoi(tmp
);
4947 p
->type
= CUPS_PRINTER_REMOTE
;
4949 cupsdClearString(&tmp
);
4956 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4960 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4962 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4965 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4967 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4970 * Make the SLP service URL that conforms to the IANA
4971 * 'printer:' template.
4974 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4977 * Deregister the printer...
4980 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4986 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4989 static int /* O - 0 on success */
4990 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4991 const char *tag
, /* I - Name of attribute */
4992 char **valbuf
) /* O - Value */
4994 char *ptr1
, /* Pointer into string */
4998 cupsdClearString(valbuf
);
5000 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
5002 ptr1
+= strlen(tag
);
5004 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
5010 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
5011 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
5014 * Dequote the value...
5017 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
5018 if (*ptr1
== '\\' && ptr1
[1])
5019 _cups_strcpy(ptr1
, ptr1
+ 1);
5030 * 'slp_reg_callback()' - Empty SLPRegReport.
5034 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
5035 SLPError errcode
, /* I - Error code, if any */
5036 void *cookie
) /* I - App data */
5047 * 'slp_url_callback()' - SLP service url callback
5050 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
5052 SLPHandle hslp
, /* I - SLP handle */
5053 const char *srvurl
, /* I - URL of service */
5054 unsigned short lifetime
, /* I - Life of service */
5055 SLPError errcode
, /* I - Existing error code */
5056 void *cookie
) /* I - Pointer to service list */
5058 slpsrvurl_t
*s
, /* New service entry */
5059 **head
; /* Pointer to head of entry */
5063 * Let the compiler know we won't be using these vars...
5070 * Bail if there was an error
5073 if (errcode
!= SLP_OK
)
5077 * Grab the head of the list...
5080 head
= (slpsrvurl_t
**)cookie
;
5083 * Allocate a *temporary* slpsrvurl_t to hold this entry.
5086 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
5090 * Copy the SLP service URL...
5093 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
5096 * Link the SLP service URL into the head of the list
5106 #endif /* HAVE_LIBSLP */
5110 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
5114 update_cups_browse(void)
5116 int i
; /* Looping var */
5117 int auth
; /* Authorization status */
5118 int len
; /* Length of name string */
5119 int bytes
; /* Number of bytes left */
5120 char packet
[1541], /* Broadcast packet */
5121 *pptr
; /* Pointer into packet */
5122 socklen_t srclen
; /* Length of source address */
5123 http_addr_t srcaddr
; /* Source address */
5124 char srcname
[1024]; /* Source hostname */
5125 unsigned address
[4]; /* Source address */
5126 unsigned type
; /* Printer type */
5127 unsigned state
; /* Printer state */
5128 char uri
[HTTP_MAX_URI
], /* Printer URI */
5129 host
[HTTP_MAX_URI
], /* Host portion of URI */
5130 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
5131 info
[IPP_MAX_NAME
], /* Information string */
5132 location
[IPP_MAX_NAME
], /* Location string */
5133 make_model
[IPP_MAX_NAME
];/* Make and model string */
5134 int num_attrs
; /* Number of attributes */
5135 cups_option_t
*attrs
; /* Attributes */
5139 * Read a packet from the browse socket...
5142 srclen
= sizeof(srcaddr
);
5143 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5144 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5147 * "Connection refused" is returned under Linux if the destination port
5148 * or address is unreachable from a previous sendto(); check for the
5149 * error here and ignore it for now...
5152 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5154 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5156 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5159 closesocket(BrowseSocket
);
5161 close(BrowseSocket
);
5164 cupsdRemoveSelect(BrowseSocket
);
5167 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5168 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5174 packet
[bytes
] = '\0';
5177 * If we're about to sleep, ignore incoming browse packets.
5184 * Figure out where it came from...
5188 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5190 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5191 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5192 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5193 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5196 #endif /* AF_INET6 */
5201 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5204 if (HostNameLookups
)
5205 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5207 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5209 len
= strlen(srcname
);
5217 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5220 * Access from localhost (127.0.0.1) is always allowed...
5223 auth
= CUPSD_AUTH_ALLOW
;
5228 * Do authorization checks on the domain/address...
5231 switch (BrowseACL
->order_type
)
5234 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5237 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5238 auth
= CUPSD_AUTH_ALLOW
;
5240 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->deny
))
5241 auth
= CUPSD_AUTH_DENY
;
5243 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->allow
))
5244 auth
= CUPSD_AUTH_ALLOW
;
5247 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5248 auth
= CUPSD_AUTH_DENY
;
5250 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->allow
))
5251 auth
= CUPSD_AUTH_ALLOW
;
5253 if (cupsdCheckAuth(address
, srcname
, len
, BrowseACL
->deny
))
5254 auth
= CUPSD_AUTH_DENY
;
5260 auth
= CUPSD_AUTH_ALLOW
;
5262 if (auth
== CUPSD_AUTH_DENY
)
5264 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5265 "update_cups_browse: Refused %d bytes from %s", bytes
,
5270 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5271 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5278 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5280 cupsdLogMessage(CUPSD_LOG_WARN
,
5281 "update_cups_browse: Garbled browse packet - %s", packet
);
5285 strcpy(location
, "Location Unknown");
5286 strcpy(info
, "No Information Available");
5287 make_model
[0] = '\0';
5291 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5294 * Have extended information; can't use sscanf for it because not all
5295 * sscanf's allow empty strings with %[^\"]...
5298 for (i
= 0, pptr
++;
5299 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5301 location
[i
] = *pptr
;
5309 while (*pptr
&& isspace(*pptr
& 255))
5314 for (i
= 0, pptr
++;
5315 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5324 while (*pptr
&& isspace(*pptr
& 255))
5329 for (i
= 0, pptr
++;
5330 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5332 make_model
[i
] = *pptr
;
5337 make_model
[i
] = '\0';
5340 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5346 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5347 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5348 type
, state
, uri
, location
, info
, make_model
));
5351 * Pull the URI apart to see if this is a local or remote printer...
5354 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5356 cupsFreeOptions(num_attrs
, attrs
);
5364 for (i
= 0; i
< NumRelays
; i
++)
5365 if (cupsdCheckAuth(address
, srcname
, len
, Relays
[i
].from
))
5366 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5367 (struct sockaddr
*)&(Relays
[i
].to
),
5368 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5370 cupsdLogMessage(CUPSD_LOG_ERROR
,
5371 "update_cups_browse: sendto failed for relay %d - %s.",
5372 i
+ 1, strerror(errno
));
5373 cupsFreeOptions(num_attrs
, attrs
);
5378 * Process the browse data...
5381 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5382 (ipp_pstate_t
)state
, location
, info
, make_model
,
5388 * 'update_lpd()' - Update the LPD configuration as needed.
5392 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5399 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5400 * setting for backwards-compatibility.
5403 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5405 #endif /* __APPLE__ */
5407 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5410 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5413 char newfile
[1024]; /* New cups-lpd.N file */
5414 cups_file_t
*ofp
, /* Original file pointer */
5415 *nfp
; /* New file pointer */
5416 char line
[1024]; /* Line from file */
5419 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5421 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5423 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5424 LPDConfigFile
+ 9, strerror(errno
));
5428 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5430 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5431 newfile
, strerror(errno
));
5437 * Copy all of the lines from the cups-lpd file...
5440 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5444 cupsFilePrintf(nfp
, "%s\n", line
);
5445 snprintf(line
, sizeof(line
), "\tdisable = %s",
5446 onoff
? "no" : "yes");
5448 else if (!strstr(line
, "disable ="))
5449 cupsFilePrintf(nfp
, "%s\n", line
);
5454 rename(newfile
, LPDConfigFile
+ 9);
5457 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5460 * Enable/disable LPD via the launchctl command...
5463 char *argv
[5], /* Arguments for command */
5464 *envp
[MAX_ENV
]; /* Environment for command */
5465 int pid
; /* Process ID */
5468 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5469 argv
[0] = (char *)"launchctl";
5470 argv
[1] = (char *)(onoff
? "load" : "unload");
5471 argv
[2] = (char *)"-w";
5472 argv
[3] = LPDConfigFile
+ 10;
5475 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5478 #endif /* __APPLE__ */
5480 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5485 * 'update_polling()' - Read status messages from the poll daemons.
5489 update_polling(void)
5491 char *ptr
, /* Pointer to end of line in buffer */
5492 message
[1024]; /* Pointer to message text */
5493 int loglevel
; /* Log level for message */
5496 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5497 message
, sizeof(message
))) != NULL
)
5499 if (loglevel
== CUPSD_LOG_INFO
)
5500 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5502 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5506 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5509 * All polling processes have died; stop polling...
5512 cupsdLogMessage(CUPSD_LOG_ERROR
,
5513 "update_polling: all polling processes have exited!");
5520 * 'update_smb()' - Update the SMB configuration as needed.
5524 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5529 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5532 * Enable/disable SMB via the specified smb.conf config file...
5535 char newfile
[1024]; /* New smb.conf.N file */
5536 cups_file_t
*ofp
, /* Original file pointer */
5537 *nfp
; /* New file pointer */
5538 char line
[1024]; /* Line from file */
5539 int in_printers
; /* In [printers] section? */
5542 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5544 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5546 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5547 SMBConfigFile
+ 8, strerror(errno
));
5551 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5553 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5554 newfile
, strerror(errno
));
5560 * Copy all of the lines from the smb.conf file...
5565 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5567 if (in_printers
&& strstr(line
, "printable ="))
5568 snprintf(line
, sizeof(line
), " printable = %s",
5569 onoff
? "yes" : "no");
5571 cupsFilePrintf(nfp
, "%s\n", line
);
5574 in_printers
= !strcmp(line
, "[printers]");
5579 rename(newfile
, SMBConfigFile
+ 8);
5582 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5587 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".