/*
- * "$Id: dirsvc.c 4906 2006-01-10 20:53:28Z mike $"
+ * "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $"
*
* Directory services routines for the Common UNIX Printing System (CUPS).
*
- * Copyright 1997-2005 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2008 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:
*
- * cupsdLoadRemoteCache() - Load the remote printer cache.
- * cupsdProcessBrowseData() - Process new browse data.
- * cupsdProcessImplicitClasses() - Create/update implicit classes as needed.
- * cupsdSaveRemoteCache() - Save the remote printer cache.
- * cupsdSendBrowseDelete() - Send a "browse delete" message for a
- * printer.
- * cupsdSendBrowseList() - Send new browsing information as necessary.
- * cupsdSendCUPSBrowse() - Send new browsing information using the
- * CUPS protocol.
- * cupsdSendSLPBrowse() - Register the specified printer with SLP.
- * cupsdStartBrowsing() - Start sending and receiving broadcast
- * information.
- * cupsdStartPolling() - Start polling servers as needed.
- * 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.
- * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
- * slp_attr_callback() - SLP attribute callback
- * slp_dereg_printer() - SLPDereg() the specified printer
- * slp_get_attr() - Get an attribute from an SLP registration.
- * slp_reg_callback() - Empty SLPRegReport.
- * slp_url_callback() - SLP service url callback
+ * 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 or update the broadcast contents.
+ * cupsdRestartPolling() - Restart polling servers as needed.
+ * cupsdSaveRemoteCache() - Save the remote printer cache.
+ * cupsdSendBrowseList() - Send new browsing information as necessary.
+ * cupsdStartBrowsing() - Start sending and receiving broadcast
+ * information.
+ * cupsdStartPolling() - Start polling servers as needed.
+ * cupsdStopBrowsing() - Stop sending and receiving broadcast
+ * information.
+ * cupsdStopPolling() - Stop polling servers as needed.
+ * cupsdUpdateDNSSDName() - Update the computer name we use for
+ * browsing...
+ * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
+ * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
+ * dequote() - Remote quotes from a string.
+ * dnssdBuildTxtRecord() - Build a TXT record from printer info.
+ * dnssdComparePrinters() - Compare the registered names of two printers.
+ * 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.
+ * dnssdUpdate() - Handle DNS-SD queries.
+ * get_hostconfig() - Get an /etc/hostconfig service setting.
+ * is_local_queue() - Determine whether the URI points at a local
+ * queue.
+ * process_browse_data() - Process new browse data.
+ * process_implicit_classes() - Create/update implicit classes as needed.
+ * send_cups_browse() - Send new browsing information using the CUPS
+ * protocol.
+ * send_ldap_browse() - Send LDAP printer registrations.
+ * send_slp_browse() - Register the specified printer with SLP.
+ * slp_attr_callback() - SLP attribute callback
+ * slp_dereg_printer() - SLPDereg() the specified printer
+ * 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 */
+
/*
- * SLP definitions...
+ * Local functions...
*/
+static char *dequote(char *d, const char *s, int dlen);
+#ifdef __APPLE__
+static int get_hostconfig(const char *name);
+#endif /* __APPLE__ */
+static int is_local_queue(const char *uri, char *host, int hostlen,
+ char *resource, int resourcelen);
+static void process_browse_data(const char *uri, const char *host,
+ const char *resource, cups_ptype_t type,
+ 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 send_cups_browse(cupsd_printer_t *p);
+#ifdef HAVE_LDAP
+static void send_ldap_browse(cupsd_printer_t *p);
+#endif /* HAVE_LDAP */
+#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_DNSSD
+static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
+ int for_lpd);
+static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
+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);
+static void dnssdUpdate(void);
+#endif /* HAVE_DNSSD */
+
+#ifdef HAVE_OPENLDAP
+static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
+ {
+ "printerDescription",
+ "printerLocation",
+ "printerMakeAndModel",
+ "printerType",
+ "printerURI",
+ NULL
+ };
+#endif /* HAVE_OPENLDAP */
+
#ifdef HAVE_LIBSLP
+/*
+ * SLP definitions...
+ */
+
/*
* SLP service name for CUPS...
*/
#endif /* HAVE_LIBSLP */
+/*
+ * '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 printer...
+ */
+
+ 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) && DNSSDRef)
+ dnssdDeregisterPrinter(p);
+#endif /* HAVE_DNSSD */
+}
+
+
/*
* 'cupsdLoadRemoteCache()' - Load the remote printer cache.
*/
int linenum; /* Current line number */
char line[1024], /* Line from file */
*value, /* Pointer to value */
- *valueptr; /* Pointer into value */
+ *valueptr, /* Pointer into value */
+ scheme[32], /* Scheme portion of URI */
+ username[64], /* Username portion of URI */
+ host[HTTP_MAX_HOST],
+ /* Hostname portion of URI */
+ resource[HTTP_MAX_URI];
+ /* Resource portion of URI */
+ int port; /* Port number */
cupsd_printer_t *p; /* Current printer */
time_t now; /* Current time */
+ /*
+ * Don't load the cache if the remote protocols are disabled...
+ */
+
+ if (!Browsing)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdLoadRemoteCache: Not loading remote cache.");
+ return;
+ }
+
/*
* Open the remote.cache file...
*/
cupsdLogMessage(CUPSD_LOG_DEBUG,
"cupsdLoadRemoteCache: Loading printer %s...", value);
- p = cupsdAddPrinter(value);
- p->accepting = 1;
- p->state = IPP_PRINTER_IDLE;
- p->type |= CUPS_PRINTER_REMOTE;
- p->browse_time = now + BrowseTimeout;
+ if ((p = cupsdFindDest(value)) != NULL)
+ {
+ if (p->type & CUPS_PRINTER_CLASS)
+ {
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "Cached remote printer \"%s\" conflicts with "
+ "existing class!",
+ value);
+ p = NULL;
+ continue;
+ }
+ }
+ else
+ p = cupsdAddPrinter(value);
+
+ 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...
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "<Class") ||
cupsdLogMessage(CUPSD_LOG_DEBUG,
"cupsdLoadRemoteCache: Loading class %s...", value);
- p = cupsdAddClass(value);
- p->accepting = 1;
- p->state = IPP_PRINTER_IDLE;
- p->type |= CUPS_PRINTER_REMOTE;
- p->browse_time = now + BrowseTimeout;
+ if ((p = cupsdFindDest(value)) != NULL)
+ p->type = CUPS_PRINTER_CLASS;
+ else
+ p = cupsdAddClass(value);
+
+ 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...
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "</Printer>") ||
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!p)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
else if (!strcasecmp(line, "Info"))
{
else if (!strcasecmp(line, "DeviceURI"))
{
if (value)
+ {
+ httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme),
+ username, sizeof(username), host, sizeof(host), &port,
+ resource, sizeof(resource));
+
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, value);
cupsdSetString(&p->device_uri, value);
+ }
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
+ }
+ }
+ else if (!strcasecmp(line, "Option") && value)
+ {
+ /*
+ * Option name value
+ */
+
+ for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
+
+ if (!*valueptr)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Syntax error on line %d of remote.cache.", linenum);
+ else
+ {
+ for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
+
+ p->num_options = cupsAddOption(value, valueptr, p->num_options,
+ &(p->options));
}
}
else if (!strcasecmp(line, "State"))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "StateMessage"))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "Type"))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "BrowseTime"))
{
time_t t = atoi(value);
- if (t > (now + BrowseInterval))
- p->browse_time = t;
+ if (t > p->browse_expire)
+ p->browse_expire = t;
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "JobSheets"))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "AllowUser"))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else if (!strcasecmp(line, "DenyUser"))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- return;
+ break;
}
}
else
* Do auto-classing if needed...
*/
- cupsdProcessImplicitClasses();
+ process_implicit_classes();
}
/*
- * 'cupsdProcessBrowseData()' - Process new browse data.
+ * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
+ * printer or update the broadcast contents.
*/
void
-cupsdProcessBrowseData(
- const char *uri, /* I - URI of printer/class */
- cups_ptype_t type, /* I - Printer type */
- ipp_pstate_t state, /* I - Printer state */
- const char *location, /* I - Printer location */
- const char *info, /* I - Printer information */
- const char *make_model, /* I - Printer make and model */
- int num_attrs, /* I - Number of attributes */
- cups_option_t *attrs) /* I - Attributes */
+cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
{
- int update; /* Update printer attributes? */
- char finaluri[HTTP_MAX_URI], /* Final URI for printer */
- method[HTTP_MAX_URI], /* Method portion of URI */
- username[HTTP_MAX_URI], /* Username portion of URI */
- host[HTTP_MAX_URI], /* Host portion of URI */
- resource[HTTP_MAX_URI]; /* Resource portion of URI */
- int port; /* Port portion of URI */
- char name[IPP_MAX_NAME], /* Name of printer */
- *hptr, /* Pointer into hostname */
- *sptr; /* Pointer into ServerName */
- char local_make_model[IPP_MAX_NAME];
- /* Local make and model */
- cupsd_printer_t *p; /* Printer information */
- const char *ipp_options; /* ipp-options value */
+ if (!Browsing || !BrowseLocalProtocols ||
+ (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ return;
+#ifdef HAVE_LIBSLP
+/* if (BrowseLocalProtocols & BROWSE_SLP)
+ slpRegisterPrinter(p); */
+#endif /* HAVE_LIBSLP */
- /*
- * Pull the URI apart to see if this is a local or remote printer...
- */
+#ifdef HAVE_DNSSD
+ if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
+ 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);
+}
+
+
+/*
+ * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
+ */
+
+void
+cupsdSaveRemoteCache(void)
+{
+ int i; /* Looping var */
+ cups_file_t *fp; /* printers.conf file */
+ char temp[1024]; /* Temporary string */
+ cupsd_printer_t *printer; /* Current printer class */
+ time_t curtime; /* Current time */
+ struct tm *curdate; /* Current date */
+ cups_option_t *option; /* Current option */
- httpSeparateURI(uri, method, sizeof(method), username, sizeof(username),
- host, sizeof(host), &port, resource, sizeof(resource));
/*
- * Determine if the URI contains any illegal characters in it...
+ * Create the remote.cache file...
*/
- if (strncmp(uri, "ipp://", 6) || !host[0] ||
- (strncmp(resource, "/printers/", 10) &&
- strncmp(resource, "/classes/", 9)))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdProcessBrowseData: Bad printer URI in browse data: %s",
- uri);
- return;
- }
+ snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir);
- if (strchr(resource, '?') ||
- (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) ||
- (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/')))
+ if ((fp = cupsFileOpen(temp, "w")) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdProcessBrowseData: Bad resource in browse data: %s",
- resource);
+ "Unable to save remote.cache - %s", strerror(errno));
return;
}
+ else
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Saving remote.cache...");
/*
- * OK, this isn't a local printer; add any remote options...
+ * Restrict access to the file...
*/
- ipp_options = cupsGetOption("ipp-options", num_attrs, attrs);
-
- if (BrowseRemoteOptions)
- {
- if (BrowseRemoteOptions[0] == '?')
- {
- /*
- * Override server-supplied options...
- */
-
- snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions);
- }
- else if (ipp_options)
- {
- /*
- * Combine the server and local options...
- */
-
- snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options,
- BrowseRemoteOptions);
- }
- else
- {
- /*
- * Just use the local options...
- */
+ fchown(cupsFileNumber(fp), getuid(), Group);
+ fchmod(cupsFileNumber(fp), ConfigFilePerm);
- snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions);
- }
+ /*
+ * Write a small header to the file...
+ */
- uri = finaluri;
- }
- else if (ipp_options)
- {
- /*
- * Just use the server-supplied options...
- */
+ curtime = time(NULL);
+ curdate = localtime(&curtime);
+ strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
- snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
- uri = finaluri;
- }
+ cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n");
+ cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
/*
- * See if we already have it listed in the Printers list, and add it if not...
+ * Write each local printer known to the system...
*/
- type |= CUPS_PRINTER_REMOTE;
- type &= ~CUPS_PRINTER_IMPLICIT;
- update = 0;
- hptr = strchr(host, '.');
- sptr = strchr(ServerName, '.');
-
- if (sptr != NULL && hptr != NULL)
+ for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ printer;
+ printer = (cupsd_printer_t *)cupsArrayNext(Printers))
{
/*
- * Strip the common domain name components...
+ * Skip local destinations...
*/
- while (hptr != NULL)
- {
- if (!strcasecmp(hptr, sptr))
- {
- *hptr = '\0';
- break;
- }
- else
- hptr = strchr(hptr + 1, '.');
- }
- }
+ if (!(printer->type & CUPS_PRINTER_DISCOVERED))
+ continue;
- if (type & CUPS_PRINTER_CLASS)
- {
/*
- * Remote destination is a class...
+ * Write printers as needed...
*/
- if (!strncmp(resource, "/classes/", 9))
- snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
+ if (printer == DefaultPrinter)
+ cupsFilePuts(fp, "<Default");
else
- return;
+ cupsFilePutChar(fp, '<');
- if ((p = cupsdFindClass(name)) == NULL && BrowseShortNames)
- {
- if ((p = cupsdFindClass(resource + 9)) != NULL)
- {
- if (p->hostname && strcasecmp(p->hostname, host))
- {
- /*
- * Nope, this isn't the same host; if the hostname isn't the local host,
- * add it to the other class and then find a class using the full host
- * name...
- */
+ if (printer->type & CUPS_PRINTER_CLASS)
+ cupsFilePrintf(fp, "Class %s>\n", printer->name);
+ else
+ cupsFilePrintf(fp, "Printer %s>\n", printer->name);
- if (p->type & CUPS_PRINTER_REMOTE)
- {
- cupsdLogMessage(CUPSD_LOG_INFO,
- "Renamed remote class \"%s\" to \"%s@%s\"...",
- p->name, p->name, p->hostname);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "Class \'%s\' deleted by directory services.",
- p->name);
+ cupsFilePrintf(fp, "Type %d\n", printer->type);
- cupsArrayRemove(Printers, p);
- cupsdSetStringf(&p->name, "%s@%s", p->name, p->hostname);
- cupsdSetPrinterAttrs(p);
- cupsArrayAdd(Printers, p);
+ cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Class \'%s\' added by directory services.",
- p->name);
- }
+ if (printer->info)
+ cupsFilePrintf(fp, "Info %s\n", printer->info);
- p = NULL;
- }
- else if (!p->hostname)
- {
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ if (printer->make_model)
+ cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model);
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
- }
- else
- {
- /*
- * Use the short name for this shared class.
- */
+ if (printer->location)
+ cupsFilePrintf(fp, "Location %s\n", printer->location);
- strlcpy(name, resource + 9, sizeof(name));
- }
- }
- else if (p && !p->hostname)
- {
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ if (printer->device_uri)
+ cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
+ if (printer->state == IPP_PRINTER_STOPPED)
+ {
+ cupsFilePuts(fp, "State Stopped\n");
+ cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
}
+ else
+ cupsFilePuts(fp, "State Idle\n");
- if (!p)
+ if (printer->accepting)
+ cupsFilePuts(fp, "Accepting Yes\n");
+ else
+ cupsFilePuts(fp, "Accepting No\n");
+
+ cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
+ printer->job_sheets[1]);
+
+ for (i = 0; i < printer->num_users; i ++)
+ cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
+ printer->users[i]);
+
+ for (i = printer->num_options, option = printer->options;
+ i > 0;
+ i --, option ++)
+ cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
+
+ if (printer->type & CUPS_PRINTER_CLASS)
+ cupsFilePuts(fp, "</Class>\n");
+ else
+ cupsFilePuts(fp, "</Printer>\n");
+ }
+
+ cupsFileClose(fp);
+}
+
+
+/*
+ * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
+ */
+
+void
+cupsdSendBrowseList(void)
+{
+ int count; /* Number of dests to update */
+ cupsd_printer_t *p; /* Current printer */
+ time_t ut, /* Minimum update time */
+ to; /* Timeout time */
+
+
+ if (!Browsing || !BrowseLocalProtocols || !Printers)
+ return;
+
+ /*
+ * Compute the update and timeout times...
+ */
+
+ to = time(NULL);
+ ut = to - BrowseInterval;
+
+ /*
+ * Figure out how many printers need an update...
+ */
+
+ if (BrowseInterval > 0)
+ {
+ int max_count; /* Maximum number to update */
+
+
+ /*
+ * Throttle the number of printers we'll be updating this time
+ * around based on the number of queues that need updating and
+ * the maximum number of queues to update each second...
+ */
+
+ max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1;
+
+ for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ count < max_count && p != NULL;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
+ p->shared && p->browse_time < ut)
+ count ++;
+
+ /*
+ * Loop through all of the printers and send local updates as needed...
+ */
+
+ if (BrowseNext)
+ p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext);
+ else
+ p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+
+ for (;
+ count > 0;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
/*
- * Class doesn't exist; add it...
+ * Check for wraparound...
*/
- p = cupsdAddClass(name);
+ if (!p)
+ p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+
+ if (!p)
+ break;
+ else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
+ !p->shared)
+ continue;
+ else if (p->browse_time < ut)
+ {
+ /*
+ * Need to send an update...
+ */
- cupsdLogMessage(CUPSD_LOG_INFO, "Added remote class \"%s\"...", name);
+ count --;
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Class \'%s\' added by directory services.", name);
+ p->browse_time = time(NULL);
- /*
- * Force the URI to point to the real server...
- */
+ if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
+ send_cups_browse(p);
- p->type = type & ~CUPS_PRINTER_REJECTING;
- p->accepting = 1;
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- cupsdSetString(&p->hostname, host);
+#ifdef HAVE_LIBSLP
+ if (BrowseLocalProtocols & BROWSE_SLP)
+ send_slp_browse(p);
+#endif /* HAVE_LIBSLP */
- update = 1;
+#ifdef HAVE_LDAP
+ if (BrowseLocalProtocols & BROWSE_LDAP)
+ send_ldap_browse(p);
+#endif /* HAVE_LDAP */
+ }
}
+
+ /*
+ * Save where we left off so that all printers get updated...
+ */
+
+ BrowseNext = p;
}
- else
+
+ /*
+ * Loop through all of the printers and send local updates as needed...
+ */
+
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
/*
- * Remote destination is a printer...
+ * If this is a remote queue, see if it needs to be timed out...
*/
- if (strncmp(resource, "/printers/", 10) == 0)
- snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
- else
- return;
-
- if ((p = cupsdFindPrinter(name)) == NULL && BrowseShortNames)
+ if (p->type & CUPS_PRINTER_DISCOVERED)
{
- if ((p = cupsdFindPrinter(resource + 10)) != NULL)
+ if (p->browse_expire < to)
{
- if (p->hostname && strcasecmp(p->hostname, host))
- {
- /*
- * Nope, this isn't the same host; if the hostname isn't the local host,
- * add it to the other printer and then find a printer using the full host
- * name...
- */
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
+ "%s \'%s\' deleted by directory services (timeout).",
+ (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
+ p->name);
- if (p->type & CUPS_PRINTER_REMOTE)
- {
- cupsdLogMessage(CUPSD_LOG_INFO,
- "Renamed remote printer \"%s\" to \"%s@%s\"...",
- p->name, p->name, p->hostname);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "Printer \'%s\' deleted by directory services.",
- p->name);
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Remote destination \"%s\" has timed out; "
+ "deleting it...",
+ p->name);
+
+ cupsArraySave(Printers);
+ cupsdDeletePrinter(p, 1);
+ cupsArrayRestore(Printers);
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ }
+ }
+ }
+}
- cupsArrayRemove(Printers, p);
- cupsdSetStringf(&p->name, "%s@%s", p->name, p->hostname);
- cupsdSetPrinterAttrs(p);
- cupsArrayAdd(Printers, p);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Printer \'%s\' added by directory services.",
- p->name);
- }
+/*
+ * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
+ */
+
+void
+cupsdStartBrowsing(void)
+{
+ int val; /* Socket option value */
+ struct sockaddr_in addr; /* Broadcast address */
+ cupsd_printer_t *p; /* Current printer */
+
+
+ BrowseNext = NULL;
+
+ if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
+ return;
+
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
+ {
+ if (BrowseSocket < 0)
+ {
+ /*
+ * Create the broadcast socket...
+ */
+
+ if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to create broadcast socket - %s.",
+ strerror(errno));
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+ return;
+ }
+
+ /*
+ * Bind the socket to browse port...
+ */
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(BrowsePort);
+
+ if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to bind broadcast socket - %s.",
+ strerror(errno));
+
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
+
+ BrowseSocket = -1;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+ return;
+ }
+ }
+
+ /*
+ * Set the "broadcast" flag...
+ */
+
+ val = 1;
+ if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.",
+ strerror(errno));
+
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
+
+ BrowseSocket = -1;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+ return;
+ }
+
+ /*
+ * Close the socket on exec...
+ */
+
+ fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
+
+ /*
+ * Finally, add the socket to the input selection set as needed...
+ */
+
+ if (BrowseRemoteProtocols & BROWSE_CUPS)
+ {
+ /*
+ * We only listen if we want remote printers...
+ */
+
+ cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse,
+ NULL, NULL);
+ }
+ }
+ else
+ BrowseSocket = -1;
+
+#ifdef HAVE_DNSSD
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
+ {
+ DNSServiceErrorType error; /* Error from service creation */
+ cupsd_listener_t *lis; /* Current listening socket */
+
+
+ /*
+ * First create a "master" connection for all registrations...
+ */
+
+ if ((error = DNSServiceCreateConnection(&DNSSDRef))
+ != kDNSServiceErr_NoError)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to create master DNS-SD reference: %d", error);
+ else
+ {
+ /*
+ * Add the master connection to the select list...
+ */
+
+ cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef),
+ (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
+
+ /*
+ * Then get the port we use for registrations. If we are not listening
+ * on any non-local ports, there is no sense sharing local printers via
+ * Bonjour...
+ */
+
+ DNSSDPort = 0;
+
+ for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
+ lis;
+ lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
+ {
+ if (httpAddrLocalhost(&(lis->address)))
+ continue;
+
+ if (lis->address.addr.sa_family == AF_INET)
+ {
+ DNSSDPort = ntohs(lis->address.ipv4.sin_port);
+ break;
+ }
+ else if (lis->address.addr.sa_family == AF_INET6)
+ {
+ DNSSDPort = ntohs(lis->address.ipv6.sin6_port);
+ break;
+ }
+ }
+
+ /*
+ * Create an array to track the printers we share...
+ */
+
+ if (BrowseRemoteProtocols & BROWSE_DNSSD)
+ DNSSDPrinters = cupsArrayNew((cups_array_func_t)dnssdComparePrinters,
+ NULL);
+
+ /*
+ * Set the computer name and register the web interface...
+ */
+
+ cupsdUpdateDNSSDName();
+ }
+ }
+#endif /* HAVE_DNSSD */
+
+#ifdef HAVE_LIBSLP
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
+ {
+ /*
+ * Open SLP handle...
+ */
+
+ if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to open an SLP handle; disabling SLP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_SLP;
+ BrowseRemoteProtocols &= ~BROWSE_SLP;
+ }
+
+ BrowseSLPRefresh = 0;
+ }
+ else
+ BrowseSLPHandle = NULL;
+#endif /* HAVE_LIBSLP */
+
+#ifdef HAVE_OPENLDAP
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP)
+ {
+ if (!BrowseLDAPDN)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Need to set BrowseLDAPDN to use LDAP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ }
+ else
+ {
+ /*
+ * Open LDAP handle...
+ */
+
+ int rc; /* LDAP API status */
+ int version = 3; /* LDAP version */
+ 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...
+ */
+
+ if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
+ rc = ldap_initialize(&BrowseLDAPHandle, "ldapi:///");
+ else
+ rc = ldap_initialize(&BrowseLDAPHandle, BrowseLDAPServer);
+
+ if (rc != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to initialize LDAP; disabling LDAP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ }
+ else if (ldap_set_option(BrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION,
+ (const void *)&version) != LDAP_SUCCESS)
+ {
+ ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL);
+ BrowseLDAPHandle = NULL;
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to set LDAP protocol version; "
+ "disabling LDAP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ }
+ else
+ {
+ if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
+ rc = ldap_sasl_bind_s(BrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL,
+ NULL, NULL);
+ else
+ rc = ldap_bind_s(BrowseLDAPHandle, BrowseLDAPBindDN,
+ BrowseLDAPPassword, LDAP_AUTH_SIMPLE);
+
+ if (rc != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to bind to LDAP server; "
+ "disabling LDAP browsing!");
+ ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL);
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ }
+ }
+ }
+
+ 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);
+}
+
+
+/*
+ * 'cupsdStartPolling()' - Start polling servers as needed.
+ */
+
+void
+cupsdStartPolling(void)
+{
+ int i; /* Looping var */
+ cupsd_dirsvc_poll_t *pollp; /* Current polling server */
+ char polld[1024]; /* Poll daemon path */
+ 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 || BrowseSocket < 0)
+ {
+ PollPipe = -1;
+ PollStatusBuffer = NULL;
+ return;
+ }
+
+ /*
+ * Setup string arguments for polld, port and interval options.
+ */
+
+ snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
+
+ sprintf(bport, "%d", BrowsePort);
+
+ if (BrowseInterval)
+ sprintf(interval, "%d", BrowseInterval);
+ else
+ strcpy(interval, "30");
+
+ argv[0] = "cups-polld";
+ argv[2] = sport;
+ argv[3] = interval;
+ argv[4] = bport;
+ argv[5] = NULL;
+
+ cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+
+ /*
+ * Create a pipe that receives the status messages from each
+ * polling daemon...
+ */
+
+ if (cupsdOpenPipe(statusfds))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to create polling status pipes - %s.",
+ strerror(errno));
+ PollPipe = -1;
+ PollStatusBuffer = NULL;
+ return;
+ }
+
+ PollPipe = statusfds[0];
+ PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]");
+
+ /*
+ * Run each polling daemon, redirecting stderr to the polling pipe...
+ */
+
+ for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
+ {
+ sprintf(sport, "%d", pollp->port);
+
+ argv[1] = pollp->hostname;
+
+ if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1,
+ 0, DefaultProfile, &(pollp->pid)) < 0)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "cupsdStartPolling: Unable to fork polling daemon - %s",
+ strerror(errno));
+ pollp->pid = 0;
+ break;
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
+ pollp->hostname, pollp->port, pollp->pid);
+ }
+
+ close(statusfds[1]);
+
+ /*
+ * Finally, add the pipe to the input selection set...
+ */
+
+ cupsdAddSelect(PollPipe, (cupsd_selfunc_t)update_polling, NULL, NULL);
+}
+
+
+/*
+ * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
+ */
+
+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 the socket and remove it from the input selection set.
+ */
+
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
+
+ cupsdRemoveSelect(BrowseSocket);
+ BrowseSocket = -1;
+ }
+
+#ifdef HAVE_DNSSD
+ if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
+ {
+ if (WebIFRef)
+ {
+ DNSServiceRefDeallocate(WebIFRef);
+ WebIFRef = NULL;
+ }
+
+ if (RemoteRef)
+ {
+ DNSServiceRefDeallocate(RemoteRef);
+ RemoteRef = NULL;
+ }
+
+ cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef));
+
+ DNSServiceRefDeallocate(DNSSDRef);
+ DNSSDRef = NULL;
+
+ cupsArrayDelete(DNSSDPrinters);
+ DNSSDPrinters = NULL;
+
+ DNSSDPort = 0;
+ }
+#endif /* HAVE_DNSSD */
+
+#ifdef HAVE_LIBSLP
+ if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
+ BrowseSLPHandle)
+ {
+ /*
+ * Close SLP handle...
+ */
+
+ SLPClose(BrowseSLPHandle);
+ BrowseSLPHandle = NULL;
+ }
+#endif /* HAVE_LIBSLP */
+
+#ifdef HAVE_OPENLDAP
+ if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) &&
+ BrowseLDAPHandle)
+ {
+ ldap_unbind(BrowseLDAPHandle);
+ 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);
+}
+
+
+/*
+ * 'cupsdStopPolling()' - Stop polling servers as needed.
+ */
+
+void
+cupsdStopPolling(void)
+{
+ int i; /* Looping var */
+ cupsd_dirsvc_poll_t *pollp; /* Current polling server */
+
+
+ if (PollPipe >= 0)
+ {
+ cupsdStatBufDelete(PollStatusBuffer);
+ close(PollPipe);
+
+ cupsdRemoveSelect(PollPipe);
+
+ PollPipe = -1;
+ PollStatusBuffer = NULL;
+ }
+
+ for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
+ if (pollp->pid)
+ cupsdEndProcess(pollp->pid, 0);
+}
+
+
+#ifdef HAVE_DNSSD
+/*
+ * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
+ */
+
+void
+cupsdUpdateDNSSDName(void)
+{
+ DNSServiceErrorType error; /* Error from service creation */
+ char webif[1024]; /* Web interface share name */
+#ifdef HAVE_COREFOUNDATION_H
+ CFStringRef nameRef; /* Computer name CFString */
+ char nameBuffer[1024]; /* C-string buffer */
+ CFStringEncoding nameEncoding; /* Computer name encoding */
+#endif /* HAVE_COREFOUNDATION_H */
+
+
+ /*
+ * Only share the web interface and printers when non-local listening is
+ * enabled...
+ */
+
+ if (!DNSSDPort)
+ return;
+
+ /*
+ * Get the computer name as a c-string...
+ */
+
+#ifdef HAVE_COREFOUNDATION_H
+ cupsdClearString(&DNSSDName);
+
+ if ((nameRef = SCDynamicStoreCopyComputerName(NULL,
+ &nameEncoding)) != NULL)
+ {
+ if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
+ kCFStringEncodingUTF8))
+ cupsdSetString(&DNSSDName, nameBuffer);
+
+ CFRelease(nameRef);
+ }
+
+#else
+ cupsdSetString(&DNSSDName, ServerName);
+#endif /* HAVE_COREFOUNDATION_H */
+
+ /*
+ * Then (re)register the web interface...
+ */
+
+ if (DNSSDName)
+ snprintf(webif, sizeof(webif), "CUPS @ %s", DNSSDName);
+ else
+ strlcpy(webif, "CUPS Web Interface", sizeof(webif));
+
+ if (WebIFRef)
+ DNSServiceRefDeallocate(WebIFRef);
+
+ WebIFRef = DNSSDRef;
+ if ((error = DNSServiceRegister(&WebIFRef,
+ kDNSServiceFlagsShareConnection,
+ 0, webif, "_http._tcp", NULL,
+ NULL, htons(DNSSDPort), 7,
+ "\006path=/", dnssdRegisterCallback,
+ NULL)) != kDNSServiceErr_NoError)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNS-SD web interface registration failed: %d", error);
+}
+#endif /* HAVE_DNSSD */
+
+
+#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 */
+
+
+ /*
+ * 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...
+ */
+
+ for (e = ldap_first_entry(BrowseLDAPHandle, res);
+ e;
+ e = ldap_next_entry(BrowseLDAPHandle, e))
+ {
+ /*
+ * Get the required values from this entry...
+ */
+
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerDescription")) == NULL)
+ continue;
+
+ strlcpy(info, *value, sizeof(info));
+ ldap_value_free(value);
+
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerLocation")) == NULL)
+ continue;
+
+ strlcpy(location, *value, sizeof(location));
+ ldap_value_free(value);
+
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerMakeAndModel")) == NULL)
+ continue;
+
+ strlcpy(make_model, *value, sizeof(make_model));
+ ldap_value_free(value);
+
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerType")) == NULL)
+ continue;
+
+ type = atoi(*value);
+ ldap_value_free(value);
+
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerURI")) == NULL)
+ continue;
+
+ strlcpy(uri, *value, sizeof(uri));
+ ldap_value_free(value);
+
+ /*
+ * Process the entry as browse data...
+ */
+
+ 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);
+
+ }
+}
+#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 */
+
+
+ /*
+ * 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);
+}
+
+
+#ifdef HAVE_DNSSD
+/*
+ * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
+ */
+
+static char * /* O - TXT record */
+dnssdBuildTxtRecord(
+ int *txt_len, /* O - TXT record length */
+ cupsd_printer_t *p, /* I - Printer information */
+ int for_lpd) /* I - 1 = LPD, 0 = IPP */
+{
+ 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 */
+
+
+ /*
+ * Load up the key value pairs...
+ */
+
+ i = 0;
+
+ keyvalue[i ][0] = "txtvers";
+ keyvalue[i++][1] = "1";
+
+ keyvalue[i ][0] = "qtotal";
+ keyvalue[i++][1] = "1";
+
+ keyvalue[i ][0] = "rp";
+ keyvalue[i++][1] = rp_str;
+ if (for_lpd)
+ strlcpy(rp_str, p->name, sizeof(rp_str));
+ else
+ snprintf(rp_str, sizeof(rp_str), "%s/%s",
+ (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
+
+ keyvalue[i ][0] = "ty";
+ keyvalue[i++][1] = p->make_model;
+
+ if (p->location && *p->location != '\0')
+ {
+ keyvalue[i ][0] = "note";
+ keyvalue[i++][1] = p->location;
+ }
+
+ keyvalue[i ][0] = "priority";
+ keyvalue[i++][1] = for_lpd ? "100" : "0";
+
+ keyvalue[i ][0] = "product";
+ keyvalue[i++][1] = p->product ? p->product : "Unknown";
+
+ snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
+ snprintf(state_str, sizeof(state_str), "%d", p->state);
+
+ keyvalue[i ][0] = "printer-state";
+ keyvalue[i++][1] = state_str;
+
+ keyvalue[i ][0] = "printer-type";
+ keyvalue[i++][1] = type_str;
+
+ keyvalue[i ][0] = "Transparent";
+ keyvalue[i++][1] = "T";
+
+ keyvalue[i ][0] = "Binary";
+ keyvalue[i++][1] = "T";
+
+ if ((p->type & CUPS_PRINTER_FAX))
+ {
+ keyvalue[i ][0] = "Fax";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_COLOR))
+ {
+ keyvalue[i ][0] = "Color";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_DUPLEX))
+ {
+ keyvalue[i ][0] = "Duplex";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_STAPLE))
+ {
+ keyvalue[i ][0] = "Staple";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_COPIES))
+ {
+ keyvalue[i ][0] = "Copies";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_COLLATE))
+ {
+ keyvalue[i ][0] = "Collate";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_PUNCH))
+ {
+ keyvalue[i ][0] = "Punch";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_BIND))
+ {
+ keyvalue[i ][0] = "Bind";
+ keyvalue[i++][1] = "T";
+ }
+
+ if ((p->type & CUPS_PRINTER_SORT))
+ {
+ keyvalue[i ][0] = "Sort";
+ keyvalue[i++][1] = "T";
+ }
+
+ keyvalue[i ][0] = "pdl";
+ keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
+
+ if (p->num_auth_info_required)
+ {
+ char *air = air_str; /* Pointer into string */
+
+
+ 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);
+ }
+
+ keyvalue[i ][0] = "air";
+ keyvalue[i++][1] = air;
+ }
+
+ /*
+ * Then pack them into a proper txt record...
+ */
+
+ return (dnssdPackTxtRecord(txt_len, keyvalue, i));
+}
+
+
+/*
+ * 'dnssdComparePrinters()' - Compare the registered names of two printers.
+ */
+
+static int /* O - Result of comparison */
+dnssdComparePrinters(cupsd_printer_t *a,/* I - First printer */
+ cupsd_printer_t *b)/* I - Second printer */
+{
+ return (strcasecmp(a->reg_name, b->reg_name));
+}
+
+
+/*
+ * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
+ * printer.
+ */
+
+static void
+dnssdDeregisterPrinter(
+ cupsd_printer_t *p) /* I - Printer */
+{
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
+
+ /*
+ * Closing the socket deregisters the service
+ */
+
+ if (p->ipp_ref)
+ {
+ DNSServiceRefDeallocate(p->ipp_ref);
+ p->ipp_ref = NULL;
+ }
+
+ cupsArrayRemove(DNSSDPrinters, p);
+ cupsdClearString(&p->reg_name);
+
+ if (p->ipp_txt)
+ {
+ /*
+ * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
+ */
+
+ free(p->ipp_txt);
+ p->ipp_txt = 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 */
+
+
+ /*
+ * Calculate the buffer size
+ */
+
+ for (length = i = 0; i < count; i++)
+ length += 1 + strlen(keyvalue[i][0]) +
+ (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
+
+ /*
+ * Allocate and fill it
+ */
+
+ txtRecord = malloc(length);
+ if (txtRecord)
+ {
+ *txt_len = length;
+
+ for (cursor = txtRecord, i = 0; i < count; i++)
+ {
+ /*
+ * Drop in the p-string style length byte followed by the data
+ */
+
+ length = strlen(keyvalue[i][0]);
+ length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0;
+
+ *cursor++ = (unsigned char)(length + length2);
+
+ memcpy(cursor, keyvalue[i][0], length);
+ cursor += length;
+
+ if (length2)
+ {
+ length2 --;
+ *cursor++ = '=';
+ memcpy(cursor, keyvalue[i][1], length2);
+ cursor += length2;
+ }
+ }
+ }
+
+ return (txtRecord);
+}
+
+
+/*
+ * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
+ */
+
+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 */
+{
+ cupsd_printer_t *p = (cupsd_printer_t *)context;
+ /* Current printer */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s",
+ name, regtype, p ? p->name : "Web Interface");
+
+ if (errorCode)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNSServiceRegister failed with error %d", (int)errorCode);
+ return;
+ }
+ else if (p && strcasecmp(name, p->reg_name))
+ {
+ cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"",
+ name, p->name);
+
+ cupsArrayRemove(DNSSDPrinters, p);
+ cupsdSetString(&p->reg_name, name);
+ cupsArrayAdd(DNSSDPrinters, p);
+
+ LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
+ }
+}
+
+
+/*
+ * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
+ * or update the broadcast contents.
+ */
+
+static void
+dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
+{
+ DNSServiceErrorType se; /* dnssd errors */
+ char *ipp_txt, /* IPP TXT record buffer */
+ *printer_txt, /* LPD TXT record buffer */
+ name[1024], /* Service name */
+ *nameptr; /* Pointer into name */
+ int ipp_len, /* IPP TXT record length */
+ printer_len; /* LPD TXT record length */
+ char resource[1024]; /* Resource path for printer */
+ const char *regtype; /* Registration type */
+ const char *domain; /* Registration domain */
+ cupsd_location_t *location, /* Printer location */
+ *policy; /* Operation policy for Print-Job */
+ unsigned address[4]; /* INADDR_ANY address */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
+ !p->ipp_ref ? "new" : "update");
+
+ /*
+ * If per-printer sharing was just disabled make sure we're not
+ * registered before returning.
+ */
+
+ if (!p->shared)
+ {
+ dnssdDeregisterPrinter(p);
+ return;
+ }
+
+ /*
+ * The registered name takes the form of "<printer-info> @ <computer name>"...
+ */
+
+ if (p->info && strlen(p->info) > 0)
+ {
+ if (DNSSDName)
+ snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDName);
+ else
+ strlcpy(name, p->info, sizeof(name));
+ }
+ else if (DNSSDName)
+ snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDName);
+ else
+ strlcpy(name, p->name, sizeof(name));
+
+ /*
+ * If an existing printer was renamed, unregister it and start over...
+ */
+
+ if (p->reg_name && strcmp(p->reg_name, name))
+ dnssdDeregisterPrinter(p);
+
+ if (!p->reg_name)
+ {
+ cupsdSetString(&p->reg_name, name);
+ cupsArrayAdd(DNSSDPrinters, p);
+ }
+
+ /*
+ * If 'Allow printing from the Internet' is enabled (i.e. from any address)
+ * let dnssd decide on the domain, otherwise restrict it to ".local".
+ */
+
+ if (p->type & CUPS_PRINTER_CLASS)
+ snprintf(resource, sizeof(resource), "/classes/%s", p->name);
+ else
+ snprintf(resource, sizeof(resource), "/printers/%s", p->name);
+
+ address[0] = address[1] = address[2] = address[3] = 0;
+ location = cupsdFindBest(resource, HTTP_POST);
+ policy = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
+
+ if ((location && !cupsdCheckAccess(address, "", 0, location)) ||
+ (policy && !cupsdCheckAccess(address, "", 0, policy)))
+ domain = "local.";
+ else
+ domain = NULL;
+
+ /*
+ * Register IPP and (optionally) LPD...
+ */
+
+ ipp_len = 0; /* anti-compiler-warning-code */
+ ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
+
+ if (!p->ipp_ref)
+ {
+ /*
+ * Initial registration. Use the _fax subtype for fax queues...
+ */
+
+ regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" :
+ "_ipp._tcp,_cups";
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Registering DNS-SD printer %s with name \"%s\", "
+ "type \"%s\", and domain \"%s\"", p->name, name, regtype,
+ domain ? domain : "(null)");
+
+ /*
+ * Register the queue, dropping characters as needed until we succeed...
+ */
- p = NULL;
- }
- else if (!p->hostname)
- {
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ nameptr = name + strlen(name);
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
- }
- else
+ do
+ {
+ p->ipp_ref = DNSSDRef;
+ if ((se = DNSServiceRegister(&p->ipp_ref, kDNSServiceFlagsShareConnection,
+ 0, name, regtype, domain, NULL,
+ htons(DNSSDPort), ipp_len, ipp_txt,
+ dnssdRegisterCallback,
+ p)) == kDNSServiceErr_BadParam)
{
/*
- * Use the short name for this shared printer.
+ * Name is too long, drop trailing characters, taking into account
+ * UTF-8 encoding...
*/
- strlcpy(name, resource + 10, sizeof(name));
+ nameptr --;
+
+ while (nameptr > name && (*nameptr & 0xc0) == 0x80)
+ nameptr --;
+
+ if (nameptr > name)
+ *nameptr = '\0';
}
}
- else if (p && !p->hostname)
+ while (se == kDNSServiceErr_BadParam && nameptr > name);
+
+ if (se == kDNSServiceErr_NoError)
+ {
+ p->ipp_txt = ipp_txt;
+ p->ipp_len = ipp_len;
+ ipp_txt = NULL;
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "DNS-SD IPP registration of \"%s\" failed: %d",
+ p->name, se);
+ }
+ else if (ipp_len != p->ipp_len || memcmp(ipp_txt, p->ipp_txt, ipp_len))
+ {
+ /*
+ * Update the existing registration...
+ */
+
+ /* A TTL of 0 means use record's original value (Radar 3176248) */
+ DNSServiceUpdateRecord(p->ipp_ref, NULL, 0, ipp_len, ipp_txt, 0);
+
+ if (p->ipp_txt)
+ free(p->ipp_txt);
+
+ p->ipp_txt = ipp_txt;
+ p->ipp_len = ipp_len;
+ ipp_txt = NULL;
+ }
+
+ if (ipp_txt)
+ free(ipp_txt);
+
+ if (BrowseLocalProtocols & BROWSE_LPD)
+ {
+ printer_len = 0; /* anti-compiler-warning-code */
+ printer_txt = dnssdBuildTxtRecord(&printer_len, p, 1);
+
+ if (!p->printer_ref)
{
/*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
+ * Initial registration...
*/
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Registering DNS-SD printer %s with name \"%s\", "
+ "type \"_printer._tcp\", and domain \"%s\"", p->name,
+ name, domain ? domain : "(null)");
+
+ p->printer_ref = DNSSDRef;
+ if ((se = DNSServiceRegister(&p->printer_ref,
+ kDNSServiceFlagsShareConnection,
+ 0, name, "_printer._tcp", domain, NULL,
+ htons(515), printer_len, printer_txt,
+ dnssdRegisterCallback,
+ p)) == kDNSServiceErr_NoError)
+ {
+ p->printer_txt = printer_txt;
+ p->printer_len = printer_len;
+ printer_txt = NULL;
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "DNS-SD LPD registration of \"%s\" failed: %d",
+ p->name, se);
}
-
- if (!p)
+ else if (printer_len != p->printer_len ||
+ memcmp(printer_txt, p->printer_txt, printer_len))
{
/*
- * Printer doesn't exist; add it...
+ * Update the existing registration...
*/
- p = cupsdAddPrinter(name);
+ /* A TTL of 0 means use record's original value (Radar 3176248) */
+ DNSServiceUpdateRecord(p->printer_ref, NULL, 0, printer_len,
+ printer_txt, 0);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Printer \'%s\' added by directory services.", name);
+ if (p->printer_txt)
+ free(p->printer_txt);
- cupsdLogMessage(CUPSD_LOG_INFO, "Added remote printer \"%s\"...", name);
+ p->printer_txt = printer_txt;
+ p->printer_len = printer_len;
+ printer_txt = NULL;
+ }
- /*
- * Force the URI to point to the real server...
- */
+ if (printer_txt)
+ free(printer_txt);
+ }
+}
- p->type = type & ~CUPS_PRINTER_REJECTING;
- p->accepting = 1;
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
- }
+/*
+ * 'dnssdUpdate()' - Handle DNS-SD queries.
+ */
+
+static void
+dnssdUpdate(void)
+{
+ DNSServiceErrorType sdErr; /* Service discovery error */
+
+
+ if ((sdErr = DNSServiceProcessResult(DNSSDRef)) != kDNSServiceErr_NoError)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNS Service Discovery registration error %d!",
+ sdErr);
+}
+#endif /* HAVE_DNSSD */
+
+
+#ifdef __APPLE__
+/*
+ * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
+ */
+
+static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
+get_hostconfig(const char *name) /* I - Name of service */
+{
+ cups_file_t *fp; /* Hostconfig file */
+ char line[1024], /* Line from file */
+ *ptr; /* Pointer to value */
+ int state = 1; /* State of service */
+
/*
- * Update the state...
+ * Try opening the /etc/hostconfig file; if we can't open it, assume that
+ * the service is enabled/auto.
*/
- p->state = state;
- p->browse_time = time(NULL);
-
- if (type & CUPS_PRINTER_REJECTING)
+ if ((fp = cupsFileOpen("/etc/hostconfig", "r")) != NULL)
{
- type &= ~CUPS_PRINTER_REJECTING;
+ /*
+ * Read lines from the file until we find the service...
+ */
- if (p->accepting)
+ while (cupsFileGets(fp, line, sizeof(line)))
{
- update = 1;
- p->accepting = 0;
- }
- }
- else if (!p->accepting)
- {
- update = 1;
- p->accepting = 1;
- }
+ if (line[0] == '#' || (ptr = strchr(line, '=')) == NULL)
+ continue;
- if (p->type != type)
- {
- p->type = type;
- update = 1;
- }
+ *ptr++ = '\0';
- if (location && (!p->location || strcmp(p->location, location)))
- {
- cupsdSetString(&p->location, location);
- update = 1;
- }
+ if (!strcasecmp(line, name))
+ {
+ /*
+ * Found the service, see if it is set to "-NO-"...
+ */
- if (info && (!p->info || strcmp(p->info, info)))
- {
- cupsdSetString(&p->info, info);
- update = 1;
- }
+ if (!strncasecmp(ptr, "-NO-", 4))
+ state = 0;
+ break;
+ }
+ }
- if (!make_model || !make_model[0])
- {
- if (type & CUPS_PRINTER_CLASS)
- snprintf(local_make_model, sizeof(local_make_model),
- "Remote Class on %s", host);
- else
- snprintf(local_make_model, sizeof(local_make_model),
- "Remote Printer on %s", host);
+ cupsFileClose(fp);
}
- else
- snprintf(local_make_model, sizeof(local_make_model),
- "%s on %s", make_model, host);
- if (!p->make_model || strcmp(p->make_model, local_make_model))
- {
- cupsdSetString(&p->make_model, local_make_model);
- update = 1;
- }
+ return (state);
+}
+#endif /* __APPLE__ */
- if (type & CUPS_PRINTER_DELETE)
- {
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "%s \'%s\' deleted by directory services.",
- (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name);
- cupsdExpireSubscriptions(p, NULL);
-
- cupsdDeletePrinter(p, 1);
- cupsdUpdateImplicitClasses();
- }
- else if (update)
- {
- cupsdSetPrinterAttrs(p);
- cupsdUpdateImplicitClasses();
- }
+/*
+ * '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 */
+{
+ char scheme[32], /* Scheme portion of URI */
+ username[HTTP_MAX_URI]; /* Username portion of URI */
+ int port; /* Port portion of URI */
+ cupsd_netif_t *iface; /* Network interface */
+
/*
- * See if we have a default printer... If not, make the first printer the
- * default.
+ * Pull the URI apart to see if this is a local or remote printer...
*/
- if (DefaultPrinter == NULL && Printers != NULL)
- DefaultPrinter = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
+ username, sizeof(username), host, hostlen, &port,
+ resource, resourcelen) < HTTP_URI_OK)
+ return (-1);
+
+ DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName));
/*
- * Do auto-classing if needed...
+ * Check for local server addresses...
*/
- cupsdProcessImplicitClasses();
+ if (!strcasecmp(host, ServerName) && port == LocalPort)
+ return (1);
+
+ cupsdNetIFUpdate();
+
+ for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
+ iface;
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
+ if (!strcasecmp(host, iface->hostname) && port == iface->port)
+ return (1);
/*
- * Update the printcap file...
+ * If we get here, the printer is remote...
*/
- cupsdWritePrintcap();
+ return (0);
}
/*
- * 'cupsdProcessImplicitClasses()' - Create/update implicit classes as needed.
+ * 'process_browse_data()' - Process new browse data.
*/
-void
-cupsdProcessImplicitClasses(void)
+static void
+process_browse_data(
+ const char *uri, /* I - URI of printer/class */
+ const char *host, /* I - Hostname */
+ const char *resource, /* I - Resource path */
+ cups_ptype_t type, /* I - Printer type */
+ ipp_pstate_t state, /* I - Printer state */
+ const char *location, /* I - Printer location */
+ const char *info, /* I - Printer information */
+ const char *make_model, /* I - Printer make and model */
+ int num_attrs, /* I - Number of attributes */
+ cups_option_t *attrs) /* I - Attributes */
{
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 */
+ char finaluri[HTTP_MAX_URI], /* Final URI for printer */
+ name[IPP_MAX_NAME], /* Name of printer */
+ newname[IPP_MAX_NAME], /* New name of printer */
+ *hptr, /* Pointer into hostname */
+ *sptr; /* Pointer into ServerName */
+ char local_make_model[IPP_MAX_NAME];
+ /* Local make and model */
+ cupsd_printer_t *p; /* Printer information */
+ const char *ipp_options, /* ipp-options value */
+ *lease_duration; /* lease-duration value */
+
+
+ /*
+ * Determine if the URI contains any illegal characters in it...
+ */
+
+ if (strncmp(uri, "ipp://", 6) || !host[0] ||
+ (strncmp(resource, "/printers/", 10) &&
+ strncmp(resource, "/classes/", 9)))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "process_browse_data: Bad printer URI in browse data: %s",
+ uri);
+ return;
+ }
+
+ if (strchr(resource, '?') ||
+ (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) ||
+ (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/')))
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "process_browse_data: Bad resource in browse data: %s",
+ resource);
+ return;
+ }
+
+ /*
+ * OK, this isn't a local printer; add any remote options...
+ */
+
+ ipp_options = cupsGetOption("ipp-options", num_attrs, attrs);
+
+ if (BrowseRemoteOptions)
+ {
+ if (BrowseRemoteOptions[0] == '?')
+ {
+ /*
+ * Override server-supplied options...
+ */
+
+ snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions);
+ }
+ else if (ipp_options)
+ {
+ /*
+ * Combine the server and local options...
+ */
+
+ snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options,
+ BrowseRemoteOptions);
+ }
+ else
+ {
+ /*
+ * Just use the local options...
+ */
+ snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions);
+ }
+
+ uri = finaluri;
+ }
+ else if (ipp_options)
+ {
+ /*
+ * Just use the server-supplied options...
+ */
- if (!ImplicitClasses || !Printers)
- return;
+ snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
+ uri = finaluri;
+ }
/*
- * Loop through all available printers and create classes as needed...
+ * See if we already have it listed in the Printers list, and add it if not...
*/
- 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))
+ type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
+ type &= ~CUPS_PRINTER_IMPLICIT;
+ update = 0;
+ hptr = strchr(host, '.');
+ sptr = strchr(ServerName, '.');
+
+ if (!ServerNameIsIP && sptr != NULL && hptr != NULL)
{
/*
- * Skip implicit classes...
+ * Strip the common domain name components...
*/
- if (p->type & CUPS_PRINTER_IMPLICIT)
+ while (hptr != NULL)
{
- len = 0;
- continue;
+ if (!strcasecmp(hptr, sptr))
+ {
+ *hptr = '\0';
+ break;
+ }
+ else
+ hptr = strchr(hptr + 1, '.');
}
+ }
+ if (type & CUPS_PRINTER_CLASS)
+ {
/*
- * If len == 0, get the length of this printer name up to the "@"
- * sign (if any).
+ * Remote destination is a class...
*/
- cupsArraySave(Printers);
+ if (!strncmp(resource, "/classes/", 9))
+ snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
+ else
+ return;
- if (len > 0 &&
- strncasecmp(p->name, name + offset, len) == 0 &&
- (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 (hptr && !*hptr)
+ *hptr = '.'; /* Resource FQDN */
- if (pclass && strcasecmp(pclass->name, name))
+ if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
+ {
+ if ((p = cupsdFindDest(resource + 9)) != NULL)
{
- if (update)
- cupsdSetPrinterAttrs(pclass);
+ if (p->hostname && strcasecmp(p->hostname, host))
+ {
+ /*
+ * Nope, this isn't the same host; if the hostname isn't the local host,
+ * add it to the other class and then find a class using the full host
+ * name...
+ */
- update = 0;
- pclass = NULL;
- }
+ if (p->type & CUPS_PRINTER_REMOTE)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Renamed remote class \"%s\" to \"%s@%s\"...",
+ p->name, p->name, p->hostname);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
+ "Class \'%s\' deleted by directory services.",
+ p->name);
- if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
+ snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
+ cupsdRenamePrinter(p, newname);
+
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Class \'%s\' added by directory services.",
+ p->name);
+ }
+
+ p = NULL;
+ }
+ else if (!p->hostname)
+ {
+ /*
+ * Hostname not set, so this must be a cached remote printer
+ * that was created for a pending print job...
+ */
+
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+ }
+ }
+ else
{
/*
- * Need to add the class...
+ * Use the short name for this shared class.
*/
- pclass = cupsdAddPrinter(name);
- cupsArrayAdd(ImplicitPrinters, pclass);
+ strlcpy(name, resource + 9, sizeof(name));
+ }
+ }
+ else if (p && !p->hostname)
+ {
+ /*
+ * Hostname not set, so this must be a cached remote printer
+ * that was created for a pending print job...
+ */
- pclass->type |= CUPS_PRINTER_IMPLICIT;
- pclass->accepting = 1;
- pclass->state = IPP_PRINTER_IDLE;
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+ }
- cupsdSetString(&pclass->location, p->location);
- cupsdSetString(&pclass->info, p->info);
+ if (!p)
+ {
+ /*
+ * Class doesn't exist; add it...
+ */
- update = 1;
+ p = cupsdAddClass(name);
- cupsdLogMessage(CUPSD_LOG_INFO, "Added implicit class \"%s\"...",
- name);
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote class \"%s\"...", name);
- if (first != NULL)
- {
- for (i = 0; i < pclass->num_printers; i ++)
- if (pclass->printers[i] == first)
- break;
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Class \'%s\' added by directory services.", name);
- if (i >= pclass->num_printers)
- {
- first->in_implicit_class = 1;
- cupsdAddPrinterToClass(pclass, first);
- }
+ /*
+ * Force the URI to point to the real server...
+ */
- first = NULL;
- }
+ p->type = type & ~CUPS_PRINTER_REJECTING;
+ p->accepting = 1;
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ cupsdSetString(&p->hostname, host);
- for (i = 0; i < pclass->num_printers; i ++)
- if (pclass->printers[i] == p)
- break;
+ update = 1;
- if (i >= pclass->num_printers)
- {
- p->in_implicit_class = 1;
- cupsdAddPrinterToClass(pclass, p);
- update = 1;
- }
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
}
- else
- {
- /*
- * First time around; just get name length and mark it as first
- * in the list...
- */
+ }
+ else
+ {
+ /*
+ * Remote destination is a printer...
+ */
- if ((hptr = strchr(p->name, '@')) != NULL)
- len = hptr - p->name;
- else
- len = strlen(p->name);
+ if (!strncmp(resource, "/printers/", 10))
+ snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
+ else
+ return;
- strncpy(name, p->name, len);
- name[len] = '\0';
- offset = 0;
+ if (hptr && !*hptr)
+ *hptr = '.'; /* Resource FQDN */
- if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
- !(first->type & CUPS_PRINTER_IMPLICIT))
+ if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
+ {
+ if ((p = cupsdFindDest(resource + 10)) != NULL)
{
- /*
- * 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))
+ if (p->hostname && strcasecmp(p->hostname, host))
{
/*
- * Add "Any" to the class name...
+ * Nope, this isn't the same host; if the hostname isn't the local host,
+ * add it to the other printer and then find a printer using the full host
+ * name...
*/
- strcpy(name, "Any");
- strncpy(name + 3, p->name, len);
- name[len + 3] = '\0';
- offset = 3;
+ if (p->type & CUPS_PRINTER_REMOTE)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Renamed remote printer \"%s\" to \"%s@%s\"...",
+ p->name, p->name, p->hostname);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
+ "Printer \'%s\' deleted by directory services.",
+ p->name);
+
+ snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
+ cupsdRenamePrinter(p, newname);
+
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Printer \'%s\' added by directory services.",
+ p->name);
+ }
+
+ p = NULL;
}
- else
+ else if (!p->hostname)
{
/*
- * Don't create an implicit class if we have a local printer
- * with the same name...
+ * Hostname not set, so this must be a cached remote printer
+ * that was created for a pending print job...
*/
- len = 0;
- cupsArrayRestore(Printers);
- continue;
- }
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+ }
}
+ else
+ {
+ /*
+ * Use the short name for this shared printer.
+ */
- first = p;
+ strlcpy(name, resource + 10, sizeof(name));
+ }
}
+ else if (p && !p->hostname)
+ {
+ /*
+ * Hostname not set, so this must be a cached remote printer
+ * that was created for a pending print job...
+ */
- cupsArrayRestore(Printers);
- }
-
- /*
- * Update the last printer class as needed...
- */
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+ }
- if (pclass && update)
- cupsdSetPrinterAttrs(pclass);
-}
+ if (!p)
+ {
+ /*
+ * Printer doesn't exist; add it...
+ */
+ p = cupsdAddPrinter(name);
-/*
- * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
- */
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Printer \'%s\' added by directory services.", name);
-void
-cupsdSaveRemoteCache(void)
-{
- int i; /* Looping var */
- cups_file_t *fp; /* printers.conf file */
- char temp[1024]; /* Temporary string */
- cupsd_printer_t *printer; /* Current printer class */
- time_t curtime; /* Current time */
- struct tm *curdate; /* Current date */
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote printer \"%s\"...", name);
+ /*
+ * Force the URI to point to the real server...
+ */
- /*
- * Create the remote.cache file...
- */
+ p->type = type & ~CUPS_PRINTER_REJECTING;
+ p->accepting = 1;
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
- snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir);
+ update = 1;
- if ((fp = cupsFileOpen(temp, "w")) == NULL)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to save remote.cache - %s", strerror(errno));
- return;
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ }
}
- else
- cupsdLogMessage(CUPSD_LOG_INFO, "Saving remote.cache...");
/*
- * Restrict access to the file...
+ * Update the state...
*/
- fchown(cupsFileNumber(fp), getuid(), Group);
- fchmod(cupsFileNumber(fp), ConfigFilePerm);
+ p->state = state;
+ p->browse_time = time(NULL);
- /*
- * Write a small header to the file...
- */
+ if ((lease_duration = cupsGetOption("lease-duration", num_attrs,
+ attrs)) != NULL)
+ {
+ /*
+ * Grab the lease-duration for the browse data; anything less then 1
+ * second or more than 1 week gets the default BrowseTimeout...
+ */
- curtime = time(NULL);
- curdate = localtime(&curtime);
- strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
+ i = atoi(lease_duration);
+ if (i < 1 || i > 604800)
+ i = BrowseTimeout;
- cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n");
- cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
+ p->browse_expire = p->browse_time + i;
+ }
+ else
+ p->browse_expire = p->browse_time + BrowseTimeout;
- /*
- * Write each local printer known to the system...
- */
+ if (type & CUPS_PRINTER_REJECTING)
+ {
+ type &= ~CUPS_PRINTER_REJECTING;
- for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
- printer;
- printer = (cupsd_printer_t *)cupsArrayNext(Printers))
+ if (p->accepting)
+ {
+ update = 1;
+ p->accepting = 0;
+ }
+ }
+ else if (!p->accepting)
{
- /*
- * Skip local destinations...
- */
+ update = 1;
+ p->accepting = 1;
+ }
- if (!(printer->type & CUPS_PRINTER_REMOTE))
- continue;
+ if (p->type != type)
+ {
+ p->type = type;
+ update = 1;
+ }
- /*
- * Write printers as needed...
- */
+ if (location && (!p->location || strcmp(p->location, location)))
+ {
+ cupsdSetString(&p->location, location);
+ update = 1;
+ }
- if (printer == DefaultPrinter)
- cupsFilePuts(fp, "<Default");
- else
- cupsFilePutChar(fp, '<');
+ if (info && (!p->info || strcmp(p->info, info)))
+ {
+ cupsdSetString(&p->info, info);
+ update = 1;
- if (printer->type & CUPS_PRINTER_CLASS)
- cupsFilePrintf(fp, "Class %s>\n", printer->name);
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ }
+
+ if (!make_model || !make_model[0])
+ {
+ if (type & CUPS_PRINTER_CLASS)
+ snprintf(local_make_model, sizeof(local_make_model),
+ "Remote Class on %s", host);
else
- cupsFilePrintf(fp, "Printer %s>\n", printer->name);
+ snprintf(local_make_model, sizeof(local_make_model),
+ "Remote Printer on %s", host);
+ }
+ else
+ snprintf(local_make_model, sizeof(local_make_model),
+ "%s on %s", make_model, host);
- cupsFilePrintf(fp, "Type %d\n", printer->type);
+ if (!p->make_model || strcmp(p->make_model, local_make_model))
+ {
+ cupsdSetString(&p->make_model, local_make_model);
+ update = 1;
+ }
- cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_time);
+ if (p->num_options)
+ {
+ if (!update && !(type & CUPS_PRINTER_DELETE))
+ {
+ /*
+ * See if we need to update the attributes...
+ */
- if (printer->info)
- cupsFilePrintf(fp, "Info %s\n", printer->info);
+ if (p->num_options != num_attrs)
+ update = 1;
+ else
+ {
+ for (i = 0; i < num_attrs; i ++)
+ if (strcmp(attrs[i].name, p->options[i].name) ||
+ (!attrs[i].value != !p->options[i].value) ||
+ (attrs[i].value && strcmp(attrs[i].value, p->options[i].value)))
+ {
+ update = 1;
+ break;
+ }
+ }
+ }
- if (printer->make_model)
- cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model);
+ /*
+ * Free the old options...
+ */
- if (printer->location)
- cupsFilePrintf(fp, "Location %s\n", printer->location);
+ cupsFreeOptions(p->num_options, p->options);
+ }
- if (printer->device_uri)
- cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
+ p->num_options = num_attrs;
+ p->options = attrs;
- if (printer->state == IPP_PRINTER_STOPPED)
- {
- cupsFilePuts(fp, "State Stopped\n");
- cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
- }
- else
- cupsFilePuts(fp, "State Idle\n");
+ if (type & CUPS_PRINTER_DELETE)
+ {
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
+ "%s \'%s\' deleted by directory services.",
+ (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name);
- if (printer->accepting)
- cupsFilePuts(fp, "Accepting Yes\n");
- else
- cupsFilePuts(fp, "Accepting No\n");
+ cupsdExpireSubscriptions(p, NULL);
+
+ cupsdDeletePrinter(p, 1);
+ cupsdUpdateImplicitClasses();
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ }
+ else if (update)
+ {
+ cupsdSetPrinterAttrs(p);
+ cupsdUpdateImplicitClasses();
+ }
- cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
- printer->job_sheets[1]);
+ /*
+ * See if we have a default printer... If not, make the first network
+ * default printer the default.
+ */
- for (i = 0; i < printer->num_users; i ++)
- cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
- printer->users[i]);
+ if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault)
+ {
+ /*
+ * Find the first network default printer and use it...
+ */
- if (printer->type & CUPS_PRINTER_CLASS)
- cupsFilePuts(fp, "</Class>\n");
- else
- cupsFilePuts(fp, "</Printer>\n");
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ if (p->type & CUPS_PRINTER_DEFAULT)
+ {
+ DefaultPrinter = p;
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ break;
+ }
}
- cupsFileClose(fp);
+ /*
+ * Do auto-classing if needed...
+ */
+
+ process_implicit_classes();
}
/*
- * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
+ * 'process_implicit_classes()' - Create/update implicit classes as needed.
*/
-void
-cupsdSendBrowseDelete(
- cupsd_printer_t *p) /* I - Printer to delete */
+static void
+process_implicit_classes(void)
{
- /*
- * Only announce if browsing is enabled...
- */
+ 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 */
- if (!Browsing || !p->shared)
+
+ if (!ImplicitClasses || !Printers)
return;
/*
- * First mark the printer for deletion...
+ * Loop through all available printers and create classes as needed...
*/
- p->type |= CUPS_PRINTER_DELETE;
+ 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...
+ */
- /*
- * Announce the deletion...
- */
+ if (p->type & CUPS_PRINTER_IMPLICIT)
+ {
+ len = 0;
+ continue;
+ }
- if (BrowseLocalProtocols & BROWSE_CUPS)
- cupsdSendCUPSBrowse(p);
-#ifdef HAVE_LIBSLP
- if (BrowseLocalProtocols & BROWSE_SLP)
- slp_dereg_printer(p);
-#endif /* HAVE_LIBSLP */
-}
+ /*
+ * If len == 0, get the length of this printer name up to the "@"
+ * sign (if any).
+ */
+ cupsArraySave(Printers);
-/*
- * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
- */
+ 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 (pclass && strcasecmp(pclass->name, name))
+ {
+ if (update)
+ cupsdSetPrinterAttrs(pclass);
+
+ update = 0;
+ pclass = NULL;
+ }
-void
-cupsdSendBrowseList(void)
-{
- int count; /* Number of dests to update */
- cupsd_printer_t *p; /* Current printer */
- time_t ut, /* Minimum update time */
- to; /* Timeout time */
+ if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
+ {
+ /*
+ * Need to add the class...
+ */
+ pclass = cupsdAddPrinter(name);
+ cupsArrayAdd(ImplicitPrinters, pclass);
- if (!Browsing || !BrowseLocalProtocols || !Printers)
- return;
+ pclass->type |= CUPS_PRINTER_IMPLICIT;
+ pclass->accepting = 1;
+ pclass->state = IPP_PRINTER_IDLE;
- /*
- * Compute the update and timeout times...
- */
+ cupsdSetString(&pclass->location, p->location);
+ cupsdSetString(&pclass->info, p->info);
- ut = time(NULL) - BrowseInterval;
- to = time(NULL) - BrowseTimeout;
+ cupsdSetString(&pclass->job_sheets[0], p->job_sheets[0]);
+ cupsdSetString(&pclass->job_sheets[1], p->job_sheets[1]);
- /*
- * Figure out how many printers need an update...
- */
+ update = 1;
- if (BrowseInterval > 0)
- {
- int max_count; /* Maximum number to update */
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...",
+ name);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Implicit class \'%s\' added by directory services.",
+ name);
+ }
- /*
- * Throttle the number of printers we'll be updating this time
- * around based on the number of queues that need updating and
- * the maximum number of queues to update each second...
- */
+ if (first != NULL)
+ {
+ for (i = 0; i < pclass->num_printers; i ++)
+ if (pclass->printers[i] == first)
+ break;
- max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1;
+ if (i >= pclass->num_printers)
+ {
+ first->in_implicit_class = 1;
+ cupsdAddPrinterToClass(pclass, first);
+ }
- for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers);
- count < max_count && p != NULL;
- p = (cupsd_printer_t *)cupsArrayNext(Printers))
- if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
- p->shared && p->browse_time < ut)
- count ++;
+ first = NULL;
+ }
- /*
- * Loop through all of the printers and send local updates as needed...
- */
+ for (i = 0; i < pclass->num_printers; i ++)
+ if (pclass->printers[i] == p)
+ break;
- if (BrowseNext)
- p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext);
+ if (i >= pclass->num_printers)
+ {
+ p->in_implicit_class = 1;
+ cupsdAddPrinterToClass(pclass, p);
+ update = 1;
+ }
+ }
else
- p = (cupsd_printer_t *)cupsArrayFirst(Printers);
-
- for (;
- count > 0;
- p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
/*
- * Check for wraparound...
+ * First time around; just get name length and mark it as first
+ * in the list...
*/
- if (!p)
- p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ if ((hptr = strchr(p->name, '@')) != NULL)
+ len = hptr - p->name;
+ else
+ len = strlen(p->name);
- if (!p)
- break;
- else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
- !p->shared)
- continue;
- else if (p->browse_time < ut)
+ strncpy(name, p->name, len);
+ name[len] = '\0';
+ offset = 0;
+
+ if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
+ !(first->type & CUPS_PRINTER_IMPLICIT))
{
/*
- * Need to send an update...
+ * Can't use same name as a local printer; add "Any" to the
+ * front of the name, unless we have explicitly disabled
+ * the "ImplicitAnyClasses"...
*/
- count --;
-
- p->browse_time = time(NULL);
+ if (ImplicitAnyClasses && len < (sizeof(name) - 4))
+ {
+ /*
+ * Add "Any" to the class name...
+ */
- if (BrowseLocalProtocols & BROWSE_CUPS)
- cupsdSendCUPSBrowse(p);
+ 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...
+ */
-#ifdef HAVE_LIBSLP
- if (BrowseLocalProtocols & BROWSE_SLP)
- cupsdSendSLPBrowse(p);
-#endif /* HAVE_LIBSLP */
+ len = 0;
+ cupsArrayRestore(Printers);
+ continue;
+ }
}
- }
- /*
- * Save where we left off so that all printers get updated...
- */
+ first = p;
+ }
- BrowseNext = p;
+ cupsArrayRestore(Printers);
}
/*
- * Loop through all of the printers and send local updates as needed...
+ * Update the last printer class as needed...
*/
- for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
- 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->browse_time < to)
- {
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "%s \'%s\' deleted by directory services (timeout).",
- (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
- p->name);
-
- cupsdLogMessage(CUPSD_LOG_INFO,
- "Remote destination \"%s\" has timed out; deleting it...",
- p->name);
-
- cupsArraySave(Printers);
- cupsdDeletePrinter(p, 1);
- cupsArrayRestore(Printers);
- }
- }
- }
+ if (pclass && update)
+ cupsdSetPrinterAttrs(pclass);
}
/*
- * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
+ * 'send_cups_browse()' - Send new browsing information using the CUPS
+ * protocol.
*/
-void
-cupsdSendCUPSBrowse(cupsd_printer_t *p) /* I - Printer to send */
+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 */
- char uri[1024]; /* Printer URI */
- char options[1024]; /* Browse local options */
+ 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 */
if (!p->accepting)
type |= CUPS_PRINTER_REJECTING;
+ if (p == DefaultPrinter)
+ type |= CUPS_PRINTER_DEFAULT;
+
/*
- * Initialize the browse options...
+ * Remove quotes from printer-info, printer-location, and
+ * printer-make-and-model attributes...
*/
- if (BrowseLocalOptions)
- snprintf(options, sizeof(options), " ipp-options=%s", BrowseLocalOptions);
+ 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
- options[0] = '\0';
+ strlcpy(make_model, "Local System V Printer", sizeof(make_model));
/*
* Send a packet to each browse address...
* Send the browse packet to one or more interfaces...
*/
- if (strcmp(b->iface, "*") == 0)
+ if (!strcmp(b->iface, "*"))
{
/*
* Send to all local interfaces...
cupsdNetIFUpdate();
- for (iface = NetIFList; iface != NULL; iface = iface->next)
+ for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
+ iface;
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
{
/*
* Only send to local, IPv4 interfaces...
iface->address.addr.sa_family != AF_INET)
continue;
- httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, iface->hostname,
- iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
+ 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, p->location ? p->location : "",
- p->info ? p->info : "",
- p->make_model ? p->make_model : "Unknown", options);
+ 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);
sendto(BrowseSocket, packet, bytes, 0,
(struct sockaddr *)&(iface->broadcast),
- sizeof(struct sockaddr_in));
+ httpAddrLength(&(iface->broadcast)));
}
}
else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
*/
while (iface)
- if (strcasecmp(b->iface, iface->name))
+ if (strcmp(b->iface, iface->name))
{
iface = NULL;
break;
else if (iface->address.addr.sa_family == AF_INET && iface->port)
break;
else
- iface = iface->next;
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
if (iface)
{
- httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, iface->hostname,
- iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
+ 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, p->location ? p->location : "",
- p->info ? p->info : "",
- p->make_model ? p->make_model : "Unknown", options);
+ 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);
sendto(BrowseSocket, packet, bytes, 0,
(struct sockaddr *)&(iface->broadcast),
- sizeof(struct sockaddr_in));
+ httpAddrLength(&(iface->broadcast)));
}
}
}
* the default server name...
*/
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
- type, p->state, p->uri,
- p->location ? p->location : "",
- p->info ? p->info : "",
- p->make_model ? p->make_model : "Unknown", options);
+ 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,
if (sendto(BrowseSocket, packet, bytes, 0,
(struct sockaddr *)&(b->to),
- sizeof(struct sockaddr_in)) <= 0)
+ httpAddrLength(&(b->to))) <= 0)
{
/*
* Unable to send browse packet, so remove this address from the
*/
cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdSendBrowseList: sendto failed for browser %d - %s.",
- b - Browsers + 1, strerror(errno));
+ "cupsdSendBrowseList: sendto failed for browser "
+ "%d - %s.",
+ (int)(b - Browsers + 1), strerror(errno));
if (i > 1)
- memcpy(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
+ 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", 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
/*
- * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP.
+ * 'send_slp_browse()' - Register the specified printer with SLP.
*/
-void
-cupsdSendSLPBrowse(cupsd_printer_t *p) /* I - Printer to register */
+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 */
SLPError error; /* SLP error, if any */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendSLPBrowse(%p = \"%s\")", p,
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p,
p->name);
/*
"(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),"
"(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, 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);
-}
-#endif /* HAVE_LIBSLP */
-
-
-/*
- * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
- */
-
-void
-cupsdStartBrowsing(void)
-{
- int val; /* Socket option value */
- struct sockaddr_in addr; /* Broadcast address */
-
-
- BrowseNext = NULL;
-
- if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
- return;
-
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
- {
- /*
- * Create the broadcast socket...
- */
-
- if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to create broadcast socket - %s.",
- strerror(errno));
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
+ "(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" : "");
- /*
- * Set the "broadcast" flag...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
- val = 1;
- if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
- strerror(errno));
+ /*
+ * Register the printer with the SLP server...
+ */
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
+ error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
+ SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
- BrowseSocket = -1;
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
+ if (error != SLP_OK)
+ cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
+ error);
+}
- /*
- * Bind the socket to browse port...
- */
- memset(&addr, 0, sizeof(addr));
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(BrowsePort);
+/*
+ * 'slp_attr_callback()' - SLP attribute callback
+ */
- if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to bind broadcast socket - %s.",
- strerror(errno));
+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 */
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
- BrowseSocket = -1;
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
+ (void)hslp; /* anti-compiler-warning-code */
- /*
- * Close the socket on exec...
- */
+ /*
+ * Bail if there was an error
+ */
- fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
+ if (errcode != SLP_OK)
+ return (SLP_TRUE);
- /*
- * Finally, add the socket to the input selection set...
- */
+ /*
+ * Parse the attrlist to obtain things needed to build CUPS browse packet
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStartBrowsing: Adding fd %d to InputSet...",
- BrowseSocket);
+ memset(p, 0, sizeof(cupsd_printer_t));
- FD_SET(BrowseSocket, InputSet);
- }
+ 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
- BrowseSocket = -1;
-
-#ifdef HAVE_LIBSLP
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
- {
- /*
- * Open SLP handle...
- */
+ p->type = CUPS_PRINTER_REMOTE;
- if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to open an SLP handle; disabling SLP browsing!");
- BrowseLocalProtocols &= ~BROWSE_SLP;
- BrowseRemoteProtocols &= ~BROWSE_SLP;
- }
+ cupsdClearString(&tmp);
- BrowseSLPRefresh = 0;
- }
-#endif /* HAVE_LIBSLP */
+ return (SLP_TRUE);
}
/*
- * 'cupsdStartPolling()' - Start polling servers as needed.
+ * 'slp_dereg_printer()' - SLPDereg() the specified printer
*/
-void
-cupsdStartPolling(void)
+static void
+slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */
{
- 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 */
- int statusfds[2]; /* Status pipe */
- char *argv[6]; /* Arguments */
- char *envp[100]; /* Environment */
+ char srvurl[HTTP_MAX_URI]; /* Printer service URI */
- /*
- * Don't do anything if we aren't polling...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
- if (NumPolled == 0)
+ if (!(p->type & CUPS_PRINTER_REMOTE))
{
- PollPipe = -1;
- PollStatusBuffer = NULL;
- return;
- }
+ /*
+ * Make the SLP service URL that conforms to the IANA
+ * 'printer:' template.
+ */
- /*
- * Setup string arguments for polld, port and interval options.
- */
+ snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
- snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
+ /*
+ * Deregister the printer...
+ */
- sprintf(bport, "%d", BrowsePort);
+ SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
+ }
+}
- if (BrowseInterval)
- sprintf(interval, "%d", BrowseInterval);
- else
- strcpy(interval, "30");
- argv[0] = "cups-polld";
- argv[2] = sport;
- argv[3] = interval;
- argv[4] = bport;
- argv[5] = NULL;
+/*
+ * 'slp_get_attr()' - Get an attribute from an SLP registration.
+ */
- cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+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; /* ... */
- /*
- * Create a pipe that receives the status messages from each
- * polling daemon...
- */
- if (cupsdOpenPipe(statusfds))
+ cupsdClearString(valbuf);
+
+ if ((ptr1 = strstr(attrlist, tag)) != NULL)
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to create polling status pipes - %s.",
- strerror(errno));
- PollPipe = -1;
- PollStatusBuffer = NULL;
- return;
- }
+ ptr1 += strlen(tag);
- PollPipe = statusfds[0];
- PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]");
+ if ((ptr2 = strchr(ptr1,')')) != NULL)
+ {
+ /*
+ * Copy the value...
+ */
- /*
- * Run each polling daemon, redirecting stderr to the polling pipe...
- */
+ *valbuf = calloc(ptr2 - ptr1 + 1, 1);
+ strncpy(*valbuf, ptr1, ptr2 - ptr1);
- for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
- {
- sprintf(sport, "%d", pollp->port);
+ /*
+ * Dequote the value...
+ */
- argv[1] = pollp->hostname;
+ for (ptr1 = *valbuf; *ptr1; ptr1 ++)
+ if (*ptr1 == '\\' && ptr1[1])
+ _cups_strcpy(ptr1, ptr1 + 1);
- if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1,
- 0, &(pollp->pid)) < 0)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartPolling: Unable to fork polling daemon - %s",
- strerror(errno));
- pollp->pid = 0;
- break;
+ return (0);
}
- else
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
- pollp->hostname, pollp->port, pollp->pid);
}
- close(statusfds[1]);
+ return (-1);
+}
- /*
- * Finally, add the pipe to the input selection set...
- */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe);
+/*
+ * '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;
- FD_SET(PollPipe, InputSet);
+ return;
}
/*
- * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
+ * 'slp_url_callback()' - SLP service url callback
*/
-void
-cupsdStopBrowsing(void)
+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 */
{
- if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
- return;
+ slpsrvurl_t *s, /* New service entry */
+ **head; /* Pointer to head of entry */
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
- {
- /*
- * Close the socket and remove it from the input selection set.
- */
- if (BrowseSocket >= 0)
- {
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
+ /*
+ * Let the compiler know we won't be using these vars...
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStopBrowsing: Removing fd %d from InputSet...",
- BrowseSocket);
+ (void)hslp;
+ (void)lifetime;
- FD_CLR(BrowseSocket, InputSet);
- BrowseSocket = -1;
- }
- }
+ /*
+ * Bail if there was an error
+ */
-#ifdef HAVE_LIBSLP
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
- {
- /*
- * Close SLP handle...
- */
+ if (errcode != SLP_OK)
+ return (SLP_TRUE);
- SLPClose(BrowseSLPHandle);
- }
-#endif /* HAVE_LIBSLP */
-}
+ /*
+ * Grab the head of the list...
+ */
+ head = (slpsrvurl_t**)cookie;
-/*
- * 'cupsdStopPolling()' - Stop polling servers as needed.
- */
+ /*
+ * Allocate a *temporary* slpsrvurl_t to hold this entry.
+ */
-void
-cupsdStopPolling(void)
-{
- int i; /* Looping var */
- cupsd_dirsvc_poll_t *pollp; /* Current polling server */
+ if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
+ return (SLP_FALSE);
+ /*
+ * Copy the SLP service URL...
+ */
- if (PollPipe >= 0)
- {
- cupsdStatBufDelete(PollStatusBuffer);
- close(PollPipe);
+ strlcpy(s->url, srvurl, sizeof(s->url));
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStopPolling: removing fd %d from InputSet.", PollPipe);
- FD_CLR(PollPipe, InputSet);
+ /*
+ * Link the SLP service URL into the head of the list
+ */
- PollPipe = -1;
- PollStatusBuffer = NULL;
- }
+ if (*head)
+ s->next = *head;
- for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
- if (pollp->pid)
- cupsdEndProcess(pollp->pid, 0);
+ *head = s;
+
+ return (SLP_TRUE);
}
+#endif /* HAVE_LIBSLP */
/*
- * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
+ * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
*/
-void
-cupsdUpdateCUPSBrowse(void)
+static void
+update_cups_browse(void)
{
int i; /* Looping var */
int auth; /* Authorization status */
unsigned type; /* Printer type */
unsigned state; /* Printer state */
char uri[HTTP_MAX_URI], /* Printer URI */
- method[HTTP_MAX_URI], /* Method portion of URI */
- username[HTTP_MAX_URI], /* Username portion of 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 port; /* Port portion of URI */
- cupsd_netif_t *iface; /* Network interface */
int num_attrs; /* Number of attributes */
cups_option_t *attrs; /* Attributes */
{
cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
strerror(errno));
- cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
+ cupsdLogMessage(CUPSD_LOG_ERROR, "CUPS browsing turned off.");
+
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
+
+ cupsdRemoveSelect(BrowseSocket);
+ BrowseSocket = -1;
- cupsdStopBrowsing();
- Browsing = 0;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
}
return;
packet[bytes] = '\0';
+ /*
+ * If we're about to sleep, ignore incoming browse packets.
+ */
+
+ if (Sleeping)
+ return;
+
/*
* Figure out where it came from...
*/
* Access from localhost (127.0.0.1) is always allowed...
*/
- auth = AUTH_ALLOW;
+ auth = CUPSD_AUTH_ALLOW;
}
else
{
switch (BrowseACL->order_type)
{
default :
- auth = AUTH_DENY; /* anti-compiler-warning-code */
+ auth = CUPSD_AUTH_DENY; /* anti-compiler-warning-code */
break;
- case AUTH_ALLOW : /* Order Deny,Allow */
- auth = AUTH_ALLOW;
+ case CUPSD_AUTH_ALLOW : /* Order Deny,Allow */
+ auth = CUPSD_AUTH_ALLOW;
if (cupsdCheckAuth(address, srcname, len,
BrowseACL->num_deny, BrowseACL->deny))
- auth = AUTH_DENY;
+ auth = CUPSD_AUTH_DENY;
if (cupsdCheckAuth(address, srcname, len,
BrowseACL->num_allow, BrowseACL->allow))
- auth = AUTH_ALLOW;
+ auth = CUPSD_AUTH_ALLOW;
break;
- case AUTH_DENY : /* Order Allow,Deny */
- auth = AUTH_DENY;
+ case CUPSD_AUTH_DENY : /* Order Allow,Deny */
+ auth = CUPSD_AUTH_DENY;
if (cupsdCheckAuth(address, srcname, len,
BrowseACL->num_allow, BrowseACL->allow))
- auth = AUTH_ALLOW;
+ auth = CUPSD_AUTH_ALLOW;
if (cupsdCheckAuth(address, srcname, len,
BrowseACL->num_deny, BrowseACL->deny))
- auth = AUTH_DENY;
+ auth = CUPSD_AUTH_DENY;
break;
}
}
}
else
- auth = AUTH_ALLOW;
+ auth = CUPSD_AUTH_ALLOW;
- if (auth == AUTH_DENY)
+ if (auth == CUPSD_AUTH_DENY)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
- "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes,
+ "update_cups_browse: Refused %d bytes from %s", bytes,
srcname);
return;
}
cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes,
+ "update_cups_browse: (%d bytes from %s) %s", bytes,
srcname, packet);
/*
if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
{
cupsdLogMessage(CUPSD_LOG_WARN,
- "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet);
+ "update_cups_browse: Garbled browse packet - %s", packet);
return;
}
*/
for (i = 0, pptr ++;
- i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- location[i] = *pptr;
-
- if (i)
- location[i] = '\0';
-
- if (*pptr == '\"')
- pptr ++;
-
- while (*pptr && isspace(*pptr & 255))
- pptr ++;
-
- if (*pptr == '\"')
- {
- for (i = 0, pptr ++;
- i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- info[i] = *pptr;
-
- info[i] = '\0';
-
- if (*pptr == '\"')
- pptr ++;
-
- 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;
-
- 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));
-
- /*
- * Pull the URI apart to see if this is a local or remote printer...
- */
-
- httpSeparateURI(uri, method, sizeof(method), username, sizeof(username),
- host, sizeof(host), &port, resource, sizeof(resource));
-
- DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName));
-
- /*
- * Check for packets from the local server...
- */
-
- if (!strcasecmp(host, ServerName) && port == LocalPort)
- {
- cupsFreeOptions(num_attrs, attrs);
- return;
- }
-
- cupsdNetIFUpdate();
-
- for (iface = NetIFList; iface != NULL; iface = iface->next)
- if (!strcasecmp(host, iface->hostname) && port == iface->port)
- {
- 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)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
- i + 1, strerror(errno));
- cupsFreeOptions(num_attrs, attrs);
- return;
- }
-
- /*
- * Process the browse data...
- */
-
- cupsdProcessBrowseData(uri, (cups_ptype_t)type, (ipp_pstate_t)state, location,
- info, make_model, num_attrs, attrs);
- cupsFreeOptions(num_attrs, attrs);
-}
-
-
-/*
- * '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 method[HTTP_MAX_URI],
- /* Method portion of URI */
- username[HTTP_MAX_URI],
- /* Username portion of URI */
- host[HTTP_MAX_URI],
- /* Host portion of URI */
- resource[HTTP_MAX_URI];
- /* Resource portion of URI */
- int port; /* Port portion of URI */
-
-
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdUpdateSLPBrowse() Start...");
-
- /*
- * 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;
+ i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ location[i] = *pptr;
- if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
- {
- /*
- * Pull the URI apart to see if this is a local or remote printer...
- */
+ if (i)
+ location[i] = '\0';
- httpSeparateURI(uri, method, sizeof(method), username, sizeof(username),
- host, sizeof(host), &port, resource, sizeof(resource));
+ if (*pptr == '\"')
+ pptr ++;
- if (strcasecmp(host, ServerName) == 0)
- continue;
+ while (*pptr && isspace(*pptr & 255))
+ pptr ++;
- /*
- * OK, at least an IPP printer, see if it is a CUPS printer or
- * class...
- */
+ if (*pptr == '\"')
+ {
+ for (i = 0, pptr ++;
+ i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ info[i] = *pptr;
- if (strstr(uri, "/printers/") != NULL)
- cupsdProcessBrowseData(uri, p.type, IPP_PRINTER_IDLE, p.location,
- p.info, p.make_model, 0, NULL);
- else if (strstr(uri, "/classes/") != NULL)
- cupsdProcessBrowseData(uri, p.type | CUPS_PRINTER_CLASS, IPP_PRINTER_IDLE,
- p.location, p.info, p.make_model, 0, NULL);
- }
+ info[i] = '\0';
- /*
- * Free this listing...
- */
+ if (*pptr == '\"')
+ pptr ++;
- free(s);
- }
+ while (*pptr && isspace(*pptr & 255))
+ pptr ++;
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdUpdateSLPBrowse() End...");
-}
+ if (*pptr == '\"')
+ {
+ for (i = 0, pptr ++;
+ i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ make_model[i] = *pptr;
+ if (*pptr == '\"')
+ pptr ++;
-/*
- * 'slp_attr_callback()' - SLP attribute callback
- */
+ make_model[i] = '\0';
-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;
- cupsd_printer_t *p = (cupsd_printer_t*)cookie;
+ 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));
/*
- * Let the compiler know we won't be using these...
+ * Pull the URI apart to see if this is a local or remote printer...
*/
- (void)hslp;
+ if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
+ {
+ cupsFreeOptions(num_attrs, attrs);
+ return;
+ }
/*
- * Bail if there was an error
+ * Do relaying...
*/
- if (errcode != SLP_OK)
- return (SLP_TRUE);
+ 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;
+ }
/*
- * Parse the attrlist to obtain things needed to build CUPS browse packet
+ * Process the browse data...
*/
- memset(p, 0, sizeof(cupsd_printer_t));
-
- p->type = CUPS_PRINTER_REMOTE;
-
- 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, "(color-supported=", &tmp))
- return (SLP_FALSE);
- if (strcasecmp(tmp, "true") == 0)
- p->type |= CUPS_PRINTER_COLOR;
-
- if (slp_get_attr(attrlist, "(finishings-supported=", &tmp))
- return (SLP_FALSE);
- if (strstr(tmp, "staple"))
- p->type |= CUPS_PRINTER_STAPLE;
- if (strstr(tmp, "bind"))
- p->type |= CUPS_PRINTER_BIND;
- if (strstr(tmp, "punch"))
- p->type |= CUPS_PRINTER_PUNCH;
-
- if (slp_get_attr(attrlist, "(sides-supported=", &tmp))
- return (SLP_FALSE);
- if (strstr(tmp,"two-sided"))
- p->type |= CUPS_PRINTER_DUPLEX;
-
- cupsdClearString(&tmp);
-
- 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 */
+ if (!LPDConfigFile)
+ return;
+#ifdef __APPLE__
+ /*
+ * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
+ * setting for backwards-compatibility.
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
+ if (onoff && !get_hostconfig("CUPS_LPD"))
+ onoff = 0;
+#endif /* __APPLE__ */
- 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);
-
- /*
- * Deregister the printer...
- */
+ 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 */
- SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
- }
-}
+ snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9);
-/*
- * 'slp_get_attr()' - Get an attribute from an SLP registration.
- */
+ if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
+ LPDConfigFile + 9, strerror(errno));
+ 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; /* ... */
+ if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
+ newfile, strerror(errno));
+ cupsFileClose(ofp);
+ return;
+ }
+ /*
+ * Copy all of the lines from the cups-lpd file...
+ */
- cupsdClearString(valbuf);
+ 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 ="))
+ cupsFilePrintf(nfp, "%s\n", line);
+ }
- if ((ptr1 = strstr(attrlist, tag)) != NULL)
+ cupsFileClose(nfp);
+ cupsFileClose(ofp);
+ rename(newfile, LPDConfigFile + 9);
+ }
+#ifdef __APPLE__
+ 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,
+ NULL, &pid);
}
-
- return (-1);
+#endif /* __APPLE__ */
+ else
+ cupsdLogMessage(CUPSD_LOG_INFO, "Unknown LPDConfigFile scheme!");
}
/*
- * '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 */
-
-
- /*
- * Let the compiler know we won't be using these vars...
- */
-
- (void)hslp;
- (void)lifetime;
+ if (!SMBConfigFile)
+ return;
- /*
- * Bail if there was an error
- */
+ if (!strncmp(SMBConfigFile, "samba:///", 9))
+ {
+ /*
+ * Enable/disable SMB via the specified smb.conf config file...
+ */
- if (errcode != SLP_OK)
- return (SLP_TRUE);
+ 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? */
- /*
- * Grab the head of the list...
- */
- head = (slpsrvurl_t**)cookie;
+ snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8);
- /*
- * Allocate a *temporary* slpsrvurl_t to hold this entry.
- */
+ if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
+ SMBConfigFile + 8, strerror(errno));
+ return;
+ }
- if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
- return (SLP_FALSE);
+ if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
+ newfile, strerror(errno));
+ cupsFileClose(ofp);
+ return;
+ }
- /*
- * Copy the SLP service URL...
- */
+ /*
+ * Copy all of the lines from the smb.conf file...
+ */
- strlcpy(s->url, srvurl, sizeof(s->url));
+ in_printers = 0;
- /*
- * Link the SLP service URL into the head of the list
- */
+ while (cupsFileGets(ofp, line, sizeof(line)))
+ {
+ if (in_printers && strstr(line, "printable ="))
+ snprintf(line, sizeof(line), " printable = %s",
+ onoff ? "yes" : "no");
- if (*head)
- s->next = *head;
+ cupsFilePrintf(nfp, "%s\n", line);
- *head = s;
+ if (line[0] == '[')
+ in_printers = !strcmp(line, "[printers]");
+ }
- return (SLP_TRUE);
+ cupsFileClose(nfp);
+ cupsFileClose(ofp);
+ rename(newfile, SMBConfigFile + 8);
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_INFO, "Unknown SMBConfigFile scheme!");
}
-#endif /* HAVE_LIBSLP */
/*
- * End of "$Id: dirsvc.c 4906 2006-01-10 20:53:28Z mike $".
+ * End of "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $".
*/