Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 3 | * micro lpd |
Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 9 | |
| 10 | /* |
| 11 | * A typical usage of BB lpd looks as follows: |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 12 | * # tcpsvd -E 0 515 lpd [SPOOLDIR] [HELPER-PROG [ARGS...]] |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 13 | * |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 14 | * 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 Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 17 | * |
| 18 | * SPOOLDIR is the spool directory which contains printing queues |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 19 | * and should have the following structure: |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 20 | * |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 21 | * SPOOLDIR/ |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 22 | * <queue1> |
| 23 | * ... |
| 24 | * <queueN> |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 25 | * |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 26 | * <queueX> can be of two types: |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 27 | * A. a printer character device, an ordinary file or a link to such; |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 28 | * B. a directory. |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 29 | * |
| 30 | * In case A lpd just dumps the data it receives from client (lpr) to the |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 31 | * end of queue file/device. This is non-spooling mode. |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 32 | * |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 33 | * 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 Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 37 | * is specified lpd is done at this point. |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 38 | * |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 39 | * NB: file names are produced by peer! They actually may be anything at all. |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 40 | * lpd only sanitizes them (by removing most non-alphanumerics). |
| 41 | * |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 42 | * 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 Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 50 | * |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 51 | * A good lpr passes control files which when parsed provides the following |
| 52 | * variables: |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 53 | * $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 Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 59 | * |
| 60 | * We specifically filter out and NOT provide: |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 61 | * $l = name of datafile ("dfAxxx") - file whose content are to be printed |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 62 | * |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 63 | * lpd provides $DATAFILE instead - the ACTUAL name |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 64 | * of the datafile under which it was saved. |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 65 | * $l would be not reliable (you would be at mercy of remote peer). |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 66 | * |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 67 | * Thus, a typical helper can be something like this: |
| 68 | * #!/bin/sh |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 69 | * cat ./"$DATAFILE" >/dev/lp0 |
| 70 | * mv -f ./"$DATAFILE" save/ |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 71 | */ |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 73 | #include "libbb.h" |
| 74 | |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 75 | // strip argument of bad chars |
| 76 | static char *sane(char *str) |
| 77 | { |
| 78 | char *s = str; |
| 79 | char *p = s; |
| 80 | while (*s) { |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 81 | if (isalnum(*s) || '-' == *s || '_' == *s) { |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 82 | *p++ = *s; |
| 83 | } |
| 84 | s++; |
| 85 | } |
| 86 | *p = '\0'; |
| 87 | return str; |
| 88 | } |
| 89 | |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 90 | static char *xmalloc_read_stdin(void) |
| 91 | { |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 92 | // SECURITY: |
| 93 | size_t max = 4 * 1024; // more than enough for commands! |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 94 | return xmalloc_reads(STDIN_FILENO, NULL, &max); |
| 95 | } |
| 96 | |
Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 97 | int lpd_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 98 | int lpd_main(int argc ATTRIBUTE_UNUSED, char *argv[]) |
Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 99 | { |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 100 | int spooling = spooling; // for compiler |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 101 | char *s, *queue; |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 102 | char *filenames[2]; |
Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 104 | // goto spool directory |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 105 | if (*++argv) |
| 106 | xchdir(*argv++); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 107 | |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 108 | // error messages of xfuncs will be sent over network |
| 109 | xdup2(STDOUT_FILENO, STDERR_FILENO); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 110 | |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 111 | // nullify ctrl/data filenames |
| 112 | memset(filenames, 0, sizeof(filenames)); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 113 | |
| 114 | // read command |
| 115 | s = queue = xmalloc_read_stdin(); |
| 116 | // we understand only "receive job" command |
| 117 | if (2 != *queue) { |
| 118 | unsupported_cmd: |
| 119 | printf("Command %02x %s\n", |
| 120 | (unsigned char)s[0], "is not supported"); |
| 121 | goto err_exit; |
| 122 | } |
| 123 | |
| 124 | // parse command: "2 | QUEUE_NAME | '\n'" |
| 125 | queue++; |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 126 | // protect against "/../" attacks |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 127 | // *strchrnul(queue, '\n') = '\0'; - redundant, sane() will do |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 128 | if (!*sane(queue)) |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 129 | return EXIT_FAILURE; |
| 130 | |
| 131 | // queue is a directory -> chdir to it and enter spooling mode |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 132 | spooling = chdir(queue) + 1; // 0: cannot chdir, 1: done |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 133 | // we don't free(s), we might need "queue" var later |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 134 | |
| 135 | while (1) { |
| 136 | char *fname; |
| 137 | int fd; |
| 138 | // int is easier than ssize_t: can use xatoi_u, |
| 139 | // and can correctly display error returns (-1) |
| 140 | int expected_len, real_len; |
| 141 | |
| 142 | // signal OK |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 143 | safe_write(STDOUT_FILENO, "", 1); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 144 | |
| 145 | // get subcommand |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 146 | // valid s must be of form: "SUBCMD | LEN | space | FNAME" |
| 147 | // N.B. we bail out on any error |
Denis Vlasenko | 0b6c6a9 | 2008-03-24 00:04:42 +0000 | [diff] [blame] | 148 | s = xmalloc_read_stdin(); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 149 | if (!s) { // (probably) EOF |
Denis Vlasenko | 0c3b0d5 | 2008-03-25 06:03:19 +0000 | [diff] [blame] | 150 | char *p, *q, var[2]; |
| 151 | |
| 152 | // non-spooling mode or no spool helper specified |
| 153 | if (!spooling || !*argv) |
| 154 | return EXIT_SUCCESS; // the only non-error exit |
| 155 | // spooling mode but we didn't see both ctrlfile & datafile |
| 156 | if (spooling != 7) |
| 157 | goto err_exit; // reject job |
| 158 | |
| 159 | // spooling mode and spool helper specified -> exec spool helper |
| 160 | // (we exit 127 if helper cannot be executed) |
| 161 | var[1] = '\0'; |
| 162 | // read and delete ctrlfile |
Denis Vlasenko | f3745ea | 2008-04-19 19:32:08 +0000 | [diff] [blame] | 163 | q = xmalloc_xopen_read_close(filenames[0], NULL); |
Denis Vlasenko | 0c3b0d5 | 2008-03-25 06:03:19 +0000 | [diff] [blame] | 164 | unlink(filenames[0]); |
| 165 | // provide datafile name |
| 166 | // we can use leaky setenv since we are about to exec or exit |
| 167 | xsetenv("DATAFILE", filenames[1]); |
| 168 | // parse control file by "\n" |
| 169 | while ((p = strchr(q, '\n')) != NULL && isalpha(*q)) { |
| 170 | *p++ = '\0'; |
| 171 | // q is a line of <SYM><VALUE>, |
| 172 | // we are setting environment string <SYM>=<VALUE>. |
| 173 | // Ignoring "l<datafile>", exporting others: |
| 174 | if (*q != 'l') { |
| 175 | var[0] = *q++; |
| 176 | xsetenv(var, q); |
| 177 | } |
| 178 | q = p; // next line |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 179 | } |
Denis Vlasenko | 0c3b0d5 | 2008-03-25 06:03:19 +0000 | [diff] [blame] | 180 | // helper should not talk over network. |
| 181 | // this call reopens stdio fds to "/dev/null" |
| 182 | // (no daemonization is done) |
| 183 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO | DAEMON_ONLY_SANITIZE, NULL); |
| 184 | BB_EXECVP(*argv, argv); |
| 185 | exit(127); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // validate input. |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 189 | // we understand only "control file" or "data file" cmds |
| 190 | if (2 != s[0] && 3 != s[0]) |
| 191 | goto unsupported_cmd; |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 192 | if (spooling & (1 << (s[0]-1))) { |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 193 | printf("Duplicated subcommand\n"); |
| 194 | goto err_exit; |
| 195 | } |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 196 | // get filename |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 197 | *strchrnul(s, '\n') = '\0'; |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 198 | fname = strchr(s, ' '); |
| 199 | if (!fname) { |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 200 | // bad_fname: |
| 201 | printf("No or bad filename\n"); |
| 202 | goto err_exit; |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 203 | } |
| 204 | *fname++ = '\0'; |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 205 | // // s[0]==2: ctrlfile, must start with 'c' |
| 206 | // // s[0]==3: datafile, must start with 'd' |
| 207 | // if (fname[0] != s[0] + ('c'-2)) |
| 208 | // goto bad_fname; |
| 209 | // get length |
| 210 | expected_len = bb_strtou(s + 1, NULL, 10); |
| 211 | if (errno || expected_len < 0) { |
| 212 | printf("Bad length\n"); |
| 213 | goto err_exit; |
| 214 | } |
| 215 | if (2 == s[0] && expected_len > 16 * 1024) { |
| 216 | // SECURITY: |
| 217 | // ctrlfile can't be big (we want to read it back later!) |
| 218 | printf("File is too big\n"); |
| 219 | goto err_exit; |
| 220 | } |
| 221 | |
| 222 | // open the file |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 223 | if (spooling) { |
| 224 | // spooling mode: dump both files |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 225 | // job in flight has mode 0200 "only writable" |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 226 | sane(fname); |
| 227 | fd = open3_or_warn(fname, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0200); |
| 228 | if (fd < 0) |
| 229 | goto err_exit; |
| 230 | filenames[s[0] - 2] = xstrdup(fname); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 231 | } else { |
| 232 | // non-spooling mode: |
| 233 | // 2: control file (ignoring), 3: data file |
| 234 | fd = -1; |
| 235 | if (3 == s[0]) |
| 236 | fd = xopen(queue, O_RDWR | O_APPEND); |
| 237 | } |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 238 | |
| 239 | // copy the file |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 240 | real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 241 | if (real_len != expected_len) { |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 242 | printf("Expected %d but got %d bytes\n", |
| 243 | expected_len, real_len); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 244 | goto err_exit; |
Denis Vlasenko | a794289 | 2008-03-23 23:40:18 +0000 | [diff] [blame] | 245 | } |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 246 | // get ACK and see whether it is NUL (ok) |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 247 | // (and don't trash s[0]!) |
| 248 | if (safe_read(STDIN_FILENO, &s[1], 1) != 1 || s[1] != 0) { |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 249 | // don't send error msg to peer - it obviously |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 250 | // doesn't follow the protocol, so probably |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 251 | // it can't understand us either |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 252 | goto err_exit; |
| 253 | } |
| 254 | |
| 255 | if (spooling) { |
| 256 | // chmod completely downloaded file as "readable+writable" |
| 257 | fchmod(fd, 0600); |
| 258 | // accumulate dump state |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 259 | // N.B. after all files are dumped spooling should be 1+2+4==7 |
| 260 | spooling |= (1 << (s[0]-1)); // bit 1: ctrlfile; bit 2: datafile |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 261 | } |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 262 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 263 | free(s); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 264 | close(fd); // NB: can do close(-1). Who cares? |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 265 | } // while (1) |
| 266 | |
| 267 | err_exit: |
| 268 | // don't keep corrupted files |
| 269 | if (spooling) { |
Denis Vlasenko | f39653e | 2008-03-24 21:19:51 +0000 | [diff] [blame] | 270 | #define i spooling |
| 271 | for (i = 2; --i >= 0; ) |
| 272 | if (filenames[i]) |
| 273 | unlink(filenames[i]); |
Denis Vlasenko | 39c77c3 | 2008-03-24 01:52:52 +0000 | [diff] [blame] | 274 | } |
| 275 | return EXIT_FAILURE; |
Denis Vlasenko | 38b8831 | 2008-02-25 14:48:15 +0000 | [diff] [blame] | 276 | } |