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