source: trunk/server/source3/nmbd/asyncdns.c@ 855

Last change on this file since 855 was 855, checked in by Silvan Scherrer, 11 years ago

Samba Server 3.6: seperate logfiles per pid in nmbd and also take smb.conf into account

File size: 9.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 a async DNS handler
4 Copyright (C) Andrew Tridgell 1997-1998
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "includes.h"
21#include "nmbd/nmbd.h"
22#ifdef __OS2__
23#define pipe(A) os2_pipe(A)
24#endif
25
26/***************************************************************************
27 Add a DNS result to the name cache.
28****************************************************************************/
29
30static struct name_record *add_dns_result(struct nmb_name *question, struct in_addr addr)
31{
32 int name_type = question->name_type;
33 unstring qname;
34
35 pull_ascii_nstring(qname, sizeof(qname), question->name);
36
37 if (!addr.s_addr) {
38 /* add the fail to WINS cache of names. give it 1 hour in the cache */
39 DEBUG(3,("add_dns_result: Negative DNS answer for %s\n", qname));
40 add_name_to_subnet( wins_server_subnet, qname, name_type,
41 NB_ACTIVE, 60*60, DNSFAIL_NAME, 1, &addr );
42 return NULL;
43 }
44
45 /* add it to our WINS cache of names. give it 2 hours in the cache */
46 DEBUG(3,("add_dns_result: DNS gave answer for %s of %s\n", qname, inet_ntoa(addr)));
47
48 add_name_to_subnet( wins_server_subnet, qname, name_type,
49 NB_ACTIVE, 2*60*60, DNS_NAME, 1, &addr);
50
51 return find_name_on_subnet(wins_server_subnet, question, FIND_ANY_NAME);
52}
53
54#ifndef SYNC_DNS
55
56static int fd_in = -1, fd_out = -1;
57static pid_t child_pid = -1;
58static int in_dns;
59
60/* this is the structure that is passed between the parent and child */
61struct query_record {
62 struct nmb_name name;
63 struct in_addr result;
64};
65
66/* a queue of pending requests waiting to be sent to the DNS child */
67static struct packet_struct *dns_queue;
68
69/* the packet currently being processed by the dns child */
70static struct packet_struct *dns_current;
71
72
73/***************************************************************************
74 return the fd used to gather async dns replies. This is added to the select
75 loop
76 ****************************************************************************/
77
78int asyncdns_fd(void)
79{
80 return fd_in;
81}
82
83/***************************************************************************
84 handle DNS queries arriving from the parent
85 ****************************************************************************/
86static void asyncdns_process(void)
87{
88 struct query_record r;
89 unstring qname;
90
91 DEBUGLEVEL = -1;
92
93 while (1) {
94 NTSTATUS status;
95
96 status = read_data(fd_in, (char *)&r, sizeof(r));
97
98 if (!NT_STATUS_IS_OK(status)) {
99 break;
100 }
101
102 pull_ascii_nstring( qname, sizeof(qname), r.name.name);
103 r.result.s_addr = interpret_addr(qname);
104
105 if (write_data(fd_out, (char *)&r, sizeof(r)) != sizeof(r))
106 break;
107 }
108
109 _exit(0);
110}
111
112/**************************************************************************** **
113 catch a sigterm (in the child process - the parent has a different handler
114 see nmbd.c for details).
115 We need a separate term handler here so we don't release any
116 names that our parent is going to release, or overwrite a
117 WINS db that our parent is going to write.
118 **************************************************************************** */
119
120static void sig_term(int sig)
121{
122 _exit(0);
123}
124
125/***************************************************************************
126 Called by the parent process when it receives a SIGTERM - also kills the
127 child so we don't get child async dns processes lying around, causing trouble.
128 ****************************************************************************/
129
130void kill_async_dns_child(void)
131{
132 if (child_pid > 0) {
133 kill(child_pid, SIGTERM);
134 child_pid = -1;
135 }
136}
137
138/***************************************************************************
139 create a child process to handle DNS lookups
140 ****************************************************************************/
141void start_async_dns(void)
142{
143 int fd1[2], fd2[2];
144 NTSTATUS status;
145
146 CatchChild();
147
148 if (pipe(fd1) || pipe(fd2)) {
149 DEBUG(0,("can't create asyncdns pipes\n"));
150 return;
151 }
152
153 child_pid = sys_fork();
154
155 if (child_pid) {
156 fd_in = fd1[0];
157 fd_out = fd2[1];
158 close(fd1[1]);
159 close(fd2[0]);
160 DEBUG(0,("started asyncdns process %d\n", (int)child_pid));
161 return;
162 }
163
164 fd_in = fd2[0];
165 fd_out = fd1[1];
166
167 CatchSignal(SIGUSR2, SIG_IGN);
168 CatchSignal(SIGUSR1, SIG_IGN);
169 CatchSignal(SIGHUP, SIG_IGN);
170 CatchSignal(SIGTERM, sig_term);
171
172#ifdef __OS2__
173 /* restore the logfile to log.nmbd always */
174 char *lfile = NULL;
175 if (asprintf(&lfile, "%s/log.nmbd.%d", get_dyn_LOGFILEBASE(), getpid()) < 0)
176 exit(1);
177
178 lp_set_logfile(lfile);
179 SAFE_FREE(lfile);
180 reopen_logs();
181#endif
182 status = reinit_after_fork(nmbd_messaging_context(),
183 nmbd_event_context(),
184 procid_self(), true);
185
186 if (!NT_STATUS_IS_OK(status)) {
187 DEBUG(0,("reinit_after_fork() failed\n"));
188 smb_panic("reinit_after_fork() failed");
189 }
190
191 asyncdns_process();
192}
193
194
195/***************************************************************************
196check if a particular name is already being queried
197 ****************************************************************************/
198static bool query_current(struct query_record *r)
199{
200 return dns_current &&
201 nmb_name_equal(&r->name,
202 &dns_current->packet.nmb.question.question_name);
203}
204
205
206/***************************************************************************
207 write a query to the child process
208 ****************************************************************************/
209static bool write_child(struct packet_struct *p)
210{
211 struct query_record r;
212
213 r.name = p->packet.nmb.question.question_name;
214
215 return write_data(fd_out, (char *)&r, sizeof(r)) == sizeof(r);
216}
217
218/***************************************************************************
219 check the DNS queue
220 ****************************************************************************/
221void run_dns_queue(void)
222{
223 struct query_record r;
224 struct packet_struct *p, *p2;
225 struct name_record *namerec;
226 NTSTATUS status;
227
228 if (fd_in == -1)
229 return;
230
231 if (!process_exists_by_pid(child_pid)) {
232 close(fd_in);
233 close(fd_out);
234 start_async_dns();
235 }
236
237 status = read_data(fd_in, (char *)&r, sizeof(r));
238
239 if (!NT_STATUS_IS_OK(status)) {
240 DEBUG(0, ("read from child failed: %s\n", nt_errstr(status)));
241 fd_in = -1;
242 return;
243 }
244
245 namerec = add_dns_result(&r.name, r.result);
246
247 if (dns_current) {
248 if (query_current(&r)) {
249 DEBUG(3,("DNS calling send_wins_name_query_response\n"));
250 in_dns = 1;
251 if(namerec == NULL)
252 send_wins_name_query_response(NAM_ERR, dns_current, NULL);
253 else
254 send_wins_name_query_response(0,dns_current,namerec);
255 in_dns = 0;
256 }
257
258 dns_current->locked = False;
259 free_packet(dns_current);
260 dns_current = NULL;
261 }
262
263 /* loop over the whole dns queue looking for entries that
264 match the result we just got */
265 for (p = dns_queue; p;) {
266 struct nmb_packet *nmb = &p->packet.nmb;
267 struct nmb_name *question = &nmb->question.question_name;
268
269 if (nmb_name_equal(question, &r.name)) {
270 DEBUG(3,("DNS calling send_wins_name_query_response\n"));
271 in_dns = 1;
272 if(namerec == NULL)
273 send_wins_name_query_response(NAM_ERR, p, NULL);
274 else
275 send_wins_name_query_response(0,p,namerec);
276 in_dns = 0;
277 p->locked = False;
278
279 p2 = p->next;
280 DLIST_REMOVE(dns_queue, p);
281 free_packet(p);
282 p = p2;
283 } else {
284 p = p->next;
285 }
286 }
287
288 if (dns_queue) {
289 dns_current = dns_queue;
290 DLIST_REMOVE(dns_queue, dns_queue);
291
292 if (!write_child(dns_current)) {
293 DEBUG(3,("failed to send DNS query to child!\n"));
294 return;
295 }
296 }
297}
298
299/***************************************************************************
300queue a DNS query
301 ****************************************************************************/
302
303bool queue_dns_query(struct packet_struct *p,struct nmb_name *question)
304{
305 if (in_dns || fd_in == -1)
306 return False;
307
308 if (!dns_current) {
309 if (!write_child(p)) {
310 DEBUG(3,("failed to send DNS query to child!\n"));
311 return False;
312 }
313 dns_current = p;
314 p->locked = True;
315 } else {
316 p->locked = True;
317 DLIST_ADD(dns_queue, p);
318 }
319
320 DEBUG(3,("added DNS query for %s\n", nmb_namestr(question)));
321 return True;
322}
323
324#else
325
326
327/***************************************************************************
328 we use this when we can't do async DNS lookups
329 ****************************************************************************/
330
331bool queue_dns_query(struct packet_struct *p,struct nmb_name *question)
332{
333 struct name_record *namerec = NULL;
334 struct in_addr dns_ip;
335 unstring qname;
336
337 pull_ascii_nstring(qname, sizeof(qname), question->name);
338
339 DEBUG(3,("DNS search for %s - ", nmb_namestr(question)));
340
341 dns_ip.s_addr = interpret_addr(qname);
342
343 namerec = add_dns_result(question, dns_ip);
344 if(namerec == NULL) {
345 send_wins_name_query_response(NAM_ERR, p, NULL);
346 } else {
347 send_wins_name_query_response(0, p, namerec);
348 }
349 return False;
350}
351
352/***************************************************************************
353 With sync dns there is no child to kill on SIGTERM.
354 ****************************************************************************/
355
356void kill_async_dns_child(void)
357{
358 return;
359}
360#endif
Note: See TracBrowser for help on using the repository browser.