1 #include <u.h> 2 #include <libc.h> 3 #include <mp.h> 4 #include <libsec.h> 5 #include "SConn.h" 6 #include "secstore.h" 7 8 void * 9 emalloc(ulong n) 10 { 11 void *p = malloc(n); 12 13 if(p == nil) 14 sysfatal("emalloc"); 15 memset(p, 0, n); 16 return p; 17 } 18 19 void * 20 erealloc(void *p, ulong n) 21 { 22 if ((p = realloc(p, n)) == nil) 23 sysfatal("erealloc"); 24 return p; 25 } 26 27 char * 28 estrdup(char *s) 29 { 30 if ((s = strdup(s)) == nil) 31 sysfatal("estrdup"); 32 return s; 33 } 34 35 char* 36 getpassm(char *prompt) 37 { 38 char *p, line[4096]; 39 int n, nr; 40 static int cons, consctl; /* closing & reopening fails in ssh environment */ 41 42 if(cons == 0){ /* first time? */ 43 cons = open("/dev/cons", ORDWR); 44 if(cons < 0) 45 sysfatal("couldn't open cons"); 46 consctl = open("/dev/consctl", OWRITE); 47 if(consctl < 0) 48 sysfatal("couldn't set raw mode via consctl"); 49 } 50 fprint(consctl, "rawon"); 51 fprint(cons, "%s", prompt); 52 nr = 0; 53 p = line; 54 for(;;){ 55 n = read(cons, p, 1); 56 if(n < 0){ 57 fprint(consctl, "rawoff"); 58 fprint(cons, "\n"); 59 return nil; 60 } 61 if(n == 0 || *p == '\n' || *p == '\r' || *p == 0x7f){ 62 *p = '\0'; 63 fprint(consctl, "rawoff"); 64 fprint(cons, "\n"); 65 p = strdup(line); 66 memset(line, 0, nr); 67 return p; 68 } 69 if(*p == '\b'){ 70 if(nr > 0){ 71 nr--; 72 p--; 73 } 74 }else if(*p == ('u' & 037)){ /* cntrl-u */ 75 fprint(cons, "\n%s", prompt); 76 nr = 0; 77 p = line; 78 }else{ 79 nr++; 80 p++; 81 } 82 if(nr+1 == sizeof line){ 83 fprint(cons, "line too long; try again\n%s", prompt); 84 nr = 0; 85 p = line; 86 } 87 } 88 } 89 90 static char * 91 illegal(char *f) 92 { 93 syslog(0, LOG, "illegal name: %s", f); 94 return nil; 95 } 96 97 char * 98 validatefile(char *f) 99 { 100 char *p; 101 102 if(f == nil || *f == '\0') 103 return nil; 104 if(strcmp(f, "..") == 0 || strlen(f) >= 250) 105 return illegal(f); 106 for(p = f; *p; p++) 107 if(*p < 040 || *p == '/') 108 return illegal(f); 109 return f; 110 } 111