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