2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/amd/info_exec.c
45 * Get info from executable map
47 * Original from Erik Kline, 2004.
52 #endif /* HAVE_CONFIG_H */
56 #define MAX_LINE_LEN 1500
58 /* forward declarations */
59 int exec_init(mnt_map *m, char *map, time_t *tp);
60 int exec_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
67 fgets_timed(char *s, int size, int rdfd, int secs)
74 if (!s || size < 0 || rdfd < 0)
81 start = clocktime(NULL);
82 while (s[i] != '\n' && i < size-1) {
83 s[i+1] = 0; /* places the requisite trailing '\0' */
85 /* ready for reading */
86 rval = read(rdfd, (void *)(s+i), 1);
94 } else if (rval == 0) {
96 } else if (rval < 0 && errno != EAGAIN && errno != EINTR) {
97 plog(XLOG_WARNING, "fgets_timed read error: %m");
102 now = clocktime(NULL) - start;
106 timeo.tv_sec = secs - now;
108 /* timed out (now>=secs) */
109 plog(XLOG_WARNING, "executable map read timed out (> %d secs)", secs);
117 rval = select(rdfd+1, &fds, 0, 0, &timeo);
119 /* error selecting */
120 plog(XLOG_WARNING, "fgets_timed select error: %m");
125 } else if (rval == 0) {
127 plog(XLOG_WARNING, "executable map read timed out (> %d secs)", secs);
137 return (rval == 0 ? s : 0);
142 read_line(char *buf, int size, int fd)
146 while (fgets_timed(buf, size, fd, gopt.exec_map_timeout)) {
147 int len = strlen(buf);
149 if (len > 1 && buf[len - 2] == '\\' &&
150 buf[len - 1] == '\n') {
165 * Try to locate a value in a query answer
168 exec_parse_qanswer(int fd, char *map, char *key, char **pval, time_t *tp)
170 char qanswer[MAX_LINE_LEN], *dc = 0;
174 while (read_line(qanswer, sizeof(qanswer), fd)) {
177 int len = strlen(qanswer);
181 * Make sure we got the whole line
183 if (qanswer[len - 1] != '\n') {
184 plog(XLOG_WARNING, "line %d in \"%s\" is too long", line_no, map);
187 qanswer[len - 1] = '\0';
193 hash = strchr(qanswer, '#');
198 * Find beginning of value (query answer)
200 for (cp = qanswer; *cp && !isascii((int)*cp) && !isspace((int)*cp); cp++)
203 /* Ignore blank lines */
208 * Return a copy of the data
212 dlog("%s returns %s", key, dc);
219 * If the last read didn't get a whole line then
220 * throw away the remainder before continuing...
223 while (fgets_timed(qanswer, sizeof(qanswer), fd, gopt.exec_map_timeout) &&
224 !strchr(qanswer, '\n')) ;
241 if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
242 plog(XLOG_WARNING, "set_nonblock fcntl F_GETFL error: %m");
247 if (fcntl(fd, F_SETFL, val) < 0) {
248 plog(XLOG_WARNING, "set_nonblock fcntl F_SETFL error: %m");
257 exec_map_open(char *emap, char *key)
260 int pdes[2], nullfd, i;
270 if ((nullfd = open("/dev/null", O_WRONLY|O_NOCTTY)) < 0)
273 if (pipe(pdes) < 0) {
278 switch ((p1 = vfork())) {
280 /* parent: fork error */
290 /* child #1: fork error */
293 /* child #2: init will reap our status */
294 if (pdes[1] != STDOUT_FILENO) {
295 dup2(pdes[1], STDOUT_FILENO);
299 if (nullfd != STDERR_FILENO) {
300 dup2(nullfd, STDERR_FILENO);
304 for (i=0; i<FD_SETSIZE; i++)
305 if (i != STDOUT_FILENO && i != STDERR_FILENO)
308 /* make the write descriptor non-blocking */
309 if (!set_nonblock(STDOUT_FILENO)) {
310 close(STDOUT_FILENO);
314 execve(emap, argv, NULL);
315 exit(errno); /* in case execve failed */
326 /* anti-zombie insurance */
327 while (waitpid(p1,0,0) < 0)
331 /* make the read descriptor non-blocking */
332 if (!set_nonblock(pdes[0])) {
342 * Check for various permissions on executable map without trying to
343 * fork a new executable-map process.
345 * return: >0 (errno) if failed
349 exec_check_perm(char *map)
353 /* sanity and permission checks */
355 dlog("exec_check_permission got a NULL map");
358 if (stat(map, &sb)) {
359 plog(XLOG_ERROR, "map \"%s\" stat failure: %m", map);
362 if (!S_ISREG(sb.st_mode)) {
363 plog(XLOG_ERROR, "map \"%s\" should be regular file", map);
366 if (sb.st_uid != 0) {
367 plog(XLOG_ERROR, "map \"%s\" owned by uid %u (must be 0)", map, (u_int) sb.st_uid);
370 if (!(sb.st_mode & S_IXUSR)) {
371 plog(XLOG_ERROR, "map \"%s\" should be executable", map);
374 if (sb.st_mode & (S_ISUID|S_ISGID)) {
375 plog(XLOG_ERROR, "map \"%s\" should not be setuid/setgid", map);
378 if (sb.st_mode & S_IWOTH) {
379 plog(XLOG_ERROR, "map \"%s\" should not be world writeable", map);
383 return 0; /* all is well */
388 exec_init(mnt_map *m, char *map, time_t *tp)
391 * Basically just test that the executable map can be found
392 * and has proper permissions.
394 return exec_check_perm(map);
399 exec_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
403 if ((ret = exec_check_perm(map)) != 0) {
412 dlog("exec_search \"%s\", key: \"%s\"", map, key);
413 mapfd = exec_map_open(map, key);
417 *tp = clocktime(NULL);
419 return exec_parse_qanswer(mapfd, map, key, pval, tp);