3
0

lpr.c 8.5 KB

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