rx.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. int eof; /* send an eof if true */
  5. int crtonl; /* convert all received \r to \n */
  6. int returns; /* strip \r on reception */
  7. char *note = "die: yankee dog";
  8. char *ruser;
  9. char *key;
  10. void rex(int, char*, char*);
  11. void tcpexec(int, char*, char*);
  12. int call(char *, char*, char*, char**);
  13. char *buildargs(char*[]);
  14. int send(int);
  15. void error(char*, char*);
  16. void sshexec(char *host, char *cmd);
  17. void
  18. usage(void)
  19. {
  20. fprint(2, "usage: %s [-e] [-T] [-r] [-k keypattern] [-l user] net!host command...\n", argv0);
  21. exits("usage");
  22. }
  23. void
  24. main(int argc, char *argv[])
  25. {
  26. char *host, *addr, *args;
  27. int fd;
  28. key = "";
  29. eof = 1;
  30. crtonl = 0;
  31. returns = 1;
  32. ARGBEGIN{
  33. case 'T':
  34. crtonl = 1;
  35. break;
  36. case 'r':
  37. returns = 0;
  38. break;
  39. case 'e':
  40. eof = 0;
  41. break;
  42. case 'k':
  43. key = EARGF(usage());
  44. break;
  45. case 'l':
  46. ruser = EARGF(usage());
  47. break;
  48. default:
  49. usage();
  50. }ARGEND
  51. if(argc < 2)
  52. usage();
  53. host = argv[0];
  54. args = buildargs(&argv[1]);
  55. /* try erexexec p9any then dial again with p9sk2 */
  56. fd = call(0, host, "rexexec", &addr);
  57. if(fd >= 0)
  58. rex(fd, args, "p9any");
  59. close(fd);
  60. fd = call(0, host, "rexexec", &addr);
  61. if(fd >= 0)
  62. rex(fd, args, "p9sk2");
  63. close(fd);
  64. /* if there's an ssh port, try that */
  65. fd = call("tcp", host, "ssh", &addr);
  66. if(fd >= 0){
  67. close(fd);
  68. sshexec(host, args);
  69. /* falls through if no ssh */
  70. }
  71. /* specific attempts */
  72. fd = call("tcp", host, "shell", &addr);
  73. if(fd >= 0)
  74. tcpexec(fd, addr, args);
  75. error("can't dial", host);
  76. exits(0);
  77. }
  78. int
  79. call(char *net, char *host, char *service, char **na)
  80. {
  81. *na = netmkaddr(host, net, service);
  82. return dial(*na, 0, 0, 0);
  83. }
  84. void
  85. rex(int fd, char *cmd, char *proto)
  86. {
  87. char buf[4096];
  88. int kid, n;
  89. AuthInfo *ai;
  90. ai = auth_proxy(fd, auth_getkey, "proto=%s role=client %s", proto, key);
  91. if(ai == nil){
  92. if(strcmp(proto, "p9any") == 0)
  93. return;
  94. error("auth_proxy", nil);
  95. }
  96. write(fd, cmd, strlen(cmd)+1);
  97. kid = send(fd);
  98. while((n=read(fd, buf, sizeof buf))>0)
  99. if(write(1, buf, n)!=n)
  100. error("write error", 0);
  101. sleep(250);
  102. postnote(PNPROC, kid, note);/**/
  103. exits(0);
  104. }
  105. void
  106. tcpexec(int fd, char *addr, char *cmd)
  107. {
  108. char *cp, *ep, *u, buf[4096];
  109. int kid, n;
  110. char *r;
  111. /*
  112. * do the ucb authentication and send command
  113. */
  114. u = getuser();
  115. r = ruser ? ruser : u;
  116. if(write(fd, "", 1)<0 || write(fd, u, strlen(u)+1)<0
  117. || write(fd, r, strlen(r)+1)<0 || write(fd, cmd, strlen(cmd)+1)<0){
  118. close(fd);
  119. error("can't authenticate to", addr);
  120. }
  121. /*
  122. * get authentication reply
  123. */
  124. if(read(fd, buf, 1) != 1){
  125. close(fd);
  126. error("can't authenticate to", addr);
  127. }
  128. if(buf[0] != 0){
  129. while(read(fd, buf, 1) == 1){
  130. write(2, buf, 1);
  131. if(buf[0] == '\n')
  132. break;
  133. }
  134. close(fd);
  135. error("rejected by", addr);
  136. }
  137. kid = send(fd);
  138. while((n=read(fd, buf, sizeof buf))>0){
  139. if(crtonl) {
  140. /* convert cr's to nl's */
  141. for (cp = buf; cp < buf + n; cp++)
  142. if (*cp == '\r')
  143. *cp = '\n';
  144. }
  145. else if(!returns){
  146. /* convert cr's to null's */
  147. cp = buf;
  148. ep = buf + n;
  149. while(cp < ep && (cp = memchr(cp, '\r', ep-cp))){
  150. memmove(cp, cp+1, ep-cp-1);
  151. ep--;
  152. n--;
  153. }
  154. }
  155. if(write(1, buf, n)!=n)
  156. error("write error", 0);
  157. }
  158. sleep(250);
  159. postnote(PNPROC, kid, note);/**/
  160. exits(0);
  161. }
  162. void
  163. sshexec(char *host, char *cmd)
  164. {
  165. execl("/bin/ssh", "ssh", "-iCm", host, cmd, 0);
  166. }
  167. int
  168. send(int fd)
  169. {
  170. char buf[4096];
  171. int n;
  172. int kid;
  173. switch(kid = fork()){
  174. case -1:
  175. error("fork error", 0);
  176. case 0:
  177. break;
  178. default:
  179. return kid;
  180. }
  181. while((n=read(0, buf, sizeof buf))>0)
  182. if(write(fd, buf, n)!=n)
  183. exits("write error");
  184. if(eof)
  185. write(fd, buf, 0);
  186. exits(0);
  187. return 0; /* to keep compiler happy */
  188. }
  189. void
  190. error(char *s, char *z)
  191. {
  192. if(z == 0)
  193. fprint(2, "%s: %s: %r\n", argv0, s);
  194. else
  195. fprint(2, "%s: %s %s: %r\n", argv0, s, z);
  196. exits(s);
  197. }
  198. char *
  199. buildargs(char *argv[])
  200. {
  201. char *args;
  202. int m, n;
  203. args = malloc(1);
  204. args[0] = '\0';
  205. n = 0;
  206. while(*argv){
  207. m = strlen(*argv) + 1;
  208. args = realloc(args, n+m +1);
  209. if(args == 0)
  210. error("malloc fail", 0);
  211. args[n] = ' '; /* smashes old null */
  212. strcpy(args+n+1, *argv);
  213. n += m;
  214. argv++;
  215. }
  216. return args;
  217. }