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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char copyright[] =
43 "@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
44 The Regents of the University of California. All rights reserved.\n";
49 static const char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
57 * remote shell server:
64 #include <sys/param.h>
65 #include <sys/ioctl.h>
67 #include <sys/socket.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in.h>
71 #include <netinet/ip.h>
72 #include <netinet/tcp.h>
73 #include <arpa/inet.h>
89 #include <login_cap.h>
91 #include <security/pam_appl.h>
92 #include <security/openpam.h>
95 static struct pam_conv pamc = { openpam_nullconv, NULL };
96 static pam_handle_t *pamh;
100 if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
101 syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", pam_strerror(pamh, pam_err)); \
102 if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \
103 syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", pam_strerror(pamh, pam_err)); \
104 if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \
105 syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", pam_strerror(pamh, pam_err)); \
109 int log_success; /* If TRUE, log all successful accesses */
113 void doit(struct sockaddr *);
114 static void rshd_errx(int, const char *, ...) __printf0like(2, 3);
115 void getstr(char *, int, const char *);
116 int local_domain(char *);
117 char *topdomain(char *);
121 char bshell[] = _PATH_BSHELL;
123 #define OPTIONS "alnDL"
126 main(int argc, char *argv[])
128 extern int __check_rhosts_file;
129 struct linger linger;
130 int ch, on = 1, fromlen;
131 struct sockaddr_storage from;
133 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
136 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
139 /* ignored for compatibility */
142 __check_rhosts_file = 0;
162 fromlen = sizeof (from);
163 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
164 syslog(LOG_ERR, "getpeername: %m");
168 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
170 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
172 linger.l_linger = 60; /* XXX */
173 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
174 sizeof (linger)) < 0)
175 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
177 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
178 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
179 doit((struct sockaddr *)&from);
184 extern char **environ;
187 doit(struct sockaddr *fromp)
189 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
192 fd_set ready, readfrom;
193 int cc, fd, nfd, pv[2], pid, s;
195 const char *cp, *errorstr;
196 char sig, buf[BUFSIZ];
197 char cmdbuf[NCARGS+1], luser[16], ruser[16];
198 char rhost[2 * MAXHOSTNAMELEN + 1];
199 char numericname[INET6_ADDRSTRLEN];
203 (void) signal(SIGINT, SIG_DFL);
204 (void) signal(SIGQUIT, SIG_DFL);
205 (void) signal(SIGTERM, SIG_DFL);
206 af = fromp->sa_family;
207 srcport = ntohs(*((in_port_t *)&fromp->sa_data));
209 inet_ntop(af, &((struct sockaddr_in *)fromp)->sin_addr,
210 numericname, sizeof numericname);
211 } else if (af == AF_INET6) {
212 inet_ntop(af, &((struct sockaddr_in6 *)fromp)->sin6_addr,
213 numericname, sizeof numericname);
215 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
220 u_char optbuf[BUFSIZ/3];
221 int optsize = sizeof(optbuf), ipproto, i;
224 if ((ip = getprotobyname("ip")) != NULL)
225 ipproto = ip->p_proto;
227 ipproto = IPPROTO_IP;
228 if (!getsockopt(0, ipproto, IP_OPTIONS, optbuf, &optsize) &&
230 for (i = 0; i < optsize; ) {
231 u_char c = optbuf[i];
232 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
234 "connection refused from %s with IP option %s",
236 c == IPOPT_LSRR ? "LSRR" : "SSRR");
241 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
247 if (srcport >= IPPORT_RESERVED ||
248 srcport < IPPORT_RESERVED/2) {
249 syslog(LOG_NOTICE|LOG_AUTH,
250 "connection from %s on illegal port %u",
258 s = 0; /* not set or used if port == 0 */
261 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
263 syslog(LOG_NOTICE, "read: %m");
269 port = port * 10 + c - '0';
274 int lport = IPPORT_RESERVED - 1;
275 s = rresvport_af(&lport, af);
277 syslog(LOG_ERR, "can't get stderr port: %m");
280 if (port >= IPPORT_RESERVED ||
281 port < IPPORT_RESERVED/2) {
282 syslog(LOG_NOTICE|LOG_AUTH,
283 "2nd socket from %s on unreserved port %u",
288 *((in_port_t *)&fromp->sa_data) = htons(port);
289 if (connect(s, fromp, fromp->sa_len) < 0) {
290 syslog(LOG_INFO, "connect second port %d: %m", port);
296 realhostname_sa(rhost, sizeof(rhost) - 1, fromp, fromp->sa_len);
297 rhost[sizeof(rhost) - 1] = '\0';
298 /* XXX truncation! */
301 getstr(ruser, sizeof(ruser), "ruser");
302 getstr(luser, sizeof(luser), "luser");
303 getstr(cmdbuf, sizeof(cmdbuf), "command");
306 pam_err = pam_start("rsh", luser, &pamc, &pamh);
307 if (pam_err != PAM_SUCCESS) {
308 syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
309 pam_strerror(pamh, pam_err));
310 rshd_errx(1, "Login incorrect.");
313 if ((pam_err = pam_set_item(pamh, PAM_RUSER, ruser)) != PAM_SUCCESS ||
314 (pam_err = pam_set_item(pamh, PAM_RHOST, rhost) != PAM_SUCCESS)) {
315 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
316 pam_strerror(pamh, pam_err));
317 rshd_errx(1, "Login incorrect.");
320 pam_err = pam_authenticate(pamh, 0);
321 if (pam_err == PAM_SUCCESS) {
322 if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
323 strncpy(luser, cp, sizeof(luser));
324 luser[sizeof(luser) - 1] = '\0';
325 /* XXX truncation! */
327 pam_err = pam_acct_mgmt(pamh, 0);
329 if (pam_err != PAM_SUCCESS) {
330 syslog(LOG_INFO|LOG_AUTH,
331 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
332 ruser, rhost, luser, pam_strerror(pamh, pam_err), cmdbuf);
333 rshd_errx(1, "Login incorrect.");
337 pwd = getpwnam(luser);
339 syslog(LOG_INFO|LOG_AUTH,
340 "%s@%s as %s: unknown login. cmd='%.80s'",
341 ruser, rhost, luser, cmdbuf);
342 if (errorstr == NULL)
343 errorstr = "Login incorrect.";
344 rshd_errx(1, errorstr, rhost);
347 lc = login_getpwclass(pwd);
349 auth_checknologin(lc);
351 if (chdir(pwd->pw_dir) < 0) {
352 if (chdir("/") < 0 ||
353 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
354 syslog(LOG_INFO|LOG_AUTH,
355 "%s@%s as %s: no home directory. cmd='%.80s'",
356 ruser, rhost, luser, cmdbuf);
357 rshd_errx(0, "No remote home directory.");
362 if (lc != NULL && fromp->sa_family == AF_INET) { /*XXX*/
363 char remote_ip[MAXHOSTNAMELEN];
365 strncpy(remote_ip, numericname,
366 sizeof(remote_ip) - 1);
367 remote_ip[sizeof(remote_ip) - 1] = 0;
368 /* XXX truncation! */
369 if (!auth_hostok(lc, rhost, remote_ip)) {
370 syslog(LOG_INFO|LOG_AUTH,
371 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
372 ruser, rhost, luser, __rcmd_errstr,
374 rshd_errx(1, "Login incorrect.");
376 if (!auth_timeok(lc, time(NULL)))
377 rshd_errx(1, "Logins not available right now");
381 * PAM modules might add supplementary groups in
382 * pam_setcred(), so initialize them first.
383 * But we need to open the session as root.
385 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
386 syslog(LOG_ERR, "setusercontext: %m");
390 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
391 syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, pam_err));
392 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
393 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
396 (void) write(STDERR_FILENO, "\0", 1);
401 rshd_errx(1, "Can't make pipe.");
404 rshd_errx(1, "Can't fork; try again.");
412 FD_SET(s, &readfrom);
413 FD_SET(pv[0], &readfrom);
418 ioctl(pv[0], FIONBIO, (char *)&one);
420 /* should set s nbio! */
424 if (select(nfd, &ready, (fd_set *)0,
425 (fd_set *)0, (struct timeval *)0) < 0)
427 if (FD_ISSET(s, &ready)) {
429 ret = read(s, &sig, 1);
431 FD_CLR(s, &readfrom);
435 if (FD_ISSET(pv[0], &ready)) {
437 cc = read(pv[0], buf, sizeof(buf));
440 FD_CLR(pv[0], &readfrom);
442 (void)write(s, buf, cc);
446 } while (FD_ISSET(s, &readfrom) ||
447 FD_ISSET(pv[0], &readfrom));
459 rshd_errx(1, "Can't fork; try again.");
462 while (wait(NULL) > 0 || errno == EINTR)
469 for (fd = getdtablesize(); fd > 2; fd--)
472 syslog(LOG_ERR, "setsid() failed: %m");
473 if (setlogin(pwd->pw_name) < 0)
474 syslog(LOG_ERR, "setlogin() failed: %m");
476 if (*pwd->pw_shell == '\0')
477 pwd->pw_shell = bshell;
478 (void) pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
479 (void) pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
480 (void) pam_setenv(pamh, "USER", pwd->pw_name, 1);
481 (void) pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
482 environ = pam_getenvlist(pamh);
483 (void) pam_end(pamh, pam_err);
484 cp = strrchr(pwd->pw_shell, '/');
490 if (setusercontext(lc, pwd, pwd->pw_uid,
491 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
492 syslog(LOG_ERR, "setusercontext(): %m");
497 if (log_success || pwd->pw_uid == 0) {
498 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
499 ruser, rhost, luser, cmdbuf);
501 execl(pwd->pw_shell, cp, "-c", cmdbuf, (char *)NULL);
502 err(1, "%s", pwd->pw_shell);
507 * Report error to client. Note: can't be used until second socket has
508 * connected to client, or older clients will hang waiting for that
513 rshd_errx(int errcode, const char *fmt, ...)
520 write(STDERR_FILENO, "\1", 1);
522 verrx(errcode, fmt, ap);
527 getstr(char *buf, int cnt, const char *error)
532 if (read(STDIN_FILENO, &c, 1) != 1)
536 rshd_errx(1, "%s too long", error);
541 * Check whether host h is in our local domain,
542 * defined as sharing the last two components of the domain part,
543 * or the entire domain part if the local domain has only one component.
544 * If either name is unqualified (contains no '.'),
545 * assume that the host is local, as it will be
546 * interpreted as such.
549 local_domain(char *h)
551 char localhost[MAXHOSTNAMELEN];
555 (void) gethostname(localhost, sizeof(localhost) - 1);
556 localhost[sizeof(localhost) - 1] = '\0';
557 /* XXX truncation! */
558 p1 = topdomain(localhost);
560 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
568 char *p, *maybe = NULL;
571 for (p = h + strlen(h); p >= h; p--) {
585 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);