Exemplo n.º 1
0
int main (int argc, char *argv[]) {

  char * filename;

  if (argc < 4 || argc < 3 || argc < 2 || argc < 1)
    show_usage(argv[0], TRUE, -1);

  filename = argv[1];

  if (file_exists(filename) != 1) {
    file_write(filename, "{ }");
  }

  char * cNewValue = argv[argc-1];
  json_object * jobj  = json_object_from_file(filename);
  json_object * jobj2 = jobj;
  json_object * jLast;
  json_object * jobj3;

  int i;
  char *key;

  //skip the first argument and the last argument
  for(i = 2; i < argc-1; i++) {
    key = argv[i];
    jLast = jobj2;

    if (i == argc-2) {
      jobj3 = json_object_new_string(argv[argc-1]);
      json_object_get(jobj3);
      json_object_object_add(jLast, key, (jobj3));
      json_object_put(jobj3);
      break;
    }

    //this gets an existing field, if it exists, otherwise creates a new one.
    if (json_object_object_get_ex(jLast, key, &jobj2) != TRUE) {
      jobj2 = json_object_new_object();
    }

    json_object_object_add(jLast, key, json_object_get(jobj2));
  }

  json_object_to_file(filename, jobj);

  exit(0);
}
Exemplo n.º 2
0
int main (int argc, char *argv[]) {

  if (argc < 3 || argc < 2 || argc < 1) {
    show_usage(argv[0], TRUE, -1);
  }

  if (file_exists(argv[1]) != 1) {
    show_message("File not found.", TRUE, -2);
  }

  const char* cfilename   = argv[1];
  const char* cfieldname  = argv[2];

  json_object* jobj       = json_object_from_file(cfilename);
  json_object* jlast;

  int i;
  jlast = jobj;
  for(i = 2; i < argc; i++) {
    //printf("arg %d = %s \n", i, argv[i]);

    //argument is referencing an array item by index
    if (json_object_is_array(jlast) && str_is_integer(argv[i])) {
      jlast = json_object_array_get_idx(jlast, atol(argv[i]));
      continue;
    }

    if (json_object_has_item(jlast, argv[i])) {
      jlast = get_object_item(jlast, argv[i]);
    } else {
      jlast = NULL;
    }
  }

  //item was not found
  if (jlast == NULL) {
    printf("%s", "");
    exit(1);
  }

  //print string value or JSON representation of the object
  printf("%s", json_object_get_string(jlast));

  exit(0);
}
Exemplo n.º 3
0
json_object *pam_multipass_read_hashes(const char *user)
{
    static const char *hashes_filename = "/.multipass/hashes.json";
    json_object *hashes = NULL;

    int home_len = 0;
    struct passwd *pw = getpwnam(user);
    if (pw != NULL && pw->pw_dir != NULL) {
	home_len = strlen(pw->pw_dir);
    }

    if (home_len == 0) {
	/* fprintf(stderr, "pam_multipass: user has no home directory:
	   %s\n", user); */
	return NULL;
    }

    char *hashes_file = malloc(home_len + strlen(hashes_filename) + 1);
    if (hashes_file == NULL) return NULL;

    memcpy(hashes_file, pw->pw_dir, home_len);
    memcpy(hashes_file + home_len, hashes_filename, strlen(hashes_filename) + 1);

    hashes = json_object_from_file((char *)hashes_file);

    if (hashes == NULL) {
	fprintf(stderr, "pam_multipass: error parsing %s\n", hashes_file);
	goto out;
    }

    if (hashes == error_ptr(-1)) { /* unable to read */
	hashes = NULL;
	goto out;
    }

    if (!json_object_is_type(hashes, json_type_object)) {
	fprintf(stderr, "pam_multipass: %s must be a JSON object like \"{}\"\n", hashes_file);
	hashes = NULL;
	goto out;
    }

 out:
    free(hashes_file);
    return hashes;
}
Exemplo n.º 4
0
int nf_json_config_get_root(char *configfile, const char *key, json_object **root, json_object **root_config)
{
    json_object *json_root;

    if((key == NULL) || (root == NULL) ||(root_config == NULL))
        return -1;

    json_root = json_object_from_file(configfile);
    if(json_root == NULL)
        return -1;

    *root_config = json_object_object_get(json_root, key);
    if(*root_config == NULL)
        return -1;

    *root = json_root;
    return 0;
}
Exemplo n.º 5
0
static struct json_script_file *
handle_file(struct json_script_ctx *ctx, const char *filename)
{
	struct json *obj;

	obj = json_object_from_file(filename);
	if (!obj) {
		fprintf(stderr, "load JSON data from %s failed.\n", filename);
		return NULL;
	}

	blob_buf_init(&b_script, 0);
	blobmsg_add_json_element(&b_script, "", obj);
	json_object_put(obj);

	return json_script_file_from_blobmsg(filename,
		blob_data(b_script.head), blob_len(b_script.head));
}
Exemplo n.º 6
0
static int init_nc_arg(nc_arg_t *nc_arg)
{
    nc_arg->func = NULL;
    json_object *jfile = json_object_from_file("config.json");
    json_object *jip = json_util_get(jfile,"CONFIG.sqlstore-address");
    if(!jip)
	return -1;
    const char *ip = json_object_get_string(jip);
    snprintf(nc_arg->ip,sizeof(nc_arg->ip),ip);

    json_object *jport = json_util_get(jfile,"CONFIG.sqlstore-port");
    if(!jport)
	return -1;
    int port = json_object_get_int(jport);
    nc_arg->port = port;
    nc_arg->timeout = 3;
    json_object_put(jfile);
    return 0;
}
Exemplo n.º 7
0
int checkUserCredentials(const char* login, const char* password){

    json_object *jvalue, *user, *plik;
    
    //parsowanie zawartosci pliku User.json
    plik = json_object_from_file("../resources/Users.json");
    
    //wydobywanie tablicy uzytkownikow
    jvalue = json_object_object_get(plik, "user");
    
    int arraylen = json_object_array_length(jvalue);
    int i;
    //petla po wszystkich uzytkownikach
    for (i=0; i< arraylen; i++){
        user = json_object_array_get_idx(jvalue, i);
        
        /*For po kluczach kazdego uzytkownika*/
        json_object_object_foreach(user, key, val){ 
            const char* value = json_object_get_string(val);

            if( strcmp(key, "login") == 0 && strcmp(value, login) != 0 )
                break;

            if( strcmp(key, "haslo") == 0 ) {
                if ( !(strcmp(value, password) == 0) )
                    return -1;
            }
            
            if ( strcmp(key, "rola") == 0 ){
                return json_object_get_int(val);
            }

        }
    }
    return -1;
    
}
Exemplo n.º 8
0
symbols_t* drakvuf_get_symbols_from_rekall(const char *rekall_profile)
{

    symbols_t *ret = g_malloc0(sizeof(symbols_t));;
    json_object *root = json_object_from_file(rekall_profile);
    if(!root) {
        fprintf(stderr, "Rekall profile couldn't be opened!\n");
        goto err_exit;
    }

    json_object *functions = NULL;
    if (!json_object_object_get_ex(root, "$FUNCTIONS", &functions)) {
        PRINT_DEBUG("Rekall profile: no $FUNCTIONS section found\n");
        goto err_exit;
    }

    ret->count = json_object_object_length(functions);
    ret->symbols = g_malloc0(sizeof(symbols_t) * ret->count);

    struct json_object_iterator it = json_object_iter_begin(functions);
    struct json_object_iterator itEnd = json_object_iter_end(functions);
    uint32_t i=0;

    while (!json_object_iter_equal(&it, &itEnd) && i < ret->count) {
        ret->symbols[i].name = g_strdup(json_object_iter_peek_name(&it));
        ret->symbols[i].rva = json_object_get_int64(json_object_iter_peek_value(&it));
        i++;
        json_object_iter_next(&it);
    }

    json_object_put(functions);

    return ret;

    err_exit: free(ret);
    return NULL;
}
Exemplo n.º 9
0
/*
    Restore a session
    Threads must be setup and spawned manually after that
*/
hash_stat session_restore(char *sessionname)
{
#ifdef HAVE_JSON_JSON_H

    FILE *sesfile;
    char sesname[1024];
    char readline[512];
    char username[HASHFILE_MAX_LINE_LENGTH];
    char hash[HASHFILE_MAX_LINE_LENGTH];
    char salt[HASHFILE_MAX_LINE_LENGTH];
    char salt2[HASHFILE_MAX_LINE_LENGTH];
    uid_t uid;
    struct passwd *pwd;
    char fname[255];
    int flag = 0;
    int cnt;
    json_object *jobj;

    pwd = getpwuid(getuid());
    snprintf(sesname,1024,"%s/.hashkill/sessions/%s.session", pwd->pw_dir, sessionname);

    /* Parse the <session>..</session> info */
    sesfile = fopen(sesname, "r");
    if (!sesfile)
    {
        elog("Cannot open session file : %s\n",sesname);
        return hash_err;
    }
    hlog("Restoring session from: %s\n",sesname);
    fclose(sesfile);
    
    root_node = json_object_from_file(sesname);
    main_header_node = json_object_object_get(root_node,"main");
    set_current_plugin(json_object_get_string(json_object_object_get(main_header_node,"plugin")));
    if (load_plugin() == hash_err) exit(EXIT_FAILURE);
    attack_method  = json_object_get_int(json_object_object_get(main_header_node,"attacktype"));

    additional_options = malloc(strlen(json_object_get_string(json_object_object_get(main_header_node,"addopts")))+1);
    strcpy(additional_options,json_object_get_string(json_object_object_get(main_header_node,"addopts")));
    process_addopts(json_object_get_string(json_object_object_get(main_header_node,"addopts")));

    out_cracked_file=malloc(strlen(json_object_get_string(json_object_object_get(main_header_node,"outcrackedfile")))+1);
    out_uncracked_file=malloc(strlen(json_object_get_string(json_object_object_get(main_header_node,"outuncrackedfile")))+1);

    if (strlen(json_object_get_string(json_object_object_get(main_header_node,"outcrackedfile")))>1) strcpy(out_cracked_file,json_object_get_string(json_object_object_get(main_header_node,"outcrackedfile")));
    else
    {
	free(out_cracked_file);
	out_cracked_file=NULL;
    }
    if (strlen(json_object_get_string(json_object_object_get(main_header_node,"outuncrackedfile")))>1) strcpy(out_uncracked_file,json_object_get_string(json_object_object_get(main_header_node,"outuncrackedfile")));
    else
    {
	free(out_uncracked_file);
	out_uncracked_file=NULL;
    }
    
    strcpy(readline,json_object_get_string(json_object_object_get(main_header_node,"workdir")));
    if (chdir(readline) != 0)
    {
	elog("Cannot set working directory to %s\n",readline);
	return hash_err;
    }
    else
    {
	hlog("Change to working directory: %s\n",readline);
    }
    hash_ret_len = json_object_get_int(json_object_object_get(main_header_node,"hashlen"));
    ocl_gpu_platform = json_object_get_int(json_object_object_get(main_header_node,"gpuplatform"));
    strcpy(hashlist_file, json_object_get_string(json_object_object_get(main_header_node,"hashlistfile")));
    if (hash_plugin_is_special()) load_hashes_file(hashlist_file);
    switch (json_object_get_int(json_object_object_get(main_header_node,"attacktype")))
    {
	case attack_method_simple_bruteforce:
	    attack_header_node = json_object_object_get(root_node,"bruteforce");
	    bruteforce_start=json_object_get_int(json_object_object_get(attack_header_node,"start"));
	    bruteforce_end=json_object_get_int(json_object_object_get(attack_header_node,"end"));
	    strcpy(bruteforce_charset, json_object_get_string(json_object_object_get(attack_header_node,"charset")));
	    attack_current_count = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"currentcount")));
	    attack_overall_count = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"overallcount")));
	    break;

	case attack_method_markov:
	    attack_header_node = json_object_object_get(root_node,"markov");
	    strcpy(markov_statfile,json_object_get_string(json_object_object_get(attack_header_node,"statfile")));
	    markov_threshold = json_object_get_int(json_object_object_get(attack_header_node,"threshold"));
	    markov_max_len = json_object_get_int(json_object_object_get(attack_header_node,"maxlen"));
	    attack_current_count = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"currentcount")));
	    attack_overall_count = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"overallcount")));
	    break;

	case attack_method_rule:
	    attack_header_node = json_object_object_get(root_node,"rule");
	    rule_file=malloc(strlen(json_object_get_string(json_object_object_get(attack_header_node,"rulefile")))+1);
	    strcpy(rule_file,json_object_get_string(json_object_object_get(attack_header_node,"rulefile")));
	    rule_current_elem = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"currentelem")));
	    rule_overall_elem = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"overall")));
	    attack_current_count = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"currentcount")));
	    attack_overall_count = (uint64_t)atoll(json_object_get_string(json_object_object_get(attack_header_node,"overallcount")));
	    
	    break;

	default:
	    elog("Unknown attack type%s!\n","");
	    return hash_err;
	    break;
    }

    if (hash_plugin_is_special() == 0)
    {
	hash_list_node = json_object_object_get(root_node,"hashlist");
	flag = json_object_array_length(hash_list_node);
	for (cnt=0;cnt<flag;cnt++)
	{
	    jobj = json_object_array_get_idx(hash_list_node, cnt);
	    strcpy(username, json_object_get_string(json_object_object_get(jobj,"username")));
	    strcpy(readline, json_object_get_string(json_object_object_get(jobj,"hash")));
	    hex2str(hash, readline, hash_ret_len*2);
	    strcpy(salt, json_object_get_string(json_object_object_get(jobj,"salt")));
	    if (add_hash_list(username, hash, salt, "") == hash_err) 
	    {
		wlog("Wrong session file hashlist (corrupted session file?)%s\n","");
		exit(EXIT_FAILURE);
	    }
	}
	if (flag<1) load_hashes_file(hashlist_file);
	cracked_list_node = json_object_object_get(root_node,"crackedlist");
	if (cracked_list_node) flag = json_object_array_length(cracked_list_node);
	if (cracked_list_node) 
	for (cnt=0;cnt<flag;cnt++)
	{
	    jobj = json_object_array_get_idx(cracked_list_node, cnt);

	    strcpy(username, json_object_get_string(json_object_object_get(jobj,"username")));
	    strcpy(readline, json_object_get_string(json_object_object_get(jobj,"hash")));
	    hex2str(hash,readline,hash_ret_len*2);
	    strcpy(salt, json_object_get_string(json_object_object_get(jobj,"salt")));
	    if (add_hash_list(username, hash, salt, "") == hash_err) 
	    {
		wlog("Wrong session file hashlist (corrupted session file?)%s\n","");
		exit(EXIT_FAILURE);
	    }
	    strcpy(readline, json_object_get_string(json_object_object_get(jobj,"salt2")));
	    strcpy(salt2, readline);
	    if (add_cracked_list(username, hash, salt, salt2) == hash_err) 
	    {
		wlog("Wrong session file hashlist (corrupted session file?)%s\n","");
		exit(EXIT_FAILURE);
	    }
	}
    }


    cpuonly=1;
    if (strstr(sessionname, "-gpu"))
    {
	cpuonly=0;
        /* rename session */
        uid = getuid();
        if (!pwd)
        {
            elog("Cannot get username for uid %d (%s)\n", (int)uid, strerror(errno));
            return hash_err;
        }

        snprintf(fname, 255, "%s/.hashkill/sessions/%s.session", pwd->pw_dir, sessionname);
        unlink(fname);
        hlog("Session %s will be renamed to %d-gpu\n", sessionname, getpid());
        session_restore_flag = 1;
        endpwent();
        /* Load scheduler data */
	session_load_scheduler_parameters();
        return hash_ok;
    }

    /* Load scheduler data */
    session_load_scheduler_parameters();


    /* rename session */
    uid = getuid();
    pwd = getpwuid(uid);
    if (!pwd)
    {
        elog("Cannot get username for uid %d (%s)\n", (int)uid, strerror(errno));
        return hash_err;
    }
    snprintf(fname, 255, "%s/.hashkill/sessions/%s.session", pwd->pw_dir, sessionname);
    unlink(fname);
    hlog("Session %s will be renamed to %d\n", sessionname,  getpid());
    session_restore_flag = 1;
    endpwent();
    return hash_ok;
#else
    wlog("This build does not support sessions. Please reconfigure with --with-json and rebuild%s\n","");
    return hash_err;
#endif

}
Exemplo n.º 10
0
/* Print to stdout detailed session summary */
hash_stat print_session_detailed(char *sessionname)
{
#ifdef HAVE_JSON_JSON_H
    FILE *sesfile;
    struct passwd *pwd;
    char sesname[1024];
    int cnt=0;
    char username[HASHFILE_MAX_LINE_LENGTH];
    char hash[HASHFILE_MAX_LINE_LENGTH];
    char rawhash[HASHFILE_MAX_LINE_LENGTH];
    char rawhash2[HASHFILE_MAX_LINE_LENGTH*2];
    char plugin_used[HASHFILE_MAX_LINE_LENGTH*2];
    char salt[HASHFILE_MAX_LINE_LENGTH];
    int flag = 0;
    json_object *jobj;
    
    printf("\nDetailed information about session: %s\n"
	    "-----------------------------------------------\n",sessionname);
    pwd = getpwuid(getuid());
    snprintf(sesname, 1024, "%s/.hashkill/sessions/%s.session", pwd->pw_dir, sessionname);
    /* Parse the <session>..</session> info */
    sesfile = fopen(sesname, "r");
    if (!sesfile)
    {
        elog("Cannot open session file : %s\n",sesname);
        return hash_err;
    }
    printf("Session file: \t%s\n",sesname);

    fclose(sesfile);
    root_node = json_object_from_file(sesname);
    main_header_node = json_object_object_get(root_node,"main");
    switch (json_object_get_int(json_object_object_get(main_header_node,"attacktype")))
    {
	case attack_method_simple_bruteforce:
	    printf("Attack type: \tBruteforce\n");
	    break;
	case attack_method_markov:
	    printf("Attack type: \tMarkov\n");
	    break;
	case attack_method_rule:
	    printf("Attack type: \tRule-based\n");
	    break;
	default:
	    printf("Attack type: \tUNKNOWN!\n");
	    break;
    }
    attack_method=json_object_get_int(json_object_object_get(main_header_node,"attacktype"));
    printf("Plugin: \t%s\n",json_object_get_string(json_object_object_get(main_header_node,"plugin")));
    printf("Progress: \t%d%%\n",json_object_get_int(json_object_object_get(main_header_node,"progress")));
    printf("Session ends: \t%s",json_object_get_string(json_object_object_get(main_header_node,"timestamp")));
    printf("Hashlist file: \t%s\n",json_object_get_string(json_object_object_get(main_header_node,"hashlistfile")));
    printf("Command line: \t%s\n",json_object_get_string(json_object_object_get(main_header_node,"commandline")));

    switch (attack_method)
    {
	case attack_method_simple_bruteforce:
	    attack_header_node = json_object_object_get(root_node,"bruteforce");
	    printf("\nBruteforce attack parameters:\n"
	             "-----------------------------\n");
	    printf("Start length: \t%d\n",json_object_get_int(json_object_object_get(attack_header_node,"start")));
	    printf("End length: \t%d\n",json_object_get_int(json_object_object_get(attack_header_node,"end")));
	    printf("Charset: \t%s\n",json_object_get_string(json_object_object_get(attack_header_node,"charset")));
	    printf("Current str: \t%s\n",json_object_get_string(json_object_object_get(attack_header_node,"currentstr")));
	break;
	case attack_method_markov:
	    attack_header_node = json_object_object_get(root_node,"markov");
	    printf("\nMarkov attack parameters:\n"
	             "-------------------------\n");
	    printf("Statfile: \t%s\n",json_object_get_string(json_object_object_get(attack_header_node,"statfile")));
	    printf("Threshold: \t%d\n",json_object_get_int(json_object_object_get(attack_header_node,"threshold")));
	    printf("End length: \t%d\n",json_object_get_int(json_object_object_get(attack_header_node,"maxlen")));
	    printf("Current str: \t%s\n",json_object_get_string(json_object_object_get(attack_header_node,"currentstr")));
	break;
	case attack_method_rule:
	    attack_header_node = json_object_object_get(root_node,"rule");
	    printf("\nRule attack parameters:\n"
	             "-----------------------\n");
	    printf("Rule file: \t%s\n",json_object_get_string(json_object_object_get(attack_header_node,"rulefile")));
	break;

    }
    printf("\nHashes list (username:hash:salt):\n---------------------------------\n");
    hash_list_node = json_object_object_get(root_node,"hashlist");
    flag = json_object_array_length(hash_list_node);
    for (cnt=0;cnt<flag;cnt++)
    {
	jobj = json_object_array_get_idx(hash_list_node, cnt);
	strcpy(username, json_object_get_string(json_object_object_get(jobj,"username")));
	strcpy(hash, json_object_get_string(json_object_object_get(jobj,"hash")));
	strcpy(salt, json_object_get_string(json_object_object_get(jobj,"salt")));
	// This is idiotic I know
	if ((strncmp(plugin_used,"md5unix",8)==0) || (strncmp(plugin_used,"sha512unix",10)==0) ||(strncmp(plugin_used,"phpbb3",6)==0) || (strncmp(plugin_used,"wordpress",9)==0) || (strncmp(plugin_used,"apr1",4)==0))
	{
	    b64_pton(hash,(unsigned char *)rawhash,512);
	    printf("%s:%s%s\n", username, salt, rawhash);
	}
	else if (  (strncmp(plugin_used,"desunix",7)==0)
		 || (strncmp(plugin_used,"ldap-sha",8)==0) || (strncmp(plugin_used,"ldap-ssha",9)==0))
	{
	    b64_pton(hash,(unsigned char *)rawhash,512);
	    printf("%s:%s:%s\n", username, rawhash, salt);
	}
	else
	{
	    str2hex(rawhash,rawhash2, (strlen(hash)*100)/147);
	    printf("%s:%s:%s\n", username, rawhash2, salt);
	}
    }
    cracked_list_node = json_object_object_get(root_node,"crackedlist");
    if (cracked_list_node)
    {
	cnt = json_object_array_length(cracked_list_node);
    }
    else
    {
	cnt=0;
    }
    printf("\n%d passwords cracked.\n",cnt);
    printf("\n");
    hlog("Session %s dumped successfully\n\n", sessionname);
    return hash_ok;
#else
    wlog("This build does not support sessions. Please reconfigure with --with-json and rebuild%s\n","");
    return hash_err;
#endif
}
Exemplo n.º 11
0
/* Print to stdout sessions summary */
hash_stat print_sessions_summary(void)
{
#ifdef HAVE_JSON_JSON_H

    DIR *dir;
    FILE *sesfile;
    struct passwd *pwd;
    struct dirent *dentry;
    char sesname[1024];
    int progress;
    char plugin[256];
    char gmtime[256];
    char shortsesname[256];
    int cnt=0;
    
    char sessiondir[255];
    pwd = getpwuid(getuid());
    snprintf(sessiondir, 255, "%s/.hashkill/sessions", pwd->pw_dir);
    
    dir=opendir(sessiondir);
    if (!dir)
    {
        elog("Cannot open sessions dir: %s", sessiondir);
        return hash_err;
    }
    hlog("Sessions list: %s\n\n","");
    printf("Session name: \t\tSession ends at: \t\tSession type: \tProgress: \tPlugin: \n"
	    "-----------------------------------------------------------------------------------------------\n");
    do
    {
        errno = 0;
        if ((dentry = readdir(dir)) != NULL)
        {
            if ((dentry->d_type == DT_REG) && (strstr(dentry->d_name, ".session")))
            {
                pwd = getpwuid(getuid());
                snprintf(sesname,1024,"%s/%s", sessiondir, dentry->d_name);
                /* Parse the <session>..</session> info */
                sesfile = fopen(sesname, "r");
                if (!sesfile)
                {
            	    goto next;
            	}
            	fclose(sesfile);
            	root_node = json_object_from_file(sesname);
            	if (!root_node) goto next;
		main_header_node = json_object_object_get(root_node,"main");
		attack_method=json_object_get_int(json_object_object_get(main_header_node,"attacktype"));
		strcpy(plugin,json_object_get_string(json_object_object_get(main_header_node,"plugin")));
		progress = json_object_get_int(json_object_object_get(main_header_node,"progress"));
		strcpy(gmtime,json_object_get_string(json_object_object_get(main_header_node,"timestamp")));
		gmtime[strlen(gmtime)-1] = 0;
        	cnt = 0;
        	while (dentry->d_name[cnt] != '.')
        	{
        	    shortsesname[cnt] = dentry->d_name[cnt];
        	    cnt++;
        	}
        	shortsesname[cnt] = 0;
		/* Print out details */
		printf("\033[1;33m%s\033[1;0m              \t",shortsesname);
		printf("%s \t",gmtime);
		switch (attack_method)
		{
		    case attack_method_simple_bruteforce: printf("Bruteforce \t");break;
		    case attack_method_markov: printf("Markov \t\t");break;
		    case attack_method_rule: printf("Rule-based \t");break;
		    default: printf("Unknown \t");break;
                }
                if (progress > 100) printf("Unknown \t");
                else printf("%d%% \t\t",progress);
                printf("%s ",plugin);
                printf("\n");

                next:
    		usleep(10);
            }
        }
    } while (dentry != NULL);
    closedir(dir);
    printf("\n");
    return hash_ok;
#else
    wlog("This build does not support sessions. Please reconfigure with --with-json and rebuild%s\n","");
    return hash_err;
#endif
}
Exemplo n.º 12
0
void read_topo_cfg(struct topo_cfg_file *p_cfg, char *name)
{
        const char *str_val = NULL;
        struct json_object *tmp = NULL;
        struct json_object *obj = NULL;
        struct json_object *cfg = json_object_from_file( name );
        if (cfg == NULL) {
                goto fail;
        }
	/*node*/
	if (json_object_object_get_ex(cfg, "node_data", &tmp)) {
        } else goto fail;
	if (json_object_object_get_ex(tmp, "port", &obj)) {
                p_cfg->node_port = (short)json_object_get_int(obj);
        } else goto fail;

	if (json_object_object_get_ex(tmp, "host", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->node_host = x_strdup(str_val);
        } else goto fail;
        
	if (json_object_object_get_ex(tmp, "username", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->node_username = x_strdup(str_val);
        } else goto fail;

	if (json_object_object_get_ex(tmp, "password", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->node_password = x_strdup(str_val);
        } else goto fail;

	if (json_object_object_get_ex(tmp, "database", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->node_database = x_strdup(str_val);
        } else goto fail;
	/*line*/
	if (json_object_object_get_ex(cfg, "line_data", &tmp)) {
        } else goto fail;
	if (json_object_object_get_ex(tmp, "port", &obj)) {
                p_cfg->line_port = (short)json_object_get_int(obj);
        } else goto fail;

	if (json_object_object_get_ex(tmp, "host", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->line_host = x_strdup(str_val);
        } else goto fail;
        
	if (json_object_object_get_ex(tmp, "username", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->line_username = x_strdup(str_val);
        } else goto fail;

	if (json_object_object_get_ex(tmp, "password", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->line_password = x_strdup(str_val);
        } else goto fail;

	if (json_object_object_get_ex(tmp, "database", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->line_database = x_strdup(str_val);
        } else goto fail;

	return;
fail:
	fprintf(stderr, "invalid topo config file :%s\n", name);
	exit(0);
}
Exemplo n.º 13
0
int openproject (char *file)
{
	json_object		*prj = NULL;
	json_object		*srcDir = NULL;
	char			name[201] = {0};
	int				nSrcDirs = 0;
	int				loop = 0;
	const char		*folder;
	char			*srcPath = NULL;
	const char		*pPrjName = NULL;

	prj = json_object_from_file(file);
	if (!prj) {
		return -1;
	}

	loop = strlen(file);
	while (file[loop] != '\\') {
		loop--;
	}
	if (loop >=0 ) {
		file[loop] = '\0';
	}

	/* Check for project folder */
	if (!directoryExists(file)) {
		return -1;
	}

	pPrjName = json_object_get_string(json_object_object_get(prj, NAME));
	while (*pPrjName) {
		if (*pPrjName == '\\') {
			folder = pPrjName;
		}
		pPrjName++;
	}
	pPrjName = ++folder;
	folder = NULL;

	/* Get the source path */
	nSrcDirs = json_object_get_int(json_object_object_get(prj, NOFSRCDIRS));
	srcDir = json_object_object_get (prj, SRCDIRS);

	for (loop = 0; loop < 1 /*nSrcDirs*/; loop++) {
		folder = json_object_get_string(json_object_array_get_idx(srcDir, loop));
	}

	/* Check for source folder */
	if (!directoryExists(folder)) {
		return -1;
	}


	generateBatchFile(pPrjName, file, folder);
	buildCscope(file);

	sprintf(name, "%s\\%s.conf", file, pPrjName);

	myinit (name);

	json_object_put(prj);

	return 1;
}
Exemplo n.º 14
0
/*******************************************************************
int memfrs_load_structs( const char* gvar_filename)

Load the global variable information into g_globalvar_info.

INPUT:     const char* gvar_filename,  the filename of json global variable database
OUTPUT:    int,                        return 0 if sucess, and not 0 otherwise

*******************************************************************/
int memfrs_load_globalvar( const char* gvar_filename)
{
    g_globalvar_info = json_object_from_file(gvar_filename);
    return 0;
}
Exemplo n.º 15
0
status_t rekall_lookup(
        const char *rekall_profile,
        const char *symbol,
        const char *subsymbol,
        addr_t *rva,
        addr_t *size)
{
    status_t ret = VMI_FAILURE;
    addr_t mask = 0;
    if(!rekall_profile || !symbol) {
        return ret;
    }

    json_object *root = json_object_from_file(rekall_profile);
    if(!root) {
        fprintf(stderr, "Rekall profile '%s' couldn't be opened!\n", rekall_profile);
        return ret;
    }

    if(!subsymbol && !size) {
        json_object *constants = NULL, *jsymbol = NULL;
        if (!json_object_object_get_ex(root, "$CONSTANTS", &constants)) {
            PRINT_DEBUG("Rekall profile: no $CONSTANTS section found\n");
            goto exit;
        }

        if (!json_object_object_get_ex(constants, symbol, &jsymbol)){
            PRINT_DEBUG("Rekall profile: symbol '%s' not found\n", symbol);
            goto exit;
        }

        *rva = json_object_get_int64(jsymbol);

        ret = VMI_SUCCESS;

        json_object_put(jsymbol);
        json_object_put(constants);
    } else {
        json_object *structs = NULL, *jstruct = NULL, *jstruct2 = NULL, *jmember = NULL, *jvalue = NULL;
        if (!json_object_object_get_ex(root, "$STRUCTS", &structs)) {
            PRINT_DEBUG("Rekall profile: no $STRUCTS section found\n");
            goto exit;
        }
        if (!json_object_object_get_ex(structs, symbol, &jstruct)) {
            PRINT_DEBUG("Rekall profile: no '%s' found\n", symbol);
            json_object_put(structs);
            goto exit;
        }

        if (size) {
            json_object *jsize = json_object_array_get_idx(jstruct, 0);
            *size = json_object_get_int64(jsize);
            json_object_put(jsize);
            json_object_put(structs);

            ret = VMI_SUCCESS;
            goto exit;
        }

        jstruct2 = json_object_array_get_idx(jstruct, 1);
        if (!jstruct2) {
            PRINT_DEBUG("Rekall profile: struct '%s' has no second element\n", symbol);
            json_object_put(jstruct);
            json_object_put(structs);
            goto exit;
        }

        if (!json_object_object_get_ex(jstruct2, subsymbol, &jmember)) {
            PRINT_DEBUG("Rekall profile: '%s' has no '%s' member\n", symbol, subsymbol);
            json_object_put(jstruct2);
            json_object_put(jstruct);
            json_object_put(structs);
            goto exit;
        }

        jvalue = json_object_array_get_idx(jmember, 0);
        if (!jvalue) {
            PRINT_DEBUG("Rekall profile: '%s'.'%s' has no RVA defined\n", symbol, subsymbol);
            json_object_put(jmember);
            json_object_put(jstruct2);
            json_object_put(jstruct);
            json_object_put(structs);
            goto exit;
        }

        *rva = json_object_get_int64(jvalue);

        ret = VMI_SUCCESS;

        json_object_put(jmember);
        json_object_put(jstruct2);
        json_object_put(jstruct);
        json_object_put(structs);
    }

exit:
    json_object_put(root);
    return ret;
}
Exemplo n.º 16
0
int get_status_cf(void)
{
	char url[BUFSIZE];
	sprintf(url, "http://codeforces.com/api/contest.status?contestId=%d&from=1&count=1&handle=%s",
			solution->problem_info.origin_id / 10, vjudge_user);

	if (DEBUG) {
		write_log("perform url is %s.\n", url);
	}

	// 设置提交地址
	curl_easy_setopt(curl, CURLOPT_URL, url);

	char filename[BUFSIZE];
	sprintf(filename, "%dstatus.txt", solution->solution_id);

	time_t begin_time = time(NULL);
	int rid = 0;
	int usedtime = 0;
	int memory = 0;
	char result[BUFSIZE];
	while (1) {
		if (time(NULL) - begin_time > vj_max_wait_time) {
			write_log("judge time out.\n");
			return OJ_JE;
		}

		perform_curl(filename);

		json_object *obj = json_object_from_file(filename);
		if (obj == NULL) {
			write_log("load json object from file %s error.\n", filename);
			return OJ_JE;
		}
		json_object *submission;
		if (strcmp(json_get_str(obj, "status"), "OK") != 0) {
			json_object_put(obj);
			return OJ_JE;
		} else {
			submission = json_get_obj(obj, "result[0]");
		}

		if (submission == NULL) {
			write_log("get status error, maybe not submit.\n");
			json_object_put(obj);
			return OJ_JE;
		}

		rid = json_get_int(submission, "id");
		strcpy(result, json_get_str(submission, "verdict"));
		usedtime = json_get_int(submission, "timeConsumedMillis");
		memory = json_get_int(submission, "memoryConsumedBytes");
		memory = memory / 1024 + ((memory % 1024) ? 1: 0);

		write_log("rid = %d\n", rid);
		write_log("usedtime = %d\n", usedtime);
		write_log("memory = %d\n", memory);
		write_log("result = %s\n", result);
		if (is_final_result(result)) {
			solution->remote_rid = rid;
			solution->time = usedtime;
			solution->memory = memory;
			int ret = convert_result(result);
			if (ret == OJ_RE) {
				strcpy(solution->runtimeinfo, result);
			}
			write_log("get solution %d status over"
					": %d.\n", solution->solution_id, ret);
			json_object_put(obj);
			return ret;
		}
	}

	return OJ_JE;
}
Exemplo n.º 17
0
int main(int argc, char **argv)
{
	struct json_object *json;
	struct array_list *tests;
	struct lh_entry *entry;
	char *key;
	struct json_object *val;
	int i;
	context ctx;

	if (argc != 2) {
		printf("Usage: %s <filename>\n", argv[0]);
		return 1;
	}

	json = json_object_from_file(argv[1]);
	assert(!is_error(json));

	assert(strcmp((char *) ((json_object_get_object(json)->head)->k),
			"tests") == 0);

	/* Get array of tests */
	tests = json_object_get_array((struct json_object *)
			(json_object_get_object(json)->head)->v);

	for (i = 0; i < array_list_length(tests); i++) {
		/* Get test */
		struct json_object *test =
			(struct json_object *) array_list_get_idx(tests, i);

		ctx.last_start_tag = NULL;
		ctx.content_model = NULL;
		ctx.process_cdata = false;

		/* Extract settings */
		for (entry = json_object_get_object(test)->head; entry;
				entry = entry->next) {
			key = (char *) entry->k;
			val = (struct json_object *) entry->v;

			if (strcmp(key, "description") == 0) {
				printf("Test: %s\n",
					json_object_get_string(val));
			} else if (strcmp(key, "input") == 0) {
				ctx.input = (const uint8_t *)
					json_object_get_string(val);
				ctx.input_len =	json_object_get_string_len(val);
			} else if (strcmp(key, "output") == 0) {
				ctx.output = json_object_get_array(val);
				ctx.output_index = 0;
				ctx.char_off = 0;
			} else if (strcmp(key, "lastStartTag") == 0) {
				ctx.last_start_tag = (const char *)
						json_object_get_string(val);
			} else if (strcmp(key, "contentModelFlags") == 0) {
				ctx.content_model =
						json_object_get_array(val);
			} else if (strcmp(key, "processCDATA") == 0) {
				ctx.process_cdata =
					json_object_get_boolean(val);
			}
		}

		/* And run the test */
		run_test(&ctx);
	}

	json_object_put(json);

	printf("PASS\n");

	return 0;
}
Exemplo n.º 18
0
void read_cfg(struct cfg_info *p_cfg, char *cfg_file)
{
    const char *str_val = NULL;
    struct json_object *obj = NULL, *tmp_obj = NULL;
    struct json_object *cfg = json_object_from_file(cfg_file);
    int array_len = 0;
    if (cfg == NULL) {
        goto fail;
    }

    /* node type. */
    if (json_object_object_get_ex(cfg, "node_type", &obj)) {
        str_val = json_object_get_string(obj);
        if (strcmp(str_val, "SINGLE") == 0) {
            p_cfg->node_type = SINGLE;
        } else if (strcmp(str_val, "CLUSTER") == 0) {
            p_cfg->node_type = CLUSTER;
        } else {
            log_out("config: node_type error");
            goto fail;
        }
    } else {
        log_out("config: node_type not found");
        goto fail;
    }

    /* mode. */
    if (json_object_object_get_ex(cfg, "mode", &obj)) {
        str_val = json_object_get_string(obj);
        p_cfg->mode = x_strdup(str_val);
    } else {
        log_out("config: mode not found");
        goto fail;
    }
    if (strcmp(p_cfg->mode, "RO") == 0 ) {
        p_cfg->ldb_readonly_switch = 1;
    } else if (strcmp(p_cfg->mode, "RW") == 0) {
        p_cfg->ldb_readonly_switch = 0;
    } else {
        log_out("config: mode error");
        goto fail;
    }

    /* data set id. */
    if (json_object_object_get_ex(cfg, "ds_id", &obj)) {
        p_cfg->ds_id = json_object_get_int(obj);
    } else {
        log_out("config: ds_id not found");
        goto fail;
    }

    /* key set 2 */
    if (json_object_object_get_ex(cfg, "key_set", &obj)) {
        if (json_object_array_length(obj) != 2) {
            log_out("config: key_set2 error");
            goto fail;
        }

        tmp_obj = json_object_array_get_idx(obj, 0);
        p_cfg->key_start = json_object_get_int(tmp_obj);
        tmp_obj = json_object_array_get_idx(obj, 1);
        p_cfg->key_end = json_object_get_int(tmp_obj);
    } else {
        log_out("config: key_set not found");
        goto fail;
    }

    /* time range. */
    if (json_object_object_get_ex(cfg, "time_range", &obj)) {
        array_len = json_object_array_length(obj);
        if (array_len != 2) {
            log_out("config: time_range error");
            goto fail;
        }

        tmp_obj = json_object_array_get_idx(obj, 0);
        p_cfg->start_time = json_object_get_int64(tmp_obj);

        tmp_obj = json_object_array_get_idx(obj, 1);
        p_cfg->end_time = json_object_get_int64(tmp_obj);
    } else {
        log_out("config: time_range not found");
        goto fail;
    }
    if (p_cfg->ldb_readonly_switch == 1) {
        if (p_cfg->start_time > p_cfg->end_time) {
            log_out("config: start_time > end_time");
            goto fail;
        }
    } else if (p_cfg->ldb_readonly_switch == 0) {
        if (p_cfg->end_time != -1 ) {
            log_out("config: end_time error");
            goto fail;
        }
    }

    /* zookeeper server. */
    if (json_object_object_get_ex(cfg, "zk_server", &obj)) {
        str_val = json_object_get_string(obj);
        p_cfg->zk_server = x_strdup(str_val);
    } else {
        log_out("config: zk_server not found");
        goto fail;
    }

    /* IP address. */
    if (json_object_object_get_ex(cfg, "ip", &obj)) {
        str_val = json_object_get_string(obj);
        p_cfg->ip = x_strdup(str_val);
    } else {
        log_out("config: ip not found");
        goto fail;
    }

    /* write and read port. */
    if (json_object_object_get_ex(cfg, "w_port", &obj)) {
        p_cfg->w_port = json_object_get_int(obj);
    } else {
        log_out("config: w_port not found");
        goto fail;
    }
    if (json_object_object_get_ex(cfg, "r_port", &obj)) {
        p_cfg->r_port = json_object_get_int(obj);
    } else {
        log_out("config: r_port not found");
        goto fail;
    }

    /* max connect count. */
    if (json_object_object_get_ex(cfg, "max_connect", &obj)) {
        p_cfg->max_connect = json_object_get_int64(obj);
    } else {
        log_out("config: max_connect not found");
        goto fail;
    }
    if (json_object_object_get_ex(cfg, "num_threads", &obj)) {
        p_cfg->num_threads = json_object_get_int(obj);
    } else {
        log_out("config: num_threads not found");
        goto fail;
    }

    /* workspace directory. */
    if (json_object_object_get_ex(cfg, "work_path", &obj)) {
        str_val = json_object_get_string(obj);
        p_cfg->work_path = x_strdup(str_val);
    } else {
        log_out("config: work_path not found");
        goto fail;
    }

    /* log configure. */
    if (json_object_object_get_ex(cfg, "log_path", &obj)) {
        str_val = json_object_get_string(obj);
        p_cfg->log_path = x_strdup(str_val);
    } else {
        log_out("config: log_path not found");
        goto fail;
    }
    if (json_object_object_get_ex(cfg, "log_file", &obj)) {
        str_val = json_object_get_string(obj);
        p_cfg->log_file = x_strdup(str_val);
    } else {
        log_out("config: log_file not found");
        goto fail;
    }
    if (json_object_object_get_ex(cfg, "log_level", &obj)) {
        p_cfg->log_verbosity = json_object_get_int(obj);
    } else {
        log_out("config: log_level not found");
        goto fail;
    }

    /* ldb configure. */
    if (json_object_object_get_ex(cfg, "ldb_write_buffer_size", &obj)) {
        p_cfg->ldb_write_buffer_size = json_object_get_int64(obj);
    } else {
        log_out("config: ldb_write_buffer_size not found");
        goto fail;
    }

    if (json_object_object_get_ex(cfg, "ldb_block_size", &obj)) {
        p_cfg->ldb_block_size = json_object_get_int64(obj);
    } else {
        log_out("config: ldb_block_size not found");
        goto fail;
    }

    if (json_object_object_get_ex(cfg, "ldb_cache_lru_size", &obj)) {
        p_cfg->ldb_cache_lru_size = json_object_get_int64(obj);
    } else {
        log_out("config: ldb_cache_lru_size not found");
        goto fail;
    }

    if (json_object_object_get_ex(cfg, "ldb_bloom_key_size", &obj)) {
        p_cfg->ldb_bloom_key_size = json_object_get_int(obj);
    } else {
        log_out("config: ldb_bloom_key_size not found");
        goto fail;
    }

    if (json_object_object_get_ex(cfg, "ldb_compaction_speed", &obj)) {
        p_cfg->ldb_compaction_speed = json_object_get_int(obj);
    } else {
        log_out("config: ldb_compaction_speed not found");
        goto fail;
    }

    /* slave. */
    if (json_object_object_get_ex(cfg, "has_slave", &obj)) {
        p_cfg->has_slave = json_object_get_int(obj);
    } else {
        log_out("config: has_slave not found");
        goto fail;
    }
    if (p_cfg->has_slave != 0) {
        if (json_object_object_get_ex(cfg, "role", &obj)) {
            str_val = json_object_get_string(obj);
            if (strcmp(str_val, "MASTER") == 0) {
                p_cfg->role = MASTER;
            } else if (strcmp(str_val, "SLAVE") == 0) {
                p_cfg->role = SLAVE;
            } else {
                log_out("config: role config error");
                goto fail;
            }
        } else {
            log_out("config: role not found");
            goto fail;
        }

        if (json_object_object_get_ex(cfg, "slave_ip", &obj)) {
            str_val = json_object_get_string(obj);
            p_cfg->slave_ip = x_strdup(str_val);
        } else {
            log_out("config: slave_ip not found");
            goto fail;
        }
        if (json_object_object_get_ex(cfg, "slave_wport", &obj)) {
            p_cfg->slave_wport = json_object_get_int(obj);
        } else {
            log_out("config: slave_wport not found");
            goto fail;
        }

        log_debug("slave ip:[%s]", p_cfg->slave_ip);
        log_debug("slave wport:[%d]", p_cfg->slave_wport);
    }

    return;
fail:
    log_out("invalid config file :%s", cfg_file);
    exit(EXIT_FAILURE);
}
Exemplo n.º 19
0
/*获得Access_Token
 * 我曾经把自己模拟成浏览器手动post用户名密码,开始挺好使的
 * 后来不行了……因为登录次数多了居然要输验证码了!!!
 */
int gettoken()
{
    char code[200];
    char buff[1024];
    char ATfile[1024];
    json_object *json_get;

    sprintf(ATfile,"%s/Access_Token",confpath);
    if (!(json_get = json_object_from_file(ATfile))) {                     //如果当前目录下面没有.Access_Token文件,那么重新获取
        fprintf(stderr, "You have to login first!\n");

        if (fork() == 0) {
            snprintf(buff, sizeof(buff) - 1,
                     "x-www-browser "                                               //什么?你说没有x-www-browser?那可不关我的事,你自己搞定吧
                     "-new-tab "
                     "\"http://openapi.baidu.com/oauth/2.0/authorize?"
                     "response_type=code&"
                     "client_id=%s&"
                     "redirect_uri=oob&"
                     "scope=netdisk&"
                     "display=page\""
                     , ak);
            system(buff);
            exit(errno);
        } else {
            puts("please copy the authorization code from the browser:");
            scanf("%199s", code);
            sprintf(buff,
                    "https://openapi.baidu.com/oauth/2.0/token?"
                    "grant_type=authorization_code&"
                    "code=%s&"
                    "client_id=%s&"
                    "client_secret=%s&"
                    "redirect_uri=oob"
                    , code, ak, sk);
            FILE *tpfile = tmpfile();

            if (!tpfile) {
                int lasterrno = errno;
                errorlog("create temp file error:%s\n", strerror(errno));
                return -lasterrno;
            }

            Http *r = Httpinit(buff);

            if (r == NULL) {
                int lasterrno = errno;
                errorlog("can't resolve domain:%s\n", strerror(errno));
                fclose(tpfile);
                return -lasterrno;
            }

            r->method = get;
            r->writefunc = savetofile;
            r->writeprame = tpfile;

            if ((errno = request(r)) != CURLE_OK) {
                errorlog("network error:%d\n", errno);
                fclose(tpfile);
                Httpdestroy(r);
                return -EPROTO;
            }

            Httpdestroy(r);
            json_get = json_object_from_FILE(tpfile);
            fclose(tpfile);

            if (json_get == NULL) {
                errorlog("json_object_from_FILE filed!\n");
                return -EPROTO;
            }

            json_object *jerror_code;
            if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
                int errorno = json_object_get_int(jerror_code) ;
                json_object_put(json_get);
                return handleerror(errorno);
            }

            json_object *jaccess_token;
            if (json_object_object_get_ex(json_get, "access_token",&jaccess_token)) {        //找到access_token了,存到文件里面
                strcpy(Access_Token, json_object_get_string(jaccess_token));
                json_object_to_file(ATfile, json_get);
                json_object_put(json_get);
            } else {
                puts("Authorization error!");
                remove(ATfile);
                json_object_put(json_get);
                errno = EPERM;
                return -errno;
            }
        }

    } else {            //如果文件里面存在,直接读取文件,当然,这里没有考虑有不怀好意的人修改我的文件的情况
        json_object *jaccess_token;
        json_object_object_get_ex(json_get, "access_token",&jaccess_token);
        strcpy(Access_Token, json_object_get_string(jaccess_token));
        json_object_put(json_get);
    }

    return 0;
}
Exemplo n.º 20
0
Arquivo: etvnet.c Projeto: serge-v/ctv
static void
get_activation_code(char **user_code, char **device_code)
{
	char url[1000];
	char fname[PATH_MAX];
	int rc;
	json_object *root;
	const char *v;

	/* create .cache dir */

	snprintf(fname, PATH_MAX-1, "%s/.cache", getenv("HOME"));
	if (!exists(fname)) {
		rc = mkdir(fname, 0700);
		sprintf(last_error, "cannot create dir %s. Error: %d", fname, rc);
		provider->error_number = 1;
		return;
	}

	snprintf(fname, PATH_MAX-1, "%s/.cache/etvcc", getenv("HOME"));
	if (!exists(fname)) {
		rc = mkdir(fname, 0700);
		sprintf(last_error, "cannot create dir %s. Error: %d", fname, rc);
		provider->error_number = 1;
		return;
	}

	strcpy(url, code_url);
	strcat(url, "?client_id=");
	strcat(url, client_id);
	strcat(url, "&client_secret=");
	strcat(url, client_secret);
	strcat(url, "&scope=");
	strcat(url, scope_encoded);

	snprintf(fname, PATH_MAX-1, "%s/.cache/etvcc/activation.json", getenv("HOME"));
	rc = fetch(url, fname);
	if (rc != 0) {
		sprintf(last_error, "cannot get activation code. Error: %d", rc);
		provider->error_number = 1;
		return;
	}

	root = json_object_from_file(fname);
	if (root == NULL) {
		sprintf(last_error, "cannot load %s", fname);
		provider->error_number = 1;
		return;
	}

	v = get_str(root, "device_code");
	if (v == NULL ) {
		sprintf(last_error, "no activation.device_code.");
		provider->error_number = 1;
		return;
	}

	*device_code = strdup(v);

	v = get_str(root, "user_code");
	if (v == NULL ) {
		sprintf(last_error, "no activation.user_code.");
		provider->error_number = 1;
		return;
	}

	provider->error_number = 0;
	*user_code = strdup(v);
}
Exemplo n.º 21
0
int main()
{
    //daemonize();
    //process signal
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = handler;
    sigaction(SIGINT, &sa, NULL);

    json_object *jfile = json_object_from_file("config.json");
    if(!jobject_ptr_isvalid(jfile))
    {
        printf("open config.json failed!\n");
        return -1;
    }
    json_object *jlog = json_util_get(jfile,"CONFIG.log");
    if(!jlog)   return -1;
    const char *strlog = json_object_get_string(jlog);

    log_open(&log_,strlog);

    json_object *jlog_level = json_util_get(jfile,"CONFIG.log_level");
    if(jlog_level)
    {
        int log_level = json_object_get_int(jlog_level);
        log_level_set(log_,log_level);
    }

    loga(log_,"starting server!");

    json_object_put(jfile);

    int rv;
    rv = segword_init();
    if(rv < 0)
    {
        loga(log_,"segword_init failed!");
        return -1;
    }

    redis_init();

    rv = cm_start();
    if(rv < 0)
	return -1;

    struct timeval delay;
    while(!stop_daemon)
    {
        delay.tv_sec = 0;
        delay.tv_usec = 100000;

        int rv = select(0,NULL,NULL,NULL,&delay); 
        if(rv == 0)
        {
            continue;
        }
    }
    cm_stop();
    cm_destroy();
    redis_fini();
    segword_fini();

    printf("Normal exit!\n");
    return 0;
}
Exemplo n.º 22
0
int refreshtoken()
{
    char buff[1024];
    char ATfile[1024];
    char Refresh_Token[100];
    json_object *json_get;

    sprintf(ATfile,"%s/Access_Token",confpath);
    if (!(json_get = json_object_from_file(ATfile))) {            //如果当前目录下面没有.Access_Token文件,那么直接调用gettoken
        return gettoken();
    } else {
        
        json_object *jrefresh_token;
        json_object_object_get_ex(json_get, "refresh_token",&jrefresh_token);
        strcpy(Refresh_Token, json_object_get_string(jrefresh_token));
        json_object_put(json_get);
        snprintf(buff, sizeof(buff) - 1,
                 "https://openapi.baidu.com/oauth/2.0/token?"
                 "grant_type=refresh_token&"
                 "refresh_token=%s&"
                 "client_id=%s&"
                 "client_secret=%s", Refresh_Token, ak, sk);
        FILE *tpfile = tmpfile();

        if (!tpfile) {
            int lasterrno = errno;
            errorlog("create temp file error:%s\n", strerror(errno));
            return -lasterrno;
        }

        Http *r = Httpinit(buff);

        if (r == NULL) {
            int lasterrno = errno;
            errorlog("can't resolve domain:%s\n", strerror(errno));
            fclose(tpfile);
            return -lasterrno;
        }

        r->method = get;
        r->writefunc = savetofile;
        r->writeprame = tpfile;

        if ((errno = request(r)) != CURLE_OK) {
            errorlog("network error:%d\n", errno);
            fclose(tpfile);
            Httpdestroy(r);
            return -EPROTO;
        }

        Httpdestroy(r);
        json_get = json_object_from_FILE(tpfile);
        fclose(tpfile);

        if (json_get == NULL) {
            errorlog("json_object_from_FILE filed!\n");
            return -EPROTO;
        }
        
        json_object *jerror_code;
        if (json_object_object_get_ex(json_get, "error_code",&jerror_code)) {
            int errorno = json_object_get_int(jerror_code) ;
            json_object_put(json_get);
            return handleerror(errorno);
        }

        json_object *jaccess_token;
        if (json_object_object_get_ex(json_get, "access_token",&jaccess_token)) {              
            //找到access_token了,存到文件里面
            strcpy(Access_Token, json_object_get_string(jaccess_token));
            json_object_to_file(ATfile, json_get);
            json_object_put(json_get);
        } else {
            puts("Authorization error!");
            json_object_put(json_get);
            errno = EPERM;
            return -errno;
        }
    }
    return 0;
}
Exemplo n.º 23
0
/*btn3_short_press*/
void btn3_short_press(void)
{
        int res = json_type_null;
        json_object * pjson_obj_read = NULL;
        struct mpd_status *status = NULL;

        ncurrt_time = 0;
        system("killall aplay");
        system("killall xfchat");

        pthread_rwlock_wrlock(&json_rwlock_voice);
        pjson_obj_read = json_object_from_file(PLAY_VOICE_JSON_PATH);
        pthread_rwlock_unlock(&json_rwlock_voice);
        res = json_object_get_type(pjson_obj_read);
        printf("json_type: %u \n", json_object_get_type(pjson_obj_read));
//        printf("json_length: %u \n", json_object_array_length(pjson_obj_read));

        json_object_put(pjson_obj_read);
        if ((json_type_array == res) && (0 < json_object_array_length(pjson_obj_read)) && (!bplay_audio))
        {
                printf("nd btn3_short_press ret: %d, bplay_audio: %d\n", res, bplay_audio);
                my_mpd_run_pause();
                bplay_audio = true;
        }
        else
        {
                int quere_len = 0;
                int online_size = 0;
                bplay_audio = false;

                online_size = get_file_size(ONLINE_LIST_PATH);
                if (online_size > 0)
                {
                        quere_len = get_mpc_quere_len(conn);

                        pthread_mutex_lock(&mutex);
                        if (quere_len > 0)
                        {
                                mpd_run_stop(conn);
                                mpd_run_clear(conn);
                        }

                        bool mpc_load = mpd_run_load(conn, "online.lst");
                        bool mpc_list_clear = mpd_run_playlist_clear(conn, "online.lst");
                        printf("nd btn3_short_press mpc_load: %d, mpc_list_clear: %d\n", mpc_load, mpc_list_clear);
                        pthread_mutex_unlock(&mutex);

                        res = i2c_smbus_write_byte_data(i2c_file, 0x00, BLUE_OFF);
                        if (res < 0)
                        {
                                printf("nd btn3_short_press i2c_smbus_write_byte_data BLUE_OFF failed, ret: %d\n", res);
                        }
                }

                quere_len = get_mpc_quere_len(conn);
                if (quere_len > 0)
                {
                        printf("nd btn3_short_press get_mpc_quere_len quere_len: %d\n", quere_len);
                        pthread_mutex_lock(&mutex);
                        status = mpd_run_status(conn);
                        if (!status)
                        {
                                printf("nd btn3_short_press mpd_run_status2 %s \n", mpd_connection_get_error_message(conn));
                        }
                        else
                        {
                                if (mpd_status_get_state(status) == MPD_STATE_PLAY)
                                {
                                        printf("nd btn3_short_press mpd_status_get_state MPD_STATE_PLAY\n");
                                        mpd_run_pause(conn, true);
                                }
                                else
                                {
                                        printf("nd btn3_short_press mpd_status_get_state other state\n");
                                        mpd_run_play(conn);
                                }

                                mpd_status_free(status);
                        }
                        pthread_mutex_unlock(&mutex);
                }
  }
}
Exemplo n.º 24
0
/*
 * 函 数:load_cfg_file
 * 功 能:加载配置文件
 * 参 数:pcfg用来保存配置文件中的内容, name 配置文件
 * 返回值: 
 * 修 改:
 */
void load_cfg_file(struct smart_cfg_file *p_cfg, char *name)
{
        const char *str_val = NULL;
        struct json_object *obj = NULL;
        struct json_object *cfg = json_object_from_file( name );
        if (cfg == NULL) {
                goto fail;
        }

	if (json_object_object_get_ex(cfg, "service_port", &obj)) {
                p_cfg->srv_port = (short)json_object_get_int(obj);
        } else goto fail;
        
	if (json_object_object_get_ex(cfg, "hander_counts", &obj)) {
                p_cfg->hander_counts = (short)json_object_get_int(obj);
        } else goto fail;

        if (json_object_object_get_ex(cfg, "worker_counts", &obj)) {
                p_cfg->worker_counts = (short)json_object_get_int(obj);
        } else goto fail;
        if(json_object_object_get_ex(cfg, "monitor_times", &obj)){
            p_cfg->monitor_times = (short)json_object_get_int(obj);
        }else goto fail;

#ifdef OPEN_CORO
	if (json_object_object_get_ex(cfg, "tasker_counts", &obj)) {
                p_cfg->tasker_counts = (short)json_object_get_int(obj);
        } else goto fail;
#endif
	if (json_object_object_get_ex(cfg, "max_req_size", &obj)) {
                p_cfg->max_req_size = json_object_get_int(obj);
        } else goto fail;



	if (json_object_object_get_ex(cfg, "log_path", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->log_path = x_strdup(str_val);
        } else goto fail;
        
	if (json_object_object_get_ex(cfg, "log_file", &obj)) {
                str_val = json_object_get_string(obj);
                p_cfg->log_file = x_strdup(str_val);
        } else goto fail;

        if (json_object_object_get_ex(cfg, "log_level", &obj)) {
                p_cfg->log_level = json_object_get_int(obj);
        } else goto fail;

#ifdef USE_HTTP_PROTOCOL
	if (json_object_object_get_ex(cfg, "api_apply", &obj)) {
		p_cfg->api_counts ++;
                str_val = json_object_get_string(obj);
                p_cfg->api_apply = x_strdup(str_val);
        }

	if (json_object_object_get_ex(cfg, "api_fetch", &obj)) {
		p_cfg->api_counts ++;
                str_val = json_object_get_string(obj);
                p_cfg->api_fetch = x_strdup(str_val);
        }
        
        if (json_object_object_get_ex(cfg, "api_merge", &obj)) {
		p_cfg->api_counts ++;
                str_val = json_object_get_string(obj);
                p_cfg->api_merge = x_strdup(str_val);
        }

	if (json_object_object_get_ex(cfg, "api_custom", &obj)) {
		int add = json_object_array_length(obj);
		p_cfg->api_counts += add;
		assert( p_cfg->api_counts <= MAX_API_COUNTS );

		int i = 0;
		struct json_object* itr_obj = NULL;
		memset( p_cfg->api_names, 0, MAX_API_COUNTS*(MAX_API_NAME_LEN + 1) );
		for(i=0; i < add; i++){
			itr_obj = json_object_array_get_idx(obj, i);
			str_val = json_object_get_string( itr_obj );
			assert( strlen(str_val) <= MAX_API_NAME_LEN );
			strncpy( &p_cfg->api_names[i][0], str_val, MIN(strlen(str_val), MAX_API_NAME_LEN) );
		}
	}
#endif
	return;
fail:
	x_printf(D, "invalid config file :%s\n", name);
	exit(EXIT_FAILURE);
}
Exemplo n.º 25
0
status_t
rekall_profile_symbol_to_rva(
    const char *rekall_profile,
    const char *symbol,
    const char *subsymbol,
    addr_t *rva)
{
    status_t ret = VMI_FAILURE;
    if (!rekall_profile || !symbol) {
        return ret;
    }

    json_object *root = json_object_from_file(rekall_profile);
    if (!root) {
        errprint("Rekall profile couldn't be opened!\n");
        return ret;
    }

    if (!subsymbol) {
        json_object *constants = NULL, *functions = NULL, *jsymbol = NULL;
        if (json_object_object_get_ex(root, "$CONSTANTS", &constants)) {
            if (json_object_object_get_ex(constants, symbol, &jsymbol)) {
                *rva = json_object_get_int64(jsymbol);

                ret = VMI_SUCCESS;
                goto exit;
            } else {
                dbprint(VMI_DEBUG_MISC, "Rekall profile: symbol '%s' not found in $CONSTANTS\n", symbol);
            }
        } else {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: no $CONSTANTS section found\n");
        }

        if (json_object_object_get_ex(root, "$FUNCTIONS", &functions)) {
            if (json_object_object_get_ex(functions, symbol, &jsymbol)) {
                *rva = json_object_get_int64(jsymbol);

                ret = VMI_SUCCESS;
                goto exit;
            } else {
                dbprint(VMI_DEBUG_MISC, "Rekall profile: symbol '%s' not found in $FUNCTIONS\n", symbol);
            }
        } else {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: no $FUNCTIONS section found\n");
        }
    } else {
        json_object *structs = NULL, *jstruct = NULL, *jstruct2 = NULL, *jmember = NULL, *jvalue = NULL;
        if (!json_object_object_get_ex(root, "$STRUCTS", &structs)) {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: no $STRUCTS section found\n");
            goto exit;
        }
        if (!json_object_object_get_ex(structs, symbol, &jstruct)) {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: no %s found\n", symbol);
            goto exit;
        }

        jstruct2 = json_object_array_get_idx(jstruct, 1);
        if (!jstruct2) {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: struct %s has no second element\n", symbol);
            goto exit;
        }

        if (!json_object_object_get_ex(jstruct2, subsymbol, &jmember)) {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: %s has no %s member\n", symbol, subsymbol);
            goto exit;
        }

        jvalue = json_object_array_get_idx(jmember, 0);
        if (!jvalue) {
            dbprint(VMI_DEBUG_MISC, "Rekall profile: %s.%s has no RVA defined\n", symbol, subsymbol);
            goto exit;
        }

        *rva = json_object_get_int64(jvalue);
        ret = VMI_SUCCESS;
    }

exit:
    json_object_put(root);
    return ret;
}
Exemplo n.º 26
0
bool EASPolicyManager::importOldPolicySettings()
{
	json_object *root = 0, *prop = 0, *key = 0;
	root = json_object_from_file((char*)s_policyFile);
	if (!root || is_error(root))
		return false;


	prop = json_object_object_get(root, "version");
	if (!prop) {

		g_message("converting version 1 schema to version %d", s_version);

		// migrate version 1 schema
		EASPolicy* p = new EASPolicy;
		if(p->fromJSON(root))
		    m_aggregate->merge (p);
	}
	else if (json_object_get_int(prop) == 2) {

		// parse version 2 schema
		g_message("parsing version 2 schema");
		
		// parse all policies and create aggregate
		prop = json_object_object_get(root, "policies");
		if (prop && !is_error(prop) && json_object_is_type(prop, json_type_array)) {

			for (int i = 0; i < json_object_array_length(prop); i++) {

				EASPolicy* p = new EASPolicy;
				key = json_object_array_get_idx(prop, i);
				if (p->fromJSON(key))
				    m_aggregate->merge(p);
                                delete p;
			}
		}
	}
	else {
		g_critical("unrecognized EAS policy schema version %d", json_object_get_int(prop));
		return false;
	}
	
	// status
	prop = json_object_object_get(root, "status");
	if (prop && !is_error(prop)) {
		
		key = json_object_object_get(prop, "enforced");
		m_isEnforced = (key == 0 ? false : json_object_get_boolean(key));

		
		key = json_object_object_get(prop, "retriesLeft");
		if (key)
			m_retriesLeft = json_object_get_int(key);
		else if (m_aggregate)
			m_retriesLeft = m_aggregate->maxRetries();
	}
	else {
		m_isEnforced = false;
		if (m_aggregate)
			m_retriesLeft = m_aggregate->maxRetries();
	}
	
	// check the installed policies against the set of email accounts

    json_object_put(root);
	return true;
}
Exemplo n.º 27
0
/*******************************************************************
int memfrs_load_structs( const char* type_filename)

Load the data structure information into g_struct_info.

INPUT:     const char* type_filename,  the filename of json data structure database
OUTPUT:    int,                        return 0 if sucess, and not 0 otherwise

*******************************************************************/
int memfrs_load_structs( const char* type_filename)
{
    g_struct_info = json_object_from_file(type_filename);
    return 0;
}
Exemplo n.º 28
0
int decode_data_to_json(char *jfile)
{
	/* Encode an empty object/array, add an item, encode again */
	json_object *object, *array, *pobj;
	int i=0;

	array = json_object_from_file(jfile);
	for( i=0; i<json_object_array_length(array); i++)
	{
		object = json_object_array_get_idx(array, i);
		pobj = json_object_object_get(object, "name");
		if(pobj==NULL)
        {
            printf("error: object %d: name is not an object\n", (int)(i + 1));
		}
		//printf("name:%s\n",json_object_to_json_string(pobj));

        pobj = json_object_object_get(object, "node");
        if(pobj==NULL)
        {
            printf("error: object %d: node is not an object\n", (int)(i + 1));
		}
		//printf("node:%s\n",json_object_to_json_string(pobj));


        pobj = json_object_object_get(object, "dlSrc");
        if(pobj==NULL)
        {
            printf("error: object %d: dlSrcis not an object\n", (int)(i + 1));
        }


        pobj = json_object_object_get(object, "dlDst");
        if(pobj==NULL)
        {
            printf("error: object %d: dlDst is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "tpSrc");
        if(pobj==NULL)
        {
            printf("error: object %d: tpSrc is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "tpDst");
        if(pobj==NULL)
        {
            printf("error: object %d: tpDst is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "nwSrc");
        if(pobj==NULL)
        {
            printf("error: object %d: nwSrc is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "nwDst");
        if(pobj==NULL)
        {
            printf("error: object %d: nwDst is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "etherType");
        if(pobj==NULL)
        {
            printf("error: object %d: etherType is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "tosBits");
        if(pobj==NULL)
        {
            printf("error: object %d: tosBits is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "ingressPort");
        if(pobj==NULL)
        {
            printf("error: object %d: ingressPort is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "vlanId");
        if(pobj==NULL)
        {
            printf("error: object %d: vlanId is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "actions");
        if(pobj==NULL)
        {
            printf("error: object %d: actions is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "cookie");
        if(pobj==NULL)
        {
            printf("error: object %d: cookie is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "installInHw");
        if(pobj==NULL)
        {
            printf("error: object %d: installlnHw is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "protocol");
        if(pobj==NULL)
        {
            printf("error: object %d: protocol is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "vlanPriority");
        if(pobj==NULL)
        {
            printf("error: object %d: vlanPriority is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "hardTimeout");
        if(pobj==NULL)
        {
            printf("error: object %d: hardTimeout is not an object\n", (int)(i + 1));
        }

        pobj = json_object_object_get(object, "priority");
        if(pobj==NULL)
        {
            printf("error: object %d: priority is not an object\n", (int)(i + 1));
        }
	}

	//json_object_put(pobj);
	//json_object_put(object);
	json_object_put(array);
	return 0;
}