Removed (not needed).
[krb5dissect.git] / krb5dissect.c
blob1f644db3571030172de1d7765c35c76fbe16e4fb
1 /* krb5dissect.c --- Dissect Kerberos ccache/keytab files.
2 * Copyright (C) 2007 Simon Josefsson.
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
7 * any later version.
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this file; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 /* Autoconf definitions. */
21 #include <config.h>
23 /* Standard headers. */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <unistd.h>
29 /* Gnulib headers. */
30 #include "progname.h"
31 #include "version-etc.h"
32 #include "read-file.h"
33 #include "error.h"
34 #include "xvasprintf.h"
36 /* Our headers. */
37 #include "ccache.h"
38 #include "keytab.h"
39 #include "krb5dissect_cmd.h"
41 const char version_etc_copyright[] = "Copyright %s %d Simon Josefsson.";
43 void
44 parse_ccache (const char *data, size_t len)
46 struct ccache ccache;
47 struct ccache_credential cred;
48 int rc;
49 size_t i = 0;
51 rc = ccache_parse (data, len, &ccache);
52 if (rc < 0)
53 error (EXIT_FAILURE, 0, "Cannot parse ccache");
55 ccache_print (&ccache);
57 while (ccache.credentialslen)
59 size_t n;
61 rc = ccache_parse_credential (ccache.credentials,
62 ccache.credentialslen, &cred, &n);
63 if (rc < 0)
64 error (EXIT_FAILURE, 0, "Cannot parse ccache entry %d", i);
66 printf ("\nCcache entry %d:\n", i++);
68 ccache_print_credential (&cred);
70 ccache.credentials += n;
71 ccache.credentialslen -= n;
75 void
76 parse_keytab (const char *data, size_t len)
78 struct keytab keytab;
79 struct keytab_entry entry;
80 int rc;
81 size_t i = 0;
83 rc = keytab_parse (data, len, &keytab);
84 if (rc < 0)
85 error (EXIT_FAILURE, 0, "Cannot parse keytab");
87 keytab_print (&keytab);
89 while (keytab.keytabslen)
91 size_t n;
93 rc = keytab_parse_entry (keytab.keytabs,
94 keytab.keytabslen, &entry, &n);
95 if (rc < 0)
96 error (EXIT_FAILURE, 0, "Cannot parse keytab entry %d", i);
98 printf ("\nKeytab entry %d:\n", i++);
100 keytab_print_entry (&entry);
102 keytab.keytabs += n;
103 keytab.keytabslen -= n;
108 main (int argc, char *argv[])
110 struct gengetopt_args_info args_info;
111 int free_filename = 0;
112 char *filename;
113 size_t len;
114 char *data;
116 set_program_name (argv[0]);
118 if (cmdline_parser (argc, argv, &args_info) != 0)
119 return EXIT_FAILURE;
121 if (!args_info.quiet_given)
123 version_etc (stdout, program_name, PACKAGE, VERSION,
124 "Simon Josefsson", (char*) NULL);
125 puts("");
128 if (args_info.keytab_given)
129 filename = "/etc/krb5.keytab";
130 else if (args_info.ccache_given)
132 filename = xasprintf("/tmp/krb5cc_%u", (unsigned long) getuid ());
133 free_filename = 1;
135 else if (args_info.inputs_num == 1)
136 filename = args_info.inputs[0];
137 else
139 cmdline_parser_print_help ();
140 return EXIT_FAILURE;
143 cmdline_parser_free (&args_info);
145 if (strcmp (filename, "-") == 0)
146 data = fread_file (stdin, &len);
147 else
148 data = read_binary_file (filename, &len);
149 if (!data)
150 error (EXIT_FAILURE, errno, "%s", filename);
152 if (free_filename)
153 free (filename);
155 if (len > 2 && data[0] == '\x05' && data[1] == '\x04')
156 parse_ccache (data, len);
157 else
158 parse_keytab (data, len);
160 free (data);
162 return 0;