"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "config.cc" see the
Fossies "Dox" file reference documentation.
1 /*
2 xstress - xk0derz SMTP Stress Tester
3
4 (c) Amit Singh amit@xkoder.com
5 http://xkoder.com
6
7 This software and related files are licensed under GNU GPL version 2
8 Please visit the following webpage for more details
9 http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 */
11
12 #include <fstream>
13 #include <cstdlib>
14 #include <time.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <dirent.h>
19 #include <cstring>
20
21 #include "common.h"
22 #include "sendmail.h"
23 #include "config.h"
24 #include "logger.h"
25
26 using namespace std;
27
28 const char * ConfigVarsStr[]=
29 {
30 " ",
31 "SERVER",
32 "PORT",
33 "THREADS",
34 "MAILS_PER_THREAD",
35 "LOG_FILE",
36 "TIMEOUT",
37 "REPORT_AFTER",
38 "LOG_TIMEOUT",
39 "MAX_RECIPIENTS",
40 "USERNAME",
41 "PASSWORD",
42 "AUTH_TYPE",
43 "DEBUG",
44 " "
45 };
46
47 const char *SectionsStr[]=
48 {
49 " ",
50 "[CONFIG]",
51 "[TO]",
52 "[FROM]",
53 "[SUBJECT]",
54 "[BODY]",
55 "[ATTACHMENT]",
56 " "
57 };
58
59 int Config::okay()
60 {
61 return iOkay;
62 }
63
64 string Config::getTo()
65 {
66 unsigned int size = toList.size(), ii;
67 srand(time(0));
68 ii = (rand()%size);
69 if(size==0) return string("");
70 return toList[ii];
71 }
72
73 string Config::getFrom()
74 {
75 unsigned int size = fromList.size(), ii;
76 srand(time(0));
77 ii = (rand()%size);
78 if(size==0) return string("");
79 return fromList[ii];
80 }
81
82 string Config::getSubject()
83 {
84 unsigned int size = subjectList.size(), ii;
85 srand(time(0));
86 ii = (rand()%size);
87 if(size==0) return string("");
88 return subjectList[ii];
89 }
90
91 string Config::getBody()
92 {
93 unsigned int size = bodyList.size(), ii;
94 srand(time(0));
95 ii = (rand()%size);
96 if(size==0) return string("");
97 return bodyList[ii];
98 }
99
100 string Config::getAttachment()
101 {
102 unsigned int size = attachList.size(), ii;
103 srand(time(0));
104 if(size==0) return string("");
105 ii = (rand()%size);
106 return attachList[ii];
107 }
108
109
110 Config::Config(string sConfigFile)
111 {
112 char cBuffer[1024];
113 int ii;
114 string sLine;
115 iOkay = 1;
116 ifstream filp(sConfigFile.c_str());
117 unsigned int currSection = 0;
118 unsigned int configVar = 0;
119 unsigned int configVarsFound = 0;
120 string LValue;
121 string RValue;
122
123 //
124 sServerIP = "";
125 uiServerPort = 0;
126 uiThreads = 0;
127 uiMailsPerThread = 0;
128 uiReportAfter = 0;
129 uiLogTimeout = 1;
130 uiMaxRecipients = 1;
131 sPassword = "";
132 sUsername = "";
133 sAuthType = "";
134
135 if(!filp.fail())
136 {
137 while(!filp.eof())
138 {
139 filp.getline(cBuffer,1023);
140 sLine = cBuffer;
141
142 // Comments
143 if(sLine[0] == '#') continue;
144 if(sLine.empty()) continue;
145
146 // Check if it's a section
147 if(sLine[0] =='[')
148 {
149 for(ii=1;ii<S_MAX;ii++)
150 {
151 if(sLine == SectionsStr[ii])
152 {
153 currSection = ii;
154 break;
155 }
156 }
157 continue;
158 }
159
160 switch(currSection)
161 {
162 case S_CONFIG:
163 ii = sLine.find("=");
164 LValue = sLine.substr(0,ii);
165 RValue = sLine.substr(ii+1,sLine.length());
166
167 configVar = C_NO_VAR;
168 for(ii=1;ii<C_MAX;ii++)
169 {
170 if(LValue == ConfigVarsStr[ii])
171 {
172 configVar = ii;
173 break;
174 }
175 }
176
177 switch(configVar)
178 {
179 case C_DEBUG:
180 {
181 char cBuf[255];
182 if(RValue.length() < 200)
183 {
184 strcpy(cBuf, RValue.c_str());
185 int ii = 0;
186 while(cBuf[ii])
187 {
188 cBuf[ii] = toupper(cBuf[ii]);
189 ii++;
190 }
191 RValue = cBuf;
192 }
193 if(RValue == "TRUE") bDebug = true;
194 }
195 break;
196 case C_AUTH_TYPE:
197 {
198 char cBuf[255];
199 if(RValue.length() < 200)
200 {
201 strcpy(cBuf, RValue.c_str());
202 int ii = 0;
203 while(cBuf[ii])
204 {
205 cBuf[ii] = toupper(cBuf[ii]);
206 ii++;
207 }
208 RValue = cBuf;
209 }
210 sAuthType = RValue;
211 }
212 break;
213 case C_USERNAME:
214 sUsername = RValue;
215 break;
216 case C_PASSWORD:
217 sPassword = RValue;
218 break;
219 case C_SERVER:
220 sServerIP = RValue;
221 break;
222 case C_PORT:
223 uiServerPort = atoi(RValue.c_str());
224 break;
225 case C_THREADS:
226 uiThreads = atoi(RValue.c_str());
227 break;
228 case C_MAILS_PER_THREAD:
229 uiMailsPerThread = atoi(RValue.c_str());
230 break;
231 case C_TIMEOUT:
232 uiTimeout = atoi(RValue.c_str());
233 break;
234 case C_LOG_FILE:
235 sLogFile = RValue;
236 break;
237 case C_REPORT_AFTER:
238 uiReportAfter = atoi(RValue.c_str());
239 break;
240 case C_LOG_TIMEOUT:
241 if((RValue == "false") || (RValue=="0"))
242 uiLogTimeout = 0;
243 else if((RValue == "true") || (RValue=="1"))
244 uiLogTimeout = 1;
245 else
246 {
247 logger.log("Invalid value for LOG_TIMEOUT, use 'false' or 'true'.");
248 uiLogTimeout = 1;
249 }
250 break;
251 case C_MAX_RECIPIENTS:
252 uiMaxRecipients = atoi(RValue.c_str());
253 break;
254 }
255
256 break;
257 case S_TO:
258 //cout << "Add to TO list " << sLine << endl;
259 toList.push_back(sLine);
260 break;
261 case S_FROM:
262 fromList.push_back(sLine);
263 break;
264 case S_SUBJECT:
265 subjectList.push_back(sLine);
266 break;
267 case S_BODY:
268 addToList(sLine, bodyList);
269 break;
270 case S_ATTACH:
271 addToList(sLine, attachList);
272 //attachList.push_back(sLine);
273 break;
274 }
275
276 }
277
278 if(sServerIP.empty() || uiServerPort == 0 || uiThreads == 0)
279 {
280 iOkay = 0;
281 }
282
283 }
284 else
285 {
286 iOkay = false;
287 }
288 }
289
290 void Config::addToList(string sFilename, vector <string> &pList)
291 {
292 struct stat filp_stat;
293 string sFiletype = "";
294
295 if(sFilename.find("!")!=string::npos)
296 {
297 string sTemp = sFilename;
298 sFilename = sTemp.substr(0,sTemp.find("!"));
299 sFiletype = sTemp.substr(sTemp.find("!")+1,sTemp.length());
300 }
301
302 if(stat(sFilename.c_str(),&filp_stat)==0)
303 {
304 if(S_ISDIR(filp_stat.st_mode))
305 {
306 struct dirent *d_ent;
307 DIR *dirp = opendir(sFilename.c_str());
308 if(dirp)
309 {
310 string sDir = sFilename;
311 while(d_ent = readdir(dirp))
312 {
313 sFilename = sDir + "/" +d_ent->d_name;
314 if(stat(sFilename.c_str(),&filp_stat)==0)
315 {
316 if(S_ISREG(filp_stat.st_mode))
317 {
318 if(!sFiletype.empty())
319 {
320 sFilename.append("!");
321 sFilename.append(sFiletype);
322 }
323 pList.push_back(sFilename);
324 }
325 }
326 }
327 }
328 else
329 {
330 char cBuf[255];
331 snprintf(cBuf,254,"Unable to open Directory: %s", sFilename.c_str());
332 cBuf[254]=0x00;
333 logger.log(cBuf);
334 }
335 }
336 else if(S_ISREG(filp_stat.st_mode))
337 {
338 if(!sFiletype.empty())
339 {
340 sFilename.append("!");
341 sFilename.append(sFiletype);
342 }
343 pList.push_back(sFilename);
344 }
345 else
346 {
347 char cBuf[255];
348 snprintf(cBuf,254,"Not a regular File or Directory: %s", sFilename.c_str());
349 cBuf[254]=0x00;
350 logger.log(cBuf);
351 }
352 }
353 else
354 {
355 char cBuf[255];
356 snprintf(cBuf,254,"Unable to get stats for file: %s", sFilename.c_str());
357 cBuf[254]=0x00;
358 logger.log(cBuf);
359 }
360 }