1 #include <u.h> 2 #include <libc.h> 3 #include <auth.h> 4 #include "authsrv.h" 5 6 int 7 readfile(char *file, char *buf, int n) 8 { 9 int fd; 10 11 fd = open(file, OREAD); 12 if(fd < 0){ 13 werrstr("%s: %r", file); 14 return -1; 15 } 16 n = read(fd, buf, n); 17 close(fd); 18 return n; 19 } 20 21 int 22 writefile(char *file, char *buf, int n) 23 { 24 int fd; 25 26 fd = open(file, OWRITE); 27 if(fd < 0) 28 return -1; 29 n = write(fd, buf, n); 30 close(fd); 31 return n; 32 } 33 34 char* 35 findkey(char *db, char *user, char *key) 36 { 37 int n; 38 char filename[3*NAMELEN]; 39 40 sprint(filename, "%s/%s/key", db, user); 41 n = readfile(filename, key, DESKEYLEN); 42 if(n != DESKEYLEN) 43 return 0; 44 else 45 return key; 46 } 47 48 char* 49 setkey(char *db, char *user, char *key) 50 { 51 int n; 52 char filename[3*NAMELEN]; 53 54 sprint(filename, "%s/%s/key", db, user); 55 n = writefile(filename, key, DESKEYLEN); 56 if(n != DESKEYLEN) 57 return 0; 58 else 59 return key; 60 } 61