rsh.c revision 1.5 1 1.5 mrg /* $NetBSD: rsh.c,v 1.5 1997/02/16 15:01:05 mrg 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.5 mrg static char copyright[] =
38 1.5 mrg "@(#) Copyright (c) 1983, 1990, 1993, 1994\n\
39 1.5 mrg The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.5 mrg /*static char sccsid[] = "from: @(#)rsh.c 8.4 (Berkeley) 4/29/95";*/
44 1.5 mrg static char rcsid[] = "$NetBSD: rsh.c,v 1.5 1997/02/16 15:01:05 mrg Exp $";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #include <sys/types.h>
48 1.1 cgd #include <sys/socket.h>
49 1.1 cgd #include <sys/ioctl.h>
50 1.1 cgd #include <sys/file.h>
51 1.1 cgd
52 1.1 cgd #include <netinet/in.h>
53 1.1 cgd #include <netdb.h>
54 1.1 cgd
55 1.5 mrg #include <err.h>
56 1.5 mrg #include <errno.h>
57 1.1 cgd #include <pwd.h>
58 1.3 jtc #include <signal.h>
59 1.1 cgd #include <stdio.h>
60 1.5 mrg #include <stdlib.h>
61 1.1 cgd #include <string.h>
62 1.5 mrg #include <unistd.h>
63 1.1 cgd #include <varargs.h>
64 1.5 mrg
65 1.1 cgd #include "pathnames.h"
66 1.1 cgd
67 1.1 cgd #ifdef KERBEROS
68 1.1 cgd #include <kerberosIV/des.h>
69 1.1 cgd #include <kerberosIV/krb.h>
70 1.1 cgd
71 1.1 cgd CREDENTIALS cred;
72 1.1 cgd Key_schedule schedule;
73 1.1 cgd int use_kerberos = 1, doencrypt;
74 1.1 cgd char dst_realm_buf[REALM_SZ], *dest_realm;
75 1.1 cgd extern char *krb_realmofhost();
76 1.1 cgd #endif
77 1.1 cgd
78 1.1 cgd /*
79 1.1 cgd * rsh - remote shell
80 1.1 cgd */
81 1.5 mrg int rfd2;
82 1.1 cgd
83 1.5 mrg char *copyargs __P((char **));
84 1.5 mrg void sendsig __P((int));
85 1.5 mrg void talk __P((int, long, pid_t, int));
86 1.5 mrg void usage __P((void));
87 1.5 mrg void warning __P(());
88 1.5 mrg
89 1.5 mrg int
90 1.1 cgd main(argc, argv)
91 1.1 cgd int argc;
92 1.1 cgd char **argv;
93 1.1 cgd {
94 1.1 cgd struct passwd *pw;
95 1.1 cgd struct servent *sp;
96 1.1 cgd long omask;
97 1.5 mrg int argoff, asrsh, ch, dflag, nflag, one, rem;
98 1.5 mrg pid_t pid;
99 1.5 mrg uid_t uid;
100 1.5 mrg char *args, *host, *p, *user;
101 1.1 cgd
102 1.1 cgd argoff = asrsh = dflag = nflag = 0;
103 1.1 cgd one = 1;
104 1.1 cgd host = user = NULL;
105 1.1 cgd
106 1.1 cgd /* if called as something other than "rsh", use it as the host name */
107 1.5 mrg if (p = strrchr(argv[0], '/'))
108 1.1 cgd ++p;
109 1.1 cgd else
110 1.1 cgd p = argv[0];
111 1.1 cgd if (strcmp(p, "rsh"))
112 1.1 cgd host = p;
113 1.1 cgd else
114 1.1 cgd asrsh = 1;
115 1.1 cgd
116 1.1 cgd /* handle "rsh host flags" */
117 1.1 cgd if (!host && argc > 2 && argv[1][0] != '-') {
118 1.1 cgd host = argv[1];
119 1.1 cgd argoff = 1;
120 1.1 cgd }
121 1.1 cgd
122 1.1 cgd #ifdef KERBEROS
123 1.1 cgd #ifdef CRYPT
124 1.1 cgd #define OPTIONS "8KLdek:l:nwx"
125 1.1 cgd #else
126 1.1 cgd #define OPTIONS "8KLdek:l:nw"
127 1.1 cgd #endif
128 1.1 cgd #else
129 1.1 cgd #define OPTIONS "8KLdel:nw"
130 1.1 cgd #endif
131 1.1 cgd while ((ch = getopt(argc - argoff, argv + argoff, OPTIONS)) != EOF)
132 1.1 cgd switch(ch) {
133 1.1 cgd case 'K':
134 1.1 cgd #ifdef KERBEROS
135 1.1 cgd use_kerberos = 0;
136 1.1 cgd #endif
137 1.1 cgd break;
138 1.1 cgd case 'L': /* -8Lew are ignored to allow rlogin aliases */
139 1.1 cgd case 'e':
140 1.1 cgd case 'w':
141 1.1 cgd case '8':
142 1.1 cgd break;
143 1.1 cgd case 'd':
144 1.1 cgd dflag = 1;
145 1.1 cgd break;
146 1.1 cgd case 'l':
147 1.1 cgd user = optarg;
148 1.1 cgd break;
149 1.1 cgd #ifdef KERBEROS
150 1.1 cgd case 'k':
151 1.1 cgd dest_realm = dst_realm_buf;
152 1.1 cgd strncpy(dest_realm, optarg, REALM_SZ);
153 1.1 cgd break;
154 1.1 cgd #endif
155 1.1 cgd case 'n':
156 1.1 cgd nflag = 1;
157 1.1 cgd break;
158 1.1 cgd #ifdef KERBEROS
159 1.1 cgd #ifdef CRYPT
160 1.1 cgd case 'x':
161 1.1 cgd doencrypt = 1;
162 1.1 cgd des_set_key(cred.session, schedule);
163 1.1 cgd break;
164 1.1 cgd #endif
165 1.1 cgd #endif
166 1.1 cgd case '?':
167 1.1 cgd default:
168 1.1 cgd usage();
169 1.1 cgd }
170 1.1 cgd optind += argoff;
171 1.1 cgd
172 1.1 cgd /* if haven't gotten a host yet, do so */
173 1.1 cgd if (!host && !(host = argv[optind++]))
174 1.1 cgd usage();
175 1.1 cgd
176 1.1 cgd /* if no further arguments, must have been called as rlogin. */
177 1.1 cgd if (!argv[optind]) {
178 1.1 cgd if (asrsh)
179 1.1 cgd *argv = "rlogin";
180 1.1 cgd execv(_PATH_RLOGIN, argv);
181 1.5 mrg err(1, "can't exec %s", _PATH_RLOGIN);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd argc -= optind;
185 1.1 cgd argv += optind;
186 1.1 cgd
187 1.5 mrg if (!(pw = getpwuid(uid = getuid())))
188 1.5 mrg errx(1, "unknown user id");
189 1.5 mrg /* Accept user1@host format, though "-l user2" overrides user1 */
190 1.5 mrg p = strchr(host, '@');
191 1.5 mrg if (p) {
192 1.5 mrg *p = '\0';
193 1.5 mrg if (!user && p > host)
194 1.5 mrg user = host;
195 1.5 mrg host = p + 1;
196 1.5 mrg if (*host == '\0')
197 1.5 mrg usage();
198 1.1 cgd }
199 1.1 cgd if (!user)
200 1.1 cgd user = pw->pw_name;
201 1.1 cgd
202 1.1 cgd #ifdef KERBEROS
203 1.1 cgd #ifdef CRYPT
204 1.1 cgd /* -x turns off -n */
205 1.1 cgd if (doencrypt)
206 1.1 cgd nflag = 0;
207 1.1 cgd #endif
208 1.1 cgd #endif
209 1.1 cgd
210 1.1 cgd args = copyargs(argv);
211 1.1 cgd
212 1.1 cgd sp = NULL;
213 1.1 cgd #ifdef KERBEROS
214 1.1 cgd if (use_kerberos) {
215 1.1 cgd sp = getservbyname((doencrypt ? "ekshell" : "kshell"), "tcp");
216 1.1 cgd if (sp == NULL) {
217 1.1 cgd use_kerberos = 0;
218 1.1 cgd warning("can't get entry for %s/tcp service",
219 1.1 cgd doencrypt ? "ekshell" : "kshell");
220 1.1 cgd }
221 1.1 cgd }
222 1.1 cgd #endif
223 1.1 cgd if (sp == NULL)
224 1.1 cgd sp = getservbyname("shell", "tcp");
225 1.5 mrg if (sp == NULL)
226 1.5 mrg errx(1, "shell/tcp: unknown service");
227 1.1 cgd
228 1.1 cgd #ifdef KERBEROS
229 1.1 cgd try_connect:
230 1.1 cgd if (use_kerberos) {
231 1.5 mrg struct hostent *hp;
232 1.5 mrg
233 1.5 mrg /* fully qualify hostname (needed for krb_realmofhost) */
234 1.5 mrg hp = gethostbyname(host);
235 1.5 mrg if (hp != NULL && !(host = strdup(hp->h_name)))
236 1.5 mrg err(1, NULL);
237 1.5 mrg
238 1.1 cgd rem = KSUCCESS;
239 1.1 cgd errno = 0;
240 1.1 cgd if (dest_realm == NULL)
241 1.1 cgd dest_realm = krb_realmofhost(host);
242 1.1 cgd
243 1.1 cgd #ifdef CRYPT
244 1.1 cgd if (doencrypt)
245 1.1 cgd rem = krcmd_mutual(&host, sp->s_port, user, args,
246 1.1 cgd &rfd2, dest_realm, &cred, schedule);
247 1.1 cgd else
248 1.1 cgd #endif
249 1.1 cgd rem = krcmd(&host, sp->s_port, user, args, &rfd2,
250 1.1 cgd dest_realm);
251 1.1 cgd if (rem < 0) {
252 1.1 cgd use_kerberos = 0;
253 1.1 cgd sp = getservbyname("shell", "tcp");
254 1.5 mrg if (sp == NULL)
255 1.5 mrg errx(1, "shell/tcp: unknown service");
256 1.1 cgd if (errno == ECONNREFUSED)
257 1.1 cgd warning("remote host doesn't support Kerberos");
258 1.1 cgd if (errno == ENOENT)
259 1.1 cgd warning("can't provide Kerberos auth data");
260 1.1 cgd goto try_connect;
261 1.1 cgd }
262 1.1 cgd } else {
263 1.5 mrg if (doencrypt)
264 1.5 mrg errx(1, "the -x flag requires Kerberos authentication");
265 1.1 cgd rem = rcmd(&host, sp->s_port, pw->pw_name, user, args, &rfd2);
266 1.1 cgd }
267 1.1 cgd #else
268 1.1 cgd rem = rcmd(&host, sp->s_port, pw->pw_name, user, args, &rfd2);
269 1.1 cgd #endif
270 1.1 cgd
271 1.1 cgd if (rem < 0)
272 1.1 cgd exit(1);
273 1.1 cgd
274 1.5 mrg if (rfd2 < 0)
275 1.5 mrg errx(1, "can't establish stderr");
276 1.1 cgd if (dflag) {
277 1.1 cgd if (setsockopt(rem, SOL_SOCKET, SO_DEBUG, &one,
278 1.1 cgd sizeof(one)) < 0)
279 1.5 mrg warn("setsockopt");
280 1.1 cgd if (setsockopt(rfd2, SOL_SOCKET, SO_DEBUG, &one,
281 1.1 cgd sizeof(one)) < 0)
282 1.5 mrg warn("setsockopt");
283 1.1 cgd }
284 1.1 cgd
285 1.1 cgd (void)setuid(uid);
286 1.1 cgd omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGTERM));
287 1.1 cgd if (signal(SIGINT, SIG_IGN) != SIG_IGN)
288 1.1 cgd (void)signal(SIGINT, sendsig);
289 1.1 cgd if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
290 1.1 cgd (void)signal(SIGQUIT, sendsig);
291 1.1 cgd if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
292 1.1 cgd (void)signal(SIGTERM, sendsig);
293 1.1 cgd
294 1.1 cgd if (!nflag) {
295 1.1 cgd pid = fork();
296 1.5 mrg if (pid < 0)
297 1.5 mrg err(1, "fork");
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd #ifdef KERBEROS
301 1.1 cgd #ifdef CRYPT
302 1.1 cgd if (!doencrypt)
303 1.1 cgd #endif
304 1.1 cgd #endif
305 1.1 cgd {
306 1.1 cgd (void)ioctl(rfd2, FIONBIO, &one);
307 1.1 cgd (void)ioctl(rem, FIONBIO, &one);
308 1.1 cgd }
309 1.1 cgd
310 1.1 cgd talk(nflag, omask, pid, rem);
311 1.1 cgd
312 1.1 cgd if (!nflag)
313 1.1 cgd (void)kill(pid, SIGKILL);
314 1.1 cgd exit(0);
315 1.1 cgd }
316 1.1 cgd
317 1.5 mrg void
318 1.1 cgd talk(nflag, omask, pid, rem)
319 1.5 mrg int nflag;
320 1.1 cgd long omask;
321 1.5 mrg pid_t pid;
322 1.5 mrg int rem;
323 1.1 cgd {
324 1.5 mrg int cc, wc;
325 1.5 mrg fd_set readfrom, ready, rembits;
326 1.5 mrg char *bp, buf[BUFSIZ];
327 1.1 cgd
328 1.1 cgd if (!nflag && pid == 0) {
329 1.1 cgd (void)close(rfd2);
330 1.1 cgd
331 1.1 cgd reread: errno = 0;
332 1.1 cgd if ((cc = read(0, buf, sizeof buf)) <= 0)
333 1.1 cgd goto done;
334 1.1 cgd bp = buf;
335 1.1 cgd
336 1.5 mrg rewrite:
337 1.5 mrg FD_ZERO(&rembits);
338 1.5 mrg FD_SET(rem, &rembits);
339 1.1 cgd if (select(16, 0, &rembits, 0, 0) < 0) {
340 1.5 mrg if (errno != EINTR)
341 1.5 mrg err(1, "select");
342 1.1 cgd goto rewrite;
343 1.1 cgd }
344 1.5 mrg if (!FD_ISSET(rem, &rembits))
345 1.1 cgd goto rewrite;
346 1.1 cgd #ifdef KERBEROS
347 1.1 cgd #ifdef CRYPT
348 1.1 cgd if (doencrypt)
349 1.1 cgd wc = des_write(rem, bp, cc);
350 1.1 cgd else
351 1.1 cgd #endif
352 1.1 cgd #endif
353 1.1 cgd wc = write(rem, bp, cc);
354 1.1 cgd if (wc < 0) {
355 1.1 cgd if (errno == EWOULDBLOCK)
356 1.1 cgd goto rewrite;
357 1.1 cgd goto done;
358 1.1 cgd }
359 1.1 cgd bp += wc;
360 1.1 cgd cc -= wc;
361 1.1 cgd if (cc == 0)
362 1.1 cgd goto reread;
363 1.1 cgd goto rewrite;
364 1.1 cgd done:
365 1.1 cgd (void)shutdown(rem, 1);
366 1.1 cgd exit(0);
367 1.1 cgd }
368 1.1 cgd
369 1.1 cgd (void)sigsetmask(omask);
370 1.5 mrg FD_ZERO(&readfrom);
371 1.5 mrg FD_SET(rfd2, &readfrom);
372 1.5 mrg FD_SET(rem, &readfrom);
373 1.1 cgd do {
374 1.1 cgd ready = readfrom;
375 1.1 cgd if (select(16, &ready, 0, 0, 0) < 0) {
376 1.5 mrg if (errno != EINTR)
377 1.5 mrg err(1, "select");
378 1.1 cgd continue;
379 1.1 cgd }
380 1.5 mrg if (FD_ISSET(rfd2, &ready)) {
381 1.1 cgd errno = 0;
382 1.1 cgd #ifdef KERBEROS
383 1.1 cgd #ifdef CRYPT
384 1.1 cgd if (doencrypt)
385 1.1 cgd cc = des_read(rfd2, buf, sizeof buf);
386 1.1 cgd else
387 1.1 cgd #endif
388 1.1 cgd #endif
389 1.1 cgd cc = read(rfd2, buf, sizeof buf);
390 1.1 cgd if (cc <= 0) {
391 1.1 cgd if (errno != EWOULDBLOCK)
392 1.5 mrg FD_CLR(rfd2, &readfrom);
393 1.1 cgd } else
394 1.1 cgd (void)write(2, buf, cc);
395 1.1 cgd }
396 1.5 mrg if (FD_ISSET(rem, &ready)) {
397 1.1 cgd errno = 0;
398 1.1 cgd #ifdef KERBEROS
399 1.1 cgd #ifdef CRYPT
400 1.1 cgd if (doencrypt)
401 1.1 cgd cc = des_read(rem, buf, sizeof buf);
402 1.1 cgd else
403 1.1 cgd #endif
404 1.1 cgd #endif
405 1.1 cgd cc = read(rem, buf, sizeof buf);
406 1.1 cgd if (cc <= 0) {
407 1.1 cgd if (errno != EWOULDBLOCK)
408 1.5 mrg FD_CLR(rem, &readfrom);
409 1.1 cgd } else
410 1.1 cgd (void)write(1, buf, cc);
411 1.1 cgd }
412 1.5 mrg } while (FD_ISSET(rfd2, &readfrom) || FD_ISSET(rem, &readfrom));
413 1.1 cgd }
414 1.1 cgd
415 1.1 cgd void
416 1.5 mrg sendsig(sig)
417 1.5 mrg int sig;
418 1.5 mrg {
419 1.1 cgd char signo;
420 1.5 mrg
421 1.5 mrg signo = sig;
422 1.1 cgd #ifdef KERBEROS
423 1.1 cgd #ifdef CRYPT
424 1.1 cgd if (doencrypt)
425 1.1 cgd (void)des_write(rfd2, &signo, 1);
426 1.1 cgd else
427 1.1 cgd #endif
428 1.1 cgd #endif
429 1.1 cgd (void)write(rfd2, &signo, 1);
430 1.1 cgd }
431 1.1 cgd
432 1.1 cgd #ifdef KERBEROS
433 1.1 cgd /* VARARGS */
434 1.5 mrg void
435 1.1 cgd warning(va_alist)
436 1.1 cgd va_dcl
437 1.1 cgd {
438 1.1 cgd va_list ap;
439 1.1 cgd char *fmt;
440 1.1 cgd
441 1.1 cgd (void)fprintf(stderr, "rsh: warning, using standard rsh: ");
442 1.1 cgd va_start(ap);
443 1.1 cgd fmt = va_arg(ap, char *);
444 1.1 cgd vfprintf(stderr, fmt, ap);
445 1.1 cgd va_end(ap);
446 1.1 cgd (void)fprintf(stderr, ".\n");
447 1.1 cgd }
448 1.1 cgd #endif
449 1.1 cgd
450 1.1 cgd char *
451 1.1 cgd copyargs(argv)
452 1.1 cgd char **argv;
453 1.1 cgd {
454 1.5 mrg int cc;
455 1.5 mrg char **ap, *args, *p;
456 1.1 cgd
457 1.1 cgd cc = 0;
458 1.1 cgd for (ap = argv; *ap; ++ap)
459 1.1 cgd cc += strlen(*ap) + 1;
460 1.5 mrg if (!(args = malloc((u_int)cc)))
461 1.5 mrg err(1, NULL);
462 1.1 cgd for (p = args, ap = argv; *ap; ++ap) {
463 1.1 cgd (void)strcpy(p, *ap);
464 1.1 cgd for (p = strcpy(p, *ap); *p; ++p);
465 1.1 cgd if (ap[1])
466 1.1 cgd *p++ = ' ';
467 1.1 cgd }
468 1.5 mrg return (args);
469 1.1 cgd }
470 1.1 cgd
471 1.5 mrg void
472 1.1 cgd usage()
473 1.1 cgd {
474 1.5 mrg
475 1.1 cgd (void)fprintf(stderr,
476 1.5 mrg "usage: rsh [-nd%s]%s[-l login] [login@]host [command]\n",
477 1.1 cgd #ifdef KERBEROS
478 1.1 cgd #ifdef CRYPT
479 1.1 cgd "x", " [-k realm] ");
480 1.1 cgd #else
481 1.1 cgd "", " [-k realm] ");
482 1.1 cgd #endif
483 1.1 cgd #else
484 1.1 cgd "", " ");
485 1.1 cgd #endif
486 1.1 cgd exit(1);
487 1.1 cgd }
488