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
33 * @(#) Copyright (c) 1988, 1989, 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)rshd.c 8.2 (Berkeley) 4/6/94
35 * $FreeBSD: src/libexec/rshd/rshd.c,v 1.30.2.5 2002/05/14 22:27:21 des Exp $
36 * $DragonFly: src/libexec/rshd/rshd.c,v 1.3 2003/11/14 03:54:31 dillon Exp $
40 * remote shell server:
47 #include <sys/param.h>
48 #include <sys/ioctl.h>
50 #include <sys/socket.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/tcp.h>
56 #include <arpa/inet.h>
71 #include <login_cap.h>
74 /* wrapper for KAME-special getnameinfo() */
75 #ifndef NI_WITHSCOPEID
76 #define NI_WITHSCOPEID 0
80 int log_success; /* If TRUE, log all successful accesses */
93 struct sockaddr_in su_sin;
94 struct sockaddr_in6 su_sin6;
96 #define su_len su_si.si_len
97 #define su_family su_si.si_family
98 #define su_port su_si.si_port
100 void doit (union sockunion *);
101 void error (const char *, ...);
102 void getstr (char *, int, char *);
103 int local_domain (char *);
104 char *topdomain (char *);
107 #define OPTIONS "alnDL"
114 extern int __check_rhosts_file;
115 struct linger linger;
116 int ch, on = 1, fromlen;
117 struct sockaddr_storage from;
119 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
122 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
125 /* ignored for compatibility */
128 __check_rhosts_file = 0;
155 syslog(LOG_ERR, "-k is required for -x");
160 fromlen = sizeof (from);
161 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
162 syslog(LOG_ERR, "getpeername: %m");
166 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
168 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
170 linger.l_linger = 60; /* XXX */
171 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
172 sizeof (linger)) < 0)
173 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
175 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
176 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
177 doit((union sockunion *)&from);
182 char username[20] = "USER=";
183 char homedir[64] = "HOME=";
184 char shell[64] = "SHELL=";
185 char path[100] = "PATH=";
187 {homedir, shell, path, username, 0};
192 union sockunion *fromp;
194 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
197 fd_set ready, readfrom;
198 int cc, nfd, pv[2], pid, s;
201 char *cp, sig, buf[BUFSIZ];
202 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
203 char fromhost[2 * MAXHOSTNAMELEN + 1];
204 char numericname[INET6_ADDRSTRLEN];
205 int af = fromp->su_family, err;
214 (void) signal(SIGINT, SIG_DFL);
215 (void) signal(SIGQUIT, SIG_DFL);
216 (void) signal(SIGTERM, SIG_DFL);
217 fromp->su_port = ntohs((u_short)fromp->su_port);
223 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
226 err = getnameinfo((struct sockaddr *)fromp, fromp->su_len, numericname,
227 sizeof(numericname), NULL, 0,
228 NI_NUMERICHOST|NI_WITHSCOPEID);
229 /* XXX: do 'err' check */
232 u_char optbuf[BUFSIZ/3];
233 int optsize = sizeof(optbuf), ipproto, i;
236 if ((ip = getprotobyname("ip")) != NULL)
237 ipproto = ip->p_proto;
239 ipproto = IPPROTO_IP;
240 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
242 for (i = 0; i < optsize; ) {
243 u_char c = optbuf[i];
244 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
246 "connection refused from %s with IP option %s",
248 c == IPOPT_LSRR ? "LSRR" : "SSRR");
253 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
259 if (fromp->su_port >= IPPORT_RESERVED ||
260 fromp->su_port < IPPORT_RESERVED/2) {
261 syslog(LOG_NOTICE|LOG_AUTH,
262 "connection from %s on illegal port %u",
270 s = 0; /* not set or used if port == 0 */
273 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
275 syslog(LOG_NOTICE, "read: %m");
281 port = port * 10 + c - '0';
286 int lport = IPPORT_RESERVED - 1;
287 s = rresvport_af(&lport, af);
289 syslog(LOG_ERR, "can't get stderr port: %m");
292 if (port >= IPPORT_RESERVED ||
293 port < IPPORT_RESERVED/2) {
294 syslog(LOG_NOTICE|LOG_AUTH,
295 "2nd socket from %s on unreserved port %u",
300 fromp->su_port = htons(port);
301 if (connect(s, (struct sockaddr *)fromp, fromp->su_len) < 0) {
302 syslog(LOG_INFO, "connect second port %d: %m", port);
308 realhostname_sa(fromhost, sizeof(fromhost) - 1,
309 (struct sockaddr *)fromp,
311 fromhost[sizeof(fromhost) - 1] = '\0';
314 if (doencrypt && af == AF_INET) {
315 struct sockaddr_in local_addr;
316 rc = sizeof(local_addr);
317 if (getsockname(0, (struct sockaddr *)&local_addr,
319 syslog(LOG_ERR, "getsockname: %m");
320 error("rlogind: getsockname: %m");
323 authopts = KOPT_DO_MUTUAL;
324 rc = krb_recvauth(authopts, 0, ticket,
325 "rcmd", instance, &fromaddr,
326 &local_addr, kdata, "", schedule,
328 des_set_key(&kdata->session, schedule);
332 getstr(remuser, sizeof(remuser), "remuser");
333 getstr(locuser, sizeof(locuser), "locuser");
334 getstr(cmdbuf, sizeof(cmdbuf), "command");
337 pwd = getpwnam(locuser);
339 syslog(LOG_INFO|LOG_AUTH,
340 "%s@%s as %s: unknown login. cmd='%.80s'",
341 remuser, fromhost, locuser, cmdbuf);
342 if (errorstr == NULL)
343 errorstr = "Login incorrect.\n";
347 lc = login_getpwclass(pwd);
349 if (chdir(pwd->pw_dir) < 0) {
351 if (chdir("/") < 0 ||
352 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
353 syslog(LOG_INFO|LOG_AUTH,
354 "%s@%s as %s: no home directory. cmd='%.80s'",
355 remuser, fromhost, locuser, cmdbuf);
356 error("No remote home directory.\n");
362 syslog(LOG_INFO|LOG_AUTH,
363 "%s@%s as %s: no home directory. cmd='%.80s'",
364 remuser, fromhost, locuser, cmdbuf);
365 error("No remote directory.\n");
373 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
374 iruserok_sa(fromp, fromp->su_len, pwd->pw_uid == 0,
375 remuser, locuser) < 0) {
377 syslog(LOG_INFO|LOG_AUTH,
378 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
379 remuser, fromhost, locuser, __rcmd_errstr,
382 syslog(LOG_INFO|LOG_AUTH,
383 "%s@%s as %s: permission denied. cmd='%.80s'",
384 remuser, fromhost, locuser, cmdbuf);
386 if (errorstr == NULL)
387 errorstr = "Login incorrect.\n";
388 error(errorstr, fromhost);
392 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
393 error("Logins currently disabled.\n");
397 if (lc != NULL && fromp->su_family == AF_INET) { /*XXX*/
398 char remote_ip[MAXHOSTNAMELEN];
400 strncpy(remote_ip, numericname,
401 sizeof(remote_ip) - 1);
402 remote_ip[sizeof(remote_ip) - 1] = 0;
403 if (!auth_hostok(lc, fromhost, remote_ip)) {
404 syslog(LOG_INFO|LOG_AUTH,
405 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
406 remuser, fromhost, locuser, __rcmd_errstr,
408 error("Login incorrect.\n");
411 if (!auth_timeok(lc, time(NULL))) {
412 error("Logins not available right now\n");
416 #endif /* !LOGIN_CAP */
418 /* before fork, while we're session leader */
419 if (setlogin(pwd->pw_name) < 0)
420 syslog(LOG_ERR, "setlogin() failed: %m");
423 (void) write(STDERR_FILENO, "\0", 1);
428 error("Can't make pipe.\n");
434 error("Can't make 2nd pipe.\n");
438 error("Can't make 3rd pipe.\n");
445 error("Can't fork; try again.\n");
451 static char msg[] = SECURE_MESSAGE;
452 (void) close(pv1[1]);
453 (void) close(pv2[1]);
454 des_enc_write(s, msg, sizeof(msg) - 1,
455 schedule, &kdata->session);
467 FD_SET(s, &readfrom);
468 FD_SET(pv[0], &readfrom);
476 FD_SET(pv2[0], &writeto);
477 FD_SET(pv1[0], &readfrom);
479 nfd = MAX(nfd, pv2[0]);
480 nfd = MAX(nfd, pv1[0]);
483 ioctl(pv[0], FIONBIO, (char *)&one);
485 /* should set s nbio! */
492 if (select(nfd, &ready,
493 &wready, (fd_set *) 0,
494 (struct timeval *) 0) < 0)
498 if (select(nfd, &ready, (fd_set *)0,
499 (fd_set *)0, (struct timeval *)0) < 0)
501 if (FD_ISSET(s, &ready)) {
505 ret = des_enc_read(s, &sig, 1,
506 schedule, &kdata->session);
509 ret = read(s, &sig, 1);
511 FD_CLR(s, &readfrom);
515 if (FD_ISSET(pv[0], &ready)) {
517 cc = read(pv[0], buf, sizeof(buf));
520 FD_CLR(pv[0], &readfrom);
525 des_enc_write(s, buf, cc,
526 schedule, &kdata->session);
534 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
536 cc = read(pv1[0], buf, sizeof(buf));
538 shutdown(pv1[0], 1+1);
539 FD_CLR(pv1[0], &readfrom);
541 (void) des_enc_write(STDOUT_FILENO,
543 schedule, &kdata->session);
546 if (doencrypt && FD_ISSET(pv2[0], &wready)) {
548 cc = des_enc_read(STDIN_FILENO,
550 schedule, &kdata->session);
552 shutdown(pv2[0], 1+1);
553 FD_CLR(pv2[0], &writeto);
555 (void) write(pv2[0], buf, cc);
559 } while (FD_ISSET(s, &readfrom) ||
561 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
563 FD_ISSET(pv[0], &readfrom));
566 setpgrp(0, getpid());
571 close(pv1[0]); close(pv2[0]);
581 if (*pwd->pw_shell == '\0')
582 pwd->pw_shell = _PATH_BSHELL;
584 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
585 strcat(path, _PATH_DEFPATH);
586 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
587 strncat(username, pwd->pw_name, sizeof(username)-6);
588 cp = strrchr(pwd->pw_shell, '/');
594 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
595 syslog(LOG_ERR, "setusercontext: %m");
600 (void) setgid((gid_t)pwd->pw_gid);
601 initgroups(pwd->pw_name, pwd->pw_gid);
602 (void) setuid((uid_t)pwd->pw_uid);
605 if (log_success || pwd->pw_uid == 0) {
606 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
607 remuser, fromhost, locuser, cmdbuf);
609 execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
610 perror(pwd->pw_shell);
615 * Report error to client. Note: can't be used until second socket has
616 * connected to client, or older clients will hang waiting for that
627 error(const char *fmt, ...)
636 char *bp, buf[BUFSIZ];
643 if (sent_null == 0) {
648 (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
649 (void)write(STDERR_FILENO, buf, len + strlen(bp));
653 getstr(buf, cnt, err)
660 if (read(STDIN_FILENO, &c, 1) != 1)
664 error("%s too long\n", err);
671 * Check whether host h is in our local domain,
672 * defined as sharing the last two components of the domain part,
673 * or the entire domain part if the local domain has only one component.
674 * If either name is unqualified (contains no '.'),
675 * assume that the host is local, as it will be
676 * interpreted as such.
682 char localhost[MAXHOSTNAMELEN];
686 (void) gethostname(localhost, sizeof(localhost) - 1);
687 localhost[sizeof(localhost) - 1] = '\0';
688 p1 = topdomain(localhost);
690 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
699 char *p, *maybe = NULL;
702 for (p = h + strlen(h); p >= h; p--) {
716 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);