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;
41 const char * GET_USERS::tag = "GetUsers";
42 const char * GET_USER::tag = "GetUser";
43 const char * ADD_USER::tag = "AddUser";
44 const char * CHG_USER::tag = "SetUser";
45 const char * DEL_USER::tag = "DelUser";
46 const char * CHECK_USER::tag = "CheckUser";
51 std::string UserToXML(const USER & user, bool loginInStart, bool showPass, time_t lastTime = 0)
56 answer += "<User result=\"ok\">";
58 answer += "<User result=\"ok\" login=\"" + user.GetLogin() + "\">";
60 answer += "<Login value=\"" + user.GetLogin() + "\"/>";
62 if (user.GetProperty().password.ModificationTime() > lastTime)
65 answer += "<Password value=\"" + user.GetProperty().password.Get() + "\" />";
67 answer += "<Password value=\"++++++\"/>";
70 if (user.GetProperty().cash.ModificationTime() > lastTime)
71 answer += "<Cash value=\"" + x2str(user.GetProperty().cash.Get()) + "\"/>";
72 if (user.GetProperty().freeMb.ModificationTime() > lastTime)
73 answer += "<FreeMb value=\"" + x2str(user.GetProperty().freeMb.Get()) + "\"/>";
74 if (user.GetProperty().credit.ModificationTime() > lastTime)
75 answer += "<Credit value=\"" + x2str(user.GetProperty().credit.Get()) + "\"/>";
77 if (user.GetProperty().nextTariff.Get() != "")
79 if (user.GetProperty().tariffName.ModificationTime() > lastTime ||
80 user.GetProperty().nextTariff.ModificationTime() > lastTime)
81 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "/" + user.GetProperty().nextTariff.Get() + "\"/>";
85 if (user.GetProperty().tariffName.ModificationTime() > lastTime)
86 answer += "<Tariff value=\"" + user.GetProperty().tariffName.Get() + "\"/>";
89 if (user.GetProperty().note.ModificationTime() > lastTime)
90 answer += "<Note value=\"" + Encode12str(user.GetProperty().note) + "\"/>";
91 if (user.GetProperty().phone.ModificationTime() > lastTime)
92 answer += "<Phone value=\"" + Encode12str(user.GetProperty().phone) + "\"/>";
93 if (user.GetProperty().address.ModificationTime() > lastTime)
94 answer += "<Address value=\"" + Encode12str(user.GetProperty().address) + "\"/>";
95 if (user.GetProperty().email.ModificationTime() > lastTime)
96 answer += "<Email value=\"" + Encode12str(user.GetProperty().email) + "\"/>";
98 std::vector<const USER_PROPERTY_LOGGED<std::string> *> userdata;
99 userdata.push_back(user.GetProperty().userdata0.GetPointer());
100 userdata.push_back(user.GetProperty().userdata1.GetPointer());
101 userdata.push_back(user.GetProperty().userdata2.GetPointer());
102 userdata.push_back(user.GetProperty().userdata3.GetPointer());
103 userdata.push_back(user.GetProperty().userdata4.GetPointer());
104 userdata.push_back(user.GetProperty().userdata5.GetPointer());
105 userdata.push_back(user.GetProperty().userdata6.GetPointer());
106 userdata.push_back(user.GetProperty().userdata7.GetPointer());
107 userdata.push_back(user.GetProperty().userdata8.GetPointer());
108 userdata.push_back(user.GetProperty().userdata9.GetPointer());
110 for (size_t i = 0; i < userdata.size(); i++)
111 if (userdata[i]->ModificationTime() > lastTime)
112 answer += "<UserData" + x2str(i) + " value=\"" + Encode12str(userdata[i]->Get()) + "\" />";
114 if (user.GetProperty().realName.ModificationTime() > lastTime)
115 answer += "<Name value=\"" + Encode12str(user.GetProperty().realName) + "\"/>";
116 if (user.GetProperty().group.ModificationTime() > lastTime)
117 answer += "<Group value=\"" + Encode12str(user.GetProperty().group) + "\"/>";
118 if (user.GetConnectedModificationTime() > lastTime)
119 answer += std::string("<Status value=\"") + (user.GetConnected() ? "1" : "0") + "\"/>";
120 if (user.GetProperty().alwaysOnline.ModificationTime() > lastTime)
121 answer += std::string("<AOnline value=\"") + (user.GetProperty().alwaysOnline.Get() ? "1" : "0") + "\"/>";
122 if (user.GetCurrIPModificationTime() > lastTime)
123 answer += "<CurrIP value=\"" + inet_ntostring(user.GetCurrIP()) + "\"/>";
124 if (user.GetPingTime() > lastTime)
125 answer += "<PingTime value=\"" + x2str(user.GetPingTime()) + "\"/>";
126 if (user.GetProperty().ips.ModificationTime() > lastTime)
127 answer += "<IP value=\"" + user.GetProperty().ips.Get().GetIpStr() + "\"/>";
130 const DIR_TRAFF & upload(user.GetProperty().down.Get());
131 const DIR_TRAFF & download(user.GetProperty().up.Get());
132 if (user.GetProperty().up.ModificationTime() > lastTime)
133 for (size_t j = 0; j < DIR_NUM; j++)
134 answer += " MU" + x2str(j) + "=\"" + x2str(upload[j]) + "\"";
135 if (user.GetProperty().down.ModificationTime() > lastTime)
136 for (size_t j = 0; j < DIR_NUM; j++)
137 answer += " MD" + x2str(j) + "=\"" + x2str(download[j]) + "\"";
138 if (user.GetSessionUploadModificationTime() > lastTime)
139 for (size_t j = 0; j < DIR_NUM; j++)
140 answer += " SU" + x2str(j) + "=\"" + x2str(user.GetSessionUpload()[j]) + "\"";
141 if (user.GetSessionDownloadModificationTime() > lastTime)
142 for (size_t j = 0; j < DIR_NUM; j++)
143 answer += " SD" + x2str(j) + "=\"" + x2str(user.GetSessionDownload()[j]) + "\"";
146 if (user.GetProperty().disabled.ModificationTime() > lastTime)
147 answer += std::string("<Down value=\"") + (user.GetProperty().disabled.Get() ? "1" : "0") + "\"/>";
148 if (user.GetProperty().disabledDetailStat.ModificationTime() > lastTime)
149 answer += std::string("<DisableDetailStat value=\"") + (user.GetProperty().disabledDetailStat.Get() ? "1" : "0") + "\"/>";
150 if (user.GetProperty().passive.ModificationTime() > lastTime)
151 answer += std::string("<Passive value=\"") + (user.GetProperty().passive.Get() ? "1" : "0") + "\"/>";
152 if (user.GetProperty().lastCashAdd.ModificationTime() > lastTime)
153 answer += "<LastCash value=\"" + x2str(user.GetProperty().lastCashAdd.Get()) + "\"/>";
154 if (user.GetProperty().lastCashAddTime.ModificationTime() > lastTime)
155 answer += "<LastTimeCash value=\"" + x2str(user.GetProperty().lastCashAddTime.Get()) + "\"/>";
156 if (user.GetProperty().lastActivityTime.ModificationTime() > lastTime)
157 answer += "<LastActivityTime value=\"" + x2str(user.GetProperty().lastActivityTime.Get()) + "\"/>";
158 if (user.GetProperty().creditExpire.ModificationTime() > lastTime)
159 answer += "<CreditExpire value=\"" + x2str(user.GetProperty().creditExpire.Get()) + "\"/>";
163 answer += "<AuthorizedBy>";
164 std::vector<std::string> list(user.GetAuthorizers());
165 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
166 answer += "<Auth name=\"" + *it + "\"/>";
167 answer += "</AuthorizedBy>";
175 } // namespace anonymous
177 int GET_USERS::Start(void *, const char * el, const char ** attr)
179 if (strcasecmp(el, m_tag.c_str()) != 0)
181 printfd(__FILE__, "Got wrong tag: '%s' instead of '%s'\n", el, m_tag.c_str());
185 while (attr && *attr && *(attr + 1))
187 if (strcasecmp(*attr, "LastUpdate") == 0)
188 str2x(*(attr + 1), m_lastUserUpdateTime);
195 void GET_USERS::CreateAnswer()
197 int h = m_users.OpenSearch();
200 if (m_lastUserUpdateTime > 0)
201 m_answer = "<Users LastUpdate=\"" + x2str(time(NULL)) + "\">";
203 m_answer = "<Users>";
207 while (m_users.SearchNext(h, &u) == 0)
208 m_answer += UserToXML(*u, true, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd, m_lastUserUpdateTime);
210 m_users.CloseSearch(h);
212 m_answer += "</Users>";
215 int GET_USER::Start(void *, const char * el, const char ** attr)
217 if (strcasecmp(el, m_tag.c_str()) != 0)
227 void GET_USER::CreateAnswer()
231 if (m_users.FindByName(m_login, &u))
232 m_answer = "<User result=\"error\" reason=\"User not found.\"/>";
234 m_answer = UserToXML(*u, false, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd);
237 int ADD_USER::Start(void *, const char * el, const char ** attr)
243 if (strcasecmp(el, m_tag.c_str()) == 0)
248 if (strcasecmp(el, "login") == 0)
257 void ADD_USER::CreateAnswer()
259 if (m_users.Exists(m_login))
260 m_answer = "<" + m_tag + " result=\"error\" reason=\"User '" + m_login + "' exists.\"/>";
261 else if (m_users.Add(m_login, &m_currAdmin) == 0)
262 m_answer = "<" + m_tag + " result=\"ok\"/>";
264 m_answer = "<" + m_tag + " result=\"error\" reason=\"Access denied\"/>";
267 int CHG_USER::Start(void *, const char * el, const char ** attr)
273 if (strcasecmp(el, m_tag.c_str()) == 0)
278 if (strcasecmp(el, "login") == 0)
284 if (strcasecmp(el, "ip") == 0)
286 m_ucr.ips = StrToIPS(attr[1]);
290 if (strcasecmp(el, "password") == 0)
292 m_ucr.password = attr[1];
296 if (strcasecmp(el, "address") == 0)
298 m_ucr.address = Decode21str(attr[1]);
302 if (strcasecmp(el, "aonline") == 0)
304 m_ucr.alwaysOnline = (*(attr[1]) != '0');
308 if (strcasecmp(el, "cash") == 0)
310 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
311 m_cashMsg = Decode21str(attr[3]);
314 if (strtodouble2(attr[1], cash) == 0)
317 m_cashMustBeAdded = (strcasecmp(attr[0], "add") == 0);
322 if (strcasecmp(el, "CreditExpire") == 0)
324 long int creditExpire = 0;
325 if (str2x(attr[1], creditExpire) == 0)
326 m_ucr.creditExpire = (time_t)creditExpire;
331 if (strcasecmp(el, "credit") == 0)
334 if (strtodouble2(attr[1], credit) == 0)
335 m_ucr.credit = credit;
339 if (strcasecmp(el, "freemb") == 0)
342 if (strtodouble2(attr[1], freeMb) == 0)
343 m_usr.freeMb = freeMb;
347 if (strcasecmp(el, "down") == 0)
350 if (str2x(attr[1], down) == 0)
351 m_ucr.disabled = down;
355 if (strcasecmp(el, "DisableDetailStat") == 0)
357 int disabledDetailStat = 0;
358 if (str2x(attr[1], disabledDetailStat) == 0)
359 m_ucr.disabledDetailStat = disabledDetailStat;
363 if (strcasecmp(el, "email") == 0)
365 m_ucr.email = Decode21str(attr[1]);
369 for (int i = 0; i < USERDATA_NUM; i++)
372 sprintf(name, "userdata%d", i);
373 if (strcasecmp(el, name) == 0)
375 m_ucr.userdata[i] = Decode21str(attr[1]);
380 if (strcasecmp(el, "group") == 0)
382 m_ucr.group = Decode21str(attr[1]);
386 if (strcasecmp(el, "note") == 0)
388 m_ucr.note = Decode21str(attr[1]);
392 if (strcasecmp(el, "passive") == 0)
395 if (str2x(attr[1], passive) == 0)
396 m_ucr.passive = passive;
400 if (strcasecmp(el, "phone") == 0)
402 m_ucr.phone = Decode21str(attr[1]);
406 if (strcasecmp(el, "Name") == 0)
408 m_ucr.realName = Decode21str(attr[1]);
412 if (strcasecmp(el, "traff") == 0)
417 int dir = attr[j][2] - '0';
419 if (strncasecmp(attr[j], "md", 2) == 0)
422 str2x(attr[j + 1], t);
425 if (strncasecmp(attr[j], "mu", 2) == 0)
428 str2x(attr[j + 1], t);
436 if (strcasecmp(el, "tariff") == 0)
438 if (strcasecmp(attr[0], "now") == 0)
439 m_ucr.tariffName = attr[1];
441 if (strcasecmp(attr[0], "delayed") == 0)
442 m_ucr.nextTariff = attr[1];
450 void CHG_USER::CreateAnswer()
452 if (ApplyChanges() == 0)
453 m_answer = "<" + m_tag + " result=\"ok\"/>";
455 m_answer = "<" + m_tag + " result=\"error\"/>";
458 int CHG_USER::ApplyChanges()
460 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
463 if (m_users.FindByName(m_login, &u))
467 bool alwaysOnline = u->GetProperty().alwaysOnline;
468 if (!m_ucr.alwaysOnline.empty())
471 alwaysOnline = m_ucr.alwaysOnline.const_data();
473 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
474 if (!m_ucr.ips.empty())
477 onlyOneIP = m_ucr.ips.const_data().OnlyOneIP();
480 if (check && alwaysOnline && !onlyOneIP)
482 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
483 GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.GetLogStr().c_str());
487 for (size_t i = 0; i < m_ucr.ips.const_data().Count(); ++i)
490 uint32_t ip = m_ucr.ips.const_data().operator[](i).ip;
491 if (m_users.IsIPInUse(ip, m_login, &user))
493 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());
494 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());
499 if (!m_ucr.ips.empty())
500 if (!u->GetProperty().ips.Set(m_ucr.ips.const_data(), &m_currAdmin, m_login, &m_store))
503 if (!m_ucr.alwaysOnline.empty())
504 if (!u->GetProperty().alwaysOnline.Set(m_ucr.alwaysOnline.const_data(),
505 &m_currAdmin, m_login, &m_store))
508 if (!m_ucr.address.empty())
509 if (!u->GetProperty().address.Set(m_ucr.address.const_data(), &m_currAdmin, m_login, &m_store))
512 if (!m_ucr.creditExpire.empty())
513 if (!u->GetProperty().creditExpire.Set(m_ucr.creditExpire.const_data(),
514 &m_currAdmin, m_login, &m_store))
517 if (!m_ucr.credit.empty())
518 if (!u->GetProperty().credit.Set(m_ucr.credit.const_data(), &m_currAdmin, m_login, &m_store))
521 if (!m_usr.freeMb.empty())
522 if (!u->GetProperty().freeMb.Set(m_usr.freeMb.const_data(), &m_currAdmin, m_login, &m_store))
525 if (!m_ucr.disabled.empty())
526 if (!u->GetProperty().disabled.Set(m_ucr.disabled.const_data(), &m_currAdmin, m_login, &m_store))
529 if (!m_ucr.disabledDetailStat.empty())
530 if (!u->GetProperty().disabledDetailStat.Set(m_ucr.disabledDetailStat.const_data(), &m_currAdmin, m_login, &m_store))
533 if (!m_ucr.email.empty())
534 if (!u->GetProperty().email.Set(m_ucr.email.const_data(), &m_currAdmin, m_login, &m_store))
537 if (!m_ucr.group.empty())
538 if (!u->GetProperty().group.Set(m_ucr.group.const_data(), &m_currAdmin, m_login, &m_store))
541 if (!m_ucr.note.empty())
542 if (!u->GetProperty().note.Set(m_ucr.note.const_data(), &m_currAdmin, m_login, &m_store))
545 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
546 userdata.push_back(u->GetProperty().userdata0.GetPointer());
547 userdata.push_back(u->GetProperty().userdata1.GetPointer());
548 userdata.push_back(u->GetProperty().userdata2.GetPointer());
549 userdata.push_back(u->GetProperty().userdata3.GetPointer());
550 userdata.push_back(u->GetProperty().userdata4.GetPointer());
551 userdata.push_back(u->GetProperty().userdata5.GetPointer());
552 userdata.push_back(u->GetProperty().userdata6.GetPointer());
553 userdata.push_back(u->GetProperty().userdata7.GetPointer());
554 userdata.push_back(u->GetProperty().userdata8.GetPointer());
555 userdata.push_back(u->GetProperty().userdata9.GetPointer());
557 for (int i = 0; i < (int)userdata.size(); i++)
558 if (!m_ucr.userdata[i].empty())
559 if(!userdata[i]->Set(m_ucr.userdata[i].const_data(), &m_currAdmin, m_login, &m_store))
562 if (!m_ucr.passive.empty())
563 if (!u->GetProperty().passive.Set(m_ucr.passive.const_data(), &m_currAdmin, m_login, &m_store))
566 if (!m_ucr.password.empty())
567 if (!u->GetProperty().password.Set(m_ucr.password.const_data(), &m_currAdmin, m_login, &m_store))
570 if (!m_ucr.phone.empty())
571 if (!u->GetProperty().phone.Set(m_ucr.phone.const_data(), &m_currAdmin, m_login, &m_store))
574 if (!m_ucr.realName.empty())
575 if (!u->GetProperty().realName.Set(m_ucr.realName.const_data(), &m_currAdmin, m_login, &m_store))
579 if (!m_usr.cash.empty())
580 if (m_cashMustBeAdded)
582 if (!u->GetProperty().cash.Set(m_usr.cash.const_data() + u->GetProperty().cash,
589 if (!u->GetProperty().cash.Set(m_usr.cash.const_data(), &m_currAdmin, m_login, &m_store, m_cashMsg))
594 if (!m_ucr.tariffName.empty())
596 if (m_tariffs.FindByName(m_ucr.tariffName.const_data()))
598 if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &m_currAdmin, m_login, &m_store))
600 u->ResetNextTariff();
604 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
609 if (!m_ucr.nextTariff.empty())
611 if (m_tariffs.FindByName(m_ucr.nextTariff.const_data()))
613 if (!u->GetProperty().nextTariff.Set(m_ucr.nextTariff.const_data(), &m_currAdmin, m_login, &m_store))
618 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
623 DIR_TRAFF up = u->GetProperty().up;
624 DIR_TRAFF down = u->GetProperty().down;
627 for (int i = 0; i < DIR_NUM; i++)
629 if (!m_upr[i].empty())
631 up[i] = m_upr[i].data();
634 if (!m_downr[i].empty())
636 down[i] = m_downr[i].data();
642 if (!u->GetProperty().up.Set(up, &m_currAdmin, m_login, &m_store))
646 if (!u->GetProperty().down.Set(down, &m_currAdmin, m_login, &m_store))
655 int DEL_USER::Start(void *, const char *el, const char **attr)
658 if (strcasecmp(el, m_tag.c_str()) == 0)
660 if (attr[0] == NULL || attr[1] == NULL)
662 //CreateAnswer("Parameters error!");
667 if (m_users.FindByName(attr[1], &u))
679 int DEL_USER::End(void *, const char *el)
681 if (strcasecmp(el, m_tag.c_str()) == 0)
684 m_users.Del(u->GetLogin(), &m_currAdmin);
691 void DEL_USER::CreateAnswer()
694 m_answer = "<" + m_tag + " value=\"error\" reason=\"User not found\"/>";
696 m_answer = "<" + m_tag + " value=\"ok\"/>";
699 int CHECK_USER::Start(void *, const char *el, const char **attr)
701 if (strcasecmp(el, m_tag.c_str()) == 0)
703 if (attr[0] == NULL || attr[1] == NULL ||
704 attr[2] == NULL || attr[3] == NULL)
706 CreateAnswer("Invalid parameters.");
707 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
712 if (m_users.FindByName(attr[1], &user))
714 CreateAnswer("User not found.");
715 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
719 if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
721 CreateAnswer("Wrong password.");
722 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
732 int CHECK_USER::End(void *, const char *el)
734 if (strcasecmp(el, m_tag.c_str()) == 0)
739 void CHECK_USER::CreateAnswer(const char * error)
742 m_answer = "<" + m_tag + " value=\"Err\" reason=\"" + error + "\"/>";
744 m_answer = "<" + m_tag + " value=\"Ok\"/>";