1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* t_std_conf.c --- This program tests standard Krb5 routines which pull
* values from the krb5 config file(s).
*/
#include "fake-addrinfo.h"
#include "k5-int.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "os-proto.h"
static void test_get_default_realm(krb5_context ctx)
{
char *realm;
krb5_error_code retval;
retval = krb5_get_default_realm(ctx, &realm);
if (retval) {
com_err("krb5_get_default_realm", retval, 0);
return;
}
printf("krb5_get_default_realm() returned '%s'\n", realm);
free(realm);
}
static void test_set_default_realm(krb5_context ctx, char *realm)
{
krb5_error_code retval;
retval = krb5_set_default_realm(ctx, realm);
if (retval) {
com_err("krb5_set_default_realm", retval, 0);
return;
}
printf("krb5_set_default_realm(%s)\n", realm);
}
static void test_get_default_ccname(krb5_context ctx)
{
const char *ccname;
ccname = krb5_cc_default_name(ctx);
if (ccname)
printf("krb5_cc_default_name() returned '%s'\n", ccname);
else
printf("krb5_cc_default_name() returned NULL\n");
}
static void test_set_default_ccname(krb5_context ctx, char *ccname)
{
krb5_error_code retval;
retval = krb5_cc_set_default_name(ctx, ccname);
if (retval) {
com_err("krb5_set_default_ccname", retval, 0);
return;
}
printf("krb5_set_default_ccname(%s)\n", ccname);
}
static void test_get_krbhst(krb5_context ctx, char *realm)
{
char **hostlist, **cpp;
krb5_data rlm;
krb5_error_code retval;
rlm.data = realm;
rlm.length = strlen(realm);
retval = krb5_get_krbhst(ctx, &rlm, &hostlist);
if (retval) {
com_err("krb5_get_krbhst", retval, 0);
return;
}
printf("krb_get_krbhst(%s) returned:", realm);
if (hostlist == 0) {
printf(" (null)\n");
return;
}
if (hostlist[0] == 0) {
printf(" (none)\n");
krb5_free_krbhst(ctx, hostlist);
return;
}
for (cpp = hostlist; *cpp; cpp++) {
printf(" '%s'", *cpp);
}
krb5_free_krbhst(ctx, hostlist);
printf("\n");
}
static void test_locate_kdc(krb5_context ctx, char *realm)
{
struct addrlist addrs;
int i;
int get_masters=0;
krb5_data rlm;
krb5_error_code retval;
rlm.data = realm;
rlm.length = strlen(realm);
retval = krb5_locate_kdc(ctx, &rlm, &addrs, get_masters, 0, 0);
if (retval) {
com_err("krb5_locate_kdc", retval, 0);
return;
}
printf("krb_locate_kdc(%s) returned:", realm);
for (i=0; i < addrs.naddrs; i++) {
struct addrinfo *ai = addrs.addrs[i].ai;
switch (ai->ai_family) {
case AF_INET:
{
struct sockaddr_in *s_sin;
s_sin = (struct sockaddr_in *) ai->ai_addr;
printf(" inet:%s/%d", inet_ntoa(s_sin->sin_addr),
ntohs(s_sin->sin_port));
}
break;
#ifdef KRB5_USE_INET6
case AF_INET6:
{
struct sockaddr_in6 *s_sin6;
int j;
s_sin6 = (struct sockaddr_in6 *) ai->ai_addr;
printf(" inet6");
for (j = 0; j < 8; j++)
printf(":%x",
(s_sin6->sin6_addr.s6_addr[2*j] * 256
+ s_sin6->sin6_addr.s6_addr[2*j+1]));
printf("/%d", ntohs(s_sin6->sin6_port));
break;
}
#endif
default:
printf(" unknown-af-%d", ai->ai_family);
break;
}
}
krb5int_free_addrlist(&addrs);
printf("\n");
}
static void test_get_host_realm(krb5_context ctx, char *host)
{
char **realms, **cpp;
krb5_error_code retval;
retval = krb5_get_host_realm(ctx, host, &realms);
if (retval) {
com_err("krb5_get_host_realm", retval, 0);
return;
}
printf("krb_get_host_realm(%s) returned:", host);
if (realms == 0) {
printf(" (null)\n");
return;
}
if (realms[0] == 0) {
printf(" (none)\n");
free(realms);
return;
}
for (cpp = realms; *cpp; cpp++) {
printf(" '%s'", *cpp);
free(*cpp);
}
free(realms);
printf("\n");
}
static void test_get_realm_domain(krb5_context ctx, char *realm)
{
krb5_error_code retval;
char *domain;
retval = krb5_get_realm_domain(ctx, realm, &domain);
if (retval) {
com_err("krb5_get_realm_domain", retval, 0);
return;
}
printf("krb5_get_realm_domain(%s) returned '%s'\n", realm, domain);
free(domain);
}
static void usage(char *progname)
{
fprintf(stderr, "%s: Usage: %s [-dc] [-k realm] [-r host] [-C ccname] [-D realm]\n",
progname, progname);
exit(1);
}
int main(int argc, char **argv)
{
int c;
krb5_context ctx;
krb5_error_code retval;
extern char *optarg;
retval = krb5_init_context(&ctx);
if (retval) {
fprintf(stderr, "krb5_init_context returned error %u\n",
retval);
exit(1);
}
while ((c = getopt(argc, argv, "cdk:r:C:D:l:s:")) != -1) {
switch (c) {
case 'c': /* Get default ccname */
test_get_default_ccname(ctx);
break;
case 'd': /* Get default realm */
test_get_default_realm(ctx);
break;
case 'k': /* Get list of KDC's */
test_get_krbhst(ctx, optarg);
break;
case 'l':
test_locate_kdc(ctx, optarg);
break;
case 'r':
test_get_host_realm(ctx, optarg);
break;
case 's':
test_set_default_realm(ctx, optarg);
break;
case 'C':
test_set_default_ccname(ctx, optarg);
break;
case 'D':
test_get_realm_domain(ctx, optarg);
break;
default:
usage(argv[0]);
}
}
krb5_free_context(ctx);
exit(0);
}
|