1 #include <u.h> 2 #include <libc.h> 3 #include <authsrv.h> 4 #include "authcmdlib.h" 5 6 void 7 getpass(char *key, char *pass, int check, int confirm) 8 { 9 char rpass[32], npass[32]; 10 char *err; 11 12 if(pass == nil) 13 pass = npass; 14 15 for(;;){ 16 readln("Password: ", pass, sizeof npass, 1); 17 if(confirm){ 18 readln("Confirm password: ", rpass, sizeof rpass, 1); 19 if(strcmp(pass, rpass) != 0){ 20 print("mismatch, try again\n"); 21 continue; 22 } 23 } 24 if(!passtokey(key, pass)){ 25 print("bad password, try again\n"); 26 continue; 27 } 28 if(check) 29 if(err = okpasswd(pass)){ 30 print("%s, try again\n", err); 31 continue; 32 } 33 break; 34 } 35 } 36 37 int 38 getsecret(int passvalid, char *p9pass) 39 { 40 char answer[32]; 41 42 readln("assign Inferno/POP secret? (y/n) ", answer, sizeof answer, 0); 43 if(*answer != 'y' && *answer != 'Y') 44 return 0; 45 46 if(passvalid){ 47 readln("make it the same as the plan 9 password? (y/n) ", 48 answer, sizeof answer, 0); 49 if(*answer == 'y' || *answer == 'Y') 50 return 1; 51 } 52 53 for(;;){ 54 readln("Secret(0 to 256 characters): ", p9pass, 55 sizeof answer, 1); 56 readln("Confirm: ", answer, sizeof answer, 1); 57 if(strcmp(p9pass, answer) == 0) 58 break; 59 print("mismatch, try again\n"); 60 } 61 return 1; 62 } 63 64 void 65 readln(char *prompt, char *line, int len, int raw) 66 { 67 char *p; 68 int fdin, fdout, ctl, n, nr; 69 70 fdin = open("/dev/cons", OREAD); 71 fdout = open("/dev/cons", OWRITE); 72 fprint(fdout, "%s", prompt); 73 if(raw){ 74 ctl = open("/dev/consctl", OWRITE); 75 if(ctl < 0) 76 error("couldn't set raw mode"); 77 write(ctl, "rawon", 5); 78 } else 79 ctl = -1; 80 nr = 0; 81 p = line; 82 for(;;){ 83 n = read(fdin, p, 1); 84 if(n < 0){ 85 close(ctl); 86 error("can't read cons\n"); 87 } 88 if(*p == 0x7f) 89 exits(0); 90 if(n == 0 || *p == '\n' || *p == '\r'){ 91 *p = '\0'; 92 if(raw){ 93 write(ctl, "rawoff", 6); 94 write(fdout, "\n", 1); 95 } 96 close(ctl); 97 return; 98 } 99 if(*p == '\b'){ 100 if(nr > 0){ 101 nr--; 102 p--; 103 } 104 }else{ 105 nr++; 106 p++; 107 } 108 if(nr == len){ 109 fprint(fdout, "line too long; try again\n"); 110 nr = 0; 111 p = line; 112 } 113 } 114 } 115