devcap.c 4.0 KB

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