sshserve.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. exits("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. /* limit of 768 bits in remote host key? */
  74. c.serverpriv = rsagen(768, 6, 0);
  75. if(c.serverpriv == nil)
  76. sysfatal("rsagen failed: %r");
  77. c.serverkey = &c.serverpriv->pub;
  78. c.nokcipher = getfields(cipherlist, f, nelem(f), 1, ", ");
  79. c.okcipher = emalloc(sizeof(Cipher*)*c.nokcipher);
  80. for(i=0; i<c.nokcipher; i++)
  81. c.okcipher[i] = findcipher(f[i], allcipher, nelem(allcipher));
  82. c.nokauthsrv = getfields(authlist, f, nelem(f), 1, ", ");
  83. c.okauthsrv = emalloc(sizeof(Authsrv*)*c.nokauthsrv);
  84. for(i=0; i<c.nokauthsrv; i++)
  85. c.okauthsrv[i] = findauthsrv(f[i], allauthsrv, nelem(allauthsrv));
  86. sshserverhandshake(&c);
  87. fromnet(&c);
  88. }
  89. void
  90. fromnet(Conn *c)
  91. {
  92. int infd, kidpid, n;
  93. char *cmd;
  94. Msg *m;
  95. infd = kidpid = -1;
  96. for(;;){
  97. m = recvmsg(c, -1);
  98. if(m == nil)
  99. exits(nil);
  100. switch(m->type){
  101. default:
  102. //badmsg(m, 0);
  103. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  104. break;
  105. case SSH_MSG_DISCONNECT:
  106. sysfatal("client disconnected");
  107. case SSH_CMSG_REQUEST_PTY:
  108. sendmsg(allocmsg(c, SSH_SMSG_SUCCESS, 0));
  109. break;
  110. case SSH_CMSG_X11_REQUEST_FORWARDING:
  111. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  112. break;
  113. case SSH_CMSG_MAX_PACKET_SIZE:
  114. maxmsg = getlong(m);
  115. sendmsg(allocmsg(c, SSH_SMSG_SUCCESS, 0));
  116. break;
  117. case SSH_CMSG_REQUEST_COMPRESSION:
  118. sendmsg(allocmsg(c, SSH_SMSG_FAILURE, 0));
  119. break;
  120. case SSH_CMSG_EXEC_SHELL:
  121. startcmd(c, nil, &kidpid, &infd);
  122. goto InteractiveMode;
  123. case SSH_CMSG_EXEC_CMD:
  124. cmd = getstring(m);
  125. startcmd(c, cmd, &kidpid, &infd);
  126. goto InteractiveMode;
  127. }
  128. free(m);
  129. }
  130. InteractiveMode:
  131. for(;;){
  132. free(m);
  133. m = recvmsg(c, -1);
  134. if(m == nil)
  135. exits(nil);
  136. switch(m->type){
  137. default:
  138. badmsg(m, 0);
  139. case SSH_MSG_DISCONNECT:
  140. postnote(PNGROUP, kidpid, "hangup");
  141. sysfatal("client disconnected");
  142. case SSH_CMSG_STDIN_DATA:
  143. if(infd != 0){
  144. n = getlong(m);
  145. write(infd, getbytes(m, n), n);
  146. }
  147. break;
  148. case SSH_CMSG_EOF:
  149. close(infd);
  150. infd = -1;
  151. break;
  152. case SSH_CMSG_EXIT_CONFIRMATION:
  153. /* sent by some clients as dying breath */
  154. exits(nil);
  155. case SSH_CMSG_WINDOW_SIZE:
  156. /* we don't care */
  157. break;
  158. }
  159. }
  160. }
  161. void
  162. copyout(Conn *c, int fd, int mtype)
  163. {
  164. char buf[8192];
  165. int n, max, pid;
  166. Msg *m;
  167. max = sizeof buf;
  168. if(max > maxmsg - 32) /* 32 is an overestimate of packet overhead */
  169. max = maxmsg - 32;
  170. if(max <= 0)
  171. sysfatal("maximum message size too small");
  172. switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
  173. case -1:
  174. sysfatal("fork: %r");
  175. case 0:
  176. break;
  177. default:
  178. atexitkill(pid);
  179. return;
  180. }
  181. while((n = read(fd, buf, max)) > 0){
  182. m = allocmsg(c, mtype, 4+n);
  183. putlong(m, n);
  184. putbytes(m, buf, n);
  185. sendmsg(m);
  186. }
  187. exits(nil);
  188. }
  189. void
  190. startcmd(Conn *c, char *cmd, int *kidpid, int *kidin)
  191. {
  192. int i, pid, kpid;
  193. int pfd[3][2];
  194. char *dir;
  195. char *sysname, *tz;
  196. Msg *m;
  197. Waitmsg *w;
  198. for(i=0; i<3; i++)
  199. if(pipe(pfd[i]) < 0)
  200. sysfatal("pipe: %r");
  201. sysname = getenv("sysname");
  202. tz = getenv("timezone");
  203. switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
  204. case -1:
  205. sysfatal("fork: %r");
  206. case 0:
  207. switch(kpid = rfork(RFPROC|RFNOTEG|RFENVG|RFFDG)){
  208. case -1:
  209. sysfatal("fork: %r");
  210. case 0:
  211. for(i=0; i<3; i++){
  212. if(dup(pfd[i][1], i) < 0)
  213. sysfatal("dup: %r");
  214. close(pfd[i][0]);
  215. close(pfd[i][1]);
  216. }
  217. putenv("user", c->user);
  218. if(sysname)
  219. putenv("sysname", sysname);
  220. if(tz)
  221. putenv("tz", tz);
  222. dir = smprint("/usr/%s", c->user);
  223. if(dir == nil || chdir(dir) < 0)
  224. chdir("/");
  225. if(cmd){
  226. putenv("service", "rx");
  227. execl("/bin/rc", "rc", "-lc", cmd, nil);
  228. sysfatal("cannot exec /bin/rc: %r");
  229. }else{
  230. putenv("service", "con");
  231. execl("/bin/ip/telnetd", "telnetd", "-tn", nil);
  232. sysfatal("cannot exec /bin/ip/telnetd: %r");
  233. }
  234. default:
  235. *kidpid = kpid;
  236. rendezvous(kidpid, 0);
  237. for(;;){
  238. if((w = wait()) == nil)
  239. sysfatal("wait: %r");
  240. if(w->pid == kpid)
  241. break;
  242. free(w);
  243. }
  244. if(w->msg[0]){
  245. m = allocmsg(c, SSH_MSG_DISCONNECT, 4+strlen(w->msg));
  246. putstring(m, w->msg);
  247. sendmsg(m);
  248. }else{
  249. m = allocmsg(c, SSH_SMSG_EXITSTATUS, 4);
  250. putlong(m, 0);
  251. sendmsg(m);
  252. }
  253. for(i=0; i<3; i++)
  254. close(pfd[i][0]);
  255. free(w);
  256. exits(nil);
  257. break;
  258. }
  259. default:
  260. atexitkill(pid);
  261. rendezvous(kidpid, 0);
  262. break;
  263. }
  264. for(i=0; i<3; i++)
  265. close(pfd[i][1]);
  266. copyout(c, pfd[1][0], SSH_SMSG_STDOUT_DATA);
  267. copyout(c, pfd[2][0], SSH_SMSG_STDERR_DATA);
  268. *kidin = pfd[0][0];
  269. }