forked from kofany/psotnic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-load.cpp
204 lines (175 loc) · 5.14 KB
/
config-load.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************
* Copyright (C) 2003-2005 by Grzegorz Rusin *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "prots.h"
#include "global-var.h"
void CONFIG::load(const char *file, bool decrypted)
{
char arg[10][MAX_LEN], buf[MAX_LEN];
int line = 0, n;
int errors = 0;
int alts = 0;
options::event *e;
inetconn f;
config.file = file;
if(creation || decrypted)
printf("[*] Loading decrypted config from '%s'\n", file);
if(f.open(file, O_RDWR) < 1)
{
printf("[-] Cannot open config file: %s\n", strerror(errno));
exit(1);
}
if(!creation && !decrypted)
f.enableLameCrypt();
while(1)
{
n = f.readln(buf, MAX_LEN);
if(n == -1) break;
if(!n) continue;
++line;
str2words(arg[0], buf, 10, MAX_LEN);
if(arg[0][0] == '#' || !*arg[0]) continue;
e = setVariable(arg[0], rtrim(srewind(buf, 1)));
if(!e->ok)
{
printf("[-] %s:%d: %s\n", file, line, (const char *) e->reason);
++errors;
}
}
/* Basic checks */
if(!nick.getLen())
{
printf("[-] Nick is not set\n");
++errors;
}
if(errors)
{
plonk:
printf("[-] Failed to load config\n");
exit(1);
}
polish();
if(creation && !decrypted)
{
printf("[*] Crypting config file\n");
// make a backup
snprintf(buf, MAX_LEN, "%s.dec", (const char *) config.file);
unlink(buf);
if(rename(config.file, buf))
{
printf("[-] cannot create backup file '%s': %s\n", buf, strerror(errno));
exit(1);
}
e = config.save();
if(!e || !e->ok)
{
printf("[-] Cannot save config file: %s\n", (const char *) e->reason);
exit(1);
}
printf("[+] All done\n");
printf("[*] Please move %s.dec to safe place and start bot without -c option\n", file);
printf("[+] Terminating.\n");
exit(0);
}
if(imUp())
{
printf("[-] Signs on the sky tell me that i am already up\n");
printf("[*] Terminating\n");
exit(1337);
}
if(config.listenport
#ifdef HAVE_SSL
|| config.ssl_listenport
#endif
)
{
if(config.hub.getPort())
{
printf("[*] Acting as SLAVE\n");
config.bottype = BOT_SLAVE;
}
else
{
printf("[*] Acting as MAIN\n");
config.bottype = BOT_MAIN;
}
}
else
{
printf("[*] Acting as LEAF\n");
config.bottype = BOT_LEAF;
}
if(config.bottype != BOT_LEAF)
{
if(alts)
{
printf("[-] Alternating slaves can only be used for leafes\n");
goto plonk;
}
}
else
{
if(!*config.hub.getHandle() && alts)
{
printf("[-] Invalid hub's handle\n");
goto plonk;
}
}
printf("[+] Config loaded\n");
if(config.listenport)
{
printf("[*] Opening listening socket at %s:%d\n", (const char *) config.myipv4, (int) config.listenport);
if((net.listenfd = startListening(config.myipv4, config.listenport)) < 1)
{
printf("[-] Cannot open socket (%s)\n", strerror(errno));
exit(1);
}
printf("[+] Socket awaits incomming connections\n");
}
#ifdef HAVE_SSL
if(config.ssl_listenport)
{
printf("[*] Creating SSL context\n");
if(!(inet::server_ctx = SSL_CTX_new(SSLv23_method())) || !SSL_CTX_set_mode(inet::server_ctx, SSL_MODE_ENABLE_PARTIAL_WRITE))
{
printf("[-] Creation of SSL conext failed\n");
exit(1);
}
printf("[*] Loading RSA private key from `server.key'\n");
if(!SSL_CTX_use_RSAPrivateKey_file(inet::server_ctx, "server.key", SSL_FILETYPE_PEM))
{
printf("[-] Error while loading key\n");
exit(1);
}
printf("[*] Loading server certificate from `server.cert'\n");
if(!SSL_CTX_use_certificate_file(inet::server_ctx, "server.crt", SSL_FILETYPE_PEM))
{
printf("[-] Error while loading cert\n");
exit(1);
}
printf("[*] Opening listening SSL socket at %s:%d\n", (const char *) config.myipv4, (int) config.ssl_listenport);
if((net.ssl_listenfd = startListening(config.myipv4, config.ssl_listenport)) < 1)
{
printf("[-] Cannot open socket (%s)\n", strerror(errno));
exit(1);
}
printf("[+] Socket awaits incomming SSL connections\n");
}
#endif
}