Sync usage with man page.
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / test / nt_gss_client.c
blob2f4cfc9f1ce773e9a67cc8d30433e524fd2de6ec
1 /*
2 * Copyright (c) 1997 - 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "test_locl.h"
35 #include <gssapi.h>
36 #include "nt_gss_common.h"
38 __RCSID("$Heimdal: nt_gss_client.c 21522 2007-07-12 13:15:04Z lha $"
39 "$NetBSD$");
42 * This program tries to act as a client for the sample in `Sample
43 * SSPI Code' in Windows 2000 RC1 SDK.
46 static int
47 proto (int sock, const char *hostname, const char *service)
49 struct sockaddr_in remote, local;
50 socklen_t addrlen;
52 int context_established = 0;
53 gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;
54 gss_buffer_t input_token, output_token;
55 gss_buffer_desc real_input_token, real_output_token;
56 OM_uint32 maj_stat, min_stat;
57 gss_name_t server;
58 gss_buffer_desc name_token;
59 char *str;
61 name_token.length = asprintf (&str,
62 "%s@%s", service, hostname);
63 if (str == NULL)
64 errx(1, "out of memory");
65 name_token.value = str;
67 maj_stat = gss_import_name (&min_stat,
68 &name_token,
69 GSS_C_NT_HOSTBASED_SERVICE,
70 &server);
71 if (GSS_ERROR(maj_stat))
72 gss_err (1, min_stat,
73 "Error importing name `%s@%s':\n", service, hostname);
75 addrlen = sizeof(local);
76 if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
77 || addrlen != sizeof(local))
78 err (1, "getsockname(%s)", hostname);
80 addrlen = sizeof(remote);
81 if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
82 || addrlen != sizeof(remote))
83 err (1, "getpeername(%s)", hostname);
85 input_token = &real_input_token;
86 output_token = &real_output_token;
88 input_token->length = 0;
89 output_token->length = 0;
91 while(!context_established) {
92 maj_stat =
93 gss_init_sec_context(&min_stat,
94 GSS_C_NO_CREDENTIAL,
95 &context_hdl,
96 server,
97 GSS_C_NO_OID,
98 GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
100 GSS_C_NO_CHANNEL_BINDINGS,
101 input_token,
102 NULL,
103 output_token,
104 NULL,
105 NULL);
106 if (GSS_ERROR(maj_stat))
107 gss_err (1, min_stat, "gss_init_sec_context");
108 if (output_token->length != 0)
109 nt_write_token (sock, output_token);
110 if (GSS_ERROR(maj_stat)) {
111 if (context_hdl != GSS_C_NO_CONTEXT)
112 gss_delete_sec_context (&min_stat,
113 &context_hdl,
114 GSS_C_NO_BUFFER);
115 break;
117 if (maj_stat & GSS_S_CONTINUE_NEEDED) {
118 nt_read_token (sock, input_token);
119 } else {
120 context_established = 1;
125 /* get_mic */
127 input_token->length = 3;
128 input_token->value = strdup("hej");
130 maj_stat = gss_get_mic(&min_stat,
131 context_hdl,
132 GSS_C_QOP_DEFAULT,
133 input_token,
134 output_token);
135 if (GSS_ERROR(maj_stat))
136 gss_err (1, min_stat, "gss_get_mic");
138 nt_write_token (sock, input_token);
139 nt_write_token (sock, output_token);
141 /* wrap */
143 input_token->length = 7;
144 input_token->value = "hemligt";
147 maj_stat = gss_wrap (&min_stat,
148 context_hdl,
150 GSS_C_QOP_DEFAULT,
151 input_token,
152 NULL,
153 output_token);
154 if (GSS_ERROR(maj_stat))
155 gss_err (1, min_stat, "gss_wrap");
157 nt_write_token (sock, output_token);
159 return 0;
163 main(int argc, char **argv)
165 krb5_context context; /* XXX */
166 int port = client_setup(&context, &argc, argv);
167 return client_doit (argv[argc], port, service, proto);