Initial import from FreeBSD RELENG_4:
[games.git] / crypto / kerberosIV / appl / bsd / rshd.c
1 /*-
2  * Copyright (c) 1988, 1989, 1992, 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
34 /*
35  * remote shell server:
36  *      [port]\0
37  *      remuser\0
38  *      locuser\0
39  *      command\0
40  *      data
41  */
42
43 #include "bsd_locl.h"
44
45 RCSID("$Id: rshd.c,v 1.60.2.3 2000/10/18 20:39:12 assar Exp $");
46
47 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
48 extern int __check_rhosts_file;
49
50 static int      keepalive = 1;
51 static int      log_success;    /* If TRUE, log all successful accesses */
52 static int      new_pag = 1;    /* Put process in new PAG by default */
53 static int      no_inetd = 0;
54 static int      sent_null;
55
56 static void              doit (struct sockaddr_in *);
57 static void              error (const char *, ...)
58 #ifdef __GNUC__
59 __attribute__ ((format (printf, 1, 2)))
60 #endif
61 ;
62 static void              usage (void);
63
64 #define VERSION_SIZE    9
65 #define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
66 #define OPTIONS         "alnkvxLp:Pi"
67 AUTH_DAT authbuf;
68 KTEXT_ST tickbuf;
69 int     doencrypt, use_kerberos, vacuous;
70 Key_schedule    schedule;
71
72 int
73 main(int argc, char *argv[])
74 {
75     struct linger linger;
76     int ch, on = 1, fromlen;
77     struct sockaddr_in from;
78     int portnum = 0;
79
80     set_progname(argv[0]);
81
82     openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
83
84     opterr = 0;
85     while ((ch = getopt(argc, argv, OPTIONS)) != -1)
86         switch (ch) {
87         case 'a':
88             break;
89         case 'l':
90             __check_rhosts_file = 0;
91             break;
92         case 'n':
93             keepalive = 0;
94             break;
95         case 'k':
96             use_kerberos = 1;
97             break;
98
99         case 'v':
100             vacuous = 1;
101             break;
102
103         case 'x':
104             doencrypt = 1;
105             break;
106         case 'L':
107             log_success = 1;
108             break;
109         case 'p':
110             portnum = htons(atoi(optarg));
111             break;
112         case 'P':
113             new_pag = 0;
114             break;
115         case 'i':
116             no_inetd = 1;
117             break;
118         case '?':
119         default:
120             usage();
121             break;
122         }
123
124     argc -= optind;
125     argv += optind;
126
127     if (use_kerberos && vacuous) {
128         syslog(LOG_ERR, "only one of -k and -v allowed");
129         exit(2);
130     }
131     if (doencrypt && !use_kerberos) {
132         syslog(LOG_ERR, "-k is required for -x");
133         exit(2);
134     }
135
136     if (no_inetd) {
137         if(portnum == 0)
138             portnum = get_shell_port (use_kerberos, doencrypt);
139         mini_inetd (portnum);
140     }
141
142     fromlen = sizeof (from);
143     if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
144         syslog(LOG_ERR, "getpeername: %m");
145         _exit(1);
146     }
147 #ifdef HAVE_SETSOCKOPT
148 #ifdef SO_KEEPALIVE
149     if (keepalive &&
150         setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
151                    sizeof(on)) < 0)
152         syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
153 #endif
154 #ifdef SO_LINGER
155     linger.l_onoff = 1;
156     linger.l_linger = 60;                       /* XXX */
157     if (setsockopt(0, SOL_SOCKET, SO_LINGER, (void *)&linger,
158                    sizeof (linger)) < 0)
159         syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
160 #endif
161 #endif /* HAVE_SETSOCKOPT */
162     doit(&from);
163     /* NOTREACHED */
164     return 0;
165 }
166
167 char    username[20] = "USER=";
168 char    homedir[64] = "HOME=";
169 char    shell[64] = "SHELL=";
170 char    path[100] = "PATH=";
171 char    *envinit[] =
172 {homedir, shell, path, username, 0};
173
174 static void
175 xgetstr(char *buf, int cnt, char *err)
176 {
177     char c;
178
179     do {
180         if (read(STDIN_FILENO, &c, 1) != 1)
181             exit(1);
182         *buf++ = c;
183         if (--cnt == 0) {
184             error("%s too long\n", err);
185             exit(1);
186         }
187     } while (c != 0);
188 }
189
190 static void
191 doit(struct sockaddr_in *fromp)
192 {
193     struct passwd *pwd;
194     u_short port;
195     fd_set ready, readfrom;
196     int cc, nfd, pv[2], pid, s;
197     int one = 1;
198     const char *errorhost = "";
199     char *errorstr;
200     char *cp, sig, buf[DES_RW_MAXWRITE];
201     char cmdbuf[NCARGS+1], locuser[16], remuser[16];
202     char remotehost[2 * MaxHostNameLen + 1];
203     uid_t uid;
204     char shell_path[MAXPATHLEN];
205
206     AUTH_DAT    *kdata;
207     KTEXT               ticket;
208     char                instance[INST_SZ], version[VERSION_SIZE];
209     struct              sockaddr_in     fromaddr;
210     int         rc;
211     long                authopts;
212     int         pv1[2], pv2[2];
213     fd_set              wready, writeto;
214
215     fromaddr = *fromp;
216
217     signal(SIGINT, SIG_DFL);
218     signal(SIGQUIT, SIG_DFL);
219     signal(SIGTERM, SIG_DFL);
220 #ifdef DEBUG
221     { int t = open(_PATH_TTY, 2);
222     if (t >= 0) {
223         ioctl(t, TIOCNOTTY, (char *)0);
224         close(t);
225     }
226     }
227 #endif
228     fromp->sin_port = ntohs((u_short)fromp->sin_port);
229     if (fromp->sin_family != AF_INET) {
230         syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
231                fromp->sin_family);
232         exit(1);
233     }
234
235
236     if (!use_kerberos) {
237         ip_options_and_die (0, fromp);
238         if (fromp->sin_port >= IPPORT_RESERVED ||
239             fromp->sin_port < IPPORT_RESERVED/2) {
240             syslog(LOG_NOTICE|LOG_AUTH,
241                    "Connection from %s on illegal port %u",
242                    inet_ntoa(fromp->sin_addr),
243                    fromp->sin_port);
244             exit(1);
245         }
246     }
247
248     alarm(60);
249     port = 0;
250     for (;;) {
251         char c;
252         if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
253             if (cc < 0)
254                 syslog(LOG_NOTICE, "read: %m");
255             shutdown(0, 1+1);
256             exit(1);
257         }
258         if (c== 0)
259             break;
260         port = port * 10 + c - '0';
261     }
262
263     alarm(0);
264     if (port != 0) {
265         int lport = IPPORT_RESERVED - 1;
266         s = rresvport(&lport);
267         if (s < 0) {
268             syslog(LOG_ERR, "can't get stderr port: %m");
269             exit(1);
270         }
271         if (!use_kerberos)
272             if (port >= IPPORT_RESERVED) {
273                 syslog(LOG_ERR, "2nd port not reserved\n");
274                 exit(1);
275             }
276         fromp->sin_port = htons(port);
277         if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
278             syslog(LOG_INFO, "connect second port %d: %m", port);
279             exit(1);
280         }
281     }
282
283     if (vacuous) {
284         error("rshd: Remote host requires Kerberos authentication.\n");
285         exit(1);
286     }
287
288     errorstr = NULL;
289     inaddr2str (fromp->sin_addr, remotehost, sizeof(remotehost));
290
291     if (use_kerberos) {
292         kdata = &authbuf;
293         ticket = &tickbuf;
294         authopts = 0L;
295         k_getsockinst(0, instance, sizeof(instance));
296         version[VERSION_SIZE - 1] = '\0';
297         if (doencrypt) {
298             struct sockaddr_in local_addr;
299             rc = sizeof(local_addr);
300             if (getsockname(0, (struct sockaddr *)&local_addr,
301                             &rc) < 0) {
302                 syslog(LOG_ERR, "getsockname: %m");
303                 error("rshd: getsockname: %m");
304                 exit(1);
305             }
306             authopts = KOPT_DO_MUTUAL;
307             rc = krb_recvauth(authopts, 0, ticket,
308                               "rcmd", instance, &fromaddr,
309                               &local_addr, kdata, "", schedule,
310                               version);
311 #ifndef NOENCRYPTION
312             des_set_key(&kdata->session, schedule);
313 #else
314             memset(schedule, 0, sizeof(schedule));
315 #endif
316         } else
317             rc = krb_recvauth(authopts, 0, ticket, "rcmd",
318                               instance, &fromaddr,
319                               (struct sockaddr_in *) 0,
320                               kdata, "", 0, version);
321         if (rc != KSUCCESS) {
322             error("Kerberos authentication failure: %s\n",
323                   krb_get_err_text(rc));
324             exit(1);
325         }
326     } else
327         xgetstr(remuser, sizeof(remuser), "remuser");
328
329     xgetstr(locuser, sizeof(locuser), "locuser");
330     xgetstr(cmdbuf, sizeof(cmdbuf), "command");
331     setpwent();
332     pwd = k_getpwnam(locuser);
333     if (pwd == NULL) {
334         syslog(LOG_INFO|LOG_AUTH,
335                "%s@%s as %s: unknown login. cmd='%.80s'",
336                remuser, remotehost, locuser, cmdbuf);
337         if (errorstr == NULL)
338             errorstr = "Login incorrect.\n";
339         goto fail;
340     }
341     if (pwd->pw_uid == 0 && strcmp("root", locuser) != 0)
342         {
343             syslog(LOG_ALERT, "NIS attack, user %s has uid 0", locuser);
344             if (errorstr == NULL)
345                 errorstr = "Login incorrect.\n";
346             goto fail;
347         }
348     if (chdir(pwd->pw_dir) < 0) {
349         chdir("/");
350 #ifdef notdef
351         syslog(LOG_INFO|LOG_AUTH,
352                "%s@%s as %s: no home directory. cmd='%.80s'",
353                remuser, remotehost, locuser, cmdbuf);
354         error("No remote directory.\n");
355         exit(1);
356 #endif
357     }
358
359     if (use_kerberos) {
360         if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
361             if (kuserok(kdata, locuser) != 0) {
362                 syslog(LOG_INFO|LOG_AUTH,
363                        "Kerberos rsh denied to %s",
364                        krb_unparse_name_long(kdata->pname, 
365                                              kdata->pinst, 
366                                              kdata->prealm));
367                 error("Permission denied.\n");
368                 exit(1);
369             }
370         }
371     } else
372
373         if (errorstr ||
374             (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
375             iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
376                      remuser, locuser) < 0)) {
377             if (__rcmd_errstr)
378                 syslog(LOG_INFO|LOG_AUTH,
379                        "%s@%s as %s: permission denied (%s). cmd='%.80s'",
380                        remuser, remotehost, locuser,
381                        __rcmd_errstr, cmdbuf);
382             else
383                 syslog(LOG_INFO|LOG_AUTH,
384                        "%s@%s as %s: permission denied. cmd='%.80s'",
385                        remuser, remotehost, locuser, cmdbuf);
386                      fail:
387             if (errorstr == NULL)
388                 errorstr = "Permission denied.\n";
389             error(errorstr, errorhost);
390             exit(1);
391         }
392
393     if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
394         error("Logins currently disabled.\n");
395         exit(1);
396     }
397
398     write(STDERR_FILENO, "\0", 1);
399     sent_null = 1;
400
401     if (port) {
402         if (pipe(pv) < 0) {
403             error("Can't make pipe.\n");
404             exit(1);
405         }
406         if (doencrypt) {
407             if (pipe(pv1) < 0) {
408                 error("Can't make 2nd pipe.\n");
409                 exit(1);
410             }
411             if (pipe(pv2) < 0) {
412                 error("Can't make 3rd pipe.\n");
413                 exit(1);
414             }
415         }
416         pid = fork();
417         if (pid == -1)  {
418             error("Can't fork; try again.\n");
419             exit(1);
420         }
421         if (pid) {
422             if (doencrypt) {
423                 static char msg[] = SECURE_MESSAGE;
424                 close(pv1[1]);
425                 close(pv2[0]);
426 #ifndef NOENCRYPTION
427                 des_enc_write(s, msg, sizeof(msg) - 1, schedule, &kdata->session);
428 #else
429                 write(s, msg, sizeof(msg) - 1);
430 #endif
431             } else {
432                 close(0);
433                 close(1);
434             }
435             close(2);
436             close(pv[1]);
437
438             if (s >= FD_SETSIZE || pv[0] >= FD_SETSIZE) {
439                 error ("fd too large\n");
440                 exit (1);
441             }
442
443             FD_ZERO(&readfrom);
444             FD_SET(s, &readfrom);
445             FD_SET(pv[0], &readfrom);
446             if (pv[0] > s)
447                 nfd = pv[0];
448             else
449                 nfd = s;
450             if (doencrypt) {
451                 if (pv2[1] >= FD_SETSIZE || pv1[0] >= FD_SETSIZE) {
452                     error ("fd too large\n");
453                     exit (1);
454                 }
455
456                 FD_ZERO(&writeto);
457                 FD_SET(pv2[1], &writeto);
458                 FD_SET(pv1[0], &readfrom);
459                 FD_SET(STDIN_FILENO, &readfrom);
460
461                 nfd = max(nfd, pv2[1]);
462                 nfd = max(nfd, pv1[0]);
463             } else
464                 ioctl(pv[0], FIONBIO, (char *)&one);
465
466             /* should set s nbio! */
467             nfd++;
468             do {
469                 ready = readfrom;
470                 if (doencrypt) {
471                     wready = writeto;
472                     if (select(nfd, &ready,
473                                &wready, 0,
474                                (struct timeval *) 0) < 0)
475                         break;
476                 } else
477                     if (select(nfd, &ready, 0,
478                                0, (struct timeval *)0) < 0)
479                         break;
480                 if (FD_ISSET(s, &ready)) {
481                     int ret;
482                     if (doencrypt)
483 #ifndef NOENCRYPTION
484                         ret = des_enc_read(s, &sig, 1, schedule, &kdata->session);
485 #else
486                     ret = read(s, &sig, 1);
487 #endif
488                     else
489                         ret = read(s, &sig, 1);
490                     if (ret <= 0)
491                         FD_CLR(s, &readfrom);
492                     else
493                         kill(-pid, sig);
494                 }
495                 if (FD_ISSET(pv[0], &ready)) {
496                     errno = 0;
497                     cc = read(pv[0], buf, sizeof(buf));
498                     if (cc <= 0) {
499                         shutdown(s, 1+1);
500                         FD_CLR(pv[0], &readfrom);
501                     } else {
502                         if (doencrypt)
503 #ifndef NOENCRYPTION
504                             des_enc_write(s, buf, cc, schedule, &kdata->session);
505 #else
506                         write(s, buf, cc);
507 #endif
508                         else
509                             (void)
510                                 write(s, buf, cc);
511                     }
512                 }
513                 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
514                     errno = 0;
515                     cc = read(pv1[0], buf, sizeof(buf));
516                     if (cc <= 0) {
517                         shutdown(pv1[0], 1+1);
518                         FD_CLR(pv1[0], &readfrom);
519                     } else
520 #ifndef NOENCRYPTION
521                         des_enc_write(STDOUT_FILENO, buf, cc, schedule, &kdata->session);
522 #else
523                     write(STDOUT_FILENO, buf, cc);
524 #endif
525                 }
526
527                 if (doencrypt
528                     && FD_ISSET(STDIN_FILENO, &ready)
529                     && FD_ISSET(pv2[1], &wready)) {
530                     errno = 0;
531 #ifndef NOENCRYPTION
532                     cc = des_enc_read(STDIN_FILENO, buf, sizeof(buf), schedule, &kdata->session);
533 #else
534                     cc = read(STDIN_FILENO, buf, sizeof(buf));
535 #endif
536                     if (cc <= 0) {
537                         shutdown(STDIN_FILENO, 0);
538                         FD_CLR(STDIN_FILENO, &readfrom);
539                         close(pv2[1]);
540                         FD_CLR(pv2[1], &writeto);
541                     } else
542                         write(pv2[1], buf, cc);
543                 }
544
545             } while (FD_ISSET(s, &readfrom) ||
546                      (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
547                      FD_ISSET(pv[0], &readfrom));
548             exit(0);
549         }
550         setsid();
551         close(s);
552         close(pv[0]);
553         if (doencrypt) {
554             close(pv1[0]);
555             close(pv2[1]);
556             dup2(pv1[1], 1);
557             dup2(pv2[0], 0);
558             close(pv1[1]);
559             close(pv2[0]);
560         }
561         dup2(pv[1], 2);
562         close(pv[1]);
563     }
564     if (*pwd->pw_shell == '\0')
565         pwd->pw_shell = _PATH_BSHELL;
566 #ifdef HAVE_SETLOGIN
567     if (setlogin(pwd->pw_name) < 0)
568         syslog(LOG_ERR, "setlogin() failed: %m");
569 #endif
570
571 #ifdef HAVE_SETPCRED
572     if (setpcred (pwd->pw_name, NULL) == -1)
573         syslog(LOG_ERR, "setpcred() failure: %m");
574 #endif /* HAVE_SETPCRED */
575     if(do_osfc2_magic(pwd->pw_uid))
576         exit(1);
577     setgid((gid_t)pwd->pw_gid);
578     initgroups(pwd->pw_name, pwd->pw_gid);
579     setuid((uid_t)pwd->pw_uid);
580     strlcat(homedir, pwd->pw_dir, sizeof(homedir));
581
582     /* Need to prepend path with BINDIR (/usr/athena/bin) to find rcp */
583     snprintf(path, sizeof(path), "PATH=%s:%s", BINDIR, _PATH_DEFPATH);
584
585     strlcat(shell, pwd->pw_shell, sizeof(shell));
586     strlcpy(shell_path, pwd->pw_shell, sizeof(shell_path));
587     strlcat(username, pwd->pw_name, sizeof(username));
588     uid = pwd->pw_uid;
589     cp = strrchr(pwd->pw_shell, '/');
590     if (cp)
591         cp++;
592     else
593         cp = pwd->pw_shell;
594     endpwent();
595     if (log_success || uid == 0) {
596         if (use_kerberos)
597             syslog(LOG_INFO|LOG_AUTH,
598                    "Kerberos shell from %s on %s as %s, cmd='%.80s'",
599                    krb_unparse_name_long(kdata->pname, 
600                                          kdata->pinst, 
601                                          kdata->prealm),
602                    remotehost, locuser, cmdbuf);
603         else
604             syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
605                    remuser, remotehost, locuser, cmdbuf);
606     }
607     if (k_hasafs()) {
608         char cell[64];
609
610         if (new_pag)
611             k_setpag(); /* Put users process in an new pag */
612         if (k_afs_cell_of_file (homedir, cell, sizeof(cell)) == 0)
613             krb_afslog_uid_home (cell, NULL, uid, homedir);
614         krb_afslog_uid_home(NULL, NULL, uid, homedir);
615     }
616     execle(shell_path, cp, "-c", cmdbuf, 0, envinit);
617     err(1, "%s", shell_path);
618 }
619
620 /*
621  * Report error to client.  Note: can't be used until second socket has
622  * connected to client, or older clients will hang waiting for that
623  * connection first.
624  */
625
626 static void
627 error(const char *fmt, ...)
628 {
629     va_list ap;
630     int len;
631     char *bp, buf[BUFSIZ];
632
633     va_start(ap, fmt);
634     bp = buf;
635     if (sent_null == 0) {
636         *bp++ = 1;
637         len = 1;
638     } else
639         len = 0;
640     len += vsnprintf(bp, sizeof(buf) - len, fmt, ap);
641     write(STDERR_FILENO, buf, len);
642     va_end(ap);
643 }
644
645 static void
646 usage()
647 {
648
649     syslog(LOG_ERR,
650            "usage: rshd [-alnkvxLPi] [-p port]");
651     exit(2);
652 }