From 39572b3d4842a144a0e511fabdb5d83678f705fd Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Sun, 12 Jan 2014 14:35:58 +0100 Subject: [PATCH] Store User-Agent information and send to occtl. --- src/ipc.proto | 1 + src/main-ctl-handler.c | 8 +++++++- src/main-misc.c | 2 ++ src/main.h | 1 + src/occtl.c | 23 +++++++++++++++++++++-- src/vpn.h | 1 + src/worker-vpn.c | 17 +++++++++++++++++ src/worker.h | 2 ++ 8 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/ipc.proto b/src/ipc.proto index 959a4780..20375c6b 100644 --- a/src/ipc.proto +++ b/src/ipc.proto @@ -93,4 +93,5 @@ message session_info_msg { required string tls_ciphersuite = 1; required string dtls_ciphersuite = 2; + required string user_agent = 3; } diff --git a/src/main-ctl-handler.c b/src/main-ctl-handler.c index a67b267c..17c19aa8 100644 --- a/src/main-ctl-handler.c +++ b/src/main-ctl-handler.c @@ -71,7 +71,7 @@ typedef struct { #define ENTRY(name, iface, desc, func) \ {name, sizeof(name)-1, iface, sizeof(iface)-1, desc, sizeof(desc)-1, func} -#define LIST_USERS_SIG "(ussssssssussss)" +#define LIST_USERS_SIG "(ussssssssusssss)" #define DESC_LIST \ " \n" \ @@ -496,6 +496,12 @@ static int append_user_info(DBusMessageIter * subs, struct proc_st *ctmp) return -1; } + strtmp = ctmp->user_agent; + if (dbus_message_iter_append_basic + (subs, DBUS_TYPE_STRING, &strtmp) == 0) { + return -1; + } + if (ctmp->auth_status == PS_AUTH_COMPLETED) strtmp = "connected"; else if (ctmp->auth_status == PS_AUTH_INIT) diff --git a/src/main-misc.c b/src/main-misc.c index b88381cc..0173c56a 100644 --- a/src/main-misc.c +++ b/src/main-misc.c @@ -432,6 +432,8 @@ int handle_commands(main_server_st *s, struct proc_st* proc) snprintf(proc->tls_ciphersuite, sizeof(proc->tls_ciphersuite), "%s", tmsg->tls_ciphersuite); if (tmsg->dtls_ciphersuite) snprintf(proc->dtls_ciphersuite, sizeof(proc->dtls_ciphersuite), "%s", tmsg->dtls_ciphersuite); + if (tmsg->user_agent) + snprintf(proc->user_agent, sizeof(proc->user_agent), "%s", tmsg->user_agent); session_info_msg__free_unpacked(tmsg, NULL); diff --git a/src/main.h b/src/main.h index 6b0b0387..28658497 100644 --- a/src/main.h +++ b/src/main.h @@ -100,6 +100,7 @@ struct proc_st { char hostname[MAX_HOSTNAME_SIZE]; /* the requested hostname */ uint8_t cookie[COOKIE_SIZE]; /* the cookie associated with the session */ + char user_agent[MAX_AGENT_SIZE]; char tls_ciphersuite[MAX_CIPHERSUITE_NAME]; char dtls_ciphersuite[MAX_DTLS_CIPHERSUITE_NAME]; diff --git a/src/occtl.c b/src/occtl.c index 70e78203..5028950d 100644 --- a/src/occtl.c +++ b/src/occtl.c @@ -534,6 +534,7 @@ int handle_list_users_cmd(DBusConnection * conn, const char *arg) char *vpn_ipv4 = "", *vpn_ptp_ipv4 = ""; char *vpn_ipv6 = "", *vpn_ptp_ipv6 = ""; char *hostname = "", *auth = "", *device = ""; + char *user_agent = ""; char str_since[64]; const char *vpn_ip; struct tm *tm; @@ -641,6 +642,13 @@ int handle_list_users_cmd(DBusConnection * conn, const char *arg) goto error_parse; dbus_message_iter_get_basic(&subs, &hostname); + if (!dbus_message_iter_next(&subs)) + goto error_recv; + + if (dbus_message_iter_get_arg_type(&subs) != DBUS_TYPE_STRING) + goto error_parse; + dbus_message_iter_get_basic(&subs, &user_agent); + if (!dbus_message_iter_next(&subs)) goto error_recv; @@ -724,6 +732,7 @@ int common_info_cmd(DBusMessageIter * args) char *vpn_ipv4 = "", *vpn_ptp_ipv4 = ""; char *vpn_ipv6 = "", *vpn_ptp_ipv6 = ""; char *hostname = "", *auth = "", *device = ""; + char *user_agent = ""; char str_since[64]; struct tm *tm; time_t t; @@ -826,6 +835,13 @@ int common_info_cmd(DBusMessageIter * args) goto error_parse; dbus_message_iter_get_basic(&subs, &hostname); + if (!dbus_message_iter_next(&subs)) + goto error_recv; + + if (dbus_message_iter_get_arg_type(&subs) != DBUS_TYPE_STRING) + goto error_parse; + dbus_message_iter_get_basic(&subs, &user_agent); + if (!dbus_message_iter_next(&subs)) goto error_recv; @@ -868,11 +884,14 @@ int common_info_cmd(DBusMessageIter * args) } fprintf(out, "\tDevice: %s ", device); - if (hostname != NULL && hostname[0] != 0) - fprintf(out, "Hostname: %s\n", hostname); + if (user_agent != NULL && user_agent[0] != 0) + fprintf(out, "User-Agent: %s\n", user_agent); else fprintf(out, "\n"); + if (hostname != NULL && hostname[0] != 0) + fprintf(out, "\tHostname: %s\n", hostname); + fprintf(out, "\tConnected at: %s (", str_since); print_time_ival7(t, out); fprintf(out, ")\n"); diff --git a/src/vpn.h b/src/vpn.h index 6696ce42..b674ffbd 100644 --- a/src/vpn.h +++ b/src/vpn.h @@ -227,6 +227,7 @@ struct main_server_st; #define MAX_BANNER_SIZE 256 #define MAX_USERNAME_SIZE 64 +#define MAX_AGENT_SIZE 32 #define MAX_PASSWORD_SIZE 64 #define TLS_MASTER_SIZE 48 #define MAX_HOSTNAME_SIZE MAX_USERNAME_SIZE diff --git a/src/worker-vpn.c b/src/worker-vpn.c index fc760ee4..9ba9e32e 100644 --- a/src/worker-vpn.c +++ b/src/worker-vpn.c @@ -169,6 +169,7 @@ int url_cb(http_parser * parser, const char *at, size_t length) } #define STR_HDR_COOKIE "Cookie" +#define STR_HDR_USER_AGENT "User-Agent" #define STR_HDR_CONNECTION "Connection" #define STR_HDR_MS "X-DTLS-Master-Secret" #define STR_HDR_CS "X-DTLS-CipherSuite" @@ -220,6 +221,14 @@ static void value_check(struct worker_st *ws, struct http_req_st *req) memcpy(req->hostname, req->value.data, req->value.length); req->hostname[req->value.length] = 0; break; + case HEADER_USER_AGENT: + if (req->value.length + 1 > MAX_AGENT_SIZE) { + req->user_agent[0] = 0; + return; + } + memcpy(req->user_agent, req->value.data, req->value.length); + req->user_agent[req->value.length] = 0; + break; case HEADER_DTLS_CIPHERSUITE: str = (char *)req->value.data; @@ -395,6 +404,10 @@ static void header_check(struct http_req_st *req) strncmp((char *)req->header.data, STR_HDR_CONNECTION, req->header.length) == 0) { req->next_header = HEADER_CONNECTION; + } else if (req->header.length == sizeof(STR_HDR_USER_AGENT) - 1 && + strncmp((char *)req->header.data, STR_HDR_USER_AGENT, + req->header.length) == 0) { + req->next_header = HEADER_USER_AGENT; } else { req->next_header = 0; } @@ -764,6 +777,10 @@ void session_info_send(worker_st * ws) msg.dtls_ciphersuite = ws->req.selected_ciphersuite; } + if (ws->req.user_agent[0] != 0) { + msg.user_agent = ws->req.user_agent; + } + send_msg_to_main(ws, CMD_SESSION_INFO, &msg, (pack_size_func) session_info_msg__get_packed_size, (pack_func) session_info_msg__pack); diff --git a/src/worker.h b/src/worker.h index 76bdc725..b1d6654f 100644 --- a/src/worker.h +++ b/src/worker.h @@ -51,6 +51,7 @@ enum { HEADER_DTLS_MTU, HEADER_DTLS_CIPHERSUITE, HEADER_CONNECTION, + HEADER_USER_AGENT, }; enum { @@ -74,6 +75,7 @@ struct http_req_st { unsigned int header_state; char hostname[MAX_HOSTNAME_SIZE]; + char user_agent[MAX_AGENT_SIZE]; unsigned int next_header; unsigned char cookie[COOKIE_SIZE]; unsigned int cookie_set;