blob: 88239343681cbd70347eee566d88b7c7118eabcd [file] [log] [blame]
Denis Vlasenko38b88312008-02-25 14:48:15 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko394eebe2008-02-25 20:30:24 +00003 * micro lpd
Denis Vlasenko38b88312008-02-25 14:48:15 +00004 *
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko38b88312008-02-25 14:48:15 +00008 */
Denis Vlasenkoa7942892008-03-23 23:40:18 +00009
10/*
11 * A typical usage of BB lpd looks as follows:
Denis Vlasenkof39653e2008-03-24 21:19:51 +000012 * # tcpsvd -E 0 515 lpd [SPOOLDIR] [HELPER-PROG [ARGS...]]
Denis Vlasenko42cc3042008-03-24 02:05:58 +000013 *
Denis Vlasenkof39653e2008-03-24 21:19:51 +000014 * This starts TCP listener on port 515 (default for LP protocol).
15 * When a client connection is made (via lpr) lpd first changes its
16 * working directory to SPOOLDIR (current dir is the default).
Denis Vlasenko42cc3042008-03-24 02:05:58 +000017 *
18 * SPOOLDIR is the spool directory which contains printing queues
Denis Vlasenkoa7942892008-03-23 23:40:18 +000019 * and should have the following structure:
Denis Vlasenko42cc3042008-03-24 02:05:58 +000020 *
Denis Vlasenkoa7942892008-03-23 23:40:18 +000021 * SPOOLDIR/
Denis Vlasenkoc6938402008-03-24 02:18:03 +000022 * <queue1>
23 * ...
24 * <queueN>
Denis Vlasenko42cc3042008-03-24 02:05:58 +000025 *
Denis Vlasenkoa7942892008-03-23 23:40:18 +000026 * <queueX> can be of two types:
Denis Vlasenkof39653e2008-03-24 21:19:51 +000027 * A. a printer character device, an ordinary file or a link to such;
Denis Vlasenkoc6938402008-03-24 02:18:03 +000028 * B. a directory.
Denis Vlasenko42cc3042008-03-24 02:05:58 +000029 *
30 * In case A lpd just dumps the data it receives from client (lpr) to the
Denis Vlasenkoa7942892008-03-23 23:40:18 +000031 * end of queue file/device. This is non-spooling mode.
Denis Vlasenko42cc3042008-03-24 02:05:58 +000032 *
Denis Vlasenkof39653e2008-03-24 21:19:51 +000033 * In case B lpd enters spooling mode. It reliably saves client data along
34 * with control info in two unique files under the queue directory. These
35 * files are named dfAXXXHHHH and cfAXXXHHHH, where XXX is the job number
36 * and HHHH is the client hostname. Unless a printing helper application
Denis Vlasenkoa7942892008-03-23 23:40:18 +000037 * is specified lpd is done at this point.
Denis Vlasenko42cc3042008-03-24 02:05:58 +000038 *
Denis Vlasenkof39653e2008-03-24 21:19:51 +000039 * NB: file names are produced by peer! They actually may be anything at all.
Denis Vlasenko42cc3042008-03-24 02:05:58 +000040 * lpd only sanitizes them (by removing most non-alphanumerics).
41 *
Denis Vlasenkof39653e2008-03-24 21:19:51 +000042 * If HELPER-PROG (with optional arguments) is specified then lpd continues
43 * to process client data:
44 * 1. it reads and parses control file (cfA...). The parse process
45 * results in setting environment variables whose values were passed
46 * in control file; when parsing is complete, lpd deletes control file.
47 * 2. it spawns specified helper application. It is then
48 * the helper application who is responsible for both actual printing
49 * and deleting of processed data file.
Denis Vlasenko42cc3042008-03-24 02:05:58 +000050 *
Denis Vlasenkof39653e2008-03-24 21:19:51 +000051 * A good lpr passes control files which when parsed provides the following
52 * variables:
Denis Vlasenkoa7942892008-03-23 23:40:18 +000053 * $H = host which issues the job
54 * $P = user who prints
55 * $C = class of printing (what is printed on banner page)
56 * $J = the name of the job
57 * $L = print banner page
58 * $M = the user to whom a mail should be sent if a problem occurs
Denis Vlasenkof39653e2008-03-24 21:19:51 +000059 *
60 * We specifically filter out and NOT provide:
Denis Vlasenkoa7942892008-03-23 23:40:18 +000061 * $l = name of datafile ("dfAxxx") - file whose content are to be printed
Denis Vlasenko42cc3042008-03-24 02:05:58 +000062 *
Denis Vlasenkof39653e2008-03-24 21:19:51 +000063 * lpd provides $DATAFILE instead - the ACTUAL name
Denis Vlasenko42cc3042008-03-24 02:05:58 +000064 * of the datafile under which it was saved.
Denis Vlasenkof39653e2008-03-24 21:19:51 +000065 * $l would be not reliable (you would be at mercy of remote peer).
Denis Vlasenko42cc3042008-03-24 02:05:58 +000066 *
Denis Vlasenkoa7942892008-03-23 23:40:18 +000067 * Thus, a typical helper can be something like this:
68 * #!/bin/sh
Denis Vlasenkof39653e2008-03-24 21:19:51 +000069 * cat ./"$DATAFILE" >/dev/lp0
70 * mv -f ./"$DATAFILE" save/
Denis Vlasenkoa7942892008-03-23 23:40:18 +000071 */
Denys Vlasenko854bb682015-10-19 00:45:46 +020072//config:config LPD
73//config: bool "lpd"
74//config: default y
75//config: help
76//config: lpd is a print spooling daemon.
77
78//applet:IF_LPD(APPLET(lpd, BB_DIR_USR_SBIN, BB_SUID_DROP))
79
80//kbuild:lib-$(CONFIG_LPD) += lpd.o
Denis Vlasenko42cc3042008-03-24 02:05:58 +000081
Pere Orga5bc8c002011-04-11 03:29:49 +020082//usage:#define lpd_trivial_usage
83//usage: "SPOOLDIR [HELPER [ARGS]]"
84//usage:#define lpd_full_usage "\n\n"
85//usage: "SPOOLDIR must contain (symlinks to) device nodes or directories"
86//usage: "\nwith names matching print queue names. In the first case, jobs are"
87//usage: "\nsent directly to the device. Otherwise each job is stored in queue"
88//usage: "\ndirectory and HELPER program is called. Name of file to print"
89//usage: "\nis passed in $DATAFILE variable."
90//usage: "\nExample:"
91//usage: "\n tcpsvd -E 0 515 softlimit -m 999999 lpd /var/spool ./print"
92
Denis Vlasenko38b88312008-02-25 14:48:15 +000093#include "libbb.h"
94
Denis Vlasenkoa7942892008-03-23 23:40:18 +000095// strip argument of bad chars
96static char *sane(char *str)
97{
98 char *s = str;
99 char *p = s;
100 while (*s) {
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000101 if (isalnum(*s) || '-' == *s || '_' == *s) {
Denis Vlasenkoa7942892008-03-23 23:40:18 +0000102 *p++ = *s;
103 }
104 s++;
105 }
106 *p = '\0';
107 return str;
108}
109
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +0000110static char *xmalloc_read_stdin(void)
111{
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000112 // SECURITY:
113 size_t max = 4 * 1024; // more than enough for commands!
Denys Vlasenko80c5b682011-05-08 21:21:10 +0200114 return xmalloc_reads(STDIN_FILENO, &max);
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +0000115}
116
Denis Vlasenko38b88312008-02-25 14:48:15 +0000117int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000118int lpd_main(int argc UNUSED_PARAM, char *argv[])
Denis Vlasenko38b88312008-02-25 14:48:15 +0000119{
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000120 int spooling = spooling; // for compiler
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000121 char *s, *queue;
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000122 char *filenames[2];
Denis Vlasenko38b88312008-02-25 14:48:15 +0000123
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000124 // goto spool directory
Denis Vlasenkoa7942892008-03-23 23:40:18 +0000125 if (*++argv)
126 xchdir(*argv++);
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000127
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000128 // error messages of xfuncs will be sent over network
129 xdup2(STDOUT_FILENO, STDERR_FILENO);
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000130
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000131 // nullify ctrl/data filenames
132 memset(filenames, 0, sizeof(filenames));
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000133
134 // read command
135 s = queue = xmalloc_read_stdin();
136 // we understand only "receive job" command
137 if (2 != *queue) {
138 unsupported_cmd:
139 printf("Command %02x %s\n",
140 (unsigned char)s[0], "is not supported");
141 goto err_exit;
142 }
143
144 // parse command: "2 | QUEUE_NAME | '\n'"
145 queue++;
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000146 // protect against "/../" attacks
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000147 // *strchrnul(queue, '\n') = '\0'; - redundant, sane() will do
Denis Vlasenkoa7942892008-03-23 23:40:18 +0000148 if (!*sane(queue))
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000149 return EXIT_FAILURE;
150
151 // queue is a directory -> chdir to it and enter spooling mode
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000152 spooling = chdir(queue) + 1; // 0: cannot chdir, 1: done
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000153 // we don't free(s), we might need "queue" var later
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000154
155 while (1) {
156 char *fname;
157 int fd;
Denys Vlasenko77832482010-08-12 14:14:45 +0200158 // int is easier than ssize_t: can use xatoi_positive,
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000159 // and can correctly display error returns (-1)
160 int expected_len, real_len;
161
162 // signal OK
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000163 safe_write(STDOUT_FILENO, "", 1);
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000164
165 // get subcommand
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000166 // valid s must be of form: "SUBCMD | LEN | space | FNAME"
167 // N.B. we bail out on any error
Denis Vlasenko0b6c6a92008-03-24 00:04:42 +0000168 s = xmalloc_read_stdin();
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000169 if (!s) { // (probably) EOF
Denis Vlasenko0c3b0d52008-03-25 06:03:19 +0000170 char *p, *q, var[2];
171
172 // non-spooling mode or no spool helper specified
173 if (!spooling || !*argv)
174 return EXIT_SUCCESS; // the only non-error exit
175 // spooling mode but we didn't see both ctrlfile & datafile
176 if (spooling != 7)
177 goto err_exit; // reject job
178
179 // spooling mode and spool helper specified -> exec spool helper
180 // (we exit 127 if helper cannot be executed)
181 var[1] = '\0';
182 // read and delete ctrlfile
Denis Vlasenkof3745ea2008-04-19 19:32:08 +0000183 q = xmalloc_xopen_read_close(filenames[0], NULL);
Denis Vlasenko0c3b0d52008-03-25 06:03:19 +0000184 unlink(filenames[0]);
185 // provide datafile name
186 // we can use leaky setenv since we are about to exec or exit
187 xsetenv("DATAFILE", filenames[1]);
188 // parse control file by "\n"
189 while ((p = strchr(q, '\n')) != NULL && isalpha(*q)) {
190 *p++ = '\0';
191 // q is a line of <SYM><VALUE>,
192 // we are setting environment string <SYM>=<VALUE>.
193 // Ignoring "l<datafile>", exporting others:
194 if (*q != 'l') {
195 var[0] = *q++;
196 xsetenv(var, q);
197 }
198 q = p; // next line
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000199 }
Denis Vlasenko0c3b0d52008-03-25 06:03:19 +0000200 // helper should not talk over network.
201 // this call reopens stdio fds to "/dev/null"
202 // (no daemonization is done)
203 bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO | DAEMON_ONLY_SANITIZE, NULL);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200204 BB_EXECVP_or_die(argv);
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000205 }
206
207 // validate input.
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000208 // we understand only "control file" or "data file" cmds
209 if (2 != s[0] && 3 != s[0])
210 goto unsupported_cmd;
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000211 if (spooling & (1 << (s[0]-1))) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200212 puts("Duplicated subcommand");
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000213 goto err_exit;
214 }
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000215 // get filename
Ron Yorston8ec1ff32015-03-12 20:18:51 +0100216 chomp(s);
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000217 fname = strchr(s, ' ');
218 if (!fname) {
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000219// bad_fname:
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200220 puts("No or bad filename");
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000221 goto err_exit;
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000222 }
223 *fname++ = '\0';
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000224// // s[0]==2: ctrlfile, must start with 'c'
225// // s[0]==3: datafile, must start with 'd'
226// if (fname[0] != s[0] + ('c'-2))
227// goto bad_fname;
228 // get length
229 expected_len = bb_strtou(s + 1, NULL, 10);
230 if (errno || expected_len < 0) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200231 puts("Bad length");
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000232 goto err_exit;
233 }
234 if (2 == s[0] && expected_len > 16 * 1024) {
235 // SECURITY:
236 // ctrlfile can't be big (we want to read it back later!)
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200237 puts("File is too big");
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000238 goto err_exit;
239 }
240
241 // open the file
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000242 if (spooling) {
243 // spooling mode: dump both files
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000244 // job in flight has mode 0200 "only writable"
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000245 sane(fname);
246 fd = open3_or_warn(fname, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0200);
247 if (fd < 0)
248 goto err_exit;
249 filenames[s[0] - 2] = xstrdup(fname);
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000250 } else {
251 // non-spooling mode:
252 // 2: control file (ignoring), 3: data file
253 fd = -1;
254 if (3 == s[0])
255 fd = xopen(queue, O_RDWR | O_APPEND);
256 }
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000257
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000258 // signal OK
259 safe_write(STDOUT_FILENO, "", 1);
260
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000261 // copy the file
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000262 real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len);
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000263 if (real_len != expected_len) {
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000264 printf("Expected %d but got %d bytes\n",
265 expected_len, real_len);
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000266 goto err_exit;
Denis Vlasenkoa7942892008-03-23 23:40:18 +0000267 }
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000268 // get EOF indicator, see whether it is NUL (ok)
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000269 // (and don't trash s[0]!)
270 if (safe_read(STDIN_FILENO, &s[1], 1) != 1 || s[1] != 0) {
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000271 // don't send error msg to peer - it obviously
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000272 // doesn't follow the protocol, so probably
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000273 // it can't understand us either
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000274 goto err_exit;
275 }
276
277 if (spooling) {
278 // chmod completely downloaded file as "readable+writable"
279 fchmod(fd, 0600);
280 // accumulate dump state
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000281 // N.B. after all files are dumped spooling should be 1+2+4==7
282 spooling |= (1 << (s[0]-1)); // bit 1: ctrlfile; bit 2: datafile
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000283 }
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000284
Denis Vlasenko394eebe2008-02-25 20:30:24 +0000285 free(s);
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000286 close(fd); // NB: can do close(-1). Who cares?
Denis Vlasenkoc4457582008-07-22 19:15:30 +0000287
288 // NB: don't do "signal OK" write here, it will be done
289 // at the top of the loop
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000290 } // while (1)
291
292 err_exit:
293 // don't keep corrupted files
294 if (spooling) {
Denis Vlasenkof39653e2008-03-24 21:19:51 +0000295#define i spooling
296 for (i = 2; --i >= 0; )
297 if (filenames[i])
298 unlink(filenames[i]);
Denis Vlasenko39c77c32008-03-24 01:52:52 +0000299 }
300 return EXIT_FAILURE;
Denis Vlasenko38b88312008-02-25 14:48:15 +0000301}