blob: fc6bca9e8dfd6fcd4d8d38766787aa599e63cafa [file] [log] [blame]
Denis Vlasenko68444b92008-02-22 22:24:48 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +00003 * bare bones version of lpr & lpq: BSD printing utilities
Denis Vlasenko68444b92008-02-22 22:24:48 +00004 *
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +00005 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
7 * Original idea and code:
8 * Walter Harms <WHarms@bfs.de>
9 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000011 *
Denis Vlasenko6d8112c2008-02-24 22:08:57 +000012 * See RFC 1179 for protocol description.
Denis Vlasenko68444b92008-02-22 22:24:48 +000013 */
Pere Orga5bc8c002011-04-11 03:29:49 +020014
15//usage:#define lpr_trivial_usage
16//usage: "-P queue[@host[:port]] -U USERNAME -J TITLE -Vmh [FILE]..."
17/* -C CLASS exists too, not shown.
18 * CLASS is supposed to be printed on banner page, if one is requested */
19//usage:#define lpr_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020020//usage: " -P lp service to connect to (else uses $PRINTER)"
Pere Orga5bc8c002011-04-11 03:29:49 +020021//usage: "\n -m Send mail on completion"
22//usage: "\n -h Print banner page too"
23//usage: "\n -V Verbose"
24//usage:
25//usage:#define lpq_trivial_usage
26//usage: "[-P queue[@host[:port]]] [-U USERNAME] [-d JOBID]... [-fs]"
27//usage:#define lpq_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020028//usage: " -P lp service to connect to (else uses $PRINTER)"
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage: "\n -d Delete jobs"
30//usage: "\n -f Force any waiting job to be printed"
31//usage: "\n -s Short display"
32
Denis Vlasenko68444b92008-02-22 22:24:48 +000033#include "libbb.h"
Denis Vlasenko68444b92008-02-22 22:24:48 +000034
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000035/*
36 * LPD returns binary 0 on success.
37 * Otherwise it returns error message.
Denis Vlasenko68444b92008-02-22 22:24:48 +000038 */
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000039static void get_response_or_say_and_die(int fd, const char *errmsg)
Denis Vlasenko68444b92008-02-22 22:24:48 +000040{
Denis Vlasenko394eebe2008-02-25 20:30:24 +000041 ssize_t sz;
42 char buf[128];
Denis Vlasenko68444b92008-02-22 22:24:48 +000043
Denis Vlasenko394eebe2008-02-25 20:30:24 +000044 buf[0] = ' ';
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000045 sz = safe_read(fd, buf, 1);
Denis Vlasenko394eebe2008-02-25 20:30:24 +000046 if ('\0' != buf[0]) {
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000047 // request has failed
Denis Vlasenko394eebe2008-02-25 20:30:24 +000048 // try to make sure last char is '\n', but do not add
49 // superfluous one
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000050 sz = full_read(fd, buf + 1, 126);
Denis Vlasenko394eebe2008-02-25 20:30:24 +000051 bb_error_msg("error while %s%s", errmsg,
52 (sz > 0 ? ". Server said:" : ""));
53 if (sz > 0) {
54 // sz = (bytes in buf) - 1
55 if (buf[sz] != '\n')
56 buf[++sz] = '\n';
57 safe_write(STDERR_FILENO, buf, sz + 1);
58 }
Denis Vlasenko68444b92008-02-22 22:24:48 +000059 xfunc_die();
60 }
61}
62
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000063int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000064int lpqr_main(int argc UNUSED_PARAM, char *argv[])
Denis Vlasenko68444b92008-02-22 22:24:48 +000065{
Denis Vlasenko68444b92008-02-22 22:24:48 +000066 enum {
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000067 OPT_P = 1 << 0, // -P queue[@host[:port]]. If no -P is given use $PRINTER, then "lp@localhost:515"
68 OPT_U = 1 << 1, // -U username
69
70 LPR_V = 1 << 2, // -V: be verbose
Denis Vlasenko42cc3042008-03-24 02:05:58 +000071 LPR_h = 1 << 3, // -h: want banner printed
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000072 LPR_C = 1 << 4, // -C class: job "class" (? supposedly printed on banner)
73 LPR_J = 1 << 5, // -J title: the job title for the banner page
74 LPR_m = 1 << 6, // -m: send mail back to user
75
76 LPQ_SHORT_FMT = 1 << 2, // -s: short listing format
77 LPQ_DELETE = 1 << 3, // -d: delete job(s)
78 LPQ_FORCE = 1 << 4, // -f: force waiting job(s) to be printed
Denis Vlasenko68444b92008-02-22 22:24:48 +000079 };
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000080 char tempfile[sizeof("/tmp/lprXXXXXX")];
81 const char *job_title;
82 const char *printer_class = ""; // printer class, max 32 char
83 const char *queue; // name of printer queue
84 const char *server = "localhost"; // server[:port] of printer queue
85 char *hostname;
86 // N.B. IMHO getenv("USER") can be way easily spoofed!
Denis Vlasenko0c68a872008-12-02 22:56:59 +000087 const char *user = xuid2uname(getuid());
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000088 unsigned job;
89 unsigned opts;
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000090 int fd;
Denis Vlasenko68444b92008-02-22 22:24:48 +000091
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000092 // parse options
93 // TODO: set opt_complementary: s,d,f are mutually exclusive
94 opts = getopt32(argv,
95 (/*lp*/'r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
96 , &queue, &user
97 , &printer_class, &job_title
98 );
Denis Vlasenko68444b92008-02-22 22:24:48 +000099 argv += optind;
Denis Vlasenko68444b92008-02-22 22:24:48 +0000100
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000101 // if queue is not specified -> use $PRINTER
102 if (!(opts & OPT_P))
103 queue = getenv("PRINTER");
104 // if queue is still not specified ->
105 if (!queue) {
106 // ... queue defaults to "lp"
107 // server defaults to "localhost"
108 queue = "lp";
109 // if queue is specified ->
110 } else {
111 // queue name is to the left of '@'
112 char *s = strchr(queue, '@');
113 if (s) {
114 // server name is to the right of '@'
115 *s = '\0';
116 server = s + 1;
117 }
Denis Vlasenko68444b92008-02-22 22:24:48 +0000118 }
119
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000120 // do connect
121 fd = create_and_connect_stream_or_die(server, 515);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000122
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000123 //
124 // LPQ ------------------------
125 //
126 if (/*lp*/'q' == applet_name[2]) {
127 char cmd;
128 // force printing of every job still in queue
129 if (opts & LPQ_FORCE) {
130 cmd = 1;
131 goto command;
132 // delete job(s)
133 } else if (opts & LPQ_DELETE) {
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000134 fdprintf(fd, "\x5" "%s %s", queue, user);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000135 while (*argv) {
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000136 fdprintf(fd, " %s", *argv++);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000137 }
138 bb_putchar('\n');
139 // dump current jobs status
140 // N.B. periodical polling should be achieved
141 // via "watch -n delay lpq"
142 // They say it's the UNIX-way :)
143 } else {
144 cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
145 command:
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000146 fdprintf(fd, "%c" "%s\n", cmd, queue);
147 bb_copyfd_eof(fd, STDOUT_FILENO);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000148 }
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000149
150 return EXIT_SUCCESS;
151 }
152
153 //
154 // LPR ------------------------
155 //
156 if (opts & LPR_V)
157 bb_error_msg("connected to server");
158
159 job = getpid() % 1000;
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000160 hostname = safe_gethostname();
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000161
162 // no files given on command line? -> use stdin
163 if (!*argv)
164 *--argv = (char *)"-";
165
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000166 fdprintf(fd, "\x2" "%s\n", queue);
167 get_response_or_say_and_die(fd, "setting queue");
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000168
169 // process files
170 do {
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000171 unsigned cflen;
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000172 int dfd;
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000173 struct stat st;
174 char *c;
175 char *remote_filename;
176 char *controlfile;
177
178 // if data file is stdin, we need to dump it first
179 if (LONE_DASH(*argv)) {
180 strcpy(tempfile, "/tmp/lprXXXXXX");
Alexander Shishkin67227372010-10-22 13:27:16 +0200181 dfd = xmkstemp(tempfile);
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000182 bb_copyfd_eof(STDIN_FILENO, dfd);
183 xlseek(dfd, 0, SEEK_SET);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000184 *argv = (char*)bb_msg_standard_input;
185 } else {
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000186 dfd = xopen(*argv, O_RDONLY);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000187 }
188
189 /* "The name ... should start with ASCII "cfA",
190 * followed by a three digit job number, followed
191 * by the host name which has constructed the file."
192 * We supply 'c' or 'd' as needed for control/data file. */
193 remote_filename = xasprintf("fA%03u%s", job, hostname);
194
195 // create control file
196 // TODO: all lines but 2 last are constants! How we can use this fact?
197 controlfile = xasprintf(
198 "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
199 "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
200 "J" "%.99s\n" /* J JOBNAME */
201 /* "class name for banner page and job name
202 * for banner page commands must precede L command" */
203 "L" "%.32s\n" /* L USER - print banner page, with given user's name */
204 "M" "%.32s\n" /* M WHOM_TO_MAIL */
205 "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
206 , hostname, user
207 , printer_class /* can be "" */
208 , ((opts & LPR_J) ? job_title : *argv)
209 , (opts & LPR_h) ? user : ""
210 , (opts & LPR_m) ? user : ""
211 , remote_filename
212 );
213 // delete possible "\nX\n" patterns
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000214 c = controlfile;
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000215 cflen = (unsigned)strlen(controlfile);
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000216 while ((c = strchr(c, '\n')) != NULL) {
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000217 if (c[1] && c[2] == '\n') {
218 /* can't use strcpy, results are undefined */
219 memmove(c, c+2, cflen - (c-controlfile) - 1);
220 cflen -= 2;
221 } else {
222 c++;
223 }
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000224 }
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000225
226 // send control file
227 if (opts & LPR_V)
228 bb_error_msg("sending control file");
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000229 /* "Acknowledgement processing must occur as usual
230 * after the command is sent." */
231 fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
232 get_response_or_say_and_die(fd, "sending control file");
Denis Vlasenko68444b92008-02-22 22:24:48 +0000233 /* "Once all of the contents have
234 * been delivered, an octet of zero bits is sent as
235 * an indication that the file being sent is complete.
236 * A second level of acknowledgement processing
237 * must occur at this point." */
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000238 full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000239 get_response_or_say_and_die(fd, "sending control file");
Denis Vlasenko68444b92008-02-22 22:24:48 +0000240
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000241 // send data file, with name "dfaXXX"
242 if (opts & LPR_V)
243 bb_error_msg("sending data file");
244 st.st_size = 0; /* paranoia: fstat may theoretically fail */
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000245 fstat(dfd, &st);
246 fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000247 get_response_or_say_and_die(fd, "sending data file");
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000248 if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000249 // We're screwed. We sent less bytes than we advertised.
250 bb_error_msg_and_die("local file changed size?!");
251 }
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000252 write(fd, "", 1); // send ACK
253 get_response_or_say_and_die(fd, "sending data file");
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000254
255 // delete temporary file if we dumped stdin
256 if (*argv == (char*)bb_msg_standard_input)
257 unlink(tempfile);
258
259 // cleanup
Denis Vlasenko68444b92008-02-22 22:24:48 +0000260 close(fd);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000261 free(remote_filename);
262 free(controlfile);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000263
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000264 // say job accepted
265 if (opts & LPR_V)
266 bb_error_msg("job accepted");
267
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000268 // next, please!
269 job = (job + 1) % 1000;
270 } while (*++argv);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000271
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000272 return EXIT_SUCCESS;
Denis Vlasenko68444b92008-02-22 22:24:48 +0000273}