Пример #1
0
char *
get_ipa_config(
    void)
{
    skstream_t *conf_stream = NULL;
    char filename[PATH_MAX];
    char line[IPA_CONFIG_LINE_LENGTH];
    char *ipa_url = NULL;
    int rv;

    /* Read in the data file */
    if (NULL == skFindFile(IPA_CONFIG_FILE, filename, sizeof(filename), 1)) {
        skAppPrintErr("Could not locate config file '%s'.",
                      IPA_CONFIG_FILE);
        return NULL;
    }

    /* open input */
    if ((rv = skStreamCreate(&conf_stream, SK_IO_READ, SK_CONTENT_TEXT))
        || (rv = skStreamBind(conf_stream, filename))
        || (rv = skStreamSetCommentStart(conf_stream, "#"))
        || (rv = skStreamOpen(conf_stream)))
    {
        skStreamPrintLastErr(conf_stream, rv, &skAppPrintErr);
        skStreamDestroy(&conf_stream);
        exit(EXIT_FAILURE);
    }
    while (skStreamGetLine(conf_stream, line, sizeof(line), NULL)
           == SKSTREAM_OK)
    {
        /* FIXME: smarter config file reading, please */
        if (strlen(line) > 0) {
            ipa_url = strdup(line);
            break;
        }
    }

    skStreamDestroy(&conf_stream);
    /* Should be free()d by the caller */
    return ipa_url;
}
Пример #2
0
/*
 *  buildIPSetWildcards(stream);
 *
 *    Read IP addresses from the stream named by 'stream' and use them
 *    to build the global ipset.  Allow the input to contain
 *    IPWildcards.  Return 0 on success or -1 on failure.
 */
static int
buildIPSetWildcards(
    skstream_t         *stream)
{
#if SK_ENABLE_IPV6
    int saw_integer = 0;
#endif
    int lc = 0;
    char line_buf[512];
    skIPWildcard_t ipwild;
    skipaddr_t ip;
    uint32_t prefix;
    char *cp;
    int rv;

    /* read until end of file */
    while ((rv = skStreamGetLine(stream, line_buf, sizeof(line_buf), &lc))
           != SKSTREAM_ERR_EOF)
    {
        switch (rv) {
          case SKSTREAM_OK:
            /* good, we got our line */
            break;
          case SKSTREAM_ERR_LONG_LINE:
            /* bad: line was longer than sizeof(line_buf) */
            skAppPrintErr("Input line %d too long. ignored",
                          lc);
            continue;
          default:
            /* unexpected error */
            skStreamPrintLastErr(stream, rv, &skAppPrintErr);
            goto END;
        }

        /* first, attempt to parse as a CIDR block */
        rv = skStringParseCIDR(&ip, &prefix, line_buf);
        if (rv == 0) {
#if SK_ENABLE_IPV6
            /* do not allow integers mixed with IPv6 addresses */
            if (saw_integer) {
                if (skipaddrIsV6(&ip)) {
                    skAppPrintErr("Error on line %d: %s",
                                  lc, SETBUILD_ERR_MIX_INT_V6);
                    rv = -1;
                    goto END;
                }
            } else if (SETBUILD_BUF_IS_INT(line_buf)) {
                saw_integer = 1;
                if (skIPSetIsV6(ipset)) {
                    skAppPrintErr("Error on line %d: %s",
                                  lc, SETBUILD_ERR_MIX_INT_V6);
                    rv = -1;
                    goto END;
                }
            }
#endif  /* SK_ENABLE_IPV6 */

            rv = skIPSetInsertAddress(ipset, &ip, prefix);
            if (rv) {
                skAppPrintErr("Error adding IP on line %d to IPset: %s",
                              lc, skIPSetStrerror(rv));
                goto END;
            }
            continue;
        }

        /* else parse the line as an IPWildcard */
        rv = skStringParseIPWildcard(&ipwild, line_buf);
        if (rv != 0) {
            /* failed to parse an IPWildcard.  See if the user has
             * entered two IP addresses, and if so, suggest they use
             * the --ip-ranges switch. */
            int rv2;
            rv2 = skStringParseIP(&ip, line_buf);
            if (rv2 > 0) {
                /* parsed an IP and there is extra text after the IP
                 * address; check to see if it is another IP addr */
#if SK_ENABLE_IPV6
                if (skipaddrIsV6(&ip)
                    && ((cp = strchr(line_buf + rv2, ':')) != NULL))
                {
                    while (isxdigit((int) *(cp - 1))) {
                        --cp;
                    }
                    if (skStringParseIP(&ip, cp) == 0) {
                        skAppPrintErr(("Invalid IP on line %d: "
                                       SUGGEST_IP_RANGES),
                                      lc);
                        goto END;
                    }
                }
#endif
                if (!skipaddrIsV6(&ip)
                    && ((cp = strchr(line_buf + rv2, '.')) != NULL))
                {
                    while (isxdigit((int) *(cp - 1))) {
                        --cp;
                    }
                    if (skStringParseIP(&ip, cp) == 0) {
                        skAppPrintErr(("Invalid IP on line %d: "
                                       SUGGEST_IP_RANGES),
                                      lc);
                        goto END;
                    }
                }
            }
            /* report initial error */
            skAppPrintErr("Invalid IP Wildcard on line %d: %s",
                          lc, skStringParseStrerror(rv));
            goto END;
        }

#if SK_ENABLE_IPV6
        /* do not allow integers mixed with IPv6 addresses */
        if (saw_integer && skIPWildcardIsV6(&ipwild)) {
            skAppPrintErr("Error on line %d: %s",
                          lc, SETBUILD_ERR_MIX_INT_V6);
            rv = -1;
            goto END;
        }
#endif  /* SK_ENABLE_IPV6 */

        rv = skIPSetInsertIPWildcard(ipset, &ipwild);
        if (rv) {
            skAppPrintErr("Error adding IP Wildcard on line %d to IPset: %s",
                          lc, skIPSetStrerror(rv));
            goto END;
        }
    }

    /* success */
    rv = 0;

  END:
    if (rv != 0) {
        return -1;
    }
    return 0;
}
Пример #3
0
/*
 *  buildIPSetRanges(stream);
 *
 *    Read IP addresses from the stream named by 'stream' and use them
 *    to build the global ipset.  Allow the input to support ranges of
 *    IPs.  Return 0 on success or -1 on failure.
 */
static int
buildIPSetRanges(
    skstream_t         *stream)
{
#if SK_ENABLE_IPV6
    int saw_integer = 0;
#endif
    int lc = 0;
    char line_buf[512];
    char *sep;
    skipaddr_t ip;
    skipaddr_t ip_min;
    skipaddr_t ip_max;
    uint32_t prefix;
    const int delim_is_space = isspace((int)delimiter);
    int rv;

    /* read until end of file */
    while ((rv = skStreamGetLine(stream, line_buf, sizeof(line_buf), &lc))
           != SKSTREAM_ERR_EOF)
    {
        switch (rv) {
          case SKSTREAM_OK:
            /* good, we got our line */
            break;
          case SKSTREAM_ERR_LONG_LINE:
            /* bad: line was longer than sizeof(line_buf) */
            skAppPrintErr("Input line %d too long. ignored",
                          lc);
            continue;
          default:
            /* unexpected error */
            skStreamPrintLastErr(stream, rv, &skAppPrintErr);
            goto END;
        }

        /* support whitespace separators */
        if (!delim_is_space) {
            sep = strchr(line_buf, delimiter);
        } else {
            /* ignore leading whitespace */
            sep = line_buf;
            while (isspace((int)*sep)) {
                ++sep;
            }
            sep = strchr(sep, delimiter);
            if (sep) {
                /* allow a lone IP to have trailing whitespace */
                char *cp = sep;
                while (isspace((int)*cp)) {
                    ++cp;
                }
                if (*cp == '\0') {
                    sep = NULL;
                }
            }
        }
        if (sep == NULL) {
            /* parse as IP with possible CIDR designation */
            rv = skStringParseCIDR(&ip, &prefix, line_buf);
            if (rv != 0) {
                skAppPrintErr("Invalid IP on line %d: %s",
                              lc, skStringParseStrerror(rv));
                goto END;
            }

#if SK_ENABLE_IPV6
            /* do not allow integers mixed with IPv6 addresses */
            if (saw_integer) {
                if (skipaddrIsV6(&ip)) {
                    skAppPrintErr("Error on line %d: %s",
                                  lc, SETBUILD_ERR_MIX_INT_V6);
                    rv = -1;
                    goto END;
                }
            } else if (SETBUILD_BUF_IS_INT(line_buf)) {
                saw_integer = 1;
                if (skIPSetIsV6(ipset)) {
                    skAppPrintErr("Error on line %d: %s",
                                  lc, SETBUILD_ERR_MIX_INT_V6);
                    rv = -1;
                    goto END;
                }
            }
#endif  /* SK_ENABLE_IPV6 */

            rv = skIPSetInsertAddress(ipset, &ip, prefix);
            if (rv) {
                skAppPrintErr("Error adding IP on line %d to IPset: %s",
                              lc, skIPSetStrerror(rv));
                goto END;
            }
            continue;
        }

        /* parse two IP addresses */
        *sep = '\0';
        ++sep;
        rv = skStringParseIP(&ip_min, line_buf);
        if (rv != 0) {
            skAppPrintErr("Invalid minimum IP on line %d: %s",
                          lc, skStringParseStrerror(rv));
            goto END;
        }
        rv = skStringParseIP(&ip_max, sep);
        if (rv != 0) {
            skAppPrintErr("Invalid maximum IP on line %d: %s",
                          lc, skStringParseStrerror(rv));
            goto END;
        }

        if (skipaddrCompare(&ip_min, &ip_max) > 0) {
            skAppPrintErr("Invalid IP range on line %d: min > max",
                          lc);
            rv = -1;
            goto END;
        }

#if SK_ENABLE_IPV6
        /* do not allow integers mixed with IPv6 addresses */
        if (saw_integer) {
            if (skipaddrIsV6(&ip_min) || skipaddrIsV6(&ip_max)) {
                skAppPrintErr("Error on line %d: %s",
                              lc, SETBUILD_ERR_MIX_INT_V6);
                rv = -1;
                goto END;
            }
        } else if (SETBUILD_BUF_IS_INT(line_buf) || SETBUILD_BUF_IS_INT(sep)) {
            saw_integer = 1;
            if (skIPSetIsV6(ipset)) {
                skAppPrintErr("Error on line %d: %s",
                              lc, SETBUILD_ERR_MIX_INT_V6);
                rv = -1;
                goto END;
            }
        }
#endif  /* SK_ENABLE_IPV6 */

        rv = skIPSetInsertRange(ipset, &ip_min, &ip_max);
        if (rv) {
            skAppPrintErr("Error adding IP range on line %d to IPset: %s",
                          lc, skIPSetStrerror(rv));
            goto END;
        }
    }

    /* success */
    rv = 0;

  END:
    if (rv != 0) {
        return -1;
    }
    return 0;
}
Пример #4
0
/*
 *  status = processInputFile(filein);
 *
 *    For every line in 'filein', look up the address in Country Code
 *    map and print out the corresponding country code.  There should
 *    be as many lines of output as there are of input.
 */
static int
processInputFile(
    const char         *f_name)
{
    char final_delim[] = {'\0', '\0'};
    char line[2048];
    skstream_t *stream = NULL;
    skIPWildcardIterator_t iter;
    skIPWildcard_t ipwild;
    skipaddr_t ip;
    int retval = 1;
    int rv;
    int lc = 0;
    char cc[32];
    char ipbuf[SK_NUM2DOT_STRLEN];

    if (!app_opt.no_final_delimiter) {
        final_delim[0] = app_opt.column_separator;
    }

    /* open input */
    if ((rv = skStreamCreate(&stream, SK_IO_READ, SK_CONTENT_TEXT))
        || (rv = skStreamBind(stream, f_name))
        || (rv = skStreamSetCommentStart(stream, "#"))
        || (rv = skStreamOpen(stream)))
    {
        skStreamPrintLastErr(stream, rv, &skAppPrintErr);
        goto END;
    }

    /* read until end of file */
    while ((rv = skStreamGetLine(stream, line, sizeof(line), &lc))
           != SKSTREAM_ERR_EOF)
    {
        switch (rv) {
          case SKSTREAM_OK:
            /* good, we got our line */
            break;
          case SKSTREAM_ERR_LONG_LINE:
            /* bad: line was longer than sizeof(line) */
            skAppPrintErr("Input line %d too long. ignored",
                          lc);
            continue;
          default:
            /* unexpected error */
            skStreamPrintLastErr(stream, rv, &skAppPrintErr);
            goto END;
        }

        /* parse the line: fill in octet_bitmap */
        rv = skStringParseIPWildcard(&ipwild, line);
        if (rv && rv != SKUTILS_ERR_EMPTY) {
            /* error */
            skAppPrintErr("Error on line %d: %s\n",
                          lc, skStringParseStrerror(rv));
            goto END;
        }
#if SK_ENABLE_IPV6
        if (skIPWildcardIsV6(&ipwild)) {
            continue;
        }
#endif /* SK_ENABLE_IPV6 */

        skIPWildcardIteratorBind(&iter, &ipwild);
        while (skIPWildcardIteratorNext(&iter, &ip) == SK_ITERATOR_OK) {

            skCountryLookupName(&ip, cc, sizeof(cc));

            if (!app_opt.print_ips) {
                skStreamPrint(out, "%s\n", cc);
            } else {
                skipaddrString(ipbuf, &ip, ip_flags);
                if (app_opt.no_columns) {
                    skStreamPrint(out, "%s%c%s%s\n",
                                  ipbuf, app_opt.column_separator,
                                  cc, final_delim);
                } else {
                    skStreamPrint(out, "%15s%c%2s%s\n",
                                  ipbuf, app_opt.column_separator,
                                  cc, final_delim);
                }
            }
        }
    }

    retval = 0;

  END:
    skStreamDestroy(&stream);
    return retval;
}