Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/krb5/src/lib/krad/t_remote.c
39536 views
1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* lib/krad/t_remote.c - Protocol test program */
3
/*
4
* Copyright 2013 Red Hat, Inc. 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 are met:
8
*
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
*
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in
14
* the documentation and/or other materials provided with the
15
* distribution.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include "t_daemon.h"
31
32
#define EVENT_COUNT 6
33
34
static struct
35
{
36
int count;
37
struct event events[EVENT_COUNT];
38
} record;
39
40
static krad_attrset *set;
41
static krad_remote *rr;
42
static verto_ctx *vctx;
43
44
static void
45
callback(krb5_error_code retval, const krad_packet *request,
46
const krad_packet *response, void *data)
47
{
48
struct event *evt;
49
50
evt = &record.events[record.count++];
51
evt->error = retval != 0;
52
if (evt->error)
53
evt->result.retval = retval;
54
else
55
evt->result.code = krad_packet_get_code(response);
56
verto_break(vctx);
57
}
58
59
static void
60
remote_new(krb5_context kctx, krad_remote **remote)
61
{
62
struct addrinfo *ai = NULL, hints;
63
64
memset(&hints, 0, sizeof(hints));
65
hints.ai_family = AF_INET;
66
hints.ai_socktype = SOCK_DGRAM;
67
noerror(gai_error_code(getaddrinfo("127.0.0.1", "radius", &hints, &ai)));
68
69
noerror(kr_remote_new(kctx, vctx, ai, "foo", remote));
70
insist(kr_remote_equals(*remote, ai, "foo"));
71
freeaddrinfo(ai);
72
}
73
74
static krb5_error_code
75
do_auth(const char *password, const krad_packet **pkt)
76
{
77
const krad_packet *tmppkt;
78
krb5_error_code retval;
79
krb5_data tmp = string2data((char *)password);
80
81
retval = krad_attrset_add(set, KRAD_ATTR_USER_PASSWORD, &tmp);
82
if (retval != 0)
83
return retval;
84
85
retval = kr_remote_send(rr, KRAD_CODE_ACCESS_REQUEST, set, callback, NULL,
86
1000, 3, &tmppkt);
87
krad_attrset_del(set, KRAD_ATTR_USER_PASSWORD, 0);
88
if (retval != 0)
89
return retval;
90
91
if (pkt != NULL)
92
*pkt = tmppkt;
93
return 0;
94
}
95
96
static void
97
test_timeout(verto_ctx *ctx, verto_ev *ev)
98
{
99
static const krad_packet *pkt;
100
101
noerror(do_auth("accept", &pkt));
102
kr_remote_cancel(rr, pkt);
103
}
104
105
int
106
main(int argc, const char **argv)
107
{
108
krb5_context kctx = NULL;
109
krb5_data tmp;
110
111
if (!daemon_start(argc, argv)) {
112
fprintf(stderr, "Unable to start pyrad daemon, skipping test...\n");
113
return 0;
114
}
115
116
/* Initialize. */
117
noerror(krb5_init_context(&kctx));
118
vctx = verto_new(NULL, VERTO_EV_TYPE_IO | VERTO_EV_TYPE_TIMEOUT);
119
insist(vctx != NULL);
120
remote_new(kctx, &rr);
121
122
/* Create attribute set. */
123
noerror(krad_attrset_new(kctx, &set));
124
tmp = string2data("testUser");
125
noerror(krad_attrset_add(set, KRAD_ATTR_USER_NAME, &tmp));
126
127
/* Send accept packet. */
128
noerror(do_auth("accept", NULL));
129
verto_run(vctx);
130
131
/* Send reject packet. */
132
noerror(do_auth("reject", NULL));
133
verto_run(vctx);
134
135
/* Send canceled packet. */
136
insist(verto_add_timeout(vctx, VERTO_EV_FLAG_NONE, test_timeout, 0) !=
137
NULL);
138
verto_run(vctx);
139
140
/* Test timeout. */
141
daemon_stop();
142
noerror(do_auth("accept", NULL));
143
verto_run(vctx);
144
145
/* Test outstanding packet freeing. */
146
noerror(do_auth("accept", NULL));
147
kr_remote_free(rr);
148
krad_attrset_free(set);
149
150
/* Verify the results. */
151
insist(record.count == EVENT_COUNT);
152
insist(record.events[0].error == FALSE);
153
insist(record.events[0].result.code == KRAD_CODE_ACCESS_ACCEPT);
154
insist(record.events[1].error == FALSE);
155
insist(record.events[1].result.code == KRAD_CODE_ACCESS_REJECT);
156
insist(record.events[2].error == TRUE);
157
insist(record.events[2].result.retval == ECANCELED);
158
insist(record.events[3].error == TRUE);
159
insist(record.events[3].result.retval == ETIMEDOUT);
160
insist(record.events[4].error == TRUE);
161
insist(record.events[4].result.retval == ECANCELED);
162
insist(record.events[5].error == TRUE);
163
insist(record.events[5].result.retval == ECANCELED);
164
165
verto_free(vctx);
166
krb5_free_context(kctx);
167
return 0;
168
}
169
170