lib: Add guestfs___remove_tmpdir helper function.
[libguestfs.git] / src / filearch.c
1 /* libguestfs
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #include <pcre.h>
30
31 #ifdef HAVE_LIBMAGIC
32 #include <magic.h>
33 #endif
34
35 #include "ignore-value.h"
36
37 #include "guestfs.h"
38 #include "guestfs-internal.h"
39 #include "guestfs-internal-actions.h"
40 #include "guestfs_protocol.h"
41
42 #if defined(HAVE_LIBMAGIC)
43
44 static pcre *re_file_elf;
45 static pcre *re_elf_ppc64;
46
47 static void compile_regexps (void) __attribute__((constructor));
48 static void free_regexps (void) __attribute__((destructor));
49
50 static void
51 compile_regexps (void)
52 {
53   const char *err;
54   int offset;
55
56 #define COMPILE(re,pattern,options)                                     \
57   do {                                                                  \
58     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
59     if (re == NULL) {                                                   \
60       ignore_value (write (2, err, strlen (err)));                      \
61       abort ();                                                         \
62     }                                                                   \
63   } while (0)
64
65   COMPILE (re_file_elf,
66            "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
67   COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
68 }
69
70 static void
71 free_regexps (void)
72 {
73   pcre_free (re_file_elf);
74   pcre_free (re_elf_ppc64);
75 }
76
77 /* Convert output from 'file' command on ELF files to the canonical
78  * architecture string.  Caller must free the result.
79  */
80 static char *
81 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
82 {
83   const char *r;
84
85   if (strstr (elf_arch, "Intel 80386"))
86     r = "i386";
87   else if (strstr (elf_arch, "Intel 80486"))
88     r = "i486";
89   else if (strstr (elf_arch, "x86-64"))
90     r = "x86_64";
91   else if (strstr (elf_arch, "AMD x86-64"))
92     r = "x86_64";
93   else if (strstr (elf_arch, "SPARC32"))
94     r = "sparc";
95   else if (strstr (elf_arch, "SPARC V9"))
96     r = "sparc64";
97   else if (strstr (elf_arch, "IA-64"))
98     r = "ia64";
99   else if (match (g, elf_arch, re_elf_ppc64))
100     r = "ppc64";
101   else if (strstr (elf_arch, "PowerPC"))
102     r = "ppc";
103   else
104     r = elf_arch;
105
106   char *ret = safe_strdup (g, r);
107   return ret;
108 }
109
110 static int
111 is_regular_file (const char *filename)
112 {
113   struct stat statbuf;
114
115   return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
116 }
117
118 /* Download and uncompress the cpio file to find binaries within.
119  * Notes:
120  * (1) Two lists must be identical.
121  * (2) Implicit limit of 31 bytes for length of each element (see code
122  * below).
123  */
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"}
126
127 static char *
128 cpio_arch (guestfs_h *g, const char *file, const char *path)
129 {
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)
135   char cmd[cmd_len];
136 #define bin_len (dir_len + 32)
137   char bin[bin_len];
138
139   char *ret = NULL;
140
141   const char *method;
142   if (strstr (file, "gzip"))
143     method = "zcat";
144   else if (strstr (file, "bzip2"))
145     method = "bzcat";
146   else
147     method = "cat";
148
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)"),
153            path, size);
154     goto out;
155   }
156
157   if (mkdtemp (dir) == NULL) {
158     perrorf (g, "mkdtemp");
159     goto out;
160   }
161
162   snprintf (initrd, initrd_len, "%s/initrd", dir);
163   if (guestfs_download (g, path, initrd) == -1)
164     goto out;
165
166   snprintf (cmd, cmd_len,
167             "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
168             dir, method);
169   int r = system (cmd);
170   if (r == -1 || WEXITSTATUS (r) != 0) {
171     perrorf (g, "cpio command failed");
172     goto out;
173   }
174
175   const char *bins[] = INITRD_BINARIES2;
176   size_t i;
177   for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
178     snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
179
180     if (is_regular_file (bin)) {
181       int flags = g->verbose ? MAGIC_DEBUG : 0;
182       flags |= MAGIC_ERROR | MAGIC_RAW;
183
184       magic_t m = magic_open (flags);
185       if (m == NULL) {
186         perrorf (g, "magic_open");
187         goto out;
188       }
189
190       if (magic_load (m, NULL) == -1) {
191         perrorf (g, "magic_load: default magic database file");
192         magic_close (m);
193         goto out;
194       }
195
196       const char *line = magic_file (m, bin);
197       if (line == NULL) {
198         perrorf (g, "magic_file: %s", bin);
199         magic_close (m);
200         goto out;
201       }
202
203       char *elf_arch;
204       if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
205         ret = canonical_elf_arch (g, elf_arch);
206         free (elf_arch);
207         magic_close (m);
208         goto out;
209       }
210       magic_close (m);
211     }
212   }
213   error (g, "file_architecture: could not determine architecture of cpio archive");
214
215  out:
216   guestfs___remove_tmpdir (dir);
217
218   return ret;
219 #undef dir_len
220 #undef initrd_len
221 #undef cmd_len
222 #undef bin_len
223 }
224
225 char *
226 guestfs__file_architecture (guestfs_h *g, const char *path)
227 {
228   char *file = NULL;
229   char *elf_arch = NULL;
230   char *ret = NULL;
231
232   /* Get the output of the "file" command.  Note that because this
233    * runs in the daemon, LANG=C so it's in English.
234    */
235   file = guestfs_file (g, path);
236   if (file == NULL)
237     return NULL;
238
239   if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
240     ret = canonical_elf_arch (g, elf_arch);
241   else if (strstr (file, "PE32 executable"))
242     ret = safe_strdup (g, "i386");
243   else if (strstr (file, "PE32+ executable"))
244     ret = safe_strdup (g, "x86_64");
245   else if (strstr (file, "cpio archive"))
246     ret = cpio_arch (g, file, path);
247   else
248     error (g, "file_architecture: unknown architecture: %s", path);
249
250   free (file);
251   free (elf_arch);
252   return ret;                   /* caller frees */
253 }
254
255 #else /* no libmagic at compile time */
256
257 /* XXX Should be an optgroup. */
258
259 #define NOT_IMPL(r)                                                     \
260   error (g, _("file-architecture API not available since this version of libguestfs was compiled without the libmagic library")); \
261   return r
262
263 char *
264 guestfs__file_architecture (guestfs_h *g, const char *path)
265 {
266   NOT_IMPL(NULL);
267 }
268
269 #endif /* no libmagic at compile time */