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