lpr.c 8.4 KB

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