| 1 | /*
|
|---|
| 2 | * Support code for Novell iPrint using the Common UNIX Printing
|
|---|
| 3 | * System ("CUPS") libraries
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright 1999-2003 by Michael R Sweet.
|
|---|
| 6 | * Portions Copyright 2005 by Joel J. Smith.
|
|---|
| 7 | *
|
|---|
| 8 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | * it under the terms of the GNU General Public License as published by
|
|---|
| 10 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | * (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This program is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | * GNU General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU General Public License
|
|---|
| 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 | #include "printing.h"
|
|---|
| 24 | #include "printing/pcap.h"
|
|---|
| 25 |
|
|---|
| 26 | #ifdef HAVE_IPRINT
|
|---|
| 27 | #include <cups/cups.h>
|
|---|
| 28 | #include <cups/language.h>
|
|---|
| 29 |
|
|---|
| 30 | #define OPERATION_NOVELL_LIST_PRINTERS 0x401A
|
|---|
| 31 | #define OPERATION_NOVELL_MGMT 0x401C
|
|---|
| 32 | #define NOVELL_SERVER_SYSNAME "sysname="
|
|---|
| 33 | #define NOVELL_SERVER_SYSNAME_NETWARE "NetWare IA32"
|
|---|
| 34 | #define NOVELL_SERVER_VERSION_STRING "iprintserverversion="
|
|---|
| 35 | #define NOVELL_SERVER_VERSION_OES_SP1 33554432
|
|---|
| 36 |
|
|---|
| 37 | #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
|
|---|
| 38 | #define HAVE_CUPS_1_6 1
|
|---|
| 39 | #endif
|
|---|
| 40 |
|
|---|
| 41 | #ifndef HAVE_CUPS_1_6
|
|---|
| 42 | #define ippGetCount(attr) attr->num_values
|
|---|
| 43 | #define ippGetGroupTag(attr) attr->group_tag
|
|---|
| 44 | #define ippGetName(attr) attr->name
|
|---|
| 45 | #define ippGetValueTag(attr) attr->value_tag
|
|---|
| 46 | #define ippGetStatusCode(ipp) ipp->request.status.status_code
|
|---|
| 47 | #define ippGetBoolean(attr, element) attr->values[element].boolean
|
|---|
| 48 | #define ippGetInteger(attr, element) attr->values[element].integer
|
|---|
| 49 | #define ippGetString(attr, element, language) attr->values[element].string.text
|
|---|
| 50 |
|
|---|
| 51 | static ipp_attribute_t *
|
|---|
| 52 | ippFirstAttribute(ipp_t *ipp)
|
|---|
| 53 | {
|
|---|
| 54 | if (!ipp)
|
|---|
| 55 | return (NULL);
|
|---|
| 56 | return (ipp->current = ipp->attrs);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | static ipp_attribute_t *
|
|---|
| 60 | ippNextAttribute(ipp_t *ipp)
|
|---|
| 61 | {
|
|---|
| 62 | if (!ipp || !ipp->current)
|
|---|
| 63 | return (NULL);
|
|---|
| 64 | return (ipp->current = ipp->current->next);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | static int ippSetOperation(ipp_t *ipp, ipp_op_t op)
|
|---|
| 68 | {
|
|---|
| 69 | ipp->request.op.operation_id = op;
|
|---|
| 70 | return (1);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | static int ippSetRequestId(ipp_t *ipp, int request_id)
|
|---|
| 74 | {
|
|---|
| 75 | ipp->request.any.request_id = request_id;
|
|---|
| 76 | return (1);
|
|---|
| 77 | }
|
|---|
| 78 | #endif
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * 'iprint_passwd_cb()' - The iPrint password callback...
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | static const char * /* O - Password or NULL */
|
|---|
| 85 | iprint_passwd_cb(const char *prompt) /* I - Prompt */
|
|---|
| 86 | {
|
|---|
| 87 | /*
|
|---|
| 88 | * Always return NULL to indicate that no password is available...
|
|---|
| 89 | */
|
|---|
| 90 |
|
|---|
| 91 | return (NULL);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | static const char *iprint_server(void)
|
|---|
| 95 | {
|
|---|
| 96 | if ((lp_iprint_server() != NULL) && (strlen(lp_iprint_server()) > 0)) {
|
|---|
| 97 | DEBUG(10, ("iprint server explicitly set to %s\n",
|
|---|
| 98 | lp_iprint_server()));
|
|---|
| 99 | return lp_iprint_server();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | DEBUG(10, ("iprint server left to default %s\n", cupsServer()));
|
|---|
| 103 | return cupsServer();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /*
|
|---|
| 107 | * Pass in an already connected http_t*
|
|---|
| 108 | * Returns the server version if one can be found, multiplied by
|
|---|
| 109 | * -1 for all NetWare versions. Returns 0 if a server version
|
|---|
| 110 | * cannot be determined
|
|---|
| 111 | */
|
|---|
| 112 |
|
|---|
| 113 | static int iprint_get_server_version(http_t *http, char* serviceUri)
|
|---|
| 114 | {
|
|---|
| 115 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 116 | *response = NULL; /* IPP Response */
|
|---|
| 117 | ipp_attribute_t *attr; /* Current attribute */
|
|---|
| 118 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 119 | char *ver; /* server version pointer */
|
|---|
| 120 | char *vertmp; /* server version tmp pointer */
|
|---|
| 121 | int serverVersion = 0; /* server version */
|
|---|
| 122 | char *os; /* server os */
|
|---|
| 123 | int osFlag = 0; /* 0 for NetWare, 1 for anything else */
|
|---|
| 124 | char *temp; /* pointer for string manipulation */
|
|---|
| 125 |
|
|---|
| 126 | /*
|
|---|
| 127 | * Build an OPERATION_NOVELL_MGMT("get-server-version") request,
|
|---|
| 128 | * which requires the following attributes:
|
|---|
| 129 | *
|
|---|
| 130 | * attributes-charset
|
|---|
| 131 | * attributes-natural-language
|
|---|
| 132 | * operation-name
|
|---|
| 133 | * service-uri
|
|---|
| 134 | */
|
|---|
| 135 |
|
|---|
| 136 | request = ippNew();
|
|---|
| 137 |
|
|---|
| 138 | ippSetOperation(request, (ipp_op_t)OPERATION_NOVELL_MGMT);
|
|---|
| 139 | ippSetRequestId(request, 1);
|
|---|
| 140 |
|
|---|
| 141 | language = cupsLangDefault();
|
|---|
| 142 |
|
|---|
| 143 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 144 | "attributes-charset", NULL, "utf-8");
|
|---|
| 145 |
|
|---|
| 146 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 147 | "attributes-natural-language", NULL, language->language);
|
|---|
| 148 |
|
|---|
| 149 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 150 | "service-uri", NULL, serviceUri);
|
|---|
| 151 |
|
|---|
| 152 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
|
|---|
| 153 | "operation-name", NULL, "get-server-version");
|
|---|
| 154 |
|
|---|
| 155 | /*
|
|---|
| 156 | * Do the request and get back a response...
|
|---|
| 157 | */
|
|---|
| 158 |
|
|---|
| 159 | if (((response = cupsDoRequest(http, request, "/ipp/")) == NULL) ||
|
|---|
| 160 | (ippGetStatusCode(response) >= IPP_OK_CONFLICT))
|
|---|
| 161 | goto out;
|
|---|
| 162 |
|
|---|
| 163 | if (((attr = ippFindAttribute(response, "server-version",
|
|---|
| 164 | IPP_TAG_STRING)) != NULL)) {
|
|---|
| 165 | if ((ver = strstr(ippGetString(attr, 0, NULL),
|
|---|
| 166 | NOVELL_SERVER_VERSION_STRING)) != NULL) {
|
|---|
| 167 | ver += strlen(NOVELL_SERVER_VERSION_STRING);
|
|---|
| 168 | /*
|
|---|
| 169 | * Strangely, libcups stores a IPP_TAG_STRING (octet
|
|---|
| 170 | * string) as a null-terminated string with no length
|
|---|
| 171 | * even though it could be binary data with nulls in
|
|---|
| 172 | * it. Luckily, in this case the value is not binary.
|
|---|
| 173 | */
|
|---|
| 174 | serverVersion = strtol(ver, &vertmp, 10);
|
|---|
| 175 |
|
|---|
| 176 | /* Check for not found, overflow or negative version */
|
|---|
| 177 | if ((ver == vertmp) || (serverVersion < 0))
|
|---|
| 178 | serverVersion = 0;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | if ((os = strstr(ippGetString(attr, 0, NULL),
|
|---|
| 182 | NOVELL_SERVER_SYSNAME)) != NULL) {
|
|---|
| 183 | os += strlen(NOVELL_SERVER_SYSNAME);
|
|---|
| 184 | if ((temp = strchr(os,'<')) != NULL)
|
|---|
| 185 | *temp = '\0';
|
|---|
| 186 | if (strcmp(os,NOVELL_SERVER_SYSNAME_NETWARE))
|
|---|
| 187 | osFlag = 1; /* 1 for non-NetWare systems */
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | out:
|
|---|
| 192 | if (response)
|
|---|
| 193 | ippDelete(response);
|
|---|
| 194 |
|
|---|
| 195 | if (language)
|
|---|
| 196 | cupsLangFree(language);
|
|---|
| 197 |
|
|---|
| 198 | if (osFlag == 0)
|
|---|
| 199 | serverVersion *= -1;
|
|---|
| 200 |
|
|---|
| 201 | return serverVersion;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 | static int iprint_cache_add_printer(http_t *http,
|
|---|
| 206 | int reqId,
|
|---|
| 207 | char* url)
|
|---|
| 208 | {
|
|---|
| 209 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 210 | *response = NULL; /* IPP Response */
|
|---|
| 211 | ipp_attribute_t *attr; /* Current attribute */
|
|---|
| 212 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 213 | char *name, /* printer-name attribute */
|
|---|
| 214 | *info, /* printer-info attribute */
|
|---|
| 215 | smb_enabled, /* smb-enabled attribute */
|
|---|
| 216 | secure; /* security-enabled attrib. */
|
|---|
| 217 |
|
|---|
| 218 | char *httpPath; /* path portion of the printer-uri */
|
|---|
| 219 |
|
|---|
| 220 | static const char *pattrs[] = /* Requested printer attributes */
|
|---|
| 221 | {
|
|---|
| 222 | "printer-name",
|
|---|
| 223 | "security-enabled",
|
|---|
| 224 | "printer-info",
|
|---|
| 225 | "smb-enabled"
|
|---|
| 226 | };
|
|---|
| 227 |
|
|---|
| 228 | request = ippNew();
|
|---|
| 229 |
|
|---|
| 230 | ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
|
|---|
| 231 | ippSetRequestId(request, reqId);
|
|---|
| 232 |
|
|---|
| 233 | language = cupsLangDefault();
|
|---|
| 234 |
|
|---|
| 235 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 236 | "attributes-charset", NULL, "utf-8");
|
|---|
| 237 |
|
|---|
| 238 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 239 | "attributes-natural-language", NULL, language->language);
|
|---|
| 240 |
|
|---|
| 241 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, url);
|
|---|
| 242 |
|
|---|
| 243 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
|
|---|
| 244 | "requested-attributes",
|
|---|
| 245 | (sizeof(pattrs) / sizeof(pattrs[0])),
|
|---|
| 246 | NULL, pattrs);
|
|---|
| 247 |
|
|---|
| 248 | /*
|
|---|
| 249 | * Do the request and get back a response...
|
|---|
| 250 | */
|
|---|
| 251 |
|
|---|
| 252 | if ((httpPath = strstr(url,"://")) == NULL ||
|
|---|
| 253 | (httpPath = strchr(httpPath+3,'/')) == NULL)
|
|---|
| 254 | {
|
|---|
| 255 | ippDelete(request);
|
|---|
| 256 | request = NULL;
|
|---|
| 257 | goto out;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
|
|---|
| 261 | ipp_status_t lastErr = cupsLastError();
|
|---|
| 262 |
|
|---|
| 263 | /*
|
|---|
| 264 | * Ignore printers that cannot be queried without credentials
|
|---|
| 265 | */
|
|---|
| 266 | if (lastErr == IPP_FORBIDDEN ||
|
|---|
| 267 | lastErr == IPP_NOT_AUTHENTICATED ||
|
|---|
| 268 | lastErr == IPP_NOT_AUTHORIZED)
|
|---|
| 269 | goto out;
|
|---|
| 270 |
|
|---|
| 271 | DEBUG(0,("Unable to get printer list - %s\n",
|
|---|
| 272 | ippErrorString(lastErr)));
|
|---|
| 273 | goto out;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | for (attr = ippFirstAttribute(response); attr != NULL;) {
|
|---|
| 277 | /*
|
|---|
| 278 | * Skip leading attributes until we hit a printer...
|
|---|
| 279 | */
|
|---|
| 280 |
|
|---|
| 281 | while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
|
|---|
| 282 | attr = ippNextAttribute(response);
|
|---|
| 283 |
|
|---|
| 284 | if (attr == NULL)
|
|---|
| 285 | break;
|
|---|
| 286 |
|
|---|
| 287 | /*
|
|---|
| 288 | * Pull the needed attributes from this printer...
|
|---|
| 289 | */
|
|---|
| 290 |
|
|---|
| 291 | name = NULL;
|
|---|
| 292 | info = NULL;
|
|---|
| 293 | smb_enabled= 1;
|
|---|
| 294 | secure = 0;
|
|---|
| 295 |
|
|---|
| 296 | while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
|
|---|
| 297 | if (strcmp(ippGetName(attr), "printer-name") == 0 &&
|
|---|
| 298 | ippGetValueTag(attr) == IPP_TAG_NAME)
|
|---|
| 299 | name = ippGetString(attr, 0, NULL);
|
|---|
| 300 |
|
|---|
| 301 | if (strcmp(ippGetName(attr), "printer-info") == 0 &&
|
|---|
| 302 | (ippGetValueTag(attr) == IPP_TAG_TEXT ||
|
|---|
| 303 | ippGetValueTag(attr) == IPP_TAG_TEXTLANG))
|
|---|
| 304 | info = ippGetString(attr, 0, NULL);
|
|---|
| 305 |
|
|---|
| 306 | /*
|
|---|
| 307 | * If the smb-enabled attribute is present and the
|
|---|
| 308 | * value is set to 0, don't show the printer.
|
|---|
| 309 | * If the attribute is not present, assume that the
|
|---|
| 310 | * printer should show up
|
|---|
| 311 | */
|
|---|
| 312 | if (!strcmp(ippGetName(attr), "smb-enabled") &&
|
|---|
| 313 | ((ippGetValueTag(attr) == IPP_TAG_INTEGER &&
|
|---|
| 314 | !ippGetInteger(attr, 0)) ||
|
|---|
| 315 | (ippGetValueTag(attr) == IPP_TAG_BOOLEAN &&
|
|---|
| 316 | !ippGetBoolean(attr, 0))))
|
|---|
| 317 | smb_enabled = 0;
|
|---|
| 318 |
|
|---|
| 319 | /*
|
|---|
| 320 | * If the security-enabled attribute is present and the
|
|---|
| 321 | * value is set to 1, don't show the printer.
|
|---|
| 322 | * If the attribute is not present, assume that the
|
|---|
| 323 | * printer should show up
|
|---|
| 324 | */
|
|---|
| 325 | if (!strcmp(ippGetName(attr), "security-enabled") &&
|
|---|
| 326 | ((ippGetValueTag(attr) == IPP_TAG_INTEGER &&
|
|---|
| 327 | ippGetInteger(attr, 0)) ||
|
|---|
| 328 | (ippGetValueTag(attr) == IPP_TAG_BOOLEAN &&
|
|---|
| 329 | ippGetBoolean(attr, 0))))
|
|---|
| 330 | secure = 1;
|
|---|
| 331 |
|
|---|
| 332 | attr = ippNextAttribute(response);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /*
|
|---|
| 336 | * See if we have everything needed...
|
|---|
| 337 | * Make sure the printer is not a secure printer
|
|---|
| 338 | * and make sure smb printing hasn't been explicitly
|
|---|
| 339 | * disabled for the printer
|
|---|
| 340 | */
|
|---|
| 341 |
|
|---|
| 342 | if (name != NULL && !secure && smb_enabled)
|
|---|
| 343 | pcap_cache_add(name, info, NULL);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | out:
|
|---|
| 347 | if (response)
|
|---|
| 348 | ippDelete(response);
|
|---|
| 349 | return(0);
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | bool iprint_cache_reload(void)
|
|---|
| 353 | {
|
|---|
| 354 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 355 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 356 | *response = NULL; /* IPP Response */
|
|---|
| 357 | ipp_attribute_t *attr; /* Current attribute */
|
|---|
| 358 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 359 | int i;
|
|---|
| 360 | bool ret = False;
|
|---|
| 361 |
|
|---|
| 362 | DEBUG(5, ("reloading iprint printcap cache\n"));
|
|---|
| 363 |
|
|---|
| 364 | /*
|
|---|
| 365 | * Make sure we don't ask for passwords...
|
|---|
| 366 | */
|
|---|
| 367 |
|
|---|
| 368 | cupsSetPasswordCB(iprint_passwd_cb);
|
|---|
| 369 |
|
|---|
| 370 | /*
|
|---|
| 371 | * Try to connect to the server...
|
|---|
| 372 | */
|
|---|
| 373 |
|
|---|
| 374 | if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
|
|---|
| 375 | DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
|
|---|
| 376 | iprint_server(), strerror(errno)));
|
|---|
| 377 | goto out;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /*
|
|---|
| 381 | * Build a OPERATION_NOVELL_LIST_PRINTERS request, which requires the following attributes:
|
|---|
| 382 | *
|
|---|
| 383 | * attributes-charset
|
|---|
| 384 | * attributes-natural-language
|
|---|
| 385 | */
|
|---|
| 386 |
|
|---|
| 387 | request = ippNew();
|
|---|
| 388 |
|
|---|
| 389 | ippSetOperation(request, (ipp_op_t)OPERATION_NOVELL_LIST_PRINTERS);
|
|---|
| 390 | ippSetRequestId(request, 1);
|
|---|
| 391 |
|
|---|
| 392 | language = cupsLangDefault();
|
|---|
| 393 |
|
|---|
| 394 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 395 | "attributes-charset", NULL, "utf-8");
|
|---|
| 396 |
|
|---|
| 397 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 398 | "attributes-natural-language", NULL, language->language);
|
|---|
| 399 |
|
|---|
| 400 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
|
|---|
| 401 | "ipp-server", NULL, "ippSrvr");
|
|---|
| 402 |
|
|---|
| 403 | /*
|
|---|
| 404 | * Do the request and get back a response...
|
|---|
| 405 | */
|
|---|
| 406 |
|
|---|
| 407 | if ((response = cupsDoRequest(http, request, "/ipp")) == NULL) {
|
|---|
| 408 | DEBUG(0,("Unable to get printer list - %s\n",
|
|---|
| 409 | ippErrorString(cupsLastError())));
|
|---|
| 410 | goto out;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | for (attr = ippFirstAttribute(response); attr != NULL;) {
|
|---|
| 414 | /*
|
|---|
| 415 | * Skip leading attributes until we hit a printer...
|
|---|
| 416 | */
|
|---|
| 417 |
|
|---|
| 418 | while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
|
|---|
| 419 | attr = ippNextAttribute(response);
|
|---|
| 420 |
|
|---|
| 421 | if (attr == NULL)
|
|---|
| 422 | break;
|
|---|
| 423 |
|
|---|
| 424 | /*
|
|---|
| 425 | * Pull the needed attributes from this printer...
|
|---|
| 426 | */
|
|---|
| 427 |
|
|---|
| 428 | while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER)
|
|---|
| 429 | {
|
|---|
| 430 | if (strcmp(ippGetName(attr), "printer-name") == 0 &&
|
|---|
| 431 | (ippGetValueTag(attr) == IPP_TAG_URI ||
|
|---|
| 432 | ippGetValueTag(attr) == IPP_TAG_NAME ||
|
|---|
| 433 | ippGetValueTag(attr) == IPP_TAG_TEXT ||
|
|---|
| 434 | ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
|
|---|
| 435 | ippGetValueTag(attr) == IPP_TAG_TEXTLANG))
|
|---|
| 436 | {
|
|---|
| 437 | for (i = 0; i<ippGetCount(attr); i++)
|
|---|
| 438 | {
|
|---|
| 439 | char *url = ippGetString(attr, i, NULL);
|
|---|
| 440 | if (!url || !strlen(url))
|
|---|
| 441 | continue;
|
|---|
| 442 | iprint_cache_add_printer(http, i+2, url);
|
|---|
| 443 | }
|
|---|
| 444 | }
|
|---|
| 445 | attr = ippNextAttribute(response);
|
|---|
| 446 | }
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | ret = True;
|
|---|
| 450 |
|
|---|
| 451 | out:
|
|---|
| 452 | if (response)
|
|---|
| 453 | ippDelete(response);
|
|---|
| 454 |
|
|---|
| 455 | if (language)
|
|---|
| 456 | cupsLangFree(language);
|
|---|
| 457 |
|
|---|
| 458 | if (http)
|
|---|
| 459 | httpClose(http);
|
|---|
| 460 |
|
|---|
| 461 | return ret;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 | /*
|
|---|
| 466 | * 'iprint_job_delete()' - Delete a job.
|
|---|
| 467 | */
|
|---|
| 468 |
|
|---|
| 469 | static int iprint_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
|
|---|
| 470 | {
|
|---|
| 471 | int ret = 1; /* Return value */
|
|---|
| 472 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 473 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 474 | *response = NULL; /* IPP Response */
|
|---|
| 475 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 476 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 477 | char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 | DEBUG(5,("iprint_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
|
|---|
| 481 |
|
|---|
| 482 | /*
|
|---|
| 483 | * Make sure we don't ask for passwords...
|
|---|
| 484 | */
|
|---|
| 485 |
|
|---|
| 486 | cupsSetPasswordCB(iprint_passwd_cb);
|
|---|
| 487 |
|
|---|
| 488 | /*
|
|---|
| 489 | * Try to connect to the server...
|
|---|
| 490 | */
|
|---|
| 491 |
|
|---|
| 492 | if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
|
|---|
| 493 | DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
|
|---|
| 494 | iprint_server(), strerror(errno)));
|
|---|
| 495 | goto out;
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | /*
|
|---|
| 499 | * Build an IPP_CANCEL_JOB request, which uses the following
|
|---|
| 500 | * attributes:
|
|---|
| 501 | *
|
|---|
| 502 | * attributes-charset
|
|---|
| 503 | * attributes-natural-language
|
|---|
| 504 | * printer-uri
|
|---|
| 505 | * job-id
|
|---|
| 506 | * requesting-user-name
|
|---|
| 507 | */
|
|---|
| 508 |
|
|---|
| 509 | request = ippNew();
|
|---|
| 510 |
|
|---|
| 511 | ippSetOperation(request, IPP_CANCEL_JOB);
|
|---|
| 512 | ippSetRequestId(request, 1);
|
|---|
| 513 |
|
|---|
| 514 | language = cupsLangDefault();
|
|---|
| 515 |
|
|---|
| 516 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 517 | "attributes-charset", NULL, "utf-8");
|
|---|
| 518 |
|
|---|
| 519 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 520 | "attributes-natural-language", NULL, language->language);
|
|---|
| 521 |
|
|---|
| 522 | slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), sharename);
|
|---|
| 523 |
|
|---|
| 524 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
|
|---|
| 525 |
|
|---|
| 526 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
|
|---|
| 527 |
|
|---|
| 528 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 529 | NULL, pjob->user);
|
|---|
| 530 |
|
|---|
| 531 | /*
|
|---|
| 532 | * Do the request and get back a response...
|
|---|
| 533 | */
|
|---|
| 534 |
|
|---|
| 535 | slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", sharename);
|
|---|
| 536 |
|
|---|
| 537 | if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
|
|---|
| 538 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 539 | DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
|
|---|
| 540 | ippErrorString(cupsLastError())));
|
|---|
| 541 | } else {
|
|---|
| 542 | ret = 0;
|
|---|
| 543 | }
|
|---|
| 544 | } else {
|
|---|
| 545 | DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
|
|---|
| 546 | ippErrorString(cupsLastError())));
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | out:
|
|---|
| 550 | if (response)
|
|---|
| 551 | ippDelete(response);
|
|---|
| 552 |
|
|---|
| 553 | if (language)
|
|---|
| 554 | cupsLangFree(language);
|
|---|
| 555 |
|
|---|
| 556 | if (http)
|
|---|
| 557 | httpClose(http);
|
|---|
| 558 |
|
|---|
| 559 | return ret;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 | /*
|
|---|
| 564 | * 'iprint_job_pause()' - Pause a job.
|
|---|
| 565 | */
|
|---|
| 566 |
|
|---|
| 567 | static int iprint_job_pause(int snum, struct printjob *pjob)
|
|---|
| 568 | {
|
|---|
| 569 | int ret = 1; /* Return value */
|
|---|
| 570 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 571 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 572 | *response = NULL; /* IPP Response */
|
|---|
| 573 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 574 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 575 | char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 | DEBUG(5,("iprint_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
|
|---|
| 579 |
|
|---|
| 580 | /*
|
|---|
| 581 | * Make sure we don't ask for passwords...
|
|---|
| 582 | */
|
|---|
| 583 |
|
|---|
| 584 | cupsSetPasswordCB(iprint_passwd_cb);
|
|---|
| 585 |
|
|---|
| 586 | /*
|
|---|
| 587 | * Try to connect to the server...
|
|---|
| 588 | */
|
|---|
| 589 |
|
|---|
| 590 | if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
|
|---|
| 591 | DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
|
|---|
| 592 | iprint_server(), strerror(errno)));
|
|---|
| 593 | goto out;
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | /*
|
|---|
| 597 | * Build an IPP_HOLD_JOB request, which requires the following
|
|---|
| 598 | * attributes:
|
|---|
| 599 | *
|
|---|
| 600 | * attributes-charset
|
|---|
| 601 | * attributes-natural-language
|
|---|
| 602 | * printer-uri
|
|---|
| 603 | * job-id
|
|---|
| 604 | * requesting-user-name
|
|---|
| 605 | */
|
|---|
| 606 |
|
|---|
| 607 | request = ippNew();
|
|---|
| 608 |
|
|---|
| 609 | ippSetOperation(request, IPP_HOLD_JOB);
|
|---|
| 610 | ippSetRequestId(request, 1);
|
|---|
| 611 |
|
|---|
| 612 | language = cupsLangDefault();
|
|---|
| 613 |
|
|---|
| 614 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 615 | "attributes-charset", NULL, "utf-8");
|
|---|
| 616 |
|
|---|
| 617 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 618 | "attributes-natural-language", NULL, language->language);
|
|---|
| 619 |
|
|---|
| 620 | slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
|
|---|
| 621 | lp_printername(snum));
|
|---|
| 622 |
|
|---|
| 623 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
|
|---|
| 624 |
|
|---|
| 625 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
|
|---|
| 626 |
|
|---|
| 627 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 628 | NULL, pjob->user);
|
|---|
| 629 |
|
|---|
| 630 | /*
|
|---|
| 631 | * Do the request and get back a response...
|
|---|
| 632 | */
|
|---|
| 633 |
|
|---|
| 634 | slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
|
|---|
| 635 | lp_printername(snum));
|
|---|
| 636 |
|
|---|
| 637 | if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
|
|---|
| 638 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 639 | DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
|
|---|
| 640 | ippErrorString(cupsLastError())));
|
|---|
| 641 | } else {
|
|---|
| 642 | ret = 0;
|
|---|
| 643 | }
|
|---|
| 644 | } else {
|
|---|
| 645 | DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
|
|---|
| 646 | ippErrorString(cupsLastError())));
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | out:
|
|---|
| 650 | if (response)
|
|---|
| 651 | ippDelete(response);
|
|---|
| 652 |
|
|---|
| 653 | if (language)
|
|---|
| 654 | cupsLangFree(language);
|
|---|
| 655 |
|
|---|
| 656 | if (http)
|
|---|
| 657 | httpClose(http);
|
|---|
| 658 |
|
|---|
| 659 | return ret;
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 | /*
|
|---|
| 664 | * 'iprint_job_resume()' - Resume a paused job.
|
|---|
| 665 | */
|
|---|
| 666 |
|
|---|
| 667 | static int iprint_job_resume(int snum, struct printjob *pjob)
|
|---|
| 668 | {
|
|---|
| 669 | int ret = 1; /* Return value */
|
|---|
| 670 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 671 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 672 | *response = NULL; /* IPP Response */
|
|---|
| 673 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 674 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 675 | char httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 | DEBUG(5,("iprint_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
|
|---|
| 679 |
|
|---|
| 680 | /*
|
|---|
| 681 | * Make sure we don't ask for passwords...
|
|---|
| 682 | */
|
|---|
| 683 |
|
|---|
| 684 | cupsSetPasswordCB(iprint_passwd_cb);
|
|---|
| 685 |
|
|---|
| 686 | /*
|
|---|
| 687 | * Try to connect to the server...
|
|---|
| 688 | */
|
|---|
| 689 |
|
|---|
| 690 | if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
|
|---|
| 691 | DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
|
|---|
| 692 | iprint_server(), strerror(errno)));
|
|---|
| 693 | goto out;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 | /*
|
|---|
| 697 | * Build an IPP_RELEASE_JOB request, which requires the following
|
|---|
| 698 | * attributes:
|
|---|
| 699 | *
|
|---|
| 700 | * attributes-charset
|
|---|
| 701 | * attributes-natural-language
|
|---|
| 702 | * printer-uri
|
|---|
| 703 | * job-id
|
|---|
| 704 | * requesting-user-name
|
|---|
| 705 | */
|
|---|
| 706 |
|
|---|
| 707 | request = ippNew();
|
|---|
| 708 |
|
|---|
| 709 | ippSetOperation(request, IPP_RELEASE_JOB);
|
|---|
| 710 | ippSetRequestId(request, 1);
|
|---|
| 711 |
|
|---|
| 712 | language = cupsLangDefault();
|
|---|
| 713 |
|
|---|
| 714 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 715 | "attributes-charset", NULL, "utf-8");
|
|---|
| 716 |
|
|---|
| 717 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 718 | "attributes-natural-language", NULL, language->language);
|
|---|
| 719 |
|
|---|
| 720 | slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
|
|---|
| 721 | lp_printername(snum));
|
|---|
| 722 |
|
|---|
| 723 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
|
|---|
| 724 |
|
|---|
| 725 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
|
|---|
| 726 |
|
|---|
| 727 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 728 | NULL, pjob->user);
|
|---|
| 729 |
|
|---|
| 730 | /*
|
|---|
| 731 | * Do the request and get back a response...
|
|---|
| 732 | */
|
|---|
| 733 |
|
|---|
| 734 | slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
|
|---|
| 735 | lp_printername(snum));
|
|---|
| 736 |
|
|---|
| 737 | if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
|
|---|
| 738 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 739 | DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
|
|---|
| 740 | ippErrorString(cupsLastError())));
|
|---|
| 741 | } else {
|
|---|
| 742 | ret = 0;
|
|---|
| 743 | }
|
|---|
| 744 | } else {
|
|---|
| 745 | DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
|
|---|
| 746 | ippErrorString(cupsLastError())));
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | out:
|
|---|
| 750 | if (response)
|
|---|
| 751 | ippDelete(response);
|
|---|
| 752 |
|
|---|
| 753 | if (language)
|
|---|
| 754 | cupsLangFree(language);
|
|---|
| 755 |
|
|---|
| 756 | if (http)
|
|---|
| 757 | httpClose(http);
|
|---|
| 758 |
|
|---|
| 759 | return ret;
|
|---|
| 760 | }
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 | /*
|
|---|
| 764 | * 'iprint_job_submit()' - Submit a job for printing.
|
|---|
| 765 | */
|
|---|
| 766 |
|
|---|
| 767 | static int iprint_job_submit(int snum, struct printjob *pjob,
|
|---|
| 768 | enum printing_types printing_type,
|
|---|
| 769 | char *lpq_cmd)
|
|---|
| 770 | {
|
|---|
| 771 | int ret = 1; /* Return value */
|
|---|
| 772 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 773 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 774 | *response = NULL; /* IPP Response */
|
|---|
| 775 | ipp_attribute_t *attr; /* Current attribute */
|
|---|
| 776 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 777 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 778 |
|
|---|
| 779 | DEBUG(5,("iprint_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
|
|---|
| 780 |
|
|---|
| 781 | /*
|
|---|
| 782 | * Make sure we don't ask for passwords...
|
|---|
| 783 | */
|
|---|
| 784 |
|
|---|
| 785 | cupsSetPasswordCB(iprint_passwd_cb);
|
|---|
| 786 |
|
|---|
| 787 | /*
|
|---|
| 788 | * Try to connect to the server...
|
|---|
| 789 | */
|
|---|
| 790 |
|
|---|
| 791 | if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
|
|---|
| 792 | DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
|
|---|
| 793 | iprint_server(), strerror(errno)));
|
|---|
| 794 | goto out;
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | /*
|
|---|
| 798 | * Build an IPP_PRINT_JOB request, which requires the following
|
|---|
| 799 | * attributes:
|
|---|
| 800 | *
|
|---|
| 801 | * attributes-charset
|
|---|
| 802 | * attributes-natural-language
|
|---|
| 803 | * printer-uri
|
|---|
| 804 | * requesting-user-name
|
|---|
| 805 | * [document-data]
|
|---|
| 806 | */
|
|---|
| 807 |
|
|---|
| 808 | request = ippNew();
|
|---|
| 809 |
|
|---|
| 810 | ippSetOperation(request, IPP_PRINT_JOB);
|
|---|
| 811 | ippSetRequestId(request, 1);
|
|---|
| 812 |
|
|---|
| 813 | language = cupsLangDefault();
|
|---|
| 814 |
|
|---|
| 815 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 816 | "attributes-charset", NULL, "utf-8");
|
|---|
| 817 |
|
|---|
| 818 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 819 | "attributes-natural-language", NULL, language->language);
|
|---|
| 820 |
|
|---|
| 821 | slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
|
|---|
| 822 | lp_printername(snum));
|
|---|
| 823 |
|
|---|
| 824 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 825 | "printer-uri", NULL, uri);
|
|---|
| 826 |
|
|---|
| 827 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 828 | NULL, pjob->user);
|
|---|
| 829 |
|
|---|
| 830 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|---|
| 831 | "job-originating-host-name", NULL,
|
|---|
| 832 | pjob->clientmachine);
|
|---|
| 833 |
|
|---|
| 834 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
|
|---|
| 835 | pjob->jobname);
|
|---|
| 836 |
|
|---|
| 837 | /*
|
|---|
| 838 | * Do the request and get back a response...
|
|---|
| 839 | */
|
|---|
| 840 |
|
|---|
| 841 | slprintf(uri, sizeof(uri) - 1, "/ipp/%s", lp_printername(snum));
|
|---|
| 842 |
|
|---|
| 843 | if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
|
|---|
| 844 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 845 | DEBUG(0,("Unable to print file to %s - %s\n",
|
|---|
| 846 | lp_printername(snum),
|
|---|
| 847 | ippErrorString(cupsLastError())));
|
|---|
| 848 | } else {
|
|---|
| 849 | ret = 0;
|
|---|
| 850 | }
|
|---|
| 851 | } else {
|
|---|
| 852 | DEBUG(0,("Unable to print file to `%s' - %s\n",
|
|---|
| 853 | lp_printername(snum),
|
|---|
| 854 | ippErrorString(cupsLastError())));
|
|---|
| 855 | }
|
|---|
| 856 |
|
|---|
| 857 | if ( ret == 0 )
|
|---|
| 858 | unlink(pjob->filename);
|
|---|
| 859 | /* else print_job_end will do it for us */
|
|---|
| 860 |
|
|---|
| 861 | if ( ret == 0 ) {
|
|---|
| 862 |
|
|---|
| 863 | attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
|
|---|
| 864 | if (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB)
|
|---|
| 865 | {
|
|---|
| 866 | pjob->sysjob = ippGetInteger(attr, 0);
|
|---|
| 867 | }
|
|---|
| 868 | }
|
|---|
| 869 |
|
|---|
| 870 | out:
|
|---|
| 871 | if (response)
|
|---|
| 872 | ippDelete(response);
|
|---|
| 873 |
|
|---|
| 874 | if (language)
|
|---|
| 875 | cupsLangFree(language);
|
|---|
| 876 |
|
|---|
| 877 | if (http)
|
|---|
| 878 | httpClose(http);
|
|---|
| 879 |
|
|---|
| 880 | return ret;
|
|---|
| 881 | }
|
|---|
| 882 |
|
|---|
| 883 | /*
|
|---|
| 884 | * 'iprint_queue_get()' - Get all the jobs in the print queue.
|
|---|
| 885 | */
|
|---|
| 886 |
|
|---|
| 887 | static int iprint_queue_get(const char *sharename,
|
|---|
| 888 | enum printing_types printing_type,
|
|---|
| 889 | char *lpq_command,
|
|---|
| 890 | print_queue_struct **q,
|
|---|
| 891 | print_status_struct *status)
|
|---|
| 892 | {
|
|---|
| 893 | fstring printername;
|
|---|
| 894 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 895 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 896 | *response = NULL; /* IPP Response */
|
|---|
| 897 | ipp_attribute_t *attr = NULL; /* Current attribute */
|
|---|
| 898 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 899 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 900 | char serviceUri[HTTP_MAX_URI]; /* service-uri attribute */
|
|---|
| 901 | char httpPath[HTTP_MAX_URI]; /* path portion of the uri */
|
|---|
| 902 | int jobUseUnixTime = 0; /* Whether job times should
|
|---|
| 903 | * be assumed to be Unix time */
|
|---|
| 904 | int qcount = 0, /* Number of active queue entries */
|
|---|
| 905 | qalloc = 0; /* Number of queue entries allocated */
|
|---|
| 906 | print_queue_struct *queue = NULL, /* Queue entries */
|
|---|
| 907 | *temp; /* Temporary pointer for queue */
|
|---|
| 908 | const char *user_name, /* job-originating-user-name attribute */
|
|---|
| 909 | *job_name; /* job-name attribute */
|
|---|
| 910 | int job_id; /* job-id attribute */
|
|---|
| 911 | int job_k_octets; /* job-k-octets attribute */
|
|---|
| 912 | time_t job_time; /* time-at-creation attribute */
|
|---|
| 913 | time_t printer_up_time = 0; /* printer's uptime */
|
|---|
| 914 | ipp_jstate_t job_status; /* job-status attribute */
|
|---|
| 915 | int job_priority; /* job-priority attribute */
|
|---|
| 916 | static const char *jattrs[] = /* Requested job attributes */
|
|---|
| 917 | {
|
|---|
| 918 | "job-id",
|
|---|
| 919 | "job-k-octets",
|
|---|
| 920 | "job-name",
|
|---|
| 921 | "job-originating-user-name",
|
|---|
| 922 | "job-priority",
|
|---|
| 923 | "job-state",
|
|---|
| 924 | "time-at-creation",
|
|---|
| 925 | };
|
|---|
| 926 | static const char *pattrs[] = /* Requested printer attributes */
|
|---|
| 927 | {
|
|---|
| 928 | "printer-state",
|
|---|
| 929 | "printer-state-message",
|
|---|
| 930 | "printer-current-time",
|
|---|
| 931 | "printer-up-time"
|
|---|
| 932 | };
|
|---|
| 933 |
|
|---|
| 934 | *q = NULL;
|
|---|
| 935 |
|
|---|
| 936 | /* HACK ALERT!!! The porblem with support the 'printer name'
|
|---|
| 937 | option is that we key the tdb off the sharename. So we will
|
|---|
| 938 | overload the lpq_command string to pass in the printername
|
|---|
| 939 | (which is basically what we do for non-cups printers ... using
|
|---|
| 940 | the lpq_command to get the queue listing). */
|
|---|
| 941 |
|
|---|
| 942 | fstrcpy( printername, lpq_command );
|
|---|
| 943 |
|
|---|
| 944 | DEBUG(5,("iprint_queue_get(%s, %p, %p)\n", printername, q, status));
|
|---|
| 945 |
|
|---|
| 946 | /*
|
|---|
| 947 | * Make sure we don't ask for passwords...
|
|---|
| 948 | */
|
|---|
| 949 |
|
|---|
| 950 | cupsSetPasswordCB(iprint_passwd_cb);
|
|---|
| 951 |
|
|---|
| 952 | /*
|
|---|
| 953 | * Try to connect to the server...
|
|---|
| 954 | */
|
|---|
| 955 |
|
|---|
| 956 | if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
|
|---|
| 957 | DEBUG(0,("Unable to connect to iPrint server %s - %s\n",
|
|---|
| 958 | iprint_server(), strerror(errno)));
|
|---|
| 959 | goto out;
|
|---|
| 960 | }
|
|---|
| 961 |
|
|---|
| 962 | /*
|
|---|
| 963 | * Generate the printer URI and the service URI that goes with it...
|
|---|
| 964 | */
|
|---|
| 965 |
|
|---|
| 966 | slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), printername);
|
|---|
| 967 | slprintf(serviceUri, sizeof(serviceUri) - 1, "ipp://%s/ipp/", iprint_server());
|
|---|
| 968 |
|
|---|
| 969 | /*
|
|---|
| 970 | * For Linux iPrint servers from OES SP1 on, the iPrint server
|
|---|
| 971 | * uses Unix time for job start times unless it detects the iPrint
|
|---|
| 972 | * client in an http User-Agent header. (This was done to accomodate
|
|---|
| 973 | * CUPS broken behavior. According to RFC 2911, section 4.3.14, job
|
|---|
| 974 | * start times are supposed to be relative to how long the printer has
|
|---|
| 975 | * been up.) Since libcups doesn't allow us to set that header before
|
|---|
| 976 | * the request is sent, this ugly hack allows us to detect the server
|
|---|
| 977 | * version and decide how to interpret the job time.
|
|---|
| 978 | */
|
|---|
| 979 | if (iprint_get_server_version(http, serviceUri) >=
|
|---|
| 980 | NOVELL_SERVER_VERSION_OES_SP1)
|
|---|
| 981 | jobUseUnixTime = 1;
|
|---|
| 982 |
|
|---|
| 983 | request = ippNew();
|
|---|
| 984 |
|
|---|
| 985 | ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
|
|---|
| 986 | ippSetRequestId(request, 2);
|
|---|
| 987 |
|
|---|
| 988 | language = cupsLangDefault();
|
|---|
| 989 |
|
|---|
| 990 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 991 | "attributes-charset", NULL, "utf-8");
|
|---|
| 992 |
|
|---|
| 993 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 994 | "attributes-natural-language", NULL, language->language);
|
|---|
| 995 |
|
|---|
| 996 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 997 | "printer-uri", NULL, uri);
|
|---|
| 998 |
|
|---|
| 999 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
|
|---|
| 1000 | "requested-attributes",
|
|---|
| 1001 | (sizeof(pattrs) / sizeof(pattrs[0])),
|
|---|
| 1002 | NULL, pattrs);
|
|---|
| 1003 |
|
|---|
| 1004 | /*
|
|---|
| 1005 | * Do the request and get back a response...
|
|---|
| 1006 | */
|
|---|
| 1007 |
|
|---|
| 1008 | slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
|
|---|
| 1009 |
|
|---|
| 1010 | if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
|
|---|
| 1011 | DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
|
|---|
| 1012 | ippErrorString(cupsLastError())));
|
|---|
| 1013 | *q = queue;
|
|---|
| 1014 | goto out;
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 1017 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1018 | DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
|
|---|
| 1019 | ippErrorString(ippGetStatusCode(response))));
|
|---|
| 1020 | *q = queue;
|
|---|
| 1021 | goto out;
|
|---|
| 1022 | }
|
|---|
| 1023 |
|
|---|
| 1024 | /*
|
|---|
| 1025 | * Get the current printer status and convert it to the SAMBA values.
|
|---|
| 1026 | */
|
|---|
| 1027 |
|
|---|
| 1028 | if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
|
|---|
| 1029 | if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
|
|---|
| 1030 | status->status = LPSTAT_STOPPED;
|
|---|
| 1031 | else
|
|---|
| 1032 | status->status = LPSTAT_OK;
|
|---|
| 1033 | }
|
|---|
| 1034 |
|
|---|
| 1035 | if ((attr = ippFindAttribute(response, "printer-state-message",
|
|---|
| 1036 | IPP_TAG_TEXT)) != NULL)
|
|---|
| 1037 | fstrcpy(status->message, ippGetString(attr, 0, NULL));
|
|---|
| 1038 |
|
|---|
| 1039 | if ((attr = ippFindAttribute(response, "printer-up-time",
|
|---|
| 1040 | IPP_TAG_INTEGER)) != NULL)
|
|---|
| 1041 | printer_up_time = ippGetInteger(attr, 0);
|
|---|
| 1042 |
|
|---|
| 1043 | ippDelete(response);
|
|---|
| 1044 | response = NULL;
|
|---|
| 1045 |
|
|---|
| 1046 | /*
|
|---|
| 1047 | * Build an IPP_GET_JOBS request, which requires the following
|
|---|
| 1048 | * attributes:
|
|---|
| 1049 | *
|
|---|
| 1050 | * attributes-charset
|
|---|
| 1051 | * attributes-natural-language
|
|---|
| 1052 | * requested-attributes
|
|---|
| 1053 | * printer-uri
|
|---|
| 1054 | */
|
|---|
| 1055 |
|
|---|
| 1056 | request = ippNew();
|
|---|
| 1057 |
|
|---|
| 1058 | ippSetOperation(request, IPP_GET_JOBS);
|
|---|
| 1059 | ippSetRequestId(request, 3);
|
|---|
| 1060 |
|
|---|
| 1061 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 1062 | "attributes-charset", NULL, "utf-8");
|
|---|
| 1063 |
|
|---|
| 1064 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 1065 | "attributes-natural-language", NULL, language->language);
|
|---|
| 1066 |
|
|---|
| 1067 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 1068 | "printer-uri", NULL, uri);
|
|---|
| 1069 |
|
|---|
| 1070 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
|
|---|
| 1071 | "requested-attributes",
|
|---|
| 1072 | (sizeof(jattrs) / sizeof(jattrs[0])),
|
|---|
| 1073 | NULL, jattrs);
|
|---|
| 1074 |
|
|---|
| 1075 | /*
|
|---|
| 1076 | * Do the request and get back a response...
|
|---|
| 1077 | */
|
|---|
| 1078 |
|
|---|
| 1079 | slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
|
|---|
| 1080 |
|
|---|
| 1081 | if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
|
|---|
| 1082 | DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
|
|---|
| 1083 | ippErrorString(cupsLastError())));
|
|---|
| 1084 | goto out;
|
|---|
| 1085 | }
|
|---|
| 1086 |
|
|---|
| 1087 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1088 | DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
|
|---|
| 1089 | ippErrorString(ippGetStatusCode(response))));
|
|---|
| 1090 | goto out;
|
|---|
| 1091 | }
|
|---|
| 1092 |
|
|---|
| 1093 | /*
|
|---|
| 1094 | * Process the jobs...
|
|---|
| 1095 | */
|
|---|
| 1096 |
|
|---|
| 1097 | qcount = 0;
|
|---|
| 1098 | qalloc = 0;
|
|---|
| 1099 | queue = NULL;
|
|---|
| 1100 |
|
|---|
| 1101 | for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
|
|---|
| 1102 | /*
|
|---|
| 1103 | * Skip leading attributes until we hit a job...
|
|---|
| 1104 | */
|
|---|
| 1105 |
|
|---|
| 1106 | while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
|
|---|
| 1107 | attr = ippNextAttribute(response);
|
|---|
| 1108 |
|
|---|
| 1109 | if (attr == NULL)
|
|---|
| 1110 | break;
|
|---|
| 1111 |
|
|---|
| 1112 | /*
|
|---|
| 1113 | * Allocate memory as needed...
|
|---|
| 1114 | */
|
|---|
| 1115 | if (qcount >= qalloc) {
|
|---|
| 1116 | qalloc += 16;
|
|---|
| 1117 |
|
|---|
| 1118 | queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
|
|---|
| 1119 |
|
|---|
| 1120 | if (queue == NULL) {
|
|---|
| 1121 | DEBUG(0,("iprint_queue_get: Not enough memory!"));
|
|---|
| 1122 | qcount = 0;
|
|---|
| 1123 | goto out;
|
|---|
| 1124 | }
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | temp = queue + qcount;
|
|---|
| 1128 | memset(temp, 0, sizeof(print_queue_struct));
|
|---|
| 1129 |
|
|---|
| 1130 | /*
|
|---|
| 1131 | * Pull the needed attributes from this job...
|
|---|
| 1132 | */
|
|---|
| 1133 |
|
|---|
| 1134 | job_id = 0;
|
|---|
| 1135 | job_priority = 50;
|
|---|
| 1136 | job_status = IPP_JOB_PENDING;
|
|---|
| 1137 | job_time = 0;
|
|---|
| 1138 | job_k_octets = 0;
|
|---|
| 1139 | user_name = NULL;
|
|---|
| 1140 | job_name = NULL;
|
|---|
| 1141 |
|
|---|
| 1142 | while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
|
|---|
| 1143 | if (ippGetName(attr) == NULL) {
|
|---|
| 1144 | attr = ippNextAttribute(response);
|
|---|
| 1145 | break;
|
|---|
| 1146 | }
|
|---|
| 1147 |
|
|---|
| 1148 | if (strcmp(ippGetName(attr), "job-id") == 0 &&
|
|---|
| 1149 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1150 | job_id = ippGetInteger(attr, 0);
|
|---|
| 1151 |
|
|---|
| 1152 | if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
|
|---|
| 1153 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1154 | job_k_octets = ippGetInteger(attr, 0);
|
|---|
| 1155 |
|
|---|
| 1156 | if (strcmp(ippGetName(attr), "job-priority") == 0 &&
|
|---|
| 1157 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1158 | job_priority = ippGetInteger(attr, 0);
|
|---|
| 1159 |
|
|---|
| 1160 | if (strcmp(ippGetName(attr), "job-state") == 0 &&
|
|---|
| 1161 | ippGetValueTag(attr) == IPP_TAG_ENUM)
|
|---|
| 1162 | job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
|
|---|
| 1163 |
|
|---|
| 1164 | if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
|
|---|
| 1165 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1166 | {
|
|---|
| 1167 | /*
|
|---|
| 1168 | * If jobs times are in Unix time, the accuracy of the job
|
|---|
| 1169 | * start time depends upon the iPrint server's time being
|
|---|
| 1170 | * set correctly. Otherwise, the accuracy depends upon
|
|---|
| 1171 | * the Samba server's time being set correctly
|
|---|
| 1172 | */
|
|---|
| 1173 |
|
|---|
| 1174 | if (jobUseUnixTime)
|
|---|
| 1175 | job_time = ippGetInteger(attr, 0);
|
|---|
| 1176 | else
|
|---|
| 1177 | job_time = time(NULL) - printer_up_time + ippGetInteger(attr, 0);
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | if (strcmp(ippGetName(attr), "job-name") == 0 &&
|
|---|
| 1181 | (ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
|
|---|
| 1182 | ippGetValueTag(attr) == IPP_TAG_NAME))
|
|---|
| 1183 | job_name = ippGetString(attr, 0, NULL);
|
|---|
| 1184 |
|
|---|
| 1185 | if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
|
|---|
| 1186 | (ippGetValueTag(attr) == IPP_TAG_NAMELANG ||
|
|---|
| 1187 | ippGetValueTag(attr) == IPP_TAG_NAME))
|
|---|
| 1188 | user_name = ippGetString(attr, 0, NULL);
|
|---|
| 1189 |
|
|---|
| 1190 | attr = ippNextAttribute(response);
|
|---|
| 1191 | }
|
|---|
| 1192 |
|
|---|
| 1193 | /*
|
|---|
| 1194 | * See if we have everything needed...
|
|---|
| 1195 | */
|
|---|
| 1196 |
|
|---|
| 1197 | if (user_name == NULL || job_name == NULL || job_id == 0) {
|
|---|
| 1198 | if (attr == NULL)
|
|---|
| 1199 | break;
|
|---|
| 1200 | else
|
|---|
| 1201 | continue;
|
|---|
| 1202 | }
|
|---|
| 1203 |
|
|---|
| 1204 | temp->sysjob = job_id;
|
|---|
| 1205 | temp->size = job_k_octets * 1024;
|
|---|
| 1206 | temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
|
|---|
| 1207 | job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
|
|---|
| 1208 | job_status == IPP_JOB_HELD ? LPQ_PAUSED :
|
|---|
| 1209 | LPQ_PRINTING;
|
|---|
| 1210 | temp->priority = job_priority;
|
|---|
| 1211 | temp->time = job_time;
|
|---|
| 1212 | strncpy(temp->fs_user, user_name, sizeof(temp->fs_user) - 1);
|
|---|
| 1213 | strncpy(temp->fs_file, job_name, sizeof(temp->fs_file) - 1);
|
|---|
| 1214 |
|
|---|
| 1215 | qcount ++;
|
|---|
| 1216 |
|
|---|
| 1217 | if (attr == NULL)
|
|---|
| 1218 | break;
|
|---|
| 1219 | }
|
|---|
| 1220 |
|
|---|
| 1221 | /*
|
|---|
| 1222 | * Return the job queue...
|
|---|
| 1223 | */
|
|---|
| 1224 |
|
|---|
| 1225 | *q = queue;
|
|---|
| 1226 |
|
|---|
| 1227 | out:
|
|---|
| 1228 | if (response)
|
|---|
| 1229 | ippDelete(response);
|
|---|
| 1230 |
|
|---|
| 1231 | if (language)
|
|---|
| 1232 | cupsLangFree(language);
|
|---|
| 1233 |
|
|---|
| 1234 | if (http)
|
|---|
| 1235 | httpClose(http);
|
|---|
| 1236 |
|
|---|
| 1237 | return qcount;
|
|---|
| 1238 | }
|
|---|
| 1239 |
|
|---|
| 1240 |
|
|---|
| 1241 | /*
|
|---|
| 1242 | * 'iprint_queue_pause()' - Pause a print queue.
|
|---|
| 1243 | */
|
|---|
| 1244 |
|
|---|
| 1245 | static int iprint_queue_pause(int snum)
|
|---|
| 1246 | {
|
|---|
| 1247 | return(-1); /* Not supported without credentials */
|
|---|
| 1248 | }
|
|---|
| 1249 |
|
|---|
| 1250 |
|
|---|
| 1251 | /*
|
|---|
| 1252 | * 'iprint_queue_resume()' - Restart a print queue.
|
|---|
| 1253 | */
|
|---|
| 1254 |
|
|---|
| 1255 | static int iprint_queue_resume(int snum)
|
|---|
| 1256 | {
|
|---|
| 1257 | return(-1); /* Not supported without credentials */
|
|---|
| 1258 | }
|
|---|
| 1259 |
|
|---|
| 1260 | /*******************************************************************
|
|---|
| 1261 | * iPrint printing interface definitions...
|
|---|
| 1262 | ******************************************************************/
|
|---|
| 1263 |
|
|---|
| 1264 | struct printif iprint_printif =
|
|---|
| 1265 | {
|
|---|
| 1266 | PRINT_IPRINT,
|
|---|
| 1267 | iprint_queue_get,
|
|---|
| 1268 | iprint_queue_pause,
|
|---|
| 1269 | iprint_queue_resume,
|
|---|
| 1270 | iprint_job_delete,
|
|---|
| 1271 | iprint_job_pause,
|
|---|
| 1272 | iprint_job_resume,
|
|---|
| 1273 | iprint_job_submit,
|
|---|
| 1274 | };
|
|---|
| 1275 |
|
|---|
| 1276 | #else
|
|---|
| 1277 | /* this keeps fussy compilers happy */
|
|---|
| 1278 | void print_iprint_dummy(void);
|
|---|
| 1279 | void print_iprint_dummy(void) {}
|
|---|
| 1280 | #endif /* HAVE_IPRINT */
|
|---|