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