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