Update the routing table when TRP updates are received.
[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   tr_debug("gssctx = %p", trp_connection_get_gssctx(conn));
45   tr_debug("*gssctx = %p", *trp_connection_get_gssctx(conn));
46   major_status=gss_inquire_context(&minor_status,
47                                    *trp_connection_get_gssctx(conn),
48                                   &source_name,
49                                   &target_name,
50                                    NULL,
51                                    NULL,
52                                    NULL,
53                                   &local,
54                                    NULL);
55
56   if (major_status != GSS_S_COMPLETE) {
57     tr_err("trp_connection_set_peer: unable to identify GSS peer.");
58     if (source_name!=GSS_C_NO_NAME)
59       gss_release_name(&minor_status, &source_name);
60     if (target_name!=GSS_C_NO_NAME)
61       gss_release_name(&minor_status, &target_name);
62     return TRP_ERROR;
63   }
64
65   if (local) {
66     /* we are the source, peer is the target */
67     major_status=gss_display_name(&minor_status, target_name, &peer_display_name, NULL);
68   } else {
69     /* we are the target, peer is the source */
70     major_status=gss_display_name(&minor_status, source_name, &peer_display_name, NULL);
71   }
72   gss_release_name(&minor_status, &source_name);
73   gss_release_name(&minor_status, &target_name);
74
75   conn->peer=tr_new_name(peer_display_name.value);
76   if (conn->peer==NULL)
77     tr_err("trp_connection_set_peer: unable to allocate peer name.");
78   else {
79     if (conn->peer->len != peer_display_name.length) {
80       tr_err("trp_connection_set_peer: error converting GSS display name to TR_NAME.");
81       tr_free_name(conn->peer);
82       conn->peer=NULL;
83     }
84   }
85   gss_release_buffer(&minor_status, &peer_display_name);
86
87   if (conn->peer==NULL)
88     return TRP_ERROR;
89   
90   return TRP_SUCCESS;
91 }
92
93 TR_NAME *trp_connection_get_peer(TRP_CONNECTION *conn)
94 {
95   return conn->peer;
96 }
97
98 TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn)
99 {
100   return conn->gssname;
101 }
102
103 void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname)
104 {
105   conn->gssname=gssname;
106 }
107
108 gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn)
109 {
110   return conn->gssctx;
111 }
112
113 void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx)
114 {
115   conn->gssctx=gssctx;
116 }
117
118 TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn)
119 {
120   TRP_CONNECTION_STATUS status;
121   trp_connection_lock(conn);
122   status=conn->status;
123   trp_connection_unlock(conn);
124   return status;
125 }
126
127 static void trp_connection_set_status(TRP_CONNECTION *conn, TRP_CONNECTION_STATUS status)
128 {
129   trp_connection_lock(conn);
130   conn->status=status;
131   trp_connection_unlock(conn);
132 }
133
134 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn)
135 {
136   return conn->thread;
137 }
138
139 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread)
140 {
141   conn->thread=thread;
142 }
143
144 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn)
145 {
146   return conn->next;
147 }
148
149 static void trp_connection_set_next(TRP_CONNECTION *conn, TRP_CONNECTION *next)
150 {
151   conn->next=next;
152 }
153
154 /* Ok to call more than once; guarantees connection no longer in the list. Does not free removed element.
155  * Returns handle to new list, you must replace your old handle on the list with this.  */
156 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove)
157 {
158   TRP_CONNECTION *cur=conn;
159   TRP_CONNECTION *last=NULL;
160
161   if (cur==NULL)
162     return NULL;
163
164   /* first element is a special case */
165   if (cur==remove) {
166     conn=trp_connection_get_next(cur); /* advance list head */
167   } else {
168     /* it was not the first element */
169     last=cur;
170     cur=trp_connection_get_next(cur);
171     while (cur!=NULL) {
172       if (cur==remove) {
173         trp_connection_set_next(last, trp_connection_get_next(cur));
174         break;
175       }
176       last=cur;
177       cur=trp_connection_get_next(cur);
178     }
179   }
180   return conn;
181 }
182
183 static TRP_CONNECTION *trp_connection_get_tail(TRP_CONNECTION *conn)
184 {
185   while((conn!=NULL)&&(trp_connection_get_next(conn)!=NULL))
186     conn=trp_connection_get_next(conn);
187   return conn;
188 }
189
190 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new)
191 {
192   trp_connection_set_next(trp_connection_get_tail(conn), new);
193 }
194
195 static void trp_connection_mutex_init(TRP_CONNECTION *conn)
196 {
197   pthread_mutex_init(&(conn->mutex), NULL);
198 }
199
200 /* talloc destructor for a connection: ensures connection is closed, memory freed */
201 static int trp_connection_destructor(void *object)
202 {
203   TRP_CONNECTION *conn=talloc_get_type_abort(object, TRP_CONNECTION); /* aborts on wrong type */
204   if ((trp_connection_get_status(conn)!=TRP_CONNECTION_DOWN)
205      && (trp_connection_get_fd(conn)!=-1))
206     close(trp_connection_get_fd(conn));
207   if (conn->peer!=NULL)
208     tr_free_name(conn->peer);
209   if (conn->gssname!=NULL)
210     tr_free_name(conn->gssname);
211   return 0;
212 }
213
214 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx)
215 {
216   TRP_CONNECTION *new_conn=talloc(mem_ctx, TRP_CONNECTION);
217   gss_ctx_id_t *gssctx=NULL;
218   pthread_t *thread=NULL;
219   
220
221   if (new_conn != NULL) {
222     trp_connection_set_next(new_conn, NULL);
223     trp_connection_set_fd(new_conn, -1);
224     trp_connection_set_gssname(new_conn, NULL);
225     trp_connection_mutex_init(new_conn);
226     trp_connection_set_status(new_conn, TRP_CONNECTION_DOWN);
227     thread=talloc(new_conn, pthread_t);
228     gssctx=talloc(new_conn, gss_ctx_id_t);
229     if (gssctx==NULL) {
230       talloc_free(new_conn);
231       return NULL;
232     }
233     trp_connection_set_gssctx(new_conn, gssctx);
234     if (thread==NULL) {
235       talloc_free(new_conn);
236       return NULL;
237     }
238     trp_connection_set_thread(new_conn, thread);
239     talloc_set_destructor((void *)new_conn, trp_connection_destructor);
240   }
241   return new_conn;
242 }
243
244 void trp_connection_free(TRP_CONNECTION *conn)
245 {
246   talloc_free(conn);
247 }
248
249 void trp_connection_close(TRP_CONNECTION *conn)
250 {
251   close(trp_connection_get_fd(conn));
252   trp_connection_set_fd(conn, -1);
253   trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
254 }
255
256 /* returns 0 on authorization success, 1 on failure, or -1 in case of error */
257 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data)
258 {
259   int rc = 0;
260   int auth, autherr = 0;
261   gss_buffer_desc nameBuffer = {0, NULL};
262   gss_ctx_id_t *gssctx=trp_connection_get_gssctx(conn);
263
264   /* TODO: shouldn't really peek into TR_NAME... */
265   nameBuffer.length = trp_connection_get_gssname(conn)->len;
266   nameBuffer.value = trp_connection_get_gssname(conn)->buf;
267
268   tr_debug("trp_connection_auth: beginning passive authentication");
269   rc = gsscon_passive_authenticate(trp_connection_get_fd(conn), nameBuffer, gssctx, auth_callback, callback_data);
270   gss_release_buffer(NULL, &nameBuffer);
271   if (rc!=0) {
272     tr_debug("trp_connection_auth: Error from gsscon_passive_authenticate(), rc = 0x%08X.", rc);
273     return -1;
274   }
275
276   tr_debug("trp_connection_auth: beginning second stage authentication");
277   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
278     tr_debug("trp_connection_auth: Error from gsscon_authorize, rc = %d, autherr = %d.", 
279              rc, autherr);
280     return -1;
281   }
282
283   trp_connection_set_peer(conn);
284   trp_connection_set_status(conn, TRP_CONNECTION_UP);
285
286   if (auth)
287     tr_debug("trp_connection_auth: Connection authenticated, fd = %d.", trp_connection_get_fd(conn));
288   else
289     tr_debug("trp_connection_auth: Authentication failed, fd = %d.", trp_connection_get_fd(conn));
290
291   return !auth;
292 }
293
294 /* Accept connection */
295 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname)
296 {
297   int conn_fd=-1;
298   TRP_CONNECTION *conn=NULL;
299
300   conn_fd = accept(listen, NULL, NULL);
301
302   if (0 > conn_fd) {
303     tr_notice("trp_connection_accept: accept() returned error.");
304     return NULL;
305   }
306   conn=trp_connection_new(mem_ctx);
307   trp_connection_set_fd(conn, conn_fd);
308   trp_connection_set_gssname(conn, gssname);
309   return conn;
310 }
311
312 /* Initiate connection */
313 TRP_RC trp_connection_initiate(TRP_CONNECTION *conn, char *server, unsigned int port)
314 {
315   int err = 0;
316   int fd=-1;
317   unsigned int use_port=0;
318
319   if (0 == port)
320     use_port = TRP_PORT;
321   else 
322     use_port = port;
323
324   if (conn==NULL) {
325     tr_err("trp_connection_initiate: null TRP_CONNECTION");
326     return TRP_BADARG;
327   }
328
329   tr_debug("trp_connection_initiate: opening GSS connection to %s:%d",
330            server,
331            use_port);
332   err = gsscon_connect(server,
333                        use_port,
334                        "trustrouter",
335                       &fd,
336                        trp_connection_get_gssctx(conn));
337   tr_debug("trp_connection_initiate: connected");
338
339   if (err) {
340     talloc_free(conn);
341     return TRP_ERROR;
342   } else {
343     trp_connection_set_fd(conn, fd);
344     trp_connection_set_peer(conn);
345     trp_connection_set_status(conn, TRP_CONNECTION_UP);
346     return TRP_SUCCESS;
347   }
348 }