Пример #1
0
/************************************************************
	add_keys()  Add any pressed key to the list of pressed keys
	Each button has a number between [0..15]
INPUT:	mKeys
OUTPUT:	mNumberKeysPressed
		keys_down[] 
		Front Edge:  Left to Right [ 0, 1, 2, 3 ]
		Top Row   :  			   [ 4, 5, 6, 7 ]
		2nd Row   :                [ 8, 9, A, B ]
		3rd Row   :                [ C, D, E, F ]
		4th Row   :                [ 10, 11, 12, 13 ]
************************************************************/
void build_key_list( byte* mkeys )
{
	NumberKeysPressed = 0;
	add_keys( mkeys[0], 0 );
	add_keys( mkeys[1], 4 );
	add_keys( mkeys[2], 8 );
	add_keys( mkeys[3], 12);
	add_keys( mkeys[4], 16);
}
Пример #2
0
Файл: mr.c Проект: 168519/linux
static ssize_t limit_write(struct file *filp, const char __user *buf,
			   size_t count, loff_t *pos)
{
	struct mlx5_cache_ent *ent = filp->private_data;
	struct mlx5_ib_dev *dev = ent->dev;
	char lbuf[20];
	u32 var;
	int err;
	int c;

	if (copy_from_user(lbuf, buf, sizeof(lbuf)))
		return -EFAULT;

	c = order2idx(dev, ent->order);
	lbuf[sizeof(lbuf) - 1] = 0;

	if (sscanf(lbuf, "%u", &var) != 1)
		return -EINVAL;

	if (var > ent->size)
		return -EINVAL;

	ent->limit = var;

	if (ent->cur < ent->limit) {
		err = add_keys(dev, c, 2 * ent->limit - ent->cur);
		if (err)
			return err;
	}

	return count;
}
Пример #3
0
static void __cache_work_func(struct mlx5_cache_ent *ent)
{
	struct mlx5_ib_dev *dev = ent->dev;
	struct mlx5_mr_cache *cache = &dev->cache;
	int i = order2idx(dev, ent->order);

	if (cache->stopped)
		return;

	ent = &dev->cache.ent[i];
	if (ent->cur < 2 * ent->limit) {
		add_keys(dev, i, 1);
		if (ent->cur < 2 * ent->limit)
			queue_work(cache->wq, &ent->work);
	} else if (ent->cur > 2 * ent->limit) {
		if (!someone_adding(cache) &&
		    time_after(jiffies, cache->last_add + 60 * HZ)) {
			remove_keys(dev, i, 1);
			if (ent->cur > ent->limit)
				queue_work(cache->wq, &ent->work);
		} else {
			queue_delayed_work(cache->wq, &ent->dwork, 60 * HZ);
		}
	}
}
Пример #4
0
bool add_keys_from_file(const char *key_file)
{
    int size;
    FILE *fd = fopen(key_file, "r");
    if(fd == NULL)
    {
        if(g_debug)
            perror("cannot open key file");
        return false;
    }
    fseek(fd, 0, SEEK_END);
    size = ftell(fd);
    fseek(fd, 0, SEEK_SET);
    char *buf = xmalloc(size + 1);
    if(fread(buf, 1, size, fd) != (size_t)size)
    {
        if(g_debug)
            perror("Cannot read key file");
        fclose(fd);
        return false;
    }
    buf[size] = 0;
    fclose(fd);

    if(g_debug)
        printf("Parsing key file '%s'...\n", key_file);
    char *p = buf;
    while(1)
    {
        struct crypto_key_t k;
        /* parse key */
        if(!parse_key(&p, &k))
        {
            if(g_debug)
                printf("invalid key file\n");
            return false;
        }
        if(g_debug)
        {
            printf("Add key: ");
            print_key(&k, true);
        }
        add_keys(&k, 1);
        /* request at least one space character before next key, or end of file */
        if(*p != 0 && !isspace(*p))
        {
            if(g_debug)
                printf("invalid key file\n");
            return false;
        }
        /* skip whitespace */
        while(isspace(*p))
            p++;
        if(*p == 0)
            break;
    }
    free(buf);
    return true;
}
Пример #5
0
void class_tag::append_super(const class_tag &tag,const std::string & path){
	add_keys(tag.keys_);
	add_links(tag.links_);
	for (tag_map::const_iterator i = tag.tags_.begin();i!=tag.tags_.end();++i){
		links_.erase(i->first);
		add_link(path + "/" + i->first);

	}
}
Пример #6
0
Файл: mr.c Проект: 168519/linux
static void __cache_work_func(struct mlx5_cache_ent *ent)
{
	struct mlx5_ib_dev *dev = ent->dev;
	struct mlx5_mr_cache *cache = &dev->cache;
	int i = order2idx(dev, ent->order);
	int err;

	if (cache->stopped)
		return;

	ent = &dev->cache.ent[i];
	if (ent->cur < 2 * ent->limit && !dev->fill_delay) {
		err = add_keys(dev, i, 1);
		if (ent->cur < 2 * ent->limit) {
			if (err == -EAGAIN) {
				mlx5_ib_dbg(dev, "returned eagain, order %d\n",
					    i + 2);
				queue_delayed_work(cache->wq, &ent->dwork,
						   msecs_to_jiffies(3));
			} else if (err) {
				mlx5_ib_warn(dev, "command failed order %d, err %d\n",
					     i + 2, err);
				queue_delayed_work(cache->wq, &ent->dwork,
						   msecs_to_jiffies(1000));
			} else {
				queue_work(cache->wq, &ent->work);
			}
		}
	} else if (ent->cur > 2 * ent->limit) {
		if (!someone_adding(cache) &&
		    time_after(jiffies, cache->last_add + 300 * HZ)) {
			remove_keys(dev, i, 1);
			if (ent->cur > ent->limit)
				queue_work(cache->wq, &ent->work);
		} else {
			queue_delayed_work(cache->wq, &ent->dwork, 300 * HZ);
		}
	}
}
Пример #7
0
Файл: mr.c Проект: 168519/linux
static ssize_t size_write(struct file *filp, const char __user *buf,
			  size_t count, loff_t *pos)
{
	struct mlx5_cache_ent *ent = filp->private_data;
	struct mlx5_ib_dev *dev = ent->dev;
	char lbuf[20];
	u32 var;
	int err;
	int c;

	if (copy_from_user(lbuf, buf, sizeof(lbuf)))
		return -EFAULT;

	c = order2idx(dev, ent->order);
	lbuf[sizeof(lbuf) - 1] = 0;

	if (sscanf(lbuf, "%u", &var) != 1)
		return -EINVAL;

	if (var < ent->limit)
		return -EINVAL;

	if (var > ent->size) {
		do {
			err = add_keys(dev, c, var - ent->size);
			if (err && err != -EAGAIN)
				return err;

			usleep_range(3000, 5000);
		} while (err);
	} else if (var < ent->size) {
		remove_keys(dev, c, ent->size - var);
	}

	return count;
}
Пример #8
0
int main(int argc, char **argv)
{
    char *cmd_filename = NULL;
    char *output_filename = NULL;
    struct crypto_key_t real_key;
    struct crypto_key_t crypto_iv;
    memset(&real_key, 0, sizeof(real_key));
    memset(&crypto_iv, 0, sizeof(crypto_iv));
    real_key.method = CRYPTO_NONE;
    crypto_iv.method = CRYPTO_NONE;

    if(argc == 1)
        usage();

    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, 'h'},
            {"debug", no_argument, 0, 'd'},
            {"add-key", required_argument, 0, 'a'},
            {"real-key", required_argument, 0, 'r'},
            {"crypto-iv", required_argument, 0, 'i'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "hdo:c:k:za:", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case 'd':
                g_debug = true;
                break;
            case 'h':
                usage();
                break;
            case 'o':
                output_filename = optarg;
                break;
            case 'c':
                cmd_filename = optarg;
                break;
            case 'k':
            {
                if(!add_keys_from_file(optarg))
                    bug("Cannot keys from %s\n", optarg);
                break;
            }
            case 'z':
            {
                struct crypto_key_t g_zero_key;
                sb_get_zero_key(&g_zero_key);
                add_keys(&g_zero_key, 1);
                break;
            }
            case 'a':
            case 'r':
            case 'i':
            {
                struct crypto_key_t key;
                char *s = optarg;
                if(!parse_key(&s, &key))
                    bug("Invalid key/iv specified as argument");
                if(*s != 0)
                    bug("Trailing characters after key/iv specified as argument");
                if(c == 'r')
                    memcpy(&real_key, &key, sizeof(key));
                else if(c == 'i')
                    memcpy(&crypto_iv, &key, sizeof(key));
                else
                    add_keys(&key, 1);
                break;
            }
            default:
                bug("Internal error: unknown option '%c'\n", c);
        }
    }

    if(!cmd_filename)
        bug("You must specify a command file\n");
    if(!output_filename)
        bug("You must specify an output file\n");

    g_extern = &argv[optind];
    g_extern_count = argc - optind;

    if(g_debug)
    {
        printf("key: %d\n", g_nr_keys);
        for(int i = 0; i < g_nr_keys; i++)
        {
            printf("  ");
            print_key(NULL, misc_std_printf, &g_key_array[i], true);
        }

        for(int i = 0; i < g_extern_count; i++)
            printf("extern(%d)=%s\n", i, g_extern[i]);
    }

    struct cmd_file_t *cmd_file = db_parse_file(cmd_filename);
    if(cmd_file == NULL)
        bug("Error parsing command file\n");
    struct sb_file_t *sb_file = apply_cmd_file(cmd_file);
    db_free(cmd_file);

    if(real_key.method == CRYPTO_KEY)
    {
        sb_file->override_real_key = true;
        memcpy(sb_file->real_key, real_key.u.key, 16);
    }
    if(crypto_iv.method == CRYPTO_KEY)
    {
        sb_file->override_crypto_iv = true;
        memcpy(sb_file->crypto_iv, crypto_iv.u.key, 16);
    }

    sb_write_file(sb_file, output_filename, 0, generic_std_printf);
    sb_free(sb_file);
    clear_keys();

    return 0;
}
Пример #9
0
int main(int argc, char *argv[])
{
	int dev_from_class = 0, write_cnt;
	int fd;
	static struct sysfs_names *names;
	struct rc_device	  rc_dev;

	argp_parse(&argp, argc, argv, 0, 0, 0);

	/* Just list all devices */
	if (!clear && !readtable && !keys.next && !ch_proto && !cfg.next && !test && !delay && !period) {
		if (devicename) {
			fd = open(devicename, O_RDONLY);
			if (fd < 0) {
				perror("Can't open device");
				return -1;
			}
			device_info(fd, "");
			close(fd);
			return 0;
		}
		if (show_sysfs_attribs(&rc_dev))
			return -1;

		return 0;
	}

	if (cfg.next && (clear || keys.next || ch_proto || devicename)) {
		fprintf (stderr, "Auto-mode can be used only with --read, --debug and --sysdev options\n");
		return -1;
	}
	if (!devicename) {
		names = find_device(devclass);
		if (!names)
			return -1;
		rc_dev.sysfs_name = names->name;
		if (get_attribs(&rc_dev, names->name)) {
			free_names(names);
			return -1;
		}
		names->name = NULL;
		free_names(names);

		devicename = rc_dev.input_name;
		dev_from_class++;
	}

	if (cfg.next) {
		struct cfgfile *cur;
		char *fname, *name;
		int rc;

		for (cur = &cfg; cur->next; cur = cur->next) {
			if ((!rc_dev.drv_name || strcasecmp(cur->driver, rc_dev.drv_name)) && strcasecmp(cur->driver, "*"))
				continue;
			if ((!rc_dev.keytable_name || strcasecmp(cur->table, rc_dev.keytable_name)) && strcasecmp(cur->table, "*"))
				continue;
			break;
		}

		if (!cur->next) {
			if (debug)
				fprintf(stderr, "Table for %s, %s not found. Keep as-is\n",
				       rc_dev.drv_name, rc_dev.keytable_name);
			return 0;
		}
		if (debug)
			fprintf(stderr, "Table for %s, %s is on %s file.\n",
				rc_dev.drv_name, rc_dev.keytable_name,
				cur->fname);
		if (cur->fname[0] == '/' || ((cur->fname[0] == '.') && strchr(cur->fname, '/'))) {
			fname = cur->fname;
			rc = parse_keyfile(fname, &name);
			if (rc < 0) {
				fprintf(stderr, "Can't load %s table\n", fname);
				return -1;
			}
		} else {
			fname = malloc(strlen(cur->fname) + strlen(IR_KEYTABLE_USER_DIR) + 2);
			strcpy(fname, IR_KEYTABLE_USER_DIR);
			strcat(fname, "/");
			strcat(fname, cur->fname);
			rc = parse_keyfile(fname, &name);
			if (rc != 0) {
				fname = malloc(strlen(cur->fname) + strlen(IR_KEYTABLE_SYSTEM_DIR) + 2);
				strcpy(fname, IR_KEYTABLE_SYSTEM_DIR);
				strcat(fname, "/");
				strcat(fname, cur->fname);
				rc = parse_keyfile(fname, &name);
			}
			if (rc != 0) {
				fprintf(stderr, "Can't load %s table from %s or %s\n", cur->fname, IR_KEYTABLE_USER_DIR, IR_KEYTABLE_SYSTEM_DIR);
				return -1;
			}
		}
		if (!keys.next) {
			fprintf(stderr, "Empty table %s\n", fname);
			return -1;
		}
		clear = 1;
	}

	if (debug)
		fprintf(stderr, "Opening %s\n", devicename);
	fd = open(devicename, O_RDONLY);
	if (fd < 0) {
		perror(devicename);
		return -1;
	}
	if (dev_from_class)
		free(devicename);
	if (get_input_protocol_version(fd))
		return -1;

	/*
	 * First step: clear, if --clear is specified
	 */
	if (clear) {
		clear_table(fd);
		fprintf(stderr, "Old keytable cleared\n");
	}

	/*
	 * Second step: stores key tables from file or from commandline
	 */
	write_cnt = add_keys(fd);
	if (write_cnt)
		fprintf(stderr, "Wrote %d keycode(s) to driver\n", write_cnt);

	/*
	 * Third step: change protocol
	 */
	if (ch_proto) {
		rc_dev.current = ch_proto;
		if (set_proto(&rc_dev))
			fprintf(stderr, "Couldn't change the IR protocols\n");
		else {
			fprintf(stderr, "Protocols changed to ");
			show_proto(rc_dev.current);
			fprintf(stderr, "\n");
		}
	}

	/*
	 * Fourth step: display current keytable
	 */
	if (readtable)
		display_table(&rc_dev, fd);

	/*
	 * Fiveth step: change repeat rate/delay
	 */
	if (delay || period) {
		unsigned int new_delay, new_period;
		get_rate(fd, &new_delay, &new_period);
		if (delay)
			new_delay = delay;
		if (period)
			new_period = period;
		set_rate(fd, new_delay, new_period);
	}

	if (test)
		test_event(fd);

	return 0;
}
Пример #10
0
static int load_provider(GKeyFile *keyfile, const char *group,
				struct vpn_config *config, enum what action)
{
	struct vpn_config_provider *config_provider;
	const char *ident, *host, *domain;
	int err;

	/* Strip off "provider_" prefix */
	ident = group + 9;

	if (strlen(ident) < 1)
		return -EINVAL;

	config_provider = g_hash_table_lookup(config->provider_table, ident);
	if (config_provider)
		return -EALREADY;

	config_provider = g_try_new0(struct vpn_config_provider, 1);
	if (!config_provider)
		return -ENOMEM;

	config_provider->ident = g_strdup(ident);

	config_provider->setting_strings = g_hash_table_new_full(g_str_hash,
						g_str_equal, g_free, g_free);

	add_keys(config_provider, keyfile, group);

	host = get_string(config_provider, "Host");
	domain = get_string(config_provider, "Domain");
	if (host && domain) {
		char *id = __vpn_provider_create_identifier(host, domain);

		struct vpn_provider *provider;
		provider = __vpn_provider_lookup(id);
		if (provider) {
			if (action == REMOVE) {
				__vpn_provider_delete(provider);
				err = 0;
			} else {
				connman_warn("Provider configuration %s "
						"already exist", id);
				err = -EALREADY;
			}

			g_free(id);
			goto err;
		}

		config_provider->provider_identifier = id;

		DBG("provider identifier %s", id);
	} else {
		DBG("invalid values host %s domain %s", host, domain);
		err = -EINVAL;
		goto err;
	}

	config_provider->config_ident = g_strdup(config->ident);
	config_provider->config_entry = g_strdup_printf("provider_%s",
						config_provider->ident);

	g_hash_table_insert(config->provider_table,
				config_provider->ident,	config_provider);

	err = __vpn_provider_create_from_config(
					config_provider->setting_strings,
					config_provider->config_ident,
					config_provider->config_entry);
	if (err != 0) {
		DBG("Cannot create provider from config file (%d/%s)",
			-err, strerror(-err));
		goto err;
	}

	DBG("Added provider configuration %s",
						config_provider->ident);
	return 0;

err:
	g_free(config_provider->ident);
	g_free(config_provider->type);
	g_free(config_provider->name);
	g_free(config_provider->host);
	g_free(config_provider->domain);
	g_free(config_provider->networks);
	g_hash_table_destroy(config_provider->setting_strings);
	g_free(config_provider);

	return err;
}
Пример #11
0
int main(int argc, char *argv[])
{
    bool source = true;
    struct agent_data_t data;
    char *password = NULL;
    enum action verb = ACTION_NONE;
    enum agent type = AGENT_DEFAULT;
    void (*print_env)(struct agent_data_t *data) = print_sh_env;

    static const struct option opts[] = {
        { "help",    no_argument,       0, 'h' },
        { "version", no_argument,       0, 'v' },
        { "add",     no_argument,       0, 'a' },
        { "clear",   no_argument,       0, 'k' },
        { "kill",    no_argument,       0, 'K' },
        { "list",    no_argument,       0, 'l' },
        { "unlock",  optional_argument, 0, 'u' },
        { "print",   no_argument,       0, 'p' },
        { "sh",      no_argument,       0, 's' },
        { "csh",     no_argument,       0, 'c' },
        { "fish",    no_argument,       0, 'f' },
        { "agent",   required_argument, 0, 't' },
        { 0, 0, 0, 0 }
    };

    while (true) {
        int opt = getopt_long(argc, argv, "hvakKlu::pscft:", opts, NULL);
        if (opt == -1)
            break;

        switch (opt) {
        case 'h':
            usage(stdout);
            break;
        case 'v':
            printf("%s %s\n", program_invocation_short_name, ENVOY_VERSION);
            return 0;
        case 'a':
            verb = ACTION_FORCE_ADD;
            break;
        case 'k':
            verb = ACTION_CLEAR;
            source = false;
            break;
        case 'K':
            verb = ACTION_KILL;
            source = false;
            break;
        case 'l':
            verb = ACTION_LIST;
            break;
        case 'u':
            verb = ACTION_UNLOCK;
            password = optarg;
            break;
        case 'p':
            verb = ACTION_PRINT;
            break;
        case 's':
            print_env = print_sh_env;
            break;
        case 'c':
            print_env = print_csh_env;
            break;
        case 'f':
            print_env = print_fish_env;
            break;
        case 't':
            type = lookup_agent(optarg);
            if (type < 0)
                errx(EXIT_FAILURE, "unknown agent: %s", optarg);
            break;
        default:
            usage(stderr);
        }
    }

    if (get_agent(&data, type, source) < 0)
        errx(EXIT_FAILURE, "recieved no data, did the agent fail to start?");

    if (data.status == ENVOY_STOPPED)
        return 0;

    if (source)
        source_env(&data);

    switch (verb) {
    case ACTION_PRINT:
        print_env(&data);
        /* fall through */
    case ACTION_NONE:
        if (data.status != ENVOY_STARTED || data.type == AGENT_GPG_AGENT)
            break;
        /* fall through */
    case ACTION_FORCE_ADD:
        add_keys(&argv[optind], argc - optind);
        break;
    case ACTION_CLEAR:
        if (data.type == AGENT_GPG_AGENT)
            kill(data.pid, SIGHUP);
        else
            errx(EXIT_FAILURE, "only gpg-agent supports this operation");
        break;
    case ACTION_KILL:
        kill(data.pid, SIGTERM);
        break;
    case ACTION_LIST:
        execlp("ssh-add", "ssh-add", "-l", NULL);
        err(EXIT_FAILURE, "failed to launch ssh-add");
    case ACTION_UNLOCK:
        unlock(&data, password);
        break;
    default:
        break;
    }

    return 0;
}
Пример #12
0
int main(int argc, char **argv)
{
    bool raw_mode = false;
    const char *loopback = NULL;
    
    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, '?'},
            {"debug", no_argument, 0, 'd'},
            {"add-key", required_argument, 0, 'a'},
            {"no-color", no_argument, 0, 'n'},
            {"loopback", required_argument, 0, 'l'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "?do:k:zra:nl:", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case -1:
                break;
            case 'l':
                if(loopback)
                    bug("Only one loopback file can be specified !\n");
                loopback = optarg;
                break;
            case 'n':
                enable_color(false);
                break;
            case 'd':
                g_debug = true;
                break;
            case '?':
                usage();
                break;
            case 'o':
                g_out_prefix = optarg;
                break;
            case 'k':
            {
                add_keys_from_file(optarg);
                break;
            }
            case 'z':
            {
                add_keys(&g_zero_key, 1);
                break;
            }
            case 'r':
                raw_mode = true;
                break;
            case 'a':
            {
                struct crypto_key_t key;
                char *s = optarg;
                if(!parse_key(&s, &key))
                    bug("Invalid key specified as argument");
                if(*s != 0)
                    bug("Trailing characters after key specified as argument");
                add_keys(&key, 1);
                break;
            }
            default:
                abort();
        }
    }

    if(argc - optind != 1)
    {
        usage();
        return 1;
    }

    const char *sb_filename = argv[optind];

    enum sb_error_t err;
    struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, sb_printf, &err);
    if(file == NULL)
    {
        color(OFF);
        printf("SB read failed: %d\n", err);
        return 1;
    }
    
    color(OFF);
    if(g_out_prefix)
        extract_sb_file(file);
    if(g_debug)
    {
        color(GREY);
        printf("[Debug output]\n");
        sb_dump(file, NULL, sb_printf);
    }
    if(loopback)
    {
        /* sb_read_file will fill real key and IV but we don't want to override
         * them when looping back otherwise the output will be inconsistent and
         * garbage */
        file->override_real_key = false;
        file->override_crypto_iv = false;
        sb_write_file(file, loopback);
    }
    sb_free(file);
    clear_keys();
    
    return 0;
}
Пример #13
0
int main(int argc, char *argv[])
{
	int dev_from_class = 0, write_cnt;
	int fd;
	static struct sysfs_names *names;
	struct rc_device	  rc_dev;

	argp_parse(&argp, argc, argv, 0, 0, 0);

	/* Just list all devices */
	if (!clear && !read && !keys.next && !ch_proto && !cfg.next) {
		static struct sysfs_names *names, *cur;

		names = find_device(NULL);
		if (!names)
			return -1;
		for (cur = names; cur->next; cur = cur->next) {
			if (cur->name) {
				if (get_attribs(&rc_dev, cur->name))
					return -1;
				fprintf(stderr, "Found %s (%s) with:\n",
					rc_dev.sysfs_name,
					rc_dev.input_name);
				fprintf(stderr, "\tDriver %s, %s decoder, table %s\n",
					rc_dev.drv_name,
					(rc_dev.type == SOFTWARE_DECODER) ?
						"raw software" : "hardware",
					rc_dev.keytable_name);
				fprintf(stderr, "\tSupported protocols: ");
				show_proto(rc_dev.supported);
				fprintf(stderr, "\t");
				display_proto(&rc_dev);
			}
		}
		return 0;
	}

	if (cfg.next && (clear || keys.next || ch_proto || devname)) {
		fprintf (stderr, "Auto-mode can be used only with --read, --debug and --sysdev options\n");
		return -1;
	}
	if (!devname) {
		names = find_device(devclass);
		if (!names)
			return -1;
		rc_dev.sysfs_name = names->name;
		if (get_attribs(&rc_dev, names->name)) {
			free_names(names);
			return -1;
		}
		names->name = NULL;
		free_names(names);

		devname = rc_dev.input_name;
		dev_from_class++;
	}

	if (cfg.next) {
		struct cfgfile *cur;
		char *fname, *name;
		int rc;

		for (cur = &cfg; cur->next; cur = cur->next) {
			if (strcasecmp(cur->driver, rc_dev.drv_name) && strcasecmp(cur->driver, "*"))
				continue;
			if (strcasecmp(cur->table, rc_dev.keytable_name) && strcasecmp(cur->table, "*"))
				continue;
			break;
		}

		if (!cur->next) {
			if (debug)
				fprintf(stderr, "Table for %s, %s not found. Keep as-is\n",
				       rc_dev.drv_name, rc_dev.keytable_name);
			return 0;
		}
		if (debug)
			fprintf(stderr, "Table for %s, %s is on %s file.\n",
				rc_dev.drv_name, rc_dev.keytable_name,
				cur->fname);
		if (cur->fname[0] == '/' || ((cur->fname[0] == '.') && strchr(cur->fname, '/'))) {
			fname = cur->fname;
		} else {
			fname = malloc(strlen(cur->fname) + strlen(CFGDIR) + 2);
			strcpy(fname, CFGDIR);
			strcat(fname, "/");
			strcat(fname, cur->fname);
		}
		rc = parse_keyfile(fname, &name);
		if (rc < 0 || !keys.next) {
			fprintf(stderr, "Can't load %s table or empty table\n", fname);
			return -1;
		}
		clear = 1;
	}

	if (debug)
		fprintf(stderr, "Opening %s\n", devname);
	fd = open(devname, O_RDONLY);
	if (fd < 0) {
		perror(devname);
		return -1;
	}
	if (dev_from_class)
		free(devname);

	/*
	 * First step: clear, if --clear is specified
	 */
	if (clear) {
		clear_table(fd);
		fprintf(stderr, "Old keytable cleared\n");
	}

	/*
	 * Second step: stores key tables from file or from commandline
	 */
	write_cnt = add_keys(fd);
	if (write_cnt)
		fprintf(stderr, "Wrote %d keycode(s) to driver\n", write_cnt);

	/*
	 * Third step: change protocol
	 */
	if (ch_proto) {
		rc_dev.current = ch_proto;
		if (set_proto(&rc_dev))
			fprintf(stderr, "Couldn't change the IR protocols\n");
		else {
			fprintf(stderr, "Protocols changed to ");
			show_proto(rc_dev.current);
			fprintf(stderr, "\n");
		}
	}

	/*
	 * Fourth step: display current keytable
	 */
	if (read)
		display_table(&rc_dev, fd);

	return 0;
}
Пример #14
0
int main(int argc, char *argv[])
{
    bool source = true;
    bool defer = false;
    struct agent_data_t data;
    char *password = NULL;
    enum action verb = ACTION_NONE;
    enum agent type = AGENT_DEFAULT;
    void (*print_env)(struct agent_data_t *data) = print_sh_env;

    static const struct option opts[] = {
        { "help",    no_argument,       0, 'h' },
        { "version", no_argument,       0, 'v' },
        { "defer",   no_argument,       0, 'd' },
        { "add",     no_argument,       0, 'a' },
        { "expunge", no_argument,       0, 'x' },
        { "kill",    no_argument,       0, 'k' },
        { "reload",  no_argument,       0, 'r' },
        { "list",    no_argument,       0, 'l' },
        { "unlock",  optional_argument, 0, 'u' },
        { "print",   no_argument,       0, 'p' },
        { "sh",      no_argument,       0, 's' },
        { "csh",     no_argument,       0, 'c' },
        { "fish",    no_argument,       0, 'f' },
        { "agent",   required_argument, 0, 't' },
        { 0, 0, 0, 0 }
    };

    while (true) {
        int opt = getopt_long(argc, argv, "hvdaxkrlu::pscft:", opts, NULL);
        if (opt == -1)
            break;

        switch (opt) {
        case 'h':
            usage(stdout);
            break;
        case 'v':
            printf("%s %s\n", program_invocation_short_name, ENVOY_VERSION);
            return 0;
        case 'd':
            defer = true;
            break;
        case 'a':
            verb = ACTION_FORCE_ADD;
            defer = false;
            break;
        case 'x':
            verb = ACTION_FORCE_EXPUNGE;
            defer = false;
            break;
        case 'k':
            verb = ACTION_KILL;
            source = false;
            break;
        case 'r':
            verb = ACTION_RELOAD;
            source = false;
            break;
        case 'l':
            verb = ACTION_LIST;
            break;
        case 'u':
            verb = ACTION_UNLOCK;
            password = optarg;
            break;
        case 'p':
            verb = ACTION_PRINT;
            break;
        case 's':
            print_env = print_sh_env;
            break;
        case 'c':
            print_env = print_csh_env;
            break;
        case 'f':
            print_env = print_fish_env;
            break;
        case 't':
            type = lookup_agent(optarg);
            if (type < 0)
                errx(EXIT_FAILURE, "unknown agent: %s", optarg);
            break;
        default:
            usage(stderr);
        }
    }

    if (get_agent(&data, type, source, defer) < 0)
        errx(EXIT_FAILURE, "recieved no data, did the agent fail to start?");

    if (data.status == ENVOY_STOPPED)
        return 0;

    if (source)
        source_env(&data);

    switch (verb) {
    case ACTION_PRINT:
        print_env(&data);
        /* fall through */
    case ACTION_NONE:
        if (data.type == AGENT_GPG_AGENT || !agent_started(&data))
            break;
        if (defer)
            break;
        /* fall through */
    case ACTION_FORCE_ADD:
        add_keys(&argv[optind], argc - optind);
        break;
    case ACTION_FORCE_EXPUNGE:
        expunge_keys(&argv[optind], argc - optind);
        break;
    case ACTION_KILL:
        if (envoy_kill_agent(type) < 0)
            errx(EXIT_FAILURE, "failed to kill agent");
        break;
    case ACTION_RELOAD:
        reload_agent(&data);
        break;
    case ACTION_LIST:
        execlp("ssh-add", "ssh-add", "-l", NULL);
        err(EXIT_FAILURE, "failed to launch ssh-add");
    case ACTION_UNLOCK:
        unlock(&data, password);
        break;
    default:
        break;
    }

    return 0;
}
Пример #15
0
int main(int argc, char **argv)
{
    char *cmd_filename = NULL;
    char *output_filename = NULL;
    struct crypto_key_t real_key;
    struct crypto_key_t crypto_iv;
    real_key.method = CRYPTO_NONE;
    crypto_iv.method = CRYPTO_NONE;
    
    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, '?'},
            {"debug", no_argument, 0, 'd'},
            {"add-key", required_argument, 0, 'a'},
            {"real-key", required_argument, 0, 'r'},
            {"crypto-iv", required_argument, 0, 'i'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "?do:c:k:za:", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case 'd':
                g_debug = true;
                break;
            case '?':
                usage();
                break;
            case 'o':
                output_filename = optarg;
                break;
            case 'c':
                cmd_filename = optarg;
                break;
            case 'k':
            {
                if(!add_keys_from_file(optarg))
                    bug("Cannot keys from %s\n", optarg);
                break;
            }
            case 'z':
            {
                add_keys(&g_zero_key, 1);
                break;
            }
            case 'a':
            case 'r':
            case 'i':
            {
                struct crypto_key_t key;
                char *s = optarg;
                if(!parse_key(&s, &key))
                    bug("Invalid key/iv specified as argument");
                if(*s != 0)
                    bug("Trailing characters after key/iv specified as argument");
                if(c == 'r')
                    memcpy(&real_key, &key, sizeof(key));
                else if(c == 'i')
                    memcpy(&crypto_iv, &key, sizeof(key));
                else
                    add_keys(&key, 1);
                break;
            }
            default:
                abort();
        }
    }

    if(!cmd_filename)
        bug("You must specify a command file\n");
    if(!output_filename)
        bug("You must specify an output file\n");

    g_extern = &argv[optind];
    g_extern_count = argc - optind;

    if(g_debug)
    {
        printf("key: %d\n", g_nr_keys);
        for(int i = 0; i < g_nr_keys; i++)
        {
            printf("  ");
            print_key(&g_key_array[i], true);
        }

        for(int i = 0; i < g_extern_count; i++)
            printf("extern(%d)=%s\n", i, g_extern[i]);
    }

    struct cmd_file_t *cmd_file = db_parse_file(cmd_filename);
    struct sb_file_t *sb_file = apply_cmd_file(cmd_file);
    db_free(cmd_file);

    if(real_key.method == CRYPTO_KEY)
    {
        sb_file->override_real_key = true;
        memcpy(sb_file->real_key, real_key.u.key, 16);
    }
    if(crypto_iv.method == CRYPTO_KEY)
    {
        sb_file->override_crypto_iv = true;
        memcpy(sb_file->crypto_iv, crypto_iv.u.key, 16);
    }

    /* fill with default parameters since there is no command file support for them */
    sb_file->drive_tag = 0;
    sb_file->first_boot_sec_id = sb_file->sections[0].identifier;
    sb_file->flags = 0;
    sb_file->minor_version = 1;
    
    sb_write_file(sb_file, output_filename);
    sb_free(sb_file);
    clear_keys();
    
    return 0;
}
static bool add_keys(int count)
{
    static const ssize_t sz = 10;
    char key_buf[sz];
    char tag_buf[sz];
    char value_buf[sz];

    for (int i = 0; i < count; i++) {

        KineticEntry entry = {
            .key = ByteBuffer_CreateAndAppendFormattedCString(key_buf, sz, "key_%d", i),
            .tag = ByteBuffer_CreateAndAppendFormattedCString(tag_buf, sz, "tag_%d", i),
            .value = ByteBuffer_CreateAndAppendFormattedCString(value_buf, sz, "val_%d", i),
            .algorithm = KINETIC_ALGORITHM_SHA1,
            .force = true,
        };

        KineticStatus status = KineticClient_Put(Fixture.session, &entry, NULL);
        TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SUCCESS, status);
    }
    return true;
}

typedef enum { CMD_NEXT, CMD_PREVIOUS } GET_CMD;

static void compare_against_offset_key(GET_CMD cmd, bool metadataOnly)
{
    static const ssize_t sz = 10;
    char key_buf[sz];
    char tag_buf[sz];
    char value_buf[sz];
    char key_exp_buf[sz];
    char value_exp_buf[sz];

    const int count = 3;

    TEST_ASSERT_TRUE(add_keys(count));    /* add keys [key_X -> test_X] */

    int low = 0;
    int high = count;
    int8_t offset = 0;

    switch (cmd) {
    case CMD_NEXT:
        low = 1;
        offset = -1;
        break;
    case CMD_PREVIOUS:
        high = count - 1;
        offset = +1;
        break;
    default:
        TEST_ASSERT(false);
    }

    for (int i = low; i < high; i++) {
        ByteBuffer keyBuffer = ByteBuffer_CreateAndAppendFormattedCString(key_buf, sz, "key_%d", i + offset);
        ByteBuffer tagBuffer = ByteBuffer_CreateAndAppendFormattedCString(tag_buf, sz, "tag_%d", i + offset);
        ByteBuffer valueBuffer = ByteBuffer_Create(value_buf, sz, 0);

        printf("KEY '%s'\n", key_buf);

        KineticEntry entry = {
            .key = keyBuffer,
            .tag = tagBuffer,
            .value = valueBuffer,
            .algorithm = KINETIC_ALGORITHM_SHA1,
            .metadataOnly = metadataOnly,
        };
        KineticStatus status = KINETIC_STATUS_INVALID;

        switch (cmd) {
        case CMD_NEXT:
            status = KineticClient_GetNext(Fixture.session, &entry, NULL);
            break;
        case CMD_PREVIOUS:
            status = KineticClient_GetPrevious(Fixture.session, &entry, NULL);
            break;
        default:
            TEST_ASSERT(false);
        }
        
        TEST_ASSERT_EQUAL_KineticStatus(KINETIC_STATUS_SUCCESS, status);

        ByteBuffer expectedKeyBuffer = ByteBuffer_CreateAndAppendFormattedCString(key_exp_buf, sz, "key_%d", i);
        ByteBuffer expectedValueBuffer = ByteBuffer_CreateAndAppendFormattedCString(value_exp_buf, sz, "val_%d", i);

        TEST_ASSERT_EQUAL_ByteBuffer(expectedKeyBuffer, entry.key);
        if (!metadataOnly) {
            TEST_ASSERT_EQUAL_ByteBuffer(expectedValueBuffer, entry.value);
        }
    }
}

void test_GetNext_should_retrieve_object_and_metadata_from_device(void)
{
    compare_against_offset_key(CMD_NEXT, false);
}
Пример #17
0
int main(int argc, char **argv)
{
    bool raw_mode = false;
    const char *loopback = NULL;
    bool force_sb1 = false;
    bool force_sb2 = false;
    bool brute_force = false;

    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, '?'},
            {"debug", no_argument, 0, 'd'},
            {"add-key", required_argument, 0, 'a'},
            {"no-color", no_argument, 0, 'n'},
            {"loopback", required_argument, 0, 'l'},
            {"force", no_argument, 0, 'f'},
            {"v1", no_argument, 0, '1'},
            {"v2", no_argument, 0, '2'},
            {"no-simpl", no_argument, 0, 's'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "?do:k:zra:nl:f12xsb", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case -1:
                break;
            case 'l':
                if(loopback)
                    bug("Only one loopback file can be specified !\n");
                loopback = optarg;
                break;
            case 'n':
                enable_color(false);
                break;
            case 'd':
                g_debug = true;
                break;
            case '?':
                usage();
                break;
            case 'o':
                g_out_prefix = optarg;
                break;
            case 'f':
                g_force = true;
                break;
            case 'k':
            {
                if(!add_keys_from_file(optarg))
                    bug("Cannot add keys from %s\n", optarg);
                break;
            }
            case 'z':
            {
                struct crypto_key_t g_zero_key;
                sb_get_zero_key(&g_zero_key);
                add_keys(&g_zero_key, 1);
                break;
            }
            case 'x':
            {
                struct crypto_key_t key;
                sb1_get_default_key(&key);
                add_keys(&key, 1);
                break;
            }
            case 'r':
                raw_mode = true;
                break;
            case 'a':
            {
                struct crypto_key_t key;
                char *s = optarg;
                if(!parse_key(&s, &key))
                    bug("Invalid key specified as argument\n");
                if(*s != 0)
                    bug("Trailing characters after key specified as argument\n");
                add_keys(&key, 1);
                break;
            }
            case '1':
                force_sb1 = true;
                break;
            case '2':
                force_sb2 = true;
                break;
            case 's':
                g_elf_simplify = false;
                break;
            case 'b':
                brute_force = true;
                break;
            default:
                bug("Internal error: unknown option '%c'\n", c);
        }
    }

    if(force_sb1 && force_sb2)
        bug("You cannot force both version 1 and 2\n");

    if(argc - optind != 1)
    {
        usage();
        return 1;
    }

    const char *sb_filename = argv[optind];

    enum sb_version_guess_t ver = guess_sb_version(sb_filename);
    if(ver == SB_VERSION_ERR)
    {
        printf("Cannot open/read SB file: %m\n");
        return 1;
    }

    if(force_sb2 || ver == SB_VERSION_2)
    {
        enum sb_error_t err;
        struct sb_file_t *file = sb_read_file(sb_filename, raw_mode, NULL, generic_std_printf, &err);
        if(file == NULL)
        {
            color(OFF);
            printf("SB read failed: %d\n", err);
            return 1;
        }

        color(OFF);
        if(g_out_prefix)
            extract_sb_file(file);
        if(g_debug)
        {
            color(GREY);
            printf("[Debug output]\n");
            sb_dump(file, NULL, generic_std_printf);
        }
        if(loopback)
        {
            /* sb_read_file will fill real key and IV but we don't want to override
            * them when looping back otherwise the output will be inconsistent and
            * garbage */
            file->override_real_key = false;
            file->override_crypto_iv = false;
            sb_write_file(file, loopback, 0, generic_std_printf);
        }
        sb_free(file);
    }
    else if(force_sb1 || ver == SB_VERSION_1)
    {
        if(brute_force)
        {
            struct crypto_key_t key;
            enum sb1_error_t err;
            if(!sb1_brute_force(sb_filename, NULL, generic_std_printf, &err, &key))
            {
                color(OFF);
                printf("Brute force failed: %d\n", err);
                return 1;
            }
            color(RED);
            printf("Key found:");
            color(YELLOW);
            for(int i = 0; i < 32; i++)
                printf(" %08x", key.u.xor_key[i / 16].k[i % 16]);
            color(OFF);
            printf("\n");
            color(RED);
            printf("Key: ");
            color(YELLOW);
            for(int i = 0; i < 128; i++)
                printf("%02x", key.u.xor_key[i / 64].key[i % 64]);
            color(OFF);
            printf("\n");
            add_keys(&key, 1);
        }

        enum sb1_error_t err;
        struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, generic_std_printf, &err);
        if(file == NULL)
        {
            color(OFF);
            printf("SB read failed: %d\n", err);
            return 1;
        }

        color(OFF);
        if(g_out_prefix)
            extract_sb1_file(file);
        if(g_debug)
        {
            color(GREY);
            printf("[Debug output]\n");
            sb1_dump(file, NULL, generic_std_printf);
        }
        if(loopback)
            sb1_write_file(file, loopback);

        sb1_free(file);
    }
    else
    {
        color(OFF);
        printf("Cannot guess file type, are you sure it's a valid image ?\n");
        return 1;
    }
    clear_keys();

    return 0;
}