示例#1
0
void luax_run_script(const char* scriptname)
{
    if (chdir(path_config_get_string(PATH_CONFIG_FOLDER_SCRIPTS))==-1)
    {
        log_printf(LOG_ERROR,"Could not change director to '%s'",path_config_get_string(PATH_CONFIG_FOLDER_SCRIPTS));
        return;
    }
    log_printf(LOG_INFO,"Running script %s (please wait)",scriptname);
    run_file(LUA_STATE,scriptname);
}
示例#2
0
void log_open_file(void)
{
  time_t now = time(NULL);
  
  LOGFILE = g_fopen(path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_LOG),"w+");

  if (LOGFILE)
    fprintf(LOGFILE,"cardpeek log start: %s",ctime(&now));
  else
    fprintf(stderr,"Could not open %s for output. Proceeding without a log file.\n",path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_LOG));
}
示例#3
0
int cardmanager_search_replay_readers(cardmanager_t *cm)
{
  a_string_t* fn;
  struct dirent **namelist;
  const char* log_folder = path_config_get_string(PATH_CONFIG_FOLDER_REPLAY); 
  int count,n;

  n = scandir(log_folder,&namelist,select_clf,alphasort);

  if (n<=0)
    return 0;
  count=0;
  
  cm->readers=(char **)realloc(cm->readers,sizeof(char*)*(cm->readers_count+n));
  while (n--)
  {
    count++;
    fn = a_strnew(NULL);
    a_sprintf(fn,"replay://%s",namelist[n]->d_name);
    cm->readers[cm->readers_count++]=a_strfinalize(fn);
    free(namelist[n]);
  }
  free(namelist);
  return count;
}
static char *new_path(unsigned basepath, const char *basename, const char *extension)
{
    const char *basepath_string = path_config_get_string(basepath);
    char *n_path;
    unsigned n_path_len;

    if (basepath_string==NULL)
        return NULL;

    n_path_len = strlen(basepath_string)+1;
    if (basename)
    {
        n_path_len += strlen(basename)+1;
        if (extension)
            n_path_len += strlen(extension)+1;
    }
    n_path = g_malloc(n_path_len);

    strcpy(n_path,basepath_string);

    if (basename)
    {
        if (basepath_string[strlen(basepath_string)-1]!='/')
            strcat(n_path,"/");
        if (*basename=='/') basename++;
        strcat(n_path,basename);
        if (extension)
        {
            strcat(n_path,extension);
        }
    }

    return n_path;
}
示例#5
0
gboolean luax_config_table_save(void)
{
    FILE *cf = fopen(path_config_get_string(PATH_CONFIG_FILE_CONFIG_LUA),"w");

    if (cf==NULL)
    {
        log_printf(LOG_ERROR,"Could not create file '%s' to save configuration data.",path_config_get_string(PATH_CONFIG_FILE_CONFIG_LUA));
        return FALSE;
    }
    fprintf(cf,"%s",config_table_header);

    lua_getglobal(LUA_STATE,"cardpeek");
    if (!lua_istable(LUA_STATE,-1))
    {
        log_printf(LOG_WARNING,"Could not find 'config' table variable, no configuration data will be saved.");
        lua_pop(LUA_STATE,1);
        fprintf(cf,"-- empty --\n");
        fclose(cf);
        return FALSE;
    }

    fprintf(cf,"cardpeek = {\n");
    internal_save_table(cf,1);
    fprintf(cf,"}\n\n");
    fprintf(cf,"dofile('scripts/lib/apdu.lua')\n");
    fprintf(cf,"-- end --\n");
    fclose(cf);
    lua_pop(LUA_STATE,1); /* remove global */
    return TRUE;
}
示例#6
0
static void display_readers_and_version(void)
{
    cardmanager_t *CTX;
    unsigned i;
    unsigned reader_count;
    const char **reader_list;

    log_set_function(NULL);

    luax_init();

    fprintf(stdout,"%sThis is %s.%s\n",ANSI_GREEN,system_string_info(),ANSI_RESET);
    fprintf(stdout,"Cardpeek path is %s\n",path_config_get_string(PATH_CONFIG_FOLDER_CARDPEEK));

    CTX = cardmanager_new();
    reader_count = cardmanager_count_readers(CTX);
    reader_list  = cardmanager_reader_name_list(CTX);
    if (reader_count == 0)
        fprintf(stdout,"There are no readers detected\n");
    else if (reader_count==1)
        fprintf(stdout,"There is 1 reader detected:\n");
    else
        fprintf(stdout,"There are %i readers detected:\n",reader_count);
    for (i=0; i<reader_count; i++)
        fprintf(stdout," -> %s\n", reader_list[i]);
    fprintf(stdout,"\n");
    cardmanager_free(CTX);
    luax_release();
}
示例#7
0
int luax_init()
{
    GStatBuf st;
    const char *config_lua;
    const char *cardpeekrc_lua;

    LUA_STATE= x_lua_begin();

    luaopen_bytes(LUA_STATE);
    luaopen_asn1(LUA_STATE);
    luaopen_bit(LUA_STATE);
    luaopen_card(LUA_STATE);
    luaopen_log(LUA_STATE);
    luaopen_crypto(LUA_STATE);
    luaopen_nodes(LUA_STATE);
    luaopen_iconv(LUA_STATE);
    luaopen_ui(LUA_STATE);

    lua_newtable(LUA_STATE);
    lua_setglobal(LUA_STATE,"cardpeek");

    config_lua = path_config_get_string(PATH_CONFIG_FILE_CONFIG_LUA);
    cardpeekrc_lua = path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_RC);

    if (chdir(path_config_get_string(PATH_CONFIG_FOLDER_CARDPEEK))==-1)
    {
        log_printf(LOG_ERROR,"Could not change to directory '%s'",path_config_get_string(PATH_CONFIG_FOLDER_CARDPEEK));
        return -1;
    }

    if (g_stat(config_lua,&st)==0)
    {
        log_printf(LOG_DEBUG,"Loading configuration script %s",config_lua);
        run_file(LUA_STATE,config_lua);
    }

    if (g_stat(cardpeekrc_lua,&st)==0)
    {
        log_printf(LOG_DEBUG,"Running user configuration script %s",cardpeekrc_lua);
        run_file(LUA_STATE,cardpeekrc_lua);
    }

    return 0;
}
示例#8
0
void luax_run_command(const char* command)
{
    int error;

    if (chdir(path_config_get_string(PATH_CONFIG_FOLDER_SCRIPTS))==-1)
    {
        log_printf(LOG_ERROR,"Could not change director to '%s'",path_config_get_string(PATH_CONFIG_FOLDER_SCRIPTS));
        return;
    }

    log_printf(LOG_DEBUG,"Executing '%s'",command);

    error = luaL_loadbuffer(LUA_STATE, command, strlen(command), "command line") ||
            lua_pcall(LUA_STATE, 0, 0, 0);
    if (error)
    {
        log_printf(LOG_ERROR,"%s", lua_tostring(LUA_STATE, -1));
        lua_pop(LUA_STATE, 1);  /* pop error message from the stack */
    }
}
示例#9
0
static void save_what_can_be_saved(int sig_num)
{
    const char *logfile;
    char buf[32];

    if (write(2,message,strlen(message))<=0) return;
    logfile = path_config_get_string(PATH_CONFIG_FILE_CARDPEEK_LOG);
    if (write(2,logfile,strlen(logfile))<=0) return;
    if (write(2,signature,strlen(signature))<=0) return;
    sprintf(buf,"Received signal %i\n",sig_num);
    if (write(2,buf,strlen(buf))<=0) return;
   
    log_printf(LOG_ERROR,"Received signal %i",sig_num); 
    do_backtrace();
    log_close_file();
    exit(-2);
}
示例#10
0
static void menu_cardview_analyzer_load_cb(GtkWidget *w, gpointer user_data)
{
    char **select_info;
    UNUSED(w);
    UNUSED(user_data);

    select_info = ui_select_file("Load card script",path_config_get_string(PATH_CONFIG_FOLDER_SCRIPTS),NULL);

    if (select_info[1])
    {
        path_config_set_string(PATH_CONFIG_FOLDER_WORKING,select_info[0]);
        chdir(select_info[0]);
        ui_set_title(select_info[1]);
        luax_run_script(select_info[1]);
        g_free(select_info[0]);
        g_free(select_info[1]);
    }
}
示例#11
0
static GtkWidget *create_analyzer_menu(GtkAccelGroup *accel_group)
{
    GtkWidget *menu = gtk_menu_new();
    GtkWidget *menuitem = NULL;
    struct dirent **namelist;
    int i,n;
    const char *script_path=path_config_get_string(PATH_CONFIG_FOLDER_SCRIPTS);

    menu = gtk_menu_new();

    n = scandir(script_path, &namelist, select_lua, alphasort);
    if (n > 0)
    {
        for (i=0; i<n; i++)
        {
            log_printf(LOG_INFO,"Adding %s script to menu", namelist[i]->d_name);

            menuitem = script_info_add(script_path, namelist[i]->d_name);

            if (menuitem) gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);

            free(namelist[i]);
        }
        free(namelist);
    }
    else
        log_printf(LOG_WARNING,"No scripts found in %s",script_path);

    menuitem = gtk_separator_menu_item_new();
    gtk_widget_show(menuitem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);

    menuitem = gtk_menu_item_new_with_label("Load a script");
    /* FIXME:
        gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem),gtk_image_new_from_icon_name("document-open",GTK_ICON_SIZE_MENU));
    */
    g_signal_connect(GTK_WIDGET(menuitem),"activate",G_CALLBACK(menu_cardview_analyzer_load_cb),NULL);

    gtk_widget_add_accelerator(menuitem, "activate", accel_group, GDK_KEY_l, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

    gtk_widget_show(menuitem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),menuitem);
    return menu;
}
static void menu_readerview_save_as_cb(GtkWidget *w, gpointer user_data)
{
  char** select_info;
  a_string_t *command;
  char *filename;
  UNUSED(w);
  UNUSED(user_data);

  select_info = ui_select_file("Save recorded data",path_config_get_string(PATH_CONFIG_FOLDER_REPLAY),"card.clf");
  if (select_info[1])
  {  
    filename = luax_escape_string(select_info[1]);
    command=a_strnew(NULL);
    a_sprintf(command,"card.log_save(\"%s\")",filename);
    luax_run_command(a_strval(command));
    a_strfree(command);
    g_free(select_info[0]);
    g_free(select_info[1]);
    g_free(filename);
  }
}
示例#13
0
static void menu_cardview_save_as_cb(GtkWidget *w, gpointer user_data)
{
    char **select_info;
    a_string_t *command;
    char *filename;
    UNUSED(w);
    UNUSED(user_data);

    select_info = ui_select_file("Save xml card description",path_config_get_string(PATH_CONFIG_FOLDER_WORKING),"card.xml");
    if (select_info[1])
    {
        path_config_set_string(PATH_CONFIG_FOLDER_WORKING,select_info[0]);
        filename = luax_escape_string(select_info[1]);
        command=a_strnew(NULL);
        a_sprintf(command,"ui.save_view(\"%s\")",filename);
        luax_run_command(a_strval(command));
        a_strfree(command);
        g_free(select_info[0]);
        g_free(select_info[1]);
        g_free(filename);
    }
}
示例#14
0
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;

}
示例#15
0
static int install_dot_file(void)
{
    const char* cardpeek_dir = path_config_get_string(PATH_CONFIG_FOLDER_CARDPEEK);
    const char* old_replay_dir = path_config_get_string(PATH_CONFIG_FOLDER_OLD_REPLAY);
    const char* new_replay_dir = path_config_get_string(PATH_CONFIG_FOLDER_REPLAY);
    const char* version_file = path_config_get_string(PATH_CONFIG_FILE_VERSION);
    GStatBuf sbuf;
    FILE* f;
    int status;
    a_string_t* astr;
    unsigned dot_version=0;
    int response;
    GResource* cardpeek_resources;
    GBytes* dot_cardpeek_tar_gz;
    unsigned char *dot_cardpeek_tar_gz_start;
    gsize dot_cardpeek_tar_gz_size;

    if (g_stat(cardpeek_dir,&sbuf)==0)
    {
        log_printf(LOG_DEBUG,"Found directory '%s'",cardpeek_dir);

        if ((f = g_fopen(version_file,"r"))!=NULL)
        {
            if (fscanf(f,"%u",&dot_version)!=1)
		dot_version=0;
            fclose(f);
            if (dot_version>=SCRIPT_VERSION)
            {
                log_printf(LOG_DEBUG,"Scripts are up to date.");
                return 1;
            }
        }
        astr = a_strnew(NULL);

        if (dot_version==0 && f==NULL)
            a_sprintf(astr,"This seems to be the first time you run Cardpeek, because '%s' does not exist\n"
                      "Do you want to install the necessary files in '%s'?",version_file,cardpeek_dir);
        else
            a_sprintf(astr,"Some scripts in '%s' seem to be outdated or missing\n"
                      "Do you want to upgrade these scripts?",cardpeek_dir);

        if ((response = gui_question(a_strval(astr),"Yes","No","No, don't ask me again",NULL))!=0)
        {
            log_printf(LOG_DEBUG,"The files in '%s' will not be upgraded.",cardpeek_dir);
            a_strfree(astr);

            if (response==2)
            {
                if ((f=g_fopen(version_file,"w"))!=NULL)
                {
                    fprintf(f,"%u\n",SCRIPT_VERSION);
                    fclose(f);
                }
            }
            return 0;
        }
        a_strfree(astr);
    }
    else
    {
        astr = a_strnew(NULL);
        a_sprintf(astr,"It seems this is the first time you run Cardpeek, because \n'%s' does not exit (%s).\n"
                  "Do you want to create '%s'?",cardpeek_dir,strerror(errno),cardpeek_dir);

        if (gui_question(a_strval(astr),"Yes","No",NULL)!=0)
        {
            log_printf(LOG_DEBUG,"'%s' will not be created",cardpeek_dir);
            a_strfree(astr);

            return 0;
        }

        a_strfree(astr);

#ifndef _WIN32
        if (mkdir(cardpeek_dir,0770)!=0)
#else
        if (mkdir(cardpeek_dir)!=0)
#endif
	{
            astr = a_strnew(NULL);
            a_sprintf(astr,"'%s' could not be created: %s",cardpeek_dir,strerror(errno));
            log_printf(LOG_ERROR,a_strval(astr));
            gui_question(a_strval(astr),"OK",NULL);
            a_strfree(astr);
            return 0;
        }
    }

    if (g_stat(old_replay_dir,&sbuf)==0)
    {
        if (rename(old_replay_dir,new_replay_dir)==0)
        {
            log_printf(LOG_INFO,"Renamed %s to %s.",
                       old_replay_dir, new_replay_dir);
        }
        else
        {
            log_printf(LOG_WARNING,"Failed to rename %s to %s: %s",
                       old_replay_dir, new_replay_dir, strerror(errno));
        }
    }

    cardpeek_resources = cardpeek_resources_get_resource();
    if (cardpeek_resources == NULL)
    {
        log_printf(LOG_ERROR,"Could not load cardpeek internal resources. This is not good.");
        return -1;
    }
    dot_cardpeek_tar_gz = g_resources_lookup_data("/cardpeek/dot_cardpeek.tar.gz",G_RESOURCE_LOOKUP_FLAGS_NONE,NULL);
    if (dot_cardpeek_tar_gz == NULL)
    {
        log_printf(LOG_ERROR,"Could not load .cardpeek.tar.gz");
        return -1;
    }
    dot_cardpeek_tar_gz_start = (unsigned char *)g_bytes_get_data(dot_cardpeek_tar_gz,&dot_cardpeek_tar_gz_size);

    if (chdir(cardpeek_dir)==-1)
    {
	log_printf(LOG_ERROR,"Could not change directory to '%s'",cardpeek_dir);
	return 0;
    }

    if ((f = g_fopen("dot_cardpeek.tar.gz","wb"))==NULL)
    {
        log_printf(LOG_ERROR,"Could not create dot_cardpeek.tar.gz in %s (%s)", cardpeek_dir, strerror(errno));
        gui_question("Could not create dot_cardpeek.tar.gz, aborting.","Ok",NULL);
        return 0;
    }

    if (fwrite(dot_cardpeek_tar_gz_start,dot_cardpeek_tar_gz_size,1,f)!=1)
    {
        log_printf(LOG_ERROR,"Could not write to dot_cardpeek.tar.gz in %s (%s)", cardpeek_dir, strerror(errno));
        gui_question("Could not write to dot_cardpeek.tar.gz, aborting.","Ok",NULL);
        fclose(f);
        return 0;
    }
    log_printf(LOG_DEBUG,"Wrote %i bytes to dot_cardpeek.tar.gz",dot_cardpeek_tar_gz_size);
    fclose(f);

    g_bytes_unref(dot_cardpeek_tar_gz);

    log_printf(LOG_INFO,"Created dot_cardpeek.tar.gz");
    log_printf(LOG_INFO,"Creating files in %s", cardpeek_dir);
    status = system("tar xzvf dot_cardpeek.tar.gz");
    log_printf(LOG_INFO,"'tar xzvf dot_cardpeek.tar.gz' returned %i",status);
    if (status!=0)
    {
        gui_question("Extraction of dot_cardpeek.tar.gz failed, aborting.","Ok",NULL);
        return 0;
    }
    status = system("rm dot_cardpeek.tar.gz");
    log_printf(LOG_INFO,"'rm dot_cardpeek.tar.gz' returned %i",status);

    gui_question("Note: The files have been created.\nIt is recommended that you quit and restart cardpeek, for changes to take effect.","Ok",NULL);
    return 1;
}