auth.c 4.1 KB

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