Ejemplo n.º 1
0
int
main(int argc, char **argv)
{
	getdns_dict *response = NULL;
	char *response_str;
	getdns_return_t r;
	getdns_dict *address = NULL;
	FILE *fp = NULL;

	name = the_root;
	if ((r = getdns_context_create(&context, 1))) {
		fprintf(stderr, "Create context failed: %d\n", r);
		return r;
	}
	extensions = getdns_dict_create();
	if (! extensions) {
		fprintf(stderr, "Could not create extensions dict\n");
		r = GETDNS_RETURN_MEMORY_ERROR;
		goto done_destroy_context;
	}
	if ((r = parse_args(argc, argv)))
		goto done_destroy_context;

	if (query_file) {
		fp = fopen(query_file, "rt");
		if (fp == NULL) {
			fprintf(stderr, "Could not open query file: %s\n", query_file);
			goto done_destroy_context;
		}
	}

	/* Make the call */
	do {
		char line[1024], *token, *linev[256];
		int linec;
		if (interactive) {
			if (!query_file) {
				fprintf(stdout, "> ");
				if (!fgets(line, 1024, stdin) || !*line)
					break;
			} else {
				if (!fgets(line, 1024, fp) || !*line)
					break;
				fprintf(stdout,"Found query: %s", line);
			}

			linev[0] = argv[0];
			linec = 1;
			if ( ! (token = strtok(line, " \t\f\n\r")))
				continue;
			do linev[linec++] = token;
			while (linec < 256 &&
			    (token = strtok(NULL, " \t\f\n\r")));
			if ((r = parse_args(linec, linev))) {
				if (r == CONTINUE)
					continue;
				else
					goto done_destroy_context;
			}

		}
		if (calltype == HOSTNAME &&
		    !(address = ipaddr_dict(context, name))) {
			fprintf(stderr, "Could not convert \"%s\" "
			                "to an IP address", name);
			continue;
		}
		if (async) {
			switch (calltype) {
			case GENERAL:
				r = getdns_general(context, name, request_type,
				    extensions, &response, NULL, callback);
				break;
			case ADDRESS:
				r = getdns_address(context, name,
				    extensions, &response, NULL, callback);
				break;
			case HOSTNAME:
				r = getdns_hostname(context, address,
				    extensions, &response, NULL, callback);
				break;
			case SERVICE:
				r = getdns_service(context, name,
				    extensions, &response, NULL, callback);
				break;
			default:
				r = GETDNS_RETURN_GENERIC_ERROR;
				break;
			}
			if (r)
				goto done_destroy_extensions;
			if (!batch_mode) 
				getdns_context_run(context);
		} else {
			switch (calltype) {
			case GENERAL:
				r = getdns_general_sync(context, name,
				    request_type, extensions, &response);
				break;
			case ADDRESS:
				r = getdns_address_sync(context, name,
				    extensions, &response);
				break;
			case HOSTNAME:
				r = getdns_hostname_sync(context, address,
				    extensions, &response);
				break;
			case SERVICE:
				r = getdns_service_sync(context, name,
				    extensions, &response);
				break;
			default:
				r = GETDNS_RETURN_GENERIC_ERROR;
				break;
			}
			if (r)
				goto done_destroy_extensions;
			if (!quiet) {
				if ((response_str = json ?
				    getdns_print_json_dict(response, json == 1)
				  : getdns_pretty_print_dict(response))) {

					fprintf( stdout, "SYNC response:\n%s\n"
					       , response_str);
					free(response_str);
				} else {
					r = GETDNS_RETURN_MEMORY_ERROR;
					fprintf( stderr
					       , "Could not print response\n");
				}
			} else if (r == GETDNS_RETURN_GOOD)
				fprintf(stdout, "Response code was: GOOD\n");
			else if (interactive)
				fprintf(stderr, "An error occurred: %d\n", r);
		}
	} while (interactive);

	if (batch_mode) 
		getdns_context_run(context);

	/* Clean up */
done_destroy_extensions:
	getdns_dict_destroy(extensions);
done_destroy_context:
	if (response) getdns_dict_destroy(response);
	getdns_context_destroy(context);

	if (fp)
		fclose(fp);

	if (r == CONTINUE)
		return 0;
	if (r)
		fprintf(stderr, "An error occurred: %d\n", r);
	return r;
}
Ejemplo n.º 2
0
/*---------------------------------------- getkeyviadane
  fetch the smime/a key identified by the encoded keyid and host name
  populate *certtxt with the key record, caller must free certtxt
*/
void
getkeyviadane(char *dname, int rrtype, char **certtxt)
{
    int      i;
    uint32_t status;
    size_t   nans;
    size_t   numrrs;
    int      rrnum;
    char     getdnserr[MAX_ERROR_STRING+1];
    uint32_t recrrtype;
    getdns_return_t getdnsret;
    getdns_context  *getdnsctx = NULL;
    getdns_dict     *getdnsrsp = NULL;
    getdns_dict     *dnsrec    = NULL;
    getdns_dict     *rr        = NULL;
    getdns_dict     *rrdata    = NULL;
    getdns_list     *dnsreplytree = NULL;
    getdns_list     *dnsans    = NULL;
    getdns_bindata  *rawrdata  = NULL;

    *certtxt = NULL;

    // create the context for DNS resolution using local OS system settings

    getdnsret = getdns_context_create(&getdnsctx, 1);
    if(getdnsret != GETDNS_RETURN_GOOD)
    {
        getdns_strerror(getdnsret, getdnserr, MAX_ERROR_STRING);
        fprintf(stderr, "error creating getdns context, %d, %s\n"
         , getdnsret, getdnserr);
        return;
    }

    // getdns_context_set_resolution_type(getdnsctx, GETDNS_RESOLUTION_STUB);

    // perform the DNS resolution request

    getdnsret = getdns_general_sync(getdnsctx, dname, rrtype, NULL, &getdnsrsp);
    if(getdnsret != GETDNS_RETURN_GOOD)
    {
        getdns_strerror(getdnsret, getdnserr, MAX_ERROR_STRING);
        fprintf(stderr, "DNS request failed, %d, %s\n", getdnsret, getdnserr);

        getdns_dict_destroy(getdnsrsp);
        getdns_context_destroy(getdnsctx);

        return;
    }

    // sanity check the result of the query

    getdnsret = getdns_dict_get_int(getdnsrsp, (char *) "status", &status);
    if(getdnsret != GETDNS_RETURN_GOOD || status != GETDNS_RESPSTATUS_GOOD)
    {
        fprintf(stderr, "DNS request did not return results\n");

        getdns_dict_destroy(getdnsrsp);
        getdns_context_destroy(getdnsctx);

        return;
    }

    getdnsret = getdns_dict_get_list(getdnsrsp, (char *)"replies_tree", &dnsreplytree);
    if(getdnsret != GETDNS_RETURN_GOOD)
    {
        fprintf(stderr, "DNS reply tree empty\n");

        getdns_dict_destroy(getdnsrsp);
        getdns_context_destroy(getdnsctx);

        return;
    }

    nans = 0;
    getdns_list_get_length(dnsreplytree, &nans); 
    for(i=0; i<nans && *certtxt == NULL; i++)
    {
        // extract a record from the reply tree, extract answer from that record

        getdns_list_get_dict(dnsreplytree, i, &dnsrec);

        getdnsret = getdns_dict_get_list(dnsrec, (char *)"answer", &dnsans);
        if(getdnsret != GETDNS_RETURN_GOOD)
        {
            fprintf(stderr, "answer missing from DNS reply tree, exiting\n");
            exit(1);
        }

        // walk the RRs in the DNS answer

        getdns_list_get_length(dnsans, &numrrs);
        for(rrnum=0; rrnum < numrrs && *certtxt == NULL; rrnum++)
        {
            getdns_list_get_dict(dnsans, rrnum, &rr);
            recrrtype = 0;
            getdns_dict_get_int(rr, (char *)"type", &recrrtype);
            if(recrrtype == rrtype)
            {
                getdns_dict_get_dict(rr, (char *)"rdata", &rrdata);
                getdnsret = getdns_dict_get_bindata(rrdata, (char *)"rdata_raw"
                 , &rawrdata);
                if(getdnsret != GETDNS_RETURN_GOOD)
                {
                    fprintf(stderr, "error, rdata missing address\n");
                }
                else
                {
                    *certtxt = (char *) malloc(rawrdata->size + 1);
                    memcpy(*certtxt, rawrdata->data, rawrdata->size);
                    *certtxt[rawrdata->size] = '\0';
                }
            }
        } // for rrnum
    } // for i in nans

    getdns_dict_destroy(getdnsrsp);
    getdns_context_destroy(getdnsctx);

    return;
} // getkeyviadane