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