2 #include <event2/event.h>
11 #include <trp_internal.h>
14 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
16 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
22 trps->mq=tr_mq_new(trps);
24 /* failed to allocate mq */
32 void trps_free (TRPS_INSTANCE *trps)
38 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
40 return tr_mq_pop(trps->mq);
43 void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
45 tr_mq_append(trps->mq, msg);
48 /* stand-in for a function that finds the connection for a particular peer */
50 static TRP_CONNECTION *trps_find_connection(TRPS_INSTANCE *trps)
56 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
61 trp_connection_append(trps->conn, new);
63 talloc_steal(trps, new);
66 int trps_send_msg (TRPS_INSTANCE *trps,
69 const char *msg_content)
74 /* Send the request over the connection */
75 if (err = gsscon_write_encrypted_token (conn,
78 strlen(msg_content))) {
79 tr_err( "trps_send_msg: Error sending message over connection.\n");
86 static int trps_listen (TRPS_INSTANCE *trps, int port)
93 struct sockaddr_storage storage;
94 struct sockaddr_in in4;
97 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
99 saddr->sin_port = htons (port);
100 saddr->sin_family = AF_INET;
101 saddr->sin_addr.s_addr = INADDR_ANY;
103 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
106 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
108 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
111 if (0 > (rc = listen(conn, 512)))
114 tr_debug("trps_listen: TRP Server listening on port %d", port);
118 /* returns EACCES if authorization is denied */
119 int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data)
121 TRPS_INSTANCE *inst = (TRPS_INSTANCE *)data;
124 if (0!=inst->auth_handler(clientName, displayName, inst->cookie)) {
125 tr_debug("trps_auth_cb: client '%.*s' denied authorization.", displayName->length, displayName->value);
126 result=EACCES; /* denied */
133 static int trps_read_message (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t *gssctx, char **msg)
139 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
145 tr_debug("trps_read_request(): Request Received, %u bytes.", (unsigned) buflen);
146 tr_debug("trps_read_request(): %.*s", buflen, buf);
148 *msg=talloc_strndup(NULL, buf, buflen); /* no context owns this! */
154 int trps_get_listener(TRPS_INSTANCE *trps,
155 TRP_REQ_FUNC req_handler,
156 TRP_AUTH_FUNC auth_handler,
157 const char *hostname,
163 if (0 > (listen = trps_listen(trps, port))) {
165 if (0 == strerror_r(errno, errbuf, 256)) {
166 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
168 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
173 /* opening port succeeded */
174 tr_debug("trps_get_listener: Opened port %d.", port);
176 /* make this socket non-blocking */
177 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
178 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
185 /* store the caller's request handler & cookie */
186 trps->req_handler = req_handler;
187 trps->auth_handler = auth_handler;
188 trps->hostname = talloc_strdup(trps, hostname);
190 trps->cookie = cookie;
198 static gss_ctx_id_t trps_establish_gss_context (TRPS_INSTANCE *trps, int conn)
200 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
201 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
206 if (trps_auth_connection(trps, conn, &gssctx))
207 tr_notice("trps_establish_gss_context: Error authorizing TID Server connection.");
209 tr_notice("trps_establish_gss_context: Connection authorized!");
212 msg_len = trps_read_message(trps, conn, &gssctx, &msg_rec);
213 talloc_steal(tmp_ctx, msg_rec); /* get this in our context */
215 tr_debug("trps_handle_connection: Error from trps_read_message()");
219 tr_debug("trps_handle_connection: msg_len=%d", msg_len);
220 reply=talloc_asprintf(tmp_ctx, "TRPS heard: %.*s", msg_len, msg_rec);
221 if (0 > (rc = trps_send_msg(trps, conn, gssctx, reply))) {
222 tr_debug("trps_handle_connection: Error from trps_send_message(), rc = %d.", rc);
226 talloc_free(tmp_ctx);