]> git.stg.codes - stg.git/blob - projects/sgauthstress/settings_impl.cpp
Authorization protocol stress test utility
[stg.git] / projects / sgauthstress / settings_impl.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 : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include <cstring>
22
23 #include "stg/dotconfpp.h"
24 #include "stg/module_settings.h"
25 #include "stg/common.h"
26
27 #include "settings_impl.h"
28
29 SETTINGS_IMPL::SETTINGS_IMPL()
30     : port(0),
31       localPort(0),
32       listenWebIP(0),
33       refreshPeriod(0),
34       daemon(false),
35       noWeb(false),
36       reconnect(false),
37       showPid(false),
38       confFile("/etc/sgauth.conf")
39 {
40 }
41 //-----------------------------------------------------------------------------
42 int SETTINGS_IMPL::ParseYesNo(const string & value, bool * val)
43 {
44 if (0 == strcasecmp(value.c_str(), "yes"))
45     {
46     *val = true;
47     return 0;
48     }
49 if (0 == strcasecmp(value.c_str(), "no"))
50     {
51     *val = false;
52     return 0;
53     }
54
55 strError = "Incorrect value \'" + value + "\'.";
56 return -1;
57 }
58 //-----------------------------------------------------------------------------
59 int SETTINGS_IMPL::ParseInt(const string & value, int * val)
60 {
61 if (str2x<int>(value, *val))
62     {
63     strError = "Cannot convert \'" + value + "\' to integer.";
64     return -1;
65     }
66 return 0;
67 }
68 //-----------------------------------------------------------------------------
69 int SETTINGS_IMPL::ParseUnsigned(const string & value, unsigned * val)
70 {
71 if (str2x<unsigned>(value, *val))
72     {
73     strError = "Cannot convert \'" + value + "\' to unsigned integer.";
74     return -1;
75     }
76 return 0;
77 }
78 //-----------------------------------------------------------------------------
79 int SETTINGS_IMPL::ParseIntInRange(const string & value, int min, int max, int * val)
80 {
81 if (ParseInt(value, val) != 0)
82     return -1;
83
84 if (*val < min || *val > max)
85     {
86     strError = "Value \'" + value + "\' out of range.";
87     return -1;
88     }
89
90 return 0;
91 }
92 //-----------------------------------------------------------------------------
93 int SETTINGS_IMPL::ParseUnsignedInRange(const string & value, unsigned min, unsigned max, unsigned * val)
94 {
95 if (ParseUnsigned(value, val) != 0)
96     return -1;
97
98 if (*val < min || *val > max)
99     {
100     strError = "Value \'" + value + "\' out of range.";
101     return -1;
102     }
103
104 return 0;
105 }
106 //-----------------------------------------------------------------------------
107 int SETTINGS_IMPL::ParseModuleSettings(const DOTCONFDocumentNode * node, std::vector<PARAM_VALUE> * params)
108 {
109 if (!node)
110     return 0;
111
112 PARAM_VALUE pv;
113
114 pv.param = node->getName();
115
116 if (node->getValue(1))
117     {
118     strError = "Unexpected value \'" + std::string(node->getValue(1)) + "\'.";
119     printfd(__FILE__, "SETTINGS_IMPL::ParseModuleSettings() - %s\n", strError.c_str());
120     return -1;
121     }
122
123 const char * value = node->getValue(0);
124
125 if (!value)
126     {
127     strError = "Module name expected.";
128     printfd(__FILE__, "SETTINGS_IMPL::ParseModuleSettings() - %s\n", strError.c_str());
129     return -1;
130     }
131
132 const DOTCONFDocumentNode * childNode = node->getChildNode();
133 while (childNode)
134     {
135     pv.param = childNode->getName();
136     int i = 0;
137     while ((value = childNode->getValue(i)) != NULL)
138         {
139         pv.value.push_back(value);
140         ++i;
141         }
142     params->push_back(pv);
143     pv.value.clear();
144     childNode = childNode->getNextNode();
145     }
146
147 return 0;
148 }
149 //-----------------------------------------------------------------------------
150 int SETTINGS_IMPL::ReadSettings()
151 {
152 const char * requiredOptions[] = {
153     "ModulesPath",
154     "StoreModule",
155     "Login",
156     "Password",
157     "ServerName",
158     "ServerPort"
159     NULL
160     };
161 int storeModulesCount = 0;
162
163 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
164 conf.setRequiredOptionNames(requiredOptions);
165
166 if(conf.setContent(confFile.c_str()) != 0)
167     {
168     strError = "Cannot read file " + confFile + ".";
169     printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str());
170     return -1;
171     }
172
173 const DOTCONFDocumentNode * node = conf.getFirstNode();
174
175 while (node)
176     {
177     if (strcasecmp(node->getName(), "ModulesPath") == 0)
178         {
179         modulesPath = node->getValue(0);
180         }
181
182     if (strcasecmp(node->getName(), "StoreModule") == 0)
183         {
184         if (node->getValue(1))
185             {
186             strError = "Unexpected \'" + std::string(node->getValue(1)) + "\'.";
187             printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str());
188             return -1;
189             }
190
191         if (storeModulesCount)
192             {
193             strError = "Should be only one source StoreModule.";
194             printfd(__FILE__, "SETTINGS_IMPL::ReadSettings() - %s\n", strError.c_str());
195             return -1;
196             }
197         ++storeModulesCount;
198
199         storeModuleSettings.moduleName = node->getValue(0);
200         ParseModuleSettings(node, &storeModuleSettings.moduleParams);
201         }
202
203     node = node->getNextNode();
204     }
205
206 CONFIGFILE cf(confFile);
207
208 if (cf.Error())
209     {
210     strError = "Cannot read file '" + confFile + "'";
211     return -1;
212     }
213
214 cf.ReadString("Login", &login, "/?--?--?*");
215 if (login == "/?--?--?*")
216     {
217     strError = "Parameter 'Login' not found.";
218     return -1;
219     }
220
221 cf.ReadString("Password", &password, "/?--?--?*");
222 if (login == "/?--?--?*")
223     {
224     strError = "Parameter 'Password' not found.";
225     return -1;
226     }
227
228 cf.ReadString("ServerName", &serverName, "?*?*?");
229 if (serverName == "?*?*?")
230     {
231     strError = "Parameter 'ServerName' not found.";
232     return -1;
233     }
234
235 cf.ReadString("ServerPort", &temp, "5555");
236 if (ParseIntInRange(temp, 1, 65535, &port))
237     {
238     strError = "Parameter 'ServerPort' is not valid.";
239     return -1;
240     }
241
242 cf.ReadString("LocalPort", &temp, "0");
243 if (ParseIntInRange(temp, 0, 65535, &localPort))
244     {
245     strError = "Parameter 'LocalPort' is not valid.";
246     return -1;
247     }
248
249 return 0;
250 }