4 * Line Printer Daemon interface for CUPS.
6 * Copyright 2007-2015 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 */
34 #endif /* __APPLE__ */
38 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
39 * with inetd or another similar program that monitors ports and starts
40 * daemons for each client connection. A typical configuration is:
42 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
44 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
47 * - This daemon does not check to make sure that the source port is
48 * between 721 and 731, since it isn't necessary for proper
49 * functioning and port-based security is no security at all!
51 * - The "Print any waiting jobs" command is a no-op.
53 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
54 * currently match the Solaris LPD mini-daemon.
61 static int create_job(http_t
*http
, const char *dest
, const char *title
,
62 const char *docname
, const char *user
,
63 int num_options
, cups_option_t
*options
);
64 static int get_printer(http_t
*http
, const char *name
, char *dest
,
65 size_t destsize
, cups_option_t
**options
,
66 int *accepting
, int *shared
, ipp_pstate_t
*state
);
67 static int print_file(http_t
*http
, int id
, const char *filename
,
68 const char *docname
, const char *user
,
69 const char *format
, int last
);
70 static int recv_print_job(const char *name
, int num_defaults
,
71 cups_option_t
*defaults
);
72 static int remove_jobs(const char *name
, const char *agent
,
74 static int send_state(const char *name
, const char *list
,
76 static char *smart_gets(char *s
, int len
, FILE *fp
);
80 * 'main()' - Process an incoming LPD request...
83 int /* O - Exit status */
84 main(int argc
, /* I - Number of command-line arguments */
85 char *argv
[]) /* I - Command-line arguments */
87 int i
; /* Looping var */
88 int num_defaults
; /* Number of default options */
89 cups_option_t
*defaults
; /* Default options */
90 char line
[256], /* Command string */
91 command
, /* Command code */
92 *dest
, /* Pointer to destination */
93 *list
, /* Pointer to list */
94 *agent
, /* Pointer to user */
95 status
; /* Status for client */
96 socklen_t hostlen
; /* Size of client address */
97 http_addr_t hostaddr
; /* Address of client */
98 char hostname
[256], /* Name of client */
99 hostip
[256], /* IP address */
100 *hostfamily
; /* Address family */
101 int hostlookups
; /* Do hostname lookups? */
103 vproc_transaction_t vtran
= vproc_transaction_begin(NULL
);
104 #endif /* __APPLE__ */
108 * Don't buffer the output...
111 setbuf(stdout
, NULL
);
114 * Log things using the "cups-lpd" name...
117 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
120 * Scan the command-line for options...
127 for (i
= 1; i
< argc
; i
++)
128 if (argv
[i
][0] == '-')
132 case 'h' : /* -h hostname[:port] */
134 cupsSetServer(argv
[i
] + 2);
139 cupsSetServer(argv
[i
]);
141 syslog(LOG_WARNING
, "Expected hostname string after -h option!");
145 case 'o' : /* Option */
147 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
153 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
156 syslog(LOG_WARNING
, "Expected option string after -o option!");
160 case 'n' : /* Don't do hostname lookups */
165 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
170 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
174 * Get the address of the client...
177 hostlen
= sizeof(hostaddr
);
179 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
181 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
182 strlcpy(hostname
, "unknown", sizeof(hostname
));
186 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
189 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
191 strlcpy(hostname
, hostip
, sizeof(hostname
));
194 if (hostaddr
.addr
.sa_family
== AF_INET6
)
197 #endif /* AF_INET6 */
200 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
204 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
205 num_defaults
, &defaults
);
208 * RFC1179 specifies that only 1 daemon command can be received for
212 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
215 * Unable to get command from client! Send an error status and return.
218 syslog(LOG_ERR
, "Unable to get command line from client!");
222 vproc_transaction_end(NULL
, vtran
);
223 #endif /* __APPLE__ */
229 * The first byte is the command byte. After that will be the queue name,
230 * resource list, and/or user name.
233 if ((command
= line
[0]) == '\0')
242 for (list
= dest
; *list
&& !isspace(*list
& 255); list
++);
244 while (isspace(*list
& 255))
254 default : /* Unknown command */
255 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
256 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
262 case 0x01 : /* Print any waiting jobs */
263 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
269 case 0x02 : /* Receive a printer job */
270 syslog(LOG_INFO
, "Receive print job for %s", dest
);
271 /* recv_print_job() sends initial status byte */
273 status
= (char)recv_print_job(dest
, num_defaults
, defaults
);
276 case 0x03 : /* Send queue state (short) */
277 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
278 /* no status byte for this command */
280 status
= (char)send_state(dest
, list
, 0);
283 case 0x04 : /* Send queue state (long) */
284 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
285 /* no status byte for this command */
287 status
= (char)send_state(dest
, list
, 1);
290 case 0x05 : /* Remove jobs */
294 * Grab the agent and skip to the list of users and/or jobs.
299 for (; *list
&& !isspace(*list
& 255); list
++);
300 while (isspace(*list
& 255))
303 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
305 status
= (char)remove_jobs(dest
, agent
, list
);
314 syslog(LOG_INFO
, "Closing connection");
318 vproc_transaction_end(NULL
, vtran
);
319 #endif /* __APPLE__ */
326 * 'create_job()' - Create a new print job.
329 static int /* O - Job ID or -1 on error */
330 create_job(http_t
*http
, /* I - HTTP connection */
331 const char *dest
, /* I - Destination name */
332 const char *title
, /* I - job-name */
333 const char *docname
, /* I - Name of job file */
334 const char *user
, /* I - requesting-user-name */
335 int num_options
, /* I - Number of options for job */
336 cups_option_t
*options
) /* I - Options for job */
338 ipp_t
*request
; /* IPP request */
339 ipp_t
*response
; /* IPP response */
340 ipp_attribute_t
*attr
; /* IPP attribute */
341 char uri
[HTTP_MAX_URI
]; /* Printer URI */
346 * Setup the Create-Job request...
349 request
= ippNewRequest(IPP_CREATE_JOB
);
351 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
352 "localhost", 0, "/printers/%s", dest
);
354 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
357 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
358 "requesting-user-name", NULL
, user
);
361 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
365 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
368 cupsEncodeOptions(request
, num_options
, options
);
374 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
376 response
= cupsDoRequest(http
, request
, uri
);
378 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
380 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
388 * Get the job-id value from the response and return it...
391 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
395 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
399 id
= attr
->values
[0].integer
;
401 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
411 * 'get_printer()' - Get the named printer and its options.
414 static int /* O - Number of options or -1 on error */
415 get_printer(http_t
*http
, /* I - HTTP connection */
416 const char *name
, /* I - Printer name from request */
417 char *dest
, /* I - Destination buffer */
418 size_t destsize
, /* I - Size of destination buffer */
419 cups_option_t
**options
, /* O - Printer options */
420 int *accepting
, /* O - printer-is-accepting-jobs value */
421 int *shared
, /* O - printer-is-shared value */
422 ipp_pstate_t
*state
) /* O - printer-state value */
424 int num_options
; /* Number of options */
425 cups_file_t
*fp
; /* lpoptions file */
426 char line
[1024], /* Line from lpoptions file */
427 *value
, /* Pointer to value on line */
428 *optptr
; /* Pointer to options on line */
429 int linenum
; /* Line number in file */
430 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
431 ipp_t
*request
; /* IPP request */
432 ipp_t
*response
; /* IPP response */
433 ipp_attribute_t
*attr
; /* IPP attribute */
434 char uri
[HTTP_MAX_URI
]; /* Printer URI */
435 static const char * const requested
[] =
436 { /* Requested attributes */
438 "printer-is-accepting-jobs",
446 * Initialize everything...
454 *state
= IPP_PRINTER_STOPPED
;
459 * See if the name is a queue name optionally with an instance name.
462 strlcpy(dest
, name
, destsize
);
463 if ((value
= strchr(dest
, '/')) != NULL
)
467 * Setup the Get-Printer-Attributes request...
470 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
472 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
473 "localhost", 0, "/printers/%s", dest
);
475 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
478 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
479 "requested-attributes",
480 (int)(sizeof(requested
) / sizeof(requested
[0])),
487 response
= cupsDoRequest(http
, request
, "/");
489 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
492 * If we can't find the printer by name, look up the printer-name
493 * using the printer-info values...
496 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
497 *info_attr
, /* printer-info */
498 *name_attr
, /* printer-name */
499 *shared_attr
, /* printer-is-shared */
500 *state_attr
; /* printer-state */
506 * Setup the CUPS-Get-Printers request...
509 request
= ippNewRequest(CUPS_GET_PRINTERS
);
511 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
512 "requested-attributes",
513 (int)(sizeof(requested
) / sizeof(requested
[0])),
520 response
= cupsDoRequest(http
, request
, "/");
522 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
524 syslog(LOG_ERR
, "Unable to get list of printers - %s",
525 cupsLastErrorString());
533 * Scan the response for printers...
537 attr
= response
->attrs
;
542 * Skip to the next printer...
545 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
552 * Get all of the attributes for the current printer...
555 accepting_attr
= NULL
;
561 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
563 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
564 attr
->value_tag
== IPP_TAG_BOOLEAN
)
565 accepting_attr
= attr
;
566 else if (!strcmp(attr
->name
, "printer-info") &&
567 attr
->value_tag
== IPP_TAG_TEXT
)
569 else if (!strcmp(attr
->name
, "printer-name") &&
570 attr
->value_tag
== IPP_TAG_NAME
)
572 else if (!strcmp(attr
->name
, "printer-is-shared") &&
573 attr
->value_tag
== IPP_TAG_BOOLEAN
)
575 else if (!strcmp(attr
->name
, "printer-state") &&
576 attr
->value_tag
== IPP_TAG_ENUM
)
582 if (info_attr
&& name_attr
&&
583 !_cups_strcasecmp(name
, info_attr
->values
[0].string
.text
))
586 * Found a match, use this one!
589 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
591 if (accepting
&& accepting_attr
)
592 *accepting
= accepting_attr
->values
[0].boolean
;
594 if (shared
&& shared_attr
)
595 *shared
= shared_attr
->values
[0].boolean
;
597 if (state
&& state_attr
)
598 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
608 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
618 * Get values from the response...
623 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
624 IPP_TAG_BOOLEAN
)) == NULL
)
625 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
626 "response from server!");
628 *accepting
= attr
->values
[0].boolean
;
633 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
634 IPP_TAG_BOOLEAN
)) == NULL
)
636 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
637 "response from server!");
641 *shared
= attr
->values
[0].boolean
;
646 if ((attr
= ippFindAttribute(response
, "printer-state",
647 IPP_TAG_ENUM
)) == NULL
)
648 syslog(LOG_ERR
, "No printer-state attribute found in "
649 "response from server!");
651 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
658 * Next look for the printer in the lpoptions file...
663 if (options
&& shared
&& accepting
)
665 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
666 cups_serverroot
= CUPS_SERVERROOT
;
668 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
669 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
672 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
675 * Make sure we have "Dest name options" or "Default name options"...
678 if ((_cups_strcasecmp(line
, "Dest") && _cups_strcasecmp(line
, "Default")) || !value
)
682 * Separate destination name from options...
685 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
687 while (*optptr
== ' ')
691 * If this is our destination, parse the options and break out of
692 * the loop - we're done!
695 if (!_cups_strcasecmp(value
, name
))
697 num_options
= cupsParseOptions(optptr
, num_options
, options
);
709 * Return the number of options for this destination...
712 return (num_options
);
717 * 'print_file()' - Add a file to the current job.
720 static int /* O - 0 on success, -1 on failure */
721 print_file(http_t
*http
, /* I - HTTP connection */
722 int id
, /* I - Job ID */
723 const char *filename
, /* I - File to print */
724 const char *docname
, /* I - document-name */
725 const char *user
, /* I - requesting-user-name */
726 const char *format
, /* I - document-format */
727 int last
) /* I - 1 = last file in job */
729 ipp_t
*request
; /* IPP request */
730 char uri
[HTTP_MAX_URI
]; /* Printer URI */
734 * Setup the Send-Document request...
737 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
739 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
742 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
743 "requesting-user-name", NULL
, user
);
746 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
747 "document-name", NULL
, docname
);
750 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
751 "document-format", NULL
, format
);
753 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last
);
759 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
761 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
763 if (cupsLastError() > IPP_OK_CONFLICT
)
765 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
775 * 'recv_print_job()' - Receive a print job from the client.
778 static int /* O - Command status */
780 const char *queue
, /* I - Printer name */
781 int num_defaults
, /* I - Number of default options */
782 cups_option_t
*defaults
) /* I - Default options */
784 http_t
*http
; /* HTTP connection */
785 int i
; /* Looping var */
786 int status
; /* Command status */
787 int fd
; /* Temporary file */
788 FILE *fp
; /* File pointer */
789 char filename
[1024]; /* Temporary filename */
790 ssize_t bytes
; /* Bytes received */
791 size_t total
; /* Total bytes */
792 char line
[256], /* Line from file/stdin */
793 command
, /* Command from line */
794 *count
, /* Number of bytes */
795 *name
; /* Name of file */
796 const char *job_sheets
; /* Job sheets */
797 int num_data
; /* Number of data files */
798 char control
[1024], /* Control filename */
799 data
[100][256], /* Data files */
800 temp
[100][1024]; /* Temporary files */
801 char user
[1024], /* User name */
802 title
[1024], /* Job title */
803 docname
[1024], /* Document name */
804 dest
[256]; /* Printer/class queue */
805 int accepting
, /* printer-is-accepting */
806 shared
, /* printer-is-shared */
807 num_options
; /* Number of options */
808 cups_option_t
*options
; /* Options */
810 int docnumber
, /* Current document number */
811 doccount
; /* Count of documents */
815 * Connect to the server...
818 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
821 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
829 * See if the printer is available...
832 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
833 &accepting
, &shared
, NULL
);
835 if (num_options
< 0 || !accepting
|| !shared
)
838 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
839 !accepting
? "accepting jobs" : "shared");
841 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
850 putchar(0); /* OK so far... */
853 * Read the request...
862 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
864 if (strlen(line
) < 2)
873 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
874 while (isspace(*name
& 255))
880 case 0x01 : /* Abort */
884 case 0x02 : /* Receive control file */
885 if (strlen(name
) < 2)
887 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
896 * Append to the existing control file - the LPD spec is
897 * not entirely clear, but at least the OS/2 LPD code sends
898 * multiple control files per connection...
901 if ((fd
= open(control
, O_WRONLY
)) < 0)
904 "Unable to append to temporary control file \"%s\" - %s",
905 control
, strerror(errno
));
911 lseek(fd
, 0, SEEK_END
);
915 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
917 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
918 control
, strerror(errno
));
924 strlcpy(filename
, control
, sizeof(filename
));
928 case 0x03 : /* Receive data file */
929 if (strlen(name
) < 2)
931 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
937 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
940 * Too many data files...
943 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
949 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
951 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
953 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
954 temp
[num_data
], strerror(errno
));
960 strlcpy(filename
, temp
[num_data
], sizeof(filename
));
972 * Copy the data or control file from the client...
975 for (total
= (size_t)strtoll(count
, NULL
, 10); total
> 0; total
-= (size_t)bytes
)
977 if (total
> sizeof(line
))
978 bytes
= (ssize_t
)sizeof(line
);
980 bytes
= (ssize_t
)total
;
982 if ((bytes
= (ssize_t
)fread(line
, 1, (size_t)bytes
, stdin
)) > 0)
983 bytes
= write(fd
, line
, (size_t)bytes
);
987 syslog(LOG_ERR
, "Error while reading file - %s",
995 * Read trailing nul...
1000 if (fread(line
, 1, 1, stdin
) < 1)
1003 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1009 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1015 * Close the file and send an acknowledgement...
1029 * Process the control file and print stuff...
1032 if ((fp
= fopen(control
, "rb")) == NULL
)
1037 * Copy the default options...
1040 for (i
= 0; i
< num_defaults
; i
++)
1041 num_options
= cupsAddOption(defaults
[i
].name
,
1043 num_options
, &options
);
1046 * Grab the job information...
1054 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1057 * Process control lines...
1062 case 'J' : /* Job name */
1063 strlcpy(title
, line
+ 1, sizeof(title
));
1066 case 'N' : /* Document name */
1067 strlcpy(docname
, line
+ 1, sizeof(docname
));
1070 case 'P' : /* User identification */
1071 strlcpy(user
, line
+ 1, sizeof(user
));
1074 case 'L' : /* Print banner page */
1076 * If a banner was requested and it's not overridden by a
1077 * command line option and the destination's default is none
1078 * then add the standard banner...
1081 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1082 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1083 options
)) == NULL
||
1084 !strcmp(job_sheets
, "none,none")))
1086 num_options
= cupsAddOption("job-sheets", "standard",
1087 num_options
, &options
);
1091 case 'c' : /* Plot CIF file */
1092 case 'd' : /* Print DVI file */
1093 case 'f' : /* Print formatted file */
1094 case 'g' : /* Plot file */
1095 case 'l' : /* Print file leaving control characters (raw) */
1096 case 'n' : /* Print ditroff output file */
1097 case 'o' : /* Print PostScript output file */
1098 case 'p' : /* Print file with 'pr' format (prettyprint) */
1099 case 'r' : /* File to print with FORTRAN carriage control */
1100 case 't' : /* Print troff output file */
1101 case 'v' : /* Print raster file */
1104 if (line
[0] == 'l' &&
1105 !cupsGetOption("document-format", num_options
, options
))
1106 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1109 num_options
= cupsAddOption("prettyprint", "", num_options
,
1119 * Check that we have a username...
1124 syslog(LOG_WARNING
, "No username specified by client! "
1125 "Using \"anonymous\"...");
1126 strlcpy(user
, "anonymous", sizeof(user
));
1133 if ((id
= create_job(http
, dest
, title
, docname
, user
, num_options
,
1139 * Then print the job files...
1147 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1150 * Process control lines...
1155 case 'N' : /* Document name */
1156 strlcpy(docname
, line
+ 1, sizeof(docname
));
1159 case 'c' : /* Plot CIF file */
1160 case 'd' : /* Print DVI file */
1161 case 'f' : /* Print formatted file */
1162 case 'g' : /* Plot file */
1163 case 'l' : /* Print file leaving control characters (raw) */
1164 case 'n' : /* Print ditroff output file */
1165 case 'o' : /* Print PostScript output file */
1166 case 'p' : /* Print file with 'pr' format (prettyprint) */
1167 case 'r' : /* File to print with FORTRAN carriage control */
1168 case 't' : /* Print troff output file */
1169 case 'v' : /* Print raster file */
1171 * Figure out which file we are printing...
1174 for (i
= 0; i
< num_data
; i
++)
1175 if (!strcmp(data
[i
], line
+ 1))
1185 * Send the print file...
1190 if (print_file(http
, id
, temp
[i
], docname
, user
,
1191 cupsGetOption("document-format", num_options
,
1193 docnumber
== doccount
))
1210 cupsFreeOptions(num_options
, options
);
1215 * Clean up all temporary files and return...
1220 for (i
= 0; i
< num_data
; i
++)
1228 * 'remove_jobs()' - Cancel one or more jobs.
1231 static int /* O - Command status */
1232 remove_jobs(const char *dest
, /* I - Destination */
1233 const char *agent
, /* I - User agent */
1234 const char *list
) /* I - List of jobs or users */
1236 int id
; /* Job ID */
1237 http_t
*http
; /* HTTP server connection */
1238 ipp_t
*request
; /* IPP Request */
1239 char uri
[HTTP_MAX_URI
]; /* Job URI */
1242 (void)dest
; /* Suppress compiler warnings... */
1245 * Try connecting to the local server...
1248 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1249 cupsEncryption())) == NULL
)
1251 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1257 * Loop for each job...
1260 while ((id
= atoi(list
)) > 0)
1263 * Skip job ID in list...
1266 while (isdigit(*list
& 255))
1268 while (isspace(*list
& 255))
1272 * Build an IPP_CANCEL_JOB request, which requires the following
1275 * attributes-charset
1276 * attributes-natural-language
1278 * requesting-user-name
1281 request
= ippNewRequest(IPP_CANCEL_JOB
);
1283 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1286 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1287 "requesting-user-name", NULL
, agent
);
1290 * Do the request and get back a response...
1293 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1295 if (cupsLastError() > IPP_OK_CONFLICT
)
1297 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1298 cupsLastErrorString());
1303 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1313 * 'send_state()' - Send the queue state.
1316 static int /* O - Command status */
1317 send_state(const char *queue
, /* I - Destination */
1318 const char *list
, /* I - Job or user */
1319 int longstatus
) /* I - List of jobs or users */
1321 int id
; /* Job ID from list */
1322 http_t
*http
; /* HTTP server connection */
1323 ipp_t
*request
, /* IPP Request */
1324 *response
; /* IPP Response */
1325 ipp_attribute_t
*attr
; /* Current attribute */
1326 ipp_pstate_t state
; /* Printer state */
1327 const char *jobdest
, /* Pointer into job-printer-uri */
1328 *jobuser
, /* Pointer to job-originating-user-name */
1329 *jobname
; /* Pointer to job-name */
1330 ipp_jstate_t jobstate
; /* job-state */
1331 int jobid
, /* job-id */
1332 jobsize
, /* job-k-octets */
1333 jobcount
, /* Number of jobs */
1334 jobcopies
, /* Number of copies */
1335 rank
; /* Rank of job */
1336 char rankstr
[255]; /* Rank string */
1337 char namestr
[1024]; /* Job name string */
1338 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1339 char dest
[256]; /* Printer/class queue */
1340 static const char * const ranks
[10] = /* Ranking strings */
1353 static const char * const requested
[] =
1354 { /* Requested attributes */
1359 "job-originating-user-name",
1366 * Try connecting to the local server...
1369 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1370 cupsEncryption())) == NULL
)
1372 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1374 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1379 * Get the actual destination name and printer state...
1382 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1384 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1385 cupsLastErrorString());
1386 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1391 * Show the queue state...
1396 case IPP_PRINTER_IDLE
:
1397 printf("%s is ready\n", dest
);
1399 case IPP_PRINTER_PROCESSING
:
1400 printf("%s is ready and printing\n", dest
);
1402 case IPP_PRINTER_STOPPED
:
1403 printf("%s is not ready\n", dest
);
1408 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1409 * the following attributes:
1411 * attributes-charset
1412 * attributes-natural-language
1413 * job-uri or printer-uri
1418 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1420 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1421 "localhost", 0, "/printers/%s", dest
);
1423 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1427 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1430 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1431 "requesting-user-name", NULL
, list
);
1432 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1435 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1436 "requested-attributes",
1437 sizeof(requested
) / sizeof(requested
[0]),
1441 * Do the request and get back a response...
1445 response
= cupsDoRequest(http
, request
, "/");
1447 if (cupsLastError() > IPP_OK_CONFLICT
)
1449 printf("get-jobs failed: %s\n", cupsLastErrorString());
1450 ippDelete(response
);
1455 * Loop through the job list and display them...
1458 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1461 * Skip leading attributes until we hit a job...
1464 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1471 * Pull the needed attributes from this job...
1476 jobstate
= IPP_JOB_PENDING
;
1477 jobname
= "untitled";
1482 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1484 if (!strcmp(attr
->name
, "job-id") &&
1485 attr
->value_tag
== IPP_TAG_INTEGER
)
1486 jobid
= attr
->values
[0].integer
;
1488 if (!strcmp(attr
->name
, "job-k-octets") &&
1489 attr
->value_tag
== IPP_TAG_INTEGER
)
1490 jobsize
= attr
->values
[0].integer
;
1492 if (!strcmp(attr
->name
, "job-state") &&
1493 attr
->value_tag
== IPP_TAG_ENUM
)
1494 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1496 if (!strcmp(attr
->name
, "job-printer-uri") &&
1497 attr
->value_tag
== IPP_TAG_URI
)
1498 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1501 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1502 attr
->value_tag
== IPP_TAG_NAME
)
1503 jobuser
= attr
->values
[0].string
.text
;
1505 if (!strcmp(attr
->name
, "job-name") &&
1506 attr
->value_tag
== IPP_TAG_NAME
)
1507 jobname
= attr
->values
[0].string
.text
;
1509 if (!strcmp(attr
->name
, "copies") &&
1510 attr
->value_tag
== IPP_TAG_INTEGER
)
1511 jobcopies
= attr
->values
[0].integer
;
1517 * See if we have everything needed...
1520 if (!jobdest
|| !jobid
)
1528 if (!longstatus
&& jobcount
== 0)
1529 puts("Rank Owner Job File(s) Total Size");
1534 * Display the job...
1537 if (jobstate
== IPP_JOB_PROCESSING
)
1538 strlcpy(rankstr
, "active", sizeof(rankstr
));
1541 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1550 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1553 strlcpy(namestr
, jobname
, sizeof(namestr
));
1555 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1556 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1559 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1560 jobid
, jobname
, 1024.0 * jobsize
);
1566 ippDelete(response
);
1578 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1581 static char * /* O - Line read or NULL */
1582 smart_gets(char *s
, /* I - Pointer to line buffer */
1583 int len
, /* I - Size of line buffer */
1584 FILE *fp
) /* I - File to read from */
1586 char *ptr
, /* Pointer into line */
1587 *end
; /* End of line */
1588 int ch
; /* Character from file */
1592 * Read the line; unlike fgets(), we read the entire line but dump
1593 * characters that go past the end of the buffer. Also, we accept
1594 * CR, LF, or CR LF for the line endings to be "safe", although
1595 * RFC 1179 specifically says "just use LF".
1601 while ((ch
= getc(fp
)) != EOF
)
1605 else if (ch
== '\r')
1608 * See if a LF follows...
1624 if (ch
== EOF
&& ptr
== s
)