devcap.c 4.0 KB

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