lpr.c 7.1 KB

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