dhis-dns-engine-5.3000755 000000 000764 00000000000 10737722214 014226 5ustar00rootdhis000000 000000 dhis-dns-engine-5.3/CHANGES000644 000000 000764 00000000245 10737722164 015305 0ustar00rootdhis000000 000000 5.3 - Same as 5.2, just upgraded to be in line with dhisd. 5.2 - Corrected bug that caused improper handling of comment character (;) in the database file. dhis-dns-engine-5.3/INSTALL000644 000000 000764 00000010406 10737722203 015335 0ustar00rootdhis000000 000000 Install instructions for DHIS R5 server module dhis-dns-engine ============================================================== This package contains a dynamic DNS service module to be used with dhisd release 5 or above. While the DHIS server dhisd retrieves dynamic IP addresses from clients, this module allows the server to update a dynamic DNS zone based on those retrieved IP addresses. In order to install dhis-dns-engine-5.3: A domain under which dynamic hosts will be updated must be available. The server system must have BIND 8 or higher installed, with an available nsupdate binary. By default this module uses /usr/sbin/nsupdate. If your nsupdate is somewhere else on the system please edit nsupdate.c and replace NSUPDATE_CMD. The machine must be permanently connected to the internet with a static IP address. The machine which will act as a server should be running a distribution of UNIX or similar system. A DHIS R5 server dhisd must be running on the same machine. This module must be included in the server's service database. 1. Compile Stage ================ If your system is System V based, edit the Makefile and add -lsocket -lnsl to the LIBS section. These are required for TCP/IP operation. Being in the source directory type "make". It should build the dhisd binary. 2. Install stage ================ You will need to have dhisd installed in your system. Move the newly compiled module dhis-dns-engine to /etc/dhis/bin Edit /etc/dhis/db/services.db and add a line in the form: dns 5 /etc/dhis/bin/dhis-dns-engine In here dns identifies the service type, 5 is the number of processes to serve DNS (may differ depending on resources and load requirements) and the command is the child process for dhisd to fork into. Edit /etc/dhis/db/dhis.db For each client that is to have dynamic DNS support add a line such as: Service dns Plus a line with the hostname to update for that record: example: HostName dyn1.exmaple.com Terminate your current DHIS server if running with SIGTERM Execute your DHIS server with something like /etc/dhis/bin/dhisd The module in installed 3. DNS stage ============ The final stage is to configure DNS, i.e. BIND, to allow DHIS operation. Let us assume that the nameserver is known by ns.example.com and that, if relaying is present, the relay machine is identified by relay.example.com. Again, these may point to the same machine. Edit /etc/named.conf and add a section such as: zone "example.com" in { type master; file "db.example.com"; allow-update { 127.0.0.1; x.x.x.x; }; }; Where x.x.x.x is the server's IP address as seen from the outside world. The allow-updates keyword tells BIND to accept updates only from the given addresses. Now cd to the current DNS database directory and create db.example.com as a text file. Example: --------------------------------------------------------------- @ IN SOA ns.example.com. root.ns.example.com. ( 1000000 ; Serial Number 28800 ; Refresh 7200 ; Retry 604800 ; Expire 86400 ; Default TTL ); IN NS ns ns IN A x.x.x.x ; If relay is present relay IN A y.y.y.y localhost IN A 127.0.0.1 ; Now for each dynamic host add dyn1 60 IN A 192.168.255.0 dyn2 60 IN A 192.168.255.0 dyn3 60 IN A 192.168.255.0 ; Note that dynamic address A records have a TTL of 60 seconds --------------------------------------------------------------- And run named (/usr/sbin/named or /usr/sbin/in.named) to load these fields. DNS is now running and accepting dynamic updates. 4. Adding Records ================= For each new host, as described above, a new record must be added to dhis.db and a kill -HUP must be sent to the dhisd process. Furthermore, DNS entries must be created. Assuming you wish to add dyn4 at a later stage, Add 1004 { hostname dyn4.example.com hostpass xpto4 } to dhis.db, run kill -HUP `cat /etc/dhis/pid/dhisd.pid` to reload the DHIS server. run nsupdate command and issue: >update add dyn4.example.com. 60 in a 192.168.255.0 And press return twice. When you finish type CTRL-D to exit nsupdate. This adds the DNS records. You can of course create a shell script to perform all these steps automatically. And that's it. The DHIS DNS module is installed. dhis-dns-engine-5.3/COPYRIGHT000644 000000 000764 00000002525 10341100462 015566 0ustar00rootdhis000000 000000 - Copyright (c) 1998-2005 Joao Cabral All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. DHIS(c) Dynamic Host Information System Release 5 dhis-dns-engine-5.3/README000644 000000 000764 00000002504 10341101253 015147 0ustar00rootdhis000000 000000 Install instructions for DHIS R5 server module dhis-dns-engine ============================================================== This package contains a dynamic DNS service module to be used with dhisd release 5 or above. While the DHIS server dhisd retrieves dynamic IP addresses from clients, this module allows the server to update a dynamic DNS zone based on those retrieved IP addresses. This module uses the DHIS server database file itself to build its ID to FQDN matrix. When reading /etc/dhis/db/dhis.db it ignores all keyword lines apart from HostName. This allows a single database file for both DHIS plus DNS module. The DHIS server likewise ignores HostName lines. Database files from release 4 may be used with DHIS R5 + this module In order versions, this module issued nsupdates directly and required to be linked with the bind libraries. Furthermore it only supported some BIND 8 versions. As of the 5.1 version of this module, any BIND versions with a valid nsupdate binary command can be used (including 9). This module simply formats the commands and issues them to the system's nsupdate binary (tipically /usr/sbin/nsupdate). For more information on modules see the dhisd documentation. Support: ======== Please address any questions regarding this module to the DHIS contacts available at http://www.dhis.org/ dhis-dns-engine-5.3/Makefile000644 000000 000764 00000003633 10341101347 015737 0ustar00rootdhis000000 000000 # - # Copyright (c) 1998-2005 Joao Cabral # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # DHIS(c) Dynamic Host Information System Release 5 CC=gcc CFLAGS=-Wall -I/usr/local/include LFLAGS=-L/usr/local/lib LIBS= RM=rm -f INSTALLDIR=/etc/dhis/bin MODE=700 OWNER=root MKDIR=mkdir -p CHOWN=chown CHMOD=chmod CP=cp dhis-dns-engine:dhis-dns-engine.c nsupdate.c $(CC) $(CFLAGS) $(LFLAGS) -o dhis-dns-engine nsupdate.c dhis-dns-engine.c $(LIBS) strip dhis-dns-engine install: dhis-dns-engine $(MKDIR) $(INSTALLDIR) $(CP) dhis-dns-engine $(INSTALLDIR) $(CHOWN) $(OWNER) $(INSTALLDIR)/dhis-dns-engine $(CHMOD) $(MODE) $(INSTALLDIR)/dhis-dns-engine clean: $(RM) *.core core dhis-dns-engine *.o dhis-dns-engine-5.3/dhis-dns-engine.c000644 000000 000764 00000017164 10737676674 017460 0ustar00rootdhis000000 000000 /*- * Copyright (c) 1998-2005 Joao Cabral * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * DHIS(c) Dynamic Host Information System Release 5 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_SYS_PARAM_H #include #endif /* FreeBSD patch submitted by S. Kruit */ #if defined __FreeBSD__ /* FreeBSD has BIND built in */ # if __FreeBSD__ >= 2 /* Test for 2.x versions */ # include # if __FreeBSD_version >= 420001 # include # endif # endif #else # if !defined BIND821 # include # endif #endif #define DYN_TTL 60 #define OFFLINE_IP "192.168.255.0" #define DHISD_DB "/etc/dhis/db/dhis.db" #define DHISE_LOG "/etc/dhis/log/dhis-dns-engine.log" #define MAX_HOSTNAME 64 #define MAX_PASS 16 typedef struct db_t { int id; unsigned char fqdn[MAX_HOSTNAME]; struct db_t *next; } db_t; /* Function prototypes */ int db_reload(void); int db_free(void); int db_add(db_t *); unsigned char *db_hostname(int); void strtolower(unsigned char *); void off_nl(unsigned char *); unsigned char *line_entry(int,unsigned char *); int msg_log(unsigned char *); int dns_update(unsigned char *,int,short int,unsigned int,int,unsigned char *); int mark_online(unsigned char *,unsigned char *); int mark_offline(unsigned char *); /* Global variables */ db_t *dbase=NULL; /* Log function */ int msg_log(unsigned char *msg) { FILE *fp; time_t tt; unsigned char buff[256]; fp=fopen(DHISE_LOG,"a"); while(fp==NULL) { sleep(1); fp=fopen(DHISE_LOG,"a"); } tt=time(NULL); strcpy(buff,ctime(&tt)); off_nl(buff); strcat(buff," : "); strcat(buff,msg); strcat(buff,"\n"); fputs(buff,fp); fclose(fp); return(1); } /* Misc functions */ void strtolower(unsigned char *s) { while(*s!='\0') { *s=tolower(*s); s++; } } void off_nl(unsigned char *s) { while(*s!='\0' && *s!='\n' && *s!='\r') s++; *s='\0'; return; } unsigned char *line_entry(int idx,unsigned char *buff) { static unsigned char b2[1024],*pb2; int i; idx--; b2[0]='\0'; pb2=b2; while((*buff==' ' || *buff=='\t') && *buff!='\0' && *buff!='\n') buff++; if(*buff=='\0' || *buff=='\n') return(b2); for(i=0;iid=atoi(line_entry(1,str)); rec->fqdn[0]='\0'; do { if(fgets(str,1024,fp)==NULL) { fclose(fp);free(rec);return(0);} off_nl(str); strcpy(keyword,line_entry(1,str)); strtolower(keyword); if(!strcmp(keyword,"hostname")) { strcpy(rec->fqdn,line_entry(2,str)); // Take leading text if comment character found if((cp=strchr(rec->fqdn,';'))!=NULL) *cp='\0'; } } while(strcmp(line_entry(1,str),"}")); if(rec->fqdn[0]=='\0') continue; db_add(rec); } } fclose(fp); return(1); } int db_free() { db_t *p1,*p2; if(dbase==NULL) return(1); p1=dbase; while(p1!=NULL) { p2=p1; p1=p1->next; free(p2); } dbase=NULL; return(1); } unsigned char *db_hostname(int id) { db_t *p1; static unsigned char host[MAX_HOSTNAME]; static int lastid=0; if(lastid==id) return(host); p1=dbase; while(p1!=NULL) { if(p1->id==id) { strcpy(host,p1->fqdn); lastid=id; return(host); } p1=p1->next; } return(NULL); } int db_add(db_t *rec) { db_t *p; p=dbase; if(dbase==NULL) { dbase=rec; dbase->next=NULL; return(1); } while(p->next!=NULL) p=p->next; p->next=rec; p->next->next=NULL; return(1); } /* DNS functions */ int mark_online(unsigned char *host,unsigned char *addr) { unsigned char str[128]; if(host==NULL) return(0); if(host[0]=='\0') return(0); if(addr==NULL) return(0); if(addr[0]=='\0') return(0); sprintf(str,"%s.",host); dns_update(str,DELETE,T_A,0,0,""); dns_update(str,ADD,T_A,DYN_TTL,strlen(addr),addr); return(0); } int mark_offline(unsigned char *host) { unsigned char str[128],str2[16]; if(host==NULL) return(0); if(host[0]=='\0') return(0); strcpy(str2,OFFLINE_IP); sprintf(str,"%s.",host); dns_update(str,DELETE,T_A,0,0,""); dns_update(str,ADD,T_A,DYN_TTL,strlen(str2),str2); return(0); } int main() { unsigned char line[1024]; db_reload(); sprintf(line,"DHIS DNS Engine Starting"); msg_log(line); while(fgets(line,1024,stdin)!=NULL) { off_nl(line); if(!strcmp(line_entry(1,line),"exit")) { db_free(); exit(0); } if(!strcmp(line_entry(1,line),"reload")) { db_reload(); continue; } if(!strcmp(line_entry(1,line),"add")) { int id; unsigned char addr[128]; unsigned char logstr[128]; id=atoi(line_entry(3,line)); strcpy(addr,line_entry(4,line)); mark_online(db_hostname(id),addr); sprintf(logstr,"-> online %s [%s]", db_hostname(id),addr); msg_log(logstr); continue; } if(!strcmp(line_entry(1,line),"update")) { int id; unsigned char addr[128]; unsigned char logstr[128]; id=atoi(line_entry(3,line)); strcpy(addr,line_entry(4,line)); mark_offline(db_hostname(id)); mark_online(db_hostname(id),addr); sprintf(logstr,"-> update %s [%s]",db_hostname(id), addr); msg_log(logstr); continue; } if(!strcmp(line_entry(1,line),"delete")) { int id; unsigned char addr[128]; unsigned char logstr[128]; id=atoi(line_entry(3,line)); strcpy(addr,line_entry(4,line)); mark_offline(db_hostname(id)); sprintf(logstr,"-> offline %s [%s]",db_hostname(id), addr); msg_log(logstr); continue; } } return(0); /* Just to keep -Wall quiet */ } dhis-dns-engine-5.3/nsupdate.c000644 000000 000764 00000005756 10341100734 016275 0ustar00rootdhis000000 000000 /*- * Copyright (c) 1998-2005 Joao Cabral * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * DHIS(c) Dynamic Host Information System Release 5 */ #include #include #include #include #include #include #include #include #include #include #include #include #define NSUPDATE_CMD "/usr/bin/nsupdate" int dns_update(unsigned char *r_dname,int r_opcode,short int r_type, unsigned int r_ttl,int r_size,unsigned char *r_data) { unsigned char str[1024]; if(r_opcode==ADD) sprintf(str,"update add %s %d IN ",r_dname,r_ttl); else if(r_opcode==DELETE) sprintf(str,"update delete %s IN ",r_dname); else return(0); if(r_type==T_A) strcat(str,"A "); else if(r_type==T_MX) strcat(str,"MX "); else if(r_type==T_CNAME) strcat(str,"CNAME "); else return(0); strcat(str,r_data); strcat(str,"\n"); strcat(str,"\n"); { // Now for the forking and running nsupdate int fildes[2]; int childpid; int r=0; if(pipe(fildes)) return(0); write(fildes[1],str,strlen(str)+1); childpid=fork(); if(!childpid) { // I am the child // Place the pipe read in stdin and close pipe write if(fildes[0]!=0) close(0); if(fildes[0]!=0) dup2(fildes[0],0); close(fildes[1]); // Redirect stdout and stderr to NULL close(1); close(2); open("/dev/null",O_WRONLY,0666); open("/dev/null",O_WRONLY,0666); r=execlp(NSUPDATE_CMD,NSUPDATE_CMD,"-d",NULL); if(r) exit(1); else exit(0); } else { int ret; close(fildes[0]); close(fildes[1]); if(waitpid(childpid,&ret,0)==-1) return(0); return(1); } } } dhis-dns-engine-5.3/CONTRIBUTORS000644 000000 000764 00000000131 10737677150 016170 0ustar00rootdhis000000 000000 Peder Fugl Ian Chilton Nuno Rodriges Jonas Jochum Emile Van Bergen S. Kruit Guus Sliepen