1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1990, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
12 #endif /* not lint */
13
14 #ifndef lint
15 static char sccsid[] = "@(#)sliplogin.c 8.2 (Berkeley) 02/01/94";
16 #endif /* not lint */
17
18 /*
19 * sliplogin.c
20 * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
21 *
22 * This program initializes its own tty port to be an async TCP/IP interface.
23 * It sets the line discipline to slip, invokes a shell script to initialize
24 * the network interface, then pauses forever waiting for hangup.
25 *
26 * It is a remote descendant of several similar programs with incestuous ties:
27 * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
28 * - slattach, probably by Rick Adams but touched by countless hordes.
29 * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
30 *
31 * There are two forms of usage:
32 *
33 * "sliplogin"
34 * Invoked simply as "sliplogin", the program looks up the username
35 * in the file /etc/slip.hosts.
36 * If an entry is found, the line on fd0 is configured for SLIP operation
37 * as specified in the file.
38 *
39 * "sliplogin IPhostlogin </dev/ttyb"
40 * Invoked by root with a username, the name is looked up in the
41 * /etc/slip.hosts file and if found fd0 is configured as in case 1.
42 */
43
44 #include <sys/param.h>
45 #include <sys/socket.h>
46 #include <sys/signal.h>
47 #include <sys/file.h>
48 #include <sys/syslog.h>
49 #include <netdb.h>
50
51 #if BSD >= 199006
52 #define POSIX
53 #endif
54 #ifdef POSIX
55 #include <sys/termios.h>
56 #include <sys/ioctl.h>
57 #include <ttyent.h>
58 #else
59 #include <sgtty.h>
60 #endif
61 #include <net/slip.h>
62
63 #include <stdio.h>
64 #include <errno.h>
65 #include <ctype.h>
66 #include <string.h>
67 #include "pathnames.h"
68
69 int unit;
70 int speed;
71 int uid;
72 char loginargs[BUFSIZ];
73 char loginfile[MAXPATHLEN];
74 char loginname[BUFSIZ];
75
76 void
findid(name)77 findid(name)
78 char *name;
79 {
80 FILE *fp;
81 static char slopt[5][16];
82 static char laddr[16];
83 static char raddr[16];
84 static char mask[16];
85 char user[16];
86 int i, j, n;
87
88 (void)strcpy(loginname, name);
89 if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
90 (void)fprintf(stderr, "sliplogin: %s: %s\n",
91 _PATH_ACCESS, strerror(errno));
92 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
93 exit(1);
94 }
95 while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
96 if (ferror(fp))
97 break;
98 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
99 user, laddr, raddr, mask, slopt[0], slopt[1],
100 slopt[2], slopt[3], slopt[4]);
101 if (user[0] == '#' || isspace(user[0]))
102 continue;
103 if (strcmp(user, name) != 0)
104 continue;
105
106 /*
107 * we've found the guy we're looking for -- see if
108 * there's a login file we can use. First check for
109 * one specific to this host. If none found, try for
110 * a generic one.
111 */
112 (void)sprintf(loginfile, "%s.%s", _PATH_LOGIN, name);
113 if (access(loginfile, R_OK|X_OK) != 0) {
114 (void)strcpy(loginfile, _PATH_LOGIN);
115 if (access(loginfile, R_OK|X_OK)) {
116 fputs("access denied - no login file\n",
117 stderr);
118 syslog(LOG_ERR,
119 "access denied for %s - no %s\n",
120 name, _PATH_LOGIN);
121 exit(5);
122 }
123 }
124
125 (void) fclose(fp);
126 return;
127 }
128 (void)fprintf(stderr, "SLIP access denied for %s\n", name);
129 syslog(LOG_ERR, "SLIP access denied for %s\n", name);
130 exit(4);
131 /* NOTREACHED */
132 }
133
134 char *
sigstr(s)135 sigstr(s)
136 int s;
137 {
138 static char buf[32];
139
140 switch (s) {
141 case SIGHUP: return("HUP");
142 case SIGINT: return("INT");
143 case SIGQUIT: return("QUIT");
144 case SIGILL: return("ILL");
145 case SIGTRAP: return("TRAP");
146 case SIGIOT: return("IOT");
147 case SIGEMT: return("EMT");
148 case SIGFPE: return("FPE");
149 case SIGKILL: return("KILL");
150 case SIGBUS: return("BUS");
151 case SIGSEGV: return("SEGV");
152 case SIGSYS: return("SYS");
153 case SIGPIPE: return("PIPE");
154 case SIGALRM: return("ALRM");
155 case SIGTERM: return("TERM");
156 case SIGURG: return("URG");
157 case SIGSTOP: return("STOP");
158 case SIGTSTP: return("TSTP");
159 case SIGCONT: return("CONT");
160 case SIGCHLD: return("CHLD");
161 case SIGTTIN: return("TTIN");
162 case SIGTTOU: return("TTOU");
163 case SIGIO: return("IO");
164 case SIGXCPU: return("XCPU");
165 case SIGXFSZ: return("XFSZ");
166 case SIGVTALRM: return("VTALRM");
167 case SIGPROF: return("PROF");
168 case SIGWINCH: return("WINCH");
169 #ifdef SIGLOST
170 case SIGLOST: return("LOST");
171 #endif
172 case SIGUSR1: return("USR1");
173 case SIGUSR2: return("USR2");
174 }
175 (void)sprintf(buf, "sig %d", s);
176 return(buf);
177 }
178
179 void
hup_handler(s)180 hup_handler(s)
181 int s;
182 {
183 char logoutfile[MAXPATHLEN];
184
185 (void)sprintf(logoutfile, "%s.%s", _PATH_LOGOUT, loginname);
186 if (access(logoutfile, R_OK|X_OK) != 0)
187 (void)strcpy(logoutfile, _PATH_LOGOUT);
188 if (access(logoutfile, R_OK|X_OK) == 0) {
189 char logincmd[2*MAXPATHLEN+32];
190
191 (void) sprintf(logincmd, "%s %d %d %s", logoutfile, unit, speed,
192 loginargs);
193 (void) system(logincmd);
194 }
195 (void) close(0);
196 syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
197 sigstr(s));
198 exit(1);
199 /* NOTREACHED */
200 }
201
main(argc,argv)202 main(argc, argv)
203 int argc;
204 char *argv[];
205 {
206 int fd, s, ldisc, odisc;
207 char *name;
208 #ifdef POSIX
209 struct termios tios, otios;
210 #else
211 struct sgttyb tty, otty;
212 #endif
213 char logincmd[2*BUFSIZ+32];
214 extern uid_t getuid();
215
216 if ((name = strrchr(argv[0], '/')) == NULL)
217 name = argv[0];
218 s = getdtablesize();
219 for (fd = 3 ; fd < s ; fd++)
220 (void) close(fd);
221 openlog(name, LOG_PID, LOG_DAEMON);
222 uid = getuid();
223 if (argc > 1) {
224 findid(argv[1]);
225
226 /*
227 * Disassociate from current controlling terminal, if any,
228 * and ensure that the slip line is our controlling terminal.
229 */
230 #ifdef POSIX
231 if (fork() > 0)
232 exit(0);
233 if (setsid() != 0)
234 perror("setsid");
235 #else
236 if ((fd = open("/dev/tty", O_RDONLY, 0)) >= 0) {
237 extern char *ttyname();
238
239 (void) ioctl(fd, TIOCNOTTY, (caddr_t)0);
240 (void) close(fd);
241 /* open slip tty again to acquire as controlling tty? */
242 fd = open(ttyname(0), O_RDWR, 0);
243 if (fd >= 0)
244 (void) close(fd);
245 }
246 (void) setpgrp(0, getpid());
247 #endif
248 if (argc > 2) {
249 if ((fd = open(argv[2], O_RDWR)) == -1) {
250 perror(argv[2]);
251 exit(2);
252 }
253 (void) dup2(fd, 0);
254 if (fd > 2)
255 close(fd);
256 }
257 #ifdef TIOCSCTTY
258 if (ioctl(0, TIOCSCTTY, (caddr_t)0) != 0)
259 perror("ioctl (TIOCSCTTY)");
260 #endif
261 } else {
262 extern char *getlogin();
263
264 if ((name = getlogin()) == NULL) {
265 (void) fprintf(stderr, "access denied - no username\n");
266 syslog(LOG_ERR, "access denied - getlogin returned 0\n");
267 exit(1);
268 }
269 findid(name);
270 }
271 (void) fchmod(0, 0600);
272 (void) fprintf(stderr, "starting slip login for %s\n", loginname);
273 #ifdef POSIX
274 /* set up the line parameters */
275 if (tcgetattr(0, &tios) < 0) {
276 syslog(LOG_ERR, "tcgetattr: %m");
277 exit(1);
278 }
279 otios = tios;
280 cfmakeraw(&tios);
281 tios.c_iflag &= ~IMAXBEL;
282 if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
283 syslog(LOG_ERR, "tcsetattr: %m");
284 exit(1);
285 }
286 speed = cfgetispeed(&tios);
287 #else
288 /* set up the line parameters */
289 if (ioctl(0, TIOCGETP, (caddr_t)&tty) < 0) {
290 syslog(LOG_ERR, "ioctl (TIOCGETP): %m");
291 exit(1);
292 }
293 otty = tty;
294 speed = tty.sg_ispeed;
295 tty.sg_flags = RAW | ANYP;
296 if (ioctl(0, TIOCSETP, (caddr_t)&tty) < 0) {
297 syslog(LOG_ERR, "ioctl (TIOCSETP): %m");
298 exit(1);
299 }
300 #endif
301 /* find out what ldisc we started with */
302 if (ioctl(0, TIOCGETD, (caddr_t)&odisc) < 0) {
303 syslog(LOG_ERR, "ioctl(TIOCGETD) (1): %m");
304 exit(1);
305 }
306 ldisc = SLIPDISC;
307 if (ioctl(0, TIOCSETD, (caddr_t)&ldisc) < 0) {
308 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
309 exit(1);
310 }
311 /* find out what unit number we were assigned */
312 if (ioctl(0, SLIOCGUNIT, (caddr_t)&unit) < 0) {
313 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
314 exit(1);
315 }
316 (void) signal(SIGHUP, hup_handler);
317 (void) signal(SIGTERM, hup_handler);
318
319 syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
320 (void)sprintf(logincmd, "%s %d %d %s", loginfile, unit, speed,
321 loginargs);
322 /*
323 * aim stdout and errout at /dev/null so logincmd output won't
324 * babble into the slip tty line.
325 */
326 (void) close(1);
327 if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
328 if (fd < 0) {
329 syslog(LOG_ERR, "open /dev/null: %m");
330 exit(1);
331 }
332 (void) dup2(fd, 1);
333 (void) close(fd);
334 }
335 (void) dup2(1, 2);
336
337 /*
338 * Run login and logout scripts as root (real and effective);
339 * current route(8) is setuid root, and checks the real uid
340 * to see whether changes are allowed (or just "route get").
341 */
342 (void) setuid(0);
343 if (s = system(logincmd)) {
344 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
345 loginname, s, loginfile);
346 (void) ioctl(0, TIOCSETD, (caddr_t)&odisc);
347 exit(6);
348 }
349
350 /* twiddle thumbs until we get a signal */
351 while (1)
352 sigpause(0);
353
354 /* NOTREACHED */
355 }
356