summaryrefslogtreecommitdiffstats
path: root/plugins/audit_json/audit_json.c
blob: c19c29be24dfb3ab4b4c3a5c68d3beeed00f09b1 (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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
 * SPDX-License-Identifier: ISC
 *
 * Copyright (c) 2020-2021 Todd C. Miller <Todd.Miller@sudo.ws>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
 */

#include <config.h>

#include <sys/stat.h>
#include <sys/wait.h>

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include <compat/stdbool.h>
#endif /* HAVE_STDBOOL_H */
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <time.h>

#include <pathnames.h>
#include <sudo_compat.h>
#include <sudo_conf.h>
#include <sudo_debug.h>
#include <sudo_dso.h>
#include <sudo_fatal.h>
#include <sudo_gettext.h>
#include <sudo_json.h>
#include <sudo_plugin.h>
#include <sudo_util.h>

static int audit_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
static sudo_conv_t audit_conv;
static sudo_printf_t audit_printf;

static struct audit_state {
    int submit_optind;
    char uuid_str[37];
    bool accepted;
    FILE *log_fp;
    char *logfile;
    char * const * settings;
    char * const * user_info;
    char * const * submit_argv;
    char * const * submit_envp;
} state = { -1 };

/* Filter out entries in settings[] that are not really options. */
const char * const settings_filter[] = {
    "debug_flags",
    "max_groups",
    "network_addrs",
    "plugin_dir",
    "plugin_path",
    "progname",
    NULL
};

static int
audit_json_open(unsigned int version, sudo_conv_t conversation,
    sudo_printf_t plugin_printf, char * const settings[],
    char * const user_info[], int submit_optind, char * const submit_argv[],
    char * const submit_envp[], char * const plugin_options[],
    const char **errstr)
{
    struct sudo_conf_debug_file_list debug_files =
	TAILQ_HEAD_INITIALIZER(debug_files);
    struct sudo_debug_file *debug_file;
    const char *cp, *plugin_path = NULL;
    unsigned char uuid[16];
    char * const *cur;
    mode_t oldmask;
    int fd, ret = -1;
    debug_decl_vars(audit_json_open, SUDO_DEBUG_PLUGIN);

    audit_conv = conversation;
    audit_printf = plugin_printf;

    /*
     * Stash initial values.
     */
    state.submit_optind = submit_optind;
    state.settings = settings;
    state.user_info = user_info;
    state.submit_argv = submit_argv;
    state.submit_envp = submit_envp;

    /* Initialize the debug subsystem.  */
    for (cur = settings; (cp = *cur) != NULL; cur++) {
        if (strncmp(cp, "debug_flags=", sizeof("debug_flags=") - 1) == 0) {
            cp += sizeof("debug_flags=") - 1;
            if (sudo_debug_parse_flags(&debug_files, cp) == -1)
                goto oom;
            continue;
        }
        if (strncmp(cp, "plugin_path=", sizeof("plugin_path=") - 1) == 0) {
            plugin_path = cp + sizeof("plugin_path=") - 1;
            continue;
        }
    }
    if (plugin_path != NULL && !TAILQ_EMPTY(&debug_files)) {
	audit_debug_instance =
	    sudo_debug_register(plugin_path, NULL, NULL, &debug_files, -1);
	if (audit_debug_instance == SUDO_DEBUG_INSTANCE_ERROR) {
	    *errstr = U_("unable to initialize debugging");
	    goto bad;
	}
	sudo_debug_enter(__func__, __FILE__, __LINE__, sudo_debug_subsys);
    }

    /* Create a UUID for this command for use with audit records. */
    sudo_uuid_create(uuid);
    if (sudo_uuid_to_string(uuid, state.uuid_str, sizeof(state.uuid_str)) == NULL) {
	*errstr = U_("unable to generate UUID");
	goto bad;
    }

    /* Parse plugin_options to check for logfile option. */
    if (plugin_options != NULL) {
	for (cur = plugin_options; (cp = *cur) != NULL; cur++) {
	    if (strncmp(cp, "logfile=", sizeof("logfile=") - 1) == 0) {
		state.logfile = strdup(cp + sizeof("logfile=") - 1);
		if (state.logfile == NULL)
		    goto oom;
	    }
	}
    }
    if (state.logfile == NULL) {
	if (asprintf(&state.logfile, "%s/sudo_audit.json", _PATH_SUDO_LOGDIR) == -1)
	    goto oom;
    }

    /* open log file */
    /* TODO: support pipe */
    oldmask = umask(S_IRWXG|S_IRWXO);
    fd = open(state.logfile, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
    (void)umask(oldmask);
    if (fd == -1 || (state.log_fp = fdopen(fd, "w")) == NULL) {
	*errstr = U_("unable to open audit system");
	if (fd != -1)
	    close(fd);
	goto bad;
    }

    ret = 1;
    goto done;

oom:
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
    *errstr = U_("unable to allocate memory");

bad:
    if (state.log_fp != NULL) {
	fclose(state.log_fp);
	state.log_fp = NULL;
    }

done:
    while ((debug_file = TAILQ_FIRST(&debug_files))) {
	TAILQ_REMOVE(&debug_files, debug_file, entries);
	free(debug_file->debug_file);
	free(debug_file->debug_flags);
	free(debug_file);
    }

    debug_return_int(ret);
}

static bool
add_key_value(struct json_container *jsonc, const char *str)
{
    struct json_value json_value;
    const char *cp, *errstr;
    char name[256];
    size_t len;
    debug_decl(add_key_value, SUDO_DEBUG_PLUGIN);

    if ((cp = strchr(str, '=')) == NULL) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "ignoring bad command info string \"%s\"", str);
	debug_return_bool(false);
    }
    len = (size_t)(cp - str);
    cp++;

    /* Variable name currently limited to 256 chars */
    if (len >= sizeof(name)) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "ignoring long command info name \"%.*s\"", (int)len, str);
	debug_return_bool(false);
    }
    memcpy(name, str, len);
    name[len] = '\0';

    /* Check for bool or number. */
    json_value.type = JSON_NULL;
    switch (cp[0]) {
    case '0':
	if (cp[1] == '\0') {
	    /* Only treat a plain "0" as number 0. */
	    json_value.u.number = 0;
	    json_value.type = JSON_NUMBER;
	}
	break;
    case '+': case '-':
	if (cp[1] == '0') {
	    /* Encode octal numbers as strings. */
	    break;
	}
	FALLTHROUGH;
    case '1': case '2': case '3': case '4': case '5':
    case '6': case '7': case '8': case '9':
	json_value.u.number = sudo_strtonum(cp, INT_MIN, INT_MAX, &errstr);
	if (errstr == NULL)
	    json_value.type = JSON_NUMBER;
	break;
    case 't':
	if (strcmp(cp, "true") == 0) {
	    json_value.type = JSON_BOOL;
	    json_value.u.boolean = true;
	}
	break;
    case 'f':
	if (strcmp(cp, "false") == 0) {
	    json_value.type = JSON_BOOL;
	    json_value.u.boolean = false;
	}
	break;
    }

    /* Default to string type. */
    if (json_value.type == JSON_NULL) {
	json_value.type = JSON_STRING;
	json_value.u.string = cp;
    }

    debug_return_bool(sudo_json_add_value(jsonc, name, &json_value));
}

static bool
add_array(struct json_container *jsonc, const char *name, char * const * array)
{
    const char *cp;
    struct json_value json_value;
    debug_decl(add_array, SUDO_DEBUG_PLUGIN);

    if (!sudo_json_open_array(jsonc, name))
	debug_return_bool(false);
    while ((cp = *array) != NULL) {
	json_value.type = JSON_STRING;
	json_value.u.string = cp;
	if (!sudo_json_add_value(jsonc, name, &json_value))
	    debug_return_bool(false);
	array++;
    }
    if (!sudo_json_close_array(jsonc))
	debug_return_bool(false);

    debug_return_bool(true);
}

static bool
filter_key_value(const char *kv, const char * const * filter)
{
    const char * const *cur;
    const char *cp;
    size_t namelen;

    if (filter != NULL) {
	namelen = strcspn(kv, "=");
	for (cur = filter; (cp = *cur) != NULL; cur++) {
	    if (strncmp(kv, cp, namelen) == 0 && cp[namelen] == '\0')
		return true;
	}
    }
    return false;
}

static bool
add_key_value_object(struct json_container *jsonc, const char *name,
	char * const * array, const char * const * filter)
{
    char * const *cur;
    const char *cp;
    bool empty = false;
    debug_decl(add_key_value_object, SUDO_DEBUG_PLUGIN);

    if (filter != NULL) {
	/* Avoid printing an empty object if everything is filtered. */
	empty = true;
	for (cur = array; (cp = *cur) != NULL; cur++) {
	    if (!filter_key_value(cp, filter)) {
		empty = false;
		break;
	    }
	}
    }
    if (!empty) {
	if (!sudo_json_open_object(jsonc, name))
	    goto bad;
	for (cur = array; (cp = *cur) != NULL; cur++) {
	    if (filter_key_value(cp, filter))
		continue;
	    if (!add_key_value(jsonc, cp))
		goto bad;
	}
	if (!sudo_json_close_object(jsonc))
	    goto bad;
    }

    debug_return_bool(true);
bad:
    debug_return_bool(false);
}

static bool
add_timestamp(struct json_container *jsonc, struct timespec *ts)
{
    struct json_value json_value;
    time_t secs = ts->tv_sec;
    char timebuf[1024];
    struct tm gmt;
    size_t len;
    debug_decl(add_timestamp, SUDO_DEBUG_PLUGIN);

    if (gmtime_r(&secs, &gmt) == NULL)
	debug_return_bool(false);

    sudo_json_open_object(jsonc, "timestamp");

    json_value.type = JSON_NUMBER;
    json_value.u.number = ts->tv_sec;
    sudo_json_add_value(jsonc, "seconds", &json_value);

    json_value.type = JSON_NUMBER;
    json_value.u.number = ts->tv_nsec;
    sudo_json_add_value(jsonc, "nanoseconds", &json_value);

    timebuf[sizeof(timebuf) - 1] = '\0';
    len = strftime(timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", &gmt);
    if (len != 0 && timebuf[sizeof(timebuf) - 1] == '\0'){
	json_value.type = JSON_STRING;
	json_value.u.string = timebuf;
	sudo_json_add_value(jsonc, "iso8601", &json_value);
    }

    timebuf[sizeof(timebuf) - 1] = '\0';
    len = strftime(timebuf, sizeof(timebuf), "%a %b %e %H:%M:%S %Z %Y", &gmt);
    if (len != 0 && timebuf[sizeof(timebuf) - 1] == '\0'){
	json_value.type = JSON_STRING;
	json_value.u.string = timebuf;
	sudo_json_add_value(jsonc, "localtime", &json_value);
    }

    sudo_json_close_object(jsonc);

    debug_return_bool(true);
}

static int
audit_write_json(struct json_container * restrict jsonc)
{
    struct stat sb;
    int ret = -1;
    debug_decl(audit_write_json, SUDO_DEBUG_PLUGIN);

    if (!sudo_lock_file(fileno(state.log_fp), SUDO_LOCK)) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
	    "unable to lock %s", state.logfile);
	goto done;
    }

    /* Note: assumes file ends in "\n}\n" */
    if (fstat(fileno(state.log_fp), &sb) == -1) {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
	    "unable to stat %s", state.logfile);
	goto done;
    }
    if (sb.st_size == 0) {
	/* New file */
	putc('{', state.log_fp);
    } else if (fseeko(state.log_fp, -3, SEEK_END) == 0) {
	/* Continue file, overwrite the final "\n}\n" */
	putc(',', state.log_fp);
    } else {
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
	    "unable to seek %s", state.logfile);
	goto done;
    }

    fputs(sudo_json_get_buf(jsonc), state.log_fp);
    fputs("\n}\n", state.log_fp);
    fflush(state.log_fp);
    (void)sudo_lock_file(fileno(state.log_fp), SUDO_UNLOCK);

    /* TODO: undo partial record on error */
    if (!ferror(state.log_fp))
	ret = true;

done:
    debug_return_int(ret);
}

static int
audit_write_exit_record(int exit_status, int error)
{
    struct json_container jsonc;
    struct json_value json_value;
    struct timespec now;
    int ret = -1;
    debug_decl(audit_write_exit_record, SUDO_DEBUG_PLUGIN);

    if (sudo_gettime_real(&now) == -1) {
	sudo_warn("%s", U_("unable to read the clock"));
	goto done;
    }

    if (!sudo_json_init(&jsonc, 4, false, false, false))
	goto oom;
    if (!sudo_json_open_object(&jsonc, "exit"))
	goto oom;

    /* Write UUID */
    json_value.type = JSON_STRING;
    json_value.u.string = state.uuid_str;
    if (!sudo_json_add_value(&jsonc, "uuid", &json_value))
	goto oom;

    /* Write time stamp */
    if (!add_timestamp(&jsonc, &now))
	goto oom;

    if (error != 0) {
	/* Error executing command */
	json_value.type = JSON_STRING;
	json_value.u.string = strerror(error);
	if (!sudo_json_add_value(&jsonc, "error", &json_value))
	    goto oom;
    } else {
        if (WIFEXITED(exit_status)) {
	    /* Command exited normally. */
	    json_value.type = JSON_NUMBER;
	    json_value.u.number = WEXITSTATUS(exit_status);
	    if (!sudo_json_add_value(&jsonc, "exit_value", &json_value))
		goto oom;
        } else if (WIFSIGNALED(exit_status)) {
	    /* Command killed by signal. */
	    char signame[SIG2STR_MAX];
            int signo = WTERMSIG(exit_status);
            if (signo <= 0 || sig2str(signo, signame) == -1) {
		json_value.type = JSON_NUMBER;
		json_value.u.number = signo;
		if (!sudo_json_add_value(&jsonc, "signal", &json_value))
		    goto oom;
            } else {
		json_value.type = JSON_STRING;
		json_value.u.string = signame; // -V507
		if (!sudo_json_add_value(&jsonc, "signal", &json_value))
		    goto oom;
	    }
	    /* Core dump? */
	    json_value.type = JSON_BOOL;
	    json_value.u.boolean = WCOREDUMP(exit_status);
	    if (!sudo_json_add_value(&jsonc, "dumped_core", &json_value))
		goto oom;
	    /* Exit value */
	    json_value.type = JSON_NUMBER;
	    json_value.u.number = WTERMSIG(exit_status) | 128;
	    if (!sudo_json_add_value(&jsonc, "exit_value", &json_value))
		goto oom;
        }
    }

    if (!sudo_json_close_object(&jsonc))
	goto oom;

    ret = audit_write_json(&jsonc);
    sudo_json_free(&jsonc);
done:
    debug_return_int(ret);
oom:
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
    sudo_json_free(&jsonc);
    debug_return_int(-1);
}

static int
audit_write_record(const char *audit_str, const char *plugin_name,
    unsigned int plugin_type, const char *reason, char * const command_info[],
    char * const run_argv[], char * const run_envp[])
{
    struct json_container jsonc;
    struct json_value json_value;
    struct timespec now;
    int ret = -1;
    debug_decl(audit_write_record, SUDO_DEBUG_PLUGIN);

    if (sudo_gettime_real(&now) == -1) {
	sudo_warn("%s", U_("unable to read the clock"));
	goto done;
    }

    if (!sudo_json_init(&jsonc, 4, false, false, false))
	goto oom;
    if (!sudo_json_open_object(&jsonc, audit_str))
	goto oom;

    json_value.type = JSON_STRING;
    json_value.u.string = plugin_name;
    if (!sudo_json_add_value(&jsonc, "plugin_name", &json_value))
	goto oom;

    switch (plugin_type) {
    case SUDO_FRONT_END:
	json_value.u.string = "front-end";
	break;
    case SUDO_POLICY_PLUGIN:
	json_value.u.string = "policy";
	break;
    case SUDO_IO_PLUGIN:
	json_value.u.string = "io";
	break;
    case SUDO_APPROVAL_PLUGIN:
	json_value.u.string = "approval";
	break;
    case SUDO_AUDIT_PLUGIN:
	json_value.u.string = "audit";
	break;
    default:
	json_value.u.string = "unknown";
	break;
    }
    json_value.type = JSON_STRING;
    if (!sudo_json_add_value(&jsonc, "plugin_type", &json_value))
	goto oom;

    /* error and reject audit events usually contain a reason. */
    if (reason != NULL) {
	json_value.type = JSON_STRING;
	json_value.u.string = reason;
	if (!sudo_json_add_value(&jsonc, "reason", &json_value))
	    goto oom;
    }

    json_value.type = JSON_STRING;
    json_value.u.string = state.uuid_str;
    if (!sudo_json_add_value(&jsonc, "uuid", &json_value))
	goto oom;

    if (!add_timestamp(&jsonc, &now))
	goto oom;

    /* Write key=value objects. */
    if (state.settings != NULL) {
	if (!add_key_value_object(&jsonc, "options", state.settings, settings_filter))
	    goto oom;
    } else {
	sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
	    "missing settings list");
    }
    if (state.user_info != NULL) {
	if (!add_key_value_object(&jsonc, "user_info", state.user_info, NULL))
	    goto oom;
    } else {
	sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
	    "missing user_info list");
    }
    if (command_info != NULL) {
	if (!add_key_value_object(&jsonc, "command_info", command_info, NULL))
	    goto oom;
    }

    /* Write submit_optind before submit_argv */
    json_value.type = JSON_NUMBER;
    json_value.u.number = state.submit_optind;
    if (!sudo_json_add_value(&jsonc, "submit_optind", &json_value))
	goto oom;

    if (state.submit_argv != NULL) {
	if (!add_array(&jsonc, "submit_argv", state.submit_argv))
	    goto oom;
    } else {
	sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
	    "missing submit_argv array");
    }
    if (state.submit_envp != NULL) {
	if (!add_array(&jsonc, "submit_envp", state.submit_envp))
	    goto oom;
    } else {
	sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
	    "missing submit_envp array");
    }
    if (run_argv != NULL) {
	if (!add_array(&jsonc, "run_argv", run_argv))
	    goto oom;
    }
    if (run_envp != NULL) {
	if (!add_array(&jsonc, "run_envp", run_envp))
	    goto oom;
    }

    if (!sudo_json_close_object(&jsonc))
	goto oom;

    ret = audit_write_json(&jsonc);
    sudo_json_free(&jsonc);

done:
    debug_return_int(ret);
oom:
    sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
    sudo_json_free(&jsonc);
    debug_return_int(-1);
}

static int
audit_json_accept(const char *plugin_name, unsigned int plugin_type,
    char * const command_info[], char * const run_argv[],
    char * const run_envp[], const char **errstr)
{
    int ret;
    debug_decl(audit_json_accept, SUDO_DEBUG_PLUGIN);

    /* Ignore the extra accept event from the sudo front-end. */
    if (plugin_type == SUDO_FRONT_END)
	debug_return_int(true);

    state.accepted = true;

    ret = audit_write_record("accept", plugin_name, plugin_type, NULL,
	command_info, run_argv, run_envp);

    debug_return_int(ret);
}

static int
audit_json_reject(const char *plugin_name, unsigned int plugin_type,
    const char *reason, char * const command_info[], const char **errstr)
{
    int ret;
    debug_decl(audit_json_reject, SUDO_DEBUG_PLUGIN);

    ret = audit_write_record("reject", plugin_name, plugin_type,
	reason, command_info, NULL, NULL);

    debug_return_int(ret);
}

static int
audit_json_error(const char *plugin_name, unsigned int plugin_type,
    const char *reason, char * const command_info[], const char **errstr)
{
    int ret;
    debug_decl(audit_json_error, SUDO_DEBUG_PLUGIN);

    ret = audit_write_record("error", plugin_name, plugin_type,
	reason, command_info, NULL, NULL);

    debug_return_int(ret);
}

static void
audit_json_close(int status_type, int status)
{
    debug_decl(audit_json_close, SUDO_DEBUG_PLUGIN);

    switch (status_type) {
    case SUDO_PLUGIN_NO_STATUS:
	break;
    case SUDO_PLUGIN_WAIT_STATUS:
	audit_write_exit_record(status, 0);
	break;
    case SUDO_PLUGIN_EXEC_ERROR:
	audit_write_exit_record(0, status);
	break;
    case SUDO_PLUGIN_SUDO_ERROR:
	audit_write_record("error", "sudo", 0, strerror(status),
	    NULL, NULL, NULL);
	break;
    default:
	sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
	    "unexpected status type %d, value %d", status_type, status);
	break;
    }

    free(state.logfile);
    if (state.log_fp != NULL)
	fclose(state.log_fp);

    debug_return;
}

static int
audit_json_show_version(int verbose)
{
    debug_decl(audit_json_show_version, SUDO_DEBUG_PLUGIN);

    audit_printf(SUDO_CONV_INFO_MSG, "JSON audit plugin version %s\n",
        PACKAGE_VERSION);

    debug_return_int(true);
}

sudo_dso_public struct audit_plugin audit_json = {
    SUDO_AUDIT_PLUGIN,
    SUDO_API_VERSION,
    audit_json_open,
    audit_json_close,
    audit_json_accept,
    audit_json_reject,
    audit_json_error,
    audit_json_show_version,
    NULL, /* register_hooks */
    NULL /* deregister_hooks */
};