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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1988, 1989, 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)rshd.c 8.2 (Berkeley) 4/6/94
31 * $FreeBSD: src/libexec/rshd/rshd.c,v 1.30.2.5 2002/05/14 22:27:21 des Exp $
35 * remote shell server:
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
45 #include <sys/socket.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/in.h>
49 #include <netinet/ip.h>
50 #include <netinet/tcp.h>
51 #include <arpa/inet.h>
67 #include <login_cap.h>
70 /* wrapper for KAME-special getnameinfo() */
71 #ifndef NI_WITHSCOPEID
72 #define NI_WITHSCOPEID 0
75 extern int __check_rhosts_file
;
76 extern char *__rcmd_errstr
; /* syslog hook from libc/net/rcmd.c. */
79 int log_success
; /* If TRUE, log all successful accesses */
92 struct sockaddr_in su_sin
;
93 struct sockaddr_in6 su_sin6
;
95 #define su_len su_si.si_len
96 #define su_family su_si.si_family
97 #define su_port su_si.si_port
99 void doit(union sockunion
*);
100 void error(const char *, ...) __printflike(1, 2);
101 void getstr(char *, int, const char *);
102 int local_domain(char *);
103 char *topdomain(char *);
107 char bshell
[] = _PATH_BSHELL
;
109 #define OPTIONS "alnDL"
112 main(int argc
, char *argv
[])
114 struct linger linger
;
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};
191 doit(union sockunion
*fromp
)
195 fd_set ready
, readfrom
;
196 int cc
, nfd
, pv
[2], pid
, s
;
198 const char *errorstr
;
199 char *cp
, sig
, buf
[BUFSIZ
];
200 char cmdbuf
[NCARGS
+1], locuser
[16], remuser
[16];
201 char fromhost
[2 * MAXHOSTNAMELEN
+ 1];
202 char numericname
[INET6_ADDRSTRLEN
];
203 int af
= fromp
->su_family
, gaierror
;
212 signal(SIGINT
, SIG_DFL
);
213 signal(SIGQUIT
, SIG_DFL
);
214 signal(SIGTERM
, SIG_DFL
);
215 fromp
->su_port
= ntohs((u_short
)fromp
->su_port
);
221 syslog(LOG_ERR
, "malformed \"from\" address (af %d)\n", af
);
224 gaierror
= getnameinfo((struct sockaddr
*)fromp
, fromp
->su_len
,
225 numericname
, sizeof(numericname
), NULL
, 0,
226 NI_NUMERICHOST
|NI_WITHSCOPEID
);
228 syslog(LOG_ERR
, "malformed \"from\" address (af %d): %s", af
,
229 gai_strerror(gaierror
));
234 u_char optbuf
[BUFSIZ
/3];
235 socklen_t optsize
= sizeof(optbuf
), i
;
239 if ((ip
= getprotobyname("ip")) != NULL
)
240 ipproto
= ip
->p_proto
;
242 ipproto
= IPPROTO_IP
;
243 if (!getsockopt(0, ipproto
, IP_OPTIONS
, (char *)optbuf
, &optsize
) &&
245 for (i
= 0; i
< optsize
; ) {
246 u_char c
= optbuf
[i
];
247 if (c
== IPOPT_LSRR
|| c
== IPOPT_SSRR
) {
249 "connection refused from %s with IP option %s",
251 c
== IPOPT_LSRR
? "LSRR" : "SSRR");
256 i
+= (c
== IPOPT_NOP
) ? 1 : optbuf
[i
+1];
262 if (fromp
->su_port
>= IPPORT_RESERVED
||
263 fromp
->su_port
< IPPORT_RESERVED
/2) {
264 syslog(LOG_NOTICE
|LOG_AUTH
,
265 "connection from %s on illegal port %u",
273 s
= 0; /* not set or used if port == 0 */
276 if ((cc
= read(STDIN_FILENO
, &c
, 1)) != 1) {
278 syslog(LOG_NOTICE
, "read: %m");
279 shutdown(0, SHUT_RDWR
);
284 port
= port
* 10 + c
- '0';
289 int lport
= IPPORT_RESERVED
- 1;
290 s
= rresvport_af(&lport
, af
);
292 syslog(LOG_ERR
, "can't get stderr port: %m");
295 if (port
>= IPPORT_RESERVED
||
296 port
< IPPORT_RESERVED
/2) {
297 syslog(LOG_NOTICE
|LOG_AUTH
,
298 "2nd socket from %s on unreserved port %u",
303 fromp
->su_port
= htons(port
);
304 if (connect(s
, (struct sockaddr
*)fromp
, fromp
->su_len
) < 0) {
305 syslog(LOG_INFO
, "connect second port %d: %m", port
);
311 realhostname_sa(fromhost
, sizeof(fromhost
) - 1,
312 (struct sockaddr
*)fromp
,
314 fromhost
[sizeof(fromhost
) - 1] = '\0';
317 if (doencrypt
&& af
== AF_INET
) {
318 struct sockaddr_in local_addr
;
319 rc
= sizeof(local_addr
);
320 if (getsockname(0, (struct sockaddr
*)&local_addr
,
322 syslog(LOG_ERR
, "getsockname: %m");
323 error("rlogind: getsockname: %m");
326 authopts
= KOPT_DO_MUTUAL
;
327 rc
= krb_recvauth(authopts
, 0, ticket
,
328 "rcmd", instance
, &fromaddr
,
329 &local_addr
, kdata
, "", schedule
,
331 des_set_key(&kdata
->session
, schedule
);
335 getstr(remuser
, sizeof(remuser
), "remuser");
336 getstr(locuser
, sizeof(locuser
), "locuser");
337 getstr(cmdbuf
, sizeof(cmdbuf
), "command");
340 pwd
= getpwnam(locuser
);
342 syslog(LOG_INFO
|LOG_AUTH
,
343 "%s@%s as %s: unknown login. cmd='%.80s'",
344 remuser
, fromhost
, locuser
, cmdbuf
);
345 if (errorstr
== NULL
)
346 errorstr
= "Login incorrect.\n";
350 lc
= login_getpwclass(pwd
);
352 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 remuser
, fromhost
, locuser
, cmdbuf
);
359 error("No remote home directory.\n");
365 syslog(LOG_INFO
|LOG_AUTH
,
366 "%s@%s as %s: no home directory. cmd='%.80s'",
367 remuser
, fromhost
, locuser
, cmdbuf
);
368 error("No remote directory.\n");
376 (pwd
->pw_expire
&& time(NULL
) >= pwd
->pw_expire
) ||
377 iruserok_sa(fromp
, fromp
->su_len
, pwd
->pw_uid
== 0,
378 remuser
, locuser
) < 0) {
380 syslog(LOG_INFO
|LOG_AUTH
,
381 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
382 remuser
, fromhost
, locuser
, __rcmd_errstr
,
385 syslog(LOG_INFO
|LOG_AUTH
,
386 "%s@%s as %s: permission denied. cmd='%.80s'",
387 remuser
, fromhost
, locuser
, cmdbuf
);
390 if (errorstr
== NULL
)
391 errorstr
= "Login incorrect.\n";
392 error(errorstr
, fromhost
);
396 if (pwd
->pw_uid
&& !access(_PATH_NOLOGIN
, F_OK
)) {
397 error("Logins currently disabled.\n");
401 if (lc
!= NULL
&& fromp
->su_family
== AF_INET
) { /*XXX*/
402 char remote_ip
[MAXHOSTNAMELEN
];
404 strncpy(remote_ip
, numericname
,
405 sizeof(remote_ip
) - 1);
406 remote_ip
[sizeof(remote_ip
) - 1] = 0;
407 if (!auth_hostok(lc
, fromhost
, remote_ip
)) {
408 syslog(LOG_INFO
|LOG_AUTH
,
409 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
410 remuser
, fromhost
, locuser
, __rcmd_errstr
,
412 error("Login incorrect.\n");
415 if (!auth_timeok(lc
, time(NULL
))) {
416 error("Logins not available right now\n");
420 #endif /* !LOGIN_CAP */
422 /* before fork, while we're session leader */
423 if (setlogin(pwd
->pw_name
) < 0)
424 syslog(LOG_ERR
, "setlogin() failed: %m");
427 write(STDERR_FILENO
, "\0", 1);
432 error("Can't make pipe.\n");
438 error("Can't make 2nd pipe.\n");
442 error("Can't make 3rd pipe.\n");
449 error("Can't fork; try again.\n");
455 static char msg
[] = SECURE_MESSAGE
;
458 des_enc_write(s
, msg
, sizeof(msg
) - 1,
459 schedule
, &kdata
->session
);
471 FD_SET(s
, &readfrom
);
472 FD_SET(pv
[0], &readfrom
);
480 FD_SET(pv2
[0], &writeto
);
481 FD_SET(pv1
[0], &readfrom
);
483 nfd
= MAX(nfd
, pv2
[0]);
484 nfd
= MAX(nfd
, pv1
[0]);
487 ioctl(pv
[0], FIONBIO
, (char *)&one
);
489 /* should set s nbio! */
496 if (select(nfd
, &ready
,
502 if (select(nfd
, &ready
, NULL
,
505 if (FD_ISSET(s
, &ready
)) {
509 ret
= des_enc_read(s
, &sig
, 1,
510 schedule
, &kdata
->session
);
513 ret
= read(s
, &sig
, 1);
515 FD_CLR(s
, &readfrom
);
519 if (FD_ISSET(pv
[0], &ready
)) {
521 cc
= read(pv
[0], buf
, sizeof(buf
));
523 shutdown(s
, SHUT_RDWR
);
524 FD_CLR(pv
[0], &readfrom
);
528 des_enc_write(s
, buf
,
537 if (doencrypt
&& FD_ISSET(pv1
[0], &ready
)) {
539 cc
= read(pv1
[0], buf
, sizeof(buf
));
541 shutdown(pv1
[0], SHUT_RDWR
);
542 FD_CLR(pv1
[0], &readfrom
);
544 des_enc_write(STDOUT_FILENO
,
550 if (doencrypt
&& FD_ISSET(pv2
[0], &wready
)) {
552 cc
= des_enc_read(STDIN_FILENO
,
554 schedule
, &kdata
->session
);
556 shutdown(pv2
[0], SHUT_RDWR
);
557 FD_CLR(pv2
[0], &writeto
);
559 write(pv2
[0], buf
, cc
);
564 } while (FD_ISSET(s
, &readfrom
) ||
566 (doencrypt
&& FD_ISSET(pv1
[0], &readfrom
)) ||
568 FD_ISSET(pv
[0], &readfrom
));
571 setpgrp(0, getpid());
576 close(pv1
[0]); close(pv2
[0]);
586 if (*pwd
->pw_shell
== '\0')
587 pwd
->pw_shell
= bshell
;
589 strncat(homedir
, pwd
->pw_dir
, sizeof(homedir
)-6);
590 strcat(path
, _PATH_DEFPATH
);
591 strncat(shell
, pwd
->pw_shell
, sizeof(shell
)-7);
592 strncat(username
, pwd
->pw_name
, sizeof(username
)-6);
593 cp
= strrchr(pwd
->pw_shell
, '/');
599 if (setusercontext(lc
, pwd
, pwd
->pw_uid
, LOGIN_SETALL
) != 0) {
600 syslog(LOG_ERR
, "setusercontext: %m");
605 setgid((gid_t
)pwd
->pw_gid
);
606 initgroups(pwd
->pw_name
, pwd
->pw_gid
);
607 setuid((uid_t
)pwd
->pw_uid
);
610 if (log_success
|| pwd
->pw_uid
== 0) {
611 syslog(LOG_INFO
|LOG_AUTH
, "%s@%s as %s: cmd='%.80s'",
612 remuser
, fromhost
, locuser
, cmdbuf
);
614 execl(pwd
->pw_shell
, cp
, "-c", cmdbuf
, NULL
);
615 perror(pwd
->pw_shell
);
620 * Report error to client. Note: can't be used until second socket has
621 * connected to client, or older clients will hang waiting for that
626 error(const char *fmt
, ...)
630 char *bp
, buf
[BUFSIZ
];
633 if (sent_null
== 0) {
638 vsnprintf(bp
, sizeof(buf
) - 1, fmt
, ap
);
639 write(STDERR_FILENO
, buf
, len
+ strlen(bp
));
643 getstr(char *buf
, int cnt
, const char *err
)
648 if (read(STDIN_FILENO
, &c
, 1) != 1)
652 error("%s too long\n", err
);
659 * Check whether host h is in our local domain,
660 * defined as sharing the last two components of the domain part,
661 * or the entire domain part if the local domain has only one component.
662 * If either name is unqualified (contains no '.'),
663 * assume that the host is local, as it will be
664 * interpreted as such.
667 local_domain(char *h
)
669 char localhost
[MAXHOSTNAMELEN
];
673 gethostname(localhost
, sizeof(localhost
) - 1);
674 localhost
[sizeof(localhost
) - 1] = '\0';
675 p1
= topdomain(localhost
);
677 if (p1
== NULL
|| p2
== NULL
|| !strcasecmp(p1
, p2
))
685 char *p
, *maybe
= NULL
;
688 for (p
= h
+ strlen(h
); p
>= h
; p
--) {
702 syslog(LOG_ERR
, "usage: rshd [-%s]", OPTIONS
);