auth.c 4.6 KB

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