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