forked from ocochard/FreeVRRPd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrrp_conf.c
353 lines (336 loc) · 9.6 KB
/
vrrp_conf.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) 2001,2002 Sebastien Petit <spe@bsdfr.org>
*
* Redistribution and use in source forms, with and without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. Obviously, it
* would be nice if you gave credit where credit is due but requiring it
* would be too onerous.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Sebastien Petit.
* 4. Neither the name of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: vrrp_conf.c,v 1.10 2004/04/02 11:16:46 rival Exp $
*/
#include <errno.h>
#include "vrrp_conf.h"
int
vrrp_conf_ident_option_arg(char *chaine, char *option, char *arg)
{
int i = 0;
char *ptr;
while (isalpha(chaine[i]) && chaine[i] != 0) {
i++;
if (i > 1022) {
syslog(LOG_ERR, "a bad line was found in your configuration file (line > 1024 char), exiting...");
exit(-1);
}
}
if (!i) {
syslog(LOG_ERR, "a bad line was found in your configuration file: %s\n", chaine);
exit(-1);
}
strncpy(option, chaine, i);
option[i] = '\0';
while (chaine[i] != '=' && chaine[i] != 0) {
i++;
if (i > 1021) {
syslog(LOG_ERR, "a bad line was found in your configuration file (line > 1024 char), exiting...");
exit(-1);
}
}
i++;
while (chaine[i] == ' ' && chaine[i] != 0) {
i++;
if (i > 1021) {
syslog(LOG_ERR, "a bad line was found in your configuration file (line > 1024 char), exiting...");
exit(-1);
}
}
ptr = &chaine[i];
strncpy(arg, ptr, strlen(chaine) - i);
if (arg[strlen(chaine) - i - 1] == '\n')
i++;
arg[strlen(chaine) - i] = '\0';
return 0;
}
char **
vrrp_conf_split_args(char *args, char delimiter)
{
char **tabargs;
int i, j, nbargs = 0;
char *ptr;
tabargs = (char **)malloc(sizeof(char *) * VRRP_CONF_MAX_ARGS);
bzero(tabargs, sizeof(char **) * VRRP_CONF_MAX_ARGS);
if (!tabargs) {
syslog(LOG_ERR, "cannot malloc memory : %s", strerror(errno));
exit(EXIT_FAILURE);
}
i = 0;
while (i < strlen(args)) {
ptr = &args[i];
j = 0;
while (ptr[j] != delimiter && ptr[j] != 0) {
i++;
j++;
}
tabargs[nbargs] = (char *)calloc(j + 1, 1);
strncpy(tabargs[nbargs], ptr, j);
i++;
while (!isalnum(args[i]) && args[i] != '.' && args[i] != '/' && args[i])
i++;
nbargs++;
if (nbargs >= VRRP_CONF_MAX_ARGS) {
syslog(LOG_ERR, "too many arguments in the configuration file");
exit(EXIT_FAILURE);
}
}
return tabargs;
}
void
vrrp_conf_freeargs(char **temp)
{
int i = 0;
while (temp[i]) {
free(temp[i]);
temp[i] = NULL;
i++;
}
free(temp);
return;
}
FILE *
vrrp_conf_open_file(char *name)
{
FILE *stream;
struct stat st;
stream = fopen(name, "r");
if (! stream) {
syslog(LOG_ERR, "cannot open configuration file %s: %s", name, strerror(errno));
return NULL;
}
if (lstat(name, &st) == -1) {
syslog(LOG_ERR, "cannot call lstat(): %s", strerror(errno));
fclose(stream);
return NULL;
}
if ((st.st_mode & S_IFMT) != S_IFREG) {
syslog(LOG_ERR, "%s is not a regular file", name);
fclose(stream);
return NULL;
}
return stream;
}
char
vrrp_conf_close_file(FILE * stream)
{
if (fclose(stream) == EOF) {
syslog(LOG_ERR, "can't close the file stream FILE *stream: %s\n", strerror(errno));
return -1;
}
return 0;
}
char
vrrp_conf_lecture_fichier(struct vrrp_vr * vr, FILE * stream)
{
char ligne[1024] = "#";
char **temp;
char **temp2;
char **temp3;
char option[1024], arg[1024];
int i, j;
fpos_t pos;
int optok;
fgetpos(stream, &pos);
if (!pos) {
while (ligne[0] == '#' || ligne[0] == 0 || ligne[0] == '\n')
fgets(ligne, 1024, stream);
if (strncmp(ligne, "[VRID]", 6)) {
syslog(LOG_ERR, "configuration file error ! cannot see [VRID] section");
return -1;
}
fgets(ligne, 1024, stream);
}
while (!feof(stream) && strncmp(ligne, "[VRID]", 6)) {
if (ligne[0] != 0 && ligne[0] != '\n' && ligne[0] != '#') {
if (feof(stream))
break;
vrrp_conf_ident_option_arg(ligne, option, arg);
optok = 0;
if (!strcmp(option, "addr")) {
temp = vrrp_conf_split_args(arg, ',');
i = 0;
while (temp[i])
i++;
vr->vr_ip = (struct vrrp_vip *) calloc(i, sizeof(struct vrrp_vip) + 1);
vr->vr_netmask = (u_int *) calloc(i, sizeof(u_int) + 1);
i = 0;
while (temp[i] && (i < VRRP_CONF_MAX_ARGS)) {
temp2 = vrrp_conf_split_args(temp[i], ':');
if (temp2[1]) {
vr->vr_ip[i].if_name = (char *)calloc(IFNAMSIZ + 1, 1);
strncpy(vr->vr_ip[i].if_name, temp2[0], IFNAMSIZ);
temp3 = vrrp_conf_split_args(temp2[1], '/');
}
else
temp3 = vrrp_conf_split_args(temp2[0], '/');
j = 0;
while (temp3[j])
j++;
if (j != 2) {
syslog(LOG_ERR, "bad value in the configuration file for addr option: %s", arg);
exit(-1);
}
vr->vr_ip[i].addr.s_addr = inet_addr(temp3[0]);
vr->vr_netmask[i] = atoi(temp3[1]);
i++;
vrrp_conf_freeargs(temp3);
vrrp_conf_freeargs(temp2);
}
vr->cnt_ip = i;
vrrp_conf_freeargs(temp);
optok = 1;
}
if (!strcmp(option, "interface")) {
temp = vrrp_conf_split_args(arg, ',');
if (!(vr->vr_if = vrrp_misc_search_if_entry(temp[0]))) {
vr->vr_if = (struct vrrp_if *)calloc(1, sizeof(struct vrrp_if));
strncpy(vr->vr_if->if_name, temp[0], sizeof(vr->vr_if->if_name));
}
vrrp_conf_freeargs(temp);
optok = 1;
}
if (!strcmp(option, "monitoredcircuits")) {
if (! strcmp(arg, "yes"))
vr->useMonitoredCircuits = 1;
else {
if (! strcmp(arg, "no"))
vr->useMonitoredCircuits = 0;
else {
syslog(LOG_ERR, "bad value for useMonitoredCircuits: %s", arg);
exit(-1);
}
}
optok = 1;
}
if (!strcmp(option, "serverid")) {
vr->vr_id = atoi(arg);
optok = 1;
}
if (!strcmp(option, "carriertimeout")) {
if (vr->vr_if)
vr->vr_if->carrier_timeout = atoi(arg);
else
syslog(LOG_ERR, "'carriertimeout' must be specified after 'interface'");
optok = 1;
}
if (!strcmp(option, "spanningtreelatency")) {
vr->spanningTreeLatency = atoi(arg);
optok = 1;
}
if (!strcmp(option, "priority")) {
temp = vrrp_conf_split_args(arg, ',');
vr->priority = atoi(temp[0]);
vrrp_conf_freeargs(temp);
optok = 1;
}
if (!strcmp(option, "password")) {
temp = vrrp_conf_split_args(arg, ',');
vr->password = (char *)calloc(8, 1);
strncpy(vr->password, temp[0], 8);
vrrp_conf_freeargs(temp);
vr->auth_type = 1;
optok = 1;
}
if (!strcmp(option, "masterscript")) {
vr->master_script = (char *)calloc(strlen(arg)+1, 1);
strncpy(vr->master_script, arg, strlen(arg));
optok = 1;
}
if (!strcmp(option, "backupscript")) {
vr->backup_script = (char *)calloc(strlen(arg)+1, 1);
strncpy(vr->backup_script, arg, strlen(arg));
optok = 1;
}
if (! strcmp(option, "vridsdep")) {
temp = vrrp_conf_split_args(arg, ',');
i = 0;
while (temp[i]) i++;
vr->vridsdeps = (int *)calloc(i+1, sizeof(int));
i = 0;
while (temp[i]) {
vr->vridsdeps[i] = atoi(temp[i]);
i++;
}
vr->vridsdeps[i] = -1;
vrrp_conf_freeargs(temp);
optok = 1;
}
if (!strcmp(option, "useIKE")) {
if (! strcmp(arg, "yes"))
vr->useIKE = 1;
else {
if (! strcmp(arg, "no"))
vr->useIKE = 0;
else {
syslog(LOG_ERR, "bad value for useIKE: %s", arg);
exit(-1);
}
}
optok = 1;
}
if (!strcmp(option, "AHencryption")) {
if (! strcmp(arg, "yes")) {
vr->AHencryption = 1;
vr->auth_type = 2;
} else {
if (! strcmp(arg, "no"))
vr->AHencryption = 0;
else {
syslog(LOG_ERR, "bad value for AHencryption: %s", arg);
exit(-1);
}
}
optok = 1;
}
if (!strcmp(option, "presharedkey")) {
if (vr->password) {
syslog(LOG_ERR, "you cannot use presharedkey option with password option");
exit(-1);
}
vr->password = (char *)calloc(strlen(arg)+1, 1);
strncpy(vr->password, arg, strlen(arg));
optok = 1;
}
if (!strcmp(option, "MCClearErrorsCount")) {
vr->monitoredCircuitsClearErrorsCount = atoi(arg);
optok = 1;
}
if (! optok)
syslog(LOG_ERR, "option '%s' unknown on configuration file, ignoring it", option);
}
fgets(ligne, 1024, stream);
}
if (feof(stream))
return 1;
return 0;
}