From b2ad2ef1387571c7b917a7fd63e8670582ae8b7f Mon Sep 17 00:00:00 2001 From: rsc Date: Tue, 4 Jan 2005 22:30:59 +0000 Subject: in with the new --- src/cmd/acmeevent.c | 93 +++++++++++++++++++++++++++++++++ src/cmd/aescbc.c | 144 ---------------------------------------------------- src/cmd/delatex.lx | 64 +++++++++++++++++++++++ src/cmd/dial.c | 62 ++++++++++++++++++++++ src/cmd/read.c | 91 +++++++++++++++++++++++++++++++++ 5 files changed, 310 insertions(+), 144 deletions(-) create mode 100644 src/cmd/acmeevent.c delete mode 100644 src/cmd/aescbc.c create mode 100644 src/cmd/delatex.lx create mode 100644 src/cmd/dial.c create mode 100644 src/cmd/read.c (limited to 'src') diff --git a/src/cmd/acmeevent.c b/src/cmd/acmeevent.c new file mode 100644 index 00000000..96e8617c --- /dev/null +++ b/src/cmd/acmeevent.c @@ -0,0 +1,93 @@ +#include +#include +#include + + +int +getn(Biobuf *b) +{ + int c, n; + + n = 0; + while((c = Bgetc(b)) != -1 && '0'<=c && c<='9') + n = n*10+c-'0'; + if(c != ' ') + sysfatal("bad number syntax"); + return n; +} + +char* +getrune(Biobuf *b, char *p) +{ + int c; + char *q; + + c = Bgetc(b); + if(c == -1) + sysfatal("eof"); + q = p; + *q++ = c; + if(c >= Runeself){ + while(!fullrune(p, q-p)){ + c = Bgetc(b); + if(c == -1) + sysfatal("eof"); + *q++ = c; + } + } + return q; +} + +void +getevent(Biobuf *b, int *c1, int *c2, int *q0, int *q1, int *flag, int *nr, char *buf) +{ + int i; + char *p; + + *c1 = Bgetc(b); + if(*c1 == -1) + exits(0); + *c2 = Bgetc(b); + *q0 = getn(b); + *q1 = getn(b); + *flag = getn(b); + *nr = getn(b); + if(*nr >= 256) + sysfatal("event string too long"); + p = buf; + for(i=0; i<*nr; i++) + p = getrune(b, p); + *p = 0; + if(Bgetc(b) != '\n') + sysfatal("expected newline"); +} + +void +main(void) +{ + int c1, c2, q0, q1, eq0, eq1, flag, nr, x; + Biobuf b; + char buf[2000], buf2[2000], buf3[2000]; + + doquote = needsrcquote; + quotefmtinstall(); + Binit(&b, 0, OREAD); + for(;;){ + getevent(&b, &c1, &c2, &q0, &q1, &flag, &nr, buf); + eq0 = q0; + eq1 = q1; + buf2[0] = 0; + buf3[0] = 0; + if(flag & 2){ + /* null string with non-null expansion */ + getevent(&b, &x, &x, &eq0, &eq1, &x, &nr, buf); + } + if(flag & 8){ + /* chorded argument */ + getevent(&b, &x, &x, &x, &x, &x, &x, buf2); + getevent(&b, &x, &x, &x, &x, &x, &x, buf3); + } + print("event %c %c %d %d %d %d %d %d %q %q %q\n", + c1, c2, q0, q1, eq0, eq1, flag, nr, buf, buf2, buf3); + } +} diff --git a/src/cmd/aescbc.c b/src/cmd/aescbc.c deleted file mode 100644 index 9f4bfff9..00000000 --- a/src/cmd/aescbc.c +++ /dev/null @@ -1,144 +0,0 @@ -/* encrypt file by writing - v2hdr, - 16byte initialization vector, - AES-CBC(key, random | file), - HMAC_SHA1(md5(key), AES-CBC(random | file)) - -With CBC, if the first plaintext block is 0, the first ciphertext block is -E(IV). Using the overflow technique adopted for compatibility with cryptolib -makes the last cipertext block decryptable. Hence the random prefix to file. -*/ -#include -#include -#include -#include -#include - -enum{ CHK = 16, BUF = 4096 }; - -uchar v2hdr[AESbsize+1] = "AES CBC SHA1 2\n"; -Biobuf bin; -Biobuf bout; - -void -safewrite(uchar *buf, int n) -{ - int i = Bwrite(&bout, buf, n); - - if(i == n) - return; - fprint(2, "write error\n"); - exits("write error"); -} - -void -saferead(uchar *buf, int n) -{ - int i = Bread(&bin, buf, n); - - if(i == n) - return; - fprint(2, "read error\n"); - exits("read error"); -} - -int -main(int argc, char **argv) -{ - int encrypt = 0; /* 0=decrypt, 1=encrypt */ - int n, nkey; - char *hex, *msg = nil; - uchar key[AESmaxkey], key2[MD5dlen]; - uchar buf[BUF+SHA1dlen]; /* assumption: CHK <= SHA1dlen */ - AESstate aes; - DigestState *dstate; - - if(argc!=2 || argv[1][0]!='-'){ - fprint(2,"usage: HEX=key %s -d < cipher.aes > clear.txt\n", argv[0]); - fprint(2," or: HEX=key %s -e < clear.txt > cipher.aes\n", argv[0]); - exits("usage"); - } - if(argv[1][1] == 'e') - encrypt = 1; - Binit(&bin, 0, OREAD); - Binit(&bout, 1, OWRITE); - - if((hex = getenv("HEX")) == nil) - hex = getpass("enter key: "); - nkey = 0; - if(hex != nil) - nkey = strlen(hex); - if(nkey == 0 || (nkey&1) || nkey>2*AESmaxkey){ - fprint(2,"key should be 32 hex digits\n"); - exits("key"); - } - nkey = dec16(key, sizeof key, hex, nkey); - md5(key, nkey, key2, 0); /* so even if HMAC_SHA1 is broken, encryption key is protected */ - - if(encrypt){ - safewrite(v2hdr, AESbsize); - genrandom(buf,2*AESbsize); /* CBC is semantically secure if IV is unpredictable. */ - setupAESstate(&aes, key, nkey, buf); /* use first AESbsize bytes as IV */ - aesCBCencrypt(buf+AESbsize, AESbsize, &aes); /* use second AESbsize bytes as initial plaintext */ - safewrite(buf, 2*AESbsize); - dstate = hmac_sha1(buf+AESbsize, AESbsize, key2, MD5dlen, 0, 0); - while(1){ - n = Bread(&bin, buf, BUF); - if(n < 0){ - msg = "read error"; - goto Exit; - } - aesCBCencrypt(buf, n, &aes); - safewrite(buf, n); - dstate = hmac_sha1(buf, n, key2, MD5dlen, 0, dstate); - if(n < BUF) - break; /* EOF */ - } - hmac_sha1(0, 0, key2, MD5dlen, buf, dstate); - safewrite(buf, SHA1dlen); - }else{ /* decrypt */ - Bread(&bin, buf, AESbsize); - if(memcmp(buf, v2hdr, AESbsize) == 0){ - saferead(buf, 2*AESbsize); /* read IV and random initial plaintext */ - setupAESstate(&aes, key, nkey, buf); - dstate = hmac_sha1(buf+AESbsize, AESbsize, key2, MD5dlen, 0, 0); - aesCBCdecrypt(buf+AESbsize, AESbsize, &aes); - saferead(buf, SHA1dlen); - while((n = Bread(&bin, buf+SHA1dlen, BUF)) > 0){ - dstate = hmac_sha1(buf, n, key2, MD5dlen, 0, dstate); - aesCBCdecrypt(buf, n, &aes); - safewrite(buf, n); - memmove(buf, buf+n, SHA1dlen); /* these bytes are not yet decrypted */ - } - hmac_sha1(0, 0, key2, MD5dlen, buf+SHA1dlen, dstate); - if(memcmp(buf, buf+SHA1dlen, SHA1dlen) != 0){ - msg = "decrypted file failed to authenticate!"; - goto Exit; - } - }else{ /* compatibility with past mistake */ - // if file was encrypted with bad aescbc use this: - // memset(key, 0, AESmaxkey); - // else assume we're decrypting secstore files - setupAESstate(&aes, key, 0, buf); - saferead(buf, CHK); - aesCBCdecrypt(buf, CHK, &aes); - while((n = Bread(&bin, buf+CHK, BUF)) > 0){ - aesCBCdecrypt(buf+CHK, n, &aes); - safewrite(buf, n); - memmove(buf, buf+n, CHK); - } - if(memcmp(buf, "XXXXXXXXXXXXXXXX", CHK) != 0){ - msg = "decrypted file failed to authenticate"; - goto Exit; - } - } - } - Exit: - memset(key, 0, sizeof(key)); - memset(key2, 0, sizeof(key2)); - memset(buf, 0, sizeof(buf)); - if(msg != nil) - fprint(2, "%s\n", msg); - exits(msg); - return 1; /* gcc */ -} diff --git a/src/cmd/delatex.lx b/src/cmd/delatex.lx new file mode 100644 index 00000000..063aa7dc --- /dev/null +++ b/src/cmd/delatex.lx @@ -0,0 +1,64 @@ +/* Make this with: lex delatex.lex; cc lex.yy.c -ll -o delatex */ +L [A-Za-z] +%Start Display Math Normal Tag +%% +\' {yyleng--; yymore(); /* ignore apostrophes */} +{L}+\\- {yyleng-=2; yymore(); /* ignore hyphens */} +[a-z]/[^A-Za-z] ; /* ignore single letter "words" */ +[A-Z]+ ; /* ignore words all in uppercase */ +{L}+('{L}*)*{L} {printf("%s\n",yytext); /* any other letter seq is a word */} +"%".* ; /* ignore comments */ +\\{L}+ ; /* ignore other control sequences */ +"\\begin{" BEGIN Tag; /* ignore this and up to next "}" */ +"\\bibitem{" BEGIN Tag; +"\\bibliography{" BEGIN Tag; +"\\bibstyle{" BEGIN Tag; +"\\cite{" BEGIN Tag; +"\\end{" BEGIN Tag; +"\\include{" BEGIN Tag; +"\\includeonly{" BEGIN Tag; +"\\input{" BEGIN Tag; +"\\label{" BEGIN Tag; +"\\pageref{" BEGIN Tag; +"\\ref{" BEGIN Tag; +[^}] ; /* ignore things up to next "}" */ +"}" BEGIN Normal; +[0-9]+ ; /* ignore numbers */ +"\\(" BEGIN Math; /* begin latex math mode */ +"\\)" BEGIN Normal; /* end latex math mode */ +.|\\[^)]|\n ; /* ignore anything else in latex math mode */ +"\\[" BEGIN Display; /* now in Latex display mode */ +[^$]|\\[^\]] ; /* ignore most things in display math mode */ +"\\]" BEGIN Normal; /* get out of Display math mode */ +\\. ; /* ignore other single character control sequences */ +\\\n ; /* more of the same */ +\n|. ; /* ignore anything else, a character at a time */ +%% +#include +#include + +int +main(int argc, char **argv) +{ + int i; + + BEGIN Normal; /* Starts yylex off in the right state */ + if (argc==1) { + yyin = stdin; + yylex(); + } + else for (i=1; i +#include + +void +usage(void) +{ + fprint(2, "usage: dial [-e] addr\n"); + exits("usage"); +} + +void +killer(void *x, char *msg) +{ + USED(x); + if(strcmp(msg, "kill") == 0) + exits(0); + noted(NDFLT); +} + +void +main(int argc, char **argv) +{ + int fd, pid; + char buf[8192]; + int n, waitforeof; + + notify(killer); + waitforeof = 0; + ARGBEGIN{ + case 'e': + waitforeof = 1; + break; + default: + usage(); + }ARGEND + + if(argc != 1) + usage(); + + if((fd = dial(argv[0], nil, nil, nil)) < 0) + sysfatal("dial: %r"); + + switch(pid = fork()){ + case -1: + sysfatal("fork: %r"); + case 0: + while((n = read(0, buf, sizeof buf)) > 0) + if(write(0, buf, n) < 0) + break; + if(!waitforeof) + postnote(PNPROC, getppid(), "kill"); + exits(nil); + } + + while((n = read(fd, buf, sizeof buf)) > 0) + if(write(1, buf, n) < 0) + break; + + postnote(PNPROC, pid, "kill"); + waitpid(); + exits(0); +} diff --git a/src/cmd/read.c b/src/cmd/read.c new file mode 100644 index 00000000..56d80136 --- /dev/null +++ b/src/cmd/read.c @@ -0,0 +1,91 @@ +#include +#include + +int multi; +int nlines; +char *status = nil; + +int +line(int fd, char *file) +{ + char c; + int m, n, nalloc; + char *buf; + + nalloc = 0; + buf = nil; + for(m=0; ; ){ + n = read(fd, &c, 1); + if(n < 0){ + fprint(2, "read: error reading %s: %r\n", file); + exits("read error"); + } + if(n == 0){ + if(m == 0) + status = "eof"; + break; + } + if(m == nalloc){ + nalloc += 1024; + buf = realloc(buf, nalloc); + if(buf == nil){ + fprint(2, "read: malloc error: %r\n"); + exits("malloc"); + } + } + buf[m++] = c; + if(c == '\n') + break; + } + if(m > 0) + write(1, buf, m); + free(buf); + return m; +} + +void +lines(int fd, char *file) +{ + do{ + if(line(fd, file) == 0) + break; + }while(multi || --nlines>0); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + char *s; + + ARGBEGIN{ + case 'm': + multi = 1; + break; + case 'n': + s = ARGF(); + if(s){ + nlines = atoi(s); + break; + } + /* fall through */ + default: + fprint(2, "usage: read [-m] [-n nlines] [files...]\n"); + exits("usage"); + }ARGEND + + if(argc == 0) + lines(0, ""); + else + for(i=0; i