tlucho changes - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 648bb6f75a801e6d605fcd061f0b14fecc07a8be
 (DIR) parent fa467fbe51e3aba23fce6639afd8939c046c8c2d
 (HTM) Author: rsc <devnull@localhost>
       Date:   Tue,  8 Feb 2005 20:58:10 +0000
       
       lucho changes
       
       Diffstat:
         M src/libauth/auth_attr.c             |       1 -
         M src/libauth/auth_challenge.c        |      17 ++++-------------
         M src/libauth/auth_getkey.c           |      17 ++++-------------
         M src/libauth/auth_getuserpasswd.c    |       7 +------
         M src/libauth/auth_proxy.c            |      95 ++++++++++++++++++++++++++++---
         M src/libauth/auth_respond.c          |      10 +---------
         M src/libauth/auth_rpc.c              |      38 +++++++++++++++++++++++++++----
         M src/libauth/auth_wep.c              |       9 +--------
         M src/libauth/mkfile                  |      22 +++++++++++-----------
       
       9 files changed, 144 insertions(+), 72 deletions(-)
       ---
 (DIR) diff --git a/src/libauth/auth_attr.c b/src/libauth/auth_attr.c
       t@@ -1,7 +1,6 @@
        #include <u.h>
        #include <libc.h>
        #include <auth.h>
       -#include <authsrv.h>
        #include "authlocal.h"
        
        Attr*
 (DIR) diff --git a/src/libauth/auth_challenge.c b/src/libauth/auth_challenge.c
       t@@ -24,18 +24,15 @@ auth_challenge(char *fmt, ...)
                        return nil;
                }
        
       -        if((c->afd = open("/mnt/factotum/rpc", ORDWR)) < 0){
       +        if((c->rpc=auth_allocrpc()) == nil
       +        || auth_rpc(c->rpc, "start", p, strlen(p)) != ARok
       +        || auth_rpc(c->rpc, "read", nil, 0) != ARok){
                Error:
                        auth_freechal(c);
                        free(p);
                        return nil;
                }
        
       -        if((c->rpc=auth_allocrpc(c->afd)) == nil
       -        || auth_rpc(c->rpc, "start", p, strlen(p)) != ARok
       -        || auth_rpc(c->rpc, "read", nil, 0) != ARok)
       -                goto Error;
       -
                if(c->rpc->narg > sizeof(c->chal)-1){
                        werrstr("buffer too small for challenge");
                        goto Error;
       t@@ -53,7 +50,7 @@ auth_response(Chalstate *c)
                AuthInfo *ai;
        
                ai = nil;
       -        if(c->afd < 0){
       +        if(c->rpc == nil){
                        werrstr("auth_response: connection not open");
                        return nil;
                }
       t@@ -94,9 +91,7 @@ auth_response(Chalstate *c)
                }
        
        Out:
       -        close(c->afd);
                auth_freerpc(c->rpc);
       -        c->afd = -1;
                c->rpc = nil;
                return ai;
        }
       t@@ -106,12 +101,8 @@ auth_freechal(Chalstate *c)
        {
                if(c == nil)
                        return;
       -
       -        if(c->afd >= 0)
       -                close(c->afd);
                if(c->rpc != nil)
                        auth_freerpc(c->rpc);
       -
                memset(c, 0xBB, sizeof(*c));
                free(c);
        }
 (DIR) diff --git a/src/libauth/auth_getkey.c b/src/libauth/auth_getkey.c
       t@@ -6,23 +6,13 @@ int
        auth_getkey(char *params)
        {
                char *name;
       -        Dir *d;
                int pid;
                Waitmsg *w;
        
                /* start /factotum to query for a key */
       -        name = "/factotum";
       -        d = dirstat(name);
       -        if(d == nil){
       -                name = "/boot/factotum";
       -                d = dirstat(name);
       -        }
       -        if(d == nil){
       -                werrstr("auth_getkey: no /factotum or /boot/factotum: didn't get key %s", params);
       -                return -1;
       -        }
       -if(0)        if(d->type != '/'){
       -                werrstr("auth_getkey: /factotum may be bad: didn't get key %s", params);
       +        name = unsharp("#9/bin/factotum");
       +        if(name == nil || access(name, AEXEC) < 0){
       +                werrstr("auth_getkey: no $PLAN9/bin/factotum: didn't get key %s", params);
                        return -1;
                }
                switch(pid = fork()){
       t@@ -33,6 +23,7 @@ if(0)        if(d->type != '/'){
                        execl(name, "getkey", "-g", params, nil);
                        exits(0);
                default:
       +                free(name);
                        for(;;){
                                w = wait();
                                if(w == nil)
 (DIR) diff --git a/src/libauth/auth_getuserpasswd.c b/src/libauth/auth_getuserpasswd.c
       t@@ -27,7 +27,6 @@ auth_getuserpasswd(AuthGetkey *getkey, char *fmt, ...)
        {
                AuthRpc *rpc;
                char *f[3], *p, *params;
       -        int fd;
                va_list arg;
                UserPasswd *up;
        
       t@@ -35,10 +34,7 @@ auth_getuserpasswd(AuthGetkey *getkey, char *fmt, ...)
                rpc = nil;
                params = nil;
        
       -        fd = open("/mnt/factotum/rpc", ORDWR);
       -        if(fd < 0)
       -                goto out;
       -        rpc = auth_allocrpc(fd);
       +        rpc = auth_allocrpc();
                if(rpc == nil)
                        goto out;
                quotefmtinstall();        /* just in case */
       t@@ -70,6 +66,5 @@ auth_getuserpasswd(AuthGetkey *getkey, char *fmt, ...)
        out:
                free(params);
                auth_freerpc(rpc);
       -        close(fd);
                return up;
        }
 (DIR) diff --git a/src/libauth/auth_proxy.c b/src/libauth/auth_proxy.c
       t@@ -2,6 +2,7 @@
        #include <libc.h>
        #include <fcall.h>
        #include <auth.h>
       +#include <9pclient.h>
        #include "authlocal.h"
        
        enum { 
       t@@ -179,7 +180,6 @@ Error:
        AuthInfo*
        auth_proxy(int fd, AuthGetkey *getkey, char *fmt, ...)
        {
       -        int afd;
                char *p;
                va_list arg;
                AuthInfo *ai;
       t@@ -190,23 +190,104 @@ auth_proxy(int fd, AuthGetkey *getkey, char *fmt, ...)
                p = vsmprint(fmt, arg);
                va_end(arg);
        
       -        afd = open("/mnt/factotum/rpc", ORDWR);
       -        if(afd < 0){
       -                werrstr("opening /mnt/factotum/rpc: %r");
       +        rpc = auth_allocrpc();
       +        if(rpc == nil){
                        free(p);
                        return nil;
                }
        
       -        rpc = auth_allocrpc(afd);
       +        ai = fauth_proxy(fd, rpc, getkey, p);
       +        free(p);
       +        auth_freerpc(rpc);
       +        return ai;
       +}
       +
       +/*
       + *  this just proxies what the factotum tells it to.
       + */
       +AuthInfo*
       +fsfauth_proxy(CFid *fid, AuthRpc *rpc, AuthGetkey *getkey, char *params)
       +{
       +        char *buf;
       +        int m, n, ret;
       +        AuthInfo *a;
       +        char oerr[ERRMAX];
       +
       +        rerrstr(oerr, sizeof oerr);
       +        werrstr("UNKNOWN AUTH ERROR");
       +
       +        if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
       +                werrstr("fauth_proxy start: %r");
       +                return nil;
       +        }
       +
       +        buf = malloc(AuthRpcMax);
       +        if(buf == nil)
       +                return nil;
       +        for(;;){
       +                switch(dorpc(rpc, "read", nil, 0, getkey)){
       +                case ARdone:
       +                        free(buf);
       +                        a = auth_getinfo(rpc);
       +                        errstr(oerr, sizeof oerr);        /* no error, restore whatever was there */
       +                        return a;
       +                case ARok:
       +                        if(fswrite(fid, rpc->arg, rpc->narg) != rpc->narg){
       +                                werrstr("auth_proxy write fid: %r");
       +                                goto Error;
       +                        }
       +                        break;
       +                case ARphase:
       +                        n = 0;
       +                        memset(buf, 0, AuthRpcMax);
       +                        while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
       +                                if(atoi(rpc->arg) > AuthRpcMax)
       +                                        break;
       +                                m = fsread(fid, buf+n, atoi(rpc->arg)-n);
       +                                if(m <= 0){
       +                                        if(m == 0)
       +                                                werrstr("auth_proxy short read: %s", buf);
       +                                        goto Error;
       +                                }
       +                                n += m;
       +                        }
       +                        if(ret != ARok){
       +                                werrstr("auth_proxy rpc write: %s: %r", buf);
       +                                goto Error;
       +                        }
       +                        break;
       +                default:
       +                        werrstr("auth_proxy rpc: %r");
       +                        goto Error;
       +                }
       +        }
       +Error:
       +        free(buf);
       +        return nil;
       +}
       +
       +AuthInfo*
       +fsauth_proxy(CFid *fid, AuthGetkey *getkey, char *fmt, ...)
       +{
       +        char *p;
       +        va_list arg;
       +        AuthInfo *ai;
       +        AuthRpc *rpc;
       +
       +        quotefmtinstall();        /* just in case */
       +        va_start(arg, fmt);
       +        p = vsmprint(fmt, arg);
       +        va_end(arg);
       +
       +        rpc = auth_allocrpc();
                if(rpc == nil){
                        free(p);
                        return nil;
                }
        
       -        ai = fauth_proxy(fd, rpc, getkey, p);
       +        ai = fsfauth_proxy(fid, rpc, getkey, p);
                free(p);
                auth_freerpc(rpc);
       -        close(afd);
                return ai;
        }
        
 (DIR) diff --git a/src/libauth/auth_respond.c b/src/libauth/auth_respond.c
       t@@ -28,17 +28,11 @@ auth_respond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nr
        {
                char *p, *s;
                va_list arg;
       -        int afd;
                AuthRpc *rpc;
                Attr *a;
        
       -        if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0)
       +        if((rpc = auth_allocrpc()) == nil)
                        return -1;
       -        
       -        if((rpc = auth_allocrpc(afd)) == nil){
       -                close(afd);
       -                return -1;
       -        }
        
                quotefmtinstall();        /* just in case */
                va_start(arg, fmt);
       t@@ -50,7 +44,6 @@ auth_respond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nr
                || dorpc(rpc, "write", chal, nchal, getkey) != ARok
                || dorpc(rpc, "read", nil, 0, getkey) != ARok){
                        free(p);
       -                close(afd);
                        auth_freerpc(rpc);
                        return -1;
                }
       t@@ -67,7 +60,6 @@ auth_respond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nr
                        user[0] = '\0';
        
                _freeattr(a);
       -        close(afd);
                auth_freerpc(rpc);
                return nresp;        
        }
 (DIR) diff --git a/src/libauth/auth_rpc.c b/src/libauth/auth_rpc.c
       t@@ -1,6 +1,7 @@
        #include <u.h>
        #include <libc.h>
        #include <auth.h>
       +#include <9pclient.h>
        #include "authlocal.h"
        
        static struct {
       t@@ -17,6 +18,24 @@ static struct {
                "error",                ARerror,
        };
        
       +static long
       +rpcread(AuthRpc *rpc, void *buf, int buflen)
       +{
       +        if (rpc->afd >= 0)
       +                return read(rpc->afd, buf, buflen);
       +        else
       +                return fsread(rpc->afid, buf, buflen);
       +}
       +
       +static long
       +rpcwrite(AuthRpc *rpc, void *buf, int buflen)
       +{
       +        if (rpc->afd >= 0)
       +                return write(rpc->afd, buf, buflen);
       +        else
       +                return fswrite(rpc->afid, buf, buflen);
       +}
       +
        static int
        classify(char *buf, uint n, AuthRpc *rpc)
        {
       t@@ -40,20 +59,31 @@ classify(char *buf, uint n, AuthRpc *rpc)
        }
        
        AuthRpc*
       -auth_allocrpc(int afd)
       +auth_allocrpc(void)
        {
                AuthRpc *rpc;
        
                rpc = mallocz(sizeof(*rpc), 1);
                if(rpc == nil)
                        return nil;
       -        rpc->afd = afd;
       +        rpc->afd = open("/mnt/factotum/rpc", ORDWR);
       +        if(rpc->afd < 0){
       +                rpc->afid = nsopen("factotum", nil, "factotum/rpc", ORDWR);
       +                if(rpc->afid == nil){
       +                        free(rpc);
       +                        return nil;
       +                }
       +        }
                return rpc;
        }
        
        void
        auth_freerpc(AuthRpc *rpc)
        {
       +        if(rpc->afd >= 0)
       +                close(rpc->afd);
       +        if(rpc->afid == nil)
       +                fsclose(rpc->afid);
                free(rpc);
        }
        
       t@@ -72,13 +102,13 @@ auth_rpc(AuthRpc *rpc, char *verb, void *a, int na)
                memmove(rpc->obuf, verb, l);
                rpc->obuf[l] = ' ';
                memmove(rpc->obuf+l+1, a, na);
       -        if((n=write(rpc->afd, rpc->obuf, l+1+na)) != l+1+na){
       +        if((n=rpcwrite(rpc, rpc->obuf, l+1+na)) != l+1+na){
                        if(n >= 0)
                                werrstr("auth_rpc short write");
                        return ARrpcfailure;
                }
        
       -        if((n=read(rpc->afd, rpc->ibuf, AuthRpcMax)) < 0)
       +        if((n=rpcread(rpc, rpc->ibuf, AuthRpcMax)) < 0)
                        return ARrpcfailure;
                rpc->ibuf[n] = '\0';
        
 (DIR) diff --git a/src/libauth/auth_wep.c b/src/libauth/auth_wep.c
       t@@ -11,7 +11,6 @@ auth_wep(char *dev, char *fmt, ...)
        {
                AuthRpc *rpc;
                char *params, *p;
       -        int fd;
                va_list arg;
                int rv;
        
       t@@ -22,11 +21,7 @@ auth_wep(char *dev, char *fmt, ...)
                        return rv;
                }
        
       -        fd = open("/mnt/factotum/rpc", ORDWR);
       -        if(fd < 0)
       -                return rv;
       -
       -        rpc = auth_allocrpc(fd);
       +        rpc = auth_allocrpc();
                if(rpc != nil){
                        quotefmtinstall();        /* just in case */
                        va_start(arg, fmt);
       t@@ -44,7 +39,5 @@ auth_wep(char *dev, char *fmt, ...)
                        }
                        auth_freerpc(rpc);
                }
       -        close(fd);
       -                
                return rv;
        }
 (DIR) diff --git a/src/libauth/mkfile b/src/libauth/mkfile
       t@@ -4,19 +4,19 @@ PLAN9=../..
        LIB=libauth.a
        OFILES=\
        #        amount.$O\
       -#        amount_getkey.$O\
       +        amount_getkey.$O\
                attr.$O\
       -#        auth_attr.$O\
       -#        auth_challenge.$O\
       +        auth_attr.$O\
       +        auth_challenge.$O\
        #        auth_chuid.$O\
       -#        auth_getkey.$O\
       -#        auth_getuserpasswd.$O\
       -#        auth_proxy.$O\
       -#        auth_respond.$O\
       -#        auth_rpc.$O\
       -#        auth_userpasswd.$O\
       -#        auth_wep.$O\
       -#        login.$O\
       +        auth_getkey.$O\
       +        auth_getuserpasswd.$O\
       +        auth_proxy.$O\
       +        auth_respond.$O\
       +        auth_rpc.$O\
       +        auth_userpasswd.$O\
       +        auth_wep.$O\
       +        login.$O\
        #        newns.$O\
        #        noworld.$O\