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 /* stand-in for a function that finds the connection for a particular peer */
40 static TRP_CONNECTION *trps_find_connection(TRPS_INSTANCE *trps)
46 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
51 trp_connection_append(trps->conn, new);
53 talloc_steal(trps, new);
56 int trps_send_msg (TRPS_INSTANCE *trps,
59 const char *msg_content)
64 /* Send the request over the connection */
65 if (err = gsscon_write_encrypted_token (conn,
68 strlen(msg_content))) {
69 tr_err( "trps_send_msg: Error sending message over connection.\n");
76 static int trps_listen (TRPS_INSTANCE *trps, int port)
83 struct sockaddr_storage storage;
84 struct sockaddr_in in4;
87 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
89 saddr->sin_port = htons (port);
90 saddr->sin_family = AF_INET;
91 saddr->sin_addr.s_addr = INADDR_ANY;
93 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
96 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
98 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
101 if (0 > (rc = listen(conn, 512)))
104 tr_debug("trps_listen: TRP Server listening on port %d", port);
108 /* returns EACCES if authorization is denied */
109 int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data)
111 TRPS_INSTANCE *inst = (TRPS_INSTANCE *)data;
114 if (0!=inst->auth_handler(clientName, displayName, inst->cookie)) {
115 tr_debug("trps_auth_cb: client '%.*s' denied authorization.", displayName->length, displayName->value);
116 result=EACCES; /* denied */
123 static int trps_read_message (TRPS_INSTANCE *trps, int conn, gss_ctx_id_t *gssctx, char **msg)
129 if (err = gsscon_read_encrypted_token(conn, *gssctx, &buf, &buflen)) {
135 tr_debug("trps_read_request(): Request Received, %u bytes.", (unsigned) buflen);
136 tr_debug("trps_read_request(): %.*s", buflen, buf);
138 *msg=talloc_strndup(NULL, buf, buflen); /* no context owns this! */
144 int trps_get_listener(TRPS_INSTANCE *trps,
145 TRP_REQ_FUNC req_handler,
146 TRP_AUTH_FUNC auth_handler,
147 const char *hostname,
153 if (0 > (listen = trps_listen(trps, port))) {
155 if (0 == strerror_r(errno, errbuf, 256)) {
156 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
158 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
163 /* opening port succeeded */
164 tr_debug("trps_get_listener: Opened port %d.", port);
166 /* make this socket non-blocking */
167 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
168 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
175 /* store the caller's request handler & cookie */
176 trps->req_handler = req_handler;
177 trps->auth_handler = auth_handler;
178 trps->hostname = talloc_strdup(trps, hostname);
180 trps->cookie = cookie;
188 static gss_ctx_id_t trps_establish_gss_context (TRPS_INSTANCE *trps, int conn)
190 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
191 gss_ctx_id_t gssctx = GSS_C_NO_CONTEXT;
196 if (trps_auth_connection(trps, conn, &gssctx))
197 tr_notice("trps_establish_gss_context: Error authorizing TID Server connection.");
199 tr_notice("trps_establish_gss_context: Connection authorized!");
202 msg_len = trps_read_message(trps, conn, &gssctx, &msg_rec);
203 talloc_steal(tmp_ctx, msg_rec); /* get this in our context */
205 tr_debug("trps_handle_connection: Error from trps_read_message()");
209 tr_debug("trps_handle_connection: msg_len=%d", msg_len);
210 reply=talloc_asprintf(tmp_ctx, "TRPS heard: %.*s", msg_len, msg_rec);
211 if (0 > (rc = trps_send_msg(trps, conn, gssctx, reply))) {
212 tr_debug("trps_handle_connection: Error from trps_send_message(), rc = %d.", rc);
216 talloc_free(tmp_ctx);