xref: /plan9-contrib/sys/src/9/port/auth.c (revision d46c239f8612929b7dbade67d0d071633df3a15d)
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 
8 #include	<authsrv.h>
9 
10 char	*eve;
11 char	hostdomain[DOMLEN];
12 
13 /*
14  *  return true if current user is eve
15  */
16 int
17 iseve(void)
18 {
19 	return strcmp(eve, up->user) == 0;
20 }
21 
22 long
23 sysfversion(ulong *arg)
24 {
25 	char *vers;
26 	uint arglen, m, msize;
27 	Chan *c;
28 
29 	msize = arg[1];
30 	vers = (char*)arg[2];
31 	arglen = arg[3];
32 	validaddr(arg[2], arglen, 1);
33 	/* check there's a NUL in the version string */
34 	if(arglen==0 || memchr(vers, 0, arglen)==0)
35 		error(Ebadarg);
36 	c = fdtochan(arg[0], ORDWR, 0, 1);
37 	if(waserror()){
38 		cclose(c);
39 		nexterror();
40 	}
41 
42 	m = mntversion(c, vers, msize, arglen);
43 
44 	cclose(c);
45 	poperror();
46 	return m;
47 }
48 
49 long
50 sys_fsession(ulong *arg)
51 {
52 	/* deprecated; backwards compatibility only */
53 
54 	if(arg[2] == 0)
55 		error(Ebadarg);
56 	validaddr(arg[1], arg[2], 1);
57 	((uchar*)arg[1])[0] = '\0';
58 	return 0;
59 }
60 
61 long
62 sysfauth(ulong *arg)
63 {
64 	Chan *c, *ac;
65 	char *aname;
66 	int fd;
67 
68 	validaddr(arg[1], 1, 0);
69 	aname = (char*)arg[1];
70 	validname(aname, 1);
71 	c = fdtochan(arg[0], ORDWR, 0, 1);
72 	if(waserror()){
73 		cclose(c);
74 		nexterror();
75 	}
76 
77 	ac = mntauth(c, aname);
78 
79 	/* at this point ac is responsible for keeping c alive */
80 	cclose(c);
81 	poperror();	/* c */
82 
83 	if(waserror()){
84 		cclose(ac);
85 		nexterror();
86 	}
87 
88 	fd = newfd(ac);
89 	if(fd < 0)
90 		error(Enofd);
91 	poperror();	/* ac */
92 
93 	/* always mark it close on exec */
94 	ac->flag |= CCEXEC;
95 
96 	return fd;
97 }
98 
99 /*
100  *  called by devcons() for user device
101  *
102  *  anyone can become none
103  */
104 long
105 userwrite(char *a, int n)
106 {
107 	if(n!=4 || strncmp(a, "none", 4)!=0)
108 		error(Eperm);
109 	kstrdup(&up->user, "none");
110 	up->basepri = PriNormal;
111 	return n;
112 }
113 
114 /*
115  *  called by devcons() for host owner/domain
116  *
117  *  writing hostowner also sets user
118  */
119 long
120 hostownerwrite(char *a, int n)
121 {
122 	char buf[128];
123 
124 	if(!iseve())
125 		error(Eperm);
126 	if(n <= 0 || n >= sizeof buf)
127 		error(Ebadarg);
128 	memmove(buf, a, n);
129 	buf[n] = 0;
130 
131 	renameuser(eve, buf);
132 	kstrdup(&eve, buf);
133 	kstrdup(&up->user, buf);
134 	up->basepri = PriNormal;
135 	return n;
136 }
137 
138 long
139 hostdomainwrite(char *a, int n)
140 {
141 	char buf[DOMLEN];
142 
143 	if(!iseve())
144 		error(Eperm);
145 	if(n >= DOMLEN)
146 		error(Ebadarg);
147 	memset(buf, 0, DOMLEN);
148 	strncpy(buf, a, n);
149 	if(buf[0] == 0)
150 		error(Ebadarg);
151 	memmove(hostdomain, buf, DOMLEN);
152 	return n;
153 }
154