1/*
2 * Copyright (c) 2003-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of KTH nor the names of its contributors may be
18 *    used to endorse or promote products derived from this software without
19 *    specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "gsskrb5_locl.h"
35#include <err.h>
36
37static void
38print_time(OM_uint32 time_rec)
39{
40    if (time_rec == GSS_C_INDEFINITE) {
41	printf("cred never expire\n");
42    } else {
43	time_t t = time_rec + time(NULL);
44	printf("expiration time: %s", ctime(&t));
45    }
46}
47
48static void
49test_add(gss_cred_id_t cred_handle)
50{
51    OM_uint32 major_status, minor_status;
52    gss_cred_id_t copy_cred;
53    OM_uint32 time_rec;
54
55    major_status = gss_add_cred (&minor_status,
56				 cred_handle,
57				 GSS_C_NO_NAME,
58				 GSS_KRB5_MECHANISM,
59				 GSS_C_INITIATE,
60				 0,
61				 0,
62				 &copy_cred,
63				 NULL,
64				 &time_rec,
65				 NULL);
66
67    if (GSS_ERROR(major_status))
68	errx(1, "add_cred failed");
69
70    print_time(time_rec);
71
72    major_status = gss_release_cred(&minor_status,
73				    &copy_cred);
74    if (GSS_ERROR(major_status))
75	errx(1, "release_cred failed");
76}
77
78static void
79copy_cred(void)
80{
81    OM_uint32 major_status, minor_status;
82    gss_cred_id_t cred_handle;
83    OM_uint32 time_rec;
84
85    major_status = gss_acquire_cred(&minor_status,
86				    GSS_C_NO_NAME,
87				    0,
88				    NULL,
89				    GSS_C_INITIATE,
90				    &cred_handle,
91				    NULL,
92				    &time_rec);
93    if (GSS_ERROR(major_status))
94	errx(1, "acquire_cred failed");
95
96    print_time(time_rec);
97
98    test_add(cred_handle);
99    test_add(cred_handle);
100    test_add(cred_handle);
101
102    major_status = gss_release_cred(&minor_status,
103				    &cred_handle);
104    if (GSS_ERROR(major_status))
105	errx(1, "release_cred failed");
106}
107
108static void
109acquire_cred_service(const char *service)
110{
111    OM_uint32 major_status, minor_status;
112    gss_cred_id_t cred_handle;
113    OM_uint32 time_rec;
114    gss_buffer_desc name_buffer;
115    gss_name_t name;
116
117    name_buffer.value = rk_UNCONST(service);
118    name_buffer.length = strlen(service);
119
120    major_status = gss_import_name(&minor_status,
121				   &name_buffer,
122				   GSS_C_NT_HOSTBASED_SERVICE,
123				   &name);
124    if (GSS_ERROR(major_status))
125	errx(1, "import_name failed");
126
127
128    major_status = gss_acquire_cred(&minor_status,
129				    name,
130				    0,
131				    NULL,
132				    GSS_C_ACCEPT,
133				    &cred_handle,
134				    NULL,
135				    &time_rec);
136    if (GSS_ERROR(major_status))
137	errx(1, "acquire_cred failed");
138
139    print_time(time_rec);
140
141    major_status = gss_release_cred(&minor_status,
142				    &cred_handle);
143    if (GSS_ERROR(major_status))
144	errx(1, "release_cred failed");
145
146
147    major_status = gss_release_name(&minor_status,
148				    &name);
149    if (GSS_ERROR(major_status))
150	errx(1, "release_name failed");
151
152}
153
154int
155main(int argc, char **argv)
156{
157    copy_cred();
158
159    acquire_cred_service("host@xen2-heimdal-linux.lab.it.su.se");
160
161    return 0;
162}
163