Пример #1
0
bool DBPermObj::AddToWhitelist(std::string & dbuser, std::string & pattern)
{
    char * q = new char [ pattern.length() + 1024 ];
    db_struct db;

    snprintf(q, pattern.length() + 1024,
        "SELECT queryid FROM query WHERE "
        "proxyid = %d and db_name = '%s' and query = '%s'",
        proxy_id, db_name.c_str(), pattern.c_str());

    /* read new queryid from the database */
    if (! db_query(&db, q ,pattern.length() + 1024)) {
        /* Failed */
        logevent(STORAGE,"DB config erorr: %s\n",db_error());
        delete [] q;
        db_cleanup(&db);
        return false;
    } 

    /* Download result from server */
    if (db_fetch_row(&db))
    {
        // nothing to do here, query is aready in whitelist
        delete [] q;
        db_cleanup(&db);
        return true;
    }

    // add pattern to the whitelist
    snprintf(q, pattern.length() + 1024,
       "INSERT into query "
       "(proxyid, perm, db_name, query) "
       "VALUES (%d,1,'%s','%s')",
       proxy_id, db_name.c_str(), pattern.c_str());

    /* read new urls from the database */
    if (! db_exec(q)) {
        /* Make query */
        logevent(STORAGE,"DB config erorr: %s\n",db_error());
        delete [] q;
        return false;
    }
    delete [] q;
    q = NULL;

    return true;
}
Пример #2
0
bool DBPermObj::LoadWhitelist()
{
    db_struct db;
    char q[1024];
    std::map<std::string, int, std::less<std::string> > q_list;

    snprintf(q, 1024, "SELECT query, perm FROM query "
              "WHERE proxyid = %d AND db_name='%s'",
              proxy_id, db_name.c_str());

    /* read new exceptions from the database */
    if (! db_query(&db,q,1024)) {
        logevent(STORAGE,"DB config erorr: %s\n",db_error());
        db_cleanup(&db);
        return false;
    } 

    int perm = 0;
    std::string q_ex = "";
        
    /* Get a row from the results */
    while (db_fetch_row(&db))
    {
        perm = db_col_int(&db,1);
        q_ex = (char *) db_col_text(&db,0);

        if (perm > 0 && q_ex.length() > 0)
        {
            //q_list[q_ex] = perm;        
            q_list.insert(std::pair<std::string, int>(q_ex, perm));
        }
    }

    /* Release memory used to store results. */
    db_cleanup(&db);

    exceptions.clear();
    exceptions = q_list;
    return true;
}
Пример #3
0
int do_vacuum_db(void)
{
	if (no_to_all) {
		qprintf("\nVacuum and optimize not performed.\n");
	}
	if (yes_to_all) {
		qprintf("\nVacuuming and optimizing database...\n");
		fflush(stdout);
		if (db_cleanup() < 0) {
			qerrorf("Failed. Please check the log.\n");
			serious_errors = 1;
			return -1;
		}

		qprintf("Ok. Database cleaned up.\n");
	}
	return 0;
}
Пример #4
0
int main(int argc, char **argv)
{
	int rc = 1;
	int opt, longind;
	char *config_file = "/etc/freight-agent/config";
	char *mode = NULL;
	char *rpm = NULL;
	struct db_api *api;
	char *list = "all";
	char *name = NULL;
	int verbose = 0;	
	/*
 	 * Parse command line options
 	 */

#ifdef HAVE_GETOPT_LONG
	while ((opt = getopt_long(argc, argv, "hc:m:r:l:vn:", lopts, &longind)) != -1) {
#else
	while ((opt = getopt(argc, argv, "hc:m:r:l:vn:") != -1) {
#endif
		switch(opt) {

		case '?':
			/* FALLTHROUGH */
		case 'h':
			usage(argv);
			goto out;
			/* NOTREACHED */
			break;
		case 'c':
			config_file = optarg;
			break;
		case 'm':
			mode = optarg;
			break;
		case 'n':
			name = optarg;
			break;
		case 'r':
			rpm = optarg;
			break;
		case 'l':
			list = optarg;
			break;
		case 'v':
			verbose = 1;
			break;
		}
	}


	if (strcmp(list, "all") &&
	    strcmp(list, "local") &&
	    strcmp(list, "running")) {
		LOG(ERROR, "list option must be all,running or local\n");
		goto out;
	}

	/*
 	 * Read in the configuration file
 	 */
	rc = read_configuration(config_file, &config);
	if (rc)
		goto out_release;

	config.cmdline.verbose = verbose;

	/*
 	 * Sanity checks
 	 */
	if (!mode) {
		LOG(ERROR, "You must specify a mode\n");
		goto out_release;
	}

	if (streq(mode, "node"))
		config.cmdline.mode = OP_MODE_NODE;
	else if (streq(mode, "init"))
		config.cmdline.mode = OP_MODE_INIT; 
	else if (streq(mode, "clean"))
		config.cmdline.mode = OP_MODE_CLEAN;
	else if (streq(mode, "install"))
		config.cmdline.mode = OP_MODE_INSTALL;
	else if (streq(mode, "uninstall"))
		config.cmdline.mode = OP_MODE_UNINSTALL;
	else if (streq(mode, "list"))
		config.cmdline.mode = OP_MODE_LIST;
	else if (streq(mode, "exec"))
		config.cmdline.mode = OP_MODE_EXEC;
	else {
		LOG(ERROR, "Invalid mode spcified\n");
		goto out_release;
	}

	if (!config.node.container_root) {
		LOG(ERROR, "Mode requires a container_root specification\n");
		goto out_release;
	}

	api = get_db_api(&config);
	if (!api) {
		LOG(ERROR, "No DB configuration selected\n");
		goto out_release;
	}

	if (db_init(api, &config)) {
		LOG(ERROR, "Unable to initalize DB subsystem\n");
		goto out_release;
	}

	if (db_connect(api, &config))
		goto out_cleanup_db;
	

	/*
 	 * Enter the appropriate function loop based on mode
 	 * Note, for all modes other than node mode (in which we 
 	 * monitor the database for mutiple tennant requests, all the modes
 	 * here are for the local user only, meaning everything goes to the 
 	 * 'local' tennant
 	 */
	switch (config.cmdline.mode) {

	case OP_MODE_INIT:
		rc = init_container_root(api, &config);
		if (rc) {
			LOG(ERROR, "Init of container root failed: %s\n",
				strerror(rc));
			goto out_disconnect;
		}
		break;
	case OP_MODE_CLEAN:
		LOG(INFO, "Removing container root %s\n",
			  config.node.container_root);
		clean_container_root(api, &config);
		break;
	case OP_MODE_INSTALL:
		if (!rpm) {
			LOG(ERROR, "Must specify a container name or rpm with "
				   "-r option in install mode\n");
			goto out_disconnect;
		}
		rc = local_install_container(rpm, &config);
		if (rc) {
			LOG(ERROR, "Failed to install container: %s\n",
				strerror(rc));
			goto out_disconnect;
		}
		break;
	case OP_MODE_UNINSTALL:
		if (!rpm) {
			LOG(ERROR, "Must specify the container name with -r\n");
			goto out_disconnect;
		}
		rc = local_uninstall_container(rpm, &config);
		if (rc) {
			LOG(ERROR, "Uninstall of container %s failed: %s\n",
				rpm, strerror(rc));
			goto out_disconnect;
		}
		break;
	case OP_MODE_LIST:
		local_list_containers(list, &config);
		break;
	case OP_MODE_EXEC:
		if (!rpm) {
			LOG(ERROR, "Must specify the container name with -r\n");
			goto out_disconnect;
		}
		if (!name) {
			LOG(ERROR, "Must specify a container instance name with -n\n");
			goto out_disconnect;
		}
		rc = local_exec_container(rpm, name, &config);
		if (rc) {
			LOG(ERROR, "Exec of container %s failed: %s\n",
				rpm, strerror(rc));
			goto out_disconnect;
		}
	case OP_MODE_NODE:
		rc = enter_mode_loop(api, &config);
		if (rc) {
			LOG(ERROR, "Mode operation terminated abnormally: %s\n",
				strerror(rc));
			goto out_disconnect;
		}
		break;
	}

	rc = 0;

out_disconnect:
	db_disconnect(api, &config);
out_cleanup_db:
	db_cleanup(api, &config);
out_release:
	release_configuration(&config);
out:
	return rc;
}