summaryrefslogtreecommitdiffstats
path: root/cuserman.cpp
blob: fc9ee450c0e6a191aab4514c2ec811107f172a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*  Copyright 2008,2009,2010 Edwin Eefting (edwin@datux.nl) 

    This file is part of Synapse.

    Synapse is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Synapse is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Synapse.  If not, see <http://www.gnu.org/licenses/>. */













#include "cuserman.h"
#include <boost/shared_ptr.hpp>
#include <iostream>

#include "cmodule.h"
#include "clog.h"
#include "cconfig.h"

#include "exception/cexception.h"

namespace synapse
{

using namespace std;
CuserMan::CuserMan()
{
	statMaxSessions=0;
	sessionCounter=0;
	sessionMaxPerUser=1000;
	shutdown=false;

	Cconfig config;
	config.load("etc/synapse/userman.conf");

	//create groups
	for(CvarList::iterator groupI=config["groups"].list().begin(); groupI!=config["groups"].list().end(); groupI++)
	{
		addGroup(CgroupPtr(new Cgroup(*groupI)));
	}

	//create users
	for(Cvar::iterator userI=config["users"].begin(); userI!=config["users"].end(); userI++)
	{
		//create user
		CuserPtr user;
		user=CuserPtr(new Cuser(userI->first, userI->second["passwd"].str()));

		//add member groups
		for(CvarList::iterator groupI=userI->second["groups"].list().begin(); groupI!=userI->second["groups"].list().end(); groupI++)
		{
			user->addMemberGroup(getGroup(*groupI));
		}

		addUser(user);
	}

}


CuserMan::~CuserMan()
{
}




/*!
    \fn CuserMan::getUser(string username)
 */
CuserPtr CuserMan::getUser(const string & userName)
{
	list<CuserPtr>::iterator userI;
	for (userI=users.begin(); userI!=users.end(); userI++)
	{
		if ((**userI).getName()==userName)
		{
			return(*userI);
		}
	}
	throw(synapse::runtime_error("User not found"));
	return (CuserPtr());
}





/*!
    \fn CuserMan::addUser(CuserPtr user)
 */
bool CuserMan::addUser(const CuserPtr & user)
{
	if (user)
	{
		users.push_back(user);
		return true;
	}
	return false;
}


/*!
    \fn CuserMan::addGroup(CgroupPtr group)
 */
bool CuserMan::addGroup(const CgroupPtr &group)
{
	if (group)
	{
		groups.push_back(group);
		return true;
	}
	return false;
}


/*!
    \fn CuserMan::getGroup(string groupName)
 */
CgroupPtr CuserMan::getGroup(const string &groupName)
{
	list<CgroupPtr>::iterator groupI;
	for (groupI=groups.begin(); groupI!=groups.end(); groupI++)
	{
		if ((**groupI).getName()==groupName)
		{
			return(*groupI);
		}
	}
	throw(synapse::runtime_error("Group not found"));
}


/*!
    \fn CuserMan::addSession(CsessionPtr session)
 */
int CuserMan::addSession( CsessionPtr session)
{
	if (shutdown)
	{
		ERROR("Shutting down, cant add new session.");
		return (SESSION_DISABLED);
	}

	//too much for this user already?
	int userSessions=0;
	int activeSessions=0;
	for (int sessionId=1; sessionId<MAX_SESSIONS; sessionId++)
	{
		CsessionPtr chkSession;
		chkSession=getSession(sessionId);
		if (chkSession)
		{
			activeSessions++;
			if (chkSession->user==session->user)
			{
				userSessions++;
				if (userSessions>=sessionMaxPerUser)
				{
					ERROR("User " << session->user->getName() << " has reached max sessions of " << sessionMaxPerUser);
					return (SESSION_DISABLED);
				}
			}
		}
	}

	//find free session ID. Start at the counter position, to prevent that we use the same numbers 
	//all the time. (which will be confusing)
	int startId=sessionCounter;
	do 
	{
		sessionCounter++;
		if (sessionCounter>=MAX_SESSIONS)
			sessionCounter=1;

		if (!sessions[sessionCounter])
		{
			//its free, store it here and return the ID.
			sessions[sessionCounter]=session;
			session->id=sessionCounter;
			DEB("added session " << sessionCounter << " with user " << session->user->getName());

			//keep stats
			activeSessions++;
			if (activeSessions>statMaxSessions)
				statMaxSessions=activeSessions;

			return (sessionCounter);
		}
	}
	while(startId!=sessionCounter);
	ERROR("Out of sessions, max=" << MAX_SESSIONS);
	return (SESSION_DISABLED);
}


/*!
    \fn CuserMan::getSession(int sessionId)
 */
CsessionPtr CuserMan::getSession(const int &sessionId)
{
	if (sessionId>=0 && sessionId<MAX_SESSIONS && sessions[sessionId]  )
	{
		if (sessions[sessionId]->isEnabled())
		{
			return (sessions[sessionId]);
		}
	}
	return (CsessionPtr());
}


/*!
    \fn CuserMan::delSession(int id)
 */
bool CuserMan::delSession(const int sessionId)
{
	if (sessionId>=0 && sessionId<MAX_SESSIONS && sessions[sessionId] )
	{
		//this is the default session of some module?
		if (sessions[sessionId]->module->defaultSessionId==sessionId)
		{
			//remove this session as default session from this module
			sessions[sessionId]->module->defaultSessionId=SESSION_DISABLED;

		}
		//reset shared ptr.
		//as soon as nobody uses the session object anymore it will be destroyed.
//NIET, is onhandig in de praktijk, ook bij shutdown:		sessions[sessionId]->id=SESSION_DISABLED;
		sessions[sessionId].reset();
		return (true);
	}
	else
	{
		return (false);
	}
}

list<int> CuserMan::delCookieSessions(int cookie, CmodulePtr module)
{
	list<int> deletedIds;

	if (cookie!=0)
	{
		for (int sessionId=1; sessionId<MAX_SESSIONS; sessionId++)
		{
			if (sessions[sessionId])
			{
				if (sessions[sessionId]->cookie==cookie && sessions[sessionId]->module==module)
				{
					delSession(sessionId);
					deletedIds.push_back(sessionId);
				}
			}
		}
	}
	return (deletedIds);
}


void CuserMan::getStatus(Cvar & var)
{
	int activeSessions=0;
	for (int sessionId=1; sessionId<MAX_SESSIONS; sessionId++)
	{
		CsessionPtr chkSession;
		chkSession=getSession(sessionId);
		if (chkSession)
		{
			activeSessions++;
			Cvar s;
			s["id"]=sessionId;
			s["user"]=chkSession->user->getName();
			s["module"]=chkSession->module->name;
			s["desc"]=chkSession->description;
			s["statSends"]=chkSession->statSends;
			s["statCalls"]=chkSession->statCalls;
			var["sessions"].list().push_back(s);
		}
	}

	var["statMaxSessions"]=statMaxSessions;
	var["activeSessions"]=activeSessions;
}

void CuserMan::doShutdown()
{
	shutdown=true;
}



string CuserMan::login(const int & sessionId, const string & userName, const string & password)
{
	CsessionPtr session=getSession(sessionId);
	if (!session)
		return (string("Session not found"));
	else
	{
		CuserPtr user(getUser(userName));
		if (!user || !user->isPassword(password))
			return (string("Login invalid"));
		else
		{
			//change user
			session->user=user;
			return (string());
		}
	}
}

}