lpr.c 7.8 KB

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