rlogind.c revision 1.41 1 1.41 plunky /* $NetBSD: rlogind.c,v 1.41 2011/09/16 16:13:17 plunky Exp $ */
2 1.20 itojun
3 1.20 itojun /*
4 1.20 itojun * Copyright (C) 1998 WIDE Project.
5 1.20 itojun * All rights reserved.
6 1.20 itojun *
7 1.20 itojun * Redistribution and use in source and binary forms, with or without
8 1.20 itojun * modification, are permitted provided that the following conditions
9 1.20 itojun * are met:
10 1.20 itojun * 1. Redistributions of source code must retain the above copyright
11 1.20 itojun * notice, this list of conditions and the following disclaimer.
12 1.20 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.20 itojun * notice, this list of conditions and the following disclaimer in the
14 1.20 itojun * documentation and/or other materials provided with the distribution.
15 1.20 itojun * 3. All advertising materials mentioning features or use of this software
16 1.20 itojun * must display the following acknowledgement:
17 1.20 itojun * This product includes software developed by WIDE Project and
18 1.20 itojun * its contributors.
19 1.20 itojun * 4. Neither the name of the project nor the names of its contributors
20 1.20 itojun * may be used to endorse or promote products derived from this software
21 1.20 itojun * without specific prior written permission.
22 1.20 itojun *
23 1.20 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 1.20 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.20 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.20 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 1.20 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.20 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.20 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.20 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.20 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.20 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.20 itojun * SUCH DAMAGE.
34 1.20 itojun */
35 1.10 mrg
36 1.1 cgd /*-
37 1.5 cgd * Copyright (c) 1983, 1988, 1989, 1993
38 1.5 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.38 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1989, 1993\
68 1.38 lukem The Regents of the University of California. All rights reserved.");
69 1.10 mrg #if 0
70 1.10 mrg static char sccsid[] = "@(#)rlogind.c 8.2 (Berkeley) 4/28/95";
71 1.10 mrg #else
72 1.41 plunky __RCSID("$NetBSD: rlogind.c,v 1.41 2011/09/16 16:13:17 plunky 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 login server:
78 1.1 cgd * \0
79 1.1 cgd * remuser\0
80 1.1 cgd * locuser\0
81 1.1 cgd * terminal_type/speed\0
82 1.1 cgd * data
83 1.1 cgd */
84 1.1 cgd
85 1.1 cgd #include <sys/param.h>
86 1.1 cgd #include <sys/stat.h>
87 1.1 cgd #include <sys/ioctl.h>
88 1.1 cgd #include <signal.h>
89 1.1 cgd #include <termios.h>
90 1.29 itojun #include <poll.h>
91 1.34 christos #include <vis.h>
92 1.1 cgd
93 1.1 cgd #include <sys/socket.h>
94 1.1 cgd #include <netinet/in.h>
95 1.1 cgd #include <netinet/in_systm.h>
96 1.1 cgd #include <netinet/ip.h>
97 1.1 cgd #include <arpa/inet.h>
98 1.1 cgd #include <netdb.h>
99 1.1 cgd
100 1.1 cgd #include <pwd.h>
101 1.1 cgd #include <syslog.h>
102 1.1 cgd #include <errno.h>
103 1.1 cgd #include <stdio.h>
104 1.1 cgd #include <unistd.h>
105 1.1 cgd #include <stdlib.h>
106 1.1 cgd #include <string.h>
107 1.9 lukem #include <util.h>
108 1.1 cgd #include "pathnames.h"
109 1.1 cgd
110 1.1 cgd #ifndef TIOCPKT_WINDOW
111 1.1 cgd #define TIOCPKT_WINDOW 0x80
112 1.1 cgd #endif
113 1.1 cgd
114 1.9 lukem #define OPTIONS "alnL"
115 1.1 cgd
116 1.40 joerg static char *env[2];
117 1.1 cgd #define NMAX 30
118 1.40 joerg static char lusername[NMAX+1], rusername[NMAX+1];
119 1.1 cgd static char term[64] = "TERM=";
120 1.1 cgd #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */
121 1.40 joerg static int keepalive = 1;
122 1.40 joerg static int check_all = 0;
123 1.40 joerg static int log_success = 0;
124 1.40 joerg
125 1.40 joerg static struct passwd *pwd;
126 1.40 joerg
127 1.40 joerg __dead static void doit(int, struct sockaddr_storage *);
128 1.40 joerg static int control(int, char *, int);
129 1.40 joerg static void protocol(int, int);
130 1.40 joerg __dead static void cleanup(int);
131 1.40 joerg __dead static void fatal(int, const char *, int);
132 1.40 joerg static int do_rlogin(struct sockaddr *, char *);
133 1.40 joerg static void getstr(char *, int, const char *);
134 1.40 joerg static void setup_term(int);
135 1.20 itojun #if 0
136 1.40 joerg static int do_krb_login(union sockunion *);
137 1.20 itojun #endif
138 1.40 joerg __dead static void usage(void);
139 1.40 joerg static int local_domain(char *);
140 1.40 joerg static char *topdomain(char *);
141 1.5 cgd
142 1.24 christos extern int __check_rhosts_file;
143 1.24 christos extern char *__rcmd_errstr; /* syslog hook from libc/net/rcmd.c */
144 1.24 christos extern char **environ;
145 1.24 christos
146 1.5 cgd int
147 1.40 joerg main(int argc, char *argv[])
148 1.1 cgd {
149 1.20 itojun struct sockaddr_storage from;
150 1.34 christos int ch, on;
151 1.34 christos socklen_t fromlen = sizeof(from);
152 1.1 cgd
153 1.14 mrg openlog("rlogind", LOG_PID, LOG_AUTH);
154 1.1 cgd
155 1.1 cgd opterr = 0;
156 1.12 enami while ((ch = getopt(argc, argv, OPTIONS)) != -1)
157 1.1 cgd switch (ch) {
158 1.1 cgd case 'a':
159 1.1 cgd check_all = 1;
160 1.1 cgd break;
161 1.1 cgd case 'l':
162 1.3 pk __check_rhosts_file = 0;
163 1.1 cgd break;
164 1.1 cgd case 'n':
165 1.1 cgd keepalive = 0;
166 1.1 cgd break;
167 1.9 lukem case 'L':
168 1.9 lukem log_success = 1;
169 1.9 lukem break;
170 1.1 cgd case '?':
171 1.1 cgd default:
172 1.1 cgd usage();
173 1.1 cgd break;
174 1.1 cgd }
175 1.1 cgd argc -= optind;
176 1.1 cgd argv += optind;
177 1.1 cgd
178 1.1 cgd if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
179 1.1 cgd syslog(LOG_ERR,"Can't get peer name of remote host: %m");
180 1.1 cgd fatal(STDERR_FILENO, "Can't get peer name of remote host", 1);
181 1.1 cgd }
182 1.21 itojun #ifdef INET6
183 1.34 christos if (from.ss_family == AF_INET6 &&
184 1.21 itojun IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr) &&
185 1.21 itojun sizeof(struct sockaddr_in) <= sizeof(from)) {
186 1.34 christos struct sockaddr_in sin4;
187 1.21 itojun struct sockaddr_in6 *sin6;
188 1.21 itojun const int off = sizeof(struct sockaddr_in6) -
189 1.21 itojun sizeof(struct sockaddr_in);
190 1.21 itojun
191 1.21 itojun sin6 = (struct sockaddr_in6 *)&from;
192 1.34 christos memset(&sin4, 0, sizeof(sin4));
193 1.34 christos sin4.sin_family = AF_INET;
194 1.34 christos sin4.sin_len = sizeof(struct sockaddr_in);
195 1.34 christos memcpy(&sin4.sin_addr, &sin6->sin6_addr.s6_addr[off],
196 1.34 christos sizeof(sin4.sin_addr));
197 1.34 christos memcpy(&from, &sin4, sizeof(sin4));
198 1.21 itojun }
199 1.21 itojun #else
200 1.34 christos if (from.ss_family == AF_INET6 &&
201 1.21 itojun IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr)) {
202 1.21 itojun char hbuf[NI_MAXHOST];
203 1.21 itojun if (getnameinfo((struct sockaddr *)&from, fromlen, hbuf,
204 1.21 itojun sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
205 1.31 itojun strlcpy(hbuf, "invalid", sizeof(hbuf));
206 1.21 itojun }
207 1.21 itojun syslog(LOG_ERR, "malformed \"from\" address (v4 mapped, %s)\n",
208 1.21 itojun hbuf);
209 1.21 itojun exit(1);
210 1.21 itojun }
211 1.21 itojun #endif
212 1.5 cgd on = 1;
213 1.1 cgd if (keepalive &&
214 1.1 cgd setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0)
215 1.1 cgd syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
216 1.20 itojun #if defined(IP_TOS)
217 1.34 christos if (from.ss_family == AF_INET) {
218 1.20 itojun on = IPTOS_LOWDELAY;
219 1.20 itojun if (setsockopt(0, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
220 1.20 itojun syslog(LOG_WARNING, "setsockopt (IP_TOS): %m");
221 1.20 itojun }
222 1.20 itojun #endif
223 1.34 christos doit(0, &from);
224 1.9 lukem /* NOTREACHED */
225 1.11 mrg #ifdef __GNUC__
226 1.11 mrg exit(0);
227 1.11 mrg #endif
228 1.1 cgd }
229 1.1 cgd
230 1.40 joerg static int netf;
231 1.40 joerg static char line[MAXPATHLEN];
232 1.40 joerg static int confirmed;
233 1.1 cgd
234 1.40 joerg static struct winsize win = { 0, 0, 0, 0 };
235 1.1 cgd
236 1.40 joerg static void
237 1.40 joerg doit(int f, struct sockaddr_storage *fromp)
238 1.1 cgd {
239 1.5 cgd int master, pid, on = 1;
240 1.5 cgd int authenticated = 0;
241 1.9 lukem char *hostname;
242 1.9 lukem char hostnamebuf[2 * MAXHOSTNAMELEN + 1];
243 1.34 christos char hostaddrbuf[sizeof(*fromp) * 4 + 1];
244 1.1 cgd char c;
245 1.20 itojun char naddr[NI_MAXHOST];
246 1.20 itojun char saddr[NI_MAXHOST];
247 1.20 itojun char raddr[NI_MAXHOST];
248 1.34 christos int af = fromp->ss_family;
249 1.20 itojun u_int16_t *portp;
250 1.20 itojun struct addrinfo hints, *res, *res0;
251 1.20 itojun int gaierror;
252 1.34 christos socklen_t fromlen = fromp->ss_len > sizeof(*fromp)
253 1.34 christos ? sizeof(*fromp) : fromp->ss_len;
254 1.20 itojun const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
255 1.1 cgd
256 1.1 cgd alarm(60);
257 1.1 cgd read(f, &c, 1);
258 1.1 cgd
259 1.1 cgd if (c != 0)
260 1.1 cgd exit(1);
261 1.1 cgd
262 1.1 cgd alarm(0);
263 1.20 itojun switch (af) {
264 1.20 itojun case AF_INET:
265 1.20 itojun portp = &((struct sockaddr_in *)fromp)->sin_port;
266 1.20 itojun break;
267 1.20 itojun #ifdef INET6
268 1.20 itojun case AF_INET6:
269 1.20 itojun portp = &((struct sockaddr_in6 *)fromp)->sin6_port;
270 1.20 itojun break;
271 1.20 itojun #endif
272 1.20 itojun default:
273 1.20 itojun syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
274 1.20 itojun exit(1);
275 1.20 itojun }
276 1.34 christos if (getnameinfo((struct sockaddr *)fromp, fromlen,
277 1.20 itojun naddr, sizeof(naddr), NULL, 0, niflags) != 0) {
278 1.20 itojun syslog(LOG_ERR, "malformed \"from\" address (af %d)\n", af);
279 1.20 itojun exit(1);
280 1.20 itojun }
281 1.20 itojun
282 1.34 christos if (getnameinfo((struct sockaddr *)fromp, fromlen,
283 1.20 itojun saddr, sizeof(saddr), NULL, 0, NI_NAMEREQD) == 0) {
284 1.9 lukem /*
285 1.33 wiz * If name returned by getnameinfo is in our domain,
286 1.9 lukem * attempt to verify that we haven't been fooled by someone
287 1.9 lukem * in a remote net; look up the name and check that this
288 1.9 lukem * address corresponds to the name.
289 1.9 lukem */
290 1.20 itojun hostname = saddr;
291 1.22 itojun res0 = NULL;
292 1.20 itojun if (check_all || local_domain(saddr)) {
293 1.31 itojun strlcpy(hostnamebuf, saddr, sizeof(hostnamebuf));
294 1.20 itojun memset(&hints, 0, sizeof(hints));
295 1.34 christos hints.ai_family = fromp->ss_family;
296 1.20 itojun hints.ai_socktype = SOCK_STREAM;
297 1.20 itojun hints.ai_flags = AI_CANONNAME;
298 1.20 itojun gaierror = getaddrinfo(hostnamebuf, "0", &hints, &res0);
299 1.20 itojun if (gaierror) {
300 1.23 lukem syslog(LOG_NOTICE,
301 1.20 itojun "Couldn't look up address for %s: %s",
302 1.20 itojun hostnamebuf, gai_strerror(gaierror));
303 1.20 itojun hostname = naddr;
304 1.20 itojun } else {
305 1.20 itojun for (res = res0; res; res = res->ai_next) {
306 1.34 christos if (res->ai_family != fromp->ss_family)
307 1.20 itojun continue;
308 1.34 christos if (res->ai_addrlen != fromp->ss_len)
309 1.20 itojun continue;
310 1.20 itojun if (getnameinfo(res->ai_addr,
311 1.20 itojun res->ai_addrlen,
312 1.20 itojun raddr, sizeof(raddr), NULL, 0,
313 1.20 itojun niflags) == 0
314 1.20 itojun && strcmp(naddr, raddr) == 0) {
315 1.20 itojun hostname = res->ai_canonname
316 1.20 itojun ? res->ai_canonname
317 1.20 itojun : saddr;
318 1.20 itojun break;
319 1.20 itojun }
320 1.20 itojun }
321 1.20 itojun if (res == NULL) {
322 1.9 lukem syslog(LOG_NOTICE,
323 1.9 lukem "Host addr %s not listed for host %s",
324 1.20 itojun naddr, res0->ai_canonname
325 1.20 itojun ? res0->ai_canonname
326 1.20 itojun : saddr);
327 1.20 itojun hostname = naddr;
328 1.9 lukem }
329 1.9 lukem }
330 1.9 lukem }
331 1.31 itojun strlcpy(hostnamebuf, hostname, sizeof(hostnamebuf));
332 1.31 itojun hostname = hostnamebuf;
333 1.22 itojun if (res0)
334 1.22 itojun freeaddrinfo(res0);
335 1.31 itojun } else {
336 1.31 itojun strlcpy(hostnamebuf, naddr, sizeof(hostnamebuf));
337 1.31 itojun hostname = hostnamebuf;
338 1.31 itojun }
339 1.9 lukem
340 1.20 itojun if (ntohs(*portp) >= IPPORT_RESERVED ||
341 1.20 itojun ntohs(*portp) < IPPORT_RESERVED/2) {
342 1.9 lukem syslog(LOG_NOTICE, "Connection from %s on illegal port",
343 1.20 itojun naddr);
344 1.9 lukem fatal(f, "Permission denied", 0);
345 1.9 lukem }
346 1.9 lukem #ifdef IP_OPTIONS
347 1.34 christos if (fromp->ss_family == AF_INET) {
348 1.5 cgd u_char optbuf[BUFSIZ/3], *cp;
349 1.31 itojun char lbuf[BUFSIZ], *lp, *ep;
350 1.37 mrg socklen_t optsize = sizeof(optbuf);
351 1.37 mrg int ipproto;
352 1.5 cgd struct protoent *ip;
353 1.5 cgd
354 1.5 cgd if ((ip = getprotobyname("ip")) != NULL)
355 1.5 cgd ipproto = ip->p_proto;
356 1.5 cgd else
357 1.5 cgd ipproto = IPPROTO_IP;
358 1.5 cgd if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf,
359 1.5 cgd &optsize) == 0 && optsize != 0) {
360 1.5 cgd lp = lbuf;
361 1.31 itojun ep = lbuf + sizeof(lbuf);
362 1.5 cgd for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
363 1.31 itojun snprintf(lp, ep - lp, " %2.2x", *cp);
364 1.5 cgd syslog(LOG_NOTICE,
365 1.5 cgd "Connection received using IP options (ignored):%s",
366 1.5 cgd lbuf);
367 1.5 cgd if (setsockopt(0, ipproto, IP_OPTIONS,
368 1.41 plunky NULL, optsize) != 0) {
369 1.5 cgd syslog(LOG_ERR,
370 1.5 cgd "setsockopt IP_OPTIONS NULL: %m");
371 1.5 cgd exit(1);
372 1.5 cgd }
373 1.5 cgd }
374 1.9 lukem }
375 1.1 cgd #endif
376 1.34 christos if (do_rlogin((struct sockaddr *)fromp, hostname) == 0)
377 1.9 lukem authenticated++;
378 1.1 cgd if (confirmed == 0) {
379 1.1 cgd write(f, "", 1);
380 1.1 cgd confirmed = 1; /* we sent the null! */
381 1.1 cgd }
382 1.1 cgd netf = f;
383 1.1 cgd
384 1.1 cgd pid = forkpty(&master, line, NULL, &win);
385 1.1 cgd if (pid < 0) {
386 1.1 cgd if (errno == ENOENT)
387 1.1 cgd fatal(f, "Out of ptys", 0);
388 1.1 cgd else
389 1.1 cgd fatal(f, "Forkpty", 1);
390 1.1 cgd }
391 1.1 cgd if (pid == 0) {
392 1.5 cgd if (f > 2) /* f should always be 0, but... */
393 1.1 cgd (void) close(f);
394 1.1 cgd setup_term(0);
395 1.34 christos (void)strvisx(hostaddrbuf, (const char *)(const void *)fromp,
396 1.35 christos sizeof(*fromp), VIS_WHITE);
397 1.7 mycroft if (authenticated)
398 1.1 cgd execl(_PATH_LOGIN, "login", "-p",
399 1.34 christos "-h", hostname, "-a", hostaddrbuf,
400 1.34 christos "-f", "--", lusername, (char *)0);
401 1.7 mycroft else
402 1.1 cgd execl(_PATH_LOGIN, "login", "-p",
403 1.34 christos "-h", hostname, "-a", hostaddrbuf,
404 1.34 christos "--", lusername, (char *)0);
405 1.1 cgd fatal(STDERR_FILENO, _PATH_LOGIN, 1);
406 1.1 cgd /*NOTREACHED*/
407 1.1 cgd }
408 1.19 itojun ioctl(f, FIONBIO, &on);
409 1.1 cgd ioctl(master, FIONBIO, &on);
410 1.1 cgd ioctl(master, TIOCPKT, &on);
411 1.1 cgd signal(SIGCHLD, cleanup);
412 1.1 cgd protocol(f, master);
413 1.1 cgd signal(SIGCHLD, SIG_IGN);
414 1.5 cgd cleanup(0);
415 1.1 cgd }
416 1.1 cgd
417 1.40 joerg static char magic[2] = { 0377, 0377 };
418 1.40 joerg static char oobdata[] = {TIOCPKT_WINDOW};
419 1.1 cgd
420 1.1 cgd /*
421 1.1 cgd * Handle a "control" request (signaled by magic being present)
422 1.1 cgd * in the data stream. For now, we are only willing to handle
423 1.1 cgd * window size changes.
424 1.1 cgd */
425 1.40 joerg static int
426 1.40 joerg control(int pty, char *cp, int n)
427 1.1 cgd {
428 1.1 cgd struct winsize w;
429 1.1 cgd
430 1.39 lukem if (n < (int)(4+sizeof (w)) || cp[2] != 's' || cp[3] != 's')
431 1.1 cgd return (0);
432 1.1 cgd oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */
433 1.10 mrg memmove(&w, cp+4, sizeof(w));
434 1.1 cgd w.ws_row = ntohs(w.ws_row);
435 1.1 cgd w.ws_col = ntohs(w.ws_col);
436 1.1 cgd w.ws_xpixel = ntohs(w.ws_xpixel);
437 1.1 cgd w.ws_ypixel = ntohs(w.ws_ypixel);
438 1.1 cgd (void)ioctl(pty, TIOCSWINSZ, &w);
439 1.1 cgd return (4+sizeof (w));
440 1.1 cgd }
441 1.1 cgd
442 1.1 cgd /*
443 1.1 cgd * rlogin "protocol" machine.
444 1.1 cgd */
445 1.40 joerg static void
446 1.40 joerg protocol(int f, int p)
447 1.1 cgd {
448 1.11 mrg char pibuf[1024+1], fibuf[1024], *pbp = NULL, *fbp = NULL;
449 1.11 mrg /* XXX gcc above */
450 1.13 mrg int pcc = 0, fcc = 0;
451 1.34 christos int cc, nfd;
452 1.1 cgd char cntl;
453 1.28 mycroft struct pollfd set[2];
454 1.1 cgd
455 1.1 cgd /*
456 1.1 cgd * Must ignore SIGTTOU, otherwise we'll stop
457 1.1 cgd * when we try and set slave pty's window shape
458 1.1 cgd * (our controlling tty is the master pty).
459 1.1 cgd */
460 1.1 cgd (void) signal(SIGTTOU, SIG_IGN);
461 1.1 cgd send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */
462 1.28 mycroft set[0].fd = p;
463 1.28 mycroft set[1].fd = f;
464 1.1 cgd for (;;) {
465 1.27 mycroft set[0].events = POLLPRI;
466 1.27 mycroft set[1].events = 0;
467 1.27 mycroft if (fcc)
468 1.27 mycroft set[0].events |= POLLOUT;
469 1.27 mycroft else
470 1.27 mycroft set[1].events |= POLLIN;
471 1.16 ross if (pcc >= 0) {
472 1.27 mycroft if (pcc)
473 1.27 mycroft set[1].events |= POLLOUT;
474 1.27 mycroft else
475 1.27 mycroft set[0].events |= POLLIN;
476 1.16 ross }
477 1.34 christos if ((nfd = poll(set, 2, INFTIM)) < 0) {
478 1.1 cgd if (errno == EINTR)
479 1.1 cgd continue;
480 1.27 mycroft fatal(f, "poll", 1);
481 1.1 cgd }
482 1.34 christos if (nfd == 0) {
483 1.1 cgd /* shouldn't happen... */
484 1.1 cgd sleep(5);
485 1.1 cgd continue;
486 1.1 cgd }
487 1.1 cgd #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP))
488 1.27 mycroft if (set[0].revents & POLLPRI) {
489 1.1 cgd cc = read(p, &cntl, 1);
490 1.1 cgd if (cc == 1 && pkcontrol(cntl)) {
491 1.1 cgd cntl |= oobdata[0];
492 1.1 cgd send(f, &cntl, 1, MSG_OOB);
493 1.27 mycroft if (cntl & TIOCPKT_FLUSHWRITE)
494 1.1 cgd pcc = 0;
495 1.1 cgd }
496 1.1 cgd }
497 1.27 mycroft if (set[1].revents & POLLIN) {
498 1.27 mycroft fcc = read(f, fibuf, sizeof(fibuf));
499 1.1 cgd if (fcc < 0 && errno == EWOULDBLOCK)
500 1.1 cgd fcc = 0;
501 1.1 cgd else {
502 1.13 mrg char *cp;
503 1.1 cgd int left, n;
504 1.1 cgd
505 1.1 cgd if (fcc <= 0)
506 1.1 cgd break;
507 1.1 cgd fbp = fibuf;
508 1.1 cgd
509 1.1 cgd top:
510 1.1 cgd for (cp = fibuf; cp < fibuf+fcc-1; cp++)
511 1.1 cgd if (cp[0] == magic[0] &&
512 1.1 cgd cp[1] == magic[1]) {
513 1.1 cgd left = fcc - (cp-fibuf);
514 1.1 cgd n = control(p, cp, left);
515 1.1 cgd if (n) {
516 1.1 cgd left -= n;
517 1.1 cgd if (left > 0)
518 1.17 perry memmove(cp,
519 1.15 perry cp+n,
520 1.9 lukem left);
521 1.1 cgd fcc -= n;
522 1.1 cgd goto top; /* n^2 */
523 1.1 cgd }
524 1.1 cgd }
525 1.1 cgd }
526 1.1 cgd }
527 1.1 cgd
528 1.27 mycroft if (set[0].revents & POLLOUT && fcc > 0) {
529 1.1 cgd cc = write(p, fbp, fcc);
530 1.1 cgd if (cc > 0) {
531 1.1 cgd fcc -= cc;
532 1.1 cgd fbp += cc;
533 1.1 cgd }
534 1.1 cgd }
535 1.1 cgd
536 1.27 mycroft if (set[0].revents & POLLIN) {
537 1.1 cgd pcc = read(p, pibuf, sizeof (pibuf));
538 1.1 cgd pbp = pibuf;
539 1.1 cgd if (pcc < 0 && errno == EWOULDBLOCK)
540 1.1 cgd pcc = 0;
541 1.1 cgd else if (pcc <= 0)
542 1.1 cgd break;
543 1.1 cgd else if (pibuf[0] == 0) {
544 1.1 cgd pbp++, pcc--;
545 1.1 cgd } else {
546 1.1 cgd if (pkcontrol(pibuf[0])) {
547 1.1 cgd pibuf[0] |= oobdata[0];
548 1.1 cgd send(f, &pibuf[0], 1, MSG_OOB);
549 1.1 cgd }
550 1.1 cgd pcc = 0;
551 1.1 cgd }
552 1.1 cgd }
553 1.27 mycroft if (set[1].revents & POLLOUT && pcc > 0) {
554 1.27 mycroft cc = write(f, pbp, pcc);
555 1.1 cgd if (cc > 0) {
556 1.1 cgd pcc -= cc;
557 1.1 cgd pbp += cc;
558 1.1 cgd }
559 1.1 cgd }
560 1.1 cgd }
561 1.1 cgd }
562 1.1 cgd
563 1.40 joerg static void
564 1.40 joerg cleanup(int signo)
565 1.1 cgd {
566 1.18 tsarna char *p, c;
567 1.1 cgd
568 1.1 cgd p = line + sizeof(_PATH_DEV) - 1;
569 1.26 christos #ifdef SUPPORT_UTMP
570 1.1 cgd if (logout(p))
571 1.1 cgd logwtmp(p, "", "");
572 1.26 christos #endif
573 1.26 christos #ifdef SUPPORT_UTMPX
574 1.26 christos if (logoutx(p, 0, DEAD_PROCESS))
575 1.26 christos logwtmpx(p, "", "", 0, DEAD_PROCESS);
576 1.26 christos #endif
577 1.1 cgd (void)chmod(line, 0666);
578 1.1 cgd (void)chown(line, 0, 0);
579 1.18 tsarna c = *p; *p = 'p';
580 1.1 cgd (void)chmod(line, 0666);
581 1.1 cgd (void)chown(line, 0, 0);
582 1.18 tsarna *p = c;
583 1.18 tsarna if (ttyaction(line, "rlogind", "root"))
584 1.18 tsarna syslog(LOG_ERR, "%s: ttyaction failed", line);
585 1.1 cgd shutdown(netf, 2);
586 1.1 cgd exit(1);
587 1.1 cgd }
588 1.1 cgd
589 1.40 joerg static void
590 1.40 joerg fatal(int f, const char *msg, int syserr)
591 1.1 cgd {
592 1.1 cgd int len;
593 1.31 itojun char buf[BUFSIZ], *bp, *ep;
594 1.31 itojun
595 1.31 itojun bp = buf;
596 1.31 itojun ep = buf + sizeof(buf);
597 1.1 cgd
598 1.1 cgd /*
599 1.1 cgd * Prepend binary one to message if we haven't sent
600 1.1 cgd * the magic null as confirmation.
601 1.1 cgd */
602 1.1 cgd if (!confirmed)
603 1.31 itojun *bp++ = '\001'; /* error indicator */
604 1.1 cgd if (syserr)
605 1.31 itojun len = snprintf(bp, ep - bp, "rlogind: %s: %s.\r\n",
606 1.1 cgd msg, strerror(errno));
607 1.1 cgd else
608 1.31 itojun len = snprintf(bp, ep - bp, "rlogind: %s.\r\n", msg);
609 1.1 cgd (void) write(f, buf, bp + len - buf);
610 1.1 cgd exit(1);
611 1.1 cgd }
612 1.1 cgd
613 1.40 joerg static int
614 1.40 joerg do_rlogin(struct sockaddr *dest, char *host)
615 1.1 cgd {
616 1.9 lukem int retval;
617 1.9 lukem
618 1.1 cgd getstr(rusername, sizeof(rusername), "remuser too long");
619 1.1 cgd getstr(lusername, sizeof(lusername), "locuser too long");
620 1.1 cgd getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long");
621 1.1 cgd
622 1.1 cgd pwd = getpwnam(lusername);
623 1.9 lukem if (pwd == NULL) {
624 1.9 lukem syslog(LOG_INFO,
625 1.9 lukem "%s@%s as %s: unknown login.", rusername, host, lusername);
626 1.5 cgd return (-1);
627 1.9 lukem }
628 1.20 itojun
629 1.20 itojun retval = iruserok_sa(dest, dest->sa_len, pwd->pw_uid == 0, rusername,
630 1.20 itojun lusername);
631 1.9 lukem /* XXX put inet_ntoa(dest->sin_addr.s_addr) into all messages below */
632 1.9 lukem if (retval == 0) {
633 1.9 lukem if (log_success)
634 1.9 lukem syslog(LOG_INFO, "%s@%s as %s: iruserok succeeded",
635 1.9 lukem rusername, host, lusername);
636 1.9 lukem } else {
637 1.9 lukem if (__rcmd_errstr)
638 1.9 lukem syslog(LOG_INFO, "%s@%s as %s: iruserok failed (%s)",
639 1.9 lukem rusername, host, lusername, __rcmd_errstr);
640 1.9 lukem else
641 1.9 lukem syslog(LOG_INFO, "%s@%s as %s: iruserok failed",
642 1.9 lukem rusername, host, lusername);
643 1.9 lukem }
644 1.9 lukem return(retval);
645 1.1 cgd }
646 1.1 cgd
647 1.40 joerg static void
648 1.40 joerg getstr(char *buf, int cnt, const char *errmsg)
649 1.1 cgd {
650 1.1 cgd char c;
651 1.1 cgd
652 1.1 cgd do {
653 1.1 cgd if (read(0, &c, 1) != 1)
654 1.1 cgd exit(1);
655 1.1 cgd if (--cnt < 0)
656 1.1 cgd fatal(STDOUT_FILENO, errmsg, 0);
657 1.1 cgd *buf++ = c;
658 1.1 cgd } while (c != 0);
659 1.1 cgd }
660 1.1 cgd
661 1.1 cgd
662 1.40 joerg static void
663 1.40 joerg setup_term(int fd)
664 1.1 cgd {
665 1.13 mrg char *cp = index(term+ENVSIZE, '/');
666 1.1 cgd char *speed;
667 1.1 cgd struct termios tt;
668 1.1 cgd
669 1.1 cgd #ifndef notyet
670 1.1 cgd tcgetattr(fd, &tt);
671 1.1 cgd if (cp) {
672 1.1 cgd *cp++ = '\0';
673 1.1 cgd speed = cp;
674 1.1 cgd cp = index(speed, '/');
675 1.1 cgd if (cp)
676 1.1 cgd *cp++ = '\0';
677 1.1 cgd cfsetspeed(&tt, atoi(speed));
678 1.1 cgd }
679 1.1 cgd
680 1.1 cgd tt.c_iflag = TTYDEF_IFLAG;
681 1.1 cgd tt.c_oflag = TTYDEF_OFLAG;
682 1.1 cgd tt.c_lflag = TTYDEF_LFLAG;
683 1.1 cgd tcsetattr(fd, TCSAFLUSH, &tt);
684 1.1 cgd #else
685 1.1 cgd if (cp) {
686 1.1 cgd *cp++ = '\0';
687 1.1 cgd speed = cp;
688 1.1 cgd cp = index(speed, '/');
689 1.1 cgd if (cp)
690 1.1 cgd *cp++ = '\0';
691 1.1 cgd tcgetattr(fd, &tt);
692 1.1 cgd cfsetspeed(&tt, atoi(speed));
693 1.1 cgd tcsetattr(fd, TCSAFLUSH, &tt);
694 1.1 cgd }
695 1.1 cgd #endif
696 1.1 cgd
697 1.1 cgd env[0] = term;
698 1.1 cgd env[1] = 0;
699 1.1 cgd environ = env;
700 1.1 cgd }
701 1.1 cgd
702 1.1 cgd
703 1.40 joerg static void
704 1.40 joerg usage(void)
705 1.1 cgd {
706 1.9 lukem syslog(LOG_ERR, "usage: rlogind [-alnL]");
707 1.40 joerg exit(1);
708 1.1 cgd }
709 1.1 cgd
710 1.1 cgd /*
711 1.1 cgd * Check whether host h is in our local domain,
712 1.1 cgd * defined as sharing the last two components of the domain part,
713 1.1 cgd * or the entire domain part if the local domain has only one component.
714 1.1 cgd * If either name is unqualified (contains no '.'),
715 1.1 cgd * assume that the host is local, as it will be
716 1.1 cgd * interpreted as such.
717 1.1 cgd */
718 1.40 joerg static int
719 1.40 joerg local_domain(char *h)
720 1.1 cgd {
721 1.14 mrg char localhost[MAXHOSTNAMELEN + 1];
722 1.5 cgd char *p1, *p2;
723 1.1 cgd
724 1.1 cgd localhost[0] = 0;
725 1.1 cgd (void) gethostname(localhost, sizeof(localhost));
726 1.14 mrg localhost[sizeof(localhost) - 1] = '\0';
727 1.1 cgd p1 = topdomain(localhost);
728 1.1 cgd p2 = topdomain(h);
729 1.1 cgd if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
730 1.5 cgd return (1);
731 1.5 cgd return (0);
732 1.1 cgd }
733 1.1 cgd
734 1.40 joerg static char *
735 1.40 joerg topdomain(char *h)
736 1.1 cgd {
737 1.13 mrg char *p;
738 1.1 cgd char *maybe = NULL;
739 1.1 cgd int dots = 0;
740 1.1 cgd
741 1.1 cgd for (p = h + strlen(h); p >= h; p--) {
742 1.1 cgd if (*p == '.') {
743 1.1 cgd if (++dots == 2)
744 1.1 cgd return (p);
745 1.1 cgd maybe = p;
746 1.1 cgd }
747 1.1 cgd }
748 1.1 cgd return (maybe);
749 1.1 cgd }
750