Authenticated conn between tpqs and tpqc works.
[trust_router.git] / gsscon / test / gsscon_server.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  * This code was adapted from the MIT Kerberos Consortium's
34  * GSS example code, which was distributed under the following
35  * license:
36  *
37  * Copyright 2004-2006 Massachusetts Institute of Technology.
38  * All Rights Reserved.
39  *
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.
53  */
54
55 #include <gsscon.h>
56
57 /* --------------------------------------------------------------------------- */
58
59 static int SetupListeningSocket (int  inPort, 
60                                  int *outFD)
61 {
62     int err = 0;
63     int fd = -1;
64     
65     if (!outFD) { err = EINVAL; }
66     
67     if (!err) {
68         fd = socket (AF_INET, SOCK_STREAM, 0);
69         if (fd < 0) { err = errno; }
70     }
71     
72     if (!err) {
73         struct sockaddr_storage addressStorage;
74         struct sockaddr_in *saddr = (struct sockaddr_in *) &addressStorage;
75         
76         saddr->sin_port = htons (inPort);
77         //        saddr->sin_len = sizeof (struct sockaddr_in);
78         saddr->sin_family = AF_INET;
79         saddr->sin_addr.s_addr = INADDR_ANY;
80         
81         err = bind (fd, (struct sockaddr *) saddr, sizeof(struct sockaddr_in));
82         if (err < 0) { err = errno; }
83     }
84     
85     if (!err) {
86         err = listen (fd, 5);
87         if (err < 0) { err = errno; }
88     }
89     
90     if (!err) {
91         printf ("listening on port %d\n", inPort);
92         *outFD = fd;
93         fd = -1; /* only close on error */
94     } else {
95         gsscon_print_error (err, "SetupListeningSocket failed");
96     }
97     
98     if (fd >= 0) { close (fd); }
99     
100     return err; 
101 }
102
103 /* --------------------------------------------------------------------------- */
104
105 static void Usage (const char *argv[])
106 {
107     fprintf (stderr, "Usage: %s [--port portNumber] [--service serviceName]\n", 
108              argv[0]);
109     exit (1);
110 }
111
112 /* --------------------------------------------------------------------------- */
113
114 int main (int argc, const char *argv[])
115 {
116     int err = 0;
117     OM_uint32 minorStatus;
118     int port = kDefaultPort;
119     int listenFD = -1;
120     gss_ctx_id_t gssContext = GSS_C_NO_CONTEXT;
121     int i = 0;
122         
123     for (i = 1; (i < argc) && !err; i++) {
124         if ((strcmp (argv[i], "--port") == 0) && (i < (argc - 1))) {
125             port = strtol (argv[++i], NULL, 0);
126             if (port == 0) { err = errno; }
127         } else if ((strcmp(argv[i], "--service") == 0) && (i < (argc - 1))) {
128             gServiceName = argv[++i];
129         } else {
130             err = EINVAL;
131         }
132     }
133
134     if (!err) {
135         printf ("%s: Starting up...\n", argv[0]);
136         
137         err = SetupListeningSocket (port, &listenFD);
138     }
139     
140     while (!err) {
141         int connectionErr = 0;
142         int connectionFD = -1;
143         int authorized = 0;
144         int authorizationError = 0;
145         
146         connectionFD = accept (listenFD, NULL, NULL);
147
148         if (connectionFD < 0) {
149             if (errno != EINTR) { 
150                 err = errno;
151             }
152             continue;  /* Try again */
153         }
154         
155         printf ("Accepting new connection...\n");
156         connectionErr = gsscon_passive_authenticate (connectionFD, &gssContext);
157         
158         if (!connectionErr) {
159             connectionErr = gsscon_authorize (gssContext, 
160                                        &authorized, 
161                                        &authorizationError);
162         }
163         
164         if (!connectionErr) {
165             char buffer[1024];
166             memset (buffer, 0, sizeof (buffer));                
167
168             /* 
169              * Here is where your protocol would go.  This sample server just
170              * writes a nul terminated string to the client telling whether 
171              * it was authorized.
172              */
173             if (authorized) {
174                 snprintf (buffer, sizeof (buffer), "SUCCESS!"); 
175             } else {
176                 snprintf (buffer, sizeof(buffer), "FAILURE! %s (err = %d)", 
177                           error_message (authorizationError), 
178                           authorizationError); 
179             }
180             connectionErr = gsscon_write_encrypted_token (connectionFD, gssContext, 
181                                                  buffer, strlen (buffer) + 1);
182         }
183         
184         if (connectionErr) {
185             gsscon_print_error (connectionErr, "Connection failed");
186         }
187         
188         if (connectionFD >= 0) { 
189             printf ("Closing connection.\n"); 
190             close (connectionFD); 
191         }
192     }
193     
194     if (err) { 
195         if (err == EINVAL) {
196             Usage (argv);
197         } else {
198             gsscon_print_error (err, "Server failed");
199         }
200     }
201     
202     if (listenFD >= 0) { close (listenFD); }
203     if (gssContext != GSS_C_NO_CONTEXT) { 
204         gss_delete_sec_context (&minorStatus, &gssContext, GSS_C_NO_BUFFER); }
205     
206     return err ? -1 : 0;
207 }
208