| 1 |
/* |
| 2 |
* GRUB -- GRand Unified Bootloader |
| 3 |
* Copyright (C) 1999,2000,2001,2002,2003,2004,2006,2007,2008,2009,2010,2011,2012,2013 Free Software Foundation, Inc. |
| 4 |
* |
| 5 |
* GRUB is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* GRUB is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with GRUB. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <config-util.h> |
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
#include <grub/emu/hostdisk.h> |
| 23 |
#include <grub/emu/exec.h> |
| 24 |
#include <grub/emu/config.h> |
| 25 |
#include <grub/util/install.h> |
| 26 |
#include <grub/util/misc.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/wait.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <stdlib.h> |
| 32 |
|
| 33 |
const char * |
| 34 |
grub_util_get_config_filename (void) |
| 35 |
{ |
| 36 |
static char *value = NULL; |
| 37 |
if (!value) |
| 38 |
value = grub_util_path_concat (3, GRUB_SYSCONFDIR, |
| 39 |
"default", "grub"); |
| 40 |
return value; |
| 41 |
} |
| 42 |
|
| 43 |
const char * |
| 44 |
grub_util_get_pkgdatadir (void) |
| 45 |
{ |
| 46 |
const char *ret = getenv ("pkgdatadir"); |
| 47 |
if (ret) |
| 48 |
return ret; |
| 49 |
return GRUB_DATADIR "/" PACKAGE; |
| 50 |
} |
| 51 |
|
| 52 |
const char * |
| 53 |
grub_util_get_pkglibdir (void) |
| 54 |
{ |
| 55 |
return GRUB_LIBDIR "/" PACKAGE; |
| 56 |
} |
| 57 |
|
| 58 |
const char * |
| 59 |
grub_util_get_localedir (void) |
| 60 |
{ |
| 61 |
return LOCALEDIR; |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
grub_util_load_config (struct grub_util_config *cfg) |
| 66 |
{ |
| 67 |
pid_t pid; |
| 68 |
const char *argv[4]; |
| 69 |
char *script, *ptr; |
| 70 |
const char *cfgfile, *iptr; |
| 71 |
FILE *f = NULL; |
| 72 |
int fd; |
| 73 |
const char *v; |
| 74 |
|
| 75 |
memset (cfg, 0, sizeof (*cfg)); |
| 76 |
|
| 77 |
v = getenv ("GRUB_ENABLE_CRYPTODISK"); |
| 78 |
if (v && v[0] == 'y' && v[1] == '\0') |
| 79 |
cfg->is_cryptodisk_enabled = 1; |
| 80 |
|
| 81 |
v = getenv ("GRUB_DISTRIBUTOR"); |
| 82 |
if (v) |
| 83 |
cfg->grub_distributor = xstrdup (v); |
| 84 |
|
| 85 |
cfgfile = grub_util_get_config_filename (); |
| 86 |
if (!grub_util_is_regular (cfgfile)) |
| 87 |
return; |
| 88 |
|
| 89 |
argv[0] = "sh"; |
| 90 |
argv[1] = "-c"; |
| 91 |
|
| 92 |
script = xcalloc (4, strlen (cfgfile) + 300); |
| 93 |
|
| 94 |
ptr = script; |
| 95 |
memcpy (ptr, ". '", 3); |
| 96 |
ptr += 3; |
| 97 |
for (iptr = cfgfile; *iptr; iptr++) |
| 98 |
{ |
| 99 |
if (*iptr == '\\') |
| 100 |
{ |
| 101 |
memcpy (ptr, "'\\''", 4); |
| 102 |
ptr += 4; |
| 103 |
continue; |
| 104 |
} |
| 105 |
*ptr++ = *iptr; |
| 106 |
} |
| 107 |
|
| 108 |
strcpy (ptr, "'; printf \"GRUB_ENABLE_CRYPTODISK=%s\\nGRUB_DISTRIBUTOR=%s\\n\" " |
| 109 |
"\"$GRUB_ENABLE_CRYPTODISK\" \"$GRUB_DISTRIBUTOR\""); |
| 110 |
|
| 111 |
argv[2] = script; |
| 112 |
argv[3] = '\0'; |
| 113 |
|
| 114 |
pid = grub_util_exec_pipe (argv, &fd); |
| 115 |
if (pid) |
| 116 |
f = fdopen (fd, "r"); |
| 117 |
if (f) |
| 118 |
{ |
| 119 |
grub_util_parse_config (f, cfg, 1); |
| 120 |
fclose (f); |
| 121 |
} |
| 122 |
if (pid) |
| 123 |
{ |
| 124 |
close (fd); |
| 125 |
waitpid (pid, NULL, 0); |
| 126 |
} |
| 127 |
if (f) |
| 128 |
return; |
| 129 |
|
| 130 |
f = grub_util_fopen (cfgfile, "r"); |
| 131 |
if (f) |
| 132 |
{ |
| 133 |
grub_util_parse_config (f, cfg, 0); |
| 134 |
fclose (f); |
| 135 |
} |
| 136 |
else |
| 137 |
grub_util_warn (_("cannot open configuration file `%s': %s"), |
| 138 |
cfgfile, strerror (errno)); |
| 139 |
} |