Main Page | Class Hierarchy | Class List | File List | Class Members

TacDb.h

00001 // This is part of tacppd project (extended tacacs++ daemon)
00002 // (c) Copyright in 2000-2004 by tacppd team and contributors
00003 // see http://tacppd.org for more information
00004 //
00005 // file: TacDb.h
00006 // description: database adaptation layer
00007 
00008 //  This program is free software; you can redistribute it and/or modify
00009 //  it under the terms of the GNU General Public License as published by
00010 //  the Free Software Foundation; either version 2 of the License, or
00011 //  (at your option) any later version.
00012 //
00013 //  This program is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //  GNU General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU General Public License
00019 //  along with this program; if not, write to the Free Software
00020 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022 // modifications:
00023 //
00024 
00025 // all comments which start from /** - this is comments for KDoc */
00026 //  for classes
00027 //    @short [short description of the class]
00028 //    @author [class author]
00029 //    @version [class version]
00030 //    @see [references to other classes or methods]
00031 //  for methods
00032 //    @see [references]
00033 //    @return [sentence describing the return value]
00034 //    @exception [list the exeptions that could be thrown]
00035 //    @param [name id] [description]    - can be multiple
00036 
00037 #ifndef __TACDB_H__
00038 #define __TACDB_H__
00039 
00040 #include "Snmp.h"       // it sometimes required
00041 #include "global.h"
00042 
00043 namespace tacpp {
00044 
00045 // database table names (this is for version 1 tables)
00046 const string T_DEV = "tacppd_v1_dev";      // network devices table
00047 #define T_USR       "tacppd_v1_usr"        // user/passwords table
00048 #define T_ACC       "tacppd_v1_acc"        // access control
00049 #define T_AVP       "tacppd_v1_avp"        // authorization
00050 #define T_CMD       "tacppd_v1_cmd"        // command authoriz
00051 #define T_ADD       "tacppd_v1_add"        // additional user data
00052 #define T_LOG       "tacppd_v1_log"        // log inform
00053 #define T_TRF       "tacppd_v1_trf"        // intf traffic info
00054 #define T_NF        "tacppd_v1_nf"         // netflow traffic info
00055 #define T_BIL       "tacppd_v1_res"        // resource group
00056 #define T_BILRES    "tacppd_v1_resdata"    // resource group resources
00057 const string T_NFUSR = "tacppd_v1_nfuser"; // user for collect netflow data
00058 
00059 #define T_DEV_ID      "DEvice"
00060 #define T_USR_ID      "USer"
00061 #define T_ACC_ID      "ACcess"
00062 #define T_AVP_ID      "AVp"
00063 #define T_CMD_ID      "COmmand"
00064 #define T_ADD_ID      "ADd"
00065 #define T_LOG_ID      "LOg"
00066 #define T_TRF_ID      "TRFInt"
00067 #define T_BIL_ID      "BILLIng"
00068 #define T_BILRES_ID   "BILLRes"
00069 #define T_NFUSR_ID   "NFUser"
00070 
00071 #define AVP_SERVICE_LEN   15
00072 #define AVP_PROTOCOL_LEN  15
00073 #define AVP_VALUE_LEN     128
00074 
00080 class user_avp {
00083   pthread_mutex_t mutex_;
00086   void lock();
00089   void unlock();
00092   string service_;
00095   string protocol_;
00098   string avp_;
00099  public:
00102   string getService();
00105   void setService(const string);
00109   bool compareService(const string);
00112   string getProtocol();
00115   void setProtocol(const string);
00118   bool compareProtocol(const string);
00121   string getAvp();
00124   void setAvp(const string);
00127   void operator =(user_avp);
00130   bool operator ==(user_avp);
00133   bool operator !=(user_avp);
00136   user_avp();
00139   ~user_avp();
00140 };
00141 
00142 const int CMD_AVP_LEN = 50;
00143 
00149 class cmd_avp {
00152   pthread_mutex_t mutex_;
00155   void lock();
00158   void unlock();
00161   string cmdperm_;
00164   string cmddeny_;
00167   string argperm_;
00170   string argdeny_;
00171  public:
00174   string getCmdperm();
00177   void setCmdperm(const string);
00180   string getCmddeny();
00183   void setCmddeny(const string);
00186   string getArgperm();
00189   void setArgperm(const string);
00192   string getArgdeny();
00195   void setArgdeny(const string);
00198   void operator =(cmd_avp);
00201   bool operator ==(cmd_avp);
00204   cmd_avp();
00207   ~cmd_avp();
00208 };
00209 
00210 #define FROM_PHONE_LEN    64
00211 #define FROM_NAS_LEN      64
00212 #define FROM_PORT_LEN     64
00213 #define FROM_TIME_LEN     64
00214 
00220 class db_user_acc {
00223   pthread_mutex_t mutex_;
00226   void lock();
00229   void unlock();
00232   string phone_;
00235   string nas_;
00238   string port_;
00241   string time_;
00242  public:
00245   string getPhone();
00248   void setPhone(const string);
00251   string getNas();
00254   void setNas(const string);
00257   string getPort();
00260   void setPort(const string);
00263   string getTime();
00266   void setTime(const string);
00269   void operator =(db_user_acc);
00272   bool operator ==(db_user_acc);
00275   db_user_acc();
00278   ~db_user_acc();
00279 };
00280 
00286 class db_bilres {
00289   pthread_mutex_t mutex_;
00292   void lock();
00295   void unlock();
00298   string attribute_;
00301   string value_;
00302  public:
00305   string getAttribute();
00308   void setAttribute(const string);
00311   string getValue();
00314   void setValue(const string);
00317   void operator =(db_bilres);
00320   bool operator !=(db_bilres);
00323   bool operator ==(db_bilres);
00326   db_bilres();
00329   ~db_bilres();
00330 };
00331 
00332 #define DB_USERNAME_LEN     32
00333 #define DB_AUTHSRC_LEN      5
00334 #define DB_PASSWORD_LEN     50
00335 #define DB_ID_LEN           16
00336 #define MAX_AVP             100
00337 #define MAX_ACC             32
00338 #define MAX_BILRES          32
00339 
00345 class db_user {
00348   pthread_mutex_t mutex_;
00351   void lock();
00354   void unlock();
00357   string username_;
00360   string authsrc_;
00363   string password_;
00366   string enable_;
00369   time_t open_;
00372   time_t close_;
00375   string avpid_;
00378   string accid_;
00381   string addid_;
00384   string bilid_;
00387   int bilmodid_;
00390   list<user_avp> avp_;
00393   list<cmd_avp> cmd_;
00396   list<db_user_acc> acc_;
00399   list<db_bilres> bilres_;
00402   bool result_;
00405   bool connect_;
00408   string result_msg_;
00411   int maxsess_;
00412  public:
00415   db_user();
00418   ~db_user();
00421   string getUsername();
00424   void setUsername(const string);
00427   string getAuthsrc();
00430   void setAuthsrc(const string);
00433   string getPassword();
00436   void setPassword(const string);
00439   string getEnable();
00442   void setEnable(const string);
00445   time_t getOpen();
00448   void setOpen(time_t);
00451   time_t getClose();
00454   void setClose(time_t);
00457   string getAvpid();
00460   void setAvpid(const string);
00463   string getAccid();
00466   void setAccid(const string);
00469   string getAddid();
00472   void setAddid(const string);
00475   string getBilid();
00478   void setBilid(const string);
00481   int getBilmodid();
00484   void setBilmodid(int);
00487   void avp_add(user_avp);
00490   int avp_size();
00493   user_avp avp_get(int);
00496   void avp_set(int,user_avp);
00499   void cmd_add(cmd_avp);
00502   int cmd_size();
00505   cmd_avp cmd_get(int);
00508   void acc_add(db_user_acc);
00511   int acc_size();
00514   db_user_acc acc_get(int);
00517   void bilres_add(db_bilres);
00520   int bilres_size();
00523   db_bilres bilres_get(int);
00526   bool getResult();
00529   void setResult(bool);
00532   bool getConnect();
00535   void setConnect(bool);
00538   string getResultMsg();
00541   void setResultMsg(const string);
00544   int getMaxsess();
00547   void setMaxsess(int);
00550   void operator =(db_user);
00553   bool operator ==(db_user);
00556   bool operator !=(db_user);
00557 };
00558 
00559 // network device information
00560 #define DB_NAS_LEN              64
00561 #define DESCRIPTION_LEN         50
00562 #define SNMP_MODULE_LEN         100
00563 #define SNMP_COMMUNITY_LEN      30
00564 #define TACACS_KEY_LEN          50
00565 #define LOGINSTRING_LEN         32
00566 #define PWDSTRING_LEN           32
00567 #define DEV_TYPE_LEN            32
00568 
00569 /*
00570 @short network device information
00571 *
00572 class db_device {
00573         DLoad *dl;                                              // snmp processor module
00574  public:
00575         bool result;                                    // request result
00576         bool connect;                                   // connect to database result
00577         mystring<200> result_msg;               // request result message
00578 
00579         db_device(ipaddr h) {           // constructor
00580                 dl = 0;
00581                 hostaddr = h;
00582                 community<<"public"<<end();
00583                 descr<<"unknown"<<end();
00584                 module<<"none.so"<<end();
00585                 tac_key<<"none"<<end();
00586                 loginstring<<"login:"<<end();
00587                 pwdstring<<"Password:"<<end();
00588                 dev_type<<"host"<<end();
00589                 trfcounter = false;
00590                 snmppoll = false;
00591                 icmppoll = false;
00592                 delay = 60;                                                             // sec
00593                 shutdown = true;                                        // disabled as default
00594                 def_author = false;                             // no permit default authorization - security!
00595         }
00596         ~db_device() {                                                  // destructor
00597                 if(dl) delete dl;
00598         }
00599         // information from config
00600         int delay;                                                                                                                      // polling delay
00601         string dev_type;                                // device type
00602         string descr;                           // description
00603         string community;       // community
00604         ipaddr hostaddr;                                                                                                // hostname/hostaddr
00605         string module;                          // snmp module name
00606         bool trfcounter;                                                                                                // store traffic or not
00607         bool snmppoll;                                                                                                  // poll or not by snmp
00608         bool icmppoll;                                                                                                  // poll or not by icmp
00609         bool shutdown;                                                                                                  // working position or not
00610         string tac_key;                         // tac+ key
00611         string loginstring;     // login req str
00612         string pwdstring;                       // passwd req str
00613         bool def_author;                                                                                                // default authorization
00614         // internal information
00615         bool status;                                                                                                            // last status (on/off)
00616         int rest;                                                                                                                               // polling delay rest
00617         char *load() {                                                                                                  // load module
00618                 static char out[80];
00619                 if(dl) delete dl;
00620                 dl = new DLoad;
00621                 mystring<400> str;
00622 //std::cout<<"try to load module "<<module.get()<<std::endl;
00623                 str<<dl->snmpload(module.get())<<end();
00624                 if(strlen(str.c_str())>0) {
00625                         //TLOG(LOG_ERROR,"snmp module: %s",str.c_str());
00626                         // error in load module
00627                         getnas = 0;
00628                         dropnas = 0;
00629                         return str.c_str();
00630                 }
00631                 getnas = (bool (*)(SnmpDevice*,snmp_d*(*)(char*,char*,char*),char*))
00632                                 dl->getfunc("getnas");
00633                 dropnas = (bool (*)(SnmpDevice*,char*,snmp_d*(*)(char*,char*,char*),
00634                                                 bool (*)(char*,char*,char*,char*,char),char*))
00635                                                         dl->getfunc("dropnas");
00636                 if(getnas == 0 || dropnas == 0) {
00637                         str<<"Unknown snmp module "<<module.get()<<" format"<<end();
00638                         return str.get();
00639                 }
00640                 return "";
00641         }
00642         // dynamically loaded functions for snmp processing
00643         bool (*getnas)(SnmpDevice*,snmp_d*(*)(char*,char*,char*),char*);
00644         bool (*dropnas)(SnmpDevice*,char*,
00645                 snmp_d*(*)(char*,char*,char*),
00646                     bool(*)(char*,char*,char*,char*,char),char*);
00647         void operator =(db_device a) {
00648                 delay = a.delay;
00649                 dev_type = a.dev_type;
00650                 descr = a.descr;
00651                 community = a.community;
00652                 hostaddr = a.hostaddr;
00653                 module = a.module;
00654                 trfcounter = a.trfcounter;
00655                 snmppoll = a.snmppoll;
00656                 icmppoll = a.icmppoll;
00657                 shutdown = a.shutdown;
00658                 tac_key = a.tac_key;
00659                 loginstring = a.loginstring;
00660                 pwdstring = a.pwdstring;
00661                 def_author = a.def_author;
00662                 status = a.status;
00663                 rest = a.rest;
00664                 // we do not load module here - do it manually
00665         }
00666 };
00667 */
00668 
00669 #define DB_H323_ORIGIN_LEN  15
00670 #define DB_H323_TYPE_LEN    15
00671 
00677 class log_user {
00678  public:
00681   int action;
00684   ipaddr server;
00687   int task_id;
00690   time_t servtime;
00693   time_t start_time;
00696   string username;
00699   string from;
00702   string port;
00705   unsigned int elapsed;
00708   ipaddr ip;
00711   ipaddr nas;
00714   string service;
00717   string protocol;
00720   int disc_cause;
00723   int disc_cause_ext;
00726   unsigned int bytes_in;
00729   unsigned int bytes_out;
00732   unsigned int rx_speed;
00735   unsigned int tx_speed;
00736   // VoIP parameters
00739   string h323_call_origin;
00742   string h323_call_type;
00745   int h323_disconnect_cause;
00748   int h323_voice_quality;
00751   ipaddr h323_remote_address;
00754   time_t h323_connect_time;
00757   time_t h323_disconnect_time;
00760   log_user() {
00761     server = UNKNOWN_IP;
00762     nas = UNKNOWN_IP;
00763     ip = UNKNOWN_IP;
00764     action = 0;
00765     servtime   = 0;
00766     start_time = 0;
00767     elapsed = 0;
00768     disc_cause = 0;
00769     disc_cause_ext = 0;
00770     bytes_in   = 0;
00771     bytes_out  = 0;
00772     rx_speed   = 0;
00773     tx_speed   = 0;
00774     task_id    = 0;
00775     h323_connect_time = 0;
00776     h323_disconnect_time = 0;
00777     h323_disconnect_cause = 0;
00778     h323_voice_quality = 0;
00779   }
00782   ~log_user() {}
00783 };
00784 
00790 class DbList {
00793   pthread_mutex_t mutex_;
00796   void lock();
00799   void unlock();
00802   list<DbTable*> dt_;
00803  public:
00806   void add(const string,const string,const string);
00809   DbTable *get(const string);
00812   DbTable *get(int);
00815   DbList();
00818   ~DbList();
00821   int size();
00822 };
00823 
00827 //class DbModule : public md5 {
00828 class DbModule {
00831   pthread_mutex_t mutex_;
00834   void lock();
00837   void unlock();
00840   //int cid_;
00843   string dbname_;
00846   ipaddr location_;
00849   unsigned int dbport_;
00852   string cryptokey_;
00855   string username_;
00858   string password_;
00861   string module_;
00864   bool shutdown_;
00865  public:
00868   void setLocation(ipaddr);
00871   ipaddr getLocation();
00874   void setDbname(string);
00877   string getDbname();
00880   void setDbport(unsigned int);
00883   unsigned int getDbport();
00886   void setCryptokey(string);
00889   string getCryptokey();
00892   void setUsername(const string);
00895   string getUsername();
00898   void setPassword(const string);
00901   string getPassword();
00904   void setModule(const string);
00907   string getModule();
00910   void setShutdown(bool);
00913   bool getShutdown();
00916   void setdbid(const char*,const char*,int);
00919   string getdbid();
00922   string encrypt(char*,char*);
00925   string decrypt(char*,char*);
00928   void create_md5_hash(unsigned char*, unsigned char*);
00931   void md5_xor(unsigned char*,unsigned char*);
00932 
00933   // pointers to functions for dynamic loading
00934 
00937   void (*dl_init)();
00940   void (*dl_destroy)();
00943   int (*dl_open)(char*,int,char*,char*,char*,char*);
00946   bool (*dl_close)(int);
00949   bool (*dl_query)(int,DbTable*,char*);
00952   char* (*dl_function)(int,char*,char*);
00955   bool (*dl_get)(int,DbTable*,int,char*);
00958   bool (*dl_create)(int,DbTable*,char*);
00961   bool (*dl_create_idx)(int,DbTable*,char*);
00964   bool (*dl_add)(int,DbTable*,char*);
00967   bool (*dl_update)(int,DbTable*,char*);
00970   bool (*dl_del)(int,DbTable*,char*);
00973   DbModule(const char*,const char*,const char*,int,const char*,const char*,const char*);
00976   bool db_open(int*);
00979   bool db_close(int);
00982   bool db_query(int,DbTable*);
00985   string db_function(int,char*);
00988   bool db_get(int,DbTable*,int);
00991   bool db_create(int,DbTable*,char*);
00994   bool db_create_idx(int,DbTable*,char*);
00997   bool db_add(int,DbTable*);
01000   bool db_update(int,DbTable*);
01003   bool db_del(int,DbTable*);
01006   ~DbModule();
01007 };
01008 
01014 class TacDb : public DbModule {
01017   pthread_mutex_t Mutex_;
01020   void lock();
01023   void unlock();
01026   ipaddr our_ip_;
01027  public:
01030   DLoad *dl;
01033   TacDb(const char*,const char*,const char*,int,const char*,const char*,const char*);
01036   ~TacDb();
01039   db_user get(const string);
01042   int size();
01045   string direct_function_query(char*);
01048   string load();
01051   string create();
01052 
01053   // universal manipulation functions
01056   string del(string,list<string>,list<string>);
01059   string show(string,list<string>,list<string>);
01062   list<string> sshow(string,list<string>,list<string>);
01065   string add(string,list<string>,list<string>);
01068   string modify(string,list<string>,list<string>,list<string>,list<string>);
01071   string add_log(log_user*);
01074   string add_trf(char*,SnmpOut*);
01077   int open_nf();
01080   void close_nf(int);
01083   string add_nf(int,Bit32,Bit32,Bit32,time_t,time_t,unsigned long,unsigned long,int);//NfData*);
01086   string get_table_name(int);
01089   string get_table_mask(int);
01092   string get_table_descr(int);
01095   string get_field_name(int,int);
01098   string get_field_descr(int,int);
01101   string get_field_defv(int,int);
01104   int get_field_type(int,int);
01107   int get_field_num(int);
01108 };
01109 
01110 };
01111 
01112 #endif //__TACDB_H__

Generated on Thu Jul 21 23:09:46 2005 for tacppd.kdevelop by doxygen 1.3.5