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
|
/*
* 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-2018 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 "options.h"
static void
argv_init(struct argv *a)
{
a->capacity = 0;
a->argc = 0;
a->argv = NULL;
}
struct argv
argv_new(void)
{
struct argv ret;
argv_init(&ret);
return ret;
}
void
argv_reset(struct argv *a)
{
size_t i;
for (i = 0; i < a->argc; ++i)
{
free(a->argv[i]);
}
free(a->argv);
argv_init(a);
}
static void
argv_extend(struct argv *a, const size_t newcap)
{
if (newcap > a->capacity)
{
char **newargv;
size_t i;
ALLOC_ARRAY_CLEAR(newargv, char *, newcap);
for (i = 0; i < a->argc; ++i)
{
newargv[i] = a->argv[i];
}
free(a->argv);
a->argv = newargv;
a->capacity = newcap;
}
}
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));
}
static void
argv_append(struct argv *a, char *str) /* str must have been malloced or be NULL */
{
argv_grow(a, 1);
a->argv[a->argc++] = str;
}
static struct argv
argv_clone(const struct argv *a, const size_t headroom)
{
struct argv r;
size_t i;
argv_init(&r);
for (i = 0; i < headroom; ++i)
{
argv_append(&r, NULL);
}
if (a)
{
for (i = 0; i < a->argc; ++i)
{
argv_append(&r, string_alloc(a->argv[i], NULL));
}
}
return r;
}
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, NULL);
return r;
}
static char *
argv_term(const char **f)
{
const char *p = *f;
const char *term = NULL;
size_t termlen = 0;
if (*p == '\0')
{
return NULL;
}
while (true)
{
const int c = *p;
if (c == '\0')
{
break;
}
if (term)
{
if (!isspace(c))
{
++termlen;
}
else
{
break;
}
}
else
{
if (!isspace(c))
{
term = p;
termlen = 1;
}
}
++p;
}
*f = p;
if (term)
{
char *ret;
ASSERT(termlen > 0);
ret = malloc(termlen + 1);
check_malloc_return(ret);
memcpy(ret, term, termlen);
ret[termlen] = '\0';
return ret;
}
else
{
return NULL;
}
}
const char *
argv_str(const struct argv *a, struct gc_arena *gc, const unsigned int flags)
{
if (a->argv)
{
return print_argv((const char **)a->argv, gc, flags);
}
else
{
return "";
}
}
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);
}
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);
}
static void
argv_printf_arglist(struct argv *a, const char *format, va_list arglist)
{
char *term;
const char *f = format;
argv_extend(a, 1); /* ensure trailing NULL */
while ((term = argv_term(&f)) != NULL)
{
if (term[0] == '%')
{
if (!strcmp(term, "%s"))
{
char *s = va_arg(arglist, char *);
if (!s)
{
s = "";
}
argv_append(a, string_alloc(s, NULL));
}
else if (!strcmp(term, "%d"))
{
char numstr[64];
openvpn_snprintf(numstr, sizeof(numstr), "%d", va_arg(arglist, int));
argv_append(a, string_alloc(numstr, NULL));
}
else if (!strcmp(term, "%u"))
{
char numstr[64];
openvpn_snprintf(numstr, sizeof(numstr), "%u", va_arg(arglist, unsigned int));
argv_append(a, string_alloc(numstr, NULL));
}
else if (!strcmp(term, "%s/%d"))
{
char numstr[64];
char *s = va_arg(arglist, char *);
if (!s)
{
s = "";
}
openvpn_snprintf(numstr, sizeof(numstr), "%d", va_arg(arglist, int));
{
const size_t len = strlen(s) + strlen(numstr) + 2;
char *combined = (char *) malloc(len);
check_malloc_return(combined);
strcpy(combined, s);
strcat(combined, "/");
strcat(combined, numstr);
argv_append(a, combined);
}
}
else if (!strcmp(term, "%s%sc"))
{
char *s1 = va_arg(arglist, char *);
char *s2 = va_arg(arglist, char *);
char *combined;
if (!s1)
{
s1 = "";
}
if (!s2)
{
s2 = "";
}
combined = (char *) malloc(strlen(s1) + strlen(s2) + 1);
check_malloc_return(combined);
strcpy(combined, s1);
strcat(combined, s2);
argv_append(a, combined);
}
else
{
ASSERT(0);
}
free(term);
}
else
{
argv_append(a, term);
}
}
}
void
argv_printf(struct argv *a, const char *format, ...)
{
va_list arglist;
argv_reset(a);
va_start(arglist, format);
argv_printf_arglist(a, format, arglist);
va_end(arglist);
}
void
argv_printf_cat(struct argv *a, const char *format, ...)
{
va_list arglist;
va_start(arglist, format);
argv_printf_arglist(a, format, arglist);
va_end(arglist);
}
void
argv_parse_cmd(struct argv *a, const char *s)
{
int nparms;
char *parms[MAX_PARMS + 1];
struct gc_arena gc = gc_new();
argv_reset(a);
argv_extend(a, 1); /* ensure trailing NULL */
nparms = parse_line(s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &gc);
if (nparms)
{
int i;
for (i = 0; i < nparms; ++i)
{
argv_append(a, string_alloc(parms[i], NULL));
}
}
else
{
argv_append(a, string_alloc(s, NULL));
}
gc_free(&gc);
}
|