2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)sliplogin.c 8.2 (Berkeley) 2/1/94
35 * $FreeBSD: src/usr.sbin/sliplogin/sliplogin.c,v 1.9.6.2 2001/07/19 05:21:28 kris Exp $
36 * $DragonFly: src/usr.sbin/sliplogin/sliplogin.c,v 1.2 2003/06/17 04:30:03 dillon Exp $
41 * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
43 * This program initializes its own tty port to be an async TCP/IP interface.
44 * It sets the line discipline to slip, invokes a shell script to initialize
45 * the network interface, then pauses forever waiting for hangup.
47 * It is a remote descendant of several similar programs with incestuous ties:
48 * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
49 * - slattach, probably by Rick Adams but touched by countless hordes.
50 * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
52 * There are two forms of usage:
55 * Invoked simply as "sliplogin", the program looks up the username
56 * in the file /etc/slip.hosts.
57 * If an entry is found, the line on fd0 is configured for SLIP operation
58 * as specified in the file.
60 * "sliplogin IPhostlogin </dev/ttyb"
61 * Invoked by root with a username, the name is looked up in the
62 * /etc/slip.hosts file and if found fd0 is configured as in case 1.
65 #include <sys/param.h>
66 #include <sys/socket.h>
73 #include <sys/ioctl.h>
85 #include "pathnames.h"
87 extern char **environ;
89 static char *restricted_environ[] = {
90 "PATH=" _PATH_STDPATH,
101 char loginargs[BUFSIZ];
102 char loginfile[MAXPATHLEN];
103 char loginname[BUFSIZ];
104 static char raddr[32]; /* remote address */
105 char ifname[IFNAMSIZ]; /* interface name */
106 static char pidfilename[MAXPATHLEN]; /* name of pid file */
107 static char iffilename[MAXPATHLEN]; /* name of if file */
108 static pid_t pid; /* our pid */
113 static char address[20] ="";
115 unsigned long ipaddr;
118 if ((he = gethostbyname(raddr)) != NULL) {
119 ipaddr = ntohl(*(long *)he->h_addr_list[0]);
120 sprintf(address, "%lu.%lu.%lu.%lu",
122 (ipaddr & 0x00ff0000) >> 16,
123 (ipaddr & 0x0000ff00) >> 8,
124 (ipaddr & 0x000000ff));
136 "compress", IFF_LINK0, IFF_LINK2,
137 "noicmp", IFF_LINK1, 0 ,
138 "autocomp", IFF_LINK2, IFF_LINK0,
146 static char slopt[5][16];
147 static char laddr[16];
148 static char mask[16];
149 char slparmsfile[MAXPATHLEN];
154 environ = restricted_environ; /* minimal protection for system() */
156 (void)strncpy(loginname, name, sizeof(loginname)-1);
157 loginname[sizeof(loginname)-1] = '\0';
159 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
161 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
164 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
167 if (loginargs[0] == '#' || isspace(loginargs[0]))
169 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
170 user, laddr, raddr, mask, slopt[0], slopt[1],
171 slopt[2], slopt[3], slopt[4]);
173 syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS);
176 if (strcmp(user, name) != 0)
182 for (i = 0; i < n - 4; i++) {
183 for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
185 if (strcmp(modes[j].sm_name, slopt[i]) == 0) {
186 slip_mode |= (modes[j].sm_or_flag);
187 slip_mode &= ~(modes[j].sm_and_flag);
194 * we've found the guy we're looking for -- see if
195 * there's a login file we can use. First check for
196 * one specific to this host. If none found, try for
199 (void)snprintf(loginfile, sizeof(loginfile), "%s.%s", _PATH_LOGIN, name);
200 if (access(loginfile, R_OK|X_OK) != 0) {
201 (void)strncpy(loginfile, _PATH_LOGIN, sizeof(loginfile)-1);
202 loginfile[sizeof(loginfile)-1] = '\0';
203 if (access(loginfile, R_OK|X_OK)) {
205 "access denied for %s - no %s\n",
210 (void)snprintf(slparmsfile, sizeof(slparmsfile), "%s.%s", _PATH_SLPARMS, name);
211 if (access(slparmsfile, R_OK|X_OK) != 0) {
212 (void)strncpy(slparmsfile, _PATH_SLPARMS, sizeof(slparmsfile)-1);
213 slparmsfile[sizeof(slparmsfile)-1] = '\0';
214 if (access(slparmsfile, R_OK|X_OK))
217 keepal = outfill = 0;
220 if ((fp = fopen(slparmsfile, "r")) == NULL) {
222 syslog(LOG_ERR, "%s: %m\n", slparmsfile);
226 while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
229 if (buf[0] == '#' || isspace(buf[0]))
231 n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit);
234 syslog(LOG_ERR, "%s: wrong format\n", slparmsfile);
246 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
258 case SIGHUP: return("HUP");
259 case SIGINT: return("INT");
260 case SIGQUIT: return("QUIT");
261 case SIGILL: return("ILL");
262 case SIGTRAP: return("TRAP");
263 case SIGIOT: return("IOT");
264 case SIGEMT: return("EMT");
265 case SIGFPE: return("FPE");
266 case SIGKILL: return("KILL");
267 case SIGBUS: return("BUS");
268 case SIGSEGV: return("SEGV");
269 case SIGSYS: return("SYS");
270 case SIGPIPE: return("PIPE");
271 case SIGALRM: return("ALRM");
272 case SIGTERM: return("TERM");
273 case SIGURG: return("URG");
274 case SIGSTOP: return("STOP");
275 case SIGTSTP: return("TSTP");
276 case SIGCONT: return("CONT");
277 case SIGCHLD: return("CHLD");
278 case SIGTTIN: return("TTIN");
279 case SIGTTOU: return("TTOU");
280 case SIGIO: return("IO");
281 case SIGXCPU: return("XCPU");
282 case SIGXFSZ: return("XFSZ");
283 case SIGVTALRM: return("VTALRM");
284 case SIGPROF: return("PROF");
285 case SIGWINCH: return("WINCH");
287 case SIGLOST: return("LOST");
289 case SIGUSR1: return("USR1");
290 case SIGUSR2: return("USR2");
292 (void)snprintf(buf, sizeof(buf), "sig %d", s);
300 char logoutfile[MAXPATHLEN];
304 (void)snprintf(logoutfile, sizeof(logoutfile), "%s.%s", _PATH_LOGOUT, loginname);
305 if (access(logoutfile, R_OK|X_OK) != 0) {
306 (void)strncpy(logoutfile, _PATH_LOGOUT, sizeof(logoutfile)-1);
307 logoutfile[sizeof(logoutfile)-1] = '\0';
309 if (access(logoutfile, R_OK|X_OK) == 0) {
310 char logincmd[2*MAXPATHLEN+32];
312 (void) snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", logoutfile, unit, speed, loginargs);
313 (void) system(logincmd);
315 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
317 if (unlink(pidfilename) < 0 && errno != ENOENT)
318 syslog(LOG_WARNING, "unable to delete pid file: %m");
319 if (unlink(iffilename) < 0 && errno != ENOENT)
320 syslog(LOG_WARNING, "unable to delete if file: %m");
326 /* Modify the slip line mode and add any compression or no-icmp flags. */
327 void line_flags(unit)
333 /* open a socket as the handle to the interface */
334 s = socket(AF_INET, SOCK_DGRAM, 0);
336 syslog(LOG_ERR, "socket: %m");
339 sprintf(ifr.ifr_name, "sl%d", unit);
341 /* get the flags for the interface */
342 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
343 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
347 /* Assert any compression or no-icmp flags. */
348 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
349 ifr.ifr_flags &= SLMASK;
350 ifr.ifr_flags |= slip_mode;
351 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
352 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
365 struct termios tios, otios;
366 char logincmd[2*BUFSIZ+32];
367 extern uid_t getuid();
369 FILE *pidfile; /* pid file */
370 FILE *iffile; /* interfaces file */
373 char devnam[MAXPATHLEN] = _PATH_TTY; /* Device name */
375 if ((name = strrchr(argv[0], '/')) == NULL)
378 for (fd = 3 ; fd < s ; fd++)
380 openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON);
386 * Disassociate from current controlling terminal, if any,
387 * and ensure that the slip line is our controlling terminal.
390 syslog(LOG_ERR, "daemon(1, 1): %m");
394 if ((fd = open(argv[2], O_RDWR)) == -1) {
395 syslog(LOG_ERR, "open %s: %m", argv[2]);
402 if (ioctl(0, TIOCSCTTY, 0) == -1) {
403 syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
406 if (tcsetpgrp(0, getpid()) < 0) {
407 syslog(LOG_ERR, "tcsetpgrp failed: %m");
411 if ((name = getlogin()) == NULL) {
412 syslog(LOG_ERR, "access denied - login name not found\n");
417 (void) fchmod(0, 0600);
418 (void) fprintf(stderr, "starting slip login for %s\n", loginname);
419 (void) fprintf(stderr, "your address is %s\n\n", make_ipaddr());
421 (void) fflush(stderr);
424 /* set up the line parameters */
425 if (tcgetattr(0, &tios) < 0) {
426 syslog(LOG_ERR, "tcgetattr: %m");
431 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
432 syslog(LOG_ERR, "tcsetattr: %m");
435 speed = cfgetispeed(&tios);
438 if (ioctl(0, TIOCSETD, &ldisc) < 0) {
439 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
442 if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) {
443 syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m");
446 /* find out what unit number we were assigned */
447 if (ioctl(0, SLIOCGUNIT, &unit) < 0) {
448 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
451 (void) signal(SIGHUP, hup_handler);
452 (void) signal(SIGTERM, hup_handler);
455 (void) signal(SIGURG, hup_handler);
456 if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) {
457 syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
461 if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) {
462 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
466 /* write pid to file */
468 (void) sprintf(ifname, "sl%d", unit);
469 (void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
470 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
471 fprintf(pidfile, "%d\n", pid);
472 (void) fclose(pidfile);
474 syslog(LOG_ERR, "Failed to create pid file %s: %m",
479 /* write interface unit number to file */
483 for (n = strlen(devnam); n > 0; n--)
484 if (devnam[n] == '/') {
488 (void) sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]);
489 if ((iffile = fopen(iffilename, "w")) != NULL) {
490 fprintf(iffile, "sl%d\n", unit);
491 (void) fclose(iffile);
493 syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename);
498 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
499 (void)snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", loginfile, unit, speed,
502 * aim stdout and errout at /dev/null so logincmd output won't
503 * babble into the slip tty line.
506 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
508 syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL);
517 * Run login and logout scripts as root (real and effective);
518 * current route(8) is setuid root, and checks the real uid
519 * to see whether changes are allowed (or just "route get").
522 if (s = system(logincmd)) {
523 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
524 loginname, s, loginfile);
528 /* Handle any compression or no-icmp flags. */
531 /* reset uid to users' to allow the user to give a signal. */
533 /* twiddle thumbs until we get a signal */