1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 */ 6 7 #ifndef lint 8 char copyright[] = 9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 10 All rights reserved.\n"; 11 #endif not lint 12 13 #ifndef lint 14 static char sccsid[] = "@(#)rlogind.c 5.12 (Berkeley) 03/10/87"; 15 #endif not lint 16 17 /* 18 * remote login server: 19 * remuser\0 20 * locuser\0 21 * terminal info\0 22 * data 23 */ 24 25 #include <stdio.h> 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <sys/socket.h> 29 #include <sys/wait.h> 30 #include <sys/file.h> 31 32 #include <netinet/in.h> 33 34 #include <errno.h> 35 #include <pwd.h> 36 #include <signal.h> 37 #include <sgtty.h> 38 #include <stdio.h> 39 #include <netdb.h> 40 #include <syslog.h> 41 #include <strings.h> 42 43 # ifndef TIOCPKT_WINDOW 44 # define TIOCPKT_WINDOW 0x80 45 # endif TIOCPKT_WINDOW 46 47 extern errno; 48 int reapchild(); 49 struct passwd *getpwnam(); 50 char *malloc(); 51 52 main(argc, argv) 53 int argc; 54 char **argv; 55 { 56 int on = 1, options = 0, fromlen; 57 struct sockaddr_in from; 58 59 openlog("rlogind", LOG_PID | LOG_AUTH, LOG_AUTH); 60 fromlen = sizeof (from); 61 if (getpeername(0, &from, &fromlen) < 0) { 62 fprintf(stderr, "%s: ", argv[0]); 63 perror("getpeername"); 64 _exit(1); 65 } 66 if (setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) { 67 syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 68 } 69 doit(0, &from); 70 } 71 72 int child; 73 int cleanup(); 74 int netf; 75 extern errno; 76 char *line; 77 extern char *inet_ntoa(); 78 79 struct winsize win = { 0, 0, 0, 0 }; 80 81 82 doit(f, fromp) 83 int f; 84 struct sockaddr_in *fromp; 85 { 86 int i, p, t, pid, on = 1; 87 register struct hostent *hp; 88 struct hostent hostent; 89 char c; 90 91 alarm(60); 92 read(f, &c, 1); 93 if (c != 0) 94 exit(1); 95 alarm(0); 96 fromp->sin_port = ntohs((u_short)fromp->sin_port); 97 hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 98 fromp->sin_family); 99 if (hp == 0) { 100 /* 101 * Only the name is used below. 102 */ 103 hp = &hostent; 104 hp->h_name = inet_ntoa(fromp->sin_addr); 105 } 106 if (fromp->sin_family != AF_INET || 107 fromp->sin_port >= IPPORT_RESERVED) 108 fatal(f, "Permission denied"); 109 write(f, "", 1); 110 for (c = 'p'; c <= 's'; c++) { 111 struct stat stb; 112 line = "/dev/ptyXX"; 113 line[strlen("/dev/pty")] = c; 114 line[strlen("/dev/ptyp")] = '0'; 115 if (stat(line, &stb) < 0) 116 break; 117 for (i = 0; i < 16; i++) { 118 line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 119 p = open(line, 2); 120 if (p > 0) 121 goto gotpty; 122 } 123 } 124 fatal(f, "Out of ptys"); 125 /*NOTREACHED*/ 126 gotpty: 127 (void) ioctl(p, TIOCSWINSZ, &win); 128 netf = f; 129 line[strlen("/dev/")] = 't'; 130 #ifdef DEBUG 131 { int tt = open("/dev/tty", 2); 132 if (tt > 0) { 133 ioctl(tt, TIOCNOTTY, 0); 134 close(tt); 135 } 136 } 137 #endif 138 t = open(line, 2); 139 if (t < 0) 140 fatalperror(f, line, errno); 141 { struct sgttyb b; 142 gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 143 } 144 pid = fork(); 145 if (pid < 0) 146 fatalperror(f, "", errno); 147 if (pid == 0) { 148 close(f), close(p); 149 dup2(t, 0), dup2(t, 1), dup2(t, 2); 150 close(t); 151 execl("/bin/login", "login", "-r", hp->h_name, 0); 152 fatalperror(2, "/bin/login", errno); 153 /*NOTREACHED*/ 154 } 155 close(t); 156 ioctl(f, FIONBIO, &on); 157 ioctl(p, FIONBIO, &on); 158 ioctl(p, TIOCPKT, &on); 159 signal(SIGTSTP, SIG_IGN); 160 signal(SIGCHLD, cleanup); 161 setpgrp(0, 0); 162 protocol(f, p); 163 signal(SIGCHLD, SIG_IGN); 164 cleanup(); 165 } 166 167 char magic[2] = { 0377, 0377 }; 168 char oobdata[] = {TIOCPKT_WINDOW}; 169 170 /* 171 * Handle a "control" request (signaled by magic being present) 172 * in the data stream. For now, we are only willing to handle 173 * window size changes. 174 */ 175 control(pty, cp, n) 176 int pty; 177 char *cp; 178 int n; 179 { 180 struct winsize w; 181 182 if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 183 return (0); 184 oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 185 bcopy(cp+4, (char *)&w, sizeof(w)); 186 w.ws_row = ntohs(w.ws_row); 187 w.ws_col = ntohs(w.ws_col); 188 w.ws_xpixel = ntohs(w.ws_xpixel); 189 w.ws_ypixel = ntohs(w.ws_ypixel); 190 (void)ioctl(pty, TIOCSWINSZ, &w); 191 return (4+sizeof (w)); 192 } 193 194 /* 195 * rlogin "protocol" machine. 196 */ 197 protocol(f, p) 198 int f, p; 199 { 200 char pibuf[1024], fibuf[1024], *pbp, *fbp; 201 register pcc = 0, fcc = 0; 202 int cc; 203 char cntl; 204 205 /* 206 * Must ignore SIGTTOU, otherwise we'll stop 207 * when we try and set slave pty's window shape 208 * (our controlling tty is the master pty). 209 */ 210 (void) signal(SIGTTOU, SIG_IGN); 211 send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 212 for (;;) { 213 int ibits, obits, ebits; 214 215 ibits = 0; 216 obits = 0; 217 if (fcc) 218 obits |= (1<<p); 219 else 220 ibits |= (1<<f); 221 if (pcc >= 0) 222 if (pcc) 223 obits |= (1<<f); 224 else 225 ibits |= (1<<p); 226 ebits = (1<<p); 227 if (select(16, &ibits, &obits, &ebits, 0) < 0) { 228 if (errno == EINTR) 229 continue; 230 fatalperror(f, "select", errno); 231 } 232 if (ibits == 0 && obits == 0 && ebits == 0) { 233 /* shouldn't happen... */ 234 sleep(5); 235 continue; 236 } 237 #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 238 if (ebits & (1<<p)) { 239 cc = read(p, &cntl, 1); 240 if (cc == 1 && pkcontrol(cntl)) { 241 cntl |= oobdata[0]; 242 send(f, &cntl, 1, MSG_OOB); 243 if (cntl & TIOCPKT_FLUSHWRITE) { 244 pcc = 0; 245 ibits &= ~(1<<p); 246 } 247 } 248 } 249 if (ibits & (1<<f)) { 250 fcc = read(f, fibuf, sizeof (fibuf)); 251 if (fcc < 0 && errno == EWOULDBLOCK) 252 fcc = 0; 253 else { 254 register char *cp; 255 int left, n; 256 257 if (fcc <= 0) 258 break; 259 fbp = fibuf; 260 261 top: 262 for (cp = fibuf; cp < fibuf+fcc-1; cp++) 263 if (cp[0] == magic[0] && 264 cp[1] == magic[1]) { 265 left = fcc - (cp-fibuf); 266 n = control(p, cp, left); 267 if (n) { 268 left -= n; 269 if (left > 0) 270 bcopy(cp+n, cp, left); 271 fcc -= n; 272 goto top; /* n^2 */ 273 } 274 } 275 } 276 } 277 278 if ((obits & (1<<p)) && fcc > 0) { 279 cc = write(p, fbp, fcc); 280 if (cc > 0) { 281 fcc -= cc; 282 fbp += cc; 283 } 284 } 285 286 if (ibits & (1<<p)) { 287 pcc = read(p, pibuf, sizeof (pibuf)); 288 pbp = pibuf; 289 if (pcc < 0 && errno == EWOULDBLOCK) 290 pcc = 0; 291 else if (pcc <= 0) 292 break; 293 else if (pibuf[0] == 0) 294 pbp++, pcc--; 295 else { 296 if (pkcontrol(pibuf[0])) { 297 pibuf[0] |= oobdata[0]; 298 send(f, &pibuf[0], 1, MSG_OOB); 299 } 300 pcc = 0; 301 } 302 } 303 if ((obits & (1<<f)) && pcc > 0) { 304 cc = write(f, pbp, pcc); 305 if (cc < 0 && errno == EWOULDBLOCK) { 306 /* also shouldn't happen */ 307 sleep(5); 308 continue; 309 } 310 if (cc > 0) { 311 pcc -= cc; 312 pbp += cc; 313 } 314 } 315 } 316 } 317 318 cleanup() 319 { 320 321 rmut(); 322 vhangup(); /* XXX */ 323 shutdown(netf, 2); 324 exit(1); 325 } 326 327 fatal(f, msg) 328 int f; 329 char *msg; 330 { 331 char buf[BUFSIZ]; 332 333 buf[0] = '\01'; /* error indicator */ 334 (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 335 (void) write(f, buf, strlen(buf)); 336 exit(1); 337 } 338 339 fatalperror(f, msg, errno) 340 int f; 341 char *msg; 342 int errno; 343 { 344 char buf[BUFSIZ]; 345 extern int sys_nerr; 346 extern char *sys_errlist[]; 347 348 if ((unsigned)errno < sys_nerr) 349 (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 350 else 351 (void) sprintf(buf, "%s: Error %d", msg, errno); 352 fatal(f, buf); 353 } 354 355 #include <utmp.h> 356 357 struct utmp wtmp; 358 char wtmpf[] = "/usr/adm/wtmp"; 359 char utmpf[] = "/etc/utmp"; 360 #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 361 #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 362 363 rmut() 364 { 365 register f; 366 int found = 0; 367 struct utmp *u, *utmp; 368 int nutmp; 369 struct stat statbf; 370 371 f = open(utmpf, O_RDWR); 372 if (f >= 0) { 373 fstat(f, &statbf); 374 utmp = (struct utmp *)malloc(statbf.st_size); 375 if (!utmp) 376 syslog(LOG_ERR, "utmp malloc failed"); 377 if (statbf.st_size && utmp) { 378 nutmp = read(f, utmp, statbf.st_size); 379 nutmp /= sizeof(struct utmp); 380 381 for (u = utmp ; u < &utmp[nutmp] ; u++) { 382 if (SCMPN(u->ut_line, line+5) || 383 u->ut_name[0]==0) 384 continue; 385 lseek(f, ((long)u)-((long)utmp), L_SET); 386 SCPYN(u->ut_name, ""); 387 SCPYN(u->ut_host, ""); 388 time(&u->ut_time); 389 write(f, (char *)u, sizeof(wtmp)); 390 found++; 391 } 392 } 393 close(f); 394 } 395 if (found) { 396 f = open(wtmpf, O_WRONLY|O_APPEND); 397 if (f >= 0) { 398 SCPYN(wtmp.ut_line, line+5); 399 SCPYN(wtmp.ut_name, ""); 400 SCPYN(wtmp.ut_host, ""); 401 time(&wtmp.ut_time); 402 write(f, (char *)&wtmp, sizeof(wtmp)); 403 close(f); 404 } 405 } 406 chmod(line, 0666); 407 chown(line, 0, 0); 408 line[strlen("/dev/")] = 'p'; 409 chmod(line, 0666); 410 chown(line, 0, 0); 411 } 412