sshserve.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "ssh.h"
  10. char *cipherlist = "blowfish rc4 3des";
  11. char *authlist = "tis";
  12. void fromnet(Conn*);
  13. void startcmd(Conn*, char*, int*, int*);
  14. int maxmsg = 256*1024;
  15. Cipher *allcipher[] = {
  16. &cipherrc4,
  17. &cipherblowfish,
  18. &cipher3des,
  19. &cipherdes,
  20. &ciphernone,
  21. &ciphertwiddle,
  22. };
  23. Authsrv *allauthsrv[] = {
  24. &authsrvpassword,
  25. &authsrvtis,
  26. };
  27. Cipher*
  28. findcipher(char *name, Cipher **list, int nlist)
  29. {
  30. int i;
  31. for(i=0; i<nlist; i++)
  32. if(strcmp(name, list[i]->name) == 0)
  33. return list[i];
  34. error("unknown cipher %s", name);
  35. return nil;
  36. }
  37. Authsrv*
  38. findauthsrv(char *name, Authsrv **list, int nlist)
  39. {
  40. int i;
  41. for(i=0; i<nlist; i++)
  42. if(strcmp(name, list[i]->name) == 0)
  43. return list[i];
  44. error("unknown authsrv %s", name);
  45. return nil;
  46. }
  47. void
  48. usage(void)
  49. {
  50. fprint(2, "usage: sshserve [-A authlist] [-c cipherlist] client-ip-address\n");
  51. exits("usage");
  52. }
  53. void
  54. main(int argc, char **argv)
  55. {
  56. char *f[16];
  57. int i;
  58. Conn c;
  59. fmtinstall('B', mpfmt);
  60. fmtinstall('H', encodefmt);
  61. atexit(atexitkiller);
  62. atexitkill(getpid());
  63. memset(&c, 0, sizeof c);
  64. ARGBEGIN{
  65. case 'D':
  66. debuglevel = atoi(EARGF(usage()));
  67. break;
  68. case 'A':
  69. authlist = EARGF(usage());
  70. break;
  71. case 'c':
  72. cipherlist = EARGF(usage());
  73. break;
  74. default:
  75. usage();
  76. }ARGEND
  77. if(argc != 1)
  78. usage();
  79. c.host = argv[0];
  80. sshlog("connect from %s", c.host);
  81. /* limit of 768 bits in remote host key? */
  82. c.serverpriv = rsagen(768, 6, 0);
  83. if(c.serverpriv == nil)
  84. sysfatal("rsagen failed: %r");
  85. c.serverkey = &c.serverpriv->pub;
  86. c.nokcipher = getfields(cipherlist, f, nelem(f), 1, ", ");
  87. c.okcipher = emalloc(sizeof(Cipher*)*c.nokcipher);
  88. for(i=0; i<c.nokcipher; i++)
  89. c.okcipher[i] = findcipher(f[i], allcipher, nelem(allcipher));
  90. c.nokauthsrv = getfields(authlist, f, nelem(f), 1, ", ");
  91. c.okauthsrv = emalloc(sizeof(Authsrv*)*c.nokauthsrv);
  92. for(i=0; i<c.nokauthsrv; i++)
  93. c.okauthsrv[i] = findauthsrv(f[i], allauthsrv, nelem(allauthsrv));
  94. sshserverhandshake(&c);
  95. fromnet(&c);
  96. }
  97. void
  98. fromnet(Conn *c)
  99. {
  100. int infd, kidpid, n;
  101. char *cmd;
  102. Msg *m;
  103. infd = kidpid = -1;
  104. for(;;){
  105. m = recvmsg(c, -1);
  106. if(m == nil)
  107. exits(nil);
  108. switch(m->type){
  109. default:
  110. //badmsg(m, 0);
  111. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  112. break;
  113. case SSH_MSG_DISCONNECT:
  114. sysfatal("client disconnected");
  115. case SSH_CMSG_REQUEST_PTY:
  116. sendmsg(allocmsg(c, SSH_SMSG_SUCCESS, 0));
  117. break;
  118. case SSH_CMSG_X11_REQUEST_FORWARDING:
  119. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  120. break;
  121. case SSH_CMSG_MAX_PACKET_SIZE:
  122. maxmsg = getlong(m);
  123. sendmsg(allocmsg(c, SSH_SMSG_SUCCESS, 0));
  124. break;
  125. case SSH_CMSG_REQUEST_COMPRESSION:
  126. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  127. break;
  128. case SSH_CMSG_EXEC_SHELL:
  129. startcmd(c, nil, &kidpid, &infd);
  130. goto InteractiveMode;
  131. case SSH_CMSG_EXEC_CMD:
  132. cmd = getstring(m);
  133. startcmd(c, cmd, &kidpid, &infd);
  134. goto InteractiveMode;
  135. }
  136. free(m);
  137. }
  138. InteractiveMode:
  139. for(;;){
  140. free(m);
  141. m = recvmsg(c, -1);
  142. if(m == nil)
  143. exits(nil);
  144. switch(m->type){
  145. default:
  146. badmsg(m, 0);
  147. case SSH_MSG_DISCONNECT:
  148. postnote(PNGROUP, kidpid, "hangup");
  149. sysfatal("client disconnected");
  150. case SSH_CMSG_STDIN_DATA:
  151. if(infd != 0){
  152. n = getlong(m);
  153. write(infd, getbytes(m, n), n);
  154. }
  155. break;
  156. case SSH_CMSG_EOF:
  157. close(infd);
  158. infd = -1;
  159. break;
  160. case SSH_CMSG_EXIT_CONFIRMATION:
  161. /* sent by some clients as dying breath */
  162. exits(nil);
  163. case SSH_CMSG_WINDOW_SIZE:
  164. /* we don't care */
  165. break;
  166. }
  167. }
  168. }
  169. void
  170. copyout(Conn *c, int fd, int mtype)
  171. {
  172. char buf[8192];
  173. int n, max, pid;
  174. Msg *m;
  175. max = sizeof buf;
  176. if(max > maxmsg - 32) /* 32 is an overestimate of packet overhead */
  177. max = maxmsg - 32;
  178. if(max <= 0)
  179. sysfatal("maximum message size too small");
  180. switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
  181. case -1:
  182. sysfatal("fork: %r");
  183. case 0:
  184. break;
  185. default:
  186. atexitkill(pid);
  187. return;
  188. }
  189. while((n = read(fd, buf, max)) > 0){
  190. m = allocmsg(c, mtype, 4+n);
  191. putlong(m, n);
  192. putbytes(m, buf, n);
  193. sendmsg(m);
  194. }
  195. exits(nil);
  196. }
  197. void
  198. startcmd(Conn *c, char *cmd, int *kidpid, int *kidin)
  199. {
  200. int i, pid, kpid;
  201. int pfd[3][2];
  202. char *dir;
  203. char *sysname, *tz;
  204. Msg *m;
  205. Waitmsg *w;
  206. for(i=0; i<3; i++)
  207. if(pipe(pfd[i]) < 0)
  208. sysfatal("pipe: %r");
  209. sysname = getenv("sysname");
  210. tz = getenv("timezone");
  211. switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
  212. case -1:
  213. sysfatal("fork: %r");
  214. case 0:
  215. switch(kpid = rfork(RFPROC|RFNOTEG|RFENVG|RFFDG)){
  216. case -1:
  217. sysfatal("fork: %r");
  218. case 0:
  219. for(i=0; i<3; i++){
  220. if(dup(pfd[i][1], i) < 0)
  221. sysfatal("dup: %r");
  222. close(pfd[i][0]);
  223. close(pfd[i][1]);
  224. }
  225. putenv("user", c->user);
  226. if(sysname)
  227. putenv("sysname", sysname);
  228. if(tz)
  229. putenv("tz", tz);
  230. dir = smprint("/usr/%s", c->user);
  231. if(dir == nil || chdir(dir) < 0)
  232. chdir("/");
  233. if(cmd){
  234. putenv("service", "rx");
  235. execl("/bin/rc", "rc", "-lc", cmd, nil);
  236. sysfatal("cannot exec /bin/rc: %r");
  237. }else{
  238. putenv("service", "con");
  239. execl("/bin/ip/telnetd", "telnetd", "-tn", nil);
  240. sysfatal("cannot exec /bin/ip/telnetd: %r");
  241. }
  242. default:
  243. *kidpid = kpid;
  244. rendezvous(kidpid, 0);
  245. for(;;){
  246. if((w = wait()) == nil)
  247. sysfatal("wait: %r");
  248. if(w->pid == kpid)
  249. break;
  250. free(w);
  251. }
  252. if(w->msg[0]){
  253. m = allocmsg(c, SSH_MSG_DISCONNECT, 4+strlen(w->msg));
  254. putstring(m, w->msg);
  255. sendmsg(m);
  256. }else{
  257. m = allocmsg(c, SSH_SMSG_EXITSTATUS, 4);
  258. putlong(m, 0);
  259. sendmsg(m);
  260. }
  261. for(i=0; i<3; i++)
  262. close(pfd[i][0]);
  263. free(w);
  264. exits(nil);
  265. break;
  266. }
  267. default:
  268. atexitkill(pid);
  269. rendezvous(kidpid, 0);
  270. break;
  271. }
  272. for(i=0; i<3; i++)
  273. close(pfd[i][1]);
  274. copyout(c, pfd[1][0], SSH_SMSG_STDOUT_DATA);
  275. copyout(c, pfd[2][0], SSH_SMSG_STDERR_DATA);
  276. *kidin = pfd[0][0];
  277. }