1*ce099b40Smartin /* $NetBSD: ttyaction.c,v 1.19 2008/04/28 20:23:03 martin Exp $ */
26ab34f58Sgwr
36ab34f58Sgwr /*-
46ab34f58Sgwr * Copyright (c) 1996 The NetBSD Foundation, Inc.
56ab34f58Sgwr * All rights reserved.
66ab34f58Sgwr *
76ab34f58Sgwr * This code is derived from software contributed to The NetBSD Foundation
86ab34f58Sgwr * by Gordon W. Ross.
96ab34f58Sgwr *
106ab34f58Sgwr * Redistribution and use in source and binary forms, with or without
116ab34f58Sgwr * modification, are permitted provided that the following conditions
126ab34f58Sgwr * are met:
136ab34f58Sgwr * 1. Redistributions of source code must retain the above copyright
146ab34f58Sgwr * notice, this list of conditions and the following disclaimer.
156ab34f58Sgwr * 2. Redistributions in binary form must reproduce the above copyright
166ab34f58Sgwr * notice, this list of conditions and the following disclaimer in the
176ab34f58Sgwr * documentation and/or other materials provided with the distribution.
186ab34f58Sgwr *
196ab34f58Sgwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206ab34f58Sgwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216ab34f58Sgwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2223bee85aSjtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2323bee85aSjtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246ab34f58Sgwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256ab34f58Sgwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266ab34f58Sgwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276ab34f58Sgwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286ab34f58Sgwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296ab34f58Sgwr * POSSIBILITY OF SUCH DAMAGE.
306ab34f58Sgwr */
316ab34f58Sgwr
326ab34f58Sgwr /*
336ab34f58Sgwr * For each matching "tty" and "action" run the "command."
346ab34f58Sgwr * See fnmatch() for matching the tty name.
356ab34f58Sgwr */
366ab34f58Sgwr
37fce98185Sad #include <sys/cdefs.h>
38fce98185Sad #if defined(LIBC_SCCS) && !defined(lint)
39*ce099b40Smartin __RCSID("$NetBSD: ttyaction.c,v 1.19 2008/04/28 20:23:03 martin Exp $");
40fce98185Sad #endif /* LIBC_SCCS and not lint */
41fce98185Sad
426ab34f58Sgwr #include <sys/types.h>
436ab34f58Sgwr #include <sys/wait.h>
446ab34f58Sgwr
45b48252f3Slukem #include <assert.h>
468feb354eSmycroft #include <err.h>
476ab34f58Sgwr #include <errno.h>
486ab34f58Sgwr #include <fcntl.h>
4920690bcbSmikel #include <fnmatch.h>
506ab34f58Sgwr #include <paths.h>
51b48252f3Slukem #include <stdio.h>
52b48252f3Slukem #include <stdlib.h>
53b48252f3Slukem #include <string.h>
54b48252f3Slukem #include <unistd.h>
556ab34f58Sgwr
566ab34f58Sgwr #include "util.h"
576ab34f58Sgwr
586ab34f58Sgwr #ifndef _PATH_TTYACTION
596ab34f58Sgwr #define _PATH_TTYACTION "/etc/ttyaction"
606ab34f58Sgwr #endif
616ab34f58Sgwr
62a5c1a01eSchristos static const char *actfile = _PATH_TTYACTION;
63a5c1a01eSchristos static const char *pathenv = "PATH=" _PATH_STDPATH;
646ab34f58Sgwr
656ab34f58Sgwr int
ttyaction(const char * tty,const char * act,const char * user)66fce98185Sad ttyaction(const char *tty, const char *act, const char *user)
676ab34f58Sgwr {
686ab34f58Sgwr FILE *fp;
696ab34f58Sgwr char *p1, *p2;
70a5c1a01eSchristos const char *argv[4];
71a5c1a01eSchristos const char *envp[8];
7295478ceaSkleink char *lastp;
736ab34f58Sgwr char line[1024];
746ab34f58Sgwr char env_tty[64];
756ab34f58Sgwr char env_act[64];
766ab34f58Sgwr char env_user[256];
77f343a3e8Swiz int error, linenum, status;
78f343a3e8Swiz pid_t pid;
796ab34f58Sgwr
80b48252f3Slukem _DIAGASSERT(tty != NULL);
81b48252f3Slukem _DIAGASSERT(act != NULL);
82b48252f3Slukem _DIAGASSERT(user != NULL);
83b48252f3Slukem
846ab34f58Sgwr fp = fopen(actfile, "r");
856ab34f58Sgwr if (fp == NULL)
866ab34f58Sgwr return 0;
876ab34f58Sgwr
889d693450Sgwr /* Skip the "/dev/" part of the first arg. */
89232f61faSelad if (!strncmp(tty, "/dev/", (size_t)5))
909d693450Sgwr tty += 5;
919d693450Sgwr
926ab34f58Sgwr /* Args will be: "sh -c ..." */
936ab34f58Sgwr argv[0] = _PATH_BSHELL;
946ab34f58Sgwr argv[1] = "-c";
956ab34f58Sgwr argv[2] = NULL; /* see below */
966ab34f58Sgwr argv[3] = NULL;
976ab34f58Sgwr
983d5027beSgwr /*
993d5027beSgwr * Environment needs: TTY, ACT, USER
1003d5027beSgwr */
101d8201fb9Sgwr snprintf(env_tty, sizeof(env_tty), "TTY=%s", tty);
102d8201fb9Sgwr snprintf(env_act, sizeof(env_act), "ACT=%s", act);
103d8201fb9Sgwr snprintf(env_user, sizeof(env_user), "USER=%s", user);
1046ab34f58Sgwr envp[0] = pathenv;
1056ab34f58Sgwr envp[1] = env_tty;
1066ab34f58Sgwr envp[2] = env_act;
1076ab34f58Sgwr envp[3] = env_user;
1086ab34f58Sgwr envp[4] = NULL;
1096ab34f58Sgwr
1106ab34f58Sgwr linenum = 0;
1116ab34f58Sgwr status = 0;
112232f61faSelad while (fgets(line, (int)sizeof(line), fp)) {
1136ab34f58Sgwr linenum++;
1149d693450Sgwr
1159d693450Sgwr /* Allow comment lines. */
1169d693450Sgwr if (line[0] == '#')
1179d693450Sgwr continue;
1189d693450Sgwr
11995478ceaSkleink p1 = strtok_r(line, " \t", &lastp);
12095478ceaSkleink p2 = strtok_r(NULL, " \t", &lastp);
1216ab34f58Sgwr /* This arg goes to end of line. */
12295478ceaSkleink argv[2] = strtok_r(NULL, "\n", &lastp);
1236ab34f58Sgwr if (!p1 || !p2 || !argv[2]) {
1246ab34f58Sgwr warnx("%s: line %d format error", actfile, linenum);
1256ab34f58Sgwr continue;
1266ab34f58Sgwr }
1276ab34f58Sgwr if (fnmatch(p1, tty, 0) || fnmatch(p2, act, 0))
1286ab34f58Sgwr continue;
1296ab34f58Sgwr /* OK, this is a match. Run the command. */
1306ab34f58Sgwr pid = fork();
1316ab34f58Sgwr if (pid == -1) {
1326ab34f58Sgwr warnx("fork failed: %s", strerror(errno));
1336ab34f58Sgwr continue;
1346ab34f58Sgwr }
1356ab34f58Sgwr if (pid == 0) {
1366ab34f58Sgwr /* This is the child. */
137a5c1a01eSchristos error = execve(argv[0],
138a5c1a01eSchristos (char *const *)__UNCONST(argv),
139a5c1a01eSchristos (char *const *)__UNCONST(envp));
1406ab34f58Sgwr /* If we get here, it is an error. */
1416ab34f58Sgwr warnx("%s: line %d: exec failed: %s",
1426ab34f58Sgwr actfile, linenum, strerror(errno));
143603bda9cSgwr _exit(1);
1446ab34f58Sgwr }
1456ab34f58Sgwr /* This is the parent. */
146983a6b1fSchristos error = waitpid(pid, &status, 0);
147983a6b1fSchristos if (error == -1) {
1486ab34f58Sgwr warnx("%s: line %d: wait failed: %s",
1496ab34f58Sgwr actfile, linenum, strerror(errno));
1506ab34f58Sgwr continue;
1516ab34f58Sgwr }
1526ab34f58Sgwr if (WTERMSIG(status)) {
1536ab34f58Sgwr warnx("%s: line %d: child died with signal %d",
1546ab34f58Sgwr actfile, linenum, WTERMSIG(status));
1556ab34f58Sgwr continue;
1566ab34f58Sgwr }
1576ab34f58Sgwr }
1586ab34f58Sgwr fclose(fp);
1596ab34f58Sgwr return status;
1606ab34f58Sgwr }
161