devcap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. #include "netif.h"
  8. #include <libsec.h>
  9. enum
  10. {
  11. Hashlen= SHA1dlen,
  12. Maxhash= 256,
  13. };
  14. /*
  15. * if a process knows cap->cap, it can change user
  16. * to capabilty->user.
  17. */
  18. typedef struct Caphash Caphash;
  19. struct Caphash
  20. {
  21. Caphash *next;
  22. char hash[Hashlen];
  23. ulong ticks;
  24. };
  25. struct
  26. {
  27. QLock;
  28. Caphash *first;
  29. int nhash;
  30. } capalloc;
  31. enum
  32. {
  33. Qdir,
  34. Qhash,
  35. Quse,
  36. };
  37. /* caphash must be last */
  38. Dirtab capdir[] =
  39. {
  40. ".", {Qdir,0,QTDIR}, 0, DMDIR|0500,
  41. "capuse", {Quse}, 0, 0222,
  42. "caphash", {Qhash}, 0, 0200,
  43. };
  44. int ncapdir = nelem(capdir);
  45. static Chan*
  46. capattach(char *spec)
  47. {
  48. return devattach(L'¤', spec);
  49. }
  50. static Walkqid*
  51. capwalk(Chan *c, Chan *nc, char **name, int nname)
  52. {
  53. return devwalk(c, nc, name, nname, capdir, ncapdir, devgen);
  54. }
  55. static void
  56. capremove(Chan *c)
  57. {
  58. if(iseve() && c->qid.path == Qhash)
  59. ncapdir = nelem(capdir)-1;
  60. else
  61. error(Eperm);
  62. }
  63. static int
  64. capstat(Chan *c, uchar *db, int n)
  65. {
  66. return devstat(c, db, n, capdir, ncapdir, devgen);
  67. }
  68. /*
  69. * if the stream doesn't exist, create it
  70. */
  71. static Chan*
  72. capopen(Chan *c, int omode)
  73. {
  74. if(c->qid.type & QTDIR){
  75. if(omode != OREAD)
  76. error(Ebadarg);
  77. c->mode = omode;
  78. c->flag |= COPEN;
  79. c->offset = 0;
  80. return c;
  81. }
  82. switch((ulong)c->qid.path){
  83. case Qhash:
  84. if(!iseve())
  85. error(Eperm);
  86. break;
  87. }
  88. c->mode = openmode(omode);
  89. c->flag |= COPEN;
  90. c->offset = 0;
  91. return c;
  92. }
  93. /*
  94. static char*
  95. hashstr(uchar *hash)
  96. {
  97. static char buf[2*Hashlen+1];
  98. int i;
  99. for(i = 0; i < Hashlen; i++)
  100. sprint(buf+2*i, "%2.2ux", hash[i]);
  101. buf[2*Hashlen] = 0;
  102. return buf;
  103. }
  104. */
  105. static Caphash*
  106. remcap(uchar *hash)
  107. {
  108. Caphash *t, **l;
  109. qlock(&capalloc);
  110. /* find the matching capability */
  111. for(l = &capalloc.first; *l != nil;){
  112. t = *l;
  113. if(memcmp(hash, t->hash, Hashlen) == 0)
  114. break;
  115. l = &t->next;
  116. }
  117. t = *l;
  118. if(t != nil){
  119. capalloc.nhash--;
  120. *l = t->next;
  121. }
  122. qunlock(&capalloc);
  123. return t;
  124. }
  125. /* add a capability, throwing out any old ones */
  126. static void
  127. addcap(uchar *hash)
  128. {
  129. Caphash *p, *t, **l;
  130. p = smalloc(sizeof *p);
  131. memmove(p->hash, hash, Hashlen);
  132. p->next = nil;
  133. p->ticks = m->ticks;
  134. qlock(&capalloc);
  135. /* trim extras */
  136. while(capalloc.nhash >= Maxhash){
  137. t = capalloc.first;
  138. if(t == nil)
  139. panic("addcap");
  140. capalloc.first = t->next;
  141. free(t);
  142. capalloc.nhash--;
  143. }
  144. /* add new one */
  145. for(l = &capalloc.first; *l != nil; l = &(*l)->next)
  146. ;
  147. *l = p;
  148. capalloc.nhash++;
  149. qunlock(&capalloc);
  150. }
  151. static void
  152. capclose(Chan*)
  153. {
  154. }
  155. static long
  156. capread(Chan *c, void *va, long n, vlong)
  157. {
  158. switch((ulong)c->qid.path){
  159. case Qdir:
  160. return devdirread(c, va, n, capdir, ncapdir, devgen);
  161. default:
  162. error(Eperm);
  163. break;
  164. }
  165. return n;
  166. }
  167. static long
  168. capwrite(Chan *c, void *va, long n, vlong)
  169. {
  170. Caphash *p;
  171. char *cp;
  172. uchar hash[Hashlen];
  173. char *key, *from, *to;
  174. char err[256];
  175. switch((ulong)c->qid.path){
  176. case Qhash:
  177. if(!iseve())
  178. error(Eperm);
  179. if(n < Hashlen)
  180. error(Eshort);
  181. memmove(hash, va, Hashlen);
  182. addcap(hash);
  183. break;
  184. case Quse:
  185. /* copy key to avoid a fault in hmac_xx */
  186. cp = nil;
  187. if(waserror()){
  188. free(cp);
  189. nexterror();
  190. }
  191. cp = smalloc(n+1);
  192. memmove(cp, va, n);
  193. cp[n] = 0;
  194. from = cp;
  195. key = strrchr(cp, '@');
  196. if(key == nil)
  197. error(Eshort);
  198. *key++ = 0;
  199. hmac_sha1((uchar*)from, strlen(from), (uchar*)key, strlen(key), hash, nil);
  200. p = remcap(hash);
  201. if(p == nil){
  202. snprint(err, sizeof err, "invalid capability %s@%s", from, key);
  203. error(err);
  204. }
  205. /* if a from user is supplied, make sure it matches */
  206. to = strchr(from, '@');
  207. if(to == nil){
  208. to = from;
  209. } else {
  210. *to++ = 0;
  211. if(strcmp(from, up->user) != 0)
  212. error("capability must match user");
  213. }
  214. /* set user id */
  215. kstrdup(&up->user, to);
  216. up->basepri = PriNormal;
  217. free(p);
  218. free(cp);
  219. poperror();
  220. break;
  221. default:
  222. error(Eperm);
  223. break;
  224. }
  225. return n;
  226. }
  227. Dev capdevtab = {
  228. L'¤',
  229. "cap",
  230. devreset,
  231. devinit,
  232. devshutdown,
  233. capattach,
  234. capwalk,
  235. capstat,
  236. capopen,
  237. devcreate,
  238. capclose,
  239. capread,
  240. devbread,
  241. capwrite,
  242. devbwrite,
  243. capremove,
  244. devwstat
  245. };