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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "parser_users.h"
24 #include "stg/users.h"
25 #include "stg/tariffs.h"
26 #include "stg/user_property.h"
27 #include "stg/user_conf.h"
28 #include "stg/user_stat.h"
29 #include "stg/common.h"
34 using STG::PARSER::GET_USERS;
35 using STG::PARSER::GET_USER;
36 using STG::PARSER::ADD_USER;
37 using STG::PARSER::DEL_USER;
38 using STG::PARSER::CHG_USER;
39 using STG::PARSER::CHECK_USER;
44 std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0)
49 answer += "<User result=\"ok\">";
51 answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
53 answer += "<Login value=\"" + user.GetLogin() + "\"/>";
55 if (user.GetProperty().password.ModificationTime() > lastTime)
58 answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
60 answer += "<Password value=\"++++++\"/>";
63 if (user.GetProperty().cash.ModificationTime() > lastTime)
64 answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
65 if (user.GetProperty().freeMb.ModificationTime() > lastTime)
66 answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
67 if (user.GetProperty().credit.ModificationTime() > lastTime)
68 answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
70 if (user.GetProperty().nextTariff.Get() != "")
72 if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
73 user.GetProperty().nextTariff.ModificationTime() > lastTime)
74 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
78 if (user.GetProperty().tariffName.ModificationTime() > lastTime)
79 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
82 if (user.GetProperty().note.ModificationTime() > lastTime)
83 answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
84 if (user.GetProperty().phone.ModificationTime() > lastTime)
85 answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
86 if (user.GetProperty().address.ModificationTime() > lastTime)
87 answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
88 if (user.GetProperty().email.ModificationTime() > lastTime)
89 answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
91 std::vector<const USER_PROPERTY_LOGGED<std::string> *> userdata;
92 userdata.push_back(user.GetProperty().userdata0.GetPointer());
93 userdata.push_back(user.GetProperty().userdata1.GetPointer());
94 userdata.push_back(user.GetProperty().userdata2.GetPointer());
95 userdata.push_back(user.GetProperty().userdata3.GetPointer());
96 userdata.push_back(user.GetProperty().userdata4.GetPointer());
97 userdata.push_back(user.GetProperty().userdata5.GetPointer());
98 userdata.push_back(user.GetProperty().userdata6.GetPointer());
99 userdata.push_back(user.GetProperty().userdata7.GetPointer());
100 userdata.push_back(user.GetProperty().userdata8.GetPointer());
101 userdata.push_back(user.GetProperty().userdata9.GetPointer());
103 for (size_t i = 0; i < userdata.size(); i++)
104 if (userdata[i]->ModificationTime() > lastTime)
105 answer += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
107 if (user.GetProperty().realName.ModificationTime() > lastTime)
108 answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
109 if (user.GetProperty().group.ModificationTime() > lastTime)
110 answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
111 if (user.GetConnectedModificationTime() > lastTime)
112 answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
113 if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
114 answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
115 if (user.GetCurrIPModificationTime() > lastTime)
116 answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
117 if (user.GetPingTime() > lastTime)
118 answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
119 if (user.GetProperty().ips.ModificationTime() > lastTime)
120 answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
123 const DIR_TRAFF & upload(user.GetProperty().down.Get());
124 const DIR_TRAFF & download(user.GetProperty().up.Get());
125 if (user.GetProperty().up.ModificationTime() > lastTime)
126 for (size_t j = 0; j < DIR_NUM; j++)
127 answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\"";
128 if (user.GetProperty().down.ModificationTime() > lastTime)
129 for (size_t j = 0; j < DIR_NUM; j++)
130 answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\"";
131 if (user.GetSessionUploadModificationTime() > lastTime)
132 for (size_t j = 0; j < DIR_NUM; j++)
133 answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\"";
134 if (user.GetSessionDownloadModificationTime() > lastTime)
135 for (size_t j = 0; j < DIR_NUM; j++)
136 answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\"";
139 if (user.GetProperty().disabled.ModificationTime() > lastTime)
140 answer += std::string("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
141 if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
142 answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
143 if (user.GetProperty().passive.ModificationTime() > lastTime)
144 answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
145 if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
146 answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
147 if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
148 answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
149 if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
150 answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
151 if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
152 answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
156 answer += "<AuthorizedBy>";
157 std::vector<std::string> list(user.GetAuthorizers());
158 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
159 answer += "<Auth name=\"" + *it + "\"/>";
160 answer += "</AuthorizedBy>";
168 } // namespace anonymous
170 int GET_USERS::Start(void *, const char * el, const char ** attr)
172 if (strcasecmp(el, m_tag.c_str()) != 0)
175 while (attr && *attr && *(attr + 1))
177 if (strcasecmp(*attr, "LastUpdate") == 0)
178 str2x(*(attr + 1), m_lastUserUpdateTime);
185 void GET_USERS::CreateAnswer()
187 int h = m_users.OpenSearch();
190 if (m_lastUserUpdateTime > 0)
191 m_answer = "<" + m_tag + " LastUpdate=\"" + x2str(time(NULL)) + "\">";
193 m_answer = GetOpenTag();
197 while (m_users.SearchNext(h, &u) == 0)
198 m_answer += UserToXML(*u, true, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd, m_lastUserUpdateTime);
200 m_users.CloseSearch(h);
202 m_answer += GetCloseTag();
205 int GET_USER::Start(void *, const char * el, const char ** attr)
207 if (strcasecmp(el, m_tag.c_str()) != 0)
217 void GET_USER::CreateAnswer()
221 if (m_users.FindByName(m_login, &u))
222 m_answer = "<" + m_tag + " result=\"error\" reason=\"User not found.\"/>";
224 m_answer = UserToXML(*u, false, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd);
227 int ADD_USER::Start(void *, const char * el, const char ** attr)
233 if (strcasecmp(el, m_tag.c_str()) == 0)
238 if (strcasecmp(el, "login") == 0)
247 void ADD_USER::CreateAnswer()
249 if (m_users.Exists(m_login))
250 m_answer = "<" + m_tag + " result=\"error\" reason=\"User '" + m_login + "' exists.\"/>";
251 else if (m_users.Add(m_login, &m_currAdmin) == 0)
252 m_answer = "<" + m_tag + " result=\"ok\"/>";
254 m_answer = "<" + m_tag + " result=\"error\" reason=\"Access denied\"/>";
257 int CHG_USER::Start(void *, const char * el, const char ** attr)
263 if (strcasecmp(el, m_tag.c_str()) == 0)
268 if (strcasecmp(el, "login") == 0)
274 if (strcasecmp(el, "ip") == 0)
276 m_ucr.ips = StrToIPS(attr[1]);
280 if (strcasecmp(el, "password") == 0)
282 m_ucr.password = attr[1];
286 if (strcasecmp(el, "address") == 0)
288 m_ucr.address = Decode21str(attr[1]);
292 if (strcasecmp(el, "aonline") == 0)
294 m_ucr.alwaysOnline = (*(attr[1]) != '0');
298 if (strcasecmp(el, "cash") == 0)
300 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
301 m_cashMsg = Decode21str(attr[3]);
304 if (strtodouble2(attr[1], cash) == 0)
307 m_cashMustBeAdded = (strcasecmp(attr[0], "add") == 0);
312 if (strcasecmp(el, "CreditExpire") == 0)
314 long int creditExpire = 0;
315 if (str2x(attr[1], creditExpire) == 0)
316 m_ucr.creditExpire = (time_t)creditExpire;
321 if (strcasecmp(el, "credit") == 0)
324 if (strtodouble2(attr[1], credit) == 0)
325 m_ucr.credit = credit;
329 if (strcasecmp(el, "freemb") == 0)
332 if (strtodouble2(attr[1], freeMb) == 0)
333 m_usr.freeMb = freeMb;
337 if (strcasecmp(el, "down") == 0)
340 if (str2x(attr[1], down) == 0)
341 m_ucr.disabled = down;
345 if (strcasecmp(el, "DisableDetailStat") == 0)
347 int disabledDetailStat = 0;
348 if (str2x(attr[1], disabledDetailStat) == 0)
349 m_ucr.disabledDetailStat = disabledDetailStat;
353 if (strcasecmp(el, "email") == 0)
355 m_ucr.email = Decode21str(attr[1]);
359 for (int i = 0; i < USERDATA_NUM; i++)
362 sprintf(name, "userdata%d", i);
363 if (strcasecmp(el, name) == 0)
365 m_ucr.userdata[i] = Decode21str(attr[1]);
370 if (strcasecmp(el, "group") == 0)
372 m_ucr.group = Decode21str(attr[1]);
376 if (strcasecmp(el, "note") == 0)
378 m_ucr.note = Decode21str(attr[1]);
382 if (strcasecmp(el, "passive") == 0)
385 if (str2x(attr[1], passive) == 0)
386 m_ucr.passive = passive;
390 if (strcasecmp(el, "phone") == 0)
392 m_ucr.phone = Decode21str(attr[1]);
396 if (strcasecmp(el, "Name") == 0)
398 m_ucr.realName = Decode21str(attr[1]);
402 if (strcasecmp(el, "traff") == 0)
407 int dir = attr[j][2] - '0';
409 if (strncasecmp(attr[j], "md", 2) == 0)
412 str2x(attr[j + 1], t);
415 if (strncasecmp(attr[j], "mu", 2) == 0)
418 str2x(attr[j + 1], t);
426 if (strcasecmp(el, "tariff") == 0)
428 if (strcasecmp(attr[0], "now") == 0)
429 m_ucr.tariffName = attr[1];
431 if (strcasecmp(attr[0], "delayed") == 0)
432 m_ucr.nextTariff = attr[1];
440 void CHG_USER::CreateAnswer()
442 if (ApplyChanges() == 0)
443 m_answer = "<" + m_tag + " result=\"ok\"/>";
445 m_answer = "<" + m_tag + " result=\"error\"/>";
448 int CHG_USER::ApplyChanges()
450 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
453 if (m_users.FindByName(m_login, &u))
457 bool alwaysOnline = u->GetProperty().alwaysOnline;
458 if (!m_ucr.alwaysOnline.empty())
461 alwaysOnline = m_ucr.alwaysOnline.const_data();
463 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
464 if (!m_ucr.ips.empty())
467 onlyOneIP = m_ucr.ips.const_data().OnlyOneIP();
470 if (check && alwaysOnline && !onlyOneIP)
472 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
473 GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.GetLogStr().c_str());
477 for (size_t i = 0; i < m_ucr.ips.const_data().Count(); ++i)
480 uint32_t ip = m_ucr.ips.const_data().operator[](i).ip;
481 if (m_users.IsIPInUse(ip, m_login, &user))
483 printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str());
484 GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", m_currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str());
489 if (!m_ucr.ips.empty())
490 if (!u->GetProperty().ips.Set(m_ucr.ips.const_data(), &m_currAdmin, m_login, &m_store))
493 if (!m_ucr.alwaysOnline.empty())
494 if (!u->GetProperty().alwaysOnline.Set(m_ucr.alwaysOnline.const_data(),
495 &m_currAdmin, m_login, &m_store))
498 if (!m_ucr.address.empty())
499 if (!u->GetProperty().address.Set(m_ucr.address.const_data(), &m_currAdmin, m_login, &m_store))
502 if (!m_ucr.creditExpire.empty())
503 if (!u->GetProperty().creditExpire.Set(m_ucr.creditExpire.const_data(),
504 &m_currAdmin, m_login, &m_store))
507 if (!m_ucr.credit.empty())
508 if (!u->GetProperty().credit.Set(m_ucr.credit.const_data(), &m_currAdmin, m_login, &m_store))
511 if (!m_usr.freeMb.empty())
512 if (!u->GetProperty().freeMb.Set(m_usr.freeMb.const_data(), &m_currAdmin, m_login, &m_store))
515 if (!m_ucr.disabled.empty())
516 if (!u->GetProperty().disabled.Set(m_ucr.disabled.const_data(), &m_currAdmin, m_login, &m_store))
519 if (!m_ucr.disabledDetailStat.empty())
520 if (!u->GetProperty().disabledDetailStat.Set(m_ucr.disabledDetailStat.const_data(), &m_currAdmin, m_login, &m_store))
523 if (!m_ucr.email.empty())
524 if (!u->GetProperty().email.Set(m_ucr.email.const_data(), &m_currAdmin, m_login, &m_store))
527 if (!m_ucr.group.empty())
528 if (!u->GetProperty().group.Set(m_ucr.group.const_data(), &m_currAdmin, m_login, &m_store))
531 if (!m_ucr.note.empty())
532 if (!u->GetProperty().note.Set(m_ucr.note.const_data(), &m_currAdmin, m_login, &m_store))
535 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
536 userdata.push_back(u->GetProperty().userdata0.GetPointer());
537 userdata.push_back(u->GetProperty().userdata1.GetPointer());
538 userdata.push_back(u->GetProperty().userdata2.GetPointer());
539 userdata.push_back(u->GetProperty().userdata3.GetPointer());
540 userdata.push_back(u->GetProperty().userdata4.GetPointer());
541 userdata.push_back(u->GetProperty().userdata5.GetPointer());
542 userdata.push_back(u->GetProperty().userdata6.GetPointer());
543 userdata.push_back(u->GetProperty().userdata7.GetPointer());
544 userdata.push_back(u->GetProperty().userdata8.GetPointer());
545 userdata.push_back(u->GetProperty().userdata9.GetPointer());
547 for (int i = 0; i < (int)userdata.size(); i++)
548 if (!m_ucr.userdata[i].empty())
549 if(!userdata[i]->Set(m_ucr.userdata[i].const_data(), &m_currAdmin, m_login, &m_store))
552 if (!m_ucr.passive.empty())
553 if (!u->GetProperty().passive.Set(m_ucr.passive.const_data(), &m_currAdmin, m_login, &m_store))
556 if (!m_ucr.password.empty())
557 if (!u->GetProperty().password.Set(m_ucr.password.const_data(), &m_currAdmin, m_login, &m_store))
560 if (!m_ucr.phone.empty())
561 if (!u->GetProperty().phone.Set(m_ucr.phone.const_data(), &m_currAdmin, m_login, &m_store))
564 if (!m_ucr.realName.empty())
565 if (!u->GetProperty().realName.Set(m_ucr.realName.const_data(), &m_currAdmin, m_login, &m_store))
569 if (!m_usr.cash.empty())
570 if (m_cashMustBeAdded)
572 if (!u->GetProperty().cash.Set(m_usr.cash.const_data() + u->GetProperty().cash,
579 if (!u->GetProperty().cash.Set(m_usr.cash.const_data(), &m_currAdmin, m_login, &m_store, m_cashMsg))
584 if (!m_ucr.tariffName.empty())
586 if (m_tariffs.FindByName(m_ucr.tariffName.const_data()))
588 if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &m_currAdmin, m_login, &m_store))
590 u->ResetNextTariff();
594 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
599 if (!m_ucr.nextTariff.empty())
601 if (m_tariffs.FindByName(m_ucr.nextTariff.const_data()))
603 if (!u->GetProperty().nextTariff.Set(m_ucr.nextTariff.const_data(), &m_currAdmin, m_login, &m_store))
608 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
613 DIR_TRAFF up = u->GetProperty().up;
614 DIR_TRAFF down = u->GetProperty().down;
617 for (int i = 0; i < DIR_NUM; i++)
619 if (!m_upr[i].empty())
621 up[i] = m_upr[i].data();
624 if (!m_downr[i].empty())
626 down[i] = m_downr[i].data();
632 if (!u->GetProperty().up.Set(up, &m_currAdmin, m_login, &m_store))
636 if (!u->GetProperty().down.Set(down, &m_currAdmin, m_login, &m_store))
645 int DEL_USER::Start(void *, const char *el, const char **attr)
648 if (strcasecmp(el, m_tag.c_str()) == 0)
650 if (attr[0] == NULL || attr[1] == NULL)
652 //CreateAnswer("Parameters error!");
657 if (m_users.FindByName(attr[1], &u))
669 int DEL_USER::End(void *, const char *el)
671 if (strcasecmp(el, m_tag.c_str()) == 0)
674 m_users.Del(u->GetLogin(), &m_currAdmin);
681 void DEL_USER::CreateAnswer()
684 m_answer = "<" + m_tag + " value=\"error\" reason=\"User not found\"/>";
686 m_answer = "<" + m_tag + " value=\"ok\"/>";
689 int CHECK_USER::Start(void *, const char *el, const char **attr)
691 if (strcasecmp(el, m_tag.c_str()) == 0)
693 if (attr[0] == NULL || attr[1] == NULL ||
694 attr[2] == NULL || attr[3] == NULL)
696 CreateAnswer("Invalid parameters.");
697 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
702 if (m_users.FindByName(attr[1], &user))
704 CreateAnswer("User not found.");
705 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
709 if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
711 CreateAnswer("Wrong password.");
712 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
722 int CHECK_USER::End(void *, const char *el)
724 if (strcasecmp(el, m_tag.c_str()) == 0)
729 void CHECK_USER::CreateAnswer(const char * error)
732 m_answer = "<" + m_tag + " value=\"Err\" reason=\"" + error + "\"/>";
734 m_answer = "<" + m_tag + " value=\"Ok\"/>";