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 strncpy(luser, cp, sizeof(luser));
326 luser[sizeof(luser) - 1] = '\0';
327 /* XXX truncation! */
329 pam_err = pam_acct_mgmt(pamh, 0);
331 if (pam_err != PAM_SUCCESS) {
332 syslog(LOG_INFO|LOG_AUTH,
333 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
334 ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf);
335 rshd_errx(1, "Login incorrect.");
339 pwd = getpwnam(luser);
341 syslog(LOG_INFO|LOG_AUTH,
342 "%s@%s as %s: unknown login. cmd='%.80s'",
343 ruser, rhost, luser, cmdbuf);
344 if (errorstr == NULL)
345 errorstr = "Login incorrect.";
346 rshd_errx(1, errorstr, rhost);
349 lc = login_getpwclass(pwd);
351 auth_checknologin(lc);
353 if (chdir(pwd->pw_dir) < 0) {
354 if (chdir("/") < 0 ||
355 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
356 syslog(LOG_INFO|LOG_AUTH,
357 "%s@%s as %s: no home directory. cmd='%.80s'",
358 ruser, rhost, luser, cmdbuf);
359 rshd_errx(0, "No remote home directory.");
364 if (lc != NULL && fromp->sa_family == AF_INET) { /*XXX*/
365 char remote_ip[MAXHOSTNAMELEN];
367 strncpy(remote_ip, numericname,
368 sizeof(remote_ip) - 1);
369 remote_ip[sizeof(remote_ip) - 1] = 0;
370 /* XXX truncation! */
371 if (!auth_hostok(lc, rhost, remote_ip)) {
372 syslog(LOG_INFO|LOG_AUTH,
373 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
374 ruser, rhost, luser, __rcmd_errstr,
376 rshd_errx(1, "Login incorrect.");
378 if (!auth_timeok(lc, time(NULL)))
379 rshd_errx(1, "Logins not available right now");
383 * PAM modules might add supplementary groups in
384 * pam_setcred(), so initialize them first.
385 * But we need to open the session as root.
387 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
388 syslog(LOG_ERR, "setusercontext: %m");
392 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
393 syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err));
394 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
395 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
398 (void) write(STDERR_FILENO, "\0", 1);
403 rshd_errx(1, "Can't make pipe.");
406 rshd_errx(1, "Can't fork; try again.");
414 FD_SET(s, &readfrom);
415 FD_SET(pv[0], &readfrom);
420 ioctl(pv[0], FIONBIO, (char *)&one);
422 /* should set s nbio! */
426 if (select(nfd, &ready, (fd_set *)0,
427 (fd_set *)0, (struct timeval *)0) < 0)
429 if (FD_ISSET(s, &ready)) {
431 ret = read(s, &sig, 1);
433 FD_CLR(s, &readfrom);
437 if (FD_ISSET(pv[0], &ready)) {
439 cc = read(pv[0], buf, sizeof(buf));
441 shutdown(s, SHUT_RDWR);
442 FD_CLR(pv[0], &readfrom);
444 (void)write(s, buf, cc);
448 } while (FD_ISSET(s, &readfrom) ||
449 FD_ISSET(pv[0], &readfrom));
461 rshd_errx(1, "Can't fork; try again.");
464 while (wait(NULL) > 0 || errno == EINTR)
471 for (fd = getdtablesize(); fd > 2; fd--)
474 syslog(LOG_ERR, "setsid() failed: %m");
475 if (setlogin(pwd->pw_name) < 0)
476 syslog(LOG_ERR, "setlogin() failed: %m");
478 if (*pwd->pw_shell == '\0')
479 pwd->pw_shell = bshell;
480 (void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
481 (void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
482 (void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
483 (void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
484 environ = pam_getenvlist(pamh);
485 (void) pam_end(pamh, pam_err);
486 cp = strrchr(pwd->pw_shell, '/');
492 if (setusercontext(lc, pwd, pwd->pw_uid,
493 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
494 syslog(LOG_ERR, "setusercontext(): %m");
499 if (log_success || pwd->pw_uid == 0) {
500 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
501 ruser, rhost, luser, cmdbuf);
503 execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL);
504 err(1, "%s", pwd->pw_shell);
509 * Report error to client. Note: can't be used until second socket has
510 * connected to client, or older clients will hang waiting for that
515 rshd_errx(int errcode, const char *fmt, ...)
522 write(STDERR_FILENO, "\1", 1);
524 verrx(errcode, fmt, ap);
529 getstr(char *buf, int cnt, const char *error)
534 if (read(STDIN_FILENO, &c, 1) != 1)
538 rshd_errx(1, "%s too long", error);
543 * Check whether host h is in our local domain,
544 * defined as sharing the last two components of the domain part,
545 * or the entire domain part if the local domain has only one component.
546 * If either name is unqualified (contains no '.'),
547 * assume that the host is local, as it will be
548 * interpreted as such.
551 local_domain(char *h)
553 char localhost[MAXHOSTNAMELEN];
557 (void) gethostname(localhost, sizeof(localhost) - 1);
558 localhost[sizeof(localhost) - 1] = '\0';
559 /* XXX truncation! */
560 p1 = topdomain(localhost);
562 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
570 char *p, *maybe = NULL;
573 for (p = h + strlen(h); p >= h; p--) {
587 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);