2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
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>.
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
22 #include <collections/hashtable.h>
25 * Free hashtable with contained strings
27 static void free_hashtable(hashtable_t
*hashtable
)
29 enumerator_t
*enumerator
;
32 enumerator
= hashtable
->create_enumerator(hashtable
);
33 while (enumerator
->enumerate(enumerator
, NULL
, &str
))
37 enumerator
->destroy(enumerator
);
39 hashtable
->destroy(hashtable
);
42 CALLBACK(policy_values
, int,
43 hashtable_t
*pol
, vici_res_t
*res
, char *name
, void *value
, int len
)
48 chunk
= chunk_create(value
, len
);
49 if (chunk_printable(chunk
, NULL
, ' '))
51 if (asprintf(&str
, "%.*s", len
, value
) >= 0)
53 free(pol
->put(pol
, name
, str
));
59 CALLBACK(policy_list
, int,
60 hashtable_t
*pol
, vici_res_t
*res
, char *name
, void *value
, int len
)
65 chunk
= chunk_create(value
, len
);
66 if (chunk_printable(chunk
, NULL
, ' '))
68 str
= pol
->get(pol
, name
);
69 if (asprintf(&str
, "%s%s%.*s",
70 str ?
: "", str ?
" " : "", len
, value
) >= 0)
72 free(pol
->put(pol
, name
, str
));
78 CALLBACK(policies
, int,
79 void *null
, vici_res_t
*res
, char *name
)
84 pol
= hashtable_create(hashtable_hash_str
, hashtable_equals_str
, 1);
85 ret
= vici_parse_cb(res
, NULL
, policy_values
, policy_list
, pol
);
87 printf("%s, %s\n", name
, pol
->get(pol
, "mode"));
88 printf(" local: %s\n", pol
->get(pol
, "local-ts"));
89 printf(" remote: %s\n", pol
->get(pol
, "remote-ts"));
95 CALLBACK(list_cb
, void,
96 bool *raw
, char *name
, vici_res_t
*res
)
100 vici_dump(res
, "list-policy event", stdout
);
104 if (vici_parse_cb(res
, policies
, NULL
, NULL
, NULL
) != 0)
106 fprintf(stderr
, "parsing policy event failed: %s\n", strerror(errno
));
111 static int list_pols(vici_conn_t
*conn
)
115 bool raw
= FALSE
, trap
= FALSE
, drop
= FALSE
, pass
= FALSE
;
116 char *arg
, *child
= NULL
;
120 switch (command_getopt(&arg
))
123 return command_usage(NULL
);
142 return command_usage("invalid --list-pols option");
146 if (!trap
&& !drop
&& !pass
)
148 trap
= drop
= pass
= TRUE
;
150 if (vici_register(conn
, "list-policy", list_cb
, &raw
) != 0)
152 fprintf(stderr
, "registering for policies failed: %s\n",
156 req
= vici_begin("list-policies");
159 vici_add_key_valuef(req
, "child", "%s", child
);
163 vici_add_key_valuef(req
, "trap", "yes");
167 vici_add_key_valuef(req
, "drop", "yes");
171 vici_add_key_valuef(req
, "pass", "yes");
173 res
= vici_submit(req
, conn
);
176 fprintf(stderr
, "list-policies request failed: %s\n", strerror(errno
));
181 vici_dump(res
, "list-policies reply", stdout
);
188 * Register the command.
190 static void __attribute__ ((constructor
))reg()
192 command_register((command_t
) {
193 list_pols
, 'P', "list-pols", "list currently installed policies",
194 {"[--child <name>] [--trap] [--drop] [--pass] [--raw]"},
196 {"help", 'h', 0, "show usage information"},
197 {"child", 'c', 1, "filter policies by CHILD_SA config name"},
198 {"trap", 't', 0, "list trap policies"},
199 {"drop", 'd', 0, "list drop policies"},
200 {"pass", 'p', 0, "list bypass policies"},
201 {"raw", 'r', 0, "dump raw response message"},