klist --json support
This commit is contained in:
177
kuser/klist.c
177
kuser/klist.c
@@ -34,7 +34,6 @@
|
||||
*/
|
||||
|
||||
#include "kuser_locl.h"
|
||||
#include "rtbl.h"
|
||||
#include "parse_units.h"
|
||||
#include "heimtools-commands.h"
|
||||
|
||||
@@ -130,13 +129,18 @@ print_cred(krb5_context context, krb5_creds *cred, rtbl_t ct, int do_flags)
|
||||
}
|
||||
|
||||
static void
|
||||
print_cred_verbose(krb5_context context, krb5_creds *cred)
|
||||
print_cred_verbose(krb5_context context, krb5_creds *cred, int do_json)
|
||||
{
|
||||
size_t j;
|
||||
char *str;
|
||||
krb5_error_code ret;
|
||||
krb5_timestamp sec;
|
||||
|
||||
if (do_json) { /* XXX support more json formating later */
|
||||
printf("{ \"verbose-supported\" : false }");
|
||||
return;
|
||||
}
|
||||
|
||||
krb5_timeofday (context, &sec);
|
||||
|
||||
ret = krb5_unparse_name(context, cred->server, &str);
|
||||
@@ -150,8 +154,8 @@ print_cred_verbose(krb5_context context, krb5_creds *cred)
|
||||
exit(1);
|
||||
printf(N_("Client: %s\n", ""), str);
|
||||
free (str);
|
||||
|
||||
{
|
||||
|
||||
if (!krb5_is_config_principal(context, cred->client)) {
|
||||
Ticket t;
|
||||
size_t len;
|
||||
char *s;
|
||||
@@ -229,10 +233,11 @@ print_tickets (krb5_context context,
|
||||
krb5_principal principal,
|
||||
int do_verbose,
|
||||
int do_flags,
|
||||
int do_hidden)
|
||||
int do_hidden,
|
||||
int do_json)
|
||||
{
|
||||
char *str, *name, *fullname;
|
||||
krb5_error_code ret;
|
||||
char *str, *name;
|
||||
krb5_cc_cursor cursor;
|
||||
krb5_creds creds;
|
||||
krb5_deltat sec;
|
||||
@@ -243,49 +248,53 @@ print_tickets (krb5_context context,
|
||||
if (ret)
|
||||
krb5_err (context, 1, ret, "krb5_unparse_name");
|
||||
|
||||
printf ("%17s: %s:%s\n",
|
||||
N_("Credentials cache", ""),
|
||||
krb5_cc_get_type(context, ccache),
|
||||
krb5_cc_get_name(context, ccache));
|
||||
printf ("%17s: %s\n", N_("Principal", ""), str);
|
||||
ret = krb5_cc_get_full_name(context, ccache, &fullname);
|
||||
if (ret)
|
||||
krb5_err (context, 1, ret, "krb5_cc_get_full_name");
|
||||
|
||||
ret = krb5_cc_get_friendly_name(context, ccache, &name);
|
||||
if (ret == 0) {
|
||||
if (strcmp(name, str) != 0)
|
||||
printf ("%17s: %s\n", N_("Friendly name", ""), name);
|
||||
free(name);
|
||||
}
|
||||
free (str);
|
||||
|
||||
if(do_verbose) {
|
||||
printf ("%17s: %d\n", N_("Cache version", ""),
|
||||
krb5_cc_get_version(context, ccache));
|
||||
} else {
|
||||
krb5_cc_set_flags(context, ccache, KRB5_TC_NOTICKET);
|
||||
}
|
||||
|
||||
ret = krb5_cc_get_kdc_offset(context, ccache, &sec);
|
||||
|
||||
if (ret == 0 && do_verbose && sec != 0) {
|
||||
char buf[BUFSIZ];
|
||||
int val;
|
||||
int sig;
|
||||
|
||||
val = sec;
|
||||
sig = 1;
|
||||
if (val < 0) {
|
||||
sig = -1;
|
||||
val = -val;
|
||||
if (!do_json) {
|
||||
printf ("%17s: %s\n", N_("Credentials cache", ""), fullname);
|
||||
printf ("%17s: %s\n", N_("Principal", ""), str);
|
||||
|
||||
ret = krb5_cc_get_friendly_name(context, ccache, &name);
|
||||
if (ret == 0) {
|
||||
if (strcmp(name, str) != 0)
|
||||
printf ("%17s: %s\n", N_("Friendly name", ""), name);
|
||||
free(name);
|
||||
}
|
||||
free (str);
|
||||
|
||||
if(do_verbose) {
|
||||
printf ("%17s: %d\n", N_("Cache version", ""),
|
||||
krb5_cc_get_version(context, ccache));
|
||||
} else {
|
||||
krb5_cc_set_flags(context, ccache, KRB5_TC_NOTICKET);
|
||||
}
|
||||
|
||||
ret = krb5_cc_get_kdc_offset(context, ccache, &sec);
|
||||
|
||||
if (ret == 0 && do_verbose && sec != 0) {
|
||||
char buf[BUFSIZ];
|
||||
int val;
|
||||
int sig;
|
||||
|
||||
val = (int)sec;
|
||||
sig = 1;
|
||||
if (val < 0) {
|
||||
sig = -1;
|
||||
val = -val;
|
||||
}
|
||||
|
||||
unparse_time (val, buf, sizeof(buf));
|
||||
|
||||
unparse_time (val, buf, sizeof(buf));
|
||||
|
||||
printf ("%17s: %s%s\n", N_("KDC time offset", ""),
|
||||
sig == -1 ? "-" : "", buf);
|
||||
printf ("%17s: %s%s\n", N_("KDC time offset", ""),
|
||||
sig == -1 ? "-" : "", buf);
|
||||
}
|
||||
printf("\n");
|
||||
} else {
|
||||
printf ("{ \"cache\" : \"%s\", \"principal\" : \"%s\", ", fullname, str);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
ret = krb5_cc_start_seq_get (context, ccache, &cursor);
|
||||
if (ret)
|
||||
krb5_err(context, 1, ret, "krb5_cc_start_seq_get");
|
||||
@@ -298,7 +307,13 @@ print_tickets (krb5_context context,
|
||||
rtbl_add_column(ct, COL_FLAGS, 0);
|
||||
rtbl_add_column(ct, COL_PRINCIPAL, 0);
|
||||
rtbl_set_separator(ct, " ");
|
||||
if (do_json) {
|
||||
rtbl_set_flags(ct, RTBL_JSON);
|
||||
printf("\"tickets\" : ");
|
||||
}
|
||||
}
|
||||
if (do_verbose && do_json)
|
||||
printf("\"tickets\" : [");
|
||||
while ((ret = krb5_cc_next_cred (context,
|
||||
ccache,
|
||||
&cursor,
|
||||
@@ -306,7 +321,7 @@ print_tickets (krb5_context context,
|
||||
if (!do_hidden && krb5_is_config_principal(context, creds.server)) {
|
||||
;
|
||||
}else if(do_verbose){
|
||||
print_cred_verbose(context, &creds);
|
||||
print_cred_verbose(context, &creds, do_json);
|
||||
}else{
|
||||
print_cred(context, &creds, ct, do_flags);
|
||||
}
|
||||
@@ -321,6 +336,11 @@ print_tickets (krb5_context context,
|
||||
rtbl_format(ct, stdout);
|
||||
rtbl_destroy(ct);
|
||||
}
|
||||
if (do_json) {
|
||||
if (do_verbose)
|
||||
printf("]");
|
||||
printf("}");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -353,7 +373,7 @@ check_for_tgt (krb5_context context,
|
||||
ret = krb5_cc_retrieve_cred (context, ccache, 0, &pattern, &creds);
|
||||
krb5_free_principal (context, pattern.server);
|
||||
if (ret) {
|
||||
if (ret == KRB5_CC_END)
|
||||
if (ret == KRB5_CC_NOTFOUND)
|
||||
return 1;
|
||||
krb5_err (context, 1, ret, "krb5_cc_retrieve_cred");
|
||||
}
|
||||
@@ -448,7 +468,8 @@ display_tokens(int do_verbose)
|
||||
static int
|
||||
display_v5_ccache (krb5_context context, krb5_ccache ccache,
|
||||
int do_test, int do_verbose,
|
||||
int do_flags, int do_hidden)
|
||||
int do_flags, int do_hidden,
|
||||
int do_json)
|
||||
{
|
||||
krb5_error_code ret;
|
||||
krb5_principal principal;
|
||||
@@ -457,6 +478,10 @@ display_v5_ccache (krb5_context context, krb5_ccache ccache,
|
||||
|
||||
ret = krb5_cc_get_principal (context, ccache, &principal);
|
||||
if (ret) {
|
||||
if (do_json) {
|
||||
printf("{}");
|
||||
return 0;
|
||||
}
|
||||
if(ret == ENOENT) {
|
||||
if (!do_test)
|
||||
krb5_warnx(context, N_("No ticket file: %s", ""),
|
||||
@@ -469,7 +494,7 @@ display_v5_ccache (krb5_context context, krb5_ccache ccache,
|
||||
exit_status = check_for_tgt (context, ccache, principal, NULL);
|
||||
else
|
||||
print_tickets (context, ccache, principal, do_verbose,
|
||||
do_flags, do_hidden);
|
||||
do_flags, do_hidden, do_json);
|
||||
|
||||
ret = krb5_cc_close (context, ccache);
|
||||
if (ret)
|
||||
@@ -485,9 +510,9 @@ display_v5_ccache (krb5_context context, krb5_ccache ccache,
|
||||
*/
|
||||
|
||||
static int
|
||||
list_caches(krb5_context context)
|
||||
list_caches(krb5_context context, struct klist_options *opt)
|
||||
{
|
||||
krb5_cc_cache_cursor cursor;
|
||||
krb5_cccol_cursor cursor;
|
||||
const char *cdef_name;
|
||||
char *def_name;
|
||||
krb5_error_code ret;
|
||||
@@ -499,21 +524,25 @@ list_caches(krb5_context context)
|
||||
krb5_errx(context, 1, "krb5_cc_default_name");
|
||||
def_name = strdup(cdef_name);
|
||||
|
||||
ret = krb5_cc_cache_get_first (context, NULL, &cursor);
|
||||
ret = krb5_cccol_cursor_new(context, &cursor);
|
||||
if (ret == KRB5_CC_NOSUPP)
|
||||
return 0;
|
||||
else if (ret)
|
||||
krb5_err (context, 1, ret, "krb5_cc_cache_get_first");
|
||||
|
||||
ct = rtbl_create();
|
||||
rtbl_add_column(ct, COL_DEFCACHE, 0);
|
||||
rtbl_add_column(ct, COL_NAME, 0);
|
||||
rtbl_add_column(ct, COL_CACHENAME, 0);
|
||||
rtbl_add_column(ct, COL_EXPIRES, 0);
|
||||
rtbl_add_column(ct, COL_DEFCACHE, 0);
|
||||
rtbl_set_prefix(ct, " ");
|
||||
rtbl_set_column_prefix(ct, COL_NAME, "");
|
||||
rtbl_set_column_prefix(ct, COL_DEFCACHE, "");
|
||||
rtbl_set_column_prefix(ct, COL_NAME, " ");
|
||||
if (opt->json_flag)
|
||||
rtbl_set_flags(ct, RTBL_JSON);
|
||||
|
||||
while (krb5_cc_cache_next (context, cursor, &id) == 0) {
|
||||
while (krb5_cccol_cursor_next(context, cursor, &id) == 0) {
|
||||
krb5_principal principal = NULL;
|
||||
int expired = 0;
|
||||
char *name;
|
||||
@@ -529,21 +558,24 @@ list_caches(krb5_context context)
|
||||
if (ret == 0) {
|
||||
const char *str;
|
||||
char *fname;
|
||||
|
||||
rtbl_add_column_entry(ct, COL_NAME, name);
|
||||
rtbl_add_column_entry(ct, COL_CACHENAME,
|
||||
krb5_cc_get_name(context, id));
|
||||
free(name);
|
||||
|
||||
if (expired)
|
||||
str = N_(">>> Expired <<<", "");
|
||||
else
|
||||
str = printable_time(t);
|
||||
rtbl_add_column_entry(ct, COL_EXPIRES, str);
|
||||
free(name);
|
||||
|
||||
ret = krb5_cc_get_full_name(context, id, &fname);
|
||||
if (ret)
|
||||
krb5_err (context, 1, ret, "krb5_cc_get_full_name");
|
||||
|
||||
if (strcmp(fname, def_name) == 0)
|
||||
rtbl_add_column_entry(ct, COL_CACHENAME, fname);
|
||||
if (opt->json_flag)
|
||||
;
|
||||
else if (strcmp(fname, def_name) == 0)
|
||||
rtbl_add_column_entry(ct, COL_DEFCACHE, "*");
|
||||
else
|
||||
rtbl_add_column_entry(ct, COL_DEFCACHE, "");
|
||||
@@ -555,12 +587,15 @@ list_caches(krb5_context context)
|
||||
krb5_free_principal(context, principal);
|
||||
}
|
||||
|
||||
krb5_cc_cache_end_seq_get(context, cursor);
|
||||
krb5_cccol_cursor_free(context, &cursor);
|
||||
|
||||
free(def_name);
|
||||
rtbl_format(ct, stdout);
|
||||
rtbl_destroy(ct);
|
||||
|
||||
if (opt->json_flag)
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -582,8 +617,13 @@ klist(struct klist_options *opt, int argc, char **argv)
|
||||
opt->test_flag ||
|
||||
opt->s_flag;
|
||||
|
||||
if(opt->version_flag) {
|
||||
print_version(NULL);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (opt->list_all_flag) {
|
||||
exit_status = list_caches(heimtools_context);
|
||||
exit_status = list_caches(heimtools_context, opt);
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
@@ -592,20 +632,29 @@ klist(struct klist_options *opt, int argc, char **argv)
|
||||
|
||||
if (opt->all_content_flag) {
|
||||
krb5_cc_cache_cursor cursor;
|
||||
int first = 1;
|
||||
|
||||
ret = krb5_cc_cache_get_first(heimtools_context, NULL, &cursor);
|
||||
if (ret)
|
||||
krb5_err(heimtools_context, 1, ret, "krb5_cc_cache_get_first");
|
||||
|
||||
|
||||
if (opt->json_flag)
|
||||
printf("[");
|
||||
while (krb5_cc_cache_next(heimtools_context, cursor, &id) == 0) {
|
||||
if (opt->json_flag && !first)
|
||||
printf(",");
|
||||
|
||||
exit_status |= display_v5_ccache(heimtools_context, id, do_test,
|
||||
do_verbose, opt->flags_flag,
|
||||
opt->hidden_flag);
|
||||
printf("\n\n");
|
||||
opt->hidden_flag, opt->json_flag);
|
||||
if (!opt->json_flag)
|
||||
printf("\n\n");
|
||||
|
||||
first = 0;
|
||||
}
|
||||
krb5_cc_cache_end_seq_get(heimtools_context, cursor);
|
||||
|
||||
if (opt->json_flag)
|
||||
printf("]");
|
||||
} else {
|
||||
if(opt->cache_string) {
|
||||
ret = krb5_cc_resolve(heimtools_context, opt->cache_string, &id);
|
||||
@@ -618,7 +667,7 @@ klist(struct klist_options *opt, int argc, char **argv)
|
||||
}
|
||||
exit_status = display_v5_ccache(heimtools_context, id, do_test,
|
||||
do_verbose, opt->flags_flag,
|
||||
opt->hidden_flag);
|
||||
opt->hidden_flag, opt->json_flag);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user