1 #include <u.h> 2 #include <libc.h> 3 #include <auth.h> 4 5 int readln(char*, char*, int); 6 7 void 8 error(char *fmt, ...) 9 { 10 char buf[8192], *s; 11 12 s = buf; 13 s += sprint(s, "netkey: "); 14 s = doprint(s, buf + sizeof(buf) / sizeof(*buf), fmt, &fmt + 1); 15 *s++ = '\n'; 16 write(2, buf, s - buf); 17 exits(buf); 18 } 19 20 void 21 usage(void) 22 { 23 fprint(2, "usage: netkey\n"); 24 exits("usage"); 25 } 26 27 void 28 main(int argc, char *argv[]) 29 { 30 char buf[32], pass[32], key[DESKEYLEN]; 31 char *s; 32 int n; 33 34 ARGBEGIN{ 35 default: 36 usage(); 37 }ARGEND 38 if(argc) 39 usage(); 40 41 s = getenv("service"); 42 if(s && strcmp(s, "cpu") == 0){ 43 fprint(2, "netkey must not be run on the cpu server\n"); 44 exits("boofhead"); 45 } 46 47 readln("Password: ", pass, sizeof pass); 48 passtokey(key, pass); 49 50 for(;;){ 51 print("challenge: "); 52 n = read(0, buf, sizeof buf - 1); 53 if(n <= 0) 54 exits(0); 55 buf[n] = '\0'; 56 n = strtol(buf, 0, 10); 57 sprint(buf, "%d", n); 58 netcrypt(key, buf); 59 print("response: %s\n", buf); 60 } 61 } 62 63 int 64 readln(char *prompt, char *line, int len) 65 { 66 char *p; 67 int fd, ctl, n, nr; 68 69 fd = open("/dev/cons", ORDWR); 70 if(fd < 0) 71 error("couldn't open cons"); 72 ctl = open("/dev/consctl", OWRITE); 73 if(ctl < 0) 74 error("couldn't set raw mode"); 75 write(ctl, "rawon", 5); 76 fprint(fd, "%s", prompt); 77 nr = 0; 78 p = line; 79 for(;;){ 80 n = read(fd, p, 1); 81 if(n < 0){ 82 close(fd); 83 close(ctl); 84 return -1; 85 } 86 if(n == 0 || *p == '\n' || *p == '\r'){ 87 *p = '\0'; 88 write(fd, "\n", 1); 89 close(fd); 90 close(ctl); 91 return nr; 92 } 93 if(*p == '\b'){ 94 if(nr > 0){ 95 nr--; 96 p--; 97 } 98 }else if(*p == 21){ /* cntrl-u */ 99 fprint(fd, "\n%s", prompt); 100 nr = 0; 101 p = line; 102 }else{ 103 nr++; 104 p++; 105 } 106 if(nr == len){ 107 fprint(fd, "line too long; try again\n%s", prompt); 108 nr = 0; 109 p = line; 110 } 111 } 112 return -1; 113 } 114