auth.c (2336B)
1 #include "u.h" 2 #include "lib.h" 3 #include "mem.h" 4 #include "dat.h" 5 #include "fns.h" 6 #include "error.h" 7 8 #include "authsrv.h" 9 10 char *eve; 11 char hostdomain[DOMLEN]; 12 13 /* 14 * return true if current user is eve 15 */ 16 int 17 iseve(void) 18 { 19 return strcmp(eve, up->user) == 0; 20 } 21 22 long 23 sysfversion(uint32 *arg) 24 { 25 char *vers; 26 uint arglen, m, msize; 27 Chan *c; 28 29 msize = arg[1]; 30 arglen = arg[3]; 31 vers = uvalidaddr(arg[2], arglen, 1); 32 /* check there's a NUL in the version string */ 33 if(arglen==0 || memchr(vers, 0, arglen)==0) 34 error(Ebadarg); 35 c = fdtochan(arg[0], ORDWR, 0, 1); 36 if(waserror()){ 37 cclose(c); 38 nexterror(); 39 } 40 41 m = mntversion(c, vers, msize, arglen); 42 43 cclose(c); 44 poperror(); 45 return m; 46 } 47 48 long 49 sys_fsession(uint32 *arg) 50 { 51 /* deprecated; backwards compatibility only */ 52 53 if(arg[2] == 0) 54 error(Ebadarg); 55 *(char*)uvalidaddr(arg[1], arg[2], 1) = '\0'; 56 return 0; 57 } 58 59 long 60 sysfauth(uint32 *arg) 61 { 62 Chan *c, *ac; 63 char *aname; 64 int fd; 65 66 aname = validnamedup(uvalidaddr(arg[1], 1, 0), 1); 67 if(waserror()){ 68 free(aname); 69 nexterror(); 70 } 71 c = fdtochan(arg[0], ORDWR, 0, 1); 72 if(waserror()){ 73 cclose(c); 74 nexterror(); 75 } 76 77 ac = mntauth(c, aname); 78 /* at this point ac is responsible for keeping c alive */ 79 cclose(c); 80 poperror(); /* c */ 81 free(aname); 82 poperror(); /* aname */ 83 84 if(waserror()){ 85 cclose(ac); 86 nexterror(); 87 } 88 89 fd = newfd(ac); 90 if(fd < 0) 91 error(Enofd); 92 poperror(); /* ac */ 93 94 /* always mark it close on exec */ 95 ac->flag |= CCEXEC; 96 97 return fd; 98 } 99 100 /* 101 * called by devcons() for user device 102 * 103 * anyone can become none 104 */ 105 long 106 userwrite(char *a, int n) 107 { 108 if(n!=4 || strncmp(a, "none", 4)!=0) 109 error(Eperm); 110 kstrdup(&up->user, "none"); 111 up->basepri = PriNormal; 112 return n; 113 } 114 115 /* 116 * called by devcons() for host owner/domain 117 * 118 * writing hostowner also sets user 119 */ 120 long 121 hostownerwrite(char *a, int n) 122 { 123 char buf[128]; 124 125 if(!iseve()) 126 error(Eperm); 127 if(n <= 0 || n >= sizeof buf) 128 error(Ebadarg); 129 memmove(buf, a, n); 130 buf[n] = 0; 131 132 renameuser(eve, buf); 133 kstrdup(&eve, buf); 134 kstrdup(&up->user, buf); 135 up->basepri = PriNormal; 136 return n; 137 } 138 139 long 140 hostdomainwrite(char *a, int n) 141 { 142 char buf[DOMLEN]; 143 144 if(!iseve()) 145 error(Eperm); 146 if(n >= DOMLEN) 147 error(Ebadarg); 148 memset(buf, 0, DOMLEN); 149 strncpy(buf, a, n); 150 if(buf[0] == 0) 151 error(Ebadarg); 152 memmove(hostdomain, buf, DOMLEN); 153 return n; 154 }