History

update libauthsrv, use common readcons() function

cinap_lenrek committed 10 years ago

Commit 0a4c2981 parent e79c78b11 files · +142 −209Browse files

Unified · Split

11 files
  1. Makefile +0 −1
  2. cpu.c +9 −0
  3. drawterm.h +0 −1
  4. include/authsrv.h +4 −2
  5. kern/devcons.c +3 −3
  6. libauthsrv/Makefile +1 −0
  7. libauthsrv/authdial.c +2 −3
  8. libauthsrv/readcons.c +82 −0
  9. libauthsrv/readnvram.c +39 −88
  10. readcons.c +0 −110
  11. secstore.c +2 −1
Makefile+0−1
@@ -6,7 +6,6 @@ OFILES=\ · 5 unmodified lines
66 main.$O\
77 cpu.$O\
88 aan.$O\
9− readcons.$O\
109 secstore.$O\
1110 latin1.$O\
1211 $(OS)-factotum.$O\
cpu.c+9−0
@@ -36,6 +36,15 @@ static int p9authtls(int); · 35 unmodified lines
3636 char *authserver;
3737 char *secstore;
3838
39+char*
40+estrdup(char *s)
41+{
42+ s = strdup(s);
43+ if(s == nil)
44+ sysfatal("out of memory");
45+ return s;
46+}
47+
3948 static void
4049 ending(void)
4150 {
drawterm.h+0−1
@@ -3,7 +3,6 @@ extern char *secstore; · 2 unmodified lines
33 extern char secstorebuf[65536];
44 extern char *secstorefetch(char *addr, char *owner, char *passwd);
55 extern char *authserver;
6−extern char *readcons(char *prompt, char *def, int secret);
76 extern int exportfs(int);
87 extern char *user;
98 extern int dialfactotum(void);
include/authsrv.h+4−2
@@ -23,6 +23,7 @@ enum · 22 unmodified lines
2323 NETCHLEN= 16, /* max network challenge length (used in AS protocol) */
2424 CONFIGLEN= 14,
2525 SECRETLEN= 32, /* secret max size */
26+ PASSWDLEN= 28, /* password max size */
2627
2728 NONCELEN= 32,
2829
@@ -99,8 +100,8 @@ struct Authenticator · 70 unmodified lines
99100 struct Passwordreq
100101 {
101102 char num;
102− char old[ANAMELEN];
103− char new[ANAMELEN];
103+ char old[PASSWDLEN];
104+ char new[PASSWDLEN];
104105 char changesecret;
105106 char secret[SECRETLEN]; /* new secret */
106107 };
@@ -184,6 +185,7 @@ struct Nvrsafe · 77 unmodified lines
184185
185186 extern uchar nvcsum(void*, int);
186187 extern int readnvram(Nvrsafe*, int);
188+extern char* readcons(char*, char*, int);
187189
188190 /*
189191 * call up auth server
kern/devcons.c+3−3
@@ -487,7 +487,7 @@ consclose(Chan *c) · 486 unmodified lines
487487 }
488488
489489 static int
490−readcons(Queue *q, char *buf, int n)
490+qreadcons(Queue *q, char *buf, int n)
491491 {
492492 if(screenputs==0 && !qcanread(q))
493493 return read(0, buf, n);
@@ -521,7 +521,7 @@ consread(Chan *c, void *buf, long n, vlong off) · 27 unmodified lines
521521 else {
522522 /* read as much as possible */
523523 do {
524− i = readcons(kbdq, cbuf, n);
524+ i = qreadcons(kbdq, cbuf, n);
525525 cbuf += i;
526526 n -= i;
527527 } while (n>0 && qcanread(kbdq));
@@ -530,7 +530,7 @@ consread(Chan *c, void *buf, long n, vlong off) · 2 unmodified lines
530530 } else {
531531 while(!qcanread(lineq)) {
532532 eol = 1;
533− if(readcons(kbdq, &kbd.line[kbd.x], 1) == 1){
533+ if(qreadcons(kbdq, &kbd.line[kbd.x], 1) == 1){
534534 eol = 0;
535535 ch = kbd.line[kbd.x];
536536 switch(ch){
libauthsrv/Makefile+1−0
@@ -15,6 +15,7 @@ OFILES=\ · 14 unmodified lines
1515 convTR2M.$O\
1616 nvcsum.$O\
1717 passtokey.$O\
18+ readcons.$O\
1819 _asgetpakkey.$O\
1920 _asgetresp.$O\
2021 _asrequest.$O\
libauthsrv/authdial.c+2−3
@@ -13,7 +13,7 @@ authdial(char *netroot, char *dom) · 12 unmodified lines
1313
1414 if(dom == nil)
1515 /* look for one relative to my machine */
16− return dial(netmkaddr("$auth", netroot, "ticket"), 0, 0, 0);
16+ return dial(netmkaddr("$auth", nil, "ticket"), nil, nil, nil);
1717
1818 /* look up an auth server in an authentication domain */
1919 p = csgetvalue(netroot, "authdom", dom, "auth", &t);
@@ -39,8 +39,7 @@ authdial(char *netroot, char *dom) · 19 unmodified lines
3939 rv = -1;
4040 for(nt = t; nt != nil; nt = nt->entry) {
4141 if(strcmp(nt->attr, "auth") == 0) {
42− p = netmkaddr(nt->val, netroot, "ticket");
43− rv = dial(p, 0, 0, 0);
42+ rv = dial(netmkaddr(nt->val, nil, "ticket"), nil, nil, nil);
4443 if(rv >= 0)
4544 break;
4645 }
libauthsrv/readcons.c+82−0
@@ -0,0 +1,82 @@
1+#include <u.h>
2+#include <libc.h>
3+
4+/*
5+ * prompt for a string with a possible default response
6+ */
7+char*
8+readcons(char *prompt, char *def, int raw)
9+{
10+ int fdin, fdout, ctl, n;
11+ char *s, *p;
12+
13+ s = p = nil;
14+ fdout = ctl = -1;
15+
16+ if((fdin = open("/dev/cons", OREAD)) < 0)
17+ goto Out;
18+ if((fdout = open("/dev/cons", OWRITE)) < 0)
19+ goto Out;
20+
21+ if(raw){
22+ if((ctl = open("/dev/consctl", OWRITE)) < 0)
23+ goto Out;
24+ write(ctl, "rawon", 5);
25+ }
26+
27+ if(def != nil)
28+ fprint(fdout, "%s[%s]: ", prompt, def);
29+ else
30+ fprint(fdout, "%s: ", prompt);
31+
32+ for(;;){
33+ n = p - s;
34+ if((n % 32) == 0){
35+ if((p = realloc(s, n+32)) == nil)
36+ break;
37+ s = p, p += n;
38+ }
39+
40+ if(read(fdin, p, 1) <= 0 || *p == 0x7f)
41+ break;
42+
43+ if(*p == '\n' || *p == '\r'){
44+ if(p == s && def != nil){
45+ free(s);
46+ s = strdup(def);
47+ } else
48+ *p = 0;
49+ if(raw)
50+ write(fdout, "\n", 1);
51+ goto Out;
52+ } else if(*p == '\b') {
53+ while(p > s && (p[-1] & 0xc0) == 0x80)
54+ *p-- = 0;
55+ if(p > s)
56+ *p-- = 0;
57+ } else if(*p == 0x15) { /* ^U: line kill */
58+ if(def != nil)
59+ fprint(fdout, "\n%s[%s]: ", prompt, def);
60+ else
61+ fprint(fdout, "\n%s: ", prompt);
62+ while(p > s)
63+ *p-- = 0;
64+ } else if(*p >= ' ')
65+ p++;
66+ }
67+ free(s);
68+ s = nil;
69+ if(raw)
70+ write(fdout, "\n", 1);
71+Out:
72+ if(ctl >= 0){
73+ write(ctl, "rawoff", 6);
74+ close(ctl);
75+ }
76+ if(fdin >= 0)
77+ close(fdin);
78+ if(fdout >= 0)
79+ close(fdout);
80+
81+ return s;
82+}
libauthsrv/readnvram.c+39−88
@@ -53,78 +53,6 @@ static struct { · 52 unmodified lines
5353 "debug", "/tmp/nvram", 0, sizeof(Nvrsafe),
5454 };
5555
56−static char*
57−readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
58−{
59− int fdin, fdout, ctl, n, m;
60− char line[10];
61−
62− fdin = open("/dev/cons", OREAD);
63− if(fdin < 0)
64− fdin = 0;
65− fdout = open("/dev/cons", OWRITE);
66− if(fdout < 0)
67− fdout = 1;
68− if(def != nil)
69− fprint(fdout, "%s[%s]: ", prompt, def);
70− else
71− fprint(fdout, "%s: ", prompt);
72− if(raw){
73− ctl = open("/dev/consctl", OWRITE);
74− if(ctl >= 0)
75− write(ctl, "rawon", 5);
76− } else
77− ctl = -1;
78−
79− m = 0;
80− for(;;){
81− n = read(fdin, line, 1);
82− if(n == 0){
83− close(ctl);
84− werrstr("readcons: EOF");
85− return nil;
86− }
87− if(n < 0){
88− close(ctl);
89− werrstr("can't read cons");
90− return nil;
91− }
92− if(line[0] == 0x7f)
93− exits(0);
94− if(n == 0 || line[0] == '\n' || line[0] == '\r'){
95− if(raw){
96− write(ctl, "rawoff", 6);
97− write(fdout, "\n", 1);
98− close(ctl);
99− }
100− buf[m] = '\0';
101− if(buf[0]=='\0' && def)
102− strcpy(buf, def);
103− return buf;
104− }
105− if(line[0] == '\b'){
106− if(m > 0)
107− m--;
108− }else if(line[0] == 0x15){ /* ^U: line kill */
109− m = 0;
110− if(def != nil)
111− fprint(fdout, "%s[%s]: ", prompt, def);
112− else
113− fprint(fdout, "%s: ", prompt);
114− }else{
115− if(m >= nbuf-1){
116− fprint(fdout, "line too long\n");
117− m = 0;
118− if(def != nil)
119− fprint(fdout, "%s[%s]: ", prompt, def);
120− else
121− fprint(fdout, "%s: ", prompt);
122− }else
123− buf[m++] = line[0];
124− }
125− }
126−}
127−
12856 typedef struct {
12957 int fd;
13058 int safelen;
@@ -211,6 +139,29 @@ findnvram(Nvrwhere *locp) · 80 unmodified lines
211139 locp->safeoff = safeoff;
212140 }
213141
142+static int
143+ask(char *prompt, char *buf, int len, int raw)
144+{
145+ char *s;
146+ int n;
147+
148+ memset(buf, 0, len);
149+ for(;;){
150+ if((s = readcons(prompt, nil, raw)) == nil)
151+ return -1;
152+ if((n = strlen(s)) >= len)
153+ fprint(2, "%s longer than %d characters; try again\n", prompt, len-1);
154+ else {
155+ memmove(buf, s, n);
156+ memset(s, 0, n);
157+ free(s);
158+ return 0;
159+ }
160+ memset(s, 0, n);
161+ free(s);
162+ }
163+}
164+
214165 /*
215166 * get key info out of nvram. since there isn't room in the PC's nvram use
216167 * a disk partition there.
@@ -219,7 +170,7 @@ int · 2 unmodified lines
219170 readnvram(Nvrsafe *safep, int flag)
220171 {
221172 int err;
222− char buf[512], in[128]; /* 512 for floppy i/o */
173+ char buf[512]; /* 512 for floppy i/o */
223174 Nvrsafe *safe;
224175 Nvrwhere loc;
225176
@@ -294,22 +245,22 @@ readnvram(Nvrsafe *safep, int flag) · 68 unmodified lines
294245
295246 if((flag&(NVwrite|NVwritemem)) || (err && (flag&NVwriteonerr))){
296247 if (!(flag&NVwritemem)) {
297− readcons("authid", nil, 0, safe->authid,
298− sizeof safe->authid);
299− readcons("authdom", nil, 0, safe->authdom,
300− sizeof safe->authdom);
301− readcons("secstore key", nil, 1, safe->config,
302− sizeof safe->config);
303− for(;;){
304− Authkey k;
248+ char pass[PASSWDLEN];
249+ Authkey k;
305250
306− if(readcons("password", nil, 1, in, sizeof in) == nil)
307− goto Out;
308− passtokey(&k, in);
309− memmove(safe->machkey, k.des, DESKEYLEN);
310− memmove(safe->aesmachkey, k.aes, AESKEYLEN);
311− break;
312− }
251+ if(ask("authid", safe->authid, sizeof safe->authid, 0))
252+ goto Out;
253+ if(ask("authdom", safe->authdom, sizeof safe->authdom, 0))
254+ goto Out;
255+ if(ask("secstore key", safe->config, sizeof safe->config, 1))
256+ goto Out;
257+ if(ask("password", pass, sizeof pass, 1))
258+ goto Out;
259+ passtokey(&k, pass);
260+ memset(pass, 0, sizeof pass);
261+ memmove(safe->machkey, k.des, DESKEYLEN);
262+ memmove(safe->aesmachkey, k.aes, AESKEYLEN);
263+ memset(&k, 0, sizeof k);
313264 }
314265
315266 safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
readcons.c+0−110
@@ -1,110 +0,0 @@
1−#include <u.h>
2−#include <libc.h>
3−#include "drawterm.h"
4−
5−void*
6−erealloc(void *v, ulong n)
7−{
8− v = realloc(v, n);
9− if(v == nil && n != 0)
10− sysfatal("out of memory");
11− return v;
12−}
13−
14−char*
15−estrdup(char *s)
16−{
17− s = strdup(s);
18− if(s == nil)
19− sysfatal("out of memory");
20− return s;
21−}
22−
23−char*
24−estrappend(char *s, char *fmt, ...)
25−{
26− char *t;
27− va_list arg;
28−
29− va_start(arg, fmt);
30− t = vsmprint(fmt, arg);
31− if(t == nil)
32− sysfatal("out of memory");
33− va_end(arg);
34− s = erealloc(s, strlen(s)+strlen(t)+1);
35− strcat(s, t);
36− free(t);
37− return s;
38−}
39−
40−/*
41− * prompt for a string with a possible default response
42− */
43−char*
44−readcons(char *prompt, char *def, int raw)
45−{
46− int fdin, fdout, ctl, n;
47− char line[10];
48− char *s;
49−
50− fdin = open("/dev/cons", OREAD);
51− if(fdin < 0)
52− fdin = 0;
53− fdout = open("/dev/cons", OWRITE);
54− if(fdout < 0)
55− fdout = 1;
56− if(def != nil)
57− fprint(fdout, "%s[%s]: ", prompt, def);
58− else
59− fprint(fdout, "%s: ", prompt);
60− if(raw){
61− ctl = open("/dev/consctl", OWRITE);
62− if(ctl >= 0)
63− write(ctl, "rawon", 5);
64− } else
65− ctl = -1;
66− s = estrdup("");
67− for(;;){
68− n = read(fdin, line, 1);
69− if(n == 0){
70− Error:
71− close(fdin);
72− close(fdout);
73− if(ctl >= 0)
74− close(ctl);
75− free(s);
76− return nil;
77− }
78− if(n < 0)
79− goto Error;
80− if(line[0] == 0x7f)
81− goto Error;
82− if(n == 0 || line[0] == '\n' || line[0] == '\r'){
83− if(raw){
84− write(ctl, "rawoff", 6);
85− write(fdout, "\n", 1);
86− }
87− close(ctl);
88− close(fdin);
89− close(fdout);
90− if(*s == 0 && def != nil)
91− s = estrappend(s, "%s", def);
92− return s;
93− }
94− if(line[0] == '\b'){
95− if(strlen(s) > 0)
96− s[strlen(s)-1] = 0;
97− } else if(line[0] == 0x15) { /* ^U: line kill */
98− if(def != nil)
99− fprint(fdout, "\n%s[%s]: ", prompt, def);
100− else
101− fprint(fdout, "\n%s: ", prompt);
102−
103− s[0] = 0;
104− } else {
105− s = estrappend(s, "%c", line[0]);
106− }
107− }
108− return nil; /* not reached */
109−}
110−
secstore.c+2−1
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 #include <libc.h>
88 #include <mp.h>
99 #include <libsec.h>
10+#include <authsrv.h>
1011 #include "drawterm.h"
1112
1213 static void*
@@ -185,7 +186,7 @@ verify(uchar secret[SHA1dlen], uchar *data, int len, int seqno, uchar d[SHA1dlen · 172 unmodified lines
185186 sha1(secret, SHA1dlen, nil, &sha);
186187 sha1(data, len, nil, &sha);
187188 sha1(seq, 4, digest, &sha);
188− return memcmp(d, digest, SHA1dlen);
189+ return tsmemcmp(d, digest, SHA1dlen);
189190 }
190191
191192 static int