1 /*-
2 * Copyright (c) 1998-2008 DHIS, Dynamic Host Information System
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36
37
38 #include "nsupdate.h"
39
dns_update(int opcode,const char * hostname,const char * ipaddr)40 int dns_update(int opcode,const char *hostname,const char *ipaddr) {
41
42 int fildes[2];
43 int childpid;
44 int ret_code=0;
45 char str[1024];
46
47 if(opcode==NSUPDATE_ADD) {
48 sprintf(str,"update add %s. %d IN A %s\n\n",hostname,DYN_TTL,ipaddr);
49 }
50 else {
51 if(opcode==NSUPDATE_DEL)
52 sprintf(str,"update delete %s. IN A\n\n",hostname);
53 else
54 return(0);
55 }
56
57 if(pipe(fildes)) return(0);
58
59 write(fildes[1],str,strlen(str)+1);
60
61 childpid=fork();
62
63 if(!childpid) { // I am the child
64
65 // Place the pipe read in stdin and close pipe write
66 if(fildes[0]!=0) close(0);
67 if(fildes[0]!=0) dup2(fildes[0],0);
68 close(fildes[1]);
69
70 // Redirect stdout and stderr to NULL
71 close(1);
72 close(2);
73 open("/dev/null",O_WRONLY,0666);
74 open("/dev/null",O_WRONLY,0666);
75
76 ret_code=execlp(NSUPDATE_CMD,NSUPDATE_CMD,"-d",NULL);
77 if(ret_code) exit(1); else exit(0);
78
79 } else { // I am the parent, feed the child and wait for it to finish
80
81 close(fildes[0]);
82 close(fildes[1]);
83 if(waitpid(childpid,&ret_code,0)==-1) return(0);
84 return(1);
85 }
86
87 }
88
89