]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/radius.h
Добавление исходников
[stg.git] / projects / stargazer / plugins / other / radius / radius.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Radius data access plugin for Stargazer
23  *
24  *  $Revision: 1.10 $
25  *  $Date: 2009/12/13 14:17:13 $
26  *
27  */
28
29 #ifndef RADIUS_H
30 #define RADIUS_H
31
32 #include <string>
33 #include <list>
34 #include <pthread.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "os_int.h"
39 #include "base_auth.h"
40 #include "notifer.h"
41 #include "user_ips.h"
42 #include "../../../user.h"
43 #include "../../../users.h"
44 #include "blowfish.h"
45 #include "rad_packets.h"
46
47 using namespace std;
48
49 extern "C" BASE_PLUGIN * GetPlugin();
50
51 #define RAD_DEBUG (1)
52
53 class RADIUS;
54 //-----------------------------------------------------------------------------
55 class RAD_SETTINGS
56 {
57 public:
58     virtual         ~RAD_SETTINGS(){};
59     const string&   GetStrError() const { return errorStr; };
60     int             ParseSettings(const MODULE_SETTINGS & s);
61     uint16_t        GetPort() const;
62     uint32_t        GetServerIP() const;
63     int             GetPassword(string * password) const;
64     int             GetAuthServices(list<string> * svcs) const;
65     int             GetAcctServices(list<string> * svcs) const;
66
67 private:
68     int             ParseIntInRange(const string & str, int min, int max, int * val);
69     int             ParseIP(const string & str, uint32_t * routerIP);
70     int             ParseServices(const vector<string> & str, list<string> * lst);
71
72     uint16_t            port;
73     string              errorStr;
74     string              password;
75     uint32_t            serverIP;
76     list<string>        authServices;
77     list<string>        acctServices;
78 };
79 //-----------------------------------------------------------------------------
80 struct RAD_SESSION {
81     std::string userName;
82     std::string serviceType;
83 };
84 //-----------------------------------------------------------------------------
85 class RADIUS :public BASE_AUTH
86 {
87 public:
88                         RADIUS();
89     virtual             ~RADIUS(){};
90
91     void                SetUsers(USERS * u);
92     void                SetTariffs(TARIFFS *){};
93     void                SetAdmins(ADMINS *){};
94     void                SetTraffcounter(TRAFFCOUNTER *){};
95     void                SetStore(BASE_STORE * );
96     void                SetStgSettings(const SETTINGS * s);
97     void                SetSettings(const MODULE_SETTINGS & s);
98     int                 ParseSettings();
99
100     int                 Start();
101     int                 Stop();
102     int                 Reload() { return 0; };
103     bool                IsRunning();
104
105     const string      & GetStrError() const { return errorStr; };
106     const string        GetVersion() const;
107     uint16_t            GetStartPosition() const;
108     uint16_t            GetStopPosition() const;
109
110     int SendMessage(const STG_MSG &, uint32_t) const { return 0; };
111
112 private:
113     static void *       Run(void *);
114     int                 PrepareNet();
115     int                 FinalizeNet();
116
117     void                InitEncrypt(BLOWFISH_CTX * ctx, const string & password);
118     void                Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
119     void                Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
120
121     int                 Send(const RAD_PACKET & packet);
122     int                 RecvData(RAD_PACKET * packet);
123     int                 ProcessData(RAD_PACKET * packet);
124
125     int                 ProcessAutzPacket(RAD_PACKET * packet);
126     int                 ProcessAuthPacket(RAD_PACKET * packet);
127     int                 ProcessPostAuthPacket(RAD_PACKET * packet);
128     int                 ProcessAcctStartPacket(RAD_PACKET * packet);
129     int                 ProcessAcctStopPacket(RAD_PACKET * packet);
130     int                 ProcessAcctUpdatePacket(RAD_PACKET * packet);
131     int                 ProcessAcctOtherPacket(RAD_PACKET * packet);
132
133     bool                FindUser(user_iter * ui, const std::string & login) const;
134     bool                CanAuthService(const std::string & svc) const;
135     bool                CanAcctService(const std::string & svc) const;
136     bool                IsAllowedService(const std::string & svc) const;
137
138     void                SetUserNotifier(user_iter u);
139     void                UnSetUserNotifier(user_iter u);
140
141     bool                WaitPackets(int sd) const;
142
143     void                PrintServices(const std::list<std::string> & svcs);
144
145     struct Printer : public unary_function<std::string, void>
146     { 
147         void operator()(const std::string & line)
148         { 
149             printfd("radius.cpp", "'%s'\n", line.c_str()); 
150         }; 
151     };
152     struct SPrinter : public unary_function<std::pair<std::string, RAD_SESSION>, void>
153     { 
154         void operator()(const std::pair<std::string, RAD_SESSION> & it)
155         { 
156             printfd("radius.cpp", "%s - ('%s', '%s')\n", it.first.c_str(), it.second.userName.c_str(), it.second.serviceType.c_str()); 
157         }; 
158     };
159
160     BLOWFISH_CTX        ctx;
161
162     mutable string      errorStr;
163     RAD_SETTINGS        radSettings;
164     MODULE_SETTINGS     settings;
165     list<string>        authServices;
166     list<string>        acctServices;
167     map<string, RAD_SESSION> sessions;
168
169     bool                nonstop;
170
171     bool                isRunning;
172
173     USERS *             users;
174     const SETTINGS *    stgSettings;
175     const BASE_STORE *  store;
176
177     pthread_t           thread;
178     pthread_mutex_t     mutex;
179
180     int                 sock;
181     struct sockaddr_in  inAddr;
182     socklen_t           inAddrLen;
183     uint16_t            port;
184     uint32_t            serverIP;
185     struct sockaddr_in  outerAddr;
186     socklen_t           outerAddrLen;
187
188     RAD_PACKET          packet;
189
190 };
191 //-----------------------------------------------------------------------------
192
193 #endif
194