chiark / gitweb /
SECURITY: adnshost: Fix missing-final-newline handling on stdin read
[adns.git] / client / adh-main.c
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,2014-2016  Ian Jackson
9  *    Copyright (C) 2014  Mark Wooding
10  *    Copyright (C) 1999-2000,2003,2006  Tony Finch
11  *    Copyright (C) 1991 Massachusetts Institute of Technology
12  *  (See the file INSTALL for full details.)
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 3, 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  */
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_aaaa,     "aaaa" },
103     { adns_r_ns_raw,   "ns-"  },
104     { adns_r_soa_raw,  "soa-" },
105     { adns_r_ptr_raw,  "ptr-" },
106     { adns_r_mx_raw,   "mx-"  },
107     { adns_r_rp_raw,   "rp-"  },
108     { adns_r_srv_raw,  "srv-" },
109
110     { adns_r_none, 0 }
111   };
112
113   const struct typename *tnp;
114   unsigned long unknowntype;
115   char *ep;
116
117   if (strlen(arg) > 4 && !memcmp(arg,"type",4) &&
118       (unknowntype= strtoul(arg+4, &ep, 10), !*ep) && unknowntype < 65536) {
119     ov_type= unknowntype | adns_r_unknown;
120     return;
121   }
122
123   for (tnp=typenames;
124        tnp->type && strcmp(arg,tnp->desc);
125        tnp++);
126   if (!tnp->type) usageerr("unknown RR type %s",arg);
127   ov_type= tnp->type;
128 }
129
130 static void process_optarg(const char *arg,
131                            const char *const **argv_p,
132                            const char *value) {
133   const struct optioninfo *oip;
134   const char *arg2;
135   int invert;
136
137   if (arg[0] == '-' || arg[0] == '+') {
138     if (arg[0] == '-' && arg[1] == '-') {
139       if (!strncmp(arg,"--no-",5)) {
140         invert= 1;
141         oip= opt_findl(arg+5);
142       } else {
143         invert= 0;
144         oip= opt_findl(arg+2);
145       }
146       if (oip->type == ot_funcarg) {
147         arg= argv_p ? *++(*argv_p) : value;
148         if (!arg) usageerr("option --%s requires a value argument",oip->lopt);
149         arg2= 0;
150       } else if (oip->type == ot_funcarg2) {
151         assert(argv_p);
152         arg= *++(*argv_p);
153         arg2= arg ? *++(*argv_p) : 0;
154         if (!arg || !arg2)
155           usageerr("option --%s requires two more arguments", oip->lopt);
156       } else {
157         if (value) usageerr("option --%s does not take a value",oip->lopt);
158         arg= 0;
159         arg2= 0;
160       }
161       opt_do(oip,invert,arg,arg2);
162     } else if (arg[0] == '-' && arg[1] == 0) {
163       arg= argv_p ? *++(*argv_p) : value;
164       if (!arg) usageerr("option `-' must be followed by a domain");
165       query_do(arg);
166     } else { /* arg[1] != '-', != '\0' */
167       invert= (arg[0] == '+');
168       ++arg;
169       while (*arg) {
170         oip= opt_finds(&arg);
171         if (oip->type == ot_funcarg) {
172           if (!*arg) {
173             arg= argv_p ? *++(*argv_p) : value;
174             if (!arg) usageerr("option -%s requires a value argument",oip->sopt);
175           } else {
176             if (value) usageerr("two values for option -%s given !",oip->sopt);
177           }
178           opt_do(oip,invert,arg,0);
179           arg= "";
180         } else {
181           if (value) usageerr("option -%s does not take a value",oip->sopt);
182           opt_do(oip,invert,0,0);
183         }
184       }
185     }
186   } else { /* arg[0] != '-' */
187     query_do(arg);
188   }
189 }
190     
191 static void read_stdin(void) {
192   int anydone, r;
193   char *newline, *space;
194
195   anydone= 0;
196   while (!anydone || used) {
197     while (!(newline= memchr(buf,'\n',used))) {
198       if (used == avail) {
199         avail += 20; avail <<= 1;
200         buf= realloc(buf,avail);
201         if (!buf) sysfail("realloc stdin buffer",errno);
202       }
203       do {
204         r= read(0,buf+used,avail-used);
205       } while (r < 0 && errno == EINTR);
206       if (r == 0) {
207         if (used) {
208           /* fake up final newline */
209           buf[used]= '\n';
210           r= 1;
211         } else {
212           ov_pipe= 0;
213           return;
214         }
215       }
216       if (r < 0) sysfail("read stdin",errno);
217       used += r;
218     }
219     *newline++= 0;
220     space= strchr(buf,' ');
221     if (space) *space++= 0;
222     process_optarg(buf,0,space);
223     used -= (newline-buf);
224     memmove(buf,newline,used);
225     anydone= 1;
226   }
227 }
228
229 int main(int argc, const char *const *argv) {
230   struct timeval *tv, tvbuf;
231   adns_query qu;
232   void *qun_v;
233   adns_answer *answer;
234   int r, maxfd;
235   fd_set readfds, writefds, exceptfds;
236   const char *arg;
237   
238   while ((arg= *++argv)) process_optarg(arg,&argv,0);
239
240   if (!ov_pipe && !ads) usageerr("no domains given, and -f/--pipe not used; try --help");
241
242   ensure_adns_init();
243
244   for (;;) {
245     for (;;) {
246       qu= ov_asynch ? 0 : outstanding.head ? outstanding.head->qu : 0;
247       r= adns_check(ads,&qu,&answer,&qun_v);
248       if (r == EAGAIN) break;
249       if (r == ESRCH) { if (!ov_pipe) goto x_quit; else break; }
250       assert(!r);
251       query_done(qun_v,answer);
252     }
253     maxfd= 0;
254     FD_ZERO(&readfds);
255     FD_ZERO(&writefds);
256     FD_ZERO(&exceptfds);
257     if (ov_pipe) {
258       maxfd= 1;
259       FD_SET(0,&readfds);
260     }
261     tv= 0;
262     adns_beforeselect(ads, &maxfd, &readfds,&writefds,&exceptfds, &tv,&tvbuf,0);
263     r= select(maxfd, &readfds,&writefds,&exceptfds, tv);
264     if (r == -1) {
265       if (errno == EINTR) continue;
266       sysfail("select",errno);
267     }
268     adns_afterselect(ads, maxfd, &readfds,&writefds,&exceptfds, 0);
269     if (ov_pipe && FD_ISSET(0,&readfds)) read_stdin();
270   }
271 x_quit:
272   if (fclose(stdout)) outerr();
273   quitnow(rcode);
274 }