⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9auth.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
字号:
#include "stdinc.h"#include "9.h"intauthRead(Fid* afid, void* data, int count){	AuthInfo *ai;	AuthRpc *rpc;	if((rpc = afid->rpc) == nil){		vtSetError("not an auth fid");		return -1;	}	switch(auth_rpc(rpc, "read", nil, 0)){	default:		vtSetError("auth protocol not finished");		return -1;	case ARdone:		if((ai = auth_getinfo(rpc)) == nil){			vtSetError("%r");			break;		}		if(ai->cuid == nil || *ai->cuid == '\0'){			vtSetError("auth with no cuid");			auth_freeAI(ai);			break;		}		assert(afid->cuname == nil);		afid->cuname = vtStrDup(ai->cuid);		auth_freeAI(ai);		if(Dflag)			fprint(2, "authRead cuname %s\n", afid->cuname);		assert(afid->uid == nil);		if((afid->uid = uidByUname(afid->cuname)) == nil){			vtSetError("unknown user %#q", afid->cuname);			break;		}		return 0;	case ARok:		if(count < rpc->narg){			vtSetError("not enough data in auth read");			break;		}		memmove(data, rpc->arg, rpc->narg);		return rpc->narg;	case ARphase:		vtSetError("%r");		break;	}	return -1;}intauthWrite(Fid* afid, void* data, int count){	assert(afid->rpc != nil);	if(auth_rpc(afid->rpc, "write", data, count) != ARok)		return -1;	return count;}intauthCheck(Fcall* t, Fid* fid, Fs* fsys){	Con *con;	Fid *afid;	uchar buf[1];	/*	 * Can't lookup with FidWlock here as there may be	 * protocol to do. Use a separate lock to protect altering	 * the auth information inside afid.	 */	con = fid->con;	if(t->afid == NOFID){		/*		 * If no authentication is asked for, allow		 * "none" provided the connection has already		 * been authenticatated.		 *		 * The console is allowed to attach without		 * authentication.		 */		vtRLock(con->alock);		if(con->isconsole){			/* anything goes */		}else if((con->flags&ConNoneAllow) || con->aok){			consPrint("attach %s as %s: allowing as none\n", fsysGetName(fsys), fid->uname);			vtMemFree(fid->uname);			fid->uname = vtStrDup(unamenone);		}else{			vtRUnlock(con->alock);			consPrint("attach %s as %s: connection not authenticated, not console\n", fsysGetName(fsys), fid->uname);			vtSetError("cannot attach as none before authentication");			return 0;		}		vtRUnlock(con->alock);		if((fid->uid = uidByUname(fid->uname)) == nil){			consPrint("attach %s as %s: unknown uname\n", fsysGetName(fsys), fid->uname);			vtSetError("unknown user");			return 0;		}		return 1;	}	if((afid = fidGet(con, t->afid, 0)) == nil){		consPrint("attach %s as %s: bad afid\n", fsysGetName(fsys), fid->uname);		vtSetError("bad authentication fid");		return 0;	}	/*	 * Check valid afid;	 * check uname and aname match.	 */	if(!(afid->qid.type & QTAUTH)){		consPrint("attach %s as %s: afid not an auth file\n", fsysGetName(fsys), fid->uname);		fidPut(afid);		vtSetError("bad authentication fid");		return 0;	}	if(strcmp(afid->uname, fid->uname) != 0 || afid->fsys != fsys){		consPrint("attach %s as %s: afid is for %s as %s\n", fsysGetName(fsys), fid->uname, fsysGetName(afid->fsys), afid->uname);		fidPut(afid);		vtSetError("attach/auth mismatch");		return 0;	}	vtLock(afid->alock);	if(afid->cuname == nil){		if(authRead(afid, buf, 0) != 0 || afid->cuname == nil){			vtUnlock(afid->alock);			consPrint("attach %s as %s: %R\n", fsysGetName(fsys), fid->uname);			fidPut(afid);			vtSetError("authentication protocol not finished");			return 0;		}	}	vtUnlock(afid->alock);	assert(fid->uid == nil);	if((fid->uid = uidByUname(afid->cuname)) == nil){		consPrint("attach %s as %s: unknown cuname %s\n", fsysGetName(fsys), fid->uname, afid->cuname);		fidPut(afid);		vtSetError("unknown user");		return 0;	}	vtMemFree(fid->uname);	fid->uname = vtStrDup(afid->cuname);	fidPut(afid);	/*	 * Allow "none" once the connection has been authenticated.	 */	vtLock(con->alock);	con->aok = 1;	vtUnlock(con->alock);	return 1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -