2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 #include "ignore-value.h"
38 #include "guestfs-internal.h"
39 #include "guestfs-internal-actions.h"
40 #include "guestfs_protocol.h"
42 #if defined(HAVE_LIBMAGIC)
44 static pcre *re_file_elf;
45 static pcre *re_elf_ppc64;
47 static void compile_regexps (void) __attribute__((constructor));
48 static void free_regexps (void) __attribute__((destructor));
51 compile_regexps (void)
56 #define COMPILE(re,pattern,options) \
58 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
60 ignore_value (write (2, err, strlen (err))); \
66 "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
67 COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
73 pcre_free (re_file_elf);
74 pcre_free (re_elf_ppc64);
77 /* Convert output from 'file' command on ELF files to the canonical
78 * architecture string. Caller must free the result.
81 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
85 if (strstr (elf_arch, "Intel 80386"))
87 else if (strstr (elf_arch, "Intel 80486"))
89 else if (strstr (elf_arch, "x86-64"))
91 else if (strstr (elf_arch, "AMD x86-64"))
93 else if (strstr (elf_arch, "SPARC32"))
95 else if (strstr (elf_arch, "SPARC V9"))
97 else if (strstr (elf_arch, "IA-64"))
99 else if (match (g, elf_arch, re_elf_ppc64))
101 else if (strstr (elf_arch, "PowerPC"))
106 char *ret = safe_strdup (g, r);
111 is_regular_file (const char *filename)
115 return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
118 /* Download and uncompress the cpio file to find binaries within.
120 * (1) Two lists must be identical.
121 * (2) Implicit limit of 31 bytes for length of each element (see code
124 #define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
125 #define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
128 cpio_arch (guestfs_h *g, const char *file, const char *path)
130 TMP_TEMPLATE_ON_STACK (dir);
131 #define dir_len (strlen (dir))
132 #define initrd_len (dir_len + 16)
133 char initrd[initrd_len];
134 #define cmd_len (dir_len + 256)
136 #define bin_len (dir_len + 32)
142 if (strstr (file, "gzip"))
144 else if (strstr (file, "bzip2"))
149 /* Security: Refuse to download initrd if it is huge. */
150 int64_t size = guestfs_filesize (g, path);
151 if (size == -1 || size > 100000000) {
152 error (g, _("size of %s unreasonable (%" PRIi64 " bytes)"),
157 if (mkdtemp (dir) == NULL) {
158 perrorf (g, "mkdtemp");
162 snprintf (initrd, initrd_len, "%s/initrd", dir);
163 if (guestfs_download (g, path, initrd) == -1)
166 snprintf (cmd, cmd_len,
167 "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
169 int r = system (cmd);
170 if (r == -1 || WEXITSTATUS (r) != 0) {
171 perrorf (g, "cpio command failed");
175 const char *bins[] = INITRD_BINARIES2;
177 for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
178 snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
180 if (is_regular_file (bin)) {
181 int flags = g->verbose ? MAGIC_DEBUG : 0;
182 flags |= MAGIC_ERROR | MAGIC_RAW;
184 magic_t m = magic_open (flags);
186 perrorf (g, "magic_open");
190 if (magic_load (m, NULL) == -1) {
191 perrorf (g, "magic_load: default magic database file");
196 const char *line = magic_file (m, bin);
198 perrorf (g, "magic_file: %s", bin);
204 if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
205 ret = canonical_elf_arch (g, elf_arch);
213 error (g, "file_architecture: could not determine architecture of cpio archive");
216 /* Free up the temporary directory. Note the directory name cannot
217 * contain shell meta-characters because of the way it was
220 snprintf (cmd, cmd_len, "rm -rf %s", dir);
221 ignore_value (system (cmd));
231 guestfs__file_architecture (guestfs_h *g, const char *path)
234 char *elf_arch = NULL;
237 /* Get the output of the "file" command. Note that because this
238 * runs in the daemon, LANG=C so it's in English.
240 file = guestfs_file (g, path);
244 if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
245 ret = canonical_elf_arch (g, elf_arch);
246 else if (strstr (file, "PE32 executable"))
247 ret = safe_strdup (g, "i386");
248 else if (strstr (file, "PE32+ executable"))
249 ret = safe_strdup (g, "x86_64");
250 else if (strstr (file, "cpio archive"))
251 ret = cpio_arch (g, file, path);
253 error (g, "file_architecture: unknown architecture: %s", path);
257 return ret; /* caller frees */
260 #else /* no libmagic at compile time */
262 /* XXX Should be an optgroup. */
264 #define NOT_IMPL(r) \
265 error (g, _("file-architecture API not available since this version of libguestfs was compiled without the libmagic library")); \
269 guestfs__file_architecture (guestfs_h *g, const char *path)
274 #endif /* no libmagic at compile time */