| 1 | /*
|
|---|
| 2 | * Support code for the Common UNIX Printing System ("CUPS")
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 1999-2003 by Michael R Sweet.
|
|---|
| 5 | * Copyright 2008 Jeremy Allison.
|
|---|
| 6 | *
|
|---|
| 7 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | * it under the terms of the GNU General Public License as published by
|
|---|
| 9 | * the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | * (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * This program is distributed in the hope that it will be useful,
|
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | * GNU General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU General Public License
|
|---|
| 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | /*
|
|---|
| 22 | * JRA. Converted to utf8 pull/push.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | #include "includes.h"
|
|---|
| 26 | #include "printing.h"
|
|---|
| 27 | #include "printing/pcap.h"
|
|---|
| 28 | #include "librpc/gen_ndr/ndr_printcap.h"
|
|---|
| 29 |
|
|---|
| 30 | #ifdef HAVE_CUPS
|
|---|
| 31 | #include <cups/cups.h>
|
|---|
| 32 | #include <cups/language.h>
|
|---|
| 33 |
|
|---|
| 34 | #if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
|
|---|
| 35 | #define HAVE_CUPS_1_6 1
|
|---|
| 36 | #endif
|
|---|
| 37 |
|
|---|
| 38 | #ifndef HAVE_CUPS_1_6
|
|---|
| 39 | #define ippGetGroupTag(attr) attr->group_tag
|
|---|
| 40 | #define ippGetName(attr) attr->name
|
|---|
| 41 | #define ippGetValueTag(attr) attr->value_tag
|
|---|
| 42 | #define ippGetStatusCode(ipp) ipp->request.status.status_code
|
|---|
| 43 | #define ippGetInteger(attr, element) attr->values[element].integer
|
|---|
| 44 | #define ippGetString(attr, element, language) attr->values[element].string.text
|
|---|
| 45 |
|
|---|
| 46 | static ipp_attribute_t *
|
|---|
| 47 | ippFirstAttribute(ipp_t *ipp)
|
|---|
| 48 | {
|
|---|
| 49 | if (!ipp)
|
|---|
| 50 | return (NULL);
|
|---|
| 51 | return (ipp->current = ipp->attrs);
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | static ipp_attribute_t *
|
|---|
| 55 | ippNextAttribute(ipp_t *ipp)
|
|---|
| 56 | {
|
|---|
| 57 | if (!ipp || !ipp->current)
|
|---|
| 58 | return (NULL);
|
|---|
| 59 | return (ipp->current = ipp->current->next);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static int ippSetOperation(ipp_t *ipp, ipp_op_t op)
|
|---|
| 63 | {
|
|---|
| 64 | ipp->request.op.operation_id = op;
|
|---|
| 65 | return (1);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | static int ippSetRequestId(ipp_t *ipp, int request_id)
|
|---|
| 69 | {
|
|---|
| 70 | ipp->request.any.request_id = request_id;
|
|---|
| 71 | return (1);
|
|---|
| 72 | }
|
|---|
| 73 | #endif
|
|---|
| 74 |
|
|---|
| 75 | static SIG_ATOMIC_T gotalarm;
|
|---|
| 76 |
|
|---|
| 77 | /***************************************************************
|
|---|
| 78 | Signal function to tell us we timed out.
|
|---|
| 79 | ****************************************************************/
|
|---|
| 80 |
|
|---|
| 81 | static void gotalarm_sig(int signum)
|
|---|
| 82 | {
|
|---|
| 83 | gotalarm = 1;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | extern userdom_struct current_user_info;
|
|---|
| 87 |
|
|---|
| 88 | /*
|
|---|
| 89 | * 'cups_passwd_cb()' - The CUPS password callback...
|
|---|
| 90 | */
|
|---|
| 91 |
|
|---|
| 92 | static const char * /* O - Password or NULL */
|
|---|
| 93 | cups_passwd_cb(const char *prompt) /* I - Prompt */
|
|---|
| 94 | {
|
|---|
| 95 | /*
|
|---|
| 96 | * Always return NULL to indicate that no password is available...
|
|---|
| 97 | */
|
|---|
| 98 |
|
|---|
| 99 | return (NULL);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | static http_t *cups_connect(TALLOC_CTX *frame)
|
|---|
| 103 | {
|
|---|
| 104 | http_t *http = NULL;
|
|---|
| 105 | char *server = NULL, *p = NULL;
|
|---|
| 106 | int port;
|
|---|
| 107 | int timeout = lp_cups_connection_timeout();
|
|---|
| 108 | size_t size;
|
|---|
| 109 |
|
|---|
| 110 | if (lp_cups_server() != NULL && strlen(lp_cups_server()) > 0) {
|
|---|
| 111 | if (!push_utf8_talloc(frame, &server, lp_cups_server(), &size)) {
|
|---|
| 112 | return NULL;
|
|---|
| 113 | }
|
|---|
| 114 | } else {
|
|---|
| 115 | server = talloc_strdup(frame,cupsServer());
|
|---|
| 116 | }
|
|---|
| 117 | if (!server) {
|
|---|
| 118 | return NULL;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | p = strchr(server, ':');
|
|---|
| 122 | if (p) {
|
|---|
| 123 | port = atoi(p+1);
|
|---|
| 124 | *p = '\0';
|
|---|
| 125 | } else {
|
|---|
| 126 | port = ippPort();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | DEBUG(10, ("connecting to cups server %s:%d\n",
|
|---|
| 130 | server, port));
|
|---|
| 131 |
|
|---|
| 132 | gotalarm = 0;
|
|---|
| 133 |
|
|---|
| 134 | if (timeout) {
|
|---|
| 135 | CatchSignal(SIGALRM, gotalarm_sig);
|
|---|
| 136 | alarm(timeout);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | #ifdef HAVE_HTTPCONNECTENCRYPT
|
|---|
| 140 | http = httpConnectEncrypt(server, port, lp_cups_encrypt());
|
|---|
| 141 | #else
|
|---|
| 142 | http = httpConnect(server, port);
|
|---|
| 143 | #endif
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | CatchSignal(SIGALRM, SIG_IGN);
|
|---|
| 147 | alarm(0);
|
|---|
| 148 |
|
|---|
| 149 | if (http == NULL) {
|
|---|
| 150 | DEBUG(0,("Unable to connect to CUPS server %s:%d - %s\n",
|
|---|
| 151 | server, port, strerror(errno)));
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | return http;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | static bool send_pcap_blob(DATA_BLOB *pcap_blob, int fd)
|
|---|
| 158 | {
|
|---|
| 159 | size_t ret;
|
|---|
| 160 |
|
|---|
| 161 | ret = sys_write(fd, &pcap_blob->length, sizeof(pcap_blob->length));
|
|---|
| 162 | if (ret != sizeof(pcap_blob->length)) {
|
|---|
| 163 | return false;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | ret = sys_write(fd, pcap_blob->data, pcap_blob->length);
|
|---|
| 167 | if (ret != pcap_blob->length) {
|
|---|
| 168 | return false;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | DEBUG(10, ("successfully sent blob of len %d\n", (int)ret));
|
|---|
| 172 | return true;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | static bool recv_pcap_blob(TALLOC_CTX *mem_ctx, int fd, DATA_BLOB *pcap_blob)
|
|---|
| 176 | {
|
|---|
| 177 | size_t blob_len;
|
|---|
| 178 | size_t ret;
|
|---|
| 179 |
|
|---|
| 180 | ret = sys_read(fd, &blob_len, sizeof(blob_len));
|
|---|
| 181 | if (ret != sizeof(blob_len)) {
|
|---|
| 182 | return false;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | *pcap_blob = data_blob_talloc_named(mem_ctx, NULL, blob_len,
|
|---|
| 186 | "cups pcap");
|
|---|
| 187 | if (pcap_blob->length != blob_len) {
|
|---|
| 188 | return false;
|
|---|
| 189 | }
|
|---|
| 190 | ret = sys_read(fd, pcap_blob->data, blob_len);
|
|---|
| 191 | if (ret != blob_len) {
|
|---|
| 192 | talloc_free(pcap_blob->data);
|
|---|
| 193 | return false;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | DEBUG(10, ("successfully recvd blob of len %d\n", (int)ret));
|
|---|
| 197 | return true;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | static bool process_cups_printers_response(TALLOC_CTX *mem_ctx,
|
|---|
| 201 | ipp_t *response,
|
|---|
| 202 | struct pcap_data *pcap_data)
|
|---|
| 203 | {
|
|---|
| 204 | ipp_attribute_t *attr;
|
|---|
| 205 | char *name;
|
|---|
| 206 | char *info;
|
|---|
| 207 | char *location = NULL;
|
|---|
| 208 | struct pcap_printer *printer;
|
|---|
| 209 | bool ret_ok = false;
|
|---|
| 210 |
|
|---|
| 211 | for (attr = ippFirstAttribute(response); attr != NULL;) {
|
|---|
| 212 | /*
|
|---|
| 213 | * Skip leading attributes until we hit a printer...
|
|---|
| 214 | */
|
|---|
| 215 |
|
|---|
| 216 | while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
|
|---|
| 217 | attr = ippNextAttribute(response);
|
|---|
| 218 |
|
|---|
| 219 | if (attr == NULL)
|
|---|
| 220 | break;
|
|---|
| 221 |
|
|---|
| 222 | /*
|
|---|
| 223 | * Pull the needed attributes from this printer...
|
|---|
| 224 | */
|
|---|
| 225 |
|
|---|
| 226 | name = NULL;
|
|---|
| 227 | info = NULL;
|
|---|
| 228 |
|
|---|
| 229 | while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_PRINTER) {
|
|---|
| 230 | size_t size;
|
|---|
| 231 | if (strcmp(ippGetName(attr), "printer-name") == 0 &&
|
|---|
| 232 | ippGetValueTag(attr) == IPP_TAG_NAME) {
|
|---|
| 233 | if (!pull_utf8_talloc(mem_ctx,
|
|---|
| 234 | &name,
|
|---|
| 235 | ippGetString(attr, 0, NULL),
|
|---|
| 236 | &size)) {
|
|---|
| 237 | goto err_out;
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | if (strcmp(ippGetName(attr), "printer-info") == 0 &&
|
|---|
| 242 | ippGetValueTag(attr) == IPP_TAG_TEXT) {
|
|---|
| 243 | if (!pull_utf8_talloc(mem_ctx,
|
|---|
| 244 | &info,
|
|---|
| 245 | ippGetString(attr, 0, NULL),
|
|---|
| 246 | &size)) {
|
|---|
| 247 | goto err_out;
|
|---|
| 248 | }
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | if (strcmp(ippGetName(attr), "printer-location") == 0 &&
|
|---|
| 252 | ippGetValueTag(attr) == IPP_TAG_TEXT) {
|
|---|
| 253 | if (!pull_utf8_talloc(mem_ctx,
|
|---|
| 254 | &location,
|
|---|
| 255 | ippGetString(attr, 0, NULL),
|
|---|
| 256 | &size)) {
|
|---|
| 257 | goto err_out;
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | attr = ippNextAttribute(response);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /*
|
|---|
| 265 | * See if we have everything needed...
|
|---|
| 266 | */
|
|---|
| 267 |
|
|---|
| 268 | if (name == NULL)
|
|---|
| 269 | break;
|
|---|
| 270 |
|
|---|
| 271 | if (pcap_data->count == 0) {
|
|---|
| 272 | printer = talloc_array(mem_ctx, struct pcap_printer, 1);
|
|---|
| 273 | } else {
|
|---|
| 274 | printer = talloc_realloc(mem_ctx, pcap_data->printers,
|
|---|
| 275 | struct pcap_printer,
|
|---|
| 276 | pcap_data->count + 1);
|
|---|
| 277 | }
|
|---|
| 278 | if (printer == NULL) {
|
|---|
| 279 | goto err_out;
|
|---|
| 280 | }
|
|---|
| 281 | pcap_data->printers = printer;
|
|---|
| 282 | pcap_data->printers[pcap_data->count].name = name;
|
|---|
| 283 | pcap_data->printers[pcap_data->count].info = info;
|
|---|
| 284 | pcap_data->printers[pcap_data->count].location = location;
|
|---|
| 285 | pcap_data->count++;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | ret_ok = true;
|
|---|
| 289 | err_out:
|
|---|
| 290 | return ret_ok;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /*
|
|---|
| 294 | * request printer list from cups, send result back to up parent via fd.
|
|---|
| 295 | * returns true if the (possibly failed) result was successfuly sent to parent.
|
|---|
| 296 | */
|
|---|
| 297 | static bool cups_cache_reload_async(int fd)
|
|---|
| 298 | {
|
|---|
| 299 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 300 | struct pcap_data pcap_data;
|
|---|
| 301 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 302 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 303 | *response = NULL; /* IPP Response */
|
|---|
| 304 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 305 | static const char *requested[] =/* Requested attributes */
|
|---|
| 306 | {
|
|---|
| 307 | "printer-name",
|
|---|
| 308 | "printer-info",
|
|---|
| 309 | "printer-location"
|
|---|
| 310 | };
|
|---|
| 311 | bool ret = False;
|
|---|
| 312 | enum ndr_err_code ndr_ret;
|
|---|
| 313 | DATA_BLOB pcap_blob;
|
|---|
| 314 |
|
|---|
| 315 | ZERO_STRUCT(pcap_data);
|
|---|
| 316 | pcap_data.status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 317 |
|
|---|
| 318 | DEBUG(5, ("reloading cups printcap cache\n"));
|
|---|
| 319 |
|
|---|
| 320 | /*
|
|---|
| 321 | * Make sure we don't ask for passwords...
|
|---|
| 322 | */
|
|---|
| 323 |
|
|---|
| 324 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 325 |
|
|---|
| 326 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 327 | goto out;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /*
|
|---|
| 331 | * Build a CUPS_GET_PRINTERS request, which requires the following
|
|---|
| 332 | * attributes:
|
|---|
| 333 | *
|
|---|
| 334 | * attributes-charset
|
|---|
| 335 | * attributes-natural-language
|
|---|
| 336 | * requested-attributes
|
|---|
| 337 | */
|
|---|
| 338 |
|
|---|
| 339 | request = ippNew();
|
|---|
| 340 |
|
|---|
| 341 | ippSetOperation(request, CUPS_GET_PRINTERS);
|
|---|
| 342 | ippSetRequestId(request, 1);
|
|---|
| 343 |
|
|---|
| 344 | language = cupsLangDefault();
|
|---|
| 345 |
|
|---|
| 346 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 347 | "attributes-charset", NULL, "utf-8");
|
|---|
| 348 |
|
|---|
| 349 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 350 | "attributes-natural-language", NULL, language->language);
|
|---|
| 351 |
|
|---|
| 352 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|---|
| 353 | "requested-attributes",
|
|---|
| 354 | (sizeof(requested) / sizeof(requested[0])),
|
|---|
| 355 | NULL, requested);
|
|---|
| 356 |
|
|---|
| 357 | if ((response = cupsDoRequest(http, request, "/")) == NULL) {
|
|---|
| 358 | DEBUG(0,("Unable to get printer list - %s\n",
|
|---|
| 359 | ippErrorString(cupsLastError())));
|
|---|
| 360 | goto out;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | ret = process_cups_printers_response(frame, response, &pcap_data);
|
|---|
| 364 | if (!ret) {
|
|---|
| 365 | DEBUG(0,("failed to process cups response\n"));
|
|---|
| 366 | goto out;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | ippDelete(response);
|
|---|
| 370 | response = NULL;
|
|---|
| 371 |
|
|---|
| 372 | /*
|
|---|
| 373 | * Build a CUPS_GET_CLASSES request, which requires the following
|
|---|
| 374 | * attributes:
|
|---|
| 375 | *
|
|---|
| 376 | * attributes-charset
|
|---|
| 377 | * attributes-natural-language
|
|---|
| 378 | * requested-attributes
|
|---|
| 379 | */
|
|---|
| 380 |
|
|---|
| 381 | request = ippNew();
|
|---|
| 382 |
|
|---|
| 383 | ippSetOperation(request, CUPS_GET_CLASSES);
|
|---|
| 384 | ippSetRequestId(request, 1);
|
|---|
| 385 |
|
|---|
| 386 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 387 | "attributes-charset", NULL, "utf-8");
|
|---|
| 388 |
|
|---|
| 389 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 390 | "attributes-natural-language", NULL, language->language);
|
|---|
| 391 |
|
|---|
| 392 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|---|
| 393 | "requested-attributes",
|
|---|
| 394 | (sizeof(requested) / sizeof(requested[0])),
|
|---|
| 395 | NULL, requested);
|
|---|
| 396 |
|
|---|
| 397 | if ((response = cupsDoRequest(http, request, "/")) == NULL) {
|
|---|
| 398 | DEBUG(0,("Unable to get printer list - %s\n",
|
|---|
| 399 | ippErrorString(cupsLastError())));
|
|---|
| 400 | goto out;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | ret = process_cups_printers_response(frame, response, &pcap_data);
|
|---|
| 404 | if (!ret) {
|
|---|
| 405 | DEBUG(0,("failed to process cups response\n"));
|
|---|
| 406 | goto out;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | pcap_data.status = NT_STATUS_OK;
|
|---|
| 410 | out:
|
|---|
| 411 | if (response)
|
|---|
| 412 | ippDelete(response);
|
|---|
| 413 |
|
|---|
| 414 | if (language)
|
|---|
| 415 | cupsLangFree(language);
|
|---|
| 416 |
|
|---|
| 417 | if (http)
|
|---|
| 418 | httpClose(http);
|
|---|
| 419 |
|
|---|
| 420 | ret = false;
|
|---|
| 421 | ndr_ret = ndr_push_struct_blob(&pcap_blob, frame, &pcap_data,
|
|---|
| 422 | (ndr_push_flags_fn_t)ndr_push_pcap_data);
|
|---|
| 423 | if (ndr_ret == NDR_ERR_SUCCESS) {
|
|---|
| 424 | ret = send_pcap_blob(&pcap_blob, fd);
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | TALLOC_FREE(frame);
|
|---|
| 428 | return ret;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | static struct fd_event *cache_fd_event;
|
|---|
| 432 |
|
|---|
| 433 | static bool cups_pcap_load_async(struct tevent_context *ev,
|
|---|
| 434 | struct messaging_context *msg_ctx,
|
|---|
| 435 | int *pfd)
|
|---|
| 436 | {
|
|---|
| 437 | int fds[2];
|
|---|
| 438 | pid_t pid;
|
|---|
| 439 | NTSTATUS status;
|
|---|
| 440 |
|
|---|
| 441 | *pfd = -1;
|
|---|
| 442 |
|
|---|
| 443 | if (cache_fd_event) {
|
|---|
| 444 | DEBUG(3,("cups_pcap_load_async: already waiting for "
|
|---|
| 445 | "a refresh event\n" ));
|
|---|
| 446 | return false;
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | DEBUG(5,("cups_pcap_load_async: asynchronously loading cups printers\n"));
|
|---|
| 450 |
|
|---|
| 451 | if (pipe(fds) == -1) {
|
|---|
| 452 | return false;
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | pid = sys_fork();
|
|---|
| 456 | if (pid == (pid_t)-1) {
|
|---|
| 457 | DEBUG(10,("cups_pcap_load_async: fork failed %s\n",
|
|---|
| 458 | strerror(errno) ));
|
|---|
| 459 | close(fds[0]);
|
|---|
| 460 | close(fds[1]);
|
|---|
| 461 | return false;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | if (pid) {
|
|---|
| 465 | DEBUG(10,("cups_pcap_load_async: child pid = %u\n",
|
|---|
| 466 | (unsigned int)pid ));
|
|---|
| 467 | /* Parent. */
|
|---|
| 468 | close(fds[1]);
|
|---|
| 469 | *pfd = fds[0];
|
|---|
| 470 | return true;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | /* Child. */
|
|---|
| 474 |
|
|---|
| 475 | close_all_print_db();
|
|---|
| 476 |
|
|---|
| 477 | status = reinit_after_fork(msg_ctx, ev, procid_self(), true);
|
|---|
| 478 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 479 | DEBUG(0,("cups_pcap_load_async: reinit_after_fork() failed\n"));
|
|---|
| 480 | smb_panic("cups_pcap_load_async: reinit_after_fork() failed");
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | close(fds[0]);
|
|---|
| 484 | cups_cache_reload_async(fds[1]);
|
|---|
| 485 | close(fds[1]);
|
|---|
| 486 | _exit(0);
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | struct cups_async_cb_args {
|
|---|
| 490 | int pipe_fd;
|
|---|
| 491 | struct event_context *event_ctx;
|
|---|
| 492 | struct messaging_context *msg_ctx;
|
|---|
| 493 | void (*post_cache_fill_fn)(struct event_context *,
|
|---|
| 494 | struct messaging_context *);
|
|---|
| 495 | };
|
|---|
| 496 |
|
|---|
| 497 | static void cups_async_callback(struct event_context *event_ctx,
|
|---|
| 498 | struct fd_event *event,
|
|---|
| 499 | uint16 flags,
|
|---|
| 500 | void *p)
|
|---|
| 501 | {
|
|---|
| 502 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 503 | struct cups_async_cb_args *cb_args = (struct cups_async_cb_args *)p;
|
|---|
| 504 | struct pcap_cache *tmp_pcap_cache = NULL;
|
|---|
| 505 | bool ret_ok;
|
|---|
| 506 | struct pcap_data pcap_data;
|
|---|
| 507 | DATA_BLOB pcap_blob;
|
|---|
| 508 | enum ndr_err_code ndr_ret;
|
|---|
| 509 | int i;
|
|---|
| 510 |
|
|---|
| 511 | DEBUG(5,("cups_async_callback: callback received for printer data. "
|
|---|
| 512 | "fd = %d\n", cb_args->pipe_fd));
|
|---|
| 513 |
|
|---|
| 514 | ret_ok = recv_pcap_blob(frame, cb_args->pipe_fd, &pcap_blob);
|
|---|
| 515 | if (!ret_ok) {
|
|---|
| 516 | DEBUG(0,("failed to recv pcap blob\n"));
|
|---|
| 517 | goto err_out;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | ndr_ret = ndr_pull_struct_blob(&pcap_blob, frame, &pcap_data,
|
|---|
| 521 | (ndr_pull_flags_fn_t)ndr_pull_pcap_data);
|
|---|
| 522 | if (ndr_ret != NDR_ERR_SUCCESS) {
|
|---|
| 523 | goto err_out;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | if (!NT_STATUS_IS_OK(pcap_data.status)) {
|
|---|
| 527 | DEBUG(0,("failed to retrieve printer list: %s\n",
|
|---|
| 528 | nt_errstr(pcap_data.status)));
|
|---|
| 529 | goto err_out;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | for (i = 0; i < pcap_data.count; i++) {
|
|---|
| 533 | ret_ok = pcap_cache_add_specific(&tmp_pcap_cache,
|
|---|
| 534 | pcap_data.printers[i].name,
|
|---|
| 535 | pcap_data.printers[i].info,
|
|---|
| 536 | pcap_data.printers[i].location);
|
|---|
| 537 | if (!ret_ok) {
|
|---|
| 538 | DEBUG(0, ("failed to add to tmp pcap cache\n"));
|
|---|
| 539 | goto err_out;
|
|---|
| 540 | }
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /* replace the system-wide pcap cache with a (possibly empty) new one */
|
|---|
| 544 | ret_ok = pcap_cache_replace(tmp_pcap_cache);
|
|---|
| 545 | if (!ret_ok) {
|
|---|
| 546 | DEBUG(0, ("failed to replace pcap cache\n"));
|
|---|
| 547 | } else if (cb_args->post_cache_fill_fn != NULL) {
|
|---|
| 548 | /* Caller requested post cache fill callback */
|
|---|
| 549 | cb_args->post_cache_fill_fn(cb_args->event_ctx,
|
|---|
| 550 | cb_args->msg_ctx);
|
|---|
| 551 | }
|
|---|
| 552 | err_out:
|
|---|
| 553 | pcap_cache_destroy_specific(&tmp_pcap_cache);
|
|---|
| 554 | TALLOC_FREE(frame);
|
|---|
| 555 | close(cb_args->pipe_fd);
|
|---|
| 556 | TALLOC_FREE(cb_args);
|
|---|
| 557 | TALLOC_FREE(cache_fd_event);
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | bool cups_cache_reload(struct tevent_context *ev,
|
|---|
| 561 | struct messaging_context *msg_ctx,
|
|---|
| 562 | void (*post_cache_fill_fn)(struct tevent_context *,
|
|---|
| 563 | struct messaging_context *))
|
|---|
| 564 | {
|
|---|
| 565 | struct cups_async_cb_args *cb_args;
|
|---|
| 566 | int *p_pipe_fd;
|
|---|
| 567 |
|
|---|
| 568 | cb_args = TALLOC_P(NULL, struct cups_async_cb_args);
|
|---|
| 569 | if (cb_args == NULL) {
|
|---|
| 570 | return false;
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | cb_args->post_cache_fill_fn = post_cache_fill_fn;
|
|---|
| 574 | cb_args->event_ctx = ev;
|
|---|
| 575 | cb_args->msg_ctx = msg_ctx;
|
|---|
| 576 | p_pipe_fd = &cb_args->pipe_fd;
|
|---|
| 577 | *p_pipe_fd = -1;
|
|---|
| 578 |
|
|---|
| 579 | /* Set up an async refresh. */
|
|---|
| 580 | if (!cups_pcap_load_async(ev, msg_ctx, p_pipe_fd)) {
|
|---|
| 581 | talloc_free(cb_args);
|
|---|
| 582 | return false;
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | DEBUG(10,("cups_cache_reload: async read on fd %d\n",
|
|---|
| 586 | *p_pipe_fd ));
|
|---|
| 587 |
|
|---|
| 588 | /* Trigger an event when the pipe can be read. */
|
|---|
| 589 | cache_fd_event = event_add_fd(ev,
|
|---|
| 590 | NULL, *p_pipe_fd,
|
|---|
| 591 | EVENT_FD_READ,
|
|---|
| 592 | cups_async_callback,
|
|---|
| 593 | (void *)cb_args);
|
|---|
| 594 | if (!cache_fd_event) {
|
|---|
| 595 | close(*p_pipe_fd);
|
|---|
| 596 | TALLOC_FREE(cb_args);
|
|---|
| 597 | return false;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | return true;
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | /*
|
|---|
| 604 | * 'cups_job_delete()' - Delete a job.
|
|---|
| 605 | */
|
|---|
| 606 |
|
|---|
| 607 | static int cups_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
|
|---|
| 608 | {
|
|---|
| 609 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 610 | int ret = 1; /* Return value */
|
|---|
| 611 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 612 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 613 | *response = NULL; /* IPP Response */
|
|---|
| 614 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 615 | char *user = NULL;
|
|---|
| 616 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 617 | size_t size;
|
|---|
| 618 |
|
|---|
| 619 | DEBUG(5,("cups_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
|
|---|
| 620 |
|
|---|
| 621 | /*
|
|---|
| 622 | * Make sure we don't ask for passwords...
|
|---|
| 623 | */
|
|---|
| 624 |
|
|---|
| 625 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 626 |
|
|---|
| 627 | /*
|
|---|
| 628 | * Try to connect to the server...
|
|---|
| 629 | */
|
|---|
| 630 |
|
|---|
| 631 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 632 | goto out;
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | /*
|
|---|
| 636 | * Build an IPP_CANCEL_JOB request, which requires the following
|
|---|
| 637 | * attributes:
|
|---|
| 638 | *
|
|---|
| 639 | * attributes-charset
|
|---|
| 640 | * attributes-natural-language
|
|---|
| 641 | * job-uri
|
|---|
| 642 | * requesting-user-name
|
|---|
| 643 | */
|
|---|
| 644 |
|
|---|
| 645 | request = ippNew();
|
|---|
| 646 |
|
|---|
| 647 | ippSetOperation(request, IPP_CANCEL_JOB);
|
|---|
| 648 | ippSetRequestId(request, 1);
|
|---|
| 649 |
|
|---|
| 650 | language = cupsLangDefault();
|
|---|
| 651 |
|
|---|
| 652 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 653 | "attributes-charset", NULL, "utf-8");
|
|---|
| 654 |
|
|---|
| 655 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 656 | "attributes-natural-language", NULL, language->language);
|
|---|
| 657 |
|
|---|
| 658 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
|
|---|
| 659 |
|
|---|
| 660 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
|
|---|
| 661 |
|
|---|
| 662 | if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
|
|---|
| 663 | goto out;
|
|---|
| 664 | }
|
|---|
| 665 |
|
|---|
| 666 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 667 | NULL, user);
|
|---|
| 668 |
|
|---|
| 669 | /*
|
|---|
| 670 | * Do the request and get back a response...
|
|---|
| 671 | */
|
|---|
| 672 |
|
|---|
| 673 | if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
|
|---|
| 674 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 675 | DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
|
|---|
| 676 | ippErrorString(cupsLastError())));
|
|---|
| 677 | } else {
|
|---|
| 678 | ret = 0;
|
|---|
| 679 | }
|
|---|
| 680 | } else {
|
|---|
| 681 | DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
|
|---|
| 682 | ippErrorString(cupsLastError())));
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | out:
|
|---|
| 686 | if (response)
|
|---|
| 687 | ippDelete(response);
|
|---|
| 688 |
|
|---|
| 689 | if (language)
|
|---|
| 690 | cupsLangFree(language);
|
|---|
| 691 |
|
|---|
| 692 | if (http)
|
|---|
| 693 | httpClose(http);
|
|---|
| 694 |
|
|---|
| 695 | TALLOC_FREE(frame);
|
|---|
| 696 | return ret;
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 | /*
|
|---|
| 701 | * 'cups_job_pause()' - Pause a job.
|
|---|
| 702 | */
|
|---|
| 703 |
|
|---|
| 704 | static int cups_job_pause(int snum, struct printjob *pjob)
|
|---|
| 705 | {
|
|---|
| 706 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 707 | int ret = 1; /* Return value */
|
|---|
| 708 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 709 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 710 | *response = NULL; /* IPP Response */
|
|---|
| 711 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 712 | char *user = NULL;
|
|---|
| 713 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 714 | size_t size;
|
|---|
| 715 |
|
|---|
| 716 | DEBUG(5,("cups_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
|
|---|
| 717 |
|
|---|
| 718 | /*
|
|---|
| 719 | * Make sure we don't ask for passwords...
|
|---|
| 720 | */
|
|---|
| 721 |
|
|---|
| 722 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 723 |
|
|---|
| 724 | /*
|
|---|
| 725 | * Try to connect to the server...
|
|---|
| 726 | */
|
|---|
| 727 |
|
|---|
| 728 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 729 | goto out;
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | /*
|
|---|
| 733 | * Build an IPP_HOLD_JOB request, which requires the following
|
|---|
| 734 | * attributes:
|
|---|
| 735 | *
|
|---|
| 736 | * attributes-charset
|
|---|
| 737 | * attributes-natural-language
|
|---|
| 738 | * job-uri
|
|---|
| 739 | * requesting-user-name
|
|---|
| 740 | */
|
|---|
| 741 |
|
|---|
| 742 | request = ippNew();
|
|---|
| 743 |
|
|---|
| 744 | ippSetOperation(request, IPP_HOLD_JOB);
|
|---|
| 745 | ippSetRequestId(request, 1);
|
|---|
| 746 |
|
|---|
| 747 | language = cupsLangDefault();
|
|---|
| 748 |
|
|---|
| 749 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 750 | "attributes-charset", NULL, "utf-8");
|
|---|
| 751 |
|
|---|
| 752 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 753 | "attributes-natural-language", NULL, language->language);
|
|---|
| 754 |
|
|---|
| 755 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
|
|---|
| 756 |
|
|---|
| 757 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
|
|---|
| 758 |
|
|---|
| 759 | if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
|
|---|
| 760 | goto out;
|
|---|
| 761 | }
|
|---|
| 762 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 763 | NULL, user);
|
|---|
| 764 |
|
|---|
| 765 | /*
|
|---|
| 766 | * Do the request and get back a response...
|
|---|
| 767 | */
|
|---|
| 768 |
|
|---|
| 769 | if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
|
|---|
| 770 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 771 | DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
|
|---|
| 772 | ippErrorString(cupsLastError())));
|
|---|
| 773 | } else {
|
|---|
| 774 | ret = 0;
|
|---|
| 775 | }
|
|---|
| 776 | } else {
|
|---|
| 777 | DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
|
|---|
| 778 | ippErrorString(cupsLastError())));
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | out:
|
|---|
| 782 | if (response)
|
|---|
| 783 | ippDelete(response);
|
|---|
| 784 |
|
|---|
| 785 | if (language)
|
|---|
| 786 | cupsLangFree(language);
|
|---|
| 787 |
|
|---|
| 788 | if (http)
|
|---|
| 789 | httpClose(http);
|
|---|
| 790 |
|
|---|
| 791 | TALLOC_FREE(frame);
|
|---|
| 792 | return ret;
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 | /*
|
|---|
| 797 | * 'cups_job_resume()' - Resume a paused job.
|
|---|
| 798 | */
|
|---|
| 799 |
|
|---|
| 800 | static int cups_job_resume(int snum, struct printjob *pjob)
|
|---|
| 801 | {
|
|---|
| 802 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 803 | int ret = 1; /* Return value */
|
|---|
| 804 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 805 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 806 | *response = NULL; /* IPP Response */
|
|---|
| 807 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 808 | char *user = NULL;
|
|---|
| 809 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 810 | size_t size;
|
|---|
| 811 |
|
|---|
| 812 | DEBUG(5,("cups_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
|
|---|
| 813 |
|
|---|
| 814 | /*
|
|---|
| 815 | * Make sure we don't ask for passwords...
|
|---|
| 816 | */
|
|---|
| 817 |
|
|---|
| 818 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 819 |
|
|---|
| 820 | /*
|
|---|
| 821 | * Try to connect to the server...
|
|---|
| 822 | */
|
|---|
| 823 |
|
|---|
| 824 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 825 | goto out;
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | /*
|
|---|
| 829 | * Build an IPP_RELEASE_JOB request, which requires the following
|
|---|
| 830 | * attributes:
|
|---|
| 831 | *
|
|---|
| 832 | * attributes-charset
|
|---|
| 833 | * attributes-natural-language
|
|---|
| 834 | * job-uri
|
|---|
| 835 | * requesting-user-name
|
|---|
| 836 | */
|
|---|
| 837 |
|
|---|
| 838 | request = ippNew();
|
|---|
| 839 |
|
|---|
| 840 | ippSetOperation(request, IPP_RELEASE_JOB);
|
|---|
| 841 | ippSetRequestId(request, 1);
|
|---|
| 842 |
|
|---|
| 843 | language = cupsLangDefault();
|
|---|
| 844 |
|
|---|
| 845 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 846 | "attributes-charset", NULL, "utf-8");
|
|---|
| 847 |
|
|---|
| 848 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 849 | "attributes-natural-language", NULL, language->language);
|
|---|
| 850 |
|
|---|
| 851 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/jobs/%d", pjob->sysjob);
|
|---|
| 852 |
|
|---|
| 853 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
|
|---|
| 854 |
|
|---|
| 855 | if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
|
|---|
| 856 | goto out;
|
|---|
| 857 | }
|
|---|
| 858 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 859 | NULL, user);
|
|---|
| 860 |
|
|---|
| 861 | /*
|
|---|
| 862 | * Do the request and get back a response...
|
|---|
| 863 | */
|
|---|
| 864 |
|
|---|
| 865 | if ((response = cupsDoRequest(http, request, "/jobs")) != NULL) {
|
|---|
| 866 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 867 | DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
|
|---|
| 868 | ippErrorString(cupsLastError())));
|
|---|
| 869 | } else {
|
|---|
| 870 | ret = 0;
|
|---|
| 871 | }
|
|---|
| 872 | } else {
|
|---|
| 873 | DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
|
|---|
| 874 | ippErrorString(cupsLastError())));
|
|---|
| 875 | }
|
|---|
| 876 |
|
|---|
| 877 | out:
|
|---|
| 878 | if (response)
|
|---|
| 879 | ippDelete(response);
|
|---|
| 880 |
|
|---|
| 881 | if (language)
|
|---|
| 882 | cupsLangFree(language);
|
|---|
| 883 |
|
|---|
| 884 | if (http)
|
|---|
| 885 | httpClose(http);
|
|---|
| 886 |
|
|---|
| 887 | TALLOC_FREE(frame);
|
|---|
| 888 | return ret;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 |
|
|---|
| 892 | /*
|
|---|
| 893 | * 'cups_job_submit()' - Submit a job for printing.
|
|---|
| 894 | */
|
|---|
| 895 |
|
|---|
| 896 | static int cups_job_submit(int snum, struct printjob *pjob,
|
|---|
| 897 | enum printing_types printing_type,
|
|---|
| 898 | char *lpq_cmd)
|
|---|
| 899 | {
|
|---|
| 900 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 901 | int ret = 1; /* Return value */
|
|---|
| 902 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 903 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 904 | *response = NULL; /* IPP Response */
|
|---|
| 905 | ipp_attribute_t *attr_job_id = NULL; /* IPP Attribute "job-id" */
|
|---|
| 906 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 907 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 908 | char *new_jobname = NULL;
|
|---|
| 909 | int num_options = 0;
|
|---|
| 910 | cups_option_t *options = NULL;
|
|---|
| 911 | char *printername = NULL;
|
|---|
| 912 | char *user = NULL;
|
|---|
| 913 | char *jobname = NULL;
|
|---|
| 914 | char *cupsoptions = NULL;
|
|---|
| 915 | char *filename = NULL;
|
|---|
| 916 | size_t size;
|
|---|
| 917 |
|
|---|
| 918 | DEBUG(5,("cups_job_submit(%d, %p)\n", snum, pjob));
|
|---|
| 919 |
|
|---|
| 920 | /*
|
|---|
| 921 | * Make sure we don't ask for passwords...
|
|---|
| 922 | */
|
|---|
| 923 |
|
|---|
| 924 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 925 |
|
|---|
| 926 | /*
|
|---|
| 927 | * Try to connect to the server...
|
|---|
| 928 | */
|
|---|
| 929 |
|
|---|
| 930 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 931 | goto out;
|
|---|
| 932 | }
|
|---|
| 933 |
|
|---|
| 934 | /*
|
|---|
| 935 | * Build an IPP_PRINT_JOB request, which requires the following
|
|---|
| 936 | * attributes:
|
|---|
| 937 | *
|
|---|
| 938 | * attributes-charset
|
|---|
| 939 | * attributes-natural-language
|
|---|
| 940 | * printer-uri
|
|---|
| 941 | * requesting-user-name
|
|---|
| 942 | * [document-data]
|
|---|
| 943 | */
|
|---|
| 944 |
|
|---|
| 945 | request = ippNew();
|
|---|
| 946 |
|
|---|
| 947 | ippSetOperation(request, IPP_PRINT_JOB);
|
|---|
| 948 | ippSetRequestId(request, 1);
|
|---|
| 949 |
|
|---|
| 950 | language = cupsLangDefault();
|
|---|
| 951 |
|
|---|
| 952 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 953 | "attributes-charset", NULL, "utf-8");
|
|---|
| 954 |
|
|---|
| 955 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 956 | "attributes-natural-language", NULL, language->language);
|
|---|
| 957 |
|
|---|
| 958 | if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
|
|---|
| 959 | &size)) {
|
|---|
| 960 | goto out;
|
|---|
| 961 | }
|
|---|
| 962 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
|
|---|
| 963 | printername);
|
|---|
| 964 |
|
|---|
| 965 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 966 | "printer-uri", NULL, uri);
|
|---|
| 967 |
|
|---|
| 968 | if (!push_utf8_talloc(frame, &user, pjob->user, &size)) {
|
|---|
| 969 | goto out;
|
|---|
| 970 | }
|
|---|
| 971 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 972 | NULL, user);
|
|---|
| 973 |
|
|---|
| 974 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|---|
| 975 | "job-originating-host-name", NULL,
|
|---|
| 976 | pjob->clientmachine);
|
|---|
| 977 |
|
|---|
| 978 | if (!push_utf8_talloc(frame, &jobname, pjob->jobname, &size)) {
|
|---|
| 979 | goto out;
|
|---|
| 980 | }
|
|---|
| 981 | new_jobname = talloc_asprintf(frame,
|
|---|
| 982 | "%s%.8u %s", PRINT_SPOOL_PREFIX,
|
|---|
| 983 | pjob->jobid, jobname);
|
|---|
| 984 | if (new_jobname == NULL) {
|
|---|
| 985 | goto out;
|
|---|
| 986 | }
|
|---|
| 987 |
|
|---|
| 988 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
|
|---|
| 989 | new_jobname);
|
|---|
| 990 |
|
|---|
| 991 | /*
|
|---|
| 992 | * add any options defined in smb.conf
|
|---|
| 993 | */
|
|---|
| 994 |
|
|---|
| 995 | if (!push_utf8_talloc(frame, &cupsoptions, lp_cups_options(snum), &size)) {
|
|---|
| 996 | goto out;
|
|---|
| 997 | }
|
|---|
| 998 | num_options = 0;
|
|---|
| 999 | options = NULL;
|
|---|
| 1000 | num_options = cupsParseOptions(cupsoptions, num_options, &options);
|
|---|
| 1001 |
|
|---|
| 1002 | if ( num_options )
|
|---|
| 1003 | cupsEncodeOptions(request, num_options, options);
|
|---|
| 1004 |
|
|---|
| 1005 | /*
|
|---|
| 1006 | * Do the request and get back a response...
|
|---|
| 1007 | */
|
|---|
| 1008 |
|
|---|
| 1009 | slprintf(uri, sizeof(uri) - 1, "/printers/%s", printername);
|
|---|
| 1010 |
|
|---|
| 1011 | if (!push_utf8_talloc(frame, &filename, pjob->filename, &size)) {
|
|---|
| 1012 | goto out;
|
|---|
| 1013 | }
|
|---|
| 1014 | if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
|
|---|
| 1015 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1016 | DEBUG(0,("Unable to print file to %s - %s\n",
|
|---|
| 1017 | lp_printername(snum),
|
|---|
| 1018 | ippErrorString(cupsLastError())));
|
|---|
| 1019 | } else {
|
|---|
| 1020 | ret = 0;
|
|---|
| 1021 | attr_job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
|
|---|
| 1022 | if(attr_job_id) {
|
|---|
| 1023 | pjob->sysjob = ippGetInteger(attr_job_id, 0);
|
|---|
| 1024 | DEBUG(5,("cups_job_submit: job-id %d\n", pjob->sysjob));
|
|---|
| 1025 | } else {
|
|---|
| 1026 | DEBUG(0,("Missing job-id attribute in IPP response"));
|
|---|
| 1027 | }
|
|---|
| 1028 | }
|
|---|
| 1029 | } else {
|
|---|
| 1030 | DEBUG(0,("Unable to print file to `%s' - %s\n",
|
|---|
| 1031 | lp_printername(snum),
|
|---|
| 1032 | ippErrorString(cupsLastError())));
|
|---|
| 1033 | }
|
|---|
| 1034 |
|
|---|
| 1035 | if ( ret == 0 )
|
|---|
| 1036 | unlink(pjob->filename);
|
|---|
| 1037 | /* else print_job_end will do it for us */
|
|---|
| 1038 |
|
|---|
| 1039 | out:
|
|---|
| 1040 | if (response)
|
|---|
| 1041 | ippDelete(response);
|
|---|
| 1042 |
|
|---|
| 1043 | if (language)
|
|---|
| 1044 | cupsLangFree(language);
|
|---|
| 1045 |
|
|---|
| 1046 | if (http)
|
|---|
| 1047 | httpClose(http);
|
|---|
| 1048 |
|
|---|
| 1049 | TALLOC_FREE(frame);
|
|---|
| 1050 |
|
|---|
| 1051 | return ret;
|
|---|
| 1052 | }
|
|---|
| 1053 |
|
|---|
| 1054 | /*
|
|---|
| 1055 | * 'cups_queue_get()' - Get all the jobs in the print queue.
|
|---|
| 1056 | */
|
|---|
| 1057 |
|
|---|
| 1058 | static int cups_queue_get(const char *sharename,
|
|---|
| 1059 | enum printing_types printing_type,
|
|---|
| 1060 | char *lpq_command,
|
|---|
| 1061 | print_queue_struct **q,
|
|---|
| 1062 | print_status_struct *status)
|
|---|
| 1063 | {
|
|---|
| 1064 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 1065 | char *printername = NULL;
|
|---|
| 1066 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 1067 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 1068 | *response = NULL; /* IPP Response */
|
|---|
| 1069 | ipp_attribute_t *attr = NULL; /* Current attribute */
|
|---|
| 1070 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 1071 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 1072 | int qcount = 0, /* Number of active queue entries */
|
|---|
| 1073 | qalloc = 0; /* Number of queue entries allocated */
|
|---|
| 1074 | print_queue_struct *queue = NULL, /* Queue entries */
|
|---|
| 1075 | *temp; /* Temporary pointer for queue */
|
|---|
| 1076 | char *user_name = NULL, /* job-originating-user-name attribute */
|
|---|
| 1077 | *job_name = NULL; /* job-name attribute */
|
|---|
| 1078 | int job_id; /* job-id attribute */
|
|---|
| 1079 | int job_k_octets; /* job-k-octets attribute */
|
|---|
| 1080 | time_t job_time; /* time-at-creation attribute */
|
|---|
| 1081 | ipp_jstate_t job_status; /* job-status attribute */
|
|---|
| 1082 | int job_priority; /* job-priority attribute */
|
|---|
| 1083 | size_t size;
|
|---|
| 1084 | static const char *jattrs[] = /* Requested job attributes */
|
|---|
| 1085 | {
|
|---|
| 1086 | "job-id",
|
|---|
| 1087 | "job-k-octets",
|
|---|
| 1088 | "job-name",
|
|---|
| 1089 | "job-originating-user-name",
|
|---|
| 1090 | "job-priority",
|
|---|
| 1091 | "job-state",
|
|---|
| 1092 | "time-at-creation",
|
|---|
| 1093 | };
|
|---|
| 1094 | static const char *pattrs[] = /* Requested printer attributes */
|
|---|
| 1095 | {
|
|---|
| 1096 | "printer-state",
|
|---|
| 1097 | "printer-state-message"
|
|---|
| 1098 | };
|
|---|
| 1099 |
|
|---|
| 1100 | *q = NULL;
|
|---|
| 1101 |
|
|---|
| 1102 | /* HACK ALERT!!! The problem with support the 'printer name'
|
|---|
| 1103 | option is that we key the tdb off the sharename. So we will
|
|---|
| 1104 | overload the lpq_command string to pass in the printername
|
|---|
| 1105 | (which is basically what we do for non-cups printers ... using
|
|---|
| 1106 | the lpq_command to get the queue listing). */
|
|---|
| 1107 |
|
|---|
| 1108 | if (!push_utf8_talloc(frame, &printername, lpq_command, &size)) {
|
|---|
| 1109 | goto out;
|
|---|
| 1110 | }
|
|---|
| 1111 | DEBUG(5,("cups_queue_get(%s, %p, %p)\n", lpq_command, q, status));
|
|---|
| 1112 |
|
|---|
| 1113 | /*
|
|---|
| 1114 | * Make sure we don't ask for passwords...
|
|---|
| 1115 | */
|
|---|
| 1116 |
|
|---|
| 1117 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 1118 |
|
|---|
| 1119 | /*
|
|---|
| 1120 | * Try to connect to the server...
|
|---|
| 1121 | */
|
|---|
| 1122 |
|
|---|
| 1123 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 1124 | goto out;
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | /*
|
|---|
| 1128 | * Generate the printer URI...
|
|---|
| 1129 | */
|
|---|
| 1130 |
|
|---|
| 1131 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s", printername);
|
|---|
| 1132 |
|
|---|
| 1133 | /*
|
|---|
| 1134 | * Build an IPP_GET_JOBS request, which requires the following
|
|---|
| 1135 | * attributes:
|
|---|
| 1136 | *
|
|---|
| 1137 | * attributes-charset
|
|---|
| 1138 | * attributes-natural-language
|
|---|
| 1139 | * requested-attributes
|
|---|
| 1140 | * printer-uri
|
|---|
| 1141 | */
|
|---|
| 1142 |
|
|---|
| 1143 | request = ippNew();
|
|---|
| 1144 |
|
|---|
| 1145 | ippSetOperation(request, IPP_GET_JOBS);
|
|---|
| 1146 | ippSetRequestId(request, 1);
|
|---|
| 1147 |
|
|---|
| 1148 | language = cupsLangDefault();
|
|---|
| 1149 |
|
|---|
| 1150 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 1151 | "attributes-charset", NULL, "utf-8");
|
|---|
| 1152 |
|
|---|
| 1153 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 1154 | "attributes-natural-language", NULL, language->language);
|
|---|
| 1155 |
|
|---|
| 1156 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|---|
| 1157 | "requested-attributes",
|
|---|
| 1158 | (sizeof(jattrs) / sizeof(jattrs[0])),
|
|---|
| 1159 | NULL, jattrs);
|
|---|
| 1160 |
|
|---|
| 1161 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 1162 | "printer-uri", NULL, uri);
|
|---|
| 1163 |
|
|---|
| 1164 | /*
|
|---|
| 1165 | * Do the request and get back a response...
|
|---|
| 1166 | */
|
|---|
| 1167 |
|
|---|
| 1168 | if ((response = cupsDoRequest(http, request, "/")) == NULL) {
|
|---|
| 1169 | DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
|
|---|
| 1170 | ippErrorString(cupsLastError())));
|
|---|
| 1171 | goto out;
|
|---|
| 1172 | }
|
|---|
| 1173 |
|
|---|
| 1174 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1175 | DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
|
|---|
| 1176 | ippErrorString(ippGetStatusCode(response))));
|
|---|
| 1177 | goto out;
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | /*
|
|---|
| 1181 | * Process the jobs...
|
|---|
| 1182 | */
|
|---|
| 1183 |
|
|---|
| 1184 | qcount = 0;
|
|---|
| 1185 | qalloc = 0;
|
|---|
| 1186 | queue = NULL;
|
|---|
| 1187 |
|
|---|
| 1188 | for (attr = ippFirstAttribute(response); attr != NULL; attr = ippNextAttribute(response)) {
|
|---|
| 1189 | /*
|
|---|
| 1190 | * Skip leading attributes until we hit a job...
|
|---|
| 1191 | */
|
|---|
| 1192 |
|
|---|
| 1193 | while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_JOB)
|
|---|
| 1194 | attr = ippNextAttribute(response);
|
|---|
| 1195 |
|
|---|
| 1196 | if (attr == NULL)
|
|---|
| 1197 | break;
|
|---|
| 1198 |
|
|---|
| 1199 | /*
|
|---|
| 1200 | * Allocate memory as needed...
|
|---|
| 1201 | */
|
|---|
| 1202 | if (qcount >= qalloc) {
|
|---|
| 1203 | qalloc += 16;
|
|---|
| 1204 |
|
|---|
| 1205 | queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
|
|---|
| 1206 |
|
|---|
| 1207 | if (queue == NULL) {
|
|---|
| 1208 | DEBUG(0,("cups_queue_get: Not enough memory!"));
|
|---|
| 1209 | qcount = 0;
|
|---|
| 1210 | goto out;
|
|---|
| 1211 | }
|
|---|
| 1212 | }
|
|---|
| 1213 |
|
|---|
| 1214 | temp = queue + qcount;
|
|---|
| 1215 | memset(temp, 0, sizeof(print_queue_struct));
|
|---|
| 1216 |
|
|---|
| 1217 | /*
|
|---|
| 1218 | * Pull the needed attributes from this job...
|
|---|
| 1219 | */
|
|---|
| 1220 |
|
|---|
| 1221 | job_id = 0;
|
|---|
| 1222 | job_priority = 50;
|
|---|
| 1223 | job_status = IPP_JOB_PENDING;
|
|---|
| 1224 | job_time = 0;
|
|---|
| 1225 | job_k_octets = 0;
|
|---|
| 1226 | user_name = NULL;
|
|---|
| 1227 | job_name = NULL;
|
|---|
| 1228 |
|
|---|
| 1229 | while (attr != NULL && ippGetGroupTag(attr) == IPP_TAG_JOB) {
|
|---|
| 1230 | if (ippGetName(attr) == NULL) {
|
|---|
| 1231 | attr = ippNextAttribute(response);
|
|---|
| 1232 | break;
|
|---|
| 1233 | }
|
|---|
| 1234 |
|
|---|
| 1235 | if (strcmp(ippGetName(attr), "job-id") == 0 &&
|
|---|
| 1236 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1237 | job_id = ippGetInteger(attr, 0);
|
|---|
| 1238 |
|
|---|
| 1239 | if (strcmp(ippGetName(attr), "job-k-octets") == 0 &&
|
|---|
| 1240 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1241 | job_k_octets = ippGetInteger(attr, 0);
|
|---|
| 1242 |
|
|---|
| 1243 | if (strcmp(ippGetName(attr), "job-priority") == 0 &&
|
|---|
| 1244 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1245 | job_priority = ippGetInteger(attr, 0);
|
|---|
| 1246 |
|
|---|
| 1247 | if (strcmp(ippGetName(attr), "job-state") == 0 &&
|
|---|
| 1248 | ippGetValueTag(attr) == IPP_TAG_ENUM)
|
|---|
| 1249 | job_status = (ipp_jstate_t)ippGetInteger(attr, 0);
|
|---|
| 1250 |
|
|---|
| 1251 | if (strcmp(ippGetName(attr), "time-at-creation") == 0 &&
|
|---|
| 1252 | ippGetValueTag(attr) == IPP_TAG_INTEGER)
|
|---|
| 1253 | job_time = ippGetInteger(attr, 0);
|
|---|
| 1254 |
|
|---|
| 1255 | if (strcmp(ippGetName(attr), "job-name") == 0 &&
|
|---|
| 1256 | ippGetValueTag(attr) == IPP_TAG_NAME) {
|
|---|
| 1257 | if (!pull_utf8_talloc(frame,
|
|---|
| 1258 | &job_name,
|
|---|
| 1259 | ippGetString(attr, 0, NULL),
|
|---|
| 1260 | &size)) {
|
|---|
| 1261 | goto out;
|
|---|
| 1262 | }
|
|---|
| 1263 | }
|
|---|
| 1264 |
|
|---|
| 1265 | if (strcmp(ippGetName(attr), "job-originating-user-name") == 0 &&
|
|---|
| 1266 | ippGetValueTag(attr) == IPP_TAG_NAME) {
|
|---|
| 1267 | if (!pull_utf8_talloc(frame,
|
|---|
| 1268 | &user_name,
|
|---|
| 1269 | ippGetString(attr, 0, NULL),
|
|---|
| 1270 | &size)) {
|
|---|
| 1271 | goto out;
|
|---|
| 1272 | }
|
|---|
| 1273 | }
|
|---|
| 1274 |
|
|---|
| 1275 | attr = ippNextAttribute(response);
|
|---|
| 1276 | }
|
|---|
| 1277 |
|
|---|
| 1278 | /*
|
|---|
| 1279 | * See if we have everything needed...
|
|---|
| 1280 | */
|
|---|
| 1281 |
|
|---|
| 1282 | if (user_name == NULL || job_name == NULL || job_id == 0) {
|
|---|
| 1283 | if (attr == NULL)
|
|---|
| 1284 | break;
|
|---|
| 1285 | else
|
|---|
| 1286 | continue;
|
|---|
| 1287 | }
|
|---|
| 1288 |
|
|---|
| 1289 | temp->sysjob = job_id;
|
|---|
| 1290 | temp->size = job_k_octets * 1024;
|
|---|
| 1291 | temp->status = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
|
|---|
| 1292 | job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
|
|---|
| 1293 | job_status == IPP_JOB_HELD ? LPQ_PAUSED :
|
|---|
| 1294 | LPQ_PRINTING;
|
|---|
| 1295 | temp->priority = job_priority;
|
|---|
| 1296 | temp->time = job_time;
|
|---|
| 1297 | strlcpy(temp->fs_user, user_name, sizeof(temp->fs_user));
|
|---|
| 1298 | strlcpy(temp->fs_file, job_name, sizeof(temp->fs_file));
|
|---|
| 1299 |
|
|---|
| 1300 | qcount ++;
|
|---|
| 1301 |
|
|---|
| 1302 | if (attr == NULL)
|
|---|
| 1303 | break;
|
|---|
| 1304 | }
|
|---|
| 1305 |
|
|---|
| 1306 | ippDelete(response);
|
|---|
| 1307 | response = NULL;
|
|---|
| 1308 |
|
|---|
| 1309 | /*
|
|---|
| 1310 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the
|
|---|
| 1311 | * following attributes:
|
|---|
| 1312 | *
|
|---|
| 1313 | * attributes-charset
|
|---|
| 1314 | * attributes-natural-language
|
|---|
| 1315 | * requested-attributes
|
|---|
| 1316 | * printer-uri
|
|---|
| 1317 | */
|
|---|
| 1318 |
|
|---|
| 1319 | request = ippNew();
|
|---|
| 1320 |
|
|---|
| 1321 | ippSetOperation(request, IPP_GET_PRINTER_ATTRIBUTES);
|
|---|
| 1322 | ippSetRequestId(request, 1);
|
|---|
| 1323 |
|
|---|
| 1324 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 1325 | "attributes-charset", NULL, "utf-8");
|
|---|
| 1326 |
|
|---|
| 1327 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 1328 | "attributes-natural-language", NULL, language->language);
|
|---|
| 1329 |
|
|---|
| 1330 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
|
|---|
| 1331 | "requested-attributes",
|
|---|
| 1332 | (sizeof(pattrs) / sizeof(pattrs[0])),
|
|---|
| 1333 | NULL, pattrs);
|
|---|
| 1334 |
|
|---|
| 1335 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
|
|---|
| 1336 | "printer-uri", NULL, uri);
|
|---|
| 1337 |
|
|---|
| 1338 | /*
|
|---|
| 1339 | * Do the request and get back a response...
|
|---|
| 1340 | */
|
|---|
| 1341 |
|
|---|
| 1342 | if ((response = cupsDoRequest(http, request, "/")) == NULL) {
|
|---|
| 1343 | DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
|
|---|
| 1344 | ippErrorString(cupsLastError())));
|
|---|
| 1345 | goto out;
|
|---|
| 1346 | }
|
|---|
| 1347 |
|
|---|
| 1348 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1349 | DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
|
|---|
| 1350 | ippErrorString(ippGetStatusCode(response))));
|
|---|
| 1351 | goto out;
|
|---|
| 1352 | }
|
|---|
| 1353 |
|
|---|
| 1354 | /*
|
|---|
| 1355 | * Get the current printer status and convert it to the SAMBA values.
|
|---|
| 1356 | */
|
|---|
| 1357 |
|
|---|
| 1358 | if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
|
|---|
| 1359 | if (ippGetInteger(attr, 0) == IPP_PRINTER_STOPPED)
|
|---|
| 1360 | status->status = LPSTAT_STOPPED;
|
|---|
| 1361 | else
|
|---|
| 1362 | status->status = LPSTAT_OK;
|
|---|
| 1363 | }
|
|---|
| 1364 |
|
|---|
| 1365 | if ((attr = ippFindAttribute(response, "printer-state-message",
|
|---|
| 1366 | IPP_TAG_TEXT)) != NULL) {
|
|---|
| 1367 | char *msg = NULL;
|
|---|
| 1368 | if (!pull_utf8_talloc(frame, &msg,
|
|---|
| 1369 | ippGetString(attr, 0, NULL),
|
|---|
| 1370 | &size)) {
|
|---|
| 1371 | SAFE_FREE(queue);
|
|---|
| 1372 | qcount = 0;
|
|---|
| 1373 | goto out;
|
|---|
| 1374 | }
|
|---|
| 1375 | fstrcpy(status->message, msg);
|
|---|
| 1376 | }
|
|---|
| 1377 |
|
|---|
| 1378 | out:
|
|---|
| 1379 |
|
|---|
| 1380 | /*
|
|---|
| 1381 | * Return the job queue...
|
|---|
| 1382 | */
|
|---|
| 1383 |
|
|---|
| 1384 | *q = queue;
|
|---|
| 1385 |
|
|---|
| 1386 | if (response)
|
|---|
| 1387 | ippDelete(response);
|
|---|
| 1388 |
|
|---|
| 1389 | if (language)
|
|---|
| 1390 | cupsLangFree(language);
|
|---|
| 1391 |
|
|---|
| 1392 | if (http)
|
|---|
| 1393 | httpClose(http);
|
|---|
| 1394 |
|
|---|
| 1395 | TALLOC_FREE(frame);
|
|---|
| 1396 | return qcount;
|
|---|
| 1397 | }
|
|---|
| 1398 |
|
|---|
| 1399 |
|
|---|
| 1400 | /*
|
|---|
| 1401 | * 'cups_queue_pause()' - Pause a print queue.
|
|---|
| 1402 | */
|
|---|
| 1403 |
|
|---|
| 1404 | static int cups_queue_pause(int snum)
|
|---|
| 1405 | {
|
|---|
| 1406 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 1407 | int ret = 1; /* Return value */
|
|---|
| 1408 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 1409 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 1410 | *response = NULL; /* IPP Response */
|
|---|
| 1411 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 1412 | char *printername = NULL;
|
|---|
| 1413 | char *username = NULL;
|
|---|
| 1414 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 1415 | size_t size;
|
|---|
| 1416 |
|
|---|
| 1417 | DEBUG(5,("cups_queue_pause(%d)\n", snum));
|
|---|
| 1418 |
|
|---|
| 1419 | /*
|
|---|
| 1420 | * Make sure we don't ask for passwords...
|
|---|
| 1421 | */
|
|---|
| 1422 |
|
|---|
| 1423 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 1424 |
|
|---|
| 1425 | /*
|
|---|
| 1426 | * Try to connect to the server...
|
|---|
| 1427 | */
|
|---|
| 1428 |
|
|---|
| 1429 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 1430 | goto out;
|
|---|
| 1431 | }
|
|---|
| 1432 |
|
|---|
| 1433 | /*
|
|---|
| 1434 | * Build an IPP_PAUSE_PRINTER request, which requires the following
|
|---|
| 1435 | * attributes:
|
|---|
| 1436 | *
|
|---|
| 1437 | * attributes-charset
|
|---|
| 1438 | * attributes-natural-language
|
|---|
| 1439 | * printer-uri
|
|---|
| 1440 | * requesting-user-name
|
|---|
| 1441 | */
|
|---|
| 1442 |
|
|---|
| 1443 | request = ippNew();
|
|---|
| 1444 |
|
|---|
| 1445 | ippSetOperation(request, IPP_PAUSE_PRINTER);
|
|---|
| 1446 | ippSetRequestId(request, 1);
|
|---|
| 1447 |
|
|---|
| 1448 | language = cupsLangDefault();
|
|---|
| 1449 |
|
|---|
| 1450 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 1451 | "attributes-charset", NULL, "utf-8");
|
|---|
| 1452 |
|
|---|
| 1453 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 1454 | "attributes-natural-language", NULL, language->language);
|
|---|
| 1455 |
|
|---|
| 1456 | if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
|
|---|
| 1457 | &size)) {
|
|---|
| 1458 | goto out;
|
|---|
| 1459 | }
|
|---|
| 1460 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
|
|---|
| 1461 | printername);
|
|---|
| 1462 |
|
|---|
| 1463 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
|
|---|
| 1464 |
|
|---|
| 1465 | if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
|
|---|
| 1466 | goto out;
|
|---|
| 1467 | }
|
|---|
| 1468 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 1469 | NULL, username);
|
|---|
| 1470 |
|
|---|
| 1471 | /*
|
|---|
| 1472 | * Do the request and get back a response...
|
|---|
| 1473 | */
|
|---|
| 1474 |
|
|---|
| 1475 | if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
|
|---|
| 1476 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1477 | DEBUG(0,("Unable to pause printer %s - %s\n",
|
|---|
| 1478 | lp_printername(snum),
|
|---|
| 1479 | ippErrorString(cupsLastError())));
|
|---|
| 1480 | } else {
|
|---|
| 1481 | ret = 0;
|
|---|
| 1482 | }
|
|---|
| 1483 | } else {
|
|---|
| 1484 | DEBUG(0,("Unable to pause printer %s - %s\n",
|
|---|
| 1485 | lp_printername(snum),
|
|---|
| 1486 | ippErrorString(cupsLastError())));
|
|---|
| 1487 | }
|
|---|
| 1488 |
|
|---|
| 1489 | out:
|
|---|
| 1490 | if (response)
|
|---|
| 1491 | ippDelete(response);
|
|---|
| 1492 |
|
|---|
| 1493 | if (language)
|
|---|
| 1494 | cupsLangFree(language);
|
|---|
| 1495 |
|
|---|
| 1496 | if (http)
|
|---|
| 1497 | httpClose(http);
|
|---|
| 1498 |
|
|---|
| 1499 | TALLOC_FREE(frame);
|
|---|
| 1500 | return ret;
|
|---|
| 1501 | }
|
|---|
| 1502 |
|
|---|
| 1503 |
|
|---|
| 1504 | /*
|
|---|
| 1505 | * 'cups_queue_resume()' - Restart a print queue.
|
|---|
| 1506 | */
|
|---|
| 1507 |
|
|---|
| 1508 | static int cups_queue_resume(int snum)
|
|---|
| 1509 | {
|
|---|
| 1510 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 1511 | int ret = 1; /* Return value */
|
|---|
| 1512 | http_t *http = NULL; /* HTTP connection to server */
|
|---|
| 1513 | ipp_t *request = NULL, /* IPP Request */
|
|---|
| 1514 | *response = NULL; /* IPP Response */
|
|---|
| 1515 | cups_lang_t *language = NULL; /* Default language */
|
|---|
| 1516 | char *printername = NULL;
|
|---|
| 1517 | char *username = NULL;
|
|---|
| 1518 | char uri[HTTP_MAX_URI]; /* printer-uri attribute */
|
|---|
| 1519 | size_t size;
|
|---|
| 1520 |
|
|---|
| 1521 | DEBUG(5,("cups_queue_resume(%d)\n", snum));
|
|---|
| 1522 |
|
|---|
| 1523 | /*
|
|---|
| 1524 | * Make sure we don't ask for passwords...
|
|---|
| 1525 | */
|
|---|
| 1526 |
|
|---|
| 1527 | cupsSetPasswordCB(cups_passwd_cb);
|
|---|
| 1528 |
|
|---|
| 1529 | /*
|
|---|
| 1530 | * Try to connect to the server...
|
|---|
| 1531 | */
|
|---|
| 1532 |
|
|---|
| 1533 | if ((http = cups_connect(frame)) == NULL) {
|
|---|
| 1534 | goto out;
|
|---|
| 1535 | }
|
|---|
| 1536 |
|
|---|
| 1537 | /*
|
|---|
| 1538 | * Build an IPP_RESUME_PRINTER request, which requires the following
|
|---|
| 1539 | * attributes:
|
|---|
| 1540 | *
|
|---|
| 1541 | * attributes-charset
|
|---|
| 1542 | * attributes-natural-language
|
|---|
| 1543 | * printer-uri
|
|---|
| 1544 | * requesting-user-name
|
|---|
| 1545 | */
|
|---|
| 1546 |
|
|---|
| 1547 | request = ippNew();
|
|---|
| 1548 |
|
|---|
| 1549 | ippSetOperation(request, IPP_RESUME_PRINTER);
|
|---|
| 1550 | ippSetRequestId(request, 1);
|
|---|
| 1551 |
|
|---|
| 1552 | language = cupsLangDefault();
|
|---|
| 1553 |
|
|---|
| 1554 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
|
|---|
| 1555 | "attributes-charset", NULL, "utf-8");
|
|---|
| 1556 |
|
|---|
| 1557 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
|
|---|
| 1558 | "attributes-natural-language", NULL, language->language);
|
|---|
| 1559 |
|
|---|
| 1560 | if (!push_utf8_talloc(frame, &printername, lp_printername(snum),
|
|---|
| 1561 | &size)) {
|
|---|
| 1562 | goto out;
|
|---|
| 1563 | }
|
|---|
| 1564 | slprintf(uri, sizeof(uri) - 1, "ipp://localhost/printers/%s",
|
|---|
| 1565 | printername);
|
|---|
| 1566 |
|
|---|
| 1567 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
|
|---|
| 1568 |
|
|---|
| 1569 | if (!push_utf8_talloc(frame, &username, current_user_info.unix_name, &size)) {
|
|---|
| 1570 | goto out;
|
|---|
| 1571 | }
|
|---|
| 1572 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
|
|---|
| 1573 | NULL, username);
|
|---|
| 1574 |
|
|---|
| 1575 | /*
|
|---|
| 1576 | * Do the request and get back a response...
|
|---|
| 1577 | */
|
|---|
| 1578 |
|
|---|
| 1579 | if ((response = cupsDoRequest(http, request, "/admin/")) != NULL) {
|
|---|
| 1580 | if (ippGetStatusCode(response) >= IPP_OK_CONFLICT) {
|
|---|
| 1581 | DEBUG(0,("Unable to resume printer %s - %s\n",
|
|---|
| 1582 | lp_printername(snum),
|
|---|
| 1583 | ippErrorString(cupsLastError())));
|
|---|
| 1584 | } else {
|
|---|
| 1585 | ret = 0;
|
|---|
| 1586 | }
|
|---|
| 1587 | } else {
|
|---|
| 1588 | DEBUG(0,("Unable to resume printer %s - %s\n",
|
|---|
| 1589 | lp_printername(snum),
|
|---|
| 1590 | ippErrorString(cupsLastError())));
|
|---|
| 1591 | }
|
|---|
| 1592 |
|
|---|
| 1593 | out:
|
|---|
| 1594 | if (response)
|
|---|
| 1595 | ippDelete(response);
|
|---|
| 1596 |
|
|---|
| 1597 | if (language)
|
|---|
| 1598 | cupsLangFree(language);
|
|---|
| 1599 |
|
|---|
| 1600 | if (http)
|
|---|
| 1601 | httpClose(http);
|
|---|
| 1602 |
|
|---|
| 1603 | TALLOC_FREE(frame);
|
|---|
| 1604 | return ret;
|
|---|
| 1605 | }
|
|---|
| 1606 |
|
|---|
| 1607 | /*******************************************************************
|
|---|
| 1608 | * CUPS printing interface definitions...
|
|---|
| 1609 | ******************************************************************/
|
|---|
| 1610 |
|
|---|
| 1611 | struct printif cups_printif =
|
|---|
| 1612 | {
|
|---|
| 1613 | PRINT_CUPS,
|
|---|
| 1614 | cups_queue_get,
|
|---|
| 1615 | cups_queue_pause,
|
|---|
| 1616 | cups_queue_resume,
|
|---|
| 1617 | cups_job_delete,
|
|---|
| 1618 | cups_job_pause,
|
|---|
| 1619 | cups_job_resume,
|
|---|
| 1620 | cups_job_submit,
|
|---|
| 1621 | };
|
|---|
| 1622 |
|
|---|
| 1623 | #else
|
|---|
| 1624 | /* this keeps fussy compilers happy */
|
|---|
| 1625 | void print_cups_dummy(void);
|
|---|
| 1626 | void print_cups_dummy(void) {}
|
|---|
| 1627 | #endif /* HAVE_CUPS */
|
|---|