3 #include <event2/event.h>
10 #include <trp_internal.h>
11 #include <tr_config.h>
16 /* hold a trps instance and a config manager */
17 struct tr_trps_event_cookie {
23 static int tr_trps_req_handler (TRPS_INSTANCE *trps,
29 return -1; /* not handling anything right now */
33 static int tr_trps_gss_handler(gss_name_t client_name, gss_buffer_t gss_name,
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 TR_NAME name={gss_name->value, gss_name->length};
42 tr_debug("tr_trps_gss_handler()");
44 if ((!client_name) || (!gss_name) || (!trps) || (!cfg_mgr)) {
45 tr_debug("tr_trps_gss_handler: Bad parameters.");
49 /* look up the RP client matching the GSS name */
50 if ((NULL == (rp = tr_rp_client_lookup(cfg_mgr->active->rp_clients, &name)))) {
51 tr_debug("tr_trps_gss_handler: Unknown GSS name %.*s", name.len, name.buf);
55 /*trps->rp_gss = rp;*/
56 tr_debug("Client's GSS Name: %.*s", name.len, name.buf);
62 /* data passed to thread */
67 /* thread to handle GSS connections to peers */
68 static void *tr_trps_conn_thread(void *arg)
70 struct thread_data *thread_data=talloc_get_type_abort(arg, struct thread_data);
71 TRP_CONNECTION *conn=thread_data->conn;
72 TRPS_INSTANCE *trps=thread_data->trps;
74 tr_debug("tr_trps_conn_thread: started");
75 /* try to establish a GSS context */
76 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
77 tr_notice("tr_trps_conn_thread: failed to authorize connection");
80 tr_notice("tr_trps_conn_thread: authorized connection");
84 /* called when a connection to the TRPS port is received */
85 static void tr_trps_event_cb(int listener, short event, void *arg)
87 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
88 TRPS_INSTANCE *trps = talloc_get_type_abort(arg, TRPS_INSTANCE); /* aborts on wrong type */
89 TRP_CONNECTION *conn=NULL;
90 TR_NAME *gssname=NULL;
92 struct thread_data *thread_data;
94 if (0==(event & EV_READ)) {
95 tr_debug("tr_trps_event_cb: unexpected event on TRPS socket (event=0x%X)", event);
97 /* create a thread to handle this connection */
98 asprintf(&name, "trustrouter@%s", trps->hostname);
99 gssname=tr_new_name(name);
100 free(name); name=NULL;
101 conn=trp_connection_accept(tmp_ctx, listener, gssname, trps_auth_cb, NULL, trps);
103 /* need to monitor this fd and trigger events when read becomes possible */
104 thread_data=talloc(conn, struct thread_data);
105 if (thread_data==NULL) {
106 tr_err("tr_trps_event_cb: unable to allocate thread_data");
107 talloc_free(tmp_ctx);
110 thread_data->conn=conn;
111 thread_data->trps=trps;
112 pthread_create(conn->thread, NULL, tr_trps_conn_thread, thread_data);
113 trps_add_connection(trps, conn); /* remember the connection */
116 talloc_free(tmp_ctx);
120 /* Configure the trps instance and set up its event handler.
121 * Returns 0 on success, nonzero on failure. Fills in
122 * *trps_event (which should be allocated by caller). */
123 int tr_trps_event_init(struct event_base *base,
126 struct tr_socket_event *trps_ev)
128 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
129 struct tr_trps_event_cookie *cookie;
132 if (trps_ev == NULL) {
133 tr_debug("tr_trps_event_init: Null trps_ev.");
138 /* Create the cookie for callbacks. It is part of the trps context, so it will
139 * be cleaned up when trps is freed by talloc_free. */
140 cookie=talloc(tmp_ctx, struct tr_trps_event_cookie);
141 if (cookie == NULL) {
142 tr_debug("tr_trps_event_init: Unable to allocate cookie.");
147 cookie->cfg_mgr=cfg_mgr;
148 talloc_steal(trps, cookie);
150 /* get a trps listener */
151 trps_ev->sock_fd=trps_get_listener(trps,
154 cfg_mgr->active->internal->hostname,
155 cfg_mgr->active->internal->trps_port,
157 if (trps_ev->sock_fd < 0) {
158 tr_crit("Error opening TRP server socket.");
164 trps_ev->ev=event_new(base,
169 event_add(trps_ev->ev, NULL);
172 talloc_free(tmp_ctx);