Connect to hard-coded peer and exchange route info. Buggy and incomplete.
[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     new_conn->peer=NULL; /* no true set function for this */
228     thread=talloc(new_conn, pthread_t);
229     gssctx=talloc(new_conn, gss_ctx_id_t);
230     if (gssctx==NULL) {
231       talloc_free(new_conn);
232       return NULL;
233     }
234     trp_connection_set_gssctx(new_conn, gssctx);
235     if (thread==NULL) {
236       talloc_free(new_conn);
237       return NULL;
238     }
239     trp_connection_set_thread(new_conn, thread);
240     talloc_set_destructor((void *)new_conn, trp_connection_destructor);
241   }
242   return new_conn;
243 }
244
245 void trp_connection_free(TRP_CONNECTION *conn)
246 {
247   talloc_free(conn);
248 }
249
250 void trp_connection_close(TRP_CONNECTION *conn)
251 {
252   close(trp_connection_get_fd(conn));
253   trp_connection_set_fd(conn, -1);
254   trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
255 }
256
257 /* returns 0 on authorization success, 1 on failure, or -1 in case of error */
258 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data)
259 {
260   int rc = 0;
261   int auth, autherr = 0;
262   gss_buffer_desc nameBuffer = {0, NULL};
263   gss_ctx_id_t *gssctx=trp_connection_get_gssctx(conn);
264
265   /* TODO: shouldn't really peek into TR_NAME... */
266   nameBuffer.length = trp_connection_get_gssname(conn)->len;
267   nameBuffer.value = trp_connection_get_gssname(conn)->buf;
268
269   tr_debug("trp_connection_auth: beginning passive authentication");
270   rc = gsscon_passive_authenticate(trp_connection_get_fd(conn), nameBuffer, gssctx, auth_callback, callback_data);
271   gss_release_buffer(NULL, &nameBuffer);
272   if (rc!=0) {
273     tr_debug("trp_connection_auth: Error from gsscon_passive_authenticate(), rc = 0x%08X.", rc);
274     return -1;
275   }
276
277   tr_debug("trp_connection_auth: beginning second stage authentication");
278   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
279     tr_debug("trp_connection_auth: Error from gsscon_authorize, rc = %d, autherr = %d.", 
280              rc, autherr);
281     return -1;
282   }
283
284   trp_connection_set_peer(conn);
285   trp_connection_set_status(conn, TRP_CONNECTION_UP);
286
287   if (auth)
288     tr_debug("trp_connection_auth: Connection authenticated, fd = %d.", trp_connection_get_fd(conn));
289   else
290     tr_debug("trp_connection_auth: Authentication failed, fd = %d.", trp_connection_get_fd(conn));
291
292   return !auth;
293 }
294
295 /* Accept connection */
296 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname)
297 {
298   int conn_fd=-1;
299   TRP_CONNECTION *conn=NULL;
300
301   conn_fd = accept(listen, NULL, NULL);
302
303   if (0 > conn_fd) {
304     tr_notice("trp_connection_accept: accept() returned error.");
305     return NULL;
306   }
307   conn=trp_connection_new(mem_ctx);
308   trp_connection_set_fd(conn, conn_fd);
309   trp_connection_set_gssname(conn, gssname);
310   return conn;
311 }
312
313 /* Initiate connection */
314 TRP_RC trp_connection_initiate(TRP_CONNECTION *conn, char *server, unsigned int port)
315 {
316   int err = 0;
317   int fd=-1;
318   unsigned int use_port=0;
319
320   if (0 == port)
321     use_port = TRP_PORT;
322   else 
323     use_port = port;
324
325   if (conn==NULL) {
326     tr_err("trp_connection_initiate: null TRP_CONNECTION");
327     return TRP_BADARG;
328   }
329
330   tr_debug("trp_connection_initiate: opening GSS connection to %s:%d",
331            server,
332            use_port);
333   err = gsscon_connect(server,
334                        use_port,
335                        "trustrouter",
336                       &fd,
337                        trp_connection_get_gssctx(conn));
338   if (err) {
339     tr_debug("trp_connection_initiate: connection failed.");
340     talloc_free(conn);
341     return TRP_ERROR;
342   } else {
343     tr_debug("trp_connection_initiate: connected.");
344     trp_connection_set_fd(conn, fd);
345     trp_connection_set_peer(conn);
346     trp_connection_set_status(conn, TRP_CONNECTION_UP);
347     return TRP_SUCCESS;
348   }
349 }