2 * Line Printer Daemon interface for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #define _CUPS_NO_DEPRECATED
19 #include <cups/cups-private.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
28 #ifdef HAVE_INTTYPES_H
29 # include <inttypes.h>
30 #endif /* HAVE_INTTYPES_H */
33 #endif /* __APPLE__ */
37 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
38 * with inetd or another similar program that monitors ports and starts
39 * daemons for each client connection. A typical configuration is:
41 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
43 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
46 * - This daemon does not check to make sure that the source port is
47 * between 721 and 731, since it isn't necessary for proper
48 * functioning and port-based security is no security at all!
50 * - The "Print any waiting jobs" command is a no-op.
52 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
53 * currently match the Solaris LPD mini-daemon.
60 static int create_job(http_t
*http
, const char *dest
, const char *title
,
61 const char *docname
, const char *user
,
62 int num_options
, cups_option_t
*options
);
63 static int get_printer(http_t
*http
, const char *name
, char *dest
,
64 size_t destsize
, cups_option_t
**options
,
65 int *accepting
, int *shared
, ipp_pstate_t
*state
);
66 static int print_file(http_t
*http
, int id
, const char *filename
,
67 const char *docname
, const char *user
,
68 const char *format
, int last
);
69 static int recv_print_job(const char *name
, int num_defaults
,
70 cups_option_t
*defaults
);
71 static int remove_jobs(const char *name
, const char *agent
,
73 static int send_state(const char *name
, const char *list
,
75 static char *smart_gets(char *s
, int len
, FILE *fp
);
79 * 'main()' - Process an incoming LPD request...
82 int /* O - Exit status */
83 main(int argc
, /* I - Number of command-line arguments */
84 char *argv
[]) /* I - Command-line arguments */
86 int i
; /* Looping var */
87 int num_defaults
; /* Number of default options */
88 cups_option_t
*defaults
; /* Default options */
89 char line
[256], /* Command string */
90 command
, /* Command code */
91 *dest
, /* Pointer to destination */
92 *list
, /* Pointer to list */
93 *agent
, /* Pointer to user */
94 status
; /* Status for client */
95 socklen_t hostlen
; /* Size of client address */
96 http_addr_t hostaddr
; /* Address of client */
97 char hostname
[256], /* Name of client */
98 hostip
[256], /* IP address */
99 *hostfamily
; /* Address family */
100 int hostlookups
; /* Do hostname lookups? */
102 vproc_transaction_t vtran
= vproc_transaction_begin(NULL
);
103 #endif /* __APPLE__ */
107 * Don't buffer the output...
110 setbuf(stdout
, NULL
);
113 * Log things using the "cups-lpd" name...
116 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
119 * Scan the command-line for options...
126 for (i
= 1; i
< argc
; i
++)
127 if (argv
[i
][0] == '-')
131 case 'h' : /* -h hostname[:port] */
133 cupsSetServer(argv
[i
] + 2);
138 cupsSetServer(argv
[i
]);
140 syslog(LOG_WARNING
, "Expected hostname string after -h option!");
144 case 'o' : /* Option */
146 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
152 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
155 syslog(LOG_WARNING
, "Expected option string after -o option!");
159 case 'n' : /* Don't do hostname lookups */
164 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
169 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
173 * Get the address of the client...
176 hostlen
= sizeof(hostaddr
);
178 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
180 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
181 strlcpy(hostname
, "unknown", sizeof(hostname
));
185 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
188 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
190 strlcpy(hostname
, hostip
, sizeof(hostname
));
193 if (hostaddr
.addr
.sa_family
== AF_INET6
)
196 #endif /* AF_INET6 */
199 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
203 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
204 num_defaults
, &defaults
);
207 * RFC1179 specifies that only 1 daemon command can be received for
211 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
214 * Unable to get command from client! Send an error status and return.
217 syslog(LOG_ERR
, "Unable to get command line from client!");
221 vproc_transaction_end(NULL
, vtran
);
222 #endif /* __APPLE__ */
228 * The first byte is the command byte. After that will be the queue name,
229 * resource list, and/or user name.
232 if ((command
= line
[0]) == '\0')
241 for (list
= dest
; *list
&& !isspace(*list
& 255); list
++);
243 while (isspace(*list
& 255))
253 default : /* Unknown command */
254 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
255 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
261 case 0x01 : /* Print any waiting jobs */
262 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
268 case 0x02 : /* Receive a printer job */
269 syslog(LOG_INFO
, "Receive print job for %s", dest
);
270 /* recv_print_job() sends initial status byte */
272 status
= (char)recv_print_job(dest
, num_defaults
, defaults
);
275 case 0x03 : /* Send queue state (short) */
276 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
277 /* no status byte for this command */
279 status
= (char)send_state(dest
, list
, 0);
282 case 0x04 : /* Send queue state (long) */
283 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
284 /* no status byte for this command */
286 status
= (char)send_state(dest
, list
, 1);
289 case 0x05 : /* Remove jobs */
293 * Grab the agent and skip to the list of users and/or jobs.
298 for (; *list
&& !isspace(*list
& 255); list
++);
299 while (isspace(*list
& 255))
302 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
304 status
= (char)remove_jobs(dest
, agent
, list
);
313 syslog(LOG_INFO
, "Closing connection");
317 vproc_transaction_end(NULL
, vtran
);
318 #endif /* __APPLE__ */
325 * 'create_job()' - Create a new print job.
328 static int /* O - Job ID or -1 on error */
329 create_job(http_t
*http
, /* I - HTTP connection */
330 const char *dest
, /* I - Destination name */
331 const char *title
, /* I - job-name */
332 const char *docname
, /* I - Name of job file */
333 const char *user
, /* I - requesting-user-name */
334 int num_options
, /* I - Number of options for job */
335 cups_option_t
*options
) /* I - Options for job */
337 ipp_t
*request
; /* IPP request */
338 ipp_t
*response
; /* IPP response */
339 ipp_attribute_t
*attr
; /* IPP attribute */
340 char uri
[HTTP_MAX_URI
]; /* Printer URI */
345 * Setup the Create-Job request...
348 request
= ippNewRequest(IPP_OP_CREATE_JOB
);
350 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
351 "localhost", 0, "/printers/%s", dest
);
353 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
356 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
357 "requesting-user-name", NULL
, user
);
360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
363 cupsEncodeOptions(request
, num_options
, options
);
369 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
371 response
= cupsDoRequest(http
, request
, uri
);
373 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
375 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
383 * Get the job-id value from the response and return it...
386 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
390 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
394 id
= attr
->values
[0].integer
;
396 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
406 * 'get_printer()' - Get the named printer and its options.
409 static int /* O - Number of options or -1 on error */
410 get_printer(http_t
*http
, /* I - HTTP connection */
411 const char *name
, /* I - Printer name from request */
412 char *dest
, /* I - Destination buffer */
413 size_t destsize
, /* I - Size of destination buffer */
414 cups_option_t
**options
, /* O - Printer options */
415 int *accepting
, /* O - printer-is-accepting-jobs value */
416 int *shared
, /* O - printer-is-shared value */
417 ipp_pstate_t
*state
) /* O - printer-state value */
419 int num_options
; /* Number of options */
420 cups_file_t
*fp
; /* lpoptions file */
421 char line
[1024], /* Line from lpoptions file */
422 *value
, /* Pointer to value on line */
423 *optptr
; /* Pointer to options on line */
424 int linenum
; /* Line number in file */
425 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
426 ipp_t
*request
; /* IPP request */
427 ipp_t
*response
; /* IPP response */
428 ipp_attribute_t
*attr
; /* IPP attribute */
429 char uri
[HTTP_MAX_URI
]; /* Printer URI */
430 static const char * const requested
[] =
431 { /* Requested attributes */
433 "printer-is-accepting-jobs",
441 * Initialize everything...
449 *state
= IPP_PSTATE_STOPPED
;
454 * See if the name is a queue name optionally with an instance name.
457 strlcpy(dest
, name
, destsize
);
458 if ((value
= strchr(dest
, '/')) != NULL
)
462 * Setup the Get-Printer-Attributes request...
465 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
467 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
468 "localhost", 0, "/printers/%s", dest
);
470 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
473 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
474 "requested-attributes",
475 (int)(sizeof(requested
) / sizeof(requested
[0])),
482 response
= cupsDoRequest(http
, request
, "/");
484 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
487 * If we can't find the printer by name, look up the printer-name
488 * using the printer-info values...
491 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
492 *info_attr
, /* printer-info */
493 *name_attr
, /* printer-name */
494 *shared_attr
, /* printer-is-shared */
495 *state_attr
; /* printer-state */
501 * Setup the CUPS-Get-Printers request...
504 request
= ippNewRequest(IPP_OP_CUPS_GET_PRINTERS
);
506 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
507 "requested-attributes",
508 (int)(sizeof(requested
) / sizeof(requested
[0])),
515 response
= cupsDoRequest(http
, request
, "/");
517 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
519 syslog(LOG_ERR
, "Unable to get list of printers - %s",
520 cupsLastErrorString());
528 * Scan the response for printers...
532 attr
= response
->attrs
;
537 * Skip to the next printer...
540 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
547 * Get all of the attributes for the current printer...
550 accepting_attr
= NULL
;
556 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
558 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
559 attr
->value_tag
== IPP_TAG_BOOLEAN
)
560 accepting_attr
= attr
;
561 else if (!strcmp(attr
->name
, "printer-info") &&
562 attr
->value_tag
== IPP_TAG_TEXT
)
564 else if (!strcmp(attr
->name
, "printer-name") &&
565 attr
->value_tag
== IPP_TAG_NAME
)
567 else if (!strcmp(attr
->name
, "printer-is-shared") &&
568 attr
->value_tag
== IPP_TAG_BOOLEAN
)
570 else if (!strcmp(attr
->name
, "printer-state") &&
571 attr
->value_tag
== IPP_TAG_ENUM
)
577 if (info_attr
&& name_attr
&&
578 !_cups_strcasecmp(name
, info_attr
->values
[0].string
.text
))
581 * Found a match, use this one!
584 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
586 if (accepting
&& accepting_attr
)
587 *accepting
= accepting_attr
->values
[0].boolean
;
589 if (shared
&& shared_attr
)
590 *shared
= shared_attr
->values
[0].boolean
;
592 if (state
&& state_attr
)
593 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
603 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
613 * Get values from the response...
618 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
619 IPP_TAG_BOOLEAN
)) == NULL
)
620 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
621 "response from server!");
623 *accepting
= attr
->values
[0].boolean
;
628 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
629 IPP_TAG_BOOLEAN
)) == NULL
)
631 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
632 "response from server!");
636 *shared
= attr
->values
[0].boolean
;
641 if ((attr
= ippFindAttribute(response
, "printer-state",
642 IPP_TAG_ENUM
)) == NULL
)
643 syslog(LOG_ERR
, "No printer-state attribute found in "
644 "response from server!");
646 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
653 * Next look for the printer in the lpoptions file...
658 if (options
&& shared
&& accepting
)
660 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
661 cups_serverroot
= CUPS_SERVERROOT
;
663 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
664 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
667 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
670 * Make sure we have "Dest name options" or "Default name options"...
673 if ((_cups_strcasecmp(line
, "Dest") && _cups_strcasecmp(line
, "Default")) || !value
)
677 * Separate destination name from options...
680 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
682 while (*optptr
== ' ')
686 * If this is our destination, parse the options and break out of
687 * the loop - we're done!
690 if (!_cups_strcasecmp(value
, name
))
692 num_options
= cupsParseOptions(optptr
, num_options
, options
);
704 * Return the number of options for this destination...
707 return (num_options
);
712 * 'print_file()' - Add a file to the current job.
715 static int /* O - 0 on success, -1 on failure */
716 print_file(http_t
*http
, /* I - HTTP connection */
717 int id
, /* I - Job ID */
718 const char *filename
, /* I - File to print */
719 const char *docname
, /* I - document-name */
720 const char *user
, /* I - requesting-user-name */
721 const char *format
, /* I - document-format */
722 int last
) /* I - 1 = last file in job */
724 ipp_t
*request
; /* IPP request */
725 char uri
[HTTP_MAX_URI
]; /* Printer URI */
729 * Setup the Send-Document request...
732 request
= ippNewRequest(IPP_OP_SEND_DOCUMENT
);
734 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
735 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
737 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
738 "requesting-user-name", NULL
, user
);
741 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
742 "document-name", NULL
, docname
);
745 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
746 "document-format", NULL
, format
);
748 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last
);
754 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
756 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
758 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
760 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
770 * 'recv_print_job()' - Receive a print job from the client.
773 static int /* O - Command status */
775 const char *queue
, /* I - Printer name */
776 int num_defaults
, /* I - Number of default options */
777 cups_option_t
*defaults
) /* I - Default options */
779 http_t
*http
; /* HTTP connection */
780 int i
; /* Looping var */
781 int status
; /* Command status */
782 int fd
; /* Temporary file */
783 FILE *fp
; /* File pointer */
784 char filename
[1024]; /* Temporary filename */
785 ssize_t bytes
; /* Bytes received */
786 size_t total
; /* Total bytes */
787 char line
[256], /* Line from file/stdin */
788 command
, /* Command from line */
789 *count
, /* Number of bytes */
790 *name
; /* Name of file */
791 const char *job_sheets
; /* Job sheets */
792 int num_data
; /* Number of data files */
793 char control
[1024], /* Control filename */
794 data
[100][256], /* Data files */
795 temp
[100][1024]; /* Temporary files */
796 char user
[1024], /* User name */
797 title
[1024], /* Job title */
798 docname
[1024], /* Document name */
799 dest
[256]; /* Printer/class queue */
800 int accepting
, /* printer-is-accepting */
801 shared
, /* printer-is-shared */
802 num_options
; /* Number of options */
803 cups_option_t
*options
; /* Options */
805 int docnumber
, /* Current document number */
806 doccount
; /* Count of documents */
810 * Connect to the server...
813 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
816 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
824 * See if the printer is available...
827 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
828 &accepting
, &shared
, NULL
);
830 if (num_options
< 0 || !accepting
|| !shared
)
833 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
834 !accepting
? "accepting jobs" : "shared");
836 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
845 putchar(0); /* OK so far... */
848 * Read the request...
857 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
859 if (strlen(line
) < 2)
868 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
869 while (isspace(*name
& 255))
875 case 0x01 : /* Abort */
879 case 0x02 : /* Receive control file */
880 if (strlen(name
) < 2)
882 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
891 * Append to the existing control file - the LPD spec is
892 * not entirely clear, but at least the OS/2 LPD code sends
893 * multiple control files per connection...
896 if ((fd
= open(control
, O_WRONLY
)) < 0)
899 "Unable to append to temporary control file \"%s\" - %s",
900 control
, strerror(errno
));
906 lseek(fd
, 0, SEEK_END
);
910 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
912 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
913 control
, strerror(errno
));
919 strlcpy(filename
, control
, sizeof(filename
));
923 case 0x03 : /* Receive data file */
924 if (strlen(name
) < 2)
926 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
932 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
935 * Too many data files...
938 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
944 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
946 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
948 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
949 temp
[num_data
], strerror(errno
));
955 strlcpy(filename
, temp
[num_data
], sizeof(filename
));
967 * Copy the data or control file from the client...
970 for (total
= (size_t)strtoll(count
, NULL
, 10); total
> 0; total
-= (size_t)bytes
)
972 if (total
> sizeof(line
))
973 bytes
= (ssize_t
)sizeof(line
);
975 bytes
= (ssize_t
)total
;
977 if ((bytes
= (ssize_t
)fread(line
, 1, (size_t)bytes
, stdin
)) > 0)
978 bytes
= write(fd
, line
, (size_t)bytes
);
982 syslog(LOG_ERR
, "Error while reading file - %s",
990 * Read trailing nul...
995 if (fread(line
, 1, 1, stdin
) < 1)
998 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1004 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1010 * Close the file and send an acknowledgement...
1024 * Process the control file and print stuff...
1027 if ((fp
= fopen(control
, "rb")) == NULL
)
1032 * Copy the default options...
1035 for (i
= 0; i
< num_defaults
; i
++)
1036 num_options
= cupsAddOption(defaults
[i
].name
,
1038 num_options
, &options
);
1041 * Grab the job information...
1049 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1052 * Process control lines...
1057 case 'J' : /* Job name */
1058 strlcpy(title
, line
+ 1, sizeof(title
));
1061 case 'N' : /* Document name */
1062 strlcpy(docname
, line
+ 1, sizeof(docname
));
1065 case 'P' : /* User identification */
1066 strlcpy(user
, line
+ 1, sizeof(user
));
1069 case 'L' : /* Print banner page */
1071 * If a banner was requested and it's not overridden by a
1072 * command line option and the destination's default is none
1073 * then add the standard banner...
1076 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1077 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1078 options
)) == NULL
||
1079 !strcmp(job_sheets
, "none,none")))
1081 num_options
= cupsAddOption("job-sheets", "standard",
1082 num_options
, &options
);
1086 case 'c' : /* Plot CIF file */
1087 case 'd' : /* Print DVI file */
1088 case 'f' : /* Print formatted file */
1089 case 'g' : /* Plot file */
1090 case 'l' : /* Print file leaving control characters (raw) */
1091 case 'n' : /* Print ditroff output file */
1092 case 'o' : /* Print PostScript output file */
1093 case 'p' : /* Print file with 'pr' format (prettyprint) */
1094 case 'r' : /* File to print with FORTRAN carriage control */
1095 case 't' : /* Print troff output file */
1096 case 'v' : /* Print raster file */
1099 if (line
[0] == 'l' &&
1100 !cupsGetOption("document-format", num_options
, options
))
1101 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1104 num_options
= cupsAddOption("prettyprint", "", num_options
,
1114 * Check that we have a username...
1119 syslog(LOG_WARNING
, "No username specified by client! "
1120 "Using \"anonymous\"...");
1121 strlcpy(user
, "anonymous", sizeof(user
));
1128 if ((id
= create_job(http
, dest
, title
, docname
, user
, num_options
,
1134 * Then print the job files...
1142 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1145 * Process control lines...
1150 case 'N' : /* Document name */
1151 strlcpy(docname
, line
+ 1, sizeof(docname
));
1154 case 'c' : /* Plot CIF file */
1155 case 'd' : /* Print DVI file */
1156 case 'f' : /* Print formatted file */
1157 case 'g' : /* Plot file */
1158 case 'l' : /* Print file leaving control characters (raw) */
1159 case 'n' : /* Print ditroff output file */
1160 case 'o' : /* Print PostScript output file */
1161 case 'p' : /* Print file with 'pr' format (prettyprint) */
1162 case 'r' : /* File to print with FORTRAN carriage control */
1163 case 't' : /* Print troff output file */
1164 case 'v' : /* Print raster file */
1166 * Figure out which file we are printing...
1169 for (i
= 0; i
< num_data
; i
++)
1170 if (!strcmp(data
[i
], line
+ 1))
1180 * Send the print file...
1185 if (print_file(http
, id
, temp
[i
], docname
, user
,
1186 cupsGetOption("document-format", num_options
,
1188 docnumber
== doccount
))
1205 cupsFreeOptions(num_options
, options
);
1210 * Clean up all temporary files and return...
1215 for (i
= 0; i
< num_data
; i
++)
1223 * 'remove_jobs()' - Cancel one or more jobs.
1226 static int /* O - Command status */
1227 remove_jobs(const char *dest
, /* I - Destination */
1228 const char *agent
, /* I - User agent */
1229 const char *list
) /* I - List of jobs or users */
1231 int id
; /* Job ID */
1232 http_t
*http
; /* HTTP server connection */
1233 ipp_t
*request
; /* IPP Request */
1234 char uri
[HTTP_MAX_URI
]; /* Job URI */
1237 (void)dest
; /* Suppress compiler warnings... */
1240 * Try connecting to the local server...
1243 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1245 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1251 * Loop for each job...
1254 while ((id
= atoi(list
)) > 0)
1257 * Skip job ID in list...
1260 while (isdigit(*list
& 255))
1262 while (isspace(*list
& 255))
1266 * Build an IPP_OP_CANCEL_JOB request, which requires the following
1269 * attributes-charset
1270 * attributes-natural-language
1272 * requesting-user-name
1275 request
= ippNewRequest(IPP_OP_CANCEL_JOB
);
1277 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1278 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1280 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1281 "requesting-user-name", NULL
, agent
);
1284 * Do the request and get back a response...
1287 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1289 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1291 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1292 cupsLastErrorString());
1297 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1307 * 'send_state()' - Send the queue state.
1310 static int /* O - Command status */
1311 send_state(const char *queue
, /* I - Destination */
1312 const char *list
, /* I - Job or user */
1313 int longstatus
) /* I - List of jobs or users */
1315 int id
; /* Job ID from list */
1316 http_t
*http
; /* HTTP server connection */
1317 ipp_t
*request
, /* IPP Request */
1318 *response
; /* IPP Response */
1319 ipp_attribute_t
*attr
; /* Current attribute */
1320 ipp_pstate_t state
; /* Printer state */
1321 const char *jobdest
, /* Pointer into job-printer-uri */
1322 *jobuser
, /* Pointer to job-originating-user-name */
1323 *jobname
; /* Pointer to job-name */
1324 ipp_jstate_t jobstate
; /* job-state */
1325 int jobid
, /* job-id */
1326 jobsize
, /* job-k-octets */
1327 jobcount
, /* Number of jobs */
1328 jobcopies
, /* Number of copies */
1329 rank
; /* Rank of job */
1330 char rankstr
[255]; /* Rank string */
1331 char namestr
[1024]; /* Job name string */
1332 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1333 char dest
[256]; /* Printer/class queue */
1334 static const char * const ranks
[10] = /* Ranking strings */
1347 static const char * const requested
[] =
1348 { /* Requested attributes */
1353 "job-originating-user-name",
1360 * Try connecting to the local server...
1363 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1365 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1367 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1372 * Get the actual destination name and printer state...
1375 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1377 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1378 cupsLastErrorString());
1379 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1384 * Show the queue state...
1389 case IPP_PSTATE_IDLE
:
1390 printf("%s is ready\n", dest
);
1392 case IPP_PSTATE_PROCESSING
:
1393 printf("%s is ready and printing\n", dest
);
1395 case IPP_PSTATE_STOPPED
:
1396 printf("%s is not ready\n", dest
);
1401 * Build an IPP_OP_GET_JOBS or IPP_OP_GET_JOB_ATTRIBUTES request, which requires
1402 * the following attributes:
1404 * attributes-charset
1405 * attributes-natural-language
1406 * job-uri or printer-uri
1411 request
= ippNewRequest(id
? IPP_OP_GET_JOB_ATTRIBUTES
: IPP_OP_GET_JOBS
);
1413 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1414 "localhost", 0, "/printers/%s", dest
);
1416 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1420 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1423 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1424 "requesting-user-name", NULL
, list
);
1425 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1428 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1429 "requested-attributes",
1430 sizeof(requested
) / sizeof(requested
[0]),
1434 * Do the request and get back a response...
1438 response
= cupsDoRequest(http
, request
, "/");
1440 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1442 printf("get-jobs failed: %s\n", cupsLastErrorString());
1443 ippDelete(response
);
1448 * Loop through the job list and display them...
1451 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1454 * Skip leading attributes until we hit a job...
1457 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1464 * Pull the needed attributes from this job...
1469 jobstate
= IPP_JSTATE_PENDING
;
1470 jobname
= "untitled";
1475 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1477 if (!strcmp(attr
->name
, "job-id") &&
1478 attr
->value_tag
== IPP_TAG_INTEGER
)
1479 jobid
= attr
->values
[0].integer
;
1481 if (!strcmp(attr
->name
, "job-k-octets") &&
1482 attr
->value_tag
== IPP_TAG_INTEGER
)
1483 jobsize
= attr
->values
[0].integer
;
1485 if (!strcmp(attr
->name
, "job-state") &&
1486 attr
->value_tag
== IPP_TAG_ENUM
)
1487 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1489 if (!strcmp(attr
->name
, "job-printer-uri") &&
1490 attr
->value_tag
== IPP_TAG_URI
)
1491 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1494 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1495 attr
->value_tag
== IPP_TAG_NAME
)
1496 jobuser
= attr
->values
[0].string
.text
;
1498 if (!strcmp(attr
->name
, "job-name") &&
1499 attr
->value_tag
== IPP_TAG_NAME
)
1500 jobname
= attr
->values
[0].string
.text
;
1502 if (!strcmp(attr
->name
, "copies") &&
1503 attr
->value_tag
== IPP_TAG_INTEGER
)
1504 jobcopies
= attr
->values
[0].integer
;
1510 * See if we have everything needed...
1513 if (!jobdest
|| !jobid
)
1521 if (!longstatus
&& jobcount
== 0)
1522 puts("Rank Owner Job File(s) Total Size");
1527 * Display the job...
1530 if (jobstate
== IPP_JSTATE_PROCESSING
)
1531 strlcpy(rankstr
, "active", sizeof(rankstr
));
1534 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1543 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1546 strlcpy(namestr
, jobname
, sizeof(namestr
));
1548 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1549 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1552 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1553 jobid
, jobname
, 1024.0 * jobsize
);
1559 ippDelete(response
);
1571 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1574 static char * /* O - Line read or NULL */
1575 smart_gets(char *s
, /* I - Pointer to line buffer */
1576 int len
, /* I - Size of line buffer */
1577 FILE *fp
) /* I - File to read from */
1579 char *ptr
, /* Pointer into line */
1580 *end
; /* End of line */
1581 int ch
; /* Character from file */
1585 * Read the line; unlike fgets(), we read the entire line but dump
1586 * characters that go past the end of the buffer. Also, we accept
1587 * CR, LF, or CR LF for the line endings to be "safe", although
1588 * RFC 1179 specifically says "just use LF".
1594 while ((ch
= getc(fp
)) != EOF
)
1598 else if (ch
== '\r')
1601 * See if a LF follows...
1617 if (ch
== EOF
&& ptr
== s
)