Cover V05, I11
Article
Figure 1
Figure 2
Listing 1
Listing 2
Listing 3
Sidebar 1

nov96.tar


Listing 3: chkaddr.c

/*
* Divert daemon 1.00
*
* Author : A. Donkers
*	    Le Reseau netwerksystemen B.V.
*
*
* Chkaddr modules
*/

/*
* System includes
*/
#ifdef DEBUG
#include 
#endif
#include 
#include 
#ifndef Linux
#include 
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#ifdef DEBUG
#include 
#endif
#include 

/*
* Application includes
*/
#include "divert.h"
#include "cdc.h"

/*
* Locals (--Yuck)
*/
static	cnf_t	cnfitem;

/*
* Prototypes
*/
void	tokenize(
char *,
int *,
unsigned long *,
char *,
unsigned long *,
unsigned long *,
char *
);

/*
* check both local (me) and remote(them) addresses.
* both should be present in the configfile (or the ALL_ADDR tag).
* remote address maybe masked with addres mask
* If a match is found, the related command is returned.
*/
int	chkaddr(
char *conffile,
unsigned long me,
char *service,
unsigned long them,
char *command
)
{
int	config;
int	goodmatch = 0;
int	bytesread = 0;
char	radrip[16], rmskip[16], ladrip[16];

/*
* Open the config file, if you can
*/
if( (config = open( conffile, O_RDONLY )) < 0 ) {
syslog( LOG_ERR, "Cannot open configfile %s, error %m",
CONFIGFILE );
exit( -1 );
}

/*
* Loop over the conf file
*/
while( !goodmatch && (bytesread = read( config, &cnfitem, sizeof(cnfitem))) > 0 ) {

/* First match local address */
/* ALL_ADDR always matches */
if( cnfitem.me != 0xffffffff ) {
if( cnfitem.me != me ) {
continue;
}
}

/* If the first address matched, match the service */
if( strncmp( cnfitem.service, ALL_ADDR, strlen( ALL_ADDR ) ) != 0 ) {
if( strncmp( cnfitem.service, service, strlen( service ) ) != 0 ) {
continue;
}
}

/* If we've got a local match, try to match the remote side */
if( cnfitem.them != 0xffffffff ) {
/* Match using mask */
if( (cnfitem.them & cnfitem.mask) != (them & cnfitem.mask) ) {
continue;
}
}

/* When we reach this point, all fields have matched */
/* Whether that's good depends on the denied flag */
if( ntohl( cnfitem.flags ) & M_DENIED ) {
return 0;
}

/* We've got a match */
strcpy( command, cnfitem.command );
goodmatch = 1;

break;
}

/*
* Close configfile
*/

close( config );

strcpy( ladrip, inet_ntoa( *((struct in_addr *)&(cnfitem.me)) ) );
strcpy( radrip, inet_ntoa( *((struct in_addr *)&(cnfitem.them)) ) );
strcpy( rmskip, inet_ntoa( *((struct in_addr *)&(cnfitem.mask)) ) );

syslog( LOG_INFO, "Matched local address %s and remote address %s, netmask %s for service %s",
ladrip, radrip, rmskip, cnfitem.service );

return goodmatch;
}

/* end of chkaddr.c */