lpdaemon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <signal.h>
  15. #include <stdarg.h>
  16. #include <time.h>
  17. #include <unistd.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. /* for Plan 9 */
  21. #ifdef PLAN9
  22. #define LP "/bin/lp"
  23. #define TMPDIR "/sys/lib/lp/tmp"
  24. #define LPDAEMONLOG "/sys/lib/lp/log/lpdaemonl"
  25. #endif
  26. /* for Tenth Edition systems */
  27. #ifdef V10
  28. #define LP "/usr/bin/lp"
  29. #define TMPDIR "/tmp"
  30. #define LPDAEMONLOG "/tmp/lpdaemonl"
  31. #endif
  32. /* for System V or BSD systems */
  33. #if defined(SYSV) || defined(BSD)
  34. #define LP "/v/bin/lp"
  35. #define TMPDIR "/tmp"
  36. #define LPDAEMONLOG "/tmp/lpdaemonl"
  37. #endif
  38. #define ARGSIZ 4096
  39. #define NAMELEN 30
  40. unsigned char argvstr[ARGSIZ]; /* arguments after parsing */
  41. unsigned char *argvals[ARGSIZ/2+1]; /* pointers to arguments after parsing */
  42. int ascnt = 0, argcnt = 0; /* number of arguments parsed */
  43. /* for 'stuff' gleened from lpr cntrl file */
  44. struct jobinfo {
  45. char user[NAMELEN+1];
  46. char host[NAMELEN+1];
  47. } *getjobinfo();
  48. #define MIN(a,b) ((a<b)?a:b)
  49. #define CPYFIELD(src, dst) { while (*(src)!=' ' && *(src)!='\t' && *(src)!='\r' && *(src)!='\n' && *(src)!='\0') *(dst)++ = *(src)++; }
  50. #define ACK() write(1, "", 1)
  51. #define NAK() write(1, "\001", 1)
  52. #define LNBFSZ 4096
  53. unsigned char lnbuf[LNBFSZ];
  54. #define RDSIZE 512
  55. unsigned char jobbuf[RDSIZE];
  56. int datafd[400], cntrlfd = -1;
  57. int dbgstate = 0;
  58. char *dbgstrings[] = {
  59. "",
  60. "sendack1",
  61. "send",
  62. "rcvack",
  63. "sendack2",
  64. "done"
  65. };
  66. void
  67. error(char *s1, ...)
  68. {
  69. FILE *fp;
  70. int32_t thetime;
  71. char *chartime;
  72. va_list ap;
  73. char *args[8];
  74. int argno = 0;
  75. if((fp=fopen(LPDAEMONLOG, "a"))==NULL) {
  76. fprintf(stderr, "cannot open %s in append mode\n", LPDAEMONLOG);
  77. return;
  78. }
  79. time(&thetime);
  80. chartime = ctime(&thetime);
  81. fprintf(fp, "%.15s [%5.5d] ", &(chartime[4]), getpid());
  82. va_start(ap, s1);
  83. while((args[argno++] = va_arg(ap, char*)) && argno<8)
  84. ;
  85. va_end(ap);
  86. fprintf(fp, s1, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
  87. fclose(fp);
  88. }
  89. void
  90. forklp(int inputfd)
  91. {
  92. int i, cpid;
  93. unsigned char *bp, *cp;
  94. unsigned char logent[LNBFSZ];
  95. /* log this call to lp */
  96. cp = logent;
  97. for (i=1; i<argcnt; i++) {
  98. bp = argvals[i];
  99. if (cp+strlen((const char *)bp)+1 < logent+LNBFSZ-1) {
  100. CPYFIELD(bp, cp);
  101. *cp++ = ' ';
  102. }
  103. }
  104. *--cp = '\n';
  105. *++cp = '\0';
  106. error((const char *)logent);
  107. switch((cpid=fork())){
  108. case -1:
  109. error("fork error\n");
  110. exit(2);
  111. case 0:
  112. if (inputfd != 0)
  113. dup2(inputfd, 0);
  114. dup2(1, 2);
  115. lseek(0, 0L, 0);
  116. execvp(LP, (const char **)argvals);
  117. error("exec failed\n");
  118. exit(3);
  119. default:
  120. while(wait((int *)0) != cpid)
  121. ;
  122. }
  123. }
  124. int
  125. tempfile(void)
  126. {
  127. static tindx = 0;
  128. char tmpf[sizeof(TMPDIR)+64];
  129. int crtfd, tmpfd;
  130. sprintf(tmpf, "%s/lp%d.%d", TMPDIR, getpid(), tindx++);
  131. if((crtfd=creat(tmpf, 0666)) < 0) {
  132. error("cannot create temp file %s\n", tmpf);
  133. NAK();
  134. exit(3);
  135. }
  136. if((tmpfd=open(tmpf, 2)) < 0) {
  137. error("cannot open temp file %s\n", tmpf);
  138. NAK();
  139. exit(3);
  140. }
  141. close(crtfd);
  142. unlink(tmpf); /* comment out for debugging */
  143. return(tmpfd);
  144. }
  145. int
  146. readfile(int outfd, int bsize)
  147. {
  148. int rv;
  149. dbgstate = 1;
  150. alarm(60);
  151. ACK();
  152. dbgstate = 2;
  153. for(; bsize > 0; bsize -= rv) {
  154. alarm(60);
  155. if((rv=read(0, jobbuf, MIN(bsize,RDSIZE))) < 0) {
  156. error("error reading input, %d unread\n", bsize);
  157. exit(4);
  158. } else if (rv == 0) {
  159. error("connection closed prematurely\n");
  160. exit(4);
  161. } else if((write(outfd, jobbuf, rv)) != rv) {
  162. error("error writing temp file, %d unread\n", bsize);
  163. exit(5);
  164. }
  165. }
  166. dbgstate = 3;
  167. alarm(60);
  168. if (((rv=read(0, jobbuf, 1))==1) && (*jobbuf=='\0')) {
  169. alarm(60);
  170. ACK();
  171. dbgstate = 4;
  172. alarm(0);
  173. return(outfd);
  174. }
  175. alarm(0);
  176. error("received bad status <%d> from sender\n", *jobbuf);
  177. error("rv=%d\n", rv);
  178. NAK();
  179. return(-1);
  180. }
  181. /* reads a line from the input into lnbuf
  182. * if there is no error, it returns
  183. * the number of characters in the buffer
  184. * if there is an error and there where characters
  185. * read, it returns the negative value of the
  186. * number of characters read
  187. * if there is an error and no characters were read,
  188. * it returns the negative value of 1 greater than
  189. * the size of the line buffer
  190. */
  191. int
  192. readline(int inpfd)
  193. {
  194. unsigned char *ap;
  195. int i, rv;
  196. ap = lnbuf;
  197. lnbuf[0] = '\0';
  198. i = 0;
  199. alarm(60);
  200. do {
  201. rv = read(inpfd, ap, 1);
  202. } while (rv==1 && ++i && *ap != '\n' && ap++ && (i < LNBFSZ - 2));
  203. alarm(0);
  204. if (i != 0 && *ap != '\n') {
  205. *++ap = '\n';
  206. i++;
  207. }
  208. *++ap = '\0';
  209. if (rv < 0) {
  210. error("read error; lost connection\n");
  211. if (i==0) i = -(LNBFSZ+1);
  212. else i = -i;
  213. }
  214. return(i);
  215. }
  216. int
  217. getfiles(void)
  218. {
  219. unsigned char *ap;
  220. int filecnt, bsize, rv;
  221. filecnt = 0;
  222. /* get a line, hopefully containing a ctrl char, size, and name */
  223. for(;;) {
  224. ap = lnbuf;
  225. if ((rv=readline(0)) < 0) NAK();
  226. if (rv <= 0) {
  227. return(filecnt);
  228. }
  229. switch(*ap++) {
  230. case '\1': /* cleanup - data sent was bad (whatever that means) */
  231. break;
  232. case '\2': /* read control file */
  233. bsize = atoi((const char *)ap);
  234. cntrlfd = tempfile();
  235. if (readfile(cntrlfd, bsize) < 0) {
  236. close(cntrlfd);
  237. NAK();
  238. return(0);
  239. }
  240. break;
  241. case '\3': /* read data file */
  242. bsize = atoi((const char *)ap);
  243. datafd[filecnt] = tempfile();
  244. if (readfile(datafd[filecnt], bsize) < 0) {
  245. close(datafd[filecnt]);
  246. NAK();
  247. return(0);
  248. }
  249. filecnt++;
  250. break;
  251. default:
  252. error("protocol error <%d>\n", *(ap-1));
  253. NAK();
  254. }
  255. }
  256. return(filecnt);
  257. }
  258. struct jobinfo *
  259. getjobinfo(int fd)
  260. {
  261. unsigned char *ap;
  262. int rv;
  263. static struct jobinfo info;
  264. if (fd < 0) error("getjobinfo: bad file descriptor\n");
  265. if (lseek(fd, 0L, 0) < 0) {
  266. error("error seeking in temp file\n");
  267. exit(7);
  268. }
  269. /* the following strings should be < NAMELEN or else they will not
  270. * be null terminated.
  271. */
  272. strncpy(info.user, "daemon", NAMELEN);
  273. strncpy(info.host, "nowhere", NAMELEN);
  274. /* there may be a space after the name and host. It will be filtered out
  275. * by CPYFIELD.
  276. */
  277. while ((rv=readline(fd)) > 0) {
  278. ap = lnbuf;
  279. ap[rv-1] = '\0'; /* remove newline from string */
  280. switch (*ap) {
  281. case 'H':
  282. if (ap[1] == '\0')
  283. strncpy(info.host, "unknown", NAMELEN);
  284. else
  285. strncpy(info.host, (const char *)&ap[1],
  286. NAMELEN);
  287. info.host[NAMELEN] = '\0';
  288. break;
  289. case 'P':
  290. if (ap[1] == '\0')
  291. strncpy(info.user, "unknown", NAMELEN);
  292. else
  293. strncpy(info.user, (const char *)&ap[1],
  294. NAMELEN);
  295. info.user[NAMELEN] = '\0';
  296. break;
  297. }
  298. }
  299. return(&info);
  300. }
  301. void
  302. alarmhandler(int sig) {
  303. signal(sig, alarmhandler);
  304. error("alarm at %d - %s\n", dbgstate, dbgstrings[dbgstate]);
  305. }
  306. void
  307. main()
  308. {
  309. unsigned char *ap, *bp, *cp, *savbufpnt;
  310. int i, blen, rv, saveflg, savargcnt;
  311. struct jobinfo *jinfop;
  312. signal(SIGHUP, SIG_IGN);
  313. signal(SIGALRM, alarmhandler);
  314. cp = argvstr;
  315. /* setup argv[0] for exec */
  316. argvals[argcnt++] = cp;
  317. for (bp = (unsigned char *)LP, i = 0; (*bp != '\0') && (i < ARGSIZ-1); *cp++ = *bp++, i++);
  318. *cp++ = '\0';
  319. /* get the first line sent and parse it as arguments for lp */
  320. if ((rv=readline(0)) < 0)
  321. exit(1);
  322. bp = lnbuf;
  323. /* setup the remaining arguments */
  324. /* check for BSD style request */
  325. /* ^A, ^B, ^C, ^D, ^E (for BSD lpr) */
  326. switch (*bp) {
  327. case '\001':
  328. case '\003':
  329. case '\004':
  330. bp++; /* drop the ctrl character from the input */
  331. argvals[argcnt++] = cp;
  332. *cp++ = '-'; *cp++ = 'q'; *cp++ = '\0'; /* -q */
  333. argvals[argcnt++] = cp;
  334. *cp++ = '-'; *cp++ = 'd'; /* -d */
  335. CPYFIELD(bp, cp); /* printer */
  336. *cp++ = '\0';
  337. break;
  338. case '\002':
  339. bp++; /* drop the ctrl character from the input */
  340. argvals[argcnt++] = cp;
  341. *cp++ = '-'; *cp++ = 'd'; /* -d */
  342. CPYFIELD(bp, cp); /* printer */
  343. *cp++ = '\0';
  344. ACK();
  345. savargcnt = argcnt;
  346. savbufpnt = cp;
  347. while ((rv=getfiles())) {
  348. jinfop = getjobinfo(cntrlfd);
  349. close(cntrlfd);
  350. argcnt = savargcnt;
  351. cp = savbufpnt;
  352. argvals[argcnt++] = cp;
  353. *cp++ = '-'; *cp++ = 'M'; /* -M */
  354. bp = (unsigned char *)jinfop->host;
  355. CPYFIELD(bp, cp); /* host name */
  356. *cp++ = '\0';
  357. argvals[argcnt++] = cp;
  358. *cp++ = '-'; *cp++ = 'u'; /* -u */
  359. bp = (unsigned char *)jinfop->user;
  360. CPYFIELD(bp, cp); /* user name */
  361. *cp++ = '\0';
  362. for(i=0;i<rv;i++)
  363. forklp(datafd[i]);
  364. }
  365. exit(0);
  366. case '\005':
  367. bp++; /* drop the ctrl character from the input */
  368. argvals[argcnt++] = cp;
  369. *cp++ = '-'; *cp++ = 'k'; *cp++ = '\0'; /* -k */
  370. argvals[argcnt++] = cp;
  371. *cp++ = '-'; *cp++ = 'd'; /* -d */
  372. CPYFIELD(bp, cp); /* printer */
  373. *cp++ = '\0';
  374. argvals[argcnt++] = cp;
  375. *cp++ = '-'; ap = cp; *cp++ = 'u'; /* -u */
  376. CPYFIELD(bp, cp); /* username */
  377. /* deal with bug in lprng where the username is not supplied
  378. */
  379. if (ap == (cp-1)) {
  380. ap = (unsigned char *)"none";
  381. CPYFIELD(ap, cp);
  382. }
  383. *cp++ = '\0';
  384. datafd[0] = tempfile();
  385. blen = strlen((const char *)bp);
  386. if (write(datafd[0], bp, blen) != blen) {
  387. error("write error\n");
  388. exit(6);
  389. }
  390. if (write(datafd[0], "\n", 1) != 1) {
  391. error("write error\n");
  392. exit(6);
  393. }
  394. break;
  395. default:
  396. /* otherwise get my lp arguments */
  397. do {
  398. /* move to next non-white space */
  399. while (*bp==' '||*bp=='\t')
  400. ++bp;
  401. if (*bp=='\n') continue;
  402. /* only accept arguments beginning with -
  403. * this is done to prevent the printing of
  404. * local files from the destination host
  405. */
  406. if (*bp=='-') {
  407. argvals[argcnt++] = cp;
  408. saveflg = 1;
  409. } else
  410. saveflg = 0;
  411. /* move to next white space copying text to argument buffer */
  412. while (*bp!=' ' && *bp!='\t' && *bp!='\n'
  413. && *bp!='\0') {
  414. *cp = *bp++;
  415. cp += saveflg;
  416. }
  417. *cp = '\0';
  418. cp += saveflg;
  419. } while (*bp!='\n' && *bp!='\0');
  420. if (readline(0) < 0) exit(7);
  421. datafd[0] = tempfile();
  422. if(readfile(datafd[0], atoi((const char *)lnbuf)) < 0) {
  423. error("readfile failed\n");
  424. exit(8);
  425. }
  426. }
  427. forklp(datafd[0]);
  428. exit(0);
  429. }