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