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
25 * Load a single certificate over vici
27 static bool load_cert(vici_conn_t
*conn
, bool raw
, char *dir
,
28 char *type
, chunk_t data
)
34 req
= vici_begin("load-cert");
36 vici_add_key_valuef(req
, "type", "%s", type
);
37 vici_add_key_value(req
, "data", data
.ptr
, data
.len
);
39 res
= vici_submit(req
, conn
);
42 fprintf(stderr
, "load-cert request failed: %s\n", strerror(errno
));
47 vici_dump(res
, "load-cert reply", stdout
);
49 else if (!streq(vici_find_str(res
, "no", "success"), "yes"))
51 fprintf(stderr
, "loading '%s' failed: %s\n",
52 dir
, vici_find_str(res
, "", "errmsg"));
60 * Load certficiates from a directory
62 static void load_certs(vici_conn_t
*conn
, bool raw
, char *type
, char *dir
)
64 enumerator_t
*enumerator
;
69 enumerator
= enumerator_create_directory(dir
);
72 while (enumerator
->enumerate(enumerator
, NULL
, &path
, &st
))
74 if (S_ISREG(st
.st_mode
))
76 map
= chunk_map(path
, FALSE
);
79 load_cert(conn
, raw
, path
, type
, *map
);
84 fprintf(stderr
, "mapping '%s' failed: %s, skipped\n",
85 path
, strerror(errno
));
89 enumerator
->destroy(enumerator
);
94 * Clear all currently loaded credentials
96 static bool clear_creds(vici_conn_t
*conn
, bool raw
)
100 res
= vici_submit(vici_begin("clear-creds"), conn
);
103 fprintf(stderr
, "clear-creds request failed: %s\n", strerror(errno
));
108 vici_dump(res
, "clear-creds reply", stdout
);
114 static int load_creds(vici_conn_t
*conn
)
116 bool raw
= FALSE
, clear
= FALSE
;
121 switch (command_getopt(&arg
))
124 return command_usage(NULL
);
134 return command_usage("invalid --load-creds option");
141 if (!clear_creds(conn
, raw
))
147 load_certs(conn
, raw
, "x509", SWANCTL_X509DIR
);
148 load_certs(conn
, raw
, "x509ca", SWANCTL_X509CADIR
);
149 load_certs(conn
, raw
, "x509aa", SWANCTL_X509AADIR
);
150 load_certs(conn
, raw
, "x509crl", SWANCTL_X509CRLDIR
);
151 load_certs(conn
, raw
, "x509ac", SWANCTL_X509ACDIR
);
157 * Register the command.
159 static void __attribute__ ((constructor
))reg()
161 command_register((command_t
) {
162 load_creds
, 's', "load-creds", "(re-)load credentials",
165 {"help", 'h', 0, "show usage information"},
166 {"clear", 'c', 0, "clear previously loaded credentials"},
167 {"raw", 'r', 0, "dump raw response message"},