]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/radius.h
Небольшой рефакторинг плагина radius
[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 <pthread.h>
33
34 #include <cstring>
35 #include <cstdlib>
36 #include <string>
37 #include <list>
38
39 #include "os_int.h"
40 #include "base_auth.h"
41 #include "notifer.h"
42 #include "user_ips.h"
43 #include "../../../user.h"
44 #include "../../../users.h"
45 #include "blowfish.h"
46 #include "rad_packets.h"
47
48 using namespace std;
49
50 extern "C" BASE_PLUGIN * GetPlugin();
51
52 #define RAD_DEBUG (1)
53
54 class RADIUS;
55 //-----------------------------------------------------------------------------
56 class RAD_SETTINGS
57 {
58 public:
59     RAD_SETTINGS() : port(0) {}
60     virtual ~RAD_SETTINGS() {}
61     const string & GetStrError() const { return errorStr; }
62     int ParseSettings(const MODULE_SETTINGS & s);
63     uint16_t GetPort() const { return port; }
64     const std::string & GetPassword() const { return password; }
65     const list<string> & GetAuthServices() const { return authServices; }
66     const list<string> & GetAcctServices() const { return acctServices; }
67
68 private:
69     int ParseIntInRange(const string & str, int min, int max, int * val);
70     int ParseServices(const vector<string> & str, list<string> * lst);
71
72     uint16_t port;
73     string errorStr;
74     string password;
75     list<string> authServices;
76     list<string> acctServices;
77 };
78 //-----------------------------------------------------------------------------
79 struct RAD_SESSION {
80     std::string userName;
81     std::string serviceType;
82 };
83 //-----------------------------------------------------------------------------
84 class RADIUS :public BASE_AUTH
85 {
86 public:
87                         RADIUS();
88     virtual             ~RADIUS(){};
89
90     void                SetUsers(USERS * u);
91     void                SetTariffs(TARIFFS *){};
92     void                SetAdmins(ADMINS *){};
93     void                SetTraffcounter(TRAFFCOUNTER *){};
94     void                SetStore(BASE_STORE * );
95     void                SetStgSettings(const SETTINGS * s);
96     void                SetSettings(const MODULE_SETTINGS & s);
97     int                 ParseSettings();
98
99     int                 Start();
100     int                 Stop();
101     int                 Reload() { return 0; };
102     bool                IsRunning();
103
104     const string      & GetStrError() const { return errorStr; };
105     const string        GetVersion() const;
106     uint16_t            GetStartPosition() const;
107     uint16_t            GetStopPosition() const;
108
109     int SendMessage(const STG_MSG &, uint32_t) const { return 0; };
110
111 private:
112     static void *       Run(void *);
113     int                 PrepareNet();
114     int                 FinalizeNet();
115
116     void                InitEncrypt(BLOWFISH_CTX * ctx, const string & password);
117     void                Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
118     void                Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8);
119
120     int                 Send(const RAD_PACKET & packet, struct sockaddr_in * outerAddr);
121     int                 RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr);
122     int                 ProcessData(RAD_PACKET * packet);
123
124     int                 ProcessAutzPacket(RAD_PACKET * packet);
125     int                 ProcessAuthPacket(RAD_PACKET * packet);
126     int                 ProcessPostAuthPacket(RAD_PACKET * packet);
127     int                 ProcessAcctStartPacket(RAD_PACKET * packet);
128     int                 ProcessAcctStopPacket(RAD_PACKET * packet);
129     int                 ProcessAcctUpdatePacket(RAD_PACKET * packet);
130     int                 ProcessAcctOtherPacket(RAD_PACKET * packet);
131
132     bool                FindUser(user_iter * ui, const std::string & login) const;
133     bool                CanAuthService(const std::string & svc) const;
134     bool                CanAcctService(const std::string & svc) const;
135     bool                IsAllowedService(const std::string & svc) const;
136
137     void                SetUserNotifier(user_iter u);
138     void                UnSetUserNotifier(user_iter u);
139
140     bool                WaitPackets(int sd) const;
141
142     void                PrintServices(const std::list<std::string> & svcs);
143
144     struct Printer : public unary_function<std::string, void>
145     { 
146         void operator()(const std::string & line)
147         { 
148             printfd("radius.cpp", "'%s'\n", line.c_str()); 
149         }; 
150     };
151     struct SPrinter : public unary_function<std::pair<std::string, RAD_SESSION>, void>
152     { 
153         void operator()(const std::pair<std::string, RAD_SESSION> & it)
154         { 
155             printfd("radius.cpp", "%s - ('%s', '%s')\n", it.first.c_str(), it.second.userName.c_str(), it.second.serviceType.c_str()); 
156         }; 
157     };
158
159     BLOWFISH_CTX        ctx;
160
161     mutable string      errorStr;
162     RAD_SETTINGS        radSettings;
163     MODULE_SETTINGS     settings;
164     list<string>        authServices;
165     list<string>        acctServices;
166     map<string, RAD_SESSION> sessions;
167
168     bool                nonstop;
169     bool                isRunning;
170
171     USERS *             users;
172     const SETTINGS *    stgSettings;
173     const BASE_STORE *  store;
174
175     pthread_t           thread;
176     pthread_mutex_t     mutex;
177
178     int                 sock;
179
180     RAD_PACKET          packet;
181
182 };
183 //-----------------------------------------------------------------------------
184
185 #endif