]> git.stg.codes - stg.git/blob - projects/sgconf/common_sg.cpp
Fixed compilation issues.
[stg.git] / projects / sgconf / common_sg.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Author: faust $
23  $Revision: 1.12 $
24  $Date: 2009/06/08 10:02:28 $
25  */
26
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <locale.h>
33 #include <langinfo.h>
34 #include <iostream>
35 #include <iconv.h>
36
37 #include "stg/common.h"
38 #include "sg_error_codes.h"
39 #include "common_sg.h"
40 #include "version_sg.h"
41
42 using namespace std;
43
44 const int usageConf = 0;
45 const int usageInfo = 1;
46
47 const int TO_KOI8 = 0;
48 const int FROM_KOI8 = 1;
49 //-----------------------------------------------------------------------------
50 struct GetUserData
51 {
52     GetUserData(REQUEST & req, bool res) : request(req), result(res) {}
53     REQUEST & request;
54     bool result;
55 };
56 //---------------------------------------------------------------------------
57 struct HelpParams
58 {
59     string setActionName;
60     string getActionName;
61     string valueName;
62     string valueParam;
63 };
64 //---------------------------------------------------------------------------
65 void Usage(int usageType)
66 {
67 printf("Sgconf version: %s\n\n", VERSION_SG);
68
69 char action[4];
70 if (usageType == usageConf)
71     strcpy(action, "set");
72 else
73     strcpy(action, "get");
74
75 printf("To add or to set cash use:\n");
76 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c <add_cash[:log message]>\n");
77 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -v <set_cash[:log message]>\n");
78 printf("To get cash use:\n");
79 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -c\n\n");
80
81 HelpParams hp[] =
82 {
83     {"set tariff",              "get tariff",           "-t",   "<tariff:now|delayed>"},
84     {"set credit",              "get credit",           "-r",   "<credit>"},
85     {"set credit expire",       "get credit expire",    "-E",   "<credit_expire_date>"},
86     {"set password",            "get password",         "-o",   "<new_password>"},
87     {"set prepaid traffic",     "get prepaid traffic",  "-e",   "<prepaid>"},
88     {"set IP-addresses",        "get IP-addresses",     "-I",   "<*|ip_addr[,ip_addr...]>"},
89     {"set name",                "get name",             "-A",   "<name>"},
90     {"set note",                "get note",             "-N",   "<note>"},
91     {"set street address",      "get street address",   "-D",   "<address>"},
92     {"set email",               "get email",            "-L",   "<email>"},
93     {"set phone",               "get phone",            "-P",   "<phone>"},
94     {"set group",               "get group",            "-G",   "<group>"},
95     {"set/unset down",          "get down",             "-d",   "<0/1>"},
96     {"set/unset \'passive\'",   "get \'passive\'",      "-i",   "<0/1>"},
97     {"set/unset \'disableDetailStat\'",   "get \'disableDetailStat\'",      "--disable-stat",   "<0/1>"},
98     {"set/unset \'alwaysOnline\'",   "get \'alwaysOnline\'",      "--always-online",   "<0/1>"},
99 };
100
101 for (unsigned i = 0; i < sizeof(hp) / sizeof(HelpParams); i++)
102     {
103     printf("To %s use:\n", hp[i].setActionName.c_str());
104     printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s %s\n",
105            hp[i].valueName.c_str(), hp[i].valueParam.c_str());
106     printf("To %s use:\n", hp[i].getActionName.c_str());
107     printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> %s\n\n",
108            hp[i].valueName.c_str());
109     }
110
111 printf("To set user\'s upload traffic value use:\n");
112 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 <traff> [--u1<traff> ...]\n");
113 printf("To get user\'s upload traffic value use:\n");
114 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --u0 [--u1 ...]\n\n");
115
116 printf("To set user\'s download traffic value use:\n");
117 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 <traff> [--d1<traff> ...]\n");
118 printf("To get user\'s download traffic value use:\n");
119 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --d0 [--d1 ...]\n\n");
120
121 printf("To set userdata<0...9> use:\n");
122 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 <userdata> [--ud1<userdata> ...]\n");
123 printf("To get userdata<0...9> use:\n");
124 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --ud0 [--ud1 ...]\n\n");
125
126 printf("To get user's authorizers list use:\n");
127 printf("sgconf get -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> --authorized-by\n\n");
128
129 printf("To send message use:\n");
130 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -m <message>\n\n");
131
132 printf("To create user use:\n");
133 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -n\n\n");
134
135 printf("To delete user use:\n");
136 printf("sgconf set -s <server> -p <port> -a <admin> -w <admin_pass> -u <user> -l\n\n");
137 }
138 //---------------------------------------------------------------------------
139 void UsageConf()
140 {
141 Usage(usageConf);
142 }
143 //---------------------------------------------------------------------------
144 void UsageInfo()
145 {
146 Usage(usageInfo);
147 }
148 //---------------------------------------------------------------------------
149 int CheckLogin(const char * login)
150 {
151 for (int i = 0; i < (int)strlen(login); i++)
152     {
153     if (!(( login[i] >= 'a' && login[i] <= 'z')
154         || (login[i] >= 'A' && login[i] <= 'Z')
155         || (login[i] >= '0' && login[i] <= '9')
156         ||  login[i] == '.'
157         ||  login[i] == '_'
158         ||  login[i] == '-'))
159         {
160         return 1;
161         }
162     }
163 return 0;
164 }
165 //-----------------------------------------------------------------------------
166 short int ParseServerPort(const char * p)
167 {
168 int port;
169 if (str2x(p, port) != 0)
170     {
171     printf("Incorresct server port %s\n", p);
172     exit(NETWORK_ERR_CODE);
173     }
174 return (short)port;
175 }
176 //-----------------------------------------------------------------------------
177 char * ParseAdminLogin(char * adm)
178 {
179 if (CheckLogin(adm))
180     {
181     printf("Incorresct admin login %s\n", adm);
182     exit(PARAMETER_PARSING_ERR_CODE);
183     }
184 return adm;
185 }
186 //-----------------------------------------------------------------------------
187 char * ParsePassword(char * pass)
188 {
189 if (strlen(pass) >= ADM_PASSWD_LEN)
190     {
191     printf("Password too big %s\n", pass);
192     exit(PARAMETER_PARSING_ERR_CODE);
193     }
194
195 return pass;
196 }
197 //-----------------------------------------------------------------------------
198 char * ParseUser(char * usr)
199 {
200 if (CheckLogin(usr))
201     {
202     printf("Incorresct user login %s\n", usr);
203     exit(PARAMETER_PARSING_ERR_CODE);
204     }
205 return usr;
206 }
207 //-----------------------------------------------------------------------------
208 void ConvertKOI8(const string & src, string * dst, int encType)
209 {
210 iconv_t cd;
211 char * ob = new char[src.size() * 2 + 1];
212 char * ib = new char[src.size() + 1];
213
214 strcpy(ib, src.c_str());
215
216 char * outbuf = ob;
217 char * inbuf = ib;
218
219 setlocale(LC_ALL, "");
220
221 char charsetF[100];
222 char charsetT[100];
223
224 if (encType == TO_KOI8)
225     {
226     strcpy(charsetF, nl_langinfo(CODESET));
227     strcpy(charsetT, "koi8-ru");
228     }
229 else
230     {
231     strcpy(charsetT, nl_langinfo(CODESET));
232     strcpy(charsetF, "koi8-ru");
233     }
234
235 size_t nconv = 1;
236
237 size_t insize = strlen(ib);
238 size_t outsize = insize * 2 + 1;
239
240 insize = src.size();
241
242 cd = iconv_open(charsetT, charsetF);
243 if (cd == (iconv_t) -1)
244     {
245     if (errno != EINVAL)
246         printf("error iconv_open\n");
247     else
248         {
249         printf("Warning: iconv from %s to %s failed\n", charsetF, charsetT);
250         *dst = src;
251         return;
252         }
253
254     exit(ICONV_ERR_CODE);
255     }
256
257 #if defined(FREE_BSD) || defined(FREE_BSD5)
258 nconv = iconv(cd, (const char **)&inbuf, &insize, &outbuf, &outsize);
259 #else
260 nconv = iconv(cd, &inbuf, &insize, &outbuf, &outsize);
261 #endif
262 //printf("charsetT=%s charsetF=%s\n", charsetT, charsetF);
263 //printf("ib=%s ob=%s\n", ib, ob);
264 //printf("nconv=%d outsize=%d\n", nconv, outsize);
265 if (nconv == (size_t) -1)
266     {
267     if (errno != EINVAL)
268         {
269         printf("iconv error\n");
270         exit(ICONV_ERR_CODE);
271         }
272     }
273
274 *outbuf = L'\0';
275
276 iconv_close(cd);
277 *dst = ob;
278
279 delete[] ob;
280 delete[] ib;
281 }
282 //-----------------------------------------------------------------------------
283 void ConvertFromKOI8(const string & src, string * dst)
284 {
285 ConvertKOI8(src, dst, FROM_KOI8);
286 }
287 //-----------------------------------------------------------------------------
288 void ConvertToKOI8(const string & src, string * dst)
289 {
290 ConvertKOI8(src, dst, TO_KOI8);
291 }
292 //-----------------------------------------------------------------------------
293 int RecvSetUserAnswer(const char * ans, void * d)
294 {
295 GetUserData * data = static_cast<GetUserData *>(d);
296
297 data->result = (strcasecmp("Ok", ans) == 0);
298
299 return 0;
300 }
301 //-----------------------------------------------------------------------------
302 struct StringReqParams
303 {
304     string name;
305     RESETABLE<string> reqParam;
306     const string * value;
307 };
308 //-----------------------------------------------------------------------------
309 void GetUserCallback(const PARSER_GET_USER::INFO & info, void * d)
310 {
311 GetUserData * data = static_cast<GetUserData *>(d);
312
313 if (info.login == "")
314     {
315     data->result = false;
316     return;
317     }
318
319 if (!data->request.cash.res_empty())
320     cout << "cash = " << info.cash << endl;
321
322 if (!data->request.credit.res_empty())
323     cout << "credit = " << info.credit << endl;
324
325 if (!data->request.creditExpire.res_empty())
326     {
327     char buf[32];
328     struct tm brokenTime;
329     time_t tt = info.creditExpire;
330
331     brokenTime.tm_wday = 0;
332     brokenTime.tm_yday = 0;
333     brokenTime.tm_isdst = 0;
334     brokenTime.tm_hour = 0;
335     brokenTime.tm_min = 0;
336     brokenTime.tm_sec = 0;
337
338     gmtime_r(&tt, &brokenTime);
339
340     strftime(buf, 32, "%Y-%m-%d", &brokenTime);
341
342     cout << "creditExpire = " << buf << endl;
343     }
344
345 if (!data->request.down.res_empty())
346     cout << "down = " << info.down << endl;
347
348 if (!data->request.passive.res_empty())
349     cout << "passive = " << info.passive << endl;
350
351 if (!data->request.disableDetailStat.res_empty())
352     cout << "disableDetailStat = " << info.disableDetailStat << endl;
353
354 if (!data->request.alwaysOnline.res_empty())
355     cout << "alwaysOnline = " << info.alwaysOnline << endl;
356
357 if (!data->request.prepaidTraff.res_empty())
358     cout << "prepaidTraff = " << info.prepaidTraff << endl;
359
360 for (int i = 0; i < DIR_NUM; i++)
361     {
362     if (!data->request.sessionUpload[i].res_empty())
363         cout << "session upload for dir " << i << " = " << info.stat.su[i] << endl;
364     if (!data->request.sessionDownload[i].res_empty())
365         cout << "session download for dir " << i << "=" << info.stat.sd[i] << endl;
366     }
367
368 for (int i = 0; i < DIR_NUM; i++)
369     {
370     if (!data->request.monthUpload[i].res_empty())
371         cout << "month upload for dir " << i << " = " << info.stat.mu[i] << endl;
372     if (!data->request.monthDownload[i].res_empty())
373         cout << "month download for dir " << i << " = " << info.stat.md[i] << endl;
374     }
375
376 for (int i = 0; i < USERDATA_NUM; i++)
377     {
378     if (!data->request.userData[i].res_empty())
379         {
380         string str;
381         ConvertFromKOI8(info.userData[i], &str);
382         cout << "user data " << i << " = " << str << endl;
383         }
384     }
385
386 StringReqParams strReqParams[] =
387 {
388     {"note",     data->request.note,        &info.note},
389     {"name",     data->request.name,        &info.name},
390     {"address",  data->request.address,     &info.address},
391     {"email",    data->request.email,       &info.email},
392     {"phone",    data->request.phone,       &info.phone},
393     {"group",    data->request.group,       &info.group},
394     {"tariff",   data->request.tariff,      &info.tariff},
395     {"password", data->request.usrPasswd,   &info.password},
396     {"ip",       data->request.ips,         &info.ips} // IP-address of user
397 };
398 for (unsigned i = 0; i < sizeof(strReqParams) / sizeof(StringReqParams); i++)
399     {
400     if (!strReqParams[i].reqParam.res_empty())
401         {
402         string str;
403         ConvertFromKOI8(*strReqParams[i].value, &str);
404         cout << strReqParams[i].name << " = " << str << endl;
405         }
406     }
407 data->result = true;
408 }
409 //-----------------------------------------------------------------------------
410 void RecvAuthByData(const PARSER_AUTH_BY::INFO & list, void *)
411 {
412 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
413     cout << *it << "\n";
414 cout << endl;
415 }
416 //-----------------------------------------------------------------------------
417 int ProcessSetUser(const std::string &server,
418                    int port,
419                    const std::string &admLogin,
420                    const std::string &admPasswd,
421                    const std::string &str,
422                    bool isMessage)
423 {
424 SERVCONF sc;
425
426 sc.SetServer(server.c_str());
427 sc.SetPort(port);
428 sc.SetAdmLogin(admLogin.c_str());
429 sc.SetAdmPassword(admPasswd.c_str());
430
431 REQUEST request;
432 GetUserData cbdata(request, false);
433
434 int res = 0;
435 if (isMessage)
436     {
437     sc.SetSendMessageCb(RecvSetUserAnswer, &cbdata);
438     res = sc.MsgUser(str.c_str());
439     }
440 else
441     {
442     sc.SetChgUserCallback(RecvSetUserAnswer, &cbdata);
443     res = sc.ChgUser(str.c_str());
444     }
445
446 if (res && cbdata.result)
447     {
448     printf("Ok\n");
449     return 0;
450     }
451 else
452     {
453     printf("Error\n");
454     return -1;
455     }
456
457 return 0;
458 }
459 //-----------------------------------------------------------------------------
460 int ProcessGetUser(const std::string &server,
461                    int port,
462                    const std::string &admLogin,
463                    const std::string &admPasswd,
464                    const std::string &login,
465                    REQUEST & request)
466 {
467 SERVCONF sc;
468
469 sc.SetServer(server.c_str());
470 sc.SetPort(port);
471 sc.SetAdmLogin(admLogin.c_str());
472 sc.SetAdmPassword(admPasswd.c_str());
473
474 // TODO Good variable name :)
475 GetUserData data(request, false);
476
477 sc.SetGetUserCallback(GetUserCallback, &data);
478 bool res = (sc.GetUser(login.c_str()) == st_ok);
479
480 if (res && data.result)
481     {
482     printf("Ok\n");
483     return 0;
484     }
485 else
486     {
487     printf("Error\n");
488     return -1;
489     }
490
491 return 0;
492 }
493 //-----------------------------------------------------------------------------
494 int ProcessAuthBy(const std::string &server,
495                   int port,
496                   const std::string &admLogin,
497                   const std::string &admPasswd,
498                   const std::string &login)
499 {
500 SERVCONF sc;
501
502 sc.SetServer(server.c_str());
503 sc.SetPort(port);
504 sc.SetAdmLogin(admLogin.c_str());
505 sc.SetAdmPassword(admPasswd.c_str());
506
507 sc.SetAuthByCallback(RecvAuthByData, NULL);
508 bool res = (sc.AuthBy(login.c_str()) == st_ok);
509
510 if (!res)
511     {
512     printf("Error\n");
513     return -1;
514     }
515
516 printf("Ok\n");
517 return 0;
518 }
519 //-----------------------------------------------------------------------------