Ejemplo n.º 1
0
/*
 * Implements random sort (-R).
 */
static int
randomcoll(struct key_value *kv1, struct key_value *kv2, size_t offset)
{
	struct bwstring *s1, *s2;
	MD5_CTX ctx1, ctx2;
	char *b1, *b2;

	UNUSED_ARG(offset);

	s1 = kv1->k;
	s2 = kv2->k;

	if (debug_sort) {
		bwsprintf(stdout, s1, "; k1=<", ">");
		bwsprintf(stdout, s2, ", k2=<", ">");
	}

	if (s1 == s2)
		return (0);

	memcpy(&ctx1,&md5_ctx,sizeof(MD5_CTX));
	memcpy(&ctx2,&md5_ctx,sizeof(MD5_CTX));

	MD5Update(&ctx1, bwsrawdata(s1), bwsrawlen(s1));
	MD5Update(&ctx2, bwsrawdata(s2), bwsrawlen(s2));
	b1 = MD5End(&ctx1, NULL);
	b2 = MD5End(&ctx2, NULL);
	if (b1 == NULL) {
		if (b2 == NULL)
			return (0);
		else {
			sort_free(b2);
			return (-1);
		}
	} else if (b2 == NULL) {
		sort_free(b1);
		return (+1);
	} else {
		int cmp_res;

		cmp_res = strcmp(b1,b2);
		sort_free(b1);
		sort_free(b2);

		if (!cmp_res)
			cmp_res = bwscoll(s1, s2, 0);

		return (cmp_res);
	}
}
Ejemplo n.º 2
0
char *
MD5Data(const u_char *data, size_t len, char *buf)
{
	MD5_CTX ctx;

	MD5Init(&ctx);
	MD5Update(&ctx, data, len);
	return (MD5End(&ctx, buf));
}
Ejemplo n.º 3
0
char *
MD5Data (const unsigned char *data, unsigned int len, char *buf)
{
    MD5_CTX ctx;

    MD5Init(&ctx);
    MD5Update(&ctx,data,len);
    return MD5End(&ctx, buf);
}
Ejemplo n.º 4
0
int
do_auth(void)
{
	char buf[100], *digest;
	char *p, *d;
	MD5_CTX ctx;
	int n;

	if ((n = read(sock_fd, buf, sizeof(buf) - 1)) == -1) {
		perror("read");
		exit(1);
	}
	buf[n] = '\0';
#ifdef DUMPSTAT_DEBUG
	fprintf(stderr, "challenge: %s\n", buf);
#endif
	for (p = buf; isblank(*p); p++)
		continue;

	if (strncasecmp("CHAL", p, 4))
		return (-1);
	p += 4;
	while (isblank(*p))
		p++;
	d = p;
	while (isascii(*p) && !isspace(*p) && *p != '\0')
		p++;
	*p = '\0';

#ifdef DUMPSTAT_DEBUG
	fprintf(stderr, "challenge: %s, len: %d\n", d, p - d);
#endif

	MD5Init(&ctx);
	MD5Update(&ctx, (u_char *) d, p - d);
	MD5Update(&ctx, password, strlen(password));
	digest = MD5End(&ctx, NULL);
	snprintf(buf, sizeof(buf), "AUTH %s\n", digest);
#ifdef DUMPSTAT_DEBUG
	fprintf(stderr, "digest: %s, len: %d\n", buf, strlen(buf));
#endif
	if ((n = write(sock_fd, buf, strlen(buf))) == -1) {
		perror("write");
		exit(1);
	}
	if ((n = read(sock_fd, buf, sizeof(buf))) == -1) {
		perror("read");
		exit(1);
	}
	if (!strncmp(buf, "200", 3))
		return (0);
	return (-1);
}
Ejemplo n.º 5
0
ATF_TC_BODY(memcpy_basic, tc)
{
	int i, j;
	testBlock_t auto1, auto2;

	start[2] = auto1;
	start[3] = auto2;

	srandom(0L);
	MD5Init(mc);
	for (i = 0; i < BLOCKTYPES; ++i)
		for (j = 0; j < BLOCKTYPES; ++j)
			if (i != j)
				runTest(start[i], start[j]);
	MD5End(mc, result);
	ATF_REQUIRE_EQ(strcmp(result, goodResult), 0);
}
Ejemplo n.º 6
0
char *MD5File (const char *filename, char *buf)
{
    unsigned char buffer[BUFSIZ];
    MD5_CTX ctx;
    int f,i,j;

    MD5Init(&ctx);
    f = open(filename,O_RDONLY);
    if (f < 0) return 0;
    while ((i = read(f,buffer,sizeof buffer)) > 0) {
	MD5Update(&ctx,buffer,i);
    }
    j = errno;
    close(f);
    errno = j;
    if (i < 0) return 0;
    return MD5End(&ctx, buf);
}
Ejemplo n.º 7
0
char *
MD5FileChunk(const char *filename, char *buf, off_t ofs, off_t len)
{
	unsigned char buffer[BUFSIZ];
	MD5_CTX ctx;
	struct stat stbuf;
	int f, i, e;
	off_t n;

	MD5Init(&ctx);
#if _WIN32
	f = _open(filename, O_RDONLY|O_BINARY);
#else
	f = open(filename, O_RDONLY);
#endif
	if (f < 0)
		return 0;
	if (fstat(f, &stbuf) < 0)
		return 0;
	if (ofs > stbuf.st_size)
		ofs = stbuf.st_size;
	if ((len == 0) || (len > stbuf.st_size - ofs))
		len = stbuf.st_size - ofs;
	if (lseek(f, ofs, SEEK_SET) < 0)
		return 0;
	n = len;
	i = 0;
	while (n > 0) {
		if (n > sizeof(buffer))
			i = read(f, buffer, sizeof(buffer));
		else
			i = read(f, buffer, n);
		if (i < 0)
			break;
		MD5Update(&ctx, buffer, i);
		n -= i;
	}
	e = errno;
	close(f);
	errno = e;
	if (i < 0)
		return 0;
	return (MD5End(&ctx, buf));
}
Ejemplo n.º 8
0
static char *
digest_end(DIGEST_CTX *c, char *buf)
{

	switch (digesttype) {
	case DIGEST_MD5:
		return (MD5End(&(c->MD5), buf));
	case DIGEST_RIPEMD160:
		return (RIPEMD160_End(&(c->RIPEMD160), buf));
	case DIGEST_SHA1:
		return (SHA1_End(&(c->SHA1), buf));
	case DIGEST_SHA256:
		return (SHA256_End(&(c->SHA256), buf));
	case DIGEST_SHA512:
		return (SHA512_End(&(c->SHA512), buf));
	default:
		return (NULL);
	}
}
Ejemplo n.º 9
0
static int
invoke_md5(size_t len, __capability char *data_input,
  __capability char *data_output)
{
	MD5_CTX md5context;
	char buf[33], ch;
	u_int count;

	MD5Init(&md5context);
	for (count = 0; count < len; count++) {
		/* XXXRW: Want a CMD5Update() to avoid copying byte by byte. */
		ch = data_input[count];
		MD5Update(&md5context, &ch, sizeof(ch));
	}
	MD5End(&md5context, buf);
	for (count = 0; count < sizeof(buf); count++)
		data_output[count] = buf[count];

	return (123456);
}
Ejemplo n.º 10
0
static int
receive_test(void)
{
	uint32_t header_length, offset, length, counter;
	struct test_header th;
	ssize_t len;
	char buf[10240];
	MD5_CTX md5ctx;
	char *rxmd5;

	len = read(accept_socket, &th, sizeof(th));
	if (len < 0 || (size_t)len < sizeof(th))
		FAIL_ERR("read")

	if (test_th(&th, &header_length, &offset, &length) != 0)
		return (-1);

	MD5Init(&md5ctx);

	counter = 0;
	while (1) {
		len = read(accept_socket, buf, sizeof(buf));
		if (len < 0 || len == 0)
			break;
		counter += len;
		MD5Update(&md5ctx, buf, len);
	}

	rxmd5 = MD5End(&md5ctx, NULL);
	
	if ((counter != header_length+length) || 
			memcmp(th.th_md5, rxmd5, 33) != 0)
		FAIL("receive length mismatch")

	free(rxmd5);
	return (0);
}
Ejemplo n.º 11
0
char *
MD5FileChunk(const char *filename, char *buf, off_t off, off_t len)
{
	struct stat sb;
	u_char buffer[BUFSIZ];
	MD5_CTX ctx;
	int fd, save_errno;
	ssize_t nr;

	MD5Init(&ctx);

	if ((fd = open(filename, O_RDONLY)) < 0)
		return (NULL);
	if (len == 0) {
		if (fstat(fd, &sb) == -1) {
			close(fd);
			return (NULL);
		}
		len = sb.st_size;
	}
	if (off > 0 && lseek(fd, off, SEEK_SET) < 0) {
		close(fd);
		return (NULL);
	}

	while ((nr = read(fd, buffer, MIN(sizeof(buffer), len))) > 0) {
		MD5Update(&ctx, buffer, (size_t)nr);
		if (len > 0 && (len -= nr) == 0)
			break;
	}

	save_errno = errno;
	close(fd);
	errno = save_errno;
	return (nr < 0 ? NULL : MD5End(&ctx, buf));
}
Ejemplo n.º 12
0
enum eav_error
extract_and_verify(unsigned char *ibuf, size_t ilen,
    unsigned char **obufp, size_t *olenp, size_t blocksize,
    enum eav_compression ctype,
    enum eav_digest dtype, const unsigned char *digest)
{
	int ret;
	unsigned char *obuf = NULL;
	size_t olen = 0, total_in, total_out;
	bz_stream bzs;
#ifdef MD5_SUPPORT
	size_t prev_total_in;
	MD5_CTX md5ctx;
	char i_md5sum[33];
#endif

	switch (ctype) {
	case EAV_COMP_NONE:
	case EAV_COMP_BZIP2:
		break;
	case EAV_COMP_GZIP:
	case EAV_COMP_XZ:
		return (EAV_ERR_COMP_UNSUPPORTED);
	default:
		return (EAV_ERR_COMP_UNKNOWN);
	}

	switch (dtype) {
	case EAV_DIGEST_NONE:
		break;
	case EAV_DIGEST_MD5:
#ifdef MD5_SUPPORT
		break;
#else
		return (EAV_ERR_DIGEST_UNSUPPORTED);
#endif

	default:
		return (EAV_ERR_DIGEST_UNKNOWN);
	}

	if (dtype || ctype) {
#ifdef MD5_SUPPORT
		if (dtype == EAV_DIGEST_MD5)
			MD5Init(&md5ctx);
#endif

		if (ctype) {
			/* XXX: assume bzip2 for now */
			olen = 1024 * 1024;
			if ((obuf = malloc(olen)) == NULL)
				return (EAV_ERR_MEM);

			total_in = 0;
#ifdef MD5_SUPPORT
			prev_total_in = 0;
#endif

			bzs.bzalloc = NULL;
			bzs.bzfree = NULL;
			bzs.opaque = NULL;
			bzs.next_in = (char *)ibuf;
			bzs.avail_in = MIN(ilen, 1024 * 1024);
			bzs.next_out = (char *)obuf;
			bzs.avail_out = olen;
			if (BZ2_bzDecompressInit(&bzs, 0, 0) != BZ_OK)
				return (EAV_ERR_COMP);

			while ((ret = BZ2_bzDecompress(&bzs)) !=
			    BZ_STREAM_END) {
				if (ret != BZ_OK) {
					free(obuf);
					BZ2_bzDecompressEnd(&bzs);
					return (EAV_ERR_COMP);
				}

				total_in = ((size_t)bzs.total_in_hi32 << 32) +
				    bzs.total_in_lo32;
				total_out = ((size_t)bzs.total_out_hi32 << 32) +
				    bzs.total_out_lo32;

#ifdef MD5_SUPPORT
				if (dtype == EAV_DIGEST_MD5)
					MD5Update(&md5ctx, ibuf + prev_total_in,
					    total_in - prev_total_in);
				prev_total_in = total_in;
#endif

				if (bzs.avail_in == 0)
					bzs.avail_in =
					    MIN(ilen - total_in, 1024 * 1024);

				if (bzs.avail_out == 0) {
					olen *= 2;
					if ((obuf = reallocf(obuf, olen))
					    == NULL) {
						BZ2_bzDecompressEnd(&bzs);
						return (EAV_ERR_COMP);
					}
					bzs.next_out = (char *)obuf + total_out;
					bzs.avail_out = olen - total_out;
				}
			}
			BZ2_bzDecompressEnd(&bzs);
			total_in = ((size_t)bzs.total_in_hi32 << 32) +
			    bzs.total_in_lo32;
			total_out = ((size_t)bzs.total_out_hi32 << 32) +
			    bzs.total_out_lo32;

#ifdef MD5_SUPPORT
			/* Push the last read block in the MD5 machine */
			if (dtype == EAV_DIGEST_MD5)
				MD5Update(&md5ctx, ibuf + prev_total_in,
				    total_in - prev_total_in);
#endif

			/* Round up to blocksize and zero pad */
			olen = roundup2(total_out, blocksize);
			if (olen != total_out)
				memset(obuf + total_out, '\0',
				    olen - total_out);
			/* XXX: realloc to shorten allocation? */
		} else if (dtype) {
#ifdef MD5_SUPPORT
			if (dtype == EAV_DIGEST_MD5)
				MD5Update(&md5ctx, ibuf, ilen);
#endif
		}

		if (dtype) {
#ifdef MD5_SUPPORT
			if (dtype == EAV_DIGEST_MD5) {
				MD5End(&md5ctx, i_md5sum);
				if (strcmp(digest, i_md5sum) != 0)
					return (EAV_ERR_DIGEST);
			}
#endif
		}
	}

	if (ctype == EAV_COMP_NONE) {
		*obufp = ibuf;
		*olenp = ilen;
	} else {
		*obufp = obuf;
		*olenp = olen;
	}
	return (EAV_SUCCESS);
}
Ejemplo n.º 13
0
int
Pass1(FILE *fd, unsigned applied)
{
    u_char *p,*q;
    MD5_CTX ctx;
    int i,j,sep,cnt;
    u_char *md5=0,*name=0,*trash=0;
    struct CTM_Syntax *sp;
    int slashwarn=0, match=0, total_matches=0;
    unsigned current;
    char md5_1[33];

    if(Verbose>3)
	printf("Pass1 -- Checking integrity of incoming CTM-patch\n");
    MD5Init (&ctx);

    GETFIELD(p,' ');				/* CTM_BEGIN */
    if(strcmp(p,"CTM_BEGIN")) {
	Fatal("Probably not a CTM-patch at all.");
	if(Verbose>3)
	    fprintf(stderr,"Expected \"CTM_BEGIN\" got \"%s\".\n",p);
	return 1;
    }

    GETFIELDCOPY(Version,' ');				/* <Version> */
    if(strcmp(Version,VERSION)) {
	Fatal("CTM-patch is wrong version.");
	if(Verbose>3)
	    fprintf(stderr,"Expected \"%s\" got \"%s\".\n",VERSION,p);
	return 1;
    }

    GETFIELDCOPY(Name,' ');				/* <Name> */
    GETFIELDCOPY(Nbr,' ');				/* <Nbr> */
    GETFIELDCOPY(TimeStamp,' ');			/* <TimeStamp> */
    GETFIELDCOPY(Prefix,'\n');				/* <Prefix> */

    sscanf(Nbr, "%u", &current);
    if (FilterList || ListIt)
	current = 0;	/* ignore if -l or if filters are present */
    if(current && current <= applied) {
	if(Verbose > 0)
	    fprintf(stderr,"Delta number %u is already applied; ignoring.\n",
		    current);
	return Exit_Version;
    }

    for(;;) {
	Delete(md5);
	Delete(name);
	Delete(trash);
	cnt = -1;
	/* if a filter list is defined we assume that all pathnames require
	   an action opposite to that requested by the first filter in the
	   list.
	   If no filter is defined, all pathnames are assumed to match. */
	match = (FilterList ? !(FilterList->Action) : CTM_FILTER_ENABLE);

	GETFIELD(p,' ');			/* CTM_something */

	if (p[0] != 'C' || p[1] != 'T' || p[2] != 'M') {
	    Fatal("Expected CTM keyword.");
	    fprintf(stderr,"Got [%s]\n",p);
	    return 1;
	}

	if(!strcmp(p+3,"_END"))
	    break;

	for(sp=Syntax;sp->Key;sp++)
	    if(!strcmp(p+3,sp->Key))
		goto found;
	Fatal("Expected CTM keyword.");
	fprintf(stderr,"Got [%s]\n",p);
	return 1;
    found:
	if(Verbose > 5)
	    fprintf(stderr,"%s ",sp->Key);
	for(i=0;(j = sp->List[i]);i++) {
	    if (sp->List[i+1] && (sp->List[i+1] & CTM_F_MASK) != CTM_F_Bytes)
		sep = ' ';
	    else
		sep = '\n';

	    if(Verbose > 5)
	        fprintf(stderr," %x(%d)",sp->List[i],sep);

	    switch (j & CTM_F_MASK) {
		case CTM_F_Name: /* XXX check for garbage and .. */
		    GETFIELDCOPY(name,sep);
		    j = strlen(name);
		    if(name[j-1] == '/' && !slashwarn)  {
			fprintf(stderr,"Warning: contains trailing slash\n");
			slashwarn++;
		    }
		    if (name[0] == '/') {
			Fatal("Absolute paths are illegal.");
			Delete(name);
			return Exit_Mess;
		    }
		    q = name;
		    for (;;) {
			if (q[0] == '.' && q[1] == '.')
			    if (q[2] == '/' || q[2] == '\0') {
				Fatal("Paths containing '..' are illegal.");
				Delete(name);
				return Exit_Mess;
			    }
			if ((q = strchr(q, '/')) == NULL)
			    break;
			q++;
		    }

		    /* if we have been asked to `keep' files then skip
		       removes; i.e. we don't match these entries at
		       all. */
		    if (KeepIt &&
			(!strcmp(sp->Key,"DR") || !strcmp(sp->Key,"FR"))) {
			match = CTM_FILTER_DISABLE;
			break;
		    }

		    /* If filter expression have been defined, match the
		       path name against the expression list.  */
		    
		    if (FilterList) {
			struct CTM_Filter *filter;

			for (filter = FilterList; filter; 
			     filter = filter->Next) {
				if (0 == regexec(&filter->CompiledRegex, name,
					0, 0, 0))
					/* if the name matches, adopt the 
					   action */
					match = filter->Action;
			}
		    }

		    /* Add up the total number of matches */
		    total_matches += match;
		    break;
		case CTM_F_Uid:
		    GETFIELD(p,sep);
		    while(*p) {
			if(!isdigit(*p)) {
			    Fatal("Non-digit in uid.");
			    return 32;
			}
			p++;
		    }
		    break;
		case CTM_F_Gid:
		    GETFIELD(p,sep);
		    while(*p) {
			if(!isdigit(*p)) {
			    Fatal("Non-digit in gid.");
			    return 32;
			}
			p++;
		    }
		    break;
		case CTM_F_Mode:
		    GETFIELD(p,sep);
		    while(*p) {
			if(!isdigit(*p)) {
			    Fatal("Non-digit in mode.");
			    return 32;
			}
			p++;
		    }
		    break;
		case CTM_F_MD5:
		    if(j & CTM_Q_MD5_Chunk) {
			GETFIELDCOPY(md5,sep);  /* XXX check for garbage */
		    } else if(j & CTM_Q_MD5_Before) {
			GETFIELD(p,sep);  /* XXX check for garbage */
		    } else if(j & CTM_Q_MD5_After) {
			GETFIELD(p,sep);  /* XXX check for garbage */
		    } else {
			fprintf(stderr,"List = 0x%x\n",j);
			Fatal("Unqualified MD5.");
			return 32;
		    }
		    break;
		case CTM_F_Count:
		    GETBYTECNT(cnt,sep);
		    break;
		case CTM_F_Bytes:
		    if(cnt < 0) WRONG
		    GETDATA(trash,cnt);
		    p = MD5Data(trash,cnt,md5_1);
		    if(md5 && strcmp(md5,p)) {
			Fatal("Internal MD5 failed.");
			return Exit_Garbage;
		default:
			fprintf(stderr,"List = 0x%x\n",j);
			Fatal("List had garbage.");
			return Exit_Garbage;
		    }
	    }
	}
	if(Verbose > 5)
	    putc('\n',stderr);
	if(ListIt && match)
	    printf("> %s %s\n", sp->Key, name);
    }

    Delete(md5);
    Delete(name);
    Delete(trash);

    q = MD5End (&ctx,md5_1);
    if(Verbose > 2)
	printf("Expecting Global MD5 <%s>\n",q);
    GETFIELD(p,'\n');			/* <MD5> */
    if(Verbose > 2)
	printf("Reference Global MD5 <%s>\n",p);
    if(strcmp(q,p)) {
	Fatal("MD5 sum doesn't match.");
	fprintf(stderr,"\tI have:<%s>\n",q);
	fprintf(stderr,"\tShould have been:<%s>\n",p);
	return Exit_Garbage;
    }
    if (-1 != getc(fd)) {
	if(!Force) {
	    Fatal("Trailing junk in CTM-file.  Can Force with -F.");
	    return 16;
	}
    }
    if ((Verbose > 1) && (0 == total_matches))
	printf("No matches in \"%s\"\n", FileName);
    return (total_matches ? Exit_OK : Exit_NoMatch);
}
Ejemplo n.º 14
0
int
PassB(FILE *fd)
{
    u_char *p,*q;
    MD5_CTX ctx;
    int i,j,sep,cnt;
    u_char *md5=0,*md5before=0,*trash=0,*name=0,*uid=0,*gid=0,*mode=0;
    struct CTM_Syntax *sp;
    FILE *b = 0;	/* backup command */
    u_char buf[BUFSIZ];
    char md5_1[33];
    int ret = 0;
    int match = 0;
    struct CTM_Filter *filter = NULL;

    if(Verbose>3)
	printf("PassB -- Backing up files which would be changed.\n");

    MD5Init (&ctx);
    snprintf(buf, sizeof(buf), fmtcheck(TarCmd, TARCMD), BackupFile);
    b=popen(buf, "w");
    if(!b) { warn("%s", buf); return Exit_Garbage; }

    GETFIELD(p,' '); if(strcmp("CTM_BEGIN",p)) WRONG
    GETFIELD(p,' '); if(strcmp(Version,p)) WRONG
    GETFIELD(p,' '); if(strcmp(Name,p)) WRONG
    GETFIELD(p,' '); if(strcmp(Nbr,p)) WRONG
    GETFIELD(p,' '); if(strcmp(TimeStamp,p)) WRONG
    GETFIELD(p,'\n'); if(strcmp(Prefix,p)) WRONG

    for(;;) {
	Delete(md5);
	Delete(uid);
	Delete(gid);
	Delete(mode);
	Delete(md5before);
	Delete(trash);
	Delete(name);
	cnt = -1;

	GETFIELD(p,' ');

	if (p[0] != 'C' || p[1] != 'T' || p[2] != 'M') WRONG

	if(!strcmp(p+3,"_END"))
	    break;

	for(sp=Syntax;sp->Key;sp++)
	    if(!strcmp(p+3,sp->Key))
		goto found;
	WRONG
    found:
	for(i=0;(j = sp->List[i]);i++) {
	    if (sp->List[i+1] && (sp->List[i+1] & CTM_F_MASK) != CTM_F_Bytes)
		sep = ' ';
	    else
		sep = '\n';

	    switch (j & CTM_F_MASK) {
		case CTM_F_Name: GETNAMECOPY(name,sep,j, Verbose); break;
		case CTM_F_Uid:  GETFIELDCOPY(uid,sep); break;
		case CTM_F_Gid:  GETFIELDCOPY(gid,sep); break;
		case CTM_F_Mode: GETFIELDCOPY(mode,sep); break;
		case CTM_F_MD5:
		    if(j & CTM_Q_MD5_Before)
			GETFIELDCOPY(md5before,sep);
		    else
			GETFIELDCOPY(md5,sep);
		    break;
		case CTM_F_Count: GETBYTECNT(cnt,sep); break;
		case CTM_F_Bytes: GETDATA(trash,cnt); break;
		default: WRONG
		}
	    }
	/* XXX This should go away.  Disallow trailing '/' */
	j = strlen(name)-1;
	if(name[j] == '/') name[j] = '\0';

	if (KeepIt && 
	    (!strcmp(sp->Key,"DR") || !strcmp(sp->Key,"FR")))
	    continue;
		
	/* match the name against the elements of the filter list.  The
	   action associated with the last matched filter determines whether
	   this file should be ignored or backed up. */
	match = (FilterList ? !(FilterList->Action) : CTM_FILTER_ENABLE);
	for (filter = FilterList; filter; filter = filter->Next) {
	    if (0 == regexec(&filter->CompiledRegex, name, 0, 0, 0))
		match = filter->Action;
	}

	if (CTM_FILTER_DISABLE == match)
		continue;

	if (!strcmp(sp->Key,"FS") || !strcmp(sp->Key,"FN") ||
	    !strcmp(sp->Key,"AS") || !strcmp(sp->Key,"DR") || 
	    !strcmp(sp->Key,"FR")) {
	    /* send name to the archiver for a backup */
	    cnt = strlen(name);
	    if (cnt != fwrite(name,1,cnt,b) || EOF == fputc('\n',b)) {
		warn("%s", name);
		pclose(b);
		WRONG;
	    }
	}
    }

    ret = pclose(b);

    Delete(md5);
    Delete(uid);
    Delete(gid);
    Delete(mode);
    Delete(md5before);
    Delete(trash);
    Delete(name);

    q = MD5End (&ctx,md5_1);
    GETFIELD(p,'\n');			/* <MD5> */
    if(strcmp(q,p)) WRONG
    if (-1 != getc(fd)) WRONG
    return ret;
}
Ejemplo n.º 15
0
int
main(int argc, char** argv)
{
	char buf[512], hash[33];
	MD5_CTX ctx;
	struct stat sb;
	struct zfsmount zfsmnt;
	dnode_phys_t dn;
#if 0
	uint64_t rootobj;
#endif
	spa_t *spa;
	off_t off;
	ssize_t n;
	int i, failures, *fd;

	zfs_init();
	if (argc == 1) {
		static char *av[] = {
			"zfsboottest",
			"/dev/gpt/system0",
			"/dev/gpt/system1",
			"-",
			"/boot/zfsloader",
			"/boot/support.4th",
			"/boot/kernel/kernel",
			NULL,
		};
		argc = sizeof(av) / sizeof(av[0]) - 1;
		argv = av;
	}
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-") == 0)
			break;
	}
	fd = malloc(sizeof(fd[0]) * (i - 1));
	if (fd == NULL)
		errx(1, "Unable to allocate memory.");
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-") == 0)
			break;
		fd[i - 1] = open(argv[i], O_RDONLY);
		if (fd[i - 1] == -1) {
			warn("open(%s) failed", argv[i]);
			continue;
		}
		if (vdev_probe(vdev_read, &fd[i - 1], NULL) != 0) {
			warnx("vdev_probe(%s) failed", argv[i]);
			close(fd[i - 1]);
		}
	}
	spa_all_status();

	spa = STAILQ_FIRST(&zfs_pools);
	if (spa == NULL) {
		fprintf(stderr, "no pools\n");
		exit(1);
	}

	if (zfs_spa_init(spa)) {
		fprintf(stderr, "can't init pool\n");
		exit(1);
	}

#if 0
	if (zfs_get_root(spa, &rootobj)) {
		fprintf(stderr, "can't get root\n");
		exit(1);
	}

	if (zfs_mount(spa, rootobj, &zfsmnt)) {
#else
	if (zfs_mount(spa, 0, &zfsmnt)) {
		fprintf(stderr, "can't mount\n");
		exit(1);
	}
#endif

	printf("\n");
	for (++i, failures = 0; i < argc; i++) {
		if (zfs_lookup(&zfsmnt, argv[i], &dn)) {
			fprintf(stderr, "%s: can't lookup\n", argv[i]);
			failures++;
			continue;
		}

		if (zfs_dnode_stat(spa, &dn, &sb)) {
			fprintf(stderr, "%s: can't stat\n", argv[i]);
			failures++;
			continue;
		}

		off = 0;
		MD5Init(&ctx);
		do {
			n = sb.st_size - off;
			n = n > sizeof(buf) ? sizeof(buf) : n;
			n = zfs_read(spa, &dn, buf, n, off);
			if (n < 0) {
				fprintf(stderr, "%s: zfs_read failed\n",
				    argv[i]);
				failures++;
				break;
			}
			MD5Update(&ctx, buf, n);
			off += n;
		} while (off < sb.st_size);
		if (off < sb.st_size)
			continue;
		MD5End(&ctx, hash);
		printf("%s %s\n", hash, argv[i]);
	}

	return (failures == 0 ? 0 : 1);
}