/*
* User-defined destination (and option) support for CUPS.
*
- * Copyright 2007-2018 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products.
+ * Copyright © 2007-2019 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products.
*
- * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more
+ * information.
*/
/*
*/
#include "cups-private.h"
+#include "debug-internal.h"
#include <sys/stat.h>
#ifdef HAVE_NOTIFY_H
*/
#ifdef __APPLE__
-# if !TARGET_OS_IOS
+# if HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME
# include <SystemConfiguration/SystemConfiguration.h>
# define _CUPS_LOCATION_DEFAULTS 1
-# endif /* !TARGET_OS_IOS */
+# endif /* HAVE_SCDYNAMICSTORECOPYCOMPUTERNAME */
# define kCUPSPrintingPrefs CFSTR("org.cups.PrintingPrefs")
# define kDefaultPaperIDKey CFSTR("DefaultPaperID")
# define kLastUsedPrintersKey CFSTR("LastUsedPrinters")
/*
- * 'cupsConnectDest()' - Open a conection to the destination.
+ * 'cupsConnectDest()' - Open a connection to the destination.
*
* Connect to the destination, returning a new @code http_t@ connection object
* and optionally the resource path to use for the destination. These calls
* returns 0. The caller is responsible for calling @link httpClose@ on the
* returned connection.
*
- * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
+ * Starting with CUPS 2.2.4, the caller can pass @code CUPS_DEST_FLAGS_DEVICE@
* for the "flags" argument to connect directly to the device associated with
* the destination. Otherwise, the connection is made to the CUPS scheduler
* associated with the destination.
* '_cupsGetDestResource()' - Get the resource path and URI for a destination.
*/
-const char * /* O - Printer URI */
+const char * /* O - URI */
_cupsGetDestResource(
cups_dest_t *dest, /* I - Destination */
unsigned flags, /* I - Destination flags */
char *resource, /* I - Resource buffer */
size_t resourcesize) /* I - Size of resource buffer */
{
- const char *uri; /* Printer URI */
+ const char *uri, /* URI */
+ *device_uri, /* Device URI */
+ *printer_uri; /* Printer URI */
char scheme[32], /* URI scheme */
userpass[256], /* Username and password (unused) */
hostname[256]; /* Hostname */
int port; /* Port number */
- DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), resource=%p, resourcesize=%d)", (void *)dest, dest->name, (void *)resource, (int)resourcesize));
+ DEBUG_printf(("_cupsGetDestResource(dest=%p(%s), flags=%u, resource=%p, resourcesize=%d)", (void *)dest, dest->name, flags, (void *)resource, (int)resourcesize));
/*
* Range check input...
}
/*
- * Grab the printer URI...
+ * Grab the printer and device URIs...
*/
- if (!(flags & CUPS_DEST_FLAGS_DEVICE))
- uri = NULL;
- else
- uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options);
+ device_uri = cupsGetOption("device-uri", dest->num_options, dest->options);
+ printer_uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options);
- if (uri)
- {
- DEBUG_printf(("1_cupsGetDestResource: printer-uri-supported=\"%s\"", uri));
- }
- else
+ DEBUG_printf(("1_cupsGetDestResource: device-uri=\"%s\", printer-uri-supported=\"%s\".", device_uri, printer_uri));
+
+#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
+ if (((flags & CUPS_DEST_FLAGS_DEVICE) || !printer_uri) && strstr(device_uri, "._tcp"))
{
- if ((uri = cupsGetOption("device-uri", dest->num_options, dest->options)) != NULL)
+ if ((device_uri = cups_dnssd_resolve(dest, device_uri, 5000, NULL, NULL, NULL)) != NULL)
{
-#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
- if (strstr(uri, "._tcp"))
- {
- uri = cups_dnssd_resolve(dest, uri, 5000, NULL, NULL, NULL);
+ DEBUG_printf(("1_cupsGetDestResource: Resolved device-uri=\"%s\".", device_uri));
+ }
+ else
+ {
+ DEBUG_puts("1_cupsGetDestResource: Unable to resolve device.");
- if (uri)
- DEBUG_printf(("1_cupsGetDestResource: Resolved device-uri=\"%s\"", uri));
- }
- else
-#endif /* HAVE_DNSSD || HAVE_AVAHI */
+ if (resource)
+ *resource = '\0';
- DEBUG_printf(("1_cupsGetDestResource: device-uri=\"%s\"", uri));
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOENT), 0);
+
+ return (NULL);
}
+ }
+#endif /* HAVE_DNSSD || HAVE_AVAHI */
- if (uri && !(flags & CUPS_DEST_FLAGS_DEVICE))
- {
- uri = _cupsCreateDest(dest->name, cupsGetOption("printer-info", dest->num_options, dest->options), NULL, uri, resource, resourcesize);
+ if (flags & CUPS_DEST_FLAGS_DEVICE)
+ {
+ uri = device_uri;
+ }
+ else if (printer_uri)
+ {
+ uri = printer_uri;
+ }
+ else
+ {
+ uri = _cupsCreateDest(dest->name, cupsGetOption("printer-info", dest->num_options, dest->options), NULL, device_uri, resource, resourcesize);
- if (uri)
- {
- DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri));
+ if (uri)
+ {
+ DEBUG_printf(("1_cupsGetDestResource: Local printer-uri-supported=\"%s\"", uri));
- dest->num_options = cupsAddOption("printer-uri-supported", uri, dest->num_options, &dest->options);
+ dest->num_options = cupsAddOption("printer-uri-supported", uri, dest->num_options, &dest->options);
- uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options);
- }
+ uri = cupsGetOption("printer-uri-supported", dest->num_options, dest->options);
}
}
return (NULL);
}
- else
+ else if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
{
- if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, (int)resourcesize) < HTTP_URI_STATUS_OK)
- {
- _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad URI."), 1);
+ _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad URI."), 1);
- return (NULL);
- }
+ return (NULL);
}
DEBUG_printf(("1_cupsGetDestResource: resource=\"%s\"", resource));
name = resource + 10;
info = temp;
}
+ else if (!strncmp(resource, "/ipp/print/", 11))
+ {
+ snprintf(temp, sizeof(temp), "%s @ %s", resource + 11, hostname);
+ name = resource + 11;
+ info = temp;
+ }
else
{
name = hostname;
cups_dest_t *dest; /* Destination */
char filename[1024], /* Path to lpoptions */
defname[256]; /* Default printer name */
- const char *home = getenv("HOME"); /* Home directory */
int set_as_default = 0; /* Set returned destination as default */
ipp_op_t op = IPP_OP_GET_PRINTER_ATTRIBUTES;
/* IPP operation to get server ops */
else
instance = NULL;
}
- else if (home)
+ else if (cg->home)
{
/*
* No default in the environment, try the user's lpoptions files...
*/
- snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
+ snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
dest_name = cups_get_default(filename, defname, sizeof(defname), &instance);
cupsEnumDests(0, 1000, NULL, 0, 0, (cups_dest_cb_t)cups_name_cb, &data);
if (!data.dest)
+ {
+ _cupsSetError(IPP_STATUS_ERROR_NOT_FOUND, _("The printer or class does not exist."), 1);
return (NULL);
+ }
dest = data.dest;
}
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
cups_get_dests(filename, dest_name, instance, 0, 1, 1, &dest);
- if (home)
+ if (cg->home)
{
- snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
+ snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
cups_get_dests(filename, dest_name, instance, 0, 1, 1, &dest);
}
cups_option_t *option; /* Current option */
_ipp_option_t *match; /* Matching attribute for option */
FILE *fp; /* File pointer */
-#ifndef WIN32
- const char *home; /* HOME environment variable */
-#endif /* WIN32 */
char filename[1024]; /* lpoptions file */
int num_temps; /* Number of temporary destinations */
cups_dest_t *temps = NULL, /* Temporary destinations */
snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
-#ifndef WIN32
- if (getuid())
+ if (cg->home)
{
/*
- * Point to user defaults...
+ * Create ~/.cups subdirectory...
*/
- if ((home = getenv("HOME")) != NULL)
- {
- /*
- * Create ~/.cups subdirectory...
- */
-
- snprintf(filename, sizeof(filename), "%s/.cups", home);
- if (access(filename, 0))
- mkdir(filename, 0700);
+ snprintf(filename, sizeof(filename), "%s/.cups", cg->home);
+ if (access(filename, 0))
+ mkdir(filename, 0700);
- snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
- }
+ snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
}
-#endif /* !WIN32 */
/*
* Try to open the file...
return (-1);
}
-#ifndef WIN32
+#ifndef _WIN32
/*
* Set the permissions to 0644 when saving to the /etc/cups/lpoptions
* file...
if (!getuid())
fchmod(fileno(fp), 0644);
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
/*
* Write each printer; each line looks like:
* system preferences...
*/
- if ((locprinter = _cupsAppleCopyDefaultPrinter()) != NULL)
+ if (!getenv("CUPS_NO_APPLE_DEFAULT") && (locprinter = _cupsAppleCopyDefaultPrinter()) != NULL)
{
CFStringGetCString(locprinter, name, (CFIndex)namesize, kCFStringEncodingUTF8);
CFRelease(locprinter);
int i, j, /* Looping vars */
num_dests; /* Number of destinations */
cups_dest_t *dests = NULL, /* Destinations */
- *dest, /* Current destination */
- *user_dest; /* User destination */
+ *dest; /* Current destination */
cups_option_t *option; /* Current option */
- char *user_default; /* User default printer */
+ const char *user_default; /* Default printer from environment */
#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
int count, /* Number of queries started */
completed, /* Number of completed queries */
#else
_cups_getdata_t data; /* Data for callback */
#endif /* HAVE_DNSSD || HAVE_AVAHI */
- const char *home; /* HOME environment variable */
char filename[1024]; /* Local lpoptions file */
_cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
memset(&data, 0, sizeof(data));
- if ((user_default = _cupsUserDefault(data.def_name, sizeof(data.def_name))) == NULL)
+ user_default = _cupsUserDefault(data.def_name, sizeof(data.def_name));
+
+ snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
+ data.num_dests = cups_get_dests(filename, NULL, NULL, 1, user_default != NULL, data.num_dests, &data.dests);
+
+ if (cg->home)
+ {
+ snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", cg->home);
+
+ data.num_dests = cups_get_dests(filename, NULL, NULL, 1, user_default != NULL, data.num_dests, &data.dests);
+ }
+
+ if (!user_default && (dest = cupsGetDest(NULL, NULL, data.num_dests, data.dests)) != NULL)
{
- const char *defprinter = cupsGetDefault2(http);
- /* Server default, if any */
+ /*
+ * Use an lpoptions default printer...
+ */
+
+ if (dest->instance)
+ snprintf(data.def_name, sizeof(data.def_name), "%s/%s", dest->name, dest->instance);
+ else
+ strlcpy(data.def_name, dest->name, sizeof(data.def_name));
+ }
+ else
+ {
+ const char *default_printer; /* Server default printer */
- if (defprinter)
- strlcpy(data.def_name, defprinter, sizeof(data.def_name));
+ if ((default_printer = cupsGetDefault2(http)) != NULL)
+ strlcpy(data.def_name, default_printer, sizeof(data.def_name));
}
if (data.def_name[0])
DEBUG_printf(("1cups_enum_dests: def_name=\"%s\", def_instance=\"%s\"", data.def_name, data.def_instance));
- snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
- data.num_dests = cups_get_dests(filename, NULL, NULL, 1, user_default != NULL, data.num_dests, &data.dests);
-
- if ((home = getenv("HOME")) != NULL)
- {
- snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
-
- data.num_dests = cups_get_dests(filename, NULL, NULL, 1, user_default != NULL, data.num_dests, &data.dests);
- }
-
/*
* Get ready to enumerate...
*/
i > 0 && (!cancel || !*cancel);
i --, dest ++)
{
+ cups_dest_t *user_dest; /* Destination from lpoptions */
#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
- const char *device_uri; /* Device URI */
+ const char *device_uri; /* Device URI */
#endif /* HAVE_DNSSD || HAVE_AVAHI */
if ((user_dest = cupsGetDest(dest->name, dest->instance, data.num_dests, data.dests)) != NULL)
if ((device->type & mask) == type)
{
+ cups_dest_t *user_dest; /* Destination from lpoptions */
+
dest = &device->dest;
if ((user_dest = cupsGetDest(dest->name, dest->instance, data.num_dests, data.dests)) != NULL)
*nameptr++ = '_';
}
+ /*
+ * Remove an underscore if it is the last character and isn't the only
+ * character in the name...
+ */
+
+ if (nameptr > (name + 1) && nameptr[-1] == '_')
+ nameptr --;
+
*nameptr = '\0';
}