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