lpr.c 6.9 KB

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