Log incoming IP address when accepting a connection
[trust_router.git] / trp / trp_conn.c
1 /*
2  * Copyright (c) 2016, 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  */
34
35 #include <gsscon.h>
36 #include <gssapi.h>
37 #include <fcntl.h>
38 #include <talloc.h>
39 #include <unistd.h>
40
41 #include <tr_debug.h>
42 #include <trp_internal.h>
43 #include <tr_socket.h>
44
45 /* Threading note: mutex lock is only used for protecting get_status() and set_status().
46  * If needed, locking for other operations (notably adding/removing connections) must be managed
47  * by whomever is holding on to the connection list. */
48
49 int trp_connection_lock(TRP_CONNECTION *conn)
50 {
51   return pthread_mutex_lock(&(conn->mutex));
52 }
53
54 int trp_connection_unlock(TRP_CONNECTION *conn)
55 {
56   return pthread_mutex_unlock(&(conn->mutex));
57 }
58
59 int trp_connection_get_fd(TRP_CONNECTION *conn)
60 {
61   return conn->fd;
62 }
63
64 void trp_connection_set_fd(TRP_CONNECTION *conn, int fd)
65 {
66   conn->fd=fd;
67 }
68
69 /* we use the gss name of the peer to identify it */
70 static TRP_RC trp_connection_set_peer(TRP_CONNECTION *conn)
71 {
72   OM_uint32 major_status=0;
73   OM_uint32 minor_status=0;
74   gss_name_t source_name=GSS_C_NO_NAME;
75   gss_name_t target_name=GSS_C_NO_NAME;
76   gss_buffer_desc peer_display_name={0,NULL};
77   int local=0;
78
79   major_status=gss_inquire_context(&minor_status,
80                                    *trp_connection_get_gssctx(conn),
81                                   &source_name,
82                                   &target_name,
83                                    NULL,
84                                    NULL,
85                                    NULL,
86                                   &local,
87                                    NULL);
88
89   if (major_status != GSS_S_COMPLETE) {
90     tr_err("trp_connection_set_peer: unable to identify GSS peer.");
91     if (source_name!=GSS_C_NO_NAME)
92       gss_release_name(&minor_status, &source_name);
93     if (target_name!=GSS_C_NO_NAME)
94       gss_release_name(&minor_status, &target_name);
95     return TRP_ERROR;
96   }
97
98   if (local) {
99     /* we are the source, peer is the target */
100     major_status=gss_display_name(&minor_status, target_name, &peer_display_name, NULL);
101   } else {
102     /* we are the target, peer is the source */
103     major_status=gss_display_name(&minor_status, source_name, &peer_display_name, NULL);
104   }
105   gss_release_name(&minor_status, &source_name);
106   gss_release_name(&minor_status, &target_name);
107
108   conn->peer=tr_new_name(peer_display_name.value);
109   if (conn->peer==NULL)
110     tr_err("trp_connection_set_peer: unable to allocate peer name.");
111   else {
112     if (conn->peer->len != peer_display_name.length) {
113       tr_err("trp_connection_set_peer: error converting GSS display name to TR_NAME.");
114       tr_free_name(conn->peer);
115       conn->peer=NULL;
116     }
117   }
118   gss_release_buffer(&minor_status, &peer_display_name);
119
120   if (conn->peer==NULL)
121     return TRP_ERROR;
122
123   tr_debug("trp_connection_set_peer: set peer for %p to %.*s (%p).", conn, conn->peer->len, conn->peer->buf, conn->peer);
124   return TRP_SUCCESS;
125 }
126
127 TR_NAME *trp_connection_get_peer(TRP_CONNECTION *conn)
128 {
129   return conn->peer;
130 }
131
132 TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn)
133 {
134   return conn->gssname;
135 }
136
137 void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname)
138 {
139   conn->gssname=gssname;
140 }
141
142 gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn)
143 {
144   return conn->gssctx;
145 }
146
147 void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx)
148 {
149   conn->gssctx=gssctx;
150 }
151
152 TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn)
153 {
154   TRP_CONNECTION_STATUS status=TRP_CONNECTION_UNKNOWN;
155   trp_connection_lock(conn);
156   status=conn->status;
157   trp_connection_unlock(conn);
158   return status;
159 }
160
161 static void trp_connection_set_status(TRP_CONNECTION *conn, TRP_CONNECTION_STATUS status)
162 {
163   TRP_CONNECTION_STATUS old_status=TRP_CONNECTION_UNKNOWN;
164   trp_connection_lock(conn);
165   old_status=conn->status;
166   conn->status=status;
167   trp_connection_unlock(conn);
168   if ((status!=old_status) && (conn->status_change_cb!=NULL))
169       conn->status_change_cb(conn, conn->status_change_cookie);
170 }
171
172 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn)
173 {
174   return conn->thread;
175 }
176
177 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread)
178 {
179   conn->thread=thread;
180 }
181
182 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn)
183 {
184   return conn->next;
185 }
186
187 static void trp_connection_set_next(TRP_CONNECTION *conn, TRP_CONNECTION *next)
188 {
189   conn->next=next;
190 }
191
192 /* Ok to call more than once; guarantees connection no longer in the list. Does not free removed element.
193  * Returns handle to new list, you must replace your old handle on the list with this.  */
194 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove)
195 {
196   TRP_CONNECTION *cur=conn;
197   TRP_CONNECTION *last=NULL;
198
199   if (cur==NULL)
200     return NULL;
201
202   /* first element is a special case */
203   if (cur==remove) {
204     conn=trp_connection_get_next(cur); /* advance list head */
205   } else {
206     /* it was not the first element */
207     last=cur;
208     cur=trp_connection_get_next(cur);
209     while (cur!=NULL) {
210       if (cur==remove) {
211         trp_connection_set_next(last, trp_connection_get_next(cur));
212         break;
213       }
214       last=cur;
215       cur=trp_connection_get_next(cur);
216     }
217   }
218   return conn;
219 }
220
221 static TRP_CONNECTION *trp_connection_get_tail(TRP_CONNECTION *conn)
222 {
223   while((conn!=NULL)&&(trp_connection_get_next(conn)!=NULL))
224     conn=trp_connection_get_next(conn);
225   return conn;
226 }
227
228 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new)
229 {
230   trp_connection_set_next(trp_connection_get_tail(conn), new);
231 }
232
233 static void trp_connection_mutex_init(TRP_CONNECTION *conn)
234 {
235   pthread_mutex_init(&(conn->mutex), NULL);
236 }
237
238 /* talloc destructor for a connection: ensures connection is closed, memory freed */
239 static int trp_connection_destructor(void *object)
240 {
241   TRP_CONNECTION *conn=talloc_get_type_abort(object, TRP_CONNECTION); /* aborts on wrong type */
242   if ((trp_connection_get_status(conn)!=TRP_CONNECTION_CLOSED)
243      && (trp_connection_get_fd(conn)!=-1))
244     close(trp_connection_get_fd(conn));
245   if (conn->peer!=NULL)
246     tr_free_name(conn->peer);
247   if (conn->gssname!=NULL)
248     tr_free_name(conn->gssname);
249   return 0;
250 }
251
252 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx)
253 {
254   TRP_CONNECTION *new_conn=talloc(mem_ctx, TRP_CONNECTION);
255   gss_ctx_id_t *gssctx=NULL;
256   pthread_t *thread=NULL;
257   
258
259   if (new_conn != NULL) {
260     trp_connection_set_next(new_conn, NULL);
261     trp_connection_set_fd(new_conn, -1);
262     trp_connection_set_gssname(new_conn, NULL);
263     trp_connection_mutex_init(new_conn);
264     new_conn->peer=NULL; /* no true set function for this */
265     new_conn->status_change_cb=NULL;
266     new_conn->status_change_cookie=NULL;
267     new_conn->status=TRP_CONNECTION_CLOSED;
268
269     thread=talloc(new_conn, pthread_t);
270     if (thread==NULL) {
271       talloc_free(new_conn);
272       return NULL;
273     }
274     trp_connection_set_thread(new_conn, thread);
275
276     gssctx=talloc(new_conn, gss_ctx_id_t);
277     if (gssctx==NULL) {
278       talloc_free(new_conn);
279       return NULL;
280     }
281     trp_connection_set_gssctx(new_conn, gssctx);
282     talloc_set_destructor((void *)new_conn, trp_connection_destructor);
283   }
284   return new_conn;
285 }
286
287 void trp_connection_free(TRP_CONNECTION *conn)
288 {
289   talloc_free(conn);
290 }
291
292 void trp_connection_close(TRP_CONNECTION *conn)
293 {
294   if ((conn->status!=TRP_CONNECTION_DOWN) && (conn->fd>0))
295     close(trp_connection_get_fd(conn));
296   trp_connection_set_fd(conn, -1);
297   trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
298 }
299
300 /* returns 0 on authorization success, 1 on failure, or -1 in case of error */
301 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data)
302 {
303   int rc = 0;
304   int auth, autherr = 0;
305   gss_buffer_desc nameBuffer = {0, NULL};
306   gss_ctx_id_t *gssctx=trp_connection_get_gssctx(conn);
307
308   nameBuffer.length = trp_connection_get_gssname(conn)->len;
309   nameBuffer.value = tr_name_strdup(trp_connection_get_gssname(conn));
310
311   tr_debug("trp_connection_auth: beginning passive authentication");
312   if (trp_connection_get_status(conn)!=TRP_CONNECTION_AUTHORIZING)
313     tr_warning("trp_connection_auth: warning: connection was not in TRP_CONNECTION_AUTHORIZING state.");
314
315   rc = gsscon_passive_authenticate(trp_connection_get_fd(conn), nameBuffer, gssctx, auth_callback, callback_data);
316   gss_release_buffer(NULL, &nameBuffer);
317   if (rc!=0) {
318     tr_debug("trp_connection_auth: Error from gsscon_passive_authenticate(), rc = 0x%08X.", rc);
319     trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
320     return -1;
321   }
322
323   tr_debug("trp_connection_auth: beginning second stage authentication");
324   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
325     tr_debug("trp_connection_auth: Error from gsscon_authorize, rc = %d, autherr = %d.", 
326              rc, autherr);
327     trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
328     return -1;
329   }
330
331   trp_connection_set_peer(conn);
332   trp_connection_set_status(conn, TRP_CONNECTION_UP);
333
334   if (auth)
335     tr_debug("trp_connection_auth: Connection authenticated, fd = %d.", trp_connection_get_fd(conn));
336   else
337     tr_debug("trp_connection_auth: Authentication failed, fd = %d.", trp_connection_get_fd(conn));
338
339   return !auth;
340 }
341
342 /* Accept connection */
343 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname)
344 {
345   int conn_fd=-1;
346   TRP_CONNECTION *conn=NULL;
347
348   conn_fd = tr_sock_accept(listen);
349
350   if (0 > conn_fd) {
351     tr_notice("trp_connection_accept: Error accepting connection.");
352     return NULL;
353   }
354   conn=trp_connection_new(mem_ctx);
355   trp_connection_set_fd(conn, conn_fd);
356   trp_connection_set_gssname(conn, gssname);
357   trp_connection_set_status(conn, TRP_CONNECTION_AUTHORIZING);
358   return conn;
359 }
360
361 /* Initiate connection */
362 TRP_RC trp_connection_initiate(TRP_CONNECTION *conn, char *server, unsigned int port)
363 {
364   int err = 0;
365   int fd=-1;
366   unsigned int use_port=0;
367
368   if (0 == port)
369     use_port = TRP_PORT;
370   else 
371     use_port = port;
372
373   if (conn==NULL) {
374     tr_err("trp_connection_initiate: null TRP_CONNECTION");
375     return TRP_BADARG;
376   }
377
378   tr_debug("trp_connection_initiate: opening GSS connection to %s:%d",
379            server,
380            use_port);
381   err = gsscon_connect(server,
382                        use_port,
383                        "trustrouter",
384                       &fd,
385                        trp_connection_get_gssctx(conn));
386   if (err) {
387     tr_err("trp_connection_initiate: connection failed.");
388     return TRP_ERROR;
389   } else {
390     tr_debug("trp_connection_initiate: connected.");
391     trp_connection_set_fd(conn, fd);
392     if (trp_connection_set_peer(conn)!=TRP_SUCCESS) {
393       tr_err("trp_connection_initiate: error setting peer gssname.");
394       trp_connection_close(conn);
395       return TRP_ERROR;
396     }
397     trp_connection_set_status(conn, TRP_CONNECTION_UP);
398     return TRP_SUCCESS;
399   }
400 }