3
0

lpr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // parse options
  85. // TODO: set opt_complementary: s,d,f are mutually exclusive
  86. opts = getopt32(argv,
  87. (/*lp*/'r' == applet_name[2]) ? "P:U:VhC:J:m" : "P:U:sdf"
  88. , &queue, &user
  89. , &printer_class, &job_title
  90. );
  91. argv += optind;
  92. // if queue is not specified -> use $PRINTER
  93. if (!(opts & OPT_P))
  94. queue = getenv("PRINTER");
  95. // if queue is still not specified ->
  96. if (!queue) {
  97. // ... queue defaults to "lp"
  98. // server defaults to "localhost"
  99. queue = "lp";
  100. // if queue is specified ->
  101. } else {
  102. // queue name is to the left of '@'
  103. char *s = strchr(queue, '@');
  104. if (s) {
  105. // server name is to the right of '@'
  106. *s = '\0';
  107. server = s + 1;
  108. }
  109. }
  110. // do connect
  111. fd = create_and_connect_stream_or_die(server, 515);
  112. //
  113. // LPQ ------------------------
  114. //
  115. if (/*lp*/'q' == applet_name[2]) {
  116. char cmd;
  117. // force printing of every job still in queue
  118. if (opts & LPQ_FORCE) {
  119. cmd = 1;
  120. goto command;
  121. // delete job(s)
  122. } else if (opts & LPQ_DELETE) {
  123. fdprintf(fd, "\x5" "%s %s", queue, user);
  124. while (*argv) {
  125. fdprintf(fd, " %s", *argv++);
  126. }
  127. bb_putchar('\n');
  128. // dump current jobs status
  129. // N.B. periodical polling should be achieved
  130. // via "watch -n delay lpq"
  131. // They say it's the UNIX-way :)
  132. } else {
  133. cmd = (opts & LPQ_SHORT_FMT) ? 3 : 4;
  134. command:
  135. fdprintf(fd, "%c" "%s\n", cmd, queue);
  136. bb_copyfd_eof(fd, STDOUT_FILENO);
  137. }
  138. return EXIT_SUCCESS;
  139. }
  140. //
  141. // LPR ------------------------
  142. //
  143. if (opts & LPR_V)
  144. bb_error_msg("connected to server");
  145. job = getpid() % 1000;
  146. hostname = safe_gethostname();
  147. // no files given on command line? -> use stdin
  148. if (!*argv)
  149. *--argv = (char *)"-";
  150. fdprintf(fd, "\x2" "%s\n", queue);
  151. get_response_or_say_and_die(fd, "setting queue");
  152. // process files
  153. do {
  154. unsigned cflen;
  155. int dfd;
  156. struct stat st;
  157. char *c;
  158. char *remote_filename;
  159. char *controlfile;
  160. // if data file is stdin, we need to dump it first
  161. if (LONE_DASH(*argv)) {
  162. strcpy(tempfile, "/tmp/lprXXXXXX");
  163. dfd = xmkstemp(tempfile);
  164. bb_copyfd_eof(STDIN_FILENO, dfd);
  165. xlseek(dfd, 0, SEEK_SET);
  166. *argv = (char*)bb_msg_standard_input;
  167. } else {
  168. dfd = xopen(*argv, O_RDONLY);
  169. }
  170. /* "The name ... should start with ASCII "cfA",
  171. * followed by a three digit job number, followed
  172. * by the host name which has constructed the file."
  173. * We supply 'c' or 'd' as needed for control/data file. */
  174. remote_filename = xasprintf("fA%03u%s", job, hostname);
  175. // create control file
  176. // TODO: all lines but 2 last are constants! How we can use this fact?
  177. controlfile = xasprintf(
  178. "H" "%.32s\n" "P" "%.32s\n" /* H HOST, P USER */
  179. "C" "%.32s\n" /* C CLASS - printed on banner page (if L cmd is also given) */
  180. "J" "%.99s\n" /* J JOBNAME */
  181. /* "class name for banner page and job name
  182. * for banner page commands must precede L command" */
  183. "L" "%.32s\n" /* L USER - print banner page, with given user's name */
  184. "M" "%.32s\n" /* M WHOM_TO_MAIL */
  185. "l" "d%.31s\n" /* l DATA_FILE_NAME ("dfAxxx") */
  186. , hostname, user
  187. , printer_class /* can be "" */
  188. , ((opts & LPR_J) ? job_title : *argv)
  189. , (opts & LPR_h) ? user : ""
  190. , (opts & LPR_m) ? user : ""
  191. , remote_filename
  192. );
  193. // delete possible "\nX\n" patterns
  194. c = controlfile;
  195. cflen = (unsigned)strlen(controlfile);
  196. while ((c = strchr(c, '\n')) != NULL) {
  197. if (c[1] && c[2] == '\n') {
  198. /* can't use strcpy, results are undefined */
  199. memmove(c, c+2, cflen - (c-controlfile) - 1);
  200. cflen -= 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. fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
  211. get_response_or_say_and_die(fd, "sending control file");
  212. /* "Once all of the contents have
  213. * been delivered, an octet of zero bits is sent as
  214. * an indication that the file being sent is complete.
  215. * A second level of acknowledgement processing
  216. * must occur at this point." */
  217. full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
  218. get_response_or_say_and_die(fd, "sending control file");
  219. // send data file, with name "dfaXXX"
  220. if (opts & LPR_V)
  221. bb_error_msg("sending data file");
  222. st.st_size = 0; /* paranoia: fstat may theoretically fail */
  223. fstat(dfd, &st);
  224. fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
  225. get_response_or_say_and_die(fd, "sending data file");
  226. if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
  227. // We're screwed. We sent less bytes than we advertised.
  228. bb_error_msg_and_die("local file changed size?!");
  229. }
  230. write(fd, "", 1); // send ACK
  231. get_response_or_say_and_die(fd, "sending data file");
  232. // delete temporary file if we dumped stdin
  233. if (*argv == (char*)bb_msg_standard_input)
  234. unlink(tempfile);
  235. // cleanup
  236. close(fd);
  237. free(remote_filename);
  238. free(controlfile);
  239. // say job accepted
  240. if (opts & LPR_V)
  241. bb_error_msg("job accepted");
  242. // next, please!
  243. job = (job + 1) % 1000;
  244. } while (*++argv);
  245. return EXIT_SUCCESS;
  246. }