devcap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. static Caphash*
  94. remcap(uchar *hash)
  95. {
  96. Caphash *t, **l;
  97. qlock(&capalloc);
  98. /* find the matching capability */
  99. for(l = &capalloc.first; *l != nil;){
  100. t = *l;
  101. if(memcmp(hash, t->hash, Hashlen) == 0)
  102. break;
  103. l = &t->next;
  104. }
  105. t = *l;
  106. if(t != nil){
  107. capalloc.nhash--;
  108. *l = t->next;
  109. }
  110. qunlock(&capalloc);
  111. return t;
  112. }
  113. /* add a capability, throwing out any old ones */
  114. static void
  115. addcap(uchar *hash)
  116. {
  117. Caphash *p, *t, **l;
  118. p = smalloc(sizeof *p);
  119. memmove(p->hash, hash, Hashlen);
  120. p->next = nil;
  121. p->ticks = m->ticks;
  122. qlock(&capalloc);
  123. /* trim extras */
  124. while(capalloc.nhash >= Maxhash){
  125. t = capalloc.first;
  126. if(t == nil)
  127. panic("addcap");
  128. capalloc.first = t->next;
  129. free(t);
  130. capalloc.nhash--;
  131. }
  132. /* add new one */
  133. for(l = &capalloc.first; *l != nil; l = &(*l)->next)
  134. ;
  135. *l = p;
  136. capalloc.nhash++;
  137. qunlock(&capalloc);
  138. }
  139. static void
  140. capclose(Chan*)
  141. {
  142. }
  143. static long
  144. capread(Chan *c, void *va, long n, vlong)
  145. {
  146. switch((ulong)c->qid.path){
  147. case Qdir:
  148. return devdirread(c, va, n, capdir, ncapdir, devgen);
  149. default:
  150. error(Eperm);
  151. break;
  152. }
  153. return n;
  154. }
  155. static long
  156. capwrite(Chan *c, void *va, long n, vlong)
  157. {
  158. Caphash *p;
  159. char *cp;
  160. uchar hash[Hashlen];
  161. char *key, *from, *to;
  162. char err[256];
  163. switch((ulong)c->qid.path){
  164. case Qhash:
  165. if(n < Hashlen)
  166. error(Eshort);
  167. memmove(hash, va, Hashlen);
  168. addcap(hash);
  169. break;
  170. case Quse:
  171. /* copy key to avoid a fault in hmac_xx */
  172. cp = nil;
  173. if(waserror()){
  174. free(cp);
  175. nexterror();
  176. }
  177. cp = smalloc(n+1);
  178. memmove(cp, va, n);
  179. cp[n] = 0;
  180. from = cp;
  181. key = strrchr(cp, '@');
  182. if(key == nil)
  183. error(Eshort);
  184. *key++ = 0;
  185. hmac_sha1((uchar*)from, strlen(from), (uchar*)key, strlen(key), hash, nil);
  186. p = remcap(hash);
  187. if(p == nil){
  188. snprint(err, sizeof err, "invalid capability %s@%s", from, key);
  189. error(err);
  190. }
  191. /* if a from user is supplied, make sure it matches */
  192. to = strchr(from, '@');
  193. if(to == nil){
  194. to = from;
  195. } else {
  196. *to++ = 0;
  197. if(strcmp(from, up->user) != 0)
  198. error("capability must match user");
  199. }
  200. /* set user id */
  201. kstrdup(&up->user, to);
  202. up->basepri = PriNormal;
  203. free(p);
  204. free(cp);
  205. poperror();
  206. break;
  207. default:
  208. error(Eperm);
  209. break;
  210. }
  211. return n;
  212. }
  213. Dev capdevtab = {
  214. .dc= L'¤',
  215. .name= "cap",
  216. .reset= devreset,
  217. .init= devinit,
  218. .shutdown= devshutdown,
  219. .attach= capattach,
  220. .walk= capwalk,
  221. .stat= capstat,
  222. .open= capopen,
  223. .create= devcreate,
  224. .close= capclose,
  225. .read= capread,
  226. .bread= devbread,
  227. .write= capwrite,
  228. .bwrite= devbwrite,
  229. .remove= capremove,
  230. .wstat= devwstat,
  231. };