Пример #1
0
ha_time_t *
parse_time(char **time_str, ha_time_t * a_time, gboolean with_offset)
{
    ha_time_t *new_time = a_time;

    tzset();
    if (a_time == NULL) {
        new_time = new_ha_date(FALSE);
    }

    CRM_CHECK(new_time != NULL, return NULL);
    CRM_CHECK(new_time->has != NULL, free_ha_date(new_time);
              return NULL);

    /* reset the time fields */
    new_time->hours = 0;
    new_time->minutes = 0;
    new_time->seconds = 0;

    crm_trace("Get hours...");
    new_time->has->hours = FALSE;
    if (parse_int(time_str, 2, 24, &new_time->hours)) {
        new_time->has->hours = TRUE;
    }

    crm_trace("Get minutes...");
    new_time->has->minutes = FALSE;
    if (parse_int(time_str, 2, 60, &new_time->minutes)) {
        new_time->has->minutes = TRUE;
    }

    crm_trace("Get seconds...");
    new_time->has->seconds = FALSE;
    if (parse_int(time_str, 2, 60, &new_time->seconds)) {
        new_time->has->seconds = TRUE;
    }

    if (with_offset) {
        crm_trace("Get offset...");
        while (isspace((int)(*time_str)[0])) {
            (*time_str)++;
        }

        new_time->offset = parse_time_offset(time_str);
        normalize_time(new_time);
    }
    return new_time;
}
Пример #2
0
/* Initialize program configuration via config file and/or command-line
 * switches.
*/
void
config_init(fko_cli_options_t *options, int argc, char **argv)
{
    int                 cmd_arg, index;

    /* Zero out options and opts_track.
    */
    memset(options, 0x00, sizeof(fko_cli_options_t));

    /* Make sure a few reasonable defaults are set
    */
    set_defaults(options);

    /* First pass over cmd_line args to see if a named-stanza in the 
     * rc file is used.
    */
    while ((cmd_arg = getopt_long(argc, argv,
            GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) {
        switch(cmd_arg) {
            case 'h':
                usage();
                exit(EXIT_SUCCESS);
            case 'n':
                options->no_save_args = 1;
                strlcpy(options->use_rc_stanza, optarg, MAX_LINE_LEN);
                break;
            case 'v':
                options->verbose++;
                break;
        }
    }

    /* First process the .fwknoprc file.
    */
    process_rc(options);

    /* Reset the options index so we can run through them again.
    */
    optind = 0;

    while ((cmd_arg = getopt_long(argc, argv,
            GETOPTS_OPTION_STRING, cmd_opts, &index)) != -1) {

        switch(cmd_arg) {
            case 'a':
                strlcpy(options->allow_ip_str, optarg, MAX_IPV4_STR_LEN);
                break;
            case 'A':
                strlcpy(options->access_str, optarg, MAX_LINE_LEN);
                break;
            case 'b':
                options->save_packet_file_append = 1;
                break;
            case 'B':
                strlcpy(options->save_packet_file, optarg, MAX_PATH_LEN);
                break;
            case 'C':
                strlcpy(options->server_command, optarg, MAX_LINE_LEN);
                break;
            case 'D':
                strlcpy(options->spa_server_str, optarg, MAX_SERVER_STR_LEN);
                break;
            case 'f':
                options->fw_timeout = atoi(optarg);
                if (options->fw_timeout < 0) {
                    fprintf(stderr, "--fw-timeout must be >= 0\n");
                    exit(EXIT_FAILURE);
                }
                break;
            case 'g':
            case GPG_ENCRYPTION:
                options->use_gpg = 1;
                break;
            case 'G':
                strlcpy(options->get_key_file, optarg, MAX_PATH_LEN);
                break;
            case 'h':
                usage();
                exit(EXIT_SUCCESS);
            case 'H':
                options->spa_proto = FKO_PROTO_HTTP;
                strlcpy(options->http_proxy, optarg, MAX_PATH_LEN);
                break;
            case SPA_ICMP_TYPE:
                options->spa_icmp_type = atoi(optarg);
                if (options->spa_icmp_type < 0 || options->spa_icmp_type > MAX_ICMP_TYPE)
                {
                    fprintf(stderr, "Unrecognized icmp type value: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case SPA_ICMP_CODE:
                options->spa_icmp_code = atoi(optarg);
                if (options->spa_icmp_code < 0 || options->spa_icmp_code > MAX_ICMP_CODE)
                {
                    fprintf(stderr, "Unrecognized icmp code value: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'l':
                options->run_last_command = 1;
                break;
            case 'm':
            case FKO_DIGEST_NAME:
                if((options->digest_type = digest_strtoint(optarg)) < 0)
                {
                    fprintf(stderr, "* Invalid digest type: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case NO_SAVE_ARGS:
                options->no_save_args = 1;
                break;
            case 'n':
                /* We already handled this earlier, so we do nothing here
                */
                break;
            case 'N':
                strlcpy(options->nat_access_str, optarg, MAX_LINE_LEN);
                break;
            case 'p':
                options->spa_dst_port = atoi(optarg);
                if (options->spa_dst_port < 0 || options->spa_dst_port > MAX_PORT)
                {
                    fprintf(stderr, "Unrecognized port: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'P':
                if((options->spa_proto = proto_strtoint(optarg)) < 0)
                {
                    fprintf(stderr, "Unrecognized protocol: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'Q':
                strlcpy(options->spoof_ip_src_str, optarg, MAX_IPV4_STR_LEN);
                break;
            case 'r':
                options->rand_port = 1;
                break;
            case 'R':
                options->resolve_ip_http = 1;
                break;
            case RESOLVE_URL:
                if(options->resolve_url != NULL)
                    free(options->resolve_url);
                options->resolve_url = malloc(strlen(optarg)+1);
                if(options->resolve_url == NULL)
                {
                    fprintf(stderr, "Memory allocation error for resolve URL.\n");
                    exit(EXIT_FAILURE);
                }
                strlcpy(options->resolve_url, optarg, strlen(optarg)+1);
                break;
            case SHOW_LAST_ARGS:
                options->show_last_command = 1;
                break;
            case 's':
                strlcpy(options->allow_ip_str, "0.0.0.0", MAX_IPV4_STR_LEN);
                break;
            case 'S':
                options->spa_src_port = atoi(optarg);
                if (options->spa_src_port < 0 || options->spa_src_port > MAX_PORT)
                {
                    fprintf(stderr, "Unrecognized port: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case 'T':
                options->test = 1;
                break;
            case 'u':
                strlcpy(options->http_user_agent, optarg, HTTP_MAX_USER_AGENT_LEN);
                break;
            case 'U':
                strlcpy(options->spoof_user, optarg, MAX_USERNAME_LEN);
                break;
            case 'v':
                /* Handled earlier.
                */
                break;
            case 'V':
                options->version = 1;
                break;
            case GPG_RECIP_KEY:
                options->use_gpg = 1;
                strlcpy(options->gpg_recipient_key, optarg, MAX_GPG_KEY_ID);
                break;
            case GPG_SIGNER_KEY:
                options->use_gpg = 1;
                strlcpy(options->gpg_signer_key, optarg, MAX_GPG_KEY_ID);
                break;
            case GPG_HOME_DIR:
                options->use_gpg = 1;
                strlcpy(options->gpg_home_dir, optarg, MAX_PATH_LEN);
                break;
            case GPG_AGENT:
                options->use_gpg = 1;
                options->use_gpg_agent = 1;
                break;
            case NAT_LOCAL:
                options->nat_local = 1;
                break;
            case NAT_RAND_PORT:
                options->nat_rand_port = 1;
                break;
            case NAT_PORT:
                options->nat_port = atoi(optarg);
                if (options->nat_port < 0 || options->nat_port > MAX_PORT)
                {
                    fprintf(stderr, "Unrecognized port: %s\n", optarg);
                    exit(EXIT_FAILURE);
                }
                break;
            case TIME_OFFSET_PLUS:
                options->time_offset_plus = parse_time_offset(optarg);
                break;
            case TIME_OFFSET_MINUS:
                options->time_offset_minus = parse_time_offset(optarg);
                break;
            default:
                usage();
                exit(EXIT_FAILURE);
        }
    }

    /* Now that we have all of our options set, we can validate them.
    */
    validate_options(options);

    return;
}
Пример #3
0
static int
parse_rc_param(fko_cli_options_t *options, const char *var, char * val)
{
    int     tmpint;

    /* Digest Type */
    if(CONF_VAR_IS(var, "DIGEST_TYPE"))
    {
        tmpint = digest_strtoint(val);
        if(tmpint < 0)
            return(-1);
        else
            options->digest_type = tmpint;
    }
    /* Server protocol */
    else if(CONF_VAR_IS(var, "SPA_SERVER_PROTO"))
    {
        tmpint = proto_strtoint(val);
        if(tmpint < 0)
            return(-1);
        else
            options->spa_proto = tmpint;
    }
    /* Server port */
    else if(CONF_VAR_IS(var, "SPA_SERVER_PORT"))
    {
        tmpint = atoi(val);
        if(tmpint < 0 || tmpint > MAX_PORT)
            return(-1);
        else
            options->spa_dst_port = tmpint;
    }
    /* Source port */
    else if(CONF_VAR_IS(var, "SPA_SOURCE_PORT"))
    {
        tmpint = atoi(val);
        if(tmpint < 0 || tmpint > MAX_PORT)
            return(-1);
        else
            options->spa_src_port = tmpint;
    }
    /* Firewall rule timeout */
    else if(CONF_VAR_IS(var, "FW_TIMEOUT"))
    {
        tmpint = atoi(val);
        if(tmpint < 0)
            return(-1);
        else
            options->fw_timeout = tmpint;
    }
    /* Allow IP */
    else if(CONF_VAR_IS(var, "ALLOW_IP"))
    {
        /* In case this was set previously
        */
        options->resolve_ip_http = 0;

        /* use source, resolve, or an actual IP
        */
        if(strcasecmp(val, "source") == 0)
            strlcpy(options->allow_ip_str, "0.0.0.0", 8);
        else if(strcasecmp(val, "resolve") == 0)
            options->resolve_ip_http = 1;
        else /* Assume IP address */
            strlcpy(options->allow_ip_str, val, MAX_IPV4_STR_LEN);
    }
    /* Time Offset */
    else if(CONF_VAR_IS(var, "TIME_OFFSET"))
    {
        if(val[0] == '-')
        {
            val++;
            options->time_offset_minus = parse_time_offset(val);
        }
        else
            options->time_offset_plus = parse_time_offset(val);
    }
    /* Use GPG ? */
    else if(CONF_VAR_IS(var, "USE_GPG"))
    {
        if(val[0] == 'y' || val[0] == 'Y')
            options->use_gpg = 1;
    }
    /* Use GPG Agent ? */
    else if(CONF_VAR_IS(var, "USE_GPG_AGENT"))
    {
        if(val[0] == 'y' || val[0] == 'Y')
            options->use_gpg_agent = 1;
    }
    /* GPG Recipient */
    else if(CONF_VAR_IS(var, "GPG_RECIPIENT"))
    {
        strlcpy(options->gpg_recipient_key, val, MAX_GPG_KEY_ID);
    }
    /* GPG Signer */
    else if(CONF_VAR_IS(var, "GPG_SIGNER"))
    {
        strlcpy(options->gpg_signer_key, val, MAX_GPG_KEY_ID);
    }
    /* GPG Homedir */
    else if(CONF_VAR_IS(var, "GPG_HOMEDIR"))
    {
        strlcpy(options->gpg_home_dir, val, MAX_PATH_LEN);
    }
    /* Spoof User */
    else if(CONF_VAR_IS(var, "SPOOF_USER"))
    {
        strlcpy(options->spoof_user, val, MAX_USERNAME_LEN);
    }
    /* Spoof Source IP */
    else if(CONF_VAR_IS(var, "SPOOF_SOURCE_IP"))
    {
        strlcpy(options->spoof_ip_src_str, val, MAX_IPV4_STR_LEN);
    }
    /* ACCESS request */
    else if(CONF_VAR_IS(var, "ACCESS"))
    {
        strlcpy(options->access_str, val, MAX_LINE_LEN);
    }
    /* SPA Server (destination) */
    else if(CONF_VAR_IS(var, "SPA_SERVER"))
    {
        strlcpy(options->spa_server_str, val, MAX_SERVER_STR_LEN);
    }
    /* Rand port ? */
    else if(CONF_VAR_IS(var, "RAND_PORT"))
    {
        if(val[0] == 'y' || val[0] == 'Y')
            options->rand_port = 1;
    }
    /* Key file */
    else if(CONF_VAR_IS(var, "KEY_FILE"))
    {
        strlcpy(options->get_key_file, val, MAX_PATH_LEN);
    }
    /* NAT Access Request */
    else if(CONF_VAR_IS(var, "NAT_ACCESS"))
    {
        strlcpy(options->nat_access_str, val, MAX_PATH_LEN);
    }
    /* HTTP User Agent */
    else if(CONF_VAR_IS(var, "HTTP_USER_AGENT"))
    {
        strlcpy(options->http_user_agent, val, HTTP_MAX_USER_AGENT_LEN);
    }
    /* Resolve URL */
    else if(CONF_VAR_IS(var, "RESOLVE_URL"))
    {
        if(options->resolve_url != NULL)
            free(options->resolve_url);
        tmpint = strlen(val)+1;
        options->resolve_url = malloc(tmpint);
        if(options->resolve_url == NULL)
        {
            fprintf(stderr, "Memory allocation error for resolve URL.\n");
            exit(EXIT_FAILURE);
        }
        strlcpy(options->resolve_url, val, tmpint);
    }
    /* NAT Local ? */
    else if(CONF_VAR_IS(var, "NAT_LOCAL"))
    {
        if(val[0] == 'y' || val[0] == 'Y')
            options->nat_local = 1;
    }
    /* NAT rand port ? */
    else if(CONF_VAR_IS(var, "NAT_RAND_PORT"))
    {
        if(val[0] == 'y' || val[0] == 'Y')
            options->nat_rand_port = 1;
    }
    /* NAT port */
    else if(CONF_VAR_IS(var, "NAT_PORT"))
    {
        tmpint = atoi(val);
        if(tmpint < 0 || tmpint > MAX_PORT)
            return(-1);
        else
            options->nat_port = tmpint;
    }

    return(0);
}