2008-02-25 14:48:15 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2008-02-25 20:30:24 +00:00
|
|
|
* micro lpd
|
2008-02-25 14:48:15 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
|
|
|
*/
|
2008-03-23 23:40:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A typical usage of BB lpd looks as follows:
|
2008-03-24 21:19:51 +00:00
|
|
|
* # tcpsvd -E 0 515 lpd [SPOOLDIR] [HELPER-PROG [ARGS...]]
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-24 21:19:51 +00:00
|
|
|
* This starts TCP listener on port 515 (default for LP protocol).
|
|
|
|
* When a client connection is made (via lpr) lpd first changes its
|
|
|
|
* working directory to SPOOLDIR (current dir is the default).
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
|
|
|
* SPOOLDIR is the spool directory which contains printing queues
|
2008-03-23 23:40:18 +00:00
|
|
|
* and should have the following structure:
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-23 23:40:18 +00:00
|
|
|
* SPOOLDIR/
|
2008-03-24 02:18:03 +00:00
|
|
|
* <queue1>
|
|
|
|
* ...
|
|
|
|
* <queueN>
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-23 23:40:18 +00:00
|
|
|
* <queueX> can be of two types:
|
2008-03-24 21:19:51 +00:00
|
|
|
* A. a printer character device, an ordinary file or a link to such;
|
2008-03-24 02:18:03 +00:00
|
|
|
* B. a directory.
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
|
|
|
* In case A lpd just dumps the data it receives from client (lpr) to the
|
2008-03-23 23:40:18 +00:00
|
|
|
* end of queue file/device. This is non-spooling mode.
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-24 21:19:51 +00:00
|
|
|
* In case B lpd enters spooling mode. It reliably saves client data along
|
|
|
|
* with control info in two unique files under the queue directory. These
|
|
|
|
* files are named dfAXXXHHHH and cfAXXXHHHH, where XXX is the job number
|
|
|
|
* and HHHH is the client hostname. Unless a printing helper application
|
2008-03-23 23:40:18 +00:00
|
|
|
* is specified lpd is done at this point.
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-24 21:19:51 +00:00
|
|
|
* NB: file names are produced by peer! They actually may be anything at all.
|
2008-03-24 02:05:58 +00:00
|
|
|
* lpd only sanitizes them (by removing most non-alphanumerics).
|
|
|
|
*
|
2008-03-24 21:19:51 +00:00
|
|
|
* If HELPER-PROG (with optional arguments) is specified then lpd continues
|
|
|
|
* to process client data:
|
|
|
|
* 1. it reads and parses control file (cfA...). The parse process
|
|
|
|
* results in setting environment variables whose values were passed
|
|
|
|
* in control file; when parsing is complete, lpd deletes control file.
|
|
|
|
* 2. it spawns specified helper application. It is then
|
|
|
|
* the helper application who is responsible for both actual printing
|
|
|
|
* and deleting of processed data file.
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-24 21:19:51 +00:00
|
|
|
* A good lpr passes control files which when parsed provides the following
|
|
|
|
* variables:
|
2008-03-23 23:40:18 +00:00
|
|
|
* $H = host which issues the job
|
|
|
|
* $P = user who prints
|
|
|
|
* $C = class of printing (what is printed on banner page)
|
|
|
|
* $J = the name of the job
|
|
|
|
* $L = print banner page
|
|
|
|
* $M = the user to whom a mail should be sent if a problem occurs
|
2008-03-24 21:19:51 +00:00
|
|
|
*
|
|
|
|
* We specifically filter out and NOT provide:
|
2008-03-23 23:40:18 +00:00
|
|
|
* $l = name of datafile ("dfAxxx") - file whose content are to be printed
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-24 21:19:51 +00:00
|
|
|
* lpd provides $DATAFILE instead - the ACTUAL name
|
2008-03-24 02:05:58 +00:00
|
|
|
* of the datafile under which it was saved.
|
2008-03-24 21:19:51 +00:00
|
|
|
* $l would be not reliable (you would be at mercy of remote peer).
|
2008-03-24 02:05:58 +00:00
|
|
|
*
|
2008-03-23 23:40:18 +00:00
|
|
|
* Thus, a typical helper can be something like this:
|
|
|
|
* #!/bin/sh
|
2008-03-24 21:19:51 +00:00
|
|
|
* cat ./"$DATAFILE" >/dev/lp0
|
|
|
|
* mv -f ./"$DATAFILE" save/
|
2008-03-23 23:40:18 +00:00
|
|
|
*/
|
2008-03-24 02:05:58 +00:00
|
|
|
|
2008-02-25 14:48:15 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-03-23 23:40:18 +00:00
|
|
|
// strip argument of bad chars
|
|
|
|
static char *sane(char *str)
|
|
|
|
{
|
|
|
|
char *s = str;
|
|
|
|
char *p = s;
|
|
|
|
while (*s) {
|
2008-03-24 01:52:52 +00:00
|
|
|
if (isalnum(*s) || '-' == *s || '_' == *s) {
|
2008-03-23 23:40:18 +00:00
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-03-24 00:04:42 +00:00
|
|
|
static char *xmalloc_read_stdin(void)
|
|
|
|
{
|
2008-03-24 01:52:52 +00:00
|
|
|
// SECURITY:
|
|
|
|
size_t max = 4 * 1024; // more than enough for commands!
|
2008-03-24 00:04:42 +00:00
|
|
|
return xmalloc_reads(STDIN_FILENO, NULL, &max);
|
|
|
|
}
|
|
|
|
|
2008-02-25 14:48:15 +00:00
|
|
|
int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
|
2008-03-17 09:00:54 +00:00
|
|
|
int lpd_main(int argc ATTRIBUTE_UNUSED, char *argv[])
|
2008-02-25 14:48:15 +00:00
|
|
|
{
|
2008-03-24 01:52:52 +00:00
|
|
|
int spooling = spooling; // for compiler
|
2008-02-25 20:30:24 +00:00
|
|
|
char *s, *queue;
|
2008-03-24 01:52:52 +00:00
|
|
|
char *filenames[2];
|
2008-02-25 20:30:24 +00:00
|
|
|
|
2008-03-24 01:52:52 +00:00
|
|
|
// goto spool directory
|
|
|
|
if (*++argv)
|
|
|
|
xchdir(*argv++);
|
|
|
|
|
|
|
|
// error messages of xfuncs will be sent over network
|
|
|
|
xdup2(STDOUT_FILENO, STDERR_FILENO);
|
2008-02-25 20:30:24 +00:00
|
|
|
|
2008-03-24 21:19:51 +00:00
|
|
|
// nullify ctrl/data filenames
|
|
|
|
memset(filenames, 0, sizeof(filenames));
|
2008-03-24 01:52:52 +00:00
|
|
|
|
|
|
|
// read command
|
|
|
|
s = queue = xmalloc_read_stdin();
|
2008-02-25 20:30:24 +00:00
|
|
|
// we understand only "receive job" command
|
2008-03-24 01:52:52 +00:00
|
|
|
if (2 != *queue) {
|
2008-02-25 20:30:24 +00:00
|
|
|
unsupported_cmd:
|
|
|
|
printf("Command %02x %s\n",
|
|
|
|
(unsigned char)s[0], "is not supported");
|
2008-03-24 01:52:52 +00:00
|
|
|
goto err_exit;
|
2008-02-25 20:30:24 +00:00
|
|
|
}
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-03-24 01:52:52 +00:00
|
|
|
// parse command: "2 | QUEUE_NAME | '\n'"
|
|
|
|
queue++;
|
2008-02-25 20:30:24 +00:00
|
|
|
// protect against "/../" attacks
|
2008-03-24 01:52:52 +00:00
|
|
|
// *strchrnul(queue, '\n') = '\0'; - redundant, sane() will do
|
2008-03-23 23:40:18 +00:00
|
|
|
if (!*sane(queue))
|
2008-02-25 20:30:24 +00:00
|
|
|
return EXIT_FAILURE;
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
// queue is a directory -> chdir to it and enter spooling mode
|
2008-03-24 01:52:52 +00:00
|
|
|
spooling = chdir(queue) + 1; // 0: cannot chdir, 1: done
|
2008-03-24 21:19:51 +00:00
|
|
|
// we don't free(s), we might need "queue" var later
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
while (1) {
|
|
|
|
char *fname;
|
|
|
|
int fd;
|
|
|
|
// int is easier than ssize_t: can use xatoi_u,
|
|
|
|
// and can correctly display error returns (-1)
|
|
|
|
int expected_len, real_len;
|
|
|
|
|
|
|
|
// signal OK
|
2008-03-24 01:52:52 +00:00
|
|
|
safe_write(STDOUT_FILENO, "", 1);
|
2008-02-25 20:30:24 +00:00
|
|
|
|
|
|
|
// get subcommand
|
2008-03-24 01:52:52 +00:00
|
|
|
// valid s must be of form: "SUBCMD | LEN | space | FNAME"
|
|
|
|
// N.B. we bail out on any error
|
2008-03-24 00:04:42 +00:00
|
|
|
s = xmalloc_read_stdin();
|
2008-03-24 01:52:52 +00:00
|
|
|
if (!s) { // (probably) EOF
|
2008-03-25 06:03:19 +00:00
|
|
|
char *p, *q, var[2];
|
|
|
|
|
|
|
|
// non-spooling mode or no spool helper specified
|
|
|
|
if (!spooling || !*argv)
|
|
|
|
return EXIT_SUCCESS; // the only non-error exit
|
|
|
|
// spooling mode but we didn't see both ctrlfile & datafile
|
|
|
|
if (spooling != 7)
|
|
|
|
goto err_exit; // reject job
|
|
|
|
|
|
|
|
// spooling mode and spool helper specified -> exec spool helper
|
|
|
|
// (we exit 127 if helper cannot be executed)
|
|
|
|
var[1] = '\0';
|
|
|
|
// read and delete ctrlfile
|
2008-04-19 19:32:08 +00:00
|
|
|
q = xmalloc_xopen_read_close(filenames[0], NULL);
|
2008-03-25 06:03:19 +00:00
|
|
|
unlink(filenames[0]);
|
|
|
|
// provide datafile name
|
|
|
|
// we can use leaky setenv since we are about to exec or exit
|
|
|
|
xsetenv("DATAFILE", filenames[1]);
|
|
|
|
// parse control file by "\n"
|
|
|
|
while ((p = strchr(q, '\n')) != NULL && isalpha(*q)) {
|
|
|
|
*p++ = '\0';
|
|
|
|
// q is a line of <SYM><VALUE>,
|
|
|
|
// we are setting environment string <SYM>=<VALUE>.
|
|
|
|
// Ignoring "l<datafile>", exporting others:
|
|
|
|
if (*q != 'l') {
|
|
|
|
var[0] = *q++;
|
|
|
|
xsetenv(var, q);
|
|
|
|
}
|
|
|
|
q = p; // next line
|
2008-03-24 01:52:52 +00:00
|
|
|
}
|
2008-03-25 06:03:19 +00:00
|
|
|
// helper should not talk over network.
|
|
|
|
// this call reopens stdio fds to "/dev/null"
|
|
|
|
// (no daemonization is done)
|
|
|
|
bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO | DAEMON_ONLY_SANITIZE, NULL);
|
|
|
|
BB_EXECVP(*argv, argv);
|
|
|
|
exit(127);
|
2008-03-24 01:52:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate input.
|
2008-02-25 20:30:24 +00:00
|
|
|
// we understand only "control file" or "data file" cmds
|
|
|
|
if (2 != s[0] && 3 != s[0])
|
|
|
|
goto unsupported_cmd;
|
2008-03-24 21:19:51 +00:00
|
|
|
if (spooling & (1 << (s[0]-1))) {
|
2008-03-24 01:52:52 +00:00
|
|
|
printf("Duplicated subcommand\n");
|
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
// get filename
|
2008-02-25 20:30:24 +00:00
|
|
|
*strchrnul(s, '\n') = '\0';
|
|
|
|
fname = strchr(s, ' ');
|
|
|
|
if (!fname) {
|
2008-03-24 01:52:52 +00:00
|
|
|
// bad_fname:
|
|
|
|
printf("No or bad filename\n");
|
|
|
|
goto err_exit;
|
2008-02-25 20:30:24 +00:00
|
|
|
}
|
|
|
|
*fname++ = '\0';
|
2008-03-24 01:52:52 +00:00
|
|
|
// // s[0]==2: ctrlfile, must start with 'c'
|
|
|
|
// // s[0]==3: datafile, must start with 'd'
|
|
|
|
// if (fname[0] != s[0] + ('c'-2))
|
|
|
|
// goto bad_fname;
|
|
|
|
// get length
|
|
|
|
expected_len = bb_strtou(s + 1, NULL, 10);
|
|
|
|
if (errno || expected_len < 0) {
|
|
|
|
printf("Bad length\n");
|
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
if (2 == s[0] && expected_len > 16 * 1024) {
|
|
|
|
// SECURITY:
|
|
|
|
// ctrlfile can't be big (we want to read it back later!)
|
|
|
|
printf("File is too big\n");
|
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open the file
|
2008-02-25 20:30:24 +00:00
|
|
|
if (spooling) {
|
|
|
|
// spooling mode: dump both files
|
|
|
|
// job in flight has mode 0200 "only writable"
|
2008-03-24 01:52:52 +00:00
|
|
|
sane(fname);
|
|
|
|
fd = open3_or_warn(fname, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0200);
|
|
|
|
if (fd < 0)
|
|
|
|
goto err_exit;
|
|
|
|
filenames[s[0] - 2] = xstrdup(fname);
|
2008-02-25 20:30:24 +00:00
|
|
|
} else {
|
|
|
|
// non-spooling mode:
|
|
|
|
// 2: control file (ignoring), 3: data file
|
|
|
|
fd = -1;
|
|
|
|
if (3 == s[0])
|
|
|
|
fd = xopen(queue, O_RDWR | O_APPEND);
|
|
|
|
}
|
2008-03-24 01:52:52 +00:00
|
|
|
|
|
|
|
// copy the file
|
2008-02-25 20:30:24 +00:00
|
|
|
real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len);
|
2008-03-24 01:52:52 +00:00
|
|
|
if (real_len != expected_len) {
|
2008-02-25 20:30:24 +00:00
|
|
|
printf("Expected %d but got %d bytes\n",
|
|
|
|
expected_len, real_len);
|
2008-03-24 01:52:52 +00:00
|
|
|
goto err_exit;
|
2008-02-25 20:30:24 +00:00
|
|
|
}
|
2008-03-24 01:52:52 +00:00
|
|
|
// get ACK and see whether it is NUL (ok)
|
2008-03-24 21:19:51 +00:00
|
|
|
// (and don't trash s[0]!)
|
|
|
|
if (safe_read(STDIN_FILENO, &s[1], 1) != 1 || s[1] != 0) {
|
2008-03-24 01:52:52 +00:00
|
|
|
// don't send error msg to peer - it obviously
|
2008-03-24 21:19:51 +00:00
|
|
|
// doesn't follow the protocol, so probably
|
2008-03-24 01:52:52 +00:00
|
|
|
// it can't understand us either
|
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
|
2008-03-23 23:40:18 +00:00
|
|
|
if (spooling) {
|
2008-03-24 01:52:52 +00:00
|
|
|
// chmod completely downloaded file as "readable+writable"
|
2008-03-23 23:40:18 +00:00
|
|
|
fchmod(fd, 0600);
|
2008-03-24 01:52:52 +00:00
|
|
|
// accumulate dump state
|
2008-03-24 21:19:51 +00:00
|
|
|
// N.B. after all files are dumped spooling should be 1+2+4==7
|
|
|
|
spooling |= (1 << (s[0]-1)); // bit 1: ctrlfile; bit 2: datafile
|
2008-03-23 23:40:18 +00:00
|
|
|
}
|
2008-03-24 21:19:51 +00:00
|
|
|
|
2008-03-24 01:52:52 +00:00
|
|
|
free(s);
|
2008-03-23 23:40:18 +00:00
|
|
|
close(fd); // NB: can do close(-1). Who cares?
|
2008-03-24 01:52:52 +00:00
|
|
|
} // while (1)
|
|
|
|
|
|
|
|
err_exit:
|
|
|
|
// don't keep corrupted files
|
|
|
|
if (spooling) {
|
2008-03-24 21:19:51 +00:00
|
|
|
#define i spooling
|
|
|
|
for (i = 2; --i >= 0; )
|
|
|
|
if (filenames[i])
|
|
|
|
unlink(filenames[i]);
|
2008-03-24 01:52:52 +00:00
|
|
|
}
|
|
|
|
return EXIT_FAILURE;
|
2008-02-25 14:48:15 +00:00
|
|
|
}
|