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.
39 #include <sys/socket.h>
40 #include <netinet/in.h>
46 static int tpqs_listen (int port)
50 struct sockaddr_storage addr;
51 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr;
53 saddr->sin_port = htons (port);
54 saddr->sin_family = AF_INET;
55 saddr->sin_addr.s_addr = INADDR_ANY;
57 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
60 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
63 if (0 > (rc = listen(conn, 512)))
66 fprintf (stdout, "TPQ Server listening on port %d\n", port);
70 static int tpqs_auth_connection (int conn, gss_ctx_id_t *gssctx)
73 int auth, autherr = 0;
75 if (rc = gsscon_passive_authenticate(conn, gssctx)) {
76 fprintf(stderr, "Error from gsscon_passive_authenticate(), rc = %d.\n", rc);
80 if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
81 fprintf(stderr, "Error from gsscon_authorize, rc = %d, autherr = %d.\n",
87 fprintf(stdout, "Connection authenticated, conn = %d.\n", conn);
89 fprintf(stderr, "Authentication failed, conn %d.\n", conn);
94 static int tpqs_read_request (int conn, gss_ctx_id_t *gssctx, TPQ_REQ *req)
100 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
106 fprintf(stdout, "Request Received, %d bytes.\n", buflen);
108 /* Parse request -- TBD */
116 static int tpqs_handle_request (TPQ_REQ *req, TPQ_RESP *resp)
121 static int tpqs_send_response (int conn, gss_ctx_id_t *gssctx, TPQ_RESP *resp)
127 /* Create a json TPQ response */
128 if (NULL == (jreq = json_object())) {
129 fprintf(stderr,"Error creating json object.\n");
133 if (0 > (err = json_object_set_new(jreq, "type", json_string("tpq_response")))) {
134 fprintf(stderr, "Error adding type to response.\n");
137 if (0 > (err = json_object_set_new(jreq, "result", json_string("error")))) {
138 fprintf(stderr, "Error adding result to response.\n");
141 if (0 > (err = json_object_set_new(jreq, "msg", json_string("No path to realm")))) {
142 fprintf(stderr, "Error adding msg to response.\n");
146 /* Encode the json response */
147 if (NULL == (resp_buf = json_dumps(jreq, 0))) {
148 fprintf(stderr, "Error encoding json response.\n");
152 printf("Encoded response:\n%s\n", resp_buf);
154 /* Send the request over the connection */
155 if (err = gsscon_write_encrypted_token (conn, *gssctx, resp_buf,
156 strlen(resp_buf) + 1)) {
157 fprintf(stderr, "Error sending request over connection.\n");
167 static void tpqs_handle_connection (int conn)
172 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
174 if (!tpqs_auth_connection(conn, &gssctx)) {
175 fprintf(stderr, "Error authorizing TPQ Server connection, rc = %d.\n", rc);
180 printf("Connection authorized!\n");
182 while (1) { /* continue until an error breaks us out */
184 if (0 > (rc = tpqs_read_request(conn, &gssctx, &req))) {
185 fprintf(stderr, "Error from tpqs_read_request(), rc = %d.\n", rc);
187 } else if (0 == rc) {
191 if (0 > (rc = tpqs_handle_request(&req, &resp))) {
192 fprintf(stderr, "Error from tpqs_handle_request(), rc = %d.\n", rc);
196 if (0 > (rc = tpqs_send_response(conn, &gssctx, &resp))) {
197 fprintf(stderr, "Error from tpqs_send_response(), rc = %d.\n", rc);
205 TPQS_INSTANCE *tpqs_create ()
207 TPQS_INSTANCE *tpqs = 0;
208 if (tpqs = malloc(sizeof(TPQS_INSTANCE)))
209 memset(tpqs, 0, sizeof(TPQS_INSTANCE));
213 int tpqs_start (TPQS_INSTANCE *tpqs,
214 TPQS_REQ_FUNC *req_handler,
221 if (0 > (listen = tpqs_listen(TPQ_PORT)))
222 perror ("Error from tpqs_listen()");
224 while(1) { /* accept incoming conns until we are stopped */
226 if (0 > (conn = accept(listen, NULL, NULL))) {
227 perror("Error from TPQS Server accept()");
231 if (0 > (pid = fork())) {
232 perror("Error on fork()");
238 tpqs_handle_connection(conn);
246 return 1; /* should never get here */
249 void tpqs_destroy (TPQS_INSTANCE *tpqs)