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