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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #define _CUPS_NO_DEPRECATED
15 #include <cups/cups-private.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
24 #ifdef HAVE_INTTYPES_H
25 # include <inttypes.h>
26 #endif /* HAVE_INTTYPES_H */
29 #endif /* __APPLE__ */
33 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
34 * with inetd or another similar program that monitors ports and starts
35 * daemons for each client connection. A typical configuration is:
37 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
39 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
42 * - This daemon does not check to make sure that the source port is
43 * between 721 and 731, since it isn't necessary for proper
44 * functioning and port-based security is no security at all!
46 * - The "Print any waiting jobs" command is a no-op.
48 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
49 * currently match the Solaris LPD mini-daemon.
56 static int create_job(http_t
*http
, const char *dest
, const char *title
, const char *user
, int num_options
, cups_option_t
*options
);
57 static int get_printer(http_t
*http
, const char *name
, char *dest
,
58 size_t destsize
, cups_option_t
**options
,
59 int *accepting
, int *shared
, ipp_pstate_t
*state
);
60 static int print_file(http_t
*http
, int id
, const char *filename
,
61 const char *docname
, const char *user
,
62 const char *format
, int last
);
63 static int recv_print_job(const char *name
, int num_defaults
,
64 cups_option_t
*defaults
);
65 static int remove_jobs(const char *name
, const char *agent
,
67 static int send_state(const char *name
, const char *list
,
69 static char *smart_gets(char *s
, int len
, FILE *fp
);
70 static void smart_strlcpy(char *dst
, const char *src
, size_t dstsize
);
74 * 'main()' - Process an incoming LPD request...
77 int /* O - Exit status */
78 main(int argc
, /* I - Number of command-line arguments */
79 char *argv
[]) /* I - Command-line arguments */
81 int i
; /* Looping var */
82 int num_defaults
; /* Number of default options */
83 cups_option_t
*defaults
; /* Default options */
84 char line
[256], /* Command string */
85 command
, /* Command code */
86 *dest
, /* Pointer to destination */
87 *list
, /* Pointer to list */
88 *agent
, /* Pointer to user */
89 status
; /* Status for client */
90 socklen_t hostlen
; /* Size of client address */
91 http_addr_t hostaddr
; /* Address of client */
92 char hostname
[256], /* Name of client */
93 hostip
[256], /* IP address */
94 *hostfamily
; /* Address family */
95 int hostlookups
; /* Do hostname lookups? */
99 xpc_transaction_begin();
100 #endif /* __APPLE__ */
103 * Don't buffer the output...
106 setbuf(stdout
, NULL
);
109 * Log things using the "cups-lpd" name...
112 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
115 * Scan the command-line for options...
122 for (i
= 1; i
< argc
; i
++)
123 if (argv
[i
][0] == '-')
127 case 'h' : /* -h hostname[:port] */
129 cupsSetServer(argv
[i
] + 2);
134 cupsSetServer(argv
[i
]);
136 syslog(LOG_WARNING
, "Expected hostname string after -h option!");
140 case 'o' : /* Option */
142 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
148 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
151 syslog(LOG_WARNING
, "Expected option string after -o option!");
155 case 'n' : /* Don't do hostname lookups */
160 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
165 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
169 * Get the address of the client...
172 hostlen
= sizeof(hostaddr
);
174 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
176 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
177 strlcpy(hostname
, "unknown", sizeof(hostname
));
181 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
184 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
186 strlcpy(hostname
, hostip
, sizeof(hostname
));
189 if (hostaddr
.addr
.sa_family
== AF_INET6
)
192 #endif /* AF_INET6 */
195 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
199 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
200 num_defaults
, &defaults
);
203 * RFC1179 specifies that only 1 daemon command can be received for
207 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
210 * Unable to get command from client! Send an error status and return.
213 syslog(LOG_ERR
, "Unable to get command line from client!");
217 xpc_transaction_end();
218 #endif /* __APPLE__ */
224 * The first byte is the command byte. After that will be the queue name,
225 * resource list, and/or user name.
228 if ((command
= line
[0]) == '\0')
237 for (list
= dest
; *list
&& !isspace(*list
& 255); list
++);
239 while (isspace(*list
& 255))
249 default : /* Unknown command */
250 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
251 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
257 case 0x01 : /* Print any waiting jobs */
258 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
264 case 0x02 : /* Receive a printer job */
265 syslog(LOG_INFO
, "Receive print job for %s", dest
);
266 /* recv_print_job() sends initial status byte */
268 status
= (char)recv_print_job(dest
, num_defaults
, defaults
);
271 case 0x03 : /* Send queue state (short) */
272 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
273 /* no status byte for this command */
275 status
= (char)send_state(dest
, list
, 0);
278 case 0x04 : /* Send queue state (long) */
279 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
280 /* no status byte for this command */
282 status
= (char)send_state(dest
, list
, 1);
285 case 0x05 : /* Remove jobs */
289 * Grab the agent and skip to the list of users and/or jobs.
294 for (; *list
&& !isspace(*list
& 255); list
++);
295 while (isspace(*list
& 255))
298 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
300 status
= (char)remove_jobs(dest
, agent
, list
);
309 syslog(LOG_INFO
, "Closing connection");
313 xpc_transaction_end();
314 #endif /* __APPLE__ */
321 * 'create_job()' - Create a new print job.
324 static int /* O - Job ID or -1 on error */
325 create_job(http_t
*http
, /* I - HTTP connection */
326 const char *dest
, /* I - Destination name */
327 const char *title
, /* I - job-name */
328 const char *user
, /* I - requesting-user-name */
329 int num_options
, /* I - Number of options for job */
330 cups_option_t
*options
) /* I - Options for job */
332 ipp_t
*request
; /* IPP request */
333 ipp_t
*response
; /* IPP response */
334 ipp_attribute_t
*attr
; /* IPP attribute */
335 char uri
[HTTP_MAX_URI
]; /* Printer URI */
340 * Setup the Create-Job request...
343 request
= ippNewRequest(IPP_OP_CREATE_JOB
);
345 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
346 "localhost", 0, "/printers/%s", dest
);
348 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
351 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
352 "requesting-user-name", NULL
, user
);
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
358 cupsEncodeOptions(request
, num_options
, options
);
364 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
366 response
= cupsDoRequest(http
, request
, uri
);
368 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
370 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
378 * Get the job-id value from the response and return it...
381 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
385 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
389 id
= attr
->values
[0].integer
;
391 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
401 * 'get_printer()' - Get the named printer and its options.
404 static int /* O - Number of options or -1 on error */
405 get_printer(http_t
*http
, /* I - HTTP connection */
406 const char *name
, /* I - Printer name from request */
407 char *dest
, /* I - Destination buffer */
408 size_t destsize
, /* I - Size of destination buffer */
409 cups_option_t
**options
, /* O - Printer options */
410 int *accepting
, /* O - printer-is-accepting-jobs value */
411 int *shared
, /* O - printer-is-shared value */
412 ipp_pstate_t
*state
) /* O - printer-state value */
414 int num_options
; /* Number of options */
415 cups_file_t
*fp
; /* lpoptions file */
416 char line
[1024], /* Line from lpoptions file */
417 *value
, /* Pointer to value on line */
418 *optptr
; /* Pointer to options on line */
419 int linenum
; /* Line number in file */
420 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
421 ipp_t
*request
; /* IPP request */
422 ipp_t
*response
; /* IPP response */
423 ipp_attribute_t
*attr
; /* IPP attribute */
424 char uri
[HTTP_MAX_URI
]; /* Printer URI */
425 static const char * const requested
[] =
426 { /* Requested attributes */
428 "printer-is-accepting-jobs",
436 * Initialize everything...
444 *state
= IPP_PSTATE_STOPPED
;
449 * See if the name is a queue name optionally with an instance name.
452 strlcpy(dest
, name
, destsize
);
453 if ((value
= strchr(dest
, '/')) != NULL
)
457 * Setup the Get-Printer-Attributes request...
460 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
462 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
463 "localhost", 0, "/printers/%s", dest
);
465 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
468 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
469 "requested-attributes",
470 (int)(sizeof(requested
) / sizeof(requested
[0])),
477 response
= cupsDoRequest(http
, request
, "/");
479 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
482 * If we can't find the printer by name, look up the printer-name
483 * using the printer-info values...
486 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
487 *info_attr
, /* printer-info */
488 *name_attr
, /* printer-name */
489 *shared_attr
, /* printer-is-shared */
490 *state_attr
; /* printer-state */
496 * Setup the CUPS-Get-Printers request...
499 request
= ippNewRequest(IPP_OP_CUPS_GET_PRINTERS
);
501 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
502 "requested-attributes",
503 (int)(sizeof(requested
) / sizeof(requested
[0])),
510 response
= cupsDoRequest(http
, request
, "/");
512 if (!response
|| cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
514 syslog(LOG_ERR
, "Unable to get list of printers - %s",
515 cupsLastErrorString());
523 * Scan the response for printers...
527 attr
= response
->attrs
;
532 * Skip to the next printer...
535 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
542 * Get all of the attributes for the current printer...
545 accepting_attr
= NULL
;
551 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
553 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
554 attr
->value_tag
== IPP_TAG_BOOLEAN
)
555 accepting_attr
= attr
;
556 else if (!strcmp(attr
->name
, "printer-info") &&
557 attr
->value_tag
== IPP_TAG_TEXT
)
559 else if (!strcmp(attr
->name
, "printer-name") &&
560 attr
->value_tag
== IPP_TAG_NAME
)
562 else if (!strcmp(attr
->name
, "printer-is-shared") &&
563 attr
->value_tag
== IPP_TAG_BOOLEAN
)
565 else if (!strcmp(attr
->name
, "printer-state") &&
566 attr
->value_tag
== IPP_TAG_ENUM
)
572 if (info_attr
&& name_attr
&&
573 !_cups_strcasecmp(name
, info_attr
->values
[0].string
.text
))
576 * Found a match, use this one!
579 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
581 if (accepting
&& accepting_attr
)
582 *accepting
= accepting_attr
->values
[0].boolean
;
584 if (shared
&& shared_attr
)
585 *shared
= shared_attr
->values
[0].boolean
;
587 if (state
&& state_attr
)
588 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
598 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
608 * Get values from the response...
613 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
614 IPP_TAG_BOOLEAN
)) == NULL
)
615 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
616 "response from server!");
618 *accepting
= attr
->values
[0].boolean
;
623 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
624 IPP_TAG_BOOLEAN
)) == NULL
)
626 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
627 "response from server!");
631 *shared
= attr
->values
[0].boolean
;
636 if ((attr
= ippFindAttribute(response
, "printer-state",
637 IPP_TAG_ENUM
)) == NULL
)
638 syslog(LOG_ERR
, "No printer-state attribute found in "
639 "response from server!");
641 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
648 * Next look for the printer in the lpoptions file...
653 if (options
&& shared
&& accepting
)
655 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
656 cups_serverroot
= CUPS_SERVERROOT
;
658 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
659 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
662 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
665 * Make sure we have "Dest name options" or "Default name options"...
668 if ((_cups_strcasecmp(line
, "Dest") && _cups_strcasecmp(line
, "Default")) || !value
)
672 * Separate destination name from options...
675 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
677 while (*optptr
== ' ')
681 * If this is our destination, parse the options and break out of
682 * the loop - we're done!
685 if (!_cups_strcasecmp(value
, name
))
687 num_options
= cupsParseOptions(optptr
, num_options
, options
);
699 * Return the number of options for this destination...
702 return (num_options
);
707 * 'print_file()' - Add a file to the current job.
710 static int /* O - 0 on success, -1 on failure */
711 print_file(http_t
*http
, /* I - HTTP connection */
712 int id
, /* I - Job ID */
713 const char *filename
, /* I - File to print */
714 const char *docname
, /* I - document-name */
715 const char *user
, /* I - requesting-user-name */
716 const char *format
, /* I - document-format */
717 int last
) /* I - 1 = last file in job */
719 ipp_t
*request
; /* IPP request */
720 char uri
[HTTP_MAX_URI
]; /* Printer URI */
724 * Setup the Send-Document request...
727 request
= ippNewRequest(IPP_OP_SEND_DOCUMENT
);
729 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
730 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
732 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
733 "requesting-user-name", NULL
, user
);
736 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
737 "document-name", NULL
, docname
);
740 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
741 "document-format", NULL
, format
);
743 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", (char)last
);
749 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
751 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
753 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
755 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
765 * 'recv_print_job()' - Receive a print job from the client.
768 static int /* O - Command status */
770 const char *queue
, /* I - Printer name */
771 int num_defaults
, /* I - Number of default options */
772 cups_option_t
*defaults
) /* I - Default options */
774 http_t
*http
; /* HTTP connection */
775 int i
; /* Looping var */
776 int status
; /* Command status */
777 int fd
; /* Temporary file */
778 FILE *fp
; /* File pointer */
779 char filename
[1024]; /* Temporary filename */
780 ssize_t bytes
; /* Bytes received */
781 size_t total
; /* Total bytes */
782 char line
[256], /* Line from file/stdin */
783 command
, /* Command from line */
784 *count
, /* Number of bytes */
785 *name
; /* Name of file */
786 const char *job_sheets
; /* Job sheets */
787 int num_data
; /* Number of data files */
788 char control
[1024], /* Control filename */
789 data
[100][256], /* Data files */
790 temp
[100][1024]; /* Temporary files */
791 char user
[1024], /* User name */
792 title
[1024], /* Job title */
793 docname
[1024], /* Document name */
794 dest
[256]; /* Printer/class queue */
795 int accepting
, /* printer-is-accepting */
796 shared
, /* printer-is-shared */
797 num_options
; /* Number of options */
798 cups_option_t
*options
; /* Options */
800 int docnumber
, /* Current document number */
801 doccount
; /* Count of documents */
805 * Connect to the server...
808 http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
);
811 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
819 * See if the printer is available...
822 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
823 &accepting
, &shared
, NULL
);
825 if (num_options
< 0 || !accepting
|| !shared
)
828 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
829 !accepting
? "accepting jobs" : "shared");
831 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
840 putchar(0); /* OK so far... */
843 * Read the request...
852 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
854 if (strlen(line
) < 2)
863 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
864 while (isspace(*name
& 255))
870 case 0x01 : /* Abort */
874 case 0x02 : /* Receive control file */
875 if (strlen(name
) < 2)
877 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
886 * Append to the existing control file - the LPD spec is
887 * not entirely clear, but at least the OS/2 LPD code sends
888 * multiple control files per connection...
891 if ((fd
= open(control
, O_WRONLY
)) < 0)
894 "Unable to append to temporary control file \"%s\" - %s",
895 control
, strerror(errno
));
901 lseek(fd
, 0, SEEK_END
);
905 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
907 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
908 control
, strerror(errno
));
914 strlcpy(filename
, control
, sizeof(filename
));
918 case 0x03 : /* Receive data file */
919 if (strlen(name
) < 2)
921 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
927 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
930 * Too many data files...
933 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
939 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
941 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
943 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
944 temp
[num_data
], strerror(errno
));
950 strlcpy(filename
, temp
[num_data
], sizeof(filename
));
962 * Copy the data or control file from the client...
965 for (total
= (size_t)strtoll(count
, NULL
, 10); total
> 0; total
-= (size_t)bytes
)
967 if (total
> sizeof(line
))
968 bytes
= (ssize_t
)sizeof(line
);
970 bytes
= (ssize_t
)total
;
972 if ((bytes
= (ssize_t
)fread(line
, 1, (size_t)bytes
, stdin
)) > 0)
973 bytes
= write(fd
, line
, (size_t)bytes
);
977 syslog(LOG_ERR
, "Error while reading file - %s",
985 * Read trailing nul...
990 if (fread(line
, 1, 1, stdin
) < 1)
993 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
999 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1005 * Close the file and send an acknowledgement...
1019 * Process the control file and print stuff...
1022 if ((fp
= fopen(control
, "rb")) == NULL
)
1027 * Copy the default options...
1030 for (i
= 0; i
< num_defaults
; i
++)
1031 num_options
= cupsAddOption(defaults
[i
].name
,
1033 num_options
, &options
);
1036 * Grab the job information...
1044 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1047 * Process control lines...
1052 case 'J' : /* Job name */
1053 smart_strlcpy(title
, line
+ 1, sizeof(title
));
1056 case 'N' : /* Document name */
1057 smart_strlcpy(docname
, line
+ 1, sizeof(docname
));
1060 case 'P' : /* User identification */
1061 smart_strlcpy(user
, line
+ 1, sizeof(user
));
1064 case 'L' : /* Print banner page */
1066 * If a banner was requested and it's not overridden by a
1067 * command line option and the destination's default is none
1068 * then add the standard banner...
1071 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1072 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1073 options
)) == NULL
||
1074 !strcmp(job_sheets
, "none,none")))
1076 num_options
= cupsAddOption("job-sheets", "standard",
1077 num_options
, &options
);
1081 case 'c' : /* Plot CIF file */
1082 case 'd' : /* Print DVI file */
1083 case 'f' : /* Print formatted file */
1084 case 'g' : /* Plot file */
1085 case 'l' : /* Print file leaving control characters (raw) */
1086 case 'n' : /* Print ditroff output file */
1087 case 'o' : /* Print PostScript output file */
1088 case 'p' : /* Print file with 'pr' format (prettyprint) */
1089 case 'r' : /* File to print with FORTRAN carriage control */
1090 case 't' : /* Print troff output file */
1091 case 'v' : /* Print raster file */
1094 if (line
[0] == 'l' &&
1095 !cupsGetOption("document-format", num_options
, options
))
1096 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1099 num_options
= cupsAddOption("prettyprint", "", num_options
,
1109 * Check that we have a username...
1114 syslog(LOG_WARNING
, "No username specified by client! "
1115 "Using \"anonymous\"...");
1116 strlcpy(user
, "anonymous", sizeof(user
));
1123 if ((id
= create_job(http
, dest
, title
, user
, num_options
, options
)) < 0)
1128 * Then print the job files...
1136 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1139 * Process control lines...
1144 case 'N' : /* Document name */
1145 smart_strlcpy(docname
, line
+ 1, sizeof(docname
));
1148 case 'c' : /* Plot CIF file */
1149 case 'd' : /* Print DVI file */
1150 case 'f' : /* Print formatted file */
1151 case 'g' : /* Plot file */
1152 case 'l' : /* Print file leaving control characters (raw) */
1153 case 'n' : /* Print ditroff output file */
1154 case 'o' : /* Print PostScript output file */
1155 case 'p' : /* Print file with 'pr' format (prettyprint) */
1156 case 'r' : /* File to print with FORTRAN carriage control */
1157 case 't' : /* Print troff output file */
1158 case 'v' : /* Print raster file */
1160 * Figure out which file we are printing...
1163 for (i
= 0; i
< num_data
; i
++)
1164 if (!strcmp(data
[i
], line
+ 1))
1174 * Send the print file...
1179 if (print_file(http
, id
, temp
[i
], docname
, user
,
1180 cupsGetOption("document-format", num_options
,
1182 docnumber
== doccount
))
1199 cupsFreeOptions(num_options
, options
);
1204 * Clean up all temporary files and return...
1209 for (i
= 0; i
< num_data
; i
++)
1217 * 'remove_jobs()' - Cancel one or more jobs.
1220 static int /* O - Command status */
1221 remove_jobs(const char *dest
, /* I - Destination */
1222 const char *agent
, /* I - User agent */
1223 const char *list
) /* I - List of jobs or users */
1225 int id
; /* Job ID */
1226 http_t
*http
; /* HTTP server connection */
1227 ipp_t
*request
; /* IPP Request */
1228 char uri
[HTTP_MAX_URI
]; /* Job URI */
1231 (void)dest
; /* Suppress compiler warnings... */
1234 * Try connecting to the local server...
1237 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1239 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1245 * Loop for each job...
1248 while ((id
= atoi(list
)) > 0)
1251 * Skip job ID in list...
1254 while (isdigit(*list
& 255))
1256 while (isspace(*list
& 255))
1260 * Build an IPP_OP_CANCEL_JOB request, which requires the following
1263 * attributes-charset
1264 * attributes-natural-language
1266 * requesting-user-name
1269 request
= ippNewRequest(IPP_OP_CANCEL_JOB
);
1271 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1272 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1274 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1275 "requesting-user-name", NULL
, agent
);
1278 * Do the request and get back a response...
1281 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1283 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1285 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1286 cupsLastErrorString());
1291 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1301 * 'send_state()' - Send the queue state.
1304 static int /* O - Command status */
1305 send_state(const char *queue
, /* I - Destination */
1306 const char *list
, /* I - Job or user */
1307 int longstatus
) /* I - List of jobs or users */
1309 int id
; /* Job ID from list */
1310 http_t
*http
; /* HTTP server connection */
1311 ipp_t
*request
, /* IPP Request */
1312 *response
; /* IPP Response */
1313 ipp_attribute_t
*attr
; /* Current attribute */
1314 ipp_pstate_t state
; /* Printer state */
1315 const char *jobdest
, /* Pointer into job-printer-uri */
1316 *jobuser
, /* Pointer to job-originating-user-name */
1317 *jobname
; /* Pointer to job-name */
1318 ipp_jstate_t jobstate
; /* job-state */
1319 int jobid
, /* job-id */
1320 jobsize
, /* job-k-octets */
1321 jobcount
, /* Number of jobs */
1322 jobcopies
, /* Number of copies */
1323 rank
; /* Rank of job */
1324 char rankstr
[255]; /* Rank string */
1325 char namestr
[1024]; /* Job name string */
1326 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1327 char dest
[256]; /* Printer/class queue */
1328 static const char * const ranks
[10] = /* Ranking strings */
1341 static const char * const requested
[] =
1342 { /* Requested attributes */
1347 "job-originating-user-name",
1354 * Try connecting to the local server...
1357 if ((http
= httpConnect2(cupsServer(), ippPort(), NULL
, AF_UNSPEC
, cupsEncryption(), 1, 30000, NULL
)) == NULL
)
1359 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1361 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1366 * Get the actual destination name and printer state...
1369 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1371 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1372 cupsLastErrorString());
1373 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1378 * Show the queue state...
1383 case IPP_PSTATE_IDLE
:
1384 printf("%s is ready\n", dest
);
1386 case IPP_PSTATE_PROCESSING
:
1387 printf("%s is ready and printing\n", dest
);
1389 case IPP_PSTATE_STOPPED
:
1390 printf("%s is not ready\n", dest
);
1395 * Build an IPP_OP_GET_JOBS or IPP_OP_GET_JOB_ATTRIBUTES request, which requires
1396 * the following attributes:
1398 * attributes-charset
1399 * attributes-natural-language
1400 * job-uri or printer-uri
1405 request
= ippNewRequest(id
? IPP_OP_GET_JOB_ATTRIBUTES
: IPP_OP_GET_JOBS
);
1407 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1408 "localhost", 0, "/printers/%s", dest
);
1410 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1414 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1417 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1418 "requesting-user-name", NULL
, list
);
1419 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1422 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1423 "requested-attributes",
1424 sizeof(requested
) / sizeof(requested
[0]),
1428 * Do the request and get back a response...
1432 response
= cupsDoRequest(http
, request
, "/");
1434 if (cupsLastError() > IPP_STATUS_OK_CONFLICTING
)
1436 printf("get-jobs failed: %s\n", cupsLastErrorString());
1437 ippDelete(response
);
1442 * Loop through the job list and display them...
1445 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1448 * Skip leading attributes until we hit a job...
1451 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1458 * Pull the needed attributes from this job...
1463 jobstate
= IPP_JSTATE_PENDING
;
1464 jobname
= "untitled";
1469 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1471 if (!strcmp(attr
->name
, "job-id") &&
1472 attr
->value_tag
== IPP_TAG_INTEGER
)
1473 jobid
= attr
->values
[0].integer
;
1475 if (!strcmp(attr
->name
, "job-k-octets") &&
1476 attr
->value_tag
== IPP_TAG_INTEGER
)
1477 jobsize
= attr
->values
[0].integer
;
1479 if (!strcmp(attr
->name
, "job-state") &&
1480 attr
->value_tag
== IPP_TAG_ENUM
)
1481 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1483 if (!strcmp(attr
->name
, "job-printer-uri") &&
1484 attr
->value_tag
== IPP_TAG_URI
)
1485 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1488 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1489 attr
->value_tag
== IPP_TAG_NAME
)
1490 jobuser
= attr
->values
[0].string
.text
;
1492 if (!strcmp(attr
->name
, "job-name") &&
1493 attr
->value_tag
== IPP_TAG_NAME
)
1494 jobname
= attr
->values
[0].string
.text
;
1496 if (!strcmp(attr
->name
, "copies") &&
1497 attr
->value_tag
== IPP_TAG_INTEGER
)
1498 jobcopies
= attr
->values
[0].integer
;
1504 * See if we have everything needed...
1507 if (!jobdest
|| !jobid
)
1515 if (!longstatus
&& jobcount
== 0)
1516 puts("Rank Owner Job File(s) Total Size");
1521 * Display the job...
1524 if (jobstate
== IPP_JSTATE_PROCESSING
)
1525 strlcpy(rankstr
, "active", sizeof(rankstr
));
1528 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1537 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1540 strlcpy(namestr
, jobname
, sizeof(namestr
));
1542 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1543 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1546 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1547 jobid
, jobname
, 1024.0 * jobsize
);
1553 ippDelete(response
);
1565 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1568 static char * /* O - Line read or NULL */
1569 smart_gets(char *s
, /* I - Pointer to line buffer */
1570 int len
, /* I - Size of line buffer */
1571 FILE *fp
) /* I - File to read from */
1573 char *ptr
, /* Pointer into line */
1574 *end
; /* End of line */
1575 int ch
; /* Character from file */
1579 * Read the line; unlike fgets(), we read the entire line but dump
1580 * characters that go past the end of the buffer. Also, we accept
1581 * CR, LF, or CR LF for the line endings to be "safe", although
1582 * RFC 1179 specifically says "just use LF".
1588 while ((ch
= getc(fp
)) != EOF
)
1592 else if (ch
== '\r')
1595 * See if a LF follows...
1611 if (ch
== EOF
&& ptr
== s
)
1619 * 'smart_strlcpy()' - Copy a string and convert from ISO-8859-1 to UTF-8 as needed.
1623 smart_strlcpy(char *dst
, /* I - Output buffer */
1624 const char *src
, /* I - Input string */
1625 size_t dstsize
) /* I - Size of output buffer */
1627 const unsigned char *srcptr
; /* Pointer into input string */
1628 unsigned char *dstptr
, /* Pointer into output buffer */
1629 *dstend
; /* End of output buffer */
1630 int saw_8859
= 0; /* Saw an extended character that was not UTF-8? */
1633 for (srcptr
= (unsigned char *)src
, dstptr
= (unsigned char *)dst
, dstend
= dstptr
+ dstsize
- 1; *srcptr
;)
1636 *dstptr
++ = *srcptr
++; /* ASCII */
1640 * Map ISO-8859-1 (most likely character set for legacy LPD clients) to
1644 if (dstptr
> (dstend
- 2))
1647 *dstptr
++ = 0xc0 | (*srcptr
>> 6);
1648 *dstptr
++ = 0x80 | (*srcptr
++ & 0x3f);
1650 else if ((*srcptr
& 0xe0) == 0xc0 && (srcptr
[1] & 0xc0) == 0x80)
1653 * 2-byte UTF-8 sequence...
1656 if (dstptr
> (dstend
- 2))
1659 *dstptr
++ = *srcptr
++;
1660 *dstptr
++ = *srcptr
++;
1662 else if ((*srcptr
& 0xf0) == 0xe0 && (srcptr
[1] & 0xc0) == 0x80 && (srcptr
[2] & 0xc0) == 0x80)
1665 * 3-byte UTF-8 sequence...
1668 if (dstptr
> (dstend
- 3))
1671 *dstptr
++ = *srcptr
++;
1672 *dstptr
++ = *srcptr
++;
1673 *dstptr
++ = *srcptr
++;
1675 else if ((*srcptr
& 0xf8) == 0xf0 && (srcptr
[1] & 0xc0) == 0x80 && (srcptr
[2] & 0xc0) == 0x80 && (srcptr
[3] & 0xc0) == 0x80)
1678 * 4-byte UTF-8 sequence...
1681 if (dstptr
> (dstend
- 4))
1684 *dstptr
++ = *srcptr
++;
1685 *dstptr
++ = *srcptr
++;
1686 *dstptr
++ = *srcptr
++;
1687 *dstptr
++ = *srcptr
++;
1692 * Bad UTF-8 sequence, this must be an ISO-8859-1 string...
1697 if (dstptr
> (dstend
- 2))
1700 *dstptr
++ = 0xc0 | (*srcptr
>> 6);
1701 *dstptr
++ = 0x80 | (*srcptr
++ & 0x3f);