Home | History | Annotate | Line # | Download | only in libutil
ttyaction.c revision 1.5
      1 /*	$NetBSD: ttyaction.c,v 1.5 1996/11/29 18:50:37 gwr Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * For each matching "tty" and "action" run the "command."
     41  * See fnmatch() for matching the tty name.
     42  */
     43 
     44 #include <sys/types.h>
     45 #include <sys/wait.h>
     46 
     47 #include <errno.h>
     48 #include <fcntl.h>
     49 #include <unistd.h>
     50 #include <stdlib.h>
     51 #include <stdio.h>
     52 #include <string.h>
     53 #include <paths.h>
     54 
     55 #include "util.h"
     56 
     57 #ifndef _PATH_TTYACTION
     58 #define _PATH_TTYACTION "/etc/ttyaction"
     59 #endif
     60 
     61 static char *actfile = _PATH_TTYACTION;
     62 static char *pathenv = "PATH=" _PATH_STDPATH;
     63 
     64 int
     65 ttyaction(tty, act, user)
     66 	char *tty;
     67 	char *act;
     68 	char *user;
     69 {
     70 	FILE *fp;
     71 	char *p1, *p2;
     72 	char *argv[4];
     73 	char *envp[8];
     74 	char line[1024];
     75 	char env_tty[64];
     76 	char env_act[64];
     77 	char env_user[256];
     78 	int err, linenum, pid, status;
     79 
     80 	fp = fopen(actfile, "r");
     81 	if (fp == NULL)
     82 		return 0;
     83 
     84 	/* Skip the "/dev/" part of the first arg. */
     85 	if (!strncmp(tty, "/dev/", 5))
     86 		tty += 5;
     87 
     88 	/* Args will be: "sh -c ..." */
     89 	argv[0] = _PATH_BSHELL;
     90 	argv[1] = "-c";
     91 	argv[2] = NULL;	/* see below */
     92 	argv[3] = NULL;
     93 
     94 	/*
     95 	 * Environment needs: TTY, ACT, USER
     96 	 */
     97 	snprintf(env_tty, sizeof(env_tty), "TTY=%s", tty);
     98 	snprintf(env_act, sizeof(env_act), "ACT=%s", act);
     99 	snprintf(env_user, sizeof(env_user), "USER=%s", user);
    100 	envp[0] = pathenv;
    101 	envp[1] = env_tty;
    102 	envp[2] = env_act;
    103 	envp[3] = env_user;
    104 	envp[4] = NULL;
    105 
    106 	linenum = 0;
    107 	status = 0;
    108 	while (fgets(line, sizeof(line), fp)) {
    109 		linenum++;
    110 
    111 		/* Allow comment lines. */
    112 		if (line[0] == '#')
    113 			continue;
    114 
    115 		p1 = strtok(line, " \t");
    116 		p2 = strtok(NULL, " \t");
    117 		/* This arg goes to end of line. */
    118 		argv[2] = strtok(NULL, "\n");
    119 		if (!p1 || !p2 || !argv[2]) {
    120 			warnx("%s: line %d format error", actfile, linenum);
    121 			continue;
    122 		}
    123 		if (fnmatch(p1, tty, 0) || fnmatch(p2, act, 0))
    124 			continue;
    125 		/* OK, this is a match.  Run the command. */
    126 		pid = fork();
    127 		if (pid == -1) {
    128 			warnx("fork failed: %s", strerror(errno));
    129 			continue;
    130 		}
    131 		if (pid == 0) {
    132 			/* This is the child. */
    133 			err = execve(argv[0], argv, envp);
    134 			/* If we get here, it is an error. */
    135 			warnx("%s: line %d: exec failed: %s",
    136 				  actfile, linenum, strerror(errno));
    137 			_exit(1);
    138 		}
    139 		/* This is the parent. */
    140 		err = waitpid(pid, &status, 0);
    141 		if (err == -1) {
    142 			warnx("%s: line %d: wait failed: %s",
    143 				  actfile, linenum, strerror(errno));
    144 			continue;
    145 		}
    146 		if (WTERMSIG(status)) {
    147 			warnx("%s: line %d: child died with signal %d",
    148 				  actfile, linenum, WTERMSIG(status));
    149 			continue;
    150 		}
    151 	}
    152 	fclose(fp);
    153 	return status;
    154 }
    155 
    156