rsh.c revision 1.25 1 1.25 ginsbach /* $NetBSD: rsh.c,v 1.25 2005/01/13 23:02:28 ginsbach Exp $ */
2 1.4 tls
3 1.1 cgd /*-
4 1.5 mrg * Copyright (c) 1983, 1990, 1993, 1994
5 1.5 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.22 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.11 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.11 christos __COPYRIGHT("@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
35 1.11 christos The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.11 christos #if 0
40 1.11 christos static char sccsid[] = "@(#)rsh.c 8.4 (Berkeley) 4/29/95";
41 1.11 christos #else
42 1.25 ginsbach __RCSID("$NetBSD: rsh.c,v 1.25 2005/01/13 23:02:28 ginsbach Exp $");
43 1.11 christos #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.1 cgd #include <sys/types.h>
47 1.1 cgd #include <sys/socket.h>
48 1.1 cgd #include <sys/ioctl.h>
49 1.1 cgd #include <sys/file.h>
50 1.7 mrg #include <poll.h>
51 1.1 cgd
52 1.1 cgd #include <netinet/in.h>
53 1.20 joff #include <netinet/tcp.h>
54 1.1 cgd #include <netdb.h>
55 1.1 cgd
56 1.5 mrg #include <err.h>
57 1.5 mrg #include <errno.h>
58 1.23 christos #include <limits.h>
59 1.1 cgd #include <pwd.h>
60 1.3 jtc #include <signal.h>
61 1.15 wiz #include <stdarg.h>
62 1.1 cgd #include <stdio.h>
63 1.5 mrg #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.5 mrg #include <unistd.h>
66 1.5 mrg
67 1.1 cgd #include "pathnames.h"
68 1.23 christos #include "getport.h"
69 1.1 cgd
70 1.1 cgd #ifdef KERBEROS
71 1.21 itojun #include <des.h>
72 1.1 cgd #include <kerberosIV/krb.h>
73 1.1 cgd
74 1.1 cgd CREDENTIALS cred;
75 1.1 cgd Key_schedule schedule;
76 1.1 cgd int use_kerberos = 1, doencrypt;
77 1.1 cgd char dst_realm_buf[REALM_SZ], *dest_realm;
78 1.10 christos
79 1.15 wiz void warning(const char *, ...);
80 1.1 cgd #endif
81 1.1 cgd
82 1.1 cgd /*
83 1.1 cgd * rsh - remote shell
84 1.1 cgd */
85 1.10 christos int remerr;
86 1.10 christos
87 1.10 christos static int sigs[] = { SIGINT, SIGTERM, SIGQUIT };
88 1.1 cgd
89 1.15 wiz char *copyargs(char **);
90 1.15 wiz void sendsig(int);
91 1.15 wiz int checkfd(struct pollfd *, int);
92 1.15 wiz void talk(int, sigset_t *, pid_t, int);
93 1.15 wiz void usage(void);
94 1.15 wiz int main(int, char **);
95 1.12 christos #ifdef IN_RCMD
96 1.15 wiz int orcmd(char **, int, const char *,
97 1.15 wiz const char *, const char *, int *);
98 1.15 wiz int orcmd_af(char **, int, const char *,
99 1.15 wiz const char *, const char *, int *, int);
100 1.12 christos #endif
101 1.5 mrg
102 1.5 mrg int
103 1.15 wiz main(int argc, char **argv)
104 1.1 cgd {
105 1.1 cgd struct passwd *pw;
106 1.1 cgd struct servent *sp;
107 1.10 christos sigset_t oset, nset;
108 1.20 joff struct protoent *proto;
109 1.10 christos
110 1.7 mrg #ifdef IN_RCMD
111 1.7 mrg char *locuser = 0, *loop;
112 1.7 mrg #endif /* IN_RCMD */
113 1.10 christos int argoff, asrsh, ch, dflag, nflag, one, rem, i;
114 1.5 mrg pid_t pid;
115 1.5 mrg uid_t uid;
116 1.9 mrg char *args, *host, *p, *user, *name;
117 1.1 cgd
118 1.1 cgd argoff = asrsh = dflag = nflag = 0;
119 1.1 cgd one = 1;
120 1.1 cgd host = user = NULL;
121 1.17 hubertf sp = NULL;
122 1.1 cgd
123 1.8 mrg #ifndef IN_RCMD
124 1.7 mrg /*
125 1.8 mrg * If called as something other than "rsh" use it as the host name,
126 1.8 mrg * only for rsh.
127 1.7 mrg */
128 1.14 cgd if (strcmp(getprogname(), "rsh") == 0)
129 1.7 mrg asrsh = 1;
130 1.14 cgd else {
131 1.14 cgd host = strdup(getprogname());
132 1.14 cgd if (host == NULL)
133 1.14 cgd err(1, NULL);
134 1.14 cgd }
135 1.8 mrg #endif /* IN_RCMD */
136 1.1 cgd
137 1.1 cgd /* handle "rsh host flags" */
138 1.1 cgd if (!host && argc > 2 && argv[1][0] != '-') {
139 1.1 cgd host = argv[1];
140 1.1 cgd argoff = 1;
141 1.1 cgd }
142 1.1 cgd
143 1.7 mrg #ifdef IN_RCMD
144 1.7 mrg if ((loop = getenv("RCMD_LOOP")) && strcmp(loop, "YES") == 0)
145 1.7 mrg warnx("rcmd appears to be looping!");
146 1.7 mrg
147 1.7 mrg putenv("RCMD_LOOP=YES");
148 1.7 mrg
149 1.7 mrg # ifdef KERBEROS
150 1.7 mrg # ifdef CRYPT
151 1.17 hubertf # define OPTIONS "8KLdek:l:np:u:wx"
152 1.7 mrg # else
153 1.17 hubertf # define OPTIONS "8KLdek:l:np:u:w"
154 1.7 mrg # endif
155 1.7 mrg # else
156 1.17 hubertf # define OPTIONS "8KLdel:np:u:w"
157 1.7 mrg # endif
158 1.7 mrg
159 1.7 mrg #else /* IN_RCMD */
160 1.7 mrg
161 1.7 mrg # ifdef KERBEROS
162 1.7 mrg # ifdef CRYPT
163 1.17 hubertf # define OPTIONS "8KLdek:l:np:wx"
164 1.7 mrg # else
165 1.17 hubertf # define OPTIONS "8KLdek:l:np:w"
166 1.7 mrg # endif
167 1.7 mrg # else
168 1.17 hubertf # define OPTIONS "8KLdel:np:w"
169 1.7 mrg # endif
170 1.7 mrg
171 1.7 mrg #endif /* IN_RCMD */
172 1.7 mrg
173 1.7 mrg if (!(pw = getpwuid(uid = getuid())))
174 1.7 mrg errx(1, "unknown user id");
175 1.7 mrg
176 1.9 mrg if ((name = strdup(pw->pw_name)) == NULL)
177 1.9 mrg err(1, "malloc");
178 1.10 christos while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != -1)
179 1.1 cgd switch(ch) {
180 1.1 cgd case 'K':
181 1.1 cgd #ifdef KERBEROS
182 1.1 cgd use_kerberos = 0;
183 1.1 cgd #endif
184 1.1 cgd break;
185 1.1 cgd case 'L': /* -8Lew are ignored to allow rlogin aliases */
186 1.1 cgd case 'e':
187 1.1 cgd case 'w':
188 1.1 cgd case '8':
189 1.1 cgd break;
190 1.1 cgd case 'd':
191 1.1 cgd dflag = 1;
192 1.1 cgd break;
193 1.1 cgd case 'l':
194 1.1 cgd user = optarg;
195 1.1 cgd break;
196 1.1 cgd #ifdef KERBEROS
197 1.1 cgd case 'k':
198 1.16 itojun strlcpy(dest_realm_buf, optarg, sizeof(dest_realm_buf));
199 1.1 cgd dest_realm = dst_realm_buf;
200 1.1 cgd break;
201 1.1 cgd #endif
202 1.1 cgd case 'n':
203 1.1 cgd nflag = 1;
204 1.1 cgd break;
205 1.17 hubertf case 'p':
206 1.23 christos sp = getport(optarg, "tcp");
207 1.17 hubertf break;
208 1.7 mrg #ifdef IN_RCMD
209 1.7 mrg case 'u':
210 1.9 mrg if (getuid() != 0 && optarg && name &&
211 1.9 mrg strcmp(name, optarg) != 0)
212 1.7 mrg errx(1,"only super user can use the -u option");
213 1.7 mrg locuser = optarg;
214 1.7 mrg break;
215 1.7 mrg #endif /* IN_RCMD */
216 1.1 cgd #ifdef KERBEROS
217 1.1 cgd #ifdef CRYPT
218 1.1 cgd case 'x':
219 1.1 cgd doencrypt = 1;
220 1.10 christos des_set_key((des_cblock *) cred.session, schedule);
221 1.1 cgd break;
222 1.1 cgd #endif
223 1.1 cgd #endif
224 1.1 cgd case '?':
225 1.1 cgd default:
226 1.1 cgd usage();
227 1.1 cgd }
228 1.1 cgd optind += argoff;
229 1.1 cgd
230 1.1 cgd /* if haven't gotten a host yet, do so */
231 1.1 cgd if (!host && !(host = argv[optind++]))
232 1.1 cgd usage();
233 1.1 cgd
234 1.1 cgd /* if no further arguments, must have been called as rlogin. */
235 1.1 cgd if (!argv[optind]) {
236 1.7 mrg #ifdef IN_RCMD
237 1.7 mrg usage();
238 1.7 mrg #else
239 1.1 cgd if (asrsh)
240 1.23 christos *argv = __UNCONST("rlogin");
241 1.1 cgd execv(_PATH_RLOGIN, argv);
242 1.5 mrg err(1, "can't exec %s", _PATH_RLOGIN);
243 1.7 mrg #endif
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd argc -= optind;
247 1.1 cgd argv += optind;
248 1.1 cgd
249 1.5 mrg /* Accept user1@host format, though "-l user2" overrides user1 */
250 1.5 mrg p = strchr(host, '@');
251 1.5 mrg if (p) {
252 1.5 mrg *p = '\0';
253 1.5 mrg if (!user && p > host)
254 1.5 mrg user = host;
255 1.5 mrg host = p + 1;
256 1.5 mrg if (*host == '\0')
257 1.5 mrg usage();
258 1.1 cgd }
259 1.1 cgd if (!user)
260 1.9 mrg user = name;
261 1.1 cgd
262 1.1 cgd #ifdef KERBEROS
263 1.1 cgd #ifdef CRYPT
264 1.1 cgd /* -x turns off -n */
265 1.1 cgd if (doencrypt)
266 1.1 cgd nflag = 0;
267 1.1 cgd #endif
268 1.1 cgd #endif
269 1.1 cgd
270 1.1 cgd args = copyargs(argv);
271 1.1 cgd
272 1.1 cgd #ifdef KERBEROS
273 1.1 cgd if (use_kerberos) {
274 1.17 hubertf if (sp == NULL) {
275 1.17 hubertf sp = getservbyname((doencrypt ? "ekshell" : "kshell"), "tcp");
276 1.17 hubertf }
277 1.1 cgd if (sp == NULL) {
278 1.1 cgd use_kerberos = 0;
279 1.1 cgd warning("can't get entry for %s/tcp service",
280 1.1 cgd doencrypt ? "ekshell" : "kshell");
281 1.1 cgd }
282 1.1 cgd }
283 1.1 cgd #endif
284 1.1 cgd if (sp == NULL)
285 1.1 cgd sp = getservbyname("shell", "tcp");
286 1.5 mrg if (sp == NULL)
287 1.5 mrg errx(1, "shell/tcp: unknown service");
288 1.1 cgd
289 1.1 cgd #ifdef KERBEROS
290 1.1 cgd try_connect:
291 1.1 cgd if (use_kerberos) {
292 1.7 mrg #if 1
293 1.5 mrg struct hostent *hp;
294 1.5 mrg
295 1.5 mrg /* fully qualify hostname (needed for krb_realmofhost) */
296 1.5 mrg hp = gethostbyname(host);
297 1.5 mrg if (hp != NULL && !(host = strdup(hp->h_name)))
298 1.10 christos err(1, "strdup");
299 1.7 mrg #endif
300 1.5 mrg
301 1.1 cgd rem = KSUCCESS;
302 1.1 cgd errno = 0;
303 1.1 cgd if (dest_realm == NULL)
304 1.1 cgd dest_realm = krb_realmofhost(host);
305 1.1 cgd
306 1.1 cgd #ifdef CRYPT
307 1.1 cgd if (doencrypt)
308 1.1 cgd rem = krcmd_mutual(&host, sp->s_port, user, args,
309 1.10 christos &remerr, dest_realm, &cred, schedule);
310 1.1 cgd else
311 1.1 cgd #endif
312 1.10 christos rem = krcmd(&host, sp->s_port, user, args, &remerr,
313 1.1 cgd dest_realm);
314 1.1 cgd if (rem < 0) {
315 1.1 cgd use_kerberos = 0;
316 1.1 cgd sp = getservbyname("shell", "tcp");
317 1.5 mrg if (sp == NULL)
318 1.5 mrg errx(1, "shell/tcp: unknown service");
319 1.1 cgd if (errno == ECONNREFUSED)
320 1.1 cgd warning("remote host doesn't support Kerberos");
321 1.1 cgd if (errno == ENOENT)
322 1.1 cgd warning("can't provide Kerberos auth data");
323 1.1 cgd goto try_connect;
324 1.1 cgd }
325 1.1 cgd } else {
326 1.5 mrg if (doencrypt)
327 1.7 mrg errx(1, "the -x flag requires Kerberos authentication.");
328 1.7 mrg #ifdef IN_RCMD
329 1.13 itojun rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
330 1.7 mrg #else
331 1.13 itojun rem = rcmd_af(&host, sp->s_port,
332 1.7 mrg #endif
333 1.9 mrg name,
334 1.13 itojun user, args, &remerr, PF_UNSPEC);
335 1.1 cgd }
336 1.7 mrg #else /* KERBEROS */
337 1.7 mrg
338 1.7 mrg #ifdef IN_RCMD
339 1.13 itojun rem = orcmd_af(&host, sp->s_port, locuser ? locuser :
340 1.1 cgd #else
341 1.13 itojun rem = rcmd_af(&host, sp->s_port,
342 1.1 cgd #endif
343 1.13 itojun name, user, args, &remerr, PF_UNSPEC);
344 1.7 mrg #endif /* KERBEROS */
345 1.9 mrg (void)free(name);
346 1.1 cgd
347 1.1 cgd if (rem < 0)
348 1.1 cgd exit(1);
349 1.1 cgd
350 1.10 christos if (remerr < 0)
351 1.5 mrg errx(1, "can't establish stderr");
352 1.1 cgd if (dflag) {
353 1.1 cgd if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
354 1.1 cgd sizeof(one)) < 0)
355 1.7 mrg warn("setsockopt remote");
356 1.10 christos if (setsockopt(remerr, SOL_SOCKET, SO_DEBUG, &one,
357 1.1 cgd sizeof(one)) < 0)
358 1.7 mrg warn("setsockopt stderr");
359 1.1 cgd }
360 1.20 joff proto = getprotobyname("tcp");
361 1.20 joff setsockopt(rem, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
362 1.20 joff setsockopt(remerr, proto->p_proto, TCP_NODELAY, &one, sizeof(one));
363 1.20 joff
364 1.1 cgd
365 1.25 ginsbach (void)setuid(uid);
366 1.10 christos
367 1.25 ginsbach (void)sigemptyset(&nset);
368 1.10 christos for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++)
369 1.25 ginsbach (void)sigaddset(&nset, sigs[i]);
370 1.10 christos
371 1.25 ginsbach (void)sigprocmask(SIG_BLOCK, &nset, &oset);
372 1.10 christos
373 1.10 christos for (i = 0; i < sizeof(sigs) / sizeof(sigs[0]); i++) {
374 1.10 christos struct sigaction sa;
375 1.10 christos
376 1.10 christos if (sa.sa_handler != SIG_IGN) {
377 1.25 ginsbach sa.sa_handler = sendsig;
378 1.25 ginsbach (void)sigaction(sigs[i], &sa, NULL);
379 1.10 christos }
380 1.10 christos }
381 1.1 cgd
382 1.1 cgd if (!nflag) {
383 1.1 cgd pid = fork();
384 1.5 mrg if (pid < 0)
385 1.5 mrg err(1, "fork");
386 1.1 cgd }
387 1.10 christos else
388 1.10 christos pid = -1;
389 1.1 cgd
390 1.10 christos #if defined(KERBEROS) && defined(CRYPT)
391 1.1 cgd if (!doencrypt)
392 1.1 cgd #endif
393 1.1 cgd {
394 1.10 christos (void)ioctl(remerr, FIONBIO, &one);
395 1.1 cgd (void)ioctl(rem, FIONBIO, &one);
396 1.1 cgd }
397 1.1 cgd
398 1.10 christos talk(nflag, &oset, pid, rem);
399 1.1 cgd
400 1.1 cgd if (!nflag)
401 1.1 cgd (void)kill(pid, SIGKILL);
402 1.1 cgd exit(0);
403 1.1 cgd }
404 1.1 cgd
405 1.10 christos int
406 1.15 wiz checkfd(struct pollfd *fdp, int outfd)
407 1.10 christos {
408 1.10 christos int nr, nw;
409 1.10 christos char buf[BUFSIZ];
410 1.10 christos
411 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
412 1.10 christos return -1;
413 1.10 christos
414 1.10 christos if ((fdp->revents & POLLIN) == 0)
415 1.10 christos return 0;
416 1.10 christos
417 1.10 christos errno = 0;
418 1.10 christos #if defined(KERBEROS) && defined(CRYPT)
419 1.10 christos if (doencrypt)
420 1.10 christos nr = des_read(fdp->fd, buf, sizeof buf);
421 1.10 christos else
422 1.10 christos #endif
423 1.10 christos nr = read(fdp->fd, buf, sizeof buf);
424 1.10 christos
425 1.10 christos if (nr <= 0) {
426 1.10 christos if (errno != EAGAIN)
427 1.10 christos return -1;
428 1.10 christos else
429 1.10 christos return 0;
430 1.10 christos }
431 1.10 christos else {
432 1.10 christos char *bc = buf;
433 1.10 christos while (nr) {
434 1.10 christos if ((nw = write(outfd, bc, nr)) <= 0)
435 1.10 christos return -1;
436 1.10 christos nr -= nw;
437 1.10 christos bc += nw;
438 1.10 christos }
439 1.10 christos return 0;
440 1.10 christos }
441 1.10 christos }
442 1.10 christos
443 1.5 mrg void
444 1.15 wiz talk(int nflag, sigset_t *oset, __pid_t pid, int rem)
445 1.1 cgd {
446 1.10 christos int nr, nw, nfds;
447 1.7 mrg struct pollfd fds[2], *fdp = &fds[0];
448 1.5 mrg char *bp, buf[BUFSIZ];
449 1.1 cgd
450 1.1 cgd if (!nflag && pid == 0) {
451 1.10 christos (void)close(remerr);
452 1.1 cgd
453 1.10 christos fdp->events = POLLOUT|POLLNVAL|POLLERR|POLLHUP;
454 1.10 christos fdp->fd = rem;
455 1.10 christos nr = 0;
456 1.1 cgd bp = buf;
457 1.1 cgd
458 1.10 christos for (;;) {
459 1.10 christos errno = 0;
460 1.10 christos
461 1.10 christos if (nr == 0) {
462 1.10 christos if ((nr = read(0, buf, sizeof buf)) == 0)
463 1.10 christos goto done;
464 1.10 christos if (nr == -1) {
465 1.10 christos if (errno == EIO)
466 1.10 christos goto done;
467 1.10 christos if (errno == EINTR)
468 1.10 christos continue;
469 1.10 christos err(1, "read");
470 1.10 christos }
471 1.10 christos bp = buf;
472 1.10 christos }
473 1.10 christos
474 1.10 christos rewrite: if (poll(fdp, 1, INFTIM) == -1) {
475 1.10 christos if (errno != EINTR)
476 1.10 christos err(1, "poll");
477 1.10 christos goto rewrite;
478 1.10 christos }
479 1.10 christos
480 1.10 christos if (fdp->revents & (POLLNVAL|POLLERR|POLLHUP))
481 1.7 mrg err(1, "poll");
482 1.10 christos
483 1.10 christos if ((fdp->revents & POLLOUT) == 0)
484 1.10 christos goto rewrite;
485 1.10 christos
486 1.10 christos #if defined(KERBEROS) && defined(CRYPT)
487 1.10 christos if (doencrypt)
488 1.10 christos nw = des_write(rem, bp, nr);
489 1.10 christos else
490 1.1 cgd #endif
491 1.10 christos nw = write(rem, bp, nr);
492 1.10 christos
493 1.10 christos if (nw < 0) {
494 1.10 christos if (errno == EAGAIN)
495 1.10 christos continue;
496 1.10 christos err(1, "write");
497 1.10 christos }
498 1.10 christos bp += nw;
499 1.10 christos nr -= nw;
500 1.1 cgd }
501 1.1 cgd done:
502 1.1 cgd (void)shutdown(rem, 1);
503 1.1 cgd exit(0);
504 1.1 cgd }
505 1.1 cgd
506 1.25 ginsbach (void)sigprocmask(SIG_SETMASK, oset, NULL);
507 1.10 christos fds[0].events = fds[1].events = POLLIN|POLLNVAL|POLLERR|POLLHUP;
508 1.10 christos fds[0].fd = remerr;
509 1.7 mrg fds[1].fd = rem;
510 1.7 mrg fdp = &fds[0];
511 1.7 mrg nfds = 2;
512 1.1 cgd do {
513 1.10 christos if (poll(fdp, nfds, INFTIM) == -1) {
514 1.5 mrg if (errno != EINTR)
515 1.7 mrg err(1, "poll");
516 1.1 cgd continue;
517 1.1 cgd }
518 1.10 christos if (fds[0].events != 0 && checkfd(&fds[0], 2) == -1) {
519 1.10 christos nfds--;
520 1.10 christos fds[0].events = 0;
521 1.10 christos fdp = &fds[1];
522 1.1 cgd }
523 1.10 christos if (fds[1].events != 0 && checkfd(&fds[1], 1) == -1) {
524 1.10 christos nfds--;
525 1.10 christos fds[1].events = 0;
526 1.1 cgd }
527 1.10 christos }
528 1.10 christos while (nfds);
529 1.1 cgd }
530 1.1 cgd
531 1.1 cgd void
532 1.15 wiz sendsig(int sig)
533 1.5 mrg {
534 1.1 cgd char signo;
535 1.5 mrg
536 1.5 mrg signo = sig;
537 1.1 cgd #ifdef KERBEROS
538 1.1 cgd #ifdef CRYPT
539 1.1 cgd if (doencrypt)
540 1.10 christos (void)des_write(remerr, &signo, 1);
541 1.1 cgd else
542 1.1 cgd #endif
543 1.1 cgd #endif
544 1.10 christos (void)write(remerr, &signo, 1);
545 1.1 cgd }
546 1.1 cgd
547 1.1 cgd #ifdef KERBEROS
548 1.1 cgd /* VARARGS */
549 1.5 mrg void
550 1.10 christos warning(const char *fmt, ...)
551 1.1 cgd {
552 1.1 cgd va_list ap;
553 1.1 cgd
554 1.10 christos va_start(ap, fmt);
555 1.25 ginsbach (void)fprintf(stderr, "%s: warning, using standard rsh: ",
556 1.14 cgd getprogname());
557 1.25 ginsbach (void)vfprintf(stderr, fmt, ap);
558 1.1 cgd va_end(ap);
559 1.25 ginsbach (void)fprintf(stderr, ".\n");
560 1.1 cgd }
561 1.1 cgd #endif
562 1.1 cgd
563 1.1 cgd char *
564 1.15 wiz copyargs(char **argv)
565 1.1 cgd {
566 1.5 mrg int cc;
567 1.18 itojun char **ap, *args, *p, *ep;
568 1.1 cgd
569 1.1 cgd cc = 0;
570 1.1 cgd for (ap = argv; *ap; ++ap)
571 1.1 cgd cc += strlen(*ap) + 1;
572 1.5 mrg if (!(args = malloc((u_int)cc)))
573 1.10 christos err(1, "malloc");
574 1.18 itojun ep = args + cc;
575 1.7 mrg for (p = args, *p = '\0', ap = argv; *ap; ++ap) {
576 1.18 itojun (void)strlcpy(p, *ap, ep - p);
577 1.7 mrg p += strlen(p);
578 1.1 cgd if (ap[1])
579 1.1 cgd *p++ = ' ';
580 1.1 cgd }
581 1.7 mrg *p = '\0';
582 1.5 mrg return (args);
583 1.1 cgd }
584 1.1 cgd
585 1.5 mrg void
586 1.15 wiz usage(void)
587 1.1 cgd {
588 1.5 mrg
589 1.1 cgd (void)fprintf(stderr,
590 1.17 hubertf "usage: %s [-nd%s]%s[-l login] [-p port]%s [login@]host %s\n", getprogname(),
591 1.1 cgd #ifdef KERBEROS
592 1.1 cgd #ifdef CRYPT
593 1.7 mrg "x", " [-k realm] ",
594 1.7 mrg #else
595 1.7 mrg "", " [-k realm] ",
596 1.7 mrg #endif
597 1.1 cgd #else
598 1.7 mrg "", " ",
599 1.1 cgd #endif
600 1.7 mrg #ifdef IN_RCMD
601 1.7 mrg " [-u locuser]", "command"
602 1.1 cgd #else
603 1.7 mrg "", "[command]"
604 1.1 cgd #endif
605 1.7 mrg );
606 1.1 cgd exit(1);
607 1.1 cgd }
608