commit 8c5c5c0f4584a974512797b4245d241ac96013e9 Author: Andrew Williams Date: Tue Jun 15 16:38:30 2021 +0100 v0.0.1 diff --git a/base64.c b/base64.c new file mode 100644 index 0000000..06dd820 --- /dev/null +++ b/base64.c @@ -0,0 +1,59 @@ + +// Shamelessly snarfed from Peter Mac Millan =/ + +#include +#include + +static const unsigned char BaseTable[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + +void benc( unsigned char *dst, unsigned char *s ) +{ + int n = strlen( s ); + int n3byt = n / 3; + int k = n3byt * 3; + int nrest = n % 3; + int i = 0; + int dstlen = 0; + + while ( i < k ) + { + dst[dstlen++] = BaseTable[(( s[i] & 0xFC) >> 2)]; + dst[dstlen++] = BaseTable[(((s[i] & 0x03) << 4) | ((s[i+1] & 0xF0) >> 4))]; + dst[dstlen++] = BaseTable[(((s[i+1] & 0x0F) << 2) | ((s[i+2] & 0xC0) >> 6))]; + dst[dstlen++] = BaseTable[( s[i+2] & 0x3F)]; + + i += 3; + } + + if (nrest==2) + { + dst[dstlen++] = BaseTable[(( s[k] & 0xFC) >> 2)]; + dst[dstlen++] = BaseTable[(((s[k] & 0x03) << 4) | ((s[k+1] & 0xF0) >> 4))]; + dst[dstlen++] = BaseTable[(( s[k+1] & 0x0F) << 2)]; + } + else if (nrest==1) + { + dst[dstlen++] = BaseTable[((s[k] & 0xFC) >> 2)]; + dst[dstlen++] = BaseTable[((s[k] & 0x03) << 4)]; + } + + while (dstlen%3) + dst[dstlen++] = '='; + + dst[dstlen] = '\0'; +} + +void encbasic( char *d, const char *u, const char *p ) +{ + char _buf[4*1024]; + + strcpy( _buf, u ); + strcat( _buf, ":" ); + strcat( _buf, p ); + benc( d, _buf ); +} + diff --git a/compile b/compile new file mode 100755 index 0000000..15cae40 --- /dev/null +++ b/compile @@ -0,0 +1,20 @@ +#!/bin/bash + +# main init +gcc -c -ggdb -Wall -o main.o main.c + +# library routines +gcc -c -ggdb -Wall -o pdetach.o pdetach.c +gcc -c -ggdb -Wall -o dprintf.o dprintf.c +gcc -c -ggdb -Wall -o base64.o base64.c + +# program functions +gcc -c -ggdb -Wall -o devcheck.o devcheck.c +gcc -c -ggdb -Wall -o updateip.o updateip.c +gcc -c -ggdb -Wall -o debug.o debug.c + +# consolodation +gcc -ggdb -Wall -o dhsd main.o debug.o pdetach.o dprintf.o base64.o devcheck.o updateip.o + +# cleanup +rm *.o diff --git a/config.h b/config.h new file mode 100644 index 0000000..bbdebd3 --- /dev/null +++ b/config.h @@ -0,0 +1,20 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +// Config variables + +#define USR_LOGIN "nikdoof" +#define USR_PASSWD "poobeast" +#define USR_HOST "doofy" +#define USR_DOM "2y.net" +#define USR_DEV "ppp0" + +#define SLEEP_TIME 10 + +// use syslog y/n? +#define USE_SYSLOG + +// use debug (no backgrounding and msgs) y/n? +#undef DEBUG + +#endif diff --git a/debug.c b/debug.c new file mode 100644 index 0000000..b694cc1 --- /dev/null +++ b/debug.c @@ -0,0 +1,24 @@ +// SegFault & Debug handler - By Nik_Doof + +#include "dhsd.h" +#include +#include + +void han_segv() +{ +#ifdef USE_SYSLOG + closelog(); +#endif + close(sockfd); + pdebug("SegFault in DHSD...exiting safely..."); + //return 1; + exit(0); +} + +void pdebug(char *dmsg) +{ +#ifdef DEBUG + printf("DhsD Debug: %s\n",dmsg); +#endif +} + diff --git a/devcheck.c b/devcheck.c new file mode 100644 index 0000000..2697afa --- /dev/null +++ b/devcheck.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "dhsd.h" + + +int devcheck(char *dev) +{ + int fd; + struct sockaddr *sa; + struct sockaddr_in *sin; + struct ifreq ifr; + + char *toldaddr; + + toldaddr = oldaddr; + + fd = socket(PF_INET,SOCK_STREAM,0); + if (fd == -1) { + pdebug("socket() failed"); + return -1; + } + + /* Copy the interface name into the buffer */ + strncpy(ifr.ifr_name,dev,IFNAMSIZ); + + if (ioctl(fd,SIOCGIFADDR,&ifr) == -1) { + pdebug("device ioctl failed"); + oldaddr = '\0'; // Hackaratus? + return -1; + } + /* Now the buffer will contain the information we requested */ + sa = (struct sockaddr *)&(ifr.ifr_addr); + if (sa->sa_family == AF_INET) { + sin = (struct sockaddr_in*) sa; + pdebug(toldaddr); + if (inet_ntoa(sin->sin_addr) != toldaddr) { + oldaddr = inet_ntoa(sin->sin_addr); + close(fd); + return 1; + } else { + pdebug(inet_ntoa(sin->sin_addr)); + close(fd); + return 0; + } + } else { + printf(": Unknown Address Family (%d)\n",sa->sa_family); + } + + close(fd); + return -1; +} diff --git a/dhsd.h b/dhsd.h new file mode 100644 index 0000000..a689f90 --- /dev/null +++ b/dhsd.h @@ -0,0 +1,39 @@ + +#ifndef __DHSD_H__ +#define __DHSD_H__ + +#include "config.h" + +#define DHSD_VERSION "v0.01 Beta" +#define DHSD_INT_VERSION 0001 + +#define DHS_IP "209.249.50.99" +#define DHS_PORT 80 +#define DHS_ADDR "/nic/hosts?" + +#ifdef USE_SYSLOG +#include +#endif + +// pdetach.c - by dyfet@sourceforge.net +void pdetach(void); + +// dprintf.c - by dfx@sourceforge.net +void dprintf(int fd, char *format, ...); + +// devcheck.c +char *oldaddr; +int devcheck(char *dev); + +// updateip.c +int sockfd; +int updateip(char *ipaddr, char *dhsdom, char *dhshost); + +// debug.c +void han_segv(); +void pdebug(char *dmsg); + +// base64.c +void encbasic( char *d, const char *u, const char *p ); + +#endif diff --git a/docs/INSTALL b/docs/INSTALL new file mode 100644 index 0000000..12d87b0 --- /dev/null +++ b/docs/INSTALL @@ -0,0 +1 @@ +Edit `config.h' to set your options then run `compile' script then run dhsd... diff --git a/docs/README b/docs/README new file mode 100644 index 0000000..14e9453 --- /dev/null +++ b/docs/README @@ -0,0 +1,25 @@ +DHSD v0.01 Beta +--------------- + +DHSD is a dynamic update engine for the dhs.org dynamic dns service. +The aim is to provide a quick and efficent update engine that will +monitor selected network devices and check for changes of the IP address. + +Project Development Staff: + + Programmers: + * Nik_Doof (nik_doof@h3o.org) + + Documentors: + * Qwaz (qwaszx@h3o.org) + +PLEASE NOTE, this is a CVS Alpha release (in other words...not working) +i have put this code onto the CVS for users to take a look at and see +how it works. + +Thanks go to: + + * Sourceforge for hosting my project + * Dyfet for his pdetach() code + * DFX for his dprintf() code + diff --git a/dprintf.c b/dprintf.c new file mode 100644 index 0000000..c2b47b2 --- /dev/null +++ b/dprintf.c @@ -0,0 +1,17 @@ +#include +#include +#include + +#define BUFLEN 4096 + +void dprintf(int fd, char *format, ...) +{ + va_list arglist; + char buffer[BUFLEN]; + + va_start(arglist, format); + vsnprintf(buffer, BUFLEN, format, arglist); + va_end(arglist); + + write(fd, buffer, strlen(buffer)); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..eb5a54a --- /dev/null +++ b/main.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include "dhsd.h" + +void cleanup() +{ +#ifdef USE_SYSLOG + closelog(); +#endif + close(sockfd); + exit(0); +} + + +int main(void) +{ + int doext; + char verstring[255]; + int res; + + printf("DhsD %s\n\n",DHSD_VERSION); + printf("Detaching to daemon...\n"); +#ifndef DEBUG + pdetach(); +#endif + signal(SIGINT,cleanup); + signal(SIGKILL,cleanup); + signal(SIGSEGV,han_segv); + +#ifdef USE_SYSLOG + verstring[0] = '\0'; + openlog("dhsd",LOG_PID,LOG_DAEMON); + strncat(verstring, "DhsD ",5); + strncat(verstring, DHSD_VERSION,strlen(DHSD_VERSION)); + strncat(verstring, " loaded...\0",10); + syslog(LOG_NOTICE,verstring); +#endif + + do { + pdebug("loop..."); + if (devcheck(USR_DEV) == 1) { +#ifdef USE_SYSLOG + syslog(LOG_NOTICE,"device ip has changed, update initiated..."); +#endif + do { + res = updateip(oldaddr,USR_DOM,USR_HOST); + if (res != 0) { + sleep(SLEEP_TIME); + } + } while(res != 0); +#ifdef USE_SYSLOG + syslog(LOG_NOTICE,"update complete"); +#endif + } + doext = 0; + sleep(SLEEP_TIME); + } while(doext == 0); + + cleanup(); + return 0; +} diff --git a/pdetach.c b/pdetach.c new file mode 100644 index 0000000..abfa69e --- /dev/null +++ b/pdetach.c @@ -0,0 +1,65 @@ +#include +#include +#include + +#ifdef SIGTSTP +#include +#include +#endif + +/* normally included from paths.h */ + +#ifndef _PATH_TTY +#define _PATH_TTY "/dev/tty" +#endif + +void pdetach(void) +{ + int pid; + int fd; + + /* if we are started from init, no need to become daemon */ + + if(getppid() == 1) + return; + +#ifdef SIGTTOU + signal(SIGTTOU, SIG_IGN); +#endif + +#ifdef SIGTTIN + signal(SIGTTIN, SIG_IGN); +#endif + +#ifdef SIGTSTP + signal(SIGTSTP, SIG_IGN); +#endif + + if((pid = fork()) < 0) + exit(EX_OSERR); + else if(pid > 0) + exit(0); + + +#if defined(SIGTSTP) && defined(TIOCNOTTY) + if(setpgid(0, getpid()) == -1) + exit(EX_OSERR); + + if((fd = open(_PATH_TTY, O_RDWR)) >= 0) + { + ioctl(fd, TIOCNOTTY, NULL); + close(fd); + } +#else + if(setpgrp() == -1) + exit(EX_OSERR); + + signal(SIGHUP, SIG_IGN); + + if((pid = fork()) < 0) + exit(OS_ERR); + else if(pid > 0) + exit(0); + +#endif +} diff --git a/updateip.c b/updateip.c new file mode 100644 index 0000000..65327c8 --- /dev/null +++ b/updateip.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "dhsd.h" + +int sconnect(char *ip) +{ + int isock; + struct sockaddr_in srvaddr; + + isock = socket(AF_INET, SOCK_STREAM, 0); + srvaddr.sin_family = AF_INET; + srvaddr.sin_port = htons(DHS_PORT); + srvaddr.sin_addr.s_addr = inet_addr(ip); + bzero(&(srvaddr.sin_zero),8); + + if (connect(isock, (struct sockaddr *)&srvaddr,sizeof(struct sockaddr)) == -1) { + perror("connect"); + close(isock); + return -1; + } + return isock; +} + +int updateip(char *ipaddr, char *dhsdom, char *dhshost) +{ + char post_data[255]; + char post_head[64]; + char auth_head[128]; + char chostname[50]; + char tmpdatabuf[8192]; + char databuf[4*8192]; + char outbuf[8192]; + char b64str[255]; + int ibytes; + + encbasic(b64str,USR_LOGIN,USR_PASSWD); + sprintf(chostname,"%s.%s",dhshost,dhsdom); + + sprintf(auth_head,"Authorization: Basic %s\r\n",b64str); + + sprintf(post_data,"%stype=4&updatetype=Online&hostscmd=edit&hostscmdstage=2&ip=%s&mx=%s&domain=%s&hostname=%s",DHS_ADDR,ipaddr,chostname,dhsdom,dhshost); + sprintf(post_head,"GET %s HTTP/1.0\r\n",post_data); + + sockfd = sconnect(DHS_IP); + if (sockfd == -1) { + pdebug("sconnect() failed"); + return -1; + } + + sprintf(outbuf,"%s%s\r\n\r\n",post_head,auth_head); + + send(sockfd,outbuf,strlen(outbuf),0); + + do { + ibytes = recv(sockfd, tmpdatabuf, 8192, 0); + if (ibytes > 0) { + tmpdatabuf[ibytes] = '\0'; + strncat(databuf,tmpdatabuf,strlen(tmpdatabuf)); + } + } while (ibytes > 0); + pdebug(tmpdatabuf); + + //close(sockfd); + + pdebug("socket closed"); + + /* Some error checking code could be handy... */ + + return 0; +} +