~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/commands/hashsum.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2009  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 <grub/dl.h>
 
20
#include <grub/extcmd.h>
 
21
#include <grub/file.h>
 
22
#include <grub/disk.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/misc.h>
 
25
#include <grub/crypto.h>
 
26
#include <grub/normal.h>
 
27
#include <grub/i18n.h>
 
28
 
 
29
static const struct grub_arg_option options[] = {
 
30
  {"hash", 'h', 0, N_("Specify hash to use."), N_("HASH"), ARG_TYPE_STRING},
 
31
  {"check", 'c', 0, N_("Check hash list file."), N_("FILE"), ARG_TYPE_STRING},
 
32
  {"prefix", 'p', 0, N_("Base directory for hash list."), N_("DIRECTORY"),
 
33
   ARG_TYPE_STRING},
 
34
  {"keep-going", 'k', 0, N_("Don't stop after first error."), 0, 0},
 
35
  {"uncompress", 'u', 0, N_("Uncompress file before checksumming."), 0, 0},
 
36
  {0, 0, 0, 0, 0, 0}
 
37
};
 
38
 
 
39
struct { const char *name; const char *hashname; } aliases[] = 
 
40
  {
 
41
    {"sha256sum", "sha256"},
 
42
    {"sha512sum", "sha512"},
 
43
    {"sha1sum", "sha1"},
 
44
    {"md5sum", "md5"},
 
45
    {"crc", "crc32"},
 
46
  };
 
47
 
 
48
static inline int
 
49
hextoval (char c)
 
50
{
 
51
  if (c >= '0' && c <= '9')
 
52
    return c - '0';
 
53
  if (c >= 'a' && c <= 'f')
 
54
    return c - 'a' + 10;
 
55
  if (c >= 'A' && c <= 'F')
 
56
    return c - 'A' + 10;
 
57
  return -1;
 
58
}
 
59
 
 
60
static grub_err_t
 
61
hash_file (grub_file_t file, const gcry_md_spec_t *hash, void *result)
 
62
{
 
63
  grub_uint8_t context[hash->contextsize];
 
64
  grub_uint8_t readbuf[4096];
 
65
 
 
66
  grub_memset (context, 0, sizeof (context));
 
67
  hash->init (context);
 
68
  while (1)
 
69
    {
 
70
      grub_ssize_t r;
 
71
      r = grub_file_read (file, readbuf, sizeof (readbuf));
 
72
      if (r < 0)
 
73
        return grub_errno;
 
74
      if (r == 0)
 
75
        break;
 
76
      hash->write (context, readbuf, r);
 
77
    }
 
78
  hash->final (context);
 
79
  grub_memcpy (result, hash->read (context), hash->mdlen);
 
80
 
 
81
  return GRUB_ERR_NONE;
 
82
}
 
83
 
 
84
static grub_err_t
 
85
check_list (const gcry_md_spec_t *hash, const char *hashfilename,
 
86
            const char *prefix, int keep, int uncompress)
 
87
{
 
88
  grub_file_t hashlist, file;
 
89
  char *buf = NULL;
 
90
  grub_uint8_t expected[hash->mdlen];
 
91
  grub_uint8_t actual[hash->mdlen];
 
92
  grub_err_t err;
 
93
  unsigned i;
 
94
  unsigned unread = 0, mismatch = 0;
 
95
 
 
96
  hashlist = grub_file_open (hashfilename);
 
97
  if (!hashlist)
 
98
    return grub_errno;
 
99
  
 
100
  while (grub_free (buf), (buf = grub_file_getline (hashlist)))
 
101
    {
 
102
      const char *p = buf;
 
103
      for (i = 0; i < hash->mdlen; i++)
 
104
        {
 
105
          int high, low;
 
106
          high = hextoval (*p++);
 
107
          low = hextoval (*p++);
 
108
          if (high < 0 || low < 0)
 
109
            return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
 
110
          expected[i] = (high << 4) | low;
 
111
        }
 
112
      if (*p++ != ' ' || *p++ != ' ')
 
113
        return grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid hash list");
 
114
      if (prefix)
 
115
        {
 
116
          char *filename;
 
117
          
 
118
          filename = grub_xasprintf ("%s/%s", prefix, p);
 
119
          if (!filename)
 
120
            return grub_errno;
 
121
          if (!uncompress)
 
122
            grub_file_filter_disable_compression ();
 
123
          file = grub_file_open (filename);
 
124
          grub_free (filename);
 
125
        }
 
126
      else
 
127
        {
 
128
          if (!uncompress)
 
129
            grub_file_filter_disable_compression ();
 
130
          file = grub_file_open (p);
 
131
        }
 
132
      if (!file)
 
133
        {
 
134
          grub_file_close (hashlist);
 
135
          grub_free (buf);
 
136
          return grub_errno;
 
137
        }
 
138
      err = hash_file (file, hash, actual);
 
139
      grub_file_close (file);
 
140
      if (err)
 
141
        {
 
142
          grub_printf ("%s: READ ERROR\n", p);
 
143
          if (!keep)
 
144
            {
 
145
              grub_file_close (hashlist);
 
146
              grub_free (buf);
 
147
              return err;
 
148
            }
 
149
          grub_print_error ();
 
150
          grub_errno = GRUB_ERR_NONE;
 
151
          unread++;
 
152
          continue;
 
153
        }
 
154
      if (grub_crypto_memcmp (expected, actual, hash->mdlen) != 0)
 
155
        {
 
156
          grub_printf ("%s: HASH MISMATCH\n", p);
 
157
          if (!keep)
 
158
            {
 
159
              grub_file_close (hashlist);
 
160
              grub_free (buf);
 
161
              return grub_error (GRUB_ERR_TEST_FAILURE,
 
162
                                 "hash of '%s' mismatches", p);
 
163
            }
 
164
          mismatch++;
 
165
          continue;       
 
166
        }
 
167
      grub_printf ("%s: OK\n", p);
 
168
    }
 
169
  if (mismatch || unread)
 
170
    return grub_error (GRUB_ERR_TEST_FAILURE,
 
171
                       "%d files couldn't be read and hash "
 
172
                       "of %d files mismatches", unread, mismatch);
 
173
  return GRUB_ERR_NONE;
 
174
}
 
175
 
 
176
static grub_err_t
 
177
grub_cmd_hashsum (struct grub_extcmd_context *ctxt,
 
178
                  int argc, char **args)
 
179
{
 
180
  struct grub_arg_list *state = ctxt->state;
 
181
  const char *hashname = NULL;
 
182
  const char *prefix = NULL;
 
183
  const gcry_md_spec_t *hash;
 
184
  unsigned i;
 
185
  int keep = state[3].set;
 
186
  int uncompress = state[4].set;
 
187
  unsigned unread = 0;
 
188
 
 
189
  for (i = 0; i < ARRAY_SIZE (aliases); i++)
 
190
    if (grub_strcmp (ctxt->extcmd->cmd->name, aliases[i].name) == 0)
 
191
      hashname = aliases[i].hashname;
 
192
  if (state[0].set)
 
193
    hashname = state[0].arg;
 
194
 
 
195
  if (!hashname)
 
196
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "no hash specified");
 
197
 
 
198
  hash = grub_crypto_lookup_md_by_name (hashname);
 
199
  if (!hash)
 
200
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown hash");
 
201
 
 
202
  if (state[2].set)
 
203
    prefix = state[2].arg;
 
204
 
 
205
  if (state[1].set)
 
206
    {
 
207
      if (argc != 0)
 
208
        return grub_error (GRUB_ERR_BAD_ARGUMENT,
 
209
                           "--check is incompatible with file list");
 
210
      return check_list (hash, state[1].arg, prefix, keep, uncompress);
 
211
    }
 
212
 
 
213
  for (i = 0; i < (unsigned) argc; i++)
 
214
    {
 
215
      grub_uint8_t result[hash->mdlen];
 
216
      grub_file_t file;
 
217
      grub_err_t err;
 
218
      unsigned j;
 
219
      if (!uncompress)
 
220
        grub_file_filter_disable_compression ();
 
221
      file = grub_file_open (args[i]);
 
222
      if (!file)
 
223
        {
 
224
          if (!keep)
 
225
            return grub_errno;
 
226
          grub_print_error ();
 
227
          grub_errno = GRUB_ERR_NONE;
 
228
          unread++;
 
229
          continue;
 
230
        }
 
231
      err = hash_file (file, hash, result);
 
232
      grub_file_close (file);
 
233
      if (err)
 
234
        {
 
235
          if (!keep)
 
236
            return err;
 
237
          grub_print_error ();
 
238
          grub_errno = GRUB_ERR_NONE;
 
239
          unread++;
 
240
          continue;
 
241
        }
 
242
      for (j = 0; j < hash->mdlen; j++)
 
243
        grub_printf ("%02x", result[j]);
 
244
      grub_printf ("  %s\n", args[i]);
 
245
    }
 
246
 
 
247
  if (unread)
 
248
    return grub_error (GRUB_ERR_TEST_FAILURE, "%d files couldn't be read.",
 
249
                       unread);
 
250
  return GRUB_ERR_NONE;
 
251
}
 
252
 
 
253
static grub_extcmd_t cmd, cmd_md5, cmd_sha1, cmd_sha256, cmd_sha512, cmd_crc;
 
254
 
 
255
GRUB_MOD_INIT(hashsum)
 
256
{
 
257
  cmd = grub_register_extcmd ("hashsum", grub_cmd_hashsum, 0,
 
258
                              "hashsum -h HASH [-c FILE [-p PREFIX]] "
 
259
                              "[FILE1 [FILE2 ...]]",
 
260
                              N_("Compute or check hash checksum."),
 
261
                              options);
 
262
  cmd_md5 = grub_register_extcmd ("md5sum", grub_cmd_hashsum, 0,
 
263
                                  N_("[-c FILE [-p PREFIX]] "
 
264
                                     "[FILE1 [FILE2 ...]]"),
 
265
                                  N_("Compute or check hash checksum."),
 
266
                                  options);
 
267
  cmd_sha1 = grub_register_extcmd ("sha1sum", grub_cmd_hashsum, 0,
 
268
                                   N_("[-c FILE [-p PREFIX]] "
 
269
                                      "[FILE1 [FILE2 ...]]"),
 
270
                                   N_("Compute or check hash checksum."),
 
271
                                   options);
 
272
  cmd_sha256 = grub_register_extcmd ("sha256sum", grub_cmd_hashsum, 0,
 
273
                                     N_("[-c FILE [-p PREFIX]] "
 
274
                                        "[FILE1 [FILE2 ...]]"),
 
275
                                     N_("Compute or check hash checksum."),
 
276
                                     options);
 
277
  cmd_sha512 = grub_register_extcmd ("sha512sum", grub_cmd_hashsum, 0,
 
278
                                     N_("[-c FILE [-p PREFIX]] "
 
279
                                        "[FILE1 [FILE2 ...]]"),
 
280
                                     N_("Compute or check hash checksum."),
 
281
                                     options);
 
282
 
 
283
  cmd_crc = grub_register_extcmd ("crc", grub_cmd_hashsum, 0,
 
284
                                     N_("[-c FILE [-p PREFIX]] "
 
285
                                        "[FILE1 [FILE2 ...]]"),
 
286
                                     N_("Compute or check hash checksum."),
 
287
                                     options);
 
288
}
 
289
 
 
290
GRUB_MOD_FINI(hashsum)
 
291
{
 
292
  grub_unregister_extcmd (cmd);
 
293
  grub_unregister_extcmd (cmd_md5);
 
294
  grub_unregister_extcmd (cmd_sha1);
 
295
  grub_unregister_extcmd (cmd_sha256);
 
296
  grub_unregister_extcmd (cmd_sha512);
 
297
  grub_unregister_extcmd (cmd_crc);
 
298
}