]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/sliplogin/sliplogin.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / sliplogin / sliplogin.c
1 /*-
2  * Copyright (c) 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static char copyright[] =
32 "@(#) Copyright (c) 1990, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 static char sccsid[] = "@(#)sliplogin.c 8.2 (Berkeley) 2/1/94";
38 static char rscid[] = "@(#)$FreeBSD$";
39 #endif /* not lint */
40
41 /*
42  * sliplogin.c
43  * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
44  *
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.
48  *
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.
53  *
54  * There are two forms of usage:
55  *
56  * "sliplogin"
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.
61  *
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.
65  */
66
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <sys/file.h>
70 #include <sys/stat.h>
71 #include <syslog.h>
72 #include <netdb.h>
73
74 #include <termios.h>
75 #include <sys/ioctl.h>
76 #include <net/slip.h>
77 #include <net/if.h>
78 #include <netinet/in.h>
79
80 #include <stdio.h>
81 #include <errno.h>
82 #include <ctype.h>
83 #include <paths.h>
84 #include <string.h>
85 #include <unistd.h>
86 #include <stdlib.h>
87 #include <signal.h>
88 #include "pathnames.h"
89
90 extern char **environ;
91
92 static char *restricted_environ[] = {
93         "PATH=" _PATH_STDPATH,
94         NULL
95 };
96
97 int     unit;
98 int     slip_mode;
99 speed_t speed;
100 int     uid;
101 int     keepal;
102 int     outfill;
103 int     slunit;
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 */
112
113 char *
114 make_ipaddr(void)
115 {
116 static char address[20] ="";
117 struct hostent *he;
118 unsigned long ipaddr;
119
120 address[0] = '\0';
121 if ((he = gethostbyname(raddr)) != NULL) {
122         ipaddr = ntohl(*(long *)he->h_addr_list[0]);
123         sprintf(address, "%lu.%lu.%lu.%lu",
124                 ipaddr >> 24,
125                 (ipaddr & 0x00ff0000) >> 16,
126                 (ipaddr & 0x0000ff00) >> 8,
127                 (ipaddr & 0x000000ff));
128         }
129
130 return address;
131 }
132
133 struct slip_modes {
134         char    *sm_name;
135         int     sm_or_flag;
136         int     sm_and_flag;
137 }        modes[] = {
138         "normal",       0        , 0        ,
139         "compress",     IFF_LINK0, IFF_LINK2,
140         "noicmp",       IFF_LINK1, 0        ,
141         "autocomp",     IFF_LINK2, IFF_LINK0,
142 };
143
144 void
145 findid(name)
146         char *name;
147 {
148         FILE *fp;
149         static char slopt[5][16];
150         static char laddr[16];
151         static char mask[16];
152         char   slparmsfile[MAXPATHLEN];
153         char user[16];
154         char buf[128];
155         int i, j, n;
156
157         environ = restricted_environ; /* minimal protection for system() */
158
159         (void)strncpy(loginname, name, sizeof(loginname)-1);
160         loginname[sizeof(loginname)-1] = '\0';
161
162         if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
163         accfile_err:
164                 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
165                 exit(1);
166         }
167         while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
168                 if (ferror(fp))
169                         goto accfile_err;
170                 if (loginargs[0] == '#' || isspace(loginargs[0]))
171                         continue;
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]);
175                 if (n < 4) {
176                         syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS);
177                         exit(1);
178                 }
179                 if (strcmp(user, name) != 0)
180                         continue;
181
182                 (void) fclose(fp);
183
184                 slip_mode = 0;
185                 for (i = 0; i < n - 4; i++) {
186                         for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
187                                 j++) {
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);
191                                         break;
192                                 }
193                         }
194                 }
195
196                 /*
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
200                  * a generic one.
201                  */
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)) {
209                                 syslog(LOG_ERR,
210                                        "access denied for %s - no %s\n",
211                                        name, _PATH_SLIP_LOGIN);
212                                 exit(5);
213                         }
214                 }
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))
220                                 *slparmsfile = '\0';
221                 }
222                 keepal = outfill = 0;
223                 slunit = -1;
224                 if (*slparmsfile) {
225                         if ((fp = fopen(slparmsfile, "r")) == NULL) {
226                         slfile_err:
227                                 syslog(LOG_ERR, "%s: %m\n", slparmsfile);
228                                 exit(1);
229                         }
230                         n = 0;
231                         while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
232                                 if (ferror(fp))
233                                         goto slfile_err;
234                                 if (buf[0] == '#' || isspace(buf[0]))
235                                         continue;
236                                 n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit);
237                                 if (n < 1) {
238                                 slwrong_fmt:
239                                         syslog(LOG_ERR, "%s: wrong format\n", slparmsfile);
240                                         exit(1);
241                                 }
242                                 (void) fclose(fp);
243                                 break;
244                         }
245                         if (n == 0)
246                                 goto slwrong_fmt;
247                 }
248
249                 return;
250         }
251         syslog(LOG_ERR, "SLIP access denied for %s\n", name);
252         exit(4);
253         /* NOTREACHED */
254 }
255
256 char *
257 sigstr(s)
258         int s;
259 {
260         static char buf[32];
261
262         switch (s) {
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");
291 #ifdef SIGLOST
292         case SIGLOST:   return("LOST");
293 #endif
294         case SIGUSR1:   return("USR1");
295         case SIGUSR2:   return("USR2");
296         }
297         (void)snprintf(buf, sizeof(buf), "sig %d", s);
298         return(buf);
299 }
300
301 void
302 hup_handler(s)
303         int s;
304 {
305         char logoutfile[MAXPATHLEN];
306
307         (void) close(0);
308         seteuid(0);
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';
315         }
316         if (access(logoutfile, R_OK|X_OK) == 0) {
317                 char logincmd[2*MAXPATHLEN+32];
318
319                 (void) snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", logoutfile, unit, speed, loginargs);
320                 (void) system(logincmd);
321         }
322         syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
323                sigstr(s));
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");
328         exit(1);
329         /* NOTREACHED */
330 }
331
332
333 /* Modify the slip line mode and add any compression or no-icmp flags. */
334 void line_flags(unit)
335         int unit;
336 {
337         struct ifreq ifr;
338         int s;
339
340         /* open a socket as the handle to the interface */
341         s = socket(AF_INET, SOCK_DGRAM, 0);
342         if (s < 0) {
343                 syslog(LOG_ERR, "socket: %m");
344                 exit(1);
345         }
346         sprintf(ifr.ifr_name, "sl%d", unit);
347
348         /* get the flags for the interface */
349         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
350                 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
351                 exit(1);
352         }
353
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");
360                 exit(1);
361         }
362         close(s);
363 }
364
365
366 int
367 main(argc, argv)
368         int argc;
369         char *argv[];
370 {
371         int fd, s, ldisc;
372         char *name;
373         struct termios tios;
374         char logincmd[2*BUFSIZ+32];
375
376         FILE *pidfile;                          /* pid file */
377         FILE *iffile;                           /* interfaces file */
378         char *p;
379         int n;
380         char devnam[MAXPATHLEN] = _PATH_TTY;   /* Device name */
381
382         if ((name = strrchr(argv[0], '/')) == NULL)
383                 name = argv[0];
384         s = getdtablesize();
385         for (fd = 3 ; fd < s ; fd++)
386                 (void) close(fd);
387         openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON);
388         uid = getuid();
389         if (argc > 1) {
390                 findid(argv[1]);
391
392                 /*
393                  * Disassociate from current controlling terminal, if any,
394                  * and ensure that the slip line is our controlling terminal.
395                  */
396                 if (daemon(1, 1)) {
397                         syslog(LOG_ERR, "daemon(1, 1): %m");
398                         exit(1);
399                 }
400                 if (argc > 2) {
401                         if ((fd = open(argv[2], O_RDWR)) == -1) {
402                                 syslog(LOG_ERR, "open %s: %m", argv[2]);
403                                 exit(2);
404                         }
405                         (void) dup2(fd, 0);
406                         if (fd > 2)
407                                 close(fd);
408                 }
409                 if (ioctl(0, TIOCSCTTY, 0) == -1) {
410                         syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
411                         exit(1);
412                 }
413                 if (tcsetpgrp(0, getpid()) < 0) {
414                         syslog(LOG_ERR, "tcsetpgrp failed: %m");
415                         exit(1);
416                 }
417         } else {
418                 if ((name = getlogin()) == NULL) {
419                         syslog(LOG_ERR, "access denied - login name not found\n");
420                         exit(1);
421                 }
422                 findid(name);
423         }
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());
427
428         (void) fflush(stderr);
429         sleep(1);
430
431         /* set up the line parameters */
432         if (tcgetattr(0, &tios) < 0) {
433                 syslog(LOG_ERR, "tcgetattr: %m");
434                 exit(1);
435         }
436         cfmakeraw(&tios);
437         if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
438                 syslog(LOG_ERR, "tcsetattr: %m");
439                 exit(1);
440         }
441         speed = cfgetispeed(&tios);
442
443         ldisc = SLIPDISC;
444         if (ioctl(0, TIOCSETD, &ldisc) < 0) {
445                 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
446                 exit(1);
447         }
448         if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) {
449                 syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m");
450                 exit(1);
451         }
452         /* find out what unit number we were assigned */
453         if (ioctl(0, SLIOCGUNIT, &unit) < 0) {
454                 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
455                 exit(1);
456         }
457         (void) signal(SIGHUP, hup_handler);
458         (void) signal(SIGTERM, hup_handler);
459
460         if (keepal > 0) {
461                 (void) signal(SIGURG, hup_handler);
462                 if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) {
463                         syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
464                         exit(1);
465                 }
466         }
467         if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) {
468                 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
469                 exit(1);
470         }
471
472         /* write pid to file */
473         pid = getpid();
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);
479         } else {
480                 syslog(LOG_ERR, "Failed to create pid file %s: %m",
481                                 pidfilename);
482                 pidfilename[0] = 0;
483         }
484
485         /* write interface unit number to file */
486         p = ttyname(0);
487         if (p)
488                 strcpy(devnam, p);
489         for (n = strlen(devnam); n > 0; n--) 
490                 if (devnam[n] == '/') {
491                         n++;
492                         break;
493                 }
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);
498         } else {
499                 syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename);
500                 iffilename[0] = 0;  
501         }
502
503
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,
506                       loginargs);
507         /*
508          * aim stdout and errout at /dev/null so logincmd output won't
509          * babble into the slip tty line.
510          */
511         (void) close(1);
512         if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
513                 if (fd < 0) {
514                         syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL);
515                         exit(1);
516                 }
517                 (void) dup2(fd, 1);
518                 (void) close(fd);
519         }
520         (void) dup2(1, 2);
521
522         /*
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").
526          */
527         (void) setuid(0);
528         if (s = system(logincmd)) {
529                 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
530                        loginname, s, loginfile);
531                 exit(6);
532         }
533
534         /* Handle any compression or no-icmp flags. */
535         line_flags(unit);
536
537         /* reset uid to users' to allow the user to give a signal. */
538         seteuid(uid);
539         /* twiddle thumbs until we get a signal */
540         while (1)
541                 sigpause(0);
542
543         /* NOTREACHED */
544 }