sysauth.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include <authsrv.h>
  16. char *eve;
  17. char hostdomain[DOMLEN];
  18. /*
  19. * return true if current user is eve
  20. */
  21. int
  22. iseve(void)
  23. {
  24. Proc *up = externup();
  25. return strcmp(eve, up->user) == 0;
  26. }
  27. void
  28. sysfversion(Ar0* ar0, ...)
  29. {
  30. Proc *up = externup();
  31. Chan *c;
  32. char *version;
  33. int fd;
  34. uint32_t msize;
  35. usize nversion;
  36. va_list list;
  37. va_start(list, ar0);
  38. /*
  39. * int fversion(int fd, int bufsize, char *version, int nversion);
  40. * should be
  41. * usize fversion(int fd, u32int msize, char *version, usize nversion);
  42. */
  43. fd = va_arg(list, int);
  44. msize = va_arg(list, uint32_t);
  45. version = va_arg(list, char*);
  46. nversion = va_arg(list, usize);
  47. va_end(list);
  48. version = validaddr(version, nversion, 1);
  49. /* check there's a NUL in the version string */
  50. if(nversion == 0 || memchr(version, 0, nversion) == nil)
  51. error(Ebadarg);
  52. c = fdtochan(fd, ORDWR, 0, 1);
  53. if(waserror()){
  54. cclose(c);
  55. nexterror();
  56. }
  57. ar0->u = mntversion(c, msize, version, nversion);
  58. cclose(c);
  59. poperror();
  60. }
  61. void
  62. sys_fsession(Ar0* ar0, ...)
  63. {
  64. int fd;
  65. char *trbuf;
  66. va_list list;
  67. va_start(list, ar0);
  68. /*
  69. * int fsession(int fd, char trbuf[TICKREQLEN]);
  70. *
  71. * Deprecated; backwards compatibility only.
  72. */
  73. fd = va_arg(list, int);
  74. trbuf = va_arg(list, char*);
  75. va_end(list);
  76. USED(fd);
  77. trbuf = validaddr(trbuf, 1, 1);
  78. *trbuf = '\0';
  79. ar0->i = 0;
  80. }
  81. void
  82. sysfauth(Ar0* ar0, ...)
  83. {
  84. Proc *up = externup();
  85. Chan *c, *ac;
  86. char *aname;
  87. int fd;
  88. va_list list;
  89. va_start(list, ar0);
  90. /*
  91. * int fauth(int fd, char *aname);
  92. */
  93. fd = va_arg(list, int);
  94. aname = va_arg(list, char*);
  95. va_end(list);
  96. aname = validaddr(aname, 1, 0);
  97. aname = validnamedup(aname, 1);
  98. if(waserror()){
  99. free(aname);
  100. nexterror();
  101. }
  102. c = fdtochan(fd, ORDWR, 0, 1);
  103. if(waserror()){
  104. cclose(c);
  105. nexterror();
  106. }
  107. ac = mntauth(c, aname);
  108. /* at this point ac is responsible for keeping c alive */
  109. cclose(c);
  110. poperror(); /* c */
  111. free(aname);
  112. poperror(); /* aname */
  113. if(waserror()){
  114. cclose(ac);
  115. nexterror();
  116. }
  117. fd = newfd(ac);
  118. if(fd < 0)
  119. error(Enofd);
  120. poperror(); /* ac */
  121. /* always mark it close on exec */
  122. ac->flag |= CCEXEC;
  123. ar0->i = fd;
  124. }
  125. /*
  126. * called by devcons() for user device
  127. *
  128. * anyone can become none
  129. */
  130. int32_t
  131. userwrite(char* a, int32_t n)
  132. {
  133. Proc *up = externup();
  134. if(n != 4 || strncmp(a, "none", 4) != 0)
  135. error(Eperm);
  136. kstrdup(&up->user, "none");
  137. up->basepri = PriNormal;
  138. return n;
  139. }
  140. /*
  141. * called by devcons() for host owner/domain
  142. *
  143. * writing hostowner also sets user
  144. */
  145. int32_t
  146. hostownerwrite(char* a, int32_t n)
  147. {
  148. Proc *up = externup();
  149. char buf[128];
  150. if(!iseve())
  151. error(Eperm);
  152. if(n <= 0 || n >= sizeof buf)
  153. error(Ebadarg);
  154. memmove(buf, a, n);
  155. buf[n] = 0;
  156. renameuser(eve, buf);
  157. kstrdup(&eve, buf);
  158. kstrdup(&up->user, buf);
  159. up->basepri = PriNormal;
  160. return n;
  161. }
  162. int32_t
  163. hostdomainwrite(char* a, int32_t n)
  164. {
  165. char buf[DOMLEN];
  166. if(!iseve())
  167. error(Eperm);
  168. if(n >= DOMLEN)
  169. error(Ebadarg);
  170. memset(buf, 0, DOMLEN);
  171. strncpy(buf, a, n);
  172. if(buf[0] == 0)
  173. error(Ebadarg);
  174. memmove(hostdomain, buf, DOMLEN);
  175. return n;
  176. }