rshd.c revision 1.43 1 /* $NetBSD: rshd.c,v 1.43 2005/04/19 03:24:24 christos 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.43 2005/04/19 03:24:24 christos 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 do { \
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 } while (/*CONSTCOND*/0)
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 *) __attribute__((__noreturn__));
141 void rshd_errx(int, const char *, ...)
142 __attribute__((__noreturn__, __format__(__printf__, 2, 3)));
143 void getstr(char *, int, const 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(STDIN_FILENO, (struct sockaddr *)&from, &fromlen) < 0) {
190 syslog(LOG_ERR, "getpeername: %m");
191 return EXIT_FAILURE;
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 (void)memset(&sin, 0, sizeof(sin));
204 sin.sin_family = AF_INET;
205 sin.sin_len = sizeof(struct sockaddr_in);
206 (void)memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[off],
207 sizeof(sin.sin_addr));
208 (void)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 return EXIT_FAILURE;
222 }
223 #endif
224 if (keepalive &&
225 setsockopt(STDIN_FILENO, 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(STDIN_FILENO, SOL_SOCKET, SO_LINGER, (char *)&linger,
231 sizeof (linger)) < 0)
232 syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
233 proto = getprotobyname("tcp");
234 (void)setsockopt(STDIN_FILENO, proto->p_proto, TCP_NODELAY, &on,
235 sizeof(on));
236 doit((struct sockaddr *)&from);
237 }
238
239 char username[20] = "USER=";
240 char homedir[64] = "HOME=";
241 char shell[64] = "SHELL=";
242 char path[100] = "PATH=";
243 char *envinit[] =
244 {homedir, shell, path, username, 0};
245 char **environ;
246
247 void
248 doit(struct sockaddr *fromp)
249 {
250 struct passwd *pwd, pwres;
251 in_port_t port;
252 struct pollfd set[2];
253 int cc, pv[2], pid, s = -1; /* XXX gcc */
254 int one = 1;
255 char *hostname, *errorhost = NULL; /* XXX gcc */
256 const char *cp;
257 char sig, buf[BUFSIZ];
258 char cmdbuf[NCARGS+1], locuser[16], remuser[16];
259 char remotehost[2 * MAXHOSTNAMELEN + 1];
260 char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
261 #ifdef LOGIN_CAP
262 login_cap_t *lc;
263 #endif
264 char naddr[NI_MAXHOST];
265 char saddr[NI_MAXHOST];
266 char raddr[NI_MAXHOST];
267 char pbuf[NI_MAXSERV];
268 int af = fromp->sa_family;
269 u_int16_t *portp;
270 struct addrinfo hints, *res, *res0;
271 int gaierror;
272 const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
273 const char *errormsg = NULL, *errorstr = NULL;
274 char pwbuf[1024];
275
276 (void)signal(SIGINT, SIG_DFL);
277 (void)signal(SIGQUIT, SIG_DFL);
278 (void)signal(SIGTERM, SIG_DFL);
279 #ifdef DEBUG
280 {
281 int t = open(_PATH_TTY, O_RDWR);
282 if (t >= 0) {
283 ioctl(t, TIOCNOTTY, NULL);
284 (void)close(t);
285 }
286 }
287 #endif
288 switch (af) {
289 case AF_INET:
290 portp = &((struct sockaddr_in *)fromp)->sin_port;
291 break;
292 #ifdef INET6
293 case AF_INET6:
294 portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
295 break;
296 #endif
297 default:
298 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
299 exit(EXIT_FAILURE);
300 }
301 if (getnameinfo(fromp, fromp->sa_len, naddr, sizeof(naddr),
302 pbuf, sizeof(pbuf), niflags) != 0) {
303 syslog(LOG_ERR, "malformed \"from\" address (af %d)", af);
304 exit(EXIT_FAILURE);
305 }
306 #ifdef IP_OPTIONS
307 if (af == AF_INET) {
308
309 u_char optbuf[BUFSIZ/3];
310 int optsize = sizeof(optbuf), ipproto, i;
311 struct protoent *ip;
312
313 if ((ip = getprotobyname("ip")) != NULL)
314 ipproto = ip->p_proto;
315 else
316 ipproto = IPPROTO_IP;
317 if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
318 optsize != 0) {
319 for (i = 0; i < optsize;) {
320 u_char c = optbuf[i];
321 if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
322 syslog(LOG_NOTICE,
323 "Connection refused from %s "
324 "with IP option %s",
325 inet_ntoa((
326 (struct sockaddr_in *)fromp)->sin_addr),
327 c == IPOPT_LSRR ? "LSRR" : "SSRR");
328 exit(EXIT_FAILURE);
329 }
330 if (c == IPOPT_EOL)
331 break;
332 i += (c == IPOPT_NOP) ? 1 : optbuf[i + 1];
333 }
334 }
335 }
336 #endif
337 if (ntohs(*portp) >= IPPORT_RESERVED
338 || ntohs(*portp) < IPPORT_RESERVED / 2) {
339 syslog(LOG_NOTICE|LOG_AUTH,
340 "Connection from %s on illegal port %u",
341 naddr, ntohs(*portp));
342 exit(EXIT_FAILURE);
343 }
344
345 (void) alarm(60);
346 port = 0;
347 for (;;) {
348 char c;
349
350 if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
351 if (cc < 0)
352 syslog(LOG_ERR, "read: %m");
353 (void)shutdown(0, SHUT_RDWR);
354 exit(EXIT_FAILURE);
355 }
356 if (c == 0)
357 break;
358 port = port * 10 + c - '0';
359 }
360
361 (void) alarm(0);
362 if (port != 0) {
363 int lport = IPPORT_RESERVED - 1;
364 s = rresvport_af(&lport, af);
365 if (s < 0) {
366 syslog(LOG_ERR, "can't get stderr port: %m");
367 exit(EXIT_FAILURE);
368 }
369 if (port >= IPPORT_RESERVED) {
370 syslog(LOG_ERR, "2nd port not reserved");
371 exit(EXIT_FAILURE);
372 }
373 *portp = htons(port);
374 if (connect(s, fromp, fromp->sa_len) < 0) {
375 syslog(LOG_ERR, "connect second port %d: %m", port);
376 exit(EXIT_FAILURE);
377 }
378 }
379
380
381 #ifdef notdef
382 /* from inetd, socket is already on 0, 1, 2 */
383 (void)dup2(f, STDIN_FILENO);
384 (void)dup2(f, STDOUT_FILENO);
385 (void)dup2(f, STDERR_FILENO);
386 #endif
387 if (getnameinfo(fromp, fromp->sa_len, saddr, sizeof(saddr),
388 NULL, 0, NI_NAMEREQD) == 0) {
389 /*
390 * If name returned by getnameinfo is in our domain,
391 * attempt to verify that we haven't been fooled by someone
392 * in a remote net; look up the name and check that this
393 * address corresponds to the name.
394 */
395 hostname = saddr;
396 res0 = NULL;
397 if (check_all || local_domain(saddr)) {
398 (void)strlcpy(remotehost, saddr, sizeof(remotehost));
399 errorhost = remotehost;
400 (void)memset(&hints, 0, sizeof(hints));
401 hints.ai_family = fromp->sa_family;
402 hints.ai_socktype = SOCK_STREAM;
403 hints.ai_flags = AI_CANONNAME;
404 gaierror = getaddrinfo(remotehost, pbuf, &hints, &res0);
405 if (gaierror) {
406 syslog(LOG_NOTICE,
407 "Couldn't look up address for %s: %s",
408 remotehost, gai_strerror(gaierror));
409 errorstr =
410 "Couldn't look up address for your host (%s)\n";
411 hostname = naddr;
412 } else {
413 for (res = res0; res; res = res->ai_next) {
414 if (res->ai_family != fromp->sa_family)
415 continue;
416 if (res->ai_addrlen != fromp->sa_len)
417 continue;
418 if (getnameinfo(res->ai_addr,
419 res->ai_addrlen,
420 raddr, sizeof(raddr), NULL, 0,
421 niflags) == 0
422 && strcmp(naddr, raddr) == 0) {
423 hostname = res->ai_canonname
424 ? res->ai_canonname
425 : saddr;
426 break;
427 }
428 }
429 if (res == NULL) {
430 syslog(LOG_NOTICE,
431 "Host addr %s not listed for host %s",
432 naddr, res0->ai_canonname
433 ? res0->ai_canonname
434 : saddr);
435 errorstr =
436 "Host address mismatch for %s\n";
437 hostname = naddr;
438 }
439 }
440 }
441 (void)strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf));
442 hostname = hostnamebuf;
443 if (res0)
444 freeaddrinfo(res0);
445 } else {
446 (void)strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf));
447 errorhost = hostname = hostnamebuf;
448 }
449
450 (void)alarm(60);
451 getstr(remuser, sizeof(remuser), "remuser");
452 getstr(locuser, sizeof(locuser), "locuser");
453 getstr(cmdbuf, sizeof(cmdbuf), "command");
454 (void)alarm(0);
455
456 #ifdef USE_PAM
457 pam_err = pam_start("rsh", locuser, &pamc, &pamh);
458 if (pam_err != PAM_SUCCESS) {
459 syslog(LOG_ERR|LOG_AUTH, "pam_start(): %s",
460 pam_strerror(pamh, pam_err));
461 rshd_errx(EXIT_FAILURE, incorrect);
462 }
463
464 if ((pam_err = pam_set_item(pamh, PAM_RUSER, remuser)) != PAM_SUCCESS ||
465 (pam_err = pam_set_item(pamh, PAM_RHOST, hostname) != PAM_SUCCESS)){
466 syslog(LOG_ERR|LOG_AUTH, "pam_set_item(): %s",
467 pam_strerror(pamh, pam_err));
468 rshd_errx(EXIT_FAILURE, incorrect);
469 }
470
471 pam_err = pam_authenticate(pamh, 0);
472 if (pam_err == PAM_SUCCESS) {
473 if ((pam_err = pam_get_user(pamh, &cp, NULL)) == PAM_SUCCESS) {
474 (void)strlcpy(locuser, cp, sizeof(locuser));
475 /* XXX truncation! */
476 }
477 pam_err = pam_acct_mgmt(pamh, 0);
478 }
479 if (pam_err != PAM_SUCCESS) {
480 errorstr = incorrect;
481 errormsg = pam_strerror(pamh, pam_err);
482 goto badlogin;
483 }
484 #endif /* USE_PAM */
485 setpwent();
486 if (getpwnam_r(locuser, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
487 pwd == NULL) {
488 syslog(LOG_INFO|LOG_AUTH,
489 "%s@%s as %s: unknown login. cmd='%.80s'",
490 remuser, hostname, locuser, cmdbuf);
491 if (errorstr == NULL)
492 errorstr = "Permission denied.";
493 rshd_errx(EXIT_FAILURE, errorstr, errorhost);
494 }
495 #ifdef LOGIN_CAP
496 lc = login_getclass(pwd ? pwd->pw_class : NULL);
497 #endif
498
499 if (chdir(pwd->pw_dir) < 0) {
500 if (chdir("/") < 0
501 #ifdef LOGIN_CAP
502 || login_getcapbool(lc, "requirehome", pwd->pw_uid ? 1 : 0)
503 #endif
504 ) {
505 syslog(LOG_INFO|LOG_AUTH,
506 "%s@%s as %s: no home directory. cmd='%.80s'",
507 remuser, hostname, locuser, cmdbuf);
508 rshd_errx(EXIT_SUCCESS, "No remote home directory.");
509 }
510 }
511
512 #ifndef USE_PAM
513 if (errorstr ||
514 (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
515 iruserok_sa(fromp, fromp->sa_len, pwd->pw_uid == 0, remuser,
516 locuser) < 0)) {
517 errormsg = __rcmd_errstr ? __rcmd_errstr : "unknown error";
518 if (errorstr == NULL)
519 errorstr = "Permission denied.";
520 goto badlogin;
521 }
522
523 if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK))
524 rshd_errx(EXIT_FAILURE, "Logins currently disabled.");
525 #endif
526
527 #ifdef LOGIN_CAP
528 /*
529 * PAM modules might add supplementary groups in
530 * pam_setcred(), so initialize them first.
531 * But we need to open the session as root.
532 */
533 if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETGROUP) != 0) {
534 syslog(LOG_ERR, "setusercontext: %m");
535 exit(EXIT_FAILURE);
536 }
537 #else
538 initgroups(pwd->pw_name, pwd->pw_gid);
539 #endif
540
541 #ifdef USE_PAM
542 if ((pam_err = pam_open_session(pamh, 0)) != PAM_SUCCESS) {
543 syslog(LOG_ERR, "pam_open_session: %s",
544 pam_strerror(pamh, pam_err));
545 } else if ((pam_err = pam_setcred(pamh, PAM_ESTABLISH_CRED))
546 != PAM_SUCCESS) {
547 syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, pam_err));
548 }
549 #endif
550
551 (void)write(STDERR_FILENO, "\0", 1);
552 sent_null = 1;
553
554 if (port) {
555 if (pipe(pv) < 0)
556 rshd_errx(EXIT_FAILURE, "Can't make pipe. (%s)",
557 strerror(errno));
558 pid = fork();
559 if (pid == -1)
560 rshd_errx(EXIT_FAILURE, "Can't fork. (%s)",
561 strerror(errno));
562 if (pid) {
563 (void)close(STDIN_FILENO);
564 (void)close(STDOUT_FILENO);
565 (void)close(STDERR_FILENO);
566 (void)close(pv[1]);
567
568 set[0].fd = s;
569 set[0].events = POLLIN;
570 set[1].fd = pv[0];
571 set[1].events = POLLIN;
572 ioctl(pv[0], FIONBIO, (char *)&one);
573
574 /* should set s nbio! */
575 do {
576 if (poll(set, 2, INFTIM) < 0)
577 break;
578 if (set[0].revents & POLLIN) {
579 int ret;
580
581 ret = read(s, &sig, 1);
582 if (ret <= 0)
583 set[0].events = 0;
584 else
585 killpg(pid, sig);
586 }
587 if (set[1].revents & POLLIN) {
588 errno = 0;
589 cc = read(pv[0], buf, sizeof(buf));
590 if (cc <= 0) {
591 shutdown(s, SHUT_RDWR);
592 set[1].events = 0;
593 } else {
594 (void)write(s, buf, cc);
595 }
596 }
597
598 } while ((set[0].revents | set[1].revents) & POLLIN);
599 PAM_END;
600 exit(EXIT_SUCCESS);
601 }
602 (void)close(s);
603 (void)close(pv[0]);
604 (void)dup2(pv[1], STDERR_FILENO);
605 close(pv[1]);
606 }
607 #ifdef USE_PAM
608 else {
609 pid = fork();
610 if (pid == -1)
611 rshd_errx(EXIT_FAILURE, "Can't fork. (%s)",
612 strerror(errno));
613 if (pid) {
614 pid_t xpid;
615 int status;
616 if ((xpid = waitpid(pid, &status, 0)) != pid) {
617 pam_err = pam_close_session(pamh, 0);
618 if (pam_err != PAM_SUCCESS) {
619 syslog(LOG_ERR,
620 "pam_close_session: %s",
621 pam_strerror(pamh, pam_err));
622 }
623 PAM_END;
624 if (xpid != -1)
625 syslog(LOG_WARNING,
626 "wrong PID: %d != %d", pid, xpid);
627 else
628 syslog(LOG_WARNING,
629 "wait pid=%d failed %m", pid);
630 exit(EXIT_FAILURE);
631 }
632 exit(EXIT_SUCCESS);
633 }
634 }
635 #endif
636
637 #ifdef F_CLOSEM
638 (void)fcntl(STDERR_FILENO + 1, F_CLOSEM, 0);
639 #else
640 for (fd = getdtablesize(); fd > STDERR_FILENO; fd--)
641 (void)close(fd);
642 #endif
643 if (setsid() == -1)
644 syslog(LOG_ERR, "setsid() failed: %m");
645 #ifdef USE_PAM
646 if (setlogin(pwd->pw_name) < 0)
647 syslog(LOG_ERR, "setlogin() failed: %m");
648
649 if (*pwd->pw_shell == '\0')
650 pwd->pw_shell = __UNCONST(_PATH_BSHELL);
651
652 (void)pam_setenv(pamh, "HOME", pwd->pw_dir, 1);
653 (void)pam_setenv(pamh, "SHELL", pwd->pw_shell, 1);
654 (void)pam_setenv(pamh, "USER", pwd->pw_name, 1);
655 (void)pam_setenv(pamh, "PATH", _PATH_DEFPATH, 1);
656 environ = pam_getenvlist(pamh);
657 (void)pam_end(pamh, pam_err);
658 #else
659 #ifdef LOGIN_CAP
660 {
661 char *sh;
662 if ((sh = login_getcapstr(lc, "shell", NULL, NULL))) {
663 if(!(sh = strdup(sh))) {
664 syslog(LOG_ERR, "Cannot alloc mem");
665 exit(EXIT_FAILURE);
666 }
667 pwd->pw_shell = sh;
668 }
669 }
670 #endif
671 environ = envinit;
672 (void)strlcat(homedir, pwd->pw_dir, sizeof(homedir));
673 (void)strlcat(path, _PATH_DEFPATH, sizeof(path));
674 (void)strlcat(shell, pwd->pw_shell, sizeof(shell));
675 (void)strlcat(username, pwd->pw_name, sizeof(username));
676 #endif
677
678 cp = strrchr(pwd->pw_shell, '/');
679 if (cp)
680 cp++;
681 else
682 cp = pwd->pw_shell;
683
684 #ifdef LOGIN_CAP
685 if (setusercontext(lc, pwd, pwd->pw_uid,
686 LOGIN_SETALL & ~LOGIN_SETGROUP) < 0) {
687 syslog(LOG_ERR, "setusercontext(): %m");
688 exit(EXIT_FAILURE);
689 }
690 login_close(lc);
691 #else
692 (void)setgid((gid_t)pwd->pw_gid);
693 (void)setuid((uid_t)pwd->pw_uid);
694 #endif
695 endpwent();
696 if (log_success || pwd->pw_uid == 0) {
697 syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
698 remuser, hostname, locuser, cmdbuf);
699 }
700 (void)execl(pwd->pw_shell, cp, "-c", cmdbuf, NULL);
701 rshd_errx(EXIT_FAILURE, "%s: %s", pwd->pw_shell, strerror(errno));
702 badlogin:
703 syslog(LOG_INFO|LOG_AUTH,
704 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
705 remuser, hostname, locuser, errormsg, cmdbuf);
706 rshd_errx(EXIT_FAILURE, errorstr, errorhost);
707 }
708
709 /*
710 * Report error to client. Note: can't be used until second socket has
711 * connected to client, or older clients will hang waiting for that
712 * connection first.
713 */
714
715 #include <stdarg.h>
716
717 void
718 rshd_errx(int error, const char *fmt, ...)
719 {
720 va_list ap;
721 int len, rv;
722 char *bp, buf[BUFSIZ];
723 va_start(ap, fmt);
724 bp = buf;
725 if (sent_null == 0) {
726 *bp++ = 1;
727 len = 1;
728 } else
729 len = 0;
730 rv = vsnprintf(bp, sizeof(buf) - 2, fmt, ap);
731 bp[rv++] = '\n';
732 (void)write(STDERR_FILENO, buf, len + rv);
733 va_end(ap);
734 exit(error);
735 }
736
737 void
738 getstr(char *buf, int cnt, const char *err)
739 {
740 char c;
741
742 do {
743 if (read(STDIN_FILENO, &c, 1) != 1)
744 exit(EXIT_FAILURE);
745 *buf++ = c;
746 if (--cnt == 0)
747 rshd_errx(EXIT_FAILURE, "%s too long", err);
748 } while (c != 0);
749 }
750
751 /*
752 * Check whether host h is in our local domain,
753 * defined as sharing the last two components of the domain part,
754 * or the entire domain part if the local domain has only one component.
755 * If either name is unqualified (contains no '.'),
756 * assume that the host is local, as it will be
757 * interpreted as such.
758 */
759 int
760 local_domain(char *h)
761 {
762 char localhost[MAXHOSTNAMELEN + 1];
763 char *p1, *p2;
764
765 localhost[0] = 0;
766 (void)gethostname(localhost, sizeof(localhost));
767 localhost[sizeof(localhost) - 1] = '\0';
768 p1 = topdomain(localhost);
769 p2 = topdomain(h);
770 if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
771 return (1);
772 return (0);
773 }
774
775 char *
776 topdomain(char *h)
777 {
778 char *p, *maybe = NULL;
779 int dots = 0;
780
781 for (p = h + strlen(h); p >= h; p--) {
782 if (*p == '.') {
783 if (++dots == 2)
784 return (p);
785 maybe = p;
786 }
787 }
788 return (maybe);
789 }
790
791 void
792 usage(void)
793 {
794
795 syslog(LOG_ERR, "Usage: %s [-%s]", getprogname(), OPTIONS);
796 exit(EXIT_FAILURE);
797 }
798