| 1 | /* |
| 2 | * adh-query.c |
| 3 | * - useful general-purpose resolver client program |
| 4 | * make queries and print answers |
| 5 | */ |
| 6 | /* |
| 7 | * This file is |
| 8 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
| 9 | * |
| 10 | * It is part of adns, which is |
| 11 | * Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
| 12 | * Copyright (C) 1999 Tony Finch <dot@dotat.at> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | #include "adnshost.h" |
| 30 | |
| 31 | adns_state ads; |
| 32 | struct outstanding_list outstanding; |
| 33 | |
| 34 | static unsigned long idcounter; |
| 35 | |
| 36 | void ensure_adns_init(void) { |
| 37 | int r; |
| 38 | |
| 39 | if (ads) return; |
| 40 | |
| 41 | if (signal(SIGPIPE,SIG_IGN) == SIG_ERR) sysfail("ignore SIGPIPE",errno); |
| 42 | r= adns_init(&ads, |
| 43 | adns_if_noautosys|adns_if_nosigpipe | |
| 44 | (ov_env ? 0 : adns_if_noenv) | |
| 45 | ov_verbose, |
| 46 | 0); |
| 47 | if (r) sysfail("adns_init",r); |
| 48 | } |
| 49 | |
| 50 | void query_do(const char *domain) { |
| 51 | struct query_node *qun; |
| 52 | char idbuf[20]; |
| 53 | int r; |
| 54 | |
| 55 | qun= malloc(sizeof(*qun)); |
| 56 | qun->pqfr= ov_pqfr; |
| 57 | if (ov_id) { |
| 58 | qun->id= xstrsave(ov_id); |
| 59 | } else { |
| 60 | sprintf(idbuf,"%lu",idcounter++); |
| 61 | idcounter &= 0x0fffffffflu; |
| 62 | qun->id= xstrsave(idbuf); |
| 63 | } |
| 64 | |
| 65 | r= adns_submit(ads, domain, |
| 66 | ov_type == adns_r_none ? adns_r_addr : ov_type, |
| 67 | (ov_search ? adns_qf_search : 0) | |
| 68 | (ov_tcp ? adns_qf_usevc : 0) | |
| 69 | (ov_pqfr.show_owner ? adns_qf_owner : 0) | |
| 70 | (ov_qc_query ? adns_qf_quoteok_query : 0) | |
| 71 | (ov_qc_anshost ? adns_qf_quoteok_anshost : 0) | |
| 72 | (ov_qc_cname ? 0 : adns_qf_quoteok_cname) | |
| 73 | ov_cname, |
| 74 | qun, |
| 75 | &qun->qu); |
| 76 | if (r) sysfail("adns_submit",r); |
| 77 | |
| 78 | LIST_LINK_TAIL(outstanding,qun); |
| 79 | } |
| 80 | |
| 81 | static void dequeue_query(struct query_node *qun) { |
| 82 | LIST_UNLINK(outstanding,qun); |
| 83 | free(qun->id); |
| 84 | free(qun); |
| 85 | } |
| 86 | |
| 87 | static void print_withspace(const char *str) { |
| 88 | if (printf("%s ", str) == EOF) outerr(); |
| 89 | } |
| 90 | |
| 91 | static void print_ttl(struct query_node *qun, adns_answer *answer) { |
| 92 | unsigned long ttl; |
| 93 | time_t now; |
| 94 | |
| 95 | switch (qun->pqfr.ttl) { |
| 96 | case tm_none: |
| 97 | return; |
| 98 | case tm_rel: |
| 99 | if (time(&now) == (time_t)-1) sysfail("get current time",errno); |
| 100 | ttl= answer->expires < now ? 0 : answer->expires - now; |
| 101 | break; |
| 102 | case tm_abs: |
| 103 | ttl= answer->expires; |
| 104 | break; |
| 105 | default: |
| 106 | abort(); |
| 107 | } |
| 108 | if (printf("%lu ",ttl) == EOF) outerr(); |
| 109 | } |
| 110 | |
| 111 | static void print_owner_ttl(struct query_node *qun, adns_answer *answer) { |
| 112 | if (qun->pqfr.show_owner) print_withspace(answer->owner); |
| 113 | print_ttl(qun,answer); |
| 114 | } |
| 115 | |
| 116 | static void print_status(adns_status st, struct query_node *qun, adns_answer *answer) { |
| 117 | static const adns_status statuspoints[]= { |
| 118 | adns_s_ok, |
| 119 | adns_s_max_localfail, adns_s_max_remotefail, adns_s_max_tempfail, |
| 120 | adns_s_max_misconfig, adns_s_max_misquery |
| 121 | }; |
| 122 | |
| 123 | const adns_status *spp; |
| 124 | const char *statustypeabbrev, *statusabbrev, *statusstring; |
| 125 | int minrcode; |
| 126 | |
| 127 | statustypeabbrev= adns_errtypeabbrev(st); |
| 128 | for (minrcode=0, spp=statuspoints; |
| 129 | spp < statuspoints + (sizeof(statuspoints)/sizeof(statuspoints[0])); |
| 130 | spp++) |
| 131 | if (st > *spp) minrcode++; |
| 132 | if (rcode < minrcode) rcode= minrcode; |
| 133 | |
| 134 | statusabbrev= adns_errabbrev(st); |
| 135 | statusstring= adns_strerror(st); |
| 136 | assert(!strchr(statusstring,'"')); |
| 137 | |
| 138 | if (printf("%s %d %s ", statustypeabbrev, st, statusabbrev) |
| 139 | == EOF) outerr(); |
| 140 | print_owner_ttl(qun,answer); |
| 141 | if (qun->pqfr.show_cname) |
| 142 | print_withspace(answer->cname ? answer->cname : "$"); |
| 143 | if (printf("\"%s\"\n", statusstring) == EOF) outerr(); |
| 144 | } |
| 145 | |
| 146 | void query_done(struct query_node *qun, adns_answer *answer) { |
| 147 | adns_status st, ist; |
| 148 | int rrn, nrrs; |
| 149 | const char *rrp, *realowner, *typename; |
| 150 | char *datastr; |
| 151 | |
| 152 | st= answer->status; |
| 153 | nrrs= answer->nrrs; |
| 154 | if (ov_asynch) { |
| 155 | if (printf("%s %d ", qun->id, nrrs) == EOF) outerr(); |
| 156 | print_status(st,qun,answer); |
| 157 | } else { |
| 158 | if (st) { |
| 159 | if (fputs("; failed ",stdout) == EOF) outerr(); |
| 160 | print_status(st,qun,answer); |
| 161 | } else if (qun->pqfr.show_cname && answer->cname) { |
| 162 | print_owner_ttl(qun,answer); |
| 163 | if (printf("CNAME %s\n",answer->cname) == EOF) outerr(); |
| 164 | } |
| 165 | } |
| 166 | if (qun->pqfr.show_owner) { |
| 167 | realowner= answer->cname ? answer->cname : answer->owner;; |
| 168 | assert(realowner); |
| 169 | } else { |
| 170 | realowner= 0; |
| 171 | } |
| 172 | if (nrrs) { |
| 173 | for (rrn=0, rrp = answer->rrs.untyped; |
| 174 | rrn < nrrs; |
| 175 | rrn++, rrp += answer->rrsz) { |
| 176 | if (realowner) print_withspace(realowner); |
| 177 | print_ttl(qun,answer); |
| 178 | ist= adns_rr_info(answer->type, &typename, 0, 0, rrp, &datastr); |
| 179 | if (ist == adns_s_nomemory) sysfail("adns_rr_info failed",ENOMEM); |
| 180 | assert(!ist); |
| 181 | if (qun->pqfr.show_type) print_withspace(typename); |
| 182 | if (printf("%s\n",datastr) == EOF) outerr(); |
| 183 | free(datastr); |
| 184 | } |
| 185 | } |
| 186 | if (fflush(stdout)) outerr(); |
| 187 | free(answer); |
| 188 | dequeue_query(qun); |
| 189 | } |
| 190 | |
| 191 | void of_asynch_id(const struct optioninfo *oi, const char *arg) { |
| 192 | free(ov_id); |
| 193 | ov_id= xstrsave(arg); |
| 194 | } |
| 195 | |
| 196 | void of_cancel_id(const struct optioninfo *oi, const char *arg) { |
| 197 | struct query_node *qun; |
| 198 | |
| 199 | for (qun= outstanding.head; |
| 200 | qun && !strcmp(qun->id,arg); |
| 201 | qun= qun->next); |
| 202 | if (!qun) return; |
| 203 | adns_cancel(qun->qu); |
| 204 | dequeue_query(qun); |
| 205 | } |