blob: f8ee9a11b20fce3cabaf9681f2c52c6ad1537d81 [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"
20//usage: "Options:"
21//usage: "\n -P lp service to connect to (else uses $PRINTER)"
22//usage: "\n -m Send mail on completion"
23//usage: "\n -h Print banner page too"
24//usage: "\n -V Verbose"
25//usage:
26//usage:#define lpq_trivial_usage
27//usage: "[-P queue[@host[:port]]] [-U USERNAME] [-d JOBID]... [-fs]"
28//usage:#define lpq_full_usage "\n\n"
29//usage: "Options:"
30//usage: "\n -P lp service to connect to (else uses $PRINTER)"
31//usage: "\n -d Delete jobs"
32//usage: "\n -f Force any waiting job to be printed"
33//usage: "\n -s Short display"
34
Denis Vlasenko68444b92008-02-22 22:24:48 +000035#include "libbb.h"
Denis Vlasenko68444b92008-02-22 22:24:48 +000036
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000037/*
38 * LPD returns binary 0 on success.
39 * Otherwise it returns error message.
Denis Vlasenko68444b92008-02-22 22:24:48 +000040 */
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000041static void get_response_or_say_and_die(int fd, const char *errmsg)
Denis Vlasenko68444b92008-02-22 22:24:48 +000042{
Denis Vlasenko394eebe2008-02-25 20:30:24 +000043 ssize_t sz;
44 char buf[128];
Denis Vlasenko68444b92008-02-22 22:24:48 +000045
Denis Vlasenko394eebe2008-02-25 20:30:24 +000046 buf[0] = ' ';
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000047 sz = safe_read(fd, buf, 1);
Denis Vlasenko394eebe2008-02-25 20:30:24 +000048 if ('\0' != buf[0]) {
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000049 // request has failed
Denis Vlasenko394eebe2008-02-25 20:30:24 +000050 // try to make sure last char is '\n', but do not add
51 // superfluous one
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000052 sz = full_read(fd, buf + 1, 126);
Denis Vlasenko394eebe2008-02-25 20:30:24 +000053 bb_error_msg("error while %s%s", errmsg,
54 (sz > 0 ? ". Server said:" : ""));
55 if (sz > 0) {
56 // sz = (bytes in buf) - 1
57 if (buf[sz] != '\n')
58 buf[++sz] = '\n';
59 safe_write(STDERR_FILENO, buf, sz + 1);
60 }
Denis Vlasenko68444b92008-02-22 22:24:48 +000061 xfunc_die();
62 }
63}
64
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000065int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000066int lpqr_main(int argc UNUSED_PARAM, char *argv[])
Denis Vlasenko68444b92008-02-22 22:24:48 +000067{
Denis Vlasenko68444b92008-02-22 22:24:48 +000068 enum {
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000069 OPT_P = 1 << 0, // -P queue[@host[:port]]. If no -P is given use $PRINTER, then "lp@localhost:515"
70 OPT_U = 1 << 1, // -U username
71
72 LPR_V = 1 << 2, // -V: be verbose
Denis Vlasenko42cc3042008-03-24 02:05:58 +000073 LPR_h = 1 << 3, // -h: want banner printed
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000074 LPR_C = 1 << 4, // -C class: job "class" (? supposedly printed on banner)
75 LPR_J = 1 << 5, // -J title: the job title for the banner page
76 LPR_m = 1 << 6, // -m: send mail back to user
77
78 LPQ_SHORT_FMT = 1 << 2, // -s: short listing format
79 LPQ_DELETE = 1 << 3, // -d: delete job(s)
80 LPQ_FORCE = 1 << 4, // -f: force waiting job(s) to be printed
Denis Vlasenko68444b92008-02-22 22:24:48 +000081 };
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000082 char tempfile[sizeof("/tmp/lprXXXXXX")];
83 const char *job_title;
84 const char *printer_class = ""; // printer class, max 32 char
85 const char *queue; // name of printer queue
86 const char *server = "localhost"; // server[:port] of printer queue
87 char *hostname;
88 // N.B. IMHO getenv("USER") can be way easily spoofed!
Denis Vlasenko0c68a872008-12-02 22:56:59 +000089 const char *user = xuid2uname(getuid());
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000090 unsigned job;
91 unsigned opts;
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +000092 int fd;
Denis Vlasenko68444b92008-02-22 22:24:48 +000093
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +000094 // parse options
95 // TODO: set opt_complementary: s,d,f are mutually exclusive
96 opts = getopt32(argv,
97 (/*lp*/'r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
98 , &queue, &user
99 , &printer_class, &job_title
100 );
Denis Vlasenko68444b92008-02-22 22:24:48 +0000101 argv += optind;
Denis Vlasenko68444b92008-02-22 22:24:48 +0000102
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000103 // if queue is not specified -> use $PRINTER
104 if (!(opts & OPT_P))
105 queue = getenv("PRINTER");
106 // if queue is still not specified ->
107 if (!queue) {
108 // ... queue defaults to "lp"
109 // server defaults to "localhost"
110 queue = "lp";
111 // if queue is specified ->
112 } else {
113 // queue name is to the left of '@'
114 char *s = strchr(queue, '@');
115 if (s) {
116 // server name is to the right of '@'
117 *s = '\0';
118 server = s + 1;
119 }
Denis Vlasenko68444b92008-02-22 22:24:48 +0000120 }
121
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000122 // do connect
123 fd = create_and_connect_stream_or_die(server, 515);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000124
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000125 //
126 // LPQ ------------------------
127 //
128 if (/*lp*/'q' == applet_name[2]) {
129 char cmd;
130 // force printing of every job still in queue
131 if (opts & LPQ_FORCE) {
132 cmd = 1;
133 goto command;
134 // delete job(s)
135 } else if (opts & LPQ_DELETE) {
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000136 fdprintf(fd, "\x5" "%s %s", queue, user);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000137 while (*argv) {
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000138 fdprintf(fd, " %s", *argv++);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000139 }
140 bb_putchar('\n');
141 // dump current jobs status
142 // N.B. periodical polling should be achieved
143 // via "watch -n delay lpq"
144 // They say it's the UNIX-way :)
145 } else {
146 cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
147 command:
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000148 fdprintf(fd, "%c" "%s\n", cmd, queue);
149 bb_copyfd_eof(fd, STDOUT_FILENO);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000150 }
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000151
152 return EXIT_SUCCESS;
153 }
154
155 //
156 // LPR ------------------------
157 //
158 if (opts & LPR_V)
159 bb_error_msg("connected to server");
160
161 job = getpid() % 1000;
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000162 hostname = safe_gethostname();
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000163
164 // no files given on command line? -> use stdin
165 if (!*argv)
166 *--argv = (char *)"-";
167
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000168 fdprintf(fd, "\x2" "%s\n", queue);
169 get_response_or_say_and_die(fd, "setting queue");
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000170
171 // process files
172 do {
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000173 unsigned cflen;
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000174 int dfd;
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000175 struct stat st;
176 char *c;
177 char *remote_filename;
178 char *controlfile;
179
180 // if data file is stdin, we need to dump it first
181 if (LONE_DASH(*argv)) {
182 strcpy(tempfile, "/tmp/lprXXXXXX");
Alexander Shishkin67227372010-10-22 13:27:16 +0200183 dfd = xmkstemp(tempfile);
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000184 bb_copyfd_eof(STDIN_FILENO, dfd);
185 xlseek(dfd, 0, SEEK_SET);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000186 *argv = (char*)bb_msg_standard_input;
187 } else {
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000188 dfd = xopen(*argv, O_RDONLY);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000189 }
190
191 /* "The name ... should start with ASCII "cfA",
192 * followed by a three digit job number, followed
193 * by the host name which has constructed the file."
194 * We supply 'c' or 'd' as needed for control/data file. */
195 remote_filename = xasprintf("fA%03u%s", job, hostname);
196
197 // create control file
198 // TODO: all lines but 2 last are constants! How we can use this fact?
199 controlfile = xasprintf(
200 "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
201 "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
202 "J" "%.99s\n" /* J JOBNAME */
203 /* "class name for banner page and job name
204 * for banner page commands must precede L command" */
205 "L" "%.32s\n" /* L USER - print banner page, with given user's name */
206 "M" "%.32s\n" /* M WHOM_TO_MAIL */
207 "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
208 , hostname, user
209 , printer_class /* can be "" */
210 , ((opts & LPR_J) ? job_title : *argv)
211 , (opts & LPR_h) ? user : ""
212 , (opts & LPR_m) ? user : ""
213 , remote_filename
214 );
215 // delete possible "\nX\n" patterns
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000216 c = controlfile;
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000217 cflen = (unsigned)strlen(controlfile);
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000218 while ((c = strchr(c, '\n')) != NULL) {
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000219 if (c[1] && c[2] == '\n') {
220 /* can't use strcpy, results are undefined */
221 memmove(c, c+2, cflen - (c-controlfile) - 1);
222 cflen -= 2;
223 } else {
224 c++;
225 }
Denis Vlasenkof99afb52008-02-24 23:32:36 +0000226 }
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000227
228 // send control file
229 if (opts & LPR_V)
230 bb_error_msg("sending control file");
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000231 /* "Acknowledgement processing must occur as usual
232 * after the command is sent." */
233 fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
234 get_response_or_say_and_die(fd, "sending control file");
Denis Vlasenko68444b92008-02-22 22:24:48 +0000235 /* "Once all of the contents have
236 * been delivered, an octet of zero bits is sent as
237 * an indication that the file being sent is complete.
238 * A second level of acknowledgement processing
239 * must occur at this point." */
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000240 full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000241 get_response_or_say_and_die(fd, "sending control file");
Denis Vlasenko68444b92008-02-22 22:24:48 +0000242
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000243 // send data file, with name "dfaXXX"
244 if (opts & LPR_V)
245 bb_error_msg("sending data file");
246 st.st_size = 0; /* paranoia: fstat may theoretically fail */
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000247 fstat(dfd, &st);
248 fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000249 get_response_or_say_and_die(fd, "sending data file");
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000250 if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000251 // We're screwed. We sent less bytes than we advertised.
252 bb_error_msg_and_die("local file changed size?!");
253 }
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000254 write(fd, "", 1); // send ACK
255 get_response_or_say_and_die(fd, "sending data file");
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000256
257 // delete temporary file if we dumped stdin
258 if (*argv == (char*)bb_msg_standard_input)
259 unlink(tempfile);
260
261 // cleanup
Denis Vlasenko68444b92008-02-22 22:24:48 +0000262 close(fd);
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000263 free(remote_filename);
264 free(controlfile);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000265
Denis Vlasenkofe66a0e2008-02-27 14:35:21 +0000266 // say job accepted
267 if (opts & LPR_V)
268 bb_error_msg("job accepted");
269
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000270 // next, please!
271 job = (job + 1) % 1000;
272 } while (*++argv);
Denis Vlasenko68444b92008-02-22 22:24:48 +0000273
Denis Vlasenko4f82bdb2008-02-24 18:44:20 +0000274 return EXIT_SUCCESS;
Denis Vlasenko68444b92008-02-22 22:24:48 +0000275}