|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <stdbool.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <netdb.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | +#include <time.h> |
| 9 | + |
| 10 | +#include <arpa/inet.h> |
| 11 | + |
| 12 | +#define MIN_PACKET 10 |
| 13 | +#define MAX_PACKET 4096 |
| 14 | +#define MAX_BODY (MAX_PACKET - (3 * sizeof(uint32_t)) - 2) |
| 15 | + |
| 16 | +#define RCON_HOST "127.0.0.1" |
| 17 | + |
| 18 | +typedef enum { |
| 19 | + RCON_TYPE_RESPONSE = 0, |
| 20 | + RCON_TYPE_EXECCOMMAND = 2, |
| 21 | + RCON_TYPE_AUTH_RESPONSE = 2, |
| 22 | + RCON_TYPE_AUTH = 3, |
| 23 | +} packet_type; |
| 24 | + |
| 25 | +typedef struct { |
| 26 | + uint32_t length; |
| 27 | + uint32_t id; |
| 28 | + packet_type type; |
| 29 | + char body[MAX_BODY]; |
| 30 | +} packet; |
| 31 | + |
| 32 | +int rcon_open(const char *port); |
| 33 | +void rcon_create(packet* pkt, packet_type type, const char* body); |
| 34 | +bool rcon_send(int rcon_socket, const packet* pkt); |
| 35 | +bool rcon_auth(int rcon_socket, const char* password); |
| 36 | +bool rcon_recv(int rcon_socket, packet* pkt, packet_type expected_type); |
| 37 | +char* combine_args(int argc, char* argv[]); |
| 38 | +char* read_password(const char* conf_dir); |
| 39 | + |
| 40 | +int main(int argc, char* argv[]) { |
| 41 | + if (argc < 2) { |
| 42 | + fprintf(stderr, "error: missing command argument\n"); |
| 43 | + return EXIT_FAILURE; |
| 44 | + } |
| 45 | + |
| 46 | + srand((unsigned int)time(NULL)); |
| 47 | + |
| 48 | + const char* port = getenv("RCON_PORT"); |
| 49 | + if (port == NULL) { |
| 50 | + fprintf(stderr, "error: missing $RCON_PORT env\n"); |
| 51 | + return EXIT_FAILURE; |
| 52 | + } |
| 53 | + |
| 54 | + const char* conf_dir = getenv("CONFIG"); |
| 55 | + if (conf_dir == NULL) { |
| 56 | + fprintf(stderr, "error: missing $CONFIG env"); |
| 57 | + exit(EXIT_FAILURE); |
| 58 | + } |
| 59 | + |
| 60 | + int rcon_socket = rcon_open(port); |
| 61 | + if (rcon_socket == -1) { |
| 62 | + fprintf(stderr, "error: could not connect\n"); |
| 63 | + return EXIT_FAILURE; |
| 64 | + } |
| 65 | + |
| 66 | + if (!rcon_auth(rcon_socket, read_password(conf_dir))) { |
| 67 | + fprintf(stderr, "error: login failed\n"); |
| 68 | + return EXIT_FAILURE; |
| 69 | + } |
| 70 | + |
| 71 | + packet pkt; |
| 72 | + rcon_create(&pkt, RCON_TYPE_EXECCOMMAND, combine_args(argc, argv)); |
| 73 | + if (!rcon_send(rcon_socket, &pkt)) { |
| 74 | + fprintf(stderr, "error: send command failed\n"); |
| 75 | + return EXIT_FAILURE; |
| 76 | + } |
| 77 | + |
| 78 | + if (rcon_recv(rcon_socket, &pkt, RCON_TYPE_RESPONSE) && pkt.length > 0) { |
| 79 | + puts(pkt.body); |
| 80 | + } |
| 81 | + |
| 82 | + return EXIT_SUCCESS; |
| 83 | +} |
| 84 | + |
| 85 | +char* combine_args(int argc, char* argv[]) { |
| 86 | + // combine all cli arguments |
| 87 | + char* command = malloc(MAX_BODY); |
| 88 | + memset(command, 0, MAX_BODY); |
| 89 | + strcat(command, argv[1]); |
| 90 | + |
| 91 | + for (int idx = 2; idx < argc; idx++) { |
| 92 | + strcat(command, " "); |
| 93 | + strcat(command, argv[idx]); |
| 94 | + } |
| 95 | + |
| 96 | + return command; |
| 97 | +} |
| 98 | + |
| 99 | +char* read_password(const char* conf_dir) { |
| 100 | + char* path = malloc(strlen(conf_dir) + 64); |
| 101 | + strcpy(path, conf_dir); |
| 102 | + strcat(path, "/rconpw"); |
| 103 | + |
| 104 | + FILE* fptr = fopen(path, "r"); |
| 105 | + fseek(fptr, 0, SEEK_END); |
| 106 | + long fsize = ftell(fptr); |
| 107 | + fseek(fptr, 0, SEEK_SET); /* same as rewind(f); */ |
| 108 | + |
| 109 | + char *password = malloc(fsize + 1); |
| 110 | + fread(password, fsize, 1, fptr); |
| 111 | + fclose(fptr); |
| 112 | + |
| 113 | + password[fsize] = 0; |
| 114 | + if (password[fsize-1] == '\n') { |
| 115 | + password[fsize-1] = 0; |
| 116 | + } |
| 117 | + |
| 118 | + return password; |
| 119 | +} |
| 120 | + |
| 121 | +int rcon_open(const char *port) { |
| 122 | + struct sockaddr_in address = { |
| 123 | + .sin_family = AF_INET, |
| 124 | + .sin_port = htons(atoi(port)) |
| 125 | + }; |
| 126 | + inet_aton(RCON_HOST, &address.sin_addr); |
| 127 | + |
| 128 | + int rcon_socket = socket(AF_INET, SOCK_STREAM, 0); |
| 129 | + if (connect(rcon_socket, (struct sockaddr*) &address, sizeof(address)) < 0) { |
| 130 | + return -1; |
| 131 | + } else { |
| 132 | + return rcon_socket; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void rcon_create(packet* pkt, packet_type type, const char* body) { |
| 137 | + size_t body_length = strlen(body); |
| 138 | + if (body_length >= MAX_BODY - 2) { |
| 139 | + fprintf(stderr, "error: command to long"); |
| 140 | + exit(EXIT_FAILURE); |
| 141 | + } |
| 142 | + |
| 143 | + pkt->id = abs(rand()); |
| 144 | + pkt->type = type; |
| 145 | + pkt->length = (uint32_t)(sizeof(pkt->id) + sizeof(pkt->type) + body_length + 2); |
| 146 | + |
| 147 | + memset(pkt->body, 0, MAX_BODY); |
| 148 | + strncpy(pkt->body, body, MAX_BODY); |
| 149 | +} |
| 150 | + |
| 151 | +bool rcon_recv(int rcon_socket, packet* pkt, packet_type expected_type) { |
| 152 | + memset(pkt, 0, sizeof(*pkt)); |
| 153 | + |
| 154 | + // Read response packet length |
| 155 | + ssize_t expected_length_bytes = sizeof(pkt->length); |
| 156 | + ssize_t rx_bytes = recv(rcon_socket, &(pkt->length), expected_length_bytes, 0); |
| 157 | + |
| 158 | + if (rx_bytes == -1) { |
| 159 | + perror("error: socket error"); |
| 160 | + return false; |
| 161 | + } else if (rx_bytes == 0) { |
| 162 | + fprintf(stderr, "error: no data recieved\n"); |
| 163 | + return false; |
| 164 | + } else if (rx_bytes < expected_length_bytes || pkt->length < MIN_PACKET || pkt->length > MAX_PACKET) { |
| 165 | + fprintf(stderr, "error: invalid data\n"); |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + ssize_t received = 0; |
| 170 | + while (received < pkt->length) { |
| 171 | + rx_bytes = recv(rcon_socket, (char *)pkt + sizeof(pkt->length) + received, pkt->length - received, 0); |
| 172 | + if (rx_bytes < 0) { |
| 173 | + perror("error: socket error"); |
| 174 | + return false; |
| 175 | + } else if (rx_bytes == 0) { |
| 176 | + fprintf(stderr, "error: connection lost\n"); |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + received += rx_bytes; |
| 181 | + } |
| 182 | + |
| 183 | + return pkt->type == expected_type; |
| 184 | +} |
| 185 | + |
| 186 | +bool rcon_send(int rcon_socket, const packet* pkt) { |
| 187 | + size_t length = sizeof(pkt->length) + pkt->length; |
| 188 | + char *ptr = (char*) pkt; |
| 189 | + |
| 190 | + while (length > 0) { |
| 191 | + ssize_t ret = send(rcon_socket, ptr, length, 0); |
| 192 | + |
| 193 | + if (ret == -1) { |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + ptr += ret; |
| 198 | + length -= ret; |
| 199 | + } |
| 200 | + |
| 201 | + return true; |
| 202 | +} |
| 203 | + |
| 204 | +bool rcon_auth(int rcon_socket, const char* password) { |
| 205 | + packet pkt; |
| 206 | + rcon_create(&pkt, RCON_TYPE_AUTH, password); |
| 207 | + |
| 208 | + if (!rcon_send(rcon_socket, &pkt)) { |
| 209 | + return false; |
| 210 | + } |
| 211 | + |
| 212 | + if (!rcon_recv(rcon_socket, &pkt, RCON_TYPE_AUTH_RESPONSE)) { |
| 213 | + return false; |
| 214 | + } |
| 215 | + |
| 216 | + return true; |
| 217 | +} |
0 commit comments