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.
240 for (list
= dest
; *list
&& !isspace(*list
& 255); list
++);
242 while (isspace(*list
& 255))
252 default : /* Unknown command */
253 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
254 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
260 case 0x01 : /* Print any waiting jobs */
261 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
267 case 0x02 : /* Receive a printer job */
268 syslog(LOG_INFO
, "Receive print job for %s", dest
);
269 /* recv_print_job() sends initial status byte */
271 status
= (char)recv_print_job(dest
, num_defaults
, defaults
);
274 case 0x03 : /* Send queue state (short) */
275 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
276 /* no status byte for this command */
278 status
= (char)send_state(dest
, list
, 0);
281 case 0x04 : /* Send queue state (long) */
282 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
283 /* no status byte for this command */
285 status
= (char)send_state(dest
, list
, 1);
288 case 0x05 : /* Remove jobs */
292 * Grab the agent and skip to the list of users and/or jobs.
297 for (; *list
&& !isspace(*list
& 255); list
++);
298 while (isspace(*list
& 255))
301 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
303 status
= (char)remove_jobs(dest
, agent
, list
);
312 syslog(LOG_INFO
, "Closing connection");
316 vproc_transaction_end(NULL
, vtran
);
317 #endif /* __APPLE__ */
324 * 'create_job()' - Create a new print job.
327 static int /* O - Job ID or -1 on error */
328 create_job(http_t
*http
, /* I - HTTP connection */
329 const char *dest
, /* I - Destination name */
330 const char *title
, /* I - job-name */
331 const char *docname
, /* I - Name of job file */
332 const char *user
, /* I - requesting-user-name */
333 int num_options
, /* I - Number of options for job */
334 cups_option_t
*options
) /* I - Options for job */
336 ipp_t
*request
; /* IPP request */
337 ipp_t
*response
; /* IPP response */
338 ipp_attribute_t
*attr
; /* IPP attribute */
339 char uri
[HTTP_MAX_URI
]; /* Printer URI */
344 * Setup the Create-Job request...
347 request
= ippNewRequest(IPP_CREATE_JOB
);
349 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
350 "localhost", 0, "/printers/%s", dest
);
352 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
356 "requesting-user-name", NULL
, user
);
359 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
363 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
366 cupsEncodeOptions(request
, num_options
, options
);
372 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
374 response
= cupsDoRequest(http
, request
, uri
);
376 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
378 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
386 * Get the job-id value from the response and return it...
389 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
393 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
397 id
= attr
->values
[0].integer
;
399 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
409 * 'get_printer()' - Get the named printer and its options.
412 static int /* O - Number of options or -1 on error */
413 get_printer(http_t
*http
, /* I - HTTP connection */
414 const char *name
, /* I - Printer name from request */
415 char *dest
, /* I - Destination buffer */
416 size_t destsize
, /* I - Size of destination buffer */
417 cups_option_t
**options
, /* O - Printer options */
418 int *accepting
, /* O - printer-is-accepting-jobs value */
419 int *shared
, /* O - printer-is-shared value */
420 ipp_pstate_t
*state
) /* O - printer-state value */
422 int num_options
; /* Number of options */
423 cups_file_t
*fp
; /* lpoptions file */
424 char line
[1024], /* Line from lpoptions file */
425 *value
, /* Pointer to value on line */
426 *optptr
; /* Pointer to options on line */
427 int linenum
; /* Line number in file */
428 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
429 ipp_t
*request
; /* IPP request */
430 ipp_t
*response
; /* IPP response */
431 ipp_attribute_t
*attr
; /* IPP attribute */
432 char uri
[HTTP_MAX_URI
]; /* Printer URI */
433 static const char * const requested
[] =
434 { /* Requested attributes */
436 "printer-is-accepting-jobs",
444 * Initialize everything...
452 *state
= IPP_PRINTER_STOPPED
;
457 * See if the name is a queue name optionally with an instance name.
460 strlcpy(dest
, name
, destsize
);
461 if ((value
= strchr(dest
, '/')) != NULL
)
465 * Setup the Get-Printer-Attributes request...
468 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
470 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
471 "localhost", 0, "/printers/%s", dest
);
473 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
476 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
477 "requested-attributes",
478 (int)(sizeof(requested
) / sizeof(requested
[0])),
485 response
= cupsDoRequest(http
, request
, "/");
487 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
490 * If we can't find the printer by name, look up the printer-name
491 * using the printer-info values...
494 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
495 *info_attr
, /* printer-info */
496 *name_attr
, /* printer-name */
497 *shared_attr
, /* printer-is-shared */
498 *state_attr
; /* printer-state */
504 * Setup the CUPS-Get-Printers request...
507 request
= ippNewRequest(CUPS_GET_PRINTERS
);
509 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
510 "requested-attributes",
511 (int)(sizeof(requested
) / sizeof(requested
[0])),
518 response
= cupsDoRequest(http
, request
, "/");
520 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
522 syslog(LOG_ERR
, "Unable to get list of printers - %s",
523 cupsLastErrorString());
531 * Scan the response for printers...
535 attr
= response
->attrs
;
540 * Skip to the next printer...
543 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
550 * Get all of the attributes for the current printer...
553 accepting_attr
= NULL
;
559 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
561 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
562 attr
->value_tag
== IPP_TAG_BOOLEAN
)
563 accepting_attr
= attr
;
564 else if (!strcmp(attr
->name
, "printer-info") &&
565 attr
->value_tag
== IPP_TAG_TEXT
)
567 else if (!strcmp(attr
->name
, "printer-name") &&
568 attr
->value_tag
== IPP_TAG_NAME
)
570 else if (!strcmp(attr
->name
, "printer-is-shared") &&
571 attr
->value_tag
== IPP_TAG_BOOLEAN
)
573 else if (!strcmp(attr
->name
, "printer-state") &&
574 attr
->value_tag
== IPP_TAG_ENUM
)
580 if (info_attr
&& name_attr
&&
581 !_cups_strcasecmp(name
, info_attr
->values
[0].string
.text
))
584 * Found a match, use this one!
587 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
589 if (accepting
&& accepting_attr
)
590 *accepting
= accepting_attr
->values
[0].boolean
;
592 if (shared
&& shared_attr
)
593 *shared
= shared_attr
->values
[0].boolean
;
595 if (state
&& state_attr
)
596 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
606 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
616 * Get values from the response...
621 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
622 IPP_TAG_BOOLEAN
)) == NULL
)
623 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
624 "response from server!");
626 *accepting
= attr
->values
[0].boolean
;
631 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
632 IPP_TAG_BOOLEAN
)) == NULL
)
634 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
635 "response from server!");
639 *shared
= attr
->values
[0].boolean
;
644 if ((attr
= ippFindAttribute(response
, "printer-state",
645 IPP_TAG_ENUM
)) == NULL
)
646 syslog(LOG_ERR
, "No printer-state attribute found in "
647 "response from server!");
649 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
656 * Next look for the printer in the lpoptions file...
661 if (options
&& shared
&& accepting
)
663 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
664 cups_serverroot
= CUPS_SERVERROOT
;
666 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
667 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
670 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
673 * Make sure we have "Dest name options" or "Default name options"...
676 if ((_cups_strcasecmp(line
, "Dest") && _cups_strcasecmp(line
, "Default")) || !value
)
680 * Separate destination name from options...
683 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
685 while (*optptr
== ' ')
689 * If this is our destination, parse the options and break out of
690 * the loop - we're done!
693 if (!_cups_strcasecmp(value
, name
))
695 num_options
= cupsParseOptions(optptr
, num_options
, options
);
707 * Return the number of options for this destination...
710 return (num_options
);
715 * 'print_file()' - Add a file to the current job.
718 static int /* O - 0 on success, -1 on failure */
719 print_file(http_t
*http
, /* I - HTTP connection */
720 int id
, /* I - Job ID */
721 const char *filename
, /* I - File to print */
722 const char *docname
, /* I - document-name */
723 const char *user
, /* I - requesting-user-name */
724 const char *format
, /* I - document-format */
725 int last
) /* I - 1 = last file in job */
727 ipp_t
*request
; /* IPP request */
728 char uri
[HTTP_MAX_URI
]; /* Printer URI */
732 * Setup the Send-Document request...
735 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
737 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
738 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
741 "requesting-user-name", NULL
, user
);
744 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
745 "document-name", NULL
, docname
);
748 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
749 "document-format", NULL
, format
);
751 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last
);
757 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
759 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
761 if (cupsLastError() > IPP_OK_CONFLICT
)
763 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
773 * 'recv_print_job()' - Receive a print job from the client.
776 static int /* O - Command status */
778 const char *queue
, /* I - Printer name */
779 int num_defaults
, /* I - Number of default options */
780 cups_option_t
*defaults
) /* I - Default options */
782 http_t
*http
; /* HTTP connection */
783 int i
; /* Looping var */
784 int status
; /* Command status */
785 int fd
; /* Temporary file */
786 FILE *fp
; /* File pointer */
787 char filename
[1024]; /* Temporary filename */
788 ssize_t bytes
; /* Bytes received */
789 size_t total
; /* Total bytes */
790 char line
[256], /* Line from file/stdin */
791 command
, /* Command from line */
792 *count
, /* Number of bytes */
793 *name
; /* Name of file */
794 const char *job_sheets
; /* Job sheets */
795 int num_data
; /* Number of data files */
796 char control
[1024], /* Control filename */
797 data
[100][256], /* Data files */
798 temp
[100][1024]; /* Temporary files */
799 char user
[1024], /* User name */
800 title
[1024], /* Job title */
801 docname
[1024], /* Document name */
802 dest
[256]; /* Printer/class queue */
803 int accepting
, /* printer-is-accepting */
804 shared
, /* printer-is-shared */
805 num_options
; /* Number of options */
806 cups_option_t
*options
; /* Options */
808 int docnumber
, /* Current document number */
809 doccount
; /* Count of documents */
813 * Connect to the server...
816 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
819 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
827 * See if the printer is available...
830 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
831 &accepting
, &shared
, NULL
);
833 if (num_options
< 0 || !accepting
|| !shared
)
836 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
837 !accepting
? "accepting jobs" : "shared");
839 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
848 putchar(0); /* OK so far... */
851 * Read the request...
860 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
862 if (strlen(line
) < 2)
871 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
872 while (isspace(*name
& 255))
878 case 0x01 : /* Abort */
882 case 0x02 : /* Receive control file */
883 if (strlen(name
) < 2)
885 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
894 * Append to the existing control file - the LPD spec is
895 * not entirely clear, but at least the OS/2 LPD code sends
896 * multiple control files per connection...
899 if ((fd
= open(control
, O_WRONLY
)) < 0)
902 "Unable to append to temporary control file \"%s\" - %s",
903 control
, strerror(errno
));
909 lseek(fd
, 0, SEEK_END
);
913 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
915 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
916 control
, strerror(errno
));
922 strlcpy(filename
, control
, sizeof(filename
));
926 case 0x03 : /* Receive data file */
927 if (strlen(name
) < 2)
929 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
935 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
938 * Too many data files...
941 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
947 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
949 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
951 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
952 temp
[num_data
], strerror(errno
));
958 strlcpy(filename
, temp
[num_data
], sizeof(filename
));
970 * Copy the data or control file from the client...
973 for (total
= (size_t)strtoll(count
, NULL
, 10); total
> 0; total
-= (size_t)bytes
)
975 if (total
> sizeof(line
))
976 bytes
= (ssize_t
)sizeof(line
);
978 bytes
= (ssize_t
)total
;
980 if ((bytes
= (ssize_t
)fread(line
, 1, (size_t)bytes
, stdin
)) > 0)
981 bytes
= write(fd
, line
, (size_t)bytes
);
985 syslog(LOG_ERR
, "Error while reading file - %s",
993 * Read trailing nul...
998 if (fread(line
, 1, 1, stdin
) < 1)
1001 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1007 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1013 * Close the file and send an acknowledgement...
1027 * Process the control file and print stuff...
1030 if ((fp
= fopen(control
, "rb")) == NULL
)
1035 * Copy the default options...
1038 for (i
= 0; i
< num_defaults
; i
++)
1039 num_options
= cupsAddOption(defaults
[i
].name
,
1041 num_options
, &options
);
1044 * Grab the job information...
1052 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1055 * Process control lines...
1060 case 'J' : /* Job name */
1061 strlcpy(title
, line
+ 1, sizeof(title
));
1064 case 'N' : /* Document name */
1065 strlcpy(docname
, line
+ 1, sizeof(docname
));
1068 case 'P' : /* User identification */
1069 strlcpy(user
, line
+ 1, sizeof(user
));
1072 case 'L' : /* Print banner page */
1074 * If a banner was requested and it's not overridden by a
1075 * command line option and the destination's default is none
1076 * then add the standard banner...
1079 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1080 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1081 options
)) == NULL
||
1082 !strcmp(job_sheets
, "none,none")))
1084 num_options
= cupsAddOption("job-sheets", "standard",
1085 num_options
, &options
);
1089 case 'c' : /* Plot CIF file */
1090 case 'd' : /* Print DVI file */
1091 case 'f' : /* Print formatted file */
1092 case 'g' : /* Plot file */
1093 case 'l' : /* Print file leaving control characters (raw) */
1094 case 'n' : /* Print ditroff output file */
1095 case 'o' : /* Print PostScript output file */
1096 case 'p' : /* Print file with 'pr' format (prettyprint) */
1097 case 'r' : /* File to print with FORTRAN carriage control */
1098 case 't' : /* Print troff output file */
1099 case 'v' : /* Print raster file */
1102 if (line
[0] == 'l' &&
1103 !cupsGetOption("document-format", num_options
, options
))
1104 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1107 num_options
= cupsAddOption("prettyprint", "", num_options
,
1117 * Check that we have a username...
1122 syslog(LOG_WARNING
, "No username specified by client! "
1123 "Using \"anonymous\"...");
1124 strlcpy(user
, "anonymous", sizeof(user
));
1131 if ((id
= create_job(http
, dest
, title
, docname
, user
, num_options
,
1137 * Then print the job files...
1145 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1148 * Process control lines...
1153 case 'N' : /* Document name */
1154 strlcpy(docname
, line
+ 1, sizeof(docname
));
1157 case 'c' : /* Plot CIF file */
1158 case 'd' : /* Print DVI file */
1159 case 'f' : /* Print formatted file */
1160 case 'g' : /* Plot file */
1161 case 'l' : /* Print file leaving control characters (raw) */
1162 case 'n' : /* Print ditroff output file */
1163 case 'o' : /* Print PostScript output file */
1164 case 'p' : /* Print file with 'pr' format (prettyprint) */
1165 case 'r' : /* File to print with FORTRAN carriage control */
1166 case 't' : /* Print troff output file */
1167 case 'v' : /* Print raster file */
1169 * Figure out which file we are printing...
1172 for (i
= 0; i
< num_data
; i
++)
1173 if (!strcmp(data
[i
], line
+ 1))
1183 * Send the print file...
1188 if (print_file(http
, id
, temp
[i
], docname
, user
,
1189 cupsGetOption("document-format", num_options
,
1191 docnumber
== doccount
))
1208 cupsFreeOptions(num_options
, options
);
1213 * Clean up all temporary files and return...
1218 for (i
= 0; i
< num_data
; i
++)
1226 * 'remove_jobs()' - Cancel one or more jobs.
1229 static int /* O - Command status */
1230 remove_jobs(const char *dest
, /* I - Destination */
1231 const char *agent
, /* I - User agent */
1232 const char *list
) /* I - List of jobs or users */
1234 int id
; /* Job ID */
1235 http_t
*http
; /* HTTP server connection */
1236 ipp_t
*request
; /* IPP Request */
1237 char uri
[HTTP_MAX_URI
]; /* Job URI */
1240 (void)dest
; /* Suppress compiler warnings... */
1243 * Try connecting to the local server...
1246 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1247 cupsEncryption())) == NULL
)
1249 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1255 * Loop for each job...
1258 while ((id
= atoi(list
)) > 0)
1261 * Skip job ID in list...
1264 while (isdigit(*list
& 255))
1266 while (isspace(*list
& 255))
1270 * Build an IPP_CANCEL_JOB request, which requires the following
1273 * attributes-charset
1274 * attributes-natural-language
1276 * requesting-user-name
1279 request
= ippNewRequest(IPP_CANCEL_JOB
);
1281 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1282 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1284 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1285 "requesting-user-name", NULL
, agent
);
1288 * Do the request and get back a response...
1291 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1293 if (cupsLastError() > IPP_OK_CONFLICT
)
1295 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1296 cupsLastErrorString());
1301 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1311 * 'send_state()' - Send the queue state.
1314 static int /* O - Command status */
1315 send_state(const char *queue
, /* I - Destination */
1316 const char *list
, /* I - Job or user */
1317 int longstatus
) /* I - List of jobs or users */
1319 int id
; /* Job ID from list */
1320 http_t
*http
; /* HTTP server connection */
1321 ipp_t
*request
, /* IPP Request */
1322 *response
; /* IPP Response */
1323 ipp_attribute_t
*attr
; /* Current attribute */
1324 ipp_pstate_t state
; /* Printer state */
1325 const char *jobdest
, /* Pointer into job-printer-uri */
1326 *jobuser
, /* Pointer to job-originating-user-name */
1327 *jobname
; /* Pointer to job-name */
1328 ipp_jstate_t jobstate
; /* job-state */
1329 int jobid
, /* job-id */
1330 jobsize
, /* job-k-octets */
1331 jobcount
, /* Number of jobs */
1332 jobcopies
, /* Number of copies */
1333 rank
; /* Rank of job */
1334 char rankstr
[255]; /* Rank string */
1335 char namestr
[1024]; /* Job name string */
1336 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1337 char dest
[256]; /* Printer/class queue */
1338 static const char * const ranks
[10] = /* Ranking strings */
1351 static const char * const requested
[] =
1352 { /* Requested attributes */
1357 "job-originating-user-name",
1364 * Try connecting to the local server...
1367 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1368 cupsEncryption())) == NULL
)
1370 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1372 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1377 * Get the actual destination name and printer state...
1380 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1382 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1383 cupsLastErrorString());
1384 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1389 * Show the queue state...
1394 case IPP_PRINTER_IDLE
:
1395 printf("%s is ready\n", dest
);
1397 case IPP_PRINTER_PROCESSING
:
1398 printf("%s is ready and printing\n", dest
);
1400 case IPP_PRINTER_STOPPED
:
1401 printf("%s is not ready\n", dest
);
1406 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1407 * the following attributes:
1409 * attributes-charset
1410 * attributes-natural-language
1411 * job-uri or printer-uri
1416 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1418 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1419 "localhost", 0, "/printers/%s", dest
);
1421 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1425 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1428 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1429 "requesting-user-name", NULL
, list
);
1430 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1433 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1434 "requested-attributes",
1435 sizeof(requested
) / sizeof(requested
[0]),
1439 * Do the request and get back a response...
1443 response
= cupsDoRequest(http
, request
, "/");
1445 if (cupsLastError() > IPP_OK_CONFLICT
)
1447 printf("get-jobs failed: %s\n", cupsLastErrorString());
1448 ippDelete(response
);
1453 * Loop through the job list and display them...
1456 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1459 * Skip leading attributes until we hit a job...
1462 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1469 * Pull the needed attributes from this job...
1474 jobstate
= IPP_JOB_PENDING
;
1475 jobname
= "untitled";
1480 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1482 if (!strcmp(attr
->name
, "job-id") &&
1483 attr
->value_tag
== IPP_TAG_INTEGER
)
1484 jobid
= attr
->values
[0].integer
;
1486 if (!strcmp(attr
->name
, "job-k-octets") &&
1487 attr
->value_tag
== IPP_TAG_INTEGER
)
1488 jobsize
= attr
->values
[0].integer
;
1490 if (!strcmp(attr
->name
, "job-state") &&
1491 attr
->value_tag
== IPP_TAG_ENUM
)
1492 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1494 if (!strcmp(attr
->name
, "job-printer-uri") &&
1495 attr
->value_tag
== IPP_TAG_URI
)
1496 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1499 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1500 attr
->value_tag
== IPP_TAG_NAME
)
1501 jobuser
= attr
->values
[0].string
.text
;
1503 if (!strcmp(attr
->name
, "job-name") &&
1504 attr
->value_tag
== IPP_TAG_NAME
)
1505 jobname
= attr
->values
[0].string
.text
;
1507 if (!strcmp(attr
->name
, "copies") &&
1508 attr
->value_tag
== IPP_TAG_INTEGER
)
1509 jobcopies
= attr
->values
[0].integer
;
1515 * See if we have everything needed...
1518 if (!jobdest
|| !jobid
)
1526 if (!longstatus
&& jobcount
== 0)
1527 puts("Rank Owner Job File(s) Total Size");
1532 * Display the job...
1535 if (jobstate
== IPP_JOB_PROCESSING
)
1536 strlcpy(rankstr
, "active", sizeof(rankstr
));
1539 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1548 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1551 strlcpy(namestr
, jobname
, sizeof(namestr
));
1553 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1554 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1557 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1558 jobid
, jobname
, 1024.0 * jobsize
);
1564 ippDelete(response
);
1576 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1579 static char * /* O - Line read or NULL */
1580 smart_gets(char *s
, /* I - Pointer to line buffer */
1581 int len
, /* I - Size of line buffer */
1582 FILE *fp
) /* I - File to read from */
1584 char *ptr
, /* Pointer into line */
1585 *end
; /* End of line */
1586 int ch
; /* Character from file */
1590 * Read the line; unlike fgets(), we read the entire line but dump
1591 * characters that go past the end of the buffer. Also, we accept
1592 * CR, LF, or CR LF for the line endings to be "safe", although
1593 * RFC 1179 specifically says "just use LF".
1599 while ((ch
= getc(fp
)) != EOF
)
1603 else if (ch
== '\r')
1606 * See if a LF follows...
1622 if (ch
== EOF
&& ptr
== s
)