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, 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 answer = "<" + tag + " LastUpdate=\"" + x2str(time(NULL)) + "\">";
193 answer = GetOpenTag();
197 while (m_users.SearchNext(h, &u) == 0)
198 answer += UserToXML(*u, true, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd, m_lastUserUpdateTime);
200 m_users.CloseSearch(h);
202 answer += GetCloseTag();
205 int GET_USER::Start(void *, const char * el, const char ** attr)
207 if (strcasecmp(el, tag.c_str()) != 0)
217 void GET_USER::CreateAnswer()
221 if (m_users.FindByName(m_login, &u))
222 answer = "<" + tag + " result=\"error\" reason=\"User not found.\"/>";
224 answer = UserToXML(*u, false, currAdmin.GetPriv()->userConf || currAdmin.GetPriv()->userPasswd);
227 int ADD_USER::Start(void *, const char * el, const char ** attr)
233 if (strcasecmp(el, tag.c_str()) == 0)
238 if (strcasecmp(el, "login") == 0)
247 int ADD_USER::End(void *, const char *el)
251 if (strcasecmp(el, tag.c_str()) == 0)
260 void ADD_USER::CreateAnswer()
262 if (m_users.Exists(m_login))
263 answer = "<" + tag + " result=\"error\" reason=\"User '" + m_login + "' exists.\"/>";
264 else if (m_users.Add(m_login, &currAdmin) == 0)
265 answer = "<" + tag + " result=\"ok\"/>";
267 answer = "<" + tag + " result=\"error\" reason=\"Access denied\"/>";
270 int CHG_USER::Start(void *, const char * el, const char ** attr)
276 if (strcasecmp(el, tag.c_str()) == 0)
281 if (strcasecmp(el, "login") == 0)
287 if (strcasecmp(el, "ip") == 0)
289 m_ucr.ips = StrToIPS(attr[1]);
293 if (strcasecmp(el, "password") == 0)
295 m_ucr.password = attr[1];
299 if (strcasecmp(el, "address") == 0)
301 m_ucr.address = Decode21str(attr[1]);
305 if (strcasecmp(el, "aonline") == 0)
307 m_ucr.alwaysOnline = (*(attr[1]) != '0');
311 if (strcasecmp(el, "cash") == 0)
313 if (attr[2] && (strcasecmp(attr[2], "msg") == 0))
314 m_cashMsg = Decode21str(attr[3]);
317 if (strtodouble2(attr[1], cash) == 0)
320 m_cashMustBeAdded = (strcasecmp(attr[0], "add") == 0);
325 if (strcasecmp(el, "CreditExpire") == 0)
327 long int creditExpire = 0;
328 if (str2x(attr[1], creditExpire) == 0)
329 m_ucr.creditExpire = (time_t)creditExpire;
334 if (strcasecmp(el, "credit") == 0)
337 if (strtodouble2(attr[1], credit) == 0)
338 m_ucr.credit = credit;
342 if (strcasecmp(el, "freemb") == 0)
345 if (strtodouble2(attr[1], freeMb) == 0)
346 m_usr.freeMb = freeMb;
350 if (strcasecmp(el, "down") == 0)
353 if (str2x(attr[1], down) == 0)
354 m_ucr.disabled = down;
358 if (strcasecmp(el, "DisableDetailStat") == 0)
360 int disabledDetailStat = 0;
361 if (str2x(attr[1], disabledDetailStat) == 0)
362 m_ucr.disabledDetailStat = disabledDetailStat;
366 if (strcasecmp(el, "email") == 0)
368 m_ucr.email = Decode21str(attr[1]);
372 for (int i = 0; i < USERDATA_NUM; i++)
375 sprintf(name, "userdata%d", i);
376 if (strcasecmp(el, name) == 0)
378 m_ucr.userdata[i] = Decode21str(attr[1]);
383 if (strcasecmp(el, "group") == 0)
385 m_ucr.group = Decode21str(attr[1]);
389 if (strcasecmp(el, "note") == 0)
391 m_ucr.note = Decode21str(attr[1]);
395 if (strcasecmp(el, "passive") == 0)
398 if (str2x(attr[1], passive) == 0)
399 m_ucr.passive = passive;
403 if (strcasecmp(el, "phone") == 0)
405 m_ucr.phone = Decode21str(attr[1]);
409 if (strcasecmp(el, "Name") == 0)
411 m_ucr.realName = Decode21str(attr[1]);
415 if (strcasecmp(el, "traff") == 0)
420 int dir = attr[j][2] - '0';
422 if (strncasecmp(attr[j], "md", 2) == 0)
425 str2x(attr[j + 1], t);
428 if (strncasecmp(attr[j], "mu", 2) == 0)
431 str2x(attr[j + 1], t);
439 if (strcasecmp(el, "tariff") == 0)
441 if (strcasecmp(attr[0], "now") == 0)
442 m_ucr.tariffName = attr[1];
444 if (strcasecmp(attr[0], "delayed") == 0)
445 m_ucr.nextTariff = attr[1];
453 int CHG_USER::End(void *, const char *el)
457 if (strcasecmp(el, tag.c_str()) != 0)
467 void CHG_USER::CreateAnswer()
469 if (ApplyChanges() == 0)
470 answer = "<" + tag + " result=\"ok\"/>";
472 answer = "<" + tag + " result=\"error\"/>";
475 int CHG_USER::ApplyChanges()
477 printfd(__FILE__, "PARSER_CHG_USER::ApplyChanges()\n");
480 if (m_users.FindByName(m_login, &u))
484 bool alwaysOnline = u->GetProperty().alwaysOnline;
485 if (!m_ucr.alwaysOnline.empty())
488 alwaysOnline = m_ucr.alwaysOnline.const_data();
490 bool onlyOneIP = u->GetProperty().ips.ConstData().OnlyOneIP();
491 if (!m_ucr.ips.empty())
494 onlyOneIP = m_ucr.ips.const_data().OnlyOneIP();
497 if (check && alwaysOnline && !onlyOneIP)
499 printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
500 GetStgLogger()("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", currAdmin.GetLogStr().c_str());
504 for (size_t i = 0; i < m_ucr.ips.const_data().Count(); ++i)
507 uint32_t ip = m_ucr.ips.const_data().operator[](i).ip;
508 if (m_users.IsIPInUse(ip, m_login, &user))
510 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());
511 GetStgLogger()("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str());
516 if (!m_ucr.ips.empty())
517 if (!u->GetProperty().ips.Set(m_ucr.ips.const_data(), &currAdmin, m_login, store))
520 if (!m_ucr.alwaysOnline.empty())
521 if (!u->GetProperty().alwaysOnline.Set(m_ucr.alwaysOnline.const_data(),
522 &currAdmin, m_login, store))
525 if (!m_ucr.address.empty())
526 if (!u->GetProperty().address.Set(m_ucr.address.const_data(), &currAdmin, m_login, store))
529 if (!m_ucr.creditExpire.empty())
530 if (!u->GetProperty().creditExpire.Set(m_ucr.creditExpire.const_data(),
531 &currAdmin, m_login, store))
534 if (!m_ucr.credit.empty())
535 if (!u->GetProperty().credit.Set(m_ucr.credit.const_data(), &currAdmin, m_login, store))
538 if (!m_usr.freeMb.empty())
539 if (!u->GetProperty().freeMb.Set(m_usr.freeMb.const_data(), &currAdmin, m_login, store))
542 if (!m_ucr.disabled.empty())
543 if (!u->GetProperty().disabled.Set(m_ucr.disabled.const_data(), &currAdmin, m_login, store))
546 if (!m_ucr.disabledDetailStat.empty())
547 if (!u->GetProperty().disabledDetailStat.Set(m_ucr.disabledDetailStat.const_data(), &currAdmin, m_login, store))
550 if (!m_ucr.email.empty())
551 if (!u->GetProperty().email.Set(m_ucr.email.const_data(), &currAdmin, m_login, store))
554 if (!m_ucr.group.empty())
555 if (!u->GetProperty().group.Set(m_ucr.group.const_data(), &currAdmin, m_login, store))
558 if (!m_ucr.note.empty())
559 if (!u->GetProperty().note.Set(m_ucr.note.const_data(), &currAdmin, m_login, store))
562 std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
563 userdata.push_back(u->GetProperty().userdata0.GetPointer());
564 userdata.push_back(u->GetProperty().userdata1.GetPointer());
565 userdata.push_back(u->GetProperty().userdata2.GetPointer());
566 userdata.push_back(u->GetProperty().userdata3.GetPointer());
567 userdata.push_back(u->GetProperty().userdata4.GetPointer());
568 userdata.push_back(u->GetProperty().userdata5.GetPointer());
569 userdata.push_back(u->GetProperty().userdata6.GetPointer());
570 userdata.push_back(u->GetProperty().userdata7.GetPointer());
571 userdata.push_back(u->GetProperty().userdata8.GetPointer());
572 userdata.push_back(u->GetProperty().userdata9.GetPointer());
574 for (int i = 0; i < (int)userdata.size(); i++)
575 if (!m_ucr.userdata[i].empty())
576 if(!userdata[i]->Set(m_ucr.userdata[i].const_data(), &currAdmin, m_login, store))
579 if (!m_ucr.passive.empty())
580 if (!u->GetProperty().passive.Set(m_ucr.passive.const_data(), &currAdmin, m_login, store))
583 if (!m_ucr.password.empty())
584 if (!u->GetProperty().password.Set(m_ucr.password.const_data(), &currAdmin, m_login, store))
587 if (!m_ucr.phone.empty())
588 if (!u->GetProperty().phone.Set(m_ucr.phone.const_data(), &currAdmin, m_login, store))
591 if (!m_ucr.realName.empty())
592 if (!u->GetProperty().realName.Set(m_ucr.realName.const_data(), &currAdmin, m_login, store))
596 if (!m_usr.cash.empty())
597 if (m_cashMustBeAdded)
599 if (!u->GetProperty().cash.Set(m_usr.cash.const_data() + u->GetProperty().cash,
606 if (!u->GetProperty().cash.Set(m_usr.cash.const_data(), &currAdmin, m_login, store, m_cashMsg))
611 if (!m_ucr.tariffName.empty())
613 if (m_tariffs.FindByName(m_ucr.tariffName.const_data()))
615 if (!u->GetProperty().tariffName.Set(m_ucr.tariffName.const_data(), &currAdmin, m_login, store))
617 u->ResetNextTariff();
621 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
626 if (!m_ucr.nextTariff.empty())
628 if (m_tariffs.FindByName(m_ucr.nextTariff.const_data()))
630 if (!u->GetProperty().nextTariff.Set(m_ucr.nextTariff.const_data(), &currAdmin, m_login, store))
635 //WriteServLog("SetUser: Tariff %s not found", ud.conf.tariffName.c_str());
640 DIR_TRAFF up = u->GetProperty().up;
641 DIR_TRAFF down = u->GetProperty().down;
644 for (int i = 0; i < DIR_NUM; i++)
646 if (!m_upr[i].empty())
648 up[i] = m_upr[i].data();
651 if (!m_downr[i].empty())
653 down[i] = m_downr[i].data();
659 if (!u->GetProperty().up.Set(up, &currAdmin, m_login, store))
663 if (!u->GetProperty().down.Set(down, &currAdmin, m_login, store))
672 int DEL_USER::Start(void *, const char *el, const char **attr)
675 if (strcasecmp(el, tag.c_str()) == 0)
677 if (attr[0] == NULL || attr[1] == NULL)
679 //CreateAnswer("Parameters error!");
684 if (m_users.FindByName(attr[1], &u))
696 int DEL_USER::End(void *, const char *el)
698 if (strcasecmp(el, tag.c_str()) == 0)
701 m_users.Del(u->GetLogin(), &currAdmin);
708 void DEL_USER::CreateAnswer()
711 answer = "<" + tag + " value=\"error\" reason=\"User not found\"/>";
713 answer = "<" + tag + " value=\"ok\"/>";
716 int CHECK_USER::Start(void *, const char *el, const char **attr)
718 if (strcasecmp(el, tag.c_str()) == 0)
720 if (attr[0] == NULL || attr[1] == NULL ||
721 attr[2] == NULL || attr[3] == NULL)
723 CreateAnswer("Invalid parameters.");
724 printfd(__FILE__, "PARSER_CHECK_USER - attr err\n");
729 if (m_users.FindByName(attr[1], &user))
731 CreateAnswer("User not found.");
732 printfd(__FILE__, "PARSER_CHECK_USER - login err\n");
736 if (strcmp(user->GetProperty().password.Get().c_str(), attr[3]))
738 CreateAnswer("Wrong password.");
739 printfd(__FILE__, "PARSER_CHECK_USER - passwd err\n");
749 int CHECK_USER::End(void *, const char *el)
751 if (strcasecmp(el, tag.c_str()) == 0)
756 void CHECK_USER::CreateAnswer(const char * error)
759 answer = "<" + tag + " value=\"Err\" reason=\"" + error + "\"/>";
761 answer = "<" + tag + " value=\"Ok\"/>";