2 * Copyright (C) 2014 Martin Willi
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 #include <collections/hashtable.h>
27 * Free hashtable with contained strings
29 static void free_hashtable(hashtable_t
*hashtable
)
31 enumerator_t
*enumerator
;
34 enumerator
= hashtable
->create_enumerator(hashtable
);
35 while (enumerator
->enumerate(enumerator
, NULL
, &str
))
39 enumerator
->destroy(enumerator
);
41 hashtable
->destroy(hashtable
);
44 CALLBACK(policy_values
, int,
45 hashtable_t
*pol
, vici_res_t
*res
, char *name
, void *value
, int len
)
50 chunk
= chunk_create(value
, len
);
51 if (chunk_printable(chunk
, NULL
, ' '))
53 if (asprintf(&str
, "%.*s", len
, value
) >= 0)
55 free(pol
->put(pol
, name
, str
));
61 CALLBACK(policy_list
, int,
62 hashtable_t
*pol
, vici_res_t
*res
, char *name
, void *value
, int len
)
67 chunk
= chunk_create(value
, len
);
68 if (chunk_printable(chunk
, NULL
, ' '))
70 str
= pol
->get(pol
, name
);
71 if (asprintf(&str
, "%s%s%.*s",
72 str
?: "", str
? " " : "", len
, value
) >= 0)
74 free(pol
->put(pol
, name
, str
));
80 CALLBACK(policies
, int,
81 void *null
, vici_res_t
*res
, char *name
)
86 pol
= hashtable_create(hashtable_hash_str
, hashtable_equals_str
, 1);
87 ret
= vici_parse_cb(res
, NULL
, policy_values
, policy_list
, pol
);
89 printf("%s, %s\n", name
, pol
->get(pol
, "mode"));
90 print_label(" label: ", pol
->get(pol
, "label"));
91 printf(" local: %s\n", pol
->get(pol
, "local-ts"));
92 printf(" remote: %s\n", pol
->get(pol
, "remote-ts"));
98 CALLBACK(list_cb
, void,
99 command_format_options_t
*format
, char *name
, vici_res_t
*res
)
101 if (*format
& COMMAND_FORMAT_RAW
)
103 vici_dump(res
, "list-policy event", *format
& COMMAND_FORMAT_PRETTY
,
108 if (vici_parse_cb(res
, policies
, NULL
, NULL
, NULL
) != 0)
110 fprintf(stderr
, "parsing policy event failed: %s\n", strerror(errno
));
115 static int list_pols(vici_conn_t
*conn
)
119 bool trap
= FALSE
, drop
= FALSE
, pass
= FALSE
;
120 command_format_options_t format
= COMMAND_FORMAT_NONE
;
121 char *arg
, *child
= NULL
;
126 switch (command_getopt(&arg
))
129 return command_usage(NULL
);
143 format
|= COMMAND_FORMAT_PRETTY
;
144 /* fall through to raw */
146 format
|= COMMAND_FORMAT_RAW
;
151 return command_usage("invalid --list-pols option");
155 if (!trap
&& !drop
&& !pass
)
157 trap
= drop
= pass
= TRUE
;
159 if (vici_register(conn
, "list-policy", list_cb
, &format
) != 0)
162 fprintf(stderr
, "registering for policies failed: %s\n",
166 req
= vici_begin("list-policies");
169 vici_add_key_valuef(req
, "child", "%s", child
);
173 vici_add_key_valuef(req
, "trap", "yes");
177 vici_add_key_valuef(req
, "drop", "yes");
181 vici_add_key_valuef(req
, "pass", "yes");
183 res
= vici_submit(req
, conn
);
187 fprintf(stderr
, "list-policies request failed: %s\n", strerror(errno
));
190 if (format
& COMMAND_FORMAT_RAW
)
192 vici_dump(res
, "list-policies reply", format
& COMMAND_FORMAT_PRETTY
, stdout
);
199 * Register the command.
201 static void __attribute__ ((constructor
))reg()
203 command_register((command_t
) {
204 list_pols
, 'P', "list-pols", "list currently installed policies",
205 {"[--child <name>] [--trap] [--drop] [--pass] [--raw|--pretty]"},
207 {"help", 'h', 0, "show usage information"},
208 {"child", 'c', 1, "filter policies by CHILD_SA config name"},
209 {"trap", 't', 0, "list trap policies"},
210 {"drop", 'd', 0, "list drop policies"},
211 {"pass", 'p', 0, "list bypass policies"},
212 {"raw", 'r', 0, "dump raw response message"},
213 {"pretty", 'P', 0, "dump raw response message in pretty print"},