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