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