1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/*********************************************************
* Copyright (C) 2007 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
/*
* locationsdb.c --
*
* Provides the QueryLocationsDB routine for finding keys in the locations
* database. Because our application is a setuid binary and we want to
* minimize risk, we retain the duplicated functionality here rather than
* link against lib/unixinstall.
*/
#if !defined(sun) && !defined(__FreeBSD__) && !defined(linux)
# error This program is not supported on your platform.
#endif
#include <sys/param.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include "vm_basic_types.h"
#include "wrapper.h"
/*
* Local data
*/
/*
* Mappings between queries and search strings
*/
typedef struct Mapping {
const char *answer; /* string to match for "answer FOO" */
const char *remove; /* string to match for "remove_answer FOO" */
size_t answerSize; /* size of answer buffer */
size_t removeSize; /* size of remove buffer */
} Mapping;
/*
* queryMappings between Selector => search strings. Used by QueryLocationsDB.
*/
#define ANSWER_LIBDIR "answer LIBDIR"
#define REMOVE_LIBDIR "remove_answer LIBDIR"
#define ANSWER_BINDIR "answer BINDIR"
#define REMOVE_BINDIR "remove_answer BINDIR"
static Mapping queryMappings[] = {
{ ANSWER_LIBDIR, REMOVE_LIBDIR, sizeof ANSWER_LIBDIR, sizeof REMOVE_LIBDIR },
{ ANSWER_BINDIR, REMOVE_BINDIR, sizeof ANSWER_BINDIR, sizeof REMOVE_BINDIR },
{ 0, 0, 0, 0 }
};
/*
* Global functions (definitions)
*/
/*
*----------------------------------------------------------------------------
*
* QueryLocationsDB --
*
* Based on the caller's Selector, determines the directory selected as
* "LIBDIR", "BINDIR", etc. when the Tools were last configured.
*
* Results:
* TRUE on success, FALSE on failure. queryDir is filled in on success.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------------
*/
Bool
QueryLocationsDB(const char *locations, // IN : path of locations database
Selector selector, // IN : DB query to search for
char *queryDir, // OUT: address to write dirname to
size_t queryDirSize) // IN : size of queryDir buffer
{
FILE *file = NULL;
char buf[MAXPATHLEN];
Bool found = FALSE;
Mapping *map;
if (selector < 0 || selector >= QUERY_MAX) {
Error("Internal logic error. This is a bug.");
return FALSE;
}
file = fopen(locations, "r");
if (!file) {
return FALSE;
}
map = &queryMappings[selector];
/*
* We need to inspect the entire locations database since there are both
* "answer"s and "remove_answer"s. We want to provide the last answer that
* has not been removed.
*/
while (fgets(buf, sizeof buf, file)) {
if (strncmp(buf, map->answer, map->answerSize - 1) == 0) {
char *newline;
strncpy(queryDir, buf + map->answerSize, queryDirSize);
if (queryDir[queryDirSize - 1] != '\0') {
found = FALSE;
continue;
}
/* Truncate the string at the newline character, if it's present. */
newline = strchr(queryDir, '\n');
if (newline && newline - queryDir < queryDirSize) {
*newline = '\0';
}
found = TRUE;
} else if (strncmp(buf, map->remove, map->removeSize - 1) == 0) {
found = FALSE;
}
}
fclose(file);
return found;
}
|