1 #ifndef lint 2 static char sccsid[] = "@(#)rlogind.c 4.12 83/02/28"; 3 #endif 4 5 #include <stdio.h> 6 #include <sys/types.h> 7 #include <sys/stat.h> 8 #include <sys/socket.h> 9 10 #include <netinet/in.h> 11 12 #include <errno.h> 13 #include <pwd.h> 14 #include <wait.h> 15 #include <signal.h> 16 #include <sgtty.h> 17 #include <stdio.h> 18 #include <netdb.h> 19 20 extern errno; 21 int reapchild(); 22 struct passwd *getpwnam(); 23 char *crypt(), *rindex(), *index(), *malloc(), *ntoa(); 24 struct sockaddr_in sin = { AF_INET }; 25 /* 26 * remote login server: 27 * remuser\0 28 * locuser\0 29 * terminal type\0 30 * data 31 */ 32 main(argc, argv) 33 int argc; 34 char **argv; 35 { 36 int f, options = SO_KEEPALIVE; 37 struct sockaddr_in from; 38 struct servent *sp; 39 40 sp = getservbyname("login", "tcp"); 41 if (sp == 0) { 42 fprintf(stderr, "rlogind: tcp/rlogin: unknown service\n"); 43 exit(1); 44 } 45 #ifndef DEBUG 46 if (fork()) 47 exit(0); 48 for (f = 0; f < 10; f++) 49 (void) close(f); 50 (void) open("/", 0); 51 (void) dup2(0, 1); 52 (void) dup2(0, 2); 53 { int tt = open("/dev/tty", 2); 54 if (tt > 0) { 55 ioctl(tt, TIOCNOTTY, 0); 56 close(tt); 57 } 58 } 59 #endif 60 sin.sin_port = sp->s_port; 61 argc--, argv++; 62 if (argc > 0 && !strcmp(argv[0], "-d")) { 63 options |= SO_DEBUG; 64 argc--, argv++; 65 } 66 if (argc > 0) { 67 int port = atoi(argv[0]); 68 69 if (port < 0) { 70 fprintf(stderr, "%s: bad port #\n", argv[0]); 71 exit(1); 72 } 73 sin.sin_port = htons((u_short)port); 74 argv++, argc--; 75 } 76 f = socket(AF_INET, SOCK_STREAM, 0, 0); 77 if (f < 0) { 78 perror("rlogind: socket"); 79 exit(1); 80 } 81 if (options & SO_DEBUG) 82 if (setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) 83 perror("rlogind: setsockopt (SO_DEBUG)"); 84 #ifdef notdef 85 if (setsockopt(f, SOL_SOCKET, SO_KEEPALIVE, 0, 0) < 0) 86 perror("rlogind: setsocktopt (SO_KEEPALIVE)"); 87 #endif 88 if (bind(f, &sin, sizeof (sin), 0) < 0) { 89 perror("rlogind: bind"); 90 exit(1); 91 } 92 sigset(SIGCHLD, reapchild); 93 listen(f, 10); 94 for (;;) { 95 int s, len = sizeof (from); 96 97 s = accept(f, &from, &len, 0); 98 if (s < 0) { 99 if (errno == EINTR) 100 continue; 101 perror("rlogind: accept"); 102 continue; 103 } 104 if (fork() == 0) { 105 signal(SIGCHLD, SIG_IGN); 106 doit(s, &from); 107 } 108 close(s); 109 } 110 } 111 112 reapchild() 113 { 114 union wait status; 115 116 while (wait3(&status, WNOHANG, 0) > 0) 117 ; 118 } 119 120 char locuser[32], remuser[32]; 121 char buf[BUFSIZ]; 122 int child; 123 int cleanup(); 124 int netf; 125 extern errno; 126 char *line; 127 128 doit(f, fromp) 129 int f; 130 struct sockaddr_in *fromp; 131 { 132 char c; 133 int i, p, cc, t, pid; 134 int stop = TIOCPKT_DOSTOP; 135 register struct hostent *hp; 136 137 alarm(60); 138 read(f, &c, 1); 139 if (c != 0) 140 exit(1); 141 alarm(0); 142 fromp->sin_port = htons((u_short)fromp->sin_port); 143 hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 144 fromp->sin_family); 145 if (hp == 0) { 146 char buf[BUFSIZ], *cp = (char *)&fromp->sin_addr; 147 148 fatal(f, sprintf(buf, "Host name for your address (%s) unknown", 149 ntoa(fromp->sin_addr))); 150 } 151 if (fromp->sin_family != AF_INET || 152 fromp->sin_port >= IPPORT_RESERVED || 153 hp == 0) 154 fatal(f, "Permission denied"); 155 write(f, "", 1); 156 for (c = 'p'; c <= 's'; c++) { 157 struct stat stb; 158 line = "/dev/ptyXX"; 159 line[strlen("/dev/pty")] = c; 160 line[strlen("/dev/ptyp")] = '0'; 161 if (stat(line, &stb) < 0) 162 break; 163 for (i = 0; i < 16; i++) { 164 line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 165 p = open(line, 2); 166 if (p > 0) 167 goto gotpty; 168 } 169 } 170 fatal(f, "All network ports in use"); 171 /*NOTREACHED*/ 172 gotpty: 173 dup2(f, 0); 174 line[strlen("/dev/")] = 't'; 175 #ifdef DEBUG 176 { int tt = open("/dev/tty", 2); 177 if (tt > 0) { 178 ioctl(tt, TIOCNOTTY, 0); 179 close(tt); 180 } 181 } 182 #endif 183 t = open(line, 2); 184 if (t < 0) 185 fatalperror(f, line, errno); 186 { struct sgttyb b; 187 gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 188 } 189 pid = fork(); 190 if (pid < 0) 191 fatalperror(f, "", errno); 192 if (pid) { 193 char pibuf[1024], fibuf[1024], *pbp, *fbp; 194 int pcc = 0, fcc = 0, on = 1; 195 /* FILE *console = fopen("/dev/console", "w"); */ 196 /* setbuf(console, 0); */ 197 198 /* fprintf(console, "f %d p %d\r\n", f, p); */ 199 ioctl(f, FIONBIO, &on); 200 ioctl(p, FIONBIO, &on); 201 ioctl(p, TIOCPKT, &on); 202 signal(SIGTSTP, SIG_IGN); 203 sigset(SIGCHLD, cleanup); 204 for (;;) { 205 int ibits = 0, obits = 0; 206 207 if (fcc) 208 obits |= (1<<p); 209 else 210 ibits |= (1<<f); 211 if (pcc >= 0) 212 if (pcc) 213 obits |= (1<<f); 214 else 215 ibits |= (1<<p); 216 if (fcc < 0 && pcc < 0) 217 break; 218 /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */ 219 select(16, &ibits, &obits, 0, 0, 0); 220 /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */ 221 if (ibits == 0 && obits == 0) { 222 sleep(5); 223 continue; 224 } 225 if (ibits & (1<<f)) { 226 fcc = read(f, fibuf, sizeof (fibuf)); 227 /* fprintf(console, "%d from f\r\n", fcc); */ 228 if (fcc < 0 && errno == EWOULDBLOCK) 229 fcc = 0; 230 else { 231 if (fcc <= 0) 232 break; 233 fbp = fibuf; 234 } 235 } 236 if (ibits & (1<<p)) { 237 pcc = read(p, pibuf, sizeof (pibuf)); 238 /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */ 239 pbp = pibuf; 240 if (pcc < 0 && errno == EWOULDBLOCK) 241 pcc = 0; 242 else if (pcc <= 0) 243 pcc = -1; 244 else if (pibuf[0] == 0) 245 pbp++, pcc--; 246 else { 247 if (pibuf[0]&(TIOCPKT_FLUSHWRITE| 248 TIOCPKT_NOSTOP| 249 TIOCPKT_DOSTOP)) { 250 int nstop = pibuf[0] & 251 (TIOCPKT_NOSTOP| 252 TIOCPKT_DOSTOP); 253 if (nstop) 254 stop = nstop; 255 pibuf[0] |= nstop; 256 send(f,&pibuf[0],1,SOF_OOB); 257 } 258 pcc = 0; 259 } 260 } 261 if ((obits & (1<<f)) && pcc > 0) { 262 cc = write(f, pbp, pcc); 263 /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */ 264 if (cc > 0) { 265 pcc -= cc; 266 pbp += cc; 267 } 268 } 269 if ((obits & (1<<p)) && fcc > 0) { 270 cc = write(p, fbp, fcc); 271 /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */ 272 if (cc > 0) { 273 fcc -= cc; 274 fbp += cc; 275 } 276 } 277 } 278 cleanup(); 279 } 280 close(f); 281 close(p); 282 dup2(t, 0); 283 dup2(t, 1); 284 dup2(t, 2); 285 close(t); 286 execl("/bin/login", "login", "-r", hp->h_name, 0); 287 fatalperror(2, "/bin/login", errno); 288 /*NOTREACHED*/ 289 } 290 291 cleanup() 292 { 293 294 rmut(); 295 vhangup(); /* XXX */ 296 shutdown(netf, 2); 297 kill(0, SIGKILL); 298 exit(1); 299 } 300 301 fatal(f, msg) 302 int f; 303 char *msg; 304 { 305 char buf[BUFSIZ]; 306 307 buf[0] = '\01'; /* error indicator */ 308 (void) sprintf(buf + 1, "rlogind: %s.\n", msg); 309 (void) write(f, buf, strlen(buf)); 310 exit(1); 311 } 312 313 fatalperror(f, msg, errno) 314 int f; 315 char *msg; 316 int errno; 317 { 318 char buf[BUFSIZ]; 319 extern char *sys_errlist[]; 320 321 (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 322 fatal(f, buf); 323 } 324 325 #include <utmp.h> 326 327 struct utmp wtmp; 328 char wtmpf[] = "/usr/adm/wtmp"; 329 char utmp[] = "/etc/utmp"; 330 #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 331 #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 332 333 rmut() 334 { 335 register f; 336 int found = 0; 337 338 f = open(utmp, 2); 339 if (f >= 0) { 340 while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) { 341 if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 342 continue; 343 lseek(f, -(long)sizeof(wtmp), 1); 344 SCPYN(wtmp.ut_name, ""); 345 time(&wtmp.ut_time); 346 write(f, (char *)&wtmp, sizeof(wtmp)); 347 found++; 348 } 349 close(f); 350 } 351 if (found) { 352 f = open(wtmpf, 1); 353 if (f >= 0) { 354 SCPYN(wtmp.ut_line, line+5); 355 SCPYN(wtmp.ut_name, ""); 356 time(&wtmp.ut_time); 357 lseek(f, (long)0, 2); 358 write(f, (char *)&wtmp, sizeof(wtmp)); 359 close(f); 360 } 361 } 362 chmod(line, 0666); 363 chown(line, 0, 0); 364 line[strlen("/dev/")] = 'p'; 365 chmod(line, 0666); 366 chown(line, 0, 0); 367 } 368 369 /* 370 * Convert network-format internet address 371 * to base 256 d.d.d.d representation. 372 */ 373 char * 374 ntoa(in) 375 struct in_addr in; 376 { 377 static char b[18]; 378 register char *p; 379 380 p = (char *)∈ 381 #define UC(b) (((int)b)&0xff) 382 sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 383 return (b); 384 } 385