Add support for adding entries to ippool databases.
authorphampson <phampson>
Thu, 2 Oct 2003 00:43:20 +0000 (00:43 +0000)
committerphampson <phampson>
Thu, 2 Oct 2003 00:43:20 +0000 (00:43 +0000)
src/modules/rlm_ippool/Makefile.in
src/modules/rlm_ippool/rlm_ippool_tool.8
src/modules/rlm_ippool/rlm_ippool_tool.c
src/modules/rlm_ippool/rlm_ippool_tool.pod

index 9633f5c..1597b9f 100644 (file)
@@ -12,7 +12,7 @@ rlm_ippool_tool: rlm_ippool_tool.o
        $(LIBTOOL) --mode=link $(CC) $^ $(LIBS) $(RLM_LIBS) -o $@
 
 rlm_ippool_tool.8: rlm_ippool_tool.pod
-       pod2man --release=" " --date="March 8th, 2003" --center="FreeRADIUS Daemon" --section=8 rlm_ippool_tool.pod rlm_ippool_tool.8
+       pod2man --release=" " --date="October 2nd, 2003" --center="FreeRADIUS Daemon" --section=8 rlm_ippool_tool.pod rlm_ippool_tool.8
 
 $(STATIC_OBJS): $(HEADERS)
 
index 8bccb1d..2711acf 100644 (file)
 .\" ========================================================================
 .\"
 .IX Title "RLM_IPPOOL_TOOL 8"
-.TH RLM_IPPOOL_TOOL 8 "March 8th, 2003" " " "FreeRADIUS Daemon"
+.TH RLM_IPPOOL_TOOL 8 "October 2nd, 2003" " " "FreeRADIUS Daemon"
 .SH "NAME"
 \&\fBrlm_ippool_tool\fR \- dump the contents of the FreeRadius ippool database files.
 .SH "SYNOPSIS"
 .IX Header "SYNOPSIS"
 \&\fBrlm_ippool_tool\fR [\fBoptions\fR] <\fIsession-db\fR> <\fIindex-db\fR> [\fBipaddress\fR]
+.PP
+\&\fBrlm_ippool_tool\fR \fI\-n\fR <\fIsession-db\fR> <\fIindex-db\fR> <\fIipaddress\fR> <\fInasIP\fR> <\fInasPort\fR>
 .SH "DESCRIPTION"
 .IX Header "DESCRIPTION"
-\&\fBrlm_ippool_tool\fR dumps the contents of the FreeRadius ippool databases for
+\&\fBrlm_ippool_tool\fR dumps the contents of the FreeRADIUS ippool databases for
 analyses or for removal of active (stuck?) entries.
 .PP
+\&\fBrlm_ippool_tool \-n\fR adds a usage entry to the FreeRADIUS ippool databases.
+.PP
 Options are:
 .IP "\fB\-a\fR" 8
 .IX Item "-a"
@@ -153,9 +157,12 @@ Remove active entries.
 .IP "\fB\-v\fR" 8
 .IX Item "-v"
 Verbose report of all entries
+.IP "\fB\-n\fR" 8
+.IX Item "-n"
+Database addition mode (\*(L"New\*(R")
 .SH "EXAMPLES"
 .IX Header "EXAMPLES"
-The syntax in the FreeRadius radiusd.conf:
+Given the syntax in the FreeRadius radiusd.conf:
 .PP
 .Vb 7
 \& ippool myippool {
@@ -206,10 +213,20 @@ To see only information of one entry, use:
 \& $ rlm_ippool_tool -v ip-pool.db ip-index.db 192.168.1.1
 \& NAS:172.16.1.1 port:0x90 - ipaddr:192.168.1.1 active:0 cli:0 num:0
 .Ve
+.PP
+To add an \s-1IP\s0 address usage entry, use:
+.PP
+.Vb 3
+\& $ rlm_ippool_tool -n ip-pool.db ip-index.db 192.168.1.1 172.16.1.1 0x90
+\& rlm_ippool_tool: Allocating ip to nas/port: 172.16.1.1/144
+\& rlm_ippool_tool: num: 1
+.Ve
 .SH "AUTHOR"
 .IX Header "AUTHOR"
+Currently part of the FreeRADIUS Project (http://www.freeradius.org)
 Originally by Edwin Groothuis, edwin@mavetju.org (http://www.mavetju.org)
 .PP
 Mailing list details are at http://www.freeradius.org/
 .SH "BUGS"
 .IX Header "BUGS"
+\&\-n doesn't support incrementing Multilink-PPP usage. It assumes usage is 1.
index fbbf7ac..0ea6b05 100755 (executable)
@@ -17,6 +17,7 @@
  *   along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
+ * Copyright 2003  FreeRADIUS Project, http://www.freeradius.org/
  * Copyright 2003  Edwin Groothuis, edwin@mavetju.org
  * Permission from Edwin Groothuis for release under GPL is archived here:
  * http://lists.cistron.nl/archives/freeradius-devel/2003/09/frm00247.html
@@ -65,6 +66,7 @@ int aflag=0;
 int cflag=0;
 int rflag=0;
 int vflag=0;
+int nflag=0;
 
 typedef struct ippool_info {
     uint32_t        ipaddr;
@@ -82,6 +84,150 @@ typedef struct ippool_key {
 #define MATCH_IP(ip1,ip2) ((ip1)==NULL || strcmp((ip1),(ip2))==0)
 #define MATCH_ACTIVE(info) ((info).active==1 || !aflag)
 
+void addip(char *sessiondbname,char *indexdbname,char *ipaddress, char* NASname, char*NASport) {
+    GDBM_FILE sessiondb;
+    GDBM_FILE indexdb;
+    datum key_datum,keynext_datum,data_datum;
+       datum nextkey;
+    ippool_key key;
+    ippool_info entry;
+    struct in_addr ipaddr;
+    int num;
+    int mode=GDBM_WRITER;
+    int rcode;
+       char *cli = NULL;
+       int delete = 0;
+
+    sessiondb=gdbm_open(sessiondbname,512,mode,0,NULL);
+    indexdb=gdbm_open(indexdbname,512,mode,0,NULL);
+
+       if (inet_pton(AF_INET, ipaddress, &ipaddr) == 0)
+       {
+               printf("rlm_ippool_tool: Unable to convert IP address '%s'\n", ipaddress);
+               return;
+       }
+       
+    if (sessiondb==NULL)
+       {
+               printf("rlm_ippool_tool: Unable to open DB '%s'\n", sessiondbname);
+               return;
+       }
+       
+    if (indexdb==NULL)
+       {
+               printf("rlm_ippool_tool: Unable to open DB '%s'\n", indexdbname);
+               return;
+       }
+       
+       /* Basically from rlm_ippool.c */
+
+       memset(key.nas,0,MAX_NAS_NAME_SIZE);
+       strncpy(key.nas,NASname,MAX_NAS_NAME_SIZE -1 );
+       key.port = strtoul(NASport,NULL,0);
+       key_datum.dptr = (char *) &key;
+       key_datum.dsize = sizeof(ippool_key);
+
+       key_datum = gdbm_firstkey(sessiondb);
+       while(key_datum.dptr){
+               data_datum = gdbm_fetch(sessiondb, key_datum);
+               if (data_datum.dptr){
+                       memcpy(&entry,data_datum.dptr, sizeof(ippool_info));
+                       free(data_datum.dptr);  
+                       /* Found our entry? */
+                       if (entry.ipaddr == ipaddr.s_addr){
+                               datum tmp;
+
+                               tmp.dptr = (char *) &entry.ipaddr;
+                               tmp.dsize = sizeof(uint32_t);
+                               data_datum = gdbm_fetch(indexdb, tmp);
+
+                               /*
+                                * If we find an entry in the ip index and the number is zero (meaning
+                                * that we haven't allocated the same ip address to another nas/port pair)
+                                * or if we don't find an entry then delete the session entry so
+                                * that we can change the key (nas/port)
+                                * Else we don't delete the session entry since we haven't yet deallocated the
+                                * corresponding ip address and we continue our search.
+                                */
+
+                               if (data_datum.dptr){
+                                       memcpy(&num,data_datum.dptr, sizeof(int));
+                                       free(data_datum.dptr);
+                                       if (num == 0){
+                                               delete = 1;
+                                               break;
+                                       }
+                               }
+                               else{
+                                       delete = 1;
+                                       break;
+                               }
+                       }
+               }
+               nextkey = gdbm_nextkey(sessiondb, key_datum);
+               free(key_datum.dptr);
+               key_datum = nextkey;
+       }
+       /*
+        * If we have found our entry set active to 1 
+        */
+       if (key_datum.dptr){
+               entry.active = 1;
+               data_datum.dptr = (char *) &entry;
+               data_datum.dsize = sizeof(ippool_info);
+
+               if (delete){
+                       /*
+                        * Delete the entry so that we can change the key
+                        */
+                       gdbm_delete(sessiondb, key_datum);
+               }
+               free(key_datum.dptr);
+               memset(key.nas,0,MAX_NAS_NAME_SIZE);
+               strncpy(key.nas,NASname,MAX_NAS_NAME_SIZE -1 );
+               key.port = strtoul(NASport,NULL,0);
+               key_datum.dptr = (char *) &key;
+               key_datum.dsize = sizeof(ippool_key);
+               
+               printf("rlm_ippool_tool: Allocating ip to nas/port: %s/%u\n",key.nas,key.port);
+               rcode = gdbm_store(sessiondb, key_datum, data_datum, GDBM_REPLACE);
+               if (rcode < 0) {
+                       printf("rlm_ippool_tool: Failed storing data to %s: %s\n",
+                               sessiondbname, gdbm_strerror(gdbm_errno));
+                       gdbm_close(indexdb);
+                       gdbm_close(sessiondb);
+                       return;
+               }
+
+               /* Increase the ip index count */
+               key_datum.dptr = (char *) &entry.ipaddr;
+               key_datum.dsize = sizeof(uint32_t);     
+               data_datum = gdbm_fetch(indexdb, key_datum);
+               if (data_datum.dptr){
+                       memcpy(&num,data_datum.dptr,sizeof(int));
+                       free(data_datum.dptr);
+               }
+               num=1;
+               printf("rlm_ippool_tool: num: %d\n",num);
+               data_datum.dptr = (char *) &num;
+               data_datum.dsize = sizeof(int);
+               rcode = gdbm_store(indexdb, key_datum, data_datum, GDBM_REPLACE);
+               if (rcode < 0) {
+                       printf("rlm_ippool_tool: Failed storing data to %s: %s\n",
+                               indexdbname, gdbm_strerror(gdbm_errno));
+                       gdbm_close(indexdb);
+                       gdbm_close(sessiondb);
+                       return;
+               }
+                       
+
+               printf("rlm_ippool_tool: Allocated ip %s to client on nas %s,port %u\n",ipaddress,
+                               key.nas,strtoul(NASport,NULL,0));
+       }
+    gdbm_close(indexdb);
+    gdbm_close(sessiondb);
+}
+
 void viewdb(char *sessiondbname,char *indexdbname,char *ipaddress) {
     GDBM_FILE sessiondb;
     GDBM_FILE indexdb;
@@ -176,8 +322,10 @@ void usage(char *argv0) {
     printf("-c: report number of active entries\n");
     printf("-r: remove active entries\n");
     printf("-v: verbose report of all entries\n");
-    printf("If an ipaddress is specified then only that address is used to\n");
-    printf("limit the actions or output to that address only.\n");
+    printf("If an ipaddress is specified then that address is used to\n");
+    printf("limit the actions or output.\n");
+    printf("Usage: %s -n  <session-db> <index-db> <ipaddress> <nasIP> <nasPort>\n",argv0);
+    printf("-n: Mark the entry nasIP/nasPort as having ipaddress\n");
     exit(0);
 }
 
@@ -185,21 +333,25 @@ int main(int argc,char **argv) {
     int ch;
     char *argv0=argv[0];
 
-    while ((ch=getopt(argc,argv,"acrv"))!=-1)
+    while ((ch=getopt(argc,argv,"acrvn"))!=-1)
        switch (ch) {
        case 'a': aflag++;break;
        case 'c': cflag++;break;
        case 'r': rflag++;break;
        case 'v': vflag=1;break;
+       case 'n': nflag=1;break;
        default: usage(argv0);
        }
     argc -= optind;
     argv += optind;
 
-    if (argc!=2 && argc!=3)
-       usage(argv0);
-    else
-       viewdb(argv[0],argv[1],argv[2]);
-    if (cflag) printf("%d\n",active);
+    if ((argc==2 || argc==3) && !nflag) {
+               viewdb(argv[0],argv[1],argv[2]);
+               if (cflag) printf("%d\n",active);
+       } else 
+               if (argc==5 && nflag)
+                       addip(argv[0],argv[1],argv[2],argv[3],argv[4]);
+               else
+                       usage(argv0);
     return 0;
 }
index 274af30..2719aa4 100644 (file)
@@ -10,11 +10,15 @@ B<rlm_ippool_tool> - dump the contents of the FreeRadius ippool database files.
 
 B<rlm_ippool_tool> [B<options>] <I<session-db>> <I<index-db>> [B<ipaddress>]
 
+B<rlm_ippool_tool> I<-n> <I<session-db>> <I<index-db>> <I<ipaddress>> <I<nasIP>> <I<nasPort>>
+
 =head1 DESCRIPTION
 
-B<rlm_ippool_tool> dumps the contents of the FreeRadius ippool databases for
+B<rlm_ippool_tool> dumps the contents of the FreeRADIUS ippool databases for
 analyses or for removal of active (stuck?) entries.
 
+B<rlm_ippool_tool -n> adds a usage entry to the FreeRADIUS ippool databases.
+
 Options are:
 
 =over 8
@@ -35,11 +39,15 @@ Remove active entries.
 
 Verbose report of all entries
 
+=item B<-n>
+
+Database addition mode ("New")
+
 =back
 
 =head1 EXAMPLES
 
-The syntax in the FreeRadius radiusd.conf:
+Given the syntax in the FreeRadius radiusd.conf:
 
  ippool myippool {
        range-start = 192.168.1.0
@@ -81,11 +89,18 @@ To see only information of one entry, use:
  $ rlm_ippool_tool -v ip-pool.db ip-index.db 192.168.1.1
  NAS:172.16.1.1 port:0x90 - ipaddr:192.168.1.1 active:0 cli:0 num:0
 
+To add an IP address usage entry, use:
+
+ $ rlm_ippool_tool -n ip-pool.db ip-index.db 192.168.1.1 172.16.1.1 0x90
+ rlm_ippool_tool: Allocating ip to nas/port: 172.16.1.1/144
+ rlm_ippool_tool: num: 1
+ rlm_ippool_tool: Allocated ip 192.168.1.1 to client on nas 172.16.1.1,port 144
+
 =head1 AUTHOR
 
+Currently part of the FreeRADIUS Project (http://www.freeradius.org)
 Originally by Edwin Groothuis, edwin@mavetju.org (http://www.mavetju.org)
 
 Mailing list details are at http://www.freeradius.org/
 
 =head1 BUGS
-