klist: partial fix of -v --json
authorNicolas Williams <nico@twosigma.com>
Mon, 1 Jul 2019 01:44:05 +0000 (20:44 -0500)
committerNicolas Williams <nico@twosigma.com>
Thu, 3 Oct 2019 18:09:18 +0000 (13:09 -0500)
It's still not supported, but at least it generates valid JSON now.

Note that --json w/o -v is functional but has issues, specifically the
rtbl_add_column_entry() does not encode strings properly for output as JSON
strings.  Fixing this will take significantly more effort.  One wonders if it
might not be better to use a proper JSON library instead of the rtbl stuff.

kuser/klist.c

index c1b4cd56daa278d6cb306c1368c0dea9acb18624..955ca139beb239e80d0ae75037f7f3e1d032823b 100644 (file)
@@ -266,6 +266,7 @@ print_tickets (krb5_context context,
     krb5_cc_cursor cursor;
     krb5_creds creds;
     krb5_deltat sec;
+    int print_comma = 0;
 
     rtbl_t ct = NULL;
 
@@ -339,19 +340,20 @@ print_tickets (krb5_context context,
     }
     if (do_verbose && do_json)
        printf("\"tickets\" : [");
-    while ((ret = krb5_cc_next_cred (context,
-                                    ccache,
-                                    &cursor,
-                                    &creds)) == 0) {
+    while ((ret = krb5_cc_next_cred(context, ccache, &cursor, &creds)) == 0) {
        if (!do_hidden && krb5_is_config_principal(context, creds.server)) {
            ;
-       }else if(do_verbose){
+       } else if (do_verbose) {
+            if (do_json && print_comma)
+                printf(",");
            print_cred_verbose(context, &creds, do_json);
-       }else{
+            print_comma = 1;
+       } else {
            print_cred(context, &creds, ct, do_flags);
        }
-       krb5_free_cred_contents (context, &creds);
+       krb5_free_cred_contents(context, &creds);
     }
+    print_comma = 0;
     if(ret != KRB5_CC_END)
        krb5_err(context, 1, ret, "krb5_cc_get_next");
     ret = krb5_cc_end_seq_get (context, ccache, &cursor);