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 CFStringEncoding nameEncoding
; /* Encoding of computer name */
1914 CFStringRef nameRef
; /* Host name CFString */
1915 char nameBuffer
[1024]; /* C-string buffer */
1916 #endif /* HAVE_COREFOUNDATION_H */
1920 * Only share the web interface and printers when non-local listening is
1929 * Get the computer name as a c-string...
1932 #ifdef HAVE_COREFOUNDATION_H
1933 sc
= SCDynamicStoreCreate(kCFAllocatorDefault
, CFSTR("cupsd"), NULL
, NULL
);
1938 * Get the computer name from the dynamic store...
1941 cupsdClearString(&DNSSDComputerName
);
1943 if ((nameRef
= SCDynamicStoreCopyComputerName(sc
, &nameEncoding
)) != NULL
)
1945 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1946 kCFStringEncodingUTF8
))
1948 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1949 "Dynamic store computer name is \"%s\".", nameBuffer
);
1950 cupsdSetString(&DNSSDComputerName
, nameBuffer
);
1956 if (!DNSSDComputerName
)
1959 * Use the ServerName instead...
1962 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1963 "Using ServerName \"%s\" as computer name.", ServerName
);
1964 cupsdSetString(&DNSSDComputerName
, ServerName
);
1968 * Get the local hostname from the dynamic store...
1971 cupsdClearString(&DNSSDHostName
);
1973 if ((nameRef
= SCDynamicStoreCopyLocalHostName(sc
)) != NULL
)
1975 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1976 kCFStringEncodingUTF8
))
1978 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1979 "Dynamic store host name is \"%s\".", nameBuffer
);
1980 cupsdSetString(&DNSSDHostName
, nameBuffer
);
1989 * Use the ServerName instead...
1992 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1993 "Using ServerName \"%s\" as host name.", ServerName
);
1994 cupsdSetString(&DNSSDHostName
, ServerName
);
1998 * Get any Back-to-My-Mac domains and add them as aliases...
2001 cupsdFreeAliases(DNSSDAlias
);
2004 btmm
= SCDynamicStoreCopyValue(sc
, CFSTR("Setup:/Network/BackToMyMac"));
2005 if (btmm
&& CFGetTypeID(btmm
) == CFDictionaryGetTypeID())
2007 cupsdLogMessage(CUPSD_LOG_DEBUG
, "%d Back to My Mac aliases to add.",
2008 (int)CFDictionaryGetCount(btmm
));
2009 CFDictionaryApplyFunction(btmm
, dnssdAddAlias
, NULL
);
2012 cupsdLogMessage(CUPSD_LOG_ERROR
,
2013 "Bad Back to My Mac data in dynamic store!");
2015 cupsdLogMessage(CUPSD_LOG_DEBUG
, "No Back to My Mac aliases to add.");
2023 #endif /* HAVE_COREFOUNDATION_H */
2025 cupsdSetString(&DNSSDComputerName
, ServerName
);
2026 cupsdSetString(&DNSSDHostName
, ServerName
);
2030 * Then (re)register the web interface if enabled...
2035 if (DNSSDComputerName
)
2036 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDComputerName
);
2038 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
2041 DNSServiceRefDeallocate(WebIFRef
);
2043 WebIFRef
= DNSSDRef
;
2044 if ((error
= DNSServiceRegister(&WebIFRef
,
2045 kDNSServiceFlagsShareConnection
,
2046 0, webif
, "_http._tcp", NULL
,
2047 NULL
, htons(DNSSDPort
), 7,
2048 "\006path=/", dnssdRegisterCallback
,
2049 NULL
)) != kDNSServiceErr_NoError
)
2050 cupsdLogMessage(CUPSD_LOG_ERROR
,
2051 "DNS-SD web interface registration failed: %d", error
);
2054 #endif /* HAVE_DNSSD */
2059 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
2063 cupsdUpdateLDAPBrowse(void)
2065 char uri
[HTTP_MAX_URI
], /* Printer URI */
2066 host
[HTTP_MAX_URI
], /* Hostname */
2067 resource
[HTTP_MAX_URI
], /* Resource path */
2068 location
[1024], /* Printer location */
2069 info
[1024], /* Printer information */
2070 make_model
[1024], /* Printer make and model */
2071 type_num
[30]; /* Printer type number */
2072 int type
; /* Printer type */
2073 int rc
; /* LDAP status */
2074 int limit
; /* Size limit */
2075 LDAPMessage
*res
, /* LDAP search results */
2076 *e
; /* Current entry from search */
2078 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
2080 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
2083 * Reconnect if LDAP Handle is invalid...
2086 if (! BrowseLDAPHandle
)
2093 * Search for cups printers in LDAP directory...
2096 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2097 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
2100 * If ldap search was successfull then exit function
2101 * and temporary disable LDAP updates...
2104 if (rc
!= LDAP_SUCCESS
)
2106 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
2108 BrowseLDAPUpdate
= FALSE
;
2109 cupsdLogMessage(CUPSD_LOG_INFO
,
2110 "LDAP update temporary disabled");
2116 * If LDAP updates were disabled, we will reenable them...
2119 if (! BrowseLDAPUpdate
)
2121 BrowseLDAPUpdate
= TRUE
;
2122 cupsdLogMessage(CUPSD_LOG_INFO
,
2123 "LDAP update enabled");
2127 * Count LDAP entries and return if no entry exist...
2130 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
2131 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
2139 * Loop through the available printers...
2142 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
2144 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
2147 * Get the required values from this entry...
2150 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2151 "printerDescription", info
, sizeof(info
)) == -1)
2154 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2155 "printerLocation", location
, sizeof(location
)) == -1)
2158 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2159 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
2162 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2163 "printerType", type_num
, sizeof(type_num
)) == -1)
2166 type
= atoi(type_num
);
2168 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
2169 "printerURI", uri
, sizeof(uri
)) == -1)
2173 * Process the entry as browse data...
2176 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2177 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2178 location
, info
, make_model
, 0, NULL
);
2184 #endif /* HAVE_LDAP */
2189 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2193 cupsdUpdateSLPBrowse(void)
2195 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2196 *next
; /* Next service in list */
2197 cupsd_printer_t p
; /* Printer information */
2198 const char *uri
; /* Pointer to printer URI */
2199 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2200 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2204 * Reset the refresh time...
2207 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2210 * Poll for remote printers using SLP...
2215 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2216 slp_url_callback
, &s
);
2219 * Loop through the list of available printers...
2225 * Save the "next" pointer...
2231 * Load a cupsd_printer_t structure with the SLP service attributes...
2234 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2237 * Process this printer entry...
2240 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2242 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2245 * Pull the URI apart to see if this is a local or remote printer...
2248 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2249 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2250 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2254 * Free this listing...
2257 cupsdClearString(&p
.info
);
2258 cupsdClearString(&p
.location
);
2259 cupsdClearString(&p
.make_model
);
2264 #endif /* HAVE_LIBSLP */
2268 * 'dequote()' - Remote quotes from a string.
2271 static char * /* O - Dequoted string */
2272 dequote(char *d
, /* I - Destination string */
2273 const char *s
, /* I - Source string */
2274 int dlen
) /* I - Destination length */
2276 char *dptr
; /* Pointer into destination */
2281 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2298 # ifdef HAVE_COREFOUNDATION
2300 * 'dnssdAddAlias()' - Add a DNS-SD alias name.
2304 dnssdAddAlias(const void *key
, /* I - Key */
2305 const void *value
, /* I - Value (domain) */
2306 void *context
) /* I - Unused */
2308 char valueStr
[1024], /* Domain string */
2309 hostname
[1024]; /* Complete hostname */
2314 if (CFGetTypeID((CFStringRef
)value
) == CFStringGetTypeID() &&
2315 CFStringGetCString((CFStringRef
)value
, valueStr
, sizeof(valueStr
),
2316 kCFStringEncodingUTF8
))
2318 snprintf(hostname
, sizeof(hostname
), "%s.%s", DNSSDHostName
, valueStr
);
2320 DNSSDAlias
= cupsArrayNew(NULL
, NULL
);
2322 cupsdAddAlias(DNSSDAlias
, hostname
);
2323 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added Back to My Mac ServerAlias %s",
2327 cupsdLogMessage(CUPSD_LOG_ERROR
,
2328 "Bad Back to My Mac domain in dynamic store!");
2330 # endif /* HAVE_COREFOUNDATION */
2334 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2337 static char * /* O - TXT record */
2338 dnssdBuildTxtRecord(
2339 int *txt_len
, /* O - TXT record length */
2340 cupsd_printer_t
*p
, /* I - Printer information */
2341 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2343 int i
; /* Looping var */
2344 char type_str
[32], /* Type to string buffer */
2345 state_str
[32], /* State to string buffer */
2346 rp_str
[1024], /* Queue name string buffer */
2347 air_str
[1024], /* auth-info-required string buffer */
2348 *keyvalue
[32][2]; /* Table of key/value pairs */
2352 * Load up the key value pairs...
2357 keyvalue
[i
][0] = "txtvers";
2358 keyvalue
[i
++][1] = "1";
2360 keyvalue
[i
][0] = "qtotal";
2361 keyvalue
[i
++][1] = "1";
2363 keyvalue
[i
][0] = "rp";
2364 keyvalue
[i
++][1] = rp_str
;
2366 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2368 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2369 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2371 keyvalue
[i
][0] = "ty";
2372 keyvalue
[i
++][1] = p
->make_model
;
2374 if (p
->location
&& *p
->location
!= '\0')
2376 keyvalue
[i
][0] = "note";
2377 keyvalue
[i
++][1] = p
->location
;
2380 keyvalue
[i
][0] = "priority";
2381 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2383 keyvalue
[i
][0] = "product";
2384 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2386 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2387 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2389 keyvalue
[i
][0] = "printer-state";
2390 keyvalue
[i
++][1] = state_str
;
2392 keyvalue
[i
][0] = "printer-type";
2393 keyvalue
[i
++][1] = type_str
;
2395 keyvalue
[i
][0] = "Transparent";
2396 keyvalue
[i
++][1] = "T";
2398 keyvalue
[i
][0] = "Binary";
2399 keyvalue
[i
++][1] = "T";
2401 if ((p
->type
& CUPS_PRINTER_FAX
))
2403 keyvalue
[i
][0] = "Fax";
2404 keyvalue
[i
++][1] = "T";
2407 if ((p
->type
& CUPS_PRINTER_COLOR
))
2409 keyvalue
[i
][0] = "Color";
2410 keyvalue
[i
++][1] = "T";
2413 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2415 keyvalue
[i
][0] = "Duplex";
2416 keyvalue
[i
++][1] = "T";
2419 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2421 keyvalue
[i
][0] = "Staple";
2422 keyvalue
[i
++][1] = "T";
2425 if ((p
->type
& CUPS_PRINTER_COPIES
))
2427 keyvalue
[i
][0] = "Copies";
2428 keyvalue
[i
++][1] = "T";
2431 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2433 keyvalue
[i
][0] = "Collate";
2434 keyvalue
[i
++][1] = "T";
2437 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2439 keyvalue
[i
][0] = "Punch";
2440 keyvalue
[i
++][1] = "T";
2443 if ((p
->type
& CUPS_PRINTER_BIND
))
2445 keyvalue
[i
][0] = "Bind";
2446 keyvalue
[i
++][1] = "T";
2449 if ((p
->type
& CUPS_PRINTER_SORT
))
2451 keyvalue
[i
][0] = "Sort";
2452 keyvalue
[i
++][1] = "T";
2455 keyvalue
[i
][0] = "pdl";
2456 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2458 if (get_auth_info_required(p
, air_str
, sizeof(air_str
)))
2460 keyvalue
[i
][0] = "air";
2461 keyvalue
[i
++][1] = air_str
;
2465 * Then pack them into a proper txt record...
2468 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2473 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2476 static int /* O - Result of comparison */
2477 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2478 cupsd_printer_t
*b
)/* I - Second printer */
2480 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2485 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2490 dnssdDeregisterPrinter(
2491 cupsd_printer_t
*p
) /* I - Printer */
2493 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2496 * Closing the socket deregisters the service
2501 DNSServiceRefDeallocate(p
->ipp_ref
);
2508 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2517 DNSServiceRefDeallocate(p
->printer_ref
);
2518 p
->printer_ref
= NULL
;
2524 * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
2527 free(p
->printer_txt
);
2528 p
->printer_txt
= NULL
;
2532 * Remove the printer from the array of DNS-SD printers, then clear the
2533 * registered name...
2536 cupsArrayRemove(DNSSDPrinters
, p
);
2537 cupsdClearString(&p
->reg_name
);
2542 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2543 * TXT record format.
2546 static char * /* O - TXT record */
2547 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2548 char *keyvalue
[][2], /* I - Table of key value pairs */
2549 int count
) /* I - Items in table */
2551 int i
; /* Looping var */
2552 int length
; /* Length of TXT record */
2553 int length2
; /* Length of value */
2554 char *txtRecord
; /* TXT record buffer */
2555 char *cursor
; /* Looping pointer */
2559 * Calculate the buffer size
2562 for (length
= i
= 0; i
< count
; i
++)
2563 length
+= 1 + strlen(keyvalue
[i
][0]) +
2564 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2567 * Allocate and fill it
2570 txtRecord
= malloc(length
);
2575 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2578 * Drop in the p-string style length byte followed by the data
2581 length
= strlen(keyvalue
[i
][0]);
2582 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2584 *cursor
++ = (unsigned char)(length
+ length2
);
2586 memcpy(cursor
, keyvalue
[i
][0], length
);
2593 memcpy(cursor
, keyvalue
[i
][1], length2
);
2604 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2608 dnssdRegisterCallback(
2609 DNSServiceRef sdRef
, /* I - DNS Service reference */
2610 DNSServiceFlags flags
, /* I - Reserved for future use */
2611 DNSServiceErrorType errorCode
, /* I - Error code */
2612 const char *name
, /* I - Service name */
2613 const char *regtype
, /* I - Service type */
2614 const char *domain
, /* I - Domain. ".local" for now */
2615 void *context
) /* I - User-defined context */
2617 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2618 /* Current printer */
2621 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s (%s)",
2622 name
, regtype
, p
? p
->name
: "Web Interface",
2623 p
? (p
->reg_name
? p
->reg_name
: "(null)") : "NA");
2627 cupsdLogMessage(CUPSD_LOG_ERROR
,
2628 "DNSServiceRegister failed with error %d", (int)errorCode
);
2631 else if (p
&& (!p
->reg_name
|| strcasecmp(name
, p
->reg_name
)))
2633 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2636 cupsArrayRemove(DNSSDPrinters
, p
);
2637 cupsdSetString(&p
->reg_name
, name
);
2638 cupsArrayAdd(DNSSDPrinters
, p
);
2640 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2646 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2647 * or update the broadcast contents.
2651 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2653 DNSServiceErrorType se
; /* dnssd errors */
2654 char *ipp_txt
, /* IPP TXT record buffer */
2655 *printer_txt
, /* LPD TXT record buffer */
2656 name
[1024], /* Service name */
2657 *nameptr
; /* Pointer into name */
2658 int ipp_len
, /* IPP TXT record length */
2659 printer_len
; /* LPD TXT record length */
2660 const char *regtype
; /* Registration type */
2663 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2664 !p
->ipp_ref
? "new" : "update");
2667 * If per-printer sharing was just disabled make sure we're not
2668 * registered before returning.
2673 dnssdDeregisterPrinter(p
);
2678 * The registered name takes the form of "<printer-info> @ <computer name>"...
2681 if (p
->info
&& strlen(p
->info
) > 0)
2683 if (DNSSDComputerName
)
2684 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDComputerName
);
2686 strlcpy(name
, p
->info
, sizeof(name
));
2688 else if (DNSSDComputerName
)
2689 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDComputerName
);
2691 strlcpy(name
, p
->name
, sizeof(name
));
2694 * If an existing printer was renamed, unregister it and start over...
2697 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2698 dnssdDeregisterPrinter(p
);
2702 cupsdSetString(&p
->reg_name
, name
);
2703 cupsArrayAdd(DNSSDPrinters
, p
);
2707 * Register IPP and (optionally) LPD...
2710 ipp_len
= 0; /* anti-compiler-warning-code */
2711 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2714 (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
)))
2717 * Update the existing registration...
2720 /* A TTL of 0 means use record's original value (Radar 3176248) */
2721 if ((se
= DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
,
2722 0)) == kDNSServiceErr_NoError
)
2727 p
->ipp_txt
= ipp_txt
;
2728 p
->ipp_len
= ipp_len
;
2734 * Failed to update record, lets close this reference and move on...
2737 cupsdLogMessage(CUPSD_LOG_ERROR
,
2738 "Unable to update IPP DNS-SD record for %s - %d", p
->name
,
2741 DNSServiceRefDeallocate(p
->ipp_ref
);
2749 * Initial registration. Use the _fax subtype for fax queues...
2752 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2755 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2756 "Registering DNS-SD printer %s with name \"%s\" and "
2757 "type \"%s\"", p
->name
, name
, regtype
);
2760 * Register the queue, dropping characters as needed until we succeed...
2763 nameptr
= name
+ strlen(name
);
2767 p
->ipp_ref
= DNSSDRef
;
2768 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2769 0, name
, regtype
, NULL
, NULL
,
2770 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2771 dnssdRegisterCallback
,
2772 p
)) == kDNSServiceErr_BadParam
)
2775 * Name is too long, drop trailing characters, taking into account
2781 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2788 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2790 if (se
== kDNSServiceErr_NoError
)
2792 p
->ipp_txt
= ipp_txt
;
2793 p
->ipp_len
= ipp_len
;
2797 cupsdLogMessage(CUPSD_LOG_WARN
,
2798 "DNS-SD IPP registration of \"%s\" failed: %d",
2805 if (BrowseLocalProtocols
& BROWSE_LPD
)
2807 printer_len
= 0; /* anti-compiler-warning-code */
2808 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2810 if (p
->printer_ref
&&
2811 (printer_len
!= p
->printer_len
||
2812 memcmp(printer_txt
, p
->printer_txt
, printer_len
)))
2815 * Update the existing registration...
2818 /* A TTL of 0 means use record's original value (Radar 3176248) */
2819 if ((se
= DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2821 0)) == kDNSServiceErr_NoError
)
2824 free(p
->printer_txt
);
2826 p
->printer_txt
= printer_txt
;
2827 p
->printer_len
= printer_len
;
2833 * Failed to update record, lets close this reference and move on...
2836 cupsdLogMessage(CUPSD_LOG_ERROR
,
2837 "Unable to update LPD DNS-SD record for %s - %d",
2840 DNSServiceRefDeallocate(p
->printer_ref
);
2841 p
->printer_ref
= NULL
;
2845 if (!p
->printer_ref
)
2848 * Initial registration...
2851 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2852 "Registering DNS-SD printer %s with name \"%s\" and "
2853 "type \"_printer._tcp\"", p
->name
, name
);
2855 p
->printer_ref
= DNSSDRef
;
2856 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2857 kDNSServiceFlagsShareConnection
,
2858 0, name
, "_printer._tcp", NULL
, NULL
,
2859 htons(515), printer_len
, printer_txt
,
2860 dnssdRegisterCallback
,
2861 p
)) == kDNSServiceErr_NoError
)
2863 p
->printer_txt
= printer_txt
;
2864 p
->printer_len
= printer_len
;
2868 cupsdLogMessage(CUPSD_LOG_WARN
,
2869 "DNS-SD LPD registration of \"%s\" failed: %d",
2880 * 'dnssdStop()' - Stop all DNS-SD registrations.
2886 cupsd_printer_t
*p
; /* Current printer */
2890 * De-register the individual printers
2893 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2895 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2896 dnssdDeregisterPrinter(p
);
2899 * Shutdown the rest of the service refs...
2904 DNSServiceRefDeallocate(WebIFRef
);
2910 DNSServiceRefDeallocate(RemoteRef
);
2914 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
2916 DNSServiceRefDeallocate(DNSSDRef
);
2919 cupsArrayDelete(DNSSDPrinters
);
2920 DNSSDPrinters
= NULL
;
2927 * 'dnssdUpdate()' - Handle DNS-SD queries.
2933 DNSServiceErrorType sdErr
; /* Service discovery error */
2936 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2938 cupsdLogMessage(CUPSD_LOG_ERROR
,
2939 "DNS Service Discovery registration error %d!",
2944 #endif /* HAVE_DNSSD */
2948 * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
2951 static char * /* O - String or NULL if none */
2952 get_auth_info_required(
2953 cupsd_printer_t
*p
, /* I - Printer */
2954 char *buffer
, /* I - Value buffer */
2955 size_t bufsize
) /* I - Size of value buffer */
2957 cupsd_location_t
*auth
; /* Pointer to authentication element */
2958 char resource
[1024]; /* Printer/class resource path */
2962 * If auth-info-required is set for this printer, return that...
2965 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2967 int i
; /* Looping var */
2968 char *bufptr
; /* Pointer into buffer */
2970 for (i
= 0, bufptr
= buffer
; i
< p
->num_auth_info_required
; i
++)
2972 if (bufptr
>= (buffer
+ bufsize
- 2))
2978 strlcpy(bufptr
, p
->auth_info_required
[i
], bufsize
- (bufptr
- buffer
));
2979 bufptr
+= strlen(bufptr
);
2986 * Figure out the authentication data requirements to advertise...
2989 if (p
->type
& CUPS_PRINTER_CLASS
)
2990 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2992 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2994 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2995 auth
->type
== CUPSD_AUTH_NONE
)
2996 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
3000 int auth_type
; /* Authentication type */
3002 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
3003 auth_type
= DefaultAuthType
;
3007 case CUPSD_AUTH_NONE
:
3010 case CUPSD_AUTH_NEGOTIATE
:
3011 strlcpy(buffer
, "negotiate", bufsize
);
3015 strlcpy(buffer
, "username,password", bufsize
);
3028 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
3031 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
3032 get_hostconfig(const char *name
) /* I - Name of service */
3034 cups_file_t
*fp
; /* Hostconfig file */
3035 char line
[1024], /* Line from file */
3036 *ptr
; /* Pointer to value */
3037 int state
= 1; /* State of service */
3041 * Try opening the /etc/hostconfig file; if we can't open it, assume that
3042 * the service is enabled/auto.
3045 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
3048 * Read lines from the file until we find the service...
3051 while (cupsFileGets(fp
, line
, sizeof(line
)))
3053 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
3058 if (!strcasecmp(line
, name
))
3061 * Found the service, see if it is set to "-NO-"...
3064 if (!strncasecmp(ptr
, "-NO-", 4))
3075 #endif /* __APPLE__ */
3079 * 'is_local_queue()' - Determine whether the URI points at a local queue.
3082 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
3083 is_local_queue(const char *uri
, /* I - Printer URI */
3084 char *host
, /* O - Host string */
3085 int hostlen
, /* I - Length of host buffer */
3086 char *resource
, /* O - Resource string */
3087 int resourcelen
) /* I - Length of resource buffer */
3089 char scheme
[32], /* Scheme portion of URI */
3090 username
[HTTP_MAX_URI
]; /* Username portion of URI */
3091 int port
; /* Port portion of URI */
3092 cupsd_netif_t
*iface
; /* Network interface */
3096 * Pull the URI apart to see if this is a local or remote printer...
3099 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3100 username
, sizeof(username
), host
, hostlen
, &port
,
3101 resource
, resourcelen
) < HTTP_URI_OK
)
3104 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
3107 * Check for local server addresses...
3110 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
3115 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3117 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3118 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
3122 * If we get here, the printer is remote...
3130 * 'process_browse_data()' - Process new browse data.
3134 process_browse_data(
3135 const char *uri
, /* I - URI of printer/class */
3136 const char *host
, /* I - Hostname */
3137 const char *resource
, /* I - Resource path */
3138 cups_ptype_t type
, /* I - Printer type */
3139 ipp_pstate_t state
, /* I - Printer state */
3140 const char *location
, /* I - Printer location */
3141 const char *info
, /* I - Printer information */
3142 const char *make_model
, /* I - Printer make and model */
3143 int num_attrs
, /* I - Number of attributes */
3144 cups_option_t
*attrs
) /* I - Attributes */
3146 int i
; /* Looping var */
3147 int update
; /* Update printer attributes? */
3148 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
3149 name
[IPP_MAX_NAME
], /* Name of printer */
3150 newname
[IPP_MAX_NAME
], /* New name of printer */
3151 *hptr
, /* Pointer into hostname */
3152 *sptr
; /* Pointer into ServerName */
3153 const char *shortname
; /* Short queue name (queue) */
3154 char local_make_model
[IPP_MAX_NAME
];
3155 /* Local make and model */
3156 cupsd_printer_t
*p
; /* Printer information */
3157 const char *ipp_options
, /* ipp-options value */
3158 *lease_duration
; /* lease-duration value */
3159 int is_class
; /* Is this queue a class? */
3162 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3163 "process_browse_data(uri=\"%s\", host=\"%s\", "
3164 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
3165 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
3166 uri
, host
, resource
, type
, state
,
3167 location
? location
: "(nil)", info
? info
: "(nil)",
3168 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
3171 * Determine if the URI contains any illegal characters in it...
3174 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
3175 (strncmp(resource
, "/printers/", 10) &&
3176 strncmp(resource
, "/classes/", 9)))
3178 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
3182 if (strchr(resource
, '?') ||
3183 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
3184 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
3186 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
3192 * OK, this isn't a local printer; add any remote options...
3195 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
3197 if (BrowseRemoteOptions
)
3199 if (BrowseRemoteOptions
[0] == '?')
3202 * Override server-supplied options...
3205 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
3207 else if (ipp_options
)
3210 * Combine the server and local options...
3213 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
3214 BrowseRemoteOptions
);
3219 * Just use the local options...
3222 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
3227 else if (ipp_options
)
3230 * Just use the server-supplied options...
3233 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
3238 * See if we already have it listed in the Printers list, and add it if not...
3241 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
3242 type
&= ~CUPS_PRINTER_IMPLICIT
;
3244 hptr
= strchr(host
, '.');
3245 sptr
= strchr(ServerName
, '.');
3246 is_class
= type
& CUPS_PRINTER_CLASS
;
3248 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
3251 * Strip the common domain name components...
3254 while (hptr
!= NULL
)
3256 if (!strcasecmp(hptr
, sptr
))
3262 hptr
= strchr(hptr
+ 1, '.');
3269 * Remote destination is a class...
3272 if (!strncmp(resource
, "/classes/", 9))
3273 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
3277 shortname
= resource
+ 9;
3282 * Remote destination is a printer...
3285 if (!strncmp(resource
, "/printers/", 10))
3286 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
3290 shortname
= resource
+ 10;
3294 *hptr
= '.'; /* Resource FQDN */
3296 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
3299 * Long name doesn't exist, try short name...
3302 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
3305 if ((p
= cupsdFindDest(shortname
)) == NULL
)
3308 * Short name doesn't exist, use it for this shared queue.
3311 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
3313 strlcpy(name
, shortname
, sizeof(name
));
3318 * Short name exists...
3321 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3322 "process_browse_data: %s found, type=%x, hostname=%s...",
3323 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3325 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3326 p
= NULL
; /* Don't replace implicit classes */
3327 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
3330 * Short name exists but is for a different host. If this is a remote
3331 * queue, rename it and use the long name...
3334 if (p
->type
& CUPS_PRINTER_REMOTE
)
3336 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3337 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3338 is_class
? "class" : "printer", p
->name
, p
->name
,
3340 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3341 "%s \'%s\' deleted by directory services.",
3342 is_class
? "Class" : "Printer", p
->name
);
3344 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
3345 cupsdRenamePrinter(p
, newname
);
3347 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3348 "%s \'%s\' added by directory services.",
3349 is_class
? "Class" : "Printer", p
->name
);
3353 * Force creation with long name...
3361 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3362 "process_browse_data: %s found, type=%x, hostname=%s...",
3363 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3368 * Queue doesn't exist; add it...
3372 p
= cupsdAddClass(name
);
3374 p
= cupsdAddPrinter(name
);
3379 cupsdClearString(&(p
->hostname
));
3381 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3382 is_class
? "class" : "printer", name
);
3384 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3385 "%s \'%s\' added by directory services.",
3386 is_class
? "Class" : "Printer", name
);
3389 * Force the URI to point to the real server...
3392 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3395 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3401 * Hostname not set, so this must be a cached remote printer
3402 * that was created for a pending print job...
3405 cupsdSetString(&p
->hostname
, host
);
3406 cupsdSetString(&p
->uri
, uri
);
3407 cupsdSetString(&p
->device_uri
, uri
);
3410 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3414 * Update the state...
3418 p
->browse_time
= time(NULL
);
3420 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3424 * Grab the lease-duration for the browse data; anything less then 1
3425 * second or more than 1 week gets the default BrowseTimeout...
3428 i
= atoi(lease_duration
);
3429 if (i
< 1 || i
> 604800)
3432 p
->browse_expire
= p
->browse_time
+ i
;
3435 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3437 if (type
& CUPS_PRINTER_REJECTING
)
3439 type
&= ~CUPS_PRINTER_REJECTING
;
3447 else if (!p
->accepting
)
3453 if (p
->type
!= type
)
3459 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3461 cupsdSetString(&p
->location
, location
);
3465 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3467 cupsdSetString(&p
->info
, info
);
3470 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3473 if (!make_model
|| !make_model
[0])
3476 snprintf(local_make_model
, sizeof(local_make_model
),
3477 "Remote Class on %s", host
);
3479 snprintf(local_make_model
, sizeof(local_make_model
),
3480 "Remote Printer on %s", host
);
3483 snprintf(local_make_model
, sizeof(local_make_model
),
3484 "%s on %s", make_model
, host
);
3486 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3488 cupsdSetString(&p
->make_model
, local_make_model
);
3494 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3497 * See if we need to update the attributes...
3500 if (p
->num_options
!= num_attrs
)
3504 for (i
= 0; i
< num_attrs
; i
++)
3505 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3506 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3507 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3516 * Free the old options...
3519 cupsFreeOptions(p
->num_options
, p
->options
);
3522 p
->num_options
= num_attrs
;
3525 if (type
& CUPS_PRINTER_DELETE
)
3527 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3528 "%s \'%s\' deleted by directory services.",
3529 is_class
? "Class" : "Printer", p
->name
);
3531 cupsdExpireSubscriptions(p
, NULL
);
3533 cupsdDeletePrinter(p
, 1);
3534 cupsdUpdateImplicitClasses();
3535 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3539 cupsdSetPrinterAttrs(p
);
3540 cupsdUpdateImplicitClasses();
3544 * See if we have a default printer... If not, make the first network
3545 * default printer the default.
3548 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3551 * Find the first network default printer and use it...
3554 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3556 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3557 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3560 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3566 * Do auto-classing if needed...
3569 process_implicit_classes();
3574 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3578 process_implicit_classes(void)
3580 int i
; /* Looping var */
3581 int update
; /* Update printer attributes? */
3582 char name
[IPP_MAX_NAME
], /* Name of printer */
3583 *hptr
; /* Pointer into hostname */
3584 cupsd_printer_t
*p
, /* Printer information */
3585 *pclass
, /* Printer class */
3586 *first
; /* First printer in class */
3587 int offset
, /* Offset of name */
3588 len
; /* Length of name */
3591 if (!ImplicitClasses
|| !Printers
)
3595 * Loop through all available printers and create classes as needed...
3598 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3599 update
= 0, pclass
= NULL
, first
= NULL
;
3601 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3604 * Skip implicit classes...
3607 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3614 * If len == 0, get the length of this printer name up to the "@"
3618 cupsArraySave(Printers
);
3621 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3622 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3625 * We have more than one printer with the same name; see if
3626 * we have a class, and if this printer is a member...
3629 if (pclass
&& strcasecmp(pclass
->name
, name
))
3632 cupsdSetPrinterAttrs(pclass
);
3638 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3641 * Need to add the class...
3644 pclass
= cupsdAddPrinter(name
);
3645 cupsArrayAdd(ImplicitPrinters
, pclass
);
3647 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3648 pclass
->accepting
= 1;
3649 pclass
->state
= IPP_PRINTER_IDLE
;
3651 cupsdSetString(&pclass
->location
, p
->location
);
3652 cupsdSetString(&pclass
->info
, p
->info
);
3654 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3655 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3659 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3661 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3663 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3664 "Implicit class \'%s\' added by directory services.",
3670 for (i
= 0; i
< pclass
->num_printers
; i
++)
3671 if (pclass
->printers
[i
] == first
)
3674 if (i
>= pclass
->num_printers
)
3676 first
->in_implicit_class
= 1;
3677 cupsdAddPrinterToClass(pclass
, first
);
3683 for (i
= 0; i
< pclass
->num_printers
; i
++)
3684 if (pclass
->printers
[i
] == p
)
3687 if (i
>= pclass
->num_printers
)
3689 p
->in_implicit_class
= 1;
3690 cupsdAddPrinterToClass(pclass
, p
);
3697 * First time around; just get name length and mark it as first
3701 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3702 len
= hptr
- p
->name
;
3704 len
= strlen(p
->name
);
3706 if (len
>= sizeof(name
))
3709 * If the printer name length somehow is greater than we normally allow,
3710 * skip this printer...
3714 cupsArrayRestore(Printers
);
3718 strncpy(name
, p
->name
, len
);
3722 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3723 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3726 * Can't use same name as a local printer; add "Any" to the
3727 * front of the name, unless we have explicitly disabled
3728 * the "ImplicitAnyClasses"...
3731 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3734 * Add "Any" to the class name...
3737 strcpy(name
, "Any");
3738 strncpy(name
+ 3, p
->name
, len
);
3739 name
[len
+ 3] = '\0';
3745 * Don't create an implicit class if we have a local printer
3746 * with the same name...
3750 cupsArrayRestore(Printers
);
3758 cupsArrayRestore(Printers
);
3762 * Update the last printer class as needed...
3765 if (pclass
&& update
)
3766 cupsdSetPrinterAttrs(pclass
);
3771 * 'send_cups_browse()' - Send new browsing information using the CUPS
3776 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3778 int i
; /* Looping var */
3779 cups_ptype_t type
; /* Printer type */
3780 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3781 int bytes
; /* Length of packet */
3782 char packet
[1453], /* Browse data packet */
3783 uri
[1024], /* Printer URI */
3784 location
[1024], /* printer-location */
3785 info
[1024], /* printer-info */
3787 /* printer-make-and-model */
3788 air
[1024]; /* auth-info-required */
3789 cupsd_netif_t
*iface
; /* Network interface */
3793 * Figure out the printer type value...
3796 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3799 type
|= CUPS_PRINTER_REJECTING
;
3801 if (p
== DefaultPrinter
)
3802 type
|= CUPS_PRINTER_DEFAULT
;
3805 * Remove quotes from printer-info, printer-location, and
3806 * printer-make-and-model attributes...
3809 dequote(location
, p
->location
, sizeof(location
));
3810 dequote(info
, p
->info
, sizeof(info
));
3813 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3814 else if (p
->type
& CUPS_PRINTER_CLASS
)
3816 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3817 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3819 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3822 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3824 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3826 if (get_auth_info_required(p
, packet
, sizeof(packet
)))
3827 snprintf(air
, sizeof(air
), " auth-info-required=%s", packet
);
3832 * Send a packet to each browse address...
3835 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3839 * Send the browse packet to one or more interfaces...
3842 if (!strcmp(b
->iface
, "*"))
3845 * Send to all local interfaces...
3850 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3852 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3855 * Only send to local, IPv4 interfaces...
3858 if (!iface
->is_local
|| !iface
->port
||
3859 iface
->address
.addr
.sa_family
!= AF_INET
)
3862 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3863 iface
->hostname
, iface
->port
,
3864 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3867 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3868 type
, p
->state
, uri
, location
, info
, make_model
,
3869 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3871 bytes
= strlen(packet
);
3873 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3874 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3875 iface
->name
, packet
);
3877 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3879 sendto(BrowseSocket
, packet
, bytes
, 0,
3880 (struct sockaddr
*)&(iface
->broadcast
),
3881 httpAddrLength(&(iface
->broadcast
)));
3884 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3887 * Send to the named interface using the IPv4 address...
3891 if (strcmp(b
->iface
, iface
->name
))
3896 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3899 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3903 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3904 iface
->hostname
, iface
->port
,
3905 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3908 snprintf(packet
, sizeof(packet
),
3909 "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3910 type
, p
->state
, uri
, location
, info
, make_model
,
3911 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3913 bytes
= strlen(packet
);
3915 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3916 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3917 iface
->name
, packet
);
3919 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3921 sendto(BrowseSocket
, packet
, bytes
, 0,
3922 (struct sockaddr
*)&(iface
->broadcast
),
3923 httpAddrLength(&(iface
->broadcast
)));
3930 * Send the browse packet to the indicated address using
3931 * the default server name...
3934 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s%s\n",
3935 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3936 p
->browse_attrs
? p
->browse_attrs
: "", air
);
3938 bytes
= strlen(packet
);
3939 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3940 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3942 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3943 (struct sockaddr
*)&(b
->to
),
3944 httpAddrLength(&(b
->to
))) <= 0)
3947 * Unable to send browse packet, so remove this address from the
3951 cupsdLogMessage(CUPSD_LOG_ERROR
,
3952 "cupsdSendBrowseList: sendto failed for browser "
3954 (int)(b
- Browsers
+ 1), strerror(errno
));
3957 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3968 * 'ldap_search_rec()' - LDAP Search with reconnect
3972 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3973 char *base
, /* I - Base dn */
3974 int scope
, /* I - LDAP search scope */
3975 char *filter
, /* I - Filter string */
3976 char *attrs
[], /* I - Requested attributes */
3977 int attrsonly
, /* I - Return only attributes? */
3978 LDAPMessage
**res
) /* I - LDAP handler */
3980 int rc
; /* Return code */
3983 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3984 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3985 NULL
, LDAP_NO_LIMIT
, res
);
3987 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3988 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3991 * If we have a connection problem try again...
3994 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3996 cupsdLogMessage(CUPSD_LOG_ERROR
,
3997 "LDAP search failed with status %d: %s",
3998 rc
, ldap_err2string(rc
));
3999 cupsdLogMessage(CUPSD_LOG_INFO
,
4000 "We try the LDAP search once again after reconnecting to "
4005 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4006 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
4007 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
4009 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
4010 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4013 if (rc
== LDAP_NO_SUCH_OBJECT
)
4014 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4015 "ldap_search_rec: LDAP entry/object not found");
4016 else if (rc
!= LDAP_SUCCESS
)
4017 cupsdLogMessage(CUPSD_LOG_ERROR
,
4018 "ldap_search_rec: LDAP search failed with status %d: %s",
4019 rc
, ldap_err2string(rc
));
4021 if (rc
!= LDAP_SUCCESS
)
4029 * 'ldap_freeres()' - Free LDAPMessage
4033 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
4035 int rc
; /* Return value */
4038 rc
= ldap_msgfree(entry
);
4040 cupsdLogMessage(CUPSD_LOG_WARN
,
4041 "Can't free LDAPMessage!");
4043 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4044 "Freeing LDAPMessage was unnecessary");
4049 * 'ldap_getval_char()' - Get first LDAP value and convert to string
4053 ldap_getval_firststring(
4054 LDAP
*ld
, /* I - LDAP handler */
4055 LDAPMessage
*entry
, /* I - LDAP message or search result */
4056 char *attr
, /* I - the wanted attribute */
4057 char *retval
, /* O - String to return */
4058 unsigned long maxsize
) /* I - Max string size */
4060 char *dn
; /* LDAP DN */
4061 int rc
= 0; /* Return code */
4062 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4063 struct berval
**bval
; /* LDAP value array */
4064 unsigned long size
; /* String size */
4068 * Get value from LDAPMessage...
4071 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
4074 dn
= ldap_get_dn(ld
, entry
);
4075 cupsdLogMessage(CUPSD_LOG_WARN
,
4076 "Failed to get LDAP value %s for %s!",
4084 * Check size and copy value into our string...
4088 if (size
< bval
[0]->bv_len
)
4091 dn
= ldap_get_dn(ld
, entry
);
4092 cupsdLogMessage(CUPSD_LOG_WARN
,
4093 "Attribute %s is too big! (dn: %s)",
4098 size
= bval
[0]->bv_len
;
4100 strlcpy(retval
, bval
[0]->bv_val
, size
);
4101 ldap_value_free_len(bval
);
4104 char **value
; /* LDAP value */
4107 * Get value from LDAPMessage...
4110 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
4113 dn
= ldap_get_dn(ld
, entry
);
4114 cupsdLogMessage(CUPSD_LOG_WARN
,
4115 "Failed to get LDAP value %s for %s!",
4121 strlcpy(retval
, *value
, maxsize
);
4122 ldap_value_free(value
);
4124 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4131 * 'send_ldap_ou()' - Send LDAP ou registrations.
4135 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
4136 char *basedn
, /* I - Our base dn */
4137 char *descstring
) /* I - Description for ou */
4139 int i
; /* Looping var... */
4140 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
4141 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
4142 LDAPMessage
*res
, /* Search result token */
4143 *e
; /* Current entry from search */
4144 int rc
; /* LDAP status */
4145 char dn
[1024], /* DN of the organizational unit we are adding */
4146 *desc
[2], /* Change records */
4148 char old_desc
[1024]; /* Old description */
4149 static const char * const objectClass_values
[] =
4150 { /* The 2 objectClass's we use in */
4151 "top", /* our LDAP entries */
4152 "organizationalUnit",
4155 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
4162 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
4165 * Reconnect if LDAP Handle is invalid...
4168 if (! BrowseLDAPHandle
)
4170 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4171 "send_ldap_ou: LDAP Handle is invalid. Try "
4178 * Prepare ldap search...
4181 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4182 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
4186 desc
[0] = descstring
;
4189 mods
[0].mod_type
= "ou";
4190 mods
[0].mod_values
= ou_value
;
4191 mods
[1].mod_type
= "description";
4192 mods
[1].mod_values
= desc
;
4193 mods
[2].mod_type
= "objectClass";
4194 mods
[2].mod_values
= (char **)objectClass_values
;
4196 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4197 (char **)ou_attrs
, 0, &res
);
4200 * If ldap search was not successfull then exit function...
4203 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4207 * Check if we need to insert or update the LDAP entry...
4210 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4211 rc
!= LDAP_NO_SUCH_OBJECT
)
4214 * Printserver has already been registered, check if
4215 * modification is required...
4218 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4221 * Get the required values from this entry...
4224 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
4225 sizeof(old_desc
)) == -1)
4229 * Check if modification is required...
4232 if ( strcmp(desc
[0], old_desc
) == 0 )
4235 * LDAP entry for the printer exists.
4236 * Printer has already been registered,
4237 * no modifications required...
4239 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4240 "send_ldap_ou: No updates required for %s", ou
);
4245 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4246 "send_ldap_ou: Replace entry for %s", ou
);
4248 for (i
= 0; i
< 3; i
++)
4250 pmods
[i
] = mods
+ i
;
4251 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4255 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4256 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4257 NULL
)) != LDAP_SUCCESS
)
4259 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4260 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4262 cupsdLogMessage(CUPSD_LOG_ERROR
,
4263 "LDAP modify for %s failed with status %d: %s",
4264 ou
, rc
, ldap_err2string(rc
));
4265 if ( LDAP_SERVER_DOWN
== rc
)
4273 * Printserver has never been registered,
4274 * add registration...
4277 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4278 "send_ldap_ou: Add entry for %s", ou
);
4280 for (i
= 0; i
< 3; i
++)
4282 pmods
[i
] = mods
+ i
;
4283 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4287 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4288 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4289 NULL
)) != LDAP_SUCCESS
)
4291 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4292 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4294 cupsdLogMessage(CUPSD_LOG_ERROR
,
4295 "LDAP add for %s failed with status %d: %s",
4296 ou
, rc
, ldap_err2string(rc
));
4297 if ( LDAP_SERVER_DOWN
== rc
)
4307 * 'send_ldap_browse()' - Send LDAP printer registrations.
4311 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4313 int i
; /* Looping var... */
4314 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
4315 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
4316 LDAPMessage
*res
, /* Search result token */
4317 *e
; /* Current entry from search */
4318 char *cn_value
[2], /* Change records */
4324 typestring
[255], /* String to hold printer-type */
4325 dn
[1024]; /* DN of the printer we are adding */
4326 int rc
; /* LDAP status */
4327 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
4328 old_location
[1024], /* Printer location */
4329 old_info
[1024], /* Printer information */
4330 old_make_model
[1024], /* Printer make and model */
4331 old_type_string
[30]; /* Temporary type number */
4332 int old_type
; /* Printer type */
4333 static const char * const objectClass_values
[] =
4334 { /* The 3 objectClass's we use in */
4335 "top", /* our LDAP entries */
4342 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
4345 * Exit function if LDAP updates has been disabled...
4348 if (!BrowseLDAPUpdate
)
4350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4351 "send_ldap_browse: Updates temporary disabled; "
4357 * Reconnect if LDAP Handle is invalid...
4360 if (!BrowseLDAPHandle
)
4362 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4363 "send_ldap_browse: LDAP Handle is invalid. Try "
4370 * Everything in ldap is ** so we fudge around it...
4373 sprintf(typestring
, "%u", p
->type
);
4375 cn_value
[0] = p
->name
;
4377 info
[0] = p
->info
? p
->info
: "Unknown";
4379 location
[0] = p
->location
? p
->location
: "Unknown";
4381 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4382 make_model
[1] = NULL
;
4383 type
[0] = typestring
;
4389 * Get ldap entry for printer ...
4392 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4394 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4396 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4397 (char **)ldap_attrs
, 0, &res
);
4400 * If ldap search was not successfull then exit function
4401 * and temporary disable LDAP updates...
4404 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4406 if (BrowseLDAPUpdate
&&
4407 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4409 BrowseLDAPUpdate
= FALSE
;
4410 cupsdLogMessage(CUPSD_LOG_INFO
,
4411 "LDAP update temporary disabled");
4418 * Fill modification array...
4421 mods
[0].mod_type
= "cn";
4422 mods
[0].mod_values
= cn_value
;
4423 mods
[1].mod_type
= "printerDescription";
4424 mods
[1].mod_values
= info
;
4425 mods
[2].mod_type
= "printerURI";
4426 mods
[2].mod_values
= uri
;
4427 mods
[3].mod_type
= "printerLocation";
4428 mods
[3].mod_values
= location
;
4429 mods
[4].mod_type
= "printerMakeAndModel";
4430 mods
[4].mod_values
= make_model
;
4431 mods
[5].mod_type
= "printerType";
4432 mods
[5].mod_values
= type
;
4433 mods
[6].mod_type
= "objectClass";
4434 mods
[6].mod_values
= (char **)objectClass_values
;
4437 * Check if we need to insert or update the LDAP entry...
4440 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4441 rc
!= LDAP_NO_SUCH_OBJECT
)
4444 * Printer has already been registered, check if
4445 * modification is required...
4448 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4451 * Get the required values from this entry...
4454 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4455 old_info
, sizeof(old_info
)) == -1)
4458 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4459 old_location
, sizeof(old_location
)) == -1)
4462 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4463 old_make_model
, sizeof(old_make_model
)) == -1)
4466 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4467 old_type_string
, sizeof(old_type_string
)) == -1)
4470 old_type
= atoi(old_type_string
);
4472 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4473 sizeof(old_uri
)) == -1)
4477 * Check if modification is required...
4480 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4481 !strcmp(location
[0], old_location
) &&
4482 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4485 * LDAP entry for the printer exists. Printer has already been registered,
4486 * no modifications required...
4489 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4490 "send_ldap_browse: No updates required for %s", p
->name
);
4495 * LDAP entry for the printer exists. Printer has already been registered,
4496 * modify the current registration...
4499 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4500 "send_ldap_browse: Replace entry for %s", p
->name
);
4502 for (i
= 0; i
< 7; i
++)
4504 pmods
[i
] = mods
+ i
;
4505 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4509 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4510 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4511 NULL
)) != LDAP_SUCCESS
)
4513 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4514 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4516 cupsdLogMessage(CUPSD_LOG_ERROR
,
4517 "LDAP modify for %s failed with status %d: %s",
4518 p
->name
, rc
, ldap_err2string(rc
));
4519 if (rc
== LDAP_SERVER_DOWN
)
4527 * No LDAP entry exists for the printer. Printer has never been registered,
4528 * add the current registration...
4531 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4533 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4534 "send_ldap_browse: Add entry for %s", p
->name
);
4536 for (i
= 0; i
< 7; i
++)
4538 pmods
[i
] = mods
+ i
;
4539 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4543 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4544 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4545 NULL
)) != LDAP_SUCCESS
)
4547 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4548 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4550 cupsdLogMessage(CUPSD_LOG_ERROR
,
4551 "LDAP add for %s failed with status %d: %s",
4552 p
->name
, rc
, ldap_err2string(rc
));
4553 if (rc
== LDAP_SERVER_DOWN
)
4563 * 'ldap_dereg_printer()' - Delete printer from directory
4567 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4569 char dn
[1024]; /* DN of the printer */
4570 int rc
; /* LDAP status */
4573 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4577 * Reconnect if LDAP Handle is invalid...
4580 if (!BrowseLDAPHandle
)
4587 * Get dn for printer and delete LDAP entry...
4590 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4592 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4594 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4595 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4596 NULL
)) != LDAP_SUCCESS
)
4598 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4599 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4601 cupsdLogMessage(CUPSD_LOG_WARN
,
4602 "LDAP delete for %s failed with status %d: %s",
4603 p
->name
, rc
, ldap_err2string(rc
));
4606 * If we had a connection problem (connection timed out, etc.)
4607 * we should reconnect and try again to delete the entry...
4610 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4612 cupsdLogMessage(CUPSD_LOG_INFO
,
4613 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4616 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4617 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4618 NULL
)) != LDAP_SUCCESS
)
4620 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4621 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4622 cupsdLogMessage(CUPSD_LOG_WARN
,
4623 "LDAP delete for %s failed with status %d: %s",
4624 p
->name
, rc
, ldap_err2string(rc
));
4631 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4632 char *basedn
) /* I - Dase dn */
4634 char dn
[1024]; /* DN of the printer */
4635 int rc
; /* LDAP status */
4638 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4641 * Reconnect if LDAP Handle is invalid...
4644 if (!BrowseLDAPHandle
)
4651 * Get dn for printer and delete LDAP entry...
4654 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4655 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4657 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4658 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4659 NULL
)) != LDAP_SUCCESS
)
4661 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4662 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4664 cupsdLogMessage(CUPSD_LOG_WARN
,
4665 "LDAP delete for %s failed with status %d: %s",
4666 ou
, rc
, ldap_err2string(rc
));
4669 * If we had a connection problem (connection timed out, etc.)
4670 * we should reconnect and try again to delete the entry...
4673 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4675 cupsdLogMessage(CUPSD_LOG_INFO
,
4676 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4678 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4679 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4680 NULL
)) != LDAP_SUCCESS
)
4682 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4683 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4684 cupsdLogMessage(CUPSD_LOG_WARN
,
4685 "LDAP delete for %s failed with status %d: %s",
4686 ou
, rc
, ldap_err2string(rc
));
4691 #endif /* HAVE_LDAP */
4696 * 'send_slp_browse()' - Register the specified printer with SLP.
4700 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4702 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4703 attrs
[8192], /* Printer attributes */
4704 finishings
[1024], /* Finishings to support */
4705 make_model
[IPP_MAX_NAME
* 2],
4706 /* Make and model, quoted */
4707 location
[IPP_MAX_NAME
* 2],
4708 /* Location, quoted */
4709 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4710 *src
, /* Pointer to original string */
4711 *dst
; /* Pointer to destination string */
4712 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4713 SLPError error
; /* SLP error, if any */
4716 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4720 * Make the SLP service URL that conforms to the IANA
4721 * 'printer:' template.
4724 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4726 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4729 * Figure out the finishings string...
4732 if (p
->type
& CUPS_PRINTER_STAPLE
)
4733 strcpy(finishings
, "staple");
4735 finishings
[0] = '\0';
4737 if (p
->type
& CUPS_PRINTER_BIND
)
4740 strlcat(finishings
, ",bind", sizeof(finishings
));
4742 strcpy(finishings
, "bind");
4745 if (p
->type
& CUPS_PRINTER_PUNCH
)
4748 strlcat(finishings
, ",punch", sizeof(finishings
));
4750 strcpy(finishings
, "punch");
4753 if (p
->type
& CUPS_PRINTER_COVER
)
4756 strlcat(finishings
, ",cover", sizeof(finishings
));
4758 strcpy(finishings
, "cover");
4761 if (p
->type
& CUPS_PRINTER_SORT
)
4764 strlcat(finishings
, ",sort", sizeof(finishings
));
4766 strcpy(finishings
, "sort");
4770 strcpy(finishings
, "none");
4773 * Quote any commas in the make and model, location, and info strings...
4776 for (src
= p
->make_model
, dst
= make_model
;
4777 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4779 if (*src
== ',' || *src
== '\\' || *src
== ')')
4788 strcpy(make_model
, "Unknown");
4790 for (src
= p
->location
, dst
= location
;
4791 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4793 if (*src
== ',' || *src
== '\\' || *src
== ')')
4802 strcpy(location
, "Unknown");
4804 for (src
= p
->info
, dst
= info
;
4805 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4807 if (*src
== ',' || *src
== '\\' || *src
== ')')
4816 strcpy(info
, "Unknown");
4819 * Get the authentication value...
4822 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4826 * Make the SLP attribute string list that conforms to
4827 * the IANA 'printer:' template.
4830 snprintf(attrs
, sizeof(attrs
),
4831 "(printer-uri-supported=%s),"
4832 "(uri-authentication-supported=%s>),"
4834 "(uri-security-supported=tls>),"
4836 "(uri-security-supported=none>),"
4837 #endif /* HAVE_SSL */
4838 "(printer-name=%s),"
4839 "(printer-location=%s),"
4840 "(printer-info=%s),"
4841 "(printer-more-info=%s),"
4842 "(printer-make-and-model=%s),"
4843 "(printer-type=%d),"
4844 "(charset-supported=utf-8),"
4845 "(natural-language-configured=%s),"
4846 "(natural-language-supported=de,en,es,fr,it),"
4847 "(color-supported=%s),"
4848 "(finishings-supported=%s),"
4849 "(sides-supported=one-sided%s),"
4850 "(multiple-document-jobs-supported=true)"
4851 "(ipp-versions-supported=1.0,1.1)",
4852 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4853 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4854 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4856 p
->type
& CUPS_PRINTER_DUPLEX
?
4857 ",two-sided-long-edge,two-sided-short-edge" : "");
4859 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4862 * Register the printer with the SLP server...
4865 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4866 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4868 if (error
!= SLP_OK
)
4869 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4875 * 'slp_attr_callback()' - SLP attribute callback
4878 static SLPBoolean
/* O - SLP_TRUE for success */
4880 SLPHandle hslp
, /* I - SLP handle */
4881 const char *attrlist
, /* I - Attribute list */
4882 SLPError errcode
, /* I - Parsing status for this attr */
4883 void *cookie
) /* I - Current printer */
4885 char *tmp
= 0; /* Temporary string */
4886 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4887 /* Current printer */
4890 (void)hslp
; /* anti-compiler-warning-code */
4893 * Bail if there was an error
4896 if (errcode
!= SLP_OK
)
4900 * Parse the attrlist to obtain things needed to build CUPS browse packet
4903 memset(p
, 0, sizeof(cupsd_printer_t
));
4905 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4907 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4909 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4911 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4912 p
->type
= atoi(tmp
);
4914 p
->type
= CUPS_PRINTER_REMOTE
;
4916 cupsdClearString(&tmp
);
4923 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4927 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4929 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4932 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4934 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4937 * Make the SLP service URL that conforms to the IANA
4938 * 'printer:' template.
4941 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4944 * Deregister the printer...
4947 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4953 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4956 static int /* O - 0 on success */
4957 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4958 const char *tag
, /* I - Name of attribute */
4959 char **valbuf
) /* O - Value */
4961 char *ptr1
, /* Pointer into string */
4965 cupsdClearString(valbuf
);
4967 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4969 ptr1
+= strlen(tag
);
4971 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4977 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4978 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4981 * Dequote the value...
4984 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4985 if (*ptr1
== '\\' && ptr1
[1])
4986 _cups_strcpy(ptr1
, ptr1
+ 1);
4997 * 'slp_reg_callback()' - Empty SLPRegReport.
5001 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
5002 SLPError errcode
, /* I - Error code, if any */
5003 void *cookie
) /* I - App data */
5014 * 'slp_url_callback()' - SLP service url callback
5017 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
5019 SLPHandle hslp
, /* I - SLP handle */
5020 const char *srvurl
, /* I - URL of service */
5021 unsigned short lifetime
, /* I - Life of service */
5022 SLPError errcode
, /* I - Existing error code */
5023 void *cookie
) /* I - Pointer to service list */
5025 slpsrvurl_t
*s
, /* New service entry */
5026 **head
; /* Pointer to head of entry */
5030 * Let the compiler know we won't be using these vars...
5037 * Bail if there was an error
5040 if (errcode
!= SLP_OK
)
5044 * Grab the head of the list...
5047 head
= (slpsrvurl_t
**)cookie
;
5050 * Allocate a *temporary* slpsrvurl_t to hold this entry.
5053 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
5057 * Copy the SLP service URL...
5060 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
5063 * Link the SLP service URL into the head of the list
5073 #endif /* HAVE_LIBSLP */
5077 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
5081 update_cups_browse(void)
5083 int i
; /* Looping var */
5084 int auth
; /* Authorization status */
5085 int len
; /* Length of name string */
5086 int bytes
; /* Number of bytes left */
5087 char packet
[1541], /* Broadcast packet */
5088 *pptr
; /* Pointer into packet */
5089 socklen_t srclen
; /* Length of source address */
5090 http_addr_t srcaddr
; /* Source address */
5091 char srcname
[1024]; /* Source hostname */
5092 unsigned address
[4]; /* Source address */
5093 unsigned type
; /* Printer type */
5094 unsigned state
; /* Printer state */
5095 char uri
[HTTP_MAX_URI
], /* Printer URI */
5096 host
[HTTP_MAX_URI
], /* Host portion of URI */
5097 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
5098 info
[IPP_MAX_NAME
], /* Information string */
5099 location
[IPP_MAX_NAME
], /* Location string */
5100 make_model
[IPP_MAX_NAME
];/* Make and model string */
5101 int num_attrs
; /* Number of attributes */
5102 cups_option_t
*attrs
; /* Attributes */
5106 * Read a packet from the browse socket...
5109 srclen
= sizeof(srcaddr
);
5110 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
5111 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
5114 * "Connection refused" is returned under Linux if the destination port
5115 * or address is unreachable from a previous sendto(); check for the
5116 * error here and ignore it for now...
5119 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
5121 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
5123 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
5126 closesocket(BrowseSocket
);
5128 close(BrowseSocket
);
5131 cupsdRemoveSelect(BrowseSocket
);
5134 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
5135 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
5141 packet
[bytes
] = '\0';
5144 * If we're about to sleep, ignore incoming browse packets.
5151 * Figure out where it came from...
5155 if (srcaddr
.addr
.sa_family
== AF_INET6
)
5157 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
5158 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
5159 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
5160 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
5163 #endif /* AF_INET6 */
5168 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
5171 if (HostNameLookups
)
5172 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
5174 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
5176 len
= strlen(srcname
);
5184 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
5187 * Access from localhost (127.0.0.1) is always allowed...
5190 auth
= CUPSD_AUTH_ALLOW
;
5195 * Do authorization checks on the domain/address...
5198 switch (BrowseACL
->order_type
)
5201 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
5204 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
5205 auth
= CUPSD_AUTH_ALLOW
;
5207 if (cupsdCheckAuth(address
, srcname
, len
,
5208 BrowseACL
->num_deny
, BrowseACL
->deny
))
5209 auth
= CUPSD_AUTH_DENY
;
5211 if (cupsdCheckAuth(address
, srcname
, len
,
5212 BrowseACL
->num_allow
, BrowseACL
->allow
))
5213 auth
= CUPSD_AUTH_ALLOW
;
5216 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
5217 auth
= CUPSD_AUTH_DENY
;
5219 if (cupsdCheckAuth(address
, srcname
, len
,
5220 BrowseACL
->num_allow
, BrowseACL
->allow
))
5221 auth
= CUPSD_AUTH_ALLOW
;
5223 if (cupsdCheckAuth(address
, srcname
, len
,
5224 BrowseACL
->num_deny
, BrowseACL
->deny
))
5225 auth
= CUPSD_AUTH_DENY
;
5231 auth
= CUPSD_AUTH_ALLOW
;
5233 if (auth
== CUPSD_AUTH_DENY
)
5235 cupsdLogMessage(CUPSD_LOG_DEBUG
,
5236 "update_cups_browse: Refused %d bytes from %s", bytes
,
5241 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
5242 "update_cups_browse: (%d bytes from %s) %s", bytes
,
5249 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
5251 cupsdLogMessage(CUPSD_LOG_WARN
,
5252 "update_cups_browse: Garbled browse packet - %s", packet
);
5256 strcpy(location
, "Location Unknown");
5257 strcpy(info
, "No Information Available");
5258 make_model
[0] = '\0';
5262 if ((pptr
= strchr(packet
, '\"')) != NULL
)
5265 * Have extended information; can't use sscanf for it because not all
5266 * sscanf's allow empty strings with %[^\"]...
5269 for (i
= 0, pptr
++;
5270 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
5272 location
[i
] = *pptr
;
5280 while (*pptr
&& isspace(*pptr
& 255))
5285 for (i
= 0, pptr
++;
5286 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
5295 while (*pptr
&& isspace(*pptr
& 255))
5300 for (i
= 0, pptr
++;
5301 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
5303 make_model
[i
] = *pptr
;
5308 make_model
[i
] = '\0';
5311 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
5317 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5318 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5319 type
, state
, uri
, location
, info
, make_model
));
5322 * Pull the URI apart to see if this is a local or remote printer...
5325 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
5327 cupsFreeOptions(num_attrs
, attrs
);
5335 for (i
= 0; i
< NumRelays
; i
++)
5336 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
5337 if (sendto(BrowseSocket
, packet
, bytes
, 0,
5338 (struct sockaddr
*)&(Relays
[i
].to
),
5339 httpAddrLength(&(Relays
[i
].to
))) <= 0)
5341 cupsdLogMessage(CUPSD_LOG_ERROR
,
5342 "update_cups_browse: sendto failed for relay %d - %s.",
5343 i
+ 1, strerror(errno
));
5344 cupsFreeOptions(num_attrs
, attrs
);
5349 * Process the browse data...
5352 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
5353 (ipp_pstate_t
)state
, location
, info
, make_model
,
5359 * 'update_lpd()' - Update the LPD configuration as needed.
5363 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
5370 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5371 * setting for backwards-compatibility.
5374 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5376 #endif /* __APPLE__ */
5378 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5381 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5384 char newfile
[1024]; /* New cups-lpd.N file */
5385 cups_file_t
*ofp
, /* Original file pointer */
5386 *nfp
; /* New file pointer */
5387 char line
[1024]; /* Line from file */
5390 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5392 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5394 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5395 LPDConfigFile
+ 9, strerror(errno
));
5399 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5401 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5402 newfile
, strerror(errno
));
5408 * Copy all of the lines from the cups-lpd file...
5411 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5415 cupsFilePrintf(nfp
, "%s\n", line
);
5416 snprintf(line
, sizeof(line
), "\tdisable = %s",
5417 onoff
? "no" : "yes");
5419 else if (!strstr(line
, "disable ="))
5420 cupsFilePrintf(nfp
, "%s\n", line
);
5425 rename(newfile
, LPDConfigFile
+ 9);
5428 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5431 * Enable/disable LPD via the launchctl command...
5434 char *argv
[5], /* Arguments for command */
5435 *envp
[MAX_ENV
]; /* Environment for command */
5436 int pid
; /* Process ID */
5439 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5440 argv
[0] = (char *)"launchctl";
5441 argv
[1] = (char *)(onoff
? "load" : "unload");
5442 argv
[2] = (char *)"-w";
5443 argv
[3] = LPDConfigFile
+ 10;
5446 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5449 #endif /* __APPLE__ */
5451 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5456 * 'update_polling()' - Read status messages from the poll daemons.
5460 update_polling(void)
5462 char *ptr
, /* Pointer to end of line in buffer */
5463 message
[1024]; /* Pointer to message text */
5464 int loglevel
; /* Log level for message */
5467 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5468 message
, sizeof(message
))) != NULL
)
5470 if (loglevel
== CUPSD_LOG_INFO
)
5471 cupsdLogMessage(CUPSD_LOG_INFO
, "%s", message
);
5473 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5477 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5480 * All polling processes have died; stop polling...
5483 cupsdLogMessage(CUPSD_LOG_ERROR
,
5484 "update_polling: all polling processes have exited!");
5491 * 'update_smb()' - Update the SMB configuration as needed.
5495 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5500 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5503 * Enable/disable SMB via the specified smb.conf config file...
5506 char newfile
[1024]; /* New smb.conf.N file */
5507 cups_file_t
*ofp
, /* Original file pointer */
5508 *nfp
; /* New file pointer */
5509 char line
[1024]; /* Line from file */
5510 int in_printers
; /* In [printers] section? */
5513 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5515 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5517 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5518 SMBConfigFile
+ 8, strerror(errno
));
5522 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5524 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5525 newfile
, strerror(errno
));
5531 * Copy all of the lines from the smb.conf file...
5536 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5538 if (in_printers
&& strstr(line
, "printable ="))
5539 snprintf(line
, sizeof(line
), " printable = %s",
5540 onoff
? "yes" : "no");
5542 cupsFilePrintf(nfp
, "%s\n", line
);
5545 in_printers
= !strcmp(line
, "[printers]");
5550 rename(newfile
, SMBConfigFile
+ 8);
5553 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5558 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".