1 /* $NetBSD: ttyaction.c,v 1.2 1996/11/14 19:16:02 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 /* Environment needs: TTY, ACT, USER */ 95 sprintf(env_tty, "TTY=%.63s", tty); 96 sprintf(env_act, "ACT=%.63s", act); 97 sprintf(env_user, "USER=%.255s", user); 98 envp[0] = pathenv; 99 envp[1] = env_tty; 100 envp[2] = env_act; 101 envp[3] = env_user; 102 envp[4] = NULL; 103 104 linenum = 0; 105 status = 0; 106 while (fgets(line, sizeof(line), fp)) { 107 linenum++; 108 109 /* Allow comment lines. */ 110 if (line[0] == '#') 111 continue; 112 113 p1 = strtok(line, " \t"); 114 p2 = strtok(NULL, " \t"); 115 /* This arg goes to end of line. */ 116 argv[2] = strtok(NULL, "\n"); 117 if (!p1 || !p2 || !argv[2]) { 118 warnx("%s: line %d format error", actfile, linenum); 119 continue; 120 } 121 if (fnmatch(p1, tty, 0) || fnmatch(p2, act, 0)) 122 continue; 123 /* OK, this is a match. Run the command. */ 124 pid = fork(); 125 if (pid == -1) { 126 warnx("fork failed: %s", strerror(errno)); 127 continue; 128 } 129 if (pid == 0) { 130 /* This is the child. */ 131 err = execve(argv[0], argv, envp); 132 /* If we get here, it is an error. */ 133 warnx("%s: line %d: exec failed: %s", 134 actfile, linenum, strerror(errno)); 135 exit(1); 136 } 137 /* This is the parent. */ 138 err = waitpid(pid, &status, 0); 139 if (err == -1) { 140 warnx("%s: line %d: wait failed: %s", 141 actfile, linenum, strerror(errno)); 142 continue; 143 } 144 if (WTERMSIG(status)) { 145 warnx("%s: line %d: child died with signal %d", 146 actfile, linenum, WTERMSIG(status)); 147 continue; 148 } 149 } 150 fclose(fp); 151 return status; 152 } 153 154