File: cmd_cycle.c

package info (click to toggle)
fwknop 2.6.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,420 kB
  • sloc: ansic: 30,106; perl: 20,346; sh: 5,560; makefile: 955; xml: 937; python: 797; java: 444; objc: 292; erlang: 128
file content (396 lines) | stat: -rw-r--r-- 11,961 bytes parent folder | download | duplicates (5)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
 * \file server/cmd_cycle.c
 *
 * \brief Fwknop routines for managing command cycles as defined via
 *          access.conf stanzas (CMD_CYCLE_OPEN and CMD_CYCLE_CLOSE).
 */

/*  Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
 *  Copyright (C) 2009-2015 fwknop developers and contributors. For a full
 *  list of contributors, see the file 'CREDITS'.
 *
 *  License (GNU General Public License):
 *
 *  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 "fwknopd_common.h"
#include "log_msg.h"
#include "extcmd.h"
#include "cmd_cycle.h"
#include "access.h"

static char cmd_buf[CMD_CYCLE_BUFSIZE];
static char err_buf[CMD_CYCLE_BUFSIZE];

static void
zero_cmd_buffers(void)
{
    memset(cmd_buf, 0x0, CMD_CYCLE_BUFSIZE);
    memset(err_buf, 0x0, CMD_CYCLE_BUFSIZE);
    return;
}

static int pid_status = 0;

static int
is_var(const char * const var, const char * const cmd_str)
{
    int i;
    for(i=0; i < strlen(var); i++)
    {
        if(cmd_str[i] != var[i])
            return 0;
    }
    return 1;
}

static int
build_cmd(spa_data_t *spadat, const char * const cmd_cycle_str, int timer)
{
    char             port_str[MAX_PORT_STR_LEN+1]   = {0};
    char             proto_str[MAX_PROTO_STR_LEN+1] = {0};
    char             timestamp_str[20] = {0};
    char             client_timeout_str[10] = {0};
    acc_port_list_t *port_list = NULL;
    int              i=0, buf_idx=0;

#if HAVE_LIBFIU
    fiu_return_on("cmd_cycle_build_err", 0);
#endif

    if(expand_acc_port_list(&port_list, spadat->spa_message_remain) != 1)
    {
        free_acc_port_list(port_list);
        return 0;
    }

    /* We only look at the first port/proto combination for command
     * open/close cycles even if the SPA message had multiple ports
     * and protocols set.
    */
    snprintf(port_str, MAX_PORT_STR_LEN+1, "%d", port_list->port);
    snprintf(proto_str, MAX_PROTO_STR_LEN+1, "%d", port_list->proto);

    zero_cmd_buffers();

    /* Look for the following variables for substitution:
     * IP, SRC, PKT_SRC, DST, PORT, and PROTO
    */
    for(i=0; i < strnlen(cmd_cycle_str, CMD_CYCLE_BUFSIZE); i++)
    {
        if(cmd_cycle_str[i] == '$')
        {
            /* Found the start of a variable, now validate it and
             * swap in the IP/port/proto.
            */
            if(is_var("IP", (cmd_cycle_str+i+1)))
            {
                strlcat(cmd_buf, spadat->use_src_ip,
                        CMD_CYCLE_BUFSIZE);
                i += strlen("IP");
                buf_idx += strlen(spadat->use_src_ip);
            }
            /* SRC is a synonym for IP
            */
            else if(is_var("SRC", (cmd_cycle_str+i+1)))
            {
                strlcat(cmd_buf, spadat->use_src_ip,
                        CMD_CYCLE_BUFSIZE);
                i += strlen("SRC");
                buf_idx += strlen(spadat->use_src_ip);
            }
            /* Special case for the SPA packet source IP in
             * the IP header (i.e. not from the decrypted SPA
             * payload) if the user really wants this.
            */
            else if(is_var("PKT_SRC", (cmd_cycle_str+i+1)))
            {
                strlcat(cmd_buf, spadat->pkt_source_ip,
                        CMD_CYCLE_BUFSIZE);
                i += strlen("PKT_SRC");
                buf_idx += strlen(spadat->pkt_source_ip);
            }
            else if(is_var("DST", (cmd_cycle_str+i+1)))
            {
                strlcat(cmd_buf, spadat->pkt_destination_ip,
                        CMD_CYCLE_BUFSIZE);
                i += strlen("DST");
                buf_idx += strlen(spadat->pkt_destination_ip);
            }
            else if (is_var("PORT", (cmd_cycle_str+i+1)))
            {
                strlcat(cmd_buf, port_str, CMD_CYCLE_BUFSIZE);
                i += strlen("PORT");
                buf_idx += strlen(port_str);
            }
            else if (is_var("PROTO", (cmd_cycle_str+i+1)))
            {
                strlcat(cmd_buf, proto_str, CMD_CYCLE_BUFSIZE);
                i += strlen("PROTO");
                buf_idx += strlen(proto_str);
            }
            else if (is_var("TIMEOUT", (cmd_cycle_str+i+1)))
            {
                snprintf(timestamp_str, sizeof(timestamp_str), "%lli",
                        (long long)spadat->timestamp +
                        (spadat->client_timeout == 0 ? timer :
                        spadat->client_timeout));
                strlcat(cmd_buf, timestamp_str, CMD_CYCLE_BUFSIZE);
                i += strlen("TIMEOUT");
                buf_idx += strlen(timestamp_str);
            }
            else if (is_var("CLIENT_TIMEOUT", (cmd_cycle_str+i+1)))
            {
                snprintf(client_timeout_str, sizeof(client_timeout_str), "%u",
                         spadat->client_timeout == 0 ? timer :
                         spadat->client_timeout);
                strlcat(cmd_buf, client_timeout_str, CMD_CYCLE_BUFSIZE);
                i += strlen("CLIENT_TIMEOUT");
                buf_idx += strlen(client_timeout_str);
            }
            continue;
        }
        if(cmd_cycle_str[i] != '\0')
            cmd_buf[buf_idx++] = cmd_cycle_str[i];
        if(buf_idx == CMD_CYCLE_BUFSIZE)
        {
            free_acc_port_list(port_list);
            return 0;
        }
    }

    free_acc_port_list(port_list);
    return 1;
}

static int
cmd_open(fko_srv_options_t *opts, acc_stanza_t *acc,
        spa_data_t *spadat, const int stanza_num)
{
    /* CMD_CYCLE_OPEN: Build the open command by taking care of variable
     * substitutions if necessary.
    */
    if(build_cmd(spadat, acc->cmd_cycle_open, acc->cmd_cycle_timer))
    {
        log_msg(LOG_INFO, "[%s] (stanza #%d) Running CMD_CYCLE_OPEN command: %s",
                spadat->pkt_source_ip, stanza_num, cmd_buf);

        /* Run the open command
        */
        run_extcmd(cmd_buf, err_buf, CMD_CYCLE_BUFSIZE,
                WANT_STDERR, NO_TIMEOUT, &pid_status, opts);
    }
    else
    {
        log_msg(LOG_ERR,
            "[%s] (stanza #%d) Could not build CMD_CYCLE_OPEN command.",
            spadat->pkt_source_ip, stanza_num
        );
        return 0;
    }
    return 1;
}

static int
add_cmd_close(fko_srv_options_t *opts, acc_stanza_t *acc,
        spa_data_t *spadat, const int stanza_num)
{
    cmd_cycle_list_t   *last_clist=NULL, *new_clist=NULL, *tmp_clist=NULL;
    time_t              now;
    int                 cmd_close_len = 0;

    /* CMD_CYCLE_CLOSE: Build the close command, but don't execute it until
     * the expiration timer has passed.
    */
    if(build_cmd(spadat, acc->cmd_cycle_close, acc->cmd_cycle_timer))
    {
        /* Now the corresponding close command is now in cmd_buf
         * for later execution when the timer expires.
        */
        cmd_close_len = strnlen(cmd_buf, CMD_CYCLE_BUFSIZE-1)+1;
        log_msg(LOG_INFO,
                "[%s] (stanza #%d) Running CMD_CYCLE_CLOSE command in %d seconds: %s",
                spadat->pkt_source_ip, stanza_num,
                (spadat->client_timeout == 0 ? acc->cmd_cycle_timer :
                spadat->client_timeout), cmd_buf);
    }
    else
    {
        log_msg(LOG_ERR,
            "[%s] (stanza #%d) Could not build CMD_CYCLE_CLOSE command.",
            spadat->pkt_source_ip, stanza_num
        );
        return 0;
    }

    /* Add the corresponding close command - to be executed after the
     * designated timer has expired.
    */
    if((new_clist = calloc(1, sizeof(cmd_cycle_list_t))) == NULL)
    {
        log_msg(LOG_ERR,
            "[*] Fatal memory allocation error creating string list entry"
        );
        clean_exit(opts, FW_CLEANUP, EXIT_FAILURE);
    }

    if(opts->cmd_cycle_list == NULL)
    {
        opts->cmd_cycle_list = new_clist;
    }
    else
    {
        tmp_clist = opts->cmd_cycle_list;

        do {
            last_clist = tmp_clist;
        } while((tmp_clist = tmp_clist->next));

        last_clist->next = new_clist;
    }

    /* Set the source IP
    */
    strlcpy(new_clist->src_ip, spadat->use_src_ip,
            sizeof(new_clist->src_ip));

    /* Set the expiration timer
    */
    time(&now);
    new_clist->expire = now + (spadat->client_timeout == 0 ?
            acc->cmd_cycle_timer : spadat->client_timeout);

    /* Set the close command
    */
    if((new_clist->close_cmd = calloc(1, cmd_close_len)) == NULL)
    {
        log_msg(LOG_ERR,
            "[*] Fatal memory allocation error creating command close string"
        );
        clean_exit(opts, FW_CLEANUP, EXIT_FAILURE);
    }
    strlcpy(new_clist->close_cmd, cmd_buf, cmd_close_len);

    /* Set the access.conf stanza number
    */
    new_clist->stanza_num = stanza_num;

    return 1;
}

/* This is the main driver for open/close command cycles
*/
int
cmd_cycle_open(fko_srv_options_t *opts, acc_stanza_t *acc,
        spa_data_t *spadat, const int stanza_num, int *res)
{
    if(! cmd_open(opts, acc, spadat, stanza_num))
        return 0;

    if(acc->cmd_cycle_do_close)
        if(! add_cmd_close(opts, acc, spadat, stanza_num))
            return 0;

     return 1;
}

static void
free_cycle_list_node(cmd_cycle_list_t *list_node)
{
    if(list_node != NULL)
    {
        if(list_node->close_cmd != NULL)
            free(list_node->close_cmd);
        free(list_node);
    }
    return;
}

/* Run all close commands based on the expiration timer
*/
void
cmd_cycle_close(fko_srv_options_t *opts)
{
    cmd_cycle_list_t   *curr=NULL, *prev=NULL;
    int                 do_delete=1;
    time_t              now;

    time(&now);

    if(opts->cmd_cycle_list == NULL)
    {
        return; /* No active command cycles */
    }
    else
    {
        while(do_delete)
        {
            do_delete = 0;

            /* Keep going through the command list for as long as
             * there are commands to be executed (and expired).
            */
            for(curr = opts->cmd_cycle_list;
                    curr != NULL;
                    prev = curr, curr=curr->next)
            {
                if(curr->expire <= now)
                {
                    log_msg(LOG_INFO,
                            "[%s] (stanza #%d) Timer expired, running CMD_CYCLE_CLOSE command: %s",
                            curr->src_ip, curr->stanza_num,
                            curr->close_cmd);

                    zero_cmd_buffers();

                    /* Run the close command
                    */
                    run_extcmd(curr->close_cmd, err_buf, CMD_CYCLE_BUFSIZE,
                            WANT_STDERR, NO_TIMEOUT, &pid_status, opts);

                    if(prev == NULL)
                        opts->cmd_cycle_list = curr->next;
                    else
                        prev->next = curr->next;

                    free_cycle_list_node(curr);
                    do_delete = 1;
                    break;
                }
            }
        }
    }

    return;
}

void
free_cmd_cycle_list(fko_srv_options_t *opts)
{
    cmd_cycle_list_t   *tmp_clist=NULL, *clist=NULL;

    clist = opts->cmd_cycle_list;

    while(clist != NULL)
    {
        tmp_clist = clist->next;
        free_cycle_list_node(clist);
        clist = tmp_clist;
    }
    return;
}