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.5 2008/09/14 22:04:36 swildner 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,
145 static char slopt[5][16];
146 static char laddr[16];
147 static char mask[16];
148 char slparmsfile[MAXPATHLEN];
153 environ = restricted_environ; /* minimal protection for system() */
155 strncpy(loginname, name, sizeof(loginname)-1);
156 loginname[sizeof(loginname)-1] = '\0';
158 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
160 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
163 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
166 if (loginargs[0] == '#' || isspace(loginargs[0]))
168 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
169 user, laddr, raddr, mask, slopt[0], slopt[1],
170 slopt[2], slopt[3], slopt[4]);
172 syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS);
175 if (strcmp(user, name) != 0)
181 for (i = 0; i < n - 4; i++) {
182 for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
184 if (strcmp(modes[j].sm_name, slopt[i]) == 0) {
185 slip_mode |= (modes[j].sm_or_flag);
186 slip_mode &= ~(modes[j].sm_and_flag);
193 * we've found the guy we're looking for -- see if
194 * there's a login file we can use. First check for
195 * one specific to this host. If none found, try for
198 snprintf(loginfile, sizeof(loginfile), "%s.%s", _PATH_SLOGIN, name);
199 if (access(loginfile, R_OK|X_OK) != 0) {
200 strncpy(loginfile, _PATH_SLOGIN, sizeof(loginfile)-1);
201 loginfile[sizeof(loginfile)-1] = '\0';
202 if (access(loginfile, R_OK|X_OK)) {
204 "access denied for %s - no %s\n",
209 snprintf(slparmsfile, sizeof(slparmsfile), "%s.%s", _PATH_SLPARMS, name);
210 if (access(slparmsfile, R_OK|X_OK) != 0) {
211 strncpy(slparmsfile, _PATH_SLPARMS, sizeof(slparmsfile)-1);
212 slparmsfile[sizeof(slparmsfile)-1] = '\0';
213 if (access(slparmsfile, R_OK|X_OK))
216 keepal = outfill = 0;
219 if ((fp = fopen(slparmsfile, "r")) == NULL) {
221 syslog(LOG_ERR, "%s: %m\n", slparmsfile);
225 while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
228 if (buf[0] == '#' || isspace(buf[0]))
230 n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit);
233 syslog(LOG_ERR, "%s: wrong format\n", slparmsfile);
245 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
256 case SIGHUP: return("HUP");
257 case SIGINT: return("INT");
258 case SIGQUIT: return("QUIT");
259 case SIGILL: return("ILL");
260 case SIGTRAP: return("TRAP");
261 case SIGIOT: return("IOT");
262 case SIGEMT: return("EMT");
263 case SIGFPE: return("FPE");
264 case SIGKILL: return("KILL");
265 case SIGBUS: return("BUS");
266 case SIGSEGV: return("SEGV");
267 case SIGSYS: return("SYS");
268 case SIGPIPE: return("PIPE");
269 case SIGALRM: return("ALRM");
270 case SIGTERM: return("TERM");
271 case SIGURG: return("URG");
272 case SIGSTOP: return("STOP");
273 case SIGTSTP: return("TSTP");
274 case SIGCONT: return("CONT");
275 case SIGCHLD: return("CHLD");
276 case SIGTTIN: return("TTIN");
277 case SIGTTOU: return("TTOU");
278 case SIGIO: return("IO");
279 case SIGXCPU: return("XCPU");
280 case SIGXFSZ: return("XFSZ");
281 case SIGVTALRM: return("VTALRM");
282 case SIGPROF: return("PROF");
283 case SIGWINCH: return("WINCH");
285 case SIGLOST: return("LOST");
287 case SIGUSR1: return("USR1");
288 case SIGUSR2: return("USR2");
290 snprintf(buf, sizeof(buf), "sig %d", s);
297 char logoutfile[MAXPATHLEN];
301 snprintf(logoutfile, sizeof(logoutfile), "%s.%s", _PATH_SLOGOUT, loginname);
302 if (access(logoutfile, R_OK|X_OK) != 0) {
303 strncpy(logoutfile, _PATH_SLOGOUT, sizeof(logoutfile)-1);
304 logoutfile[sizeof(logoutfile)-1] = '\0';
306 if (access(logoutfile, R_OK|X_OK) == 0) {
307 char logincmd[2*MAXPATHLEN+32];
309 snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", logoutfile, unit, speed, loginargs);
312 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
314 if (unlink(pidfilename) < 0 && errno != ENOENT)
315 syslog(LOG_WARNING, "unable to delete pid file: %m");
316 if (unlink(iffilename) < 0 && errno != ENOENT)
317 syslog(LOG_WARNING, "unable to delete if file: %m");
323 /* Modify the slip line mode and add any compression or no-icmp flags. */
330 /* open a socket as the handle to the interface */
331 s = socket(AF_INET, SOCK_DGRAM, 0);
333 syslog(LOG_ERR, "socket: %m");
336 sprintf(ifr.ifr_name, "sl%d", unit);
338 /* get the flags for the interface */
339 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
340 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
344 /* Assert any compression or no-icmp flags. */
345 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
346 ifr.ifr_flags &= SLMASK;
347 ifr.ifr_flags |= slip_mode;
348 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
349 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
356 main(int argc, char *argv[])
360 struct termios tios, otios;
361 char logincmd[2*BUFSIZ+32];
362 extern uid_t getuid();
364 FILE *pidfile; /* pid file */
365 FILE *iffile; /* interfaces file */
368 char devnam[MAXPATHLEN] = _PATH_TTY; /* Device name */
370 if ((name = strrchr(argv[0], '/')) == NULL)
373 for (fd = 3 ; fd < s ; fd++)
375 openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON);
381 * Disassociate from current controlling terminal, if any,
382 * and ensure that the slip line is our controlling terminal.
385 syslog(LOG_ERR, "daemon(1, 1): %m");
389 if ((fd = open(argv[2], O_RDWR)) == -1) {
390 syslog(LOG_ERR, "open %s: %m", argv[2]);
397 if (ioctl(0, TIOCSCTTY, 0) == -1) {
398 syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
401 if (tcsetpgrp(0, getpid()) < 0) {
402 syslog(LOG_ERR, "tcsetpgrp failed: %m");
406 if ((name = getlogin()) == NULL) {
407 syslog(LOG_ERR, "access denied - login name not found\n");
413 fprintf(stderr, "starting slip login for %s\n", loginname);
414 fprintf(stderr, "your address is %s\n\n", make_ipaddr());
419 /* set up the line parameters */
420 if (tcgetattr(0, &tios) < 0) {
421 syslog(LOG_ERR, "tcgetattr: %m");
426 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
427 syslog(LOG_ERR, "tcsetattr: %m");
430 speed = cfgetispeed(&tios);
433 if (ioctl(0, TIOCSETD, &ldisc) < 0) {
434 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
437 if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) {
438 syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m");
441 /* find out what unit number we were assigned */
442 if (ioctl(0, SLIOCGUNIT, &unit) < 0) {
443 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
446 signal(SIGHUP, hup_handler);
447 signal(SIGTERM, hup_handler);
450 signal(SIGURG, hup_handler);
451 if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) {
452 syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
456 if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) {
457 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
461 /* write pid to file */
463 sprintf(ifname, "sl%d", unit);
464 sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
465 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
466 fprintf(pidfile, "%d\n", pid);
469 syslog(LOG_ERR, "Failed to create pid file %s: %m",
474 /* write interface unit number to file */
478 for (n = strlen(devnam); n > 0; n--)
479 if (devnam[n] == '/') {
483 sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]);
484 if ((iffile = fopen(iffilename, "w")) != NULL) {
485 fprintf(iffile, "sl%d\n", unit);
488 syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename);
493 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
494 snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", loginfile, unit, speed,
497 * aim stdout and errout at /dev/null so logincmd output won't
498 * babble into the slip tty line.
501 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
503 syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL);
512 * Run login and logout scripts as root (real and effective);
513 * current route(8) is setuid root, and checks the real uid
514 * to see whether changes are allowed (or just "route get").
517 if (s = system(logincmd)) {
518 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
519 loginname, s, loginfile);
523 /* Handle any compression or no-icmp flags. */
526 /* reset uid to users' to allow the user to give a signal. */
528 /* twiddle thumbs until we get a signal */