summaryrefslogtreecommitdiff
path: root/src/openvpn/argv.c
blob: 2c61e6637fa8c39a37618cd3b5f836e4aeff958d (plain)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 *  OpenVPN -- An application to securely tunnel IP networks
 *             over a single TCP/UDP port, with support for SSL/TLS-based
 *             session authentication and key exchange,
 *             packet encryption, packet authentication, and
 *             packet compression.
 *
 *  Copyright (C) 2002-2021 OpenVPN Inc <sales@openvpn.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *
 *  A printf-like function (that only recognizes a subset of standard printf
 *  format operators) that prints arguments to an argv list instead
 *  of a standard string.  This is used to build up argv arrays for passing
 *  to execve.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_MSC_VER)
#include "config-msvc.h"
#endif

#include "syshead.h"

#include "argv.h"
#include "integer.h"
#include "env_set.h"
#include "options.h"

/**
 *  Resizes the list of arguments struct argv can carry.  This resize
 *  operation will only increase the size, never decrease the size.
 *
 *  @param *a      Valid pointer to a struct argv to resize
 *  @param newcap  size_t with the new size of the argument list.
 */
static void
argv_extend(struct argv *a, const size_t newcap)
{
    if (newcap > a->capacity)
    {
        char **newargv;
        size_t i;
        ALLOC_ARRAY_CLEAR_GC(newargv, char *, newcap, &a->gc);
        for (i = 0; i < a->argc; ++i)
        {
            newargv[i] = a->argv[i];
        }
        a->argv = newargv;
        a->capacity = newcap;
    }
}

/**
 *  Initialise an already allocated struct argv.
 *  It is expected that the input argument is a valid pointer.
 *
 *  @param *a  Pointer to a struct argv to initialise
 */
static void
argv_init(struct argv *a)
{
    a->capacity = 0;
    a->argc = 0;
    a->argv = NULL;
    a->gc = gc_new();
    argv_extend(a, 8);
}

/**
 *  Allocates a new struct argv and ensures it is initialised.
 *  Note that it does not return a pointer, but a struct argv directly.
 *
 *  @returns Returns an initialised and empty struct argv.
 */
struct argv
argv_new(void)
{
    struct argv ret;
    argv_init(&ret);
    return ret;
}

/**
 *  Frees all memory allocations allocated by the struct argv
 *  related functions.
 *
 *  @param *a  Valid pointer to a struct argv to release memory from
 */
void
argv_free(struct argv *a)
{
    gc_free(&a->gc);
}

/**
 *  Resets the struct argv to an initial state.  No memory buffers
 *  will be released by this call.
 *
 *  @param *a      Valid pointer to a struct argv to resize
 */
static void
argv_reset(struct argv *a)
{
    if (a->argc)
    {
        size_t i;
        for (i = 0; i < a->argc; ++i)
        {
            a->argv[i] = NULL;
        }
        a->argc = 0;
    }
}

/**
 *  Extends an existing struct argv to carry minimum 'add' number
 *  of new arguments.  This builds on argv_extend(), which ensures the
 *  new size will only be higher than the current capacity.
 *
 *  The new size is also calculated based on the result of adjust_power_of_2().
 *  This approach ensures that the list does grow bulks and only when the
 *  current limit is reached.
 *
 *  @param *a   Valid pointer to the struct argv to extend
 *  @param add  size_t with the number of elements to add.
 *
 */
static void
argv_grow(struct argv *a, const size_t add)
{
    const size_t newargc = a->argc + add + 1;
    ASSERT(newargc > a->argc);
    argv_extend(a, adjust_power_of_2(newargc));
}

/**
 *  Appends a string to to the list of arguments stored in a struct argv
 *  This will ensure the list size in struct argv has the needed capacity to
 *  store the value.
 *
 *  @param *a    struct argv where to append the new string value
 *  @param *str  Pointer to string to append.  The provided string *MUST* have
 *               been malloc()ed or NULL.
 */
static void
argv_append(struct argv *a, char *str)
{
    argv_grow(a, 1);
    a->argv[a->argc++] = str;
}

/**
 *  Clones a struct argv with all the contents to a new allocated struct argv.
 *  If 'headroom' is larger than 0, it will create a head-room in front of the
 *  values being copied from the source input.
 *
 *
 *  @param *source   Valid pointer to the source struct argv to clone.  It may
 *                   be NULL.
 *  @param headroom  Number of slots to leave empty in front of the slots
 *                   copied from the source.
 *
 *  @returns Returns a new struct argv containing a copy of the source
 *           struct argv, with the given headroom in front of the copy.
 *
 */
static struct argv
argv_clone(const struct argv *source, const size_t headroom)
{
    struct argv r;
    argv_init(&r);

    for (size_t i = 0; i < headroom; ++i)
    {
        argv_append(&r, NULL);
    }
    if (source)
    {
        for (size_t i = 0; i < source->argc; ++i)
        {
            argv_append(&r, string_alloc(source->argv[i], &r.gc));
        }
    }
    return r;
}

/**
 *  Inserts an argument string in front of all other argument slots.
 *
 *  @param  *a     Valid pointer to the struct argv to insert the argument into
 *  @param  *head  Pointer to the char * string with the argument to insert
 *
 *  @returns Returns a new struct argv with the inserted argument in front
 */
struct argv
argv_insert_head(const struct argv *a, const char *head)
{
    struct argv r;
    r = argv_clone(a, 1);
    r.argv[0] = string_alloc(head, &r.gc);
    return r;
}

/**
 *  Generate a single string with all the arguments in a struct argv
 *  concatenated.
 *
 *  @param *a    Valid pointer to the struct argv with the arguments to list
 *  @param *gc   Pointer to a struct gc_arena managed buffer
 *  @param flags Flags passed to the print_argv() function.
 *
 *  @returns Returns a string generated by print_argv() with all the arguments
 *           concatenated.  If the argument count is 0, it will return an empty
 *           string.  The return string is allocated in the gc_arena managed
 *           buffer.  If the gc_arena pointer is NULL, the returned string
 *           must be free()d explicitly to avoid memory leaks.
 */
const char *
argv_str(const struct argv *a, struct gc_arena *gc, const unsigned int flags)
{
    return print_argv((const char **)a->argv, gc, flags);
}

/**
 *  Write the arguments stored in a struct argv via the msg() command.
 *
 *  @param msglev  Integer with the message level used by msg().
 *  @param *a      Valid pointer to the struct argv with the arguments to write.
 */
void
argv_msg(const int msglev, const struct argv *a)
{
    struct gc_arena gc = gc_new();
    msg(msglev, "%s", argv_str(a, &gc, 0));
    gc_free(&gc);
}

/**
 *  Similar to argv_msg() but prefixes the messages being written with a
 *  given string.
 *
 *  @param msglev   Integer with the message level used by msg().
 *  @param *a       Valid pointer to the struct argv with the arguments to write
 *  @param *prefix  Valid char * pointer to the prefix string
 *
 */
void
argv_msg_prefix(const int msglev, const struct argv *a, const char *prefix)
{
    struct gc_arena gc = gc_new();
    msg(msglev, "%s: %s", prefix, argv_str(a, &gc, 0));
    gc_free(&gc);
}

/**
 *  Prepares argv format string for further processing
 *
 *  Individual argument must be separated by space. Ignores leading and
 *  trailing spaces.  Consecutive spaces count as one. Returns prepared
 *  format string, with space replaced by delim and adds the number of
 *  arguments to the count parameter.
 *
 *  @param *format  Pointer to a the format string to process
 *  @param delim    Char with the delimiter to use
 *  @param *count   size_t pointer used to return the number of
 *                  tokens (argument slots) found in the format string.
 *  @param *gc      Pointer to a gc_arena managed buffer.
 *
 *  @returns Returns a parsed format string (char *), together with the
 *           number of tokens parts found (via *count).  The result string
 *           is allocated within the gc_arena managed buffer.  If the
 *           gc_arena pointer is NULL, the returned string must be explicitly
 *           free()d to avoid memory leaks.
 */
static char *
argv_prep_format(const char *format, const char delim, size_t *count,
                 struct gc_arena *gc)
{
    if (format == NULL)
    {
        return NULL;
    }

    bool in_token = false;
    char *f = gc_malloc(strlen(format) + 1, true, gc);
    for (int i = 0, j = 0; i < strlen(format); i++)
    {
        if (format[i] == ' ')
        {
            in_token = false;
            continue;
        }

        if (!in_token)
        {
            (*count)++;

            /*
             * We don't add any delimiter to the output string if
             * the string is empty; the resulting format string
             * will never start with a delimiter.
             */
            if (j > 0)  /* Has anything been written to the output string? */
            {
                f[j++] = delim;
            }
        }

        f[j++] = format[i];
        in_token = true;
    }

    return f;
}

/**
 *  Create a struct argv based on a format string
 *
 *  Instead of parsing the format string ourselves place delimiters via
 *  argv_prep_format() before we let libc's printf() do the parsing.
 *  Then split the resulting string at the injected delimiters.
 *
 *  @param *argres  Valid pointer to a struct argv where the resulting parsed
 *                  arguments, based on the format string.
 *  @param *format  Char* string with a printf() compliant format string
 *  @param arglist  A va_list with the arguments to be consumed by the format
 *                  string
 *
 *  @returns Returns true if the parsing and processing was successfully.  If
 *           the resulting number of arguments does not match the expected
 *           number of arguments (based on the format string), it is
 *           considered a failure, which returns false.  This can happen if
 *           the ASCII Group Separator (GS - 0x1D) is put into the arguments
 *           list or format string.
 */
static bool
argv_printf_arglist(struct argv *argres, const char *format, va_list arglist)
{
    const char delim = 0x1D;  /* ASCII Group Separator (GS) */
    bool res = false;

    /*
     * Prepare a format string which will be used by vsnprintf() later on.
     *
     * This means all space separators in the input format string will be
     * replaced by the GS (0x1D), so we can split this up again after the
     * the vsnprintf() call into individual arguments again which will be
     * saved in the struct argv.
     *
     */
    size_t argc = argres->argc;
    char *f = argv_prep_format(format, delim, &argc, &argres->gc);
    if (f == NULL)
    {
        goto out;
    }

    /*
     * Determine minimum buffer size.
     *
     * With C99, vsnprintf(NULL, 0, ...) will return the number of bytes
     * it would have written, had the buffer been large enough.
     */
    va_list tmplist;
    va_copy(tmplist, arglist);
    int len = vsnprintf(NULL, 0, f, tmplist);
    va_end(tmplist);
    if (len < 0)
    {
        goto out;
    }

    /*
     *  Do the actual vsnprintf() operation, which expands the format
     *  string with the provided arguments.
     */
    size_t size = len + 1;
    char *buf = gc_malloc(size, false, &argres->gc);
    len = vsnprintf(buf, size, f, arglist);
    if (len < 0 || len >= size)
    {
        goto out;
    }

    /*
     * Split the string at the GS (0x1D) delimiters and put each elemen
     * into the struct argv being returned to the caller.
     */
    char *end = strchr(buf, delim);
    while (end)
    {
        *end = '\0';
        argv_append(argres, buf);
        buf = end + 1;
        end = strchr(buf, delim);
    }
    argv_append(argres, buf);

    if (argres->argc != argc)
    {
        /* Someone snuck in a GS (0x1D), fail gracefully */
        argv_reset(argres);
        goto out;
    }
    res = true;

out:
    return res;
}

/**
 *  printf() variant which populates a struct argv.  It processes the
 *  format string with the provided arguments.  For each space separator found
 *  in the format string, a new argument will be added to the resulting
 *  struct argv.
 *
 *  This will always reset and ensure the result is based on a pristine
 *  struct argv.
 *
 *  @param *argres  Valid pointer to a struct argv where the result will be put.
 *  @param *format  printf() compliant (char *) format string.
 *
 *  @returns Returns true if the parsing was successful.  See
 *           argv_printf_arglist() for more details.  The parsed result will
 *           be put into argres.
 */
bool
argv_printf(struct argv *argres, const char *format, ...)
{
    va_list arglist;
    va_start(arglist, format);

    argv_reset(argres);
    bool res = argv_printf_arglist(argres, format, arglist);
    va_end(arglist);
    return res;
}

/**
 *  printf() inspired argv concatenation.  Adds arguments to an existing
 *  struct argv and populets the argument slots based on the printf() based
 *  format string.
 *
 *  @param *argres  Valid pointer to a struct argv where the result will be put.
 *  @param *format  printf() compliant (char *) format string.
 *
 *  @returns Returns true if the parsing was successful.  See
 *           argv_printf_arglist() for more details.  The parsed result will
 *           be put into argres.
 */
bool
argv_printf_cat(struct argv *argres, const char *format, ...)
{
    va_list arglist;
    va_start(arglist, format);
    bool res = argv_printf_arglist(argres, format, arglist);
    va_end(arglist);
    return res;
}

/**
 *  Parses a command string, tokenizes it and puts each element into a separate
 *  struct argv argument slot.
 *
 *  @params *argres  Valid pointer to a struct argv where the parsed result
 *                   will be found.
 *  @params *cmdstr  Char * based string to parse
 *
 */
void
argv_parse_cmd(struct argv *argres, const char *cmdstr)
{
    argv_reset(argres);

    char *parms[MAX_PARMS + 1] = { 0 };
    int nparms = parse_line(cmdstr, parms, MAX_PARMS, "SCRIPT-ARGV", 0,
                            D_ARGV_PARSE_CMD, &argres->gc);
    if (nparms)
    {
        int i;
        for (i = 0; i < nparms; ++i)
        {
            argv_append(argres, parms[i]);
        }
    }
    else
    {
        argv_append(argres, string_alloc(cmdstr, &argres->gc));
    }
}