[141b079] | 1 | #include <jansson.h> |
---|
| 2 | #include <netinet/in.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <syslog.h> |
---|
| 5 | |
---|
| 6 | #include "json.h" |
---|
| 7 | #include "rest.h" |
---|
| 8 | #include "legacy.h" |
---|
| 9 | |
---|
| 10 | #define LEGACY_CMD_MAX 4096 |
---|
| 11 | |
---|
| 12 | static const char *og_cmd_wol_to_legacy(struct og_cmd_json *cmd) |
---|
| 13 | { |
---|
| 14 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 15 | const json_t *root = cmd->json; |
---|
| 16 | const char *wol_type; |
---|
| 17 | uint32_t type; |
---|
| 18 | int len; |
---|
| 19 | |
---|
| 20 | wol_type = json_string_value(json_object_get(root, "type")); |
---|
| 21 | if (!wol_type) |
---|
| 22 | return NULL; |
---|
| 23 | |
---|
| 24 | if (!strcmp(wol_type, "broadcast")) |
---|
| 25 | type = 1; |
---|
| 26 | else |
---|
| 27 | type = 2; |
---|
| 28 | |
---|
| 29 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), "nfn=Arrancar\rmar=%u", type); |
---|
| 30 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 31 | return NULL; |
---|
| 32 | |
---|
| 33 | return strdup(legacy_cmd); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | static const char *og_cmd_poweroff_to_legacy(struct og_cmd_json *cmd) |
---|
| 37 | { |
---|
| 38 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 39 | int len; |
---|
| 40 | |
---|
| 41 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), "nfn=Apagar"); |
---|
| 42 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 43 | return NULL; |
---|
| 44 | |
---|
| 45 | return strdup(legacy_cmd); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | static const char *og_cmd_reboot_to_legacy(struct og_cmd_json *cmd) |
---|
| 49 | { |
---|
| 50 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 51 | int len; |
---|
| 52 | |
---|
| 53 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), "nfn=Reiniciar"); |
---|
| 54 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 55 | return NULL; |
---|
| 56 | |
---|
| 57 | return strdup(legacy_cmd); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static const char *og_cmd_session_to_legacy(struct og_cmd_json *cmd) |
---|
| 61 | { |
---|
| 62 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 63 | const json_t *root = cmd->json; |
---|
| 64 | const char *dsk, *par; |
---|
| 65 | int len; |
---|
| 66 | |
---|
| 67 | dsk = json_string_value(json_object_get(root, "disk")); |
---|
| 68 | if (!dsk) |
---|
| 69 | return NULL; |
---|
| 70 | par = json_string_value(json_object_get(root, "part")); |
---|
| 71 | if (!par) |
---|
| 72 | return NULL; |
---|
| 73 | |
---|
| 74 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), |
---|
| 75 | "nfn=IniciarSesion\rdsk=%s\rpar=%s", |
---|
| 76 | dsk, par); |
---|
| 77 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 78 | return NULL; |
---|
| 79 | |
---|
| 80 | return strdup(legacy_cmd); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | static const char *og_cmd_software_to_legacy(struct og_cmd_json *cmd) |
---|
| 84 | { |
---|
| 85 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 86 | const json_t *root = cmd->json; |
---|
| 87 | const char *dsk, *par; |
---|
| 88 | int len; |
---|
| 89 | |
---|
| 90 | dsk = json_string_value(json_object_get(root, "disk")); |
---|
| 91 | if (!dsk) |
---|
| 92 | return NULL; |
---|
| 93 | par = json_string_value(json_object_get(root, "partition")); |
---|
| 94 | if (!par) |
---|
| 95 | return NULL; |
---|
| 96 | |
---|
| 97 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), |
---|
| 98 | "nfn=InventarioSoftware\rdsk=%s\rpar=%s", |
---|
| 99 | dsk, par); |
---|
| 100 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 101 | return NULL; |
---|
| 102 | |
---|
| 103 | return strdup(legacy_cmd); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | static const char *og_cmd_hardware_to_legacy(struct og_cmd_json *cmd) |
---|
| 107 | { |
---|
| 108 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 109 | int len; |
---|
| 110 | |
---|
| 111 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), |
---|
| 112 | "nfn=InventarioHardware"); |
---|
| 113 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 114 | return NULL; |
---|
| 115 | |
---|
| 116 | return strdup(legacy_cmd); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | static const char *og_cmd_shell_run_to_legacy(struct og_cmd_json *cmd) |
---|
| 120 | { |
---|
| 121 | const json_t *root = cmd->json; |
---|
| 122 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 123 | const char *scp; |
---|
| 124 | int len; |
---|
| 125 | |
---|
| 126 | scp = json_string_value(json_object_get(root, "run")); |
---|
| 127 | if (!scp) |
---|
| 128 | return NULL; |
---|
| 129 | |
---|
| 130 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), |
---|
| 131 | "nfn=EjecutarScript\rscp=%s", scp); |
---|
| 132 | if (len >= (int)sizeof(legacy_cmd)) { |
---|
| 133 | syslog(LOG_ERR, "script payload too large (%s:%d)\n", |
---|
| 134 | __func__, __LINE__); |
---|
| 135 | return NULL; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return strdup(legacy_cmd); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | static char *og_cmd_image_create_to_legacy(struct og_cmd_json *cmd) |
---|
| 142 | { |
---|
| 143 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 144 | struct og_msg_params params = {}; |
---|
| 145 | json_t *root = cmd->json; |
---|
| 146 | int len; |
---|
| 147 | |
---|
| 148 | if (og_json_parse_create_image(root, ¶ms) < 0) |
---|
| 149 | return NULL; |
---|
| 150 | |
---|
| 151 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), |
---|
| 152 | "nfn=CrearImagen\rdsk=%s\rpar=%s\rcpt=%s\ridi=%s\rnci=%s\ripr=%s", |
---|
| 153 | params.disk, params.partition, params.code, params.id, |
---|
| 154 | params.name, params.repository); |
---|
| 155 | if (len >= (int)sizeof(legacy_cmd)) |
---|
| 156 | return NULL; |
---|
| 157 | |
---|
| 158 | return strdup(legacy_cmd); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | static const char *og_cmd_image_restore_to_legacy(struct og_cmd_json *cmd) |
---|
| 162 | { |
---|
| 163 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 164 | struct og_msg_params params = {}; |
---|
| 165 | json_t *root = cmd->json; |
---|
| 166 | int len; |
---|
| 167 | |
---|
| 168 | if (og_json_parse_restore_image(root, ¶ms) < 0) |
---|
| 169 | return NULL; |
---|
| 170 | |
---|
| 171 | len = snprintf(legacy_cmd, sizeof(legacy_cmd), |
---|
| 172 | "nfn=RestaurarImagen\rdsk=%s\rpar=%s\ridi=%s\rnci=%s\ripr=%s\rifs=%s\rptc=%s", |
---|
| 173 | params.disk, params.partition, params.id, params.name, |
---|
| 174 | params.repository, params.profile, params.type); |
---|
| 175 | if (len >= (int)sizeof(legacy_cmd)) { |
---|
| 176 | return NULL; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return strdup(legacy_cmd); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | static const char *og_cmd_setup_to_legacy(struct og_cmd_json *cmd) |
---|
| 183 | { |
---|
| 184 | char legacy_cmd[LEGACY_CMD_MAX + 1] = {}; |
---|
| 185 | uint32_t bufsiz = sizeof(legacy_cmd); |
---|
| 186 | const char *dsk, *ttp, *che, *tch; |
---|
| 187 | struct og_msg_params params = {}; |
---|
| 188 | json_t *partition_setup, *value; |
---|
| 189 | const json_t *root = cmd->json; |
---|
| 190 | uint32_t consumed = 0; |
---|
| 191 | size_t index; |
---|
| 192 | int len; |
---|
| 193 | |
---|
| 194 | dsk = json_string_value(json_object_get(root, "disk")); |
---|
| 195 | if (!dsk) |
---|
| 196 | return NULL; |
---|
| 197 | ttp = json_string_value(json_object_get(root, "type")); |
---|
| 198 | if (!ttp) |
---|
| 199 | return NULL; |
---|
| 200 | che = json_string_value(json_object_get(root, "cache")); |
---|
| 201 | if (!che) |
---|
| 202 | return NULL; |
---|
| 203 | tch = json_string_value(json_object_get(root, "cache_size")); |
---|
| 204 | if (!tch) |
---|
| 205 | return NULL; |
---|
| 206 | |
---|
| 207 | len = snprintf(legacy_cmd + consumed, bufsiz, "nfn=Configurar\rttp=%s\rdsk=%s\rcfg=dis=%s*che=%s*tch=%s!", |
---|
| 208 | ttp, dsk, dsk, che, tch); |
---|
| 209 | if (len >= bufsiz) |
---|
| 210 | return NULL; |
---|
| 211 | consumed += len; |
---|
| 212 | if (consumed < bufsiz) |
---|
| 213 | bufsiz -= len; |
---|
| 214 | |
---|
| 215 | partition_setup = json_object_get(root, "partition_setup"); |
---|
| 216 | if (!partition_setup) |
---|
| 217 | return NULL; |
---|
| 218 | if (og_json_parse_partition_setup(partition_setup, ¶ms) < 0) |
---|
| 219 | return NULL; |
---|
| 220 | |
---|
| 221 | json_array_foreach(partition_setup, index, value) { |
---|
| 222 | len = snprintf(legacy_cmd + consumed, bufsiz, "par=%s*cpt=%s*sfi=%s*tam=%s*ope=%s%%", |
---|
| 223 | params.partition_setup[index].number, |
---|
| 224 | params.partition_setup[index].code, |
---|
| 225 | params.partition_setup[index].filesystem, |
---|
| 226 | params.partition_setup[index].size, |
---|
| 227 | params.partition_setup[index].format); |
---|
| 228 | if (len >= bufsiz) |
---|
| 229 | return NULL; |
---|
| 230 | consumed += len; |
---|
| 231 | if (consumed < bufsiz) |
---|
| 232 | bufsiz -= len; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | return strdup(legacy_cmd); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | const char *og_msg_params_to_legacy(struct og_cmd_json *cmd) |
---|
| 239 | { |
---|
| 240 | const char *legacy_cmd = NULL; |
---|
| 241 | |
---|
| 242 | if (!strncmp(cmd->type, "wol", strlen("wol"))) |
---|
| 243 | legacy_cmd = og_cmd_wol_to_legacy(cmd); |
---|
| 244 | else if (!strncmp(cmd->type, "poweroff", strlen("poweroff"))) |
---|
| 245 | legacy_cmd = og_cmd_poweroff_to_legacy(cmd); |
---|
| 246 | else if (!strncmp(cmd->type, "reboot", strlen("reboot"))) |
---|
| 247 | legacy_cmd = og_cmd_reboot_to_legacy(cmd); |
---|
| 248 | else if (!strncmp(cmd->type, "session", strlen("session"))) |
---|
| 249 | legacy_cmd = og_cmd_session_to_legacy(cmd); |
---|
| 250 | else if (!strncmp(cmd->type, "software", strlen("software"))) |
---|
| 251 | legacy_cmd = og_cmd_software_to_legacy(cmd); |
---|
| 252 | else if (!strncmp(cmd->type, "hardware", strlen("hardware"))) |
---|
| 253 | legacy_cmd = og_cmd_hardware_to_legacy(cmd); |
---|
| 254 | else if (!strncmp(cmd->type, "run", strlen("run"))) |
---|
| 255 | legacy_cmd = og_cmd_shell_run_to_legacy(cmd); |
---|
| 256 | else if (!strncmp(cmd->type, "image_create", strlen("image_create"))) |
---|
| 257 | legacy_cmd = og_cmd_image_create_to_legacy(cmd); |
---|
| 258 | else if (!strncmp(cmd->type, "image_restore", strlen("image_restore"))) |
---|
| 259 | legacy_cmd = og_cmd_image_restore_to_legacy(cmd); |
---|
| 260 | else if (!strncmp(cmd->type, "setup", strlen("setup"))) |
---|
| 261 | legacy_cmd = og_cmd_setup_to_legacy(cmd); |
---|
| 262 | |
---|
| 263 | if (!legacy_cmd) { |
---|
| 264 | syslog(LOG_ERR, "failed to translate command %s (%s:%d)\n", |
---|
| 265 | cmd->type, __func__, __LINE__); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | return legacy_cmd; |
---|
| 269 | } |
---|