1*0a77b69aSchristos /* $NetBSD: sliplogin.c,v 1.24 2013/10/19 17:16:38 christos Exp $ */
218b882a9Smrg
361f28255Scgd /*-
418b882a9Smrg * Copyright (c) 1990, 1993
518b882a9Smrg * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15326b2259Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32745600d8Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
349c194566Slukem __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
359c194566Slukem The Regents of the University of California. All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd
3861f28255Scgd #ifndef lint
3918b882a9Smrg #if 0
4018b882a9Smrg static char sccsid[] = "@(#)sliplogin.c 8.2 (Berkeley) 2/1/94";
4118b882a9Smrg #else
42*0a77b69aSchristos __RCSID("$NetBSD: sliplogin.c,v 1.24 2013/10/19 17:16:38 christos Exp $");
4318b882a9Smrg #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd
4661f28255Scgd /*
4761f28255Scgd * sliplogin.c
4861f28255Scgd * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
4961f28255Scgd *
5061f28255Scgd * This program initializes its own tty port to be an async TCP/IP interface.
5161f28255Scgd * It sets the line discipline to slip, invokes a shell script to initialize
5261f28255Scgd * the network interface, then pauses forever waiting for hangup.
5361f28255Scgd *
5461f28255Scgd * It is a remote descendant of several similar programs with incestuous ties:
5561f28255Scgd * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
5661f28255Scgd * - slattach, probably by Rick Adams but touched by countless hordes.
5761f28255Scgd * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
5861f28255Scgd *
5961f28255Scgd * There are two forms of usage:
6061f28255Scgd *
6161f28255Scgd * "sliplogin"
6261f28255Scgd * Invoked simply as "sliplogin", the program looks up the username
6361f28255Scgd * in the file /etc/slip.hosts.
6461f28255Scgd * If an entry is found, the line on fd0 is configured for SLIP operation
6561f28255Scgd * as specified in the file.
6661f28255Scgd *
6761f28255Scgd * "sliplogin IPhostlogin </dev/ttyb"
6861f28255Scgd * Invoked by root with a username, the name is looked up in the
6961f28255Scgd * /etc/slip.hosts file and if found fd0 is configured as in case 1.
7061f28255Scgd */
7161f28255Scgd
72745600d8Slukem #include <sys/types.h>
73745600d8Slukem #include <sys/file.h>
7461f28255Scgd #include <sys/param.h>
7561f28255Scgd #include <sys/socket.h>
76745600d8Slukem #include <sys/stat.h>
7761f28255Scgd #include <sys/syslog.h>
7861f28255Scgd
7961f28255Scgd #if BSD >= 199006
8061f28255Scgd #define POSIX
8161f28255Scgd #endif
8261f28255Scgd #ifdef POSIX
831847eaedSjtc #include <termios.h>
8461f28255Scgd #include <sys/ioctl.h>
8561f28255Scgd #include <ttyent.h>
8661f28255Scgd #else
8761f28255Scgd #include <sgtty.h>
8861f28255Scgd #endif
89532d2bb3Scgd #include <net/slip.h>
9061f28255Scgd
9161f28255Scgd #include <ctype.h>
920099f993Scgd #include <err.h>
930099f993Scgd #include <errno.h>
945a91a662Smrg #include <netdb.h>
955a91a662Smrg #include <signal.h>
960099f993Scgd #include <stdio.h>
975a91a662Smrg #include <stdlib.h>
9861f28255Scgd #include <string.h>
991676bc34Smycroft #include <unistd.h>
1005a91a662Smrg
10161f28255Scgd #include "pathnames.h"
10261f28255Scgd
103f796930cSjoerg static int unit;
104f796930cSjoerg static int speed;
105f796930cSjoerg static int uid;
106f796930cSjoerg static char loginargs[BUFSIZ];
107f796930cSjoerg static char loginfile[MAXPATHLEN];
108f796930cSjoerg static char loginname[BUFSIZ];
10961f28255Scgd
110f796930cSjoerg static void findid(const char *);
111f796930cSjoerg __dead static void hup_handler(int);
112f796930cSjoerg static const char *sigstr(int);
113745600d8Slukem
114f796930cSjoerg static void
findid(const char * name)115f796930cSjoerg findid(const char *name)
11661f28255Scgd {
11761f28255Scgd FILE *fp;
11861f28255Scgd static char slopt[5][16];
11961f28255Scgd static char laddr[16];
12061f28255Scgd static char raddr[16];
12161f28255Scgd static char mask[16];
12261f28255Scgd char user[16];
12361f28255Scgd
124881cb512Sitojun (void)strlcpy(loginname, name, sizeof(loginname));
12561f28255Scgd if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
12661f28255Scgd syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
1270099f993Scgd err(1, "%s", _PATH_ACCESS);
12861f28255Scgd }
12961f28255Scgd while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
13061f28255Scgd if (ferror(fp))
13161f28255Scgd break;
132*0a77b69aSchristos (void)sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]"
133*0a77b69aSchristos "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s"
134*0a77b69aSchristos "%*[ \t]%15s\n",
13561f28255Scgd user, laddr, raddr, mask, slopt[0], slopt[1],
13661f28255Scgd slopt[2], slopt[3], slopt[4]);
1379122339bSdsl if (user[0] == '#' || isspace((unsigned char)user[0]))
13861f28255Scgd continue;
13961f28255Scgd if (strcmp(user, name) != 0)
14061f28255Scgd continue;
14161f28255Scgd
14261f28255Scgd /*
14361f28255Scgd * we've found the guy we're looking for -- see if
14461f28255Scgd * there's a login file we can use. First check for
14561f28255Scgd * one specific to this host. If none found, try for
14661f28255Scgd * a generic one.
14761f28255Scgd */
148a9437d11Smrg (void)snprintf(loginfile, sizeof loginfile, "%s.%s",
149a9437d11Smrg _PATH_LOGIN, name);
15061f28255Scgd if (access(loginfile, R_OK|X_OK) != 0) {
151881cb512Sitojun (void)strlcpy(loginfile, _PATH_LOGIN, sizeof(loginfile));
15261f28255Scgd if (access(loginfile, R_OK|X_OK)) {
15361f28255Scgd fputs("access denied - no login file\n",
15461f28255Scgd stderr);
15561f28255Scgd syslog(LOG_ERR,
15661f28255Scgd "access denied for %s - no %s\n",
15761f28255Scgd name, _PATH_LOGIN);
15861f28255Scgd exit(5);
15961f28255Scgd }
16061f28255Scgd }
16161f28255Scgd
16261f28255Scgd (void)fclose(fp);
16361f28255Scgd return;
16461f28255Scgd }
16561f28255Scgd syslog(LOG_ERR, "SLIP access denied for %s\n", name);
1660099f993Scgd errx(1, "SLIP access denied for %s", name);
16761f28255Scgd /* NOTREACHED */
16861f28255Scgd }
16961f28255Scgd
170f796930cSjoerg static const char *
sigstr(int s)171f796930cSjoerg sigstr(int s)
17261f28255Scgd {
1735a91a662Smrg
1741676bc34Smycroft if (s > 0 && s < NSIG)
1751676bc34Smycroft return (sys_signame[s]);
1761676bc34Smycroft else {
17761f28255Scgd static char buf[32];
178a9437d11Smrg
179a9437d11Smrg (void)snprintf(buf, sizeof buf, "sig %d", s);
18061f28255Scgd return (buf);
18161f28255Scgd }
1821676bc34Smycroft }
18361f28255Scgd
184f796930cSjoerg static void
hup_handler(int s)185f796930cSjoerg hup_handler(int s)
18661f28255Scgd {
18761f28255Scgd char logoutfile[MAXPATHLEN];
18861f28255Scgd
189a9437d11Smrg (void)snprintf(logoutfile, sizeof logoutfile, "%s.%s", _PATH_LOGOUT,
190a9437d11Smrg loginname);
19161f28255Scgd if (access(logoutfile, R_OK|X_OK) != 0)
192881cb512Sitojun (void)strlcpy(logoutfile, _PATH_LOGOUT, sizeof(logoutfile));
19361f28255Scgd if (access(logoutfile, R_OK|X_OK) == 0) {
19461f28255Scgd char logincmd[2*MAXPATHLEN+32];
19561f28255Scgd
196a9437d11Smrg (void)snprintf(logincmd, sizeof logincmd, "%s %d %d %s",
197a9437d11Smrg logoutfile, unit, speed, loginargs);
19861f28255Scgd (void)system(logincmd);
19961f28255Scgd }
20061f28255Scgd (void)close(0);
20161f28255Scgd syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
20261f28255Scgd sigstr(s));
20361f28255Scgd exit(1);
20461f28255Scgd /* NOTREACHED */
20561f28255Scgd }
20661f28255Scgd
207745600d8Slukem int
main(int argc,char * argv[])208f796930cSjoerg main(int argc, char *argv[])
20961f28255Scgd {
21061f28255Scgd int fd, s, ldisc, odisc;
21161f28255Scgd char *name;
21261f28255Scgd #ifdef POSIX
21361f28255Scgd struct termios tios, otios;
21461f28255Scgd #else
21561f28255Scgd struct sgttyb tty, otty;
21661f28255Scgd #endif
21761f28255Scgd char logincmd[2*BUFSIZ+32];
21861f28255Scgd
2199bc65afcShubertf if (strlen(argv[0]) > MAXLOGNAME)
2209bc65afcShubertf errx(1, "login %s too long", argv[0]);
22161f28255Scgd if ((name = strrchr(argv[0], '/')) == NULL)
22261f28255Scgd name = argv[0];
223f0ccfb9aSlukem else
224f0ccfb9aSlukem name++;
22561f28255Scgd s = getdtablesize();
22661f28255Scgd for (fd = 3 ; fd < s ; fd++)
22761f28255Scgd (void)close(fd);
22861f28255Scgd openlog(name, LOG_PID, LOG_DAEMON);
22961f28255Scgd uid = getuid();
23061f28255Scgd if (argc > 1) {
23161f28255Scgd findid(argv[1]);
23261f28255Scgd
23361f28255Scgd /*
23461f28255Scgd * Disassociate from current controlling terminal, if any,
23561f28255Scgd * and ensure that the slip line is our controlling terminal.
23661f28255Scgd */
23761f28255Scgd #ifdef POSIX
2385a91a662Smrg switch (fork()) {
2395a91a662Smrg case -1:
2405a91a662Smrg perror("fork");
2415a91a662Smrg syslog(LOG_ERR, "could not fork: %m");
2425a91a662Smrg exit(1);
2435a91a662Smrg case 0:
2445a91a662Smrg break;
2455a91a662Smrg default:
24661f28255Scgd exit(0);
2475a91a662Smrg }
2483558cc99Smycroft if (setsid() < 0)
24961f28255Scgd perror("setsid");
25061f28255Scgd #else
25161f28255Scgd if ((fd = open("/dev/tty", O_RDONLY, 0)) >= 0) {
25261f28255Scgd extern char *ttyname();
25361f28255Scgd
25461f28255Scgd (void)ioctl(fd, TIOCNOTTY, (caddr_t)0);
25561f28255Scgd (void)close(fd);
25661f28255Scgd /* open slip tty again to acquire as controlling tty? */
25761f28255Scgd fd = open(ttyname(0), O_RDWR, 0);
25861f28255Scgd if (fd >= 0)
25961f28255Scgd (void)close(fd);
26061f28255Scgd }
26161f28255Scgd (void)setpgrp(0, getpid());
26261f28255Scgd #endif
26361f28255Scgd if (argc > 2) {
26461f28255Scgd if ((fd = open(argv[2], O_RDWR)) == -1) {
26561f28255Scgd perror(argv[2]);
26661f28255Scgd exit(2);
26761f28255Scgd }
26861f28255Scgd (void)dup2(fd, 0);
26961f28255Scgd if (fd > 2)
27061f28255Scgd close(fd);
27161f28255Scgd }
27261f28255Scgd #ifdef TIOCSCTTY
2730099f993Scgd if (ioctl(STDIN_FILENO, TIOCSCTTY, (caddr_t)0) != 0)
27461f28255Scgd perror("ioctl (TIOCSCTTY)");
27561f28255Scgd #endif
27661f28255Scgd } else {
27761f28255Scgd if ((name = getlogin()) == NULL) {
2780099f993Scgd syslog(LOG_ERR,
2790099f993Scgd "access denied - getlogin returned 0\n");
2800099f993Scgd errx(1, "access denied - no username");
28161f28255Scgd }
28261f28255Scgd findid(name);
28361f28255Scgd }
2840099f993Scgd if (!isatty(STDIN_FILENO)) {
2850099f993Scgd syslog(LOG_ERR, "stdin not a tty");
2860099f993Scgd errx(1, "stdin not a tty");
2870099f993Scgd }
2880099f993Scgd (void)fchmod(STDIN_FILENO, 0600);
2890099f993Scgd warnx("starting slip login for %s", loginname);
29061f28255Scgd #ifdef POSIX
29161f28255Scgd /* set up the line parameters */
2920099f993Scgd if (tcgetattr(STDIN_FILENO, &tios) < 0) {
29361f28255Scgd syslog(LOG_ERR, "tcgetattr: %m");
29461f28255Scgd exit(1);
29561f28255Scgd }
29661f28255Scgd otios = tios;
29761f28255Scgd cfmakeraw(&tios);
29861f28255Scgd tios.c_iflag &= ~IMAXBEL;
2990099f993Scgd if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios) < 0) {
30061f28255Scgd syslog(LOG_ERR, "tcsetattr: %m");
30161f28255Scgd exit(1);
30261f28255Scgd }
30361f28255Scgd speed = cfgetispeed(&tios);
30461f28255Scgd #else
30561f28255Scgd /* set up the line parameters */
3060099f993Scgd if (ioctl(STDIN_FILENO, TIOCGETP, (caddr_t)&tty) < 0) {
30761f28255Scgd syslog(LOG_ERR, "ioctl (TIOCGETP): %m");
30861f28255Scgd exit(1);
30961f28255Scgd }
31061f28255Scgd otty = tty;
31161f28255Scgd speed = tty.sg_ispeed;
31261f28255Scgd tty.sg_flags = RAW | ANYP;
3130099f993Scgd if (ioctl(STDIN_FILENO, TIOCSETP, (caddr_t)&tty) < 0) {
31461f28255Scgd syslog(LOG_ERR, "ioctl (TIOCSETP): %m");
31561f28255Scgd exit(1);
31661f28255Scgd }
31761f28255Scgd #endif
31861f28255Scgd /* find out what ldisc we started with */
3190099f993Scgd if (ioctl(STDIN_FILENO, TIOCGETD, (caddr_t)&odisc) < 0) {
32061f28255Scgd syslog(LOG_ERR, "ioctl(TIOCGETD) (1): %m");
32161f28255Scgd exit(1);
32261f28255Scgd }
32361f28255Scgd ldisc = SLIPDISC;
3240099f993Scgd if (ioctl(STDIN_FILENO, TIOCSETD, (caddr_t)&ldisc) < 0) {
32561f28255Scgd syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
32661f28255Scgd exit(1);
32761f28255Scgd }
32861f28255Scgd /* find out what unit number we were assigned */
3290099f993Scgd if (ioctl(STDIN_FILENO, SLIOCGUNIT, (caddr_t)&unit) < 0) {
33061f28255Scgd syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
33161f28255Scgd exit(1);
33261f28255Scgd }
33361f28255Scgd (void)signal(SIGHUP, hup_handler);
33461f28255Scgd (void)signal(SIGTERM, hup_handler);
33561f28255Scgd
33661f28255Scgd syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
337a9437d11Smrg (void)snprintf(logincmd, sizeof logincmd, "%s %d %d %s", loginfile,
338a9437d11Smrg unit, speed, loginargs);
33961f28255Scgd /*
34061f28255Scgd * aim stdout and errout at /dev/null so logincmd output won't
34161f28255Scgd * babble into the slip tty line.
34261f28255Scgd */
34361f28255Scgd (void)close(1);
34461f28255Scgd if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
34561f28255Scgd if (fd < 0) {
34661f28255Scgd syslog(LOG_ERR, "open /dev/null: %m");
34761f28255Scgd exit(1);
34861f28255Scgd }
34961f28255Scgd (void)dup2(fd, 1);
35061f28255Scgd (void)close(fd);
35161f28255Scgd }
35261f28255Scgd (void)dup2(1, 2);
35361f28255Scgd
35461f28255Scgd /*
35561f28255Scgd * Run login and logout scripts as root (real and effective);
35661f28255Scgd * current route(8) is setuid root, and checks the real uid
35761f28255Scgd * to see whether changes are allowed (or just "route get").
35861f28255Scgd */
35961f28255Scgd (void)setuid(0);
360cddedd60Sfvdl if ((s = system(logincmd)) != 0) {
36161f28255Scgd syslog(LOG_ERR, "%s login failed: exit status %d from %s",
36261f28255Scgd loginname, s, loginfile);
3630099f993Scgd (void)ioctl(STDIN_FILENO, TIOCSETD, (caddr_t)&odisc);
364ddd4b762Scgd #ifdef POSIX
3650099f993Scgd (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &otios);
366ddd4b762Scgd #else
3670099f993Scgd (void)ioctl(STDIN_FILENO, TIOCSETP, (caddr_t)&otty);
368ddd4b762Scgd #endif
36961f28255Scgd exit(6);
37061f28255Scgd }
37161f28255Scgd
37261f28255Scgd /* twiddle thumbs until we get a signal */
37361f28255Scgd while (1)
37461f28255Scgd sigpause(0);
37561f28255Scgd
37661f28255Scgd /* NOTREACHED */
37761f28255Scgd }
378