10ea35aa2425a6e6762777f4bed8658f6de3f733
[samba.git] / source4 / heimdal / kdc / test_token_validator.c
1 #include "kdc_locl.h"
2
3 static int help_flag;
4 static int version_flag;
5 static char *realm;
6 static char *app;
7 static struct getarg_strings audiences;
8
9 struct getargs args[] = {
10     {   "app",          'A',    arg_string,    &app,
11         "app name (krb5.conf section)", "APP-NAME" },
12     {   "help",         'h',    arg_flag,    &help_flag,
13         "Print usage message", NULL },
14     {   NULL,           'r',    arg_string,  &realm,
15         "Realm name for plugin configuration", "REALM" },
16     {   NULL,           'a',    arg_strings, &audiences,
17         "expected token acceptor audience (hostname)", "ACCEPTOR-HOSTNAME" },
18     {   "version",      'v',    arg_flag,    &version_flag, "Print version", NULL }
19 };
20 size_t num_args = sizeof(args) / sizeof(args[0]);
21
22 static int
23 usage(int e)
24 {
25     arg_printusage(args, num_args, NULL, "TOKEN-TYPE TOKEN");
26     exit(e);
27     return e;
28 }
29
30 static const char *sysplugin_dirs[] =  {
31 #ifdef _WIN32
32     "$ORIGIN",
33 #else
34     "$ORIGIN/../lib/plugin/kdc",
35 #endif
36 #ifdef __APPLE__
37     LIBDIR "/plugin/kdc",
38 #endif
39     NULL
40 };
41
42 static void
43 load_plugins(krb5_context context)
44 {
45     const char * const *dirs = sysplugin_dirs;
46 #ifndef _WIN32
47     char **cfdirs;
48
49     cfdirs = krb5_config_get_strings(context, NULL, "kdc", "plugin_dir", NULL);
50     if (cfdirs)
51         dirs = (const char * const *)cfdirs;
52 #endif
53
54     _krb5_load_plugins(context, "kdc", (const char **)dirs);
55
56 #ifndef _WIN32
57     krb5_config_free_strings(cfdirs);
58 #endif
59 }
60
61 int
62 main(int argc, char **argv)
63 {
64     krb5_error_code ret;
65     krb5_context context;
66     krb5_data token;
67     const char *token_type;
68     krb5_principal actual_princ = NULL;
69     krb5_times token_times;
70     size_t bufsz = 0;
71     char *buf = NULL;
72     char *s = NULL;
73     int optidx = 0;
74
75     setprogname(argv[0]);
76     if (getarg(args, num_args, argc, argv, &optidx))
77         return usage(1);
78     if (help_flag)
79         return usage(0);
80     if (version_flag) {
81         print_version(argv[0]);
82         return 0;
83     }
84
85     argc -= optidx;
86     argv += optidx;
87
88     if (argc != 2)
89         usage(1);
90
91     if ((ret = krb5_init_context(&context)))
92         err(1, "Could not initialize krb5_context");
93
94     load_plugins(context);
95
96     token_type = argv[0];
97     token.data = argv[1];
98     if (strcmp(token.data, "-") == 0) {
99         if (getline(&buf, &bufsz, stdin) < 0)
100             err(1, "Could not read token from stdin");
101         token.length = bufsz;
102         token.data = buf;
103     } else {
104         token.length = strlen(token.data);
105     }
106     if ((ret = kdc_validate_token(context, realm, token_type, &token,
107                                   (const char * const *)audiences.strings,
108                                   audiences.num_strings, &actual_princ,
109                                   &token_times)))
110         krb5_err(context, 1, ret, "Could not validate %s token", token_type);
111     if (actual_princ && (ret = krb5_unparse_name(context, actual_princ, &s)))
112         krb5_err(context, 1, ret, "Could not display principal name");
113     if (s)
114         printf("Token is valid.  Actual principal: %s\n", s);
115     else
116         printf("Token is valid.");
117     _krb5_unload_plugins(context, "kdc");
118     krb5_free_principal(context, actual_princ);
119     krb5_free_context(context);
120     return 0;
121 }