help-gnu-radius
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gnu-radius] A Filter script in C


From: Erik DUH Olson
Subject: [Help-gnu-radius] A Filter script in C
Date: Wed, 5 Oct 2005 11:14:47 -0400

/*--------------------------------------------
FYI - for anyone who might find this useful:

Here is a simple filter that I wrote in standard C language.  It logs it's
activity to a file called log.txt.
It checks a succession of text files for a match against the caller's ANI
(CLID).
new.txt - new users
ani.txt - regular users
beta.txt - beta users
prem.txt - premium users
It also checks a "deny" file called deny.txt

If the caller does not have a visible ANI (CLID) then the caller is allowed
to enter his ANI as the first 10 digits of the user ID.  If the caller's ANI
(CLID) is found inside the "premium" file then they are allowed to log in
using just their phone number as the user ID regardless of where they are
calling from (meaning that regular users can only call from a pre-arranged
phone number but premium users can call from anywhere.  The log file shows
under which file and rules the user was authenticated or not authenticated.
Maintaining the list is extremely easy through apache and cgi scripts.  My
files contain  thousands of user id's and there does not seem to be any kind
of time delay in scanning them.  The format of each text file is simply
ani(cr)ani(cr)ani(cr)... etc

-Erik Olson
--------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
 {
 FILE *f;
 FILE *af;
 int i;
 char strANI[99];
 char strUSER[99];
 char strARG[99];

 if( (f = fopen("/usr/local/etc/raddb/log.txt", "a")) == NULL)
  {
     /* error-handling code */
     printf("Problem opening file\n");
     /* more instructions may be added */
  }
 else
  {
  for (i=0;i<argc;i++)
   fprintf(f,"argv[%d] of %d == %s\n",i,argc,argv[i]);
  }
 /* Authenticate this user now */
 if (argc==1) exit(1); /* if no args then abort */

 strcpy(strUSER,argv[argc-1]);
 if (strlen(strUSER)>10) strUSER[10]=0; /* stick a null here */
 fprintf(f,"User ID is : %s\n",strUSER);


 /* Check in prem dir */
 fprintf(f,"Checking prem file\n");
 if ((af=fopen("/usr/local/etc/raddb/prem.txt","r")) == NULL)
  {
  fprintf(f,"Problem opening prem file\n");
  exit(1);
  }
 while ( fgets(strANI, 11, af) != NULL)
  {
  if (!strcmp(strANI,strUSER))
   {
   fprintf(f,"%s prem Auth OK\n",strUSER);
   fclose (af);
   fclose (f);
   exit(0);
   }
  }
 fclose(af);



 /* if no ANI available, try use the user ID instead */
  i=strlen(argv[1]);
 if (i==10)
  {
  /* arg 1 is 10 digits, so use this ANI unconditionally */
  strcpy(strARG,argv[1]);
  fprintf(f,"auth on ANI: %s\n",strARG);
  }
 else
  {
  /* arg 1 is not 10 digits, so use up to the first 10 digits of the LAST
arg */
  strcpy(strARG,argv[argc-1]);
  if (strlen(strARG)>10) strARG[10]=0; /* stick a null here */
  fprintf(f,"auth on user: %s\n",strARG);
  }

 /* So right now, strARG is either the ANI or it is the first 10 digits of
the 2nd to the last arg */


 i=strlen(strARG);
 if (i!=10)
  {
  /* First ARG is not an ANI, and the last ARG is not 10 digits (plus
@evodialer.com) */
  fprintf(f,"Bad len on [%s]\n",strARG);
  }
 else
  {
  if ((af=fopen("/usr/local/etc/raddb/deny.txt","r")) == NULL)
   {
   /* (FATAL) Error opening file */
   fprintf(f,"Problem opening DENY file\n");
   exit(1);
   }
  while ( fgets(strANI, 11, af) != NULL)
   {
   if (!strcmp(strANI,strARG))
    {
    fprintf(f,"%s *** DENY ANI ***\n",strARG);
    fclose (af);
    fclose (f);
    exit(1);
    }
   }
  fclose(af);

  if ((af=fopen("/usr/local/etc/raddb/ani.txt","r")) == NULL)
   {
   /* (FATAL) Error opening file */
   fprintf(f,"Problem opening ANI file\n");
   exit(1);
   }
  while ( fgets(strANI, 11, af) != NULL)
   {
   if (!strcmp(strANI,strARG))
    {
    fprintf(f,"%s ANI Auth OK\n",strARG);
    fclose (af);
    fclose (f);
    exit(0);
    }
   }
  fclose(af);

  if ((af=fopen("/usr/local/etc/raddb/new.txt","r")) == NULL)
   {
   /* (FATAL) Error opening file */
   fprintf(f,"Problem opening NEW file\n");
   exit(1);
   }
  while ( fgets(strANI, 11, af) != NULL)
   {
   if (!strcmp(strANI,strARG))
    {
    fprintf(f,"%s NEW Auth OK\n",strARG);
    fclose (af);
    fclose (f);
    exit(0);
    }
   }
  fclose(af);

  if ((af=fopen("/usr/local/etc/raddb/beta.txt","r")) == NULL)
   {
   /* (FATAL) Error opening file */
   fprintf(f,"Problem opening BETA file\n");
   exit(1);
   }
  while ( fgets(strANI, 11, af) != NULL)
   {
   if (!strcmp(strANI,strARG))
    {
    fprintf(f,"%s BETA Auth OK\n",strARG);
    fclose (af);
    fclose (f);
    exit(0);
    }
   }
  fclose(af);


  fprintf(f,"%s Auth BAD\n",strARG);
  fclose(f);
  exit(1);
  }
 }








reply via email to

[Prev in Thread] Current Thread [Next in Thread]