2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
33 * This code was adapted from the MIT Kerberos Consortium's
34 * GSS example code, which was distributed under the following
37 * Copyright 2004-2006 Massachusetts Institute of Technology.
38 * All Rights Reserved.
40 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
41 * distribute this software and its documentation for any purpose and
42 * without fee is hereby granted, provided that the above copyright
43 * notice appear in all copies and that both that copyright notice and
44 * this permission notice appear in supporting documentation, and that
45 * the name of M.I.T. not be used in advertising or publicity pertaining
46 * to distribution of the software without specific, written prior
47 * permission. Furthermore if you modify this software you must label
48 * your software as modified software and not distribute it in such a
49 * fashion that it might be confused with the original M.I.T. software.
50 * M.I.T. makes no representations about the suitability of
51 * this software for any purpose. It is provided "as is" without express
52 * or implied warranty.
58 static int cb_print_names(gss_name_t clientName,
59 gss_buffer_t displayName,
62 assert(clientName != NULL);
64 printf("Gss name: %-*s\n",
65 displayName->length, displayName->value);
70 /* --------------------------------------------------------------------------- */
72 static int SetupListeningSocket (int inPort,
78 if (!outFD) { err = EINVAL; }
81 fd = socket (AF_INET, SOCK_STREAM, 0);
82 if (fd < 0) { err = errno; }
86 struct sockaddr_storage addressStorage;
87 struct sockaddr_in *saddr = (struct sockaddr_in *) &addressStorage;
89 saddr->sin_port = htons (inPort);
90 // saddr->sin_len = sizeof (struct sockaddr_in);
91 saddr->sin_family = AF_INET;
92 saddr->sin_addr.s_addr = INADDR_ANY;
94 err = bind (fd, (struct sockaddr *) saddr, sizeof(struct sockaddr_in));
95 if (err < 0) { err = errno; }
100 if (err < 0) { err = errno; }
104 printf ("listening on port %d\n", inPort);
106 fd = -1; /* only close on error */
108 gsscon_print_error (err, "SetupListeningSocket failed");
111 if (fd >= 0) { close (fd); }
116 /* --------------------------------------------------------------------------- */
118 static void Usage (const char *argv[])
120 fprintf (stderr, "Usage: %s [--port portNumber] [--service serviceName]\n",
125 /* --------------------------------------------------------------------------- */
127 int main (int argc, const char *argv[])
130 OM_uint32 minorStatus;
131 int port = kDefaultPort;
133 gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
135 gss_buffer_desc nameBuffer = {0, "trustidentity"};
137 for (i = 1; (i < argc) && !err; i++) {
138 if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
139 port = strtol (argv[++i], NULL, 0);
140 if (port == 0) { err = errno; }
141 } else if ((strcmp(argv[i], "--service") == 0) && (i < (argc - 1))) {
142 gServiceName = argv[++i];
149 printf ("%s: Starting up...\n", argv[0]);
151 err = SetupListeningSocket (port, &listenFD);
155 int connectionErr = 0;
156 int connectionFD = -1;
158 int authorizationError = 0;
160 connectionFD = accept (listenFD, NULL, NULL);
162 if (connectionFD < 0) {
163 if (errno != EINTR) {
166 continue; /* Try again */
169 printf ("Accepting new connection...\n");
170 connectionErr = gsscon_passive_authenticate (connectionFD, nameBuffer, &gssContext,
171 cb_print_names, NULL);
173 if (!connectionErr) {
174 connectionErr = gsscon_authorize (gssContext,
176 &authorizationError);
179 if (!connectionErr) {
181 memset (buffer, 0, sizeof (buffer));
184 * Here is where your protocol would go. This sample server just
185 * writes a nul terminated string to the client telling whether
189 snprintf (buffer, sizeof (buffer), "SUCCESS!");
191 snprintf (buffer, sizeof(buffer), "FAILURE! %s (err = %d)",
192 error_message (authorizationError),
195 connectionErr = gsscon_write_encrypted_token (connectionFD, gssContext,
196 buffer, strlen (buffer) + 1);
200 gsscon_print_error (connectionErr, "Connection failed");
203 if (connectionFD >= 0) {
204 printf ("Closing connection.\n");
205 close (connectionFD);
213 gsscon_print_error (err, "Server failed");
217 if (listenFD >= 0) { close (listenFD); }
218 if (gssContext != GSS_C_NO_CONTEXT) {
219 gss_delete_sec_context (&minorStatus, &gssContext, GSS_C_NO_BUFFER); }