Separate TRP from main trust router code.
[trust_router.git] / tr / tr_trp.c
1 #include <fcntl.h>
2 #include <event2/event.h>
3 #include <talloc.h>
4 #include <errno.h>
5 #include <unistd.h>
6
7 #include <gsscon.h>
8 #include <tr_rp.h>
9 #include <trp_internal.h>
10 #include <tr_config.h>
11 #include <tr_event.h>
12 #include <tr_debug.h>
13 #include <tr_trp.h>
14
15 /* hold a trps instance and a config manager */
16 struct tr_trps_event_cookie {
17   TRPS_INSTANCE *trps;
18   TR_CFG_MGR *cfg_mgr;
19 };
20
21
22 static int tr_trps_req_handler (TRPS_INSTANCE *trps,
23                                 TRP_REQ *orig_req, 
24                                 TRP_RESP *resp,
25                                 void *tr_in)
26 {
27   if (orig_req != NULL) 
28     free(orig_req);
29   return -1; /* not handling anything right now */
30 }
31
32
33 static int tr_trps_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
34                                void *cookie_in)
35 {
36   TR_RP_CLIENT *rp;
37   struct tr_trps_event_cookie *cookie=(struct tr_trps_event_cookie *)cookie_in;
38   TRPS_INSTANCE *trps = cookie->trps;
39   TR_CFG_MGR *cfg_mgr = cookie->cfg_mgr;
40
41   tr_debug("tr_trps_gss_handler()");
42
43   if ((!client_name) || (!gss_name) || (!trps) || (!cfg_mgr)) {
44     tr_debug("tr_trps_gss_handler: Bad parameters.");
45     return -1;
46   }
47   
48   /* look up the RP client matching the GSS name */
49   if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, gss_name)))) {
50     tr_debug("tr_trps_gss_handler: Unknown GSS name %s", gss_name->buf);
51     return -1;
52   }
53
54   trps->rp_gss = rp;
55   tr_debug("Client's GSS Name: %s", gss_name->buf);
56
57   return 0;
58 }
59
60
61 /* called when a connection to the TRPS port is received */
62 static void tr_trps_event_cb(int listener, short event, void *arg)
63 {
64   TRPS_INSTANCE *trps = (TRPS_INSTANCE *)arg;
65   int conn=-1;
66
67   if (0==(event & EV_READ)) {
68     tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
69   } else {
70     conn=trps_accept(trps, listener);
71     if (conn>0) {
72       /* need to monitor this fd and trigger events when read becomes possible */
73     }
74   }
75 }
76
77
78 /* Configure the trps instance and set up its event handler.
79  * Returns 0 on success, nonzero on failure. Fills in
80  * *trps_event (which should be allocated by caller). */
81 int tr_trps_event_init(struct event_base *base,
82                        TRPS_INSTANCE *trps,
83                        TR_CFG_MGR *cfg_mgr,
84                        struct tr_socket_event *trps_ev)
85 {
86   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
87   struct tr_trps_event_cookie *cookie;
88   int retval=0;
89
90   if (trps_ev == NULL) {
91     tr_debug("tr_trps_event_init: Null trps_ev.");
92     retval=1;
93     goto cleanup;
94   }
95
96   /* Create the cookie for callbacks. It is part of the trps context, so it will
97    * be cleaned up when trps is freed by talloc_free. */
98   cookie=talloc(tmp_ctx, struct tr_trps_event_cookie);
99   if (cookie == NULL) {
100     tr_debug("tr_trps_event_init: Unable to allocate cookie.");
101     retval=1;
102     goto cleanup;
103   }
104   cookie->trps=trps;
105   cookie->cfg_mgr=cfg_mgr;
106   talloc_steal(trps, cookie);
107
108   /* get a trps listener */
109   trps_ev->sock_fd=trps_get_listener(trps,
110                                      tr_trps_req_handler,
111                                      tr_trps_gss_handler,
112                                      cfg_mgr->active->internal->hostname,
113                                      cfg_mgr->active->internal->trps_port,
114                                      (void *)cookie);
115   if (trps_ev->sock_fd < 0) {
116     tr_crit("Error opening TRP server socket.");
117     retval=1;
118     goto cleanup;
119   }
120
121   /* and its event */
122   trps_ev->ev=event_new(base,
123                         trps_ev->sock_fd,
124                         EV_READ|EV_PERSIST,
125                         tr_trps_event_cb,
126                         (void *)trps);
127   event_add(trps_ev->ev, NULL);
128
129 cleanup:
130   talloc_free(tmp_ctx);
131   return retval;
132 }