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