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>
 
  24  $Date: 2009/06/08 10:02:28 $
 
  27 #include "stg/common.h"
 
  28 #include "sg_error_codes.h"
 
  29 #include "common_sg.h"
 
  30 #include "version_sg.h"
 
  43 const int usageConf = 0;
 
  44 const int usageInfo = 1;
 
  46 const int TO_KOI8 = 0;
 
  47 const int FROM_KOI8 = 1;
 
  48 //-----------------------------------------------------------------------------
 
  54 //-----------------------------------------------------------------------------
 
  60 //---------------------------------------------------------------------------
 
  68 //---------------------------------------------------------------------------
 
  69 void Usage(int usageType)
 
  71 printf("Sgconf version: %s\n\n", VERSION_SG);
 
  74 if (usageType == usageConf)
 
  75     strcpy(action, "set");
 
  77     strcpy(action, "get");
 
  79 printf("To add or to set cash use:\n");
 
  80 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
 
  81 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
 
  82 printf("To get cash use:\n");
 
  83 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
 
  87     {"set tariff",              "get tariff",           "-t",   "<tariff:now|delayed>"},
 
  88     {"set credit",              "get credit",           "-r",   "<credit>"},
 
  89     {"set credit expire",       "get credit expire",    "-E",   "<credit_expire_date>"},
 
  90     {"set password",            "get password",         "-o",   "<new_password>"},
 
  91     {"set prepaid traffic",     "get prepaid traffic",  "-e",   "<prepaid>"},
 
  92     {"set IP-addresses",        "get IP-addresses",     "-I",   "<*|ip_addr[,ip_addr...]>"},
 
  93     {"set name",                "get name",             "-A",   "<name>"},
 
  94     {"set note",                "get note",             "-N",   "<note>"},
 
  95     {"set street address",      "get street address",   "-D",   "<address>"},
 
  96     {"set email",               "get email",            "-L",   "<email>"},
 
  97     {"set phone",               "get phone",            "-P",   "<phone>"},
 
  98     {"set group",               "get group",            "-G",   "<group>"},
 
  99     {"set/unset down",          "get down",             "-d",   "<0/1>"},
 
 100     {"set/unset \'passive\'",   "get \'passive\'",      "-i",   "<0/1>"},
 
 101     {"set/unset \'disableDetailStat\'",   "get \'disableDetailStat\'",      "--disable-stat",   "<0/1>"},
 
 102     {"set/unset \'alwaysOnline\'",   "get \'alwaysOnline\'",      "--always-online",   "<0/1>"},
 
 105 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
 
 107     printf("To %s use:\n", hp[i].setActionName.c_str());
 
 108     printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
 
 109            hp[i].valueName.c_str(), hp[i].valueParam.c_str());
 
 110     printf("To %s use:\n", hp[i].getActionName.c_str());
 
 111     printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
 
 112            hp[i].valueName.c_str());
 
 115 printf("To set user\'s upload traffic value use:\n");
 
 116 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
 
 117 printf("To get user\'s upload traffic value use:\n");
 
 118 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
 
 120 printf("To set user\'s download traffic value use:\n");
 
 121 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
 
 122 printf("To get user\'s download traffic value use:\n");
 
 123 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
 
 125 printf("To set userdata<0...9> use:\n");
 
 126 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
 
 127 printf("To get userdata<0...9> use:\n");
 
 128 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
 
 130 printf("To get user's authorizers list use:\n");
 
 131 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
 
 133 printf("To send message use:\n");
 
 134 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
 
 136 printf("To create user use:\n");
 
 137 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
 
 139 printf("To delete user use:\n");
 
 140 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
 
 142 //---------------------------------------------------------------------------
 
 147 //---------------------------------------------------------------------------
 
 152 //---------------------------------------------------------------------------
 
 153 int CheckLogin(const char * login)
 
 155 for (int i = 0; i < (int)strlen(login); i++)
 
 157     if (!(( login[i] >= 'a' && login[i] <= 'z')
 
 158         || (login[i] >= 'A' && login[i] <= 'Z')
 
 159         || (login[i] >= '0' && login[i] <= '9')
 
 169 //-----------------------------------------------------------------------------
 
 170 short int ParseServerPort(const char * p)
 
 173 if (str2x(p, port) != 0)
 
 175     printf("Incorresct server port %s\n", p);
 
 176     exit(NETWORK_ERR_CODE);
 
 180 //-----------------------------------------------------------------------------
 
 181 char * ParseAdminLogin(char * adm)
 
 185     printf("Incorresct admin login %s\n", adm);
 
 186     exit(PARAMETER_PARSING_ERR_CODE);
 
 190 //-----------------------------------------------------------------------------
 
 191 char * ParsePassword(char * pass)
 
 193 if (strlen(pass) >= ADM_PASSWD_LEN)
 
 195     printf("Password too big %s\n", pass);
 
 196     exit(PARAMETER_PARSING_ERR_CODE);
 
 201 //-----------------------------------------------------------------------------
 
 202 char * ParseUser(char * usr)
 
 206     printf("Incorresct user login %s\n", usr);
 
 207     exit(PARAMETER_PARSING_ERR_CODE);
 
 211 //-----------------------------------------------------------------------------
 
 212 void ConvertKOI8(const string & src, string * dst, int encType)
 
 216 setlocale(LC_ALL, "");
 
 221 if (encType == TO_KOI8)
 
 223     strcpy(charsetF, nl_langinfo(CODESET));
 
 224     strcpy(charsetT, "koi8-ru");
 
 228     strcpy(charsetT, nl_langinfo(CODESET));
 
 229     strcpy(charsetF, "koi8-ru");
 
 234 cd = iconv_open(charsetT, charsetF);
 
 235 if (cd == (iconv_t) -1)
 
 238         printf("error iconv_open\n");
 
 241         printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
 
 246     exit(ICONV_ERR_CODE);
 
 249 char * ob = new char[src.size() * 2 + 1];
 
 250 char * ib = new char[src.size() + 1];
 
 252 strcpy(ib, src.c_str());
 
 257 size_t insize = strlen(ib);
 
 258 size_t outsize = insize * 2 + 1;
 
 262 #if defined(CONST_ICONV)
 
 263 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
 
 265 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
 
 267 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
 
 268 //printf("ib=%s ob=%s\n", ib, ob);
 
 269 //printf("nconv=%d outsize=%d\n", nconv, outsize);
 
 270 if (nconv == (size_t) -1)
 
 274         printf("iconv error\n");
 
 275         exit(ICONV_ERR_CODE);
 
 287 //-----------------------------------------------------------------------------
 
 288 void ConvertFromKOI8(const string & src, string * dst)
 
 290 ConvertKOI8(src, dst, FROM_KOI8);
 
 292 //-----------------------------------------------------------------------------
 
 293 void ConvertToKOI8(const string & src, string * dst)
 
 295 ConvertKOI8(src, dst, TO_KOI8);
 
 297 //-----------------------------------------------------------------------------
 
 298 int RecvSetUserAnswer(const char * ans, void * d)
 
 300 GetUserCbData * gucbd = static_cast<GetUserCbData*>(d);
 
 302 bool * result = gucbd->result;
 
 304 if (strcasecmp("Ok", ans) == 0)
 
 311 //-----------------------------------------------------------------------------
 
 312 struct StringReqParams
 
 315     RESETABLE<string> reqParam;
 
 318 //-----------------------------------------------------------------------------
 
 319 void RecvUserData(USERDATA * ud, void * d)
 
 321 GetUserCbData * gucbd = static_cast<GetUserCbData*>(d);
 
 323 bool * result = gucbd->result;
 
 325 REQUEST * req = (REQUEST *)gucbd->data;
 
 333 if (!req->cash.empty())
 
 334     cout << "cash=" << ud->cash << endl;
 
 336 if (!req->credit.empty())
 
 337     cout << "credit=" << ud->credit << endl;
 
 339 if (!req->creditExpire.empty())
 
 342     struct tm brokenTime;
 
 343     time_t tt = ud->creditExpire;
 
 345     brokenTime.tm_wday = 0;
 
 346     brokenTime.tm_yday = 0;
 
 347     brokenTime.tm_isdst = 0;
 
 348     brokenTime.tm_hour = 0;
 
 349     brokenTime.tm_min = 0;
 
 350     brokenTime.tm_sec = 0;
 
 352     gmtime_r(&tt, &brokenTime);
 
 354     strftime(buf, 32, "%Y-%m-%d", &brokenTime);
 
 356     cout << "creditExpire=" << buf << endl;
 
 359 if (!req->down.empty())
 
 360     cout << "down=" << ud->down << endl;
 
 362 if (!req->passive.empty())
 
 363     cout << "passive=" << ud->passive << endl;
 
 365 if (!req->disableDetailStat.empty())
 
 366     cout << "disableDetailStat=" << ud->disableDetailStat << endl;
 
 368 if (!req->alwaysOnline.empty())
 
 369     cout << "alwaysOnline=" << ud->alwaysOnline << endl;
 
 371 if (!req->prepaidTraff.empty())
 
 372     cout << "prepaidTraff=" << ud->prepaidTraff << endl;
 
 374 for (int i = 0; i < DIR_NUM; i++)
 
 376     if (!req->u[i].empty())
 
 377         cout << "u" << i << "=" << ud->stat.mu[i] << endl;
 
 378     if (!req->d[i].empty())
 
 379         cout << "d" << i << "=" << ud->stat.md[i] << endl;
 
 382 for (int i = 0; i < USERDATA_NUM; i++)
 
 384     if (!req->ud[i].empty())
 
 387         ConvertFromKOI8(ud->userData[i], &str);
 
 388         cout << "userdata" << i << "=" << str << endl;
 
 392 StringReqParams strReqParams[] =
 
 394     {"note",     req->note,        &ud->note},
 
 395     {"name",     req->name,        &ud->name},
 
 396     {"address",  req->address,     &ud->address},
 
 397     {"email",    req->email,       &ud->email},
 
 398     {"phone",    req->phone,       &ud->phone},
 
 399     {"group",    req->group,       &ud->group},
 
 400     {"tariff",   req->tariff,      &ud->tariff},
 
 401     {"password", req->usrPasswd,   &ud->password},
 
 402     {"ip",       req->ips,         &ud->ips} // IP-address of user
 
 404 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
 
 406     if (!strReqParams[i].reqParam.empty())
 
 409         ConvertFromKOI8(*strReqParams[i].value, &str);
 
 410         cout << strReqParams[i].name << "=" << str << endl;
 
 415 //-----------------------------------------------------------------------------
 
 416 void RecvAuthByData(const std::vector<std::string> & list, void * d)
 
 418 AuthByCbData * abcbd = static_cast<AuthByCbData*>(d);
 
 420 bool * result = abcbd->result;
 
 422 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
 
 428 //-----------------------------------------------------------------------------
 
 429 int ProcessSetUser(const std::string &server,
 
 431                    const std::string &admLogin,
 
 432                    const std::string &admPasswd,
 
 433                    const std::string &str,
 
 442 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
 
 443 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
 
 444 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
 
 445 sc.SetAdmPassword(admPasswd.c_str());
 
 447 // TODO Good variable name :)
 
 451 gucbd.result = &result;
 
 455     sc.SetSendMessageCb(RecvSetUserAnswer, &gucbd);
 
 456     sc.MsgUser(str.c_str());
 
 460     sc.SetChgUserCb(RecvSetUserAnswer, &gucbd);
 
 461     sc.ChgUser(str.c_str());
 
 477 //-----------------------------------------------------------------------------
 
 478 int ProcessGetUser(const std::string &server,
 
 480                    const std::string &admLogin,
 
 481                    const std::string &admPasswd,
 
 482                    const std::string &login,
 
 489 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
 
 490 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
 
 491 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
 
 492 sc.SetAdmPassword(admPasswd.c_str());
 
 494 // TODO Good variable name :)
 
 498 gucbd.result = &result;
 
 500 sc.SetGetUserDataRecvCb(RecvUserData, &gucbd);
 
 501 sc.GetUser(login.c_str());
 
 516 //-----------------------------------------------------------------------------
 
 517 int ProcessAuthBy(const std::string &server,
 
 519                   const std::string &admLogin,
 
 520                   const std::string &admPasswd,
 
 521                   const std::string &login,
 
 528 sc.SetServer(server.c_str());  // õÓÔÁÎÁ×ÌÉ×ÁÅÍ ÉÍÑ ÓÅÒ×ÅÒÁ Ó ËÏÔÏÒÇÏ ÚÁÂÉÒÁÔØ ÉÎÆÕ
 
 529 sc.SetPort(port);           // ÁÄÍÉÎÓËÉÊ ÐÏÒÔ ÓÅÒ×ÅÒÁÐÏÒÔ
 
 530 sc.SetAdmLogin(admLogin.c_str());    // ÷ÙÓÔÁ×ÌÑÅÍ ÌÏÇÉΠɠÐÁÒÏÌØ ÁÄÍÉÎÁ
 
 531 sc.SetAdmPassword(admPasswd.c_str());
 
 533 // TODO Good variable name :)
 
 537 abcbd.result = &result;
 
 539 sc.SetGetUserAuthByRecvCb(RecvAuthByData, &abcbd);
 
 540 sc.GetUserAuthBy(login.c_str());
 
 555 //-----------------------------------------------------------------------------