Change-Id: I3e99b8317fc57fbfb775fd4242e5fb2f36411a46
Signed-off-by: Ronald G. Minnich <rminn...@gmail.com>
---
 kern/drivers/dev/capability.c | 181 +++++++++++++++++++-----------------------
 1 file changed, 81 insertions(+), 100 deletions(-)

diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c
index 53a59af..a3bafd7 100644
--- a/kern/drivers/dev/capability.c
+++ b/kern/drivers/dev/capability.c
@@ -7,79 +7,68 @@
  * in the LICENSE file.
  */
 
-#include       "u.h"
-#include       "../port/lib.h"
-#include       "mem.h"
-#include       "dat.h"
-#include       "fns.h"
-#include       "../port/error.h"
-
-#include       <libsec.h>
-
-enum
-{
-       Hashlen=        SHA1dlen,
-       Maxhash=        256,
+#include "../port/error.h"
+#include "../port/lib.h"
+#include "dat.h"
+#include "fns.h"
+#include "mem.h"
+#include "u.h"
+
+#include <libsec.h>
+
+enum {
+       Hashlen = SHA1dlen,
+       Maxhash = 256,
 };
 
 /*
  *  if a process knows cap->cap, it can change user
  *  to capabilty->user.
  */
-typedef struct Caphash Caphash;
-struct Caphash
-{
-       Caphash *next;
-       char            hash[Hashlen];
+typedef struct Caphash Caphash;
+struct Caphash {
+       Caphash *next;
+       char hash[Hashlen];
 };
 
-struct
-{
+struct {
        QLock QLock;
-       Caphash *first;
-       int     nhash;
+       Caphash *first;
+       int nhash;
 } capalloc;
 
-enum
-{
+enum {
        Qdir,
        Qhash,
        Quse,
 };
 
 /* caphash must be last */
-Dirtab capdir[] =
-{
-       ".",            {Qdir,0,QTDIR}, 0,              DMDIR|0500,
-       "capuse",       {Quse},         0,              0222,
-       "caphash",      {Qhash},        0,              0200,
+Dirtab capdir[] = {
+    ".",       {Qdir, 0, QTDIR}, 0, DMDIR | 0500, "capuse", {Quse}, 0, 0222,
+    "caphash", {Qhash},          0, 0200,
 };
 int ncapdir = nelem(capdir);
 
-static Chan*
-capattach(char *spec)
+static Chan *capattach(char *spec)
 {
        return devattach(L'¤', spec);
 }
 
-static Walkqid*
-capwalk(Chan *c, Chan *nc, char **name, int nname)
+static Walkqid *capwalk(Chan *c, Chan *nc, char **name, int nname)
 {
        return devwalk(c, nc, name, nname, capdir, ncapdir, devgen);
 }
 
-static void
-capremove(Chan *c)
+static void capremove(Chan *c)
 {
-       if(iseve() && c->qid.path == Qhash)
-               ncapdir = nelem(capdir)-1;
+       if (iseve() && c->qid.path == Qhash)
+               ncapdir = nelem(capdir) - 1;
        else
                error(Eperm);
 }
 
-
-static int32_t
-capstat(Chan *c, uint8_t *db, int32_t n)
+static int32_t capstat(Chan *c, uint8_t *db, int32_t n)
 {
        return devstat(c, db, n, capdir, ncapdir, devgen);
 }
@@ -87,11 +76,10 @@ capstat(Chan *c, uint8_t *db, int32_t n)
 /*
  *  if the stream doesn't exist, create it
  */
-static Chan*
-capopen(Chan *c, int omode)
+static Chan *capopen(Chan *c, int omode)
 {
-       if(c->qid.type & QTDIR){
-               if(omode != OREAD)
+       if (c->qid.type & QTDIR) {
+               if (omode != OREAD)
                        error(Ebadarg);
                c->mode = omode;
                c->flag |= COPEN;
@@ -99,9 +87,9 @@ capopen(Chan *c, int omode)
                return c;
        }
 
-       switch((uint32_t)c->qid.path){
+       switch ((uint32_t)c->qid.path) {
        case Qhash:
-               if(!iseve())
+               if (!iseve())
                        error(Eperm);
                break;
        }
@@ -116,32 +104,31 @@ capopen(Chan *c, int omode)
 static char*
 hashstr(uchar *hash)
 {
-       static char buf[2*Hashlen+1];
-       int i;
+    static char buf[2*Hashlen+1];
+    int i;
 
-       for(i = 0; i < Hashlen; i++)
-               sprint(buf+2*i, "%2.2x", hash[i]);
-       buf[2*Hashlen] = 0;
-       return buf;
+    for(i = 0; i < Hashlen; i++)
+        sprint(buf+2*i, "%2.2x", hash[i]);
+    buf[2*Hashlen] = 0;
+    return buf;
 }
  */
 
-static Caphash*
-remcap(uint8_t *hash)
+static Caphash *remcap(uint8_t *hash)
 {
        Caphash *t, **l;
 
        qlock(&capalloc.QLock);
 
        /* find the matching capability */
-       for(l = &capalloc.first; *l != nil;){
+       for (l = &capalloc.first; *l != nil;) {
                t = *l;
-               if(memcmp(hash, t->hash, Hashlen) == 0)
+               if (memcmp(hash, t->hash, Hashlen) == 0)
                        break;
                l = &t->next;
        }
        t = *l;
-       if(t != nil){
+       if (t != nil) {
                capalloc.nhash--;
                *l = t->next;
        }
@@ -151,8 +138,7 @@ remcap(uint8_t *hash)
 }
 
 /* add a capability, throwing out any old ones */
-static void
-addcap(uint8_t *hash)
+static void addcap(uint8_t *hash)
 {
        Caphash *p, *t, **l;
 
@@ -163,9 +149,9 @@ addcap(uint8_t *hash)
        qlock(&capalloc.QLock);
 
        /* trim extras */
-       while(capalloc.nhash >= Maxhash){
+       while (capalloc.nhash >= Maxhash) {
                t = capalloc.first;
-               if(t == nil)
+               if (t == nil)
                        panic("addcap");
                capalloc.first = t->next;
                free(t);
@@ -173,7 +159,7 @@ addcap(uint8_t *hash)
        }
 
        /* add new one */
-       for(l = &capalloc.first; *l != nil; l = &(*l)->next)
+       for (l = &capalloc.first; *l != nil; l = &(*l)->next)
                ;
        *l = p;
        capalloc.nhash++;
@@ -181,15 +167,13 @@ addcap(uint8_t *hash)
        qunlock(&capalloc.QLock);
 }
 
-static void
-capclose(Chan* c)
+static void capclose(Chan *c)
 {
 }
 
-static int32_t
-capread(Chan *c, void *va, int32_t n, int64_t m)
+static int32_t capread(Chan *c, void *va, int32_t n, int64_t m)
 {
-       switch((uint32_t)c->qid.path){
+       switch ((uint32_t)c->qid.path) {
        case Qdir:
                return devdirread(c, va, n, capdir, ncapdir, devgen);
 
@@ -200,8 +184,7 @@ capread(Chan *c, void *va, int32_t n, int64_t m)
        return n;
 }
 
-static int32_t
-capwrite(Chan *c, void *va, int32_t n, int64_t m)
+static int32_t capwrite(Chan *c, void *va, int32_t n, int64_t m)
 {
        Caphash *p;
        char *cp;
@@ -210,11 +193,11 @@ capwrite(Chan *c, void *va, int32_t n, int64_t m)
        char err[256];
        Proc *up = externup();
 
-       switch((uint32_t)c->qid.path){
+       switch ((uint32_t)c->qid.path) {
        case Qhash:
-               if(!iseve())
+               if (!iseve())
                        error(Eperm);
-               if(n < Hashlen)
+               if (n < Hashlen)
                        error(Eshort);
                memmove(hash, va, Hashlen);
                addcap(hash);
@@ -223,36 +206,36 @@ capwrite(Chan *c, void *va, int32_t n, int64_t m)
        case Quse:
                /* copy key to avoid a fault in hmac_xx */
                cp = nil;
-               if(waserror()){
+               if (waserror()) {
                        free(cp);
                        nexterror();
                }
-               cp = smalloc(n+1);
+               cp = smalloc(n + 1);
                memmove(cp, va, n);
                cp[n] = 0;
 
                from = cp;
                key = strrchr(cp, '@');
-               if(key == nil)
+               if (key == nil)
                        error(Eshort);
                *key++ = 0;
 
-               hmac_sha1((uint8_t*)from, strlen(from), (uint8_t*)key,
-                         strlen(key), hash, nil);
+               hmac_sha1((uint8_t *)from, strlen(from), (uint8_t *)key, 
strlen(key),
+                         hash, nil);
 
                p = remcap(hash);
-               if(p == nil){
+               if (p == nil) {
                        snprint(err, sizeof err, "invalid capability %s@%s", 
from, key);
                        error(err);
                }
 
                /* if a from user is supplied, make sure it matches */
                to = strchr(from, '@');
-               if(to == nil){
+               if (to == nil) {
                        to = from;
                } else {
                        *to++ = 0;
-                       if(strcmp(from, up->user) != 0)
+                       if (strcmp(from, up->user) != 0)
                                error("capability must match user");
                }
 
@@ -273,23 +256,21 @@ capwrite(Chan *c, void *va, int32_t n, int64_t m)
        return n;
 }
 
-Dev capdevtab = {
-       .dc = L'¤',
-       .name = "cap",
-
-       .reset = devreset,
-       .init = devinit,
-       .shutdown = devshutdown,
-       .attach = capattach,
-       .walk = capwalk,
-       .stat = capstat,
-       .open = capopen,
-       .create = devcreate,
-       .close = capclose,
-       .read = capread,
-       .bread = devbread,
-       .write = capwrite,
-       .bwrite = devbwrite,
-       .remove = capremove,
-       .wstat = devwstat
-};
+Dev capdevtab = {.dc = L'¤',
+                 .name = "cap",
+
+                 .reset = devreset,
+                 .init = devinit,
+                 .shutdown = devshutdown,
+                 .attach = capattach,
+                 .walk = capwalk,
+                 .stat = capstat,
+                 .open = capopen,
+                 .create = devcreate,
+                 .close = capclose,
+                 .read = capread,
+                 .bread = devbread,
+                 .write = capwrite,
+                 .bwrite = devbwrite,
+                 .remove = capremove,
+                 .wstat = devwstat};
-- 
2.8.0.rc3.226.g39d4020

-- 
You received this message because you are subscribed to the Google Groups 
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akaros+unsubscr...@googlegroups.com.
To post to this group, send email to akaros@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to