2 * Copyright (c) 2016, 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.
42 #include <trp_internal.h>
44 /* Threading note: mutex lock is only used for protecting get_status() and set_status().
45 * If needed, locking for other operations (notably adding/removing connections) must be managed
46 * by whomever is holding on to the connection list. */
48 int trp_connection_lock(TRP_CONNECTION *conn)
50 return pthread_mutex_lock(&(conn->mutex));
53 int trp_connection_unlock(TRP_CONNECTION *conn)
55 return pthread_mutex_unlock(&(conn->mutex));
58 int trp_connection_get_fd(TRP_CONNECTION *conn)
63 void trp_connection_set_fd(TRP_CONNECTION *conn, int fd)
68 /* we use the gss name of the peer to identify it */
69 static TRP_RC trp_connection_set_peer(TRP_CONNECTION *conn)
71 OM_uint32 major_status=0;
72 OM_uint32 minor_status=0;
73 gss_name_t source_name=GSS_C_NO_NAME;
74 gss_name_t target_name=GSS_C_NO_NAME;
75 gss_buffer_desc peer_display_name={0,NULL};
78 major_status=gss_inquire_context(&minor_status,
79 *trp_connection_get_gssctx(conn),
88 if (major_status != GSS_S_COMPLETE) {
89 tr_err("trp_connection_set_peer: unable to identify GSS peer.");
90 if (source_name!=GSS_C_NO_NAME)
91 gss_release_name(&minor_status, &source_name);
92 if (target_name!=GSS_C_NO_NAME)
93 gss_release_name(&minor_status, &target_name);
98 /* we are the source, peer is the target */
99 major_status=gss_display_name(&minor_status, target_name, &peer_display_name, NULL);
101 /* we are the target, peer is the source */
102 major_status=gss_display_name(&minor_status, source_name, &peer_display_name, NULL);
104 gss_release_name(&minor_status, &source_name);
105 gss_release_name(&minor_status, &target_name);
107 conn->peer=tr_new_name(peer_display_name.value);
108 if (conn->peer==NULL)
109 tr_err("trp_connection_set_peer: unable to allocate peer name.");
111 if (conn->peer->len != peer_display_name.length) {
112 tr_err("trp_connection_set_peer: error converting GSS display name to TR_NAME.");
113 tr_free_name(conn->peer);
117 gss_release_buffer(&minor_status, &peer_display_name);
119 if (conn->peer==NULL)
122 tr_debug("trp_connection_set_peer: set peer for %p to %.*s (%p).", conn, conn->peer->len, conn->peer->buf, conn->peer);
126 TR_NAME *trp_connection_get_peer(TRP_CONNECTION *conn)
131 TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn)
133 return conn->gssname;
136 void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname)
138 conn->gssname=gssname;
141 gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn)
146 void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx)
151 TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn)
153 TRP_CONNECTION_STATUS status=TRP_CONNECTION_UNKNOWN;
154 trp_connection_lock(conn);
156 trp_connection_unlock(conn);
160 static void trp_connection_set_status(TRP_CONNECTION *conn, TRP_CONNECTION_STATUS status)
162 TRP_CONNECTION_STATUS old_status=TRP_CONNECTION_UNKNOWN;
163 trp_connection_lock(conn);
164 old_status=conn->status;
166 trp_connection_unlock(conn);
167 if ((status!=old_status) && (conn->status_change_cb!=NULL))
168 conn->status_change_cb(conn, conn->status_change_cookie);
171 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn)
176 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread)
181 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn)
186 static void trp_connection_set_next(TRP_CONNECTION *conn, TRP_CONNECTION *next)
191 /* Ok to call more than once; guarantees connection no longer in the list. Does not free removed element.
192 * Returns handle to new list, you must replace your old handle on the list with this. */
193 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove)
195 TRP_CONNECTION *cur=conn;
196 TRP_CONNECTION *last=NULL;
201 /* first element is a special case */
203 conn=trp_connection_get_next(cur); /* advance list head */
205 /* it was not the first element */
207 cur=trp_connection_get_next(cur);
210 trp_connection_set_next(last, trp_connection_get_next(cur));
214 cur=trp_connection_get_next(cur);
220 static TRP_CONNECTION *trp_connection_get_tail(TRP_CONNECTION *conn)
222 while((conn!=NULL)&&(trp_connection_get_next(conn)!=NULL))
223 conn=trp_connection_get_next(conn);
227 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new)
229 trp_connection_set_next(trp_connection_get_tail(conn), new);
232 static void trp_connection_mutex_init(TRP_CONNECTION *conn)
234 pthread_mutex_init(&(conn->mutex), NULL);
237 /* talloc destructor for a connection: ensures connection is closed, memory freed */
238 static int trp_connection_destructor(void *object)
240 TRP_CONNECTION *conn=talloc_get_type_abort(object, TRP_CONNECTION); /* aborts on wrong type */
241 if ((trp_connection_get_status(conn)!=TRP_CONNECTION_CLOSED)
242 && (trp_connection_get_fd(conn)!=-1))
243 close(trp_connection_get_fd(conn));
244 if (conn->peer!=NULL)
245 tr_free_name(conn->peer);
246 if (conn->gssname!=NULL)
247 tr_free_name(conn->gssname);
251 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx)
253 TRP_CONNECTION *new_conn=talloc(mem_ctx, TRP_CONNECTION);
254 gss_ctx_id_t *gssctx=NULL;
255 pthread_t *thread=NULL;
258 if (new_conn != NULL) {
259 trp_connection_set_next(new_conn, NULL);
260 trp_connection_set_fd(new_conn, -1);
261 trp_connection_set_gssname(new_conn, NULL);
262 trp_connection_mutex_init(new_conn);
263 new_conn->peer=NULL; /* no true set function for this */
264 new_conn->status_change_cb=NULL;
265 new_conn->status_change_cookie=NULL;
266 new_conn->status=TRP_CONNECTION_CLOSED;
268 thread=talloc(new_conn, pthread_t);
270 talloc_free(new_conn);
273 trp_connection_set_thread(new_conn, thread);
275 gssctx=talloc(new_conn, gss_ctx_id_t);
277 talloc_free(new_conn);
280 trp_connection_set_gssctx(new_conn, gssctx);
281 talloc_set_destructor((void *)new_conn, trp_connection_destructor);
286 void trp_connection_free(TRP_CONNECTION *conn)
291 void trp_connection_close(TRP_CONNECTION *conn)
293 if ((conn->status!=TRP_CONNECTION_DOWN) && (conn->fd>0))
294 close(trp_connection_get_fd(conn));
295 trp_connection_set_fd(conn, -1);
296 trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
299 /* returns 0 on authorization success, 1 on failure, or -1 in case of error */
300 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data)
303 int auth, autherr = 0;
304 gss_buffer_desc nameBuffer = {0, NULL};
305 gss_ctx_id_t *gssctx=trp_connection_get_gssctx(conn);
307 nameBuffer.length = trp_connection_get_gssname(conn)->len;
308 nameBuffer.value = tr_name_strdup(trp_connection_get_gssname(conn));
310 tr_debug("trp_connection_auth: beginning passive authentication");
311 if (trp_connection_get_status(conn)!=TRP_CONNECTION_AUTHORIZING)
312 tr_warning("trp_connection_auth: warning: connection was not in TRP_CONNECTION_AUTHORIZING state.");
314 rc = gsscon_passive_authenticate(trp_connection_get_fd(conn), nameBuffer, gssctx, auth_callback, callback_data);
315 gss_release_buffer(NULL, &nameBuffer);
317 tr_debug("trp_connection_auth: Error from gsscon_passive_authenticate(), rc = 0x%08X.", rc);
318 trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
322 tr_debug("trp_connection_auth: beginning second stage authentication");
323 if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
324 tr_debug("trp_connection_auth: Error from gsscon_authorize, rc = %d, autherr = %d.",
326 trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
330 trp_connection_set_peer(conn);
331 trp_connection_set_status(conn, TRP_CONNECTION_UP);
334 tr_debug("trp_connection_auth: Connection authenticated, fd = %d.", trp_connection_get_fd(conn));
336 tr_debug("trp_connection_auth: Authentication failed, fd = %d.", trp_connection_get_fd(conn));
341 /* Accept connection */
342 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname)
345 TRP_CONNECTION *conn=NULL;
347 conn_fd = accept(listen, NULL, NULL);
350 tr_notice("trp_connection_accept: accept() returned error.");
353 conn=trp_connection_new(mem_ctx);
354 trp_connection_set_fd(conn, conn_fd);
355 trp_connection_set_gssname(conn, gssname);
356 trp_connection_set_status(conn, TRP_CONNECTION_AUTHORIZING);
360 /* Initiate connection */
361 TRP_RC trp_connection_initiate(TRP_CONNECTION *conn, char *server, unsigned int port)
365 unsigned int use_port=0;
373 tr_err("trp_connection_initiate: null TRP_CONNECTION");
377 tr_debug("trp_connection_initiate: opening GSS connection to %s:%d",
380 err = gsscon_connect(server,
384 trp_connection_get_gssctx(conn));
386 tr_err("trp_connection_initiate: connection failed.");
389 tr_debug("trp_connection_initiate: connected.");
390 trp_connection_set_fd(conn, fd);
391 if (trp_connection_set_peer(conn)!=TRP_SUCCESS) {
392 tr_err("trp_connection_initiate: error setting peer gssname.");
393 trp_connection_close(conn);
396 trp_connection_set_status(conn, TRP_CONNECTION_UP);