sshserve.c 5.7 KB

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