| 1 | /* |
| 2 | * adh-main.c |
| 3 | * - useful general-purpose resolver client program |
| 4 | * main program and useful subroutines |
| 5 | */ |
| 6 | /* |
| 7 | * This file is part of adns, which is |
| 8 | * Copyright (C) 1997-2000,2003,2006 Ian Jackson |
| 9 | * Copyright (C) 1999-2000,2003,2006 Tony Finch |
| 10 | * Copyright (C) 1991 Massachusetts Institute of Technology |
| 11 | * (See the file INSTALL for full details.) |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2, or (at your option) |
| 16 | * any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software Foundation, |
| 25 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #include "adnshost.h" |
| 29 | |
| 30 | int rcode; |
| 31 | const char *config_text; |
| 32 | |
| 33 | static int used, avail; |
| 34 | static char *buf; |
| 35 | |
| 36 | void quitnow(int rc) { |
| 37 | if (ads) adns_finish(ads); |
| 38 | free(buf); |
| 39 | free(ov_id); |
| 40 | exit(rc); |
| 41 | } |
| 42 | |
| 43 | void sysfail(const char *what, int errnoval) { |
| 44 | fprintf(stderr,"adnshost failed: %s: %s\n",what,strerror(errnoval)); |
| 45 | quitnow(10); |
| 46 | } |
| 47 | |
| 48 | void usageerr(const char *fmt, ...) { |
| 49 | va_list al; |
| 50 | fputs("adnshost usage error: ",stderr); |
| 51 | va_start(al,fmt); |
| 52 | vfprintf(stderr,fmt,al); |
| 53 | va_end(al); |
| 54 | putc('\n',stderr); |
| 55 | quitnow(11); |
| 56 | } |
| 57 | |
| 58 | void outerr(void) { |
| 59 | sysfail("write to stdout",errno); |
| 60 | } |
| 61 | |
| 62 | void *xmalloc(size_t sz) { |
| 63 | void *p; |
| 64 | |
| 65 | p= malloc(sz); if (!p) sysfail("malloc",sz); |
| 66 | return p; |
| 67 | } |
| 68 | |
| 69 | char *xstrsave(const char *str) { |
| 70 | char *p; |
| 71 | |
| 72 | p= xmalloc(strlen(str)+1); |
| 73 | strcpy(p,str); |
| 74 | return p; |
| 75 | } |
| 76 | |
| 77 | void of_config(const struct optioninfo *oi, const char *arg, const char *arg2) { |
| 78 | config_text= arg; |
| 79 | } |
| 80 | |
| 81 | void of_type(const struct optioninfo *oi, const char *arg, const char *arg2) { |
| 82 | static const struct typename { |
| 83 | adns_rrtype type; |
| 84 | const char *desc; |
| 85 | } typenames[]= { |
| 86 | /* enhanced versions */ |
| 87 | { adns_r_ns, "ns" }, |
| 88 | { adns_r_soa, "soa" }, |
| 89 | { adns_r_ptr, "ptr" }, |
| 90 | { adns_r_mx, "mx" }, |
| 91 | { adns_r_rp, "rp" }, |
| 92 | { adns_r_srv, "srv" }, |
| 93 | { adns_r_addr, "addr" }, |
| 94 | |
| 95 | /* types with only one version */ |
| 96 | { adns_r_cname, "cname" }, |
| 97 | { adns_r_hinfo, "hinfo" }, |
| 98 | { adns_r_txt, "txt" }, |
| 99 | |
| 100 | /* raw versions */ |
| 101 | { adns_r_a, "a" }, |
| 102 | { adns_r_ns_raw, "ns-" }, |
| 103 | { adns_r_soa_raw, "soa-" }, |
| 104 | { adns_r_ptr_raw, "ptr-" }, |
| 105 | { adns_r_mx_raw, "mx-" }, |
| 106 | { adns_r_rp_raw, "rp-" }, |
| 107 | { adns_r_srv_raw, "srv-" }, |
| 108 | |
| 109 | { adns_r_none, 0 } |
| 110 | }; |
| 111 | |
| 112 | const struct typename *tnp; |
| 113 | unsigned long unknowntype; |
| 114 | char *ep; |
| 115 | |
| 116 | if (strlen(arg) > 4 && !memcmp(arg,"type",4) && |
| 117 | (unknowntype= strtoul(arg+4, &ep, 10), !*ep) && unknowntype < 65536) { |
| 118 | ov_type= unknowntype | adns_r_unknown; |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | for (tnp=typenames; |
| 123 | tnp->type && strcmp(arg,tnp->desc); |
| 124 | tnp++); |
| 125 | if (!tnp->type) usageerr("unknown RR type %s",arg); |
| 126 | ov_type= tnp->type; |
| 127 | } |
| 128 | |
| 129 | static void process_optarg(const char *arg, |
| 130 | const char *const **argv_p, |
| 131 | const char *value) { |
| 132 | const struct optioninfo *oip; |
| 133 | const char *arg2; |
| 134 | int invert; |
| 135 | |
| 136 | if (arg[0] == '-' || arg[0] == '+') { |
| 137 | if (arg[0] == '-' && arg[1] == '-') { |
| 138 | if (!strncmp(arg,"--no-",5)) { |
| 139 | invert= 1; |
| 140 | oip= opt_findl(arg+5); |
| 141 | } else { |
| 142 | invert= 0; |
| 143 | oip= opt_findl(arg+2); |
| 144 | } |
| 145 | if (oip->type == ot_funcarg) { |
| 146 | arg= argv_p ? *++(*argv_p) : value; |
| 147 | if (!arg) usageerr("option --%s requires a value argument",oip->lopt); |
| 148 | arg2= 0; |
| 149 | } else if (oip->type == ot_funcarg2) { |
| 150 | assert(argv_p); |
| 151 | arg= *++(*argv_p); |
| 152 | arg2= arg ? *++(*argv_p) : 0; |
| 153 | if (!arg || !arg2) |
| 154 | usageerr("option --%s requires two more arguments", oip->lopt); |
| 155 | } else { |
| 156 | if (value) usageerr("option --%s does not take a value",oip->lopt); |
| 157 | arg= 0; |
| 158 | arg2= 0; |
| 159 | } |
| 160 | opt_do(oip,invert,arg,arg2); |
| 161 | } else if (arg[0] == '-' && arg[1] == 0) { |
| 162 | arg= argv_p ? *++(*argv_p) : value; |
| 163 | if (!arg) usageerr("option `-' must be followed by a domain"); |
| 164 | query_do(arg); |
| 165 | } else { /* arg[1] != '-', != '\0' */ |
| 166 | invert= (arg[0] == '+'); |
| 167 | ++arg; |
| 168 | while (*arg) { |
| 169 | oip= opt_finds(&arg); |
| 170 | if (oip->type == ot_funcarg) { |
| 171 | if (!*arg) { |
| 172 | arg= argv_p ? *++(*argv_p) : value; |
| 173 | if (!arg) usageerr("option -%s requires a value argument",oip->sopt); |
| 174 | } else { |
| 175 | if (value) usageerr("two values for option -%s given !",oip->sopt); |
| 176 | } |
| 177 | opt_do(oip,invert,arg,0); |
| 178 | arg= ""; |
| 179 | } else { |
| 180 | if (value) usageerr("option -%s does not take a value",oip->sopt); |
| 181 | opt_do(oip,invert,0,0); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } else { /* arg[0] != '-' */ |
| 186 | query_do(arg); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static void read_stdin(void) { |
| 191 | int anydone, r; |
| 192 | char *newline, *space; |
| 193 | |
| 194 | anydone= 0; |
| 195 | while (!anydone || used) { |
| 196 | while (!(newline= memchr(buf,'\n',used))) { |
| 197 | if (used == avail) { |
| 198 | avail += 20; avail <<= 1; |
| 199 | buf= realloc(buf,avail); |
| 200 | if (!buf) sysfail("realloc stdin buffer",errno); |
| 201 | } |
| 202 | do { |
| 203 | r= read(0,buf+used,avail-used); |
| 204 | } while (r < 0 && errno == EINTR); |
| 205 | if (r == 0) { |
| 206 | if (used) { |
| 207 | /* fake up final newline */ |
| 208 | buf[used++]= '\n'; |
| 209 | r= 1; |
| 210 | } else { |
| 211 | ov_pipe= 0; |
| 212 | return; |
| 213 | } |
| 214 | } |
| 215 | if (r < 0) sysfail("read stdin",errno); |
| 216 | used += r; |
| 217 | } |
| 218 | *newline++= 0; |
| 219 | space= strchr(buf,' '); |
| 220 | if (space) *space++= 0; |
| 221 | process_optarg(buf,0,space); |
| 222 | used -= (newline-buf); |
| 223 | memmove(buf,newline,used); |
| 224 | anydone= 1; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | int main(int argc, const char *const *argv) { |
| 229 | struct timeval *tv, tvbuf; |
| 230 | adns_query qu; |
| 231 | void *qun_v; |
| 232 | adns_answer *answer; |
| 233 | int r, maxfd; |
| 234 | fd_set readfds, writefds, exceptfds; |
| 235 | const char *arg; |
| 236 | |
| 237 | while ((arg= *++argv)) process_optarg(arg,&argv,0); |
| 238 | |
| 239 | if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help"); |
| 240 | |
| 241 | ensure_adns_init(); |
| 242 | |
| 243 | for (;;) { |
| 244 | for (;;) { |
| 245 | qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0; |
| 246 | r= adns_check(ads,&qu,&answer,&qun_v); |
| 247 | if (r == EAGAIN) break; |
| 248 | if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; } |
| 249 | assert(!r); |
| 250 | query_done(qun_v,answer); |
| 251 | } |
| 252 | maxfd= 0; |
| 253 | FD_ZERO(&readfds); |
| 254 | FD_ZERO(&writefds); |
| 255 | FD_ZERO(&exceptfds); |
| 256 | if (ov_pipe) { |
| 257 | maxfd= 1; |
| 258 | FD_SET(0,&readfds); |
| 259 | } |
| 260 | tv= 0; |
| 261 | adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0); |
| 262 | r= select(maxfd, &readfds,&writefds,&exceptfds, tv); |
| 263 | if (r == -1) { |
| 264 | if (errno == EINTR) continue; |
| 265 | sysfail("select",errno); |
| 266 | } |
| 267 | adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0); |
| 268 | if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin(); |
| 269 | } |
| 270 | x_quit: |
| 271 | if (fclose(stdout)) outerr(); |
| 272 | quitnow(rcode); |
| 273 | } |