Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 3 | * bare bones version of lpr & lpq: BSD printing utilities |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 4 | * |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
| 6 | * |
| 7 | * Original idea and code: |
| 8 | * Walter Harms <WHarms@bfs.de> |
| 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 11 | * |
Denis Vlasenko | 6d8112c | 2008-02-24 22:08:57 +0000 | [diff] [blame] | 12 | * See RFC 1179 for protocol description. |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 13 | */ |
| 14 | #include "libbb.h" |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 16 | /* |
| 17 | * LPD returns binary 0 on success. |
| 18 | * Otherwise it returns error message. |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 19 | */ |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 20 | static void get_response_or_say_and_die(int fd, const char *errmsg) |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 21 | { |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 22 | ssize_t sz; |
| 23 | char buf[128]; |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 25 | buf[0] = ' '; |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 26 | sz = safe_read(fd, buf, 1); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 27 | if ('\0' != buf[0]) { |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 28 | // request has failed |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 29 | // try to make sure last char is '\n', but do not add |
| 30 | // superfluous one |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 31 | sz = full_read(fd, buf + 1, 126); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 32 | bb_error_msg("error while %s%s", errmsg, |
| 33 | (sz > 0 ? ". Server said:" : "")); |
| 34 | if (sz > 0) { |
| 35 | // sz = (bytes in buf) - 1 |
| 36 | if (buf[sz] != '\n') |
| 37 | buf[++sz] = '\n'; |
| 38 | safe_write(STDERR_FILENO, buf, sz + 1); |
| 39 | } |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 40 | xfunc_die(); |
| 41 | } |
| 42 | } |
| 43 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 44 | int lpqr_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 45 | int lpqr_main(int argc UNUSED_PARAM, char *argv[]) |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 46 | { |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 47 | enum { |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 48 | OPT_P = 1 << 0, // -P queue[@host[:port]]. If no -P is given use $PRINTER, then "lp@localhost:515" |
| 49 | OPT_U = 1 << 1, // -U username |
| 50 | |
| 51 | LPR_V = 1 << 2, // -V: be verbose |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 52 | LPR_h = 1 << 3, // -h: want banner printed |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 53 | LPR_C = 1 << 4, // -C class: job "class" (? supposedly printed on banner) |
| 54 | LPR_J = 1 << 5, // -J title: the job title for the banner page |
| 55 | LPR_m = 1 << 6, // -m: send mail back to user |
| 56 | |
| 57 | LPQ_SHORT_FMT = 1 << 2, // -s: short listing format |
| 58 | LPQ_DELETE = 1 << 3, // -d: delete job(s) |
| 59 | LPQ_FORCE = 1 << 4, // -f: force waiting job(s) to be printed |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 60 | }; |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 61 | char tempfile[sizeof("/tmp/lprXXXXXX")]; |
| 62 | const char *job_title; |
| 63 | const char *printer_class = ""; // printer class, max 32 char |
| 64 | const char *queue; // name of printer queue |
| 65 | const char *server = "localhost"; // server[:port] of printer queue |
| 66 | char *hostname; |
| 67 | // N.B. IMHO getenv("USER") can be way easily spoofed! |
Denis Vlasenko | 0c68a87 | 2008-12-02 22:56:59 +0000 | [diff] [blame] | 68 | const char *user = xuid2uname(getuid()); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 69 | unsigned job; |
| 70 | unsigned opts; |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 71 | int fd; |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 73 | // parse options |
| 74 | // TODO: set opt_complementary: s,d,f are mutually exclusive |
| 75 | opts = getopt32(argv, |
| 76 | (/*lp*/'r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf" |
| 77 | , &queue, &user |
| 78 | , &printer_class, &job_title |
| 79 | ); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 80 | argv += optind; |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 82 | // if queue is not specified -> use $PRINTER |
| 83 | if (!(opts & OPT_P)) |
| 84 | queue = getenv("PRINTER"); |
| 85 | // if queue is still not specified -> |
| 86 | if (!queue) { |
| 87 | // ... queue defaults to "lp" |
| 88 | // server defaults to "localhost" |
| 89 | queue = "lp"; |
| 90 | // if queue is specified -> |
| 91 | } else { |
| 92 | // queue name is to the left of '@' |
| 93 | char *s = strchr(queue, '@'); |
| 94 | if (s) { |
| 95 | // server name is to the right of '@' |
| 96 | *s = '\0'; |
| 97 | server = s + 1; |
| 98 | } |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 101 | // do connect |
| 102 | fd = create_and_connect_stream_or_die(server, 515); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 103 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 104 | // |
| 105 | // LPQ ------------------------ |
| 106 | // |
| 107 | if (/*lp*/'q' == applet_name[2]) { |
| 108 | char cmd; |
| 109 | // force printing of every job still in queue |
| 110 | if (opts & LPQ_FORCE) { |
| 111 | cmd = 1; |
| 112 | goto command; |
| 113 | // delete job(s) |
| 114 | } else if (opts & LPQ_DELETE) { |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 115 | fdprintf(fd, "\x5" "%s %s", queue, user); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 116 | while (*argv) { |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 117 | fdprintf(fd, " %s", *argv++); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 118 | } |
| 119 | bb_putchar('\n'); |
| 120 | // dump current jobs status |
| 121 | // N.B. periodical polling should be achieved |
| 122 | // via "watch -n delay lpq" |
| 123 | // They say it's the UNIX-way :) |
| 124 | } else { |
| 125 | cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4; |
| 126 | command: |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 127 | fdprintf(fd, "%c" "%s\n", cmd, queue); |
| 128 | bb_copyfd_eof(fd, STDOUT_FILENO); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 129 | } |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 130 | |
| 131 | return EXIT_SUCCESS; |
| 132 | } |
| 133 | |
| 134 | // |
| 135 | // LPR ------------------------ |
| 136 | // |
| 137 | if (opts & LPR_V) |
| 138 | bb_error_msg("connected to server"); |
| 139 | |
| 140 | job = getpid() % 1000; |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 141 | hostname = safe_gethostname(); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 142 | |
| 143 | // no files given on command line? -> use stdin |
| 144 | if (!*argv) |
| 145 | *--argv = (char *)"-"; |
| 146 | |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 147 | fdprintf(fd, "\x2" "%s\n", queue); |
| 148 | get_response_or_say_and_die(fd, "setting queue"); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 149 | |
| 150 | // process files |
| 151 | do { |
Denis Vlasenko | c445758 | 2008-07-22 19:15:30 +0000 | [diff] [blame] | 152 | unsigned cflen; |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 153 | int dfd; |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 154 | struct stat st; |
| 155 | char *c; |
| 156 | char *remote_filename; |
| 157 | char *controlfile; |
| 158 | |
| 159 | // if data file is stdin, we need to dump it first |
| 160 | if (LONE_DASH(*argv)) { |
| 161 | strcpy(tempfile, "/tmp/lprXXXXXX"); |
Alexander Shishkin | 6722737 | 2010-10-22 13:27:16 +0200 | [diff] [blame] | 162 | dfd = xmkstemp(tempfile); |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 163 | bb_copyfd_eof(STDIN_FILENO, dfd); |
| 164 | xlseek(dfd, 0, SEEK_SET); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 165 | *argv = (char*)bb_msg_standard_input; |
| 166 | } else { |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 167 | dfd = xopen(*argv, O_RDONLY); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* "The name ... should start with ASCII "cfA", |
| 171 | * followed by a three digit job number, followed |
| 172 | * by the host name which has constructed the file." |
| 173 | * We supply 'c' or 'd' as needed for control/data file. */ |
| 174 | remote_filename = xasprintf("fA%03u%s", job, hostname); |
| 175 | |
| 176 | // create control file |
| 177 | // TODO: all lines but 2 last are constants! How we can use this fact? |
| 178 | controlfile = xasprintf( |
| 179 | "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */ |
| 180 | "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */ |
| 181 | "J" "%.99s\n" /* J JOBNAME */ |
| 182 | /* "class name for banner page and job name |
| 183 | * for banner page commands must precede L command" */ |
| 184 | "L" "%.32s\n" /* L USER - print banner page, with given user's name */ |
| 185 | "M" "%.32s\n" /* M WHOM_TO_MAIL */ |
| 186 | "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */ |
| 187 | , hostname, user |
| 188 | , printer_class /* can be "" */ |
| 189 | , ((opts & LPR_J) ? job_title : *argv) |
| 190 | , (opts & LPR_h) ? user : "" |
| 191 | , (opts & LPR_m) ? user : "" |
| 192 | , remote_filename |
| 193 | ); |
| 194 | // delete possible "\nX\n" patterns |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 195 | c = controlfile; |
Denis Vlasenko | c445758 | 2008-07-22 19:15:30 +0000 | [diff] [blame] | 196 | cflen = (unsigned)strlen(controlfile); |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 197 | while ((c = strchr(c, '\n')) != NULL) { |
Denis Vlasenko | c445758 | 2008-07-22 19:15:30 +0000 | [diff] [blame] | 198 | if (c[1] && c[2] == '\n') { |
| 199 | /* can't use strcpy, results are undefined */ |
| 200 | memmove(c, c+2, cflen - (c-controlfile) - 1); |
| 201 | cflen -= 2; |
| 202 | } else { |
| 203 | c++; |
| 204 | } |
Denis Vlasenko | f99afb5 | 2008-02-24 23:32:36 +0000 | [diff] [blame] | 205 | } |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 206 | |
| 207 | // send control file |
| 208 | if (opts & LPR_V) |
| 209 | bb_error_msg("sending control file"); |
Denis Vlasenko | c445758 | 2008-07-22 19:15:30 +0000 | [diff] [blame] | 210 | /* "Acknowledgement processing must occur as usual |
| 211 | * after the command is sent." */ |
| 212 | fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename); |
| 213 | get_response_or_say_and_die(fd, "sending control file"); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 214 | /* "Once all of the contents have |
| 215 | * been delivered, an octet of zero bits is sent as |
| 216 | * an indication that the file being sent is complete. |
| 217 | * A second level of acknowledgement processing |
| 218 | * must occur at this point." */ |
Denis Vlasenko | c445758 | 2008-07-22 19:15:30 +0000 | [diff] [blame] | 219 | full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */ |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 220 | get_response_or_say_and_die(fd, "sending control file"); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 221 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 222 | // send data file, with name "dfaXXX" |
| 223 | if (opts & LPR_V) |
| 224 | bb_error_msg("sending data file"); |
| 225 | st.st_size = 0; /* paranoia: fstat may theoretically fail */ |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 226 | fstat(dfd, &st); |
| 227 | fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename); |
Denis Vlasenko | c445758 | 2008-07-22 19:15:30 +0000 | [diff] [blame] | 228 | get_response_or_say_and_die(fd, "sending data file"); |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 229 | if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) { |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 230 | // We're screwed. We sent less bytes than we advertised. |
| 231 | bb_error_msg_and_die("local file changed size?!"); |
| 232 | } |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 233 | write(fd, "", 1); // send ACK |
| 234 | get_response_or_say_and_die(fd, "sending data file"); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 235 | |
| 236 | // delete temporary file if we dumped stdin |
| 237 | if (*argv == (char*)bb_msg_standard_input) |
| 238 | unlink(tempfile); |
| 239 | |
| 240 | // cleanup |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 241 | close(fd); |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 242 | free(remote_filename); |
| 243 | free(controlfile); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 244 | |
Denis Vlasenko | fe66a0e | 2008-02-27 14:35:21 +0000 | [diff] [blame] | 245 | // say job accepted |
| 246 | if (opts & LPR_V) |
| 247 | bb_error_msg("job accepted"); |
| 248 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 249 | // next, please! |
| 250 | job = (job + 1) % 1000; |
| 251 | } while (*++argv); |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 252 | |
Denis Vlasenko | 4f82bdb | 2008-02-24 18:44:20 +0000 | [diff] [blame] | 253 | return EXIT_SUCCESS; |
Denis Vlasenko | 68444b9 | 2008-02-22 22:24:48 +0000 | [diff] [blame] | 254 | } |