Example #1
0
int main (int argc, char *argv[])
{
	struct cpe_lang_model *lang_model = NULL;
	struct cpe_platform *platform = NULL, *new_platform = NULL;
	struct cpe_testexpr *testexpr = NULL;
	struct cpe_platform_iterator *platform_it = NULL;
	struct oscap_text_iterator *title_it = NULL;
	struct oscap_text *title = NULL;
	int ret_val = 0, i;

	if (argc == 2 && !strcmp(argv[1], "--help")) {
		print_usage(argv[0], stdout);
		ret_val = 0;
	}

	// Print complete content.
	else if (argc == 4 && !strcmp(argv[1], "--get-all")) {

		if ((lang_model = cpe_lang_model_import(argv[2])) == NULL)
			return 1;

		//printf("%s:", cpe_lang_model_get_ns_href(lang_model));
		//printf("%s\n", cpe_lang_model_get_ns_prefix(lang_model));
		platform_it = cpe_lang_model_get_platforms(lang_model);
		while (cpe_platform_iterator_has_more(platform_it)) {
			print_platform(cpe_platform_iterator_next(platform_it));
		}
		cpe_platform_iterator_free(platform_it);
		cpe_lang_model_free(lang_model);
	}

	// Print platform of given key only.
	else if (argc == 5 && !strcmp(argv[1], "--get-key")) {

		if ((lang_model = cpe_lang_model_import(argv[2])) == NULL)
			return 1;

		if ((platform = cpe_lang_model_get_item(lang_model, argv[4])) == NULL)
			return 2;

		print_platform(platform);

		cpe_lang_model_free(lang_model);
	}

	// Set ns_prefix, ns_href, add new platforms.
	else if (argc >= 6 && !strcmp(argv[1], "--set-all")) {
		if ((lang_model = cpe_lang_model_import(argv[2])) == NULL)
			return 1;

		for (i = 6; i < argc; i++) {
			if ((new_platform =  cpe_platform_new()) == NULL)
				return 1;
			cpe_platform_set_id(new_platform, argv[i]);
			if (!cpe_lang_model_add_platform(lang_model, new_platform))
				return 2;
		}

		cpe_lang_model_export(lang_model, argv[2]);
		cpe_lang_model_free(lang_model);
	}

	// Set id, change titles of platform of given key.
	else if (argc >= 6 && !strcmp(argv[1], "--set-key")) {
		if ((lang_model = cpe_lang_model_import(argv[2])) == NULL)
			return 1;

		if ((platform = cpe_lang_model_get_item(lang_model, argv[4])) == NULL)
			return 2;

		if (strcmp(argv[5], "-"))
			cpe_platform_set_id(platform, argv[5]);

		i = 6;
		title_it = cpe_platform_get_titles(platform);
		while (i < argc && oscap_text_iterator_has_more(title_it)) {
			title = oscap_text_iterator_next(title_it);
			if (strcmp(argv[i], "-"))
				oscap_text_set_text(title, argv[i]);
			i++;
		}

		cpe_lang_model_export(lang_model, argv[2]);
		cpe_lang_model_free(lang_model);
	}

	// Create new content with new platforms.
	else if (argc >= 6 && !strcmp(argv[1], "--set-new")) {
		if ((lang_model = cpe_lang_model_new()) == NULL)
			return 1;

		for (i = 6; i < argc; i++) {
			if ((new_platform =  cpe_platform_new()) == NULL)
				return 1;
			cpe_platform_set_id(new_platform, argv[i]);
			/*
			   struct cpe_testexpr *expr = cpe_testexpr_new();
			   cpe_testexpr_set_oper(expr, CPE_LANG_OPER_MATCH);
			   cpe_testexpr_set_name(expr, cpe_name_new("cpe:/a:nevim"));
			   cpe_platform_set_expr(new_platform, expr);
			   */
			if (!cpe_lang_model_add_platform(lang_model, new_platform))
				return 2;
		}

		cpe_lang_model_export(lang_model, argv[2]);
		cpe_lang_model_free(lang_model);
	}

	// Sanity checks.
	else if (argc == 2 && !strcmp(argv[1], "--smoke-test")) {

		if ((lang_model = cpe_lang_model_new()) == NULL)
			return 1;
		else
			cpe_lang_model_free(lang_model);

		if ((new_platform =  cpe_platform_new()) == NULL)
			return 1;
		else
			cpe_platform_free(new_platform);

		if ((testexpr = cpe_testexpr_new()) == NULL)
			return 1;
		else
			cpe_testexpr_free(testexpr);
	}

	else if (argc == 6 && !strcmp(argv[1], "--export-all")) {
		if ((lang_model = cpe_lang_model_import(argv[2])) == NULL)
			return 1;

		cpe_lang_model_export(lang_model, argv[4]);
		cpe_lang_model_free(lang_model);
	}
	else if (argc == 6 && !strcmp(argv[1], "--match-cpe")) {
		if ((lang_model = cpe_lang_model_import(argv[2])) == NULL)
			return 1;

		struct cpe_name *name1 = NULL;
		struct cpe_name *name2 = NULL;
		char * uri = NULL;

		// make cpe_name
		name1 = cpe_name_new(argv[4]);
		name2 = cpe_name_new(argv[5]);
		if ( (uri = cpe_name_get_as_str(name1)) == NULL ) return 1;
		if ( (uri = cpe_name_get_as_str(name2)) == NULL ) return 1;
		// actually we need array of cpe_name-s
		struct cpe_name ** names = (struct cpe_name **) malloc(2*sizeof(struct cpe_name *)); // <-- just for clear what I'm doing
		names[0] = name1;
		names[1] = name2;

		// let's get platform cpe's to match
		platform_it = cpe_lang_model_get_platforms(lang_model);
		platform = cpe_platform_iterator_next(platform_it); // we just need first one (no more there)
		cpe_platform_iterator_free(platform_it);

		ret_val = !cpe_platform_match_cpe(names, 2, platform);

		cpe_name_free(name1);
		cpe_name_free(name2);
		free(names);

		cpe_lang_model_free(lang_model);

		return ret_val;
	}
	else {
		print_usage(argv[0], stderr);
		ret_val = 1;
	}

	oscap_cleanup();

	return ret_val;
}
Example #2
0
int
main(
    int		argc,
    char **	argv)
{
    char *line = NULL;
    char *qdisk = NULL;
    char *qamdevice = NULL;
    char *optstr = NULL;
    char *err_extra = NULL;
    char *s, *fp;
    int ch;
    dle_t *dle = NULL;
    int level;
    GSList *errlist;
    level_t *alevel;

    if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
	printf("selfcheck-%s\n", VERSION);
	return (0);
    }

    /* initialize */

    /*
     * Configure program for internationalization:
     *   1) Only set the message locale for now.
     *   2) Set textdomain for all amanda related programs to "amanda"
     *      We don't want to be forced to support dozens of message catalogs.
     */  
    setlocale(LC_MESSAGES, "C");
    textdomain("amanda"); 

    safe_fd(-1, 0);
    openbsd_fd_inform();
    safe_cd();

    set_pname("selfcheck");

    /* Don't die when child closes pipe */
    signal(SIGPIPE, SIG_IGN);

    add_amanda_log_handler(amanda_log_stderr);
    add_amanda_log_handler(amanda_log_syslog);
    dbopen(DBG_SUBDIR_CLIENT);
    startclock();
    dbprintf(_("version %s\n"), VERSION);
    g_printf("OK version %s\n", VERSION);

    print_platform();
    if(argc > 2 && g_str_equal(argv[1], "amandad")) {
	amandad_auth = g_strdup(argv[2]);
    }

    config_init(CONFIG_INIT_CLIENT, NULL);
    /* (check for config errors comes later) */

    check_running_as(RUNNING_AS_CLIENT_LOGIN);

    our_features = am_init_feature_set();
    our_feature_string = am_feature_to_string(our_features);

    /* handle all service requests */

    /*@ignore@*/
    for(; (line = agets(stdin)) != NULL; free(line)) {
    /*@end@*/
	if (line[0] == '\0')
	    continue;

	if(strncmp_const(line, "OPTIONS ") == 0) {
	    if (g_options) {
		g_printf(_("ERROR [Multiple OPTIONS line in selfcheck input]\n"));
		error(_("Multiple OPTIONS line in selfcheck input\n"));
		/*NOTREACHED*/
	    }
	    g_options = parse_g_options(line+8, 1);
	    if(!g_options->hostname) {
		g_options->hostname = g_malloc(MAX_HOSTNAME_LENGTH+1);
		gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
		g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
	    }

	    g_printf("OPTIONS ");
	    if(am_has_feature(g_options->features, fe_rep_options_features)) {
		g_printf("features=%s;", our_feature_string);
	    }
	    if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
		g_printf("hostname=%s;", g_options->hostname);
	    }
	    g_printf("\n");
	    fflush(stdout);

	    if (g_options->config) {
		/* overlay this configuration on the existing (nameless) configuration */
		config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
			    g_options->config);

		dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
	    }

	    /* check for any config errors now */
	    if (config_errors(&errlist) >= CFGERR_ERRORS) {
		char *errstr = config_errors_to_error_string(errlist);
		g_printf("%s\n", errstr);
		amfree(errstr);
		amfree(line);
		dbclose();
		return 1;
	    }

	    if (am_has_feature(g_options->features, fe_req_xml)) {
		break;
	    }
	    continue;
	}

	dle = alloc_dle();
	s = line;
	ch = *s++;

	skip_whitespace(s, ch);			/* find program name */
	if (ch == '\0') {
	    goto err;				/* no program */
	}
	dle->program = s - 1;
	skip_non_whitespace(s, ch);
	s[-1] = '\0';				/* terminate the program name */

	dle->program_is_application_api = 0;
	if(g_str_equal(dle->program, "APPLICATION")) {
	    dle->program_is_application_api = 1;
	    skip_whitespace(s, ch);		/* find dumper name */
	    if (ch == '\0') {
		goto err;			/* no program */
	    }
	    dle->program = s - 1;
	    skip_non_whitespace(s, ch);
	    s[-1] = '\0';			/* terminate the program name */
	}

	if(strncmp_const(dle->program, "CALCSIZE") == 0) {
	    skip_whitespace(s, ch);		/* find program name */
	    if (ch == '\0') {
		goto err;			/* no program */
	    }
	    dle->program = s - 1;
	    skip_non_whitespace(s, ch);
	    s[-1] = '\0';
	    dle->estimatelist = g_slist_append(dle->estimatelist,
					       GINT_TO_POINTER(ES_CALCSIZE));
	}
	else {
	    dle->estimatelist = g_slist_append(dle->estimatelist,
					       GINT_TO_POINTER(ES_CLIENT));
	}

	skip_whitespace(s, ch);			/* find disk name */
	if (ch == '\0') {
	    goto err;				/* no disk */
	}
	qdisk = s - 1;
	skip_quoted_string(s, ch);
	s[-1] = '\0';				/* terminate the disk name */
	dle->disk = unquote_string(qdisk);

	skip_whitespace(s, ch);                 /* find the device or level */
	if (ch == '\0') {
	    goto err;				/* no device or level */
	}
	if(!isdigit((int)s[-1])) {
	    fp = s - 1;
	    skip_quoted_string(s, ch);
	     s[-1] = '\0';			/* terminate the device */
	    qamdevice = g_strdup(fp);
	    dle->device = unquote_string(qamdevice);
	    skip_whitespace(s, ch);		/* find level number */
	}
	else {
	    dle->device = g_strdup(dle->disk);
	    qamdevice = g_strdup(qdisk);
	}
	amfree(qamdevice);

						/* find level number */
	if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
	    goto err;				/* bad level */
	}
	alevel = g_new0(level_t, 1);
	alevel->level = level;
	dle->levellist = g_slist_append(dle->levellist, alevel);
	skip_integer(s, ch);

	skip_whitespace(s, ch);
	if (ch && strncmp_const_skip(s - 1, "OPTIONS ", s, ch) == 0) {
	    skip_whitespace(s, ch);		/* find the option string */
	    if(ch == '\0') {
		goto err;			/* bad options string */
	    }
	    optstr = s - 1;
	    skip_quoted_string(s, ch);
	    s[-1] = '\0';			/* terminate the options */
	    parse_options(optstr, dle, g_options->features, 1);
	    /*@ignore@*/

	    check_options(dle);
	    check_disk(dle);

	    /*@end@*/
	} else if (ch == '\0') {
	    /* check all since no option */
	    need_samba=1;
	    need_rundump=1;
	    need_dump=1;
	    need_restore=1;
	    need_vdump=1;
	    need_vrestore=1;
	    need_xfsdump=1;
	    need_xfsrestore=1;
	    need_vxdump=1;
	    need_vxrestore=1;
	    need_runtar=1;
	    need_gnutar=1;
	    need_compress_path=1;
	    need_calcsize=1;
	    need_global_check=1;
	    /*@ignore@*/
	    check_disk(dle);
	    /*@end@*/
	} else {
	    goto err;				/* bad syntax */
	}
	free_dle(dle);
	dle = NULL;
    }
    if (g_options == NULL) {
	g_printf(_("ERROR [Missing OPTIONS line in selfcheck input]\n"));
	error(_("Missing OPTIONS line in selfcheck input\n"));
	/*NOTREACHED*/
    }

    if (am_has_feature(g_options->features, fe_req_xml)) {
	char  *errmsg = NULL;
	dle_t *dles, *dle, *dle_next;

	dles = amxml_parse_node_FILE(stdin, &errmsg);
	if (errmsg) {
	    err_extra = errmsg;
	    goto err;
	}
	if (merge_dles_properties(dles, 1) == 0) {
	    goto checkoverall;
	}
	for (dle = dles; dle != NULL; dle = dle->next) {
	    run_client_scripts(EXECUTE_ON_PRE_HOST_AMCHECK, g_options, dle,
			       stdout);
	}
	for (dle = dles; dle != NULL; dle = dle->next) {
	    check_options(dle);
	    run_client_scripts(EXECUTE_ON_PRE_DLE_AMCHECK, g_options, dle,
			       stdout);
	    check_disk(dle);
	    run_client_scripts(EXECUTE_ON_POST_DLE_AMCHECK, g_options, dle,
			       stdout);
	}
	for (dle = dles; dle != NULL; dle = dle->next) {
	    run_client_scripts(EXECUTE_ON_POST_HOST_AMCHECK, g_options, dle,
			       stdout);
	}
	for (dle = dles; dle != NULL; dle = dle_next) {
	    dle_next = dle->next;
	    free_dle(dle);
	}
    }

checkoverall:
    check_overall();

    amfree(line);
    amfree(our_feature_string);
    am_release_feature_set(our_features);
    our_features = NULL;
    free_g_options(g_options);

    dbclose();
    return 0;

 err:
    if (err_extra) {
	g_printf(_("ERROR [FORMAT ERROR IN REQUEST PACKET %s]\n"), err_extra);
	dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
    } else {
	g_printf(_("ERROR [FORMAT ERROR IN REQUEST PACKET]\n"));
	dbprintf(_("REQ packet is bogus\n"));
    }
    amfree(err_extra);
    amfree(line);
    if (dle)
	free_dle(dle);
    dbclose();
    return 1;
}