2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a local
18 * printer and remove any pending references to
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * ldap_rebind_proc() - Callback function for LDAP rebind
27 * ldap_connect() - Start new LDAP connection
28 * ldap_reconnect() - Reconnect to LDAP Server
29 * ldap_disconnect() - Disconnect from LDAP Server
30 * cupsdStartBrowsing() - Start sending and receiving broadcast
32 * cupsdStartPolling() - Start polling servers as needed.
33 * cupsdStopBrowsing() - Stop sending and receiving broadcast
35 * cupsdStopPolling() - Stop polling servers as needed.
36 * cupsdUpdateDNSSDName() - Update the computer name we use for
38 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
39 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
40 * dequote() - Remote quotes from a string.
41 * dnssdAddAlias() - Add a DNS-SD alias name.
42 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
43 * dnssdComparePrinters() - Compare the registered names of two printers.
44 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
46 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
48 * dnssdRegisterCallback() - DNSServiceRegister callback.
49 * dnssdRegisterPrinter() - Start sending broadcast information for a
50 * printer or update the broadcast contents.
51 * dnssdStop() - Stop all DNS-SD registrations.
52 * dnssdUpdate() - Handle DNS-SD queries.
53 * get_hostconfig() - Get an /etc/hostconfig service setting.
54 * is_local_queue() - Determine whether the URI points at a local
56 * process_browse_data() - Process new browse data.
57 * process_implicit_classes() - Create/update implicit classes as needed.
58 * send_cups_browse() - Send new browsing information using the CUPS
60 * ldap_search_rec() - LDAP Search with reconnect
61 * ldap_freeres() - Free LDAPMessage
62 * ldap_getval_char() - Get first LDAP value and convert to string
63 * send_ldap_ou() - Send LDAP ou registrations.
64 * send_ldap_browse() - Send LDAP printer registrations.
65 * ldap_dereg_printer() - Delete printer from directory
66 * send_slp_browse() - Register the specified printer with SLP.
67 * slp_attr_callback() - SLP attribute callback
68 * slp_dereg_printer() - SLPDereg() the specified printer
69 * slp_get_attr() - Get an attribute from an SLP registration.
70 * slp_reg_callback() - Empty SLPRegReport.
71 * slp_url_callback() - SLP service url callback
72 * update_cups_browse() - Update the browse lists using the CUPS
74 * update_lpd() - Update the LPD configuration as needed.
75 * update_polling() - Read status messages from the poll daemons.
76 * update_smb() - Update the SMB configuration as needed.
80 * Include necessary headers...
90 # ifdef HAVE_COREFOUNDATION
91 # include <CoreFoundation/CoreFoundation.h>
92 # endif /* HAVE_COREFOUNDATION */
93 # ifdef HAVE_SYSTEMCONFIGURATION
94 # include <SystemConfiguration/SystemConfiguration.h>
95 # endif /* HAVE_SYSTEMCONFIGURATION */
96 # endif /* __APPLE__ */
97 #endif /* HAVE_DNSSD */
104 static char *dequote(char *d
, const char *s
, int dlen
);
105 static char *get_auth_info_required(cupsd_printer_t
*p
, char *buffer
,
108 static int get_hostconfig(const char *name
);
109 #endif /* __APPLE__ */
110 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
111 char *resource
, int resourcelen
);
112 static void process_browse_data(const char *uri
, const char *host
,
113 const char *resource
, cups_ptype_t type
,
114 ipp_pstate_t state
, const char *location
,
115 const char *info
, const char *make_model
,
116 int num_attrs
, cups_option_t
*attrs
);
117 static void process_implicit_classes(void);
118 static void send_cups_browse(cupsd_printer_t
*p
);
120 static LDAP
*ldap_connect(void);
121 static void ldap_reconnect(void);
122 static void ldap_disconnect(LDAP
*ld
);
123 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
124 char *filter
, char *attrs
[],
125 int attrsonly
, LDAPMessage
**res
);
126 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
127 char *attr
, char *retval
,
128 unsigned long maxsize
);
129 static void ldap_freeres(LDAPMessage
*entry
);
130 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
131 static void send_ldap_browse(cupsd_printer_t
*p
);
132 static void ldap_dereg_printer(cupsd_printer_t
*p
);
133 static void ldap_dereg_ou(char *ou
, char *basedn
);
134 # ifdef HAVE_LDAP_REBIND_PROC
135 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
136 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
137 LDAP_CONST
char *refsp
,
142 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
148 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
149 # endif /* HAVE_LDAP_REBIND_PROC */
150 #endif /* HAVE_LDAP */
152 static void send_slp_browse(cupsd_printer_t
*p
);
153 #endif /* HAVE_LIBSLP */
154 static void update_cups_browse(void);
155 static void update_lpd(int onoff
);
156 static void update_polling(void);
157 static void update_smb(int onoff
);
161 # ifdef HAVE_COREFOUNDATION
162 static void dnssdAddAlias(const void *key
, const void *value
,
164 # endif /* HAVE_COREFOUNDATION */
165 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
167 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
168 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
169 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
171 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
172 DNSServiceFlags flags
,
173 DNSServiceErrorType errorCode
,
174 const char *name
, const char *regtype
,
175 const char *domain
, void *context
);
176 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
177 static void dnssdStop(void);
178 static void dnssdUpdate(void);
179 #endif /* HAVE_DNSSD */
182 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
184 "printerDescription",
186 "printerMakeAndModel",
191 #endif /* HAVE_LDAP */
199 * SLP service name for CUPS...
202 # define SLP_CUPS_SRVTYPE "service:printer"
203 # define SLP_CUPS_SRVLEN 15
207 * Printer service URL structure
210 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
212 struct _slpsrvurl_s
*next
; /* Next URL in list */
213 char url
[HTTP_MAX_URI
];
222 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
223 SLPError errcode
, void *cookie
);
224 static void slp_dereg_printer(cupsd_printer_t
*p
);
225 static int slp_get_attr(const char *attrlist
, const char *tag
,
227 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
229 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
230 unsigned short lifetime
,
231 SLPError errcode
, void *cookie
);
232 #endif /* HAVE_LIBSLP */
236 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
237 * local printer and remove any pending
238 * references to remote printers.
242 cupsdDeregisterPrinter(
243 cupsd_printer_t
*p
, /* I - Printer to register */
244 int removeit
) /* I - Printer being permanently removed */
247 * Only deregister if browsing is enabled and it's a local printer...
250 cupsdLogMessage(CUPSD_LOG_DEBUG
,
251 "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p
, p
->name
,
254 if (!Browsing
|| !p
->shared
||
255 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
256 CUPS_PRINTER_SCANNER
)))
260 * Announce the deletion...
263 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
265 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
267 p
->type
|= CUPS_PRINTER_DELETE
;
275 if (BrowseLocalProtocols
& BROWSE_SLP
)
276 slp_dereg_printer(p
);
277 #endif /* HAVE_LIBSLP */
280 if (BrowseLocalProtocols
& BROWSE_LDAP
)
281 ldap_dereg_printer(p
);
282 #endif /* HAVE_LDAP */
285 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
286 dnssdDeregisterPrinter(p
);
287 #endif /* HAVE_DNSSD */
292 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
296 cupsdLoadRemoteCache(void)
298 int i
; /* Looping var */
299 cups_file_t
*fp
; /* remote.cache file */
300 int linenum
; /* Current line number */
301 char line
[4096], /* Line from file */
302 *value
, /* Pointer to value */
303 *valueptr
, /* Pointer into value */
304 scheme
[32], /* Scheme portion of URI */
305 username
[64], /* Username portion of URI */
307 /* Hostname portion of URI */
308 resource
[HTTP_MAX_URI
];
309 /* Resource portion of URI */
310 int port
; /* Port number */
311 cupsd_printer_t
*p
; /* Current printer */
312 time_t now
; /* Current time */
316 * Don't load the cache if the remote protocols are disabled...
321 cupsdLogMessage(CUPSD_LOG_DEBUG
,
322 "cupsdLoadRemoteCache: Not loading remote cache.");
327 * Open the remote.cache file...
330 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
331 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
335 * Read printer configurations until we hit EOF...
342 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
345 * Decode the directive...
348 if (!strcasecmp(line
, "<Printer") ||
349 !strcasecmp(line
, "<DefaultPrinter"))
352 * <Printer name> or <DefaultPrinter name>
355 if (p
== NULL
&& value
)
358 * Add the printer and a base file type...
361 cupsdLogMessage(CUPSD_LOG_DEBUG
,
362 "cupsdLoadRemoteCache: Loading printer %s...", value
);
364 if ((p
= cupsdFindDest(value
)) != NULL
)
366 if (p
->type
& CUPS_PRINTER_CLASS
)
368 cupsdLogMessage(CUPSD_LOG_WARN
,
369 "Cached remote printer \"%s\" conflicts with "
377 p
= cupsdAddPrinter(value
);
380 p
->state
= IPP_PRINTER_IDLE
;
381 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
382 p
->browse_time
= now
;
383 p
->browse_expire
= now
+ BrowseTimeout
;
386 * Set the default printer as needed...
389 if (!strcasecmp(line
, "<DefaultPrinter"))
394 cupsdLogMessage(CUPSD_LOG_ERROR
,
395 "Syntax error on line %d of remote.cache.", linenum
);
399 else if (!strcasecmp(line
, "<Class") ||
400 !strcasecmp(line
, "<DefaultClass"))
403 * <Class name> or <DefaultClass name>
406 if (p
== NULL
&& value
)
409 * Add the printer and a base file type...
412 cupsdLogMessage(CUPSD_LOG_DEBUG
,
413 "cupsdLoadRemoteCache: Loading class %s...", value
);
415 if ((p
= cupsdFindDest(value
)) != NULL
)
416 p
->type
= CUPS_PRINTER_CLASS
;
418 p
= cupsdAddClass(value
);
421 p
->state
= IPP_PRINTER_IDLE
;
422 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
423 p
->browse_time
= now
;
424 p
->browse_expire
= now
+ BrowseTimeout
;
427 * Set the default printer as needed...
430 if (!strcasecmp(line
, "<DefaultClass"))
435 cupsdLogMessage(CUPSD_LOG_ERROR
,
436 "Syntax error on line %d of remote.cache.", linenum
);
440 else if (!strcasecmp(line
, "</Printer>") ||
441 !strcasecmp(line
, "</Class>"))
446 * Close out the current printer...
449 cupsdSetPrinterAttrs(p
);
454 cupsdLogMessage(CUPSD_LOG_ERROR
,
455 "Syntax error on line %d of remote.cache.", linenum
);
459 cupsdLogMessage(CUPSD_LOG_ERROR
,
460 "Syntax error on line %d of remote.cache.", linenum
);
462 else if (!strcasecmp(line
, "Info"))
465 cupsdSetString(&p
->info
, value
);
467 else if (!strcasecmp(line
, "MakeModel"))
470 cupsdSetString(&p
->make_model
, value
);
472 else if (!strcasecmp(line
, "Location"))
475 cupsdSetString(&p
->location
, value
);
477 else if (!strcasecmp(line
, "DeviceURI"))
481 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
482 username
, sizeof(username
), host
, sizeof(host
), &port
,
483 resource
, sizeof(resource
));
485 cupsdSetString(&p
->hostname
, host
);
486 cupsdSetString(&p
->uri
, value
);
487 cupsdSetDeviceURI(p
, value
);
490 cupsdLogMessage(CUPSD_LOG_ERROR
,
491 "Syntax error on line %d of remote.cache.", linenum
);
493 else if (!strcasecmp(line
, "Option") && value
)
499 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
502 cupsdLogMessage(CUPSD_LOG_ERROR
,
503 "Syntax error on line %d of remote.cache.", linenum
);
506 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
508 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
512 else if (!strcasecmp(line
, "Reason"))
516 for (i
= 0 ; i
< p
->num_reasons
; i
++)
517 if (!strcmp(value
, p
->reasons
[i
]))
520 if (i
>= p
->num_reasons
&&
521 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
523 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
528 cupsdLogMessage(CUPSD_LOG_ERROR
,
529 "Syntax error on line %d of remote.cache.", linenum
);
531 else if (!strcasecmp(line
, "State"))
534 * Set the initial queue state...
537 if (value
&& !strcasecmp(value
, "idle"))
538 p
->state
= IPP_PRINTER_IDLE
;
539 else if (value
&& !strcasecmp(value
, "stopped"))
541 p
->state
= IPP_PRINTER_STOPPED
;
542 cupsdSetPrinterReasons(p
, "+paused");
545 cupsdLogMessage(CUPSD_LOG_ERROR
,
546 "Syntax error on line %d of remote.cache.", linenum
);
548 else if (!strcasecmp(line
, "StateMessage"))
551 * Set the initial queue state message...
555 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
557 else if (!strcasecmp(line
, "Accepting"))
560 * Set the initial accepting state...
564 (!strcasecmp(value
, "yes") ||
565 !strcasecmp(value
, "on") ||
566 !strcasecmp(value
, "true")))
569 (!strcasecmp(value
, "no") ||
570 !strcasecmp(value
, "off") ||
571 !strcasecmp(value
, "false")))
574 cupsdLogMessage(CUPSD_LOG_ERROR
,
575 "Syntax error on line %d of remote.cache.", linenum
);
577 else if (!strcasecmp(line
, "Type"))
580 p
->type
= atoi(value
);
582 cupsdLogMessage(CUPSD_LOG_ERROR
,
583 "Syntax error on line %d of remote.cache.", linenum
);
585 else if (!strcasecmp(line
, "BrowseTime"))
589 time_t t
= atoi(value
);
591 if (t
> p
->browse_expire
)
592 p
->browse_expire
= t
;
595 cupsdLogMessage(CUPSD_LOG_ERROR
,
596 "Syntax error on line %d of remote.cache.", linenum
);
598 else if (!strcasecmp(line
, "JobSheets"))
601 * Set the initial job sheets...
606 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
611 cupsdSetString(&p
->job_sheets
[0], value
);
613 while (isspace(*valueptr
& 255))
618 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
623 cupsdSetString(&p
->job_sheets
[1], value
);
627 cupsdLogMessage(CUPSD_LOG_ERROR
,
628 "Syntax error on line %d of remote.cache.", linenum
);
630 else if (!strcasecmp(line
, "AllowUser"))
635 cupsdAddPrinterUser(p
, value
);
638 cupsdLogMessage(CUPSD_LOG_ERROR
,
639 "Syntax error on line %d of remote.cache.", linenum
);
641 else if (!strcasecmp(line
, "DenyUser"))
646 cupsdAddPrinterUser(p
, value
);
649 cupsdLogMessage(CUPSD_LOG_ERROR
,
650 "Syntax error on line %d of remote.cache.", linenum
);
655 * Something else we don't understand...
658 cupsdLogMessage(CUPSD_LOG_ERROR
,
659 "Unknown configuration directive %s on line %d of remote.cache.",
667 * Do auto-classing if needed...
670 process_implicit_classes();
675 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
676 * printer or update the broadcast contents.
680 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
682 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdRegisterPrinter(p=%p(%s))", p
,
685 if (!Browsing
|| !BrowseLocalProtocols
||
686 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
687 CUPS_PRINTER_SCANNER
)))
691 /* if (BrowseLocalProtocols & BROWSE_SLP)
692 slpRegisterPrinter(p); */
693 #endif /* HAVE_LIBSLP */
696 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
697 dnssdRegisterPrinter(p
);
698 #endif /* HAVE_DNSSD */
703 * 'cupsdRestartPolling()' - Restart polling servers as needed.
707 cupsdRestartPolling(void)
709 int i
; /* Looping var */
710 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
713 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
715 kill(pollp
->pid
, SIGHUP
);
720 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
724 cupsdSaveRemoteCache(void)
726 int i
; /* Looping var */
727 cups_file_t
*fp
; /* printers.conf file */
728 char temp
[1024], /* Temporary string */
729 value
[2048]; /* Value string */
730 cupsd_printer_t
*printer
; /* Current printer class */
731 time_t curtime
; /* Current time */
732 struct tm
*curdate
; /* Current date */
733 cups_option_t
*option
; /* Current option */
737 * Create the remote.cache file...
740 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
742 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
744 cupsdLogMessage(CUPSD_LOG_ERROR
,
745 "Unable to save remote.cache - %s", strerror(errno
));
749 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
752 * Restrict access to the file...
755 fchown(cupsFileNumber(fp
), getuid(), Group
);
756 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
759 * Write a small header to the file...
762 curtime
= time(NULL
);
763 curdate
= localtime(&curtime
);
764 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
766 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
767 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
770 * Write each local printer known to the system...
773 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
775 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
778 * Skip local destinations...
781 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
785 * Write printers as needed...
788 if (printer
== DefaultPrinter
)
789 cupsFilePuts(fp
, "<Default");
791 cupsFilePutChar(fp
, '<');
793 if (printer
->type
& CUPS_PRINTER_CLASS
)
794 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
796 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
798 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
801 cupsFilePutConf(fp
, "Info", printer
->info
);
803 if (printer
->location
)
804 cupsFilePutConf(fp
, "Location", printer
->location
);
806 if (printer
->make_model
)
807 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
809 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
811 if (printer
->state
== IPP_PRINTER_STOPPED
)
812 cupsFilePuts(fp
, "State Stopped\n");
814 cupsFilePuts(fp
, "State Idle\n");
816 for (i
= 0; i
< printer
->num_reasons
; i
++)
817 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
819 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
821 if (printer
->accepting
)
822 cupsFilePuts(fp
, "Accepting Yes\n");
824 cupsFilePuts(fp
, "Accepting No\n");
826 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
827 printer
->job_sheets
[1]);
828 cupsFilePutConf(fp
, "JobSheets", value
);
830 for (i
= 0; i
< printer
->num_users
; i
++)
831 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
834 for (i
= printer
->num_options
, option
= printer
->options
;
838 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
839 cupsFilePutConf(fp
, "Option", value
);
842 if (printer
->type
& CUPS_PRINTER_CLASS
)
843 cupsFilePuts(fp
, "</Class>\n");
845 cupsFilePuts(fp
, "</Printer>\n");
853 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
857 cupsdSendBrowseList(void)
859 int count
; /* Number of dests to update */
860 cupsd_printer_t
*p
; /* Current printer */
861 time_t ut
, /* Minimum update time */
862 to
; /* Timeout time */
865 if (!Browsing
|| !Printers
)
869 * Compute the update and timeout times...
873 ut
= to
- BrowseInterval
;
876 * Figure out how many printers need an update...
879 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
881 int max_count
; /* Maximum number to update */
885 * Throttle the number of printers we'll be updating this time
886 * around based on the number of queues that need updating and
887 * the maximum number of queues to update each second...
890 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
892 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
893 count
< max_count
&& p
!= NULL
;
894 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
895 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
896 CUPS_PRINTER_SCANNER
)) &&
897 p
->shared
&& p
->browse_time
< ut
)
901 * Loop through all of the printers and send local updates as needed...
905 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
907 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
911 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
914 * Check for wraparound...
918 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
922 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
923 CUPS_PRINTER_SCANNER
)) ||
926 else if (p
->browse_time
< ut
)
929 * Need to send an update...
934 p
->browse_time
= time(NULL
);
936 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
940 if (BrowseLocalProtocols
& BROWSE_SLP
)
942 #endif /* HAVE_LIBSLP */
945 if (BrowseLocalProtocols
& BROWSE_LDAP
)
947 #endif /* HAVE_LDAP */
952 * Save where we left off so that all printers get updated...
959 * Loop through all of the printers and timeout old printers as needed...
962 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
964 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
967 * If this is a remote queue, see if it needs to be timed out...
970 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
971 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
972 p
->browse_expire
< to
)
974 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
975 "%s \'%s\' deleted by directory services (timeout).",
976 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
979 cupsdLogMessage(CUPSD_LOG_DEBUG
,
980 "Remote destination \"%s\" has timed out; "
984 cupsArraySave(Printers
);
985 cupsdDeletePrinter(p
, 1);
986 cupsArrayRestore(Printers
);
987 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
993 #ifdef HAVE_LDAP_REBIND_PROC
994 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
996 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1000 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1001 LDAP_CONST
char *refsp
,
1009 * Bind to new LDAP server...
1012 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1013 "ldap_rebind_proc: Rebind to %s", refsp
);
1015 # if LDAP_API_VERSION > 3000
1017 bval
.bv_val
= BrowseLDAPPassword
;
1018 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1020 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1022 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
1023 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1024 # endif /* LDAP_API_VERSION > 3000 */
1029 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1032 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1036 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
1048 * Free current values...
1051 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1052 "ldap_rebind_proc: Free values...");
1054 if ( dnp
&& *dnp
) {
1057 if ( passwdp
&& *passwdp
) {
1065 * Return credentials for LDAP referal...
1068 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1069 "ldap_rebind_proc: Return necessary values...");
1071 *dnp
= strdup(BrowseLDAPBindDN
);
1072 *passwdp
= strdup(BrowseLDAPPassword
);
1073 *authmethodp
= LDAP_AUTH_SIMPLE
;
1079 * Should never happen...
1082 cupsdLogMessage(CUPSD_LOG_ERROR
,
1083 "LDAP rebind has been called with wrong freeit value!");
1088 return (LDAP_SUCCESS
);
1090 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1091 #endif /* HAVE_LDAP_REBIND_PROC */
1096 * 'ldap_connect()' - Start new LDAP connection
1103 * Open LDAP handle...
1106 int rc
; /* LDAP API status */
1107 int version
= 3; /* LDAP version */
1108 struct berval bv
= {0, ""}; /* SASL bind value */
1109 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1110 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1111 int ldap_ssl
= 0; /* LDAP SSL indicator */
1112 int ssl_err
= 0; /* LDAP SSL error value */
1113 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1115 # ifdef HAVE_OPENLDAP
1116 # ifdef HAVE_LDAP_SSL
1119 * Set the certificate file to use for encrypted LDAP sessions...
1122 if (BrowseLDAPCACertFile
)
1124 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1125 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1126 BrowseLDAPCACertFile
);
1128 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1129 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1130 cupsdLogMessage(CUPSD_LOG_ERROR
,
1131 "Unable to set CA certificate file for LDAP "
1132 "connections: %d - %s", rc
, ldap_err2string(rc
));
1135 # endif /* HAVE_LDAP_SSL */
1137 * Initialize OPENLDAP connection...
1138 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1141 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1142 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1144 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1146 # else /* HAVE_OPENLDAP */
1148 int ldap_port
= 0; /* LDAP port */
1149 char ldap_protocol
[11], /* LDAP protocol */
1150 ldap_host
[255]; /* LDAP host */
1153 * Split LDAP URI into its components...
1156 if (! BrowseLDAPServer
)
1158 cupsdLogMessage(CUPSD_LOG_ERROR
,
1159 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1160 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1161 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1165 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1167 if (strcmp(ldap_protocol
, "ldap") == 0) {
1169 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1172 cupsdLogMessage(CUPSD_LOG_ERROR
,
1173 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1174 cupsdLogMessage(CUPSD_LOG_ERROR
,
1175 "Disable LDAP browsing!");
1176 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1177 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1184 ldap_port
= LDAPS_PORT
;
1186 ldap_port
= LDAP_PORT
;
1189 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1190 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1191 ldap_protocol
, ldap_host
, ldap_port
);
1194 * Initialize LDAP connection...
1199 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1200 rc
= LDAP_OPERATIONS_ERROR
;
1204 # ifdef HAVE_LDAP_SSL
1210 * Initialize SSL LDAP connection...
1212 if (BrowseLDAPCACertFile
)
1214 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1215 if (rc
!= LDAP_SUCCESS
) {
1216 cupsdLogMessage(CUPSD_LOG_ERROR
,
1217 "Failed to initialize LDAP SSL client!");
1218 rc
= LDAP_OPERATIONS_ERROR
;
1220 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1221 rc
= LDAP_OPERATIONS_ERROR
;
1228 cupsdLogMessage(CUPSD_LOG_ERROR
,
1229 "LDAP SSL certificate file/database not configured!");
1230 rc
= LDAP_OPERATIONS_ERROR
;
1233 # else /* HAVE_LDAP_SSL */
1236 * Return error, because client libraries doesn't support SSL
1239 cupsdLogMessage(CUPSD_LOG_ERROR
,
1240 "LDAP client libraries does not support TLS");
1241 rc
= LDAP_OPERATIONS_ERROR
;
1243 # endif /* HAVE_LDAP_SSL */
1245 # endif /* HAVE_OPENLDAP */
1248 * Check return code from LDAP initialize...
1251 if (rc
!= LDAP_SUCCESS
)
1253 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1255 cupsdLogMessage(CUPSD_LOG_ERROR
,
1256 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1260 cupsdLogMessage(CUPSD_LOG_ERROR
,
1261 "Unable to initialize LDAP! Disable LDAP browsing!");
1262 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1263 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1266 ldap_disconnect(TempBrowseLDAPHandle
);
1267 TempBrowseLDAPHandle
= NULL
;
1271 * Upgrade LDAP version...
1274 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1275 (const void *)&version
) != LDAP_SUCCESS
)
1277 cupsdLogMessage(CUPSD_LOG_ERROR
,
1278 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1280 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1281 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1282 ldap_disconnect(TempBrowseLDAPHandle
);
1283 TempBrowseLDAPHandle
= NULL
;
1289 * Register LDAP rebind procedure...
1292 # ifdef HAVE_LDAP_REBIND_PROC
1293 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1295 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1296 if ( rc
!= LDAP_SUCCESS
)
1297 cupsdLogMessage(CUPSD_LOG_ERROR
,
1298 "Setting LDAP rebind function failed with status %d: %s",
1299 rc
, ldap_err2string(rc
));
1303 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1305 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1306 # endif /* HAVE_LDAP_REBIND_PROC */
1309 * Start LDAP bind...
1312 # if LDAP_API_VERSION > 3000
1314 bval
.bv_val
= BrowseLDAPPassword
;
1315 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1317 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1318 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1321 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1323 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1324 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1325 # endif /* LDAP_API_VERSION > 3000 */
1327 if (rc
!= LDAP_SUCCESS
)
1329 cupsdLogMessage(CUPSD_LOG_ERROR
,
1330 "LDAP bind failed with error %d: %s",
1331 rc
, ldap_err2string(rc
));
1332 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1333 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1335 ssl_err
= PORT_GetError();
1337 cupsdLogMessage(CUPSD_LOG_ERROR
,
1338 "LDAP SSL error %d: %s",
1339 ssl_err
, ldapssl_err2string(ssl_err
));
1341 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1342 ldap_disconnect(TempBrowseLDAPHandle
);
1343 TempBrowseLDAPHandle
= NULL
;
1347 cupsdLogMessage(CUPSD_LOG_INFO
,
1348 "LDAP connection established");
1352 return (TempBrowseLDAPHandle
);
1357 * 'ldap_reconnect()' - Reconnect to LDAP Server
1361 ldap_reconnect(void)
1363 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1365 cupsdLogMessage(CUPSD_LOG_INFO
,
1366 "Try LDAP reconnect...");
1369 * Get a new LDAP Handle and replace the global Handle
1370 * if the new connection was successful
1373 TempBrowseLDAPHandle
= ldap_connect();
1375 if (TempBrowseLDAPHandle
!= NULL
)
1377 if (BrowseLDAPHandle
!= NULL
)
1379 ldap_disconnect(BrowseLDAPHandle
);
1381 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1387 * 'ldap_disconnect()' - Disconnect from LDAP Server
1391 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1393 int rc
; /* return code */
1396 * Close LDAP handle...
1399 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1400 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1402 rc
= ldap_unbind_s(ld
);
1403 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1404 if (rc
!= LDAP_SUCCESS
)
1405 cupsdLogMessage(CUPSD_LOG_ERROR
,
1406 "Unbind from LDAP server failed with status %d: %s",
1407 rc
, ldap_err2string(rc
));
1409 #endif /* HAVE_LDAP */
1413 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1417 cupsdStartBrowsing(void)
1419 int val
; /* Socket option value */
1420 struct sockaddr_in addr
; /* Broadcast address */
1421 cupsd_printer_t
*p
; /* Current printer */
1426 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1429 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1431 if (BrowseSocket
< 0)
1434 * Create the broadcast socket...
1437 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1439 cupsdLogMessage(CUPSD_LOG_ERROR
,
1440 "Unable to create broadcast socket - %s.",
1442 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1443 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1445 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1446 cupsdEndProcess(getpid(), 0);
1450 if (BrowseSocket
>= 0)
1453 * Bind the socket to browse port...
1456 memset(&addr
, 0, sizeof(addr
));
1457 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1458 addr
.sin_family
= AF_INET
;
1459 addr
.sin_port
= htons(BrowsePort
);
1461 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1463 cupsdLogMessage(CUPSD_LOG_ERROR
,
1464 "Unable to bind broadcast socket - %s.",
1468 closesocket(BrowseSocket
);
1470 close(BrowseSocket
);
1474 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1475 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1477 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1478 cupsdEndProcess(getpid(), 0);
1482 if (BrowseSocket
>= 0)
1485 * Set the "broadcast" flag...
1489 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1491 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1495 closesocket(BrowseSocket
);
1497 close(BrowseSocket
);
1501 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1502 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1504 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1505 cupsdEndProcess(getpid(), 0);
1509 if (BrowseSocket
>= 0)
1512 * Close the socket on exec...
1515 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1518 * Finally, add the socket to the input selection set as needed...
1521 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1524 * We only listen if we want remote printers...
1527 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1536 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1538 DNSServiceErrorType error
; /* Error from service creation */
1539 cupsd_listener_t
*lis
; /* Current listening socket */
1543 * First create a "master" connection for all registrations...
1546 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1547 != kDNSServiceErr_NoError
)
1549 cupsdLogMessage(CUPSD_LOG_ERROR
,
1550 "Unable to create master DNS-SD reference: %d", error
);
1552 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1553 cupsdEndProcess(getpid(), 0);
1558 * Add the master connection to the select list...
1561 int fd
= DNSServiceRefSockFD(DNSSDRef
);
1563 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
1565 cupsdAddSelect(fd
, (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1568 * Then get the port we use for registrations. If we are not listening
1569 * on any non-local ports, there is no sense sharing local printers via
1575 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1577 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1579 if (httpAddrLocalhost(&(lis
->address
)))
1582 if (lis
->address
.addr
.sa_family
== AF_INET
)
1584 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1587 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1589 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1595 * Create an array to track the printers we share...
1598 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1599 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1603 * Set the computer name and register the web interface...
1606 cupsdUpdateDNSSDName();
1609 #endif /* HAVE_DNSSD */
1612 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1615 * Open SLP handle...
1618 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1620 cupsdLogMessage(CUPSD_LOG_ERROR
,
1621 "Unable to open an SLP handle; disabling SLP browsing!");
1622 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1623 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1624 BrowseSLPHandle
= NULL
;
1626 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1627 cupsdEndProcess(getpid(), 0);
1630 BrowseSLPRefresh
= 0;
1633 BrowseSLPHandle
= NULL
;
1634 #endif /* HAVE_LIBSLP */
1637 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1641 cupsdLogMessage(CUPSD_LOG_ERROR
,
1642 "Need to set BrowseLDAPDN to use LDAP browsing!");
1643 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1644 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1646 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1647 cupsdEndProcess(getpid(), 0);
1652 * Open LDAP handle...
1655 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1656 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1657 cupsdEndProcess(getpid(), 0);
1660 BrowseLDAPRefresh
= 0;
1662 #endif /* HAVE_LDAP */
1665 * Enable LPD and SMB printer sharing as needed through external programs...
1668 if (BrowseLocalProtocols
& BROWSE_LPD
)
1671 if (BrowseLocalProtocols
& BROWSE_SMB
)
1675 * Register the individual printers
1678 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1680 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1681 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1682 CUPS_PRINTER_SCANNER
)))
1683 cupsdRegisterPrinter(p
);
1688 * 'cupsdStartPolling()' - Start polling servers as needed.
1692 cupsdStartPolling(void)
1694 int i
; /* Looping var */
1695 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1696 char polld
[1024]; /* Poll daemon path */
1697 char sport
[255]; /* Server port */
1698 char bport
[255]; /* Browser port */
1699 char interval
[255]; /* Poll interval */
1700 int statusfds
[2]; /* Status pipe */
1701 char *argv
[6]; /* Arguments */
1702 char *envp
[100]; /* Environment */
1706 * Don't do anything if we aren't polling...
1709 if (NumPolled
== 0 || BrowseSocket
< 0)
1712 PollStatusBuffer
= NULL
;
1717 * Setup string arguments for polld, port and interval options.
1720 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1722 sprintf(bport
, "%d", BrowsePort
);
1725 sprintf(interval
, "%d", BrowseInterval
);
1727 strcpy(interval
, "30");
1729 argv
[0] = "cups-polld";
1735 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1738 * Create a pipe that receives the status messages from each
1742 if (cupsdOpenPipe(statusfds
))
1744 cupsdLogMessage(CUPSD_LOG_ERROR
,
1745 "Unable to create polling status pipes - %s.",
1748 PollStatusBuffer
= NULL
;
1752 PollPipe
= statusfds
[0];
1753 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1756 * Run each polling daemon, redirecting stderr to the polling pipe...
1759 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1761 sprintf(sport
, "%d", pollp
->port
);
1763 argv
[1] = pollp
->hostname
;
1765 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1766 0, DefaultProfile
, NULL
, &(pollp
->pid
)) < 0)
1768 cupsdLogMessage(CUPSD_LOG_ERROR
,
1769 "cupsdStartPolling: Unable to fork polling daemon - %s",
1775 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1776 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1777 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1780 close(statusfds
[1]);
1783 * Finally, add the pipe to the input selection set...
1786 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1791 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1795 cupsdStopBrowsing(void)
1797 cupsd_printer_t
*p
; /* Current printer */
1800 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1804 * De-register the individual printers
1807 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1809 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1810 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1811 CUPS_PRINTER_SCANNER
)))
1812 cupsdDeregisterPrinter(p
, 1);
1815 * Shut down browsing sockets...
1818 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1822 * Close the socket and remove it from the input selection set.
1826 closesocket(BrowseSocket
);
1828 close(BrowseSocket
);
1831 cupsdRemoveSelect(BrowseSocket
);
1836 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1838 #endif /* HAVE_DNSSD */
1841 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1845 * Close SLP handle...
1848 SLPClose(BrowseSLPHandle
);
1849 BrowseSLPHandle
= NULL
;
1851 #endif /* HAVE_LIBSLP */
1854 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1857 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1858 ldap_disconnect(BrowseLDAPHandle
);
1859 BrowseLDAPHandle
= NULL
;
1861 #endif /* HAVE_OPENLDAP */
1864 * Disable LPD and SMB printer sharing as needed through external programs...
1867 if (BrowseLocalProtocols
& BROWSE_LPD
)
1870 if (BrowseLocalProtocols
& BROWSE_SMB
)
1876 * 'cupsdStopPolling()' - Stop polling servers as needed.
1880 cupsdStopPolling(void)
1882 int i
; /* Looping var */
1883 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1888 cupsdStatBufDelete(PollStatusBuffer
);
1891 cupsdRemoveSelect(PollPipe
);
1894 PollStatusBuffer
= NULL
;
1897 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1899 cupsdEndProcess(pollp
->pid
, 0);
1905 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1909 cupsdUpdateDNSSDName(void)
1911 DNSServiceErrorType error
; /* Error from service creation */
1912 char webif
[1024]; /* Web interface share name */
1913 #ifdef HAVE_COREFOUNDATION_H
1914 SCDynamicStoreRef sc
; /* Context for dynamic store */
1915 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1916 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1917 CFStringRef nameRef
; /* Host name CFString */
1918 char nameBuffer
[1024]; /* C-string buffer */
1919 #endif /* HAVE_COREFOUNDATION_H */
1923 * Only share the web interface and printers when non-local listening is
1932 * Get the computer name as a c-string...
1935 #ifdef HAVE_COREFOUNDATION_H
1936 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1941 * Get the computer name from the dynamic store...
1944 cupsdClearString(&DNSSDComputerName
);
1946 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1948 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1949 kCFStringEncodingUTF8
))
1951 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1952 "Dynamic store computer name is \"%s\".", nameBuffer
);
1953 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1959 if (!DNSSDComputerName
)
1962 * Use the ServerName instead...
1965 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1966 "Using ServerName \"%s\" as computer name.", ServerName
);
1967 cupsdSetString(&DNSSDComputerName
, ServerName
);
1971 * Get the local hostname from the dynamic store...
1974 cupsdClearString(&DNSSDHostName
);
1976 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1978 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1979 kCFStringEncodingUTF8
))
1981 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1982 "Dynamic store host name is \"%s\".", nameBuffer
);
1983 cupsdSetString(&DNSSDHostName
, nameBuffer
);
1992 * Use the ServerName instead...
1995 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1996 "Using ServerName \"%s\" as host name.", ServerName
);
1997 cupsdSetString(&DNSSDHostName
, ServerName
);
2001 * Get any Back-to-My-Mac domains and add them as aliases...
2004 cupsdFreeAliases(DNSSDAlias
);
2007 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
2008 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
2010 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
2011 (int)CFDictionaryGetCount(btmm
));
2012 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
2015 cupsdLogMessage(CUPSD_LOG_ERROR
,
2016 "Bad Back to My Mac data in dynamic store!");
2018 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
2026 #endif /* HAVE_COREFOUNDATION_H */
2028 cupsdSetString(&DNSSDComputerName
, ServerName
);
2029 cupsdSetString(&DNSSDHostName
, ServerName
);
2033 * Then (re)register the web interface if enabled...
2038 if (DNSSDComputerName
)
2039 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
2041 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2044 DNSServiceRefDeallocate(WebIFRef
);
2046 WebIFRef
= DNSSDRef
;
2047 if ((error
= DNSServiceRegister(&WebIFRef
,
2048 kDNSServiceFlagsShareConnection
,
2049 0, webif
, "_http._tcp", NULL
,
2050 NULL
, htons(DNSSDPort
), 7,
2051 "\006path=/", dnssdRegisterCallback
,
2052 NULL
)) != kDNSServiceErr_NoError
)
2053 cupsdLogMessage(CUPSD_LOG_ERROR
,
2054 "DNS-SD web interface registration failed: %d", error
);
2057 #endif /* HAVE_DNSSD */
2062 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2066 cupsdUpdateLDAPBrowse(void)
2068 char uri
[HTTP_MAX_URI
], /* Printer URI */
2069 host
[HTTP_MAX_URI
], /* Hostname */
2070 resource
[HTTP_MAX_URI
], /* Resource path */
2071 location
[1024], /* Printer location */
2072 info
[1024], /* Printer information */
2073 make_model
[1024], /* Printer make and model */
2074 type_num
[30]; /* Printer type number */
2075 int type
; /* Printer type */
2076 int rc
; /* LDAP status */
2077 int limit
; /* Size limit */
2078 LDAPMessage
*res
, /* LDAP search results */
2079 *e
; /* Current entry from search */
2081 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2083 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2086 * Reconnect if LDAP Handle is invalid...
2089 if (! BrowseLDAPHandle
)
2096 * Search for cups printers in LDAP directory...
2099 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2100 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2103 * If ldap search was successfull then exit function
2104 * and temporary disable LDAP updates...
2107 if (rc
!= LDAP_SUCCESS
)
2109 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2111 BrowseLDAPUpdate
= FALSE
;
2112 cupsdLogMessage(CUPSD_LOG_INFO
,
2113 "LDAP update temporary disabled");
2119 * If LDAP updates were disabled, we will reenable them...
2122 if (! BrowseLDAPUpdate
)
2124 BrowseLDAPUpdate
= TRUE
;
2125 cupsdLogMessage(CUPSD_LOG_INFO
,
2126 "LDAP update enabled");
2130 * Count LDAP entries and return if no entry exist...
2133 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2134 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2142 * Loop through the available printers...
2145 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2147 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2150 * Get the required values from this entry...
2153 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2154 "printerDescription", info
, sizeof(info
)) == -1)
2157 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2158 "printerLocation", location
, sizeof(location
)) == -1)
2161 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2162 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2165 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2166 "printerType", type_num
, sizeof(type_num
)) == -1)
2169 type
= atoi(type_num
);
2171 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2172 "printerURI", uri
, sizeof(uri
)) == -1)
2176 * Process the entry as browse data...
2179 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2180 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2181 location
, info
, make_model
, 0, NULL
);
2187 #endif /* HAVE_LDAP */
2192 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2196 cupsdUpdateSLPBrowse(void)
2198 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2199 *next
; /* Next service in list */
2200 cupsd_printer_t p
; /* Printer information */
2201 const char *uri
; /* Pointer to printer URI */
2202 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2203 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2207 * Reset the refresh time...
2210 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2213 * Poll for remote printers using SLP...
2218 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2219 slp_url_callback
, &s
);
2222 * Loop through the list of available printers...
2228 * Save the "next" pointer...
2234 * Load a cupsd_printer_t structure with the SLP service attributes...
2237 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2240 * Process this printer entry...
2243 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2245 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2248 * Pull the URI apart to see if this is a local or remote printer...
2251 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2252 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2253 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2257 * Free this listing...
2260 cupsdClearString(&p
.info
);
2261 cupsdClearString(&p
.location
);
2262 cupsdClearString(&p
.make_model
);
2267 #endif /* HAVE_LIBSLP */
2271 * 'dequote()' - Remote quotes from a string.
2274 static char * /* O - Dequoted string */
2275 dequote(char *d
, /* I - Destination string */
2276 const char *s
, /* I - Source string */
2277 int dlen
) /* I - Destination length */
2279 char *dptr
; /* Pointer into destination */
2284 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2301 # ifdef HAVE_COREFOUNDATION
2303 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2307 dnssdAddAlias(const void *key
, /* I - Key */
2308 const void *value
, /* I - Value (domain) */
2309 void *context
) /* I - Unused */
2311 char valueStr
[1024], /* Domain string */
2312 hostname
[1024]; /* Complete hostname */
2317 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2318 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2319 kCFStringEncodingUTF8
))
2321 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
2323 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2325 cupsdAddAlias(DNSSDAlias
, hostname
);
2326 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2330 cupsdLogMessage(CUPSD_LOG_ERROR
,
2331 "Bad Back to My Mac domain in dynamic store!");
2333 # endif /* HAVE_COREFOUNDATION */
2337 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2340 static char * /* O - TXT record */
2341 dnssdBuildTxtRecord(
2342 int *txt_len
, /* O - TXT record length */
2343 cupsd_printer_t
*p
, /* I - Printer information */
2344 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2346 int i
; /* Looping var */
2347 char type_str
[32], /* Type to string buffer */
2348 state_str
[32], /* State to string buffer */
2349 rp_str
[1024], /* Queue name string buffer */
2350 air_str
[1024], /* auth-info-required string buffer */
2351 *keyvalue
[32][2]; /* Table of key/value pairs */
2355 * Load up the key value pairs...
2360 keyvalue
[i
][0] = "txtvers";
2361 keyvalue
[i
++][1] = "1";
2363 keyvalue
[i
][0] = "qtotal";
2364 keyvalue
[i
++][1] = "1";
2366 keyvalue
[i
][0] = "rp";
2367 keyvalue
[i
++][1] = rp_str
;
2369 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2371 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2372 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2374 keyvalue
[i
][0] = "ty";
2375 keyvalue
[i
++][1] = p
->make_model
? p
->make_model
: "Unknown";
2377 if (p
->location
&& *p
->location
!= '\0')
2379 keyvalue
[i
][0] = "note";
2380 keyvalue
[i
++][1] = p
->location
;
2383 keyvalue
[i
][0] = "priority";
2384 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2386 keyvalue
[i
][0] = "product";
2387 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2389 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2390 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2392 keyvalue
[i
][0] = "printer-state";
2393 keyvalue
[i
++][1] = state_str
;
2395 keyvalue
[i
][0] = "printer-type";
2396 keyvalue
[i
++][1] = type_str
;
2398 keyvalue
[i
][0] = "Transparent";
2399 keyvalue
[i
++][1] = "T";
2401 keyvalue
[i
][0] = "Binary";
2402 keyvalue
[i
++][1] = "T";
2404 if ((p
->type
& CUPS_PRINTER_FAX
))
2406 keyvalue
[i
][0] = "Fax";
2407 keyvalue
[i
++][1] = "T";
2410 if ((p
->type
& CUPS_PRINTER_COLOR
))
2412 keyvalue
[i
][0] = "Color";
2413 keyvalue
[i
++][1] = "T";
2416 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2418 keyvalue
[i
][0] = "Duplex";
2419 keyvalue
[i
++][1] = "T";
2422 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2424 keyvalue
[i
][0] = "Staple";
2425 keyvalue
[i
++][1] = "T";
2428 if ((p
->type
& CUPS_PRINTER_COPIES
))
2430 keyvalue
[i
][0] = "Copies";
2431 keyvalue
[i
++][1] = "T";
2434 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2436 keyvalue
[i
][0] = "Collate";
2437 keyvalue
[i
++][1] = "T";
2440 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2442 keyvalue
[i
][0] = "Punch";
2443 keyvalue
[i
++][1] = "T";
2446 if ((p
->type
& CUPS_PRINTER_BIND
))
2448 keyvalue
[i
][0] = "Bind";
2449 keyvalue
[i
++][1] = "T";
2452 if ((p
->type
& CUPS_PRINTER_SORT
))
2454 keyvalue
[i
][0] = "Sort";
2455 keyvalue
[i
++][1] = "T";
2458 keyvalue
[i
][0] = "pdl";
2459 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2461 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
2463 keyvalue
[i
][0] = "air";
2464 keyvalue
[i
++][1] = air_str
;
2468 * Then pack them into a proper txt record...
2471 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2476 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2479 static int /* O - Result of comparison */
2480 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2481 cupsd_printer_t
*b
)/* I - Second printer */
2483 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2488 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2493 dnssdDeregisterPrinter(
2494 cupsd_printer_t
*p
) /* I - Printer */
2496 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2499 * Closing the socket deregisters the service
2504 DNSServiceRefDeallocate(p
->ipp_ref
);
2511 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2520 DNSServiceRefDeallocate(p
->printer_ref
);
2521 p
->printer_ref
= NULL
;
2527 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2530 free(p
->printer_txt
);
2531 p
->printer_txt
= NULL
;
2535 * Remove the printer from the array of DNS-SD printers, then clear the
2536 * registered name...
2539 cupsArrayRemove(DNSSDPrinters
, p
);
2540 cupsdClearString(&p
->reg_name
);
2545 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2546 * TXT record format.
2549 static char * /* O - TXT record */
2550 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2551 char *keyvalue
[][2], /* I - Table of key value pairs */
2552 int count
) /* I - Items in table */
2554 int i
; /* Looping var */
2555 int length
; /* Length of TXT record */
2556 int length2
; /* Length of value */
2557 char *txtRecord
; /* TXT record buffer */
2558 char *cursor
; /* Looping pointer */
2562 * Calculate the buffer size
2565 for (length
= i
= 0; i
< count
; i
++)
2566 length
+= 1 + strlen(keyvalue
[i
][0]) +
2567 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2570 * Allocate and fill it
2573 txtRecord
= malloc(length
);
2578 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2581 * Drop in the p-string style length byte followed by the data
2584 length
= strlen(keyvalue
[i
][0]);
2585 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2587 *cursor
++ = (unsigned char)(length
+ length2
);
2589 memcpy(cursor
, keyvalue
[i
][0], length
);
2596 memcpy(cursor
, keyvalue
[i
][1], length2
);
2607 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2611 dnssdRegisterCallback(
2612 DNSServiceRef sdRef
, /* I - DNS Service reference */
2613 DNSServiceFlags flags
, /* I - Reserved for future use */
2614 DNSServiceErrorType errorCode
, /* I - Error code */
2615 const char *name
, /* I - Service name */
2616 const char *regtype
, /* I - Service type */
2617 const char *domain
, /* I - Domain. ".local" for now */
2618 void *context
) /* I - User-defined context */
2620 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2621 /* Current printer */
2624 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2625 name
, regtype
, p
? p
->name
: "Web Interface",
2626 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2630 cupsdLogMessage(CUPSD_LOG_ERROR
,
2631 "DNSServiceRegister failed with error %d", (int)errorCode
);
2634 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2636 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2639 cupsArrayRemove(DNSSDPrinters
, p
);
2640 cupsdSetString(&p
->reg_name
, name
);
2641 cupsArrayAdd(DNSSDPrinters
, p
);
2643 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2649 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2650 * or update the broadcast contents.
2654 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2656 DNSServiceErrorType se
; /* dnssd errors */
2657 char *ipp_txt
, /* IPP TXT record buffer */
2658 *printer_txt
, /* LPD TXT record buffer */
2659 name
[1024], /* Service name */
2660 *nameptr
; /* Pointer into name */
2661 int ipp_len
, /* IPP TXT record length */
2662 printer_len
; /* LPD TXT record length */
2663 const char *regtype
; /* Registration type */
2666 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2667 !p
->ipp_ref
? "new" : "update");
2670 * If per-printer sharing was just disabled make sure we're not
2671 * registered before returning.
2676 dnssdDeregisterPrinter(p
);
2681 * The registered name takes the form of "<printer-info> @ <computer name>"...
2684 if (p
->info
&& strlen(p
->info
) > 0)
2686 if (DNSSDComputerName
)
2687 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
2689 strlcpy(name
, p
->info
, sizeof(name
));
2691 else if (DNSSDComputerName
)
2692 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
2694 strlcpy(name
, p
->name
, sizeof(name
));
2697 * If an existing printer was renamed, unregister it and start over...
2700 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2701 dnssdDeregisterPrinter(p
);
2705 cupsdSetString(&p
->reg_name
, name
);
2706 cupsArrayAdd(DNSSDPrinters
, p
);
2710 * Register IPP and (optionally) LPD...
2713 ipp_len
= 0; /* anti-compiler-warning-code */
2714 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2717 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2720 * Update the existing registration...
2723 /* A TTL of 0 means use record's original value (Radar 3176248) */
2724 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2725 0)) == kDNSServiceErr_NoError
)
2730 p
->ipp_txt
= ipp_txt
;
2731 p
->ipp_len
= ipp_len
;
2737 * Failed to update record, lets close this reference and move on...
2740 cupsdLogMessage(CUPSD_LOG_ERROR
,
2741 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2744 DNSServiceRefDeallocate(p
->ipp_ref
);
2752 * Initial registration. Use the _fax subtype for fax queues...
2755 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2758 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2759 "Registering DNS-SD printer %s with name \"%s\" and "
2760 "type \"%s\"", p
->name
, name
, regtype
);
2763 * Register the queue, dropping characters as needed until we succeed...
2766 nameptr
= name
+ strlen(name
);
2770 p
->ipp_ref
= DNSSDRef
;
2771 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2772 0, name
, regtype
, NULL
, NULL
,
2773 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2774 dnssdRegisterCallback
,
2775 p
)) == kDNSServiceErr_BadParam
)
2778 * Name is too long, drop trailing characters, taking into account
2784 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2791 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2793 if (se
== kDNSServiceErr_NoError
)
2795 p
->ipp_txt
= ipp_txt
;
2796 p
->ipp_len
= ipp_len
;
2800 cupsdLogMessage(CUPSD_LOG_WARN
,
2801 "DNS-SD IPP registration of \"%s\" failed: %d",
2808 if (BrowseLocalProtocols
& BROWSE_LPD
)
2810 printer_len
= 0; /* anti-compiler-warning-code */
2811 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2813 if (p
->printer_ref
&&
2814 (printer_len
!= p
->printer_len
||
2815 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2818 * Update the existing registration...
2821 /* A TTL of 0 means use record's original value (Radar 3176248) */
2822 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2824 0)) == kDNSServiceErr_NoError
)
2827 free(p
->printer_txt
);
2829 p
->printer_txt
= printer_txt
;
2830 p
->printer_len
= printer_len
;
2836 * Failed to update record, lets close this reference and move on...
2839 cupsdLogMessage(CUPSD_LOG_ERROR
,
2840 "Unable to update LPD DNS-SD record for %s - %d",
2843 DNSServiceRefDeallocate(p
->printer_ref
);
2844 p
->printer_ref
= NULL
;
2848 if (!p
->printer_ref
)
2851 * Initial registration...
2854 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2855 "Registering DNS-SD printer %s with name \"%s\" and "
2856 "type \"_printer._tcp\"", p
->name
, name
);
2858 p
->printer_ref
= DNSSDRef
;
2859 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2860 kDNSServiceFlagsShareConnection
,
2861 0, name
, "_printer._tcp", NULL
, NULL
,
2862 htons(515), printer_len
, printer_txt
,
2863 dnssdRegisterCallback
,
2864 p
)) == kDNSServiceErr_NoError
)
2866 p
->printer_txt
= printer_txt
;
2867 p
->printer_len
= printer_len
;
2871 cupsdLogMessage(CUPSD_LOG_WARN
,
2872 "DNS-SD LPD registration of \"%s\" failed: %d",
2883 * 'dnssdStop()' - Stop all DNS-SD registrations.
2889 cupsd_printer_t
*p
; /* Current printer */
2893 * De-register the individual printers
2896 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2898 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2899 dnssdDeregisterPrinter(p
);
2902 * Shutdown the rest of the service refs...
2907 DNSServiceRefDeallocate(WebIFRef
);
2913 DNSServiceRefDeallocate(RemoteRef
);
2917 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2919 DNSServiceRefDeallocate(DNSSDRef
);
2922 cupsArrayDelete(DNSSDPrinters
);
2923 DNSSDPrinters
= NULL
;
2930 * 'dnssdUpdate()' - Handle DNS-SD queries.
2936 DNSServiceErrorType sdErr
; /* Service discovery error */
2939 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2941 cupsdLogMessage(CUPSD_LOG_ERROR
,
2942 "DNS Service Discovery registration error %d!",
2947 #endif /* HAVE_DNSSD */
2951 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2954 static char * /* O - String or NULL if none */
2955 get_auth_info_required(
2956 cupsd_printer_t
*p
, /* I - Printer */
2957 char *buffer
, /* I - Value buffer */
2958 size_t bufsize
) /* I - Size of value buffer */
2960 cupsd_location_t
*auth
; /* Pointer to authentication element */
2961 char resource
[1024]; /* Printer/class resource path */
2965 * If auth-info-required is set for this printer, return that...
2968 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2970 int i
; /* Looping var */
2971 char *bufptr
; /* Pointer into buffer */
2973 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
2975 if (bufptr
>= (buffer
+ bufsize
- 2))
2981 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
2982 bufptr
+= strlen(bufptr
);
2989 * Figure out the authentication data requirements to advertise...
2992 if (p
->type
& CUPS_PRINTER_CLASS
)
2993 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2995 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2997 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2998 auth
->type
== CUPSD_AUTH_NONE
)
2999 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
3003 int auth_type
; /* Authentication type */
3005 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
3006 auth_type
= DefaultAuthType
;
3010 case CUPSD_AUTH_NONE
:
3013 case CUPSD_AUTH_NEGOTIATE
:
3014 strlcpy(buffer
, "negotiate", bufsize
);
3018 strlcpy(buffer
, "username,password", bufsize
);
3031 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
3034 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
3035 get_hostconfig(const char *name
) /* I - Name of service */
3037 cups_file_t
*fp
; /* Hostconfig file */
3038 char line
[1024], /* Line from file */
3039 *ptr
; /* Pointer to value */
3040 int state
= 1; /* State of service */
3044 * Try opening the /etc/hostconfig file; if we can't open it, assume that
3045 * the service is enabled/auto.
3048 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
3051 * Read lines from the file until we find the service...
3054 while (cupsFileGets(fp
, line
, sizeof(line
)))
3056 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
3061 if (!strcasecmp(line
, name
))
3064 * Found the service, see if it is set to "-NO-"...
3067 if (!strncasecmp(ptr
, "-NO-", 4))
3078 #endif /* __APPLE__ */
3082 * 'is_local_queue()' - Determine whether the URI points at a local queue.
3085 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
3086 is_local_queue(const char *uri
, /* I - Printer URI */
3087 char *host
, /* O - Host string */
3088 int hostlen
, /* I - Length of host buffer */
3089 char *resource
, /* O - Resource string */
3090 int resourcelen
) /* I - Length of resource buffer */
3092 char scheme
[32], /* Scheme portion of URI */
3093 username
[HTTP_MAX_URI
]; /* Username portion of URI */
3094 int port
; /* Port portion of URI */
3095 cupsd_netif_t
*iface
; /* Network interface */
3099 * Pull the URI apart to see if this is a local or remote printer...
3102 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3103 username
, sizeof(username
), host
, hostlen
, &port
,
3104 resource
, resourcelen
) < HTTP_URI_OK
)
3107 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3110 * Check for local server addresses...
3113 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3118 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3120 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3121 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3125 * If we get here, the printer is remote...
3133 * 'process_browse_data()' - Process new browse data.
3137 process_browse_data(
3138 const char *uri
, /* I - URI of printer/class */
3139 const char *host
, /* I - Hostname */
3140 const char *resource
, /* I - Resource path */
3141 cups_ptype_t type
, /* I - Printer type */
3142 ipp_pstate_t state
, /* I - Printer state */
3143 const char *location
, /* I - Printer location */
3144 const char *info
, /* I - Printer information */
3145 const char *make_model
, /* I - Printer make and model */
3146 int num_attrs
, /* I - Number of attributes */
3147 cups_option_t
*attrs
) /* I - Attributes */
3149 int i
; /* Looping var */
3150 int update
; /* Update printer attributes? */
3151 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3152 name
[IPP_MAX_NAME
], /* Name of printer */
3153 newname
[IPP_MAX_NAME
], /* New name of printer */
3154 *hptr
, /* Pointer into hostname */
3155 *sptr
; /* Pointer into ServerName */
3156 const char *shortname
; /* Short queue name (queue) */
3157 char local_make_model
[IPP_MAX_NAME
];
3158 /* Local make and model */
3159 cupsd_printer_t
*p
; /* Printer information */
3160 const char *ipp_options
, /* ipp-options value */
3161 *lease_duration
; /* lease-duration value */
3162 int is_class
; /* Is this queue a class? */
3165 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3166 "process_browse_data(uri=\"%s\", host=\"%s\", "
3167 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3168 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3169 uri
, host
, resource
, type
, state
,
3170 location
? location
: "(nil)", info
? info
: "(nil)",
3171 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3174 * Determine if the URI contains any illegal characters in it...
3177 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3178 (strncmp(resource
, "/printers/", 10) &&
3179 strncmp(resource
, "/classes/", 9)))
3181 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3185 if (strchr(resource
, '?') ||
3186 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3187 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3189 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3195 * OK, this isn't a local printer; add any remote options...
3198 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3200 if (BrowseRemoteOptions
)
3202 if (BrowseRemoteOptions
[0] == '?')
3205 * Override server-supplied options...
3208 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3210 else if (ipp_options
)
3213 * Combine the server and local options...
3216 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3217 BrowseRemoteOptions
);
3222 * Just use the local options...
3225 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3230 else if (ipp_options
)
3233 * Just use the server-supplied options...
3236 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3241 * See if we already have it listed in the Printers list, and add it if not...
3244 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3245 type
&= ~CUPS_PRINTER_IMPLICIT
;
3247 hptr
= strchr(host
, '.');
3248 sptr
= strchr(ServerName
, '.');
3249 is_class
= type
& CUPS_PRINTER_CLASS
;
3251 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3254 * Strip the common domain name components...
3257 while (hptr
!= NULL
)
3259 if (!strcasecmp(hptr
, sptr
))
3265 hptr
= strchr(hptr
+ 1, '.');
3272 * Remote destination is a class...
3275 if (!strncmp(resource
, "/classes/", 9))
3276 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3280 shortname
= resource
+ 9;
3285 * Remote destination is a printer...
3288 if (!strncmp(resource
, "/printers/", 10))
3289 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3293 shortname
= resource
+ 10;
3297 *hptr
= '.'; /* Resource FQDN */
3299 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3302 * Long name doesn't exist, try short name...
3305 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3308 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3311 * Short name doesn't exist, use it for this shared queue.
3314 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3316 strlcpy(name
, shortname
, sizeof(name
));
3321 * Short name exists...
3324 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3325 "process_browse_data: %s found, type=%x, hostname=%s...",
3326 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3328 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3329 p
= NULL
; /* Don't replace implicit classes */
3330 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3333 * Short name exists but is for a different host. If this is a remote
3334 * queue, rename it and use the long name...
3337 if (p
->type
& CUPS_PRINTER_REMOTE
)
3339 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3340 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3341 is_class
? "class" : "printer", p
->name
, p
->name
,
3343 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3344 "%s \'%s\' deleted by directory services.",
3345 is_class
? "Class" : "Printer", p
->name
);
3347 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3348 cupsdRenamePrinter(p
, newname
);
3350 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3351 "%s \'%s\' added by directory services.",
3352 is_class
? "Class" : "Printer", p
->name
);
3356 * Force creation with long name...
3364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3365 "process_browse_data: %s found, type=%x, hostname=%s...",
3366 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3371 * Queue doesn't exist; add it...
3375 p
= cupsdAddClass(name
);
3377 p
= cupsdAddPrinter(name
);
3382 cupsdClearString(&(p
->hostname
));
3384 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3385 is_class
? "class" : "printer", name
);
3387 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3388 "%s \'%s\' added by directory services.",
3389 is_class
? "Class" : "Printer", name
);
3392 * Force the URI to point to the real server...
3395 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3398 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3404 * Hostname not set, so this must be a cached remote printer
3405 * that was created for a pending print job...
3408 cupsdSetString(&p
->hostname
, host
);
3409 cupsdSetString(&p
->uri
, uri
);
3410 cupsdSetString(&p
->device_uri
, uri
);
3413 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3417 * Update the state...
3421 p
->browse_time
= time(NULL
);
3423 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3427 * Grab the lease-duration for the browse data; anything less then 1
3428 * second or more than 1 week gets the default BrowseTimeout...
3431 i
= atoi(lease_duration
);
3432 if (i
< 1 || i
> 604800)
3435 p
->browse_expire
= p
->browse_time
+ i
;
3438 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3440 if (type
& CUPS_PRINTER_REJECTING
)
3442 type
&= ~CUPS_PRINTER_REJECTING
;
3450 else if (!p
->accepting
)
3456 if (p
->type
!= type
)
3462 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3464 cupsdSetString(&p
->location
, location
);
3468 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3470 cupsdSetString(&p
->info
, info
);
3473 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3476 if (!make_model
|| !make_model
[0])
3479 snprintf(local_make_model
, sizeof(local_make_model
),
3480 "Remote Class on %s", host
);
3482 snprintf(local_make_model
, sizeof(local_make_model
),
3483 "Remote Printer on %s", host
);
3486 snprintf(local_make_model
, sizeof(local_make_model
),
3487 "%s on %s", make_model
, host
);
3489 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3491 cupsdSetString(&p
->make_model
, local_make_model
);
3497 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3500 * See if we need to update the attributes...
3503 if (p
->num_options
!= num_attrs
)
3507 for (i
= 0; i
< num_attrs
; i
++)
3508 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3509 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3510 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3519 * Free the old options...
3522 cupsFreeOptions(p
->num_options
, p
->options
);
3525 p
->num_options
= num_attrs
;
3528 if (type
& CUPS_PRINTER_DELETE
)
3530 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3531 "%s \'%s\' deleted by directory services.",
3532 is_class
? "Class" : "Printer", p
->name
);
3534 cupsdExpireSubscriptions(p
, NULL
);
3536 cupsdDeletePrinter(p
, 1);
3537 cupsdUpdateImplicitClasses();
3538 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3542 cupsdSetPrinterAttrs(p
);
3543 cupsdUpdateImplicitClasses();
3547 * See if we have a default printer... If not, make the first network
3548 * default printer the default.
3551 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3554 * Find the first network default printer and use it...
3557 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3559 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3560 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3563 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3569 * Do auto-classing if needed...
3572 process_implicit_classes();
3577 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3581 process_implicit_classes(void)
3583 int i
; /* Looping var */
3584 int update
; /* Update printer attributes? */
3585 char name
[IPP_MAX_NAME
], /* Name of printer */
3586 *hptr
; /* Pointer into hostname */
3587 cupsd_printer_t
*p
, /* Printer information */
3588 *pclass
, /* Printer class */
3589 *first
; /* First printer in class */
3590 int offset
, /* Offset of name */
3591 len
; /* Length of name */
3594 if (!ImplicitClasses
|| !Printers
)
3598 * Loop through all available printers and create classes as needed...
3601 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3602 update
= 0, pclass
= NULL
, first
= NULL
;
3604 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3607 * Skip implicit classes...
3610 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3617 * If len == 0, get the length of this printer name up to the "@"
3621 cupsArraySave(Printers
);
3624 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3625 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3628 * We have more than one printer with the same name; see if
3629 * we have a class, and if this printer is a member...
3632 if (pclass
&& strcasecmp(pclass
->name
, name
))
3635 cupsdSetPrinterAttrs(pclass
);
3641 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3644 * Need to add the class...
3647 pclass
= cupsdAddPrinter(name
);
3648 cupsArrayAdd(ImplicitPrinters
, pclass
);
3650 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3651 pclass
->accepting
= 1;
3652 pclass
->state
= IPP_PRINTER_IDLE
;
3654 cupsdSetString(&pclass
->location
, p
->location
);
3655 cupsdSetString(&pclass
->info
, p
->info
);
3657 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3658 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3662 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3664 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3666 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3667 "Implicit class \'%s\' added by directory services.",
3673 for (i
= 0; i
< pclass
->num_printers
; i
++)
3674 if (pclass
->printers
[i
] == first
)
3677 if (i
>= pclass
->num_printers
)
3679 first
->in_implicit_class
= 1;
3680 cupsdAddPrinterToClass(pclass
, first
);
3686 for (i
= 0; i
< pclass
->num_printers
; i
++)
3687 if (pclass
->printers
[i
] == p
)
3690 if (i
>= pclass
->num_printers
)
3692 p
->in_implicit_class
= 1;
3693 cupsdAddPrinterToClass(pclass
, p
);
3700 * First time around; just get name length and mark it as first
3704 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3705 len
= hptr
- p
->name
;
3707 len
= strlen(p
->name
);
3709 if (len
>= sizeof(name
))
3712 * If the printer name length somehow is greater than we normally allow,
3713 * skip this printer...
3717 cupsArrayRestore(Printers
);
3721 strncpy(name
, p
->name
, len
);
3725 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3726 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3729 * Can't use same name as a local printer; add "Any" to the
3730 * front of the name, unless we have explicitly disabled
3731 * the "ImplicitAnyClasses"...
3734 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3737 * Add "Any" to the class name...
3740 strcpy(name
, "Any");
3741 strncpy(name
+ 3, p
->name
, len
);
3742 name
[len
+ 3] = '\0';
3748 * Don't create an implicit class if we have a local printer
3749 * with the same name...
3753 cupsArrayRestore(Printers
);
3761 cupsArrayRestore(Printers
);
3765 * Update the last printer class as needed...
3768 if (pclass
&& update
)
3769 cupsdSetPrinterAttrs(pclass
);
3774 * 'send_cups_browse()' - Send new browsing information using the CUPS
3779 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3781 int i
; /* Looping var */
3782 cups_ptype_t type
; /* Printer type */
3783 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3784 int bytes
; /* Length of packet */
3785 char packet
[1453], /* Browse data packet */
3786 uri
[1024], /* Printer URI */
3787 location
[1024], /* printer-location */
3788 info
[1024], /* printer-info */
3790 /* printer-make-and-model */
3791 air
[1024]; /* auth-info-required */
3792 cupsd_netif_t
*iface
; /* Network interface */
3796 * Figure out the printer type value...
3799 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3802 type
|= CUPS_PRINTER_REJECTING
;
3804 if (p
== DefaultPrinter
)
3805 type
|= CUPS_PRINTER_DEFAULT
;
3808 * Remove quotes from printer-info, printer-location, and
3809 * printer-make-and-model attributes...
3812 dequote(location
, p
->location
, sizeof(location
));
3813 dequote(info
, p
->info
, sizeof(info
));
3816 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3817 else if (p
->type
& CUPS_PRINTER_CLASS
)
3819 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3820 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3822 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3825 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3827 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3829 if (get_auth_info_required(p
, packet
, sizeof(packet
)))
3830 snprintf(air
, sizeof(air
), " auth-info-required=%s", packet
);
3835 * Send a packet to each browse address...
3838 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3842 * Send the browse packet to one or more interfaces...
3845 if (!strcmp(b
->iface
, "*"))
3848 * Send to all local interfaces...
3853 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3855 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3858 * Only send to local, IPv4 interfaces...
3861 if (!iface
->is_local
|| !iface
->port
||
3862 iface
->address
.addr
.sa_family
!= AF_INET
)
3865 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3866 iface
->hostname
, iface
->port
,
3867 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3870 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3871 type
, p
->state
, uri
, location
, info
, make_model
,
3872 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3874 bytes
= strlen(packet
);
3876 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3877 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3878 iface
->name
, packet
);
3880 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3882 sendto(BrowseSocket
, packet
, bytes
, 0,
3883 (struct sockaddr
*)&(iface
->broadcast
),
3884 httpAddrLength(&(iface
->broadcast
)));
3887 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3890 * Send to the named interface using the IPv4 address...
3894 if (strcmp(b
->iface
, iface
->name
))
3899 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3902 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3906 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3907 iface
->hostname
, iface
->port
,
3908 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3911 snprintf(packet
, sizeof(packet
),
3912 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3913 type
, p
->state
, uri
, location
, info
, make_model
,
3914 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3916 bytes
= strlen(packet
);
3918 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3919 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3920 iface
->name
, packet
);
3922 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3924 sendto(BrowseSocket
, packet
, bytes
, 0,
3925 (struct sockaddr
*)&(iface
->broadcast
),
3926 httpAddrLength(&(iface
->broadcast
)));
3933 * Send the browse packet to the indicated address using
3934 * the default server name...
3937 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3938 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3939 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3941 bytes
= strlen(packet
);
3942 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3943 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3945 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3946 (struct sockaddr
*)&(b
->to
),
3947 httpAddrLength(&(b
->to
))) <= 0)
3950 * Unable to send browse packet, so remove this address from the
3954 cupsdLogMessage(CUPSD_LOG_ERROR
,
3955 "cupsdSendBrowseList: sendto failed for browser "
3957 (int)(b
- Browsers
+ 1), strerror(errno
));
3960 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3971 * 'ldap_search_rec()' - LDAP Search with reconnect
3975 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3976 char *base
, /* I - Base dn */
3977 int scope
, /* I - LDAP search scope */
3978 char *filter
, /* I - Filter string */
3979 char *attrs
[], /* I - Requested attributes */
3980 int attrsonly
, /* I - Return only attributes? */
3981 LDAPMessage
**res
) /* I - LDAP handler */
3983 int rc
; /* Return code */
3986 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3987 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3988 NULL
, LDAP_NO_LIMIT
, res
);
3990 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3991 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3994 * If we have a connection problem try again...
3997 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3999 cupsdLogMessage(CUPSD_LOG_ERROR
,
4000 "LDAP search failed with status %d: %s",
4001 rc
, ldap_err2string(rc
));
4002 cupsdLogMessage(CUPSD_LOG_INFO
,
4003 "We try the LDAP search once again after reconnecting to "
4008 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4009 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
4010 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
4012 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4013 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4016 if (rc
== LDAP_NO_SUCH_OBJECT
)
4017 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4018 "ldap_search_rec: LDAP entry/object not found");
4019 else if (rc
!= LDAP_SUCCESS
)
4020 cupsdLogMessage(CUPSD_LOG_ERROR
,
4021 "ldap_search_rec: LDAP search failed with status %d: %s",
4022 rc
, ldap_err2string(rc
));
4024 if (rc
!= LDAP_SUCCESS
)
4032 * 'ldap_freeres()' - Free LDAPMessage
4036 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
4038 int rc
; /* Return value */
4041 rc
= ldap_msgfree(entry
);
4043 cupsdLogMessage(CUPSD_LOG_WARN
,
4044 "Can't free LDAPMessage!");
4046 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4047 "Freeing LDAPMessage was unnecessary");
4052 * 'ldap_getval_char()' - Get first LDAP value and convert to string
4056 ldap_getval_firststring(
4057 LDAP
*ld
, /* I - LDAP handler */
4058 LDAPMessage
*entry
, /* I - LDAP message or search result */
4059 char *attr
, /* I - the wanted attribute */
4060 char *retval
, /* O - String to return */
4061 unsigned long maxsize
) /* I - Max string size */
4063 char *dn
; /* LDAP DN */
4064 int rc
= 0; /* Return code */
4065 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4066 struct berval
**bval
; /* LDAP value array */
4067 unsigned long size
; /* String size */
4071 * Get value from LDAPMessage...
4074 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
4077 dn
= ldap_get_dn(ld
, entry
);
4078 cupsdLogMessage(CUPSD_LOG_WARN
,
4079 "Failed to get LDAP value %s for %s!",
4087 * Check size and copy value into our string...
4091 if (size
< bval
[0]->bv_len
)
4094 dn
= ldap_get_dn(ld
, entry
);
4095 cupsdLogMessage(CUPSD_LOG_WARN
,
4096 "Attribute %s is too big! (dn: %s)",
4101 size
= bval
[0]->bv_len
;
4103 strlcpy(retval
, bval
[0]->bv_val
, size
);
4104 ldap_value_free_len(bval
);
4107 char **value
; /* LDAP value */
4110 * Get value from LDAPMessage...
4113 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4116 dn
= ldap_get_dn(ld
, entry
);
4117 cupsdLogMessage(CUPSD_LOG_WARN
,
4118 "Failed to get LDAP value %s for %s!",
4124 strlcpy(retval
, *value
, maxsize
);
4125 ldap_value_free(value
);
4127 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4134 * 'send_ldap_ou()' - Send LDAP ou registrations.
4138 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4139 char *basedn
, /* I - Our base dn */
4140 char *descstring
) /* I - Description for ou */
4142 int i
; /* Looping var... */
4143 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4144 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4145 LDAPMessage
*res
, /* Search result token */
4146 *e
; /* Current entry from search */
4147 int rc
; /* LDAP status */
4148 char dn
[1024], /* DN of the organizational unit we are adding */
4149 *desc
[2], /* Change records */
4151 char old_desc
[1024]; /* Old description */
4152 static const char * const objectClass_values
[] =
4153 { /* The 2 objectClass's we use in */
4154 "top", /* our LDAP entries */
4155 "organizationalUnit",
4158 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4165 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4168 * Reconnect if LDAP Handle is invalid...
4171 if (! BrowseLDAPHandle
)
4173 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4174 "send_ldap_ou: LDAP Handle is invalid. Try "
4181 * Prepare ldap search...
4184 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4185 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4189 desc
[0] = descstring
;
4192 mods
[0].mod_type
= "ou";
4193 mods
[0].mod_values
= ou_value
;
4194 mods
[1].mod_type
= "description";
4195 mods
[1].mod_values
= desc
;
4196 mods
[2].mod_type
= "objectClass";
4197 mods
[2].mod_values
= (char **)objectClass_values
;
4199 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4200 (char **)ou_attrs
, 0, &res
);
4203 * If ldap search was not successfull then exit function...
4206 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4210 * Check if we need to insert or update the LDAP entry...
4213 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4214 rc
!= LDAP_NO_SUCH_OBJECT
)
4217 * Printserver has already been registered, check if
4218 * modification is required...
4221 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4224 * Get the required values from this entry...
4227 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4228 sizeof(old_desc
)) == -1)
4232 * Check if modification is required...
4235 if ( strcmp(desc
[0], old_desc
) == 0 )
4238 * LDAP entry for the printer exists.
4239 * Printer has already been registered,
4240 * no modifications required...
4242 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4243 "send_ldap_ou: No updates required for %s", ou
);
4248 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4249 "send_ldap_ou: Replace entry for %s", ou
);
4251 for (i
= 0; i
< 3; i
++)
4253 pmods
[i
] = mods
+ i
;
4254 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4258 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4259 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4260 NULL
)) != LDAP_SUCCESS
)
4262 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4263 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4265 cupsdLogMessage(CUPSD_LOG_ERROR
,
4266 "LDAP modify for %s failed with status %d: %s",
4267 ou
, rc
, ldap_err2string(rc
));
4268 if ( LDAP_SERVER_DOWN
== rc
)
4276 * Printserver has never been registered,
4277 * add registration...
4280 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4281 "send_ldap_ou: Add entry for %s", ou
);
4283 for (i
= 0; i
< 3; i
++)
4285 pmods
[i
] = mods
+ i
;
4286 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4290 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4291 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4292 NULL
)) != LDAP_SUCCESS
)
4294 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4295 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4297 cupsdLogMessage(CUPSD_LOG_ERROR
,
4298 "LDAP add for %s failed with status %d: %s",
4299 ou
, rc
, ldap_err2string(rc
));
4300 if ( LDAP_SERVER_DOWN
== rc
)
4310 * 'send_ldap_browse()' - Send LDAP printer registrations.
4314 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4316 int i
; /* Looping var... */
4317 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4318 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4319 LDAPMessage
*res
, /* Search result token */
4320 *e
; /* Current entry from search */
4321 char *cn_value
[2], /* Change records */
4327 typestring
[255], /* String to hold printer-type */
4328 dn
[1024]; /* DN of the printer we are adding */
4329 int rc
; /* LDAP status */
4330 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4331 old_location
[1024], /* Printer location */
4332 old_info
[1024], /* Printer information */
4333 old_make_model
[1024], /* Printer make and model */
4334 old_type_string
[30]; /* Temporary type number */
4335 int old_type
; /* Printer type */
4336 static const char * const objectClass_values
[] =
4337 { /* The 3 objectClass's we use in */
4338 "top", /* our LDAP entries */
4345 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4348 * Exit function if LDAP updates has been disabled...
4351 if (!BrowseLDAPUpdate
)
4353 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4354 "send_ldap_browse: Updates temporary disabled; "
4360 * Reconnect if LDAP Handle is invalid...
4363 if (!BrowseLDAPHandle
)
4365 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4366 "send_ldap_browse: LDAP Handle is invalid. Try "
4373 * Everything in ldap is ** so we fudge around it...
4376 sprintf(typestring
, "%u", p
->type
);
4378 cn_value
[0] = p
->name
;
4380 info
[0] = p
->info
? p
->info
: "Unknown";
4382 location
[0] = p
->location
? p
->location
: "Unknown";
4384 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4385 make_model
[1] = NULL
;
4386 type
[0] = typestring
;
4392 * Get ldap entry for printer ...
4395 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4397 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4399 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4400 (char **)ldap_attrs
, 0, &res
);
4403 * If ldap search was not successfull then exit function
4404 * and temporary disable LDAP updates...
4407 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4409 if (BrowseLDAPUpdate
&&
4410 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4412 BrowseLDAPUpdate
= FALSE
;
4413 cupsdLogMessage(CUPSD_LOG_INFO
,
4414 "LDAP update temporary disabled");
4421 * Fill modification array...
4424 mods
[0].mod_type
= "cn";
4425 mods
[0].mod_values
= cn_value
;
4426 mods
[1].mod_type
= "printerDescription";
4427 mods
[1].mod_values
= info
;
4428 mods
[2].mod_type
= "printerURI";
4429 mods
[2].mod_values
= uri
;
4430 mods
[3].mod_type
= "printerLocation";
4431 mods
[3].mod_values
= location
;
4432 mods
[4].mod_type
= "printerMakeAndModel";
4433 mods
[4].mod_values
= make_model
;
4434 mods
[5].mod_type
= "printerType";
4435 mods
[5].mod_values
= type
;
4436 mods
[6].mod_type
= "objectClass";
4437 mods
[6].mod_values
= (char **)objectClass_values
;
4440 * Check if we need to insert or update the LDAP entry...
4443 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4444 rc
!= LDAP_NO_SUCH_OBJECT
)
4447 * Printer has already been registered, check if
4448 * modification is required...
4451 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4454 * Get the required values from this entry...
4457 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4458 old_info
, sizeof(old_info
)) == -1)
4461 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4462 old_location
, sizeof(old_location
)) == -1)
4465 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4466 old_make_model
, sizeof(old_make_model
)) == -1)
4469 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4470 old_type_string
, sizeof(old_type_string
)) == -1)
4473 old_type
= atoi(old_type_string
);
4475 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4476 sizeof(old_uri
)) == -1)
4480 * Check if modification is required...
4483 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4484 !strcmp(location
[0], old_location
) &&
4485 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4488 * LDAP entry for the printer exists. Printer has already been registered,
4489 * no modifications required...
4492 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4493 "send_ldap_browse: No updates required for %s", p
->name
);
4498 * LDAP entry for the printer exists. Printer has already been registered,
4499 * modify the current registration...
4502 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4503 "send_ldap_browse: Replace entry for %s", p
->name
);
4505 for (i
= 0; i
< 7; i
++)
4507 pmods
[i
] = mods
+ i
;
4508 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4512 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4513 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4514 NULL
)) != LDAP_SUCCESS
)
4516 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4517 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4519 cupsdLogMessage(CUPSD_LOG_ERROR
,
4520 "LDAP modify for %s failed with status %d: %s",
4521 p
->name
, rc
, ldap_err2string(rc
));
4522 if (rc
== LDAP_SERVER_DOWN
)
4530 * No LDAP entry exists for the printer. Printer has never been registered,
4531 * add the current registration...
4534 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4536 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4537 "send_ldap_browse: Add entry for %s", p
->name
);
4539 for (i
= 0; i
< 7; i
++)
4541 pmods
[i
] = mods
+ i
;
4542 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4546 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4547 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4548 NULL
)) != LDAP_SUCCESS
)
4550 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4551 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4553 cupsdLogMessage(CUPSD_LOG_ERROR
,
4554 "LDAP add for %s failed with status %d: %s",
4555 p
->name
, rc
, ldap_err2string(rc
));
4556 if (rc
== LDAP_SERVER_DOWN
)
4566 * 'ldap_dereg_printer()' - Delete printer from directory
4570 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4572 char dn
[1024]; /* DN of the printer */
4573 int rc
; /* LDAP status */
4576 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4580 * Reconnect if LDAP Handle is invalid...
4583 if (!BrowseLDAPHandle
)
4590 * Get dn for printer and delete LDAP entry...
4593 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4595 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4597 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4598 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4599 NULL
)) != LDAP_SUCCESS
)
4601 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4602 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4604 cupsdLogMessage(CUPSD_LOG_WARN
,
4605 "LDAP delete for %s failed with status %d: %s",
4606 p
->name
, rc
, ldap_err2string(rc
));
4609 * If we had a connection problem (connection timed out, etc.)
4610 * we should reconnect and try again to delete the entry...
4613 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4615 cupsdLogMessage(CUPSD_LOG_INFO
,
4616 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4619 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4620 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4621 NULL
)) != LDAP_SUCCESS
)
4623 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4624 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4625 cupsdLogMessage(CUPSD_LOG_WARN
,
4626 "LDAP delete for %s failed with status %d: %s",
4627 p
->name
, rc
, ldap_err2string(rc
));
4634 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4635 char *basedn
) /* I - Dase dn */
4637 char dn
[1024]; /* DN of the printer */
4638 int rc
; /* LDAP status */
4641 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4644 * Reconnect if LDAP Handle is invalid...
4647 if (!BrowseLDAPHandle
)
4654 * Get dn for printer and delete LDAP entry...
4657 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4658 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4660 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4661 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4662 NULL
)) != LDAP_SUCCESS
)
4664 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4665 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4667 cupsdLogMessage(CUPSD_LOG_WARN
,
4668 "LDAP delete for %s failed with status %d: %s",
4669 ou
, rc
, ldap_err2string(rc
));
4672 * If we had a connection problem (connection timed out, etc.)
4673 * we should reconnect and try again to delete the entry...
4676 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4678 cupsdLogMessage(CUPSD_LOG_INFO
,
4679 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4681 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4682 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4683 NULL
)) != LDAP_SUCCESS
)
4685 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4686 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4687 cupsdLogMessage(CUPSD_LOG_WARN
,
4688 "LDAP delete for %s failed with status %d: %s",
4689 ou
, rc
, ldap_err2string(rc
));
4694 #endif /* HAVE_LDAP */
4699 * 'send_slp_browse()' - Register the specified printer with SLP.
4703 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4705 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4706 attrs
[8192], /* Printer attributes */
4707 finishings
[1024], /* Finishings to support */
4708 make_model
[IPP_MAX_NAME
* 2],
4709 /* Make and model, quoted */
4710 location
[IPP_MAX_NAME
* 2],
4711 /* Location, quoted */
4712 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4713 *src
, /* Pointer to original string */
4714 *dst
; /* Pointer to destination string */
4715 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4716 SLPError error
; /* SLP error, if any */
4719 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4723 * Make the SLP service URL that conforms to the IANA
4724 * 'printer:' template.
4727 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4729 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4732 * Figure out the finishings string...
4735 if (p
->type
& CUPS_PRINTER_STAPLE
)
4736 strcpy(finishings
, "staple");
4738 finishings
[0] = '\0';
4740 if (p
->type
& CUPS_PRINTER_BIND
)
4743 strlcat(finishings
, ",bind", sizeof(finishings
));
4745 strcpy(finishings
, "bind");
4748 if (p
->type
& CUPS_PRINTER_PUNCH
)
4751 strlcat(finishings
, ",punch", sizeof(finishings
));
4753 strcpy(finishings
, "punch");
4756 if (p
->type
& CUPS_PRINTER_COVER
)
4759 strlcat(finishings
, ",cover", sizeof(finishings
));
4761 strcpy(finishings
, "cover");
4764 if (p
->type
& CUPS_PRINTER_SORT
)
4767 strlcat(finishings
, ",sort", sizeof(finishings
));
4769 strcpy(finishings
, "sort");
4773 strcpy(finishings
, "none");
4776 * Quote any commas in the make and model, location, and info strings...
4779 for (src
= p
->make_model
, dst
= make_model
;
4780 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4782 if (*src
== ',' || *src
== '\\' || *src
== ')')
4791 strcpy(make_model
, "Unknown");
4793 for (src
= p
->location
, dst
= location
;
4794 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4796 if (*src
== ',' || *src
== '\\' || *src
== ')')
4805 strcpy(location
, "Unknown");
4807 for (src
= p
->info
, dst
= info
;
4808 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4810 if (*src
== ',' || *src
== '\\' || *src
== ')')
4819 strcpy(info
, "Unknown");
4822 * Get the authentication value...
4825 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4829 * Make the SLP attribute string list that conforms to
4830 * the IANA 'printer:' template.
4833 snprintf(attrs
, sizeof(attrs
),
4834 "(printer-uri-supported=%s),"
4835 "(uri-authentication-supported=%s>),"
4837 "(uri-security-supported=tls>),"
4839 "(uri-security-supported=none>),"
4840 #endif /* HAVE_SSL */
4841 "(printer-name=%s),"
4842 "(printer-location=%s),"
4843 "(printer-info=%s),"
4844 "(printer-more-info=%s),"
4845 "(printer-make-and-model=%s),"
4846 "(printer-type=%d),"
4847 "(charset-supported=utf-8),"
4848 "(natural-language-configured=%s),"
4849 "(natural-language-supported=de,en,es,fr,it),"
4850 "(color-supported=%s),"
4851 "(finishings-supported=%s),"
4852 "(sides-supported=one-sided%s),"
4853 "(multiple-document-jobs-supported=true)"
4854 "(ipp-versions-supported=1.0,1.1)",
4855 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4856 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4857 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4859 p
->type
& CUPS_PRINTER_DUPLEX
?
4860 ",two-sided-long-edge,two-sided-short-edge" : "");
4862 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4865 * Register the printer with the SLP server...
4868 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4869 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4871 if (error
!= SLP_OK
)
4872 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4878 * 'slp_attr_callback()' - SLP attribute callback
4881 static SLPBoolean
/* O - SLP_TRUE for success */
4883 SLPHandle hslp
, /* I - SLP handle */
4884 const char *attrlist
, /* I - Attribute list */
4885 SLPError errcode
, /* I - Parsing status for this attr */
4886 void *cookie
) /* I - Current printer */
4888 char *tmp
= 0; /* Temporary string */
4889 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4890 /* Current printer */
4893 (void)hslp
; /* anti-compiler-warning-code */
4896 * Bail if there was an error
4899 if (errcode
!= SLP_OK
)
4903 * Parse the attrlist to obtain things needed to build CUPS browse packet
4906 memset(p
, 0, sizeof(cupsd_printer_t
));
4908 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4910 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4912 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4914 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4915 p
->type
= atoi(tmp
);
4917 p
->type
= CUPS_PRINTER_REMOTE
;
4919 cupsdClearString(&tmp
);
4926 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4930 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4932 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4935 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4937 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4940 * Make the SLP service URL that conforms to the IANA
4941 * 'printer:' template.
4944 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4947 * Deregister the printer...
4950 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4956 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4959 static int /* O - 0 on success */
4960 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4961 const char *tag
, /* I - Name of attribute */
4962 char **valbuf
) /* O - Value */
4964 char *ptr1
, /* Pointer into string */
4968 cupsdClearString(valbuf
);
4970 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4972 ptr1
+= strlen(tag
);
4974 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4980 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4981 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4984 * Dequote the value...
4987 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4988 if (*ptr1
== '\\' && ptr1
[1])
4989 _cups_strcpy(ptr1
, ptr1
+ 1);
5000 * 'slp_reg_callback()' - Empty SLPRegReport.
5004 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
5005 SLPError errcode
, /* I - Error code, if any */
5006 void *cookie
) /* I - App data */
5017 * 'slp_url_callback()' - SLP service url callback
5020 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
5022 SLPHandle hslp
, /* I - SLP handle */
5023 const char *srvurl
, /* I - URL of service */
5024 unsigned short lifetime
, /* I - Life of service */
5025 SLPError errcode
, /* I - Existing error code */
5026 void *cookie
) /* I - Pointer to service list */
5028 slpsrvurl_t
*s
, /* New service entry */
5029 **head
; /* Pointer to head of entry */
5033 * Let the compiler know we won't be using these vars...
5040 * Bail if there was an error
5043 if (errcode
!= SLP_OK
)
5047 * Grab the head of the list...
5050 head
= (slpsrvurl_t
**)cookie
;
5053 * Allocate a *temporary* slpsrvurl_t to hold this entry.
5056 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
5060 * Copy the SLP service URL...
5063 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
5066 * Link the SLP service URL into the head of the list
5076 #endif /* HAVE_LIBSLP */
5080 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
5084 update_cups_browse(void)
5086 int i
; /* Looping var */
5087 int auth
; /* Authorization status */
5088 int len
; /* Length of name string */
5089 int bytes
; /* Number of bytes left */
5090 char packet
[1541], /* Broadcast packet */
5091 *pptr
; /* Pointer into packet */
5092 socklen_t srclen
; /* Length of source address */
5093 http_addr_t srcaddr
; /* Source address */
5094 char srcname
[1024]; /* Source hostname */
5095 unsigned address
[4]; /* Source address */
5096 unsigned type
; /* Printer type */
5097 unsigned state
; /* Printer state */
5098 char uri
[HTTP_MAX_URI
], /* Printer URI */
5099 host
[HTTP_MAX_URI
], /* Host portion of URI */
5100 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
5101 info
[IPP_MAX_NAME
], /* Information string */
5102 location
[IPP_MAX_NAME
], /* Location string */
5103 make_model
[IPP_MAX_NAME
];/* Make and model string */
5104 int num_attrs
; /* Number of attributes */
5105 cups_option_t
*attrs
; /* Attributes */
5109 * Read a packet from the browse socket...
5112 srclen
= sizeof(srcaddr
);
5113 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5114 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5117 * "Connection refused" is returned under Linux if the destination port
5118 * or address is unreachable from a previous sendto(); check for the
5119 * error here and ignore it for now...
5122 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5124 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5126 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5129 closesocket(BrowseSocket
);
5131 close(BrowseSocket
);
5134 cupsdRemoveSelect(BrowseSocket
);
5137 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5138 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5144 packet
[bytes
] = '\0';
5147 * If we're about to sleep, ignore incoming browse packets.
5154 * Figure out where it came from...
5158 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5160 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5161 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5162 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5163 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5166 #endif /* AF_INET6 */
5171 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5174 if (HostNameLookups
)
5175 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5177 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5179 len
= strlen(srcname
);
5187 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5190 * Access from localhost (127.0.0.1) is always allowed...
5193 auth
= CUPSD_AUTH_ALLOW
;
5198 * Do authorization checks on the domain/address...
5201 switch (BrowseACL
->order_type
)
5204 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5207 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5208 auth
= CUPSD_AUTH_ALLOW
;
5210 if (cupsdCheckAuth(address
, srcname
, len
,
5211 BrowseACL
->num_deny
, BrowseACL
->deny
))
5212 auth
= CUPSD_AUTH_DENY
;
5214 if (cupsdCheckAuth(address
, srcname
, len
,
5215 BrowseACL
->num_allow
, BrowseACL
->allow
))
5216 auth
= CUPSD_AUTH_ALLOW
;
5219 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5220 auth
= CUPSD_AUTH_DENY
;
5222 if (cupsdCheckAuth(address
, srcname
, len
,
5223 BrowseACL
->num_allow
, BrowseACL
->allow
))
5224 auth
= CUPSD_AUTH_ALLOW
;
5226 if (cupsdCheckAuth(address
, srcname
, len
,
5227 BrowseACL
->num_deny
, BrowseACL
->deny
))
5228 auth
= CUPSD_AUTH_DENY
;
5234 auth
= CUPSD_AUTH_ALLOW
;
5236 if (auth
== CUPSD_AUTH_DENY
)
5238 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5239 "update_cups_browse: Refused %d bytes from %s", bytes
,
5244 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5245 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5252 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5254 cupsdLogMessage(CUPSD_LOG_WARN
,
5255 "update_cups_browse: Garbled browse packet - %s", packet
);
5259 strcpy(location
, "Location Unknown");
5260 strcpy(info
, "No Information Available");
5261 make_model
[0] = '\0';
5265 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5268 * Have extended information; can't use sscanf for it because not all
5269 * sscanf's allow empty strings with %[^\"]...
5272 for (i
= 0, pptr
++;
5273 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5275 location
[i
] = *pptr
;
5283 while (*pptr
&& isspace(*pptr
& 255))
5288 for (i
= 0, pptr
++;
5289 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5298 while (*pptr
&& isspace(*pptr
& 255))
5303 for (i
= 0, pptr
++;
5304 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5306 make_model
[i
] = *pptr
;
5311 make_model
[i
] = '\0';
5314 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5320 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5321 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5322 type
, state
, uri
, location
, info
, make_model
));
5325 * Pull the URI apart to see if this is a local or remote printer...
5328 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5330 cupsFreeOptions(num_attrs
, attrs
);
5338 for (i
= 0; i
< NumRelays
; i
++)
5339 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5340 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5341 (struct sockaddr
*)&(Relays
[i
].to
),
5342 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5344 cupsdLogMessage(CUPSD_LOG_ERROR
,
5345 "update_cups_browse: sendto failed for relay %d - %s.",
5346 i
+ 1, strerror(errno
));
5347 cupsFreeOptions(num_attrs
, attrs
);
5352 * Process the browse data...
5355 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5356 (ipp_pstate_t
)state
, location
, info
, make_model
,
5362 * 'update_lpd()' - Update the LPD configuration as needed.
5366 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5373 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5374 * setting for backwards-compatibility.
5377 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5379 #endif /* __APPLE__ */
5381 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5384 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5387 char newfile
[1024]; /* New cups-lpd.N file */
5388 cups_file_t
*ofp
, /* Original file pointer */
5389 *nfp
; /* New file pointer */
5390 char line
[1024]; /* Line from file */
5393 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5395 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5397 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5398 LPDConfigFile
+ 9, strerror(errno
));
5402 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5404 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5405 newfile
, strerror(errno
));
5411 * Copy all of the lines from the cups-lpd file...
5414 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5418 cupsFilePrintf(nfp
, "%s\n", line
);
5419 snprintf(line
, sizeof(line
), "\tdisable = %s",
5420 onoff
? "no" : "yes");
5422 else if (!strstr(line
, "disable ="))
5423 cupsFilePrintf(nfp
, "%s\n", line
);
5428 rename(newfile
, LPDConfigFile
+ 9);
5431 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5434 * Enable/disable LPD via the launchctl command...
5437 char *argv
[5], /* Arguments for command */
5438 *envp
[MAX_ENV
]; /* Environment for command */
5439 int pid
; /* Process ID */
5442 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5443 argv
[0] = (char *)"launchctl";
5444 argv
[1] = (char *)(onoff
? "load" : "unload");
5445 argv
[2] = (char *)"-w";
5446 argv
[3] = LPDConfigFile
+ 10;
5449 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5452 #endif /* __APPLE__ */
5454 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5459 * 'update_polling()' - Read status messages from the poll daemons.
5463 update_polling(void)
5465 char *ptr
, /* Pointer to end of line in buffer */
5466 message
[1024]; /* Pointer to message text */
5467 int loglevel
; /* Log level for message */
5470 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5471 message
, sizeof(message
))) != NULL
)
5473 if (loglevel
== CUPSD_LOG_INFO
)
5474 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5476 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5480 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5483 * All polling processes have died; stop polling...
5486 cupsdLogMessage(CUPSD_LOG_ERROR
,
5487 "update_polling: all polling processes have exited!");
5494 * 'update_smb()' - Update the SMB configuration as needed.
5498 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5503 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5506 * Enable/disable SMB via the specified smb.conf config file...
5509 char newfile
[1024]; /* New smb.conf.N file */
5510 cups_file_t
*ofp
, /* Original file pointer */
5511 *nfp
; /* New file pointer */
5512 char line
[1024]; /* Line from file */
5513 int in_printers
; /* In [printers] section? */
5516 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5518 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5520 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5521 SMBConfigFile
+ 8, strerror(errno
));
5525 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5527 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5528 newfile
, strerror(errno
));
5534 * Copy all of the lines from the smb.conf file...
5539 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5541 if (in_printers
&& strstr(line
, "printable ="))
5542 snprintf(line
, sizeof(line
), " printable = %s",
5543 onoff
? "yes" : "no");
5545 cupsFilePrintf(nfp
, "%s\n", line
);
5548 in_printers
= !strcmp(line
, "[printers]");
5553 rename(newfile
, SMBConfigFile
+ 8);
5556 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5561 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".