8 * Encrypt n bytes using the password
9 * as key, padded with zeros to 8 bytes.
16 static char version[VerLen+1] = "RFB 003.003\n";
18 static uchar tab[256];
20 /* VNC reverses the bits of each byte before using as a des key */
35 tab[i] = (tab[i]<<1) | (j&1);
42 vncencrypt(uchar *buf, int n, char *pw)
49 memset(key, 0, sizeof key);
50 strncpy((char*)key, pw, 8);
54 setupDESstate(&s, key, nil);
55 desECBencrypt(buf, n, &s);
59 readln(char *prompt, char *line, int len)
64 fd = open("/dev/cons", ORDWR);
66 sysfatal("couldn't open cons");
67 ctl = open("/dev/consctl", OWRITE);
69 sysfatal("couldn't open consctl");
70 write(ctl, "rawon", 5);
71 fprint(fd, "%s", prompt);
81 if(n == 0 || *p == '\n' || *p == '\r'){
93 }else if(*p == 21){ /* cntrl-u */
94 fprint(fd, "\n%s", prompt);
102 fprint(fd, "line too long; try again\n%s", prompt);
110 vncsrvhandshake(Vnc *v)
114 strecpy(msg, msg+sizeof msg, version);
116 fprint(2, "server version: %s", msg);
117 vncwrbytes(v, msg, VerLen);
120 vncrdbytes(v, msg, VerLen);
122 fprint(2, "client version: %s", msg);
132 vncrdbytes(v, msg, VerLen);
133 if(strncmp(msg, "RFB ", 4) != 0){
134 werrstr("bad rfb version \"%s\"", msg);
138 fprint(2, "server version: %s", msg);
139 strcpy(msg, version);
140 vncwrbytes(v, msg, VerLen);
146 vncauth(Vnc *v, char *keypattern)
148 char pw[128], *reason;
149 uchar chal[VncChalLen];
153 if(keypattern == nil)
158 werrstr("unknown auth type 0x%lux", auth);
160 fprint(2, "unknown auth type 0x%lux", auth);
164 reason = vncrdstring(v);
165 werrstr("%s", reason);
167 fprint(2, "auth failed: %s\n", reason);
172 fprint(2, "no auth needed");
176 vncrdbytes(v, chal, VncChalLen);
177 server = strdup(serveraddr);
178 p = strrchr(server, ':');
181 if(auth_respond(chal, VncChalLen, nil, 0, chal, VncChalLen, auth_getkey,
182 "proto=vnc role=client server=%s %s", server, keypattern) != VncChalLen){
183 /* BUG This is for drawterm users who don't start their own factotums */
184 readln("password: ", pw, sizeof(pw));
185 vncencrypt(chal, VncChalLen, pw);
186 memset(pw, 0, sizeof pw);
189 vncwrbytes(v, chal, VncChalLen);
195 werrstr("unknown server response 0x%lux", auth);
198 werrstr("server says authentication failed");
201 werrstr("server says too many tries");
217 if((c = auth_challenge("proto=vnc role=server user=%q", getuser()))==nil)
218 sysfatal("vncchal: %r");
219 if(c->nchal != VncChalLen)
220 sysfatal("vncchal got %d bytes wanted %d", c->nchal, VncChalLen);
221 vncwrlong(v, AVncAuth);
222 vncwrbytes(v, c->chal, VncChalLen);
225 vncrdbytes(v, c->chal, VncChalLen);
227 c->nresp = VncChalLen;
228 ai = auth_response(c);
231 fprint(2, "vnc auth failed: server factotum: %r\n");
232 vncwrlong(v, VncAuthFailed);
237 vncwrlong(v, VncAuthOK);