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:
|
|
|
|
* # tcpsvd -E 0 515 lpd SPOOLDIR [HELPER-PROG [ARGS...]]
|
|
|
|
*
|
|
|
|
* This means a network listener is started on port 515 (default for LP protocol).
|
|
|
|
* When a client connection is made (via lpr) lpd first change its working directory to SPOOLDIR.
|
|
|
|
*
|
|
|
|
* SPOOLDIR is the spool directory which contains printing queues
|
|
|
|
* and should have the following structure:
|
|
|
|
*
|
|
|
|
* SPOOLDIR/
|
|
|
|
* <queue1>
|
|
|
|
* ...
|
|
|
|
* <queueN>
|
|
|
|
*
|
|
|
|
* <queueX> can be of two types:
|
|
|
|
* A. a printer character device or an ordinary file a link to such;
|
|
|
|
* B. a directory.
|
|
|
|
*
|
|
|
|
* In case A lpd just dumps the data it receives from client (lpr) to the
|
|
|
|
* end of queue file/device. This is non-spooling mode.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* is specified lpd is done at this point.
|
|
|
|
*
|
|
|
|
* 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 processed data file.
|
|
|
|
*
|
|
|
|
* A good lpr passes control files which when parsed provide the following variables:
|
|
|
|
* $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
|
|
|
|
* $l = name of datafile ("dfAxxx") - file whose content are to be printed
|
|
|
|
*
|
|
|
|
* Thus, a typical helper can be something like this:
|
|
|
|
* #!/bin/sh
|
|
|
|
* cat "$l" >/dev/lp0
|
|
|
|
* mv -f "$l" save/
|
|
|
|
*
|
|
|
|
*/
|
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) {
|
|
|
|
if (isalnum(*s) || '-' == *s) {
|
|
|
|
*p++ = *s;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-03-24 00:04:42 +00:00
|
|
|
/* vfork() disables some optimizations. Moving its use
|
|
|
|
* to minimal, non-inlined function saves bytes */
|
|
|
|
static NOINLINE void vfork_close_stdio_and_exec(char **argv)
|
|
|
|
{
|
|
|
|
if (vfork() == 0) {
|
|
|
|
// CHILD
|
|
|
|
// we are the helper. we wanna be silent.
|
|
|
|
// 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-23 23:40:18 +00:00
|
|
|
static void exec_helper(const char *fname, char **argv)
|
|
|
|
{
|
|
|
|
char *p, *q, *file;
|
|
|
|
char *our_env[12];
|
|
|
|
int env_idx;
|
|
|
|
|
|
|
|
// read control file
|
|
|
|
file = q = xmalloc_open_read_close(fname, NULL);
|
|
|
|
// delete control file
|
|
|
|
unlink(fname);
|
|
|
|
// parse control file by "\n"
|
|
|
|
env_idx = 0;
|
|
|
|
while ((p = strchr(q, '\n')) != NULL
|
|
|
|
&& isalpha(*q)
|
|
|
|
&& env_idx < ARRAY_SIZE(our_env)
|
|
|
|
) {
|
|
|
|
*p++ = '\0';
|
|
|
|
// here q is a line of <SYM><VALUE>
|
|
|
|
// let us set environment string <SYM>=<VALUE>
|
|
|
|
// N.B. setenv is leaky!
|
|
|
|
// We have to use putenv(malloced_str),
|
|
|
|
// and unsetenv+free (in parent)
|
|
|
|
our_env[env_idx] = xasprintf("%c=%s", *q, q+1);
|
|
|
|
putenv(our_env[env_idx]);
|
|
|
|
env_idx++;
|
|
|
|
// next line, plz!
|
|
|
|
q = p;
|
|
|
|
}
|
2008-03-24 00:04:42 +00:00
|
|
|
free(file);
|
2008-03-23 23:40:18 +00:00
|
|
|
|
2008-03-24 00:04:42 +00:00
|
|
|
vfork_close_stdio_and_exec(argv);
|
2008-03-23 23:40:18 +00:00
|
|
|
|
|
|
|
// PARENT (or vfork error)
|
|
|
|
// clean up...
|
|
|
|
while (--env_idx >= 0) {
|
|
|
|
*strchrnul(our_env[env_idx], '=') = '\0';
|
|
|
|
unsetenv(our_env[env_idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-24 00:04:42 +00:00
|
|
|
static char *xmalloc_read_stdin(void)
|
|
|
|
{
|
|
|
|
size_t max = 4 * 1024; /* more than enough for commands! */
|
|
|
|
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-02-25 20:30:24 +00:00
|
|
|
int spooling;
|
|
|
|
char *s, *queue;
|
|
|
|
|
|
|
|
// read command
|
2008-03-24 00:04:42 +00:00
|
|
|
s = xmalloc_read_stdin();
|
2008-02-25 20:30:24 +00:00
|
|
|
|
|
|
|
// we understand only "receive job" command
|
|
|
|
if (2 != *s) {
|
|
|
|
unsupported_cmd:
|
|
|
|
printf("Command %02x %s\n",
|
|
|
|
(unsigned char)s[0], "is not supported");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
// goto spool directory
|
2008-03-23 23:40:18 +00:00
|
|
|
if (*++argv)
|
|
|
|
xchdir(*argv++);
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
// parse command: "\x2QUEUE_NAME\n"
|
|
|
|
queue = s + 1;
|
|
|
|
*strchrnul(s, '\n') = '\0';
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
// protect against "/../" attacks
|
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
|
|
|
|
spooling = chdir(queue) + 1; /* 0: cannot chdir, 1: done */
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
xdup2(STDOUT_FILENO, STDERR_FILENO);
|
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
|
|
|
|
write(STDOUT_FILENO, "", 1);
|
|
|
|
|
|
|
|
// get subcommand
|
2008-03-24 00:04:42 +00:00
|
|
|
s = xmalloc_read_stdin();
|
2008-02-25 20:30:24 +00:00
|
|
|
if (!s)
|
|
|
|
return EXIT_SUCCESS; // probably EOF
|
|
|
|
// we understand only "control file" or "data file" cmds
|
|
|
|
if (2 != s[0] && 3 != s[0])
|
|
|
|
goto unsupported_cmd;
|
2008-02-25 14:48:15 +00:00
|
|
|
|
2008-02-25 20:30:24 +00:00
|
|
|
*strchrnul(s, '\n') = '\0';
|
|
|
|
// valid s must be of form: SUBCMD | LEN | SP | FNAME
|
|
|
|
// N.B. we bail out on any error
|
|
|
|
fname = strchr(s, ' ');
|
|
|
|
if (!fname) {
|
|
|
|
printf("Command %02x %s\n",
|
|
|
|
(unsigned char)s[0], "lacks filename");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
*fname++ = '\0';
|
|
|
|
if (spooling) {
|
|
|
|
// spooling mode: dump both files
|
|
|
|
// job in flight has mode 0200 "only writable"
|
2008-03-23 23:40:18 +00:00
|
|
|
fd = xopen3(sane(fname), O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0200);
|
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);
|
|
|
|
}
|
|
|
|
expected_len = xatoi_u(s + 1);
|
|
|
|
real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len);
|
|
|
|
if (spooling && real_len != expected_len) {
|
|
|
|
unlink(fname); // don't keep corrupted files
|
|
|
|
printf("Expected %d but got %d bytes\n",
|
|
|
|
expected_len, real_len);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2008-03-23 23:40:18 +00:00
|
|
|
// chmod completely downloaded file as "readable+writable" ...
|
|
|
|
if (spooling) {
|
|
|
|
fchmod(fd, 0600);
|
|
|
|
// ... and accumulate dump state.
|
|
|
|
// N.B. after all files are dumped spooling should be 1+2+3==6
|
|
|
|
spooling += s[0];
|
|
|
|
}
|
|
|
|
close(fd); // NB: can do close(-1). Who cares?
|
|
|
|
|
|
|
|
// are all files dumped? -> spawn spool helper
|
|
|
|
if (6 == spooling && *argv) {
|
|
|
|
fname[0] = 'c'; // pass control file name
|
|
|
|
exec_helper(fname, argv);
|
|
|
|
}
|
2008-02-25 20:30:24 +00:00
|
|
|
// get ACK and see whether it is NUL (ok)
|
|
|
|
if (read(STDIN_FILENO, s, 1) != 1 || s[0] != 0) {
|
|
|
|
// don't send error msg to peer - it obviously
|
|
|
|
// don't follow the protocol, so probably
|
|
|
|
// it can't understand us either
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
} /* while (1) */
|
2008-02-25 14:48:15 +00:00
|
|
|
}
|