auth.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. }
  96. int
  97. vncsrvhandshake(Vnc *v)
  98. {
  99. char msg[VerLen+1];
  100. strecpy(msg, msg+sizeof msg, version);
  101. if(verbose)
  102. fprint(2, "server version: %s", msg);
  103. vncwrbytes(v, msg, VerLen);
  104. vncflush(v);
  105. vncrdbytes(v, msg, VerLen);
  106. if(verbose)
  107. fprint(2, "client version: %s", msg);
  108. return 0;
  109. }
  110. int
  111. vnchandshake(Vnc *v)
  112. {
  113. char msg[VerLen+1];
  114. msg[VerLen] = 0;
  115. vncrdbytes(v, msg, VerLen);
  116. if(strncmp(msg, "RFB ", 4) != 0){
  117. werrstr("bad rfb version \"%s\"", msg);
  118. return -1;
  119. }
  120. if(verbose)
  121. fprint(2, "server version: %s", msg);
  122. strcpy(msg, version);
  123. vncwrbytes(v, msg, VerLen);
  124. vncflush(v);
  125. return 0;
  126. }
  127. int
  128. vncauth(Vnc *v, char *keypattern)
  129. {
  130. char pw[128], *reason;
  131. uchar chal[VncChalLen];
  132. ulong auth;
  133. char *p, *server;
  134. if(keypattern == nil)
  135. keypattern = "";
  136. auth = vncrdlong(v);
  137. switch(auth){
  138. default:
  139. werrstr("unknown auth type 0x%lux", auth);
  140. if(verbose)
  141. fprint(2, "unknown auth type 0x%lux", auth);
  142. return -1;
  143. case AFailed:
  144. reason = vncrdstring(v);
  145. werrstr("%s", reason);
  146. if(verbose)
  147. fprint(2, "auth failed: %s\n", reason);
  148. return -1;
  149. case ANoAuth:
  150. if(verbose)
  151. fprint(2, "no auth needed");
  152. break;
  153. case AVncAuth:
  154. vncrdbytes(v, chal, VncChalLen);
  155. server = strdup(serveraddr);
  156. p = strrchr(server, ':');
  157. if(p)
  158. *p = 0;
  159. if(auth_respond(chal, VncChalLen, nil, 0, chal, VncChalLen, auth_getkey,
  160. "proto=vnc role=client server=%s %s", server, keypattern) != VncChalLen){
  161. /* BUG This is for drawterm users who don't start their own factotums */
  162. readln("password: ", pw, sizeof(pw));
  163. vncencrypt(chal, VncChalLen, pw);
  164. memset(pw, 0, sizeof pw);
  165. }
  166. free(server);
  167. vncwrbytes(v, chal, VncChalLen);
  168. vncflush(v);
  169. auth = vncrdlong(v);
  170. switch(auth){
  171. default:
  172. werrstr("unknown server response 0x%lux", auth);
  173. return -1;
  174. case VncAuthFailed:
  175. werrstr("server says authentication failed");
  176. return -1;
  177. case VncAuthTooMany:
  178. werrstr("server says too many tries");
  179. return -1;
  180. case VncAuthOK:
  181. break;
  182. }
  183. break;
  184. }
  185. return 0;
  186. }
  187. int
  188. vncsrvauth(Vnc *v)
  189. {
  190. Chalstate *c;
  191. AuthInfo *ai;
  192. if((c = auth_challenge("proto=vnc role=server user=%q", getuser()))==nil)
  193. sysfatal("vncchal: %r");
  194. if(c->nchal != VncChalLen)
  195. sysfatal("vncchal got %d bytes wanted %d", c->nchal, VncChalLen);
  196. vncwrlong(v, AVncAuth);
  197. vncwrbytes(v, c->chal, VncChalLen);
  198. vncflush(v);
  199. vncrdbytes(v, c->chal, VncChalLen);
  200. c->resp = c->chal;
  201. c->nresp = VncChalLen;
  202. ai = auth_response(c);
  203. auth_freechal(c);
  204. if(ai == nil){
  205. fprint(2, "vnc auth failed: server factotum: %r\n");
  206. vncwrlong(v, VncAuthFailed);
  207. vncflush(v);
  208. return -1;
  209. }
  210. auth_freeAI(ai);
  211. vncwrlong(v, VncAuthOK);
  212. vncflush(v);
  213. return 0;
  214. }