1 #ifndef lint 2 static char sccsid[] = "@(#)rlogind.c 4.3 82/11/14"; 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 struct passwd *getpwnam(); 22 char *crypt(), *rindex(), *index(), *malloc(); 23 int options = SO_ACCEPTCONN|SO_KEEPALIVE; 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 union wait status; 37 int f, debug = 0; 38 struct sockaddr_in from; 39 struct servent *sp; 40 41 sp = getservbyname("login", "tcp"); 42 if (sp == 0) { 43 fprintf(stderr, "rlogind: tcp/rlogin: unknown service\n"); 44 exit(1); 45 } 46 #ifndef DEBUG 47 if (fork()) 48 exit(0); 49 for (f = 0; f < 10; f++) 50 (void) close(f); 51 (void) open("/", 0); 52 (void) dup2(0, 1); 53 (void) dup2(0, 2); 54 { int tt = open("/dev/tty", 2); 55 if (tt > 0) { 56 ioctl(tt, TIOCNOTTY, 0); 57 close(tt); 58 } 59 } 60 #endif 61 sin.sin_port = htons(sp->s_port); 62 argc--, argv++; 63 if (argc > 0 && !strcmp(argv[0], "-d")) { 64 int port = atoi(argv[0]); 65 66 if (port < 0) { 67 fprintf(stderr, "%s: bad port #\n", argv[0]); 68 exit(1); 69 } 70 sin.sin_port = htons(port); 71 argv++, argc--; 72 } 73 f = socket(0, SOCK_STREAM, 0, 0); 74 if (f < 0) { 75 perror("rlogind: socket"); 76 exit(1); 77 } 78 if (bind(f, &sin, sizeof (sin), 0) < 0) { 79 perror("rlogind: bind"); 80 exit(1); 81 } 82 listen(f, 10); 83 for (;;) { 84 int s, len = sizeof (from); 85 86 s = accept(f, &from, &len, 0); 87 if (s < 0) { 88 perror("accept"); 89 sleep(1); 90 continue; 91 } 92 if (fork() == 0) 93 doit(s, &from); 94 close(s); 95 while (wait3(status, WNOHANG, 0) > 0) 96 continue; 97 } 98 } 99 100 char locuser[32], remuser[32]; 101 char buf[BUFSIZ]; 102 int child; 103 int cleanup(); 104 int netf; 105 extern errno; 106 char *line; 107 108 doit(f, fromp) 109 int f; 110 struct sockaddr_in *fromp; 111 { 112 char c; 113 int i, p, cc, t; 114 int stop = TIOCPKT_DOSTOP; 115 register struct hostent *hp; 116 117 alarm(60); 118 read(f, &c, 1); 119 if (c != 0) 120 exit(1); 121 alarm(0); 122 fromp->sin_port = htons(fromp->sin_port); 123 hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 124 fromp->sin_family); 125 if (fromp->sin_family != AF_INET || 126 fromp->sin_port >= IPPORT_RESERVED || 127 hp == 0) { 128 write(f, "\01Permission denied.\n", 20); 129 exit(1); 130 } 131 write(f, "", 1); 132 for (c = 'p'; c <= 's'; c++) { 133 struct stat stb; 134 line = "/dev/ptyXX"; 135 line[strlen("/dev/pty")] = c; 136 line[strlen("/dev/ptyp")] = '0'; 137 if (stat(line, &stb) < 0) 138 break; 139 for (i = 0; i < 16; i++) { 140 line[strlen("/dev/ptyp")] = "0123456789abcdef"[i]; 141 p = open(line, 2); 142 if (p > 0) 143 goto gotpty; 144 } 145 } 146 dup2(f, 1); 147 printf("All network ports in use.\r\n"); 148 exit(1); 149 gotpty: 150 dup2(f, 0); 151 line[strlen("/dev/")] = 't'; 152 #ifdef DEBUG 153 { int tt = open("/dev/tty", 2); 154 if (tt > 0) { 155 ioctl(tt, TIOCNOTTY, 0); 156 close(tt); 157 } 158 } 159 #endif 160 t = open(line, 2); 161 if (t < 0) { 162 dup2(f, 2); 163 perror(line); 164 exit(1); 165 } 166 { struct sgttyb b; 167 gtty(t, &b); b.sg_flags = RAW|ANYP; stty(t, &b); 168 } 169 if (fork()) { 170 char pibuf[1024], fibuf[1024], *pbp, *fbp; 171 int pcc = 0, fcc = 0, on = 1; 172 /* FILE *console = fopen("/dev/console", "w"); */ 173 /* setbuf(console, 0); */ 174 175 /* fprintf(console, "f %d p %d\r\n", f, p); */ 176 ioctl(f, FIONBIO, &on); 177 ioctl(p, FIONBIO, &on); 178 ioctl(p, TIOCPKT, &on); 179 signal(SIGTSTP, SIG_IGN); 180 sigset(SIGCHLD, cleanup); 181 for (;;) { 182 int ibits = 0, obits = 0; 183 if (fcc) obits |= (1<<p); else ibits |= (1<<f); 184 if (pcc >= 0) 185 if (pcc) obits |= (1<<f); else ibits |= (1<<p); 186 if (fcc < 0 && pcc < 0) break; 187 /* fprintf(console, "ibits from %d obits from %d\r\n", ibits, obits); */ 188 select(16, &ibits, &obits, 0, 0, 0); 189 /* fprintf(console, "ibits %d obits %d\r\n", ibits, obits); */ 190 if (ibits == 0 && obits == 0) { 191 sleep(5); 192 continue; 193 } 194 if (ibits & (1<<f)) { 195 fcc = read(f, fibuf, sizeof (fibuf)); 196 /* fprintf(console, "%d from f\r\n", fcc); */ 197 if (fcc < 0 && errno == EWOULDBLOCK) 198 fcc = 0; 199 else { 200 if (fcc <= 0) 201 break; 202 fbp = fibuf; 203 } 204 } 205 if (ibits & (1<<p)) { 206 pcc = read(p, pibuf, sizeof (pibuf)); 207 /* fprintf(console, "%d from p, buf[0] %x, errno %d\r\n", pcc, buf[0], errno); */ 208 pbp = pibuf; 209 if (pcc < 0 && errno == EWOULDBLOCK) 210 pcc = 0; 211 else if (pcc <= 0) 212 pcc = -1; 213 else if (pibuf[0] == 0) 214 pbp++, pcc--; 215 else { 216 if (pibuf[0]&(TIOCPKT_FLUSHWRITE| 217 TIOCPKT_NOSTOP| 218 TIOCPKT_DOSTOP)) { 219 int nstop = pibuf[0] & 220 (TIOCPKT_NOSTOP| 221 TIOCPKT_DOSTOP); 222 if (nstop) 223 stop = nstop; 224 pibuf[0] |= nstop; 225 send(f,&pibuf[0],1,SOF_OOB); 226 } 227 pcc = 0; 228 } 229 } 230 if ((obits & (1<<f)) && pcc > 0) { 231 cc = write(f, pbp, pcc); 232 /* fprintf(console, "%d of %d to f\r\n", cc, pcc); */ 233 if (cc > 0) { 234 pcc -= cc; 235 pbp += cc; 236 } 237 } 238 if ((obits & (1<<p)) && fcc > 0) { 239 cc = write(p, fbp, fcc); 240 /* fprintf(console, "%d of %d to p\r\n", cc, fcc); */ 241 if (cc > 0) { 242 fcc -= cc; 243 fbp += cc; 244 } 245 } 246 } 247 cleanup(); 248 } 249 close(f); 250 close(p); 251 dup2(t, 0); 252 dup2(t, 1); 253 dup2(t, 2); 254 close(t); 255 execl("/bin/login", "login", "-r", hp->h_name, 0); 256 perror("/bin/login"); 257 exit(1); 258 } 259 260 cleanup() 261 { 262 int how = 2; 263 264 rmut(); 265 ioctl(netf, SIOCDONE, &how); 266 kill(0, SIGKILL); 267 exit(1); 268 } 269 270 #include <utmp.h> 271 272 struct utmp wtmp; 273 char wtmpf[] = "/usr/adm/wtmp"; 274 char utmp[] = "/etc/utmp"; 275 #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 276 #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 277 278 rmut() 279 { 280 register f; 281 int found = 0; 282 283 f = open(utmp, 2); 284 if (f >= 0) { 285 while(read(f, (char *)&wtmp, sizeof(wtmp)) == sizeof(wtmp)) { 286 if (SCMPN(wtmp.ut_line, line+5) || wtmp.ut_name[0]==0) 287 continue; 288 lseek(f, -(long)sizeof(wtmp), 1); 289 SCPYN(wtmp.ut_name, ""); 290 time(&wtmp.ut_time); 291 write(f, (char *)&wtmp, sizeof(wtmp)); 292 found++; 293 } 294 close(f); 295 } 296 if (found) { 297 f = open(wtmpf, 1); 298 if (f >= 0) { 299 SCPYN(wtmp.ut_line, line+5); 300 SCPYN(wtmp.ut_name, ""); 301 time(&wtmp.ut_time); 302 lseek(f, (long)0, 2); 303 write(f, (char *)&wtmp, sizeof(wtmp)); 304 close(f); 305 } 306 } 307 chmod(line, 0666); 308 chown(line, 0, 0); 309 line[strlen("/dev/")] = 'p'; 310 chmod(line, 0666); 311 chown(line, 0, 0); 312 } 313