int do_efuse_sha256sum(char *pSRC, int nDataLen, char *pOUT)
{
	sha256_hash_t dest;
	sha256sum(&dest, pSRC, nDataLen);
	memcpy(pOUT, dest, 32);
	return 0;	
}
Beispiel #2
0
int printSHA256(const char *fileName) {
    char *sha256 = sha256sum(fileName);
    if(sha256) {
        printf("%s  %s\n", sha256, fileName);
        return 1;
    } else {
        return 0;
    }
}
static int update_filter_files(update_t *update)
{
    unsigned char digest[SHA256_DIGEST_LENGTH];
    char* filename;
    update_item_t **prev = &(update->items);
    update_item_t *item = update->items;
    int excluded = 0;

    while (item)
    {
        filename = new_path(PATH_CONFIG_FOLDER_CARDPEEK,item->file,NULL);

        if (sha256sum(filename,digest)!=0)
        {
            if (memcmp(digest,bytestring_get_data(&item->digest),SHA256_DIGEST_LENGTH)!=0)
            {
                log_printf(LOG_INFO,"File %s needs to be updated.",filename);
                prev = &(item->next);
                item = item->next;
            }
            else
            {
                /* log_printf(LOG_DEBUG,"No update needed for %s",filename); */
                *prev = item->next;                    
                update_item_free(item);
                excluded++;
                item = *prev;
                update->item_count--;
            }
        }
        else
        {
            log_printf(LOG_INFO,"File %s is proposed for creation in current script updates.",filename);
            prev = &(item->next);
            item = item->next;
        }

        g_free(filename);
    }
    return excluded;
}
int cardpeek_update_perform(void)
{
    const char* cardpeek_update_file = path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_UPDATE);
    a_string_t *contents;
    update_t *update;
    int remove;
    update_item_t *item;
    time_t now = time(NULL);
    int updated = 0;
    char *url = NULL;
    char *local_file;
    char *local_dnld;
    unsigned char digest[SHA256_DIGEST_LENGTH];
    a_string_t *url_request;
    unsigned first_update;

    first_update = (unsigned)luax_variable_get_integer("cardpeek.updates.first_update");
    
    /* STEP 1: get cardpeek.update file */

    url=luax_variable_get_strdup("cardpeek.updates.url");

    if (url==NULL)
        url = g_strdup(DEFAULT_UPDATE_URL);

    log_printf(LOG_INFO,"Fetching '%s'",url);

    url_request = a_strnew(NULL);
    a_sprintf(url_request,"%s?u=%x&v=%s",url,first_update,VERSION);

    if (http_download(a_strval(url_request),cardpeek_update_file)==0)
    {
        g_free(url);
        return 0;
    }
    g_free(url);
    a_strfree(url_request);


    /* STEP 2: parse file */

    if ((contents=file_get_contents(cardpeek_update_file))==NULL)
    {
        log_printf(LOG_ERROR,"failed to read update file information.");
        unlink(cardpeek_update_file);
        return 0;
    }

    update = update_new();

    if ((update_load(update,a_strval(contents),a_strlen(contents)))==0)
    {
        unlink(cardpeek_update_file);
        a_strfree(contents);
        update_free(update);
        return 0;
    }
    a_strfree(contents);

    /* log_printf(LOG_DEBUG,"Updates correctly loaded from '%s'",cardpeek_update_file); */
    if ((remove = update_filter_version(update,VERSION))>0)
        log_printf(LOG_WARNING,"%d updates will not be installed because they require a newer version of Cardpeek.");
    
    remove = update_filter_files(update);

    if (update->item_count)
        log_printf(LOG_INFO,"A total of %d files will be updated, %d files are kept unchanged.",update->item_count,remove);
    else
        log_printf(LOG_INFO,"No files will be updated, %d files are kept unchanged.",remove);

    item = update->items;

    while (item)
    {
        local_dnld = new_path(PATH_CONFIG_FOLDER_CARDPEEK,item->file,".download");
        local_file = new_path(PATH_CONFIG_FOLDER_CARDPEEK,item->file,NULL);

        if (http_download(item->url,local_dnld)!=0)
        {        
            if (sha256sum(local_dnld,digest))
            {
                if (memcmp(digest,bytestring_get_data(&item->digest),SHA256_DIGEST_LENGTH)==0)
                {
                    unlink(local_file);

                    if (rename(local_dnld,local_file)==0)
                    {
                        log_printf(LOG_INFO,"Successfuly updated %s", local_file);
                        updated++;
                    }
                    else
                    {
                        log_printf(LOG_ERROR,"Failed to copy %s to %s: %s", 
                                local_dnld,
                                local_file, 
                                strerror(errno));
                    }
                }
                else
                {
                    log_printf(LOG_WARNING,"File %s was not updated: authentication failed.",local_file);
                }
            }
            unlink(local_dnld);
        }
        g_free(local_dnld);
        g_free(local_file);
        item =  item->next; 
    }

    if (updated == update->item_count)
    {    
        luax_variable_set_integer("cardpeek.updates.next_update",(int)(now+7*(24*3600)));  
        luax_config_table_save();
    }

    unlink(cardpeek_update_file);
    update_free(update);

    /* STEP 3: finish */

    return 1;

}
Beispiel #5
0
int checkSHA256(const char *sumFile) {
    FILE *fp;
    char line[1024];
    char *sha256, *storedSHA256;
    char *file = malloc(FILENAME_MAX), *filebuf = file;
    char *b;
    
    int fileok = 0, fileerr = 0, filetotal = 0, lineno = 0;
    
    if(!strncmp("-", sumFile, 1) || !strncmp("/dev/stdin", sumFile, 10)) {
		fp = stdin;
	} else {
		fp = fopen(sumFile, "r");
	}

    if(!fp) {
        fprintf(stderr, "ERROR: Cannot open sumfile: %s(%d)\n", strerror(errno), errno);
        free(filebuf);
        return 1;
    }
    
    /*fprintf(stderr, "DEBUG: Entering to reading file...\n");*/
    while(fgets(line, 1024, fp)) {
        /*fprintf(stderr, "DEBUG: line='%s'\n", line);*/
        lineno++;
        storedSHA256 = strtok(line, " ");
        if(storedSHA256 == NULL) {
            /* Maybe an empty line given */
            continue;
        }
        if(!strncmp("#", storedSHA256, 1)) {
            continue;
        }

        memset(file, 0, FILENAME_MAX);
        
        while((b = strtok(NULL, " ")) != NULL) {            
            strncat(file, " ", 2);
            strncat(file, b, strlen(b) + 1);
        }
        
        /* We remove space and asterisk from beginning of string */
        ltrim(&file, ' ');
        ltrim(&file, '*');
        
        chomp(file);
        
        if(!file || strlen(file) < 1) {
            fprintf(stderr, "WARNING: invalid formatted line given at %s:%d\n", sumFile, lineno);
            fprintf(stderr, "WARNING: but keep going...");
            continue;
        }
        
        chomp(storedSHA256);
        chop(storedSHA256, ' ');
        
        sha256 = sha256sum(file);
        filetotal++;
                
        if(!sha256 || strncmp(sha256, storedSHA256, 32)) {
            fileerr++;
            printf("%s: FAILED\n", file);
        } else {
            fileok++;
            printf("%s: OK\n", file);
        }
        free(sha256);
    }
    
    free(filebuf);
    
    if(fileerr > 0) {
        printf("WARNING: %d of %d did NOT match\n", fileerr, filetotal);
        return 1;
    } else {
        return 0;
    }
}