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