int process_arguments (int argc, char **argv) {
    int c;
    int option = 0;

    static struct option long_opts[] = {
        MP_LONGOPTS_DEFAULT,
        MP_LONGOPTS_HOST,
        LDNS_LONGOPTS,
        {"domain", required_argument, 0, 'D'},
        {"trace-from", required_argument, 0, 'T'},
        {"trusted-keys", required_argument, 0, 'k'},
        {"resolver", required_argument, 0, 'R'},
        MP_LONGOPTS_END
    };

    if (argc < 2) {
        print_help();
        exit (STATE_OK);
    }

    while (1) {
        c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"H:D:T:k:R:"LDNS_OPTSTR, long_opts, &option);
        if (c == -1 || c == EOF)
            break;

        getopt_ldns(c);

        switch (c) {
            /* Host opt */
            case 'H':
                getopt_host_ip(optarg, &hostname);
                break;
            case 'D':
                if (!is_hostname(optarg))
                    usage("Illegal domain name.");
                domainname = optarg;
                break;
            case 'k':
                trusted_keys = loadKeyfile(optarg);
                if (trusted_keys == NULL)
                    usage("Parsing keyfile failed.");
                break;
            case 'T':
                if (!is_hostname(optarg))
                    usage("Illegal trace domain name.");
                domaintrace = optarg;
                break;
            case 'R':
                getopt_host_ip(optarg, &resolver);
                break;
        }
    }

    /* Check requirements */
    if(!domainname)
        usage("Domain is mandatory");

    return OK;
}
示例#2
0
int
is_host (const char *address)
{
	if (is_addr (address) || is_hostname (address))
		return (TRUE);

	return (FALSE);
}
/**
 * @brief Determines the host type in a buffer.
 *
 * @param[in] str   Buffer that contains host definition, could a be hostname,
 *                  single IPv4 or IPv6, CIDR-expressed block etc,.
 *
 * @return Host_TYPE_*, -1 if error.
 */
static int
determine_host_type (const gchar *str_stripped)
{
  /*
   * We have a single element with no leading or trailing
   * white spaces. This element could represent different host
   * definitions: single IPs, host names, CIDR-expressed blocks,
   * range-expressed networks, IPv6 addresses.
   */

  /* Null or empty string. */
  if (str_stripped == NULL || *str_stripped == '\0')
    return -1;

  /* Check for regular single IPv4 address. */
  if (is_ipv4_address (str_stripped))
    return HOST_TYPE_IPV4;

  /* Check for regular single IPv6 address. */
  if (is_ipv6_address (str_stripped))
    return HOST_TYPE_IPV6;

  /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
  if (is_cidr_block (str_stripped))
    return HOST_TYPE_CIDR_BLOCK;

  /* Check for short range-expressed networks "192.168.12.5-40" */
  if (is_short_range_network (str_stripped))
    return HOST_TYPE_RANGE_SHORT;

  /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
  if (is_long_range_network (str_stripped))
    return HOST_TYPE_RANGE_LONG;

  /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
  if (is_cidr6_block (str_stripped))
    return HOST_TYPE_CIDR6_BLOCK;

  /* Check for short range-expressed networks "::1-ef12" */
  if (is_short_range6_network (str_stripped))
    return HOST_TYPE_RANGE6_SHORT;

  /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
  if (is_long_range6_network (str_stripped))
    return HOST_TYPE_RANGE6_LONG;

  /* Check for hostname. */
  if (is_hostname (str_stripped))
    return HOST_TYPE_NAME;

  /* @todo: If everything else fails, fallback to hostname ? */
  return -1;
}
示例#4
0
static int
matchhostname(const char *hostname1, const char *hostname2)
{
	struct addrinfo *results1 = NULL, *results2 = NULL;
	struct addrinfo *ai1, *ai2;
	int result = 0;

	if (strcasecmp(hostname1, hostname2) == 0)
		return 1;

	/*
	 * Don't pass export wildcards or netgroup names to DNS
	 */
	if (!is_hostname(hostname1) || !is_hostname(hostname2))
		return 0;

	results1 = address_list(hostname1);
	if (results1 == NULL)
		goto out;
	results2 = address_list(hostname2);
	if (results2 == NULL)
		goto out;

	if (strcasecmp(results1->ai_canonname, results2->ai_canonname) == 0) {
		result = 1;
		goto out;
	}

	for (ai1 = results1; ai1 != NULL; ai1 = ai1->ai_next)
		for (ai2 = results2; ai2 != NULL; ai2 = ai2->ai_next)
			if (nfs_compare_sockaddr(ai1->ai_addr, ai2->ai_addr)) {
				result = 1;
				break;
			}

out:
	freeaddrinfo(results1);
	freeaddrinfo(results2);
	return result;
}
示例#5
0
int is_url(const char *url) {
    char *buf, *buf2;
    char *remain = strdup(url);

    /* Schema */
    buf = strsep(&remain, ":");
    if (!remain)
        return FALSE;
    if (!isalpha(*buf))
        return FALSE;
    while(*(++buf)) {
        if (isalnum(*buf) || *buf == '+' || *buf == '-' || *buf == '.')
            continue;
        return FALSE;
    }
    if (*(remain) != '/' || *(remain+1) != '/')
        return FALSE;
    remain+=2;

    /* Authority */
    buf = strsep(&remain, "/?#");

    if (*buf != '\0') {

        /* Authority - Userinfo */
        if (strstr(buf, "@")) {
            buf2 = strsep(&buf, "@");

            do {
                if (isalnum(*buf2) || *buf2 == '+' || *buf2 == '-' || *buf2 == '.' ||
                        *buf2 == '~' || *buf2 == '!' || *buf2 == '$' || *buf2 == '&' ||
                        *buf2 == '\'' || *buf2 == '(' || *buf2 == ')' || *buf2 == '*' ||
                        *buf2 == ',' || *buf2 == ';' || *buf2 == '=' || *buf2 == ':')
                    continue;
                if (*buf2 == '%' && isxdigit(*(buf2+1)) && isxdigit(*(buf2+2))) {
                    buf2+=2;
                    continue;
                }
                return FALSE;
            } while(*(++buf2));
        }

        /* Authority - Host */
        if (*buf == '[') {
            buf++;
            buf2 = strsep(&buf, "]");
            if(*buf2 == ':')
                buf2++;
            if (!buf)
                return FALSE;
            do {
                if (isxdigit(*buf2) || *buf2 == ':')
                    continue;
                return FALSE;
            } while(*(++buf2));
        } else if (isdigit(*buf)) {
            buf2 = strsep(&buf, ":");
            if (!is_hostaddr(buf2))
                return FALSE;
        } else {
            buf2 = strsep(&buf, ":");
            if (!is_hostname(buf2))
                return FALSE;
        }

        /* Authority - Port */
        if (buf && *buf != '\0') {
            do {
                if (isdigit(*buf))
                    continue;
                return FALSE;
            } while(*(++buf));
        }

    }

    if (!remain || *remain == '\0')
        return TRUE;

    /* Path */
    buf = strsep(&remain, "?#");
    while(1) {
        if (*buf == '\0')
            break;
        if (isalnum(*buf) || *buf == '+' || *buf == '-' || *buf == '.' ||
                *buf == '~' || *buf == '!' || *buf == '$' || *buf == '&' ||
                *buf == '\'' || *buf == '(' || *buf == ')' || *buf == '*' ||
                *buf == ',' || *buf == ';' || *buf == '=' || *buf == ':' ||
                *buf == '@' || *buf == '/') {
            buf++;
            continue;
        }
        if (*buf == '%' && isxdigit(*(buf+1)) && isxdigit(*(buf+2))) {
            buf+=3;
            continue;
        }
        return FALSE;
    }

    if (!remain || *remain == '\0')
        return TRUE;

    /* Query & Fragment */
    buf = remain;
    while(1) {
        if (*buf == '\0')
            break;
        if (isalnum(*buf) || *buf == '+' || *buf == '-' || *buf == '.' ||
                *buf == '~' || *buf == '!' || *buf == '$' || *buf == '&' ||
                *buf == '\'' || *buf == '(' || *buf == ')' || *buf == '*' ||
                *buf == ',' || *buf == ';' || *buf == '=' || *buf == ':' ||
                *buf == '@' || *buf == '/' || *buf == '?' || *buf == '#') {
            buf++;
            continue;
        }
        if (*buf == '%' && isxdigit(*(buf+1)) && isxdigit(*(buf+2))) {
            buf+=3;
            continue;
        }
        return FALSE;
    }

    return TRUE;
}
示例#6
0
void
host_or_die(const char *str)
{
	if(!str || (!is_addr(str) && !is_hostname(str)))
		usage_va(_("Invalid hostname/address - %s"), str);
}