/*
- * "$Id: dirsvc.c 5117 2006-02-16 14:10:43Z mike $"
+ * "$Id: dirsvc.c 5663 2006-06-15 20:36:42Z mike $"
*
* Directory services routines for the Common UNIX Printing System (CUPS).
*
* 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.
* cupsdStopPolling() - Stop polling servers as needed.
* cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
* protocol.
+ * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
* cupsdUpdatePolling() - Read status messages from the poll daemons.
* cupsdUpdateSLPBrowse() - Get browsing information via SLP.
* dequote() - Remote quotes from a string.
* 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.
*/
static char *dequote(char *d, const char *s, int dlen);
-static void process_browse_data(const char *uri, cups_ptype_t type,
+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 */
+#ifdef HAVE_OPENLDAP
+static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
+ {
+ "printerDescription",
+ "printerLocation",
+ "printerMakeAndModel",
+ "printerType",
+ "printerURI",
+ NULL
+ };
+#endif /* HAVE_OPENLDAP */
#ifdef HAVE_LIBSLP
/*
return;
}
}
+ 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"))
{
/*
cupsd_printer_t *printer; /* Current printer class */
time_t curtime; /* Current time */
struct tm *curdate; /* Current date */
+ cups_option_t *option; /* Current option */
/*
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
cupsd_printer_t *p) /* I - Printer to delete */
{
/*
- * Only announce if browsing is enabled...
+ * Only announce if browsing is enabled and this is a local queue...
*/
- if (!Browsing || !p->shared)
+ if (!Browsing || !p->shared ||
+ (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
return;
/*
* Announce the deletion...
*/
- if (BrowseLocalProtocols & BROWSE_CUPS)
- cupsdSendCUPSBrowse(p);
+ if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
+ send_cups_browse(p);
#ifdef HAVE_LIBSLP
- if (BrowseLocalProtocols & BROWSE_SLP)
+ if ((BrowseLocalProtocols & BROWSE_SLP) && BrowseSLPHandle)
slp_dereg_printer(p);
#endif /* HAVE_LIBSLP */
}
* Compute the update and timeout times...
*/
- ut = time(NULL) - BrowseInterval;
- to = time(NULL) - BrowseTimeout;
+ to = time(NULL);
+ ut = to - BrowseInterval;
/*
* Figure out how many printers need an update...
p->browse_time = time(NULL);
if (BrowseLocalProtocols & BROWSE_CUPS)
- cupsdSendCUPSBrowse(p);
+ send_cups_browse(p);
#ifdef HAVE_LIBSLP
if (BrowseLocalProtocols & BROWSE_SLP)
- cupsdSendSLPBrowse(p);
+ send_slp_browse(p);
#endif /* HAVE_LIBSLP */
+
+#ifdef HAVE_LDAP
+ if (BrowseLocalProtocols & BROWSE_LDAP)
+ send_ldap_browse(p);
+#endif /* HAVE_LDAP */
}
}
if (p->type & CUPS_PRINTER_REMOTE)
{
- if (p->browse_time < to)
+ if (p->browse_expire < 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...",
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Remote destination \"%s\" has timed out; "
+ "deleting it...",
p->name);
cupsArraySave(Printers);
/*
- * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS protocol.
+ * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
*/
void
-cupsdSendCUPSBrowse(cupsd_printer_t *p) /* I - Printer to send */
+cupsdStartBrowsing(void)
{
- int i; /* Looping var */
- cups_ptype_t type; /* Printer type */
- cupsd_dirsvc_addr_t *b; /* Browse address */
- int bytes; /* Length of packet */
- char packet[1453], /* Browse data packet */
- uri[1024], /* Printer URI */
- options[1024], /* Browse local options */
- location[1024], /* printer-location */
- info[1024], /* printer-info */
- make_model[1024];
- /* printer-make-and-model */
- cupsd_netif_t *iface; /* Network interface */
-
-
- /*
- * Figure out the printer type value...
- */
-
- type = p->type | CUPS_PRINTER_REMOTE;
-
- if (!p->accepting)
- type |= CUPS_PRINTER_REJECTING;
-
- if (p == DefaultPrinter)
- type |= CUPS_PRINTER_DEFAULT;
+ int val; /* Socket option value */
+ struct sockaddr_in addr; /* Broadcast address */
- /*
- * Initialize the browse options...
- */
- if (BrowseLocalOptions)
- snprintf(options, sizeof(options), " ipp-options=%s", BrowseLocalOptions);
- else
- options[0] = '\0';
+ BrowseNext = NULL;
- /*
- * Remove quotes from printer-info, printer-location, and
- * printer-make-and-model attributes...
- */
+ if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
+ return;
- dequote(location, p->location, sizeof(p->location));
- dequote(info, p->info, sizeof(p->info));
- dequote(make_model, p->make_model ? p->make_model : "Unknown",
- sizeof(make_model));
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
+ {
+ if (BrowseSocket < 0)
+ {
+ /*
+ * Create the broadcast socket...
+ */
- /*
- * Send a packet to each browse address...
- */
+ 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;
+ }
- for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
- if (b->iface[0])
- {
/*
- * Send the browse packet to one or more interfaces...
+ * Bind the socket to browse port...
*/
- if (!strcmp(b->iface, "*"))
+ 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)))
{
- /*
- * Send to all local interfaces...
- */
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "cupsdStartBrowsing: Unable to bind broadcast "
+ "socket - %s.", strerror(errno));
- cupsdNetIFUpdate();
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
- for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
- iface;
- iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
- {
- /*
- * Only send to local, IPv4 interfaces...
- */
+ BrowseSocket = -1;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+ return;
+ }
+ }
- if (!iface->is_local || !iface->port ||
- iface->address.addr.sa_family != AF_INET)
- continue;
+ /*
+ * Set the "broadcast" flag...
+ */
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- iface->hostname, iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
- "/printers/%s",
- p->name);
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
- type, p->state, uri, location, info, make_model, options);
+ 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));
- bytes = strlen(packet);
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
- iface->name, packet);
+ BrowseSocket = -1;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+ return;
+ }
- iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+ /*
+ * Close the socket on exec...
+ */
- sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(iface->broadcast),
- sizeof(struct sockaddr_in));
- }
- }
- else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
- {
- /*
- * Send to the named interface using the IPv4 address...
- */
+ fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
- while (iface)
- if (strcmp(b->iface, iface->name))
- {
- iface = NULL;
- break;
- }
- else if (iface->address.addr.sa_family == AF_INET && iface->port)
- break;
- else
- iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
+ /*
+ * Finally, add the socket to the input selection set...
+ */
- if (iface)
- {
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- iface->hostname, iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
- "/printers/%s",
- p->name);
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
- type, p->state, uri, location, info, make_model, options);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdStartBrowsing: Adding fd %d to InputSet...",
+ BrowseSocket);
- bytes = strlen(packet);
+ FD_SET(BrowseSocket, InputSet);
+ }
+ else
+ BrowseSocket = -1;
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
- iface->name, packet);
+#ifdef HAVE_LIBSLP
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
+ {
+ /*
+ * Open SLP handle...
+ */
- iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+ 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;
+ }
- sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(iface->broadcast),
- sizeof(struct sockaddr_in));
- }
- }
+ 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
{
- /*
- * Send the browse packet to the indicated address using
- * the default server name...
+ /*
+ * Open LDAP handle...
*/
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\"%s\n",
- type, p->state, p->uri, location, info, make_model, options);
+ int rc; /* LDAP API status */
+ int version = 3; /* LDAP version */
+ struct berval bv = {0, ""}; /* SASL bind value */
- bytes = strlen(packet);
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
- if (sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(b->to),
- sizeof(struct sockaddr_in)) <= 0)
- {
- /*
- * Unable to send browse packet, so remove this address from the
- * list...
- */
+ /*
+ * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
+ */
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdSendBrowseList: sendto failed for browser %d - %s.",
- b - Browsers + 1, strerror(errno));
+ if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
+ rc = ldap_initialize(&BrowseLDAPHandle, "ldapi:///");
+ else
+ rc = ldap_initialize(&BrowseLDAPHandle, BrowseLDAPServer);
- if (i > 1)
- memcpy(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
+ 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);
- b --;
- NumBrowsers --;
+ 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 */
}
-#ifdef HAVE_LIBSLP
/*
- * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP.
+ * 'cupsdStartPolling()' - Start polling servers as needed.
*/
-void
-cupsdSendSLPBrowse(cupsd_printer_t *p) /* I - Printer to register */
+void
+cupsdStartPolling(void)
{
- char srvurl[HTTP_MAX_URI], /* Printer service URI */
- attrs[8192], /* Printer attributes */
- finishings[1024], /* Finishings to support */
- make_model[IPP_MAX_NAME * 2],
- /* Make and model, quoted */
- location[IPP_MAX_NAME * 2],
- /* Location, quoted */
- info[IPP_MAX_NAME * 2], /* Info, quoted */
- *src, /* Pointer to original string */
- *dst; /* Pointer to destination string */
- ipp_attribute_t *authentication; /* uri-authentication-supported value */
- SLPError error; /* SLP error, if any */
-
+ int i; /* Looping var */
+ 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 */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendSLPBrowse(%p = \"%s\")", p,
- p->name);
/*
- * Make the SLP service URL that conforms to the IANA
- * 'printer:' template.
+ * Don't do anything if we aren't polling...
*/
- snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
-
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
+ if (NumPolled == 0)
+ {
+ PollPipe = -1;
+ PollStatusBuffer = NULL;
+ return;
+ }
/*
- * Figure out the finishings string...
+ * Setup string arguments for polld, port and interval options.
*/
- if (p->type & CUPS_PRINTER_STAPLE)
- strcpy(finishings, "staple");
+ snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
+
+ sprintf(bport, "%d", BrowsePort);
+
+ if (BrowseInterval)
+ sprintf(interval, "%d", BrowseInterval);
else
- finishings[0] = '\0';
+ strcpy(interval, "30");
- if (p->type & CUPS_PRINTER_BIND)
- {
- if (finishings[0])
- strlcat(finishings, ",bind", sizeof(finishings));
- else
- strcpy(finishings, "bind");
- }
+ argv[0] = "cups-polld";
+ argv[2] = sport;
+ argv[3] = interval;
+ argv[4] = bport;
+ argv[5] = NULL;
- if (p->type & CUPS_PRINTER_PUNCH)
- {
- if (finishings[0])
- strlcat(finishings, ",punch", sizeof(finishings));
- else
- strcpy(finishings, "punch");
- }
+ cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
- if (p->type & CUPS_PRINTER_COVER)
+ /*
+ * Create a pipe that receives the status messages from each
+ * polling daemon...
+ */
+
+ if (cupsdOpenPipe(statusfds))
{
- if (finishings[0])
- strlcat(finishings, ",cover", sizeof(finishings));
- else
- strcpy(finishings, "cover");
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to create polling status pipes - %s.",
+ strerror(errno));
+ PollPipe = -1;
+ PollStatusBuffer = NULL;
+ return;
}
- if (p->type & CUPS_PRINTER_SORT)
+ 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 ++)
{
- if (finishings[0])
- strlcat(finishings, ",sort", sizeof(finishings));
+ sprintf(sport, "%d", pollp->port);
+
+ argv[1] = pollp->hostname;
+
+ 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;
+ }
else
- strcpy(finishings, "sort");
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
+ pollp->hostname, pollp->port, pollp->pid);
}
- if (!finishings[0])
- strcpy(finishings, "none");
+ close(statusfds[1]);
/*
- * Quote any commas in the make and model, location, and info strings...
+ * Finally, add the pipe to the input selection set...
*/
- for (src = p->make_model, dst = make_model;
- src && *src && dst < (make_model + sizeof(make_model) - 2);)
- {
- if (*src == ',' || *src == '\\' || *src == ')')
- *dst++ = '\\';
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe);
- *dst++ = *src++;
- }
+ FD_SET(PollPipe, InputSet);
+}
- *dst = '\0';
- if (!make_model[0])
- strcpy(make_model, "Unknown");
+/*
+ * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
+ */
- for (src = p->location, dst = location;
- src && *src && dst < (location + sizeof(location) - 2);)
+void
+cupsdStopBrowsing(void)
+{
+ if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
+ return;
+
+ if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) &&
+ BrowseSocket >= 0)
{
- if (*src == ',' || *src == '\\' || *src == ')')
- *dst++ = '\\';
+ /*
+ * Close the socket and remove it from the input selection set.
+ */
- *dst++ = *src++;
- }
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
- *dst = '\0';
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdStopBrowsing: Removing fd %d from InputSet...",
+ BrowseSocket);
- if (!location[0])
- strcpy(location, "Unknown");
+ FD_CLR(BrowseSocket, InputSet);
+ BrowseSocket = -1;
+ }
- for (src = p->info, dst = info;
- src && *src && dst < (info + sizeof(info) - 2);)
+#ifdef HAVE_LIBSLP
+ if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
+ BrowseSLPHandle)
{
- if (*src == ',' || *src == '\\' || *src == ')')
- *dst++ = '\\';
+ /*
+ * Close SLP handle...
+ */
- *dst++ = *src++;
+ SLPClose(BrowseSLPHandle);
+ BrowseSLPHandle = NULL;
}
+#endif /* HAVE_LIBSLP */
- *dst = '\0';
-
- if (!info[0])
- strcpy(info, "Unknown");
+#ifdef HAVE_OPENLDAP
+ if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) &&
+ BrowseLDAPHandle)
+ {
+ ldap_unbind(BrowseLDAPHandle);
+ BrowseLDAPHandle = NULL;
+ }
+#endif /* HAVE_OPENLDAP */
+}
- /*
- * Get the authentication value...
- */
- authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
- IPP_TAG_KEYWORD);
+/*
+ * 'cupsdStopPolling()' - Stop polling servers as needed.
+ */
- /*
- * Make the SLP attribute string list that conforms to
- * the IANA 'printer:' template.
- */
+void
+cupsdStopPolling(void)
+{
+ int i; /* Looping var */
+ cupsd_dirsvc_poll_t *pollp; /* Current polling server */
- snprintf(attrs, sizeof(attrs),
- "(printer-uri-supported=%s),"
- "(uri-authentication-supported=%s>),"
-#ifdef HAVE_SSL
- "(uri-security-supported=tls>),"
-#else
- "(uri-security-supported=none>),"
-#endif /* HAVE_SSL */
- "(printer-name=%s),"
- "(printer-location=%s),"
- "(printer-info=%s),"
- "(printer-more-info=%s),"
- "(printer-make-and-model=%s),"
- "(charset-supported=utf-8),"
- "(natural-language-configured=%s),"
- "(natural-language-supported=de,en,es,fr,it),"
- "(color-supported=%s),"
- "(finishings-supported=%s),"
- "(sides-supported=one-sided%s),"
- "(multiple-document-jobs-supported=true)"
- "(ipp-versions-supported=1.0,1.1)",
- p->uri, authentication->values[0].string.text, p->name, location,
- info, p->uri, make_model, 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);
+ if (PollPipe >= 0)
+ {
+ cupsdStatBufDelete(PollStatusBuffer);
+ close(PollPipe);
- /*
- * Register the printer with the SLP server...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdStopPolling: removing fd %d from InputSet.", PollPipe);
+ FD_CLR(PollPipe, InputSet);
- error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
- SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
+ PollPipe = -1;
+ PollStatusBuffer = NULL;
+ }
- if (error != SLP_OK)
- cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
- error);
+ for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
+ if (pollp->pid)
+ cupsdEndProcess(pollp->pid, 0);
}
-#endif /* HAVE_LIBSLP */
/*
- * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
+ * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
*/
void
-cupsdStartBrowsing(void)
+cupsdUpdateCUPSBrowse(void)
{
- int val; /* Socket option value */
- struct sockaddr_in addr; /* Broadcast address */
-
-
- 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,
- "cupsdStartBrowsing: 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,
- "cupsdStartBrowsing: Unable to bind broadcast "
- "socket - %s.", strerror(errno));
+ int i; /* Looping var */
+ int auth; /* Authorization status */
+ int len; /* Length of name string */
+ int bytes; /* Number of bytes left */
+ char packet[1541], /* Broadcast packet */
+ *pptr; /* Pointer into packet */
+ socklen_t srclen; /* Length of source address */
+ http_addr_t srcaddr; /* Source address */
+ char srcname[1024]; /* Source hostname */
+ unsigned address[4]; /* Source address */
+ unsigned type; /* Printer type */
+ unsigned state; /* Printer state */
+ char uri[HTTP_MAX_URI], /* Printer URI */
+ host[HTTP_MAX_URI], /* Host portion of URI */
+ resource[HTTP_MAX_URI], /* Resource portion of URI */
+ info[IPP_MAX_NAME], /* Information string */
+ location[IPP_MAX_NAME], /* Location string */
+ make_model[IPP_MAX_NAME];/* Make and model string */
+ int num_attrs; /* Number of attributes */
+ cups_option_t *attrs; /* Attributes */
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
- BrowseSocket = -1;
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
- }
+ /*
+ * Read a packet from the browse socket...
+ */
+ srclen = sizeof(srcaddr);
+ if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
+ (struct sockaddr *)&srcaddr, &srclen)) < 0)
+ {
/*
- * Set the "broadcast" flag...
+ * "Connection refused" is returned under Linux if the destination port
+ * or address is unreachable from a previous sendto(); check for the
+ * error here and ignore it for now...
*/
- val = 1;
- if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
+ if (errno != ECONNREFUSED && errno != EAGAIN)
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
- strerror(errno));
-
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
+ strerror(errno));
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
- BrowseSocket = -1;
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
+ cupsdStopBrowsing();
+ Browsing = 0;
}
- /*
- * Close the socket on exec...
- */
+ return;
+ }
- fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
+ packet[bytes] = '\0';
- /*
- * Finally, add the socket to the input selection set...
- */
+ /*
+ * If we're about to sleep, ignore incoming browse packets.
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStartBrowsing: Adding fd %d to InputSet...",
- BrowseSocket);
+ if (Sleeping)
+ return;
- FD_SET(BrowseSocket, InputSet);
+ /*
+ * Figure out where it came from...
+ */
+
+#ifdef AF_INET6
+ if (srcaddr.addr.sa_family == AF_INET6)
+ {
+ address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
+ address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
+ address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
+ address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
}
else
- BrowseSocket = -1;
-
-#ifdef HAVE_LIBSLP
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
+#endif /* AF_INET6 */
{
- /*
- * 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;
+ address[0] = 0;
+ address[1] = 0;
+ address[2] = 0;
+ address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
}
-#endif /* HAVE_LIBSLP */
-}
-
-
-/*
- * '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[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 */
+ if (HostNameLookups)
+ httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
+ else
+ httpAddrString(&srcaddr, srcname, sizeof(srcname));
+ len = strlen(srcname);
/*
- * Don't do anything if we aren't polling...
+ * Do ACL stuff...
*/
- if (NumPolled == 0)
+ if (BrowseACL)
{
- PollPipe = -1;
- PollStatusBuffer = NULL;
- return;
- }
+ if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
+ {
+ /*
+ * Access from localhost (127.0.0.1) is always allowed...
+ */
- /*
- * Setup string arguments for polld, port and interval options.
- */
+ auth = AUTH_ALLOW;
+ }
+ else
+ {
+ /*
+ * Do authorization checks on the domain/address...
+ */
- snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
+ switch (BrowseACL->order_type)
+ {
+ default :
+ auth = AUTH_DENY; /* anti-compiler-warning-code */
+ break;
- sprintf(bport, "%d", BrowsePort);
+ case AUTH_ALLOW : /* Order Deny,Allow */
+ auth = AUTH_ALLOW;
- if (BrowseInterval)
- sprintf(interval, "%d", BrowseInterval);
- else
- strcpy(interval, "30");
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_deny, BrowseACL->deny))
+ auth = AUTH_DENY;
- argv[0] = "cups-polld";
- argv[2] = sport;
- argv[3] = interval;
- argv[4] = bport;
- argv[5] = NULL;
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_allow, BrowseACL->allow))
+ auth = AUTH_ALLOW;
+ break;
- cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
+ case AUTH_DENY : /* Order Allow,Deny */
+ auth = AUTH_DENY;
- /*
- * Create a pipe that receives the status messages from each
- * polling daemon...
- */
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_allow, BrowseACL->allow))
+ auth = AUTH_ALLOW;
- if (cupsdOpenPipe(statusfds))
+ if (cupsdCheckAuth(address, srcname, len,
+ BrowseACL->num_deny, BrowseACL->deny))
+ auth = AUTH_DENY;
+ break;
+ }
+ }
+ }
+ else
+ auth = AUTH_ALLOW;
+
+ if (auth == AUTH_DENY)
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to create polling status pipes - %s.",
- strerror(errno));
- PollPipe = -1;
- PollStatusBuffer = NULL;
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes,
+ srcname);
return;
}
- PollPipe = statusfds[0];
- PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]");
-
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes,
+ srcname, packet);
+
/*
- * Run each polling daemon, redirecting stderr to the polling pipe...
+ * Parse packet...
*/
- for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
+ if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
{
- sprintf(sport, "%d", pollp->port);
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet);
+ return;
+ }
- argv[1] = pollp->hostname;
+ strcpy(location, "Location Unknown");
+ strcpy(info, "No Information Available");
+ make_model[0] = '\0';
+ num_attrs = 0;
+ attrs = NULL;
- if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1,
- 0, &(pollp->pid)) < 0)
+ if ((pptr = strchr(packet, '\"')) != NULL)
+ {
+ /*
+ * Have extended information; can't use sscanf for it because not all
+ * sscanf's allow empty strings with %[^\"]...
+ */
+
+ for (i = 0, pptr ++;
+ i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
+ i ++, pptr ++)
+ location[i] = *pptr;
+
+ if (i)
+ location[i] = '\0';
+
+ if (*pptr == '\"')
+ pptr ++;
+
+ while (*pptr && isspace(*pptr & 255))
+ pptr ++;
+
+ if (*pptr == '\"')
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdStartPolling: Unable to fork polling daemon - %s",
- strerror(errno));
- pollp->pid = 0;
- break;
+ 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);
+ }
}
- else
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
- pollp->hostname, pollp->port, pollp->pid);
}
- close(statusfds[1]);
+ 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));
/*
- * Finally, add the pipe to the input selection set...
+ * Pull the URI apart to see if this is a local or remote printer...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe);
+ if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
+ {
+ cupsFreeOptions(num_attrs, attrs);
+ return;
+ }
- FD_SET(PollPipe, InputSet);
+ /*
+ * 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...
+ */
+
+ process_browse_data(uri, host, resource, (cups_ptype_t)type,
+ (ipp_pstate_t)state, location, info, make_model,
+ num_attrs, attrs);
}
+#ifdef HAVE_OPENLDAP
/*
- * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
+ * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
*/
void
-cupsdStopBrowsing(void)
+cupsdUpdateLDAPBrowse(void)
{
- if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
+ 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;
+ }
- if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) &&
- BrowseSocket >= 0)
+ 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))
{
/*
- * Close the socket and remove it from the input selection set.
+ * Get the required values from this entry...
*/
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerDescription")) == NULL)
+ continue;
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStopBrowsing: Removing fd %d from InputSet...",
- BrowseSocket);
+ strlcpy(info, *value, sizeof(info));
+ ldap_value_free(value);
- FD_CLR(BrowseSocket, InputSet);
- BrowseSocket = -1;
- }
+ if ((value = ldap_get_values(BrowseLDAPHandle, e,
+ "printerLocation")) == NULL)
+ continue;
-#ifdef HAVE_LIBSLP
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
- {
- /*
- * Close SLP handle...
+ 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...
*/
- SLPClose(BrowseSLPHandle);
+ 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_LIBSLP */
}
+#endif /* HAVE_OPENLDAP */
/*
- * 'cupsdStopPolling()' - Stop polling servers as needed.
+ * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
*/
void
-cupsdStopPolling(void)
+cupsdUpdatePolling(void)
{
- int i; /* Looping var */
- cupsd_dirsvc_poll_t *pollp; /* Current polling server */
+ char *ptr, /* Pointer to end of line in buffer */
+ message[1024]; /* Pointer to message text */
+ int loglevel; /* Log level for message */
- if (PollPipe >= 0)
- {
- cupsdStatBufDelete(PollStatusBuffer);
- close(PollPipe);
+ while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
+ message, sizeof(message))) != NULL)
+ if (!strchr(PollStatusBuffer->buffer, '\n'))
+ break;
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdStopPolling: removing fd %d from InputSet.", PollPipe);
- FD_CLR(PollPipe, InputSet);
+ if (ptr == NULL)
+ {
+ /*
+ * All polling processes have died; stop polling...
+ */
- PollPipe = -1;
- PollStatusBuffer = NULL;
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "cupsdUpdatePolling: all polling processes have exited!");
+ cupsdStopPolling();
}
-
- for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
- if (pollp->pid)
- cupsdEndProcess(pollp->pid, 0);
}
+#ifdef HAVE_LIBSLP
/*
- * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
+ * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
*/
void
-cupsdUpdateCUPSBrowse(void)
+cupsdUpdateSLPBrowse(void)
{
- int i; /* Looping var */
- int auth; /* Authorization status */
- int len; /* Length of name string */
- int bytes; /* Number of bytes left */
- char packet[1541], /* Broadcast packet */
- *pptr; /* Pointer into packet */
- socklen_t srclen; /* Length of source address */
- http_addr_t srcaddr; /* Source address */
- char srcname[1024]; /* Source hostname */
- unsigned address[4]; /* Source address */
- unsigned type; /* Printer type */
- unsigned state; /* Printer state */
- char uri[HTTP_MAX_URI], /* Printer URI */
- 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 */
+ 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 */
/*
- * Read a packet from the browse socket...
+ * Reset the refresh time...
*/
- srclen = sizeof(srcaddr);
- if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
- (struct sockaddr *)&srcaddr, &srclen)) < 0)
+ 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)
{
/*
- * "Connection refused" is returned under Linux if the destination port
- * or address is unreachable from a previous sendto(); check for the
- * error here and ignore it for now...
+ * Save the "next" pointer...
*/
- if (errno != ECONNREFUSED && errno != EAGAIN)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
- strerror(errno));
- cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off.");
-
- cupsdStopBrowsing();
- Browsing = 0;
- }
-
- return;
- }
-
- packet[bytes] = '\0';
-
- /*
- * If we're about to sleep, ignore incoming browse packets.
- */
-
- if (Sleeping)
- return;
-
- /*
- * Figure out where it came from...
- */
-
-#ifdef AF_INET6
- if (srcaddr.addr.sa_family == AF_INET6)
- {
- address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
- address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
- address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
- address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
- }
- else
-#endif /* AF_INET6 */
- {
- address[0] = 0;
- address[1] = 0;
- address[2] = 0;
- address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
- }
+ next = s->next;
- if (HostNameLookups)
- httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
- else
- httpAddrString(&srcaddr, srcname, sizeof(srcname));
+ /*
+ * Load a cupsd_printer_t structure with the SLP service attributes...
+ */
- len = strlen(srcname);
+ SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
- /*
- * Do ACL stuff...
- */
+ /*
+ * Process this printer entry...
+ */
- if (BrowseACL)
- {
- if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
- {
- /*
- * Access from localhost (127.0.0.1) is always allowed...
- */
+ uri = s->url + SLP_CUPS_SRVLEN + 1;
- auth = AUTH_ALLOW;
- }
- else
+ if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
{
/*
- * Do authorization checks on the domain/address...
+ * Pull the URI apart to see if this is a local or remote printer...
*/
- switch (BrowseACL->order_type)
- {
- default :
- auth = AUTH_DENY; /* anti-compiler-warning-code */
- break;
-
- case AUTH_ALLOW : /* Order Deny,Allow */
- auth = AUTH_ALLOW;
-
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_deny, BrowseACL->deny))
- auth = AUTH_DENY;
-
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_allow, BrowseACL->allow))
- auth = AUTH_ALLOW;
- break;
-
- case AUTH_DENY : /* Order Allow,Deny */
- auth = AUTH_DENY;
-
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_allow, BrowseACL->allow))
- auth = AUTH_ALLOW;
-
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_deny, BrowseACL->deny))
- auth = AUTH_DENY;
- break;
- }
+ 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);
}
- }
- else
- auth = AUTH_ALLOW;
-
- if (auth == AUTH_DENY)
- {
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes,
- srcname);
- return;
- }
-
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes,
- srcname, packet);
-
- /*
- * Parse packet...
- */
-
- if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
- {
- cupsdLogMessage(CUPSD_LOG_WARN,
- "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet);
- return;
- }
- strcpy(location, "Location Unknown");
- strcpy(info, "No Information Available");
- make_model[0] = '\0';
- num_attrs = 0;
- attrs = NULL;
-
- if ((pptr = strchr(packet, '\"')) != NULL)
- {
/*
- * Have extended information; can't use sscanf for it because not all
- * sscanf's allow empty strings with %[^\"]...
+ * Free this listing...
*/
- for (i = 0, pptr ++;
- i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- location[i] = *pptr;
+ cupsdClearString(&p.info);
+ cupsdClearString(&p.location);
+ cupsdClearString(&p.make_model);
- if (i)
- location[i] = '\0';
+ free(s);
+ }
+}
+#endif /* HAVE_LIBSLP */
- if (*pptr == '\"')
- pptr ++;
- while (*pptr && isspace(*pptr & 255))
- pptr ++;
+/*
+ * 'dequote()' - Remote quotes from a string.
+ */
- if (*pptr == '\"')
- {
- for (i = 0, pptr ++;
- i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- info[i] = *pptr;
+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 */
- info[i] = '\0';
- if (*pptr == '\"')
- pptr ++;
+ if (s)
+ {
+ for (dptr = d, dlen --; *s && dlen > 0; s ++)
+ if (*s != '\"')
+ {
+ *dptr++ = *s;
+ dlen --;
+ }
- while (*pptr && isspace(*pptr & 255))
- pptr ++;
+ *dptr = '\0';
+ }
+ else
+ *d = '\0';
- if (*pptr == '\"')
- {
- for (i = 0, pptr ++;
- i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
- i ++, pptr ++)
- make_model[i] = *pptr;
+ return (d);
+}
- if (*pptr == '\"')
- pptr ++;
- make_model[i] = '\0';
+/*
+ * 'is_local_queue()' - Determine whether the URI points at a local queue.
+ */
- if (*pptr)
- num_attrs = cupsParseOptions(pptr, num_attrs, &attrs);
- }
- }
- }
+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 */
- 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(HTTP_URI_CODING_ALL, uri, method, sizeof(method), username,
- sizeof(username), host, sizeof(host), &port, resource,
- sizeof(resource));
+ 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));
/*
- * Check for packets from the local server...
+ * Check for local server addresses...
*/
if (!strcasecmp(host, ServerName) && port == LocalPort)
- {
- cupsFreeOptions(num_attrs, attrs);
- return;
- }
+ return (1);
cupsdNetIFUpdate();
iface;
iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
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;
- }
+ return (1);
/*
- * Process the browse data...
+ * If we get here, the printer is remote...
*/
- process_browse_data(uri, (cups_ptype_t)type, (ipp_pstate_t)state, location,
- info, make_model, num_attrs, attrs);
- cupsFreeOptions(num_attrs, attrs);
+ return (0);
}
/*
- * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
+ * 'process_browse_data()' - Process new browse data.
*/
-void
-cupsdUpdatePolling(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 */
{
- 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;
+ int i; /* Looping var */
+ int update; /* Update printer attributes? */
+ 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 */
- if (ptr == NULL)
+
+ /*
+ * 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)))
{
- /*
- * All polling processes have died; stop polling...
- */
+ 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,
- "cupsdUpdatePolling: all polling processes have exited!");
- cupsdStopPolling();
+ "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);
-#ifdef HAVE_LIBSLP
-/*
- * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
- */
+ if (BrowseRemoteOptions)
+ {
+ if (BrowseRemoteOptions[0] == '?')
+ {
+ /*
+ * Override server-supplied options...
+ */
-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 */
+ 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...
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdUpdateSLPBrowse() Start...");
+ snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
+ uri = finaluri;
+ }
/*
- * Reset the refresh time...
+ * See if we already have it listed in the Printers list, and add it if not...
*/
- BrowseSLPRefresh = time(NULL) + BrowseInterval;
+ type |= CUPS_PRINTER_REMOTE;
+ type &= ~CUPS_PRINTER_IMPLICIT;
+ update = 0;
+ hptr = strchr(host, '.');
+ sptr = strchr(ServerName, '.');
- /*
- * Poll for remote printers using SLP...
- */
+ if (sptr != NULL && hptr != NULL)
+ {
+ /*
+ * Strip the common domain name components...
+ */
+
+ while (hptr != NULL)
+ {
+ if (!strcasecmp(hptr, sptr))
+ {
+ *hptr = '\0';
+ break;
+ }
+ else
+ hptr = strchr(hptr + 1, '.');
+ }
+ }
+
+ if (type & CUPS_PRINTER_CLASS)
+ {
+ /*
+ * Remote destination is a class...
+ */
+
+ if (!strncmp(resource, "/classes/", 9))
+ snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
+ else
+ return;
+
+ 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 (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);
+
+ 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
+ {
+ /*
+ * Use the short name for this shared class.
+ */
+
+ 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...
+ */
+
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+ }
+
+ if (!p)
+ {
+ /*
+ * Class doesn't exist; add it...
+ */
+
+ p = cupsdAddClass(name);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote class \"%s\"...", name);
+
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Class \'%s\' added by directory services.", name);
+
+ /*
+ * Force the URI to point to the real server...
+ */
+
+ p->type = type & ~CUPS_PRINTER_REJECTING;
+ p->accepting = 1;
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ cupsdSetString(&p->hostname, host);
+
+ update = 1;
+ }
+ }
+ else
+ {
+ /*
+ * Remote destination is a printer...
+ */
+
+ if (!strncmp(resource, "/printers/", 10))
+ snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
+ else
+ return;
+
+ if ((p = cupsdFindPrinter(name)) == NULL && BrowseShortNames)
+ {
+ if ((p = cupsdFindPrinter(resource + 10)) != 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 printer and then find a printer using the full host
+ * name...
+ */
+
+ 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 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
+ {
+ /*
+ * Use the short name for this shared printer.
+ */
+
+ 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...
+ */
+
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+ }
+
+ if (!p)
+ {
+ /*
+ * Printer doesn't exist; add it...
+ */
+
+ p = cupsdAddPrinter(name);
+
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Printer \'%s\' added by directory services.", name);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote printer \"%s\"...", name);
+
+ /*
+ * Force the URI to point to the real server...
+ */
- s = NULL;
+ p->type = type & ~CUPS_PRINTER_REJECTING;
+ p->accepting = 1;
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
- SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "",
- slp_url_callback, &s);
+ update = 1;
+ }
+ }
/*
- * Loop through the list of available printers...
+ * Update the state...
*/
- for (; s; s = next)
+ p->state = state;
+ p->browse_time = time(NULL);
+
+ if ((lease_duration = cupsGetOption("lease-duration", num_attrs,
+ attrs)) != NULL)
{
/*
- * Save the "next" pointer...
+ * Grab the lease-duration for the browse data; anything less then 1
+ * second or more than 1 week gets the default BrowseTimeout...
*/
- next = s->next;
+ i = atoi(lease_duration);
+ if (i < 1 || i > 604800)
+ i = BrowseTimeout;
- /*
- * Load a cupsd_printer_t structure with the SLP service attributes...
- */
+ p->browse_expire = p->browse_time + i;
+ }
+ else
+ p->browse_expire = p->browse_time + BrowseTimeout;
- SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
+ if (type & CUPS_PRINTER_REJECTING)
+ {
+ type &= ~CUPS_PRINTER_REJECTING;
- /*
- * Process this printer entry...
- */
+ if (p->accepting)
+ {
+ update = 1;
+ p->accepting = 0;
+ }
+ }
+ else if (!p->accepting)
+ {
+ update = 1;
+ p->accepting = 1;
+ }
- uri = s->url + SLP_CUPS_SRVLEN + 1;
+ if (p->type != type)
+ {
+ p->type = type;
+ update = 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 (location && (!p->location || strcmp(p->location, location)))
+ {
+ cupsdSetString(&p->location, location);
+ update = 1;
+ }
+
+ if (info && (!p->info || strcmp(p->info, info)))
+ {
+ cupsdSetString(&p->info, info);
+ update = 1;
+ }
- httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method),
- username, sizeof(username), host, sizeof(host), &port,
- resource, sizeof(resource));
+ 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);
+ }
+ else
+ snprintf(local_make_model, sizeof(local_make_model),
+ "%s on %s", make_model, host);
- if (!strcasecmp(host, ServerName))
- continue;
+ if (!p->make_model || strcmp(p->make_model, local_make_model))
+ {
+ cupsdSetString(&p->make_model, local_make_model);
+ update = 1;
+ }
+ if (p->num_options)
+ {
+ if (!update && !(type & CUPS_PRINTER_DELETE))
+ {
/*
- * OK, at least an IPP printer, see if it is a CUPS printer or
- * class...
+ * See if we need to update the attributes...
*/
- if (strstr(uri, "/printers/") != NULL)
- process_browse_data(uri, p.type, IPP_PRINTER_IDLE, p.location,
- p.info, p.make_model, 0, NULL);
- else if (strstr(uri, "/classes/") != NULL)
- process_browse_data(uri, p.type | CUPS_PRINTER_CLASS, IPP_PRINTER_IDLE,
- p.location, p.info, p.make_model, 0, NULL);
+ 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;
+ }
+ }
}
/*
- * Free this listing...
+ * Free the old options...
*/
- free(s);
- }
-
- cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdUpdateSLPBrowse() End...");
-}
-#endif /* HAVE_LIBSLP */
+ cupsFreeOptions(p->num_options, p->options);
+ }
+ p->num_options = num_attrs;
+ p->options = attrs;
-/*
- * 'dequote()' - Remote quotes from a string.
- */
+ 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);
-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 */
+ cupsdExpireSubscriptions(p, NULL);
+
+ cupsdDeletePrinter(p, 1);
+ cupsdUpdateImplicitClasses();
+ }
+ else if (update)
+ {
+ cupsdSetPrinterAttrs(p);
+ cupsdUpdateImplicitClasses();
+ }
+ /*
+ * See if we have a default printer... If not, make the first network
+ * default printer the default.
+ */
- if (s)
+ if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault)
{
- for (dptr = d, dlen --; *s && dlen > 0; s ++)
- if (*s != '\"')
+ /*
+ * Find the first network default printer and use it...
+ */
+
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ if (p->type & CUPS_PRINTER_DEFAULT)
{
- *dptr++ = *s;
- dlen --;
+ DefaultPrinter = p;
+ break;
}
-
- *dptr = '\0';
}
- else
- *d = '\0';
- return (d);
+ /*
+ * Do auto-classing if needed...
+ */
+
+ process_implicit_classes();
+
+ /*
+ * Update the printcap file...
+ */
+
+ cupsdWritePrintcap();
}
/*
- * 'process_browse_data()' - Process new browse data.
+ * 'process_implicit_classes()' - Create/update implicit classes as needed.
*/
static void
-process_browse_data(
- 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 */
+process_implicit_classes(void)
{
+ int i; /* Looping var */
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 */
-
+ *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 */
- /*
- * Pull the URI apart to see if this is a local or remote printer...
- */
- httpSeparateURI(HTTP_URI_CODING_ALL, uri, method, sizeof(method), username,
- sizeof(username), host, sizeof(host), &port, resource,
- sizeof(resource));
+ if (!ImplicitClasses || !Printers)
+ return;
/*
- * Determine if the URI contains any illegal characters in it...
+ * Loop through all available printers and create classes as needed...
*/
- if (strncmp(uri, "ipp://", 6) || !host[0] ||
- (strncmp(resource, "/printers/", 10) &&
- strncmp(resource, "/classes/", 9)))
+ 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))
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "process_browse_data: Bad printer URI in browse data: %s",
- uri);
- return;
- }
+ /*
+ * Skip implicit classes...
+ */
- 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;
- }
+ if (p->type & CUPS_PRINTER_IMPLICIT)
+ {
+ len = 0;
+ continue;
+ }
- /*
- * OK, this isn't a local printer; add any remote options...
- */
+ /*
+ * If len == 0, get the length of this printer name up to the "@"
+ * sign (if any).
+ */
- ipp_options = cupsGetOption("ipp-options", num_attrs, attrs);
+ cupsArraySave(Printers);
- if (BrowseRemoteOptions)
- {
- if (BrowseRemoteOptions[0] == '?')
+ if (len > 0 &&
+ !strncasecmp(p->name, name + offset, len) &&
+ (p->name[len] == '\0' || p->name[len] == '@'))
{
/*
- * Override server-supplied options...
+ * We have more than one printer with the same name; see if
+ * we have a class, and if this printer is a member...
*/
- snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions);
- }
- else if (ipp_options)
- {
- /*
- * Combine the server and local options...
- */
+ if (pclass && strcasecmp(pclass->name, name))
+ {
+ if (update)
+ cupsdSetPrinterAttrs(pclass);
+
+ update = 0;
+ pclass = NULL;
+ }
+
+ if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
+ {
+ /*
+ * Need to add the class...
+ */
- snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options,
- BrowseRemoteOptions);
- }
- else
- {
- /*
- * Just use the local options...
- */
+ pclass = cupsdAddPrinter(name);
+ cupsArrayAdd(ImplicitPrinters, pclass);
- snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions);
- }
+ pclass->type |= CUPS_PRINTER_IMPLICIT;
+ pclass->accepting = 1;
+ pclass->state = IPP_PRINTER_IDLE;
- uri = finaluri;
- }
- else if (ipp_options)
- {
- /*
- * Just use the server-supplied options...
- */
+ cupsdSetString(&pclass->location, p->location);
+ cupsdSetString(&pclass->info, p->info);
- snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
- uri = finaluri;
- }
+ update = 1;
- /*
- * See if we already have it listed in the Printers list, and add it if not...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...",
+ name);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "Implicit class \'%s\' added by directory services.",
+ name);
+ }
- type |= CUPS_PRINTER_REMOTE;
- type &= ~CUPS_PRINTER_IMPLICIT;
- update = 0;
- hptr = strchr(host, '.');
- sptr = strchr(ServerName, '.');
+ if (first != NULL)
+ {
+ for (i = 0; i < pclass->num_printers; i ++)
+ if (pclass->printers[i] == first)
+ break;
- if (sptr != NULL && hptr != NULL)
- {
- /*
- * Strip the common domain name components...
- */
+ if (i >= pclass->num_printers)
+ {
+ first->in_implicit_class = 1;
+ cupsdAddPrinterToClass(pclass, first);
+ }
- while (hptr != NULL)
- {
- if (!strcasecmp(hptr, sptr))
+ first = NULL;
+ }
+
+ for (i = 0; i < pclass->num_printers; i ++)
+ if (pclass->printers[i] == p)
+ break;
+
+ if (i >= pclass->num_printers)
{
- *hptr = '\0';
- break;
+ p->in_implicit_class = 1;
+ cupsdAddPrinterToClass(pclass, p);
+ update = 1;
}
- else
- hptr = strchr(hptr + 1, '.');
}
- }
+ else
+ {
+ /*
+ * First time around; just get name length and mark it as first
+ * in the list...
+ */
- if (type & CUPS_PRINTER_CLASS)
- {
- /*
- * Remote destination is a class...
- */
+ if ((hptr = strchr(p->name, '@')) != NULL)
+ len = hptr - p->name;
+ else
+ len = strlen(p->name);
- if (!strncmp(resource, "/classes/", 9))
- snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
- else
- return;
+ strncpy(name, p->name, len);
+ name[len] = '\0';
+ offset = 0;
- if ((p = cupsdFindClass(name)) == NULL && BrowseShortNames)
- {
- if ((p = cupsdFindClass(resource + 9)) != NULL)
+ if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
+ !(first->type & CUPS_PRINTER_IMPLICIT))
{
- if (p->hostname && strcasecmp(p->hostname, host))
+ /*
+ * 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))
{
/*
- * 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...
+ * Add "Any" to the class 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);
-
- cupsArrayRemove(Printers, p);
- cupsdSetStringf(&p->name, "%s@%s", p->name, p->hostname);
- cupsdSetPrinterAttrs(p);
- cupsArrayAdd(Printers, p);
-
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Class \'%s\' added by directory services.",
- p->name);
- }
-
- p = NULL;
+ strcpy(name, "Any");
+ strncpy(name + 3, p->name, len);
+ name[len + 3] = '\0';
+ offset = 3;
}
- else if (!p->hostname)
+ else
{
/*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
+ * Don't create an implicit class if we have a local printer
+ * with the same name...
*/
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
+ len = 0;
+ cupsArrayRestore(Printers);
+ continue;
+ }
}
- else
- {
- /*
- * Use the short name for this shared class.
- */
- strlcpy(name, resource + 9, sizeof(name));
- }
+ first = p;
}
- else if (p && !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;
- }
+ cupsArrayRestore(Printers);
+ }
- if (!p)
- {
- /*
- * Class doesn't exist; add it...
- */
+ /*
+ * Update the last printer class as needed...
+ */
- p = cupsdAddClass(name);
+ if (pclass && update)
+ cupsdSetPrinterAttrs(pclass);
+}
- cupsdLogMessage(CUPSD_LOG_INFO, "Added remote class \"%s\"...", name);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Class \'%s\' added by directory services.", name);
+/*
+ * 'send_cups_browse()' - Send new browsing information using the CUPS
+ * protocol.
+ */
- /*
- * Force the URI to point to the real server...
- */
+static void
+send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */
+{
+ int i; /* Looping var */
+ cups_ptype_t type; /* Printer type */
+ cupsd_dirsvc_addr_t *b; /* Browse address */
+ int bytes; /* Length of packet */
+ char packet[1453], /* Browse data packet */
+ uri[1024], /* Printer URI */
+ location[1024], /* printer-location */
+ info[1024], /* printer-info */
+ make_model[1024];
+ /* printer-make-and-model */
+ cupsd_netif_t *iface; /* Network interface */
- p->type = type & ~CUPS_PRINTER_REJECTING;
- p->accepting = 1;
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- cupsdSetString(&p->hostname, host);
- update = 1;
- }
+ /*
+ * Figure out the printer type value...
+ */
+
+ type = p->type | CUPS_PRINTER_REMOTE;
+
+ if (!p->accepting)
+ type |= CUPS_PRINTER_REJECTING;
+
+ if (p == DefaultPrinter)
+ type |= CUPS_PRINTER_DEFAULT;
+
+ /*
+ * Remove quotes from printer-info, printer-location, and
+ * printer-make-and-model attributes...
+ */
+
+ dequote(location, p->location, sizeof(location));
+ dequote(info, p->info, sizeof(info));
+
+ if (p->make_model)
+ dequote(make_model, p->make_model, sizeof(make_model));
+ else if (p->type & CUPS_PRINTER_CLASS)
+ {
+ if (p->num_printers > 0 && p->printers[0]->make_model)
+ strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model));
+ else
+ strlcpy(make_model, "Local Printer Class", sizeof(make_model));
}
+ else if (p->raw)
+ strlcpy(make_model, "Local Raw Printer", sizeof(make_model));
else
- {
- /*
- * Remote destination is a printer...
- */
+ strlcpy(make_model, "Local System V Printer", sizeof(make_model));
- if (!strncmp(resource, "/printers/", 10))
- snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
- else
- return;
+ /*
+ * Send a packet to each browse address...
+ */
- if ((p = cupsdFindPrinter(name)) == NULL && BrowseShortNames)
+ for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
+ if (b->iface[0])
{
- if ((p = cupsdFindPrinter(resource + 10)) != NULL)
+ /*
+ * Send the browse packet to one or more interfaces...
+ */
+
+ if (!strcmp(b->iface, "*"))
{
- if (p->hostname && strcasecmp(p->hostname, host))
+ /*
+ * Send to all local interfaces...
+ */
+
+ cupsdNetIFUpdate();
+
+ for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
+ iface;
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
{
/*
- * 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...
+ * Only send to local, IPv4 interfaces...
*/
- 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);
+ if (!iface->is_local || !iface->port ||
+ iface->address.addr.sa_family != AF_INET)
+ continue;
- cupsArrayRemove(Printers, p);
- cupsdSetStringf(&p->name, "%s@%s", p->name, p->hostname);
- cupsdSetPrinterAttrs(p);
- cupsArrayAdd(Printers, p);
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ iface->hostname, iface->port,
+ (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
+ "/printers/%s",
+ p->name);
+ snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
+ type, p->state, uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "");
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Printer \'%s\' added by directory services.",
- p->name);
- }
+ bytes = strlen(packet);
- 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...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
+ iface->name, packet);
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
+ iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+
+ sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(iface->broadcast),
+ sizeof(struct sockaddr_in));
}
}
- else
+ else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
{
/*
- * Use the short name for this shared printer.
+ * Send to the named interface using the IPv4 address...
*/
- 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...
- */
-
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
+ while (iface)
+ if (strcmp(b->iface, iface->name))
+ {
+ iface = NULL;
+ break;
+ }
+ else if (iface->address.addr.sa_family == AF_INET && iface->port)
+ break;
+ else
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
- if (!p)
- {
- /*
- * Printer doesn't exist; add it...
- */
+ if (iface)
+ {
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ iface->hostname, iface->port,
+ (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" :
+ "/printers/%s",
+ p->name);
+ snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
+ type, p->state, uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "");
- p = cupsdAddPrinter(name);
+ bytes = strlen(packet);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Printer \'%s\' added by directory services.", name);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
+ iface->name, packet);
- cupsdLogMessage(CUPSD_LOG_INFO, "Added remote printer \"%s\"...", name);
+ iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+ sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(iface->broadcast),
+ sizeof(struct sockaddr_in));
+ }
+ }
+ }
+ else
+ {
/*
- * Force the URI to point to the real server...
+ * Send the browse packet to the indicated address using
+ * the default server name...
*/
- p->type = type & ~CUPS_PRINTER_REJECTING;
- p->accepting = 1;
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
+ 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 : "");
- update = 1;
- }
- }
+ bytes = strlen(packet);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
- /*
- * Update the state...
- */
+ if (sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(b->to),
+ sizeof(struct sockaddr_in)) <= 0)
+ {
+ /*
+ * Unable to send browse packet, so remove this address from the
+ * list...
+ */
- p->state = state;
- p->browse_time = time(NULL);
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "cupsdSendBrowseList: sendto failed for browser "
+ "%d - %s.",
+ (int)(b - Browsers + 1), strerror(errno));
- if (type & CUPS_PRINTER_REJECTING)
- {
- type &= ~CUPS_PRINTER_REJECTING;
+ if (i > 1)
+ memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
- if (p->accepting)
- {
- update = 1;
- p->accepting = 0;
+ b --;
+ NumBrowsers --;
+ }
}
- }
- else if (!p->accepting)
- {
- update = 1;
- p->accepting = 1;
- }
-
- if (p->type != type)
- {
- p->type = type;
- update = 1;
- }
-
- if (location && (!p->location || strcmp(p->location, location)))
- {
- cupsdSetString(&p->location, location);
- update = 1;
- }
-
- if (info && (!p->info || strcmp(p->info, info)))
- {
- cupsdSetString(&p->info, info);
- update = 1;
- }
-
- 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);
- }
- 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;
- }
- 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);
+#ifdef HAVE_OPENLDAP
+/*
+ * 'send_ldap_browse()' - Send LDAP printer registrations.
+ */
- cupsdExpireSubscriptions(p, NULL);
-
- cupsdDeletePrinter(p, 1);
- cupsdUpdateImplicitClasses();
- }
- else if (update)
- {
- cupsdSetPrinterAttrs(p);
- cupsdUpdateImplicitClasses();
- }
+static void
+send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */
+{
+ int i; /* Looping var... */
+ LDAPMod mods[7]; /* The 7 attributes we will be adding */
+ LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */
+ LDAPMessage *res; /* Search result token */
+ char *cn_value[2], /* Change records */
+ *uri[2],
+ *info[2],
+ *location[2],
+ *make_model[2],
+ *type[2],
+ typestring[255], /* String to hold printer-type */
+ filter[256], /* Search filter for possible UPDATEs */
+ dn[1024]; /* DN of the printer we are adding */
+ int rc; /* LDAP status */
+ static const char * const objectClass_values[] =
+ { /* The 3 objectClass's we use in */
+ "top", /* our LDAP entries */
+ "device",
+ "cupsPrinter",
+ NULL
+ };
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s\n", p->name);
/*
- * See if we have a default printer... If not, make the first printer the
- * default.
+ * Everything in ldap is ** so we fudge around it...
*/
- if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault)
+ 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)
{
/*
- * Find the first network default printer and use it...
+ * Printer has already been registered, modify the current
+ * registration...
*/
- for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
- p;
- p = (cupsd_printer_t *)cupsArrayNext(Printers))
- if (p->type & CUPS_PRINTER_DEFAULT)
- {
- DefaultPrinter = p;
- break;
- }
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: Replacing entry...");
- /*
- * Do auto-classing if needed...
- */
+ for (i = 0; i < 7; i ++)
+ {
+ pmods[i] = mods + i;
+ pmods[i]->mod_op = LDAP_MOD_REPLACE;
+ }
+ pmods[i] = NULL;
- process_implicit_classes();
+ 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...
+ */
- /*
- * Update the printcap file...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: Adding entry...");
- cupsdWritePrintcap();
+ 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
/*
- * 'process_implicit_classes()' - Create/update implicit classes as needed.
+ * 'send_slp_browse()' - Register the specified printer with SLP.
*/
static void
-process_implicit_classes(void)
+send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */
{
- 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 srvurl[HTTP_MAX_URI], /* Printer service URI */
+ attrs[8192], /* Printer attributes */
+ finishings[1024], /* Finishings to support */
+ make_model[IPP_MAX_NAME * 2],
+ /* Make and model, quoted */
+ location[IPP_MAX_NAME * 2],
+ /* Location, quoted */
+ info[IPP_MAX_NAME * 2], /* Info, quoted */
+ *src, /* Pointer to original string */
+ *dst; /* Pointer to destination string */
+ ipp_attribute_t *authentication; /* uri-authentication-supported value */
+ SLPError error; /* SLP error, if any */
- if (!ImplicitClasses || !Printers)
- return;
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p,
+ p->name);
/*
- * Loop through all available printers and create classes as needed...
+ * Make the SLP service URL that conforms to the IANA
+ * 'printer:' template.
*/
- 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...
- */
+ snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
- if (p->type & CUPS_PRINTER_IMPLICIT)
- {
- len = 0;
- continue;
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
- /*
- * If len == 0, get the length of this printer name up to the "@"
- * sign (if any).
- */
+ /*
+ * Figure out the finishings string...
+ */
- cupsArraySave(Printers);
+ if (p->type & CUPS_PRINTER_STAPLE)
+ strcpy(finishings, "staple");
+ else
+ finishings[0] = '\0';
- if (len > 0 &&
- !strncasecmp(p->name, name + offset, len) &&
- (p->name[len] == '\0' || p->name[len] == '@'))
- {
- /*
- * We have more than one printer with the same name; see if
- * we have a class, and if this printer is a member...
- */
+ if (p->type & CUPS_PRINTER_BIND)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",bind", sizeof(finishings));
+ else
+ strcpy(finishings, "bind");
+ }
- if (pclass && strcasecmp(pclass->name, name))
- {
- if (update)
- cupsdSetPrinterAttrs(pclass);
+ if (p->type & CUPS_PRINTER_PUNCH)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",punch", sizeof(finishings));
+ else
+ strcpy(finishings, "punch");
+ }
- update = 0;
- pclass = NULL;
- }
+ if (p->type & CUPS_PRINTER_COVER)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",cover", sizeof(finishings));
+ else
+ strcpy(finishings, "cover");
+ }
- if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
- {
- /*
- * Need to add the class...
- */
+ if (p->type & CUPS_PRINTER_SORT)
+ {
+ if (finishings[0])
+ strlcat(finishings, ",sort", sizeof(finishings));
+ else
+ strcpy(finishings, "sort");
+ }
- pclass = cupsdAddPrinter(name);
- cupsArrayAdd(ImplicitPrinters, pclass);
+ if (!finishings[0])
+ strcpy(finishings, "none");
- pclass->type |= CUPS_PRINTER_IMPLICIT;
- pclass->accepting = 1;
- pclass->state = IPP_PRINTER_IDLE;
+ /*
+ * Quote any commas in the make and model, location, and info strings...
+ */
- cupsdSetString(&pclass->location, p->location);
- cupsdSetString(&pclass->info, p->info);
+ for (src = p->make_model, dst = make_model;
+ src && *src && dst < (make_model + sizeof(make_model) - 2);)
+ {
+ if (*src == ',' || *src == '\\' || *src == ')')
+ *dst++ = '\\';
- update = 1;
+ *dst++ = *src++;
+ }
- cupsdLogMessage(CUPSD_LOG_INFO, "Added implicit class \"%s\"...",
- name);
- }
+ *dst = '\0';
- if (first != NULL)
- {
- for (i = 0; i < pclass->num_printers; i ++)
- if (pclass->printers[i] == first)
- break;
+ if (!make_model[0])
+ strcpy(make_model, "Unknown");
- if (i >= pclass->num_printers)
- {
- first->in_implicit_class = 1;
- cupsdAddPrinterToClass(pclass, first);
- }
+ for (src = p->location, dst = location;
+ src && *src && dst < (location + sizeof(location) - 2);)
+ {
+ if (*src == ',' || *src == '\\' || *src == ')')
+ *dst++ = '\\';
- first = NULL;
- }
+ *dst++ = *src++;
+ }
- for (i = 0; i < pclass->num_printers; i ++)
- if (pclass->printers[i] == p)
- break;
+ *dst = '\0';
- if (i >= pclass->num_printers)
- {
- p->in_implicit_class = 1;
- cupsdAddPrinterToClass(pclass, p);
- update = 1;
- }
- }
- else
- {
- /*
- * First time around; just get name length and mark it as first
- * in the list...
- */
+ if (!location[0])
+ strcpy(location, "Unknown");
- if ((hptr = strchr(p->name, '@')) != NULL)
- len = hptr - p->name;
- else
- len = strlen(p->name);
+ for (src = p->info, dst = info;
+ src && *src && dst < (info + sizeof(info) - 2);)
+ {
+ if (*src == ',' || *src == '\\' || *src == ')')
+ *dst++ = '\\';
- strncpy(name, p->name, len);
- name[len] = '\0';
- offset = 0;
+ *dst++ = *src++;
+ }
- if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
- !(first->type & CUPS_PRINTER_IMPLICIT))
- {
- /*
- * Can't use same name as a local printer; add "Any" to the
- * front of the name, unless we have explicitly disabled
- * the "ImplicitAnyClasses"...
- */
+ *dst = '\0';
- if (ImplicitAnyClasses && len < (sizeof(name) - 4))
- {
- /*
- * Add "Any" to the class name...
- */
+ if (!info[0])
+ strcpy(info, "Unknown");
- 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...
- */
+ /*
+ * Get the authentication value...
+ */
- len = 0;
- cupsArrayRestore(Printers);
- continue;
- }
- }
+ authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
+ IPP_TAG_KEYWORD);
- first = p;
- }
+ /*
+ * Make the SLP attribute string list that conforms to
+ * the IANA 'printer:' template.
+ */
- cupsArrayRestore(Printers);
- }
+ snprintf(attrs, sizeof(attrs),
+ "(printer-uri-supported=%s),"
+ "(uri-authentication-supported=%s>),"
+#ifdef HAVE_SSL
+ "(uri-security-supported=tls>),"
+#else
+ "(uri-security-supported=none>),"
+#endif /* HAVE_SSL */
+ "(printer-name=%s),"
+ "(printer-location=%s),"
+ "(printer-info=%s),"
+ "(printer-more-info=%s),"
+ "(printer-make-and-model=%s),"
+ "(printer-type=%d),"
+ "(charset-supported=utf-8),"
+ "(natural-language-configured=%s),"
+ "(natural-language-supported=de,en,es,fr,it),"
+ "(color-supported=%s),"
+ "(finishings-supported=%s),"
+ "(sides-supported=one-sided%s),"
+ "(multiple-document-jobs-supported=true)"
+ "(ipp-versions-supported=1.0,1.1)",
+ p->uri, authentication->values[0].string.text, p->name, location,
+ info, p->uri, make_model, p->type, DefaultLanguage,
+ p->type & CUPS_PRINTER_COLOR ? "true" : "false",
+ finishings,
+ p->type & CUPS_PRINTER_DUPLEX ?
+ ",two-sided-long-edge,two-sided-short-edge" : "");
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
/*
- * Update the last printer class as needed...
+ * Register the printer with the SLP server...
*/
- if (pclass && update)
- cupsdSetPrinterAttrs(pclass);
+ 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);
}
-#ifdef HAVE_LIBSLP
/*
* 'slp_attr_callback()' - SLP attribute callback
*/
SLPError errcode, /* I - Parsing status for this attr */
void *cookie) /* I - Current printer */
{
- char *tmp = 0;
+ char *tmp = 0; /* Temporary string */
cupsd_printer_t *p = (cupsd_printer_t*)cookie;
+ /* Current printer */
- /*
- * Let the compiler know we won't be using these...
- */
-
- (void)hslp;
+ (void)hslp; /* anti-compiler-warning-code */
/*
* Bail if there was an error
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"))
- 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;
+ if (!slp_get_attr(attrlist, "(printer-type=", &tmp))
+ p->type = atoi(tmp);
+ else
+ p->type = CUPS_PRINTER_REMOTE;
cupsdClearString(&tmp);
/*
- * End of "$Id: dirsvc.c 5117 2006-02-16 14:10:43Z mike $".
+ * End of "$Id: dirsvc.c 5663 2006-06-15 20:36:42Z mike $".
*/