1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)sliplogin.c 8.2 (Berkeley) 2/1/94 31 * $FreeBSD: src/usr.sbin/sliplogin/sliplogin.c,v 1.9.6.2 2001/07/19 05:21:28 kris Exp $ 32 */ 33 34 /* 35 * sliplogin.c 36 * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!] 37 * 38 * This program initializes its own tty port to be an async TCP/IP interface. 39 * It sets the line discipline to slip, invokes a shell script to initialize 40 * the network interface, then pauses forever waiting for hangup. 41 * 42 * It is a remote descendant of several similar programs with incestuous ties: 43 * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL. 44 * - slattach, probably by Rick Adams but touched by countless hordes. 45 * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it. 46 * 47 * There are two forms of usage: 48 * 49 * "sliplogin" 50 * Invoked simply as "sliplogin", the program looks up the username 51 * in the file /etc/slip.hosts. 52 * If an entry is found, the line on fd0 is configured for SLIP operation 53 * as specified in the file. 54 * 55 * "sliplogin IPhostlogin </dev/ttyb" 56 * Invoked by root with a username, the name is looked up in the 57 * /etc/slip.hosts file and if found fd0 is configured as in case 1. 58 */ 59 60 #include <sys/param.h> 61 #include <sys/socket.h> 62 #include <sys/stat.h> 63 #include <syslog.h> 64 #include <netdb.h> 65 66 #include <termios.h> 67 #include <sys/ioctl.h> 68 #include <net/slip.h> 69 #include <net/if.h> 70 71 #include <fcntl.h> 72 #include <stdio.h> 73 #include <errno.h> 74 #include <ctype.h> 75 #include <paths.h> 76 #include <string.h> 77 #include <unistd.h> 78 #include <stdlib.h> 79 #include <signal.h> 80 #include "pathnames.h" 81 82 extern char **environ; 83 84 static char *restricted_environ[] = { 85 "PATH=" _PATH_STDPATH, 86 NULL 87 }; 88 89 static int unit; 90 static int slip_mode; 91 static speed_t speed; 92 static int uid; 93 static int keepal; 94 static int outfill; 95 static int slunit; 96 static char loginargs[BUFSIZ]; 97 static char loginfile[MAXPATHLEN]; 98 static char loginname[BUFSIZ]; 99 static char raddr[32]; /* remote address */ 100 static char ifname[IFNAMSIZ]; /* interface name */ 101 static char pidfilename[MAXPATHLEN]; /* name of pid file */ 102 static char iffilename[MAXPATHLEN]; /* name of if file */ 103 static pid_t pid; /* our pid */ 104 105 static char * 106 make_ipaddr(void) 107 { 108 static char address[20] =""; 109 struct hostent *he; 110 unsigned long ipaddr; 111 112 address[0] = '\0'; 113 if ((he = gethostbyname(raddr)) != NULL) { 114 ipaddr = ntohl(*(long *)he->h_addr_list[0]); 115 sprintf(address, "%hu.%hu.%hu.%hu", 116 (ushort)(ipaddr >> 24), 117 (ushort)((ipaddr & 0x00ff0000) >> 16), 118 (ushort)((ipaddr & 0x0000ff00) >> 8), 119 (ushort)((ipaddr & 0x000000ff))); 120 } 121 122 return address; 123 } 124 125 static struct slip_modes { 126 const char *sm_name; 127 int sm_or_flag; 128 int sm_and_flag; 129 } modes[] = { 130 { "normal", 0, 0 }, 131 { "compress", IFF_LINK0, IFF_LINK2 }, 132 { "noicmp", IFF_LINK1, 0 }, 133 { "autocomp", IFF_LINK2, IFF_LINK0 } 134 }; 135 136 static void 137 findid(char *name) 138 { 139 FILE *fp; 140 static char slopt[5][16]; 141 static char laddr[16]; 142 static char mask[16]; 143 char slparmsfile[MAXPATHLEN]; 144 char user[16]; 145 char buf[128]; 146 int i, j, n; 147 148 environ = restricted_environ; /* minimal protection for system() */ 149 150 strncpy(loginname, name, sizeof(loginname)-1); 151 loginname[sizeof(loginname)-1] = '\0'; 152 153 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) { 154 accfile_err: 155 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS); 156 exit(1); 157 } 158 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) { 159 if (ferror(fp)) 160 goto accfile_err; 161 if (loginargs[0] == '#' || isspace(loginargs[0])) 162 continue; 163 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n", 164 user, laddr, raddr, mask, slopt[0], slopt[1], 165 slopt[2], slopt[3], slopt[4]); 166 if (n < 4) { 167 syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS); 168 exit(1); 169 } 170 if (strcmp(user, name) != 0) 171 continue; 172 173 fclose(fp); 174 175 slip_mode = 0; 176 for (i = 0; i < n - 4; i++) { 177 for (j = 0; j < (int)NELEM(modes); j++) { 178 if (strcmp(modes[j].sm_name, slopt[i]) == 0) { 179 slip_mode |= (modes[j].sm_or_flag); 180 slip_mode &= ~(modes[j].sm_and_flag); 181 break; 182 } 183 } 184 } 185 186 /* 187 * we've found the guy we're looking for -- see if 188 * there's a login file we can use. First check for 189 * one specific to this host. If none found, try for 190 * a generic one. 191 */ 192 snprintf(loginfile, sizeof(loginfile), "%s.%s", _PATH_SLOGIN, name); 193 if (access(loginfile, R_OK|X_OK) != 0) { 194 strncpy(loginfile, _PATH_SLOGIN, sizeof(loginfile)-1); 195 loginfile[sizeof(loginfile)-1] = '\0'; 196 if (access(loginfile, R_OK|X_OK)) { 197 syslog(LOG_ERR, 198 "access denied for %s - no %s\n", 199 name, _PATH_SLOGIN); 200 exit(5); 201 } 202 } 203 snprintf(slparmsfile, sizeof(slparmsfile), "%s.%s", _PATH_SLPARMS, name); 204 if (access(slparmsfile, R_OK|X_OK) != 0) { 205 strncpy(slparmsfile, _PATH_SLPARMS, sizeof(slparmsfile)-1); 206 slparmsfile[sizeof(slparmsfile)-1] = '\0'; 207 if (access(slparmsfile, R_OK|X_OK)) 208 *slparmsfile = '\0'; 209 } 210 keepal = outfill = 0; 211 slunit = -1; 212 if (*slparmsfile) { 213 if ((fp = fopen(slparmsfile, "r")) == NULL) { 214 slfile_err: 215 syslog(LOG_ERR, "%s: %m\n", slparmsfile); 216 exit(1); 217 } 218 n = 0; 219 while (fgets(buf, sizeof(buf) - 1, fp) != NULL) { 220 if (ferror(fp)) 221 goto slfile_err; 222 if (buf[0] == '#' || isspace(buf[0])) 223 continue; 224 n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit); 225 if (n < 1) { 226 slwrong_fmt: 227 syslog(LOG_ERR, "%s: wrong format\n", slparmsfile); 228 exit(1); 229 } 230 fclose(fp); 231 break; 232 } 233 if (n == 0) 234 goto slwrong_fmt; 235 } 236 237 return; 238 } 239 syslog(LOG_ERR, "SLIP access denied for %s\n", name); 240 exit(4); 241 /* NOTREACHED */ 242 } 243 244 static const char * 245 sigstr(int s) 246 { 247 static char buf[32]; 248 249 switch (s) { 250 case SIGHUP: return("HUP"); 251 case SIGINT: return("INT"); 252 case SIGQUIT: return("QUIT"); 253 case SIGILL: return("ILL"); 254 case SIGTRAP: return("TRAP"); 255 case SIGIOT: return("IOT"); 256 case SIGEMT: return("EMT"); 257 case SIGFPE: return("FPE"); 258 case SIGKILL: return("KILL"); 259 case SIGBUS: return("BUS"); 260 case SIGSEGV: return("SEGV"); 261 case SIGSYS: return("SYS"); 262 case SIGPIPE: return("PIPE"); 263 case SIGALRM: return("ALRM"); 264 case SIGTERM: return("TERM"); 265 case SIGURG: return("URG"); 266 case SIGSTOP: return("STOP"); 267 case SIGTSTP: return("TSTP"); 268 case SIGCONT: return("CONT"); 269 case SIGCHLD: return("CHLD"); 270 case SIGTTIN: return("TTIN"); 271 case SIGTTOU: return("TTOU"); 272 case SIGIO: return("IO"); 273 case SIGXCPU: return("XCPU"); 274 case SIGXFSZ: return("XFSZ"); 275 case SIGVTALRM: return("VTALRM"); 276 case SIGPROF: return("PROF"); 277 case SIGWINCH: return("WINCH"); 278 #ifdef SIGLOST 279 case SIGLOST: return("LOST"); 280 #endif 281 case SIGUSR1: return("USR1"); 282 case SIGUSR2: return("USR2"); 283 } 284 snprintf(buf, sizeof(buf), "sig %d", s); 285 return(buf); 286 } 287 288 static void 289 hup_handler(int s) 290 { 291 char logoutfile[MAXPATHLEN]; 292 293 close(0); 294 seteuid(0); 295 snprintf(logoutfile, sizeof(logoutfile), "%s.%.990s", _PATH_SLOGOUT, loginname); 296 if (access(logoutfile, R_OK|X_OK) != 0) { 297 strncpy(logoutfile, _PATH_SLOGOUT, sizeof(logoutfile)-1); 298 logoutfile[sizeof(logoutfile)-1] = '\0'; 299 } 300 if (access(logoutfile, R_OK|X_OK) == 0) { 301 char logincmd[2*MAXPATHLEN+32]; 302 303 snprintf(logincmd, sizeof(logincmd), "%s %d %u %s", logoutfile, unit, speed, loginargs); 304 system(logincmd); 305 } 306 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit, 307 sigstr(s)); 308 if (unlink(pidfilename) < 0 && errno != ENOENT) 309 syslog(LOG_WARNING, "unable to delete pid file: %m"); 310 if (unlink(iffilename) < 0 && errno != ENOENT) 311 syslog(LOG_WARNING, "unable to delete if file: %m"); 312 exit(1); 313 /* NOTREACHED */ 314 } 315 316 317 /* Modify the slip line mode and add any compression or no-icmp flags. */ 318 static void 319 line_flags(int _unit) 320 { 321 struct ifreq ifr; 322 int s; 323 324 /* open a socket as the handle to the interface */ 325 s = socket(AF_INET, SOCK_DGRAM, 0); 326 if (s < 0) { 327 syslog(LOG_ERR, "socket: %m"); 328 exit(1); 329 } 330 sprintf(ifr.ifr_name, "sl%d", _unit); 331 332 /* get the flags for the interface */ 333 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { 334 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m"); 335 exit(1); 336 } 337 338 /* Assert any compression or no-icmp flags. */ 339 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2)) 340 ifr.ifr_flags &= SLMASK; 341 ifr.ifr_flags |= slip_mode; 342 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) { 343 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m"); 344 exit(1); 345 } 346 close(s); 347 } 348 349 int 350 main(int argc, char *argv[]) 351 { 352 int fd, s, ldisc; 353 char *name; 354 struct termios tios; 355 char logincmd[2*BUFSIZ+32]; 356 357 FILE *pidfile; /* pid file */ 358 FILE *iffile; /* interfaces file */ 359 char *p; 360 int n; 361 char devnam[MAXPATHLEN] = _PATH_TTY; /* Device name */ 362 363 if ((name = strrchr(argv[0], '/')) == NULL) 364 name = argv[0]; 365 s = getdtablesize(); 366 for (fd = 3 ; fd < s ; fd++) 367 close(fd); 368 openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON); 369 uid = getuid(); 370 if (argc > 1) { 371 findid(argv[1]); 372 373 /* 374 * Disassociate from current controlling terminal, if any, 375 * and ensure that the slip line is our controlling terminal. 376 */ 377 if (daemon(1, 1)) { 378 syslog(LOG_ERR, "daemon(1, 1): %m"); 379 exit(1); 380 } 381 if (argc > 2) { 382 if ((fd = open(argv[2], O_RDWR)) == -1) { 383 syslog(LOG_ERR, "open %s: %m", argv[2]); 384 exit(2); 385 } 386 dup2(fd, 0); 387 if (fd > 2) 388 close(fd); 389 } 390 if (ioctl(0, TIOCSCTTY, 0) == -1) { 391 syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m"); 392 exit(1); 393 } 394 if (tcsetpgrp(0, getpid()) < 0) { 395 syslog(LOG_ERR, "tcsetpgrp failed: %m"); 396 exit(1); 397 } 398 } else { 399 if ((name = getlogin()) == NULL) { 400 syslog(LOG_ERR, "access denied - login name not found\n"); 401 exit(1); 402 } 403 findid(name); 404 } 405 fchmod(0, 0600); 406 fprintf(stderr, "starting slip login for %s\n", loginname); 407 fprintf(stderr, "your address is %s\n\n", make_ipaddr()); 408 409 fflush(stderr); 410 sleep(1); 411 412 /* set up the line parameters */ 413 if (tcgetattr(0, &tios) < 0) { 414 syslog(LOG_ERR, "tcgetattr: %m"); 415 exit(1); 416 } 417 cfmakeraw(&tios); 418 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) { 419 syslog(LOG_ERR, "tcsetattr: %m"); 420 exit(1); 421 } 422 speed = cfgetispeed(&tios); 423 424 ldisc = SLIPDISC; 425 if (ioctl(0, TIOCSETD, &ldisc) < 0) { 426 syslog(LOG_ERR, "ioctl(TIOCSETD): %m"); 427 exit(1); 428 } 429 if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) { 430 syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m"); 431 exit(1); 432 } 433 /* find out what unit number we were assigned */ 434 if (ioctl(0, SLIOCGUNIT, &unit) < 0) { 435 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m"); 436 exit(1); 437 } 438 signal(SIGHUP, hup_handler); 439 signal(SIGTERM, hup_handler); 440 441 if (keepal > 0) { 442 signal(SIGURG, hup_handler); 443 if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) { 444 syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m"); 445 exit(1); 446 } 447 } 448 if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) { 449 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m"); 450 exit(1); 451 } 452 453 /* write pid to file */ 454 pid = getpid(); 455 sprintf(ifname, "sl%d", unit); 456 sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname); 457 if ((pidfile = fopen(pidfilename, "w")) != NULL) { 458 fprintf(pidfile, "%d\n", pid); 459 fclose(pidfile); 460 } else { 461 syslog(LOG_ERR, "Failed to create pid file %s: %m", 462 pidfilename); 463 pidfilename[0] = 0; 464 } 465 466 /* write interface unit number to file */ 467 p = ttyname(0); 468 if (p) 469 strcpy(devnam, p); 470 for (n = strlen(devnam); n > 0; n--) 471 if (devnam[n] == '/') { 472 n++; 473 break; 474 } 475 sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]); 476 if ((iffile = fopen(iffilename, "w")) != NULL) { 477 fprintf(iffile, "sl%d\n", unit); 478 fclose(iffile); 479 } else { 480 syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename); 481 iffilename[0] = 0; 482 } 483 484 485 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname); 486 snprintf(logincmd, sizeof(logincmd), "%s %d %u %s", loginfile, unit, speed, 487 loginargs); 488 /* 489 * aim stdout and errout at /dev/null so logincmd output won't 490 * babble into the slip tty line. 491 */ 492 close(1); 493 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) { 494 if (fd < 0) { 495 syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL); 496 exit(1); 497 } 498 dup2(fd, 1); 499 close(fd); 500 } 501 dup2(1, 2); 502 503 /* 504 * Run login and logout scripts as root (real and effective); 505 * current route(8) is setuid root, and checks the real uid 506 * to see whether changes are allowed (or just "route get"). 507 */ 508 setuid(0); 509 if ((s = system(logincmd)) == -1) { 510 syslog(LOG_ERR, "%s login failed: exit status %d from %s", 511 loginname, s, loginfile); 512 exit(6); 513 } 514 515 /* Handle any compression or no-icmp flags. */ 516 line_flags(unit); 517 518 /* reset uid to users' to allow the user to give a signal. */ 519 seteuid(uid); 520 /* twiddle thumbs until we get a signal */ 521 while (1) 522 sigpause(0); 523 524 /* NOTREACHED */ 525 } 526