1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/gssapi/t_store_cred.c - gss_store_cred() test harness */
3 /*
4 * Copyright (C) 2021 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Usage: t_store_cred [-d] [-i] [-o] src_ccname [dest_ccname]
35 *
36 * Acquires creds from src_ccname using gss_acquire_cred_from() and then stores
37 * them, using gss_store_cred_into() if -i is specified or gss_store_cred()
38 * otherwise. If dest_ccname is specified with -i, it is included in the cred
39 * store for the store operation; if it is specified without -i, it is set with
40 * gss_krb5_ccache_name() before the store operation. If -d and/or -o are
41 * specified they set the default_cred and overwrite_cred flags to true
42 * respectively.
43 */
44
45 #include "k5-platform.h"
46 #include <gssapi/gssapi_ext.h>
47 #include "common.h"
48
49 int
main(int argc,char * argv[])50 main(int argc, char *argv[])
51 {
52 OM_uint32 major, minor;
53 gss_key_value_set_desc store;
54 gss_key_value_element_desc elem;
55 gss_cred_id_t cred;
56 krb5_boolean def = FALSE, into = FALSE, overwrite = FALSE;
57 const char *src_ccname, *dest_ccname;
58 int c;
59
60 /* Parse arguments. */
61 while ((c = getopt(argc, argv, "dio")) != -1) {
62 switch (c) {
63 case 'd':
64 def = TRUE;
65 break;
66 case 'i':
67 into = TRUE;
68 break;
69 case 'o':
70 overwrite = TRUE;
71 break;
72 default:
73 abort();
74 }
75 }
76 argc -= optind;
77 argv += optind;
78 assert(argc == 1 || argc == 2);
79 src_ccname = argv[0];
80 dest_ccname = argv[1];
81
82 elem.key = "ccache";
83 elem.value = src_ccname;
84 store.count = 1;
85 store.elements = &elem;
86 major = gss_acquire_cred_from(&minor, GSS_C_NO_NAME, GSS_C_INDEFINITE,
87 &mechset_krb5, GSS_C_INITIATE, &store, &cred,
88 NULL, NULL);
89 check_gsserr("acquire_cred", major, minor);
90
91 if (into) {
92 if (dest_ccname != NULL) {
93 elem.key = "ccache";
94 elem.value = dest_ccname;
95 store.count = 1;
96 } else {
97 store.count = 0;
98 }
99 major = gss_store_cred_into(&minor, cred, GSS_C_INITIATE, &mech_krb5,
100 overwrite, def, &store, NULL, NULL);
101 check_gsserr("store_cred_into", major, minor);
102 } else {
103 if (dest_ccname != NULL) {
104 major = gss_krb5_ccache_name(&minor, dest_ccname, NULL);
105 check_gsserr("ccache_name", major, minor);
106 }
107 major = gss_store_cred(&minor, cred, GSS_C_INITIATE, &mech_krb5,
108 overwrite, def, NULL, NULL);
109 check_gsserr("store_cred", major, minor);
110 }
111
112 gss_release_cred(&minor, &cred);
113 return 0;
114 }
115