1 /*
2  *  radiusplugin -- An OpenVPN plugin for do radius authentication
3  *					and accounting.
4  *
5  *  Copyright (C) 2005 EWE TEL GmbH/Ralf Luebben <ralfluebben@gmx.de>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "PluginContext.h"
23 
24 
25 
26 /** The constructor. All sockets all set to -1, the process ids and the
27  * verbosity level are set to 0. The session id is set to to 1.*/
PluginContext()28 PluginContext::PluginContext()
29 {
30 
31   	this->authsocketforegr.setSocket(-1);
32   	this->authsocketbackgr.setSocket(-1);
33 	this->acctsocketforegr.setSocket(-1);
34 	this->acctsocketbackgr.setSocket(-1);
35 
36   	this->authpid=0;
37   	this->acctpid=0;
38 
39   	this->verb=0;
40   	this->sessionid=1;
41 
42         this->stopthread=false;
43 	this->startthread=true;
44 }
45 
46 /** The destructor clears the users and nasportlist.*/
~PluginContext()47 PluginContext::~PluginContext()
48 {
49 	this->users.clear();
50 	this->nasportlist.clear();
51 
52 }
53 
54 /** The method searches the first free nas port in a list.
55  * @return The nas port.
56  */
addNasPort(void)57 int PluginContext::addNasPort(void)
58 {
59 	int newport=0;
60 	list<int>::iterator i;
61 	list<int>::iterator j;
62 	i=nasportlist.begin();
63 	j=nasportlist.end();
64 
65 	if (this->nasportlist.empty())
66 	{
67 		newport=1;
68 		this->nasportlist.push_front(newport);
69 	}
70 
71 	else
72 	{
73 		newport=1;
74 		while( i != this->nasportlist.end())
75 		{
76 		    if (newport < *i)
77 		    {
78 		    	j=i;
79 		    	i=this->nasportlist.end();
80 		    }
81 		    else
82 		    {
83 		    	i++;
84 		    	newport++;
85 		    }
86 		}
87 		this->nasportlist.insert(j, newport);
88 	}
89 	return newport;
90 }
91 
92 /**The method deletes the nas port from the list.
93  * @param The nas port number to delete.
94  */
delNasPort(int num)95 void PluginContext::delNasPort(int num)
96 {
97 	this->nasportlist.remove(num);
98 }
99 
100 /**The method adds an user to the user map of the foreground
101  * process.
102  * @param newuser A pointer to the user.
103  * @throws Exception::ALREADYAUTHENTICATED if the user could not add to the map, this happens if a user with the key is already in the list.
104  */
addUser(UserPlugin * newuser)105 void PluginContext::addUser(UserPlugin * newuser)
106 {
107 	pair<map<string,UserPlugin *>::iterator,bool> success;
108 
109 	success=users.insert(make_pair(newuser->getKey(),newuser));
110 
111 	if(success.second==false)
112 	{
113 		throw Exception(Exception::ALREADYAUTHENTICATED);
114 	}
115 	else
116 	{
117 		this->sessionid++;
118 	}
119 
120 }
121 
122 /**The method deletes the user from the map with the key.
123  * @param key The key of the user.
124  */
delUser(string key)125 void PluginContext::delUser(string key)
126 {
127 	users.erase(key);
128 }
129 
130 /**The method finds a user in the user map.
131  * @param key The key of the user.
132  * @return A pointer to the user.
133  */
findUser(string key)134 UserPlugin * PluginContext::findUser(string key)
135 {
136 	map<string,UserPlugin *>::iterator iter =  users.find(key);
137 	if (iter != users.end())
138 	{
139 		return iter->second;
140 	}
141 	return NULL;
142 }
143 
144 
145 /** The getter method for the verbosity level.
146  * @return The verbosity level.
147  */
getVerbosity(void)148 int PluginContext::getVerbosity(void)
149 {
150 	return this->verb;
151 }
152 
153 /** The setter method for the verbosisty level.
154  * @param v The verbosity level.
155  */
setVerbosity(int v)156 void PluginContext::setVerbosity(int v)
157 {
158 	this->verb=v;
159 }
160 
161 /** The getter method for the authentication
162  * background proccess id.
163  * @returns The process id.
164  */
getAuthPid(void)165 pid_t PluginContext::getAuthPid(void)
166 {
167 	return this->authpid;
168 }
169 
170 /** The setter method for the authentication
171  * background proccess id.
172  * @param The process id.
173  */
setAuthPid(pid_t p)174 void PluginContext::setAuthPid(pid_t p)
175 {
176 	this->authpid=p;
177 }
178 
179 /** The getter method for the accounting
180  * background proccess id.
181  * @returns The process id.
182  */
getAcctPid(void)183 pid_t PluginContext::getAcctPid(void)
184 {
185 	return this->acctpid;
186 }
187 
188 /** The setter method for the accounting
189  * background proccess id.
190  * @param The process id.
191  */
setAcctPid(pid_t p)192 void PluginContext::setAcctPid(pid_t p)
193 {
194 	this->acctpid=p;
195 }
196 
197 
198 /** The setter method method for the session id.
199  * @returns The sessionid.
200  */
getSessionId(void)201 int PluginContext::getSessionId(void)
202 {
203 	return this->sessionid;
204 }
205 
206 
207 /**The method adds an new user to the user list of users waiting for authentication
208  * @param newuser A pointer to the user.
209  */
addNewUser(UserPlugin * newuser)210 void PluginContext::addNewUser(UserPlugin * newuser)
211 {
212   this->newusers.push_back(newuser);
213 }
214 
215 /**The method return the first element in the list of waiting users.
216  */
getNewUser()217 UserPlugin * PluginContext::getNewUser()
218 {
219 
220 
221       UserPlugin * user = this->newusers.front();
222       this->newusers.pop_front();
223       return user;
224 
225 }
226 
getCondSend(void)227 pthread_cond_t  * PluginContext::getCondSend(void )
228 {
229   return &condsend;
230 }
getCondRecv(void)231 pthread_cond_t  * PluginContext::getCondRecv(void )
232 {
233   return &condrecv;
234 }
235 
getMutexSend(void)236 pthread_mutex_t * PluginContext::getMutexSend(void )
237 {
238   return &mutexsend;
239 }
240 
getMutexRecv(void)241 pthread_mutex_t * PluginContext::getMutexRecv(void )
242 {
243   return &mutexrecv;
244 }
245 
246 
getThread()247 pthread_t * PluginContext::getThread()
248 {
249   return &thread;
250 }
251 
getResult()252 int PluginContext::getResult()
253 {
254   return result;
255 }
256 
setResult(int r)257 void PluginContext::setResult(int r)
258 {
259   result=r;
260 }
261 
UserWaitingtoAuth()262 bool PluginContext::UserWaitingtoAuth()
263 {
264   if (this->newusers.size()>0) return true;
265   else return false;
266 }
267 
268 
getStopThread()269 bool PluginContext::getStopThread()
270 {
271   return stopthread;
272 }
273 
setStopThread(bool s)274 void PluginContext::setStopThread(bool s)
275 {
276   stopthread=s;
277 }
278 
279 
getStartThread()280 bool PluginContext::getStartThread()
281 {
282   return startthread;
283 }
284 
setStartThread(bool value)285 void PluginContext::setStartThread(bool value)
286 {
287   startthread=value;
288 }
289 
290