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 * 4. 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.
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
31 static char copyright[] =
32 "@(#) Copyright (c) 1990, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
37 static char sccsid[] = "@(#)sliplogin.c 8.2 (Berkeley) 2/1/94";
38 static char rscid[] = "@(#)$FreeBSD$";
43 * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
45 * This program initializes its own tty port to be an async TCP/IP interface.
46 * It sets the line discipline to slip, invokes a shell script to initialize
47 * the network interface, then pauses forever waiting for hangup.
49 * It is a remote descendant of several similar programs with incestuous ties:
50 * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
51 * - slattach, probably by Rick Adams but touched by countless hordes.
52 * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
54 * There are two forms of usage:
57 * Invoked simply as "sliplogin", the program looks up the username
58 * in the file /etc/slip.hosts.
59 * If an entry is found, the line on fd0 is configured for SLIP operation
60 * as specified in the file.
62 * "sliplogin IPhostlogin </dev/ttyb"
63 * Invoked by root with a username, the name is looked up in the
64 * /etc/slip.hosts file and if found fd0 is configured as in case 1.
67 #include <sys/param.h>
68 #include <sys/socket.h>
75 #include <sys/ioctl.h>
78 #include <netinet/in.h>
88 #include "pathnames.h"
90 extern char **environ;
92 static char *restricted_environ[] = {
93 "PATH=" _PATH_STDPATH,
104 char loginargs[BUFSIZ];
105 char loginfile[MAXPATHLEN];
106 char loginname[BUFSIZ];
107 static char raddr[32]; /* remote address */
108 char ifname[IFNAMSIZ]; /* interface name */
109 static char pidfilename[MAXPATHLEN]; /* name of pid file */
110 static char iffilename[MAXPATHLEN]; /* name of if file */
111 static pid_t pid; /* our pid */
116 static char address[20] ="";
118 unsigned long ipaddr;
121 if ((he = gethostbyname(raddr)) != NULL) {
122 ipaddr = ntohl(*(long *)he->h_addr_list[0]);
123 sprintf(address, "%lu.%lu.%lu.%lu",
125 (ipaddr & 0x00ff0000) >> 16,
126 (ipaddr & 0x0000ff00) >> 8,
127 (ipaddr & 0x000000ff));
139 "compress", IFF_LINK0, IFF_LINK2,
140 "noicmp", IFF_LINK1, 0 ,
141 "autocomp", IFF_LINK2, IFF_LINK0,
149 static char slopt[5][16];
150 static char laddr[16];
151 static char mask[16];
152 char slparmsfile[MAXPATHLEN];
157 environ = restricted_environ; /* minimal protection for system() */
159 (void)strncpy(loginname, name, sizeof(loginname)-1);
160 loginname[sizeof(loginname)-1] = '\0';
162 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
164 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
167 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
170 if (loginargs[0] == '#' || isspace(loginargs[0]))
172 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
173 user, laddr, raddr, mask, slopt[0], slopt[1],
174 slopt[2], slopt[3], slopt[4]);
176 syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS);
179 if (strcmp(user, name) != 0)
185 for (i = 0; i < n - 4; i++) {
186 for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
188 if (strcmp(modes[j].sm_name, slopt[i]) == 0) {
189 slip_mode |= (modes[j].sm_or_flag);
190 slip_mode &= ~(modes[j].sm_and_flag);
197 * we've found the guy we're looking for -- see if
198 * there's a login file we can use. First check for
199 * one specific to this host. If none found, try for
202 (void)snprintf(loginfile, sizeof(loginfile), "%s.%s",
203 _PATH_SLIP_LOGIN, name);
204 if (access(loginfile, R_OK|X_OK) != 0) {
205 (void)strncpy(loginfile, _PATH_SLIP_LOGIN,
206 sizeof(loginfile) - 1);
207 loginfile[sizeof(loginfile) - 1] = '\0';
208 if (access(loginfile, R_OK|X_OK)) {
210 "access denied for %s - no %s\n",
211 name, _PATH_SLIP_LOGIN);
215 (void)snprintf(slparmsfile, sizeof(slparmsfile), "%s.%s", _PATH_SLPARMS, name);
216 if (access(slparmsfile, R_OK|X_OK) != 0) {
217 (void)strncpy(slparmsfile, _PATH_SLPARMS, sizeof(slparmsfile)-1);
218 slparmsfile[sizeof(slparmsfile)-1] = '\0';
219 if (access(slparmsfile, R_OK|X_OK))
222 keepal = outfill = 0;
225 if ((fp = fopen(slparmsfile, "r")) == NULL) {
227 syslog(LOG_ERR, "%s: %m\n", slparmsfile);
231 while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
234 if (buf[0] == '#' || isspace(buf[0]))
236 n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit);
239 syslog(LOG_ERR, "%s: wrong format\n", slparmsfile);
251 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
263 case SIGHUP: return("HUP");
264 case SIGINT: return("INT");
265 case SIGQUIT: return("QUIT");
266 case SIGILL: return("ILL");
267 case SIGTRAP: return("TRAP");
268 case SIGIOT: return("IOT");
269 case SIGEMT: return("EMT");
270 case SIGFPE: return("FPE");
271 case SIGKILL: return("KILL");
272 case SIGBUS: return("BUS");
273 case SIGSEGV: return("SEGV");
274 case SIGSYS: return("SYS");
275 case SIGPIPE: return("PIPE");
276 case SIGALRM: return("ALRM");
277 case SIGTERM: return("TERM");
278 case SIGURG: return("URG");
279 case SIGSTOP: return("STOP");
280 case SIGTSTP: return("TSTP");
281 case SIGCONT: return("CONT");
282 case SIGCHLD: return("CHLD");
283 case SIGTTIN: return("TTIN");
284 case SIGTTOU: return("TTOU");
285 case SIGIO: return("IO");
286 case SIGXCPU: return("XCPU");
287 case SIGXFSZ: return("XFSZ");
288 case SIGVTALRM: return("VTALRM");
289 case SIGPROF: return("PROF");
290 case SIGWINCH: return("WINCH");
292 case SIGLOST: return("LOST");
294 case SIGUSR1: return("USR1");
295 case SIGUSR2: return("USR2");
297 (void)snprintf(buf, sizeof(buf), "sig %d", s);
305 char logoutfile[MAXPATHLEN];
309 (void)snprintf(logoutfile, sizeof(logoutfile), "%s.%s",
310 _PATH_SLIP_LOGOUT, loginname);
311 if (access(logoutfile, R_OK|X_OK) != 0) {
312 (void)strncpy(logoutfile, _PATH_SLIP_LOGOUT,
313 sizeof(logoutfile) - 1);
314 logoutfile[sizeof(logoutfile) - 1] = '\0';
316 if (access(logoutfile, R_OK|X_OK) == 0) {
317 char logincmd[2*MAXPATHLEN+32];
319 (void) snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", logoutfile, unit, speed, loginargs);
320 (void) system(logincmd);
322 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
324 if (unlink(pidfilename) < 0 && errno != ENOENT)
325 syslog(LOG_WARNING, "unable to delete pid file: %m");
326 if (unlink(iffilename) < 0 && errno != ENOENT)
327 syslog(LOG_WARNING, "unable to delete if file: %m");
333 /* Modify the slip line mode and add any compression or no-icmp flags. */
334 void line_flags(unit)
340 /* open a socket as the handle to the interface */
341 s = socket(AF_INET, SOCK_DGRAM, 0);
343 syslog(LOG_ERR, "socket: %m");
346 sprintf(ifr.ifr_name, "sl%d", unit);
348 /* get the flags for the interface */
349 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
350 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
354 /* Assert any compression or no-icmp flags. */
355 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
356 ifr.ifr_flags &= SLMASK;
357 ifr.ifr_flags |= slip_mode;
358 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
359 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
374 char logincmd[2*BUFSIZ+32];
376 FILE *pidfile; /* pid file */
377 FILE *iffile; /* interfaces file */
380 char devnam[MAXPATHLEN] = _PATH_TTY; /* Device name */
382 if ((name = strrchr(argv[0], '/')) == NULL)
385 for (fd = 3 ; fd < s ; fd++)
387 openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON);
393 * Disassociate from current controlling terminal, if any,
394 * and ensure that the slip line is our controlling terminal.
397 syslog(LOG_ERR, "daemon(1, 1): %m");
401 if ((fd = open(argv[2], O_RDWR)) == -1) {
402 syslog(LOG_ERR, "open %s: %m", argv[2]);
409 if (ioctl(0, TIOCSCTTY, 0) == -1) {
410 syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
413 if (tcsetpgrp(0, getpid()) < 0) {
414 syslog(LOG_ERR, "tcsetpgrp failed: %m");
418 if ((name = getlogin()) == NULL) {
419 syslog(LOG_ERR, "access denied - login name not found\n");
424 (void) fchmod(0, 0600);
425 (void) fprintf(stderr, "starting slip login for %s\n", loginname);
426 (void) fprintf(stderr, "your address is %s\n\n", make_ipaddr());
428 (void) fflush(stderr);
431 /* set up the line parameters */
432 if (tcgetattr(0, &tios) < 0) {
433 syslog(LOG_ERR, "tcgetattr: %m");
437 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
438 syslog(LOG_ERR, "tcsetattr: %m");
441 speed = cfgetispeed(&tios);
444 if (ioctl(0, TIOCSETD, &ldisc) < 0) {
445 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
448 if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) {
449 syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m");
452 /* find out what unit number we were assigned */
453 if (ioctl(0, SLIOCGUNIT, &unit) < 0) {
454 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
457 (void) signal(SIGHUP, hup_handler);
458 (void) signal(SIGTERM, hup_handler);
461 (void) signal(SIGURG, hup_handler);
462 if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) {
463 syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
467 if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) {
468 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
472 /* write pid to file */
474 (void) sprintf(ifname, "sl%d", unit);
475 (void) sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
476 if ((pidfile = fopen(pidfilename, "w")) != NULL) {
477 fprintf(pidfile, "%d\n", pid);
478 (void) fclose(pidfile);
480 syslog(LOG_ERR, "Failed to create pid file %s: %m",
485 /* write interface unit number to file */
489 for (n = strlen(devnam); n > 0; n--)
490 if (devnam[n] == '/') {
494 (void) sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]);
495 if ((iffile = fopen(iffilename, "w")) != NULL) {
496 fprintf(iffile, "sl%d\n", unit);
497 (void) fclose(iffile);
499 syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename);
504 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
505 (void)snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", loginfile, unit, speed,
508 * aim stdout and errout at /dev/null so logincmd output won't
509 * babble into the slip tty line.
512 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
514 syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL);
523 * Run login and logout scripts as root (real and effective);
524 * current route(8) is setuid root, and checks the real uid
525 * to see whether changes are allowed (or just "route get").
528 if (s = system(logincmd)) {
529 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
530 loginname, s, loginfile);
534 /* Handle any compression or no-icmp flags. */
537 /* reset uid to users' to allow the user to give a signal. */
539 /* twiddle thumbs until we get a signal */