/* grub_fatal() on error */ static grub_err_t grub_cmd_readpcr( grub_command_t cmd __attribute__ ((unused)), int argc, char **args) { if ( argc == 0 ) { grub_fatal( "grub_cmd_readpcr: index expected" ); } if ( argc > 1 ) { grub_fatal( "grub_cmd_readpcr: Too many arguments" ); } unsigned long index = grub_strtoul( args[0], NULL, 10 ); /* if index is invalid */ if( grub_errno != GRUB_ERR_NONE ) { grub_fatal( "grub_cmd_readpcr: invalid format for index" ); } grub_uint8_t result[SHA1_DIGEST_SIZE] = { 0 }; grub_TPM_readpcr( index, &result[0] ); grub_printf( "PCR[%lu]=", index ); print_sha1( result ); grub_printf("\n"); return GRUB_ERR_NONE; }
int build_xpath(char *xpath, const char *path, int rev) { struct file_record *fr; char sha1_digest[40]; if (!rev) { /* Search on FS */ strcpy(xpath, ROOTENV->fsback); strcat(xpath, path); return 0; } if (!(fr = find_fr(path, rev))) { PHOENIXFS_DBG("build_xpath:: missing: %s@%d", path, rev); /* Might be a directory; copy and give to caller */ strcpy(xpath, path); return -1; } print_sha1(sha1_digest, fr->sha1); sprintf(xpath, "%s/.git/loose/%s", ROOTENV->fsback, sha1_digest); if (access(xpath, F_OK) < 0) { /* Try extracting from packfile */ sprintf(xpath, "%s/.git/loose", ROOTENV->fsback); if (unpack_entry(fr->sha1, xpath) < 0) return -1; else PHOENIXFS_DBG("open:: pack %s", sha1_digest); } else PHOENIXFS_DBG("open:: loose %s", sha1_digest); return 0; }
static int phoenixfs_open(const char *path, struct fuse_file_info *fi) { int rev, fd; FILE *infile, *fsfile; char fspath[PATH_MAX]; struct file_record *fr; char sha1_digest[40]; rev = parse_pathspec(xpath, path); build_xpath(fspath, xpath, 0); /* Skip zinflate for entries not in fstree */ if (!(fr = find_fr(xpath, rev))) goto END; /* Build openpath by hand */ print_sha1(sha1_digest, fr->sha1); sprintf(openpath, "%s/.git/loose/%s", ROOTENV->fsback, sha1_digest); if (access(openpath, F_OK) < 0) { /* Try extracting from packfile */ sprintf(xpath, "%s/.git/loose", ROOTENV->fsback); if (unpack_entry(fr->sha1, xpath) < 0) return -ENOENT; else PHOENIXFS_DBG("open:: pack %s", sha1_digest); } else PHOENIXFS_DBG("open:: loose %s", sha1_digest); /* zinflate openpath onto fspath */ PHOENIXFS_DBG("open:: zinflate %s onto %s", sha1_digest, fspath); if (!(infile = fopen(openpath, "rb")) || !(fsfile = fopen(fspath, "wb+"))) return -errno; if (zinflate(infile, fsfile) != Z_OK) PHOENIXFS_DBG("open:: zinflate issue"); fclose(infile); fclose(fsfile); END: if ((fd = open(fspath, fi->flags)) < 0) return -errno; fi->fh = fd; return 0; }
int main(int argc, char* argv[]) { int sock_fd = 0; struct sockaddr_in serv_addr; if(argc < 3) { fprintf(stderr, "%s\n", "No specified address"); exit(EXIT_FAILURE); } if( sock_create( &serv_addr, argv[1], argv[2], &sock_fd) == 1) { fprintf(stderr, "%s\n", "Connection failed"); return 1; } //connection established memset(server_reply, 0, BUFFER_SIZE); if (read(sock_fd, server_reply, BUFFER_SIZE) < 0 ) { fprintf(stderr, "%s\n", strerror(errno)); return 1; } printf("%s\n", server_reply); while(1) { unsigned char hash[SHA_DIGEST_LENGTH] = {0}; if( sha1_of_a_file(sock_fd, hash) == 1) { return 1; } print_sha1(hash); } close(sock_fd); return 0; }
int main(int argc, char* argv[]) { FILE *infile; unsigned char sha1[20]; struct stat st; if (argc < 2) usage(argv[0]); if (!(infile = fopen(argv[1], "rb"))) die("Could not open %s", argv[1]); if (stat(argv[1], &st) < 0) die("Could not stat %s", argv[1]); if (sha1_file(infile, st.st_size, sha1) < 0) die("SHA1 failed"); fclose(infile); print_sha1(NULL, sha1); printf(" %s\n", argv[1]); return 0; }
static int phoenixfs_release(const char *path, struct fuse_file_info *fi) { struct file_record *fr; FILE *infile, *outfile; struct stat st; unsigned char sha1[20]; char sha1_digest[40]; char outfilename[40]; char inpath[PATH_MAX]; char outpath[PATH_MAX]; int rev, ret; pthread_mutex_lock(&phoenixfs_mutexlock); /* Don't recursively backup history */ if ((rev = parse_pathspec(xpath, path))) { PHOENIXFS_DBG("release:: history: %s", path); /* Inflate the original version back onto the filesystem */ if (!(fr = find_fr(xpath, 0))) { PHOENIXFS_DBG("release:: Can't find revision 0!"); pthread_mutex_unlock(&phoenixfs_mutexlock); return 0; } print_sha1(sha1_digest, fr->sha1); sprintf(inpath, "%s/.git/loose/%s", ROOTENV->fsback, sha1_digest); build_xpath(outpath, xpath, 0); if (!(infile = fopen(inpath, "rb")) || !(outfile = fopen(outpath, "wb+"))) { pthread_mutex_unlock(&phoenixfs_mutexlock); return -errno; } PHOENIXFS_DBG("release:: history: zinflate %s onto %s", sha1_digest, outpath); rewind(infile); rewind(outfile); if (zinflate(infile, outfile) != Z_OK) PHOENIXFS_DBG("release:: zinflate issue"); fflush(outfile); fclose(infile); fclose(outfile); if (close(fi->fh) < 0) { PHOENIXFS_DBG("release:: can't really close"); pthread_mutex_unlock(&phoenixfs_mutexlock); return -errno; } pthread_mutex_unlock(&phoenixfs_mutexlock); return 0; } /* Attempt to create a backup */ build_xpath(xpath, path, 0); if (!(infile = fopen(xpath, "rb")) || (lstat(xpath, &st) < 0)) { pthread_mutex_unlock(&phoenixfs_mutexlock); return -errno; } if ((ret = sha1_file(infile, st.st_size, sha1)) < 0) { fclose(infile); pthread_mutex_unlock(&phoenixfs_mutexlock); return ret; } print_sha1(outfilename, sha1); sprintf(outpath, "%s/.git/loose/%s", ROOTENV->fsback, outfilename); if (!access(outpath, F_OK)) { /* SHA1 match; don't overwrite file as an optimization */ PHOENIXFS_DBG("release:: not overwriting: %s", outpath); goto END; } if (!(outfile = fopen(outpath, "wb"))) { fclose(infile); pthread_mutex_unlock(&phoenixfs_mutexlock); return -errno; } /* Rewind and seek back */ rewind(infile); PHOENIXFS_DBG("release:: zdeflate %s onto %s", xpath, outfilename); if (zdeflate(infile, outfile, -1) != Z_OK) PHOENIXFS_DBG("release:: zdeflate issue"); mark_for_packing(sha1, st.st_size); fclose(outfile); END: fclose(infile); if (close(fi->fh) < 0) { PHOENIXFS_DBG("release:: can't really close"); return -errno; } /* Update the fstree */ fstree_insert_update_file(path, NULL); pthread_mutex_unlock(&phoenixfs_mutexlock); return 0; }
/* grub_fatal() on error */ static void grub_TPM_read_tcglog( const unsigned long index ) { grub_uint32_t returnCode, featureFlags, eventLog = 0, logAddr = 0, edi = 0; grub_uint8_t major, minor; /* get event log pointer */ grub_TPM_int1A_statusCheck( &returnCode, &major, &minor, &featureFlags, &eventLog, &edi ); /* edi = 0 means event log is empty */ if( edi == 0 ) { grub_fatal( "Event log is empty" ); } logAddr = eventLog; TCG_PCClientPCREvent *event = NULL; /* index = 0: print all entries */ if ( index == 0 ) { /* eventLog = absolute pointer to the beginning of the event log. */ event = (TCG_PCClientPCREvent *) logAddr; /* If there is exactly one entry */ if( edi == eventLog ) { grub_printf( "pcrIndex: %u \n", event->pcrIndex ); grub_printf( "eventType: %u \n", event->eventType ); grub_printf( "digest: " ); print_sha1( event->digest ); grub_printf( "\n\n" ); } else { /* If there is more than one entry */ do { grub_printf( "pcrIndex: %u \n", event->pcrIndex ); grub_printf( "eventType: %u \n", event->eventType ); grub_printf( "digest: " ); print_sha1( event->digest ); grub_printf( "\n\n" ); logAddr += TCG_PCR_EVENT_SIZE + event->eventDataSize; event = (TCG_PCClientPCREvent *)logAddr; } while( logAddr != edi ); /* print the last one */ grub_printf( "pcrIndex: %u \n", event->pcrIndex ); grub_printf( "eventType: %u \n", event->eventType ); grub_printf( "digest: " ); print_sha1( event->digest ); grub_printf( "\n\n" ); } } else { /* print specific entry */ logAddr = eventLog; unsigned long i; for( i = 1; i < index; i++ ) { event = (TCG_PCClientPCREvent *)logAddr; logAddr += TCG_PCR_EVENT_SIZE + event->eventDataSize; if( logAddr > edi ) { /* index not valid. */ grub_fatal( "No entry at specified index" ); } } event = (TCG_PCClientPCREvent *)logAddr; grub_printf( "pcrIndex: %u \n", event->pcrIndex ); grub_printf( "eventType: %u \n", event->eventType ); grub_printf( "digest: " ); print_sha1( event->digest ); grub_printf( "\n\n" ); } }