📄 rlm_ippool_tool.c
字号:
/* * rlm_ippool_tool.c * * Version: $Id: rlm_ippool_tool.c,v 1.21 2008/01/05 18:56:32 nbk Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA * * Copyright 2003,2006 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 * *//* The original license follows. This license applies to the tarball at http://www.mavetju.org/unix/general.php Copyright 2003 by Edwin Groothuis, edwin@mavetju.org All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/#include <freeradius-devel/ident.h>RCSID("$Id: rlm_ippool_tool.c,v 1.21 2008/01/05 18:56:32 nbk Exp $")#include <freeradius-devel/libradius.h>#include <fcntl.h>#include <gdbm.h>#include "../../include/md5.h"int active=0;int aflag=0;int cflag=0;int rflag=0;int vflag=0;int nflag=0;int oflag=0;int uflag=0;typedef struct ippool_info { uint32_t ipaddr; char active; char cli[32]; char extra;} ippool_info;#define MAX_NAS_NAME_SIZE 64typedef struct old_ippool_key { char nas[MAX_NAS_NAME_SIZE]; unsigned int port;} old_ippool_key;typedef struct ippool_key { char key[16];} 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,int old);void viewdb(char *sessiondbname,char *indexdbname,char *ipaddress, int old);void tonewformat(char *sessiondbname,char *newsessiondbname);void usage(char *argv0);void addip(char *sessiondbname,char *indexdbname,char *ipaddress, char* NASname, char*NASport, int old) { GDBM_FILE sessiondb; GDBM_FILE indexdb; datum key_datum,data_datum,save_datum; datum nextkey; ippool_key key; old_ippool_key old_key; ippool_info entry; struct in_addr ipaddr; uint8_t key_str[17]; char hex_str[35]; int num=0; int mppp=0; int mode=GDBM_WRITER; signed int rcode; char *cli = NULL; int delete = 0; int port; int extra = 0; int found = 0; sessiondb=gdbm_open(sessiondbname,512,mode,0,NULL); indexdb=gdbm_open(indexdbname,512,mode,0,NULL); if (inet_aton(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; } port = strtoul(NASport,NULL,0); /* Basically from rlm_ippool.c */ if (old){ strlcpy(old_key.nas,NASname,sizeof(old_key.nas)); old_key.port = port; key_datum.dptr = (char *) &old_key; key_datum.dsize = sizeof(old_ippool_key); } else{ char md5_input_str[MAX_STRING_LEN]; FR_MD5_CTX md5_context; snprintf(md5_input_str,MAX_STRING_LEN, "%s %s",NASname,NASport); fr_MD5Init(&md5_context); fr_MD5Update(&md5_context, md5_input_str, strlen(md5_input_str)); fr_MD5Final(key_str, &md5_context); memcpy(key.key,key_str,16); fr_bin2hex(key_str,hex_str,16); hex_str[32] = '\0'; key_datum.dptr = (char *) &key; key_datum.dsize = sizeof(ippool_key); } data_datum = gdbm_fetch(sessiondb, key_datum); if (data_datum.dptr != NULL){ found = 1; memcpy(&entry,data_datum.dptr, sizeof(ippool_info)); free(data_datum.dptr); if (entry.active){ if (old) printf("rlm_ippool_tool: Deleting stale entry for ip/port %s/%u", ipaddress, port); else printf("rlm_ippool_tool: Deleting stale entry for key: '%s'",hex_str); entry.active = 0; save_datum.dptr = key_datum.dptr; save_datum.dsize = key_datum.dsize; data_datum.dptr = (char*) &entry; data_datum.dsize = sizeof(ippool_info); 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; } key_datum.dptr = (char *) &entry.ipaddr; key_datum.dsize = sizeof(uint32_t); data_datum = gdbm_fetch(indexdb, key_datum); if (data_datum.dptr != NULL) { memcpy(&num, data_datum.dptr, sizeof(int)); free(data_datum.dptr); if (num > 0) { num--; data_datum.dptr = (char *) # 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; } if (num > 0 && entry.extra == 1) { gdbm_delete(sessiondb, save_datum); } } } } } key_datum.dptr = NULL; if (cli != NULL){ 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); /* * If we find an entry for the same caller-id and nas with active=1 * then we use that for multilink (MPPP) to work properly. */ if (strcmp(entry.cli,cli) == 0 && entry.active){ if (old){ memcpy(&old_key,key_datum.dptr,sizeof(ippool_key)); if (!strcmp(old_key.nas,NASname)){ mppp = 1; break; } } else{ mppp = 1; break; } } } nextkey = gdbm_nextkey(sessiondb, key_datum); free(key_datum.dptr); key_datum = nextkey; } } if (key_datum.dptr == NULL) { key_datum = gdbm_firstkey(sessiondb); while (key_datum.dptr) { data_datum = gdbm_fetch(sessiondb, key_datum); if (data_datum.dptr != NULL) { memcpy(&entry, data_datum.dptr, sizeof(ippool_info)); free(data_datum.dptr); if (entry.active == 0 && entry.ipaddr == ipaddr.s_addr) { datum tmp; tmp.dptr = (char *) &entry.ipaddr; tmp.dsize = sizeof(uint32_t); data_datum = gdbm_fetch(indexdb, tmp); 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 (key_datum.dptr){ if (found && ! mppp){ datum key_datum_tmp,data_datum_tmp; old_ippool_key old_key_tmp; ippool_key key_tmp; if (old){ strlcpy(old_key_tmp.nas,NASname, sizeof(old_key_tmp.nas)); old_key_tmp.port=port; key_datum_tmp.dptr = (char *) &old_key_tmp; key_datum_tmp.dsize = sizeof(old_ippool_key); } else{ memcpy(key_tmp.key,key_str,16); key_datum_tmp.dptr = (char *) &key_tmp; key_datum_tmp.dsize = sizeof(ippool_key); } data_datum_tmp = gdbm_fetch(sessiondb, key_datum_tmp); if (data_datum_tmp.dptr != NULL) { rcode = gdbm_store(sessiondb, key_datum, data_datum_tmp, GDBM_REPLACE); if (rcode < 0) { printf("rlm_ippool_tool: Failed storing data to %s: %s\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -