2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 * remote shell server:
45 RCSID("$Id: rshd.c,v 1.60.2.3 2000/10/18 20:39:12 assar Exp $");
47 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
48 extern int __check_rhosts_file;
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;
56 static void doit (struct sockaddr_in *);
57 static void error (const char *, ...)
59 __attribute__ ((format (printf, 1, 2)))
62 static void usage (void);
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"
69 int doencrypt, use_kerberos, vacuous;
70 Key_schedule schedule;
73 main(int argc, char *argv[])
76 int ch, on = 1, fromlen;
77 struct sockaddr_in from;
80 set_progname(argv[0]);
82 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
85 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
90 __check_rhosts_file = 0;
110 portnum = htons(atoi(optarg));
127 if (use_kerberos && vacuous) {
128 syslog(LOG_ERR, "only one of -k and -v allowed");
131 if (doencrypt && !use_kerberos) {
132 syslog(LOG_ERR, "-k is required for -x");
138 portnum = get_shell_port (use_kerberos, doencrypt);
139 mini_inetd (portnum);
142 fromlen = sizeof (from);
143 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
144 syslog(LOG_ERR, "getpeername: %m");
147 #ifdef HAVE_SETSOCKOPT
150 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (void *)&on,
152 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
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");
161 #endif /* HAVE_SETSOCKOPT */
167 char username[20] = "USER=";
168 char homedir[64] = "HOME=";
169 char shell[64] = "SHELL=";
170 char path[100] = "PATH=";
172 {homedir, shell, path, username, 0};
175 xgetstr(char *buf, int cnt, char *err)
180 if (read(STDIN_FILENO, &c, 1) != 1)
184 error("%s too long\n", err);
191 doit(struct sockaddr_in *fromp)
195 fd_set ready, readfrom;
196 int cc, nfd, pv[2], pid, s;
198 const char *errorhost = "";
200 char *cp, sig, buf[DES_RW_MAXWRITE];
201 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
202 char remotehost[2 * MaxHostNameLen + 1];
204 char shell_path[MAXPATHLEN];
208 char instance[INST_SZ], version[VERSION_SIZE];
209 struct sockaddr_in fromaddr;
213 fd_set wready, writeto;
217 signal(SIGINT, SIG_DFL);
218 signal(SIGQUIT, SIG_DFL);
219 signal(SIGTERM, SIG_DFL);
221 { int t = open(_PATH_TTY, 2);
223 ioctl(t, TIOCNOTTY, (char *)0);
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",
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),
252 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
254 syslog(LOG_NOTICE, "read: %m");
260 port = port * 10 + c - '0';
265 int lport = IPPORT_RESERVED - 1;
266 s = rresvport(&lport);
268 syslog(LOG_ERR, "can't get stderr port: %m");
272 if (port >= IPPORT_RESERVED) {
273 syslog(LOG_ERR, "2nd port not reserved\n");
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);
284 error("rshd: Remote host requires Kerberos authentication.\n");
289 inaddr2str (fromp->sin_addr, remotehost, sizeof(remotehost));
295 k_getsockinst(0, instance, sizeof(instance));
296 version[VERSION_SIZE - 1] = '\0';
298 struct sockaddr_in local_addr;
299 rc = sizeof(local_addr);
300 if (getsockname(0, (struct sockaddr *)&local_addr,
302 syslog(LOG_ERR, "getsockname: %m");
303 error("rshd: getsockname: %m");
306 authopts = KOPT_DO_MUTUAL;
307 rc = krb_recvauth(authopts, 0, ticket,
308 "rcmd", instance, &fromaddr,
309 &local_addr, kdata, "", schedule,
312 des_set_key(&kdata->session, schedule);
314 memset(schedule, 0, sizeof(schedule));
317 rc = krb_recvauth(authopts, 0, ticket, "rcmd",
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));
327 xgetstr(remuser, sizeof(remuser), "remuser");
329 xgetstr(locuser, sizeof(locuser), "locuser");
330 xgetstr(cmdbuf, sizeof(cmdbuf), "command");
332 pwd = k_getpwnam(locuser);
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";
341 if (pwd->pw_uid == 0 && strcmp("root", locuser) != 0)
343 syslog(LOG_ALERT, "NIS attack, user %s has uid 0", locuser);
344 if (errorstr == NULL)
345 errorstr = "Login incorrect.\n";
348 if (chdir(pwd->pw_dir) < 0) {
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");
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,
367 error("Permission denied.\n");
374 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
375 iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
376 remuser, locuser) < 0)) {
378 syslog(LOG_INFO|LOG_AUTH,
379 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
380 remuser, remotehost, locuser,
381 __rcmd_errstr, cmdbuf);
383 syslog(LOG_INFO|LOG_AUTH,
384 "%s@%s as %s: permission denied. cmd='%.80s'",
385 remuser, remotehost, locuser, cmdbuf);
387 if (errorstr == NULL)
388 errorstr = "Permission denied.\n";
389 error(errorstr, errorhost);
393 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
394 error("Logins currently disabled.\n");
398 write(STDERR_FILENO, "\0", 1);
403 error("Can't make pipe.\n");
408 error("Can't make 2nd pipe.\n");
412 error("Can't make 3rd pipe.\n");
418 error("Can't fork; try again.\n");
423 static char msg[] = SECURE_MESSAGE;
427 des_enc_write(s, msg, sizeof(msg) - 1, schedule, &kdata->session);
429 write(s, msg, sizeof(msg) - 1);
438 if (s >= FD_SETSIZE || pv[0] >= FD_SETSIZE) {
439 error ("fd too large\n");
444 FD_SET(s, &readfrom);
445 FD_SET(pv[0], &readfrom);
451 if (pv2[1] >= FD_SETSIZE || pv1[0] >= FD_SETSIZE) {
452 error ("fd too large\n");
457 FD_SET(pv2[1], &writeto);
458 FD_SET(pv1[0], &readfrom);
459 FD_SET(STDIN_FILENO, &readfrom);
461 nfd = max(nfd, pv2[1]);
462 nfd = max(nfd, pv1[0]);
464 ioctl(pv[0], FIONBIO, (char *)&one);
466 /* should set s nbio! */
472 if (select(nfd, &ready,
474 (struct timeval *) 0) < 0)
477 if (select(nfd, &ready, 0,
478 0, (struct timeval *)0) < 0)
480 if (FD_ISSET(s, &ready)) {
484 ret = des_enc_read(s, &sig, 1, schedule, &kdata->session);
486 ret = read(s, &sig, 1);
489 ret = read(s, &sig, 1);
491 FD_CLR(s, &readfrom);
495 if (FD_ISSET(pv[0], &ready)) {
497 cc = read(pv[0], buf, sizeof(buf));
500 FD_CLR(pv[0], &readfrom);
504 des_enc_write(s, buf, cc, schedule, &kdata->session);
513 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
515 cc = read(pv1[0], buf, sizeof(buf));
517 shutdown(pv1[0], 1+1);
518 FD_CLR(pv1[0], &readfrom);
521 des_enc_write(STDOUT_FILENO, buf, cc, schedule, &kdata->session);
523 write(STDOUT_FILENO, buf, cc);
528 && FD_ISSET(STDIN_FILENO, &ready)
529 && FD_ISSET(pv2[1], &wready)) {
532 cc = des_enc_read(STDIN_FILENO, buf, sizeof(buf), schedule, &kdata->session);
534 cc = read(STDIN_FILENO, buf, sizeof(buf));
537 shutdown(STDIN_FILENO, 0);
538 FD_CLR(STDIN_FILENO, &readfrom);
540 FD_CLR(pv2[1], &writeto);
542 write(pv2[1], buf, cc);
545 } while (FD_ISSET(s, &readfrom) ||
546 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
547 FD_ISSET(pv[0], &readfrom));
564 if (*pwd->pw_shell == '\0')
565 pwd->pw_shell = _PATH_BSHELL;
567 if (setlogin(pwd->pw_name) < 0)
568 syslog(LOG_ERR, "setlogin() failed: %m");
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))
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));
582 /* Need to prepend path with BINDIR (/usr/athena/bin) to find rcp */
583 snprintf(path, sizeof(path), "PATH=%s:%s", BINDIR, _PATH_DEFPATH);
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));
589 cp = strrchr(pwd->pw_shell, '/');
595 if (log_success || uid == 0) {
597 syslog(LOG_INFO|LOG_AUTH,
598 "Kerberos shell from %s on %s as %s, cmd='%.80s'",
599 krb_unparse_name_long(kdata->pname,
602 remotehost, locuser, cmdbuf);
604 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
605 remuser, remotehost, locuser, cmdbuf);
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);
616 execle(shell_path, cp, "-c", cmdbuf, 0, envinit);
617 err(1, "%s", shell_path);
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
627 error(const char *fmt, ...)
631 char *bp, buf[BUFSIZ];
635 if (sent_null == 0) {
640 len += vsnprintf(bp, sizeof(buf) - len, fmt, ap);
641 write(STDERR_FILENO, buf, len);
650 "usage: rshd [-alnkvxLPi] [-p port]");