2 * Copyright (c) 1988, 1989, 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD$");
53 * remote shell server:
60 #include <sys/param.h>
61 #include <sys/ioctl.h>
63 #include <sys/socket.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in.h>
67 #include <netinet/ip.h>
68 #include <netinet/tcp.h>
69 #include <arpa/inet.h>
85 #include <login_cap.h>
87 #include <security/pam_appl.h>
88 #include <security/openpam.h>
91 static struct pam_conv pamc = { openpam_nullconv, NULL };
92 static pam_handle_t *pamh;
96 if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
97 syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", pam_strerror(pamh, pam_err)); \
98 if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \
99 syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", pam_strerror(pamh, pam_err)); \
100 if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \
101 syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", pam_strerror(pamh, pam_err)); \
105 int log_success; /* If TRUE, log all successful accesses */
109 void doit(struct sockaddr *);
110 static void rshd_errx(int, const char *, ...) __printf0like(2, 3);
111 void getstr(char *, int, const char *);
112 int local_domain(char *);
113 char *topdomain(char *);
117 char bshell[] = _PATH_BSHELL;
119 #define OPTIONS "aDLln"
122 main(int argc, char *argv[])
124 extern int __check_rhosts_file;
125 struct linger linger;
128 struct sockaddr_storage from;
130 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
133 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
136 /* ignored for compatibility */
139 __check_rhosts_file = 0;
159 fromlen = sizeof (from);
160 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
161 syslog(LOG_ERR, "getpeername: %m");
165 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
167 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
169 linger.l_linger = 60; /* XXX */
170 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
171 sizeof (linger)) < 0)
172 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
174 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
175 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
176 doit((struct sockaddr *)&from);
181 extern char **environ;
184 doit(struct sockaddr *fromp)
186 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
189 fd_set ready, readfrom;
190 int cc, fd, nfd, pv[2], pid, s;
192 const char *cp, *errorstr;
193 char sig, buf[BUFSIZ];
194 char *cmdbuf, luser[16], ruser[16];
195 char rhost[2 * MAXHOSTNAMELEN + 1];
196 char numericname[INET6_ADDRSTRLEN];
201 maxcmdlen = (int)sysconf(_SC_ARG_MAX);
202 if (maxcmdlen <= 0 || (cmdbuf = malloc(maxcmdlen)) == NULL)
205 (void) signal(SIGINT, SIG_DFL);
206 (void) signal(SIGQUIT, SIG_DFL);
207 (void) signal(SIGTERM, SIG_DFL);
208 af = fromp->sa_family;
209 srcport = ntohs(*((in_port_t *)&fromp->sa_data));
211 inet_ntop(af, &((struct sockaddr_in *)fromp)->sin_addr,
212 numericname, sizeof numericname);
213 } else if (af == AF_INET6) {
214 inet_ntop(af, &((struct sockaddr_in6 *)fromp)->sin6_addr,
215 numericname, sizeof numericname);
217 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
222 u_char optbuf[BUFSIZ/3];
223 socklen_t optsize = sizeof(optbuf), ipproto, i;
226 if ((ip = getprotobyname("ip")) != NULL)
227 ipproto = ip->p_proto;
229 ipproto = IPPROTO_IP;
230 if (!getsockopt(0, ipproto, IP_OPTIONS, optbuf, &optsize) &&
232 for (i = 0; i < optsize; ) {
233 u_char c = optbuf[i];
234 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
236 "connection refused from %s with IP option %s",
238 c == IPOPT_LSRR ? "LSRR" : "SSRR");
243 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
249 if (srcport >= IPPORT_RESERVED ||
250 srcport < IPPORT_RESERVED/2) {
251 syslog(LOG_NOTICE|LOG_AUTH,
252 "connection from %s on illegal port %u",
260 s = 0; /* not set or used if port == 0 */
263 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
265 syslog(LOG_NOTICE, "read: %m");
266 shutdown(0, SHUT_RDWR);
271 port = port * 10 + c - '0';
276 int lport = IPPORT_RESERVED - 1;
277 s = rresvport_af(&lport, af);
279 syslog(LOG_ERR, "can't get stderr port: %m");
282 if (port >= IPPORT_RESERVED ||
283 port < IPPORT_RESERVED/2) {
284 syslog(LOG_NOTICE|LOG_AUTH,
285 "2nd socket from %s on unreserved port %u",
290 *((in_port_t *)&fromp->sa_data) = htons(port);
291 if (connect(s, fromp, fromp->sa_len) < 0) {
292 syslog(LOG_INFO, "connect second port %d: %m", port);
298 realhostname_sa(rhost, sizeof(rhost) - 1, fromp, fromp->sa_len);
299 rhost[sizeof(rhost) - 1] = '\0';
300 /* XXX truncation! */
303 getstr(ruser, sizeof(ruser), "ruser");
304 getstr(luser, sizeof(luser), "luser");
305 getstr(cmdbuf, maxcmdlen, "command");
308 pam_err = pam_start("rsh", luser, &pamc, &pamh);
309 if (pam_err != PAM_SUCCESS) {
310 syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
311 pam_strerror(pamh, pam_err));
312 rshd_errx(1, "Login incorrect.");
315 if ((pam_err = pam_set_item(pamh, PAM_RUSER, ruser)) != PAM_SUCCESS ||
316 (pam_err = pam_set_item(pamh, PAM_RHOST, rhost)) != PAM_SUCCESS) {
317 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
318 pam_strerror(pamh, pam_err));
319 rshd_errx(1, "Login incorrect.");
322 pam_err = pam_authenticate(pamh, 0);
323 if (pam_err == PAM_SUCCESS) {
324 if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
325 strlcpy(luser, cp, sizeof(luser));
326 /* XXX truncation! */
328 pam_err = pam_acct_mgmt(pamh, 0);
330 if (pam_err != PAM_SUCCESS) {
331 syslog(LOG_INFO|LOG_AUTH,
332 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
333 ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf);
334 rshd_errx(1, "Login incorrect.");
338 pwd = getpwnam(luser);
340 syslog(LOG_INFO|LOG_AUTH,
341 "%s@%s as %s: unknown login. cmd='%.80s'",
342 ruser, rhost, luser, cmdbuf);
343 if (errorstr == NULL)
344 errorstr = "Login incorrect.";
345 rshd_errx(1, errorstr, rhost);
348 lc = login_getpwclass(pwd);
350 auth_checknologin(lc);
352 if (chdir(pwd->pw_dir) < 0) {
353 if (chdir("/") < 0 ||
354 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
355 syslog(LOG_INFO|LOG_AUTH,
356 "%s@%s as %s: no home directory. cmd='%.80s'",
357 ruser, rhost, luser, cmdbuf);
358 rshd_errx(0, "No remote home directory.");
363 if (lc != NULL && fromp->sa_family == AF_INET) { /*XXX*/
364 char remote_ip[MAXHOSTNAMELEN];
366 strlcpy(remote_ip, numericname, sizeof(remote_ip));
367 /* XXX truncation! */
368 if (!auth_hostok(lc, rhost, remote_ip)) {
369 syslog(LOG_INFO|LOG_AUTH,
370 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
371 ruser, rhost, luser, __rcmd_errstr,
373 rshd_errx(1, "Login incorrect.");
375 if (!auth_timeok(lc, time(NULL)))
376 rshd_errx(1, "Logins not available right now");
380 * PAM modules might add supplementary groups in
381 * pam_setcred(), so initialize them first.
382 * But we need to open the session as root.
384 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
385 syslog(LOG_ERR, "setusercontext: %m");
389 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
390 syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err));
391 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
392 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
395 (void) write(STDERR_FILENO, "\0", 1);
400 rshd_errx(1, "Can't make pipe.");
403 rshd_errx(1, "Can't fork; try again.");
411 FD_SET(s, &readfrom);
412 FD_SET(pv[0], &readfrom);
417 ioctl(pv[0], FIONBIO, (char *)&one);
419 /* should set s nbio! */
423 if (select(nfd, &ready, (fd_set *)0,
424 (fd_set *)0, (struct timeval *)0) < 0)
426 if (FD_ISSET(s, &ready)) {
428 ret = read(s, &sig, 1);
430 FD_CLR(s, &readfrom);
434 if (FD_ISSET(pv[0], &ready)) {
436 cc = read(pv[0], buf, sizeof(buf));
438 shutdown(s, SHUT_RDWR);
439 FD_CLR(pv[0], &readfrom);
441 (void)write(s, buf, cc);
445 } while (FD_ISSET(s, &readfrom) ||
446 FD_ISSET(pv[0], &readfrom));
458 rshd_errx(1, "Can't fork; try again.");
461 while (wait(NULL) > 0 || errno == EINTR)
468 for (fd = getdtablesize(); fd > 2; fd--)
471 syslog(LOG_ERR, "setsid() failed: %m");
472 if (setlogin(pwd->pw_name) < 0)
473 syslog(LOG_ERR, "setlogin() failed: %m");
475 if (*pwd->pw_shell == '\0')
476 pwd->pw_shell = bshell;
477 (void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
478 (void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
479 (void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
480 (void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
481 environ = pam_getenvlist(pamh);
482 (void) pam_end(pamh, pam_err);
483 cp = strrchr(pwd->pw_shell, '/');
489 if (setusercontext(lc, pwd, pwd->pw_uid,
490 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
491 syslog(LOG_ERR, "setusercontext(): %m");
496 if (log_success || pwd->pw_uid == 0) {
497 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
498 ruser, rhost, luser, cmdbuf);
500 execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL);
501 err(1, "%s", pwd->pw_shell);
506 * Report error to client. Note: can't be used until second socket has
507 * connected to client, or older clients will hang waiting for that
512 rshd_errx(int errcode, const char *fmt, ...)
519 write(STDERR_FILENO, "\1", 1);
521 verrx(errcode, fmt, ap);
526 getstr(char *buf, int cnt, const char *error)
531 if (read(STDIN_FILENO, &c, 1) != 1)
535 rshd_errx(1, "%s too long", error);
540 * Check whether host h is in our local domain,
541 * defined as sharing the last two components of the domain part,
542 * or the entire domain part if the local domain has only one component.
543 * If either name is unqualified (contains no '.'),
544 * assume that the host is local, as it will be
545 * interpreted as such.
548 local_domain(char *h)
550 char localhost[MAXHOSTNAMELEN];
554 (void) gethostname(localhost, sizeof(localhost) - 1);
555 localhost[sizeof(localhost) - 1] = '\0';
556 /* XXX truncation! */
557 p1 = topdomain(localhost);
559 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
567 char *p, *maybe = NULL;
570 for (p = h + strlen(h); p >= h; p--) {
584 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);