rshd.c revision 1.37 1 /* $NetBSD: rshd.c,v 1.37 2005/03/09 16:43:37 wiz Exp $ */
2
3 /*
4 * Copyright (C) 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by WIDE Project and
18 * its contributors.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 /*-
37 * Copyright (c) 1988, 1989, 1992, 1993, 1994
38 * The Regents of the University of California. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 #include <sys/cdefs.h>
66 #ifndef lint
67 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
68 The Regents of the University of California. All rights reserved.\n");
69 #if 0
70 static char sccsid[] = "@(#)rshd.c 8.2 (Berkeley) 4/6/94";
71 #else
72 __RCSID("$NetBSD: rshd.c,v 1.37 2005/03/09 16:43:37 wiz Exp $");
73 #endif
74 #endif /* not lint */
75
76 /*
77 * remote shell server:
78 * [port]\0
79 * remuser\0
80 * locuser\0
81 * command\0
82 * data
83 */
84 #include <sys/param.h>
85 #include <sys/ioctl.h>
86 #include <sys/time.h>
87 #include <sys/socket.h>
88
89 #include <netinet/in_systm.h>
90 #include <netinet/in.h>
91 #include <netinet/ip.h>
92 #include <netinet/tcp.h>
93 #include <arpa/inet.h>
94 #include <netdb.h>
95
96 #include <errno.h>
97 #include <fcntl.h>
98 #include <paths.h>
99 #include <pwd.h>
100 #include <signal.h>
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include <string.h>
104 #include <syslog.h>
105 #include <unistd.h>
106 #include <poll.h>
107 #ifdef LOGIN_CAP
108 #include <login_cap.h>
109 #endif
110
111 #ifdef USE_PAM
112 #include <security/pam_appl.h>
113 #include <security/openpam.h>
114 #include <sys/wait.h>
115
116 static struct pam_conv pamc = { openpam_nullconv, NULL };
117 static pam_handle_t *pamh;
118 static int pam_err;
119
120 #define PAM_END { \
121 if ((pam_err = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) \
122 syslog(LOG_ERR|LOG_AUTH, "pam_setcred(): %s", \
123 pam_strerror(pamh, pam_err)); \
124 if ((pam_err = pam_close_session(pamh,0)) != PAM_SUCCESS) \
125 syslog(LOG_ERR|LOG_AUTH, "pam_close_session(): %s", \
126 pam_strerror(pamh, pam_err)); \
127 if ((pam_err = pam_end(pamh, pam_err)) != PAM_SUCCESS) \
128 syslog(LOG_ERR|LOG_AUTH, "pam_end(): %s", \
129 pam_strerror(pamh, pam_err)); \
130 }
131 #else
132 #define PAM_END
133 #endif
134
135 int keepalive = 1;
136 int check_all;
137 int log_success; /* If TRUE, log all successful accesses */
138 int sent_null;
139
140 void doit(struct sockaddr *);
141 void rshd_errx(int, const char *, ...)
142 __attribute__((__noreturn__, __format__(__printf__, 2, 3)));
143 void getstr(char *, int, char *);
144 int local_domain(char *);
145 char *topdomain(char *);
146 void usage(void);
147 int main(int, char *[]);
148
149 #define OPTIONS "aLln"
150 extern int __check_rhosts_file;
151 extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c. */
152 static const char incorrect[] = "Login incorrect.";
153
154 int
155 main(int argc, char *argv[])
156 {
157 struct linger linger;
158 int ch, on = 1, fromlen;
159 struct sockaddr_storage from;
160 struct protoent *proto;
161
162 openlog("rshd", LOG_PID, LOG_DAEMON);
163
164 opterr = 0;
165 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
166 switch (ch) {
167 case 'a':
168 check_all = 1;
169 break;
170 case 'l':
171 __check_rhosts_file = 0;
172 break;
173 case 'n':
174 keepalive = 0;
175 break;
176 case 'L':
177 log_success = 1;
178 break;
179 case '?':
180 default:
181 usage();
182 break;
183 }
184
185 argc -= optind;
186 argv += optind;
187
188 fromlen = sizeof (from); /* xxx */
189 if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
190 syslog(LOG_ERR, "getpeername: %m");
191 exit(1);
192 }
193 #if 0
194 if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
195 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) &&
196 sizeof(struct sockaddr_in) <= sizeof(from)) {
197 struct sockaddr_in sin;
198 struct sockaddr_in6 *sin6;
199 const int off = sizeof(struct sockaddr_in6) -
200 sizeof(struct sockaddr_in);
201
202 sin6 = (struct sockaddr_in6 *)&from;
203 memset(&sin, 0, sizeof(sin));
204 sin.sin_family = AF_INET;
205 sin.sin_len = sizeof(struct sockaddr_in);
206 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off],
207 sizeof(sin.sin_addr));
208 memcpy(&from, &sin, sizeof(sin));
209 fromlen = sin.sin_len;
210 }
211 #else
212 if (((struct sockaddr *)&from)->sa_family == AF_INET6 &&
213 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) {
214 char hbuf[NI_MAXHOST];
215 if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf,
216 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
217 strlcpy(hbuf, "invalid", sizeof(hbuf));
218 }
219 syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)",
220 hbuf);
221 exit(1);
222 }
223 #endif
224 if (keepalive &&
225 setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
226 sizeof(on)) < 0)
227 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
228 linger.l_onoff = 1;
229 linger.l_linger = 60; /* XXX */
230 if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
231 sizeof (linger)) < 0)
232 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
233 proto = getprotobyname("tcp");
234 setsockopt(0, proto->p_proto, TCP_NODELAY, &on, sizeof(on));
235 doit((struct sockaddr *)&from);
236 /* NOTREACHED */
237 #ifdef __GNUC__
238 exit(0);
239 #endif
240 }
241
242 char username[20] = "USER=";
243 char homedir[64] = "HOME=";
244 char shell[64] = "SHELL=";
245 char path[100] = "PATH=";
246 char *envinit[] =
247 {homedir, shell, path, username, 0};
248 char **environ;
249
250 void
251 doit(struct sockaddr *fromp)
252 {
253 struct passwd *pwd;
254 in_port_t port;
255 struct pollfd set[2];
256 int cc, pv[2], pid, s = -1; /* XXX gcc */
257 int one = 1;
258 char *hostname, *errorhost = NULL; /* XXX gcc */
259 const char *cp;
260 char sig, buf[BUFSIZ];
261 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
262 char remotehost[2 * MAXHOSTNAMELEN + 1];
263 char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
264 #ifdef LOGIN_CAP
265 login_cap_t *lc;
266 char *sh;
267 #endif
268 char naddr[NI_MAXHOST];
269 char saddr[NI_MAXHOST];
270 char raddr[NI_MAXHOST];
271 char pbuf[NI_MAXSERV];
272 int af = fromp->sa_family;
273 u_int16_t *portp;
274 struct addrinfo hints, *res, *res0;
275 int gaierror;
276 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
277 const char *errormsg = NULL, *errorstr = NULL;
278
279 (void) signal(SIGINT, SIG_DFL);
280 (void) signal(SIGQUIT, SIG_DFL);
281 (void) signal(SIGTERM, SIG_DFL);
282 #ifdef DEBUG
283 {
284 int t = open(_PATH_TTY, 2);
285 if (t >= 0) {
286 ioctl(t, TIOCNOTTY, (char *)0);
287 (void)close(t);
288 }
289 }
290 #endif
291 switch (af) {
292 case AF_INET:
293 portp = &((struct sockaddr_in *)fromp)->sin_port;
294 break;
295 #ifdef INET6
296 case AF_INET6:
297 portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
298 break;
299 #endif
300 default:
301 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
302 exit(1);
303 }
304 if (getnameinfo(fromp, fromp->sa_len, naddr, sizeof(naddr),
305 pbuf, sizeof(pbuf), niflags) != 0) {
306 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
307 exit(1);
308 }
309 #ifdef IP_OPTIONS
310 if (af == AF_INET) {
311
312 u_char optbuf[BUFSIZ/3];
313 int optsize = sizeof(optbuf), ipproto, i;
314 struct protoent *ip;
315
316 if ((ip = getprotobyname("ip")) != NULL)
317 ipproto = ip->p_proto;
318 else
319 ipproto = IPPROTO_IP;
320 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
321 optsize != 0) {
322 for (i = 0; i < optsize;) {
323 u_char c = optbuf[i];
324 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
325 syslog(LOG_NOTICE,
326 "Connection refused from %s "
327 "with IP option %s",
328 inet_ntoa((
329 (struct sockaddr_in *)fromp)->sin_addr),
330 c == IPOPT_LSRR ? "LSRR" : "SSRR");
331 exit(1);
332 }
333 if (c == IPOPT_EOL)
334 break;
335 i += (c == IPOPT_NOP) ? 1 : optbuf[i + 1];
336 }
337 }
338 }
339 #endif
340 if (ntohs(*portp) >= IPPORT_RESERVED
341 || ntohs(*portp) < IPPORT_RESERVED/2) {
342 syslog(LOG_NOTICE|LOG_AUTH,
343 "Connection from %s on illegal port %u",
344 naddr, ntohs(*portp));
345 exit(1);
346 }
347
348 (void) alarm(60);
349 port = 0;
350 for (;;) {
351 char c;
352
353 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
354 if (cc < 0)
355 syslog(LOG_ERR, "read: %m");
356 shutdown(0, SHUT_RDWR);
357 exit(1);
358 }
359 if (c == 0)
360 break;
361 port = port * 10 + c - '0';
362 }
363
364 (void) alarm(0);
365 if (port != 0) {
366 int lport = IPPORT_RESERVED - 1;
367 s = rresvport_af(&lport, af);
368 if (s < 0) {
369 syslog(LOG_ERR, "can't get stderr port: %m");
370 exit(1);
371 }
372 if (port >= IPPORT_RESERVED) {
373 syslog(LOG_ERR, "2nd port not reserved");
374 exit(1);
375 }
376 *portp = htons(port);
377 if (connect(s, (struct sockaddr *)fromp, fromp->sa_len) < 0) {
378 syslog(LOG_ERR, "connect second port %d: %m", port);
379 exit(1);
380 }
381 }
382
383
384 #ifdef notdef
385 /* from inetd, socket is already on 0, 1, 2 */
386 dup2(f, 0);
387 dup2(f, 1);
388 dup2(f, 2);
389 #endif
390 if (getnameinfo(fromp, fromp->sa_len, saddr, sizeof(saddr),
391 NULL, 0, NI_NAMEREQD) == 0) {
392 /*
393 * If name returned by getnameinfo is in our domain,
394 * attempt to verify that we haven't been fooled by someone
395 * in a remote net; look up the name and check that this
396 * address corresponds to the name.
397 */
398 hostname = saddr;
399 res0 = NULL;
400 if (check_all || local_domain(saddr)) {
401 strlcpy(remotehost, saddr, sizeof(remotehost));
402 errorhost = remotehost;
403 memset(&hints, 0, sizeof(hints));
404 hints.ai_family = fromp->sa_family;
405 hints.ai_socktype = SOCK_STREAM;
406 hints.ai_flags = AI_CANONNAME;
407 gaierror = getaddrinfo(remotehost, pbuf, &hints, &res0);
408 if (gaierror) {
409 syslog(LOG_NOTICE,
410 "Couldn't look up address for %s: %s",
411 remotehost, gai_strerror(gaierror));
412 errorstr =
413 "Couldn't look up address for your host (%s)\n";
414 hostname = naddr;
415 } else {
416 for (res = res0; res; res = res->ai_next) {
417 if (res->ai_family != fromp->sa_family)
418 continue;
419 if (res->ai_addrlen != fromp->sa_len)
420 continue;
421 if (getnameinfo(res->ai_addr,
422 res->ai_addrlen,
423 raddr, sizeof(raddr), NULL, 0,
424 niflags) == 0
425 && strcmp(naddr, raddr) == 0) {
426 hostname = res->ai_canonname
427 ? res->ai_canonname
428 : saddr;
429 break;
430 }
431 }
432 if (res == NULL) {
433 syslog(LOG_NOTICE,
434 "Host addr %s not listed for host %s",
435 naddr, res0->ai_canonname
436 ? res0->ai_canonname
437 : saddr);
438 errorstr =
439 "Host address mismatch for %s\n";
440 hostname = naddr;
441 }
442 }
443 }
444 strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf));
445 hostname = hostnamebuf;
446 if (res0)
447 freeaddrinfo(res0);
448 } else {
449 strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf));
450 errorhost = hostname = hostnamebuf;
451 }
452
453 (void)alarm(60);
454 getstr(remuser, sizeof(remuser), "remuser");
455 getstr(locuser, sizeof(locuser), "locuser");
456 getstr(cmdbuf, sizeof(cmdbuf), "command");
457 (void)alarm(0);
458
459 #ifdef USE_PAM
460 pam_err = pam_start("rsh", locuser, &pamc, &pamh);
461 if (pam_err != PAM_SUCCESS) {
462 syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
463 pam_strerror(pamh, pam_err));
464 rshd_errx(1, incorrect);
465 }
466
467 if ((pam_err = pam_set_item(pamh, PAM_RUSER, remuser)) != PAM_SUCCESS ||
468 (pam_err = pam_set_item(pamh, PAM_RHOST, hostname) != PAM_SUCCESS)){
469 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
470 pam_strerror(pamh, pam_err));
471 rshd_errx(1, incorrect);
472 }
473
474 pam_err = pam_authenticate(pamh, 0);
475 if (pam_err == PAM_SUCCESS) {
476 if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
477 strlcpy(locuser, cp, sizeof(locuser));
478 /* XXX truncation! */
479 }
480 pam_err = pam_acct_mgmt(pamh, 0);
481 }
482 if (pam_err != PAM_SUCCESS) {
483 errorstr = incorrect;
484 errormsg = pam_strerror(pamh, pam_err);
485 goto badlogin;
486 }
487 #endif /* USE_PAM */
488 setpwent();
489 pwd = getpwnam(locuser);
490 if (pwd == NULL) {
491 syslog(LOG_INFO|LOG_AUTH,
492 "%s@%s as %s: unknown login. cmd='%.80s'",
493 remuser, hostname, locuser, cmdbuf);
494 if (errorstr == NULL)
495 errorstr = "Permission denied.";
496 rshd_errx(1, errorstr, errorhost);
497 }
498 #ifdef LOGIN_CAP
499 lc = login_getclass(pwd ? pwd->pw_class : NULL);
500 #endif
501
502 if (chdir(pwd->pw_dir) < 0) {
503 if (chdir("/") < 0
504 #ifdef LOGIN_CAP
505 || login_getcapbool(lc, "requirehome", pwd->pw_uid ? 1 : 0)
506 #endif
507 ) {
508 syslog(LOG_INFO|LOG_AUTH,
509 "%s@%s as %s: no home directory. cmd='%.80s'",
510 remuser, hostname, locuser, cmdbuf);
511 rshd_errx(0, "No remote home directory.");
512 }
513 }
514
515 #ifndef USE_PAM
516 if (errorstr ||
517 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
518 iruserok_sa(fromp, fromp->sa_len, pwd->pw_uid == 0, remuser,
519 locuser) < 0)) {
520 errormsg = __rcmd_errstr ? __rcmd_errstr : "unknown error";
521 if (errorstr == NULL)
522 errorstr = "Permission denied.";
523 goto badlogin;
524 }
525 #endif
526
527 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK))
528 rshd_errx(1, "Logins currently disabled.");
529
530 (void) write(STDERR_FILENO, "\0", 1);
531 sent_null = 1;
532
533 if (port) {
534 if (pipe(pv) < 0)
535 rshd_errx(1, "Can't make pipe. (%s)", strerror(errno));
536 pid = fork();
537 if (pid == -1)
538 rshd_errx(1, "Can't fork. (%s)", strerror(errno));
539 if (pid) {
540 {
541 (void) close(0);
542 (void) close(1);
543 }
544 (void) close(2);
545 (void) close(pv[1]);
546
547 set[0].fd = s;
548 set[0].events = POLLIN;
549 set[1].fd = pv[0];
550 set[1].events = POLLIN;
551 ioctl(pv[0], FIONBIO, (char *)&one);
552
553 /* should set s nbio! */
554 do {
555 if (poll(set, 2, INFTIM) < 0)
556 break;
557 if (set[0].revents & POLLIN) {
558 int ret;
559
560 ret = read(s, &sig, 1);
561 if (ret <= 0)
562 set[0].events = 0;
563 else
564 killpg(pid, sig);
565 }
566 if (set[1].revents & POLLIN) {
567 errno = 0;
568 cc = read(pv[0], buf, sizeof(buf));
569 if (cc <= 0) {
570 shutdown(s, SHUT_RDWR);
571 set[1].events = 0;
572 } else {
573 (void) write(s, buf, cc);
574 }
575 }
576
577 } while ((set[0].revents | set[1].revents) & POLLIN);
578 PAM_END
579 exit(0);
580 }
581 (void) close(s);
582 (void) close(pv[0]);
583 dup2(pv[1], 2);
584 close(pv[1]);
585 }
586 #ifdef USE_PAM
587 else {
588 pid = fork();
589 if (pid == -1)
590 rshd_errx(1, "Can't fork. (%s)", strerror(errno));
591 if (pid) {
592 /* Parent. */
593 while (wait(NULL) > 0 || errno == EINTR)
594 continue;
595 PAM_END
596 exit(0);
597 }
598 }
599 #endif
600
601 if (*pwd->pw_shell == '\0')
602 pwd->pw_shell = _PATH_BSHELL;
603 #ifdef F_CLOSEM
604 (void)fcntl(3, F_CLOSEM, 0);
605 #else
606 for (fd = getdtablesize(); fd > 2; fd--)
607 (void)close(fd);
608 #endif
609 if (setsid() == -1)
610 syslog(LOG_ERR, "setsid() failed: %m");
611 #ifdef USE_PAM
612 if (setlogin(pwd->pw_name) < 0)
613 syslog(LOG_ERR, "setlogin() failed: %m");
614
615 (void)pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
616 (void)pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
617 (void)pam_setenv(pamh, "USER", pwd->pw_name, 1);
618 (void)pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
619 environ = pam_getenvlist(pamh);
620 (void)pam_end(pamh, pam_err);
621 #endif
622 #ifdef LOGIN_CAP
623 if ((sh = login_getcapstr(lc, "shell", NULL, NULL))) {
624 if(!(sh = strdup(sh))) {
625 syslog(LOG_ERR, "Cannot alloc mem");
626 exit(1);
627 }
628 pwd->pw_shell = sh;
629 }
630 #endif
631 environ = envinit;
632 strlcat(homedir, pwd->pw_dir, sizeof(homedir));
633 strlcat(path, _PATH_DEFPATH, sizeof(path));
634 strlcat(shell, pwd->pw_shell, sizeof(shell));
635 strlcat(username, pwd->pw_name, sizeof(username));
636 #ifdef LOGIN_CAP
637 /*
638 * PAM modules might add supplementary groups in
639 * pam_setcred(), so initialize them first.
640 * But we need to open the session as root.
641 */
642 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
643 syslog(LOG_ERR, "setusercontext: %m");
644 exit(1);
645 }
646 #else
647 initgroups(pwd->pw_name, pwd->pw_gid);
648 #endif
649
650 #ifdef USE_PAM
651 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
652 syslog(LOG_ERR, "pam_open_session: %s",
653 pam_strerror(pamh, pam_err));
654 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED))
655 != PAM_SUCCESS) {
656 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
657 }
658 #endif
659
660 cp = strrchr(pwd->pw_shell, '/');
661 if (cp)
662 cp++;
663 else
664 cp = pwd->pw_shell;
665
666 #ifdef LOGIN_CAP
667 if (setusercontext(lc, pwd, pwd->pw_uid,
668 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
669 syslog(LOG_ERR, "setusercontext(): %m");
670 exit(1);
671 }
672 login_close(lc);
673 #else
674 (void)setgid((gid_t)pwd->pw_gid);
675 (void)setuid((uid_t)pwd->pw_uid);
676 #endif
677 endpwent();
678 if (log_success || pwd->pw_uid == 0) {
679 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
680 remuser, hostname, locuser, cmdbuf);
681 }
682 execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL);
683 rshd_errx(1, "%s: %s", pwd->pw_shell, strerror(errno));
684 badlogin:
685 syslog(LOG_INFO|LOG_AUTH,
686 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
687 remuser, hostname, locuser, errormsg, cmdbuf);
688 rshd_errx(1, errorstr, errorhost);
689 }
690
691 /*
692 * Report error to client. Note: can't be used until second socket has
693 * connected to client, or older clients will hang waiting for that
694 * connection first.
695 */
696
697 #include <stdarg.h>
698
699 void
700 rshd_errx(int error, const char *fmt, ...)
701 {
702 va_list ap;
703 int len, rv;
704 char *bp, buf[BUFSIZ];
705 va_start(ap, fmt);
706 bp = buf;
707 if (sent_null == 0) {
708 *bp++ = 1;
709 len = 1;
710 } else
711 len = 0;
712 rv = vsnprintf(bp, sizeof(buf) - 2, fmt, ap);
713 bp[rv++] = '\n';
714 (void)write(STDERR_FILENO, buf, len + rv);
715 va_end(ap);
716 exit(error);
717 }
718
719 void
720 getstr(char *buf, int cnt, char *err)
721 {
722 char c;
723
724 do {
725 if (read(STDIN_FILENO, &c, 1) != 1)
726 exit(1);
727 *buf++ = c;
728 if (--cnt == 0)
729 rshd_errx(1, "%s too long", err);
730 } while (c != 0);
731 }
732
733 /*
734 * Check whether host h is in our local domain,
735 * defined as sharing the last two components of the domain part,
736 * or the entire domain part if the local domain has only one component.
737 * If either name is unqualified (contains no '.'),
738 * assume that the host is local, as it will be
739 * interpreted as such.
740 */
741 int
742 local_domain(char *h)
743 {
744 char localhost[MAXHOSTNAMELEN + 1];
745 char *p1, *p2;
746
747 localhost[0] = 0;
748 (void)gethostname(localhost, sizeof(localhost));
749 localhost[sizeof(localhost) - 1] = '\0';
750 p1 = topdomain(localhost);
751 p2 = topdomain(h);
752 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
753 return (1);
754 return (0);
755 }
756
757 char *
758 topdomain(char *h)
759 {
760 char *p, *maybe = NULL;
761 int dots = 0;
762
763 for (p = h + strlen(h); p >= h; p--) {
764 if (*p == '.') {
765 if (++dots == 2)
766 return (p);
767 maybe = p;
768 }
769 }
770 return (maybe);
771 }
772
773 void
774 usage(void)
775 {
776
777 syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
778 exit(2);
779 }
780