Merge from vendor branch DHCP:
[dragonfly.git] / contrib / isc-dhcp / common / resolv.c
1 /* resolv.c
2
3    Parser for /etc/resolv.conf file. */
4
5 /*
6  * Copyright (c) 1996-2002 Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38  * To learn more about the Internet Software Consortium, see
39  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
40  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
41  * ``http://www.nominum.com''.
42  */
43
44 #ifndef lint
45 static char copyright[] =
46 "$Id: resolv.c,v 1.16.2.1 2002/11/17 02:26:59 dhankins Exp $ Copyright (c) 1996-2002 The Internet Software Consortium.  All rights reserved.\n";
47 #endif /* not lint */
48
49 #include "dhcpd.h"
50
51 struct name_server *name_servers;
52 struct domain_search_list *domains;
53 char path_resolv_conf [] = _PATH_RESOLV_CONF;
54
55 void read_resolv_conf (parse_time)
56         TIME parse_time;
57 {
58         int file;
59         struct parse *cfile;
60         const char *val;
61         int token;
62         int declaration = 0;
63         struct name_server *sp, *sl, *ns;
64         struct domain_search_list *dp, *dl, *nd;
65         struct iaddr *iaddr;
66
67         if ((file = open (path_resolv_conf, O_RDONLY)) < 0) {
68                 log_error ("Can't open %s: %m", path_resolv_conf);
69                 return;
70         }
71
72         cfile = (struct parse *)0;
73         new_parse (&cfile, file, (char *)0, 0, path_resolv_conf, 1);
74
75         do {
76                 token = next_token (&val, (unsigned *)0, cfile);
77                 if (token == END_OF_FILE)
78                         break;
79                 else if (token == EOL)
80                         continue;
81                 else if (token == DOMAIN || token == SEARCH) {
82                         do {
83                                 struct domain_search_list *nd, **dp;
84                                 char *dn;
85
86                                 dn = parse_host_name (cfile);
87                                 if (!dn)
88                                         break;
89
90                                 dp = &domains;
91                                 for (nd = domains; nd; nd = nd -> next) {
92                                         dp = &nd -> next;
93                                         if (!strcmp (nd -> domain, dn))
94                                                 break;
95                                 }
96                                 if (!nd) {
97                                         nd = new_domain_search_list (MDL);
98                                         if (!nd)
99                                                 log_fatal ("No memory for %s",
100                                                            dn);
101                                         nd -> next =
102                                                 (struct domain_search_list *)0;
103                                         *dp = nd;
104                                         nd -> domain = dn;
105                                         dn = (char *)0;
106                                 }
107                                 nd -> rcdate = parse_time;
108                                 token = peek_token (&val,
109                                                     (unsigned *)0, cfile);
110                         } while (token != EOL);
111                         if (token != EOL) {
112                                 parse_warn (cfile,
113                                             "junk after domain declaration");
114                                 skip_to_semi (cfile);
115                         }
116                         token = next_token (&val, (unsigned *)0, cfile);
117                 } else if (token == NAMESERVER) {
118                         struct name_server *ns, **sp;
119                         struct iaddr iaddr;
120
121                         parse_ip_addr (cfile, &iaddr);
122
123                         sp = &name_servers;
124                         for (ns = name_servers; ns; ns = ns -> next) {
125                                 sp = &ns -> next;
126                                 if (!memcmp (&ns -> addr.sin_addr,
127                                              iaddr.iabuf, iaddr.len))
128                                         break;
129                         }
130                         if (!ns) {
131                                 ns = new_name_server (MDL);
132                                 if (!ns)
133                                     log_fatal ("No memory for nameserver %s",
134                                                piaddr (iaddr));
135                                 ns -> next = (struct name_server *)0;
136                                 *sp = ns;
137                                 memcpy (&ns -> addr.sin_addr,
138                                         iaddr.iabuf, iaddr.len);
139 #ifdef HAVE_SA_LEN
140                                 ns -> addr.sin_len = sizeof ns -> addr;
141 #endif
142                                 ns -> addr.sin_family = AF_INET;
143                                 ns -> addr.sin_port = htons (53);
144                                 memset (ns -> addr.sin_zero, 0,
145                                         sizeof ns -> addr.sin_zero);
146                         }
147                         ns -> rcdate = parse_time;
148                         skip_to_semi (cfile);
149                 } else
150                         skip_to_semi (cfile); /* Ignore what we don't grok. */
151         } while (1);
152         token = next_token (&val, (unsigned *)0, cfile);
153
154         /* Lose servers that are no longer in /etc/resolv.conf. */
155         sl = (struct name_server *)0;
156         for (sp = name_servers; sp; sp = ns) {
157                 ns = sp -> next;
158                 if (sp -> rcdate != parse_time) {
159                         if (sl)
160                                 sl -> next = sp -> next;
161                         else
162                                 name_servers = sp -> next;
163                         /* We can't actually free the name server structure,
164                            because somebody might be hanging on to it.    If
165                            your /etc/resolv.conf file changes a lot, this
166                            could be a noticable memory leak. */
167                 } else
168                         sl = sp;
169         }
170
171         /* Lose domains that are no longer in /etc/resolv.conf. */
172         dl = (struct domain_search_list *)0;
173         for (dp = domains; dp; dp = nd) {
174                 nd = dp -> next;
175                 if (dp -> rcdate != parse_time) {
176                         if (dl)
177                                 dl -> next = dp -> next;
178                         else
179                                 domains = dp -> next;
180                         free_domain_search_list (dp, MDL);
181                 } else
182                         dl = dp;
183         }
184         close (file);
185         end_parse (&cfile);
186 }
187
188 /* Pick a name server from the /etc/resolv.conf file. */
189
190 struct name_server *first_name_server ()
191 {
192         FILE *rc;
193         static TIME rcdate;
194         struct stat st;
195
196         /* Check /etc/resolv.conf and reload it if it's changed. */
197         if (cur_time > rcdate) {
198                 if (stat (path_resolv_conf, &st) < 0) {
199                         log_error ("Can't stat %s", path_resolv_conf);
200                         return (struct name_server *)0;
201                 }
202                 if (st.st_mtime > rcdate) {
203                         char rcbuf [512];
204                         char *s, *t, *u;
205                         rcdate = cur_time + 1;
206                         
207                         read_resolv_conf (rcdate);
208                 }
209         }
210
211         return name_servers;
212 }