This commit is contained in:
2021-06-15 16:38:30 +01:00
commit 8c5c5c0f45
12 changed files with 468 additions and 0 deletions

59
base64.c Normal file
View File

@@ -0,0 +1,59 @@
// Shamelessly snarfed from Peter Mac Millan =/
#include <string.h>
#include <stdio.h>
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 );
}

20
compile Executable file
View File

@@ -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

20
config.h Normal file
View File

@@ -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

24
debug.c Normal file
View File

@@ -0,0 +1,24 @@
// SegFault & Debug handler - By Nik_Doof
#include "dhsd.h"
#include <stdio.h>
#include <unistd.h>
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
}

57
devcheck.c Normal file
View File

@@ -0,0 +1,57 @@
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#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;
}

39
dhsd.h Normal file
View File

@@ -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 <syslog.h>
#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

1
docs/INSTALL Normal file
View File

@@ -0,0 +1 @@
Edit `config.h' to set your options then run `compile' script then run dhsd...

25
docs/README Normal file
View File

@@ -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

17
dprintf.c Normal file
View File

@@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#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));
}

64
main.c Normal file
View File

@@ -0,0 +1,64 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#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;
}

65
pdetach.c Normal file
View File

@@ -0,0 +1,65 @@
#include <unistd.h>
#include <signal.h>
#include <sysexits.h>
#ifdef SIGTSTP
#include <sys/file.h>
#include <sys/ioctl.h>
#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
}

77
updateip.c Normal file
View File

@@ -0,0 +1,77 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#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;
}