Clean (void) casts from usr.sbin
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1990, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)sliplogin.c        8.2 (Berkeley) 2/1/94
35  * $FreeBSD: src/usr.sbin/sliplogin/sliplogin.c,v 1.9.6.2 2001/07/19 05:21:28 kris Exp $
36  * $DragonFly: src/usr.sbin/sliplogin/sliplogin.c,v 1.3 2004/12/18 22:48:14 swildner Exp $
37  */
38
39 /*
40  * sliplogin.c
41  * [MUST BE RUN SUID, SLOPEN DOES A SUSER()!]
42  *
43  * This program initializes its own tty port to be an async TCP/IP interface.
44  * It sets the line discipline to slip, invokes a shell script to initialize
45  * the network interface, then pauses forever waiting for hangup.
46  *
47  * It is a remote descendant of several similar programs with incestuous ties:
48  * - Kirk Smith's slipconf, modified by Richard Johnsson @ DEC WRL.
49  * - slattach, probably by Rick Adams but touched by countless hordes.
50  * - the original sliplogin for 4.2bsd, Doug Kingston the mover behind it.
51  *
52  * There are two forms of usage:
53  *
54  * "sliplogin"
55  * Invoked simply as "sliplogin", the program looks up the username
56  * in the file /etc/slip.hosts.
57  * If an entry is found, the line on fd0 is configured for SLIP operation
58  * as specified in the file.
59  *
60  * "sliplogin IPhostlogin </dev/ttyb"
61  * Invoked by root with a username, the name is looked up in the
62  * /etc/slip.hosts file and if found fd0 is configured as in case 1.
63  */
64
65 #include <sys/param.h>
66 #include <sys/socket.h>
67 #include <sys/file.h>
68 #include <sys/stat.h>
69 #include <syslog.h>
70 #include <netdb.h>
71
72 #include <termios.h>
73 #include <sys/ioctl.h>
74 #include <net/slip.h>
75 #include <net/if.h>
76
77 #include <stdio.h>
78 #include <errno.h>
79 #include <ctype.h>
80 #include <paths.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <stdlib.h>
84 #include <signal.h>
85 #include "pathnames.h"
86
87 extern char **environ;
88
89 static char *restricted_environ[] = {
90         "PATH=" _PATH_STDPATH,
91         NULL
92 };
93
94 int     unit;
95 int     slip_mode;
96 speed_t speed;
97 int     uid;
98 int     keepal;
99 int     outfill;
100 int     slunit;
101 char    loginargs[BUFSIZ];
102 char    loginfile[MAXPATHLEN];
103 char    loginname[BUFSIZ];
104 static char raddr[32];                  /* remote address */
105 char ifname[IFNAMSIZ];                  /* interface name */
106 static  char pidfilename[MAXPATHLEN];   /* name of pid file */
107 static  char iffilename[MAXPATHLEN];    /* name of if file */
108 static  pid_t   pid;                    /* our pid */
109
110 char *
111 make_ipaddr(void)
112 {
113 static char address[20] ="";
114 struct hostent *he;
115 unsigned long ipaddr;
116
117 address[0] = '\0';
118 if ((he = gethostbyname(raddr)) != NULL) {
119         ipaddr = ntohl(*(long *)he->h_addr_list[0]);
120         sprintf(address, "%lu.%lu.%lu.%lu",
121                 ipaddr >> 24,
122                 (ipaddr & 0x00ff0000) >> 16,
123                 (ipaddr & 0x0000ff00) >> 8,
124                 (ipaddr & 0x000000ff));
125         }
126
127 return address;
128 }
129
130 struct slip_modes {
131         char    *sm_name;
132         int     sm_or_flag;
133         int     sm_and_flag;
134 }        modes[] = {
135         "normal",       0        , 0        ,
136         "compress",     IFF_LINK0, IFF_LINK2,
137         "noicmp",       IFF_LINK1, 0        ,
138         "autocomp",     IFF_LINK2, IFF_LINK0,
139 };
140
141 void
142 findid(name)
143         char *name;
144 {
145         FILE *fp;
146         static char slopt[5][16];
147         static char laddr[16];
148         static char mask[16];
149         char   slparmsfile[MAXPATHLEN];
150         char user[16];
151         char buf[128];
152         int i, j, n;
153
154         environ = restricted_environ; /* minimal protection for system() */
155
156         strncpy(loginname, name, sizeof(loginname)-1);
157         loginname[sizeof(loginname)-1] = '\0';
158
159         if ((fp = fopen(_PATH_ACCESS, "r")) == NULL) {
160         accfile_err:
161                 syslog(LOG_ERR, "%s: %m\n", _PATH_ACCESS);
162                 exit(1);
163         }
164         while (fgets(loginargs, sizeof(loginargs) - 1, fp)) {
165                 if (ferror(fp))
166                         goto accfile_err;
167                 if (loginargs[0] == '#' || isspace(loginargs[0]))
168                         continue;
169                 n = sscanf(loginargs, "%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s%*[ \t]%15s\n",
170                         user, laddr, raddr, mask, slopt[0], slopt[1],
171                         slopt[2], slopt[3], slopt[4]);
172                 if (n < 4) {
173                         syslog(LOG_ERR, "%s: wrong format\n", _PATH_ACCESS);
174                         exit(1);
175                 }
176                 if (strcmp(user, name) != 0)
177                         continue;
178
179                 fclose(fp);
180
181                 slip_mode = 0;
182                 for (i = 0; i < n - 4; i++) {
183                         for (j = 0; j < sizeof(modes)/sizeof(struct slip_modes);
184                                 j++) {
185                                 if (strcmp(modes[j].sm_name, slopt[i]) == 0) {
186                                         slip_mode |= (modes[j].sm_or_flag);
187                                         slip_mode &= ~(modes[j].sm_and_flag);
188                                         break;
189                                 }
190                         }
191                 }
192
193                 /*
194                  * we've found the guy we're looking for -- see if
195                  * there's a login file we can use.  First check for
196                  * one specific to this host.  If none found, try for
197                  * a generic one.
198                  */
199                 snprintf(loginfile, sizeof(loginfile), "%s.%s", _PATH_LOGIN, name);
200                 if (access(loginfile, R_OK|X_OK) != 0) {
201                         strncpy(loginfile, _PATH_LOGIN, sizeof(loginfile)-1);
202                         loginfile[sizeof(loginfile)-1] = '\0';
203                         if (access(loginfile, R_OK|X_OK)) {
204                                 syslog(LOG_ERR,
205                                        "access denied for %s - no %s\n",
206                                        name, _PATH_LOGIN);
207                                 exit(5);
208                         }
209                 }
210                 snprintf(slparmsfile, sizeof(slparmsfile), "%s.%s", _PATH_SLPARMS, name);
211                 if (access(slparmsfile, R_OK|X_OK) != 0) {
212                         strncpy(slparmsfile, _PATH_SLPARMS, sizeof(slparmsfile)-1);
213                         slparmsfile[sizeof(slparmsfile)-1] = '\0';
214                         if (access(slparmsfile, R_OK|X_OK))
215                                 *slparmsfile = '\0';
216                 }
217                 keepal = outfill = 0;
218                 slunit = -1;
219                 if (*slparmsfile) {
220                         if ((fp = fopen(slparmsfile, "r")) == NULL) {
221                         slfile_err:
222                                 syslog(LOG_ERR, "%s: %m\n", slparmsfile);
223                                 exit(1);
224                         }
225                         n = 0;
226                         while (fgets(buf, sizeof(buf) - 1, fp) != NULL) {
227                                 if (ferror(fp))
228                                         goto slfile_err;
229                                 if (buf[0] == '#' || isspace(buf[0]))
230                                         continue;
231                                 n = sscanf(buf, "%d %d %d", &keepal, &outfill, &slunit);
232                                 if (n < 1) {
233                                 slwrong_fmt:
234                                         syslog(LOG_ERR, "%s: wrong format\n", slparmsfile);
235                                         exit(1);
236                                 }
237                                 fclose(fp);
238                                 break;
239                         }
240                         if (n == 0)
241                                 goto slwrong_fmt;
242                 }
243
244                 return;
245         }
246         syslog(LOG_ERR, "SLIP access denied for %s\n", name);
247         exit(4);
248         /* NOTREACHED */
249 }
250
251 char *
252 sigstr(s)
253         int s;
254 {
255         static char buf[32];
256
257         switch (s) {
258         case SIGHUP:    return("HUP");
259         case SIGINT:    return("INT");
260         case SIGQUIT:   return("QUIT");
261         case SIGILL:    return("ILL");
262         case SIGTRAP:   return("TRAP");
263         case SIGIOT:    return("IOT");
264         case SIGEMT:    return("EMT");
265         case SIGFPE:    return("FPE");
266         case SIGKILL:   return("KILL");
267         case SIGBUS:    return("BUS");
268         case SIGSEGV:   return("SEGV");
269         case SIGSYS:    return("SYS");
270         case SIGPIPE:   return("PIPE");
271         case SIGALRM:   return("ALRM");
272         case SIGTERM:   return("TERM");
273         case SIGURG:    return("URG");
274         case SIGSTOP:   return("STOP");
275         case SIGTSTP:   return("TSTP");
276         case SIGCONT:   return("CONT");
277         case SIGCHLD:   return("CHLD");
278         case SIGTTIN:   return("TTIN");
279         case SIGTTOU:   return("TTOU");
280         case SIGIO:     return("IO");
281         case SIGXCPU:   return("XCPU");
282         case SIGXFSZ:   return("XFSZ");
283         case SIGVTALRM: return("VTALRM");
284         case SIGPROF:   return("PROF");
285         case SIGWINCH:  return("WINCH");
286 #ifdef SIGLOST
287         case SIGLOST:   return("LOST");
288 #endif
289         case SIGUSR1:   return("USR1");
290         case SIGUSR2:   return("USR2");
291         }
292         snprintf(buf, sizeof(buf), "sig %d", s);
293         return(buf);
294 }
295
296 void
297 hup_handler(s)
298         int s;
299 {
300         char logoutfile[MAXPATHLEN];
301
302         close(0);
303         seteuid(0);
304         snprintf(logoutfile, sizeof(logoutfile), "%s.%s", _PATH_LOGOUT, loginname);
305         if (access(logoutfile, R_OK|X_OK) != 0) {
306                 strncpy(logoutfile, _PATH_LOGOUT, sizeof(logoutfile)-1);
307                 logoutfile[sizeof(logoutfile)-1] = '\0';
308         }
309         if (access(logoutfile, R_OK|X_OK) == 0) {
310                 char logincmd[2*MAXPATHLEN+32];
311
312                 snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", logoutfile, unit, speed, loginargs);
313                 system(logincmd);
314         }
315         syslog(LOG_INFO, "closed %s slip unit %d (%s)\n", loginname, unit,
316                sigstr(s));
317         if (unlink(pidfilename) < 0 && errno != ENOENT)
318                 syslog(LOG_WARNING, "unable to delete pid file: %m");
319         if (unlink(iffilename) < 0 && errno != ENOENT)
320                 syslog(LOG_WARNING, "unable to delete if file: %m");
321         exit(1);
322         /* NOTREACHED */
323 }
324
325
326 /* Modify the slip line mode and add any compression or no-icmp flags. */
327 void line_flags(unit)
328         int unit;
329 {
330         struct ifreq ifr;
331         int s;
332
333         /* open a socket as the handle to the interface */
334         s = socket(AF_INET, SOCK_DGRAM, 0);
335         if (s < 0) {
336                 syslog(LOG_ERR, "socket: %m");
337                 exit(1);
338         }
339         sprintf(ifr.ifr_name, "sl%d", unit);
340
341         /* get the flags for the interface */
342         if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
343                 syslog(LOG_ERR, "ioctl (SIOCGIFFLAGS): %m");
344                 exit(1);
345         }
346
347         /* Assert any compression or no-icmp flags. */
348 #define SLMASK (~(IFF_LINK0 | IFF_LINK1 | IFF_LINK2))
349         ifr.ifr_flags &= SLMASK;
350         ifr.ifr_flags |= slip_mode;
351         if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
352                 syslog(LOG_ERR, "ioctl (SIOCSIFFLAGS): %m");
353                 exit(1);
354         }
355         close(s);
356 }
357
358
359 main(argc, argv)
360         int argc;
361         char *argv[];
362 {
363         int fd, s, ldisc;
364         char *name;
365         struct termios tios, otios;
366         char logincmd[2*BUFSIZ+32];
367         extern uid_t getuid();
368
369         FILE *pidfile;                          /* pid file */
370         FILE *iffile;                           /* interfaces file */
371         char *p;
372         int n;
373         char devnam[MAXPATHLEN] = _PATH_TTY;   /* Device name */
374
375         if ((name = strrchr(argv[0], '/')) == NULL)
376                 name = argv[0];
377         s = getdtablesize();
378         for (fd = 3 ; fd < s ; fd++)
379                 close(fd);
380         openlog(name, LOG_PID|LOG_PERROR, LOG_DAEMON);
381         uid = getuid();
382         if (argc > 1) {
383                 findid(argv[1]);
384
385                 /*
386                  * Disassociate from current controlling terminal, if any,
387                  * and ensure that the slip line is our controlling terminal.
388                  */
389                 if (daemon(1, 1)) {
390                         syslog(LOG_ERR, "daemon(1, 1): %m");
391                         exit(1);
392                 }
393                 if (argc > 2) {
394                         if ((fd = open(argv[2], O_RDWR)) == -1) {
395                                 syslog(LOG_ERR, "open %s: %m", argv[2]);
396                                 exit(2);
397                         }
398                         dup2(fd, 0);
399                         if (fd > 2)
400                                 close(fd);
401                 }
402                 if (ioctl(0, TIOCSCTTY, 0) == -1) {
403                         syslog(LOG_ERR, "ioctl (TIOCSCTTY): %m");
404                         exit(1);
405                 }
406                 if (tcsetpgrp(0, getpid()) < 0) {
407                         syslog(LOG_ERR, "tcsetpgrp failed: %m");
408                         exit(1);
409                 }
410         } else {
411                 if ((name = getlogin()) == NULL) {
412                         syslog(LOG_ERR, "access denied - login name not found\n");
413                         exit(1);
414                 }
415                 findid(name);
416         }
417         fchmod(0, 0600);
418         fprintf(stderr, "starting slip login for %s\n", loginname);
419         fprintf(stderr, "your address is %s\n\n", make_ipaddr());
420
421         fflush(stderr);
422         sleep(1);
423
424         /* set up the line parameters */
425         if (tcgetattr(0, &tios) < 0) {
426                 syslog(LOG_ERR, "tcgetattr: %m");
427                 exit(1);
428         }
429         otios = tios;
430         cfmakeraw(&tios);
431         if (tcsetattr(0, TCSAFLUSH, &tios) < 0) {
432                 syslog(LOG_ERR, "tcsetattr: %m");
433                 exit(1);
434         }
435         speed = cfgetispeed(&tios);
436
437         ldisc = SLIPDISC;
438         if (ioctl(0, TIOCSETD, &ldisc) < 0) {
439                 syslog(LOG_ERR, "ioctl(TIOCSETD): %m");
440                 exit(1);
441         }
442         if (slunit >= 0 && ioctl(0, SLIOCSUNIT, &slunit) < 0) {
443                 syslog(LOG_ERR, "ioctl (SLIOCSUNIT): %m");
444                 exit(1);
445         }
446         /* find out what unit number we were assigned */
447         if (ioctl(0, SLIOCGUNIT, &unit) < 0) {
448                 syslog(LOG_ERR, "ioctl (SLIOCGUNIT): %m");
449                 exit(1);
450         }
451         signal(SIGHUP, hup_handler);
452         signal(SIGTERM, hup_handler);
453
454         if (keepal > 0) {
455                 signal(SIGURG, hup_handler);
456                 if (ioctl(0, SLIOCSKEEPAL, &keepal) < 0) {
457                         syslog(LOG_ERR, "ioctl(SLIOCSKEEPAL): %m");
458                         exit(1);
459                 }
460         }
461         if (outfill > 0 && ioctl(0, SLIOCSOUTFILL, &outfill) < 0) {
462                 syslog(LOG_ERR, "ioctl(SLIOCSOUTFILL): %m");
463                 exit(1);
464         }
465
466         /* write pid to file */
467         pid = getpid();
468         sprintf(ifname, "sl%d", unit);
469         sprintf(pidfilename, "%s%s.pid", _PATH_VARRUN, ifname);
470         if ((pidfile = fopen(pidfilename, "w")) != NULL) {
471                 fprintf(pidfile, "%d\n", pid);
472                 fclose(pidfile);
473         } else {
474                 syslog(LOG_ERR, "Failed to create pid file %s: %m",
475                                 pidfilename);
476                 pidfilename[0] = 0;
477         }
478
479         /* write interface unit number to file */
480         p = ttyname(0);
481         if (p)
482                 strcpy(devnam, p);
483         for (n = strlen(devnam); n > 0; n--) 
484                 if (devnam[n] == '/') {
485                         n++;
486                         break;
487                 }
488         sprintf(iffilename, "%s%s.if", _PATH_VARRUN, &devnam[n]);
489         if ((iffile = fopen(iffilename, "w")) != NULL) {
490                 fprintf(iffile, "sl%d\n", unit); 
491                 fclose(iffile);
492         } else {
493                 syslog(LOG_ERR, "Failed to create if file %s: %m", iffilename);
494                 iffilename[0] = 0;  
495         }
496
497
498         syslog(LOG_INFO, "attaching slip unit %d for %s\n", unit, loginname);
499         snprintf(logincmd, sizeof(logincmd), "%s %d %ld %s", loginfile, unit, speed,
500                  loginargs);
501         /*
502          * aim stdout and errout at /dev/null so logincmd output won't
503          * babble into the slip tty line.
504          */
505         close(1);
506         if ((fd = open(_PATH_DEVNULL, O_WRONLY)) != 1) {
507                 if (fd < 0) {
508                         syslog(LOG_ERR, "open %s: %m", _PATH_DEVNULL);
509                         exit(1);
510                 }
511                 dup2(fd, 1);
512                 close(fd);
513         }
514         dup2(1, 2);
515
516         /*
517          * Run login and logout scripts as root (real and effective);
518          * current route(8) is setuid root, and checks the real uid
519          * to see whether changes are allowed (or just "route get").
520          */
521         setuid(0);
522         if (s = system(logincmd)) {
523                 syslog(LOG_ERR, "%s login failed: exit status %d from %s",
524                        loginname, s, loginfile);
525                 exit(6);
526         }
527
528         /* Handle any compression or no-icmp flags. */
529         line_flags(unit);
530
531         /* reset uid to users' to allow the user to give a signal. */
532         seteuid(uid);
533         /* twiddle thumbs until we get a signal */
534         while (1)
535                 sigpause(0);
536
537         /* NOTREACHED */
538 }