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