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 $
39 * remote shell server:
46 #include <sys/param.h>
47 #include <sys/ioctl.h>
49 #include <sys/socket.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
53 #include <netinet/ip.h>
54 #include <netinet/tcp.h>
55 #include <arpa/inet.h>
71 #include <login_cap.h>
74 /* wrapper for KAME-special getnameinfo() */
75 #ifndef NI_WITHSCOPEID
76 #define NI_WITHSCOPEID 0
79 extern int __check_rhosts_file;
80 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
83 int log_success; /* If TRUE, log all successful accesses */
96 struct sockaddr_in su_sin;
97 struct sockaddr_in6 su_sin6;
99 #define su_len su_si.si_len
100 #define su_family su_si.si_family
101 #define su_port su_si.si_port
103 void doit(union sockunion *);
104 void error(const char *, ...) __printflike(1, 2);
105 void getstr(char *, int, const char *);
106 int local_domain(char *);
107 char *topdomain(char *);
111 char bshell[] = _PATH_BSHELL;
113 #define OPTIONS "alnDL"
116 main(int argc, char *argv[])
118 struct linger linger;
121 struct sockaddr_storage from;
123 openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
126 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
129 /* ignored for compatibility */
132 __check_rhosts_file = 0;
159 syslog(LOG_ERR, "-k is required for -x");
164 fromlen = sizeof (from);
165 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
166 syslog(LOG_ERR, "getpeername: %m");
170 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
172 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
174 linger.l_linger = 60; /* XXX */
175 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
176 sizeof (linger)) < 0)
177 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
179 setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
180 syslog(LOG_WARNING, "setsockopt (TCP_NODELAY): %m");
181 doit((union sockunion *)&from);
186 char username[20] = "USER=";
187 char homedir[64] = "HOME=";
188 char shell[64] = "SHELL=";
189 char path[100] = "PATH=";
191 {homedir, shell, path, username, 0};
195 doit(union sockunion *fromp)
199 fd_set ready, readfrom;
200 int cc, nfd, pv[2], pid, s;
202 const char *errorstr;
203 char *cp, sig, buf[BUFSIZ];
204 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
205 char fromhost[2 * MAXHOSTNAMELEN + 1];
206 char numericname[INET6_ADDRSTRLEN];
207 int af = fromp->su_family, gaierror;
216 signal(SIGINT, SIG_DFL);
217 signal(SIGQUIT, SIG_DFL);
218 signal(SIGTERM, SIG_DFL);
219 fromp->su_port = ntohs((u_short)fromp->su_port);
225 syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
228 gaierror = getnameinfo((struct sockaddr *)fromp, fromp->su_len,
229 numericname, sizeof(numericname), NULL, 0,
230 NI_NUMERICHOST|NI_WITHSCOPEID);
232 syslog(LOG_ERR, "malformed \"from\" address (af %d): %s", af,
233 gai_strerror(gaierror));
238 u_char optbuf[BUFSIZ/3];
239 socklen_t optsize = sizeof(optbuf), i;
243 if ((ip = getprotobyname("ip")) != NULL)
244 ipproto = ip->p_proto;
246 ipproto = IPPROTO_IP;
247 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
249 for (i = 0; i < optsize; ) {
250 u_char c = optbuf[i];
251 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
253 "connection refused from %s with IP option %s",
255 c == IPOPT_LSRR ? "LSRR" : "SSRR");
260 i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
266 if (fromp->su_port >= IPPORT_RESERVED ||
267 fromp->su_port < IPPORT_RESERVED/2) {
268 syslog(LOG_NOTICE|LOG_AUTH,
269 "connection from %s on illegal port %u",
277 s = 0; /* not set or used if port == 0 */
280 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
282 syslog(LOG_NOTICE, "read: %m");
283 shutdown(0, SHUT_RDWR);
288 port = port * 10 + c - '0';
293 int lport = IPPORT_RESERVED - 1;
294 s = rresvport_af(&lport, af);
296 syslog(LOG_ERR, "can't get stderr port: %m");
299 if (port >= IPPORT_RESERVED ||
300 port < IPPORT_RESERVED/2) {
301 syslog(LOG_NOTICE|LOG_AUTH,
302 "2nd socket from %s on unreserved port %u",
307 fromp->su_port = htons(port);
308 if (connect(s, (struct sockaddr *)fromp, fromp->su_len) < 0) {
309 syslog(LOG_INFO, "connect second port %d: %m", port);
315 realhostname_sa(fromhost, sizeof(fromhost) - 1,
316 (struct sockaddr *)fromp,
318 fromhost[sizeof(fromhost) - 1] = '\0';
321 if (doencrypt && af == AF_INET) {
322 struct sockaddr_in local_addr;
323 rc = sizeof(local_addr);
324 if (getsockname(0, (struct sockaddr *)&local_addr,
326 syslog(LOG_ERR, "getsockname: %m");
327 error("rlogind: getsockname: %m");
330 authopts = KOPT_DO_MUTUAL;
331 rc = krb_recvauth(authopts, 0, ticket,
332 "rcmd", instance, &fromaddr,
333 &local_addr, kdata, "", schedule,
335 des_set_key(&kdata->session, schedule);
339 getstr(remuser, sizeof(remuser), "remuser");
340 getstr(locuser, sizeof(locuser), "locuser");
341 getstr(cmdbuf, sizeof(cmdbuf), "command");
344 pwd = getpwnam(locuser);
346 syslog(LOG_INFO|LOG_AUTH,
347 "%s@%s as %s: unknown login. cmd='%.80s'",
348 remuser, fromhost, locuser, cmdbuf);
349 if (errorstr == NULL)
350 errorstr = "Login incorrect.\n";
354 lc = login_getpwclass(pwd);
356 if (chdir(pwd->pw_dir) < 0) {
358 if (chdir("/") < 0 ||
359 login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
360 syslog(LOG_INFO|LOG_AUTH,
361 "%s@%s as %s: no home directory. cmd='%.80s'",
362 remuser, fromhost, locuser, cmdbuf);
363 error("No remote home directory.\n");
369 syslog(LOG_INFO|LOG_AUTH,
370 "%s@%s as %s: no home directory. cmd='%.80s'",
371 remuser, fromhost, locuser, cmdbuf);
372 error("No remote directory.\n");
380 (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
381 iruserok_sa(fromp, fromp->su_len, pwd->pw_uid == 0,
382 remuser, locuser) < 0) {
384 syslog(LOG_INFO|LOG_AUTH,
385 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
386 remuser, fromhost, locuser, __rcmd_errstr,
389 syslog(LOG_INFO|LOG_AUTH,
390 "%s@%s as %s: permission denied. cmd='%.80s'",
391 remuser, fromhost, locuser, cmdbuf);
394 if (errorstr == NULL)
395 errorstr = "Login incorrect.\n";
396 error(errorstr, fromhost);
400 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
401 error("Logins currently disabled.\n");
405 if (lc != NULL && fromp->su_family == AF_INET) { /*XXX*/
406 char remote_ip[MAXHOSTNAMELEN];
408 strncpy(remote_ip, numericname,
409 sizeof(remote_ip) - 1);
410 remote_ip[sizeof(remote_ip) - 1] = 0;
411 if (!auth_hostok(lc, fromhost, remote_ip)) {
412 syslog(LOG_INFO|LOG_AUTH,
413 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
414 remuser, fromhost, locuser, __rcmd_errstr,
416 error("Login incorrect.\n");
419 if (!auth_timeok(lc, time(NULL))) {
420 error("Logins not available right now\n");
424 #endif /* !LOGIN_CAP */
426 /* before fork, while we're session leader */
427 if (setlogin(pwd->pw_name) < 0)
428 syslog(LOG_ERR, "setlogin() failed: %m");
431 write(STDERR_FILENO, "\0", 1);
436 error("Can't make pipe.\n");
442 error("Can't make 2nd pipe.\n");
446 error("Can't make 3rd pipe.\n");
453 error("Can't fork; try again.\n");
459 static char msg[] = SECURE_MESSAGE;
462 des_enc_write(s, msg, sizeof(msg) - 1,
463 schedule, &kdata->session);
475 FD_SET(s, &readfrom);
476 FD_SET(pv[0], &readfrom);
484 FD_SET(pv2[0], &writeto);
485 FD_SET(pv1[0], &readfrom);
487 nfd = MAX(nfd, pv2[0]);
488 nfd = MAX(nfd, pv1[0]);
491 ioctl(pv[0], FIONBIO, (char *)&one);
493 /* should set s nbio! */
500 if (select(nfd, &ready,
506 if (select(nfd, &ready, NULL,
509 if (FD_ISSET(s, &ready)) {
513 ret = des_enc_read(s, &sig, 1,
514 schedule, &kdata->session);
517 ret = read(s, &sig, 1);
519 FD_CLR(s, &readfrom);
523 if (FD_ISSET(pv[0], &ready)) {
525 cc = read(pv[0], buf, sizeof(buf));
527 shutdown(s, SHUT_RDWR);
528 FD_CLR(pv[0], &readfrom);
532 des_enc_write(s, buf,
541 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
543 cc = read(pv1[0], buf, sizeof(buf));
545 shutdown(pv1[0], SHUT_RDWR);
546 FD_CLR(pv1[0], &readfrom);
548 des_enc_write(STDOUT_FILENO,
554 if (doencrypt && FD_ISSET(pv2[0], &wready)) {
556 cc = des_enc_read(STDIN_FILENO,
558 schedule, &kdata->session);
560 shutdown(pv2[0], SHUT_RDWR);
561 FD_CLR(pv2[0], &writeto);
563 write(pv2[0], buf, cc);
568 } while (FD_ISSET(s, &readfrom) ||
570 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
572 FD_ISSET(pv[0], &readfrom));
575 setpgrp(0, getpid());
580 close(pv1[0]); close(pv2[0]);
590 if (*pwd->pw_shell == '\0')
591 pwd->pw_shell = bshell;
593 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
594 strcat(path, _PATH_DEFPATH);
595 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
596 strncat(username, pwd->pw_name, sizeof(username)-6);
597 cp = strrchr(pwd->pw_shell, '/');
603 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
604 syslog(LOG_ERR, "setusercontext: %m");
609 setgid((gid_t)pwd->pw_gid);
610 initgroups(pwd->pw_name, pwd->pw_gid);
611 setuid((uid_t)pwd->pw_uid);
614 if (log_success || pwd->pw_uid == 0) {
615 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
616 remuser, fromhost, locuser, cmdbuf);
618 execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL);
619 perror(pwd->pw_shell);
624 * Report error to client. Note: can't be used until second socket has
625 * connected to client, or older clients will hang waiting for that
630 error(const char *fmt, ...)
634 char *bp, buf[BUFSIZ];
637 if (sent_null == 0) {
642 vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
643 write(STDERR_FILENO, buf, len + strlen(bp));
647 getstr(char *buf, int cnt, const char *err)
652 if (read(STDIN_FILENO, &c, 1) != 1)
656 error("%s too long\n", err);
663 * Check whether host h is in our local domain,
664 * defined as sharing the last two components of the domain part,
665 * or the entire domain part if the local domain has only one component.
666 * If either name is unqualified (contains no '.'),
667 * assume that the host is local, as it will be
668 * interpreted as such.
671 local_domain(char *h)
673 char localhost[MAXHOSTNAMELEN];
677 gethostname(localhost, sizeof(localhost) - 1);
678 localhost[sizeof(localhost) - 1] = '\0';
679 p1 = topdomain(localhost);
681 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
689 char *p, *maybe = NULL;
692 for (p = h + strlen(h); p >= h; p--) {
706 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);