-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathusers.cpp
198 lines (172 loc) · 4.36 KB
/
users.cpp
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
/*
* users.cpp: User specific rights for the LIVE plugin.
*
* See the README file for copyright information and how to reach the author.
*/
#include "users.h"
#include "tools.h"
#include "setup.h"
#include <sstream>
namespace vdrlive {
std::string cUsers::logged_in_user;
// -- cUser -----------------------------------------------------------------
cUser::cUser(int ID, const std::string& Name, const std::string& Password)
: m_ID(ID), m_Name(Name)
{
SetPassword(Password);
}
void cUser::SetPassword(const std::string& Password)
{
std::stringstream passwordStr;
passwordStr << Password.size() << "|" << MD5Hash(Password);
m_PasswordMD5 = passwordStr.str();
}
int cUser::GetPasswordLength() const
{
// format is <length>:<md5-hash of password>
std::vector<std::string> parts = StringSplit( m_PasswordMD5, '|' );
return (parts.size() > 0) ? parse_int<int>( parts[0] ) : 0;
}
std::string const cUser::GetMD5HashPassword() const
{
// format is <length>:<md5-hash of password>
std::vector<std::string> parts = StringSplit( m_PasswordMD5, '|' );
return (parts.size() > 1) ? parts[1] : "";
}
bool cUser::Parse(const char *s)
{
char *line;
char *pos;
char *pos_next;
int parameter = 1;
int valuelen;
#define MAXVALUELEN (10 * MaxFileName)
char value[MAXVALUELEN];
pos = line = strdup(s);
pos_next = pos + strlen(pos);
if (*pos_next == '\n') *pos_next = 0;
while (*pos) {
while (*pos == ' ') pos++;
if (*pos) {
if (*pos != ':') {
pos_next = strchr(pos, ':');
if (!pos_next)
pos_next = pos + strlen(pos);
valuelen = pos_next - pos + 1;
if (valuelen > MAXVALUELEN)
{
esyslog("live: entry '%s' is too long. Will be truncated!", pos);
valuelen = MAXVALUELEN;
}
strn0cpy(value, pos, valuelen);
pos = pos_next;
switch (parameter) {
case 1: m_ID = parse_int<int>(value);
break;
case 2: m_Name = value;
break;
case 3: m_PasswordMD5 = value;
break;
case 4:
m_Userrights = parse_int<int>(value);
break;
default:
break;
} //switch
}
parameter++;
}
if (*pos) pos++;
} //while
free(line);
return (parameter >= 4) ? true : false;
}
const char *cUser::ToText(void)
{
char* buffer = NULL;
if (asprintf(&buffer, "%d:%s:%s:%d", m_ID, m_Name.c_str(), m_PasswordMD5.c_str(), m_Userrights) < 0)
return NULL;
return buffer;
}
bool cUser::Save(FILE *f)
{
return fprintf(f, "%s\n", ToText()) > 0;
}
bool cUser::HasRightTo(eUserRights right)
{
return ((m_Userrights & (1 << (right-1))) != 0);
}
bool cUser::CurrentUserHasRightTo(eUserRights right)
{
if (!LiveSetup().UseAuth()) return true;
cUser* user = cUsers::GetByUserName(cUsers::logged_in_user);
return (cUsers::logged_in_user == LiveSetup().GetAdminLogin() || (user && (user->m_Userrights & (1 << (right-1))) != 0));
}
void cUser::SetRight(eUserRights right)
{
isyslog("live: set right '%d' in '%d'", right, m_Userrights);
m_Userrights |= (1 << (right-1));
isyslog("live: now rights are '%d'", m_Userrights);
}
bool cUsers::Delete(const std::string& Name)
{
cUser* user = Users.First();
while (user)
{
if (user->Name() == Name)
{
Users.Del(user);
Users.Save();
return true;
}
user = Users.Next(user);
}
return false;
}
cUser* cUsers::GetByUserId(const std::string& Id)
{
cUser* user = Users.First();
while (user)
{
if (user->Id() == atoi(Id.c_str()))
return user;
user = Users.Next(user);
}
return NULL;
}
cUser* cUsers::GetByUserName(const std::string& Name)
{
cUser* user = Users.First();
while (user)
{
if (user->Name() == Name)
return user;
user = Users.Next(user);
}
return NULL;
}
int cUsers::GetNewId()
{
int iMaxId = -1;
cUser* user = Users.First();
while (user)
{
if (iMaxId < user->Id())
iMaxId = user->Id();
user = Users.Next(user);
}
return iMaxId + 1;
}
bool cUsers::ValidUserLogin(const std::string& login, const std::string& password)
{
cUser* user = GetByUserName(login);
if (user && MD5Hash(password) == user->GetMD5HashPassword())
return true;
return false;
}
bool cUsers::ValidLogin(const std::string& login, const std::string& password)
{
return ((login == LiveSetup().GetAdminLogin() && MD5Hash(password) == LiveSetup().GetMD5HashAdminPassword()) ||
ValidUserLogin(login, password));
}
}