ttyaction.c revision 1.6 1 /* $NetBSD: ttyaction.c,v 1.6 1997/05/09 18:37:48 mycroft 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 <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <paths.h>
55
56 #include "util.h"
57
58 #ifndef _PATH_TTYACTION
59 #define _PATH_TTYACTION "/etc/ttyaction"
60 #endif
61
62 static char *actfile = _PATH_TTYACTION;
63 static char *pathenv = "PATH=" _PATH_STDPATH;
64
65 int
66 ttyaction(tty, act, user)
67 char *tty;
68 char *act;
69 char *user;
70 {
71 FILE *fp;
72 char *p1, *p2;
73 char *argv[4];
74 char *envp[8];
75 char line[1024];
76 char env_tty[64];
77 char env_act[64];
78 char env_user[256];
79 int err, linenum, pid, status;
80
81 fp = fopen(actfile, "r");
82 if (fp == NULL)
83 return 0;
84
85 /* Skip the "/dev/" part of the first arg. */
86 if (!strncmp(tty, "/dev/", 5))
87 tty += 5;
88
89 /* Args will be: "sh -c ..." */
90 argv[0] = _PATH_BSHELL;
91 argv[1] = "-c";
92 argv[2] = NULL; /* see below */
93 argv[3] = NULL;
94
95 /*
96 * Environment needs: TTY, ACT, USER
97 */
98 snprintf(env_tty, sizeof(env_tty), "TTY=%s", tty);
99 snprintf(env_act, sizeof(env_act), "ACT=%s", act);
100 snprintf(env_user, sizeof(env_user), "USER=%s", user);
101 envp[0] = pathenv;
102 envp[1] = env_tty;
103 envp[2] = env_act;
104 envp[3] = env_user;
105 envp[4] = NULL;
106
107 linenum = 0;
108 status = 0;
109 while (fgets(line, sizeof(line), fp)) {
110 linenum++;
111
112 /* Allow comment lines. */
113 if (line[0] == '#')
114 continue;
115
116 p1 = strtok(line, " \t");
117 p2 = strtok(NULL, " \t");
118 /* This arg goes to end of line. */
119 argv[2] = strtok(NULL, "\n");
120 if (!p1 || !p2 || !argv[2]) {
121 warnx("%s: line %d format error", actfile, linenum);
122 continue;
123 }
124 if (fnmatch(p1, tty, 0) || fnmatch(p2, act, 0))
125 continue;
126 /* OK, this is a match. Run the command. */
127 pid = fork();
128 if (pid == -1) {
129 warnx("fork failed: %s", strerror(errno));
130 continue;
131 }
132 if (pid == 0) {
133 /* This is the child. */
134 err = execve(argv[0], argv, envp);
135 /* If we get here, it is an error. */
136 warnx("%s: line %d: exec failed: %s",
137 actfile, linenum, strerror(errno));
138 _exit(1);
139 }
140 /* This is the parent. */
141 err = waitpid(pid, &status, 0);
142 if (err == -1) {
143 warnx("%s: line %d: wait failed: %s",
144 actfile, linenum, strerror(errno));
145 continue;
146 }
147 if (WTERMSIG(status)) {
148 warnx("%s: line %d: child died with signal %d",
149 actfile, linenum, WTERMSIG(status));
150 continue;
151 }
152 }
153 fclose(fp);
154 return status;
155 }
156
157