*
* "lpadmin" command for CUPS.
*
- * Copyright 2007-2014 by Apple Inc.
+ * Copyright 2007-2015 by Apple Inc.
* Copyright 1997-2006 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* Include necessary headers...
*/
+#define _CUPS_NO_DEPRECATED
+#define _PPD_DEPRECATED
#include <cups/cups-private.h>
static int delete_printer_option(http_t *http, char *printer,
char *option);
static int enable_printer(http_t *http, char *printer);
+static char *get_printer_ppd(const char *uri, char *buffer, size_t bufsize);
static cups_ptype_t get_printer_type(http_t *http, char *printer, char *uri,
size_t urisize);
static int set_printer_options(http_t *http, char *printer,
*val; /* Pointer to allow/deny value */
int num_options; /* Number of options */
cups_option_t *options; /* Options */
- char *file; /* New PPD file/interface script */
+ char *file, /* New PPD file/interface script */
+ evefile[1024] = "";
+ /* IPP Everywhere PPD */
+ const char *ppd_name, /* ppd-name value */
+ *device_uri; /* device-uri value */
_cupsSetLocale(argv);
case 'c' : /* Add printer to class */
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(),
- cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
case 'd' : /* Set as default destination */
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(),
- cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
if (printer == NULL)
{
#ifdef HAVE_SSL
- cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
+ cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED);
if (http)
- httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+ httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
#else
_cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."),
argv[0]);
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(),
- cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
case 'r' : /* Remove printer from class */
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(),
- cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
case 'R' : /* Remove option */
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(),
- cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
case 'x' : /* Delete a printer */
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(),
- cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
* Set options as needed...
*/
+ if ((ppd_name = cupsGetOption("ppd-name", num_options, options)) != NULL && !strcmp(ppd_name, "everywhere") && (device_uri = cupsGetOption("device-uri", num_options, options)) != NULL)
+ {
+ if ((file = get_printer_ppd(device_uri, evefile, sizeof(evefile))) == NULL)
+ return (1);
+
+ num_options = cupsRemoveOption("ppd-name", num_options, &options);
+ }
+
if (num_options || file)
{
if (!http)
{
- http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
+ http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL);
if (http == NULL)
{
return (1);
}
+ if (evefile[0])
+ unlink(evefile);
+
if (printer == NULL)
{
_cupsLangPuts(stdout,
printer, pclass));
/*
- * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
+ * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
* attributes:
*
* attributes-charset
* requesting-user-name
*/
- request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+ request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/classes/%s", pclass);
response = cupsDoRequest(http, request, "/");
/*
- * Build a CUPS_ADD_MODIFY_CLASS request, which requires the following
+ * Build a CUPS-Add-Modify-Class request, which requires the following
* attributes:
*
* attributes-charset
* member-uris
*/
- request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippDelete(response);
ippDelete(cupsDoRequest(http, request, "/admin/"));
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
DEBUG_printf(("default_printer(%p, \"%s\")\n", http, printer));
/*
- * Build a CUPS_SET_DEFAULT request, which requires the following
+ * Build a CUPS-Set-Default request, which requires the following
* attributes:
*
* attributes-charset
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", printer);
- request = ippNewRequest(CUPS_SET_DEFAULT);
+ request = ippNewRequest(IPP_OP_CUPS_SET_DEFAULT);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippDelete(cupsDoRequest(http, request, "/admin/"));
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
DEBUG_printf(("delete_printer(%p, \"%s\")\n", http, printer));
/*
- * Build a CUPS_DELETE_PRINTER request, which requires the following
+ * Build a CUPS-Delete-Printer request, which requires the following
* attributes:
*
* attributes-charset
* requesting-user-name
*/
- request = ippNewRequest(CUPS_DELETE_PRINTER);
+ request = ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/printers/%s", printer);
ippDelete(cupsDoRequest(http, request, "/admin/"));
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
printer, pclass));
/*
- * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
+ * Build an IPP_OP_GET_PRINTER_ATTRIBUTES request, which requires the following
* attributes:
*
* attributes-charset
* requesting-user-name
*/
- request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+ request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", 0, "/classes/%s", pclass);
*/
if ((response = cupsDoRequest(http, request, "/classes/")) == NULL ||
- response->request.status.status_code == IPP_NOT_FOUND)
+ response->request.status.status_code == IPP_STATUS_ERROR_NOT_FOUND)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
if (members->num_values == 1)
{
/*
- * Build a CUPS_DELETE_CLASS request, which requires the following
+ * Build a CUPS-Delete-Class request, which requires the following
* attributes:
*
* attributes-charset
* requesting-user-name
*/
- request = ippNewRequest(CUPS_DELETE_CLASS);
+ request = ippNewRequest(IPP_OP_CUPS_DELETE_CLASS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
else
{
/*
- * Build a CUPS_ADD_MODIFY_CLASS request, which requires the following
+ * Build a IPP_OP_CUPS_ADD_MODIFY_CLASS request, which requires the following
* attributes:
*
* attributes-charset
* member-uris
*/
- request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippDelete(cupsDoRequest(http, request, "/admin/"));
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
/*
- * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
+ * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
* requires the following attributes:
*
* attributes-charset
*/
if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
- request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
else
- request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippDelete(cupsDoRequest(http, request, "/admin/"));
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
DEBUG_printf(("enable_printer(%p, \"%s\")\n", http, printer));
/*
- * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
+ * Build a IPP_OP_CUPS_ADD_MODIFY_PRINTER or IPP_OP_CUPS_ADD_MODIFY_CLASS request, which
* require the following attributes:
*
* attributes-charset
*/
if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
- request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
else
- request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
- IPP_PRINTER_IDLE);
+ IPP_PSTATE_IDLE);
ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
/*
ippDelete(cupsDoRequest(http, request, "/admin/"));
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());
}
+/*
+ * 'get_printer_ppd()' - Get an IPP Everywhere PPD file for the given URI.
+ */
+
+static char * /* O - Filename or NULL */
+get_printer_ppd(const char *uri, /* I - Printer URI */
+ char *buffer, /* I - Filename buffer */
+ size_t bufsize) /* I - Size of filename buffer */
+{
+ http_t *http; /* Connection to printer */
+ ipp_t *request, /* Get-Printer-Attributes request */
+ *response; /* Get-Printer-Attributes response */
+ char scheme[32], /* URI scheme */
+ userpass[256], /* Username:password */
+ host[256], /* Hostname */
+ resource[256]; /* Resource path */
+ int port; /* Port number */
+
+
+ /*
+ * Connect to the printer...
+ */
+
+ if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
+ {
+ _cupsLangPrintf(stderr, _("%s: Bad printer URI \"%s\"."), "lpadmin", uri);
+ return (NULL);
+ }
+
+ http = httpConnect2(host, port, NULL, AF_UNSPEC, !strcmp(scheme, "ipps") ? HTTP_ENCRYPTION_ALWAYS : HTTP_ENCRYPTION_IF_REQUESTED, 1, 30000, NULL);
+ if (!http)
+ {
+ _cupsLangPrintf(stderr, _("%s: Unable to connect to \"%s:%d\": %s"), "lpadmin", host, port, cupsLastErrorString());
+ return (NULL);
+ }
+
+ /*
+ * Send a Get-Printer-Attributes request...
+ */
+
+ request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
+ response = cupsDoRequest(http, request, resource);
+
+ if (!_ppdCreateFromIPP(buffer, bufsize, response))
+ _cupsLangPrintf(stderr, _("%s: Unable to create PPD file: %s"), "lpadmin", strerror(errno));
+
+ ippDelete(response);
+ httpClose(http);
+
+ if (buffer[0])
+ return (buffer);
+ else
+ return (NULL);
+}
+
+
/*
* 'get_printer_type()' - Determine the printer type and URI.
*/
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, (int)urisize, "ipp", NULL, "localhost", ippPort(), "/printers/%s", printer);
- request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
+ request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
options, file));
/*
- * Build a CUPS_ADD_MODIFY_PRINTER or CUPS_ADD_MODIFY_CLASS request, which
- * requires the following attributes:
+ * Build a CUPS-Add-Modify-Printer or CUPS-Add-Modify-Class request,
+ * which requires the following attributes:
*
* attributes-charset
* attributes-natural-language
*/
if (get_printer_type(http, printer, uri, sizeof(uri)) & CUPS_PRINTER_CLASS)
- request = ippNewRequest(CUPS_ADD_MODIFY_CLASS);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_CLASS);
else
- request = ippNewRequest(CUPS_ADD_MODIFY_PRINTER);
+ request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
- ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
- "printer-uri", NULL, uri);
- ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
- "requesting-user-name", NULL, cupsUser());
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
+ ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
/*
* Add the options...
*/
+ if (file)
+ ppdfile = file;
+ else if (request->request.op.operation_id == IPP_OP_CUPS_ADD_MODIFY_PRINTER)
+ ppdfile = cupsGetPPD(printer);
+ else
+ ppdfile = NULL;
+
cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
cupsEncodeOptions2(request, num_options, options, IPP_TAG_PRINTER);
NULL, "tbcp");
}
- if (file)
- ppdfile = file;
- else if (request->request.op.operation_id == CUPS_ADD_MODIFY_PRINTER)
- ppdfile = cupsGetPPD(printer);
- else
- ppdfile = NULL;
-
- if (ppdfile != NULL)
+ if (ppdfile)
{
/*
* Set default options in the PPD file...
* Check the response...
*/
- if (cupsLastError() > IPP_OK_CONFLICT)
+ if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
{
_cupsLangPrintf(stderr, _("%s: %s"), "lpadmin", cupsLastErrorString());