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 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1562 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1565 * Then get the port we use for registrations. If we are not listening
1566 * on any non-local ports, there is no sense sharing local printers via
1572 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1574 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1576 if (httpAddrLocalhost(&(lis
->address
)))
1579 if (lis
->address
.addr
.sa_family
== AF_INET
)
1581 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1584 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1586 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1592 * Create an array to track the printers we share...
1595 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1596 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1600 * Set the computer name and register the web interface...
1603 cupsdUpdateDNSSDName();
1606 #endif /* HAVE_DNSSD */
1609 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1612 * Open SLP handle...
1615 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1617 cupsdLogMessage(CUPSD_LOG_ERROR
,
1618 "Unable to open an SLP handle; disabling SLP browsing!");
1619 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1620 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1621 BrowseSLPHandle
= NULL
;
1623 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1624 cupsdEndProcess(getpid(), 0);
1627 BrowseSLPRefresh
= 0;
1630 BrowseSLPHandle
= NULL
;
1631 #endif /* HAVE_LIBSLP */
1634 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1638 cupsdLogMessage(CUPSD_LOG_ERROR
,
1639 "Need to set BrowseLDAPDN to use LDAP browsing!");
1640 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1641 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1643 if (FatalErrors
& CUPSD_FATAL_BROWSE
)
1644 cupsdEndProcess(getpid(), 0);
1649 * Open LDAP handle...
1652 if ((BrowseLDAPHandle
= ldap_connect()) == NULL
&&
1653 (FatalErrors
& CUPSD_FATAL_BROWSE
))
1654 cupsdEndProcess(getpid(), 0);
1657 BrowseLDAPRefresh
= 0;
1659 #endif /* HAVE_LDAP */
1662 * Enable LPD and SMB printer sharing as needed through external programs...
1665 if (BrowseLocalProtocols
& BROWSE_LPD
)
1668 if (BrowseLocalProtocols
& BROWSE_SMB
)
1672 * Register the individual printers
1675 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1677 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1678 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1679 CUPS_PRINTER_SCANNER
)))
1680 cupsdRegisterPrinter(p
);
1685 * 'cupsdStartPolling()' - Start polling servers as needed.
1689 cupsdStartPolling(void)
1691 int i
; /* Looping var */
1692 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1693 char polld
[1024]; /* Poll daemon path */
1694 char sport
[255]; /* Server port */
1695 char bport
[255]; /* Browser port */
1696 char interval
[255]; /* Poll interval */
1697 int statusfds
[2]; /* Status pipe */
1698 char *argv
[6]; /* Arguments */
1699 char *envp
[100]; /* Environment */
1703 * Don't do anything if we aren't polling...
1706 if (NumPolled
== 0 || BrowseSocket
< 0)
1709 PollStatusBuffer
= NULL
;
1714 * Setup string arguments for polld, port and interval options.
1717 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1719 sprintf(bport
, "%d", BrowsePort
);
1722 sprintf(interval
, "%d", BrowseInterval
);
1724 strcpy(interval
, "30");
1726 argv
[0] = "cups-polld";
1732 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1735 * Create a pipe that receives the status messages from each
1739 if (cupsdOpenPipe(statusfds
))
1741 cupsdLogMessage(CUPSD_LOG_ERROR
,
1742 "Unable to create polling status pipes - %s.",
1745 PollStatusBuffer
= NULL
;
1749 PollPipe
= statusfds
[0];
1750 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1753 * Run each polling daemon, redirecting stderr to the polling pipe...
1756 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1758 sprintf(sport
, "%d", pollp
->port
);
1760 argv
[1] = pollp
->hostname
;
1762 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1763 0, DefaultProfile
, NULL
, &(pollp
->pid
)) < 0)
1765 cupsdLogMessage(CUPSD_LOG_ERROR
,
1766 "cupsdStartPolling: Unable to fork polling daemon - %s",
1772 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1773 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1774 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1777 close(statusfds
[1]);
1780 * Finally, add the pipe to the input selection set...
1783 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1788 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1792 cupsdStopBrowsing(void)
1794 cupsd_printer_t
*p
; /* Current printer */
1797 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1801 * De-register the individual printers
1804 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1806 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1807 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
|
1808 CUPS_PRINTER_SCANNER
)))
1809 cupsdDeregisterPrinter(p
, 1);
1812 * Shut down browsing sockets...
1815 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1819 * Close the socket and remove it from the input selection set.
1823 closesocket(BrowseSocket
);
1825 close(BrowseSocket
);
1828 cupsdRemoveSelect(BrowseSocket
);
1833 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1835 #endif /* HAVE_DNSSD */
1838 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1842 * Close SLP handle...
1845 SLPClose(BrowseSLPHandle
);
1846 BrowseSLPHandle
= NULL
;
1848 #endif /* HAVE_LIBSLP */
1851 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1854 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1855 ldap_disconnect(BrowseLDAPHandle
);
1856 BrowseLDAPHandle
= NULL
;
1858 #endif /* HAVE_OPENLDAP */
1861 * Disable LPD and SMB printer sharing as needed through external programs...
1864 if (BrowseLocalProtocols
& BROWSE_LPD
)
1867 if (BrowseLocalProtocols
& BROWSE_SMB
)
1873 * 'cupsdStopPolling()' - Stop polling servers as needed.
1877 cupsdStopPolling(void)
1879 int i
; /* Looping var */
1880 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1885 cupsdStatBufDelete(PollStatusBuffer
);
1888 cupsdRemoveSelect(PollPipe
);
1891 PollStatusBuffer
= NULL
;
1894 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1896 cupsdEndProcess(pollp
->pid
, 0);
1902 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1906 cupsdUpdateDNSSDName(void)
1908 DNSServiceErrorType error
; /* Error from service creation */
1909 char webif
[1024]; /* Web interface share name */
1910 #ifdef HAVE_COREFOUNDATION_H
1911 SCDynamicStoreRef sc
; /* Context for dynamic store */
1912 CFDictionaryRef btmm
; /* Back-to-My-Mac domains */
1913 CFStringRef nameRef
; /* Host name CFString */
1914 char nameBuffer
[1024]; /* C-string buffer */
1915 #endif /* HAVE_COREFOUNDATION_H */
1919 * Only share the web interface and printers when non-local listening is
1928 * Get the computer name as a c-string...
1931 #ifdef HAVE_COREFOUNDATION_H
1932 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1937 * Get the computer name from the dynamic store...
1940 cupsdClearString(&DNSSDName
);
1942 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1944 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1945 kCFStringEncodingUTF8
))
1947 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1948 "Dynamic store host name is \"%s\".", nameBuffer
);
1949 cupsdSetString(&DNSSDName
, nameBuffer
);
1958 * Use the ServerName instead...
1961 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1962 "Using ServerName \"%s\" as host name.", ServerName
);
1963 cupsdSetString(&DNSSDName
, ServerName
);
1967 * Get any Back-to-My-Mac domains and add them as aliases...
1970 cupsdFreeAliases(DNSSDAlias
);
1973 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
1974 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
1976 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
1977 (int)CFDictionaryGetCount(btmm
));
1978 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
1981 cupsdLogMessage(CUPSD_LOG_ERROR
,
1982 "Bad Back to My Mac data in dynamic store!");
1984 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
1992 #endif /* HAVE_COREFOUNDATION_H */
1993 cupsdSetString(&DNSSDName
, ServerName
);
1996 * Then (re)register the web interface if enabled...
2002 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
2004 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2007 DNSServiceRefDeallocate(WebIFRef
);
2009 WebIFRef
= DNSSDRef
;
2010 if ((error
= DNSServiceRegister(&WebIFRef
,
2011 kDNSServiceFlagsShareConnection
,
2012 0, webif
, "_http._tcp", NULL
,
2013 NULL
, htons(DNSSDPort
), 7,
2014 "\006path=/", dnssdRegisterCallback
,
2015 NULL
)) != kDNSServiceErr_NoError
)
2016 cupsdLogMessage(CUPSD_LOG_ERROR
,
2017 "DNS-SD web interface registration failed: %d", error
);
2020 #endif /* HAVE_DNSSD */
2025 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2029 cupsdUpdateLDAPBrowse(void)
2031 char uri
[HTTP_MAX_URI
], /* Printer URI */
2032 host
[HTTP_MAX_URI
], /* Hostname */
2033 resource
[HTTP_MAX_URI
], /* Resource path */
2034 location
[1024], /* Printer location */
2035 info
[1024], /* Printer information */
2036 make_model
[1024], /* Printer make and model */
2037 type_num
[30]; /* Printer type number */
2038 int type
; /* Printer type */
2039 int rc
; /* LDAP status */
2040 int limit
; /* Size limit */
2041 LDAPMessage
*res
, /* LDAP search results */
2042 *e
; /* Current entry from search */
2044 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2046 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2049 * Reconnect if LDAP Handle is invalid...
2052 if (! BrowseLDAPHandle
)
2059 * Search for cups printers in LDAP directory...
2062 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2063 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2066 * If ldap search was successfull then exit function
2067 * and temporary disable LDAP updates...
2070 if (rc
!= LDAP_SUCCESS
)
2072 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2074 BrowseLDAPUpdate
= FALSE
;
2075 cupsdLogMessage(CUPSD_LOG_INFO
,
2076 "LDAP update temporary disabled");
2082 * If LDAP updates were disabled, we will reenable them...
2085 if (! BrowseLDAPUpdate
)
2087 BrowseLDAPUpdate
= TRUE
;
2088 cupsdLogMessage(CUPSD_LOG_INFO
,
2089 "LDAP update enabled");
2093 * Count LDAP entries and return if no entry exist...
2096 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2097 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2105 * Loop through the available printers...
2108 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2110 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2113 * Get the required values from this entry...
2116 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2117 "printerDescription", info
, sizeof(info
)) == -1)
2120 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2121 "printerLocation", location
, sizeof(location
)) == -1)
2124 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2125 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2128 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2129 "printerType", type_num
, sizeof(type_num
)) == -1)
2132 type
= atoi(type_num
);
2134 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2135 "printerURI", uri
, sizeof(uri
)) == -1)
2139 * Process the entry as browse data...
2142 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2143 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2144 location
, info
, make_model
, 0, NULL
);
2150 #endif /* HAVE_LDAP */
2155 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2159 cupsdUpdateSLPBrowse(void)
2161 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2162 *next
; /* Next service in list */
2163 cupsd_printer_t p
; /* Printer information */
2164 const char *uri
; /* Pointer to printer URI */
2165 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2166 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2170 * Reset the refresh time...
2173 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2176 * Poll for remote printers using SLP...
2181 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2182 slp_url_callback
, &s
);
2185 * Loop through the list of available printers...
2191 * Save the "next" pointer...
2197 * Load a cupsd_printer_t structure with the SLP service attributes...
2200 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2203 * Process this printer entry...
2206 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2208 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2211 * Pull the URI apart to see if this is a local or remote printer...
2214 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2215 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2216 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2220 * Free this listing...
2223 cupsdClearString(&p
.info
);
2224 cupsdClearString(&p
.location
);
2225 cupsdClearString(&p
.make_model
);
2230 #endif /* HAVE_LIBSLP */
2234 * 'dequote()' - Remote quotes from a string.
2237 static char * /* O - Dequoted string */
2238 dequote(char *d
, /* I - Destination string */
2239 const char *s
, /* I - Source string */
2240 int dlen
) /* I - Destination length */
2242 char *dptr
; /* Pointer into destination */
2247 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2264 # ifdef HAVE_COREFOUNDATION
2266 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2270 dnssdAddAlias(const void *key
, /* I - Key */
2271 const void *value
, /* I - Value (domain) */
2272 void *context
) /* I - Unused */
2274 char valueStr
[1024], /* Domain string */
2275 hostname
[1024]; /* Complete hostname */
2280 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2281 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2282 kCFStringEncodingUTF8
))
2284 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDName
, valueStr
);
2286 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2288 cupsdAddAlias(DNSSDAlias
, hostname
);
2289 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2293 cupsdLogMessage(CUPSD_LOG_ERROR
,
2294 "Bad Back to My Mac domain in dynamic store!");
2296 # endif /* HAVE_COREFOUNDATION */
2300 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2303 static char * /* O - TXT record */
2304 dnssdBuildTxtRecord(
2305 int *txt_len
, /* O - TXT record length */
2306 cupsd_printer_t
*p
, /* I - Printer information */
2307 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2309 int i
; /* Looping var */
2310 char type_str
[32], /* Type to string buffer */
2311 state_str
[32], /* State to string buffer */
2312 rp_str
[1024], /* Queue name string buffer */
2313 air_str
[1024], /* auth-info-required string buffer */
2314 *keyvalue
[32][2]; /* Table of key/value pairs */
2318 * Load up the key value pairs...
2323 keyvalue
[i
][0] = "txtvers";
2324 keyvalue
[i
++][1] = "1";
2326 keyvalue
[i
][0] = "qtotal";
2327 keyvalue
[i
++][1] = "1";
2329 keyvalue
[i
][0] = "rp";
2330 keyvalue
[i
++][1] = rp_str
;
2332 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2334 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2335 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2337 keyvalue
[i
][0] = "ty";
2338 keyvalue
[i
++][1] = p
->make_model
;
2340 if (p
->location
&& *p
->location
!= '\0')
2342 keyvalue
[i
][0] = "note";
2343 keyvalue
[i
++][1] = p
->location
;
2346 keyvalue
[i
][0] = "priority";
2347 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2349 keyvalue
[i
][0] = "product";
2350 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2352 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2353 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2355 keyvalue
[i
][0] = "printer-state";
2356 keyvalue
[i
++][1] = state_str
;
2358 keyvalue
[i
][0] = "printer-type";
2359 keyvalue
[i
++][1] = type_str
;
2361 keyvalue
[i
][0] = "Transparent";
2362 keyvalue
[i
++][1] = "T";
2364 keyvalue
[i
][0] = "Binary";
2365 keyvalue
[i
++][1] = "T";
2367 if ((p
->type
& CUPS_PRINTER_FAX
))
2369 keyvalue
[i
][0] = "Fax";
2370 keyvalue
[i
++][1] = "T";
2373 if ((p
->type
& CUPS_PRINTER_COLOR
))
2375 keyvalue
[i
][0] = "Color";
2376 keyvalue
[i
++][1] = "T";
2379 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2381 keyvalue
[i
][0] = "Duplex";
2382 keyvalue
[i
++][1] = "T";
2385 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2387 keyvalue
[i
][0] = "Staple";
2388 keyvalue
[i
++][1] = "T";
2391 if ((p
->type
& CUPS_PRINTER_COPIES
))
2393 keyvalue
[i
][0] = "Copies";
2394 keyvalue
[i
++][1] = "T";
2397 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2399 keyvalue
[i
][0] = "Collate";
2400 keyvalue
[i
++][1] = "T";
2403 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2405 keyvalue
[i
][0] = "Punch";
2406 keyvalue
[i
++][1] = "T";
2409 if ((p
->type
& CUPS_PRINTER_BIND
))
2411 keyvalue
[i
][0] = "Bind";
2412 keyvalue
[i
++][1] = "T";
2415 if ((p
->type
& CUPS_PRINTER_SORT
))
2417 keyvalue
[i
][0] = "Sort";
2418 keyvalue
[i
++][1] = "T";
2421 keyvalue
[i
][0] = "pdl";
2422 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2424 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
2426 keyvalue
[i
][0] = "air";
2427 keyvalue
[i
++][1] = air_str
;
2431 * Then pack them into a proper txt record...
2434 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2439 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2442 static int /* O - Result of comparison */
2443 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2444 cupsd_printer_t
*b
)/* I - Second printer */
2446 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2451 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2456 dnssdDeregisterPrinter(
2457 cupsd_printer_t
*p
) /* I - Printer */
2459 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2462 * Closing the socket deregisters the service
2467 DNSServiceRefDeallocate(p
->ipp_ref
);
2474 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2483 DNSServiceRefDeallocate(p
->printer_ref
);
2484 p
->printer_ref
= NULL
;
2490 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2493 free(p
->printer_txt
);
2494 p
->printer_txt
= NULL
;
2498 * Remove the printer from the array of DNS-SD printers, then clear the
2499 * registered name...
2502 cupsArrayRemove(DNSSDPrinters
, p
);
2503 cupsdClearString(&p
->reg_name
);
2508 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2509 * TXT record format.
2512 static char * /* O - TXT record */
2513 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2514 char *keyvalue
[][2], /* I - Table of key value pairs */
2515 int count
) /* I - Items in table */
2517 int i
; /* Looping var */
2518 int length
; /* Length of TXT record */
2519 int length2
; /* Length of value */
2520 char *txtRecord
; /* TXT record buffer */
2521 char *cursor
; /* Looping pointer */
2525 * Calculate the buffer size
2528 for (length
= i
= 0; i
< count
; i
++)
2529 length
+= 1 + strlen(keyvalue
[i
][0]) +
2530 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2533 * Allocate and fill it
2536 txtRecord
= malloc(length
);
2541 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2544 * Drop in the p-string style length byte followed by the data
2547 length
= strlen(keyvalue
[i
][0]);
2548 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2550 *cursor
++ = (unsigned char)(length
+ length2
);
2552 memcpy(cursor
, keyvalue
[i
][0], length
);
2559 memcpy(cursor
, keyvalue
[i
][1], length2
);
2570 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2574 dnssdRegisterCallback(
2575 DNSServiceRef sdRef
, /* I - DNS Service reference */
2576 DNSServiceFlags flags
, /* I - Reserved for future use */
2577 DNSServiceErrorType errorCode
, /* I - Error code */
2578 const char *name
, /* I - Service name */
2579 const char *regtype
, /* I - Service type */
2580 const char *domain
, /* I - Domain. ".local" for now */
2581 void *context
) /* I - User-defined context */
2583 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2584 /* Current printer */
2587 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2588 name
, regtype
, p
? p
->name
: "Web Interface",
2589 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2593 cupsdLogMessage(CUPSD_LOG_ERROR
,
2594 "DNSServiceRegister failed with error %d", (int)errorCode
);
2597 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2599 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2602 cupsArrayRemove(DNSSDPrinters
, p
);
2603 cupsdSetString(&p
->reg_name
, name
);
2604 cupsArrayAdd(DNSSDPrinters
, p
);
2606 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2612 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2613 * or update the broadcast contents.
2617 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2619 DNSServiceErrorType se
; /* dnssd errors */
2620 char *ipp_txt
, /* IPP TXT record buffer */
2621 *printer_txt
, /* LPD TXT record buffer */
2622 name
[1024], /* Service name */
2623 *nameptr
; /* Pointer into name */
2624 int ipp_len
, /* IPP TXT record length */
2625 printer_len
; /* LPD TXT record length */
2626 const char *regtype
; /* Registration type */
2629 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2630 !p
->ipp_ref
? "new" : "update");
2633 * If per-printer sharing was just disabled make sure we're not
2634 * registered before returning.
2639 dnssdDeregisterPrinter(p
);
2644 * The registered name takes the form of "<printer-info> @ <computer name>"...
2647 if (p
->info
&& strlen(p
->info
) > 0)
2650 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2652 strlcpy(name
, p
->info
, sizeof(name
));
2655 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2657 strlcpy(name
, p
->name
, sizeof(name
));
2660 * If an existing printer was renamed, unregister it and start over...
2663 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2664 dnssdDeregisterPrinter(p
);
2668 cupsdSetString(&p
->reg_name
, name
);
2669 cupsArrayAdd(DNSSDPrinters
, p
);
2673 * Register IPP and (optionally) LPD...
2676 ipp_len
= 0; /* anti-compiler-warning-code */
2677 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2680 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2683 * Update the existing registration...
2686 /* A TTL of 0 means use record's original value (Radar 3176248) */
2687 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2688 0)) == kDNSServiceErr_NoError
)
2693 p
->ipp_txt
= ipp_txt
;
2694 p
->ipp_len
= ipp_len
;
2700 * Failed to update record, lets close this reference and move on...
2703 cupsdLogMessage(CUPSD_LOG_ERROR
,
2704 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2707 DNSServiceRefDeallocate(p
->ipp_ref
);
2715 * Initial registration. Use the _fax subtype for fax queues...
2718 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2721 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2722 "Registering DNS-SD printer %s with name \"%s\" and "
2723 "type \"%s\"", p
->name
, name
, regtype
);
2726 * Register the queue, dropping characters as needed until we succeed...
2729 nameptr
= name
+ strlen(name
);
2733 p
->ipp_ref
= DNSSDRef
;
2734 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2735 0, name
, regtype
, NULL
, NULL
,
2736 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2737 dnssdRegisterCallback
,
2738 p
)) == kDNSServiceErr_BadParam
)
2741 * Name is too long, drop trailing characters, taking into account
2747 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2754 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2756 if (se
== kDNSServiceErr_NoError
)
2758 p
->ipp_txt
= ipp_txt
;
2759 p
->ipp_len
= ipp_len
;
2763 cupsdLogMessage(CUPSD_LOG_WARN
,
2764 "DNS-SD IPP registration of \"%s\" failed: %d",
2771 if (BrowseLocalProtocols
& BROWSE_LPD
)
2773 printer_len
= 0; /* anti-compiler-warning-code */
2774 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2776 if (p
->printer_ref
&&
2777 (printer_len
!= p
->printer_len
||
2778 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2781 * Update the existing registration...
2784 /* A TTL of 0 means use record's original value (Radar 3176248) */
2785 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2787 0)) == kDNSServiceErr_NoError
)
2790 free(p
->printer_txt
);
2792 p
->printer_txt
= printer_txt
;
2793 p
->printer_len
= printer_len
;
2799 * Failed to update record, lets close this reference and move on...
2802 cupsdLogMessage(CUPSD_LOG_ERROR
,
2803 "Unable to update LPD DNS-SD record for %s - %d",
2806 DNSServiceRefDeallocate(p
->printer_ref
);
2807 p
->printer_ref
= NULL
;
2811 if (!p
->printer_ref
)
2814 * Initial registration...
2817 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2818 "Registering DNS-SD printer %s with name \"%s\" and "
2819 "type \"_printer._tcp\"", p
->name
, name
);
2821 p
->printer_ref
= DNSSDRef
;
2822 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2823 kDNSServiceFlagsShareConnection
,
2824 0, name
, "_printer._tcp", NULL
, NULL
,
2825 htons(515), printer_len
, printer_txt
,
2826 dnssdRegisterCallback
,
2827 p
)) == kDNSServiceErr_NoError
)
2829 p
->printer_txt
= printer_txt
;
2830 p
->printer_len
= printer_len
;
2834 cupsdLogMessage(CUPSD_LOG_WARN
,
2835 "DNS-SD LPD registration of \"%s\" failed: %d",
2846 * 'dnssdStop()' - Stop all DNS-SD registrations.
2852 cupsd_printer_t
*p
; /* Current printer */
2856 * De-register the individual printers
2859 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2861 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2862 dnssdDeregisterPrinter(p
);
2865 * Shutdown the rest of the service refs...
2870 DNSServiceRefDeallocate(WebIFRef
);
2876 DNSServiceRefDeallocate(RemoteRef
);
2880 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2882 DNSServiceRefDeallocate(DNSSDRef
);
2885 cupsArrayDelete(DNSSDPrinters
);
2886 DNSSDPrinters
= NULL
;
2893 * 'dnssdUpdate()' - Handle DNS-SD queries.
2899 DNSServiceErrorType sdErr
; /* Service discovery error */
2902 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2904 cupsdLogMessage(CUPSD_LOG_ERROR
,
2905 "DNS Service Discovery registration error %d!",
2910 #endif /* HAVE_DNSSD */
2914 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2917 static char * /* O - String or NULL if none */
2918 get_auth_info_required(
2919 cupsd_printer_t
*p
, /* I - Printer */
2920 char *buffer
, /* I - Value buffer */
2921 size_t bufsize
) /* I - Size of value buffer */
2923 cupsd_location_t
*auth
; /* Pointer to authentication element */
2924 char resource
[1024]; /* Printer/class resource path */
2928 * If auth-info-required is set for this printer, return that...
2931 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2933 int i
; /* Looping var */
2934 char *bufptr
; /* Pointer into buffer */
2936 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
2938 if (bufptr
>= (buffer
+ bufsize
- 2))
2944 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
2945 bufptr
+= strlen(bufptr
);
2952 * Figure out the authentication data requirements to advertise...
2955 if (p
->type
& CUPS_PRINTER_CLASS
)
2956 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2958 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2960 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2961 auth
->type
== CUPSD_AUTH_NONE
)
2962 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2966 int auth_type
; /* Authentication type */
2968 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2969 auth_type
= DefaultAuthType
;
2973 case CUPSD_AUTH_NONE
:
2976 case CUPSD_AUTH_NEGOTIATE
:
2977 strlcpy(buffer
, "negotiate", bufsize
);
2981 strlcpy(buffer
, "username,password", bufsize
);
2994 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2997 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2998 get_hostconfig(const char *name
) /* I - Name of service */
3000 cups_file_t
*fp
; /* Hostconfig file */
3001 char line
[1024], /* Line from file */
3002 *ptr
; /* Pointer to value */
3003 int state
= 1; /* State of service */
3007 * Try opening the /etc/hostconfig file; if we can't open it, assume that
3008 * the service is enabled/auto.
3011 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
3014 * Read lines from the file until we find the service...
3017 while (cupsFileGets(fp
, line
, sizeof(line
)))
3019 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
3024 if (!strcasecmp(line
, name
))
3027 * Found the service, see if it is set to "-NO-"...
3030 if (!strncasecmp(ptr
, "-NO-", 4))
3041 #endif /* __APPLE__ */
3045 * 'is_local_queue()' - Determine whether the URI points at a local queue.
3048 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
3049 is_local_queue(const char *uri
, /* I - Printer URI */
3050 char *host
, /* O - Host string */
3051 int hostlen
, /* I - Length of host buffer */
3052 char *resource
, /* O - Resource string */
3053 int resourcelen
) /* I - Length of resource buffer */
3055 char scheme
[32], /* Scheme portion of URI */
3056 username
[HTTP_MAX_URI
]; /* Username portion of URI */
3057 int port
; /* Port portion of URI */
3058 cupsd_netif_t
*iface
; /* Network interface */
3062 * Pull the URI apart to see if this is a local or remote printer...
3065 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3066 username
, sizeof(username
), host
, hostlen
, &port
,
3067 resource
, resourcelen
) < HTTP_URI_OK
)
3070 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3073 * Check for local server addresses...
3076 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3081 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3083 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3084 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3088 * If we get here, the printer is remote...
3096 * 'process_browse_data()' - Process new browse data.
3100 process_browse_data(
3101 const char *uri
, /* I - URI of printer/class */
3102 const char *host
, /* I - Hostname */
3103 const char *resource
, /* I - Resource path */
3104 cups_ptype_t type
, /* I - Printer type */
3105 ipp_pstate_t state
, /* I - Printer state */
3106 const char *location
, /* I - Printer location */
3107 const char *info
, /* I - Printer information */
3108 const char *make_model
, /* I - Printer make and model */
3109 int num_attrs
, /* I - Number of attributes */
3110 cups_option_t
*attrs
) /* I - Attributes */
3112 int i
; /* Looping var */
3113 int update
; /* Update printer attributes? */
3114 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3115 name
[IPP_MAX_NAME
], /* Name of printer */
3116 newname
[IPP_MAX_NAME
], /* New name of printer */
3117 *hptr
, /* Pointer into hostname */
3118 *sptr
; /* Pointer into ServerName */
3119 const char *shortname
; /* Short queue name (queue) */
3120 char local_make_model
[IPP_MAX_NAME
];
3121 /* Local make and model */
3122 cupsd_printer_t
*p
; /* Printer information */
3123 const char *ipp_options
, /* ipp-options value */
3124 *lease_duration
; /* lease-duration value */
3125 int is_class
; /* Is this queue a class? */
3128 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3129 "process_browse_data(uri=\"%s\", host=\"%s\", "
3130 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3131 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3132 uri
, host
, resource
, type
, state
,
3133 location
? location
: "(nil)", info
? info
: "(nil)",
3134 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3137 * Determine if the URI contains any illegal characters in it...
3140 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3141 (strncmp(resource
, "/printers/", 10) &&
3142 strncmp(resource
, "/classes/", 9)))
3144 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3148 if (strchr(resource
, '?') ||
3149 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3150 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3152 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3158 * OK, this isn't a local printer; add any remote options...
3161 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3163 if (BrowseRemoteOptions
)
3165 if (BrowseRemoteOptions
[0] == '?')
3168 * Override server-supplied options...
3171 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3173 else if (ipp_options
)
3176 * Combine the server and local options...
3179 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3180 BrowseRemoteOptions
);
3185 * Just use the local options...
3188 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3193 else if (ipp_options
)
3196 * Just use the server-supplied options...
3199 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3204 * See if we already have it listed in the Printers list, and add it if not...
3207 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3208 type
&= ~CUPS_PRINTER_IMPLICIT
;
3210 hptr
= strchr(host
, '.');
3211 sptr
= strchr(ServerName
, '.');
3212 is_class
= type
& CUPS_PRINTER_CLASS
;
3214 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3217 * Strip the common domain name components...
3220 while (hptr
!= NULL
)
3222 if (!strcasecmp(hptr
, sptr
))
3228 hptr
= strchr(hptr
+ 1, '.');
3235 * Remote destination is a class...
3238 if (!strncmp(resource
, "/classes/", 9))
3239 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3243 shortname
= resource
+ 9;
3248 * Remote destination is a printer...
3251 if (!strncmp(resource
, "/printers/", 10))
3252 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3256 shortname
= resource
+ 10;
3260 *hptr
= '.'; /* Resource FQDN */
3262 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3265 * Long name doesn't exist, try short name...
3268 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3271 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3274 * Short name doesn't exist, use it for this shared queue.
3277 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3279 strlcpy(name
, shortname
, sizeof(name
));
3284 * Short name exists...
3287 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3288 "process_browse_data: %s found, type=%x, hostname=%s...",
3289 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3291 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3292 p
= NULL
; /* Don't replace implicit classes */
3293 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3296 * Short name exists but is for a different host. If this is a remote
3297 * queue, rename it and use the long name...
3300 if (p
->type
& CUPS_PRINTER_REMOTE
)
3302 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3303 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3304 is_class
? "class" : "printer", p
->name
, p
->name
,
3306 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3307 "%s \'%s\' deleted by directory services.",
3308 is_class
? "Class" : "Printer", p
->name
);
3310 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3311 cupsdRenamePrinter(p
, newname
);
3313 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3314 "%s \'%s\' added by directory services.",
3315 is_class
? "Class" : "Printer", p
->name
);
3319 * Force creation with long name...
3327 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3328 "process_browse_data: %s found, type=%x, hostname=%s...",
3329 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3334 * Queue doesn't exist; add it...
3338 p
= cupsdAddClass(name
);
3340 p
= cupsdAddPrinter(name
);
3345 cupsdClearString(&(p
->hostname
));
3347 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3348 is_class
? "class" : "printer", name
);
3350 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3351 "%s \'%s\' added by directory services.",
3352 is_class
? "Class" : "Printer", name
);
3355 * Force the URI to point to the real server...
3358 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3361 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3367 * Hostname not set, so this must be a cached remote printer
3368 * that was created for a pending print job...
3371 cupsdSetString(&p
->hostname
, host
);
3372 cupsdSetString(&p
->uri
, uri
);
3373 cupsdSetString(&p
->device_uri
, uri
);
3376 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3380 * Update the state...
3384 p
->browse_time
= time(NULL
);
3386 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3390 * Grab the lease-duration for the browse data; anything less then 1
3391 * second or more than 1 week gets the default BrowseTimeout...
3394 i
= atoi(lease_duration
);
3395 if (i
< 1 || i
> 604800)
3398 p
->browse_expire
= p
->browse_time
+ i
;
3401 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3403 if (type
& CUPS_PRINTER_REJECTING
)
3405 type
&= ~CUPS_PRINTER_REJECTING
;
3413 else if (!p
->accepting
)
3419 if (p
->type
!= type
)
3425 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3427 cupsdSetString(&p
->location
, location
);
3431 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3433 cupsdSetString(&p
->info
, info
);
3436 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3439 if (!make_model
|| !make_model
[0])
3442 snprintf(local_make_model
, sizeof(local_make_model
),
3443 "Remote Class on %s", host
);
3445 snprintf(local_make_model
, sizeof(local_make_model
),
3446 "Remote Printer on %s", host
);
3449 snprintf(local_make_model
, sizeof(local_make_model
),
3450 "%s on %s", make_model
, host
);
3452 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3454 cupsdSetString(&p
->make_model
, local_make_model
);
3460 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3463 * See if we need to update the attributes...
3466 if (p
->num_options
!= num_attrs
)
3470 for (i
= 0; i
< num_attrs
; i
++)
3471 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3472 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3473 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3482 * Free the old options...
3485 cupsFreeOptions(p
->num_options
, p
->options
);
3488 p
->num_options
= num_attrs
;
3491 if (type
& CUPS_PRINTER_DELETE
)
3493 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3494 "%s \'%s\' deleted by directory services.",
3495 is_class
? "Class" : "Printer", p
->name
);
3497 cupsdExpireSubscriptions(p
, NULL
);
3499 cupsdDeletePrinter(p
, 1);
3500 cupsdUpdateImplicitClasses();
3501 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3505 cupsdSetPrinterAttrs(p
);
3506 cupsdUpdateImplicitClasses();
3510 * See if we have a default printer... If not, make the first network
3511 * default printer the default.
3514 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3517 * Find the first network default printer and use it...
3520 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3522 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3523 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3526 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3532 * Do auto-classing if needed...
3535 process_implicit_classes();
3540 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3544 process_implicit_classes(void)
3546 int i
; /* Looping var */
3547 int update
; /* Update printer attributes? */
3548 char name
[IPP_MAX_NAME
], /* Name of printer */
3549 *hptr
; /* Pointer into hostname */
3550 cupsd_printer_t
*p
, /* Printer information */
3551 *pclass
, /* Printer class */
3552 *first
; /* First printer in class */
3553 int offset
, /* Offset of name */
3554 len
; /* Length of name */
3557 if (!ImplicitClasses
|| !Printers
)
3561 * Loop through all available printers and create classes as needed...
3564 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3565 update
= 0, pclass
= NULL
, first
= NULL
;
3567 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3570 * Skip implicit classes...
3573 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3580 * If len == 0, get the length of this printer name up to the "@"
3584 cupsArraySave(Printers
);
3587 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3588 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3591 * We have more than one printer with the same name; see if
3592 * we have a class, and if this printer is a member...
3595 if (pclass
&& strcasecmp(pclass
->name
, name
))
3598 cupsdSetPrinterAttrs(pclass
);
3604 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3607 * Need to add the class...
3610 pclass
= cupsdAddPrinter(name
);
3611 cupsArrayAdd(ImplicitPrinters
, pclass
);
3613 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3614 pclass
->accepting
= 1;
3615 pclass
->state
= IPP_PRINTER_IDLE
;
3617 cupsdSetString(&pclass
->location
, p
->location
);
3618 cupsdSetString(&pclass
->info
, p
->info
);
3620 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3621 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3625 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3627 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3629 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3630 "Implicit class \'%s\' added by directory services.",
3636 for (i
= 0; i
< pclass
->num_printers
; i
++)
3637 if (pclass
->printers
[i
] == first
)
3640 if (i
>= pclass
->num_printers
)
3642 first
->in_implicit_class
= 1;
3643 cupsdAddPrinterToClass(pclass
, first
);
3649 for (i
= 0; i
< pclass
->num_printers
; i
++)
3650 if (pclass
->printers
[i
] == p
)
3653 if (i
>= pclass
->num_printers
)
3655 p
->in_implicit_class
= 1;
3656 cupsdAddPrinterToClass(pclass
, p
);
3663 * First time around; just get name length and mark it as first
3667 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3668 len
= hptr
- p
->name
;
3670 len
= strlen(p
->name
);
3672 if (len
>= sizeof(name
))
3675 * If the printer name length somehow is greater than we normally allow,
3676 * skip this printer...
3680 cupsArrayRestore(Printers
);
3684 strncpy(name
, p
->name
, len
);
3688 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3689 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3692 * Can't use same name as a local printer; add "Any" to the
3693 * front of the name, unless we have explicitly disabled
3694 * the "ImplicitAnyClasses"...
3697 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3700 * Add "Any" to the class name...
3703 strcpy(name
, "Any");
3704 strncpy(name
+ 3, p
->name
, len
);
3705 name
[len
+ 3] = '\0';
3711 * Don't create an implicit class if we have a local printer
3712 * with the same name...
3716 cupsArrayRestore(Printers
);
3724 cupsArrayRestore(Printers
);
3728 * Update the last printer class as needed...
3731 if (pclass
&& update
)
3732 cupsdSetPrinterAttrs(pclass
);
3737 * 'send_cups_browse()' - Send new browsing information using the CUPS
3742 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3744 int i
; /* Looping var */
3745 cups_ptype_t type
; /* Printer type */
3746 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3747 int bytes
; /* Length of packet */
3748 char packet
[1453], /* Browse data packet */
3749 uri
[1024], /* Printer URI */
3750 location
[1024], /* printer-location */
3751 info
[1024], /* printer-info */
3753 /* printer-make-and-model */
3754 air
[1024]; /* auth-info-required */
3755 cupsd_netif_t
*iface
; /* Network interface */
3759 * Figure out the printer type value...
3762 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3765 type
|= CUPS_PRINTER_REJECTING
;
3767 if (p
== DefaultPrinter
)
3768 type
|= CUPS_PRINTER_DEFAULT
;
3771 * Remove quotes from printer-info, printer-location, and
3772 * printer-make-and-model attributes...
3775 dequote(location
, p
->location
, sizeof(location
));
3776 dequote(info
, p
->info
, sizeof(info
));
3779 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3780 else if (p
->type
& CUPS_PRINTER_CLASS
)
3782 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3783 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3785 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3788 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3790 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3792 if (get_auth_info_required(p
, packet
, sizeof(packet
)))
3793 snprintf(air
, sizeof(air
), " auth-info-required=%s", packet
);
3798 * Send a packet to each browse address...
3801 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3805 * Send the browse packet to one or more interfaces...
3808 if (!strcmp(b
->iface
, "*"))
3811 * Send to all local interfaces...
3816 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3818 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3821 * Only send to local, IPv4 interfaces...
3824 if (!iface
->is_local
|| !iface
->port
||
3825 iface
->address
.addr
.sa_family
!= AF_INET
)
3828 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3829 iface
->hostname
, iface
->port
,
3830 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3833 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3834 type
, p
->state
, uri
, location
, info
, make_model
,
3835 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3837 bytes
= strlen(packet
);
3839 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3840 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3841 iface
->name
, packet
);
3843 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3845 sendto(BrowseSocket
, packet
, bytes
, 0,
3846 (struct sockaddr
*)&(iface
->broadcast
),
3847 httpAddrLength(&(iface
->broadcast
)));
3850 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3853 * Send to the named interface using the IPv4 address...
3857 if (strcmp(b
->iface
, iface
->name
))
3862 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3865 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3869 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3870 iface
->hostname
, iface
->port
,
3871 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3874 snprintf(packet
, sizeof(packet
),
3875 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3876 type
, p
->state
, uri
, location
, info
, make_model
,
3877 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3879 bytes
= strlen(packet
);
3881 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3882 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3883 iface
->name
, packet
);
3885 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3887 sendto(BrowseSocket
, packet
, bytes
, 0,
3888 (struct sockaddr
*)&(iface
->broadcast
),
3889 httpAddrLength(&(iface
->broadcast
)));
3896 * Send the browse packet to the indicated address using
3897 * the default server name...
3900 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3901 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3902 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3904 bytes
= strlen(packet
);
3905 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3906 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3908 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3909 (struct sockaddr
*)&(b
->to
),
3910 httpAddrLength(&(b
->to
))) <= 0)
3913 * Unable to send browse packet, so remove this address from the
3917 cupsdLogMessage(CUPSD_LOG_ERROR
,
3918 "cupsdSendBrowseList: sendto failed for browser "
3920 (int)(b
- Browsers
+ 1), strerror(errno
));
3923 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3934 * 'ldap_search_rec()' - LDAP Search with reconnect
3938 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3939 char *base
, /* I - Base dn */
3940 int scope
, /* I - LDAP search scope */
3941 char *filter
, /* I - Filter string */
3942 char *attrs
[], /* I - Requested attributes */
3943 int attrsonly
, /* I - Return only attributes? */
3944 LDAPMessage
**res
) /* I - LDAP handler */
3946 int rc
; /* Return code */
3949 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3950 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3951 NULL
, LDAP_NO_LIMIT
, res
);
3953 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3954 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3957 * If we have a connection problem try again...
3960 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3962 cupsdLogMessage(CUPSD_LOG_ERROR
,
3963 "LDAP search failed with status %d: %s",
3964 rc
, ldap_err2string(rc
));
3965 cupsdLogMessage(CUPSD_LOG_INFO
,
3966 "We try the LDAP search once again after reconnecting to "
3971 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3972 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3973 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3975 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3976 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3979 if (rc
== LDAP_NO_SUCH_OBJECT
)
3980 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3981 "ldap_search_rec: LDAP entry/object not found");
3982 else if (rc
!= LDAP_SUCCESS
)
3983 cupsdLogMessage(CUPSD_LOG_ERROR
,
3984 "ldap_search_rec: LDAP search failed with status %d: %s",
3985 rc
, ldap_err2string(rc
));
3987 if (rc
!= LDAP_SUCCESS
)
3995 * 'ldap_freeres()' - Free LDAPMessage
3999 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
4001 int rc
; /* Return value */
4004 rc
= ldap_msgfree(entry
);
4006 cupsdLogMessage(CUPSD_LOG_WARN
,
4007 "Can't free LDAPMessage!");
4009 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4010 "Freeing LDAPMessage was unnecessary");
4015 * 'ldap_getval_char()' - Get first LDAP value and convert to string
4019 ldap_getval_firststring(
4020 LDAP
*ld
, /* I - LDAP handler */
4021 LDAPMessage
*entry
, /* I - LDAP message or search result */
4022 char *attr
, /* I - the wanted attribute */
4023 char *retval
, /* O - String to return */
4024 unsigned long maxsize
) /* I - Max string size */
4026 char *dn
; /* LDAP DN */
4027 int rc
= 0; /* Return code */
4028 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4029 struct berval
**bval
; /* LDAP value array */
4030 unsigned long size
; /* String size */
4034 * Get value from LDAPMessage...
4037 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
4040 dn
= ldap_get_dn(ld
, entry
);
4041 cupsdLogMessage(CUPSD_LOG_WARN
,
4042 "Failed to get LDAP value %s for %s!",
4050 * Check size and copy value into our string...
4054 if (size
< bval
[0]->bv_len
)
4057 dn
= ldap_get_dn(ld
, entry
);
4058 cupsdLogMessage(CUPSD_LOG_WARN
,
4059 "Attribute %s is too big! (dn: %s)",
4064 size
= bval
[0]->bv_len
;
4066 strlcpy(retval
, bval
[0]->bv_val
, size
);
4067 ldap_value_free_len(bval
);
4070 char **value
; /* LDAP value */
4073 * Get value from LDAPMessage...
4076 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4079 dn
= ldap_get_dn(ld
, entry
);
4080 cupsdLogMessage(CUPSD_LOG_WARN
,
4081 "Failed to get LDAP value %s for %s!",
4087 strlcpy(retval
, *value
, maxsize
);
4088 ldap_value_free(value
);
4090 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4097 * 'send_ldap_ou()' - Send LDAP ou registrations.
4101 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4102 char *basedn
, /* I - Our base dn */
4103 char *descstring
) /* I - Description for ou */
4105 int i
; /* Looping var... */
4106 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4107 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4108 LDAPMessage
*res
, /* Search result token */
4109 *e
; /* Current entry from search */
4110 int rc
; /* LDAP status */
4111 char dn
[1024], /* DN of the organizational unit we are adding */
4112 *desc
[2], /* Change records */
4114 char old_desc
[1024]; /* Old description */
4115 static const char * const objectClass_values
[] =
4116 { /* The 2 objectClass's we use in */
4117 "top", /* our LDAP entries */
4118 "organizationalUnit",
4121 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4128 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4131 * Reconnect if LDAP Handle is invalid...
4134 if (! BrowseLDAPHandle
)
4136 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4137 "send_ldap_ou: LDAP Handle is invalid. Try "
4144 * Prepare ldap search...
4147 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4148 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4152 desc
[0] = descstring
;
4155 mods
[0].mod_type
= "ou";
4156 mods
[0].mod_values
= ou_value
;
4157 mods
[1].mod_type
= "description";
4158 mods
[1].mod_values
= desc
;
4159 mods
[2].mod_type
= "objectClass";
4160 mods
[2].mod_values
= (char **)objectClass_values
;
4162 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4163 (char **)ou_attrs
, 0, &res
);
4166 * If ldap search was not successfull then exit function...
4169 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4173 * Check if we need to insert or update the LDAP entry...
4176 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4177 rc
!= LDAP_NO_SUCH_OBJECT
)
4180 * Printserver has already been registered, check if
4181 * modification is required...
4184 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4187 * Get the required values from this entry...
4190 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4191 sizeof(old_desc
)) == -1)
4195 * Check if modification is required...
4198 if ( strcmp(desc
[0], old_desc
) == 0 )
4201 * LDAP entry for the printer exists.
4202 * Printer has already been registered,
4203 * no modifications required...
4205 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4206 "send_ldap_ou: No updates required for %s", ou
);
4211 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4212 "send_ldap_ou: Replace entry for %s", ou
);
4214 for (i
= 0; i
< 3; i
++)
4216 pmods
[i
] = mods
+ i
;
4217 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4221 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4222 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4223 NULL
)) != LDAP_SUCCESS
)
4225 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4226 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4228 cupsdLogMessage(CUPSD_LOG_ERROR
,
4229 "LDAP modify for %s failed with status %d: %s",
4230 ou
, rc
, ldap_err2string(rc
));
4231 if ( LDAP_SERVER_DOWN
== rc
)
4239 * Printserver has never been registered,
4240 * add registration...
4243 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4244 "send_ldap_ou: Add entry for %s", ou
);
4246 for (i
= 0; i
< 3; i
++)
4248 pmods
[i
] = mods
+ i
;
4249 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4253 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4254 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4255 NULL
)) != LDAP_SUCCESS
)
4257 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4258 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4260 cupsdLogMessage(CUPSD_LOG_ERROR
,
4261 "LDAP add for %s failed with status %d: %s",
4262 ou
, rc
, ldap_err2string(rc
));
4263 if ( LDAP_SERVER_DOWN
== rc
)
4273 * 'send_ldap_browse()' - Send LDAP printer registrations.
4277 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4279 int i
; /* Looping var... */
4280 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4281 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4282 LDAPMessage
*res
, /* Search result token */
4283 *e
; /* Current entry from search */
4284 char *cn_value
[2], /* Change records */
4290 typestring
[255], /* String to hold printer-type */
4291 dn
[1024]; /* DN of the printer we are adding */
4292 int rc
; /* LDAP status */
4293 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4294 old_location
[1024], /* Printer location */
4295 old_info
[1024], /* Printer information */
4296 old_make_model
[1024], /* Printer make and model */
4297 old_type_string
[30]; /* Temporary type number */
4298 int old_type
; /* Printer type */
4299 static const char * const objectClass_values
[] =
4300 { /* The 3 objectClass's we use in */
4301 "top", /* our LDAP entries */
4308 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4311 * Exit function if LDAP updates has been disabled...
4314 if (!BrowseLDAPUpdate
)
4316 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4317 "send_ldap_browse: Updates temporary disabled; "
4323 * Reconnect if LDAP Handle is invalid...
4326 if (!BrowseLDAPHandle
)
4328 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4329 "send_ldap_browse: LDAP Handle is invalid. Try "
4336 * Everything in ldap is ** so we fudge around it...
4339 sprintf(typestring
, "%u", p
->type
);
4341 cn_value
[0] = p
->name
;
4343 info
[0] = p
->info
? p
->info
: "Unknown";
4345 location
[0] = p
->location
? p
->location
: "Unknown";
4347 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4348 make_model
[1] = NULL
;
4349 type
[0] = typestring
;
4355 * Get ldap entry for printer ...
4358 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4360 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4362 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4363 (char **)ldap_attrs
, 0, &res
);
4366 * If ldap search was not successfull then exit function
4367 * and temporary disable LDAP updates...
4370 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4372 if (BrowseLDAPUpdate
&&
4373 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4375 BrowseLDAPUpdate
= FALSE
;
4376 cupsdLogMessage(CUPSD_LOG_INFO
,
4377 "LDAP update temporary disabled");
4384 * Fill modification array...
4387 mods
[0].mod_type
= "cn";
4388 mods
[0].mod_values
= cn_value
;
4389 mods
[1].mod_type
= "printerDescription";
4390 mods
[1].mod_values
= info
;
4391 mods
[2].mod_type
= "printerURI";
4392 mods
[2].mod_values
= uri
;
4393 mods
[3].mod_type
= "printerLocation";
4394 mods
[3].mod_values
= location
;
4395 mods
[4].mod_type
= "printerMakeAndModel";
4396 mods
[4].mod_values
= make_model
;
4397 mods
[5].mod_type
= "printerType";
4398 mods
[5].mod_values
= type
;
4399 mods
[6].mod_type
= "objectClass";
4400 mods
[6].mod_values
= (char **)objectClass_values
;
4403 * Check if we need to insert or update the LDAP entry...
4406 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4407 rc
!= LDAP_NO_SUCH_OBJECT
)
4410 * Printer has already been registered, check if
4411 * modification is required...
4414 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4417 * Get the required values from this entry...
4420 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4421 old_info
, sizeof(old_info
)) == -1)
4424 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4425 old_location
, sizeof(old_location
)) == -1)
4428 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4429 old_make_model
, sizeof(old_make_model
)) == -1)
4432 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4433 old_type_string
, sizeof(old_type_string
)) == -1)
4436 old_type
= atoi(old_type_string
);
4438 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4439 sizeof(old_uri
)) == -1)
4443 * Check if modification is required...
4446 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4447 !strcmp(location
[0], old_location
) &&
4448 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4451 * LDAP entry for the printer exists. Printer has already been registered,
4452 * no modifications required...
4455 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4456 "send_ldap_browse: No updates required for %s", p
->name
);
4461 * LDAP entry for the printer exists. Printer has already been registered,
4462 * modify the current registration...
4465 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4466 "send_ldap_browse: Replace entry for %s", p
->name
);
4468 for (i
= 0; i
< 7; i
++)
4470 pmods
[i
] = mods
+ i
;
4471 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4475 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4476 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4477 NULL
)) != LDAP_SUCCESS
)
4479 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4480 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4482 cupsdLogMessage(CUPSD_LOG_ERROR
,
4483 "LDAP modify for %s failed with status %d: %s",
4484 p
->name
, rc
, ldap_err2string(rc
));
4485 if (rc
== LDAP_SERVER_DOWN
)
4493 * No LDAP entry exists for the printer. Printer has never been registered,
4494 * add the current registration...
4497 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4499 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4500 "send_ldap_browse: Add entry for %s", p
->name
);
4502 for (i
= 0; i
< 7; i
++)
4504 pmods
[i
] = mods
+ i
;
4505 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4509 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4510 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4511 NULL
)) != LDAP_SUCCESS
)
4513 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4514 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4516 cupsdLogMessage(CUPSD_LOG_ERROR
,
4517 "LDAP add for %s failed with status %d: %s",
4518 p
->name
, rc
, ldap_err2string(rc
));
4519 if (rc
== LDAP_SERVER_DOWN
)
4529 * 'ldap_dereg_printer()' - Delete printer from directory
4533 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4535 char dn
[1024]; /* DN of the printer */
4536 int rc
; /* LDAP status */
4539 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4543 * Reconnect if LDAP Handle is invalid...
4546 if (!BrowseLDAPHandle
)
4553 * Get dn for printer and delete LDAP entry...
4556 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4558 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4560 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4561 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4562 NULL
)) != LDAP_SUCCESS
)
4564 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4565 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4567 cupsdLogMessage(CUPSD_LOG_WARN
,
4568 "LDAP delete for %s failed with status %d: %s",
4569 p
->name
, rc
, ldap_err2string(rc
));
4572 * If we had a connection problem (connection timed out, etc.)
4573 * we should reconnect and try again to delete the entry...
4576 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4578 cupsdLogMessage(CUPSD_LOG_INFO
,
4579 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4582 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4583 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4584 NULL
)) != LDAP_SUCCESS
)
4586 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4587 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4588 cupsdLogMessage(CUPSD_LOG_WARN
,
4589 "LDAP delete for %s failed with status %d: %s",
4590 p
->name
, rc
, ldap_err2string(rc
));
4597 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4598 char *basedn
) /* I - Dase dn */
4600 char dn
[1024]; /* DN of the printer */
4601 int rc
; /* LDAP status */
4604 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4607 * Reconnect if LDAP Handle is invalid...
4610 if (!BrowseLDAPHandle
)
4617 * Get dn for printer and delete LDAP entry...
4620 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4621 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4623 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4624 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4625 NULL
)) != LDAP_SUCCESS
)
4627 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4628 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4630 cupsdLogMessage(CUPSD_LOG_WARN
,
4631 "LDAP delete for %s failed with status %d: %s",
4632 ou
, rc
, ldap_err2string(rc
));
4635 * If we had a connection problem (connection timed out, etc.)
4636 * we should reconnect and try again to delete the entry...
4639 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4641 cupsdLogMessage(CUPSD_LOG_INFO
,
4642 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4644 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4645 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4646 NULL
)) != LDAP_SUCCESS
)
4648 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4649 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4650 cupsdLogMessage(CUPSD_LOG_WARN
,
4651 "LDAP delete for %s failed with status %d: %s",
4652 ou
, rc
, ldap_err2string(rc
));
4657 #endif /* HAVE_LDAP */
4662 * 'send_slp_browse()' - Register the specified printer with SLP.
4666 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4668 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4669 attrs
[8192], /* Printer attributes */
4670 finishings
[1024], /* Finishings to support */
4671 make_model
[IPP_MAX_NAME
* 2],
4672 /* Make and model, quoted */
4673 location
[IPP_MAX_NAME
* 2],
4674 /* Location, quoted */
4675 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4676 *src
, /* Pointer to original string */
4677 *dst
; /* Pointer to destination string */
4678 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4679 SLPError error
; /* SLP error, if any */
4682 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4686 * Make the SLP service URL that conforms to the IANA
4687 * 'printer:' template.
4690 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4692 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4695 * Figure out the finishings string...
4698 if (p
->type
& CUPS_PRINTER_STAPLE
)
4699 strcpy(finishings
, "staple");
4701 finishings
[0] = '\0';
4703 if (p
->type
& CUPS_PRINTER_BIND
)
4706 strlcat(finishings
, ",bind", sizeof(finishings
));
4708 strcpy(finishings
, "bind");
4711 if (p
->type
& CUPS_PRINTER_PUNCH
)
4714 strlcat(finishings
, ",punch", sizeof(finishings
));
4716 strcpy(finishings
, "punch");
4719 if (p
->type
& CUPS_PRINTER_COVER
)
4722 strlcat(finishings
, ",cover", sizeof(finishings
));
4724 strcpy(finishings
, "cover");
4727 if (p
->type
& CUPS_PRINTER_SORT
)
4730 strlcat(finishings
, ",sort", sizeof(finishings
));
4732 strcpy(finishings
, "sort");
4736 strcpy(finishings
, "none");
4739 * Quote any commas in the make and model, location, and info strings...
4742 for (src
= p
->make_model
, dst
= make_model
;
4743 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4745 if (*src
== ',' || *src
== '\\' || *src
== ')')
4754 strcpy(make_model
, "Unknown");
4756 for (src
= p
->location
, dst
= location
;
4757 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4759 if (*src
== ',' || *src
== '\\' || *src
== ')')
4768 strcpy(location
, "Unknown");
4770 for (src
= p
->info
, dst
= info
;
4771 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4773 if (*src
== ',' || *src
== '\\' || *src
== ')')
4782 strcpy(info
, "Unknown");
4785 * Get the authentication value...
4788 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4792 * Make the SLP attribute string list that conforms to
4793 * the IANA 'printer:' template.
4796 snprintf(attrs
, sizeof(attrs
),
4797 "(printer-uri-supported=%s),"
4798 "(uri-authentication-supported=%s>),"
4800 "(uri-security-supported=tls>),"
4802 "(uri-security-supported=none>),"
4803 #endif /* HAVE_SSL */
4804 "(printer-name=%s),"
4805 "(printer-location=%s),"
4806 "(printer-info=%s),"
4807 "(printer-more-info=%s),"
4808 "(printer-make-and-model=%s),"
4809 "(printer-type=%d),"
4810 "(charset-supported=utf-8),"
4811 "(natural-language-configured=%s),"
4812 "(natural-language-supported=de,en,es,fr,it),"
4813 "(color-supported=%s),"
4814 "(finishings-supported=%s),"
4815 "(sides-supported=one-sided%s),"
4816 "(multiple-document-jobs-supported=true)"
4817 "(ipp-versions-supported=1.0,1.1)",
4818 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4819 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4820 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4822 p
->type
& CUPS_PRINTER_DUPLEX
?
4823 ",two-sided-long-edge,two-sided-short-edge" : "");
4825 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4828 * Register the printer with the SLP server...
4831 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4832 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4834 if (error
!= SLP_OK
)
4835 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4841 * 'slp_attr_callback()' - SLP attribute callback
4844 static SLPBoolean
/* O - SLP_TRUE for success */
4846 SLPHandle hslp
, /* I - SLP handle */
4847 const char *attrlist
, /* I - Attribute list */
4848 SLPError errcode
, /* I - Parsing status for this attr */
4849 void *cookie
) /* I - Current printer */
4851 char *tmp
= 0; /* Temporary string */
4852 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4853 /* Current printer */
4856 (void)hslp
; /* anti-compiler-warning-code */
4859 * Bail if there was an error
4862 if (errcode
!= SLP_OK
)
4866 * Parse the attrlist to obtain things needed to build CUPS browse packet
4869 memset(p
, 0, sizeof(cupsd_printer_t
));
4871 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4873 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4875 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4877 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4878 p
->type
= atoi(tmp
);
4880 p
->type
= CUPS_PRINTER_REMOTE
;
4882 cupsdClearString(&tmp
);
4889 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4893 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4895 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4898 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4900 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4903 * Make the SLP service URL that conforms to the IANA
4904 * 'printer:' template.
4907 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4910 * Deregister the printer...
4913 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4919 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4922 static int /* O - 0 on success */
4923 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4924 const char *tag
, /* I - Name of attribute */
4925 char **valbuf
) /* O - Value */
4927 char *ptr1
, /* Pointer into string */
4931 cupsdClearString(valbuf
);
4933 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4935 ptr1
+= strlen(tag
);
4937 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4943 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4944 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4947 * Dequote the value...
4950 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4951 if (*ptr1
== '\\' && ptr1
[1])
4952 _cups_strcpy(ptr1
, ptr1
+ 1);
4963 * 'slp_reg_callback()' - Empty SLPRegReport.
4967 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4968 SLPError errcode
, /* I - Error code, if any */
4969 void *cookie
) /* I - App data */
4980 * 'slp_url_callback()' - SLP service url callback
4983 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4985 SLPHandle hslp
, /* I - SLP handle */
4986 const char *srvurl
, /* I - URL of service */
4987 unsigned short lifetime
, /* I - Life of service */
4988 SLPError errcode
, /* I - Existing error code */
4989 void *cookie
) /* I - Pointer to service list */
4991 slpsrvurl_t
*s
, /* New service entry */
4992 **head
; /* Pointer to head of entry */
4996 * Let the compiler know we won't be using these vars...
5003 * Bail if there was an error
5006 if (errcode
!= SLP_OK
)
5010 * Grab the head of the list...
5013 head
= (slpsrvurl_t
**)cookie
;
5016 * Allocate a *temporary* slpsrvurl_t to hold this entry.
5019 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
5023 * Copy the SLP service URL...
5026 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
5029 * Link the SLP service URL into the head of the list
5039 #endif /* HAVE_LIBSLP */
5043 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
5047 update_cups_browse(void)
5049 int i
; /* Looping var */
5050 int auth
; /* Authorization status */
5051 int len
; /* Length of name string */
5052 int bytes
; /* Number of bytes left */
5053 char packet
[1541], /* Broadcast packet */
5054 *pptr
; /* Pointer into packet */
5055 socklen_t srclen
; /* Length of source address */
5056 http_addr_t srcaddr
; /* Source address */
5057 char srcname
[1024]; /* Source hostname */
5058 unsigned address
[4]; /* Source address */
5059 unsigned type
; /* Printer type */
5060 unsigned state
; /* Printer state */
5061 char uri
[HTTP_MAX_URI
], /* Printer URI */
5062 host
[HTTP_MAX_URI
], /* Host portion of URI */
5063 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
5064 info
[IPP_MAX_NAME
], /* Information string */
5065 location
[IPP_MAX_NAME
], /* Location string */
5066 make_model
[IPP_MAX_NAME
];/* Make and model string */
5067 int num_attrs
; /* Number of attributes */
5068 cups_option_t
*attrs
; /* Attributes */
5072 * Read a packet from the browse socket...
5075 srclen
= sizeof(srcaddr
);
5076 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5077 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5080 * "Connection refused" is returned under Linux if the destination port
5081 * or address is unreachable from a previous sendto(); check for the
5082 * error here and ignore it for now...
5085 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5087 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5089 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5092 closesocket(BrowseSocket
);
5094 close(BrowseSocket
);
5097 cupsdRemoveSelect(BrowseSocket
);
5100 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5101 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5107 packet
[bytes
] = '\0';
5110 * If we're about to sleep, ignore incoming browse packets.
5117 * Figure out where it came from...
5121 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5123 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5124 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5125 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5126 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5129 #endif /* AF_INET6 */
5134 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5137 if (HostNameLookups
)
5138 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5140 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5142 len
= strlen(srcname
);
5150 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5153 * Access from localhost (127.0.0.1) is always allowed...
5156 auth
= CUPSD_AUTH_ALLOW
;
5161 * Do authorization checks on the domain/address...
5164 switch (BrowseACL
->order_type
)
5167 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5170 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5171 auth
= CUPSD_AUTH_ALLOW
;
5173 if (cupsdCheckAuth(address
, srcname
, len
,
5174 BrowseACL
->num_deny
, BrowseACL
->deny
))
5175 auth
= CUPSD_AUTH_DENY
;
5177 if (cupsdCheckAuth(address
, srcname
, len
,
5178 BrowseACL
->num_allow
, BrowseACL
->allow
))
5179 auth
= CUPSD_AUTH_ALLOW
;
5182 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5183 auth
= CUPSD_AUTH_DENY
;
5185 if (cupsdCheckAuth(address
, srcname
, len
,
5186 BrowseACL
->num_allow
, BrowseACL
->allow
))
5187 auth
= CUPSD_AUTH_ALLOW
;
5189 if (cupsdCheckAuth(address
, srcname
, len
,
5190 BrowseACL
->num_deny
, BrowseACL
->deny
))
5191 auth
= CUPSD_AUTH_DENY
;
5197 auth
= CUPSD_AUTH_ALLOW
;
5199 if (auth
== CUPSD_AUTH_DENY
)
5201 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5202 "update_cups_browse: Refused %d bytes from %s", bytes
,
5207 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5208 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5215 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5217 cupsdLogMessage(CUPSD_LOG_WARN
,
5218 "update_cups_browse: Garbled browse packet - %s", packet
);
5222 strcpy(location
, "Location Unknown");
5223 strcpy(info
, "No Information Available");
5224 make_model
[0] = '\0';
5228 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5231 * Have extended information; can't use sscanf for it because not all
5232 * sscanf's allow empty strings with %[^\"]...
5235 for (i
= 0, pptr
++;
5236 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5238 location
[i
] = *pptr
;
5246 while (*pptr
&& isspace(*pptr
& 255))
5251 for (i
= 0, pptr
++;
5252 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5261 while (*pptr
&& isspace(*pptr
& 255))
5266 for (i
= 0, pptr
++;
5267 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5269 make_model
[i
] = *pptr
;
5274 make_model
[i
] = '\0';
5277 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5283 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5284 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5285 type
, state
, uri
, location
, info
, make_model
));
5288 * Pull the URI apart to see if this is a local or remote printer...
5291 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5293 cupsFreeOptions(num_attrs
, attrs
);
5301 for (i
= 0; i
< NumRelays
; i
++)
5302 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5303 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5304 (struct sockaddr
*)&(Relays
[i
].to
),
5305 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5307 cupsdLogMessage(CUPSD_LOG_ERROR
,
5308 "update_cups_browse: sendto failed for relay %d - %s.",
5309 i
+ 1, strerror(errno
));
5310 cupsFreeOptions(num_attrs
, attrs
);
5315 * Process the browse data...
5318 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5319 (ipp_pstate_t
)state
, location
, info
, make_model
,
5325 * 'update_lpd()' - Update the LPD configuration as needed.
5329 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5336 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5337 * setting for backwards-compatibility.
5340 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5342 #endif /* __APPLE__ */
5344 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5347 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5350 char newfile
[1024]; /* New cups-lpd.N file */
5351 cups_file_t
*ofp
, /* Original file pointer */
5352 *nfp
; /* New file pointer */
5353 char line
[1024]; /* Line from file */
5356 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5358 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5360 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5361 LPDConfigFile
+ 9, strerror(errno
));
5365 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5367 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5368 newfile
, strerror(errno
));
5374 * Copy all of the lines from the cups-lpd file...
5377 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5381 cupsFilePrintf(nfp
, "%s\n", line
);
5382 snprintf(line
, sizeof(line
), "\tdisable = %s",
5383 onoff
? "no" : "yes");
5385 else if (!strstr(line
, "disable ="))
5386 cupsFilePrintf(nfp
, "%s\n", line
);
5391 rename(newfile
, LPDConfigFile
+ 9);
5394 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5397 * Enable/disable LPD via the launchctl command...
5400 char *argv
[5], /* Arguments for command */
5401 *envp
[MAX_ENV
]; /* Environment for command */
5402 int pid
; /* Process ID */
5405 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5406 argv
[0] = (char *)"launchctl";
5407 argv
[1] = (char *)(onoff
? "load" : "unload");
5408 argv
[2] = (char *)"-w";
5409 argv
[3] = LPDConfigFile
+ 10;
5412 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5415 #endif /* __APPLE__ */
5417 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5422 * 'update_polling()' - Read status messages from the poll daemons.
5426 update_polling(void)
5428 char *ptr
, /* Pointer to end of line in buffer */
5429 message
[1024]; /* Pointer to message text */
5430 int loglevel
; /* Log level for message */
5433 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5434 message
, sizeof(message
))) != NULL
)
5436 if (loglevel
== CUPSD_LOG_INFO
)
5437 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5439 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5443 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5446 * All polling processes have died; stop polling...
5449 cupsdLogMessage(CUPSD_LOG_ERROR
,
5450 "update_polling: all polling processes have exited!");
5457 * 'update_smb()' - Update the SMB configuration as needed.
5461 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5466 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5469 * Enable/disable SMB via the specified smb.conf config file...
5472 char newfile
[1024]; /* New smb.conf.N file */
5473 cups_file_t
*ofp
, /* Original file pointer */
5474 *nfp
; /* New file pointer */
5475 char line
[1024]; /* Line from file */
5476 int in_printers
; /* In [printers] section? */
5479 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5481 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5483 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5484 SMBConfigFile
+ 8, strerror(errno
));
5488 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5490 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5491 newfile
, strerror(errno
));
5497 * Copy all of the lines from the smb.conf file...
5502 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5504 if (in_printers
&& strstr(line
, "printable ="))
5505 snprintf(line
, sizeof(line
), " printable = %s",
5506 onoff
? "yes" : "no");
5508 cupsFilePrintf(nfp
, "%s\n", line
);
5511 in_printers
= !strcmp(line
, "[printers]");
5516 rename(newfile
, SMBConfigFile
+ 8);
5519 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5524 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".