1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* tests/conccache.c - ccache concurrent get_creds/refresh test program */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert * Copyright (C) 2021 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert * All rights reserved.
6*7f2fe78bSCy Schubert *
7*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert * are met:
10*7f2fe78bSCy Schubert *
11*7f2fe78bSCy Schubert * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert *
14*7f2fe78bSCy Schubert * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert * the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert * distribution.
18*7f2fe78bSCy Schubert *
19*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert */
32*7f2fe78bSCy Schubert
33*7f2fe78bSCy Schubert /*
34*7f2fe78bSCy Schubert * Usage: conccache ccname clientprinc serverprinc
35*7f2fe78bSCy Schubert *
36*7f2fe78bSCy Schubert * This program spawns two subprocesses. One repeatedly runs
37*7f2fe78bSCy Schubert * krb5_get_credentials() on ccname, and the other repeatedly refreshes ccname
38*7f2fe78bSCy Schubert * from the default keytab. If either subprocess fails, the program exits with
39*7f2fe78bSCy Schubert * status 1. The goal is to expose time windows where cache refreshes cause
40*7f2fe78bSCy Schubert * get_cred operations to fail.
41*7f2fe78bSCy Schubert */
42*7f2fe78bSCy Schubert
43*7f2fe78bSCy Schubert #include "k5-platform.h"
44*7f2fe78bSCy Schubert #include <sys/types.h>
45*7f2fe78bSCy Schubert #include <sys/wait.h>
46*7f2fe78bSCy Schubert #include <pthread.h>
47*7f2fe78bSCy Schubert #include <krb5.h>
48*7f2fe78bSCy Schubert
49*7f2fe78bSCy Schubert /* Run this many iterations of each operation. */
50*7f2fe78bSCy Schubert static const int iterations = 200;
51*7f2fe78bSCy Schubert
52*7f2fe78bSCy Schubert /* Saved command-line arguments. */
53*7f2fe78bSCy Schubert static const char *ccname, *server_name, *client_name;
54*7f2fe78bSCy Schubert
55*7f2fe78bSCy Schubert static void
check(krb5_error_code code)56*7f2fe78bSCy Schubert check(krb5_error_code code)
57*7f2fe78bSCy Schubert {
58*7f2fe78bSCy Schubert if (code)
59*7f2fe78bSCy Schubert abort();
60*7f2fe78bSCy Schubert }
61*7f2fe78bSCy Schubert
62*7f2fe78bSCy Schubert static krb5_boolean
get_cred(krb5_context context)63*7f2fe78bSCy Schubert get_cred(krb5_context context)
64*7f2fe78bSCy Schubert {
65*7f2fe78bSCy Schubert krb5_error_code ret;
66*7f2fe78bSCy Schubert krb5_ccache cc;
67*7f2fe78bSCy Schubert krb5_principal client, server;
68*7f2fe78bSCy Schubert krb5_creds mcred, *cred;
69*7f2fe78bSCy Schubert
70*7f2fe78bSCy Schubert check(krb5_cc_resolve(context, ccname, &cc));
71*7f2fe78bSCy Schubert check(krb5_parse_name(context, client_name, &client));
72*7f2fe78bSCy Schubert check(krb5_parse_name(context, server_name, &server));
73*7f2fe78bSCy Schubert
74*7f2fe78bSCy Schubert memset(&mcred, 0, sizeof(mcred));
75*7f2fe78bSCy Schubert mcred.client = client;
76*7f2fe78bSCy Schubert mcred.server = server;
77*7f2fe78bSCy Schubert ret = krb5_get_credentials(context, 0, cc, &mcred, &cred);
78*7f2fe78bSCy Schubert
79*7f2fe78bSCy Schubert krb5_free_creds(context, cred);
80*7f2fe78bSCy Schubert krb5_free_principal(context, client);
81*7f2fe78bSCy Schubert krb5_free_principal(context, server);
82*7f2fe78bSCy Schubert krb5_cc_close(context, cc);
83*7f2fe78bSCy Schubert
84*7f2fe78bSCy Schubert return ret == 0;
85*7f2fe78bSCy Schubert }
86*7f2fe78bSCy Schubert
87*7f2fe78bSCy Schubert static krb5_boolean
refresh_cache(krb5_context context)88*7f2fe78bSCy Schubert refresh_cache(krb5_context context)
89*7f2fe78bSCy Schubert {
90*7f2fe78bSCy Schubert krb5_error_code ret;
91*7f2fe78bSCy Schubert krb5_ccache cc;
92*7f2fe78bSCy Schubert krb5_principal client;
93*7f2fe78bSCy Schubert krb5_get_init_creds_opt *opt;
94*7f2fe78bSCy Schubert krb5_creds cred;
95*7f2fe78bSCy Schubert
96*7f2fe78bSCy Schubert check(krb5_cc_resolve(context, ccname, &cc));
97*7f2fe78bSCy Schubert check(krb5_parse_name(context, client_name, &client));
98*7f2fe78bSCy Schubert
99*7f2fe78bSCy Schubert check(krb5_get_init_creds_opt_alloc(context, &opt));
100*7f2fe78bSCy Schubert check(krb5_get_init_creds_opt_set_out_ccache(context, opt, cc));
101*7f2fe78bSCy Schubert ret = krb5_get_init_creds_keytab(context, &cred, client, NULL, 0, NULL,
102*7f2fe78bSCy Schubert opt);
103*7f2fe78bSCy Schubert
104*7f2fe78bSCy Schubert krb5_get_init_creds_opt_free(context, opt);
105*7f2fe78bSCy Schubert krb5_free_cred_contents(context, &cred);
106*7f2fe78bSCy Schubert krb5_free_principal(context, client);
107*7f2fe78bSCy Schubert krb5_cc_close(context, cc);
108*7f2fe78bSCy Schubert
109*7f2fe78bSCy Schubert return ret == 0;
110*7f2fe78bSCy Schubert }
111*7f2fe78bSCy Schubert
112*7f2fe78bSCy Schubert static pid_t
spawn_cred_subprocess()113*7f2fe78bSCy Schubert spawn_cred_subprocess()
114*7f2fe78bSCy Schubert {
115*7f2fe78bSCy Schubert krb5_context context;
116*7f2fe78bSCy Schubert pid_t pid;
117*7f2fe78bSCy Schubert int i;
118*7f2fe78bSCy Schubert
119*7f2fe78bSCy Schubert pid = fork();
120*7f2fe78bSCy Schubert assert(pid >= 0);
121*7f2fe78bSCy Schubert if (pid > 0)
122*7f2fe78bSCy Schubert return pid;
123*7f2fe78bSCy Schubert
124*7f2fe78bSCy Schubert check(krb5_init_context(&context));
125*7f2fe78bSCy Schubert for (i = 0; i < iterations; i++) {
126*7f2fe78bSCy Schubert if (!get_cred(context)) {
127*7f2fe78bSCy Schubert fprintf(stderr, "cred worker failed after %d successes\n", i);
128*7f2fe78bSCy Schubert exit(1);
129*7f2fe78bSCy Schubert }
130*7f2fe78bSCy Schubert }
131*7f2fe78bSCy Schubert krb5_free_context(context);
132*7f2fe78bSCy Schubert exit(0);
133*7f2fe78bSCy Schubert }
134*7f2fe78bSCy Schubert
135*7f2fe78bSCy Schubert static pid_t
spawn_refresh_subprocess()136*7f2fe78bSCy Schubert spawn_refresh_subprocess()
137*7f2fe78bSCy Schubert {
138*7f2fe78bSCy Schubert krb5_context context;
139*7f2fe78bSCy Schubert pid_t pid;
140*7f2fe78bSCy Schubert int i;
141*7f2fe78bSCy Schubert
142*7f2fe78bSCy Schubert pid = fork();
143*7f2fe78bSCy Schubert assert(pid >= 0);
144*7f2fe78bSCy Schubert if (pid > 0)
145*7f2fe78bSCy Schubert return pid;
146*7f2fe78bSCy Schubert
147*7f2fe78bSCy Schubert check(krb5_init_context(&context));
148*7f2fe78bSCy Schubert for (i = 0; i < iterations; i++) {
149*7f2fe78bSCy Schubert if (!refresh_cache(context)) {
150*7f2fe78bSCy Schubert fprintf(stderr, "refresh worker failed after %d successes\n", i);
151*7f2fe78bSCy Schubert exit(1);
152*7f2fe78bSCy Schubert }
153*7f2fe78bSCy Schubert }
154*7f2fe78bSCy Schubert krb5_free_context(context);
155*7f2fe78bSCy Schubert exit(0);
156*7f2fe78bSCy Schubert }
157*7f2fe78bSCy Schubert
158*7f2fe78bSCy Schubert int
main(int argc,char * argv[])159*7f2fe78bSCy Schubert main(int argc, char *argv[])
160*7f2fe78bSCy Schubert {
161*7f2fe78bSCy Schubert krb5_context context;
162*7f2fe78bSCy Schubert pid_t cred_pid, refresh_pid, pid;
163*7f2fe78bSCy Schubert int cstatus, rstatus;
164*7f2fe78bSCy Schubert
165*7f2fe78bSCy Schubert assert(argc == 4);
166*7f2fe78bSCy Schubert ccname = argv[1];
167*7f2fe78bSCy Schubert client_name = argv[2];
168*7f2fe78bSCy Schubert server_name = argv[3];
169*7f2fe78bSCy Schubert
170*7f2fe78bSCy Schubert /* Begin with an initialized cache. */
171*7f2fe78bSCy Schubert check(krb5_init_context(&context));
172*7f2fe78bSCy Schubert refresh_cache(context);
173*7f2fe78bSCy Schubert krb5_free_context(context);
174*7f2fe78bSCy Schubert
175*7f2fe78bSCy Schubert cred_pid = spawn_cred_subprocess();
176*7f2fe78bSCy Schubert refresh_pid = spawn_refresh_subprocess();
177*7f2fe78bSCy Schubert
178*7f2fe78bSCy Schubert pid = waitpid(cred_pid, &cstatus, 0);
179*7f2fe78bSCy Schubert if (pid == -1)
180*7f2fe78bSCy Schubert abort();
181*7f2fe78bSCy Schubert pid = waitpid(refresh_pid, &rstatus, 0);
182*7f2fe78bSCy Schubert if (pid == -1)
183*7f2fe78bSCy Schubert abort();
184*7f2fe78bSCy Schubert
185*7f2fe78bSCy Schubert if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus) != 0)
186*7f2fe78bSCy Schubert return 1;
187*7f2fe78bSCy Schubert if (!WIFEXITED(rstatus) || WEXITSTATUS(rstatus) != 0)
188*7f2fe78bSCy Schubert return 1;
189*7f2fe78bSCy Schubert return 0;
190*7f2fe78bSCy Schubert }
191