Make outgoing connections. Connect to self as a test.
[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 /* Threading note: mutex lock is only used for protecting get_status() and set_status().
10  * If needed, locking for other operations (notably adding/removing connections) must be managed
11  * by whomever is holding on to the connection list. */
12
13 int trp_connection_lock(TRP_CONNECTION *conn)
14 {
15   return pthread_mutex_lock(&(conn->mutex));
16 }
17
18 int trp_connection_unlock(TRP_CONNECTION *conn)
19 {
20   return pthread_mutex_unlock(&(conn->mutex));
21 }
22
23 int trp_connection_get_fd(TRP_CONNECTION *conn)
24 {
25   return conn->fd;
26 }
27
28 void trp_connection_set_fd(TRP_CONNECTION *conn, int fd)
29 {
30   conn->fd=fd;
31 }
32
33 TR_NAME *trp_connection_get_gssname(TRP_CONNECTION *conn)
34 {
35   return conn->gssname;
36 }
37
38 void trp_connection_set_gssname(TRP_CONNECTION *conn, TR_NAME *gssname)
39 {
40   conn->gssname=gssname;
41 }
42
43 gss_ctx_id_t *trp_connection_get_gssctx(TRP_CONNECTION *conn)
44 {
45   return conn->gssctx;
46 }
47
48 void trp_connection_set_gssctx(TRP_CONNECTION *conn, gss_ctx_id_t *gssctx)
49 {
50   conn->gssctx=gssctx;
51 }
52
53 TRP_CONNECTION_STATUS trp_connection_get_status(TRP_CONNECTION *conn)
54 {
55   TRP_CONNECTION_STATUS status;
56   trp_connection_lock(conn);
57   status=conn->status;
58   trp_connection_unlock(conn);
59   return status;
60 }
61
62 static void trp_connection_set_status(TRP_CONNECTION *conn, TRP_CONNECTION_STATUS status)
63 {
64   trp_connection_lock(conn);
65   conn->status=status;
66   trp_connection_unlock(conn);
67 }
68
69 pthread_t *trp_connection_get_thread(TRP_CONNECTION *conn)
70 {
71   return conn->thread;
72 }
73
74 void trp_connection_set_thread(TRP_CONNECTION *conn, pthread_t *thread)
75 {
76   conn->thread=thread;
77 }
78
79 TRP_CONNECTION *trp_connection_get_next(TRP_CONNECTION *conn)
80 {
81   return conn->next;
82 }
83
84 static void trp_connection_set_next(TRP_CONNECTION *conn, TRP_CONNECTION *next)
85 {
86   conn->next=next;
87 }
88
89 /* Ok to call more than once; guarantees connection no longer in the list. Does not free removed element.
90  * Returns handle to new list, you must replace your old handle on the list with this.  */
91 TRP_CONNECTION *trp_connection_remove(TRP_CONNECTION *conn, TRP_CONNECTION *remove)
92 {
93   TRP_CONNECTION *cur=conn;
94   TRP_CONNECTION *last=NULL;
95
96   if (cur==NULL)
97     return NULL;
98
99   /* first element is a special case */
100   if (cur==remove) {
101     conn=trp_connection_get_next(cur); /* advance list head */
102   } else {
103     /* it was not the first element */
104     last=cur;
105     cur=trp_connection_get_next(cur);
106     while (cur!=NULL) {
107       if (cur==remove) {
108         trp_connection_set_next(last, trp_connection_get_next(cur));
109         break;
110       }
111       last=cur;
112       cur=trp_connection_get_next(cur);
113     }
114   }
115   return conn;
116 }
117
118 static TRP_CONNECTION *trp_connection_get_tail(TRP_CONNECTION *conn)
119 {
120   while((conn!=NULL)&&(trp_connection_get_next(conn)!=NULL))
121     conn=trp_connection_get_next(conn);
122   return conn;
123 }
124
125 void trp_connection_append(TRP_CONNECTION *conn, TRP_CONNECTION *new)
126 {
127   trp_connection_set_next(trp_connection_get_tail(conn), new);
128 }
129
130 static void trp_connection_mutex_init(TRP_CONNECTION *conn)
131 {
132   pthread_mutex_init(&(conn->mutex), NULL);
133 }
134
135 /* talloc destructor for a connection: ensures connection is closed, memory freed */
136 static int trp_connection_destructor(void *object)
137 {
138   TRP_CONNECTION *conn=talloc_get_type_abort(object, TRP_CONNECTION); /* aborts on wrong type */
139   if ((trp_connection_get_status(conn)!=TRP_CONNECTION_DOWN)
140      && (trp_connection_get_fd(conn)!=-1))
141     close(trp_connection_get_fd(conn));
142   if (conn->gssname!=NULL)
143     tr_free_name(conn->gssname);
144   return 0;
145 }
146
147 TRP_CONNECTION *trp_connection_new(TALLOC_CTX *mem_ctx)
148 {
149   TRP_CONNECTION *new_conn=talloc(mem_ctx, TRP_CONNECTION);
150   gss_ctx_id_t *gssctx=NULL;
151   pthread_t *thread=NULL;
152   
153
154   if (new_conn != NULL) {
155     trp_connection_set_next(new_conn, NULL);
156     trp_connection_set_fd(new_conn, -1);
157     trp_connection_set_gssname(new_conn, NULL);
158     trp_connection_mutex_init(new_conn);
159     trp_connection_set_status(new_conn, TRP_CONNECTION_DOWN);
160     thread=talloc(new_conn, pthread_t);
161     gssctx=talloc(new_conn, gss_ctx_id_t);
162     if (gssctx==NULL) {
163       talloc_free(new_conn);
164       return NULL;
165     }
166     trp_connection_set_gssctx(new_conn, gssctx);
167     if (thread==NULL) {
168       talloc_free(new_conn);
169       return NULL;
170     }
171     trp_connection_set_thread(new_conn, thread);
172     talloc_set_destructor((void *)new_conn, trp_connection_destructor);
173   }
174   return new_conn;
175 }
176
177 void trp_connection_free(TRP_CONNECTION *conn)
178 {
179   talloc_free(conn);
180 }
181
182 void trp_connection_close(TRP_CONNECTION *conn)
183 {
184   close(trp_connection_get_fd(conn));
185   trp_connection_set_fd(conn, -1);
186   trp_connection_set_status(conn, TRP_CONNECTION_DOWN);
187 }
188
189 /* returns 0 on authorization success, 1 on failure, or -1 in case of error */
190 int trp_connection_auth(TRP_CONNECTION *conn, TRP_AUTH_FUNC auth_callback, void *callback_data)
191 {
192   int rc = 0;
193   int auth, autherr = 0;
194   gss_buffer_desc nameBuffer = {0, NULL};
195   gss_ctx_id_t *gssctx=trp_connection_get_gssctx(conn);
196
197   /* TODO: shouldn't really peek into TR_NAME... */
198   nameBuffer.length = trp_connection_get_gssname(conn)->len;
199   nameBuffer.value = trp_connection_get_gssname(conn)->buf;
200
201   tr_debug("trp_connection_auth: beginning passive authentication");
202   rc = gsscon_passive_authenticate(trp_connection_get_fd(conn), nameBuffer, gssctx, auth_callback, callback_data);
203   gss_release_buffer(NULL, &nameBuffer);
204   if (rc!=0) {
205     tr_debug("trp_connection_auth: Error from gsscon_passive_authenticate(), rc = 0x%08X.", rc);
206     return -1;
207   }
208
209   tr_debug("trp_connection_auth: beginning second stage authentication");
210   if (rc = gsscon_authorize(*gssctx, &auth, &autherr)) {
211     tr_debug("trp_connection_auth: Error from gsscon_authorize, rc = %d, autherr = %d.", 
212              rc, autherr);
213     return -1;
214   }
215
216   if (auth)
217     tr_debug("trp_connection_auth: Connection authenticated, fd = %d.", trp_connection_get_fd(conn));
218   else
219     tr_debug("trp_connection_auth: Authentication failed, fd = %d.", trp_connection_get_fd(conn));
220
221   return !auth;
222 }
223
224 /* Accept connection */
225 TRP_CONNECTION *trp_connection_accept(TALLOC_CTX *mem_ctx, int listen, TR_NAME *gssname)
226 {
227   int conn_fd=-1;
228   TRP_CONNECTION *conn=NULL;
229
230   conn_fd = accept(listen, NULL, NULL);
231
232   if (0 > conn_fd) {
233     tr_notice("trp_connection_accept: accept() returned error.");
234     return NULL;
235   }
236   conn=trp_connection_new(mem_ctx);
237   trp_connection_set_fd(conn, conn_fd);
238   trp_connection_set_gssname(conn, gssname);
239   trp_connection_set_status(conn, TRP_CONNECTION_UP);
240   return conn;
241 }
242
243 /* Initiate connection */
244 TRP_RC trp_connection_initiate(TRP_CONNECTION *conn, const char *server, unsigned int port)
245 {
246   int err = 0;
247   int fd=-1;
248   unsigned int use_port=0;
249
250   if (0 == port)
251     use_port = TRP_PORT;
252   else 
253     use_port = port;
254
255   if (conn==NULL) {
256     tr_err("trp_connection_initiate: null TRP_CONNECTION");
257     return TRP_BADARG;
258   }
259
260   tr_debug("trp_connection_initiate: opening GSS connection to %s:%d",
261            server,
262            use_port);
263   err = gsscon_connect(server,
264                        use_port,
265                        "trustrouter",
266                       &fd,
267                        trp_connection_get_gssctx(conn));
268   tr_debug("trp_connection_initiate: connected");
269
270   if (err) {
271     talloc_free(conn);
272     return TRP_ERROR;
273   } else {
274     trp_connection_set_fd(conn, fd);
275     trp_connection_set_status(conn, TRP_CONNECTION_UP);
276     return TRP_SUCCESS;
277   }
278 }