6 int eof; /* send an eof if true */
7 int crtonl; /* convert all received \r to \n */
8 int returns; /* strip \r on reception */
9 char *note = "die: yankee dog";
10 char *ruser; /* for BSD authentication */
14 void rcpu(int, char*);
15 void tcpexec(int, char*, char*);
16 int call(char *, char*, char*, char**);
17 char *buildargs(char*[]);
19 void error(char*, char*);
24 fprint(2, "usage: %s [-e] [-T] [-r] [-k keypattern] [-l user] net!host command...\n", argv0);
29 main(int argc, char *argv[])
31 char *host, *addr, *args;
54 ruser = EARGF(usage());
63 args = buildargs(&argv[1]);
65 fd = call(0, host, "rcpu", &addr);
68 fd = call(0, host, "rexexec", &addr);
73 /* specific attempts */
74 fd = call("tcp", host, "shell", &addr);
76 tcpexec(fd, addr, args);
78 error("can't dial", host);
83 call(char *net, char *host, char *service, char **na)
85 *na = netmkaddr(host, net, service);
86 return dial(*na, 0, 0, 0);
90 rcpu(int fd, char *cmd)
97 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", key);
99 error("auth_proxy", nil);
101 conn = (TLSconn*)mallocz(sizeof *conn, 1);
102 conn->pskID = "p9secret";
103 conn->psk = ai->secret;
104 conn->psklen = ai->nsecret;
105 fd = tlsClient(fd, conn);
107 error("tlsClient", nil);
111 cmd = smprint("service=rx exec rc -lc %q\n", cmd);
112 if(fprint(fd, "%7ld\n%s", strlen(cmd), cmd) < 0)
117 while((n=read(fd, buf, sizeof buf))>0)
118 if(write(1, buf, n)!=n)
119 error("write error", 0);
121 postnote(PNPROC, kid, note);/**/
126 rex(int fd, char *cmd)
132 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", key);
134 error("auth_proxy", nil);
137 write(fd, cmd, strlen(cmd)+1);
140 while((n=read(fd, buf, sizeof buf))>0)
141 if(write(1, buf, n)!=n)
142 error("write error", 0);
144 postnote(PNPROC, kid, note);/**/
149 tcpexec(int fd, char *addr, char *cmd)
151 char *cp, *ep, *u, *ru, buf[4096];
155 * do the ucb authentication and send command
161 if(write(fd, "", 1)<0 || write(fd, u, strlen(u)+1)<0
162 || write(fd, ru, strlen(ru)+1)<0 || write(fd, cmd, strlen(cmd)+1)<0){
164 error("can't authenticate to", addr);
168 * get authentication reply
170 if(read(fd, buf, 1) != 1){
172 error("can't authenticate to", addr);
175 while(read(fd, buf, 1) == 1){
181 error("rejected by", addr);
185 while((n=read(fd, buf, sizeof buf))>0){
187 /* convert cr's to nl's */
188 for (cp = buf; cp < buf + n; cp++)
193 /* convert cr's to null's */
196 while(cp < ep && (cp = memchr(cp, '\r', ep-cp))){
197 memmove(cp, cp+1, ep-cp-1);
202 if(write(1, buf, n)!=n)
203 error("write error", 0);
206 postnote(PNPROC, kid, note);/**/
216 switch(kid = fork()){
218 error("fork error", 0);
224 while((n=read(0, buf, sizeof buf))>0)
225 if(write(fd, buf, n)!=n)
226 exits("write error");
231 return 0; /* to keep compiler happy */
235 error(char *s, char *z)
238 fprint(2, "%s: %s: %r\n", argv0, s);
240 fprint(2, "%s: %s %s: %r\n", argv0, s, z);
245 buildargs(char *argv[])
254 m = strlen(*argv) + 1;
255 args = realloc(args, n+m +1);
257 error("malloc fail", 0);
258 args[n] = ' '; /* smashes old null */
259 strcpy(args+n+1, *argv);