vici: Match identity with wildcards against remote ID in redirect command
[strongswan.git] / src / swanctl / commands / log.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "command.h"
17
18 #include <errno.h>
19 #include <unistd.h>
20
21 CALLBACK(log_cb, void,
22 command_format_options_t *format, char *name, vici_res_t *msg)
23 {
24 if (*format & COMMAND_FORMAT_RAW)
25 {
26 vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
27 }
28 else
29 {
30 char *current, *next;
31
32 current = vici_find_str(msg, NULL, "msg");
33 while (current)
34 {
35 next = strchr(current, '\n');
36 printf("%.2d[%s] ", vici_find_int(msg, 0, "thread"),
37 vici_find_str(msg, " ", "group"));
38 if (next == NULL)
39 {
40 printf("%s\n", current);
41 break;
42 }
43 printf("%.*s\n", (int)(next - current), current);
44 current = next + 1;
45 }
46 }
47 }
48
49 static int logcmd(vici_conn_t *conn)
50 {
51 command_format_options_t format = COMMAND_FORMAT_NONE;
52 char *arg;
53 int ret;
54
55 while (TRUE)
56 {
57 switch (command_getopt(&arg))
58 {
59 case 'h':
60 return command_usage(NULL);
61 case 'P':
62 format |= COMMAND_FORMAT_PRETTY;
63 /* fall through to raw */
64 case 'r':
65 format |= COMMAND_FORMAT_RAW;
66 continue;
67 case EOF:
68 break;
69 default:
70 return command_usage("invalid --log option");
71 }
72 break;
73 }
74
75 if (vici_register(conn, "log", log_cb, &format) != 0)
76 {
77 ret = errno;
78 fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
79 return ret;
80 }
81
82 wait_sigint();
83
84 fprintf(stderr, "disconnecting...\n");
85
86 return 0;
87 }
88
89 /**
90 * Register the command.
91 */
92 static void __attribute__ ((constructor))reg()
93 {
94 command_register((command_t) {
95 logcmd, 'T', "log", "trace logging output",
96 {"[--raw|--pretty]"},
97 {
98 {"help", 'h', 0, "show usage information"},
99 {"raw", 'r', 0, "dump raw response message"},
100 {"pretty", 'P', 0, "dump raw response message in pretty print"},
101 }
102 });
103 }