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.
57 static void Usage (const char *argv[])
59 fprintf (stderr, "Usage: %s [--port portNumber] [--server serverHostName]\n"
60 "\t[--sprinc servicePrincipal] [--cprinc clientPrincipal]\n", argv[0]);
64 /* --------------------------------------------------------------------------- */
66 int main (int argc, const char *argv[])
70 int port = kDefaultPort;
72 const char *server = "127.0.0.1";
73 const char *clientName = NULL;
74 const char *serviceName = "host";
75 gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
76 OM_uint32 minorStatus = 0;
79 for (i = 1; (i < argc) && !err; i++) {
80 if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
81 errno = 0; /* ensure this starts off as 0 */
82 tmp = strtol (argv[++i], NULL, 0);
85 else if ((tmp <= 0) || (tmp > 65535))
89 } else if ((strcmp (argv[i], "--server") == 0) && (i < (argc - 1))) {
91 } else if ((strcmp(argv[i], "--cprinc") == 0) && (i < (argc - 1))) {
92 clientName = argv[++i];
93 } else if ((strcmp(argv[i], "--sprinc") == 0) && (i < (argc - 1))) {
94 serviceName = argv[++i];
101 printf ("%s: Starting up...\n", argv[0]);
103 err = gsscon_connect (server, (unsigned int) port, serviceName, &fd, &gssContext);
108 size_t bufferLength = 0;
111 * Here is where your protocol would go. This sample client just
112 * reads a nul terminated string from the server.
114 err = gsscon_read_encrypted_token (fd, gssContext, &buffer, &bufferLength);
117 printf ("Server message: '%s'\n", buffer);
120 if (buffer != NULL) { free (buffer); }
128 gsscon_print_error (err, "Client failed");
132 if (fd >= 0) { printf ("Closing socket.\n"); close (fd); }
133 if (gssContext != GSS_C_NO_CONTEXT) {
134 gss_delete_sec_context (&minorStatus, &gssContext, GSS_C_NO_BUFFER); }