1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
* md5sum.c - Generate/check MD5 Message Digests
*
* Compile and link with md5.c. If you don't have getopt() in your library
* also include getopt.c. For MSDOS you can also link with the wildcard
* initialization function (wildargs.obj for Turbo C and setargv.obj for MSC)
* so that you can use wildcards on the commandline.
*
* Written March 1993 by Branko Lankester
* Modified June 1993 by Colin Plumb for altered md5.c.
* Modified Feburary 1995 by Ian Jackson for use with Colin Plumb's md5.c.
* Hacked (modified is too nice a word) January 1997 by Galen Hazelwood
* to support GNU gettext.
* This file is in the public domain.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
/* Take care of NLS matters. */
#if HAVE_LOCALE_H
# include <locale.h>
#endif
#if !HAVE_SETLOCALE
# define setlocale(Category, Locale) /* empty */
#endif
#if ENABLE_NLS
# include <libintl.h>
# define _(Text) gettext (Text)
#else
# undef bindtextdomain
# define bindtextdomain(Domain, Directory) /* empty */
# undef textdomain
# define textdomain(Domain) /* empty */
# define _(Text) Text
#endif
#include <dpkg.h>
#ifdef UNIX
#define FOPRTXT "r"
#define FOPRBIN "r"
#else
#ifdef VMS
#define FOPRTXT "r","ctx=stm"
#define FOPRBIN "rb","ctx=stm"
#else
#define FOPRTXT "r"
#define FOPRBIN "rb"
#endif
#endif
const char printforhelp[]= N_("Type md5sum --help for help.");
const char thisname[]= MD5SUM;
void usage(void) NONRETURNING;
void print_digest(unsigned char *p);
int mdfile(int fd, unsigned char **digest);
int do_check(FILE *chkf);
int hex_digit(int c);
int get_md5_line(FILE *fp, unsigned char *digest, char *file);
int process_arg(const char* arg, int bin_mode, unsigned char **digest);
char *progname;
int verbose = 0;
int bin_mode = 0;
static
void
print_md5sum_error(const char* emsg, const char* arg) {
fprintf(stderr, _("error processing %s: %s\n"), arg, emsg);
}
static void cu_closefile(int argc, void** argv) {
FILE *f = (FILE*)(argv[0]);
fclose(f);
}
int
main(int argc, char **argv)
{
jmp_buf ejbuf;
int opt, rc = 0;
int check = 0;
FILE *fp = NULL;
unsigned char *digest = NULL;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
progname = *argv;
while ((opt = getopt(argc, argv, "cbvp:h")) != EOF) {
switch (opt) {
case 'c': check = 1; break;
case 'v': verbose = 1; break;
case 'b': bin_mode = 1; break;
default: usage();
}
}
argc -= optind;
argv += optind;
if (check) {
switch (argc) {
case 0: fp = stdin; break;
case 1: if ((fp = fopen(*argv, FOPRTXT)) == NULL) {
perror(*argv);
exit(2);
}
break;
default: usage();
}
exit(do_check(fp));
}
if (argc == 0) {
if (setjmp(ejbuf)) {
error_unwind(ehflag_bombout);
exit(1);
}
push_error_handler(&ejbuf, print_md5sum_error, "stdin");
mdfile(fileno(stdin), &digest);
printf("%s %c-\n", digest, bin_mode ? '*' : ' ');
set_error_display(0, 0);
error_unwind(ehflag_normaltidy);
exit(0);
}
for ( ; argc > 0; --argc, ++argv) {
switch (process_arg(*argv, bin_mode, &digest)) {
case 1:
rc++;
break;
case 2:
perror(*argv);
rc++;
break;
case 3:
perror(*argv);
rc++;
break;
default:
printf("%s %c%s\n", digest, bin_mode ? '*' : ' ', *argv);
}
}
return rc;
}
int process_arg(const char* arg, int bin_mode, unsigned char **digest)
{
jmp_buf ejbuf;
FILE *fp = NULL;
if (setjmp(ejbuf)) {
error_unwind(ehflag_bombout);
return 1;
}
push_error_handler(&ejbuf, print_md5sum_error, arg);
if (bin_mode)
fp = fopen(arg, FOPRBIN);
else
fp = fopen(arg, FOPRTXT);
if (fp == NULL)
return 2;
push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)fp);
if (mdfile(fileno(fp), digest)) {
fclose(fp);
return 3;
}
pop_cleanup(ehflag_normaltidy); /* fd= fopen() */
fclose(fp);
set_error_display(0, 0);
error_unwind(ehflag_normaltidy);
return 0;
}
void
usage(void)
{
fputs(_("usage: md5sum [-bv] [-c [file]] | [file...]\n\
Generates or checks MD5 Message Digests\n\
-c check message digests (default is generate)\n\
-v verbose, print file names when checking\n\
-b read files in binary mode\n\
The input for -c should be the list of message digests and file names\n\
that is printed on stdout by this program when it generates digests.\n"), stderr);
exit(2);
}
int
mdfile(int fd, unsigned char **digest)
{
off_t ret = fd_md5(fd, digest, -1, _("mdfile"));
if ( ret >= 0 )
return 0;
else
return ret;
}
int
hex_digit(int c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return -1;
}
int
get_md5_line(FILE *fp, unsigned char *digest, char *file)
{
char buf[1024];
int i, rc;
char *p = buf;
if (fgets(buf, sizeof(buf), fp) == NULL)
return -1;
/* A line must have: a digest (32), a separator (2), and a
* filename (at least 1)
*
* That means it must be at least 35 characters long.
*/
if (strlen(buf) < 35)
return 0;
memcpy(digest, p, 32);
p += 32;
if (*p++ != ' ')
return 0;
/*
* next char is an attribute char, space means text file
* if it's a '*' the file should be checked in binary mode.
*/
if (*p == ' ')
rc = 1;
else if (*p == '*')
rc = 2;
else {
/* Don't print the buffer; we might be dealing with a
* non-text file.
*/
fprintf(stderr, _("%s: unrecognized line\n"), progname);
return 0;
}
++p;
i = strlen(p);
if (i < 2 || i > 255)
return 0;
/* Strip the trailing newline, if present */
if (p[i-1] == '\n') {
if (p[i-2] == '\r') {
if (i < 3)
return 0;
p[i-2] = '\0';
} else {
p[i-1] = '\0';
}
}
strcpy(file, p);
return rc;
}
int
do_check(FILE *chkf)
{
int rc, ex = 0, failed = 0, checked = 0;
unsigned char chk_digest[32], *file_digest = NULL;
char filename[256];
size_t flen = 14;
while ((rc = get_md5_line(chkf, chk_digest, filename)) >= 0) {
if (rc == 0) /* not an md5 line */
continue;
if (verbose) {
if (strlen(filename) > flen)
flen = strlen(filename);
fprintf(stderr, "%-*s ", (int)flen, filename);
}
switch (process_arg(filename, bin_mode || rc == 2, &file_digest)) {
case 2:
fprintf(stderr, _("%s: can't open %s\n"), progname, filename);
ex = 2;
continue;
case 3:
fprintf(stderr, _("%s: error reading %s\n"), progname, filename);
ex = 2;
continue;
}
if (memcmp(chk_digest, file_digest, 32) != 0) {
if (verbose)
fprintf(stderr, _("FAILED\n"));
else
fprintf(stderr, _("%s: MD5 check failed for '%s'\n"), progname, filename);
++failed;
} else if (verbose)
fprintf(stderr, _("OK\n"));
++checked;
}
if (verbose && failed)
fprintf(stderr, _("%s: %d of %d file(s) failed MD5 check\n"), progname, failed, checked);
if (!checked) {
fprintf(stderr, _("%s: no files checked\n"), progname);
return 3;
}
if (!ex && failed)
ex = 1;
return ex;
}
|