lpr.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 tarball for details.
  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 = bb_getpwuid(NULL, -1, 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 = mkstemp(tempfile);
  147. if (dfd < 0)
  148. bb_perror_msg_and_die("mkstemp");
  149. bb_copyfd_eof(STDIN_FILENO, dfd);
  150. xlseek(dfd, 0, SEEK_SET);
  151. *argv = (char*)bb_msg_standard_input;
  152. } else {
  153. dfd = xopen(*argv, O_RDONLY);
  154. }
  155. /* "The name ... should start with ASCII "cfA",
  156. * followed by a three digit job number, followed
  157. * by the host name which has constructed the file."
  158. * We supply 'c' or 'd' as needed for control/data file. */
  159. remote_filename = xasprintf("fA%03u%s", job, hostname);
  160. // create control file
  161. // TODO: all lines but 2 last are constants! How we can use this fact?
  162. controlfile = xasprintf(
  163. "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
  164. "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
  165. "J" "%.99s\n" /* J JOBNAME */
  166. /* "class name for banner page and job name
  167. * for banner page commands must precede L command" */
  168. "L" "%.32s\n" /* L USER - print banner page, with given user's name */
  169. "M" "%.32s\n" /* M WHOM_TO_MAIL */
  170. "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
  171. , hostname, user
  172. , printer_class /* can be "" */
  173. , ((opts & LPR_J) ? job_title : *argv)
  174. , (opts & LPR_h) ? user : ""
  175. , (opts & LPR_m) ? user : ""
  176. , remote_filename
  177. );
  178. // delete possible "\nX\n" patterns
  179. c = controlfile;
  180. cflen = (unsigned)strlen(controlfile);
  181. while ((c = strchr(c, '\n')) != NULL) {
  182. if (c[1] && c[2] == '\n') {
  183. /* can't use strcpy, results are undefined */
  184. memmove(c, c+2, cflen - (c-controlfile) - 1);
  185. cflen -= 2;
  186. } else {
  187. c++;
  188. }
  189. }
  190. // send control file
  191. if (opts & LPR_V)
  192. bb_error_msg("sending control file");
  193. /* "Acknowledgement processing must occur as usual
  194. * after the command is sent." */
  195. fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
  196. get_response_or_say_and_die(fd, "sending control file");
  197. /* "Once all of the contents have
  198. * been delivered, an octet of zero bits is sent as
  199. * an indication that the file being sent is complete.
  200. * A second level of acknowledgement processing
  201. * must occur at this point." */
  202. full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
  203. get_response_or_say_and_die(fd, "sending control file");
  204. // send data file, with name "dfaXXX"
  205. if (opts & LPR_V)
  206. bb_error_msg("sending data file");
  207. st.st_size = 0; /* paranoia: fstat may theoretically fail */
  208. fstat(dfd, &st);
  209. fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
  210. get_response_or_say_and_die(fd, "sending data file");
  211. if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
  212. // We're screwed. We sent less bytes than we advertised.
  213. bb_error_msg_and_die("local file changed size?!");
  214. }
  215. write(fd, "", 1); // send ACK
  216. get_response_or_say_and_die(fd, "sending data file");
  217. // delete temporary file if we dumped stdin
  218. if (*argv == (char*)bb_msg_standard_input)
  219. unlink(tempfile);
  220. // cleanup
  221. close(fd);
  222. free(remote_filename);
  223. free(controlfile);
  224. // say job accepted
  225. if (opts & LPR_V)
  226. bb_error_msg("job accepted");
  227. // next, please!
  228. job = (job + 1) % 1000;
  229. } while (*++argv);
  230. return EXIT_SUCCESS;
  231. }