575f96d340fcbeccacff85e87d7907f32a878afc
[asn/mit-krb5.git] / src / tests / gssapi / t_credstore.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2011 Red Hat, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation files
7  * (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "common.h"
31
32 static void
33 usage(void)
34 {
35     fprintf(stderr,
36             "Usage: t_credstore [-sabi] principal [{key value} ...]\n");
37     exit(1);
38 }
39
40 int
41 main(int argc, char *argv[])
42 {
43     OM_uint32 minor, major;
44     gss_key_value_set_desc store;
45     gss_name_t name;
46     gss_cred_usage_t cred_usage = GSS_C_BOTH;
47     gss_OID_set mechs = GSS_C_NO_OID_SET;
48     gss_cred_id_t cred = GSS_C_NO_CREDENTIAL;
49     krb5_boolean store_creds = FALSE;
50     char opt;
51
52     /* Parse options. */
53     for (argv++; *argv != NULL && **argv == '-'; argv++) {
54         opt = (*argv)[1];
55         if (opt == 's')
56             store_creds = TRUE;
57         else if (opt == 'a')
58             cred_usage = GSS_C_ACCEPT;
59         else if (opt == 'b')
60             cred_usage = GSS_C_BOTH;
61         else if (opt == 'i')
62             cred_usage = GSS_C_INITIATE;
63         else
64             usage();
65     }
66
67     /* Get the principal name. */
68     if (*argv == NULL)
69         usage();
70     name = import_name(*argv++);
71
72     /* Put any remaining arguments into the store. */
73     store.elements = calloc(argc, sizeof(struct gss_key_value_element_struct));
74     if (!store.elements)
75         errout("OOM");
76     store.count = 0;
77     while (*argv != NULL) {
78         if (*(argv + 1) == NULL)
79             usage();
80         store.elements[store.count].key = *argv;
81         store.elements[store.count].value = *(argv + 1);
82         store.count++;
83         argv += 2;
84     }
85
86     if (store_creds) {
87         /* Acquire default creds and try to store them in the cred store. */
88         major = gss_acquire_cred(&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET,
89                                  GSS_C_INITIATE, &cred, NULL, NULL);
90         check_gsserr("gss_acquire_cred", major, minor);
91
92         major = gss_store_cred_into(&minor, cred, GSS_C_INITIATE,
93                                     GSS_C_NO_OID, 1, 0, &store, NULL, NULL);
94         check_gsserr("gss_store_cred_into", major, minor);
95
96         gss_release_cred(&minor, &cred);
97     }
98
99     /* Try to acquire creds from store. */
100     major = gss_acquire_cred_from(&minor, name, 0, mechs, cred_usage,
101                                   &store, &cred, NULL, NULL);
102     check_gsserr("gss_acquire_cred_from", major, minor);
103
104     gss_release_name(&minor, &name);
105     gss_release_cred(&minor, &cred);
106     free(store.elements);
107     return 0;
108 }