4 * Line Printer Daemon interface for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * Include necessary headers...
20 #include <cups/cups-private.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
29 #ifdef HAVE_INTTYPES_H
30 # include <inttypes.h>
31 #endif /* HAVE_INTTYPES_H */
35 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
36 * with inetd or another similar program that monitors ports and starts
37 * daemons for each client connection. A typical configuration is:
39 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
41 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
44 * - This daemon does not check to make sure that the source port is
45 * between 721 and 731, since it isn't necessary for proper
46 * functioning and port-based security is no security at all!
48 * - The "Print any waiting jobs" command is a no-op.
50 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
51 * currently match the Solaris LPD mini-daemon.
58 static int create_job(http_t
*http
, const char *dest
, const char *title
,
59 const char *docname
, const char *user
,
60 int num_options
, cups_option_t
*options
);
61 static int get_printer(http_t
*http
, const char *name
, char *dest
,
62 size_t destsize
, cups_option_t
**options
,
63 int *accepting
, int *shared
, ipp_pstate_t
*state
);
64 static int print_file(http_t
*http
, int id
, const char *filename
,
65 const char *docname
, const char *user
,
66 const char *format
, int last
);
67 static int recv_print_job(const char *name
, int num_defaults
,
68 cups_option_t
*defaults
);
69 static int remove_jobs(const char *name
, const char *agent
,
71 static int send_state(const char *name
, const char *list
,
73 static char *smart_gets(char *s
, int len
, FILE *fp
);
77 * 'main()' - Process an incoming LPD request...
80 int /* O - Exit status */
81 main(int argc
, /* I - Number of command-line arguments */
82 char *argv
[]) /* I - Command-line arguments */
84 int i
; /* Looping var */
85 int num_defaults
; /* Number of default options */
86 cups_option_t
*defaults
; /* Default options */
87 char line
[256], /* Command string */
88 command
, /* Command code */
89 *dest
, /* Pointer to destination */
90 *list
, /* Pointer to list */
91 *agent
, /* Pointer to user */
92 status
; /* Status for client */
93 socklen_t hostlen
; /* Size of client address */
94 http_addr_t hostaddr
; /* Address of client */
95 char hostname
[256], /* Name of client */
96 hostip
[256], /* IP address */
97 *hostfamily
; /* Address family */
98 int hostlookups
; /* Do hostname lookups? */
102 * Don't buffer the output...
105 setbuf(stdout
, NULL
);
108 * Log things using the "cups-lpd" name...
111 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
114 * Scan the command-line for options...
121 for (i
= 1; i
< argc
; i
++)
122 if (argv
[i
][0] == '-')
126 case 'h' : /* -h hostname[:port] */
128 cupsSetServer(argv
[i
] + 2);
133 cupsSetServer(argv
[i
]);
135 syslog(LOG_WARNING
, "Expected hostname string after -h option!");
139 case 'o' : /* Option */
141 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
147 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
150 syslog(LOG_WARNING
, "Expected option string after -o option!");
154 case 'n' : /* Don't do hostname lookups */
159 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
164 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
168 * Get the address of the client...
171 hostlen
= sizeof(hostaddr
);
173 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
175 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
176 strlcpy(hostname
, "unknown", sizeof(hostname
));
180 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
183 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
185 strlcpy(hostname
, hostip
, sizeof(hostname
));
188 if (hostaddr
.addr
.sa_family
== AF_INET6
)
191 #endif /* AF_INET6 */
194 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
198 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
199 num_defaults
, &defaults
);
202 * RFC1179 specifies that only 1 daemon command can be received for
206 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
209 * Unable to get command from client! Send an error status and return.
212 syslog(LOG_ERR
, "Unable to get command line from client!");
218 * The first byte is the command byte. After that will be the queue name,
219 * resource list, and/or user name.
229 for (list
= dest
; *list
&& !isspace(*list
& 255); list
++);
231 while (isspace(*list
& 255))
241 default : /* Unknown command */
242 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
243 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
249 case 0x01 : /* Print any waiting jobs */
250 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
256 case 0x02 : /* Receive a printer job */
257 syslog(LOG_INFO
, "Receive print job for %s", dest
);
258 /* recv_print_job() sends initial status byte */
260 status
= (char)recv_print_job(dest
, num_defaults
, defaults
);
263 case 0x03 : /* Send queue state (short) */
264 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
265 /* no status byte for this command */
267 status
= (char)send_state(dest
, list
, 0);
270 case 0x04 : /* Send queue state (long) */
271 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
272 /* no status byte for this command */
274 status
= (char)send_state(dest
, list
, 1);
277 case 0x05 : /* Remove jobs */
281 * Grab the agent and skip to the list of users and/or jobs.
286 for (; *list
&& !isspace(*list
& 255); list
++);
287 while (isspace(*list
& 255))
290 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
292 status
= (char)remove_jobs(dest
, agent
, list
);
301 syslog(LOG_INFO
, "Closing connection");
309 * 'create_job()' - Create a new print job.
312 static int /* O - Job ID or -1 on error */
313 create_job(http_t
*http
, /* I - HTTP connection */
314 const char *dest
, /* I - Destination name */
315 const char *title
, /* I - job-name */
316 const char *docname
, /* I - Name of job file */
317 const char *user
, /* I - requesting-user-name */
318 int num_options
, /* I - Number of options for job */
319 cups_option_t
*options
) /* I - Options for job */
321 ipp_t
*request
; /* IPP request */
322 ipp_t
*response
; /* IPP response */
323 ipp_attribute_t
*attr
; /* IPP attribute */
324 char uri
[HTTP_MAX_URI
]; /* Printer URI */
329 * Setup the Create-Job request...
332 request
= ippNewRequest(IPP_CREATE_JOB
);
334 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
335 "localhost", 0, "/printers/%s", dest
);
337 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
341 "requesting-user-name", NULL
, user
);
344 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
348 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
351 cupsEncodeOptions(request
, num_options
, options
);
357 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
359 response
= cupsDoRequest(http
, request
, uri
);
361 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
363 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
371 * Get the job-id value from the response and return it...
374 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
378 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
382 id
= attr
->values
[0].integer
;
384 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
394 * 'get_printer()' - Get the named printer and its options.
397 static int /* O - Number of options or -1 on error */
398 get_printer(http_t
*http
, /* I - HTTP connection */
399 const char *name
, /* I - Printer name from request */
400 char *dest
, /* I - Destination buffer */
401 size_t destsize
, /* I - Size of destination buffer */
402 cups_option_t
**options
, /* O - Printer options */
403 int *accepting
, /* O - printer-is-accepting-jobs value */
404 int *shared
, /* O - printer-is-shared value */
405 ipp_pstate_t
*state
) /* O - printer-state value */
407 int num_options
; /* Number of options */
408 cups_file_t
*fp
; /* lpoptions file */
409 char line
[1024], /* Line from lpoptions file */
410 *value
, /* Pointer to value on line */
411 *optptr
; /* Pointer to options on line */
412 int linenum
; /* Line number in file */
413 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
414 ipp_t
*request
; /* IPP request */
415 ipp_t
*response
; /* IPP response */
416 ipp_attribute_t
*attr
; /* IPP attribute */
417 char uri
[HTTP_MAX_URI
]; /* Printer URI */
418 static const char * const requested
[] =
419 { /* Requested attributes */
421 "printer-is-accepting-jobs",
429 * Initialize everything...
437 *state
= IPP_PRINTER_STOPPED
;
442 * See if the name is a queue name optionally with an instance name.
445 strlcpy(dest
, name
, destsize
);
446 if ((value
= strchr(dest
, '/')) != NULL
)
450 * Setup the Get-Printer-Attributes request...
453 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
455 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
456 "localhost", 0, "/printers/%s", dest
);
458 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
461 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
462 "requested-attributes",
463 (int)(sizeof(requested
) / sizeof(requested
[0])),
470 response
= cupsDoRequest(http
, request
, "/");
472 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
475 * If we can't find the printer by name, look up the printer-name
476 * using the printer-info values...
479 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
480 *info_attr
, /* printer-info */
481 *name_attr
, /* printer-name */
482 *shared_attr
, /* printer-is-shared */
483 *state_attr
; /* printer-state */
489 * Setup the CUPS-Get-Printers request...
492 request
= ippNewRequest(CUPS_GET_PRINTERS
);
494 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
495 "requested-attributes",
496 (int)(sizeof(requested
) / sizeof(requested
[0])),
503 response
= cupsDoRequest(http
, request
, "/");
505 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
507 syslog(LOG_ERR
, "Unable to get list of printers - %s",
508 cupsLastErrorString());
516 * Scan the response for printers...
520 attr
= response
->attrs
;
525 * Skip to the next printer...
528 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
535 * Get all of the attributes for the current printer...
538 accepting_attr
= NULL
;
544 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
546 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
547 attr
->value_tag
== IPP_TAG_BOOLEAN
)
548 accepting_attr
= attr
;
549 else if (!strcmp(attr
->name
, "printer-info") &&
550 attr
->value_tag
== IPP_TAG_TEXT
)
552 else if (!strcmp(attr
->name
, "printer-name") &&
553 attr
->value_tag
== IPP_TAG_NAME
)
555 else if (!strcmp(attr
->name
, "printer-is-shared") &&
556 attr
->value_tag
== IPP_TAG_BOOLEAN
)
558 else if (!strcmp(attr
->name
, "printer-state") &&
559 attr
->value_tag
== IPP_TAG_ENUM
)
565 if (info_attr
&& name_attr
&&
566 !_cups_strcasecmp(name
, info_attr
->values
[0].string
.text
))
569 * Found a match, use this one!
572 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
574 if (accepting
&& accepting_attr
)
575 *accepting
= accepting_attr
->values
[0].boolean
;
577 if (shared
&& shared_attr
)
578 *shared
= shared_attr
->values
[0].boolean
;
580 if (state
&& state_attr
)
581 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
591 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
601 * Get values from the response...
606 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
607 IPP_TAG_BOOLEAN
)) == NULL
)
608 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
609 "response from server!");
611 *accepting
= attr
->values
[0].boolean
;
616 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
617 IPP_TAG_BOOLEAN
)) == NULL
)
619 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
620 "response from server!");
624 *shared
= attr
->values
[0].boolean
;
629 if ((attr
= ippFindAttribute(response
, "printer-state",
630 IPP_TAG_ENUM
)) == NULL
)
631 syslog(LOG_ERR
, "No printer-state attribute found in "
632 "response from server!");
634 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
641 * Next look for the printer in the lpoptions file...
646 if (options
&& shared
&& accepting
)
648 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
649 cups_serverroot
= CUPS_SERVERROOT
;
651 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
652 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
655 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
658 * Make sure we have "Dest name options" or "Default name options"...
661 if ((_cups_strcasecmp(line
, "Dest") && _cups_strcasecmp(line
, "Default")) || !value
)
665 * Separate destination name from options...
668 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
670 while (*optptr
== ' ')
674 * If this is our destination, parse the options and break out of
675 * the loop - we're done!
678 if (!_cups_strcasecmp(value
, name
))
680 num_options
= cupsParseOptions(optptr
, num_options
, options
);
692 * Return the number of options for this destination...
695 return (num_options
);
700 * 'print_file()' - Add a file to the current job.
703 static int /* O - 0 on success, -1 on failure */
704 print_file(http_t
*http
, /* I - HTTP connection */
705 int id
, /* I - Job ID */
706 const char *filename
, /* I - File to print */
707 const char *docname
, /* I - document-name */
708 const char *user
, /* I - requesting-user-name */
709 const char *format
, /* I - document-format */
710 int last
) /* I - 1 = last file in job */
712 ipp_t
*request
; /* IPP request */
713 char uri
[HTTP_MAX_URI
]; /* Printer URI */
717 * Setup the Send-Document request...
720 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
722 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
723 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
725 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
726 "requesting-user-name", NULL
, user
);
729 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
730 "document-name", NULL
, docname
);
733 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
734 "document-format", NULL
, format
);
736 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last
);
742 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
744 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
746 if (cupsLastError() > IPP_OK_CONFLICT
)
748 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
758 * 'recv_print_job()' - Receive a print job from the client.
761 static int /* O - Command status */
763 const char *queue
, /* I - Printer name */
764 int num_defaults
, /* I - Number of default options */
765 cups_option_t
*defaults
) /* I - Default options */
767 http_t
*http
; /* HTTP connection */
768 int i
; /* Looping var */
769 int status
; /* Command status */
770 int fd
; /* Temporary file */
771 FILE *fp
; /* File pointer */
772 char filename
[1024]; /* Temporary filename */
773 ssize_t bytes
; /* Bytes received */
774 size_t total
; /* Total bytes */
775 char line
[256], /* Line from file/stdin */
776 command
, /* Command from line */
777 *count
, /* Number of bytes */
778 *name
; /* Name of file */
779 const char *job_sheets
; /* Job sheets */
780 int num_data
; /* Number of data files */
781 char control
[1024], /* Control filename */
782 data
[100][256], /* Data files */
783 temp
[100][1024]; /* Temporary files */
784 char user
[1024], /* User name */
785 title
[1024], /* Job title */
786 docname
[1024], /* Document name */
787 dest
[256]; /* Printer/class queue */
788 int accepting
, /* printer-is-accepting */
789 shared
, /* printer-is-shared */
790 num_options
; /* Number of options */
791 cups_option_t
*options
; /* Options */
793 int docnumber
, /* Current document number */
794 doccount
; /* Count of documents */
798 * Connect to the server...
801 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
804 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
812 * See if the printer is available...
815 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
816 &accepting
, &shared
, NULL
);
818 if (num_options
< 0 || !accepting
|| !shared
)
821 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
822 !accepting
? "accepting jobs" : "shared");
824 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
833 putchar(0); /* OK so far... */
836 * Read the request...
845 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
847 if (strlen(line
) < 2)
856 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
857 while (isspace(*name
& 255))
863 case 0x01 : /* Abort */
867 case 0x02 : /* Receive control file */
868 if (strlen(name
) < 2)
870 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
879 * Append to the existing control file - the LPD spec is
880 * not entirely clear, but at least the OS/2 LPD code sends
881 * multiple control files per connection...
884 if ((fd
= open(control
, O_WRONLY
)) < 0)
887 "Unable to append to temporary control file \"%s\" - %s",
888 control
, strerror(errno
));
894 lseek(fd
, 0, SEEK_END
);
898 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
900 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
901 control
, strerror(errno
));
907 strlcpy(filename
, control
, sizeof(filename
));
911 case 0x03 : /* Receive data file */
912 if (strlen(name
) < 2)
914 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
920 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
923 * Too many data files...
926 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
932 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
934 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
936 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
937 temp
[num_data
], strerror(errno
));
943 strlcpy(filename
, temp
[num_data
], sizeof(filename
));
955 * Copy the data or control file from the client...
958 for (total
= (size_t)strtoll(count
, NULL
, 10); total
> 0; total
-= (size_t)bytes
)
960 if (total
> sizeof(line
))
961 bytes
= (ssize_t
)sizeof(line
);
963 bytes
= (ssize_t
)total
;
965 if ((bytes
= (ssize_t
)fread(line
, 1, (size_t)bytes
, stdin
)) > 0)
966 bytes
= write(fd
, line
, (size_t)bytes
);
970 syslog(LOG_ERR
, "Error while reading file - %s",
978 * Read trailing nul...
983 if (fread(line
, 1, 1, stdin
) < 1)
986 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
992 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
998 * Close the file and send an acknowledgement...
1012 * Process the control file and print stuff...
1015 if ((fp
= fopen(control
, "rb")) == NULL
)
1020 * Copy the default options...
1023 for (i
= 0; i
< num_defaults
; i
++)
1024 num_options
= cupsAddOption(defaults
[i
].name
,
1026 num_options
, &options
);
1029 * Grab the job information...
1037 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1040 * Process control lines...
1045 case 'J' : /* Job name */
1046 strlcpy(title
, line
+ 1, sizeof(title
));
1049 case 'N' : /* Document name */
1050 strlcpy(docname
, line
+ 1, sizeof(docname
));
1053 case 'P' : /* User identification */
1054 strlcpy(user
, line
+ 1, sizeof(user
));
1057 case 'L' : /* Print banner page */
1059 * If a banner was requested and it's not overridden by a
1060 * command line option and the destination's default is none
1061 * then add the standard banner...
1064 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1065 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1066 options
)) == NULL
||
1067 !strcmp(job_sheets
, "none,none")))
1069 num_options
= cupsAddOption("job-sheets", "standard",
1070 num_options
, &options
);
1074 case 'c' : /* Plot CIF file */
1075 case 'd' : /* Print DVI file */
1076 case 'f' : /* Print formatted file */
1077 case 'g' : /* Plot file */
1078 case 'l' : /* Print file leaving control characters (raw) */
1079 case 'n' : /* Print ditroff output file */
1080 case 'o' : /* Print PostScript output file */
1081 case 'p' : /* Print file with 'pr' format (prettyprint) */
1082 case 'r' : /* File to print with FORTRAN carriage control */
1083 case 't' : /* Print troff output file */
1084 case 'v' : /* Print raster file */
1087 if (line
[0] == 'l' &&
1088 !cupsGetOption("document-format", num_options
, options
))
1089 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1092 num_options
= cupsAddOption("prettyprint", "", num_options
,
1102 * Check that we have a username...
1107 syslog(LOG_WARNING
, "No username specified by client! "
1108 "Using \"anonymous\"...");
1109 strlcpy(user
, "anonymous", sizeof(user
));
1116 if ((id
= create_job(http
, dest
, title
, docname
, user
, num_options
,
1122 * Then print the job files...
1130 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1133 * Process control lines...
1138 case 'N' : /* Document name */
1139 strlcpy(docname
, line
+ 1, sizeof(docname
));
1142 case 'c' : /* Plot CIF file */
1143 case 'd' : /* Print DVI file */
1144 case 'f' : /* Print formatted file */
1145 case 'g' : /* Plot file */
1146 case 'l' : /* Print file leaving control characters (raw) */
1147 case 'n' : /* Print ditroff output file */
1148 case 'o' : /* Print PostScript output file */
1149 case 'p' : /* Print file with 'pr' format (prettyprint) */
1150 case 'r' : /* File to print with FORTRAN carriage control */
1151 case 't' : /* Print troff output file */
1152 case 'v' : /* Print raster file */
1154 * Figure out which file we are printing...
1157 for (i
= 0; i
< num_data
; i
++)
1158 if (!strcmp(data
[i
], line
+ 1))
1168 * Send the print file...
1173 if (print_file(http
, id
, temp
[i
], docname
, user
,
1174 cupsGetOption("document-format", num_options
,
1176 docnumber
== doccount
))
1193 cupsFreeOptions(num_options
, options
);
1198 * Clean up all temporary files and return...
1203 for (i
= 0; i
< num_data
; i
++)
1211 * 'remove_jobs()' - Cancel one or more jobs.
1214 static int /* O - Command status */
1215 remove_jobs(const char *dest
, /* I - Destination */
1216 const char *agent
, /* I - User agent */
1217 const char *list
) /* I - List of jobs or users */
1219 int id
; /* Job ID */
1220 http_t
*http
; /* HTTP server connection */
1221 ipp_t
*request
; /* IPP Request */
1222 char uri
[HTTP_MAX_URI
]; /* Job URI */
1225 (void)dest
; /* Suppress compiler warnings... */
1228 * Try connecting to the local server...
1231 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1232 cupsEncryption())) == NULL
)
1234 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1240 * Loop for each job...
1243 while ((id
= atoi(list
)) > 0)
1246 * Skip job ID in list...
1249 while (isdigit(*list
& 255))
1251 while (isspace(*list
& 255))
1255 * Build an IPP_CANCEL_JOB request, which requires the following
1258 * attributes-charset
1259 * attributes-natural-language
1261 * requesting-user-name
1264 request
= ippNewRequest(IPP_CANCEL_JOB
);
1266 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1267 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1269 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1270 "requesting-user-name", NULL
, agent
);
1273 * Do the request and get back a response...
1276 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1278 if (cupsLastError() > IPP_OK_CONFLICT
)
1280 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1281 cupsLastErrorString());
1286 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1296 * 'send_state()' - Send the queue state.
1299 static int /* O - Command status */
1300 send_state(const char *queue
, /* I - Destination */
1301 const char *list
, /* I - Job or user */
1302 int longstatus
) /* I - List of jobs or users */
1304 int id
; /* Job ID from list */
1305 http_t
*http
; /* HTTP server connection */
1306 ipp_t
*request
, /* IPP Request */
1307 *response
; /* IPP Response */
1308 ipp_attribute_t
*attr
; /* Current attribute */
1309 ipp_pstate_t state
; /* Printer state */
1310 const char *jobdest
, /* Pointer into job-printer-uri */
1311 *jobuser
, /* Pointer to job-originating-user-name */
1312 *jobname
; /* Pointer to job-name */
1313 ipp_jstate_t jobstate
; /* job-state */
1314 int jobid
, /* job-id */
1315 jobsize
, /* job-k-octets */
1316 jobcount
, /* Number of jobs */
1317 jobcopies
, /* Number of copies */
1318 rank
; /* Rank of job */
1319 char rankstr
[255]; /* Rank string */
1320 char namestr
[1024]; /* Job name string */
1321 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1322 char dest
[256]; /* Printer/class queue */
1323 static const char * const ranks
[10] = /* Ranking strings */
1336 static const char * const requested
[] =
1337 { /* Requested attributes */
1342 "job-originating-user-name",
1349 * Try connecting to the local server...
1352 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1353 cupsEncryption())) == NULL
)
1355 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1357 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1362 * Get the actual destination name and printer state...
1365 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1367 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1368 cupsLastErrorString());
1369 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1374 * Show the queue state...
1379 case IPP_PRINTER_IDLE
:
1380 printf("%s is ready\n", dest
);
1382 case IPP_PRINTER_PROCESSING
:
1383 printf("%s is ready and printing\n", dest
);
1385 case IPP_PRINTER_STOPPED
:
1386 printf("%s is not ready\n", dest
);
1391 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1392 * the following attributes:
1394 * attributes-charset
1395 * attributes-natural-language
1396 * job-uri or printer-uri
1401 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1403 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1404 "localhost", 0, "/printers/%s", dest
);
1406 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1410 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1413 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1414 "requesting-user-name", NULL
, list
);
1415 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1418 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1419 "requested-attributes",
1420 sizeof(requested
) / sizeof(requested
[0]),
1424 * Do the request and get back a response...
1428 response
= cupsDoRequest(http
, request
, "/");
1430 if (cupsLastError() > IPP_OK_CONFLICT
)
1432 printf("get-jobs failed: %s\n", cupsLastErrorString());
1433 ippDelete(response
);
1438 * Loop through the job list and display them...
1441 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1444 * Skip leading attributes until we hit a job...
1447 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1454 * Pull the needed attributes from this job...
1459 jobstate
= IPP_JOB_PENDING
;
1460 jobname
= "untitled";
1465 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1467 if (!strcmp(attr
->name
, "job-id") &&
1468 attr
->value_tag
== IPP_TAG_INTEGER
)
1469 jobid
= attr
->values
[0].integer
;
1471 if (!strcmp(attr
->name
, "job-k-octets") &&
1472 attr
->value_tag
== IPP_TAG_INTEGER
)
1473 jobsize
= attr
->values
[0].integer
;
1475 if (!strcmp(attr
->name
, "job-state") &&
1476 attr
->value_tag
== IPP_TAG_ENUM
)
1477 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1479 if (!strcmp(attr
->name
, "job-printer-uri") &&
1480 attr
->value_tag
== IPP_TAG_URI
)
1481 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1484 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1485 attr
->value_tag
== IPP_TAG_NAME
)
1486 jobuser
= attr
->values
[0].string
.text
;
1488 if (!strcmp(attr
->name
, "job-name") &&
1489 attr
->value_tag
== IPP_TAG_NAME
)
1490 jobname
= attr
->values
[0].string
.text
;
1492 if (!strcmp(attr
->name
, "copies") &&
1493 attr
->value_tag
== IPP_TAG_INTEGER
)
1494 jobcopies
= attr
->values
[0].integer
;
1500 * See if we have everything needed...
1503 if (!jobdest
|| !jobid
)
1511 if (!longstatus
&& jobcount
== 0)
1512 puts("Rank Owner Job File(s) Total Size");
1517 * Display the job...
1520 if (jobstate
== IPP_JOB_PROCESSING
)
1521 strlcpy(rankstr
, "active", sizeof(rankstr
));
1524 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1533 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1536 strlcpy(namestr
, jobname
, sizeof(namestr
));
1538 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1539 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1542 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1543 jobid
, jobname
, 1024.0 * jobsize
);
1549 ippDelete(response
);
1561 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1564 static char * /* O - Line read or NULL */
1565 smart_gets(char *s
, /* I - Pointer to line buffer */
1566 int len
, /* I - Size of line buffer */
1567 FILE *fp
) /* I - File to read from */
1569 char *ptr
, /* Pointer into line */
1570 *end
; /* End of line */
1571 int ch
; /* Character from file */
1575 * Read the line; unlike fgets(), we read the entire line but dump
1576 * characters that go past the end of the buffer. Also, we accept
1577 * CR, LF, or CR LF for the line endings to be "safe", although
1578 * RFC 1179 specifically says "just use LF".
1584 while ((ch
= getc(fp
)) != EOF
)
1588 else if (ch
== '\r')
1591 * See if a LF follows...
1607 if (ch
== EOF
&& ptr
== s
)