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