diff --git a/src/occtl/occtl.c b/src/occtl/occtl.c index aaba1a31..93934640 100644 --- a/src/occtl/occtl.c +++ b/src/occtl/occtl.c @@ -63,6 +63,8 @@ static const commands_st commands[] = { "Prints the banned IP addresses", 1, 1), ENTRY("show ip ban points", NULL, handle_list_banned_points_cmd, "Prints all the known IP addresses which have points", 1, 1), + ENTRY("show iroutes", NULL, handle_list_iroutes_cmd, + "Prints the routes provided by users of the server", 1, 1), ENTRY("show user", "[NAME]", handle_show_user_cmd, "Prints information on the specified user", 1, 1), ENTRY("show id", "[ID]", handle_show_id_cmd, diff --git a/src/occtl/occtl.h b/src/occtl/occtl.h index b71b471c..fdca7e10 100644 --- a/src/occtl/occtl.h +++ b/src/occtl/occtl.h @@ -77,6 +77,7 @@ typedef int (*cmd_func) (CONN_TYPE * conn, const char *arg, cmd_params_st *param int handle_status_cmd(CONN_TYPE * conn, const char *arg, cmd_params_st *params); int handle_list_users_cmd(CONN_TYPE * conn, const char *arg, cmd_params_st *params); +int handle_list_iroutes_cmd(CONN_TYPE * conn, const char *arg, cmd_params_st *params); int handle_list_banned_ips_cmd(CONN_TYPE * conn, const char *arg, cmd_params_st *params); int handle_list_banned_points_cmd(CONN_TYPE * conn, const char *arg, cmd_params_st *params); int handle_show_user_cmd(CONN_TYPE * conn, const char *arg, cmd_params_st *params); diff --git a/src/occtl/unix.c b/src/occtl/unix.c index 93564f4d..dc93cb06 100644 --- a/src/occtl/unix.c +++ b/src/occtl/unix.c @@ -641,6 +641,91 @@ int handle_list_users_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st * return ret; } +int handle_list_iroutes_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st *params) +{ + int ret; + struct cmd_reply_st raw; + UserListRep *rep = NULL; + FILE *out; + unsigned i, j; + PROTOBUF_ALLOCATOR(pa, ctx); + + init_reply(&raw); + + entries_clear(); + + out = pager_start(params); + + /* get all user info */ + ret = send_cmd(ctx, CTL_CMD_LIST, NULL, NULL, NULL, &raw); + if (ret < 0) { + goto error; + } + + rep = user_list_rep__unpack(&pa, raw.data_size, raw.data); + if (rep == NULL) + goto error; + + /* print iroutes */ + if (NO_JSON(params)) { + for (i=0;in_user;i++) { + const char *username, *vpn_ip; + + username = rep->user[i]->username; + if (username == NULL || username[0] == 0) + username = NO_USER; + + vpn_ip = get_ip(rep->user[i]->local_ip, rep->user[i]->local_ip6); + + /* add header */ + if (i == 0) { + fprintf(out, "%6s %8s %6s %16s %28s\n", + "id", "user", "device", "vpn-ip", "iroute"); + } + + for (j=0;juser[i]->n_iroutes;j++) + fprintf(out, "%6d %8s %6s %16s %28s", + (int)rep->user[i]->id, username, rep->user[i]->tun, vpn_ip, rep->user[i]->iroutes[j]); + + } + } else { + print_start_block(out, params); + for (i=0;in_user;i++) { + const char *username, *vpn_ip; + + username = rep->user[i]->username; + if (username == NULL || username[0] == 0) + username = NO_USER; + + vpn_ip = get_ip(rep->user[i]->local_ip, rep->user[i]->local_ip6); + + print_single_value_int(out, params, "ID", rep->user[i]->id, 1); + print_single_value(out, params, "Username", username, 1); + print_single_value(out, params, "Device", rep->user[i]->tun, 1); + print_single_value(out, params, "IP", vpn_ip, 1); + print_list_entries(out, params, "iRoutes", rep->user[i]->iroutes, rep->user[i]->n_iroutes, 1); + print_single_value(out, params, "IP", vpn_ip, 0); + } + print_end_block(out, params, 0); + } + + ret = 0; + goto cleanup; + + error: + ret = 1; + fprintf(stderr, ERR_SERVER_UNREACHABLE); + + cleanup: + if (rep != NULL) + user_list_rep__free_unpacked(rep, &pa); + + free_reply(&raw); + pager_stop(out); + + return ret; +} + static int handle_list_banned_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st *params, unsigned points) {