Fix memory freeing bugs. Seems stable, even through loss of connections.
[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=TRP_CONNECTION_UNKNOWN;
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_STATUS old_status=TRP_CONNECTION_UNKNOWN;
130   trp_connection_lock(conn);
131   old_status=conn->status;
132   conn->status=status;
133   trp_connection_unlock(conn);
134   if ((status!=old_status) && (conn->status_change_cb!=NULL))
135       conn->status_change_cb(conn, conn->status_change_cookie);
136 }
137
138 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn)
139 {
140   return conn->thread;
141 }
142
143 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread)
144 {
145   conn->thread=thread;
146 }
147
148 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn)
149 {
150   return conn->next;
151 }
152
153 static void trp_connection_set_next(TRP_CONNECTION *conn, TRP_CONNECTION *next)
154 {
155   conn->next=next;
156 }
157
158 /* Ok to call more than once; guarantees connection no longer in the list. Does not free removed element.
159  * Returns handle to new list, you must replace your old handle on the list with this.  */
160 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove)
161 {
162   TRP_CONNECTION *cur=conn;
163   TRP_CONNECTION *last=NULL;
164
165   if (cur==NULL)
166     return NULL;
167
168   /* first element is a special case */
169   if (cur==remove) {
170     conn=trp_connection_get_next(cur); /* advance list head */
171   } else {
172     /* it was not the first element */
173     last=cur;
174     cur=trp_connection_get_next(cur);
175     while (cur!=NULL) {
176       if (cur==remove) {
177         trp_connection_set_next(last, trp_connection_get_next(cur));
178         break;
179       }
180       last=cur;
181       cur=trp_connection_get_next(cur);
182     }
183   }
184   return conn;
185 }
186
187 static TRP_CONNECTION *trp_connection_get_tail(TRP_CONNECTION *conn)
188 {
189   while((conn!=NULL)&&(trp_connection_get_next(conn)!=NULL))
190     conn=trp_connection_get_next(conn);
191   return conn;
192 }
193
194 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new)
195 {
196   trp_connection_set_next(trp_connection_get_tail(conn), new);
197 }
198
199 static void trp_connection_mutex_init(TRP_CONNECTION *conn)
200 {
201   pthread_mutex_init(&(conn->mutex), NULL);
202 }
203
204 /* talloc destructor for a connection: ensures connection is closed, memory freed */
205 static int trp_connection_destructor(void *object)
206 {
207   TRP_CONNECTION *conn=talloc_get_type_abort(object, TRP_CONNECTION); /* aborts on wrong type */
208   if ((trp_connection_get_status(conn)!=TRP_CONNECTION_CLOSED)
209      && (trp_connection_get_fd(conn)!=-1))
210     close(trp_connection_get_fd(conn));
211   if (conn->peer!=NULL)
212     tr_free_name(conn->peer);
213   if (conn->gssname!=NULL)
214     tr_free_name(conn->gssname);
215   return 0;
216 }
217
218 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx)
219 {
220   TRP_CONNECTION *new_conn=talloc(mem_ctx, TRP_CONNECTION);
221   gss_ctx_id_t *gssctx=NULL;
222   pthread_t *thread=NULL;
223   
224
225   if (new_conn != NULL) {
226     trp_connection_set_next(new_conn, NULL);
227     trp_connection_set_fd(new_conn, -1);
228     trp_connection_set_gssname(new_conn, NULL);
229     trp_connection_mutex_init(new_conn);
230     new_conn->peer=NULL; /* no true set function for this */
231     new_conn->status_change_cb=NULL;
232     new_conn->status_change_cookie=NULL;
233     new_conn->status=TRP_CONNECTION_CLOSED;
234
235     thread=talloc(new_conn, pthread_t);
236     if (thread==NULL) {
237       talloc_free(new_conn);
238       return NULL;
239     }
240     trp_connection_set_thread(new_conn, thread);
241
242     gssctx=talloc(new_conn, gss_ctx_id_t);
243     if (gssctx==NULL) {
244       talloc_free(new_conn);
245       return NULL;
246     }
247     trp_connection_set_gssctx(new_conn, gssctx);
248     talloc_set_destructor((void *)new_conn, trp_connection_destructor);
249   }
250   return new_conn;
251 }
252
253 void trp_connection_free(TRP_CONNECTION *conn)
254 {
255   talloc_free(conn);
256 }
257
258 void trp_connection_close(TRP_CONNECTION *conn)
259 {
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_debug("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     trp_connection_set_peer(conn);
358     trp_connection_set_status(conn, TRP_CONNECTION_UP);
359     return TRP_SUCCESS;
360   }
361 }