xref: /freebsd/crypto/heimdal/appl/test/nt_gss_server.c (revision c25d7ab7414672a680e73cefc0a681d10427bf77)
1 /*
2  * Copyright (c) 1997 - 2000 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 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.
20  *
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.
32  */
33 
34 #include "test_locl.h"
35 #include <gssapi.h>
36 #include <krb5.h>
37 #include "nt_gss_common.h"
38 
39 RCSID("$Id: nt_gss_server.c,v 1.5 2000/08/09 20:53:07 assar Exp $");
40 
41 /*
42  * This program tries to act as a server for the sample in `Sample
43  * SSPI Code' in Windows 2000 RC1 SDK.
44  *
45  * use --dump-auth to get a binary dump of the authorization data in the ticket
46  */
47 
48 static int help_flag;
49 static int version_flag;
50 static char *port_str;
51 char *service = SERVICE;
52 static char *auth_file;
53 
54 static struct getargs args[] = {
55     { "port", 'p', arg_string, &port_str, "port to listen to", "port" },
56     { "service", 's', arg_string, &service, "service to use", "service" },
57     { "dump-auth", 0, arg_string, &auth_file, "dump authorization data",
58       "file" },
59     { "help", 'h', arg_flag, &help_flag },
60     { "version", 0, arg_flag, &version_flag }
61 };
62 
63 static int num_args = sizeof(args) / sizeof(args[0]);
64 
65 static int
66 proto (int sock, const char *service)
67 {
68     struct sockaddr_in remote, local;
69     socklen_t addrlen;
70     gss_ctx_id_t context_hdl = GSS_C_NO_CONTEXT;
71     gss_buffer_t input_token, output_token;
72     gss_buffer_desc real_input_token, real_output_token;
73     OM_uint32 maj_stat, min_stat;
74     gss_name_t client_name;
75     gss_buffer_desc name_token;
76 
77     addrlen = sizeof(local);
78     if (getsockname (sock, (struct sockaddr *)&local, &addrlen) < 0
79 	|| addrlen != sizeof(local))
80 	err (1, "getsockname)");
81 
82     addrlen = sizeof(remote);
83     if (getpeername (sock, (struct sockaddr *)&remote, &addrlen) < 0
84 	|| addrlen != sizeof(remote))
85 	err (1, "getpeername");
86 
87     input_token = &real_input_token;
88     output_token = &real_output_token;
89 
90     do {
91 	nt_read_token (sock, input_token);
92 	maj_stat =
93 	    gss_accept_sec_context (&min_stat,
94 				    &context_hdl,
95 				    GSS_C_NO_CREDENTIAL,
96 				    input_token,
97 				    GSS_C_NO_CHANNEL_BINDINGS,
98 				    &client_name,
99 				    NULL,
100 				    output_token,
101 				    NULL,
102 				    NULL,
103 				    NULL);
104 	if(GSS_ERROR(maj_stat))
105 	    gss_err (1, min_stat, "gss_accept_sec_context");
106 	if (output_token->length != 0)
107 	    nt_write_token (sock, output_token);
108 	if (GSS_ERROR(maj_stat)) {
109 	    if (context_hdl != GSS_C_NO_CONTEXT)
110 		gss_delete_sec_context (&min_stat,
111 					&context_hdl,
112 					GSS_C_NO_BUFFER);
113 	    break;
114 	}
115     } while(maj_stat & GSS_S_CONTINUE_NEEDED);
116 
117     if (auth_file != NULL) {
118 	int fd = open (auth_file, O_WRONLY | O_CREAT, 0666);
119 	krb5_ticket *ticket = context_hdl->ticket;
120 	krb5_data *data = &ticket->ticket.authorization_data->val[0].ad_data;
121 
122 	if(fd < 0)
123 	    err (1, "open %s", auth_file);
124 	if (write (fd, data->data, data->length) != data->length)
125 	    errx (1, "write to %s failed", auth_file);
126 	if (close (fd))
127 	    err (1, "close %s", auth_file);
128     }
129 
130     maj_stat = gss_display_name (&min_stat,
131 				 client_name,
132 				 &name_token,
133 				 NULL);
134     if (GSS_ERROR(maj_stat))
135 	gss_err (1, min_stat, "gss_display_name");
136 
137     fprintf (stderr, "User is `%.*s'\n", (int)name_token.length,
138 	    (char *)name_token.value);
139 
140     /* write something back */
141 
142     output_token->value  = strdup ("hejsan");
143     output_token->length = strlen (output_token->value) + 1;
144     nt_write_token (sock, output_token);
145 
146     output_token->value  = strdup ("hoppsan");
147     output_token->length = strlen (output_token->value) + 1;
148     nt_write_token (sock, output_token);
149 
150     return 0;
151 }
152 
153 static int
154 doit (int port, const char *service)
155 {
156     int sock, sock2;
157     struct sockaddr_in my_addr;
158     int one = 1;
159 
160     sock = socket (AF_INET, SOCK_STREAM, 0);
161     if (sock < 0)
162 	err (1, "socket");
163 
164     memset (&my_addr, 0, sizeof(my_addr));
165     my_addr.sin_family      = AF_INET;
166     my_addr.sin_port        = port;
167     my_addr.sin_addr.s_addr = INADDR_ANY;
168 
169     if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
170 		    (void *)&one, sizeof(one)) < 0)
171 	warn ("setsockopt SO_REUSEADDR");
172 
173     if (bind (sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
174 	err (1, "bind");
175 
176     if (listen (sock, 1) < 0)
177 	err (1, "listen");
178 
179     sock2 = accept (sock, NULL, NULL);
180     if (sock2 < 0)
181 	err (1, "accept");
182 
183     return proto (sock2, service);
184 }
185 
186 static void
187 usage(int code, struct getargs *args, int num_args)
188 {
189     arg_printusage(args, num_args, NULL, "");
190     exit(code);
191 }
192 
193 static int
194 common_setup(krb5_context *context, int *argc, char **argv,
195 	     void (*usage)(int, struct getargs*, int))
196 {
197     int port = 0;
198     *argc = krb5_program_setup(context, *argc, argv, args, num_args, usage);
199 
200     if(help_flag)
201 	(*usage)(0, args, num_args);
202     if(version_flag) {
203 	print_version(NULL);
204 	exit(0);
205     }
206 
207     if(port_str){
208 	struct servent *s = roken_getservbyname(port_str, "tcp");
209 	if(s)
210 	    port = s->s_port;
211 	else {
212 	    char *ptr;
213 
214 	    port = strtol (port_str, &ptr, 10);
215 	    if (port == 0 && ptr == port_str)
216 		errx (1, "Bad port `%s'", port_str);
217 	    port = htons(port);
218 	}
219     }
220 
221     if (port == 0)
222 	port = krb5_getportbyname (*context, PORT, "tcp", 4711);
223 
224     return port;
225 }
226 
227 static int
228 setup(krb5_context *context, int argc, char **argv)
229 {
230     int port = common_setup(context, &argc, argv, usage);
231     if(argv[argc] != NULL)
232 	usage(1, args, num_args);
233     return port;
234 }
235 
236 int
237 main(int argc, char **argv)
238 {
239     krb5_context context = NULL; /* XXX */
240     int port = setup(&context, argc, argv);
241     return doit (port, service);
242 }
243