]> git.ipfire.org Git - thirdparty/cups.git/blame - backend/ipp.c
Updated configure script to set PAMDIR when PAM is enabled.
[thirdparty/cups.git] / backend / ipp.c
CommitLineData
c8f9565c 1/*
0a3944d6 2 * "$Id: ipp.c,v 1.22 2000/02/24 21:40:29 mike Exp $"
c8f9565c 3 *
4 * IPP backend for the Common UNIX Printing System (CUPS).
5 *
71fe22b7 6 * Copyright 1997-2000 by Easy Software Products, all rights reserved.
c8f9565c 7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
8784b6a6 17 * 44141 Airport View Drive, Suite 204
c8f9565c 18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
6d0582d2 26 * main() - Send a file to the printer or server.
c8f9565c 27 */
28
29/*
30 * Include necessary headers.
31 */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <errno.h>
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <cups/cups.h>
39#include <cups/language.h>
40#include <cups/string.h>
41
42
43/*
44 * 'main()' - Send a file to the printer or server.
45 *
46 * Usage:
47 *
48 * printer-uri job-id user title copies options [file]
49 */
50
51int /* O - Exit status */
52main(int argc, /* I - Number of command-line arguments (6 or 7) */
53 char *argv[]) /* I - Command-line arguments */
54{
55 int i; /* Looping var */
56 int n, n2; /* Attribute values */
57 char *option, /* Name of option */
58 *val, /* Pointer to option value */
59 *s; /* Pointer into option value */
60 int num_options; /* Number of printer options */
61 cups_option_t *options; /* Printer options */
62 char method[255], /* Method in URI */
63 hostname[1024], /* Hostname */
64 username[255], /* Username info */
65 resource[1024], /* Resource info (printer name) */
66 filename[1024]; /* File to print */
67 int port; /* Port number (not used) */
68 char password[255], /* Password info */
69 uri[HTTP_MAX_URI];/* Updated URI without user/pass */
70 http_status_t status; /* Status of HTTP job */
97fcaf92 71 ipp_status_t ipp_status; /* Status of IPP request */
c8f9565c 72 FILE *fp; /* File to print */
73 http_t *http; /* HTTP connection */
74 ipp_t *request, /* IPP request */
75 *response; /* IPP response */
76 ipp_attribute_t *job_id; /* job-id attribute */
97fcaf92 77 ipp_attribute_t *copies_sup; /* copies-supported attribute */
c8f9565c 78 cups_lang_t *language; /* Default language */
79 struct stat fileinfo; /* File statistics */
80 size_t nbytes, /* Number of bytes written */
81 tbytes; /* Total bytes written */
82 char buffer[8192]; /* Output buffer */
97fcaf92 83 int copies; /* Number of copies remaining */
0a3944d6 84 const char *content_type; /* CONTENT_TYPE environment variable */
c8f9565c 85
86
68edc300 87 if (argc == 1)
88 {
d4c438d4 89 if ((s = strrchr(argv[0], '/')) != NULL)
90 s ++;
91 else
92 s = argv[0];
93
94 printf("network %s \"Unknown\" \"Internet Printing Protocol\"\n", s);
68edc300 95 return (0);
96 }
97 else if (argc < 6 || argc > 7)
c8f9565c 98 {
99 fprintf(stderr, "Usage: %s job-id user title copies options [file]\n",
100 argv[0]);
101 return (1);
102 }
103
104 /*
105 * If we have 7 arguments, print the file named on the command-line.
97fcaf92 106 * Otherwise, copy stdin to a temporary file and print the temporary
107 * file.
c8f9565c 108 */
109
110 if (argc == 6)
97fcaf92 111 {
112 /*
113 * Copy stdin to a temporary file...
114 */
115
116 FILE *fp; /* Temporary file */
117 char buffer[8192]; /* Buffer for copying */
118 int bytes; /* Number of bytes read */
119
120
121 if ((fp = fopen(cupsTempFile(filename, sizeof(filename)), "w")) == NULL)
122 {
123 perror("ERROR: unable to create temporary file");
124 return (1);
125 }
126
127 while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
128 if (fwrite(buffer, 1, bytes, fp) < bytes)
129 {
130 perror("ERROR: unable to write to temporary file");
131 fclose(fp);
132 unlink(filename);
133 return (1);
134 }
135
136 fclose(fp);
137 }
138 else
139 {
140 strncpy(filename, argv[6], sizeof(filename) - 1);
141 filename[sizeof(filename) - 1] = '\0';
142 }
143
144 /*
145 * Open the print file...
146 */
147
148 if ((fp = fopen(filename, "rb")) == NULL)
c8f9565c 149 {
150 perror("ERROR: Unable to open print file");
151 return (1);
152 }
153 else
97fcaf92 154 stat(filename, &fileinfo);
c8f9565c 155
156 /*
157 * Extract the hostname and printer name from the URI...
158 */
159
160 httpSeparate(argv[0], method, username, hostname, &port, resource);
161
162 /*
163 * Try connecting to the remote server...
164 */
165
97fcaf92 166 do
c8f9565c 167 {
97fcaf92 168 fprintf(stderr, "INFO: Connecting to %s...\n", hostname);
c8f9565c 169
97fcaf92 170 if ((http = httpConnect(hostname, port)) == NULL)
171 if (errno == ECONNREFUSED)
172 {
173 fprintf(stderr, "INFO: Network host \'%s\' is busy; will retry in 30 seconds...",
174 hostname);
175 sleep(30);
176 }
177 else
178 {
179 perror("ERROR: Unable to connect to IPP host");
2cc18dd2 180 sleep(30);
97fcaf92 181 }
c8f9565c 182 }
97fcaf92 183 while (http == NULL);
c8f9565c 184
185 /*
186 * Build a URI for the printer and fill the standard IPP attributes for
8ce7c000 187 * an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
188 * might contain username:password information...
c8f9565c 189 */
190
3f9cb6c6 191 snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource);
c8f9565c 192
193 /*
97fcaf92 194 * First validate the destination and see if the device supports multiple
195 * copies. We have to do this because some IPP servers (e.g. HP JetDirect)
196 * don't support the copies attribute...
c8f9565c 197 */
198
97fcaf92 199 language = cupsLangDefault();
200 copies_sup = NULL;
c8f9565c 201
97fcaf92 202 do
c8f9565c 203 {
204 /*
97fcaf92 205 * Build the IPP request...
c8f9565c 206 */
207
97fcaf92 208 request = ippNew();
209 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
210 request->request.op.request_id = 1;
c8f9565c 211
97fcaf92 212 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
213 "attributes-charset", NULL, cupsLangEncoding(language));
214
215 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
216 "attributes-natural-language", NULL,
217 language != NULL ? language->language : "C");
c8f9565c 218
97fcaf92 219 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
220 NULL, uri);
c8f9565c 221
97fcaf92 222 /*
223 * Now fill in the HTTP request stuff...
224 */
c8f9565c 225
97fcaf92 226 httpClearFields(http);
227 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
228 if (username[0])
c8f9565c 229 {
97fcaf92 230 httpEncode64(password, username);
231 httpSetField(http, HTTP_FIELD_AUTHORIZATION, password);
232 }
c8f9565c 233
97fcaf92 234 sprintf(buffer, "%u", ippLength(request));
235 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, buffer);
c8f9565c 236
97fcaf92 237 /*
238 * Do the request...
239 */
c8f9565c 240
97fcaf92 241 for (;;)
c8f9565c 242 {
97fcaf92 243 /*
244 * POST the request, retrying as needed...
245 */
246
247 if (httpPost(http, resource))
c8f9565c 248 {
97fcaf92 249 fputs("INFO: Unable to POST get-printer-attributes request; retrying...\n", stderr);
250 sleep(10);
251 httpReconnect(http);
252 continue;
c8f9565c 253 }
c8f9565c 254
97fcaf92 255 fputs("INFO: POST successful, sending IPP request...\n", stderr);
c8f9565c 256
c8f9565c 257 /*
97fcaf92 258 * Send the IPP request...
c8f9565c 259 */
97fcaf92 260
261 request->state = IPP_IDLE;
262
263 if (ippWrite(http, request) == IPP_ERROR)
264 {
265 fputs("ERROR: Unable to send IPP request!\n", stderr);
266 status = HTTP_ERROR;
267 break;
268 }
269
270 fputs("INFO: IPP request sent, getting status...\n", stderr);
271
c8f9565c 272 /*
97fcaf92 273 * Finally, check the status from the HTTP server...
c8f9565c 274 */
275
97fcaf92 276 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
277
278 if (status == HTTP_OK)
c8f9565c 279 {
97fcaf92 280 response = ippNew();
281 ippRead(http, response);
282
283 ipp_status = response->request.status.status_code;
284
285 if (ipp_status > IPP_OK_CONFLICT)
286 {
287 if (ipp_status == IPP_PRINTER_BUSY ||
288 ipp_status == IPP_SERVICE_UNAVAILABLE)
289 {
290 fputs("INFO: Printer busy; will retry in 10 seconds...\n", stderr);
291 sleep(10);
292 }
293 else
294 {
295 fprintf(stderr, "ERROR: Printer will not accept print file (%x)!\n",
296 ipp_status);
297 status = HTTP_ERROR;
298 }
299 }
300 else if ((copies_sup = ippFindAttribute(response, "copies-supported",
301 IPP_TAG_RANGE)) != NULL)
302 {
303 /*
304 * Has the "copies-supported" attribute - does it have an upper
305 * bound > 1?
306 */
307
308 if (copies_sup->values[0].range.upper <= 1)
309 copies_sup = NULL; /* No */
310 }
311
312 ippDelete(response);
c8f9565c 313 }
97fcaf92 314 else
c8f9565c 315 {
97fcaf92 316 if (status == HTTP_ERROR)
317 {
318 fprintf(stderr, "WARNING: Did not receive the IPP response (%d)\n",
319 errno);
320 status = HTTP_OK;
321 ipp_status = IPP_PRINTER_BUSY;
322 }
323 else
324 fprintf(stderr, "ERROR: Validate request was not accepted (%d)!\n", status);
c8f9565c 325 }
97fcaf92 326
327 httpFlush(http);
328
329 break;
330 }
331
332 if (status != HTTP_OK)
333 {
334 if (fp != stdin)
335 fclose(fp);
336
337 httpClose(http);
338
339 return (1);
c8f9565c 340 }
c8f9565c 341 }
97fcaf92 342 while (ipp_status > IPP_OK_CONFLICT);
c8f9565c 343
344 /*
97fcaf92 345 * See if the printer supports multiple copies...
c8f9565c 346 */
347
97fcaf92 348 if (copies_sup)
349 copies = 1;
0c5b0932 350 else
97fcaf92 351 copies = atoi(argv[4]);
0c5b0932 352
c8f9565c 353 /*
97fcaf92 354 * Then issue the print-job request...
c8f9565c 355 */
356
97fcaf92 357 while (copies > 0)
c8f9565c 358 {
359 /*
97fcaf92 360 * Build the IPP request...
c8f9565c 361 */
362
97fcaf92 363 request = ippNew();
364 request->request.op.operation_id = IPP_PRINT_JOB;
365 request->request.op.request_id = 1;
c8f9565c 366
97fcaf92 367 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
368 "attributes-charset", NULL, cupsLangEncoding(language));
8ce7c000 369
97fcaf92 370 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
371 "attributes-natural-language", NULL,
372 language != NULL ? language->language : "C");
c8f9565c 373
97fcaf92 374 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
375 NULL, uri);
c8f9565c 376
97fcaf92 377 fprintf(stderr, "DEBUG: printer-uri = \"%s\"\n", uri);
378
379 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
380 NULL, argv[2]);
381
382 fprintf(stderr, "DEBUG: requesting-user-name = \"%s\"\n", argv[2]);
383
384 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
385 argv[3]);
c8f9565c 386
97fcaf92 387 fprintf(stderr, "DEBUG: job-name = \"%s\"\n", argv[3]);
8ce7c000 388
c8f9565c 389 /*
97fcaf92 390 * Handle options on the command-line...
c8f9565c 391 */
392
97fcaf92 393 options = NULL;
394 num_options = cupsParseOptions(argv[5], 0, &options);
395
0a3944d6 396 if (cupsGetOption("raw", num_options, options) ||
397 ((content_type = getenv("CONTENT_TYPE")) != NULL &&
398 strcasecmp(content_type, "application/vnd.cups-raw") == 0))
97fcaf92 399 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
400 NULL, "application/vnd.cups-raw");
401 else
402 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
403 NULL, "application/octet-stream");
404
405 if (copies_sup)
406 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", atoi(argv[4]));
407
408 for (i = 0; i < num_options; i ++)
c8f9565c 409 {
97fcaf92 410 /*
411 * Skip the "raw" option - handled above...
412 */
413
414 if (strcasecmp(options[i].name, "raw") == 0)
415 continue;
416
417 /*
418 * See what the option value is; for compatibility with older interface
419 * scripts, we have to support single-argument options as well as
420 * option=value, option=low-high, and option=MxN.
421 */
422
423 option = options[i].name;
424 val = options[i].value;
c8f9565c 425
97fcaf92 426 if (*val == '\0')
427 val = NULL;
428
429 if (val != NULL)
c8f9565c 430 {
97fcaf92 431 if (strcasecmp(val, "true") == 0 ||
432 strcasecmp(val, "on") == 0 ||
433 strcasecmp(val, "yes") == 0)
434 {
435 /*
436 * Boolean value - true...
437 */
438
439 n = 1;
440 val = "";
441 }
442 else if (strcasecmp(val, "false") == 0 ||
443 strcasecmp(val, "off") == 0 ||
444 strcasecmp(val, "no") == 0)
445 {
446 /*
447 * Boolean value - false...
448 */
449
450 n = 0;
451 val = "";
452 }
453
454 n = strtol(val, &s, 0);
455 }
456 else
457 {
458 if (strncasecmp(option, "no", 2) == 0)
459 {
460 option += 2;
461 n = 0;
462 }
463 else
464 n = 1;
465
466 s = "";
c8f9565c 467 }
97fcaf92 468
469 if (*s != '\0' && *s != '-' && (*s != 'x' || s == val))
470 /*
471 * String value(s)...
472 */
473 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, option, NULL, val);
474 else if (val != NULL)
475 {
476 /*
477 * Numeric value, range, or resolution...
478 */
479
480 if (*s == '-')
481 {
482 n2 = strtol(s + 1, NULL, 0);
483 ippAddRange(request, IPP_TAG_JOB, option, n, n2);
484 }
485 else if (*s == 'x')
486 {
487 n2 = strtol(s + 1, &s, 0);
488
489 if (strcasecmp(s, "dpc") == 0)
490 ippAddResolution(request, IPP_TAG_JOB, option, IPP_RES_PER_CM, n, n2);
491 else if (strcasecmp(s, "dpi") == 0)
492 ippAddResolution(request, IPP_TAG_JOB, option, IPP_RES_PER_INCH, n, n2);
493 else
494 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, option, NULL, val);
495 }
496 else
497 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, option, n);
498 }
499 else
500 /*
501 * Boolean value...
502 */
503 ippAddBoolean(request, IPP_TAG_JOB, option, (char)n);
c8f9565c 504 }
505
cb555bcf 506 /*
97fcaf92 507 * Now fill in the HTTP request stuff...
cb555bcf 508 */
509
97fcaf92 510 httpClearFields(http);
511 httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
512 if (username[0])
513 {
514 httpEncode64(password, username);
515 httpSetField(http, HTTP_FIELD_AUTHORIZATION, password);
516 }
cb555bcf 517
97fcaf92 518 sprintf(buffer, "%u", ippLength(request) + (size_t)fileinfo.st_size);
519 httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, buffer);
8ce7c000 520
c8f9565c 521 /*
97fcaf92 522 * Do the request...
c8f9565c 523 */
524
97fcaf92 525 for (;;)
c8f9565c 526 {
97fcaf92 527 /*
528 * POST the request, retrying as needed...
529 */
530
531 httpReconnect(http);
c8f9565c 532
97fcaf92 533 if (httpPost(http, resource))
3f9cb6c6 534 {
97fcaf92 535 fputs("INFO: Unable to POST print request; retrying...\n", stderr);
536 sleep(10);
537 continue;
538 }
539
540 fputs("INFO: POST successful, sending IPP request...\n", stderr);
3f9cb6c6 541
97fcaf92 542 /*
543 * Send the IPP request...
544 */
545
546 request->state = IPP_IDLE;
3f9cb6c6 547
97fcaf92 548 if (ippWrite(http, request) == IPP_ERROR)
549 {
550 fputs("ERROR: Unable to send IPP request!\n", stderr);
551 status = HTTP_ERROR;
552 break;
3f9cb6c6 553 }
c8f9565c 554
97fcaf92 555 fputs("INFO: IPP request sent, sending print file...\n", stderr);
556
557 /*
558 * Then send the file...
559 */
560
561 rewind(fp);
562
563 tbytes = 0;
564 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
565 {
566 tbytes += nbytes;
567 fprintf(stderr, "INFO: Sending print file, %uk...\n", tbytes / 1024);
568
569 if (httpWrite(http, buffer, nbytes) < nbytes)
570 {
571 perror("ERROR: Unable to send print file to printer");
572 status = HTTP_ERROR;
573 break;
574 }
575 }
576
577 fputs("INFO: Print file sent; checking status...\n", stderr);
578
579 /*
580 * Finally, check the status from the HTTP server...
581 */
582
583 while ((status = httpUpdate(http)) == HTTP_CONTINUE);
584
585 if (status == HTTP_OK)
6a536282 586 {
97fcaf92 587 response = ippNew();
588 ippRead(http, response);
589
590 if ((ipp_status = response->request.status.status_code) > IPP_OK_CONFLICT)
591 {
592 if (ipp_status == IPP_SERVICE_UNAVAILABLE ||
593 ipp_status == IPP_PRINTER_BUSY)
594 {
595 fputs("INFO: Printer is busy; retrying print job...\n", stderr);
596 sleep(10);
597 }
598 else
599 fprintf(stderr, "ERROR: Print file was not accepted (%04x)!\n",
600 response->request.status.status_code);
601 }
602 else if ((job_id = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL)
603 fputs("INFO: Print file accepted - job ID unknown.\n", stderr);
604 else
605 fprintf(stderr, "INFO: Print file accepted - job ID %d.\n",
606 job_id->values[0].integer);
6a536282 607 }
608 else
97fcaf92 609 {
610 response = NULL;
611 ipp_status = IPP_PRINTER_BUSY;
612
613 if (status == HTTP_ERROR)
614 {
615 fprintf(stderr, "WARNING: Did not receive the IPP response (%d)\n",
616 errno);
617 status = HTTP_OK;
618 }
619 else
620 fprintf(stderr, "ERROR: Print request was not accepted (%d)!\n", status);
621 }
622
623 httpFlush(http);
624
625 break;
c8f9565c 626 }
627
97fcaf92 628 if (request != NULL)
629 ippDelete(request);
630 if (response != NULL)
631 ippDelete(response);
632
633 if (ipp_status <= IPP_OK_CONFLICT)
634 {
635 fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
636 copies --;
637 }
48419d23 638 else if (ipp_status != IPP_SERVICE_UNAVAILABLE &&
639 ipp_status != IPP_PRINTER_BUSY)
640 break;
c8f9565c 641 }
642
643 /*
644 * Free memory...
645 */
646
647 httpClose(http);
c8f9565c 648
649 /*
97fcaf92 650 * Close and remove the temporary file if necessary...
c8f9565c 651 */
652
97fcaf92 653 fclose(fp);
3f9cb6c6 654
97fcaf92 655 if (argc < 7)
656 unlink(filename);
c8f9565c 657
658 /*
659 * Return the queue status...
660 */
661
662 return (status != HTTP_OK);
663}
664
665
666/*
0a3944d6 667 * End of "$Id: ipp.c,v 1.22 2000/02/24 21:40:29 mike Exp $".
c8f9565c 668 */