Decode JSON TRP messages, then send to main thread.
[trust_router.git] / trp / trp_conn.c
1 #include <gsscon.h>
2 #include <fcntl.h>
3 #include <talloc.h>
4 #include <unistd.h>
5
6 #include <tr_debug.h>
7 #include <trp_internal.h>
8
9 int trp_connection_get_fd(TRP_CONNECTION *conn)
10 {
11   return conn->fd;
12 }
13
14 void trp_connection_set_fd(TRP_CONNECTION *conn, int fd)
15 {
16   conn->fd=fd;
17 }
18
19 TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn)
20 {
21   return conn->gssname;
22 }
23
24 void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname)
25 {
26   conn->gssname=gssname;
27 }
28
29 gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn)
30 {
31   return conn->gssctx;
32 }
33
34 void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx)
35 {
36   conn->gssctx=gssctx;
37 }
38
39 TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn)
40 {
41   TRP_CONNECTION_STATUS status;
42   pthread_mutex_lock(&(conn->status_mutex));
43   status=conn->status;
44   pthread_mutex_unlock(&(conn->status_mutex));
45   return status;
46 }
47
48 static void trp_connection_set_status(TRP_CONNECTION *conn, TRP_CONNECTION_STATUS status)
49 {
50   pthread_mutex_lock(&(conn->status_mutex));
51   conn->status=status;
52   pthread_mutex_unlock(&(conn->status_mutex));
53 }
54
55 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn)
56 {
57   return conn->thread;
58 }
59
60 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread)
61 {
62   conn->thread=thread;
63 }
64
65 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn)
66 {
67   return conn->next;
68 }
69
70 static void trp_connection_set_next(TRP_CONNECTION *conn, TRP_CONNECTION *next)
71 {
72   conn->next=next;
73 }
74
75 /* Ok to call more than once; guarantees connection no longer in the list.
76  * Returns handle to new list, you must replace your old handle on the list with this.  */
77 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove)
78 {
79   TRP_CONNECTION *cur=conn;
80   TRP_CONNECTION *last=NULL;
81
82   if (cur==NULL)
83     return NULL;
84
85   /* first element is a special case */
86   if (cur==remove) {
87     conn=trp_connection_get_next(cur); /* advance list head */
88     trp_connection_free(cur);
89   } else {
90     /* it was not the first element */
91     last=cur;
92     cur=trp_connection_get_next(cur);
93     while (cur!=NULL) {
94       if (cur==remove) {
95         trp_connection_set_next(last, trp_connection_get_next(cur));
96         trp_connection_free(cur);
97         break;
98       }
99       last=cur;
100       cur=trp_connection_get_next(cur);
101     }
102   }
103   return conn;
104 }
105
106 static TRP_CONNECTION *trp_connection_get_tail(TRP_CONNECTION *conn)
107 {
108   while((conn!=NULL)&&(trp_connection_get_next(conn)!=NULL))
109     conn=trp_connection_get_next(conn);
110   return conn;
111 }
112
113 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new)
114 {
115   trp_connection_set_next(trp_connection_get_tail(conn), new);
116 }
117
118 static void trp_connection_mutex_init(TRP_CONNECTION *conn)
119 {
120   pthread_mutex_init(&(conn->status_mutex), NULL);
121 }
122
123 /* talloc destructor for a connection: ensures connection is closed, memory freed */
124 static int trp_connection_destructor(void *object)
125 {
126   TRP_CONNECTION *conn=talloc_get_type_abort(object, TRP_CONNECTION); /* aborts on wrong type */
127   if ((trp_connection_get_status(conn)!=TRP_CONNECTION_DOWN)
128      && (trp_connection_get_fd(conn)!=-1))
129     close(trp_connection_get_fd(conn));
130   if (conn->gssname!=NULL)
131     tr_free_name(conn->gssname);
132   return 0;
133 }
134
135 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx)
136 {
137   TRP_CONNECTION *new_conn=talloc(mem_ctx, TRP_CONNECTION);
138   gss_ctx_id_t *gssctx=NULL;
139   pthread_t *thread=NULL;
140   
141
142   if (new_conn != NULL) {
143     trp_connection_set_next(new_conn, NULL);
144     trp_connection_set_fd(new_conn, -1);
145     trp_connection_set_gssctx(new_conn, NULL);
146     trp_connection_mutex_init(new_conn);
147     trp_connection_set_status(new_conn, TRP_CONNECTION_DOWN);
148     thread=talloc(new_conn, pthread_t);
149     gssctx=talloc(new_conn, gss_ctx_id_t);
150     if (gssctx==NULL) {
151       talloc_free(new_conn);
152       return NULL;
153     }
154     trp_connection_set_gssctx(new_conn, gssctx);
155     if (thread==NULL) {
156       talloc_free(new_conn);
157       return NULL;
158     }
159     trp_connection_set_thread(new_conn, thread);
160     talloc_set_destructor((void *)new_conn, trp_connection_destructor);
161   }
162   return new_conn;
163 }
164
165 void trp_connection_free(TRP_CONNECTION *conn)
166 {
167   talloc_free(conn);
168 }
169
170 void trp_connection_close(TRP_CONNECTION *conn)
171 {
172   close(trp_connection_get_fd(conn));
173   trp_connection_set_fd(conn, -1);
174   trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
175 }
176
177 /* returns 0 on authorization success, 1 on failure, or -1 in case of error */
178 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data)
179 {
180   int rc = 0;
181   int auth, autherr = 0;
182   gss_buffer_desc nameBuffer = {0, NULL};
183   gss_ctx_id_t *gssctx=trp_connection_get_gssctx(conn);
184
185   /* TODO: shouldn't really peek into TR_NAME... */
186   nameBuffer.length = trp_connection_get_gssname(conn)->len;
187   nameBuffer.value = trp_connection_get_gssname(conn)->buf;
188
189   tr_debug("trp_connection_auth: beginning passive authentication");
190   rc = gsscon_passive_authenticate(trp_connection_get_fd(conn), nameBuffer, gssctx, auth_callback, callback_data);
191   gss_release_buffer(NULL, &nameBuffer);
192   if (rc!=0) {
193     tr_debug("trp_connection_auth: Error from gsscon_passive_authenticate(), rc = 0x%08X.", rc);
194     return -1;
195   }
196
197   tr_debug("trp_connection_auth: beginning second stage authentication");
198   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
199     tr_debug("trp_connection_auth: Error from gsscon_authorize, rc = %d, autherr = %d.", 
200              rc, autherr);
201     return -1;
202   }
203
204   if (auth)
205     tr_debug("trp_connection_auth: Connection authenticated, fd = %d.", trp_connection_get_fd(conn));
206   else
207     tr_debug("trp_connection_auth: Authentication failed, fd = %d.", trp_connection_get_fd(conn));
208
209   return !auth;
210 }
211
212 /* Accept connection */
213 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname)
214 {
215   int conn_fd=-1;
216   TRP_CONNECTION *conn=NULL;
217
218   conn_fd = accept(listen, NULL, NULL);
219
220   if (0 > conn_fd) {
221     tr_notice("trp_connection_accept: accept() returned error.");
222     return NULL;
223   }
224   conn=trp_connection_new(mem_ctx);
225   trp_connection_set_fd(conn, conn_fd);
226   trp_connection_set_gssname(conn, gssname);
227   trp_connection_set_status(conn, TRP_CONNECTION_UP);
228   return conn;
229 }
230