/*
- * "$Id: dirsvc.c 5548 2006-05-19 19:38:31Z mike $"
+ * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $"
*
* Directory services routines for the Common UNIX Printing System (CUPS).
*
- * Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ * Copyright 2007 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
- * property of Easy Software Products and are protected by Federal
- * copyright law. Distribution and use rights are outlined in the file
- * "LICENSE.txt" which should have been included with this file. If this
- * file is missing or damaged please contact Easy Software Products
- * at:
- *
- * Attn: CUPS Licensing Information
- * Easy Software Products
- * 44141 Airport View Drive, Suite 204
- * Hollywood, Maryland 20636 USA
- *
- * Voice: (301) 373-9600
- * EMail: cups-info@cups.org
- * WWW: http://www.cups.org
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
* Contents:
*
+ * cupsdDeregisterPrinter() - Stop sending broadcast information for a
+ * local printer and remove any pending
+ * references to remote printers.
* cupsdLoadRemoteCache() - Load the remote printer cache.
+ * cupsdRegisterPrinter() - Start sending broadcast information for a
+ * printer update the broadcast contents.
* cupsdSaveRemoteCache() - Save the remote printer cache.
- * cupsdSendBrowseDelete() - Send a "browse delete" message for a
- * printer.
* cupsdSendBrowseList() - Send new browsing information as necessary.
* cupsdStartBrowsing() - Start sending and receiving broadcast
* information.
* cupsdStopBrowsing() - Stop sending and receiving broadcast
* information.
* cupsdStopPolling() - Stop polling servers as needed.
- * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
- * protocol.
- * cupsdUpdatePolling() - Read status messages from the poll daemons.
+ * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
+ * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
* cupsdUpdateSLPBrowse() - Get browsing information via SLP.
+ * dnssdBuildTxtRecord() - Build a TXT record from printer info.
+ * dnssdDeregisterPrinter() - Stop sending broadcast information for a
+ * printer.
+ * dnssdPackTxtRecord() - Pack an array of key/value pairs into the
+ * TXT record format.
+ * dnssdRegisterCallback() - DNSServiceRegister callback.
+ * dnssdRegisterPrinter() - Start sending broadcast information for a
+ * printer or update the broadcast contents.
* dequote() - Remote quotes from a string.
* process_browse_data() - Process new browse data.
* process_implicit_classes() - Create/update implicit classes as needed.
* slp_get_attr() - Get an attribute from an SLP registration.
* slp_reg_callback() - Empty SLPRegReport.
* slp_url_callback() - SLP service url callback
+ * update_cups_browse() - Update the browse lists using the CUPS
+ * protocol.
+ * update_lpd() - Update the LPD configuration as needed.
+ * update_polling() - Read status messages from the poll daemons.
+ * update_smb() - Update the SMB configuration as needed.
*/
/*
#include "cupsd.h"
#include <grp.h>
+#ifdef HAVE_DNSSD
+# include <dns_sd.h>
+# ifdef __APPLE__
+# include <nameser.h>
+# ifdef HAVE_COREFOUNDATION
+# include <CoreFoundation/CoreFoundation.h>
+# endif /* HAVE_COREFOUNDATION */
+# ifdef HAVE_SYSTEMCONFIGURATION
+# include <SystemConfiguration/SystemConfiguration.h>
+# endif /* HAVE_SYSTEMCONFIGURATION */
+# endif /* __APPLE__ */
+#endif /* HAVE_DNSSD */
+
/*
* Local functions...
ipp_pstate_t state, const char *location,
const char *info, const char *make_model,
int num_attrs, cups_option_t *attrs);
-static void process_implicit_classes(void);
+static void process_implicit_classes(int *write_printcap);
static void send_cups_browse(cupsd_printer_t *p);
#ifdef HAVE_LDAP
static void send_ldap_browse(cupsd_printer_t *p);
#ifdef HAVE_LIBSLP
static void send_slp_browse(cupsd_printer_t *p);
#endif /* HAVE_LIBSLP */
+static void update_cups_browse(void);
+static void update_lpd(int onoff);
+static void update_polling(void);
+static void update_smb(int onoff);
+
#ifdef HAVE_OPENLDAP
static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
SLPError errcode, void *cookie);
#endif /* HAVE_LIBSLP */
+#ifdef HAVE_DNSSD
+/*
+ * For IPP register using a subtype of 'cups' so that shared printer browsing
+ * only finds other CUPS servers (not all IPP based printers).
+ */
+static char dnssdIPPRegType[] = "_ipp._tcp,_cups";
+static char dnssdIPPFaxRegType[] = "_fax-ipp._tcp";
+
+static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p);
+static void dnssdDeregisterPrinter(cupsd_printer_t *p);
+static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
+ int count);
+static void dnssdRegisterCallback(DNSServiceRef sdRef,
+ DNSServiceFlags flags,
+ DNSServiceErrorType errorCode,
+ const char *name, const char *regtype,
+ const char *domain, void *context);
+static void dnssdRegisterPrinter(cupsd_printer_t *p);
+#endif /* HAVE_DNSSD */
+
+
+/*
+ * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
+ * local printer and remove any pending
+ * references to remote printers.
+ */
+
+void
+cupsdDeregisterPrinter(
+ cupsd_printer_t *p, /* I - Printer to register */
+ int removeit) /* I - Printer being permanently removed */
+{
+ /*
+ * Only deregister if browsing is enabled and it's a local printers...
+ */
+
+ if (!Browsing || !p->shared ||
+ (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ return;
+
+ /*
+ * Announce the deletion...
+ */
+
+ if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
+ {
+ cups_ptype_t savedtype = p->type; /* Saved printer type */
+
+ p->type |= CUPS_PRINTER_DELETE;
+
+ send_cups_browse(p);
+
+ p->type = savedtype;
+ }
+
+#ifdef HAVE_LIBSLP
+ if (BrowseLocalProtocols & BROWSE_SLP)
+ slp_dereg_printer(p);
+#endif /* HAVE_LIBSLP */
+
+#ifdef HAVE_DNSSD
+ if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD))
+ dnssdDeregisterPrinter(p);
+#endif /* HAVE_DNSSD */
+}
+
/*
* 'cupsdLoadRemoteCache()' - Load the remote printer cache.
time_t now; /* Current time */
+ /*
+ * Don't load the cache if the CUPS remote protocol is disabled...
+ */
+
+ if (!Browsing || !(BrowseRemoteProtocols & BROWSE_CUPS))
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdLoadRemoteCache: Not loading remote cache.");
+ return;
+ }
+
/*
* Open the remote.cache file...
*/
else
p = cupsdAddPrinter(value);
- p->accepting = 1;
- p->state = IPP_PRINTER_IDLE;
- p->type |= CUPS_PRINTER_REMOTE;
- p->browse_time = now + BrowseTimeout;
+ p->accepting = 1;
+ p->state = IPP_PRINTER_IDLE;
+ p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
+ p->browse_time = now;
+ p->browse_expire = now + BrowseTimeout;
/*
* Set the default printer as needed...
else
p = cupsdAddClass(value);
- p->accepting = 1;
- p->state = IPP_PRINTER_IDLE;
- p->type |= CUPS_PRINTER_REMOTE;
- p->browse_time = now + BrowseTimeout;
+ p->accepting = 1;
+ p->state = IPP_PRINTER_IDLE;
+ p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
+ p->browse_time = now;
+ p->browse_expire = now + BrowseTimeout;
/*
* Set the default printer as needed...
{
time_t t = atoi(value);
- if (t > (now + BrowseInterval))
- p->browse_time = t;
+ if (t > p->browse_expire)
+ p->browse_expire = t;
}
else
{
* Do auto-classing if needed...
*/
- process_implicit_classes();
+ process_implicit_classes(NULL);
+}
+
+
+/*
+ * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
+ * printer or update the broadcast contents.
+ */
+
+void
+cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
+{
+ if (!Browsing || !BrowseLocalProtocols || !BrowseInterval || !NumBrowsers ||
+ (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ return;
+
+#ifdef HAVE_LIBSLP
+/* if (BrowseLocalProtocols & BROWSE_SLP)
+ slpRegisterPrinter(p); */
+#endif /* HAVE_LIBSLP */
+
+#ifdef HAVE_DNSSD
+ if (BrowseLocalProtocols & BROWSE_DNSSD)
+ dnssdRegisterPrinter(p);
+#endif /* HAVE_DNSSD */
+}
+
+
+/*
+ * 'cupsdRestartPolling()' - Restart polling servers as needed.
+ */
+
+void
+cupsdRestartPolling(void)
+{
+ int i; /* Looping var */
+ cupsd_dirsvc_poll_t *pollp; /* Current polling server */
+
+
+ for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
+ if (pollp->pid)
+ kill(pollp->pid, SIGHUP);
}
return;
}
else
- cupsdLogMessage(CUPSD_LOG_INFO, "Saving remote.cache...");
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Saving remote.cache...");
/*
* Restrict access to the file...
* Skip local destinations...
*/
- if (!(printer->type & CUPS_PRINTER_REMOTE))
+ if (!(printer->type & CUPS_PRINTER_DISCOVERED))
continue;
/*
cupsFilePrintf(fp, "Type %d\n", printer->type);
- cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_time);
+ cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire);
if (printer->info)
cupsFilePrintf(fp, "Info %s\n", printer->info);
}
-/*
- * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
- */
-
-void
-cupsdSendBrowseDelete(
- cupsd_printer_t *p) /* I - Printer to delete */
-{
- /*
- * Only announce if browsing is enabled and this is a local queue...
- */
-
- if (!Browsing || !p->shared ||
- (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
- return;
-
- /*
- * First mark the printer for deletion...
- */
-
- p->type |= CUPS_PRINTER_DELETE;
-
- /*
- * Announce the deletion...
- */
-
- if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
- send_cups_browse(p);
-#ifdef HAVE_LIBSLP
- if ((BrowseLocalProtocols & BROWSE_SLP) && BrowseSLPHandle)
- slp_dereg_printer(p);
-#endif /* HAVE_LIBSLP */
-}
-
-
/*
* 'cupsdSendBrowseList()' - Send new browsing information as necessary.
*/
cupsd_printer_t *p; /* Current printer */
time_t ut, /* Minimum update time */
to; /* Timeout time */
+ int write_printcap; /* Write the printcap file? */
if (!Browsing || !BrowseLocalProtocols || !Printers)
p->browse_time = time(NULL);
- if (BrowseLocalProtocols & BROWSE_CUPS)
+ if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
send_cups_browse(p);
#ifdef HAVE_LIBSLP
* Loop through all of the printers and send local updates as needed...
*/
- for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), write_printcap = 0;
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
* If this is a remote queue, see if it needs to be timed out...
*/
- if (p->type & CUPS_PRINTER_REMOTE)
+ if (p->type & CUPS_PRINTER_DISCOVERED)
{
if (p->browse_expire < to)
{
cupsArraySave(Printers);
cupsdDeletePrinter(p, 1);
cupsArrayRestore(Printers);
+ write_printcap = 1;
}
}
}
+
+ if (write_printcap)
+ cupsdWritePrintcap();
}
{
int val; /* Socket option value */
struct sockaddr_in addr; /* Broadcast address */
+ cupsd_printer_t *p; /* Current printer */
BrowseNext = NULL;
if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to create broadcast "
- "socket - %s.", strerror(errno));
+ "Unable to create broadcast socket - %s.",
+ strerror(errno));
BrowseLocalProtocols &= ~BROWSE_CUPS;
BrowseRemoteProtocols &= ~BROWSE_CUPS;
return;
if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to bind broadcast "
- "socket - %s.", strerror(errno));
+ "Unable to bind broadcast socket - %s.",
+ strerror(errno));
#ifdef WIN32
closesocket(BrowseSocket);
val = 1;
if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.",
strerror(errno));
#ifdef WIN32
fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
/*
- * Finally, add the socket to the input selection set...
+ * Finally, add the socket to the input selection set as needed...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStartBrowsing: Adding fd %d to InputSet...",
- BrowseSocket);
+ if (BrowseRemoteProtocols & BROWSE_CUPS)
+ {
+ /*
+ * We only listen if we want remote printers...
+ */
- FD_SET(BrowseSocket, InputSet);
+ cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse,
+ NULL, NULL);
+ }
}
else
BrowseSocket = -1;
struct berval bv = {0, ""}; /* SASL bind value */
+ /*
+ * Set the certificate file to use for encrypted LDAP sessions...
+ */
+
+ if (BrowseLDAPCACertFile)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
+ BrowseLDAPCACertFile);
+
+ if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
+ (void *)BrowseLDAPCACertFile))
+ != LDAP_SUCCESS)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to set CA certificate file for LDAP "
+ "connections: %d - %s", rc, ldap_err2string(rc));
+ }
+
/*
* LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
*/
BrowseLDAPRefresh = 0;
}
#endif /* HAVE_OPENLDAP */
+
+ /*
+ * Enable LPD and SMB printer sharing as needed through external programs...
+ */
+
+ if (BrowseLocalProtocols & BROWSE_LPD)
+ update_lpd(1);
+
+ if (BrowseLocalProtocols & BROWSE_SMB)
+ update_smb(1);
+
+ /*
+ * Register the individual printers
+ */
+
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ cupsdRegisterPrinter(p);
}
int i; /* Looping var */
cupsd_dirsvc_poll_t *pollp; /* Current polling server */
char polld[1024]; /* Poll daemon path */
- char sport[10]; /* Server port */
- char bport[10]; /* Browser port */
- char interval[10]; /* Poll interval */
+ char sport[255]; /* Server port */
+ char bport[255]; /* Browser port */
+ char interval[255]; /* Poll interval */
int statusfds[2]; /* Status pipe */
char *argv[6]; /* Arguments */
char *envp[100]; /* Environment */
* Don't do anything if we aren't polling...
*/
- if (NumPolled == 0)
+ if (NumPolled == 0 || BrowseSocket < 0)
{
PollPipe = -1;
PollStatusBuffer = NULL;
argv[1] = pollp->hostname;
- if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1,
+ if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1,
0, &(pollp->pid)) < 0)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
* Finally, add the pipe to the input selection set...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe);
-
- FD_SET(PollPipe, InputSet);
+ cupsdAddSelect(PollPipe, (cupsd_selfunc_t)update_polling, NULL, NULL);
}
void
cupsdStopBrowsing(void)
{
+ cupsd_printer_t *p; /* Current printer */
+
+
if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
return;
+ /*
+ * De-register the individual printers
+ */
+
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ cupsdDeregisterPrinter(p, 1);
+
+ /*
+ * Shut down browsing sockets...
+ */
+
if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) &&
BrowseSocket >= 0)
{
close(BrowseSocket);
#endif /* WIN32 */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStopBrowsing: Removing fd %d from InputSet...",
- BrowseSocket);
-
- FD_CLR(BrowseSocket, InputSet);
+ cupsdRemoveSelect(BrowseSocket);
BrowseSocket = -1;
}
BrowseLDAPHandle = NULL;
}
#endif /* HAVE_OPENLDAP */
+
+ /*
+ * Disable LPD and SMB printer sharing as needed through external programs...
+ */
+
+ if (BrowseLocalProtocols & BROWSE_LPD)
+ update_lpd(0);
+
+ if (BrowseLocalProtocols & BROWSE_SMB)
+ update_smb(0);
}
cupsdStatBufDelete(PollStatusBuffer);
close(PollPipe);
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStopPolling: removing fd %d from InputSet.", PollPipe);
- FD_CLR(PollPipe, InputSet);
+ cupsdRemoveSelect(PollPipe);
PollPipe = -1;
PollStatusBuffer = NULL;
}
+#ifdef HAVE_DNSSD
/*
- * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
+ * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
*/
void
-cupsdUpdateCUPSBrowse(void)
+cupsdUpdateDNSSDBrowse(
+ cupsd_printer_t *p) /* I - Printer being queried */
{
- int i; /* Looping var */
- int auth; /* Authorization status */
- int len; /* Length of name string */
- int bytes; /* Number of bytes left */
- char packet[1541], /* Broadcast packet */
- *pptr; /* Pointer into packet */
- socklen_t srclen; /* Length of source address */
- http_addr_t srcaddr; /* Source address */
- char srcname[1024]; /* Source hostname */
- unsigned address[4]; /* Source address */
- unsigned type; /* Printer type */
- unsigned state; /* Printer state */
- char uri[HTTP_MAX_URI], /* Printer URI */
- host[HTTP_MAX_URI], /* Host portion of URI */
- resource[HTTP_MAX_URI], /* Resource portion of URI */
- info[IPP_MAX_NAME], /* Information string */
- location[IPP_MAX_NAME], /* Location string */
- make_model[IPP_MAX_NAME];/* Make and model string */
- int num_attrs; /* Number of attributes */
- cups_option_t *attrs; /* Attributes */
+ DNSServiceErrorType sdErr; /* Service discovery error */
- /*
- * Read a packet from the browse socket...
- */
-
- srclen = sizeof(srcaddr);
- if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
- (struct sockaddr *)&srcaddr, &srclen)) < 0)
+ if ((sdErr = DNSServiceProcessResult(p->dnssd_ipp_ref))
+ != kDNSServiceErr_NoError)
{
- /*
- * "Connection refused" is returned under Linux if the destination port
- * or address is unreachable from a previous sendto(); check for the
- * error here and ignore it for now...
- */
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNS Service Discovery registration error %d for \"%s\"!",
+ sdErr, p->name);
+ cupsdRemoveSelect(p->dnssd_ipp_fd);
+ DNSServiceRefDeallocate(p->dnssd_ipp_ref);
- if (errno != ECONNREFUSED && errno != EAGAIN)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
- strerror(errno));
- cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
+ p->dnssd_ipp_ref = NULL;
+ p->dnssd_ipp_fd = -1;
+ }
+}
+#endif /* HAVE_DNSSD */
- cupsdStopBrowsing();
- Browsing = 0;
- }
- return;
- }
+#ifdef HAVE_OPENLDAP
+/*
+ * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
+ */
+
+void
+cupsdUpdateLDAPBrowse(void)
+{
+ char uri[HTTP_MAX_URI], /* Printer URI */
+ host[HTTP_MAX_URI], /* Hostname */
+ resource[HTTP_MAX_URI], /* Resource path */
+ location[1024], /* Printer location */
+ info[1024], /* Printer information */
+ make_model[1024], /* Printer make and model */
+ **value; /* Holds the returned data from LDAP */
+ int type; /* Printer type */
+ int rc; /* LDAP status */
+ int limit; /* Size limit */
+ LDAPMessage *res, /* LDAP search results */
+ *e; /* Current entry from search */
- packet[bytes] = '\0';
/*
- * If we're about to sleep, ignore incoming browse packets.
+ * Search for printers...
*/
- if (Sleeping)
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName);
+
+ BrowseLDAPRefresh = time(NULL) + BrowseInterval;
+
+ rc = ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
+ "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res);
+ if (rc != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP search returned error %d: %s", rc,
+ ldap_err2string(rc));
+ return;
+ }
+
+ limit = ldap_count_entries(BrowseLDAPHandle, res);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit);
+ if (limit < 1)
return;
/*
- * Figure out where it came from...
+ * Loop through the available printers...
*/
-#ifdef AF_INET6
- if (srcaddr.addr.sa_family == AF_INET6)
- {
- address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
- address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
- address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
- address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
- }
- else
-#endif /* AF_INET6 */
+ for (e = ldap_first_entry(BrowseLDAPHandle, res);
+ e;
+ e = ldap_next_entry(BrowseLDAPHandle, e))
{
- address[0] = 0;
- address[1] = 0;
- address[2] = 0;
- address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
- }
+ /*
+ * Get the required values from this entry...
+ */
- if (HostNameLookups)
- httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
- else
- httpAddrString(&srcaddr, srcname, sizeof(srcname));
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerDescription")) == NULL)
+ continue;
- len = strlen(srcname);
+ strlcpy(info, *value, sizeof(info));
+ ldap_value_free(value);
- /*
- * Do ACL stuff...
- */
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerLocation")) == NULL)
+ continue;
- if (BrowseACL)
- {
- if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
- {
- /*
- * Access from localhost (127.0.0.1) is always allowed...
- */
+ strlcpy(location, *value, sizeof(location));
+ ldap_value_free(value);
- auth = AUTH_ALLOW;
- }
- else
- {
- /*
- * Do authorization checks on the domain/address...
- */
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerMakeAndModel")) == NULL)
+ continue;
- switch (BrowseACL->order_type)
- {
- default :
- auth = AUTH_DENY; /* anti-compiler-warning-code */
- break;
+ strlcpy(make_model, *value, sizeof(make_model));
+ ldap_value_free(value);
- case AUTH_ALLOW : /* Order Deny,Allow */
- auth = AUTH_ALLOW;
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerType")) == NULL)
+ continue;
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_deny, BrowseACL->deny))
- auth = AUTH_DENY;
+ type = atoi(*value);
+ ldap_value_free(value);
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_allow, BrowseACL->allow))
- auth = AUTH_ALLOW;
- break;
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerURI")) == NULL)
+ continue;
- case AUTH_DENY : /* Order Allow,Deny */
- auth = AUTH_DENY;
+ strlcpy(uri, *value, sizeof(uri));
+ ldap_value_free(value);
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_allow, BrowseACL->allow))
- auth = AUTH_ALLOW;
+ /*
+ * Process the entry as browse data...
+ */
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_deny, BrowseACL->deny))
- auth = AUTH_DENY;
- break;
- }
- }
- }
- else
- auth = AUTH_ALLOW;
+ if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
+ process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE,
+ location, info, make_model, 0, NULL);
- if (auth == AUTH_DENY)
- {
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes,
- srcname);
- return;
}
+}
+#endif /* HAVE_OPENLDAP */
+
+
+#ifdef HAVE_LIBSLP
+/*
+ * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
+ */
+
+void
+cupsdUpdateSLPBrowse(void)
+{
+ slpsrvurl_t *s, /* Temporary list of service URLs */
+ *next; /* Next service in list */
+ cupsd_printer_t p; /* Printer information */
+ const char *uri; /* Pointer to printer URI */
+ char host[HTTP_MAX_URI], /* Host portion of URI */
+ resource[HTTP_MAX_URI]; /* Resource portion of URI */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes,
- srcname, packet);
/*
- * Parse packet...
+ * Reset the refresh time...
*/
- if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
- {
- cupsdLogMessage(CUPSD_LOG_WARN,
- "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet);
- return;
- }
+ BrowseSLPRefresh = time(NULL) + BrowseInterval;
- strcpy(location, "Location Unknown");
- strcpy(info, "No Information Available");
- make_model[0] = '\0';
- num_attrs = 0;
- attrs = NULL;
+ /*
+ * Poll for remote printers using SLP...
+ */
- if ((pptr = strchr(packet, '\"')) != NULL)
+ s = NULL;
+
+ SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "",
+ slp_url_callback, &s);
+
+ /*
+ * Loop through the list of available printers...
+ */
+
+ for (; s; s = next)
{
/*
- * Have extended information; can't use sscanf for it because not all
- * sscanf's allow empty strings with %[^\"]...
+ * Save the "next" pointer...
*/
- for (i = 0, pptr ++;
- i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- location[i] = *pptr;
+ next = s->next;
- if (i)
- location[i] = '\0';
+ /*
+ * Load a cupsd_printer_t structure with the SLP service attributes...
+ */
- if (*pptr == '\"')
- pptr ++;
+ SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
- while (*pptr && isspace(*pptr & 255))
- pptr ++;
+ /*
+ * Process this printer entry...
+ */
- if (*pptr == '\"')
- {
- for (i = 0, pptr ++;
- i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- info[i] = *pptr;
+ uri = s->url + SLP_CUPS_SRVLEN + 1;
- info[i] = '\0';
+ if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
+ {
+ /*
+ * Pull the URI apart to see if this is a local or remote printer...
+ */
- if (*pptr == '\"')
- pptr ++;
+ if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
+ process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE,
+ p.location, p.info, p.make_model, 0, NULL);
+ }
- while (*pptr && isspace(*pptr & 255))
- pptr ++;
+ /*
+ * Free this listing...
+ */
- if (*pptr == '\"')
- {
- for (i = 0, pptr ++;
- i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- make_model[i] = *pptr;
+ cupsdClearString(&p.info);
+ cupsdClearString(&p.location);
+ cupsdClearString(&p.make_model);
- if (*pptr == '\"')
- pptr ++;
+ free(s);
+ }
+}
+#endif /* HAVE_LIBSLP */
- make_model[i] = '\0';
- if (*pptr)
- num_attrs = cupsParseOptions(pptr, num_attrs, &attrs);
- }
- }
- }
+/*
+ * 'dequote()' - Remote quotes from a string.
+ */
- DEBUG_puts(packet);
- DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
- "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
- type, state, uri, location, info, make_model));
+static char * /* O - Dequoted string */
+dequote(char *d, /* I - Destination string */
+ const char *s, /* I - Source string */
+ int dlen) /* I - Destination length */
+{
+ char *dptr; /* Pointer into destination */
- /*
- * Pull the URI apart to see if this is a local or remote printer...
- */
- if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
+ if (s)
{
- cupsFreeOptions(num_attrs, attrs);
- return;
- }
-
- /*
- * Do relaying...
- */
-
- for (i = 0; i < NumRelays; i ++)
- if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from)))
- if (sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(Relays[i].to),
- sizeof(http_addr_t)) <= 0)
+ for (dptr = d, dlen --; *s && dlen > 0; s ++)
+ if (*s != '\"')
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
- i + 1, strerror(errno));
- cupsFreeOptions(num_attrs, attrs);
- return;
+ *dptr++ = *s;
+ dlen --;
}
- /*
- * Process the browse data...
- */
+ *dptr = '\0';
+ }
+ else
+ *d = '\0';
- process_browse_data(uri, host, resource, (cups_ptype_t)type,
- (ipp_pstate_t)state, location, info, make_model,
- num_attrs, attrs);
+ return (d);
}
-#ifdef HAVE_OPENLDAP
/*
- * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
+ * 'is_local_queue()' - Determine whether the URI points at a local queue.
*/
-void
-cupsdUpdateLDAPBrowse(void)
-{
- char uri[HTTP_MAX_URI], /* Printer URI */
- host[HTTP_MAX_URI], /* Hostname */
- resource[HTTP_MAX_URI], /* Resource path */
- location[1024], /* Printer location */
- info[1024], /* Printer information */
- make_model[1024], /* Printer make and model */
- **value; /* Holds the returned data from LDAP */
- int type; /* Printer type */
- int rc; /* LDAP status */
- int limit; /* Size limit */
- LDAPMessage *res, /* LDAP search results */
- *e; /* Current entry from search */
-
-
- /*
- * Search for printers...
- */
-
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName);
-
- BrowseLDAPRefresh = time(NULL) + BrowseInterval;
-
- rc = ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
- "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res);
- if (rc != LDAP_SUCCESS)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "LDAP search returned error %d: %s", rc,
- ldap_err2string(rc));
- return;
- }
-
- limit = ldap_count_entries(BrowseLDAPHandle, res);
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit);
- if (limit < 1)
- return;
-
- /*
- * Loop through the available printers...
- */
-
- if ((e = ldap_first_entry(BrowseLDAPHandle, res)) == NULL)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get LDAP printer entry!");
- return;
- }
-
- while (e)
- {
- value = ldap_get_values(BrowseLDAPHandle, e, "printerDescription");
- strlcpy(info, *value, sizeof(info));
- ldap_value_free(value);
-
- value = ldap_get_values(BrowseLDAPHandle, e, "printerLocation");
- strlcpy(location, *value, sizeof(location));
- ldap_value_free(value);
-
- value = ldap_get_values(BrowseLDAPHandle, e, "printerMakeAndModel");
- strlcpy(make_model, *value, sizeof(make_model));
- ldap_value_free(value);
-
- value = ldap_get_values(BrowseLDAPHandle, e, "printerType");
- type = atoi(*value);
- ldap_value_free(value);
-
- value = ldap_get_values(BrowseLDAPHandle, e, "printerURI");
- strlcpy(uri, *value, sizeof(uri));
- ldap_value_free(value);
-
- if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
- process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE,
- location, info, make_model, 0, NULL);
-
- e = ldap_next_entry(BrowseLDAPHandle, e);
- }
-}
-#endif /* HAVE_OPENLDAP */
-
-
-/*
- * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
- */
-
-void
-cupsdUpdatePolling(void)
-{
- char *ptr, /* Pointer to end of line in buffer */
- message[1024]; /* Pointer to message text */
- int loglevel; /* Log level for message */
-
-
- while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
- message, sizeof(message))) != NULL)
- if (!strchr(PollStatusBuffer->buffer, '\n'))
- break;
-
- if (ptr == NULL)
- {
- /*
- * All polling processes have died; stop polling...
- */
-
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdUpdatePolling: all polling processes have exited!");
- cupsdStopPolling();
- }
-}
-
-
-#ifdef HAVE_LIBSLP
-/*
- * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
- */
-
-void
-cupsdUpdateSLPBrowse(void)
-{
- slpsrvurl_t *s, /* Temporary list of service URLs */
- *next; /* Next service in list */
- cupsd_printer_t p; /* Printer information */
- const char *uri; /* Pointer to printer URI */
- char host[HTTP_MAX_URI], /* Host portion of URI */
- resource[HTTP_MAX_URI]; /* Resource portion of URI */
-
-
- /*
- * Reset the refresh time...
- */
-
- BrowseSLPRefresh = time(NULL) + BrowseInterval;
-
- /*
- * Poll for remote printers using SLP...
- */
-
- s = NULL;
-
- SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "",
- slp_url_callback, &s);
-
- /*
- * Loop through the list of available printers...
- */
-
- for (; s; s = next)
- {
- /*
- * Save the "next" pointer...
- */
-
- next = s->next;
-
- /*
- * Load a cupsd_printer_t structure with the SLP service attributes...
- */
-
- SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
-
- /*
- * Process this printer entry...
- */
-
- uri = s->url + SLP_CUPS_SRVLEN + 1;
-
- if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
- {
- /*
- * Pull the URI apart to see if this is a local or remote printer...
- */
-
- if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
- process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE,
- p.location, p.info, p.make_model, 0, NULL);
- }
-
- /*
- * Free this listing...
- */
-
- cupsdClearString(&p.info);
- cupsdClearString(&p.location);
- cupsdClearString(&p.make_model);
-
- free(s);
- }
-}
-#endif /* HAVE_LIBSLP */
-
-
-/*
- * 'dequote()' - Remote quotes from a string.
- */
-
-static char * /* O - Dequoted string */
-dequote(char *d, /* I - Destination string */
- const char *s, /* I - Source string */
- int dlen) /* I - Destination length */
-{
- char *dptr; /* Pointer into destination */
-
-
- if (s)
- {
- for (dptr = d, dlen --; *s && dlen > 0; s ++)
- if (*s != '\"')
- {
- *dptr++ = *s;
- dlen --;
- }
-
- *dptr = '\0';
- }
- else
- *d = '\0';
-
- return (d);
-}
-
-
-/*
- * 'is_local_queue()' - Determine whether the URI points at a local queue.
- */
-
-static int /* O - 1 = local, 0 = remote, -1 = bad URI */
-is_local_queue(const char *uri, /* I - Printer URI */
- char *host, /* O - Host string */
- int hostlen, /* I - Length of host buffer */
- char *resource, /* O - Resource string */
- int resourcelen) /* I - Length of resource buffer */
+static int /* O - 1 = local, 0 = remote, -1 = bad URI */
+is_local_queue(const char *uri, /* I - Printer URI */
+ char *host, /* O - Host string */
+ int hostlen, /* I - Length of host buffer */
+ char *resource, /* O - Resource string */
+ int resourcelen) /* I - Length of resource buffer */
{
char scheme[32], /* Scheme portion of URI */
username[HTTP_MAX_URI]; /* Username portion of URI */
cups_option_t *attrs) /* I - Attributes */
{
int i; /* Looping var */
- int update; /* Update printer attributes? */
+ int update, /* Update printer attributes? */
+ write_printcap; /* Write the printcap file? */
char finaluri[HTTP_MAX_URI], /* Final URI for printer */
name[IPP_MAX_NAME], /* Name of printer */
newname[IPP_MAX_NAME], /* New name of printer */
* See if we already have it listed in the Printers list, and add it if not...
*/
- type |= CUPS_PRINTER_REMOTE;
- type &= ~CUPS_PRINTER_IMPLICIT;
- update = 0;
- hptr = strchr(host, '.');
- sptr = strchr(ServerName, '.');
+ type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
+ type &= ~CUPS_PRINTER_IMPLICIT;
+ update = 0;
+ write_printcap = 0;
+ hptr = strchr(host, '.');
+ sptr = strchr(ServerName, '.');
- if (sptr != NULL && hptr != NULL)
+ if (!ServerNameIsIP && sptr != NULL && hptr != NULL)
{
/*
* Strip the common domain name components...
else
return;
+ if (hptr && !*hptr)
+ *hptr = '.'; /* Resource FQDN */
+
if ((p = cupsdFindClass(name)) == NULL && BrowseShortNames)
{
if ((p = cupsdFindClass(resource + 9)) != NULL)
cupsdSetString(&p->device_uri, uri);
cupsdSetString(&p->hostname, host);
- update = 1;
+ update = 1;
+ write_printcap = 1;
}
}
else
else
return;
+ if (hptr && !*hptr)
+ *hptr = '.'; /* Resource FQDN */
+
if ((p = cupsdFindPrinter(name)) == NULL && BrowseShortNames)
{
if ((p = cupsdFindPrinter(resource + 10)) != NULL)
cupsdSetString(&p->uri, uri);
cupsdSetString(&p->device_uri, uri);
- update = 1;
+ write_printcap = 1;
+ update = 1;
}
}
if (info && (!p->info || strcmp(p->info, info)))
{
cupsdSetString(&p->info, info);
- update = 1;
+ update = 1;
+ write_printcap = 1;
}
if (!make_model || !make_model[0])
cupsdDeletePrinter(p, 1);
cupsdUpdateImplicitClasses();
+ write_printcap = 1;
}
else if (update)
{
if (p->type & CUPS_PRINTER_DEFAULT)
{
DefaultPrinter = p;
+ write_printcap = 1;
break;
}
}
* Do auto-classing if needed...
*/
- process_implicit_classes();
+ process_implicit_classes(&write_printcap);
/*
* Update the printcap file...
*/
- cupsdWritePrintcap();
+ if (write_printcap)
+ cupsdWritePrintcap();
}
+#ifdef HAVE_DNSSD
/*
- * 'process_implicit_classes()' - Create/update implicit classes as needed.
+ * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
*/
-static void
-process_implicit_classes(void)
+static char * /* O - TXT record */
+dnssdBuildTxtRecord(
+ int *txt_len, /* O - TXT record length */
+ cupsd_printer_t *p) /* I - Printer information */
{
- int i; /* Looping var */
- int update; /* Update printer attributes? */
- char name[IPP_MAX_NAME], /* Name of printer */
- *hptr; /* Pointer into hostname */
- cupsd_printer_t *p, /* Printer information */
- *pclass, /* Printer class */
- *first; /* First printer in class */
- int offset, /* Offset of name */
- len; /* Length of name */
-
+ int i, j; /* Looping vars */
+ char type_str[32], /* Type to string buffer */
+ state_str[32], /* State to string buffer */
+ rp_str[1024], /* Queue name string buffer */
+ air_str[1024], /* auth-info-required string buffer */
+ *keyvalue[32][2]; /* Table of key/value pairs */
- if (!ImplicitClasses || !Printers)
- return;
/*
- * Loop through all available printers and create classes as needed...
+ * Load up the key value pairs...
*/
- for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0,
- update = 0, pclass = NULL, first = NULL;
- p != NULL;
- p = (cupsd_printer_t *)cupsArrayNext(Printers))
- {
- /*
- * Skip implicit classes...
- */
+ i = 0;
- if (p->type & CUPS_PRINTER_IMPLICIT)
- {
- len = 0;
- continue;
- }
+ keyvalue[i ][0] = "txtvers";
+ keyvalue[i++][1] = "1";
- /*
- * If len == 0, get the length of this printer name up to the "@"
- * sign (if any).
- */
+ keyvalue[i ][0] = "qtotal";
+ keyvalue[i++][1] = "1";
- cupsArraySave(Printers);
+ keyvalue[i ][0] = "rp";
+ keyvalue[i++][1] = rp_str;
+ snprintf(rp_str, sizeof(rp_str), "%s/%s",
+ (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
- if (len > 0 &&
- !strncasecmp(p->name, name + offset, len) &&
- (p->name[len] == '\0' || p->name[len] == '@'))
- {
- /*
- * We have more than one printer with the same name; see if
- * we have a class, and if this printer is a member...
- */
+ keyvalue[i ][0] = "ty";
+ keyvalue[i++][1] = p->make_model;
- if (pclass && strcasecmp(pclass->name, name))
- {
- if (update)
- cupsdSetPrinterAttrs(pclass);
+ if (p->location && *p->location != '\0')
+ {
+ keyvalue[i ][0] = "note";
+ keyvalue[i++][1] = p->location;
+ }
- update = 0;
- pclass = NULL;
- }
+ keyvalue[i ][0] = "product";
+ keyvalue[i++][1] = p->product ? p->product : "Unknown";
- if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
- {
- /*
- * Need to add the class...
- */
+ snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
+ snprintf(state_str, sizeof(state_str), "%d", p->state);
- pclass = cupsdAddPrinter(name);
- cupsArrayAdd(ImplicitPrinters, pclass);
+ keyvalue[i ][0] = "printer-state";
+ keyvalue[i++][1] = state_str;
- pclass->type |= CUPS_PRINTER_IMPLICIT;
- pclass->accepting = 1;
- pclass->state = IPP_PRINTER_IDLE;
+ keyvalue[i ][0] = "printer-type";
+ keyvalue[i++][1] = type_str;
- cupsdSetString(&pclass->location, p->location);
- cupsdSetString(&pclass->info, p->info);
+ keyvalue[i ][0] = "Transparent";
+ keyvalue[i++][1] = "T";
- update = 1;
+ keyvalue[i ][0] = "Binary";
+ keyvalue[i++][1] = "T";
- cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...",
- name);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Implicit class \'%s\' added by directory services.",
- name);
- }
+ if ((p->type & CUPS_PRINTER_FAX))
+ {
+ keyvalue[i ][0] = "Fax";
+ keyvalue[i++][1] = "T";
+ }
- if (first != NULL)
- {
- for (i = 0; i < pclass->num_printers; i ++)
- if (pclass->printers[i] == first)
- break;
+ if ((p->type & CUPS_PRINTER_COLOR))
+ {
+ keyvalue[i ][0] = "Color";
+ keyvalue[i++][1] = "T";
+ }
- if (i >= pclass->num_printers)
- {
- first->in_implicit_class = 1;
- cupsdAddPrinterToClass(pclass, first);
- }
+ if ((p->type & CUPS_PRINTER_DUPLEX))
+ {
+ keyvalue[i ][0] = "Duplex";
+ keyvalue[i++][1] = "T";
+ }
- first = NULL;
- }
+ if ((p->type & CUPS_PRINTER_STAPLE))
+ {
+ keyvalue[i ][0] = "Staple";
+ keyvalue[i++][1] = "T";
+ }
- for (i = 0; i < pclass->num_printers; i ++)
- if (pclass->printers[i] == p)
- break;
+ if ((p->type & CUPS_PRINTER_COPIES))
+ {
+ keyvalue[i ][0] = "Copies";
+ keyvalue[i++][1] = "T";
+ }
- if (i >= pclass->num_printers)
- {
- p->in_implicit_class = 1;
- cupsdAddPrinterToClass(pclass, p);
- update = 1;
- }
- }
- else
- {
- /*
- * First time around; just get name length and mark it as first
- * in the list...
- */
+ if ((p->type & CUPS_PRINTER_COLLATE))
+ {
+ keyvalue[i ][0] = "Collate";
+ keyvalue[i++][1] = "T";
+ }
- if ((hptr = strchr(p->name, '@')) != NULL)
- len = hptr - p->name;
- else
- len = strlen(p->name);
+ if ((p->type & CUPS_PRINTER_PUNCH))
+ {
+ keyvalue[i ][0] = "Punch";
+ keyvalue[i++][1] = "T";
+ }
- strncpy(name, p->name, len);
- name[len] = '\0';
- offset = 0;
+ if ((p->type & CUPS_PRINTER_BIND))
+ {
+ keyvalue[i ][0] = "Bind";
+ keyvalue[i++][1] = "T";
+ }
- if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
- !(first->type & CUPS_PRINTER_IMPLICIT))
- {
- /*
- * Can't use same name as a local printer; add "Any" to the
- * front of the name, unless we have explicitly disabled
- * the "ImplicitAnyClasses"...
- */
+ if ((p->type & CUPS_PRINTER_SORT))
+ {
+ keyvalue[i ][0] = "Sort";
+ keyvalue[i++][1] = "T";
+ }
- if (ImplicitAnyClasses && len < (sizeof(name) - 4))
- {
- /*
- * Add "Any" to the class name...
- */
+ keyvalue[i ][0] = "pdl";
+ keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
- strcpy(name, "Any");
- strncpy(name + 3, p->name, len);
- name[len + 3] = '\0';
- offset = 3;
- }
- else
- {
- /*
- * Don't create an implicit class if we have a local printer
- * with the same name...
- */
+ if (p->num_auth_info_required)
+ {
+ char *air = air_str; /* Pointer into string */
- len = 0;
- cupsArrayRestore(Printers);
- continue;
- }
- }
- first = p;
+ for (j = 0; j < p->num_auth_info_required; j ++)
+ {
+ if (air >= (air_str + sizeof(air_str) - 2))
+ break;
+
+ if (j)
+ *air++ = ',';
+
+ strlcpy(air, p->auth_info_required[j], sizeof(air_str) - (air - air_str));
+ air += strlen(air);
}
- cupsArrayRestore(Printers);
+ keyvalue[i ][0] = "air";
+ keyvalue[i++][1] = air;
}
/*
- * Update the last printer class as needed...
+ * Then pack them into a proper txt record...
*/
- if (pclass && update)
- cupsdSetPrinterAttrs(pclass);
+ return (dnssdPackTxtRecord(txt_len, keyvalue, i));
}
/*
- * 'send_cups_browse()' - Send new browsing information using the CUPS
- * protocol.
+ * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
+ * printer.
*/
static void
-send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */
+dnssdDeregisterPrinter(
+ cupsd_printer_t *p) /* I - Printer */
{
- int i; /* Looping var */
- cups_ptype_t type; /* Printer type */
- cupsd_dirsvc_addr_t *b; /* Browse address */
- int bytes; /* Length of packet */
- char packet[1453], /* Browse data packet */
- uri[1024], /* Printer URI */
- location[1024], /* printer-location */
- info[1024], /* printer-info */
- make_model[1024];
- /* printer-make-and-model */
- cupsd_netif_t *iface; /* Network interface */
-
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
/*
- * Figure out the printer type value...
+ * Closing the socket deregisters the service
*/
- type = p->type | CUPS_PRINTER_REMOTE;
+ if (p->dnssd_ipp_ref)
+ {
+ cupsdRemoveSelect(p->dnssd_ipp_fd);
+ DNSServiceRefDeallocate(p->dnssd_ipp_ref);
+ p->dnssd_ipp_ref = NULL;
+ p->dnssd_ipp_fd = -1;
+ }
- if (!p->accepting)
- type |= CUPS_PRINTER_REJECTING;
+ cupsdClearString(&p->reg_name);
+
+ if (p->txt_record)
+ {
+ /*
+ * p->txt_record is malloc'd, not _cupsStrAlloc'd...
+ */
+
+ free(p->txt_record);
+ p->txt_record = NULL;
+ }
+}
+
+
+/*
+ * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
+ * TXT record format.
+ */
+
+static char * /* O - TXT record */
+dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */
+ char *keyvalue[][2], /* I - Table of key value pairs */
+ int count) /* I - Items in table */
+{
+ int i; /* Looping var */
+ int length; /* Length of TXT record */
+ int length2; /* Length of value */
+ char *txtRecord; /* TXT record buffer */
+ char *cursor; /* Looping pointer */
- if (p == DefaultPrinter)
- type |= CUPS_PRINTER_DEFAULT;
/*
- * Remove quotes from printer-info, printer-location, and
- * printer-make-and-model attributes...
+ * Calculate the buffer size
*/
- dequote(location, p->location, sizeof(location));
- dequote(info, p->info, sizeof(info));
-
- if (p->make_model)
- dequote(make_model, p->make_model, sizeof(make_model));
- else if (p->type & CUPS_PRINTER_CLASS)
- {
- if (p->num_printers > 0 && p->printers[0]->make_model)
- strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model));
- else
- strlcpy(make_model, "Local Printer Class", sizeof(make_model));
- }
- else if (p->raw)
- strlcpy(make_model, "Local Raw Printer", sizeof(make_model));
- else
- strlcpy(make_model, "Local System V Printer", sizeof(make_model));
+ for (length = i = 0; i < count; i++)
+ length += 1 + strlen(keyvalue[i][0]) +
+ (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
/*
- * Send a packet to each browse address...
+ * Allocate and fill it
*/
- for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
- if (b->iface[0])
+ txtRecord = malloc(length);
+ if (txtRecord)
+ {
+ *txt_len = length;
+
+ for (cursor = txtRecord, i = 0; i < count; i++)
{
/*
- * Send the browse packet to one or more interfaces...
+ * Drop in the p-string style length byte followed by the data
*/
- if (!strcmp(b->iface, "*"))
- {
- /*
- * Send to all local interfaces...
- */
+ length = strlen(keyvalue[i][0]);
+ length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0;
- cupsdNetIFUpdate();
+ *cursor++ = (unsigned char)(length + length2);
- for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
- iface;
- iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
- {
- /*
- * Only send to local, IPv4 interfaces...
- */
+ memcpy(cursor, keyvalue[i][0], length);
+ cursor += length;
- if (!iface->is_local || !iface->port ||
- iface->address.addr.sa_family != AF_INET)
- continue;
+ if (length2)
+ {
+ length2 --;
+ *cursor++ = '=';
+ memcpy(cursor, keyvalue[i][1], length2);
+ cursor += length2;
+ }
+ }
+ }
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- iface->hostname, iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
- "/printers/%s",
- p->name);
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
- type, p->state, uri, location, info, make_model,
- p->browse_attrs ? p->browse_attrs : "");
+ return (txtRecord);
+}
- bytes = strlen(packet);
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
- iface->name, packet);
+/*
+ * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
+ */
- iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+static void
+dnssdRegisterCallback(
+ DNSServiceRef sdRef, /* I - DNS Service reference */
+ DNSServiceFlags flags, /* I - Reserved for future use */
+ DNSServiceErrorType errorCode, /* I - Error code */
+ const char *name, /* I - Service name */
+ const char *regtype, /* I - Service type */
+ const char *domain, /* I - Domain. ".local" for now */
+ void *context) /* I - User-defined context */
+{
+ (void)context;
- sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(iface->broadcast),
- sizeof(struct sockaddr_in));
- }
- }
- else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
- {
- /*
- * Send to the named interface using the IPv4 address...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "dnssdRegisterCallback(%s, %s)", name, regtype);
- while (iface)
- if (strcmp(b->iface, iface->name))
- {
- iface = NULL;
- break;
- }
- else if (iface->address.addr.sa_family == AF_INET && iface->port)
- break;
- else
- iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
+ if (errorCode)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNSServiceRegister failed with error %d", (int)errorCode);
+ return;
+ }
+}
- if (iface)
- {
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- iface->hostname, iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
- "/printers/%s",
- p->name);
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
- type, p->state, uri, location, info, make_model,
- p->browse_attrs ? p->browse_attrs : "");
- bytes = strlen(packet);
+/*
+ * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
+ * or update the broadcast contents.
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
- iface->name, packet);
-
- iface->broadcast.ipv4.sin_port = htons(BrowsePort);
-
- sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(iface->broadcast),
- sizeof(struct sockaddr_in));
- }
- }
- }
- else
- {
- /*
- * Send the browse packet to the indicated address using
- * the default server name...
- */
-
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
- type, p->state, p->uri, location, info, make_model,
- p->browse_attrs ? p->browse_attrs : "");
+static void
+dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
+{
+ DNSServiceErrorType se; /* dnssd errors */
+ cupsd_listener_t *lis; /* Current listening socket */
+ char *txt_record, /* TXT record buffer */
+ *name; /* Service name */
+ int txt_len, /* TXT record length */
+ port; /* IPP port number */
+ char str_buffer[1024];
+ /* C-string buffer */
+ const char *computerName; /* Computer name c-string ptr */
+ const char *regtype; /* Registration type */
+#ifdef HAVE_COREFOUNDATION_H
+ CFStringRef computerNameRef;/* Computer name CFString */
+ CFStringEncoding nameEncoding; /* Computer name encoding */
+ CFMutableStringRef shortNameRef; /* Mutable name string */
+ CFIndex nameLength; /* Name string length */
+#else
+ int nameLength; /* Name string length */
+#endif /* HAVE_COREFOUNDATION_H */
- bytes = strlen(packet);
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
- if (sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(b->to),
- sizeof(struct sockaddr_in)) <= 0)
- {
- /*
- * Unable to send browse packet, so remove this address from the
- * list...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
+ !p->dnssd_ipp_ref ? "new" : "update");
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdSendBrowseList: sendto failed for browser "
- "%d - %s.",
- (int)(b - Browsers + 1), strerror(errno));
+ /*
+ * If per-printer sharing was just disabled make sure we're not
+ * registered before returning.
+ */
- if (i > 1)
- memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
+ if (!p->shared)
+ {
+ dnssdDeregisterPrinter(p);
+ return;
+ }
- b --;
- NumBrowsers --;
- }
- }
-}
+ /*
+ * Get the computer name as a c-string...
+ */
+#ifdef HAVE_COREFOUNDATION_H
+ computerName = NULL;
+ if ((computerNameRef = SCDynamicStoreCopyComputerName(NULL, &nameEncoding)))
+ if ((computerName = CFStringGetCStringPtr(computerNameRef,
+ kCFStringEncodingUTF8)) == NULL)
+ if (CFStringGetCString(computerNameRef, str_buffer, sizeof(str_buffer),
+ kCFStringEncodingUTF8))
+ computerName = str_buffer;
+#else
+ computerName = ServerName;
+#endif /* HAVE_COREFOUNDATION_H */
-#ifdef HAVE_OPENLDAP
-/*
- * 'send_ldap_browse()' - Send LDAP printer registrations.
- */
+ /*
+ * The registered name takes the form of "<printer-info> @ <computer name>"...
+ */
-static void
-send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */
-{
- int i; /* Looping var... */
- LDAPMod mods[7]; /* The 7 attributes we will be adding */
- LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */
- LDAPMessage *res; /* Search result token */
- char *cn_value[2], /* Change records */
- *uri[2],
- *info[2],
- *location[2],
- *make_model[2],
- *type[2],
- typestring[255], /* String to hold printer-type */
- filter[256], /* Search filter for possible UPDATEs */
- dn[1024]; /* DN of the printer we are adding */
- int rc; /* LDAP status */
- static const char * const objectClass_values[] =
- { /* The 3 objectClass's we use in */
- "top", /* our LDAP entries */
- "device",
- "cupsPrinter",
- NULL
- };
+ name = NULL;
+ if (computerName)
+ cupsdSetStringf(&name, "%s @ %s",
+ (p->info && strlen(p->info)) ? p->info : p->name,
+ computerName);
+ else
+ cupsdSetString(&name, (p->info && strlen(p->info)) ? p->info : p->name);
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s\n", p->name);
+#ifdef HAVE_COREFOUNDATION_H
+ if (computerNameRef)
+ CFRelease(computerNameRef);
+#endif /* HAVE_COREFOUNDATION_H */
/*
- * Everything in ldap is ** so we fudge around it...
+ * If an existing printer was renamed, unregister it and start over...
*/
- sprintf(typestring, "%u", p->type);
+ if (p->reg_name && strcmp(p->reg_name, name))
+ dnssdDeregisterPrinter(p);
- cn_value[0] = p->info;
- cn_value[1] = NULL;
- info[0] = p->info;
- info[1] = NULL;
- location[0] = p->location;
- location[1] = NULL;
- make_model[0] = p->make_model;
- make_model[1] = NULL;
- type[0] = typestring;
- type[1] = NULL;
- uri[0] = p->uri;
- uri[1] = NULL;
+ txt_len = 0; /* anti-compiler-warning-code */
+ txt_record = dnssdBuildTxtRecord(&txt_len, p);
- snprintf(filter, sizeof(filter),
- "(&(objectclass=cupsPrinter)(printerDescription~=%s))", p->info);
+ if (!p->dnssd_ipp_ref)
+ {
+ /*
+ * Initial registration...
+ */
- ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
- filter, (char **)ldap_attrs, 0, &res);
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: Searching \"%s\"",
- filter);
+ cupsdSetString(&p->reg_name, name);
- mods[0].mod_type = "cn";
- mods[0].mod_values = cn_value;
- mods[1].mod_type = "printerDescription";
- mods[1].mod_values = info;
- mods[2].mod_type = "printerURI";
- mods[2].mod_values = uri;
- mods[3].mod_type = "printerLocation";
- mods[3].mod_values = location;
- mods[4].mod_type = "printerMakeAndModel";
- mods[4].mod_values = make_model;
- mods[5].mod_type = "printerType";
- mods[5].mod_values = type;
- mods[6].mod_type = "objectClass";
- mods[6].mod_values = (char **)objectClass_values;
+ port = ippPort();
- snprintf(dn, sizeof(dn), "cn=%s,ou=printers,%s", p->info, BrowseLDAPDN);
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn);
+ for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
+ lis;
+ lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
+ {
+ if (lis->address.addr.sa_family == AF_INET)
+ {
+ port = ntohs(lis->address.ipv4.sin_port);
+ break;
+ }
+ else if (lis->address.addr.sa_family == AF_INET6)
+ {
+ port = ntohs(lis->address.ipv6.sin6_port);
+ break;
+ }
+ }
- if (ldap_count_entries(BrowseLDAPHandle, res) > 0)
- {
/*
- * Printer has already been registered, modify the current
- * registration...
+ * Use the _fax subtype for fax queues...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "send_ldap_browse: Replacing entry...");
+ regtype = (p->type & CUPS_PRINTER_FAX) ? dnssdIPPFaxRegType :
+ dnssdIPPRegType;
- for (i = 0; i < 7; i ++)
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) type is \"%s\"",
+ p->name, regtype);
+
+ se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, name, regtype,
+ NULL, NULL, htons(port), txt_len, txt_record,
+ dnssdRegisterCallback, p);
+
+ /*
+ * In case the name is too long, try shortening the string one character
+ * at a time...
+ */
+
+ if (se == kDNSServiceErr_BadParam)
{
- pmods[i] = mods + i;
- pmods[i]->mod_op = LDAP_MOD_REPLACE;
+#ifdef HAVE_COREFOUNDATION_H
+ if ((shortNameRef = CFStringCreateMutable(NULL, 0)) != NULL)
+ {
+ CFStringAppendCString(shortNameRef, name, kCFStringEncodingUTF8);
+ nameLength = CFStringGetLength(shortNameRef);
+
+ while (se == kDNSServiceErr_BadParam && nameLength > 1)
+ {
+ CFStringDelete(shortNameRef, CFRangeMake(--nameLength, 1));
+ if (CFStringGetCString(shortNameRef, str_buffer, sizeof(str_buffer),
+ kCFStringEncodingUTF8))
+ {
+ se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, str_buffer,
+ regtype, NULL, NULL, htons(port),
+ txt_len, txt_record,
+ dnssdRegisterCallback, p);
+ }
+ }
+
+ CFRelease(shortNameRef);
+ }
+#else
+ nameLength = strlen(name);
+ while (se == kDNSServiceErr_BadParam && nameLength > 1)
+ {
+ name[--nameLength] = '\0';
+ se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, str_buffer, regtype,
+ NULL, NULL, htons(port), txt_len, txt_record,
+ dnssdRegisterCallback, p);
+ }
+#endif /* HAVE_COREFOUNDATION_H */
}
- pmods[i] = NULL;
- if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "LDAP modify for %s failed with status %d: %s",
- p->name, rc, ldap_err2string(rc));
+ if (se == kDNSServiceErr_NoError)
+ {
+ p->dnssd_ipp_fd = DNSServiceRefSockFD(p->dnssd_ipp_ref);
+ p->txt_record = txt_record;
+ p->txt_len = txt_len;
+ txt_record = NULL;
+
+ cupsdAddSelect(p->dnssd_ipp_fd, (cupsd_selfunc_t)cupsdUpdateDNSSDBrowse,
+ NULL, (void *)p);
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "DNS-SD registration of \"%s\" failed with %d",
+ p->name, se);
}
- else
+ else if (txt_len != p->txt_len || memcmp(txt_record, p->txt_record, txt_len))
{
/*
- * Printer has already been registered, modify the current
- * registration...
+ * Update the existing registration...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "send_ldap_browse: Adding entry...");
+ /* A TTL of 0 means use record's original value (Radar 3176248) */
+ se = DNSServiceUpdateRecord(p->dnssd_ipp_ref, NULL, 0,
+ txt_len, txt_record, 0);
- for (i = 0; i < 7; i ++)
- {
- pmods[i] = mods + i;
- pmods[i]->mod_op = LDAP_MOD_REPLACE;
- }
- pmods[i] = NULL;
+ if (p->txt_record)
+ free(p->txt_record);
- if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "LDAP add for %s failed with status %d: %s",
- p->name, rc, ldap_err2string(rc));
+ p->txt_record = txt_record;
+ p->txt_len = txt_len;
+ txt_record = NULL;
}
+
+ if (txt_record)
+ free(txt_record);
+
+ cupsdClearString(&name);
}
-#endif /* HAVE_OPENLDAP */
+#endif /* HAVE_DNSSD */
-#ifdef HAVE_LIBSLP
/*
- * 'send_slp_browse()' - Register the specified printer with SLP.
+ * 'process_implicit_classes()' - Create/update implicit classes as needed.
*/
static void
-send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */
+process_implicit_classes(
+ int *write_printcap) /* O - Write printcap file? */
{
- char srvurl[HTTP_MAX_URI], /* Printer service URI */
- attrs[8192], /* Printer attributes */
- finishings[1024], /* Finishings to support */
- make_model[IPP_MAX_NAME * 2],
- /* Make and model, quoted */
- location[IPP_MAX_NAME * 2],
- /* Location, quoted */
- info[IPP_MAX_NAME * 2], /* Info, quoted */
- *src, /* Pointer to original string */
- *dst; /* Pointer to destination string */
- ipp_attribute_t *authentication; /* uri-authentication-supported value */
- SLPError error; /* SLP error, if any */
+ int i; /* Looping var */
+ int update; /* Update printer attributes? */
+ char name[IPP_MAX_NAME], /* Name of printer */
+ *hptr; /* Pointer into hostname */
+ cupsd_printer_t *p, /* Printer information */
+ *pclass, /* Printer class */
+ *first; /* First printer in class */
+ int offset, /* Offset of name */
+ len; /* Length of name */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p,
- p->name);
+ if (!ImplicitClasses || !Printers)
+ return;
/*
- * Make the SLP service URL that conforms to the IANA
- * 'printer:' template.
+ * Loop through all available printers and create classes as needed...
*/
- snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0,
+ update = 0, pclass = NULL, first = NULL;
+ p != NULL;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ {
+ /*
+ * Skip implicit classes...
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
+ if (p->type & CUPS_PRINTER_IMPLICIT)
+ {
+ len = 0;
+ continue;
+ }
- /*
- * Figure out the finishings string...
- */
+ /*
+ * If len == 0, get the length of this printer name up to the "@"
+ * sign (if any).
+ */
- if (p->type & CUPS_PRINTER_STAPLE)
- strcpy(finishings, "staple");
- else
- finishings[0] = '\0';
+ cupsArraySave(Printers);
- if (p->type & CUPS_PRINTER_BIND)
- {
- if (finishings[0])
- strlcat(finishings, ",bind", sizeof(finishings));
- else
- strcpy(finishings, "bind");
- }
+ if (len > 0 &&
+ !strncasecmp(p->name, name + offset, len) &&
+ (p->name[len] == '\0' || p->name[len] == '@'))
+ {
+ /*
+ * We have more than one printer with the same name; see if
+ * we have a class, and if this printer is a member...
+ */
- if (p->type & CUPS_PRINTER_PUNCH)
- {
- if (finishings[0])
- strlcat(finishings, ",punch", sizeof(finishings));
+ if (pclass && strcasecmp(pclass->name, name))
+ {
+ if (update)
+ cupsdSetPrinterAttrs(pclass);
+
+ update = 0;
+ pclass = NULL;
+ }
+
+ if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
+ {
+ /*
+ * Need to add the class...
+ */
+
+ pclass = cupsdAddPrinter(name);
+ cupsArrayAdd(ImplicitPrinters, pclass);
+
+ pclass->type |= CUPS_PRINTER_IMPLICIT;
+ pclass->accepting = 1;
+ pclass->state = IPP_PRINTER_IDLE;
+
+ cupsdSetString(&pclass->location, p->location);
+ cupsdSetString(&pclass->info, p->info);
+
+ cupsdSetString(&pclass->job_sheets[0], p->job_sheets[0]);
+ cupsdSetString(&pclass->job_sheets[1], p->job_sheets[1]);
+
+ update = 1;
+
+ if (write_printcap)
+ *write_printcap = 1;
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...",
+ name);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Implicit class \'%s\' added by directory services.",
+ name);
+ }
+
+ if (first != NULL)
+ {
+ for (i = 0; i < pclass->num_printers; i ++)
+ if (pclass->printers[i] == first)
+ break;
+
+ if (i >= pclass->num_printers)
+ {
+ first->in_implicit_class = 1;
+ cupsdAddPrinterToClass(pclass, first);
+ }
+
+ first = NULL;
+ }
+
+ for (i = 0; i < pclass->num_printers; i ++)
+ if (pclass->printers[i] == p)
+ break;
+
+ if (i >= pclass->num_printers)
+ {
+ p->in_implicit_class = 1;
+ cupsdAddPrinterToClass(pclass, p);
+ update = 1;
+ }
+ }
else
- strcpy(finishings, "punch");
+ {
+ /*
+ * First time around; just get name length and mark it as first
+ * in the list...
+ */
+
+ if ((hptr = strchr(p->name, '@')) != NULL)
+ len = hptr - p->name;
+ else
+ len = strlen(p->name);
+
+ strncpy(name, p->name, len);
+ name[len] = '\0';
+ offset = 0;
+
+ if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
+ !(first->type & CUPS_PRINTER_IMPLICIT))
+ {
+ /*
+ * Can't use same name as a local printer; add "Any" to the
+ * front of the name, unless we have explicitly disabled
+ * the "ImplicitAnyClasses"...
+ */
+
+ if (ImplicitAnyClasses && len < (sizeof(name) - 4))
+ {
+ /*
+ * Add "Any" to the class name...
+ */
+
+ strcpy(name, "Any");
+ strncpy(name + 3, p->name, len);
+ name[len + 3] = '\0';
+ offset = 3;
+ }
+ else
+ {
+ /*
+ * Don't create an implicit class if we have a local printer
+ * with the same name...
+ */
+
+ len = 0;
+ cupsArrayRestore(Printers);
+ continue;
+ }
+ }
+
+ first = p;
+ }
+
+ cupsArrayRestore(Printers);
+ }
+
+ /*
+ * Update the last printer class as needed...
+ */
+
+ if (pclass && update)
+ cupsdSetPrinterAttrs(pclass);
+}
+
+
+/*
+ * 'send_cups_browse()' - Send new browsing information using the CUPS
+ * protocol.
+ */
+
+static void
+send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */
+{
+ int i; /* Looping var */
+ cups_ptype_t type; /* Printer type */
+ cupsd_dirsvc_addr_t *b; /* Browse address */
+ int bytes; /* Length of packet */
+ char packet[1453], /* Browse data packet */
+ uri[1024], /* Printer URI */
+ location[1024], /* printer-location */
+ info[1024], /* printer-info */
+ make_model[1024];
+ /* printer-make-and-model */
+ cupsd_netif_t *iface; /* Network interface */
+
+
+ /*
+ * Figure out the printer type value...
+ */
+
+ type = p->type | CUPS_PRINTER_REMOTE;
+
+ if (!p->accepting)
+ type |= CUPS_PRINTER_REJECTING;
+
+ if (p == DefaultPrinter)
+ type |= CUPS_PRINTER_DEFAULT;
+
+ /*
+ * Remove quotes from printer-info, printer-location, and
+ * printer-make-and-model attributes...
+ */
+
+ dequote(location, p->location, sizeof(location));
+ dequote(info, p->info, sizeof(info));
+
+ if (p->make_model)
+ dequote(make_model, p->make_model, sizeof(make_model));
+ else if (p->type & CUPS_PRINTER_CLASS)
+ {
+ if (p->num_printers > 0 && p->printers[0]->make_model)
+ strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model));
+ else
+ strlcpy(make_model, "Local Printer Class", sizeof(make_model));
}
+ else if (p->raw)
+ strlcpy(make_model, "Local Raw Printer", sizeof(make_model));
+ else
+ strlcpy(make_model, "Local System V Printer", sizeof(make_model));
+
+ /*
+ * Send a packet to each browse address...
+ */
+
+ for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
+ if (b->iface[0])
+ {
+ /*
+ * Send the browse packet to one or more interfaces...
+ */
+
+ if (!strcmp(b->iface, "*"))
+ {
+ /*
+ * Send to all local interfaces...
+ */
+
+ cupsdNetIFUpdate();
+
+ for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
+ iface;
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
+ {
+ /*
+ * Only send to local, IPv4 interfaces...
+ */
+
+ if (!iface->is_local || !iface->port ||
+ iface->address.addr.sa_family != AF_INET)
+ continue;
+
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ iface->hostname, iface->port,
+ (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
+ "/printers/%s",
+ p->name);
+ snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
+ type, p->state, uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "");
+
+ bytes = strlen(packet);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
+ iface->name, packet);
+
+ iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+
+ sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(iface->broadcast),
+ httpAddrLength(&(iface->broadcast)));
+ }
+ }
+ else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
+ {
+ /*
+ * Send to the named interface using the IPv4 address...
+ */
+
+ while (iface)
+ if (strcmp(b->iface, iface->name))
+ {
+ iface = NULL;
+ break;
+ }
+ else if (iface->address.addr.sa_family == AF_INET && iface->port)
+ break;
+ else
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
+
+ if (iface)
+ {
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ iface->hostname, iface->port,
+ (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
+ "/printers/%s",
+ p->name);
+ snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
+ type, p->state, uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "");
+
+ bytes = strlen(packet);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
+ iface->name, packet);
+
+ iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+
+ sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(iface->broadcast),
+ httpAddrLength(&(iface->broadcast)));
+ }
+ }
+ }
+ else
+ {
+ /*
+ * Send the browse packet to the indicated address using
+ * the default server name...
+ */
+
+ snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
+ type, p->state, p->uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "");
+
+ bytes = strlen(packet);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
+
+ if (sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(b->to),
+ httpAddrLength(&(b->to))) <= 0)
+ {
+ /*
+ * Unable to send browse packet, so remove this address from the
+ * list...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "cupsdSendBrowseList: sendto failed for browser "
+ "%d - %s.",
+ (int)(b - Browsers + 1), strerror(errno));
+
+ if (i > 1)
+ memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
+
+ b --;
+ NumBrowsers --;
+ }
+ }
+}
+
+
+#ifdef HAVE_OPENLDAP
+/*
+ * 'send_ldap_browse()' - Send LDAP printer registrations.
+ */
+
+static void
+send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */
+{
+ int i; /* Looping var... */
+ LDAPMod mods[7]; /* The 7 attributes we will be adding */
+ LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */
+ LDAPMessage *res; /* Search result token */
+ char *cn_value[2], /* Change records */
+ *uri[2],
+ *info[2],
+ *location[2],
+ *make_model[2],
+ *type[2],
+ typestring[255], /* String to hold printer-type */
+ filter[256], /* Search filter for possible UPDATEs */
+ dn[1024]; /* DN of the printer we are adding */
+ int rc; /* LDAP status */
+ static const char * const objectClass_values[] =
+ { /* The 3 objectClass's we use in */
+ "top", /* our LDAP entries */
+ "device",
+ "cupsPrinter",
+ NULL
+ };
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s\n", p->name);
+
+ /*
+ * Everything in ldap is ** so we fudge around it...
+ */
+
+ sprintf(typestring, "%u", p->type);
+
+ cn_value[0] = p->name;
+ cn_value[1] = NULL;
+ info[0] = p->info ? p->info : "Unknown";
+ info[1] = NULL;
+ location[0] = p->location ? p->location : "Unknown";
+ location[1] = NULL;
+ make_model[0] = p->make_model ? p->make_model : "Unknown";
+ make_model[1] = NULL;
+ type[0] = typestring;
+ type[1] = NULL;
+ uri[0] = p->uri;
+ uri[1] = NULL;
+
+ snprintf(filter, sizeof(filter),
+ "(&(objectclass=cupsPrinter)(printerURI=%s))", p->uri);
+
+ ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
+ filter, (char **)ldap_attrs, 0, &res);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: Searching \"%s\"",
+ filter);
+
+ mods[0].mod_type = "cn";
+ mods[0].mod_values = cn_value;
+ mods[1].mod_type = "printerDescription";
+ mods[1].mod_values = info;
+ mods[2].mod_type = "printerURI";
+ mods[2].mod_values = uri;
+ mods[3].mod_type = "printerLocation";
+ mods[3].mod_values = location;
+ mods[4].mod_type = "printerMakeAndModel";
+ mods[4].mod_values = make_model;
+ mods[5].mod_type = "printerType";
+ mods[5].mod_values = type;
+ mods[6].mod_type = "objectClass";
+ mods[6].mod_values = (char **)objectClass_values;
+
+ snprintf(dn, sizeof(dn), "cn=%s,ou=printers,%s", p->name, BrowseLDAPDN);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn);
+
+ if (ldap_count_entries(BrowseLDAPHandle, res) > 0)
+ {
+ /*
+ * Printer has already been registered, modify the current
+ * registration...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: Replacing entry...");
+
+ for (i = 0; i < 7; i ++)
+ {
+ pmods[i] = mods + i;
+ pmods[i]->mod_op = LDAP_MOD_REPLACE;
+ }
+ pmods[i] = NULL;
+
+ if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP modify for %s failed with status %d: %s",
+ p->name, rc, ldap_err2string(rc));
+ }
+ else
+ {
+ /*
+ * Printer has never been registered, add the current
+ * registration...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: Adding entry...");
+
+ for (i = 0; i < 7; i ++)
+ {
+ pmods[i] = mods + i;
+ pmods[i]->mod_op = LDAP_MOD_ADD;
+ }
+ pmods[i] = NULL;
+
+ if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP add for %s failed with status %d: %s",
+ p->name, rc, ldap_err2string(rc));
+ }
+}
+#endif /* HAVE_OPENLDAP */
+
+
+#ifdef HAVE_LIBSLP
+/*
+ * 'send_slp_browse()' - Register the specified printer with SLP.
+ */
+
+static void
+send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */
+{
+ char srvurl[HTTP_MAX_URI], /* Printer service URI */
+ attrs[8192], /* Printer attributes */
+ finishings[1024], /* Finishings to support */
+ make_model[IPP_MAX_NAME * 2],
+ /* Make and model, quoted */
+ location[IPP_MAX_NAME * 2],
+ /* Location, quoted */
+ info[IPP_MAX_NAME * 2], /* Info, quoted */
+ *src, /* Pointer to original string */
+ *dst; /* Pointer to destination string */
+ ipp_attribute_t *authentication; /* uri-authentication-supported value */
+ SLPError error; /* SLP error, if any */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p,
+ p->name);
+
+ /*
+ * Make the SLP service URL that conforms to the IANA
+ * 'printer:' template.
+ */
+
+ snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
+
+ /*
+ * Figure out the finishings string...
+ */
+
+ if (p->type & CUPS_PRINTER_STAPLE)
+ strcpy(finishings, "staple");
+ else
+ finishings[0] = '\0';
+
+ if (p->type & CUPS_PRINTER_BIND)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",bind", sizeof(finishings));
+ else
+ strcpy(finishings, "bind");
+ }
+
+ if (p->type & CUPS_PRINTER_PUNCH)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",punch", sizeof(finishings));
+ else
+ strcpy(finishings, "punch");
+ }
+
+ if (p->type & CUPS_PRINTER_COVER)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",cover", sizeof(finishings));
+ else
+ strcpy(finishings, "cover");
+ }
+
+ if (p->type & CUPS_PRINTER_SORT)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",sort", sizeof(finishings));
+ else
+ strcpy(finishings, "sort");
+ }
+
+ if (!finishings[0])
+ strcpy(finishings, "none");
+
+ /*
+ * Quote any commas in the make and model, location, and info strings...
+ */
+
+ for (src = p->make_model, dst = make_model;
+ src && *src && dst < (make_model + sizeof(make_model) - 2);)
+ {
+ if (*src == ',' || *src == '\\' || *src == ')')
+ *dst++ = '\\';
+
+ *dst++ = *src++;
+ }
+
+ *dst = '\0';
+
+ if (!make_model[0])
+ strcpy(make_model, "Unknown");
+
+ for (src = p->location, dst = location;
+ src && *src && dst < (location + sizeof(location) - 2);)
+ {
+ if (*src == ',' || *src == '\\' || *src == ')')
+ *dst++ = '\\';
+
+ *dst++ = *src++;
+ }
+
+ *dst = '\0';
+
+ if (!location[0])
+ strcpy(location, "Unknown");
+
+ for (src = p->info, dst = info;
+ src && *src && dst < (info + sizeof(info) - 2);)
+ {
+ if (*src == ',' || *src == '\\' || *src == ')')
+ *dst++ = '\\';
+
+ *dst++ = *src++;
+ }
+
+ *dst = '\0';
+
+ if (!info[0])
+ strcpy(info, "Unknown");
+
+ /*
+ * Get the authentication value...
+ */
+
+ authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
+ IPP_TAG_KEYWORD);
+
+ /*
+ * Make the SLP attribute string list that conforms to
+ * the IANA 'printer:' template.
+ */
+
+ snprintf(attrs, sizeof(attrs),
+ "(printer-uri-supported=%s),"
+ "(uri-authentication-supported=%s>),"
+#ifdef HAVE_SSL
+ "(uri-security-supported=tls>),"
+#else
+ "(uri-security-supported=none>),"
+#endif /* HAVE_SSL */
+ "(printer-name=%s),"
+ "(printer-location=%s),"
+ "(printer-info=%s),"
+ "(printer-more-info=%s),"
+ "(printer-make-and-model=%s),"
+ "(printer-type=%d),"
+ "(charset-supported=utf-8),"
+ "(natural-language-configured=%s),"
+ "(natural-language-supported=de,en,es,fr,it),"
+ "(color-supported=%s),"
+ "(finishings-supported=%s),"
+ "(sides-supported=one-sided%s),"
+ "(multiple-document-jobs-supported=true)"
+ "(ipp-versions-supported=1.0,1.1)",
+ p->uri, authentication->values[0].string.text, p->name, location,
+ info, p->uri, make_model, p->type, DefaultLanguage,
+ p->type & CUPS_PRINTER_COLOR ? "true" : "false",
+ finishings,
+ p->type & CUPS_PRINTER_DUPLEX ?
+ ",two-sided-long-edge,two-sided-short-edge" : "");
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
+
+ /*
+ * Register the printer with the SLP server...
+ */
+
+ error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
+ SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
+
+ if (error != SLP_OK)
+ cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
+ error);
+}
+
+
+/*
+ * 'slp_attr_callback()' - SLP attribute callback
+ */
+
+static SLPBoolean /* O - SLP_TRUE for success */
+slp_attr_callback(
+ SLPHandle hslp, /* I - SLP handle */
+ const char *attrlist, /* I - Attribute list */
+ SLPError errcode, /* I - Parsing status for this attr */
+ void *cookie) /* I - Current printer */
+{
+ char *tmp = 0; /* Temporary string */
+ cupsd_printer_t *p = (cupsd_printer_t*)cookie;
+ /* Current printer */
+
+
+ (void)hslp; /* anti-compiler-warning-code */
+
+ /*
+ * Bail if there was an error
+ */
+
+ if (errcode != SLP_OK)
+ return (SLP_TRUE);
+
+ /*
+ * Parse the attrlist to obtain things needed to build CUPS browse packet
+ */
+
+ memset(p, 0, sizeof(cupsd_printer_t));
+
+ if (slp_get_attr(attrlist, "(printer-location=", &(p->location)))
+ return (SLP_FALSE);
+ if (slp_get_attr(attrlist, "(printer-info=", &(p->info)))
+ return (SLP_FALSE);
+ if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model)))
+ return (SLP_FALSE);
+ if (!slp_get_attr(attrlist, "(printer-type=", &tmp))
+ p->type = atoi(tmp);
+ else
+ p->type = CUPS_PRINTER_REMOTE;
+
+ cupsdClearString(&tmp);
+
+ return (SLP_TRUE);
+}
+
+
+/*
+ * 'slp_dereg_printer()' - SLPDereg() the specified printer
+ */
+
+static void
+slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */
+{
+ char srvurl[HTTP_MAX_URI]; /* Printer service URI */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
+
+ if (!(p->type & CUPS_PRINTER_REMOTE))
+ {
+ /*
+ * Make the SLP service URL that conforms to the IANA
+ * 'printer:' template.
+ */
+
+ snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
+
+ /*
+ * Deregister the printer...
+ */
+
+ SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
+ }
+}
+
+
+/*
+ * 'slp_get_attr()' - Get an attribute from an SLP registration.
+ */
+
+static int /* O - 0 on success */
+slp_get_attr(const char *attrlist, /* I - Attribute list string */
+ const char *tag, /* I - Name of attribute */
+ char **valbuf) /* O - Value */
+{
+ char *ptr1, /* Pointer into string */
+ *ptr2; /* ... */
+
+
+ cupsdClearString(valbuf);
+
+ if ((ptr1 = strstr(attrlist, tag)) != NULL)
+ {
+ ptr1 += strlen(tag);
+
+ if ((ptr2 = strchr(ptr1,')')) != NULL)
+ {
+ /*
+ * Copy the value...
+ */
+
+ *valbuf = calloc(ptr2 - ptr1 + 1, 1);
+ strncpy(*valbuf, ptr1, ptr2 - ptr1);
+
+ /*
+ * Dequote the value...
+ */
+
+ for (ptr1 = *valbuf; *ptr1; ptr1 ++)
+ if (*ptr1 == '\\' && ptr1[1])
+ _cups_strcpy(ptr1, ptr1 + 1);
+
+ return (0);
+ }
+ }
+
+ return (-1);
+}
+
+
+/*
+ * 'slp_reg_callback()' - Empty SLPRegReport.
+ */
+
+static void
+slp_reg_callback(SLPHandle hslp, /* I - SLP handle */
+ SLPError errcode, /* I - Error code, if any */
+ void *cookie) /* I - App data */
+{
+ (void)hslp;
+ (void)errcode;
+ (void)cookie;
+
+ return;
+}
+
+
+/*
+ * 'slp_url_callback()' - SLP service url callback
+ */
+
+static SLPBoolean /* O - TRUE = OK, FALSE = error */
+slp_url_callback(
+ SLPHandle hslp, /* I - SLP handle */
+ const char *srvurl, /* I - URL of service */
+ unsigned short lifetime, /* I - Life of service */
+ SLPError errcode, /* I - Existing error code */
+ void *cookie) /* I - Pointer to service list */
+{
+ slpsrvurl_t *s, /* New service entry */
+ **head; /* Pointer to head of entry */
+
+
+ /*
+ * Let the compiler know we won't be using these vars...
+ */
+
+ (void)hslp;
+ (void)lifetime;
+
+ /*
+ * Bail if there was an error
+ */
+
+ if (errcode != SLP_OK)
+ return (SLP_TRUE);
+
+ /*
+ * Grab the head of the list...
+ */
+
+ head = (slpsrvurl_t**)cookie;
+
+ /*
+ * Allocate a *temporary* slpsrvurl_t to hold this entry.
+ */
- if (p->type & CUPS_PRINTER_COVER)
- {
- if (finishings[0])
- strlcat(finishings, ",cover", sizeof(finishings));
- else
- strcpy(finishings, "cover");
- }
+ if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
+ return (SLP_FALSE);
- if (p->type & CUPS_PRINTER_SORT)
- {
- if (finishings[0])
- strlcat(finishings, ",sort", sizeof(finishings));
- else
- strcpy(finishings, "sort");
- }
+ /*
+ * Copy the SLP service URL...
+ */
+
+ strlcpy(s->url, srvurl, sizeof(s->url));
+
+ /*
+ * Link the SLP service URL into the head of the list
+ */
+
+ if (*head)
+ s->next = *head;
+
+ *head = s;
+
+ return (SLP_TRUE);
+}
+#endif /* HAVE_LIBSLP */
+
+
+/*
+ * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
+ */
+
+static void
+update_cups_browse(void)
+{
+ int i; /* Looping var */
+ int auth; /* Authorization status */
+ int len; /* Length of name string */
+ int bytes; /* Number of bytes left */
+ char packet[1541], /* Broadcast packet */
+ *pptr; /* Pointer into packet */
+ socklen_t srclen; /* Length of source address */
+ http_addr_t srcaddr; /* Source address */
+ char srcname[1024]; /* Source hostname */
+ unsigned address[4]; /* Source address */
+ unsigned type; /* Printer type */
+ unsigned state; /* Printer state */
+ char uri[HTTP_MAX_URI], /* Printer URI */
+ host[HTTP_MAX_URI], /* Host portion of URI */
+ resource[HTTP_MAX_URI], /* Resource portion of URI */
+ info[IPP_MAX_NAME], /* Information string */
+ location[IPP_MAX_NAME], /* Location string */
+ make_model[IPP_MAX_NAME];/* Make and model string */
+ int num_attrs; /* Number of attributes */
+ cups_option_t *attrs; /* Attributes */
- if (!finishings[0])
- strcpy(finishings, "none");
/*
- * Quote any commas in the make and model, location, and info strings...
+ * Read a packet from the browse socket...
*/
- for (src = p->make_model, dst = make_model;
- src && *src && dst < (make_model + sizeof(make_model) - 2);)
+ srclen = sizeof(srcaddr);
+ if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
+ (struct sockaddr *)&srcaddr, &srclen)) < 0)
{
- if (*src == ',' || *src == '\\' || *src == ')')
- *dst++ = '\\';
+ /*
+ * "Connection refused" is returned under Linux if the destination port
+ * or address is unreachable from a previous sendto(); check for the
+ * error here and ignore it for now...
+ */
- *dst++ = *src++;
+ if (errno != ECONNREFUSED && errno != EAGAIN)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
+ strerror(errno));
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
+
+ cupsdStopBrowsing();
+ Browsing = 0;
+ }
+
+ return;
}
- *dst = '\0';
+ packet[bytes] = '\0';
- if (!make_model[0])
- strcpy(make_model, "Unknown");
+ /*
+ * If we're about to sleep, ignore incoming browse packets.
+ */
- for (src = p->location, dst = location;
- src && *src && dst < (location + sizeof(location) - 2);)
- {
- if (*src == ',' || *src == '\\' || *src == ')')
- *dst++ = '\\';
+ if (Sleeping)
+ return;
- *dst++ = *src++;
+ /*
+ * Figure out where it came from...
+ */
+
+#ifdef AF_INET6
+ if (srcaddr.addr.sa_family == AF_INET6)
+ {
+ address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
+ address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
+ address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
+ address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
+ }
+ else
+#endif /* AF_INET6 */
+ {
+ address[0] = 0;
+ address[1] = 0;
+ address[2] = 0;
+ address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
}
- *dst = '\0';
+ if (HostNameLookups)
+ httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
+ else
+ httpAddrString(&srcaddr, srcname, sizeof(srcname));
- if (!location[0])
- strcpy(location, "Unknown");
+ len = strlen(srcname);
- for (src = p->info, dst = info;
- src && *src && dst < (info + sizeof(info) - 2);)
+ /*
+ * Do ACL stuff...
+ */
+
+ if (BrowseACL)
{
- if (*src == ',' || *src == '\\' || *src == ')')
- *dst++ = '\\';
+ if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
+ {
+ /*
+ * Access from localhost (127.0.0.1) is always allowed...
+ */
- *dst++ = *src++;
+ auth = AUTH_ALLOW;
+ }
+ else
+ {
+ /*
+ * Do authorization checks on the domain/address...
+ */
+
+ switch (BrowseACL->order_type)
+ {
+ default :
+ auth = AUTH_DENY; /* anti-compiler-warning-code */
+ break;
+
+ case AUTH_ALLOW : /* Order Deny,Allow */
+ auth = AUTH_ALLOW;
+
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_deny, BrowseACL->deny))
+ auth = AUTH_DENY;
+
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_allow, BrowseACL->allow))
+ auth = AUTH_ALLOW;
+ break;
+
+ case AUTH_DENY : /* Order Allow,Deny */
+ auth = AUTH_DENY;
+
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_allow, BrowseACL->allow))
+ auth = AUTH_ALLOW;
+
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_deny, BrowseACL->deny))
+ auth = AUTH_DENY;
+ break;
+ }
+ }
}
+ else
+ auth = AUTH_ALLOW;
- *dst = '\0';
+ if (auth == AUTH_DENY)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "update_cups_browse: Refused %d bytes from %s", bytes,
+ srcname);
+ return;
+ }
- if (!info[0])
- strcpy(info, "Unknown");
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "update_cups_browse: (%d bytes from %s) %s", bytes,
+ srcname, packet);
/*
- * Get the authentication value...
+ * Parse packet...
*/
- authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
- IPP_TAG_KEYWORD);
+ if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
+ {
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "update_cups_browse: Garbled browse packet - %s", packet);
+ return;
+ }
- /*
- * Make the SLP attribute string list that conforms to
- * the IANA 'printer:' template.
- */
+ strcpy(location, "Location Unknown");
+ strcpy(info, "No Information Available");
+ make_model[0] = '\0';
+ num_attrs = 0;
+ attrs = NULL;
- snprintf(attrs, sizeof(attrs),
- "(printer-uri-supported=%s),"
- "(uri-authentication-supported=%s>),"
-#ifdef HAVE_SSL
- "(uri-security-supported=tls>),"
-#else
- "(uri-security-supported=none>),"
-#endif /* HAVE_SSL */
- "(printer-name=%s),"
- "(printer-location=%s),"
- "(printer-info=%s),"
- "(printer-more-info=%s),"
- "(printer-make-and-model=%s),"
- "(printer-type=%d),"
- "(charset-supported=utf-8),"
- "(natural-language-configured=%s),"
- "(natural-language-supported=de,en,es,fr,it),"
- "(color-supported=%s),"
- "(finishings-supported=%s),"
- "(sides-supported=one-sided%s),"
- "(multiple-document-jobs-supported=true)"
- "(ipp-versions-supported=1.0,1.1)",
- p->uri, authentication->values[0].string.text, p->name, location,
- info, p->uri, make_model, p->type, DefaultLanguage,
- p->type & CUPS_PRINTER_COLOR ? "true" : "false",
- finishings,
- p->type & CUPS_PRINTER_DUPLEX ?
- ",two-sided-long-edge,two-sided-short-edge" : "");
+ if ((pptr = strchr(packet, '\"')) != NULL)
+ {
+ /*
+ * Have extended information; can't use sscanf for it because not all
+ * sscanf's allow empty strings with %[^\"]...
+ */
+
+ for (i = 0, pptr ++;
+ i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ location[i] = *pptr;
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
+ if (i)
+ location[i] = '\0';
- /*
- * Register the printer with the SLP server...
- */
+ if (*pptr == '\"')
+ pptr ++;
- error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
- SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
+ while (*pptr && isspace(*pptr & 255))
+ pptr ++;
- if (error != SLP_OK)
- cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
- error);
-}
+ if (*pptr == '\"')
+ {
+ for (i = 0, pptr ++;
+ i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ info[i] = *pptr;
+ info[i] = '\0';
-/*
- * 'slp_attr_callback()' - SLP attribute callback
- */
+ if (*pptr == '\"')
+ pptr ++;
-static SLPBoolean /* O - SLP_TRUE for success */
-slp_attr_callback(
- SLPHandle hslp, /* I - SLP handle */
- const char *attrlist, /* I - Attribute list */
- SLPError errcode, /* I - Parsing status for this attr */
- void *cookie) /* I - Current printer */
-{
- char *tmp = 0; /* Temporary string */
- cupsd_printer_t *p = (cupsd_printer_t*)cookie;
- /* Current printer */
+ while (*pptr && isspace(*pptr & 255))
+ pptr ++;
+ if (*pptr == '\"')
+ {
+ for (i = 0, pptr ++;
+ i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ make_model[i] = *pptr;
- (void)hslp; /* anti-compiler-warning-code */
+ if (*pptr == '\"')
+ pptr ++;
+
+ make_model[i] = '\0';
+
+ if (*pptr)
+ num_attrs = cupsParseOptions(pptr, num_attrs, &attrs);
+ }
+ }
+ }
+
+ DEBUG_puts(packet);
+ DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
+ "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
+ type, state, uri, location, info, make_model));
/*
- * Bail if there was an error
+ * Pull the URI apart to see if this is a local or remote printer...
*/
- if (errcode != SLP_OK)
- return (SLP_TRUE);
+ if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
+ {
+ cupsFreeOptions(num_attrs, attrs);
+ return;
+ }
/*
- * Parse the attrlist to obtain things needed to build CUPS browse packet
+ * Do relaying...
*/
- memset(p, 0, sizeof(cupsd_printer_t));
-
- if (slp_get_attr(attrlist, "(printer-location=", &(p->location)))
- return (SLP_FALSE);
- if (slp_get_attr(attrlist, "(printer-info=", &(p->info)))
- return (SLP_FALSE);
- if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model)))
- return (SLP_FALSE);
- if (!slp_get_attr(attrlist, "(printer-type=", &tmp))
- p->type = atoi(tmp);
- else
- p->type = CUPS_PRINTER_REMOTE;
+ for (i = 0; i < NumRelays; i ++)
+ if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from)))
+ if (sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(Relays[i].to),
+ httpAddrLength(&(Relays[i].to))) <= 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "update_cups_browse: sendto failed for relay %d - %s.",
+ i + 1, strerror(errno));
+ cupsFreeOptions(num_attrs, attrs);
+ return;
+ }
- cupsdClearString(&tmp);
+ /*
+ * Process the browse data...
+ */
- return (SLP_TRUE);
+ process_browse_data(uri, host, resource, (cups_ptype_t)type,
+ (ipp_pstate_t)state, location, info, make_model,
+ num_attrs, attrs);
}
/*
- * 'slp_dereg_printer()' - SLPDereg() the specified printer
+ * 'update_lpd()' - Update the LPD configuration as needed.
*/
-static void
-slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */
+static void
+update_lpd(int onoff) /* - 1 = turn on, 0 = turn off */
{
- char srvurl[HTTP_MAX_URI]; /* Printer service URI */
-
-
- cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
+ if (!LPDConfigFile)
+ return;
- if (!(p->type & CUPS_PRINTER_REMOTE))
+ if (!strncmp(LPDConfigFile, "xinetd:///", 10))
{
/*
- * Make the SLP service URL that conforms to the IANA
- * 'printer:' template.
+ * Enable/disable LPD via the xinetd.d config file for cups-lpd...
*/
- snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
+ char newfile[1024]; /* New cups-lpd.N file */
+ cups_file_t *ofp, /* Original file pointer */
+ *nfp; /* New file pointer */
+ char line[1024]; /* Line from file */
- /*
- * Deregister the printer...
- */
- SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
- }
-}
+ snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9);
+ if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
+ LPDConfigFile + 9, strerror(errno));
+ return;
+ }
-/*
- * 'slp_get_attr()' - Get an attribute from an SLP registration.
- */
+ if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
+ newfile, strerror(errno));
+ cupsFileClose(ofp);
+ return;
+ }
-static int /* O - 0 on success */
-slp_get_attr(const char *attrlist, /* I - Attribute list string */
- const char *tag, /* I - Name of attribute */
- char **valbuf) /* O - Value */
-{
- char *ptr1, /* Pointer into string */
- *ptr2; /* ... */
+ /*
+ * Copy all of the lines from the cups-lpd file...
+ */
+ while (cupsFileGets(ofp, line, sizeof(line)))
+ {
+ if (line[0] == '{')
+ {
+ cupsFilePrintf(nfp, "%s\n", line);
+ snprintf(line, sizeof(line), "\tdisable = %s",
+ onoff ? "no" : "yes");
+ }
+ else if (strstr(line, "disable ="))
+ continue;
- cupsdClearString(valbuf);
+ cupsFilePrintf(nfp, "%s\n", line);
+ }
- if ((ptr1 = strstr(attrlist, tag)) != NULL)
+ cupsFileClose(nfp);
+ cupsFileClose(ofp);
+ rename(newfile, LPDConfigFile + 9);
+ }
+ else if (!strncmp(LPDConfigFile, "launchd:///", 11))
{
- ptr1 += strlen(tag);
-
- if ((ptr2 = strchr(ptr1,')')) != NULL)
- {
- /*
- * Copy the value...
- */
+ /*
+ * Enable/disable LPD via the launchctl command...
+ */
- *valbuf = calloc(ptr2 - ptr1 + 1, 1);
- strncpy(*valbuf, ptr1, ptr2 - ptr1);
+ char *argv[5], /* Arguments for command */
+ *envp[MAX_ENV]; /* Environment for command */
+ int pid; /* Process ID */
- /*
- * Dequote the value...
- */
- for (ptr1 = *valbuf; *ptr1; ptr1 ++)
- if (*ptr1 == '\\' && ptr1[1])
- _cups_strcpy(ptr1, ptr1 + 1);
+ cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+ argv[0] = (char *)"launchctl";
+ argv[1] = (char *)(onoff ? "load" : "unload");
+ argv[2] = (char *)"-w";
+ argv[3] = LPDConfigFile + 10;
+ argv[4] = NULL;
- return (0);
- }
+ cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1, &pid);
}
-
- return (-1);
}
/*
- * 'slp_reg_callback()' - Empty SLPRegReport.
+ * 'update_polling()' - Read status messages from the poll daemons.
*/
static void
-slp_reg_callback(SLPHandle hslp, /* I - SLP handle */
- SLPError errcode, /* I - Error code, if any */
- void *cookie) /* I - App data */
+update_polling(void)
{
- (void)hslp;
- (void)errcode;
- (void)cookie;
+ char *ptr, /* Pointer to end of line in buffer */
+ message[1024]; /* Pointer to message text */
+ int loglevel; /* Log level for message */
- return;
+
+ while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
+ message, sizeof(message))) != NULL)
+ if (!strchr(PollStatusBuffer->buffer, '\n'))
+ break;
+
+ if (ptr == NULL && !PollStatusBuffer->bufused)
+ {
+ /*
+ * All polling processes have died; stop polling...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "update_polling: all polling processes have exited!");
+ cupsdStopPolling();
+ }
}
/*
- * 'slp_url_callback()' - SLP service url callback
+ * 'update_smb()' - Update the SMB configuration as needed.
*/
-static SLPBoolean /* O - TRUE = OK, FALSE = error */
-slp_url_callback(
- SLPHandle hslp, /* I - SLP handle */
- const char *srvurl, /* I - URL of service */
- unsigned short lifetime, /* I - Life of service */
- SLPError errcode, /* I - Existing error code */
- void *cookie) /* I - Pointer to service list */
+static void
+update_smb(int onoff) /* I - 1 = turn on, 0 = turn off */
{
- slpsrvurl_t *s, /* New service entry */
- **head; /* Pointer to head of entry */
+ if (!SMBConfigFile)
+ return;
+ if (!strncmp(SMBConfigFile, "samba:///", 9))
+ {
+ /*
+ * Enable/disable SMB via the specified smb.conf config file...
+ */
- /*
- * Let the compiler know we won't be using these vars...
- */
+ char newfile[1024]; /* New smb.conf.N file */
+ cups_file_t *ofp, /* Original file pointer */
+ *nfp; /* New file pointer */
+ char line[1024]; /* Line from file */
+ int in_printers; /* In [printers] section? */
- (void)hslp;
- (void)lifetime;
- /*
- * Bail if there was an error
- */
+ snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8);
- if (errcode != SLP_OK)
- return (SLP_TRUE);
+ if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
+ SMBConfigFile + 8, strerror(errno));
+ return;
+ }
- /*
- * Grab the head of the list...
- */
+ if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
+ newfile, strerror(errno));
+ cupsFileClose(ofp);
+ return;
+ }
- head = (slpsrvurl_t**)cookie;
+ /*
+ * Copy all of the lines from the smb.conf file...
+ */
- /*
- * Allocate a *temporary* slpsrvurl_t to hold this entry.
- */
+ in_printers = 0;
- if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
- return (SLP_FALSE);
+ while (cupsFileGets(ofp, line, sizeof(line)))
+ {
+ if (in_printers && strstr(line, "printable ="))
+ snprintf(line, sizeof(line), " printable = %s",
+ onoff ? "yes" : "no");
- /*
- * Copy the SLP service URL...
- */
+ cupsFilePrintf(nfp, "%s\n", line);
- strlcpy(s->url, srvurl, sizeof(s->url));
+ if (line[0] == '[')
+ in_printers = !strcmp(line, "[printers]");
+ }
- /*
- * Link the SLP service URL into the head of the list
- */
+ cupsFileClose(nfp);
+ cupsFileClose(ofp);
+ rename(newfile, SMBConfigFile + 8);
+ }
+ else if (!strncmp(SMBConfigFile, "launchd:///", 11))
+ {
+ /*
+ * Enable/disable SMB via the launchctl command...
+ */
- if (*head)
- s->next = *head;
+ char *argv[5], /* Arguments for command */
+ *envp[MAX_ENV]; /* Environment for command */
+ int pid; /* Process ID */
- *head = s;
- return (SLP_TRUE);
+ cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+ argv[0] = (char *)"launchctl";
+ argv[1] = (char *)(onoff ? "load" : "unload");
+ argv[2] = (char *)"-w";
+ argv[3] = SMBConfigFile + 10;
+ argv[4] = NULL;
+
+ cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1, &pid);
+ }
}
-#endif /* HAVE_LIBSLP */
/*
- * End of "$Id: dirsvc.c 5548 2006-05-19 19:38:31Z mike $".
+ * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".
*/