xref: /plan9/sys/src/cmd/vnc/auth.c (revision ff8c3af2f44d95267f67219afa20ba82ff6cf7e4)
1 #include "vnc.h"
2 #include <libsec.h>
3 #include <auth.h>
4 
5 char *serveraddr;
6 
7 /*
8  * Encrypt n bytes using the password
9  * as key, padded with zeros to 8 bytes.
10  */
11 enum
12 {
13 	VerLen	= 12
14 };
15 
16 static char version[VerLen+1] = "RFB 003.003\n";
17 
18 static uchar tab[256];
19 
20 /* VNC reverses the bits of each byte before using as a des key */
21 static void
22 mktab(void)
23 {
24 	int i, j, k;
25 	static int once;
26 
27 	if(once)
28 		return;
29 	once = 1;
30 
31 	for(i=0; i<256; i++){
32 		j=i;
33 		tab[i] = 0;
34 		for(k=0; k<8; k++){
35 			tab[i] = (tab[i]<<1) | (j&1);
36 			j >>= 1;
37 		}
38 	}
39 }
40 
41 static void
42 vncencrypt(uchar *buf, int n, char *pw)
43 {
44 	uchar *p;
45 	uchar key[9];
46 	DESstate s;
47 
48 	mktab();
49 	memset(key, 0, sizeof key);
50 	strncpy((char*)key, pw, 8);
51 	for(p=key; *p; p++)
52 		*p = tab[*p];
53 
54 	setupDESstate(&s, key, nil);
55 	desECBencrypt(buf, n, &s);
56 }
57 
58 static int
59 readln(char *prompt, char *line, int len)
60 {
61 	char *p;
62 	int fd, ctl, n, nr;
63 
64 	fd = open("/dev/cons", ORDWR);
65 	if(fd < 0)
66 		sysfatal("couldn't open cons");
67 	ctl = open("/dev/consctl", OWRITE);
68 	if(ctl < 0)
69 		sysfatal("couldn't open consctl");
70 	write(ctl, "rawon", 5);
71 	fprint(fd, "%s", prompt);
72 	nr = 0;
73 	p = line;
74 	for(;;){
75 		n = read(fd, p, 1);
76 		if(n < 0){
77 			close(fd);
78 			close(ctl);
79 			return -1;
80 		}
81 		if(n == 0 || *p == '\n' || *p == '\r'){
82 			*p = '\0';
83 			write(fd, "\n", 1);
84 			close(fd);
85 			close(ctl);
86 			return nr;
87 		}
88 		if(*p == '\b'){
89 			if(nr > 0){
90 				nr--;
91 				p--;
92 			}
93 		}else if(*p == 21){		/* cntrl-u */
94 			fprint(fd, "\n%s", prompt);
95 			nr = 0;
96 			p = line;
97 		}else{
98 			nr++;
99 			p++;
100 		}
101 		if(nr == len){
102 			fprint(fd, "line too long; try again\n%s", prompt);
103 			nr = 0;
104 			p = line;
105 		}
106 	}
107 	return -1;
108 }
109 
110 int
111 vncsrvhandshake(Vnc *v)
112 {
113 	char msg[VerLen+1];
114 
115 	strecpy(msg, msg+sizeof msg, version);
116 	if(verbose)
117 		fprint(2, "server version: %s", msg);
118 	vncwrbytes(v, msg, VerLen);
119 	vncflush(v);
120 
121 	vncrdbytes(v, msg, VerLen);
122 	if(verbose)
123 		fprint(2, "client version: %s", msg);
124 	return 0;
125 }
126 
127 int
128 vnchandshake(Vnc *v)
129 {
130 	char msg[VerLen+1];
131 
132 	msg[VerLen] = 0;
133 	vncrdbytes(v, msg, VerLen);
134 	if(strncmp(msg, "RFB ", 4) != 0){
135 		werrstr("bad rfb version \"%s\"", msg);
136 		return -1;
137 	}
138 	if(verbose)
139 		fprint(2, "server version: %s", msg);
140 	strcpy(msg, version);
141 	vncwrbytes(v, msg, VerLen);
142 	vncflush(v);
143 	return 0;
144 }
145 
146 int
147 vncauth(Vnc *v)
148 {
149 	char pw[128], *reason;
150 	uchar chal[VncChalLen];
151 	ulong auth;
152 	char *p, *server;
153 
154 	auth = vncrdlong(v);
155 	switch(auth){
156 	default:
157 		werrstr("unknown auth type 0x%lux", auth);
158 		if(verbose)
159 			fprint(2, "unknown auth type 0x%lux", auth);
160 		return -1;
161 
162 	case AFailed:
163 		reason = vncrdstring(v);
164 		werrstr("%s", reason);
165 		if(verbose)
166 			fprint(2, "auth failed: %s\n", reason);
167 		return -1;
168 
169 	case ANoAuth:
170 		if(verbose)
171 			fprint(2, "no auth needed");
172 		break;
173 
174 	case AVncAuth:
175 		vncrdbytes(v, chal, VncChalLen);
176 		server = strdup(serveraddr);
177 		p = strrchr(server, ':');
178 		if(p)
179 			*p = 0;
180 		if(auth_respond(chal, VncChalLen, nil, 0, chal, VncChalLen, auth_getkey,
181 			"proto=vnc role=client server=%s", server) != VncChalLen){
182 			/* BUG This is for drawterm users who don't start their own factotums */
183 			readln("password: ", pw, sizeof(pw));
184 			vncencrypt(chal, VncChalLen, pw);
185 			memset(pw, 0, sizeof pw);
186 		}
187 		free(server);
188 		vncwrbytes(v, chal, VncChalLen);
189 		vncflush(v);
190 
191 		auth = vncrdlong(v);
192 		switch(auth){
193 		default:
194 			werrstr("unknown server response 0x%lux", auth);
195 			return -1;
196 		case VncAuthFailed:
197 			werrstr("server says authentication failed");
198 			return -1;
199 		case VncAuthTooMany:
200 			werrstr("server says too many tries");
201 			return -1;
202 		case VncAuthOK:
203 			break;
204 		}
205 		break;
206 	}
207 	return 0;
208 }
209 
210 int
211 vncsrvauth(Vnc *v)
212 {
213 	Chalstate *c;
214 	AuthInfo *ai;
215 
216 	if((c = auth_challenge("proto=vnc role=server user=%q", getuser()))==nil)
217 		sysfatal("vncchal: %r");
218 	if(c->nchal != VncChalLen)
219 		sysfatal("vncchal got %d bytes wanted %d", c->nchal, VncChalLen);
220 	vncwrlong(v, AVncAuth);
221 	vncwrbytes(v, c->chal, VncChalLen);
222 	vncflush(v);
223 
224 	vncrdbytes(v, c->chal, VncChalLen);
225 	c->resp = c->chal;
226 	c->nresp = VncChalLen;
227 	ai = auth_response(c);
228 	auth_freechal(c);
229 	if(ai == nil){
230 		fprint(2, "vnc auth failed: server factotum: %r\n");
231 		vncwrlong(v, VncAuthFailed);
232 		vncflush(v);
233 		return -1;
234 	}
235 	auth_freeAI(ai);
236 	vncwrlong(v, VncAuthOK);
237 	vncflush(v);
238 
239 	return 0;
240 }
241 
242