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