1 | /* |
---|
2 | * Copyright (C) 2020 Soleta Networks <info@soleta.eu> |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify it under |
---|
5 | * the terms of the GNU Affero General Public License as published by the |
---|
6 | * Free Software Foundation, version 3. |
---|
7 | */ |
---|
8 | |
---|
9 | #include "ogAdmServer.h" |
---|
10 | #include "dbi.h" |
---|
11 | #include "utils.h" |
---|
12 | #include "list.h" |
---|
13 | #include "rest.h" |
---|
14 | #include "cfg.h" |
---|
15 | #include "schedule.h" |
---|
16 | #include <ev.h> |
---|
17 | #include <syslog.h> |
---|
18 | #include <sys/ioctl.h> |
---|
19 | #include <ifaddrs.h> |
---|
20 | #include <sys/types.h> |
---|
21 | #include <sys/stat.h> |
---|
22 | #include <fcntl.h> |
---|
23 | #include <jansson.h> |
---|
24 | #include <dirent.h> |
---|
25 | #include <time.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include <sys/wait.h> |
---|
29 | #include <sys/statvfs.h> |
---|
30 | |
---|
31 | struct ev_loop *og_loop; |
---|
32 | |
---|
33 | #define OG_REST_PARAM_ADDR (1UL << 0) |
---|
34 | #define OG_REST_PARAM_MAC (1UL << 1) |
---|
35 | #define OG_REST_PARAM_WOL_TYPE (1UL << 2) |
---|
36 | #define OG_REST_PARAM_RUN_CMD (1UL << 3) |
---|
37 | #define OG_REST_PARAM_DISK (1UL << 4) |
---|
38 | #define OG_REST_PARAM_PARTITION (1UL << 5) |
---|
39 | #define OG_REST_PARAM_REPO (1UL << 6) |
---|
40 | #define OG_REST_PARAM_NAME (1UL << 7) |
---|
41 | #define OG_REST_PARAM_ID (1UL << 8) |
---|
42 | #define OG_REST_PARAM_CODE (1UL << 9) |
---|
43 | #define OG_REST_PARAM_TYPE (1UL << 10) |
---|
44 | #define OG_REST_PARAM_PROFILE (1UL << 11) |
---|
45 | #define OG_REST_PARAM_CACHE (1UL << 12) |
---|
46 | #define OG_REST_PARAM_CACHE_SIZE (1UL << 13) |
---|
47 | #define OG_REST_PARAM_PART_0 (1UL << 14) |
---|
48 | #define OG_REST_PARAM_PART_1 (1UL << 15) |
---|
49 | #define OG_REST_PARAM_PART_2 (1UL << 16) |
---|
50 | #define OG_REST_PARAM_PART_3 (1UL << 17) |
---|
51 | #define OG_REST_PARAM_SYNC_SYNC (1UL << 18) |
---|
52 | #define OG_REST_PARAM_SYNC_DIFF (1UL << 19) |
---|
53 | #define OG_REST_PARAM_SYNC_REMOVE (1UL << 20) |
---|
54 | #define OG_REST_PARAM_SYNC_COMPRESS (1UL << 21) |
---|
55 | #define OG_REST_PARAM_SYNC_CLEANUP (1UL << 22) |
---|
56 | #define OG_REST_PARAM_SYNC_CACHE (1UL << 23) |
---|
57 | #define OG_REST_PARAM_SYNC_CLEANUP_CACHE (1UL << 24) |
---|
58 | #define OG_REST_PARAM_SYNC_REMOVE_DST (1UL << 25) |
---|
59 | #define OG_REST_PARAM_SYNC_DIFF_ID (1UL << 26) |
---|
60 | #define OG_REST_PARAM_SYNC_DIFF_NAME (1UL << 27) |
---|
61 | #define OG_REST_PARAM_SYNC_PATH (1UL << 28) |
---|
62 | #define OG_REST_PARAM_SYNC_METHOD (1UL << 29) |
---|
63 | #define OG_REST_PARAM_ECHO (1UL << 30) |
---|
64 | #define OG_REST_PARAM_TASK (1UL << 31) |
---|
65 | #define OG_REST_PARAM_TIME_YEARS (1UL << 32) |
---|
66 | #define OG_REST_PARAM_TIME_MONTHS (1UL << 33) |
---|
67 | #define OG_REST_PARAM_TIME_WEEKS (1UL << 34) |
---|
68 | #define OG_REST_PARAM_TIME_WEEK_DAYS (1UL << 35) |
---|
69 | #define OG_REST_PARAM_TIME_DAYS (1UL << 36) |
---|
70 | #define OG_REST_PARAM_TIME_HOURS (1UL << 37) |
---|
71 | #define OG_REST_PARAM_TIME_AM_PM (1UL << 38) |
---|
72 | #define OG_REST_PARAM_TIME_MINUTES (1UL << 39) |
---|
73 | #define OG_REST_PARAM_NETMASK (1UL << 40) |
---|
74 | #define OG_REST_PARAM_SCOPE (1UL << 41) |
---|
75 | #define OG_REST_PARAM_MODE (1UL << 42) |
---|
76 | |
---|
77 | static LIST_HEAD(client_list); |
---|
78 | |
---|
79 | void og_client_add(struct og_client *cli) |
---|
80 | { |
---|
81 | list_add(&cli->list, &client_list); |
---|
82 | } |
---|
83 | |
---|
84 | static struct og_client *og_client_find(const char *ip) |
---|
85 | { |
---|
86 | struct og_client *client; |
---|
87 | struct in_addr addr; |
---|
88 | int res; |
---|
89 | |
---|
90 | res = inet_aton(ip, &addr); |
---|
91 | if (!res) { |
---|
92 | syslog(LOG_ERR, "Invalid IP string: %s\n", ip); |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | list_for_each_entry(client, &client_list, list) { |
---|
97 | if (client->addr.sin_addr.s_addr == addr.s_addr && client->agent) { |
---|
98 | return client; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | return NULL; |
---|
103 | } |
---|
104 | |
---|
105 | static const char *og_client_status(const struct og_client *cli) |
---|
106 | { |
---|
107 | switch (cli->last_cmd) { |
---|
108 | case OG_CMD_UNSPEC: |
---|
109 | case OG_CMD_PROBE: |
---|
110 | break; |
---|
111 | default: |
---|
112 | return "BSY"; |
---|
113 | } |
---|
114 | |
---|
115 | switch (cli->status) { |
---|
116 | case OG_CLIENT_STATUS_BUSY: |
---|
117 | return "BSY"; |
---|
118 | case OG_CLIENT_STATUS_OGLIVE: |
---|
119 | return "OPG"; |
---|
120 | case OG_CLIENT_STATUS_VIRTUAL: |
---|
121 | return "VDI"; |
---|
122 | default: |
---|
123 | return "OFF"; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | static bool og_msg_params_validate(const struct og_msg_params *params, |
---|
128 | const uint64_t flags) |
---|
129 | { |
---|
130 | return (params->flags & flags) == flags; |
---|
131 | } |
---|
132 | |
---|
133 | static bool og_flags_validate(const uint64_t flags, |
---|
134 | const uint64_t required_flags) |
---|
135 | { |
---|
136 | return (flags & required_flags) == required_flags; |
---|
137 | } |
---|
138 | |
---|
139 | static int og_json_parse_clients(json_t *element, struct og_msg_params *params) |
---|
140 | { |
---|
141 | unsigned int i; |
---|
142 | json_t *k; |
---|
143 | |
---|
144 | if (json_typeof(element) != JSON_ARRAY) |
---|
145 | return -1; |
---|
146 | |
---|
147 | for (i = 0; i < json_array_size(element); i++) { |
---|
148 | k = json_array_get(element, i); |
---|
149 | if (json_typeof(k) != JSON_STRING) |
---|
150 | return -1; |
---|
151 | |
---|
152 | params->ips_array[params->ips_array_len++] = |
---|
153 | json_string_value(k); |
---|
154 | |
---|
155 | params->flags |= OG_REST_PARAM_ADDR; |
---|
156 | } |
---|
157 | |
---|
158 | return 0; |
---|
159 | } |
---|
160 | |
---|
161 | static int og_json_parse_partition_setup(json_t *element, |
---|
162 | struct og_msg_params *params) |
---|
163 | { |
---|
164 | unsigned int i; |
---|
165 | json_t *k; |
---|
166 | |
---|
167 | if (json_typeof(element) != JSON_ARRAY) |
---|
168 | return -1; |
---|
169 | |
---|
170 | for (i = 0; i < json_array_size(element) && i < OG_PARTITION_MAX; ++i) { |
---|
171 | k = json_array_get(element, i); |
---|
172 | |
---|
173 | if (json_typeof(k) != JSON_OBJECT) |
---|
174 | return -1; |
---|
175 | |
---|
176 | if (og_json_parse_partition(k, ¶ms->partition_setup[i], |
---|
177 | OG_PARAM_PART_NUMBER | |
---|
178 | OG_PARAM_PART_CODE | |
---|
179 | OG_PARAM_PART_FILESYSTEM | |
---|
180 | OG_PARAM_PART_SIZE | |
---|
181 | OG_PARAM_PART_FORMAT) < 0) |
---|
182 | return -1; |
---|
183 | |
---|
184 | params->flags |= (OG_REST_PARAM_PART_0 << i); |
---|
185 | } |
---|
186 | return 0; |
---|
187 | } |
---|
188 | |
---|
189 | static int og_json_parse_time_params(json_t *element, |
---|
190 | struct og_msg_params *params) |
---|
191 | { |
---|
192 | const char *key; |
---|
193 | json_t *value; |
---|
194 | int err = 0; |
---|
195 | |
---|
196 | json_object_foreach(element, key, value) { |
---|
197 | if (!strcmp(key, "years")) { |
---|
198 | err = og_json_parse_uint(value, ¶ms->time.years); |
---|
199 | params->flags |= OG_REST_PARAM_TIME_YEARS; |
---|
200 | } else if (!strcmp(key, "months")) { |
---|
201 | err = og_json_parse_uint(value, ¶ms->time.months); |
---|
202 | params->flags |= OG_REST_PARAM_TIME_MONTHS; |
---|
203 | } else if (!strcmp(key, "weeks")) { |
---|
204 | err = og_json_parse_uint(value, ¶ms->time.weeks); |
---|
205 | params->flags |= OG_REST_PARAM_TIME_WEEKS; |
---|
206 | } else if (!strcmp(key, "week_days")) { |
---|
207 | err = og_json_parse_uint(value, ¶ms->time.week_days); |
---|
208 | params->flags |= OG_REST_PARAM_TIME_WEEK_DAYS; |
---|
209 | } else if (!strcmp(key, "days")) { |
---|
210 | err = og_json_parse_uint(value, ¶ms->time.days); |
---|
211 | params->flags |= OG_REST_PARAM_TIME_DAYS; |
---|
212 | } else if (!strcmp(key, "hours")) { |
---|
213 | err = og_json_parse_uint(value, ¶ms->time.hours); |
---|
214 | params->flags |= OG_REST_PARAM_TIME_HOURS; |
---|
215 | } else if (!strcmp(key, "am_pm")) { |
---|
216 | err = og_json_parse_uint(value, ¶ms->time.am_pm); |
---|
217 | params->flags |= OG_REST_PARAM_TIME_AM_PM; |
---|
218 | } else if (!strcmp(key, "minutes")) { |
---|
219 | err = og_json_parse_uint(value, ¶ms->time.minutes); |
---|
220 | params->flags |= OG_REST_PARAM_TIME_MINUTES; |
---|
221 | } |
---|
222 | if (err != 0) |
---|
223 | return err; |
---|
224 | } |
---|
225 | |
---|
226 | return err; |
---|
227 | } |
---|
228 | |
---|
229 | static const char *og_cmd_to_uri[OG_CMD_MAX] = { |
---|
230 | [OG_CMD_WOL] = "wol", |
---|
231 | [OG_CMD_PROBE] = "probe", |
---|
232 | [OG_CMD_SHELL_RUN] = "shell/run", |
---|
233 | [OG_CMD_SESSION] = "session", |
---|
234 | [OG_CMD_POWEROFF] = "poweroff", |
---|
235 | [OG_CMD_REFRESH] = "refresh", |
---|
236 | [OG_CMD_REBOOT] = "reboot", |
---|
237 | [OG_CMD_STOP] = "stop", |
---|
238 | [OG_CMD_HARDWARE] = "hardware", |
---|
239 | [OG_CMD_SOFTWARE] = "software", |
---|
240 | [OG_CMD_IMAGE_CREATE] = "image/create", |
---|
241 | [OG_CMD_IMAGE_RESTORE] = "image/restore", |
---|
242 | [OG_CMD_SETUP] = "setup", |
---|
243 | [OG_CMD_RUN_SCHEDULE] = "run/schedule", |
---|
244 | [OG_CMD_IMAGES] = "images", |
---|
245 | }; |
---|
246 | |
---|
247 | static bool og_client_is_busy(const struct og_client *cli, |
---|
248 | enum og_cmd_type type) |
---|
249 | { |
---|
250 | switch (type) { |
---|
251 | case OG_CMD_REBOOT: |
---|
252 | case OG_CMD_POWEROFF: |
---|
253 | case OG_CMD_STOP: |
---|
254 | break; |
---|
255 | default: |
---|
256 | if (cli->last_cmd != OG_CMD_UNSPEC) |
---|
257 | return true; |
---|
258 | break; |
---|
259 | } |
---|
260 | |
---|
261 | return false; |
---|
262 | } |
---|
263 | |
---|
264 | int og_send_request(enum og_rest_method method, enum og_cmd_type type, |
---|
265 | const struct og_msg_params *params, |
---|
266 | const json_t *data) |
---|
267 | { |
---|
268 | const char *content_type = "Content-Type: application/json"; |
---|
269 | char content [OG_MSG_REQUEST_MAXLEN - 700] = {}; |
---|
270 | char buf[OG_MSG_REQUEST_MAXLEN] = {}; |
---|
271 | unsigned int content_length; |
---|
272 | char method_str[5] = {}; |
---|
273 | struct og_client *cli; |
---|
274 | const char *uri; |
---|
275 | unsigned int i; |
---|
276 | int client_sd; |
---|
277 | |
---|
278 | if (method == OG_METHOD_GET) |
---|
279 | snprintf(method_str, 5, "GET"); |
---|
280 | else if (method == OG_METHOD_POST) |
---|
281 | snprintf(method_str, 5, "POST"); |
---|
282 | else |
---|
283 | return -1; |
---|
284 | |
---|
285 | if (!data) |
---|
286 | content_length = 0; |
---|
287 | else |
---|
288 | content_length = json_dumpb(data, content, |
---|
289 | OG_MSG_REQUEST_MAXLEN - 700, |
---|
290 | JSON_COMPACT); |
---|
291 | |
---|
292 | uri = og_cmd_to_uri[type]; |
---|
293 | snprintf(buf, OG_MSG_REQUEST_MAXLEN, |
---|
294 | "%s /%s HTTP/1.1\r\nContent-Length: %d\r\n%s\r\n\r\n%s", |
---|
295 | method_str, uri, content_length, content_type, content); |
---|
296 | |
---|
297 | for (i = 0; i < params->ips_array_len; i++) { |
---|
298 | cli = og_client_find(params->ips_array[i]); |
---|
299 | if (!cli) |
---|
300 | continue; |
---|
301 | |
---|
302 | if (og_client_is_busy(cli, type)) |
---|
303 | continue; |
---|
304 | |
---|
305 | client_sd = cli->io.fd; |
---|
306 | if (client_sd < 0) { |
---|
307 | syslog(LOG_INFO, "Client %s not conected\n", |
---|
308 | params->ips_array[i]); |
---|
309 | continue; |
---|
310 | } |
---|
311 | |
---|
312 | if (send(client_sd, buf, strlen(buf), 0) < 0) |
---|
313 | continue; |
---|
314 | |
---|
315 | cli->last_cmd = type; |
---|
316 | } |
---|
317 | |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|
321 | static int og_cmd_post_clients(json_t *element, struct og_msg_params *params) |
---|
322 | { |
---|
323 | const char *key; |
---|
324 | json_t *value; |
---|
325 | int err = 0; |
---|
326 | |
---|
327 | if (json_typeof(element) != JSON_OBJECT) |
---|
328 | return -1; |
---|
329 | |
---|
330 | json_object_foreach(element, key, value) { |
---|
331 | if (!strcmp(key, "clients")) |
---|
332 | err = og_json_parse_clients(value, params); |
---|
333 | |
---|
334 | if (err < 0) |
---|
335 | break; |
---|
336 | } |
---|
337 | |
---|
338 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
339 | return -1; |
---|
340 | |
---|
341 | return og_send_request(OG_METHOD_POST, OG_CMD_PROBE, params, NULL); |
---|
342 | } |
---|
343 | |
---|
344 | struct og_buffer { |
---|
345 | char *data; |
---|
346 | int len; |
---|
347 | }; |
---|
348 | |
---|
349 | static int og_json_dump_clients(const char *buffer, size_t size, void *data) |
---|
350 | { |
---|
351 | struct og_buffer *og_buffer = (struct og_buffer *)data; |
---|
352 | |
---|
353 | memcpy(og_buffer->data + og_buffer->len, buffer, size); |
---|
354 | og_buffer->len += size; |
---|
355 | |
---|
356 | return 0; |
---|
357 | } |
---|
358 | |
---|
359 | static int og_cmd_get_clients(json_t *element, struct og_msg_params *params, |
---|
360 | char *buffer_reply) |
---|
361 | { |
---|
362 | json_t *root, *array, *addr, *state, *object; |
---|
363 | struct og_client *client; |
---|
364 | struct og_buffer og_buffer = { |
---|
365 | .data = buffer_reply, |
---|
366 | }; |
---|
367 | |
---|
368 | array = json_array(); |
---|
369 | if (!array) |
---|
370 | return -1; |
---|
371 | |
---|
372 | list_for_each_entry(client, &client_list, list) { |
---|
373 | if (!client->agent) |
---|
374 | continue; |
---|
375 | |
---|
376 | object = json_object(); |
---|
377 | if (!object) { |
---|
378 | json_decref(array); |
---|
379 | return -1; |
---|
380 | } |
---|
381 | addr = json_string(inet_ntoa(client->addr.sin_addr)); |
---|
382 | if (!addr) { |
---|
383 | json_decref(object); |
---|
384 | json_decref(array); |
---|
385 | return -1; |
---|
386 | } |
---|
387 | json_object_set_new(object, "addr", addr); |
---|
388 | state = json_string(og_client_status(client)); |
---|
389 | if (!state) { |
---|
390 | json_decref(object); |
---|
391 | json_decref(array); |
---|
392 | return -1; |
---|
393 | } |
---|
394 | json_object_set_new(object, "state", state); |
---|
395 | json_array_append_new(array, object); |
---|
396 | } |
---|
397 | root = json_pack("{s:o}", "clients", array); |
---|
398 | if (!root) { |
---|
399 | json_decref(array); |
---|
400 | return -1; |
---|
401 | } |
---|
402 | |
---|
403 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
404 | json_decref(root); |
---|
405 | |
---|
406 | return 0; |
---|
407 | } |
---|
408 | |
---|
409 | static int og_json_parse_target(json_t *element, struct og_msg_params *params) |
---|
410 | { |
---|
411 | const char *key; |
---|
412 | json_t *value; |
---|
413 | |
---|
414 | if (json_typeof(element) != JSON_OBJECT) { |
---|
415 | return -1; |
---|
416 | } |
---|
417 | |
---|
418 | json_object_foreach(element, key, value) { |
---|
419 | if (!strcmp(key, "addr")) { |
---|
420 | if (json_typeof(value) != JSON_STRING) |
---|
421 | return -1; |
---|
422 | |
---|
423 | params->ips_array[params->ips_array_len] = |
---|
424 | json_string_value(value); |
---|
425 | |
---|
426 | params->flags |= OG_REST_PARAM_ADDR; |
---|
427 | } else if (!strcmp(key, "mac")) { |
---|
428 | if (json_typeof(value) != JSON_STRING) |
---|
429 | return -1; |
---|
430 | |
---|
431 | params->mac_array[params->ips_array_len] = |
---|
432 | json_string_value(value); |
---|
433 | |
---|
434 | params->flags |= OG_REST_PARAM_MAC; |
---|
435 | } else if (!strcmp(key, "netmask")) { |
---|
436 | if (json_typeof(value) != JSON_STRING) |
---|
437 | return -1; |
---|
438 | |
---|
439 | params->netmask_array[params->ips_array_len] = |
---|
440 | json_string_value(value); |
---|
441 | |
---|
442 | params->flags |= OG_REST_PARAM_NETMASK; |
---|
443 | } |
---|
444 | } |
---|
445 | |
---|
446 | return 0; |
---|
447 | } |
---|
448 | |
---|
449 | static int og_json_parse_targets(json_t *element, struct og_msg_params *params) |
---|
450 | { |
---|
451 | unsigned int i; |
---|
452 | json_t *k; |
---|
453 | int err; |
---|
454 | |
---|
455 | if (json_typeof(element) != JSON_ARRAY) |
---|
456 | return -1; |
---|
457 | |
---|
458 | for (i = 0; i < json_array_size(element); i++) { |
---|
459 | k = json_array_get(element, i); |
---|
460 | |
---|
461 | if (json_typeof(k) != JSON_OBJECT) |
---|
462 | return -1; |
---|
463 | |
---|
464 | err = og_json_parse_target(k, params); |
---|
465 | if (err < 0) |
---|
466 | return err; |
---|
467 | |
---|
468 | params->ips_array_len++; |
---|
469 | } |
---|
470 | return 0; |
---|
471 | } |
---|
472 | |
---|
473 | static int og_json_parse_type(json_t *element, struct og_msg_params *params) |
---|
474 | { |
---|
475 | const char *type; |
---|
476 | |
---|
477 | if (json_typeof(element) != JSON_STRING) |
---|
478 | return -1; |
---|
479 | |
---|
480 | params->wol_type = json_string_value(element); |
---|
481 | |
---|
482 | type = json_string_value(element); |
---|
483 | if (!strcmp(type, "unicast")) |
---|
484 | params->wol_type = "2"; |
---|
485 | else if (!strcmp(type, "broadcast")) |
---|
486 | params->wol_type = "1"; |
---|
487 | |
---|
488 | params->flags |= OG_REST_PARAM_WOL_TYPE; |
---|
489 | |
---|
490 | return 0; |
---|
491 | } |
---|
492 | |
---|
493 | static int og_cmd_wol(json_t *element, struct og_msg_params *params) |
---|
494 | { |
---|
495 | const char *key; |
---|
496 | json_t *value; |
---|
497 | int err = 0; |
---|
498 | |
---|
499 | if (json_typeof(element) != JSON_OBJECT) |
---|
500 | return -1; |
---|
501 | |
---|
502 | json_object_foreach(element, key, value) { |
---|
503 | if (!strcmp(key, "clients")) { |
---|
504 | err = og_json_parse_targets(value, params); |
---|
505 | } else if (!strcmp(key, "type")) { |
---|
506 | err = og_json_parse_type(value, params); |
---|
507 | } |
---|
508 | |
---|
509 | if (err < 0) |
---|
510 | break; |
---|
511 | } |
---|
512 | |
---|
513 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
514 | OG_REST_PARAM_MAC | |
---|
515 | OG_REST_PARAM_NETMASK | |
---|
516 | OG_REST_PARAM_WOL_TYPE)) |
---|
517 | return -1; |
---|
518 | |
---|
519 | if (!Levanta((char **)params->ips_array, (char **)params->mac_array, |
---|
520 | (char **)params->netmask_array, params->ips_array_len, |
---|
521 | (char *)params->wol_type)) |
---|
522 | return -1; |
---|
523 | |
---|
524 | return 0; |
---|
525 | } |
---|
526 | |
---|
527 | static int og_json_parse_run(json_t *element, struct og_msg_params *params) |
---|
528 | { |
---|
529 | if (json_typeof(element) != JSON_STRING) |
---|
530 | return -1; |
---|
531 | |
---|
532 | snprintf(params->run_cmd, sizeof(params->run_cmd), "%s", |
---|
533 | json_string_value(element)); |
---|
534 | |
---|
535 | params->flags |= OG_REST_PARAM_RUN_CMD; |
---|
536 | |
---|
537 | return 0; |
---|
538 | } |
---|
539 | |
---|
540 | static int og_cmd_run_post(json_t *element, struct og_msg_params *params) |
---|
541 | { |
---|
542 | json_t *value, *clients; |
---|
543 | const char *key; |
---|
544 | unsigned int i; |
---|
545 | int err = 0; |
---|
546 | |
---|
547 | if (json_typeof(element) != JSON_OBJECT) |
---|
548 | return -1; |
---|
549 | |
---|
550 | json_object_foreach(element, key, value) { |
---|
551 | if (!strcmp(key, "clients")) |
---|
552 | err = og_json_parse_clients(value, params); |
---|
553 | else if (!strcmp(key, "run")) |
---|
554 | err = og_json_parse_run(value, params); |
---|
555 | else if (!strcmp(key, "echo")) { |
---|
556 | err = og_json_parse_bool(value, ¶ms->echo); |
---|
557 | params->flags |= OG_REST_PARAM_ECHO; |
---|
558 | } |
---|
559 | |
---|
560 | if (err < 0) |
---|
561 | break; |
---|
562 | } |
---|
563 | |
---|
564 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
565 | OG_REST_PARAM_RUN_CMD | |
---|
566 | OG_REST_PARAM_ECHO)) |
---|
567 | return -1; |
---|
568 | |
---|
569 | clients = json_copy(element); |
---|
570 | json_object_del(clients, "clients"); |
---|
571 | |
---|
572 | err = og_send_request(OG_METHOD_POST, OG_CMD_SHELL_RUN, params, clients); |
---|
573 | if (err < 0) |
---|
574 | return err; |
---|
575 | |
---|
576 | for (i = 0; i < params->ips_array_len; i++) { |
---|
577 | char filename[4096]; |
---|
578 | FILE *f; |
---|
579 | |
---|
580 | sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); |
---|
581 | f = fopen(filename, "wt"); |
---|
582 | fclose(f); |
---|
583 | } |
---|
584 | |
---|
585 | return 0; |
---|
586 | } |
---|
587 | |
---|
588 | static int og_cmd_run_get(json_t *element, struct og_msg_params *params, |
---|
589 | char *buffer_reply) |
---|
590 | { |
---|
591 | struct og_buffer og_buffer = { |
---|
592 | .data = buffer_reply, |
---|
593 | }; |
---|
594 | json_t *root, *value, *array; |
---|
595 | const char *key; |
---|
596 | unsigned int i; |
---|
597 | int err = 0; |
---|
598 | |
---|
599 | if (json_typeof(element) != JSON_OBJECT) |
---|
600 | return -1; |
---|
601 | |
---|
602 | json_object_foreach(element, key, value) { |
---|
603 | if (!strcmp(key, "clients")) |
---|
604 | err = og_json_parse_clients(value, params); |
---|
605 | |
---|
606 | if (err < 0) |
---|
607 | return err; |
---|
608 | } |
---|
609 | |
---|
610 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
611 | return -1; |
---|
612 | |
---|
613 | array = json_array(); |
---|
614 | if (!array) |
---|
615 | return -1; |
---|
616 | |
---|
617 | for (i = 0; i < params->ips_array_len; i++) { |
---|
618 | json_t *object, *output, *addr; |
---|
619 | char data[4096] = {}; |
---|
620 | char filename[4096]; |
---|
621 | int fd, numbytes; |
---|
622 | |
---|
623 | sprintf(filename, "/tmp/_Seconsola_%s", params->ips_array[i]); |
---|
624 | |
---|
625 | fd = open(filename, O_RDONLY); |
---|
626 | if (!fd) |
---|
627 | return -1; |
---|
628 | |
---|
629 | numbytes = read(fd, data, sizeof(data)); |
---|
630 | if (numbytes < 0) { |
---|
631 | close(fd); |
---|
632 | return -1; |
---|
633 | } |
---|
634 | data[sizeof(data) - 1] = '\0'; |
---|
635 | close(fd); |
---|
636 | |
---|
637 | object = json_object(); |
---|
638 | if (!object) { |
---|
639 | json_decref(array); |
---|
640 | return -1; |
---|
641 | } |
---|
642 | addr = json_string(params->ips_array[i]); |
---|
643 | if (!addr) { |
---|
644 | json_decref(object); |
---|
645 | json_decref(array); |
---|
646 | return -1; |
---|
647 | } |
---|
648 | json_object_set_new(object, "addr", addr); |
---|
649 | |
---|
650 | output = json_string(data); |
---|
651 | if (!output) { |
---|
652 | json_decref(object); |
---|
653 | json_decref(array); |
---|
654 | return -1; |
---|
655 | } |
---|
656 | json_object_set_new(object, "output", output); |
---|
657 | |
---|
658 | json_array_append_new(array, object); |
---|
659 | } |
---|
660 | |
---|
661 | root = json_pack("{s:o}", "clients", array); |
---|
662 | if (!root) |
---|
663 | return -1; |
---|
664 | |
---|
665 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
666 | json_decref(root); |
---|
667 | |
---|
668 | return 0; |
---|
669 | } |
---|
670 | |
---|
671 | static int og_cmd_session(json_t *element, struct og_msg_params *params) |
---|
672 | { |
---|
673 | json_t *clients, *value; |
---|
674 | const char *key; |
---|
675 | int err = 0; |
---|
676 | |
---|
677 | if (json_typeof(element) != JSON_OBJECT) |
---|
678 | return -1; |
---|
679 | |
---|
680 | json_object_foreach(element, key, value) { |
---|
681 | if (!strcmp(key, "clients")) { |
---|
682 | err = og_json_parse_clients(value, params); |
---|
683 | } else if (!strcmp(key, "disk")) { |
---|
684 | err = og_json_parse_string(value, ¶ms->disk); |
---|
685 | params->flags |= OG_REST_PARAM_DISK; |
---|
686 | } else if (!strcmp(key, "partition")) { |
---|
687 | err = og_json_parse_string(value, ¶ms->partition); |
---|
688 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
689 | } |
---|
690 | |
---|
691 | if (err < 0) |
---|
692 | return err; |
---|
693 | } |
---|
694 | |
---|
695 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
696 | OG_REST_PARAM_DISK | |
---|
697 | OG_REST_PARAM_PARTITION)) |
---|
698 | return -1; |
---|
699 | |
---|
700 | clients = json_copy(element); |
---|
701 | json_object_del(clients, "clients"); |
---|
702 | |
---|
703 | return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients); |
---|
704 | } |
---|
705 | |
---|
706 | static int og_cmd_get_session(json_t *element, struct og_msg_params *params, |
---|
707 | char *buffer_reply) |
---|
708 | { |
---|
709 | json_t *value, *root, *array, *item; |
---|
710 | const char *key, *msglog, *os_name; |
---|
711 | unsigned int disk, partition; |
---|
712 | struct og_dbi *dbi; |
---|
713 | dbi_result result; |
---|
714 | int err = 0; |
---|
715 | |
---|
716 | struct og_buffer og_buffer = { |
---|
717 | .data = buffer_reply |
---|
718 | }; |
---|
719 | |
---|
720 | json_object_foreach(element, key, value) { |
---|
721 | if (!strcmp(key, "client")) |
---|
722 | err = og_json_parse_clients(value, params); |
---|
723 | else |
---|
724 | err = -1; |
---|
725 | |
---|
726 | if (err < 0) |
---|
727 | return err; |
---|
728 | } |
---|
729 | |
---|
730 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
731 | return -1; |
---|
732 | |
---|
733 | dbi = og_dbi_open(&ogconfig.db); |
---|
734 | if (!dbi) { |
---|
735 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
736 | __func__, __LINE__); |
---|
737 | return -1; |
---|
738 | } |
---|
739 | |
---|
740 | result = dbi_conn_queryf(dbi->conn, |
---|
741 | "SELECT op.numdisk, op.numpar, nom.nombreso " |
---|
742 | "FROM ordenadores o " |
---|
743 | "INNER JOIN ordenadores_particiones op " |
---|
744 | " ON o.idordenador = op.idordenador " |
---|
745 | "INNER JOIN nombresos nom " |
---|
746 | " ON op.idnombreso = nom.idnombreso " |
---|
747 | "WHERE o.ip = '%s'", |
---|
748 | params->ips_array[0]); |
---|
749 | if (!result) { |
---|
750 | dbi_conn_error(dbi->conn, &msglog); |
---|
751 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
752 | __func__, __LINE__, msglog); |
---|
753 | og_dbi_close(dbi); |
---|
754 | return -1; |
---|
755 | } |
---|
756 | |
---|
757 | array = json_array(); |
---|
758 | if (!array) { |
---|
759 | dbi_result_free(result); |
---|
760 | og_dbi_close(dbi); |
---|
761 | return -1; |
---|
762 | } |
---|
763 | |
---|
764 | while (dbi_result_next_row(result)) { |
---|
765 | item = json_object(); |
---|
766 | if (!item) { |
---|
767 | dbi_result_free(result); |
---|
768 | og_dbi_close(dbi); |
---|
769 | json_decref(array); |
---|
770 | return -1; |
---|
771 | } |
---|
772 | |
---|
773 | disk = dbi_result_get_uint(result, "numdisk"); |
---|
774 | partition = dbi_result_get_uint(result, "numpar"); |
---|
775 | os_name = dbi_result_get_string(result, "nombreso"); |
---|
776 | |
---|
777 | json_object_set_new(item, "disk", json_integer(disk)); |
---|
778 | json_object_set_new(item, "partition", json_integer(partition)); |
---|
779 | json_object_set_new(item, "name", json_string(os_name)); |
---|
780 | json_array_append_new(array, item); |
---|
781 | } |
---|
782 | |
---|
783 | dbi_result_free(result); |
---|
784 | og_dbi_close(dbi); |
---|
785 | |
---|
786 | root = json_object(); |
---|
787 | if (!root){ |
---|
788 | json_decref(array); |
---|
789 | return -1; |
---|
790 | } |
---|
791 | |
---|
792 | json_object_set_new(root, "sessions", array); |
---|
793 | |
---|
794 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
795 | json_decref(root); |
---|
796 | return 0; |
---|
797 | } |
---|
798 | |
---|
799 | static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) |
---|
800 | { |
---|
801 | const char *key; |
---|
802 | json_t *value; |
---|
803 | int err = 0; |
---|
804 | |
---|
805 | if (json_typeof(element) != JSON_OBJECT) |
---|
806 | return -1; |
---|
807 | |
---|
808 | json_object_foreach(element, key, value) { |
---|
809 | if (!strcmp(key, "clients")) |
---|
810 | err = og_json_parse_clients(value, params); |
---|
811 | |
---|
812 | if (err < 0) |
---|
813 | break; |
---|
814 | } |
---|
815 | |
---|
816 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
817 | return -1; |
---|
818 | |
---|
819 | return og_send_request(OG_METHOD_POST, OG_CMD_POWEROFF, params, NULL); |
---|
820 | } |
---|
821 | |
---|
822 | static int og_cmd_refresh(json_t *element, struct og_msg_params *params) |
---|
823 | { |
---|
824 | const char *key; |
---|
825 | json_t *value; |
---|
826 | int err = 0; |
---|
827 | |
---|
828 | if (json_typeof(element) != JSON_OBJECT) |
---|
829 | return -1; |
---|
830 | |
---|
831 | json_object_foreach(element, key, value) { |
---|
832 | if (!strcmp(key, "clients")) |
---|
833 | err = og_json_parse_clients(value, params); |
---|
834 | |
---|
835 | if (err < 0) |
---|
836 | break; |
---|
837 | } |
---|
838 | |
---|
839 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
840 | return -1; |
---|
841 | |
---|
842 | return og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, params, NULL); |
---|
843 | } |
---|
844 | |
---|
845 | static int og_cmd_reboot(json_t *element, struct og_msg_params *params) |
---|
846 | { |
---|
847 | const char *key; |
---|
848 | json_t *value; |
---|
849 | int err = 0; |
---|
850 | |
---|
851 | if (json_typeof(element) != JSON_OBJECT) |
---|
852 | return -1; |
---|
853 | |
---|
854 | json_object_foreach(element, key, value) { |
---|
855 | if (!strcmp(key, "clients")) |
---|
856 | err = og_json_parse_clients(value, params); |
---|
857 | |
---|
858 | if (err < 0) |
---|
859 | break; |
---|
860 | } |
---|
861 | |
---|
862 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
863 | return -1; |
---|
864 | |
---|
865 | return og_send_request(OG_METHOD_POST, OG_CMD_REBOOT, params, NULL); |
---|
866 | } |
---|
867 | |
---|
868 | #define OG_TFTP_TMPL_PATH "/opt/opengnsys/tftpboot/menu.lst/templates" |
---|
869 | |
---|
870 | static int og_cmd_get_modes(json_t *element, struct og_msg_params *params, |
---|
871 | char *buffer_reply) |
---|
872 | { |
---|
873 | struct og_buffer og_buffer = { |
---|
874 | .data = buffer_reply |
---|
875 | }; |
---|
876 | json_t *root, *modes; |
---|
877 | struct dirent *dent; |
---|
878 | DIR *d = NULL; |
---|
879 | |
---|
880 | root = json_object(); |
---|
881 | if (!root) |
---|
882 | return -1; |
---|
883 | |
---|
884 | modes = json_array(); |
---|
885 | if (!modes) { |
---|
886 | json_decref(root); |
---|
887 | return -1; |
---|
888 | } |
---|
889 | |
---|
890 | d = opendir(OG_TFTP_TMPL_PATH); |
---|
891 | if (!d) { |
---|
892 | json_decref(modes); |
---|
893 | json_decref(root); |
---|
894 | syslog(LOG_ERR, "Cannot open directory %s\n", |
---|
895 | OG_TFTP_TMPL_PATH); |
---|
896 | return -1; |
---|
897 | } |
---|
898 | |
---|
899 | dent = readdir(d); |
---|
900 | while (dent) { |
---|
901 | if (dent->d_type != DT_REG) { |
---|
902 | dent = readdir(d); |
---|
903 | continue; |
---|
904 | } |
---|
905 | json_array_append_new(modes, json_string(dent->d_name)); |
---|
906 | dent = readdir(d); |
---|
907 | } |
---|
908 | |
---|
909 | json_object_set_new(root, "modes", modes); |
---|
910 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
911 | json_decref(root); |
---|
912 | closedir(d); |
---|
913 | |
---|
914 | return 0; |
---|
915 | } |
---|
916 | |
---|
917 | static int og_change_db_mode(struct og_dbi *dbi, const char *mac, |
---|
918 | const char * mode) |
---|
919 | { |
---|
920 | const char *msglog; |
---|
921 | dbi_result result; |
---|
922 | |
---|
923 | result = dbi_conn_queryf(dbi->conn, |
---|
924 | "UPDATE ordenadores SET arranque='%s' " |
---|
925 | "WHERE mac='%s'", |
---|
926 | mode, mac); |
---|
927 | |
---|
928 | if (!result) { |
---|
929 | dbi_conn_error(dbi->conn, &msglog); |
---|
930 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
931 | __func__, __LINE__, msglog); |
---|
932 | return -1; |
---|
933 | } |
---|
934 | |
---|
935 | dbi_result_free(result); |
---|
936 | return 0; |
---|
937 | } |
---|
938 | |
---|
939 | static int og_set_client_mode(struct og_dbi *dbi, const char *mac, |
---|
940 | const char *mode, const char *template_name) |
---|
941 | { |
---|
942 | char filename[PATH_MAX + 1] = "/tmp/mode_params_XXXXXX"; |
---|
943 | char cmd_params[16384] = {}; |
---|
944 | char params[4096] = "\0"; |
---|
945 | const char *msglog; |
---|
946 | dbi_result result; |
---|
947 | unsigned int i; |
---|
948 | int numbytes; |
---|
949 | int status; |
---|
950 | int fd; |
---|
951 | |
---|
952 | result = dbi_conn_queryf(dbi->conn, |
---|
953 | "SELECT ' LANG=%s', ' ip=', CONCAT_WS(':', ordenadores.ip, (SELECT (@serverip:=ipserveradm) FROM entornos LIMIT 1), aulas.router, aulas.netmask, ordenadores.nombreordenador, ordenadores.netiface, 'none'), ' group=', REPLACE(TRIM(aulas.nombreaula), ' ', '_'), ' ogrepo=', (@repoip:=IFNULL(repositorios.ip, '')), ' oglive=', @serverip, ' oglog=', @serverip, ' ogshare=', @serverip, ' oglivedir=', ordenadores.oglivedir, ' ogprof=', IF(ordenadores.idordenador=aulas.idordprofesor, 'true', 'false'), IF(perfileshard.descripcion<>'', CONCAT(' hardprofile=', REPLACE(TRIM(perfileshard.descripcion), ' ', '_')), ''), IF(aulas.ntp<>'', CONCAT(' ogntp=', aulas.ntp), ''), IF(aulas.dns<>'', CONCAT(' ogdns=', aulas.dns), ''), IF(aulas.proxy<>'', CONCAT(' ogproxy=', aulas.proxy), ''), IF(entidades.ogunit=1 AND NOT centros.directorio='', CONCAT(' ogunit=', centros.directorio), ''), CASE WHEN menus.resolucion IS NULL THEN '' WHEN menus.resolucion <= '999' THEN CONCAT(' vga=', menus.resolucion) WHEN menus.resolucion LIKE '%:%' THEN CONCAT(' video=', menus.resolucion) ELSE menus.resolucion END FROM ordenadores JOIN aulas USING(idaula) JOIN centros USING(idcentro) JOIN entidades USING(identidad) LEFT JOIN repositorios USING(idrepositorio) LEFT JOIN perfileshard USING(idperfilhard) LEFT JOIN menus USING(idmenu) WHERE ordenadores.mac='%s'", getenv("LANG"), mac); |
---|
954 | |
---|
955 | if (dbi_result_get_numrows(result) != 1) { |
---|
956 | dbi_conn_error(dbi->conn, &msglog); |
---|
957 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
958 | __FILE__, __LINE__, msglog); |
---|
959 | dbi_result_free(result); |
---|
960 | return -1; |
---|
961 | } |
---|
962 | dbi_result_next_row(result); |
---|
963 | |
---|
964 | for (i = 1; i <= dbi_result_get_numfields(result); ++i) |
---|
965 | strcat(params, dbi_result_get_string_idx(result, i)); |
---|
966 | |
---|
967 | dbi_result_free(result); |
---|
968 | |
---|
969 | snprintf(cmd_params, sizeof(cmd_params), |
---|
970 | "MODE_FILE='%s'\nMAC='%s'\nDATA='%s'\n" |
---|
971 | "MODE='PERM'\nTEMPLATE_NAME='%s'", |
---|
972 | mode, mac, params, template_name); |
---|
973 | |
---|
974 | fd = mkstemp(filename); |
---|
975 | if (fd < 0) { |
---|
976 | syslog(LOG_ERR, "cannot generate temp file (%s:%d)\n", |
---|
977 | __func__, __LINE__); |
---|
978 | return -1; |
---|
979 | } |
---|
980 | |
---|
981 | numbytes = write(fd, cmd_params, strlen(cmd_params) + 1); |
---|
982 | close(fd); |
---|
983 | |
---|
984 | if (numbytes < 0) { |
---|
985 | syslog(LOG_ERR, "cannot write file\n"); |
---|
986 | unlink(filename); |
---|
987 | return -1; |
---|
988 | } |
---|
989 | |
---|
990 | if (fork() == 0) { |
---|
991 | execlp("/bin/bash", "/bin/bash", |
---|
992 | "/opt/opengnsys/bin/setclientmode", filename, NULL); |
---|
993 | syslog(LOG_ERR, "failed script execution (%s:%d)\n", |
---|
994 | __func__, __LINE__); |
---|
995 | exit(EXIT_FAILURE); |
---|
996 | } else { |
---|
997 | wait(&status); |
---|
998 | } |
---|
999 | unlink(filename); |
---|
1000 | |
---|
1001 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
---|
1002 | syslog(LOG_ERR, "failed script execution (%s:%d)\n", |
---|
1003 | __func__, __LINE__); |
---|
1004 | return -1; |
---|
1005 | } |
---|
1006 | |
---|
1007 | if (og_change_db_mode(dbi, mac, mode) < 0) { |
---|
1008 | syslog(LOG_ERR, "failed to change db mode (%s:%d)\n", |
---|
1009 | __func__, __LINE__); |
---|
1010 | return -1; |
---|
1011 | } |
---|
1012 | |
---|
1013 | return 0; |
---|
1014 | } |
---|
1015 | |
---|
1016 | static int og_cmd_post_modes(json_t *element, struct og_msg_params *params) |
---|
1017 | { |
---|
1018 | char ips_str[(OG_DB_IP_MAXLEN + 1) * OG_CLIENTS_MAX + 1] = {}; |
---|
1019 | char template_file[PATH_MAX + 1] = {}; |
---|
1020 | char template_name[PATH_MAX + 1] = {}; |
---|
1021 | char first_line[PATH_MAX + 1] = {}; |
---|
1022 | const char *mode_str, *mac; |
---|
1023 | int ips_str_len = 0; |
---|
1024 | struct og_dbi *dbi; |
---|
1025 | uint64_t flags = 0; |
---|
1026 | dbi_result result; |
---|
1027 | const char *key; |
---|
1028 | json_t *value; |
---|
1029 | int err = 0; |
---|
1030 | FILE *f; |
---|
1031 | int i; |
---|
1032 | |
---|
1033 | json_object_foreach(element, key, value) { |
---|
1034 | if (!strcmp(key, "clients")) { |
---|
1035 | err = og_json_parse_clients(value, params); |
---|
1036 | } else if (!strcmp(key, "mode")) { |
---|
1037 | err = og_json_parse_string(value, &mode_str); |
---|
1038 | flags |= OG_REST_PARAM_MODE; |
---|
1039 | } else { |
---|
1040 | err = -1; |
---|
1041 | } |
---|
1042 | |
---|
1043 | if (err < 0) |
---|
1044 | break; |
---|
1045 | } |
---|
1046 | |
---|
1047 | if (!og_flags_validate(flags, OG_REST_PARAM_MODE) || |
---|
1048 | !og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1049 | return -1; |
---|
1050 | |
---|
1051 | snprintf(template_file, sizeof(template_file), "%s/%s", |
---|
1052 | OG_TFTP_TMPL_PATH, mode_str); |
---|
1053 | f = fopen(template_file, "r"); |
---|
1054 | if (!f) { |
---|
1055 | syslog(LOG_ERR, "cannot open file (%s:%d)\n", |
---|
1056 | __func__, __LINE__); |
---|
1057 | return -1; |
---|
1058 | } |
---|
1059 | |
---|
1060 | if (!fgets(first_line, sizeof(first_line), f)) { |
---|
1061 | fclose(f); |
---|
1062 | syslog(LOG_ERR, "cannot read file (%s:%d)\n", |
---|
1063 | __func__, __LINE__); |
---|
1064 | return -1; |
---|
1065 | } |
---|
1066 | |
---|
1067 | fclose(f); |
---|
1068 | |
---|
1069 | if (sscanf(first_line, "##NO-TOCAR-ESTA-LINEA %s", template_name) != 1) { |
---|
1070 | syslog(LOG_ERR, "malformed template: %s", first_line); |
---|
1071 | return -1; |
---|
1072 | } |
---|
1073 | |
---|
1074 | for (i = 0; i < params->ips_array_len; ++i) { |
---|
1075 | ips_str_len += snprintf(ips_str + ips_str_len, |
---|
1076 | sizeof(ips_str) - ips_str_len, |
---|
1077 | "'%s',", params->ips_array[i]); |
---|
1078 | } |
---|
1079 | ips_str[ips_str_len - 1] = '\0'; |
---|
1080 | |
---|
1081 | dbi = og_dbi_open(&ogconfig.db); |
---|
1082 | if (!dbi) { |
---|
1083 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
1084 | __func__, __LINE__); |
---|
1085 | return -1; |
---|
1086 | } |
---|
1087 | |
---|
1088 | result = dbi_conn_queryf(dbi->conn, |
---|
1089 | "SELECT mac FROM ordenadores " |
---|
1090 | "WHERE ip IN (%s)", ips_str); |
---|
1091 | |
---|
1092 | while (dbi_result_next_row(result)) { |
---|
1093 | mac = dbi_result_get_string(result, "mac"); |
---|
1094 | err = og_set_client_mode(dbi, mac, mode_str, template_name); |
---|
1095 | if (err != 0) { |
---|
1096 | dbi_result_free(result); |
---|
1097 | og_dbi_close(dbi); |
---|
1098 | return -1; |
---|
1099 | } |
---|
1100 | } |
---|
1101 | |
---|
1102 | dbi_result_free(result); |
---|
1103 | og_dbi_close(dbi); |
---|
1104 | |
---|
1105 | return 0; |
---|
1106 | } |
---|
1107 | |
---|
1108 | static int og_cmd_get_client_setup(json_t *element, |
---|
1109 | struct og_msg_params *params, |
---|
1110 | char *buffer_reply) |
---|
1111 | { |
---|
1112 | json_t *value, *root, *partitions_array, *partition_json; |
---|
1113 | const char *key, *msglog; |
---|
1114 | unsigned int len_part; |
---|
1115 | struct og_dbi *dbi; |
---|
1116 | dbi_result result; |
---|
1117 | int err = 0; |
---|
1118 | |
---|
1119 | struct og_buffer og_buffer = { |
---|
1120 | .data = buffer_reply |
---|
1121 | }; |
---|
1122 | |
---|
1123 | struct { |
---|
1124 | int disk; |
---|
1125 | int number; |
---|
1126 | int code; |
---|
1127 | int size; |
---|
1128 | int filesystem; |
---|
1129 | int format; |
---|
1130 | int os; |
---|
1131 | int used_size; |
---|
1132 | int image; |
---|
1133 | int software; |
---|
1134 | } partition; |
---|
1135 | |
---|
1136 | json_object_foreach(element, key, value) { |
---|
1137 | if (!strcmp(key, "client")) { |
---|
1138 | err = og_json_parse_clients(value, params); |
---|
1139 | } |
---|
1140 | |
---|
1141 | if (err < 0) |
---|
1142 | break; |
---|
1143 | } |
---|
1144 | |
---|
1145 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1146 | return -1; |
---|
1147 | |
---|
1148 | if (params->ips_array_len != 1) |
---|
1149 | return -1; |
---|
1150 | |
---|
1151 | root = json_object(); |
---|
1152 | if (!root) |
---|
1153 | return -1; |
---|
1154 | |
---|
1155 | partitions_array = json_array(); |
---|
1156 | if (!partitions_array) { |
---|
1157 | json_decref(root); |
---|
1158 | return -1; |
---|
1159 | } |
---|
1160 | json_object_set_new(root, "partitions", partitions_array); |
---|
1161 | |
---|
1162 | dbi = og_dbi_open(&ogconfig.db); |
---|
1163 | if (!dbi) { |
---|
1164 | json_decref(root); |
---|
1165 | syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", |
---|
1166 | __func__, __LINE__); |
---|
1167 | return -1; |
---|
1168 | } |
---|
1169 | |
---|
1170 | result = dbi_conn_queryf(dbi->conn, |
---|
1171 | "SELECT numdisk, numpar, codpar, tamano, " |
---|
1172 | " uso, idsistemafichero, idnombreso, " |
---|
1173 | " idimagen, idperfilsoft " |
---|
1174 | "FROM ordenadores_particiones " |
---|
1175 | "INNER JOIN ordenadores " |
---|
1176 | "ON ordenadores.idordenador = ordenadores_particiones.idordenador " |
---|
1177 | "WHERE ordenadores.ip='%s'", |
---|
1178 | params->ips_array[0]); |
---|
1179 | if (!result) { |
---|
1180 | json_decref(root); |
---|
1181 | dbi_conn_error(dbi->conn, &msglog); |
---|
1182 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1183 | __func__, __LINE__, msglog); |
---|
1184 | og_dbi_close(dbi); |
---|
1185 | return -1; |
---|
1186 | } |
---|
1187 | |
---|
1188 | len_part = 0; |
---|
1189 | /* partition 0 represents the full disk, hence OG_PARTITION_MAX + 1. */ |
---|
1190 | while (dbi_result_next_row(result) && len_part < OG_PARTITION_MAX + 1) { |
---|
1191 | partition.disk = dbi_result_get_int(result, "numdisk"); |
---|
1192 | partition.number = dbi_result_get_int(result, "numpar"); |
---|
1193 | partition.code = dbi_result_get_int(result, "codpar"); |
---|
1194 | partition.size = dbi_result_get_int(result, "tamano"); |
---|
1195 | partition.used_size = dbi_result_get_int(result, "uso"); |
---|
1196 | partition.filesystem = dbi_result_get_int(result, "idsistemafichero"); |
---|
1197 | partition.os = dbi_result_get_int(result, "idnombreso"); |
---|
1198 | partition.image = dbi_result_get_int(result, "idimagen"); |
---|
1199 | partition.software = dbi_result_get_int(result, "idperfilsoft"); |
---|
1200 | |
---|
1201 | partition_json = json_object(); |
---|
1202 | if (!partition_json) { |
---|
1203 | json_decref(root); |
---|
1204 | dbi_result_free(result); |
---|
1205 | og_dbi_close(dbi); |
---|
1206 | return -1; |
---|
1207 | } |
---|
1208 | |
---|
1209 | json_object_set_new(partition_json, "disk", |
---|
1210 | json_integer(partition.disk)); |
---|
1211 | json_object_set_new(partition_json, "partition", |
---|
1212 | json_integer(partition.number)); |
---|
1213 | json_object_set_new(partition_json, "code", |
---|
1214 | json_integer(partition.code)); |
---|
1215 | json_object_set_new(partition_json, "size", |
---|
1216 | json_integer(partition.size)); |
---|
1217 | json_object_set_new(partition_json, "used_size", |
---|
1218 | json_integer(partition.used_size)); |
---|
1219 | json_object_set_new(partition_json, "filesystem", |
---|
1220 | json_integer(partition.filesystem)); |
---|
1221 | json_object_set_new(partition_json, "os", |
---|
1222 | json_integer(partition.os)); |
---|
1223 | json_object_set_new(partition_json, "image", |
---|
1224 | json_integer(partition.image)); |
---|
1225 | json_object_set_new(partition_json, "software", |
---|
1226 | json_integer(partition.software)); |
---|
1227 | json_array_append_new(partitions_array, partition_json); |
---|
1228 | |
---|
1229 | ++len_part; |
---|
1230 | } |
---|
1231 | |
---|
1232 | dbi_result_free(result); |
---|
1233 | og_dbi_close(dbi); |
---|
1234 | |
---|
1235 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
1236 | json_decref(root); |
---|
1237 | return 0; |
---|
1238 | } |
---|
1239 | |
---|
1240 | static int og_cmd_get_client_info(json_t *element, |
---|
1241 | struct og_msg_params *params, |
---|
1242 | char *buffer_reply) |
---|
1243 | { |
---|
1244 | struct og_computer computer = {}; |
---|
1245 | json_t *value, *root; |
---|
1246 | struct in_addr addr; |
---|
1247 | struct og_dbi *dbi; |
---|
1248 | const char *key; |
---|
1249 | int err = 0; |
---|
1250 | |
---|
1251 | struct og_buffer og_buffer = { |
---|
1252 | .data = buffer_reply |
---|
1253 | }; |
---|
1254 | |
---|
1255 | json_object_foreach(element, key, value) { |
---|
1256 | if (!strcmp(key, "client")) { |
---|
1257 | err = og_json_parse_clients(value, params); |
---|
1258 | } |
---|
1259 | |
---|
1260 | if (err < 0) |
---|
1261 | break; |
---|
1262 | } |
---|
1263 | |
---|
1264 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1265 | return -1; |
---|
1266 | |
---|
1267 | if (params->ips_array_len != 1) |
---|
1268 | return -1; |
---|
1269 | |
---|
1270 | if (inet_aton(params->ips_array[0], &addr) == 0) |
---|
1271 | return -1; |
---|
1272 | |
---|
1273 | dbi = og_dbi_open(&ogconfig.db); |
---|
1274 | if (!dbi) { |
---|
1275 | syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", |
---|
1276 | __func__, __LINE__); |
---|
1277 | return -1; |
---|
1278 | } |
---|
1279 | |
---|
1280 | if (og_dbi_get_computer_info(dbi, &computer, addr)) { |
---|
1281 | og_dbi_close(dbi); |
---|
1282 | return -1; |
---|
1283 | } |
---|
1284 | |
---|
1285 | og_dbi_close(dbi); |
---|
1286 | |
---|
1287 | root = json_object(); |
---|
1288 | if (!root) |
---|
1289 | return -1; |
---|
1290 | |
---|
1291 | json_object_set_new(root, "serial_number", |
---|
1292 | json_string(computer.serial_number)); |
---|
1293 | json_object_set_new(root, "hardware_id", |
---|
1294 | json_integer(computer.hardware_id)); |
---|
1295 | json_object_set_new(root, "netdriver", json_string(computer.netdriver)); |
---|
1296 | json_object_set_new(root, "maintenance", json_boolean(computer.name)); |
---|
1297 | json_object_set_new(root, "netiface", json_string(computer.netiface)); |
---|
1298 | json_object_set_new(root, "repo_id", json_integer(computer.repo_id)); |
---|
1299 | json_object_set_new(root, "livedir", json_string(computer.livedir)); |
---|
1300 | json_object_set_new(root, "netmask", json_string(computer.netmask)); |
---|
1301 | json_object_set_new(root, "center", json_integer(computer.center)); |
---|
1302 | json_object_set_new(root, "remote", json_boolean(computer.remote)); |
---|
1303 | json_object_set_new(root, "room", json_integer(computer.room)); |
---|
1304 | json_object_set_new(root, "name", json_string(computer.name)); |
---|
1305 | json_object_set_new(root, "boot", json_string(computer.boot)); |
---|
1306 | json_object_set_new(root, "mac", json_string(computer.mac)); |
---|
1307 | json_object_set_new(root, "id", json_integer(computer.id)); |
---|
1308 | json_object_set_new(root, "ip", json_string(computer.ip)); |
---|
1309 | |
---|
1310 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
1311 | json_decref(root); |
---|
1312 | return 0; |
---|
1313 | } |
---|
1314 | |
---|
1315 | static int og_cmd_post_client_add(json_t *element, |
---|
1316 | struct og_msg_params *params, |
---|
1317 | char *buffer_reply) |
---|
1318 | { |
---|
1319 | struct og_computer computer = {}; |
---|
1320 | const char *key, *msglog; |
---|
1321 | struct og_dbi *dbi; |
---|
1322 | dbi_result result; |
---|
1323 | json_t *value; |
---|
1324 | int err = 0; |
---|
1325 | |
---|
1326 | json_object_foreach(element, key, value) { |
---|
1327 | if (!strcmp(key, "serial_number")) { |
---|
1328 | err = og_json_parse_string_copy(value, |
---|
1329 | computer.serial_number, |
---|
1330 | sizeof(computer.serial_number)); |
---|
1331 | } else if (!strcmp(key, "hardware_id")) { |
---|
1332 | err = og_json_parse_uint(value, &computer.hardware_id); |
---|
1333 | } else if (!strcmp(key, "netdriver")) { |
---|
1334 | err = og_json_parse_string_copy(value, |
---|
1335 | computer.netdriver, |
---|
1336 | sizeof(computer.netdriver)); |
---|
1337 | } else if (!strcmp(key, "maintenance")) { |
---|
1338 | err = og_json_parse_bool(value, &computer.maintenance); |
---|
1339 | } else if (!strcmp(key, "netiface")) { |
---|
1340 | err = og_json_parse_string_copy(value, |
---|
1341 | computer.netiface, |
---|
1342 | sizeof(computer.netiface)); |
---|
1343 | } else if (!strcmp(key, "repo_id")) { |
---|
1344 | err = og_json_parse_uint(value, &computer.repo_id); |
---|
1345 | } else if (!strcmp(key, "livedir")) { |
---|
1346 | err = og_json_parse_string_copy(value, |
---|
1347 | computer.livedir, |
---|
1348 | sizeof(computer.livedir)); |
---|
1349 | } else if (!strcmp(key, "netmask")) { |
---|
1350 | err = og_json_parse_string_copy(value, |
---|
1351 | computer.netmask, |
---|
1352 | sizeof(computer.netmask)); |
---|
1353 | } else if (!strcmp(key, "remote")) { |
---|
1354 | err = og_json_parse_bool(value, &computer.remote); |
---|
1355 | } else if (!strcmp(key, "room")) { |
---|
1356 | err = og_json_parse_uint(value, &computer.room); |
---|
1357 | } else if (!strcmp(key, "name")) { |
---|
1358 | err = og_json_parse_string_copy(value, |
---|
1359 | computer.name, |
---|
1360 | sizeof(computer.name)); |
---|
1361 | } else if (!strcmp(key, "boot")) { |
---|
1362 | err = og_json_parse_string_copy(value, |
---|
1363 | computer.boot, |
---|
1364 | sizeof(computer.boot)); |
---|
1365 | } else if (!strcmp(key, "mac")) { |
---|
1366 | err = og_json_parse_string_copy(value, |
---|
1367 | computer.mac, |
---|
1368 | sizeof(computer.mac)); |
---|
1369 | } else if (!strcmp(key, "ip")) { |
---|
1370 | err = og_json_parse_string_copy(value, |
---|
1371 | computer.ip, |
---|
1372 | sizeof(computer.ip)); |
---|
1373 | } |
---|
1374 | |
---|
1375 | if (err < 0) |
---|
1376 | break; |
---|
1377 | } |
---|
1378 | |
---|
1379 | dbi = og_dbi_open(&ogconfig.db); |
---|
1380 | if (!dbi) { |
---|
1381 | syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", |
---|
1382 | __func__, __LINE__); |
---|
1383 | return -1; |
---|
1384 | } |
---|
1385 | |
---|
1386 | result = dbi_conn_queryf(dbi->conn, |
---|
1387 | "SELECT ip FROM ordenadores WHERE ip='%s'", |
---|
1388 | computer.ip); |
---|
1389 | |
---|
1390 | if (!result) { |
---|
1391 | dbi_conn_error(dbi->conn, &msglog); |
---|
1392 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1393 | __func__, __LINE__, msglog); |
---|
1394 | og_dbi_close(dbi); |
---|
1395 | return -1; |
---|
1396 | } |
---|
1397 | |
---|
1398 | if (dbi_result_get_numrows(result) > 0) { |
---|
1399 | syslog(LOG_ERR, "client with the same IP already exists: %s\n", |
---|
1400 | computer.ip); |
---|
1401 | dbi_result_free(result); |
---|
1402 | og_dbi_close(dbi); |
---|
1403 | return -1; |
---|
1404 | } |
---|
1405 | dbi_result_free(result); |
---|
1406 | |
---|
1407 | result = dbi_conn_queryf(dbi->conn, |
---|
1408 | "INSERT INTO ordenadores(" |
---|
1409 | " nombreordenador," |
---|
1410 | " numserie," |
---|
1411 | " ip," |
---|
1412 | " mac," |
---|
1413 | " idaula," |
---|
1414 | " idperfilhard," |
---|
1415 | " idrepositorio," |
---|
1416 | " mascara," |
---|
1417 | " arranque," |
---|
1418 | " netiface," |
---|
1419 | " netdriver," |
---|
1420 | " oglivedir," |
---|
1421 | " inremotepc," |
---|
1422 | " maintenance" |
---|
1423 | ") VALUES ('%s', '%s', '%s', '%s', %u, %u," |
---|
1424 | " %u, '%s', '%s', '%s', '%s'," |
---|
1425 | " '%s', %u, %u)", |
---|
1426 | computer.name, computer.serial_number, |
---|
1427 | computer.ip, computer.mac, computer.room, |
---|
1428 | computer.hardware_id, computer.repo_id, |
---|
1429 | computer.netmask, computer.boot, |
---|
1430 | computer.netiface, computer.netdriver, |
---|
1431 | computer.livedir, computer.remote, |
---|
1432 | computer.maintenance); |
---|
1433 | |
---|
1434 | if (!result) { |
---|
1435 | dbi_conn_error(dbi->conn, &msglog); |
---|
1436 | syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n", |
---|
1437 | __func__, __LINE__, msglog); |
---|
1438 | og_dbi_close(dbi); |
---|
1439 | return -1; |
---|
1440 | } |
---|
1441 | |
---|
1442 | dbi_result_free(result); |
---|
1443 | og_dbi_close(dbi); |
---|
1444 | return 0; |
---|
1445 | } |
---|
1446 | |
---|
1447 | static int og_cmd_post_client_delete(json_t *element, |
---|
1448 | struct og_msg_params *params) |
---|
1449 | { |
---|
1450 | const char *key, *msglog; |
---|
1451 | struct og_dbi *dbi; |
---|
1452 | dbi_result result; |
---|
1453 | unsigned int i; |
---|
1454 | json_t *value; |
---|
1455 | int err = 0; |
---|
1456 | |
---|
1457 | json_object_foreach(element, key, value) { |
---|
1458 | if (!strcmp(key, "clients")) |
---|
1459 | err = og_json_parse_clients(value, params); |
---|
1460 | else |
---|
1461 | err = -1; |
---|
1462 | |
---|
1463 | if (err < 0) |
---|
1464 | break; |
---|
1465 | } |
---|
1466 | |
---|
1467 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1468 | return -1; |
---|
1469 | |
---|
1470 | dbi = og_dbi_open(&ogconfig.db); |
---|
1471 | if (!dbi) { |
---|
1472 | syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", |
---|
1473 | __func__, __LINE__); |
---|
1474 | return -1; |
---|
1475 | } |
---|
1476 | |
---|
1477 | for (i = 0; i < params->ips_array_len; i++) { |
---|
1478 | result = dbi_conn_queryf(dbi->conn, |
---|
1479 | "DELETE FROM ordenadores WHERE ip='%s'", |
---|
1480 | params->ips_array[i]); |
---|
1481 | |
---|
1482 | if (!result) { |
---|
1483 | dbi_conn_error(dbi->conn, &msglog); |
---|
1484 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1485 | __func__, __LINE__, msglog); |
---|
1486 | og_dbi_close(dbi); |
---|
1487 | return -1; |
---|
1488 | } |
---|
1489 | |
---|
1490 | dbi_result_free(result); |
---|
1491 | } |
---|
1492 | |
---|
1493 | og_dbi_close(dbi); |
---|
1494 | return 0; |
---|
1495 | } |
---|
1496 | |
---|
1497 | static int og_cmd_stop(json_t *element, struct og_msg_params *params) |
---|
1498 | { |
---|
1499 | const char *key; |
---|
1500 | json_t *value; |
---|
1501 | int err = 0; |
---|
1502 | |
---|
1503 | if (json_typeof(element) != JSON_OBJECT) |
---|
1504 | return -1; |
---|
1505 | |
---|
1506 | json_object_foreach(element, key, value) { |
---|
1507 | if (!strcmp(key, "clients")) |
---|
1508 | err = og_json_parse_clients(value, params); |
---|
1509 | |
---|
1510 | if (err < 0) |
---|
1511 | break; |
---|
1512 | } |
---|
1513 | |
---|
1514 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1515 | return -1; |
---|
1516 | |
---|
1517 | return og_send_request(OG_METHOD_POST, OG_CMD_STOP, params, NULL); |
---|
1518 | } |
---|
1519 | |
---|
1520 | static int og_cmd_hardware(json_t *element, struct og_msg_params *params) |
---|
1521 | { |
---|
1522 | const char *key; |
---|
1523 | json_t *value; |
---|
1524 | int err = 0; |
---|
1525 | |
---|
1526 | if (json_typeof(element) != JSON_OBJECT) |
---|
1527 | return -1; |
---|
1528 | |
---|
1529 | json_object_foreach(element, key, value) { |
---|
1530 | if (!strcmp(key, "clients")) |
---|
1531 | err = og_json_parse_clients(value, params); |
---|
1532 | |
---|
1533 | if (err < 0) |
---|
1534 | break; |
---|
1535 | } |
---|
1536 | |
---|
1537 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1538 | return -1; |
---|
1539 | |
---|
1540 | return og_send_request(OG_METHOD_GET, OG_CMD_HARDWARE, params, NULL); |
---|
1541 | } |
---|
1542 | |
---|
1543 | static int og_cmd_get_hardware(json_t *element, struct og_msg_params *params, |
---|
1544 | char *buffer_reply) |
---|
1545 | { |
---|
1546 | const char *key, *msglog, *hw_item, *hw_type; |
---|
1547 | json_t *value, *root, *array, *item; |
---|
1548 | struct og_dbi *dbi; |
---|
1549 | dbi_result result; |
---|
1550 | int err = 0; |
---|
1551 | |
---|
1552 | struct og_buffer og_buffer = { |
---|
1553 | .data = buffer_reply |
---|
1554 | }; |
---|
1555 | |
---|
1556 | json_object_foreach(element, key, value) { |
---|
1557 | if (!strcmp(key, "client")) |
---|
1558 | err = og_json_parse_clients(value, params); |
---|
1559 | else |
---|
1560 | err = -1; |
---|
1561 | |
---|
1562 | if (err < 0) |
---|
1563 | return err; |
---|
1564 | } |
---|
1565 | |
---|
1566 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
1567 | return -1; |
---|
1568 | |
---|
1569 | dbi = og_dbi_open(&ogconfig.db); |
---|
1570 | if (!dbi) { |
---|
1571 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
1572 | __func__, __LINE__); |
---|
1573 | return -1; |
---|
1574 | } |
---|
1575 | |
---|
1576 | result = dbi_conn_queryf(dbi->conn, |
---|
1577 | "SELECT hardwares.descripcion AS item, " |
---|
1578 | " tipohardwares.descripcion AS type " |
---|
1579 | "FROM hardwares " |
---|
1580 | "INNER JOIN perfileshard_hardwares " |
---|
1581 | " ON hardwares.idhardware = perfileshard_hardwares.idhardware " |
---|
1582 | "INNER JOIN ordenadores " |
---|
1583 | " ON perfileshard_hardwares.idperfilhard = ordenadores.idperfilhard " |
---|
1584 | "INNER JOIN tipohardwares " |
---|
1585 | " ON hardwares.idtipohardware = tipohardwares.idtipohardware " |
---|
1586 | "WHERE ordenadores.ip = '%s'", |
---|
1587 | params->ips_array[0]); |
---|
1588 | if (!result) { |
---|
1589 | dbi_conn_error(dbi->conn, &msglog); |
---|
1590 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1591 | __func__, __LINE__, msglog); |
---|
1592 | og_dbi_close(dbi); |
---|
1593 | return -1; |
---|
1594 | } |
---|
1595 | |
---|
1596 | array = json_array(); |
---|
1597 | if (!array) { |
---|
1598 | dbi_result_free(result); |
---|
1599 | og_dbi_close(dbi); |
---|
1600 | return -1; |
---|
1601 | } |
---|
1602 | |
---|
1603 | while (dbi_result_next_row(result)) { |
---|
1604 | item = json_object(); |
---|
1605 | if (!item) { |
---|
1606 | dbi_result_free(result); |
---|
1607 | og_dbi_close(dbi); |
---|
1608 | json_decref(array); |
---|
1609 | return -1; |
---|
1610 | } |
---|
1611 | |
---|
1612 | hw_item = dbi_result_get_string(result, "item"); |
---|
1613 | hw_type = dbi_result_get_string(result, "type"); |
---|
1614 | |
---|
1615 | json_object_set_new(item, "type", json_string(hw_type)); |
---|
1616 | json_object_set_new(item, "description", json_string(hw_item)); |
---|
1617 | json_array_append_new(array, item); |
---|
1618 | } |
---|
1619 | |
---|
1620 | dbi_result_free(result); |
---|
1621 | og_dbi_close(dbi); |
---|
1622 | |
---|
1623 | root = json_object(); |
---|
1624 | if (!root){ |
---|
1625 | json_decref(array); |
---|
1626 | return -1; |
---|
1627 | } |
---|
1628 | |
---|
1629 | json_object_set_new(root, "hardware", array); |
---|
1630 | |
---|
1631 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
1632 | json_decref(root); |
---|
1633 | return 0; |
---|
1634 | } |
---|
1635 | |
---|
1636 | static int og_cmd_software(json_t *element, struct og_msg_params *params) |
---|
1637 | { |
---|
1638 | json_t *clients, *value; |
---|
1639 | const char *key; |
---|
1640 | int err = 0; |
---|
1641 | |
---|
1642 | if (json_typeof(element) != JSON_OBJECT) |
---|
1643 | return -1; |
---|
1644 | |
---|
1645 | json_object_foreach(element, key, value) { |
---|
1646 | if (!strcmp(key, "clients")) |
---|
1647 | err = og_json_parse_clients(value, params); |
---|
1648 | else if (!strcmp(key, "disk")) { |
---|
1649 | err = og_json_parse_string(value, ¶ms->disk); |
---|
1650 | params->flags |= OG_REST_PARAM_DISK; |
---|
1651 | } |
---|
1652 | else if (!strcmp(key, "partition")) { |
---|
1653 | err = og_json_parse_string(value, ¶ms->partition); |
---|
1654 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
1655 | } |
---|
1656 | |
---|
1657 | if (err < 0) |
---|
1658 | break; |
---|
1659 | } |
---|
1660 | |
---|
1661 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
1662 | OG_REST_PARAM_DISK | |
---|
1663 | OG_REST_PARAM_PARTITION)) |
---|
1664 | return -1; |
---|
1665 | |
---|
1666 | clients = json_copy(element); |
---|
1667 | json_object_del(clients, "clients"); |
---|
1668 | |
---|
1669 | return og_send_request(OG_METHOD_POST, OG_CMD_SOFTWARE, params, clients); |
---|
1670 | } |
---|
1671 | |
---|
1672 | static int og_cmd_get_software(json_t *element, struct og_msg_params *params, |
---|
1673 | char *buffer_reply) |
---|
1674 | { |
---|
1675 | json_t *value, *software, *root; |
---|
1676 | const char *key, *msglog, *name; |
---|
1677 | uint64_t disk, partition; |
---|
1678 | uint64_t flags = 0; |
---|
1679 | struct og_dbi *dbi; |
---|
1680 | dbi_result result; |
---|
1681 | int err = 0; |
---|
1682 | |
---|
1683 | struct og_buffer og_buffer = { |
---|
1684 | .data = buffer_reply |
---|
1685 | }; |
---|
1686 | |
---|
1687 | if (json_typeof(element) != JSON_OBJECT) |
---|
1688 | return -1; |
---|
1689 | |
---|
1690 | json_object_foreach(element, key, value) { |
---|
1691 | if (!strcmp(key, "client")) { |
---|
1692 | err = og_json_parse_clients(value, params); |
---|
1693 | } else if (!strcmp(key, "disk")) { |
---|
1694 | err = og_json_parse_uint64(value, &disk); |
---|
1695 | flags |= OG_REST_PARAM_DISK; |
---|
1696 | } else if (!strcmp(key, "partition")) { |
---|
1697 | err = og_json_parse_uint64(value, &partition); |
---|
1698 | flags |= OG_REST_PARAM_PARTITION; |
---|
1699 | } |
---|
1700 | |
---|
1701 | if (err < 0) |
---|
1702 | break; |
---|
1703 | } |
---|
1704 | |
---|
1705 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR) || |
---|
1706 | !og_flags_validate(flags, OG_REST_PARAM_DISK | |
---|
1707 | OG_REST_PARAM_PARTITION)) |
---|
1708 | return -1; |
---|
1709 | |
---|
1710 | dbi = og_dbi_open(&ogconfig.db); |
---|
1711 | if (!dbi) { |
---|
1712 | syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", |
---|
1713 | __func__, __LINE__); |
---|
1714 | return -1; |
---|
1715 | } |
---|
1716 | |
---|
1717 | result = dbi_conn_queryf(dbi->conn, |
---|
1718 | "SELECT s.descripcion " |
---|
1719 | "FROM softwares s " |
---|
1720 | "INNER JOIN perfilessoft_softwares pss " |
---|
1721 | "ON s.idsoftware = pss.idsoftware " |
---|
1722 | "INNER JOIN ordenadores_particiones op " |
---|
1723 | "ON pss.idperfilsoft = op.idperfilsoft " |
---|
1724 | "INNER JOIN ordenadores o " |
---|
1725 | "ON o.idordenador = op.idordenador " |
---|
1726 | "WHERE o.ip='%s' AND " |
---|
1727 | " op.numdisk=%lu AND " |
---|
1728 | " op.numpar=%lu", |
---|
1729 | params->ips_array[0], disk, partition); |
---|
1730 | if (!result) { |
---|
1731 | dbi_conn_error(dbi->conn, &msglog); |
---|
1732 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
1733 | __func__, __LINE__, msglog); |
---|
1734 | return -1; |
---|
1735 | } |
---|
1736 | software = json_array(); |
---|
1737 | if (!software) { |
---|
1738 | dbi_result_free(result); |
---|
1739 | og_dbi_close(dbi); |
---|
1740 | return -1; |
---|
1741 | } |
---|
1742 | |
---|
1743 | while (dbi_result_next_row(result)) { |
---|
1744 | name = dbi_result_get_string(result, "descripcion"); |
---|
1745 | json_array_append_new(software, json_string(name)); |
---|
1746 | } |
---|
1747 | |
---|
1748 | dbi_result_free(result); |
---|
1749 | og_dbi_close(dbi); |
---|
1750 | |
---|
1751 | root = json_object(); |
---|
1752 | if (!root) { |
---|
1753 | json_decref(software); |
---|
1754 | return -1; |
---|
1755 | } |
---|
1756 | json_object_set_new(root, "software", software); |
---|
1757 | |
---|
1758 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
1759 | json_decref(root); |
---|
1760 | return 0; |
---|
1761 | } |
---|
1762 | |
---|
1763 | #define OG_IMAGE_TYPE_MAXLEN 4 |
---|
1764 | |
---|
1765 | static int og_get_image_stats(const char *name, |
---|
1766 | struct stat *image_stats) |
---|
1767 | { |
---|
1768 | const char *dir = ogconfig.repo.dir; |
---|
1769 | char filename[PATH_MAX + 1]; |
---|
1770 | |
---|
1771 | snprintf(filename, sizeof(filename), "%s/%s.img", dir, name); |
---|
1772 | if (stat(filename, image_stats) < 0) { |
---|
1773 | syslog(LOG_ERR, "%s image does not exists", name); |
---|
1774 | return -1; |
---|
1775 | } |
---|
1776 | return 0; |
---|
1777 | } |
---|
1778 | |
---|
1779 | static json_t *og_json_disk_alloc() |
---|
1780 | { |
---|
1781 | const char *dir = ogconfig.repo.dir; |
---|
1782 | struct statvfs buffer; |
---|
1783 | json_t *disk_json; |
---|
1784 | int ret; |
---|
1785 | |
---|
1786 | ret = statvfs(dir, &buffer); |
---|
1787 | if (ret) |
---|
1788 | return NULL; |
---|
1789 | |
---|
1790 | disk_json = json_object(); |
---|
1791 | if (!disk_json) |
---|
1792 | return NULL; |
---|
1793 | |
---|
1794 | json_object_set_new(disk_json, "total", |
---|
1795 | json_integer(buffer.f_blocks * buffer.f_frsize)); |
---|
1796 | json_object_set_new(disk_json, "free", |
---|
1797 | json_integer(buffer.f_bfree * buffer.f_frsize)); |
---|
1798 | |
---|
1799 | return disk_json; |
---|
1800 | } |
---|
1801 | |
---|
1802 | #define OG_PERMS_IRWX (S_IRWXU | S_IRWXG | S_IRWXO) |
---|
1803 | #define OG_PERMS_MAXLEN 4 |
---|
1804 | |
---|
1805 | static json_t *og_json_image_alloc(struct og_image *image) |
---|
1806 | { |
---|
1807 | char perms_string[OG_PERMS_MAXLEN]; |
---|
1808 | json_t *image_json; |
---|
1809 | char *modified; |
---|
1810 | uint16_t perms; |
---|
1811 | |
---|
1812 | image_json = json_object(); |
---|
1813 | if (!image_json) |
---|
1814 | return NULL; |
---|
1815 | |
---|
1816 | perms = image->image_stats.st_mode & OG_PERMS_IRWX; |
---|
1817 | snprintf(perms_string, sizeof(perms_string), "%o", perms); |
---|
1818 | |
---|
1819 | modified = ctime(&image->image_stats.st_mtime); |
---|
1820 | modified[strlen(modified) - 1] = '\0'; |
---|
1821 | |
---|
1822 | json_object_set_new(image_json, "name", |
---|
1823 | json_string(image->name)); |
---|
1824 | json_object_set_new(image_json, "datasize", |
---|
1825 | json_integer(image->datasize)); |
---|
1826 | json_object_set_new(image_json, "size", |
---|
1827 | json_integer(image->image_stats.st_size)); |
---|
1828 | json_object_set_new(image_json, "modified", |
---|
1829 | json_string(modified)); |
---|
1830 | json_object_set_new(image_json, "permissions", |
---|
1831 | json_string(perms_string)); |
---|
1832 | json_object_set_new(image_json, "software_id", |
---|
1833 | json_integer(image->software_id)); |
---|
1834 | json_object_set_new(image_json, "type", |
---|
1835 | json_integer(image->type)); |
---|
1836 | json_object_set_new(image_json, "id", |
---|
1837 | json_integer(image->id)); |
---|
1838 | |
---|
1839 | return image_json; |
---|
1840 | } |
---|
1841 | |
---|
1842 | static int og_cmd_images(char *buffer_reply) |
---|
1843 | { |
---|
1844 | json_t *root, *images, *image_json, *disk_json; |
---|
1845 | struct og_buffer og_buffer = { |
---|
1846 | .data = buffer_reply |
---|
1847 | }; |
---|
1848 | struct og_image image; |
---|
1849 | struct og_dbi *dbi; |
---|
1850 | dbi_result result; |
---|
1851 | |
---|
1852 | root = json_object(); |
---|
1853 | if (!root) |
---|
1854 | return -1; |
---|
1855 | |
---|
1856 | images = json_array(); |
---|
1857 | if (!images) { |
---|
1858 | json_decref(root); |
---|
1859 | return -1; |
---|
1860 | } |
---|
1861 | |
---|
1862 | json_object_set_new(root, "images", images); |
---|
1863 | |
---|
1864 | dbi = og_dbi_open(&ogconfig.db); |
---|
1865 | if (!dbi) { |
---|
1866 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
1867 | __func__, __LINE__); |
---|
1868 | json_decref(root); |
---|
1869 | return -1; |
---|
1870 | } |
---|
1871 | |
---|
1872 | result = dbi_conn_queryf(dbi->conn, |
---|
1873 | "SELECT i.nombreca, o.nombreordenador, " |
---|
1874 | " i.clonator, i.compressor, " |
---|
1875 | " i.filesystem, i.datasize, " |
---|
1876 | " i.idperfilsoft, i.tipo, " |
---|
1877 | " i.idimagen " |
---|
1878 | "FROM imagenes i " |
---|
1879 | "LEFT JOIN ordenadores o " |
---|
1880 | "ON i.idordenador = o.idordenador"); |
---|
1881 | |
---|
1882 | while (dbi_result_next_row(result)) { |
---|
1883 | image = (struct og_image){0}; |
---|
1884 | image.datasize = dbi_result_get_ulonglong(result, "datasize"); |
---|
1885 | image.software_id = dbi_result_get_ulonglong(result, "idperfilsoft"); |
---|
1886 | image.type = dbi_result_get_ulonglong(result, "tipo"); |
---|
1887 | image.id = dbi_result_get_ulonglong(result, "idimagen"); |
---|
1888 | snprintf(image.name, sizeof(image.name), "%s", |
---|
1889 | dbi_result_get_string(result, "nombreca")); |
---|
1890 | |
---|
1891 | if (og_get_image_stats(image.name, &image.image_stats)) { |
---|
1892 | continue; |
---|
1893 | } |
---|
1894 | |
---|
1895 | image_json = og_json_image_alloc(&image); |
---|
1896 | if (!image_json) { |
---|
1897 | dbi_result_free(result); |
---|
1898 | og_dbi_close(dbi); |
---|
1899 | json_decref(root); |
---|
1900 | return -1; |
---|
1901 | } |
---|
1902 | |
---|
1903 | json_array_append(images, image_json); |
---|
1904 | } |
---|
1905 | |
---|
1906 | dbi_result_free(result); |
---|
1907 | og_dbi_close(dbi); |
---|
1908 | |
---|
1909 | disk_json = og_json_disk_alloc(); |
---|
1910 | if (!disk_json) { |
---|
1911 | syslog(LOG_ERR, "cannot allocate disk json"); |
---|
1912 | json_decref(root); |
---|
1913 | return -1; |
---|
1914 | } |
---|
1915 | |
---|
1916 | json_object_set_new(root, "disk", disk_json); |
---|
1917 | |
---|
1918 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
1919 | json_decref(root); |
---|
1920 | |
---|
1921 | return 0; |
---|
1922 | } |
---|
1923 | |
---|
1924 | static int og_cmd_create_image(json_t *element, struct og_msg_params *params) |
---|
1925 | { |
---|
1926 | char new_image_id[OG_DB_INT_MAXLEN + 1]; |
---|
1927 | struct og_image image = {}; |
---|
1928 | json_t *value, *clients; |
---|
1929 | struct og_dbi *dbi; |
---|
1930 | const char *key; |
---|
1931 | int err = 0; |
---|
1932 | |
---|
1933 | if (json_typeof(element) != JSON_OBJECT) |
---|
1934 | return -1; |
---|
1935 | |
---|
1936 | json_object_foreach(element, key, value) { |
---|
1937 | if (!strcmp(key, "disk")) { |
---|
1938 | err = og_json_parse_string(value, ¶ms->disk); |
---|
1939 | params->flags |= OG_REST_PARAM_DISK; |
---|
1940 | } else if (!strcmp(key, "partition")) { |
---|
1941 | err = og_json_parse_string(value, ¶ms->partition); |
---|
1942 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
1943 | } else if (!strcmp(key, "name")) { |
---|
1944 | err = og_json_parse_string(value, ¶ms->name); |
---|
1945 | params->flags |= OG_REST_PARAM_NAME; |
---|
1946 | } else if (!strcmp(key, "repository")) { |
---|
1947 | err = og_json_parse_string(value, ¶ms->repository); |
---|
1948 | params->flags |= OG_REST_PARAM_REPO; |
---|
1949 | } else if (!strcmp(key, "clients")) { |
---|
1950 | err = og_json_parse_clients(value, params); |
---|
1951 | } else if (!strcmp(key, "id")) { |
---|
1952 | err = og_json_parse_string(value, ¶ms->id); |
---|
1953 | params->flags |= OG_REST_PARAM_ID; |
---|
1954 | } else if (!strcmp(key, "code")) { |
---|
1955 | err = og_json_parse_string(value, ¶ms->code); |
---|
1956 | params->flags |= OG_REST_PARAM_CODE; |
---|
1957 | } else if (!strcmp(key, "description")) { |
---|
1958 | err = og_json_parse_string_copy(value, |
---|
1959 | image.description, |
---|
1960 | sizeof(image.description)); |
---|
1961 | } else if (!strcmp(key, "group_id")) { |
---|
1962 | err = og_json_parse_uint64(value, &image.group_id); |
---|
1963 | } else if (!strcmp(key, "center_id")) { |
---|
1964 | err = og_json_parse_uint64(value, &image.center_id); |
---|
1965 | } |
---|
1966 | |
---|
1967 | |
---|
1968 | if (err < 0) |
---|
1969 | break; |
---|
1970 | } |
---|
1971 | |
---|
1972 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
1973 | OG_REST_PARAM_DISK | |
---|
1974 | OG_REST_PARAM_PARTITION | |
---|
1975 | OG_REST_PARAM_CODE | |
---|
1976 | OG_REST_PARAM_ID | |
---|
1977 | OG_REST_PARAM_NAME | |
---|
1978 | OG_REST_PARAM_REPO)) |
---|
1979 | return -1; |
---|
1980 | |
---|
1981 | /* If there is a description, this means the image is not in the DB. */ |
---|
1982 | if (image.description[0]) { |
---|
1983 | snprintf(image.name, sizeof(image.name), "%s", params->name); |
---|
1984 | |
---|
1985 | dbi = og_dbi_open(&ogconfig.db); |
---|
1986 | if (!dbi) { |
---|
1987 | syslog(LOG_ERR, |
---|
1988 | "cannot open connection database (%s:%d)\n", |
---|
1989 | __func__, __LINE__); |
---|
1990 | return -1; |
---|
1991 | } |
---|
1992 | |
---|
1993 | err = og_dbi_add_image(dbi, &image); |
---|
1994 | |
---|
1995 | og_dbi_close(dbi); |
---|
1996 | if (err < 0) |
---|
1997 | return err; |
---|
1998 | |
---|
1999 | snprintf(new_image_id, sizeof(new_image_id), "%u", err); |
---|
2000 | params->id = new_image_id; |
---|
2001 | } |
---|
2002 | |
---|
2003 | clients = json_copy(element); |
---|
2004 | json_object_del(clients, "clients"); |
---|
2005 | |
---|
2006 | return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params, |
---|
2007 | clients); |
---|
2008 | } |
---|
2009 | |
---|
2010 | static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) |
---|
2011 | { |
---|
2012 | json_t *clients, *value; |
---|
2013 | const char *key; |
---|
2014 | int err = 0; |
---|
2015 | |
---|
2016 | if (json_typeof(element) != JSON_OBJECT) |
---|
2017 | return -1; |
---|
2018 | |
---|
2019 | json_object_foreach(element, key, value) { |
---|
2020 | if (!strcmp(key, "disk")) { |
---|
2021 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2022 | params->flags |= OG_REST_PARAM_DISK; |
---|
2023 | } else if (!strcmp(key, "partition")) { |
---|
2024 | err = og_json_parse_string(value, ¶ms->partition); |
---|
2025 | params->flags |= OG_REST_PARAM_PARTITION; |
---|
2026 | } else if (!strcmp(key, "name")) { |
---|
2027 | err = og_json_parse_string(value, ¶ms->name); |
---|
2028 | params->flags |= OG_REST_PARAM_NAME; |
---|
2029 | } else if (!strcmp(key, "repository")) { |
---|
2030 | err = og_json_parse_string(value, ¶ms->repository); |
---|
2031 | params->flags |= OG_REST_PARAM_REPO; |
---|
2032 | } else if (!strcmp(key, "clients")) { |
---|
2033 | err = og_json_parse_clients(value, params); |
---|
2034 | } else if (!strcmp(key, "type")) { |
---|
2035 | err = og_json_parse_string(value, ¶ms->type); |
---|
2036 | params->flags |= OG_REST_PARAM_TYPE; |
---|
2037 | } else if (!strcmp(key, "profile")) { |
---|
2038 | err = og_json_parse_string(value, ¶ms->profile); |
---|
2039 | params->flags |= OG_REST_PARAM_PROFILE; |
---|
2040 | } else if (!strcmp(key, "id")) { |
---|
2041 | err = og_json_parse_string(value, ¶ms->id); |
---|
2042 | params->flags |= OG_REST_PARAM_ID; |
---|
2043 | } |
---|
2044 | |
---|
2045 | if (err < 0) |
---|
2046 | break; |
---|
2047 | } |
---|
2048 | |
---|
2049 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2050 | OG_REST_PARAM_DISK | |
---|
2051 | OG_REST_PARAM_PARTITION | |
---|
2052 | OG_REST_PARAM_NAME | |
---|
2053 | OG_REST_PARAM_REPO | |
---|
2054 | OG_REST_PARAM_TYPE | |
---|
2055 | OG_REST_PARAM_PROFILE | |
---|
2056 | OG_REST_PARAM_ID)) |
---|
2057 | return -1; |
---|
2058 | |
---|
2059 | clients = json_copy(element); |
---|
2060 | json_object_del(clients, "clients"); |
---|
2061 | |
---|
2062 | return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, params, |
---|
2063 | clients); |
---|
2064 | } |
---|
2065 | |
---|
2066 | static int og_cmd_setup(json_t *element, struct og_msg_params *params) |
---|
2067 | { |
---|
2068 | json_t *value, *clients; |
---|
2069 | const char *key; |
---|
2070 | int err = 0; |
---|
2071 | |
---|
2072 | if (json_typeof(element) != JSON_OBJECT) |
---|
2073 | return -1; |
---|
2074 | |
---|
2075 | json_object_foreach(element, key, value) { |
---|
2076 | if (!strcmp(key, "clients")) { |
---|
2077 | err = og_json_parse_clients(value, params); |
---|
2078 | } else if (!strcmp(key, "type")) { |
---|
2079 | err = og_json_parse_string(value, ¶ms->type); |
---|
2080 | params->flags |= OG_REST_PARAM_TYPE; |
---|
2081 | } else if (!strcmp(key, "disk")) { |
---|
2082 | err = og_json_parse_string(value, ¶ms->disk); |
---|
2083 | params->flags |= OG_REST_PARAM_DISK; |
---|
2084 | } else if (!strcmp(key, "cache")) { |
---|
2085 | err = og_json_parse_string(value, ¶ms->cache); |
---|
2086 | params->flags |= OG_REST_PARAM_CACHE; |
---|
2087 | } else if (!strcmp(key, "cache_size")) { |
---|
2088 | err = og_json_parse_string(value, ¶ms->cache_size); |
---|
2089 | params->flags |= OG_REST_PARAM_CACHE_SIZE; |
---|
2090 | } else if (!strcmp(key, "partition_setup")) { |
---|
2091 | err = og_json_parse_partition_setup(value, params); |
---|
2092 | } |
---|
2093 | |
---|
2094 | if (err < 0) |
---|
2095 | break; |
---|
2096 | } |
---|
2097 | |
---|
2098 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR | |
---|
2099 | OG_REST_PARAM_TYPE | |
---|
2100 | OG_REST_PARAM_DISK | |
---|
2101 | OG_REST_PARAM_CACHE | |
---|
2102 | OG_REST_PARAM_CACHE_SIZE | |
---|
2103 | OG_REST_PARAM_PART_0 | |
---|
2104 | OG_REST_PARAM_PART_1 | |
---|
2105 | OG_REST_PARAM_PART_2 | |
---|
2106 | OG_REST_PARAM_PART_3)) |
---|
2107 | return -1; |
---|
2108 | |
---|
2109 | clients = json_copy(element); |
---|
2110 | json_object_del(clients, "clients"); |
---|
2111 | |
---|
2112 | return og_send_request(OG_METHOD_POST, OG_CMD_SETUP, params, clients); |
---|
2113 | } |
---|
2114 | |
---|
2115 | static int og_cmd_run_schedule(json_t *element, struct og_msg_params *params) |
---|
2116 | { |
---|
2117 | const char *key; |
---|
2118 | json_t *value; |
---|
2119 | int err = 0; |
---|
2120 | |
---|
2121 | json_object_foreach(element, key, value) { |
---|
2122 | if (!strcmp(key, "clients")) |
---|
2123 | err = og_json_parse_clients(value, params); |
---|
2124 | |
---|
2125 | if (err < 0) |
---|
2126 | break; |
---|
2127 | } |
---|
2128 | |
---|
2129 | if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) |
---|
2130 | return -1; |
---|
2131 | |
---|
2132 | return og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, params, |
---|
2133 | NULL); |
---|
2134 | } |
---|
2135 | |
---|
2136 | static LIST_HEAD(cmd_list); |
---|
2137 | |
---|
2138 | const struct og_cmd *og_cmd_find(const char *client_ip) |
---|
2139 | { |
---|
2140 | struct og_cmd *cmd, *next; |
---|
2141 | |
---|
2142 | list_for_each_entry_safe(cmd, next, &cmd_list, list) { |
---|
2143 | if (strcmp(cmd->ip, client_ip)) |
---|
2144 | continue; |
---|
2145 | |
---|
2146 | list_del(&cmd->list); |
---|
2147 | return cmd; |
---|
2148 | } |
---|
2149 | |
---|
2150 | return NULL; |
---|
2151 | } |
---|
2152 | |
---|
2153 | void og_cmd_free(const struct og_cmd *cmd) |
---|
2154 | { |
---|
2155 | struct og_msg_params *params = (struct og_msg_params *)&cmd->params; |
---|
2156 | int i; |
---|
2157 | |
---|
2158 | for (i = 0; i < params->ips_array_len; i++) { |
---|
2159 | free((void *)params->ips_array[i]); |
---|
2160 | free((void *)params->mac_array[i]); |
---|
2161 | } |
---|
2162 | free((void *)params->wol_type); |
---|
2163 | |
---|
2164 | if (cmd->json) |
---|
2165 | json_decref(cmd->json); |
---|
2166 | |
---|
2167 | free((void *)cmd->ip); |
---|
2168 | free((void *)cmd->mac); |
---|
2169 | free((void *)cmd); |
---|
2170 | } |
---|
2171 | |
---|
2172 | static void og_cmd_init(struct og_cmd *cmd, enum og_rest_method method, |
---|
2173 | enum og_cmd_type type, json_t *root) |
---|
2174 | { |
---|
2175 | cmd->type = type; |
---|
2176 | cmd->method = method; |
---|
2177 | cmd->params.ips_array[0] = strdup(cmd->ip); |
---|
2178 | cmd->params.ips_array_len = 1; |
---|
2179 | cmd->json = root; |
---|
2180 | } |
---|
2181 | |
---|
2182 | static int og_cmd_legacy_wol(const char *input, struct og_cmd *cmd) |
---|
2183 | { |
---|
2184 | char wol_type[2] = {}; |
---|
2185 | |
---|
2186 | if (sscanf(input, "mar=%s", wol_type) != 1) { |
---|
2187 | syslog(LOG_ERR, "malformed database legacy input\n"); |
---|
2188 | return -1; |
---|
2189 | } |
---|
2190 | |
---|
2191 | og_cmd_init(cmd, OG_METHOD_NO_HTTP, OG_CMD_WOL, NULL); |
---|
2192 | cmd->params.mac_array[0] = strdup(cmd->mac); |
---|
2193 | cmd->params.wol_type = strdup(wol_type); |
---|
2194 | |
---|
2195 | return 0; |
---|
2196 | } |
---|
2197 | |
---|
2198 | static int og_cmd_legacy_shell_run(const char *input, struct og_cmd *cmd) |
---|
2199 | { |
---|
2200 | json_t *root, *script, *echo; |
---|
2201 | |
---|
2202 | script = json_string(input + 4); |
---|
2203 | echo = json_boolean(false); |
---|
2204 | |
---|
2205 | root = json_object(); |
---|
2206 | if (!root) |
---|
2207 | return -1; |
---|
2208 | json_object_set_new(root, "run", script); |
---|
2209 | json_object_set_new(root, "echo", echo); |
---|
2210 | |
---|
2211 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_SHELL_RUN, root); |
---|
2212 | |
---|
2213 | return 0; |
---|
2214 | } |
---|
2215 | |
---|
2216 | static int og_cmd_legacy_session(const char *input, struct og_cmd *cmd) |
---|
2217 | { |
---|
2218 | char part_str[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
2219 | char disk_str[OG_DB_SMALLINT_MAXLEN + 1]; |
---|
2220 | json_t *root, *disk, *partition; |
---|
2221 | |
---|
2222 | if (sscanf(input, "dsk=%s\rpar=%s\r", disk_str, part_str) != 2) |
---|
2223 | return -1; |
---|
2224 | partition = json_string(part_str); |
---|
2225 | disk = json_string(disk_str); |
---|
2226 | |
---|
2227 | root = json_object(); |
---|
2228 | if (!root) |
---|
2229 | return -1; |
---|
2230 | json_object_set_new(root, "partition", partition); |
---|
2231 | json_object_set_new(root, "disk", disk); |
---|
2232 | |
---|
2233 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_SESSION, root); |
---|
2234 | |
---|
2235 | return 0; |
---|
2236 | } |
---|
2237 | |
---|
2238 | static int og_cmd_legacy_poweroff(const char *input, struct og_cmd *cmd) |
---|
2239 | { |
---|
2240 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_POWEROFF, NULL); |
---|
2241 | |
---|
2242 | return 0; |
---|
2243 | } |
---|
2244 | |
---|
2245 | static int og_cmd_legacy_refresh(const char *input, struct og_cmd *cmd) |
---|
2246 | { |
---|
2247 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_REFRESH, NULL); |
---|
2248 | |
---|
2249 | return 0; |
---|
2250 | } |
---|
2251 | |
---|
2252 | static int og_cmd_legacy_reboot(const char *input, struct og_cmd *cmd) |
---|
2253 | { |
---|
2254 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_REBOOT, NULL); |
---|
2255 | |
---|
2256 | return 0; |
---|
2257 | } |
---|
2258 | |
---|
2259 | static int og_cmd_legacy_stop(const char *input, struct og_cmd *cmd) |
---|
2260 | { |
---|
2261 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_STOP, NULL); |
---|
2262 | |
---|
2263 | return 0; |
---|
2264 | } |
---|
2265 | |
---|
2266 | static int og_cmd_legacy_hardware(const char *input, struct og_cmd *cmd) |
---|
2267 | { |
---|
2268 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_HARDWARE, NULL); |
---|
2269 | |
---|
2270 | return 0; |
---|
2271 | } |
---|
2272 | |
---|
2273 | static int og_cmd_legacy_software(const char *input, struct og_cmd *cmd) |
---|
2274 | { |
---|
2275 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_SOFTWARE, NULL); |
---|
2276 | |
---|
2277 | return 0; |
---|
2278 | } |
---|
2279 | |
---|
2280 | static int og_cmd_legacy_image_create(const char *input, struct og_cmd *cmd) |
---|
2281 | { |
---|
2282 | json_t *root, *disk, *partition, *code, *image_id, *name, *repo; |
---|
2283 | struct og_image_legacy img = {}; |
---|
2284 | |
---|
2285 | if (sscanf(input, "dsk=%s\rpar=%s\rcpt=%s\ridi=%s\rnci=%s\ripr=%s\r", |
---|
2286 | img.disk, img.part, img.code, img.image_id, img.name, |
---|
2287 | img.repo) != 6) |
---|
2288 | return -1; |
---|
2289 | image_id = json_string(img.image_id); |
---|
2290 | partition = json_string(img.part); |
---|
2291 | code = json_string(img.code); |
---|
2292 | name = json_string(img.name); |
---|
2293 | repo = json_string(img.repo); |
---|
2294 | disk = json_string(img.disk); |
---|
2295 | |
---|
2296 | root = json_object(); |
---|
2297 | if (!root) |
---|
2298 | return -1; |
---|
2299 | json_object_set_new(root, "partition", partition); |
---|
2300 | json_object_set_new(root, "repository", repo); |
---|
2301 | json_object_set_new(root, "id", image_id); |
---|
2302 | json_object_set_new(root, "code", code); |
---|
2303 | json_object_set_new(root, "name", name); |
---|
2304 | json_object_set_new(root, "disk", disk); |
---|
2305 | |
---|
2306 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_IMAGE_CREATE, root); |
---|
2307 | |
---|
2308 | return 0; |
---|
2309 | } |
---|
2310 | |
---|
2311 | #define OG_DB_RESTORE_TYPE_MAXLEN 64 |
---|
2312 | |
---|
2313 | static int og_cmd_legacy_image_restore(const char *input, struct og_cmd *cmd) |
---|
2314 | { |
---|
2315 | json_t *root, *disk, *partition, *image_id, *name, *repo; |
---|
2316 | char restore_type_str[OG_DB_RESTORE_TYPE_MAXLEN + 1] = {}; |
---|
2317 | char software_id_str[OG_DB_INT_MAXLEN + 1] = {}; |
---|
2318 | json_t *software_id, *restore_type; |
---|
2319 | struct og_image_legacy img = {}; |
---|
2320 | |
---|
2321 | if (sscanf(input, |
---|
2322 | "dsk=%s\rpar=%s\ridi=%s\rnci=%s\ripr=%s\rifs=%s\rptc=%s\r", |
---|
2323 | img.disk, img.part, img.image_id, img.name, img.repo, |
---|
2324 | software_id_str, restore_type_str) != 7) |
---|
2325 | return -1; |
---|
2326 | |
---|
2327 | restore_type = json_string(restore_type_str); |
---|
2328 | software_id = json_string(software_id_str); |
---|
2329 | image_id = json_string(img.image_id); |
---|
2330 | partition = json_string(img.part); |
---|
2331 | name = json_string(img.name); |
---|
2332 | repo = json_string(img.repo); |
---|
2333 | disk = json_string(img.disk); |
---|
2334 | |
---|
2335 | root = json_object(); |
---|
2336 | if (!root) |
---|
2337 | return -1; |
---|
2338 | json_object_set_new(root, "profile", software_id); |
---|
2339 | json_object_set_new(root, "partition", partition); |
---|
2340 | json_object_set_new(root, "type", restore_type); |
---|
2341 | json_object_set_new(root, "repository", repo); |
---|
2342 | json_object_set_new(root, "id", image_id); |
---|
2343 | json_object_set_new(root, "name", name); |
---|
2344 | json_object_set_new(root, "disk", disk); |
---|
2345 | |
---|
2346 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_IMAGE_RESTORE, root); |
---|
2347 | |
---|
2348 | return 0; |
---|
2349 | } |
---|
2350 | |
---|
2351 | static int og_cmd_legacy_setup(const char *input, struct og_cmd *cmd) |
---|
2352 | { |
---|
2353 | json_t *root, *disk, *cache, *cache_size, *partition_setup, *object; |
---|
2354 | struct og_legacy_partition part_cfg[OG_PARTITION_MAX] = {}; |
---|
2355 | char cache_size_str [OG_DB_INT_MAXLEN + 1]; |
---|
2356 | char disk_str [OG_DB_SMALLINT_MAXLEN + 1]; |
---|
2357 | json_t *part, *code, *fs, *size, *format; |
---|
2358 | unsigned int partition_len = 0; |
---|
2359 | const char *in_ptr; |
---|
2360 | char cache_str[2]; |
---|
2361 | |
---|
2362 | if (sscanf(input, "dsk=%s\rcfg=dis=%*[^*]*che=%[^*]*tch=%[^!]!", |
---|
2363 | disk_str, cache_str, cache_size_str) != 3) |
---|
2364 | return -1; |
---|
2365 | |
---|
2366 | in_ptr = strstr(input, "!") + 1; |
---|
2367 | while (strlen(in_ptr) > 0) { |
---|
2368 | if(sscanf(in_ptr, |
---|
2369 | "par=%[^*]*cpt=%[^*]*sfi=%[^*]*tam=%[^*]*ope=%[^%%]%%", |
---|
2370 | part_cfg[partition_len].partition, |
---|
2371 | part_cfg[partition_len].code, |
---|
2372 | part_cfg[partition_len].filesystem, |
---|
2373 | part_cfg[partition_len].size, |
---|
2374 | part_cfg[partition_len].format) != 5) |
---|
2375 | return -1; |
---|
2376 | in_ptr = strstr(in_ptr, "%") + 1; |
---|
2377 | partition_len++; |
---|
2378 | } |
---|
2379 | |
---|
2380 | root = json_object(); |
---|
2381 | if (!root) |
---|
2382 | return -1; |
---|
2383 | |
---|
2384 | cache_size = json_string(cache_size_str); |
---|
2385 | cache = json_string(cache_str); |
---|
2386 | partition_setup = json_array(); |
---|
2387 | disk = json_string(disk_str); |
---|
2388 | |
---|
2389 | for (unsigned int i = 0; i < partition_len; ++i) { |
---|
2390 | object = json_object(); |
---|
2391 | if (!object) { |
---|
2392 | json_decref(root); |
---|
2393 | return -1; |
---|
2394 | } |
---|
2395 | |
---|
2396 | part = json_string(part_cfg[i].partition); |
---|
2397 | fs = json_string(part_cfg[i].filesystem); |
---|
2398 | format = json_string(part_cfg[i].format); |
---|
2399 | code = json_string(part_cfg[i].code); |
---|
2400 | size = json_string(part_cfg[i].size); |
---|
2401 | |
---|
2402 | json_object_set_new(object, "partition", part); |
---|
2403 | json_object_set_new(object, "filesystem", fs); |
---|
2404 | json_object_set_new(object, "format", format); |
---|
2405 | json_object_set_new(object, "code", code); |
---|
2406 | json_object_set_new(object, "size", size); |
---|
2407 | |
---|
2408 | json_array_append_new(partition_setup, object); |
---|
2409 | } |
---|
2410 | |
---|
2411 | json_object_set_new(root, "partition_setup", partition_setup); |
---|
2412 | json_object_set_new(root, "cache_size", cache_size); |
---|
2413 | json_object_set_new(root, "cache", cache); |
---|
2414 | json_object_set_new(root, "disk", disk); |
---|
2415 | |
---|
2416 | og_cmd_init(cmd, OG_METHOD_POST, OG_CMD_SETUP, root); |
---|
2417 | |
---|
2418 | return 0; |
---|
2419 | } |
---|
2420 | |
---|
2421 | static int og_cmd_legacy_run_schedule(const char *input, struct og_cmd *cmd) |
---|
2422 | { |
---|
2423 | og_cmd_init(cmd, OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, NULL); |
---|
2424 | |
---|
2425 | return 0; |
---|
2426 | } |
---|
2427 | |
---|
2428 | static int og_cmd_legacy(const char *input, struct og_cmd *cmd) |
---|
2429 | { |
---|
2430 | char legacy_cmd[32] = {}; |
---|
2431 | int err = -1; |
---|
2432 | |
---|
2433 | if (sscanf(input, "nfn=%31s\r", legacy_cmd) != 1) { |
---|
2434 | syslog(LOG_ERR, "malformed database legacy input\n"); |
---|
2435 | return -1; |
---|
2436 | } |
---|
2437 | input = strchr(input, '\r') + 1; |
---|
2438 | |
---|
2439 | if (!strcmp(legacy_cmd, "Arrancar")) { |
---|
2440 | err = og_cmd_legacy_wol(input, cmd); |
---|
2441 | } else if (!strcmp(legacy_cmd, "EjecutarScript")) { |
---|
2442 | err = og_cmd_legacy_shell_run(input, cmd); |
---|
2443 | } else if (!strcmp(legacy_cmd, "IniciarSesion")) { |
---|
2444 | err = og_cmd_legacy_session(input, cmd); |
---|
2445 | } else if (!strcmp(legacy_cmd, "Apagar")) { |
---|
2446 | err = og_cmd_legacy_poweroff(input, cmd); |
---|
2447 | } else if (!strcmp(legacy_cmd, "Actualizar")) { |
---|
2448 | err = og_cmd_legacy_refresh(input, cmd); |
---|
2449 | } else if (!strcmp(legacy_cmd, "Reiniciar")) { |
---|
2450 | err = og_cmd_legacy_reboot(input, cmd); |
---|
2451 | } else if (!strcmp(legacy_cmd, "Purgar")) { |
---|
2452 | err = og_cmd_legacy_stop(input, cmd); |
---|
2453 | } else if (!strcmp(legacy_cmd, "InventarioHardware")) { |
---|
2454 | err = og_cmd_legacy_hardware(input, cmd); |
---|
2455 | } else if (!strcmp(legacy_cmd, "InventarioSoftware")) { |
---|
2456 | err = og_cmd_legacy_software(input, cmd); |
---|
2457 | } else if (!strcmp(legacy_cmd, "CrearImagen")) { |
---|
2458 | err = og_cmd_legacy_image_create(input, cmd); |
---|
2459 | } else if (!strcmp(legacy_cmd, "RestaurarImagen")) { |
---|
2460 | err = og_cmd_legacy_image_restore(input, cmd); |
---|
2461 | } else if (!strcmp(legacy_cmd, "Configurar")) { |
---|
2462 | err = og_cmd_legacy_setup(input, cmd); |
---|
2463 | } else if (!strcmp(legacy_cmd, "EjecutaComandosPendientes") || |
---|
2464 | !strcmp(legacy_cmd, "Actualizar")) { |
---|
2465 | err = og_cmd_legacy_run_schedule(input, cmd); |
---|
2466 | } |
---|
2467 | |
---|
2468 | return err; |
---|
2469 | } |
---|
2470 | |
---|
2471 | static int og_dbi_add_action(const struct og_dbi *dbi, const struct og_task *task, |
---|
2472 | struct og_cmd *cmd) |
---|
2473 | { |
---|
2474 | char start_date_string[24]; |
---|
2475 | struct tm *start_date; |
---|
2476 | const char *msglog; |
---|
2477 | dbi_result result; |
---|
2478 | time_t now; |
---|
2479 | |
---|
2480 | time(&now); |
---|
2481 | start_date = localtime(&now); |
---|
2482 | |
---|
2483 | sprintf(start_date_string, "%hu/%hhu/%hhu %hhu:%hhu:%hhu", |
---|
2484 | start_date->tm_year + 1900, start_date->tm_mon + 1, |
---|
2485 | start_date->tm_mday, start_date->tm_hour, start_date->tm_min, |
---|
2486 | start_date->tm_sec); |
---|
2487 | result = dbi_conn_queryf(dbi->conn, |
---|
2488 | "INSERT INTO acciones (idordenador, " |
---|
2489 | "tipoaccion, idtipoaccion, descriaccion, ip, " |
---|
2490 | "sesion, idcomando, parametros, fechahorareg, " |
---|
2491 | "estado, resultado, ambito, idambito, " |
---|
2492 | "restrambito, idprocedimiento, idcentro, " |
---|
2493 | "idprogramacion) " |
---|
2494 | "VALUES (%d, %d, %d, '%s', '%s', %d, %d, '%s', " |
---|
2495 | "'%s', %d, %d, %d, %d, '%s', %d, %d, %d)", |
---|
2496 | cmd->client_id, EJECUCION_TAREA, task->task_id, |
---|
2497 | "", cmd->ip, 0, task->command_id, |
---|
2498 | task->params, start_date_string, |
---|
2499 | ACCION_INICIADA, ACCION_SINRESULTADO, |
---|
2500 | task->type_scope, task->scope, "", |
---|
2501 | task->procedure_id, task->center_id, |
---|
2502 | task->schedule_id); |
---|
2503 | if (!result) { |
---|
2504 | dbi_conn_error(dbi->conn, &msglog); |
---|
2505 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2506 | __func__, __LINE__, msglog); |
---|
2507 | return -1; |
---|
2508 | } |
---|
2509 | cmd->id = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
2510 | dbi_result_free(result); |
---|
2511 | |
---|
2512 | return 0; |
---|
2513 | } |
---|
2514 | |
---|
2515 | static int og_queue_task_command(struct og_dbi *dbi, const struct og_task *task, |
---|
2516 | char *query) |
---|
2517 | { |
---|
2518 | struct og_cmd *cmd; |
---|
2519 | const char *msglog; |
---|
2520 | dbi_result result; |
---|
2521 | |
---|
2522 | result = dbi_conn_queryf(dbi->conn, query); |
---|
2523 | if (!result) { |
---|
2524 | dbi_conn_error(dbi->conn, &msglog); |
---|
2525 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2526 | __func__, __LINE__, msglog); |
---|
2527 | return -1; |
---|
2528 | } |
---|
2529 | |
---|
2530 | while (dbi_result_next_row(result)) { |
---|
2531 | cmd = (struct og_cmd *)calloc(1, sizeof(struct og_cmd)); |
---|
2532 | if (!cmd) { |
---|
2533 | dbi_result_free(result); |
---|
2534 | return -1; |
---|
2535 | } |
---|
2536 | |
---|
2537 | cmd->client_id = dbi_result_get_uint(result, "idordenador"); |
---|
2538 | cmd->ip = strdup(dbi_result_get_string(result, "ip")); |
---|
2539 | cmd->mac = strdup(dbi_result_get_string(result, "mac")); |
---|
2540 | |
---|
2541 | og_cmd_legacy(task->params, cmd); |
---|
2542 | |
---|
2543 | if (task->procedure_id) { |
---|
2544 | if (og_dbi_add_action(dbi, task, cmd)) { |
---|
2545 | dbi_result_free(result); |
---|
2546 | return -1; |
---|
2547 | } |
---|
2548 | } else { |
---|
2549 | cmd->id = task->task_id; |
---|
2550 | } |
---|
2551 | |
---|
2552 | list_add_tail(&cmd->list, &cmd_list); |
---|
2553 | } |
---|
2554 | |
---|
2555 | dbi_result_free(result); |
---|
2556 | |
---|
2557 | return 0; |
---|
2558 | } |
---|
2559 | |
---|
2560 | static int og_queue_task_group_clients(struct og_dbi *dbi, struct og_task *task, |
---|
2561 | char *query) |
---|
2562 | { |
---|
2563 | |
---|
2564 | const char *msglog; |
---|
2565 | dbi_result result; |
---|
2566 | |
---|
2567 | result = dbi_conn_queryf(dbi->conn, query); |
---|
2568 | if (!result) { |
---|
2569 | dbi_conn_error(dbi->conn, &msglog); |
---|
2570 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2571 | __func__, __LINE__, msglog); |
---|
2572 | return -1; |
---|
2573 | } |
---|
2574 | |
---|
2575 | while (dbi_result_next_row(result)) { |
---|
2576 | uint32_t group_id = dbi_result_get_uint(result, "idgrupo"); |
---|
2577 | |
---|
2578 | sprintf(query, "SELECT idgrupo FROM gruposordenadores " |
---|
2579 | "WHERE grupoid=%d", group_id); |
---|
2580 | if (og_queue_task_group_clients(dbi, task, query)) { |
---|
2581 | dbi_result_free(result); |
---|
2582 | return -1; |
---|
2583 | } |
---|
2584 | |
---|
2585 | sprintf(query,"SELECT ip, mac, idordenador FROM ordenadores " |
---|
2586 | "WHERE grupoid=%d", group_id); |
---|
2587 | if (og_queue_task_command(dbi, task, query)) { |
---|
2588 | dbi_result_free(result); |
---|
2589 | return -1; |
---|
2590 | } |
---|
2591 | |
---|
2592 | } |
---|
2593 | |
---|
2594 | dbi_result_free(result); |
---|
2595 | |
---|
2596 | return 0; |
---|
2597 | } |
---|
2598 | |
---|
2599 | static int og_queue_task_group_classrooms(struct og_dbi *dbi, |
---|
2600 | struct og_task *task, char *query) |
---|
2601 | { |
---|
2602 | |
---|
2603 | const char *msglog; |
---|
2604 | dbi_result result; |
---|
2605 | |
---|
2606 | result = dbi_conn_queryf(dbi->conn, query); |
---|
2607 | if (!result) { |
---|
2608 | dbi_conn_error(dbi->conn, &msglog); |
---|
2609 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2610 | __func__, __LINE__, msglog); |
---|
2611 | return -1; |
---|
2612 | } |
---|
2613 | |
---|
2614 | while (dbi_result_next_row(result)) { |
---|
2615 | uint32_t group_id = dbi_result_get_uint(result, "idgrupo"); |
---|
2616 | |
---|
2617 | sprintf(query, "SELECT idgrupo FROM grupos " |
---|
2618 | "WHERE grupoid=%d AND tipo=%d", group_id, AMBITO_GRUPOSAULAS); |
---|
2619 | if (og_queue_task_group_classrooms(dbi, task, query)) { |
---|
2620 | dbi_result_free(result); |
---|
2621 | return -1; |
---|
2622 | } |
---|
2623 | |
---|
2624 | sprintf(query, |
---|
2625 | "SELECT ip,mac,idordenador " |
---|
2626 | "FROM ordenadores INNER JOIN aulas " |
---|
2627 | "WHERE ordenadores.idaula=aulas.idaula " |
---|
2628 | "AND aulas.grupoid=%d", |
---|
2629 | group_id); |
---|
2630 | if (og_queue_task_command(dbi, task, query)) { |
---|
2631 | dbi_result_free(result); |
---|
2632 | return -1; |
---|
2633 | } |
---|
2634 | |
---|
2635 | } |
---|
2636 | |
---|
2637 | dbi_result_free(result); |
---|
2638 | |
---|
2639 | return 0; |
---|
2640 | } |
---|
2641 | |
---|
2642 | static int og_queue_task_clients(struct og_dbi *dbi, struct og_task *task) |
---|
2643 | { |
---|
2644 | char query[4096]; |
---|
2645 | |
---|
2646 | switch (task->type_scope) { |
---|
2647 | case AMBITO_CENTROS: |
---|
2648 | sprintf(query, |
---|
2649 | "SELECT ip,mac,idordenador " |
---|
2650 | "FROM ordenadores INNER JOIN aulas " |
---|
2651 | "WHERE ordenadores.idaula=aulas.idaula " |
---|
2652 | "AND idcentro=%d", |
---|
2653 | task->scope); |
---|
2654 | return og_queue_task_command(dbi, task, query); |
---|
2655 | case AMBITO_GRUPOSAULAS: |
---|
2656 | sprintf(query, |
---|
2657 | "SELECT idgrupo FROM grupos " |
---|
2658 | "WHERE idgrupo=%i AND tipo=%d", |
---|
2659 | task->scope, AMBITO_GRUPOSAULAS); |
---|
2660 | return og_queue_task_group_classrooms(dbi, task, query); |
---|
2661 | case AMBITO_AULAS: |
---|
2662 | sprintf(query, |
---|
2663 | "SELECT ip,mac,idordenador FROM ordenadores " |
---|
2664 | "WHERE idaula=%d", |
---|
2665 | task->scope); |
---|
2666 | return og_queue_task_command(dbi, task, query); |
---|
2667 | case AMBITO_GRUPOSORDENADORES: |
---|
2668 | sprintf(query, |
---|
2669 | "SELECT idgrupo FROM gruposordenadores " |
---|
2670 | "WHERE idgrupo = %d", |
---|
2671 | task->scope); |
---|
2672 | return og_queue_task_group_clients(dbi, task, query); |
---|
2673 | case AMBITO_ORDENADORES: |
---|
2674 | sprintf(query, |
---|
2675 | "SELECT ip, mac, idordenador FROM ordenadores " |
---|
2676 | "WHERE idordenador = %d", |
---|
2677 | task->scope); |
---|
2678 | return og_queue_task_command(dbi, task, query); |
---|
2679 | } |
---|
2680 | return 0; |
---|
2681 | } |
---|
2682 | |
---|
2683 | int og_dbi_queue_procedure(struct og_dbi *dbi, struct og_task *task) |
---|
2684 | { |
---|
2685 | uint32_t procedure_id; |
---|
2686 | const char *msglog; |
---|
2687 | dbi_result result; |
---|
2688 | |
---|
2689 | result = dbi_conn_queryf(dbi->conn, |
---|
2690 | "SELECT parametros, procedimientoid, idcomando " |
---|
2691 | "FROM procedimientos_acciones " |
---|
2692 | "WHERE idprocedimiento=%d ORDER BY orden", task->procedure_id); |
---|
2693 | if (!result) { |
---|
2694 | dbi_conn_error(dbi->conn, &msglog); |
---|
2695 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2696 | __func__, __LINE__, msglog); |
---|
2697 | return -1; |
---|
2698 | } |
---|
2699 | |
---|
2700 | while (dbi_result_next_row(result)) { |
---|
2701 | procedure_id = dbi_result_get_uint(result, "procedimientoid"); |
---|
2702 | if (procedure_id > 0) { |
---|
2703 | task->procedure_id = procedure_id; |
---|
2704 | if (og_dbi_queue_procedure(dbi, task)) |
---|
2705 | return -1; |
---|
2706 | continue; |
---|
2707 | } |
---|
2708 | |
---|
2709 | task->params = strdup(dbi_result_get_string(result, "parametros")); |
---|
2710 | task->command_id = dbi_result_get_uint(result, "idcomando"); |
---|
2711 | if (og_queue_task_clients(dbi, task)) |
---|
2712 | return -1; |
---|
2713 | } |
---|
2714 | |
---|
2715 | dbi_result_free(result); |
---|
2716 | |
---|
2717 | return 0; |
---|
2718 | } |
---|
2719 | |
---|
2720 | static int og_dbi_queue_task(struct og_dbi *dbi, uint32_t task_id, |
---|
2721 | uint32_t schedule_id) |
---|
2722 | { |
---|
2723 | struct og_task task = {}; |
---|
2724 | uint32_t task_id_next; |
---|
2725 | const char *msglog; |
---|
2726 | dbi_result result; |
---|
2727 | |
---|
2728 | task.schedule_id = schedule_id; |
---|
2729 | |
---|
2730 | result = dbi_conn_queryf(dbi->conn, |
---|
2731 | "SELECT tareas_acciones.orden, " |
---|
2732 | "tareas_acciones.idprocedimiento, " |
---|
2733 | "tareas_acciones.tareaid, " |
---|
2734 | "tareas.idtarea, " |
---|
2735 | "tareas.idcentro, " |
---|
2736 | "tareas.ambito, " |
---|
2737 | "tareas.idambito, " |
---|
2738 | "tareas.restrambito " |
---|
2739 | " FROM tareas" |
---|
2740 | " INNER JOIN tareas_acciones ON tareas_acciones.idtarea=tareas.idtarea" |
---|
2741 | " WHERE tareas_acciones.idtarea=%u ORDER BY tareas_acciones.orden ASC", task_id); |
---|
2742 | if (!result) { |
---|
2743 | dbi_conn_error(dbi->conn, &msglog); |
---|
2744 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2745 | __func__, __LINE__, msglog); |
---|
2746 | return -1; |
---|
2747 | } |
---|
2748 | |
---|
2749 | while (dbi_result_next_row(result)) { |
---|
2750 | task_id_next = dbi_result_get_uint(result, "tareaid"); |
---|
2751 | |
---|
2752 | if (task_id_next > 0) { |
---|
2753 | if (og_dbi_queue_task(dbi, task_id_next, schedule_id)) |
---|
2754 | return -1; |
---|
2755 | |
---|
2756 | continue; |
---|
2757 | } |
---|
2758 | task.task_id = dbi_result_get_uint(result, "idtarea"); |
---|
2759 | task.center_id = dbi_result_get_uint(result, "idcentro"); |
---|
2760 | task.procedure_id = dbi_result_get_uint(result, "idprocedimiento"); |
---|
2761 | task.type_scope = dbi_result_get_uint(result, "ambito"); |
---|
2762 | task.scope = dbi_result_get_uint(result, "idambito"); |
---|
2763 | task.filtered_scope = dbi_result_get_string(result, "restrambito"); |
---|
2764 | |
---|
2765 | og_dbi_queue_procedure(dbi, &task); |
---|
2766 | } |
---|
2767 | |
---|
2768 | dbi_result_free(result); |
---|
2769 | |
---|
2770 | return 0; |
---|
2771 | } |
---|
2772 | |
---|
2773 | static int og_dbi_queue_command(struct og_dbi *dbi, uint32_t task_id, |
---|
2774 | uint32_t schedule_id) |
---|
2775 | { |
---|
2776 | struct og_task task = {}; |
---|
2777 | const char *msglog; |
---|
2778 | dbi_result result; |
---|
2779 | char query[4096]; |
---|
2780 | |
---|
2781 | result = dbi_conn_queryf(dbi->conn, |
---|
2782 | "SELECT idaccion, idcentro, idordenador, parametros " |
---|
2783 | "FROM acciones " |
---|
2784 | "WHERE sesion = %u", task_id); |
---|
2785 | if (!result) { |
---|
2786 | dbi_conn_error(dbi->conn, &msglog); |
---|
2787 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2788 | __func__, __LINE__, msglog); |
---|
2789 | return -1; |
---|
2790 | } |
---|
2791 | |
---|
2792 | while (dbi_result_next_row(result)) { |
---|
2793 | task.task_id = dbi_result_get_uint(result, "idaccion"); |
---|
2794 | task.center_id = dbi_result_get_uint(result, "idcentro"); |
---|
2795 | task.scope = dbi_result_get_uint(result, "idordenador"); |
---|
2796 | task.params = strdup(dbi_result_get_string(result, "parametros")); |
---|
2797 | |
---|
2798 | sprintf(query, |
---|
2799 | "SELECT ip, mac, idordenador FROM ordenadores " |
---|
2800 | "WHERE idordenador = %d", |
---|
2801 | task.scope); |
---|
2802 | if (og_queue_task_command(dbi, &task, query)) { |
---|
2803 | dbi_result_free(result); |
---|
2804 | return -1; |
---|
2805 | } |
---|
2806 | } |
---|
2807 | |
---|
2808 | dbi_result_free(result); |
---|
2809 | |
---|
2810 | return 0; |
---|
2811 | } |
---|
2812 | |
---|
2813 | int og_dbi_update_action(uint32_t id, bool success) |
---|
2814 | { |
---|
2815 | char end_date_string[24]; |
---|
2816 | struct tm *end_date; |
---|
2817 | const char *msglog; |
---|
2818 | struct og_dbi *dbi; |
---|
2819 | uint8_t status = 2; |
---|
2820 | dbi_result result; |
---|
2821 | time_t now; |
---|
2822 | |
---|
2823 | if (!id) |
---|
2824 | return 0; |
---|
2825 | |
---|
2826 | dbi = og_dbi_open(&ogconfig.db); |
---|
2827 | if (!dbi) { |
---|
2828 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
2829 | __func__, __LINE__); |
---|
2830 | return -1; |
---|
2831 | } |
---|
2832 | |
---|
2833 | time(&now); |
---|
2834 | end_date = localtime(&now); |
---|
2835 | |
---|
2836 | sprintf(end_date_string, "%hu/%hhu/%hhu %hhu:%hhu:%hhu", |
---|
2837 | end_date->tm_year + 1900, end_date->tm_mon + 1, |
---|
2838 | end_date->tm_mday, end_date->tm_hour, end_date->tm_min, |
---|
2839 | end_date->tm_sec); |
---|
2840 | result = dbi_conn_queryf(dbi->conn, |
---|
2841 | "UPDATE acciones SET fechahorafin='%s', " |
---|
2842 | "estado=%d, resultado=%d WHERE idaccion=%d", |
---|
2843 | end_date_string, ACCION_FINALIZADA, |
---|
2844 | status - success, id); |
---|
2845 | |
---|
2846 | if (!result) { |
---|
2847 | dbi_conn_error(dbi->conn, &msglog); |
---|
2848 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2849 | __func__, __LINE__, msglog); |
---|
2850 | og_dbi_close(dbi); |
---|
2851 | return -1; |
---|
2852 | } |
---|
2853 | dbi_result_free(result); |
---|
2854 | og_dbi_close(dbi); |
---|
2855 | |
---|
2856 | return 0; |
---|
2857 | } |
---|
2858 | |
---|
2859 | void og_schedule_run(unsigned int task_id, unsigned int schedule_id, |
---|
2860 | enum og_schedule_type type) |
---|
2861 | { |
---|
2862 | struct og_msg_params params = {}; |
---|
2863 | bool duplicated = false; |
---|
2864 | struct og_cmd *cmd, *next; |
---|
2865 | struct og_dbi *dbi; |
---|
2866 | unsigned int i; |
---|
2867 | |
---|
2868 | dbi = og_dbi_open(&ogconfig.db); |
---|
2869 | if (!dbi) { |
---|
2870 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
2871 | __func__, __LINE__); |
---|
2872 | return; |
---|
2873 | } |
---|
2874 | |
---|
2875 | switch (type) { |
---|
2876 | case OG_SCHEDULE_TASK: |
---|
2877 | og_dbi_queue_task(dbi, task_id, schedule_id); |
---|
2878 | break; |
---|
2879 | case OG_SCHEDULE_PROCEDURE: |
---|
2880 | case OG_SCHEDULE_COMMAND: |
---|
2881 | og_dbi_queue_command(dbi, task_id, schedule_id); |
---|
2882 | break; |
---|
2883 | } |
---|
2884 | og_dbi_close(dbi); |
---|
2885 | |
---|
2886 | list_for_each_entry(cmd, &cmd_list, list) { |
---|
2887 | for (i = 0; i < params.ips_array_len; i++) { |
---|
2888 | if (!strncmp(cmd->ip, params.ips_array[i], |
---|
2889 | OG_DB_IP_MAXLEN)) { |
---|
2890 | duplicated = true; |
---|
2891 | break; |
---|
2892 | } |
---|
2893 | } |
---|
2894 | |
---|
2895 | if (!duplicated) |
---|
2896 | params.ips_array[params.ips_array_len++] = cmd->ip; |
---|
2897 | else |
---|
2898 | duplicated = false; |
---|
2899 | } |
---|
2900 | |
---|
2901 | list_for_each_entry_safe(cmd, next, &cmd_list, list) { |
---|
2902 | if (cmd->type != OG_CMD_WOL) |
---|
2903 | continue; |
---|
2904 | |
---|
2905 | if (Levanta((char **)cmd->params.ips_array, |
---|
2906 | (char **)cmd->params.mac_array, |
---|
2907 | (char **)cmd->params.netmask_array, |
---|
2908 | cmd->params.ips_array_len, |
---|
2909 | (char *)cmd->params.wol_type)) |
---|
2910 | og_dbi_update_action(cmd->id, true); |
---|
2911 | |
---|
2912 | list_del(&cmd->list); |
---|
2913 | og_cmd_free(cmd); |
---|
2914 | } |
---|
2915 | |
---|
2916 | og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, ¶ms, NULL); |
---|
2917 | } |
---|
2918 | |
---|
2919 | static int og_cmd_task_post(json_t *element, struct og_msg_params *params) |
---|
2920 | { |
---|
2921 | struct og_cmd *cmd; |
---|
2922 | struct og_dbi *dbi; |
---|
2923 | const char *key; |
---|
2924 | json_t *value; |
---|
2925 | int err; |
---|
2926 | |
---|
2927 | if (json_typeof(element) != JSON_OBJECT) |
---|
2928 | return -1; |
---|
2929 | |
---|
2930 | json_object_foreach(element, key, value) { |
---|
2931 | if (!strcmp(key, "task")) { |
---|
2932 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
2933 | params->flags |= OG_REST_PARAM_TASK; |
---|
2934 | } |
---|
2935 | |
---|
2936 | if (err < 0) |
---|
2937 | break; |
---|
2938 | } |
---|
2939 | |
---|
2940 | if (!og_msg_params_validate(params, OG_REST_PARAM_TASK)) |
---|
2941 | return -1; |
---|
2942 | |
---|
2943 | dbi = og_dbi_open(&ogconfig.db); |
---|
2944 | if (!dbi) { |
---|
2945 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
2946 | __func__, __LINE__); |
---|
2947 | return -1; |
---|
2948 | } |
---|
2949 | |
---|
2950 | og_schedule_run(atoi(params->task_id), 0, OG_SCHEDULE_TASK); |
---|
2951 | og_dbi_close(dbi); |
---|
2952 | |
---|
2953 | list_for_each_entry(cmd, &cmd_list, list) |
---|
2954 | params->ips_array[params->ips_array_len++] = cmd->ip; |
---|
2955 | |
---|
2956 | return og_send_request(OG_METHOD_GET, OG_CMD_RUN_SCHEDULE, params, |
---|
2957 | NULL); |
---|
2958 | } |
---|
2959 | |
---|
2960 | static int og_dbi_scope_get_computer(struct og_dbi *dbi, json_t *array, |
---|
2961 | uint32_t room_id) |
---|
2962 | { |
---|
2963 | const char *computer_name, *computer_ip; |
---|
2964 | uint32_t computer_id; |
---|
2965 | const char *msglog; |
---|
2966 | dbi_result result; |
---|
2967 | json_t *computer; |
---|
2968 | |
---|
2969 | result = dbi_conn_queryf(dbi->conn, |
---|
2970 | "SELECT idordenador, nombreordenador, ip " |
---|
2971 | "FROM ordenadores WHERE idaula=%d", |
---|
2972 | room_id); |
---|
2973 | if (!result) { |
---|
2974 | dbi_conn_error(dbi->conn, &msglog); |
---|
2975 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
2976 | __func__, __LINE__, msglog); |
---|
2977 | return -1; |
---|
2978 | } |
---|
2979 | |
---|
2980 | while (dbi_result_next_row(result)) { |
---|
2981 | computer_id = dbi_result_get_uint(result, "idordenador"); |
---|
2982 | computer_name = dbi_result_get_string(result, "nombreordenador"); |
---|
2983 | computer_ip = dbi_result_get_string(result, "ip"); |
---|
2984 | |
---|
2985 | computer = json_object(); |
---|
2986 | if (!computer) { |
---|
2987 | dbi_result_free(result); |
---|
2988 | return -1; |
---|
2989 | } |
---|
2990 | |
---|
2991 | json_object_set_new(computer, "name", json_string(computer_name)); |
---|
2992 | json_object_set_new(computer, "type", json_string("computer")); |
---|
2993 | json_object_set_new(computer, "id", json_integer(computer_id)); |
---|
2994 | json_object_set_new(computer, "scope", json_array()); |
---|
2995 | json_object_set_new(computer, "ip", json_string(computer_ip)); |
---|
2996 | json_array_append(array, computer); |
---|
2997 | json_decref(computer); |
---|
2998 | } |
---|
2999 | dbi_result_free(result); |
---|
3000 | |
---|
3001 | return 0; |
---|
3002 | } |
---|
3003 | |
---|
3004 | static int og_dbi_scope_get_room(struct og_dbi *dbi, json_t *array, |
---|
3005 | uint32_t center_id) |
---|
3006 | { |
---|
3007 | char room_name[OG_DB_ROOM_NAME_MAXLEN + 1] = {}; |
---|
3008 | json_t *room, *room_array; |
---|
3009 | const char *msglog; |
---|
3010 | dbi_result result; |
---|
3011 | uint32_t room_id; |
---|
3012 | |
---|
3013 | result = dbi_conn_queryf(dbi->conn, |
---|
3014 | "SELECT idaula, nombreaula FROM aulas WHERE " |
---|
3015 | "idcentro=%d", |
---|
3016 | center_id); |
---|
3017 | if (!result) { |
---|
3018 | dbi_conn_error(dbi->conn, &msglog); |
---|
3019 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3020 | __func__, __LINE__, msglog); |
---|
3021 | return -1; |
---|
3022 | } |
---|
3023 | |
---|
3024 | while (dbi_result_next_row(result)) { |
---|
3025 | room_id = dbi_result_get_uint(result, "idaula"); |
---|
3026 | strncpy(room_name, |
---|
3027 | dbi_result_get_string(result, "nombreaula"), |
---|
3028 | OG_DB_CENTER_NAME_MAXLEN); |
---|
3029 | |
---|
3030 | room = json_object(); |
---|
3031 | if (!room) { |
---|
3032 | dbi_result_free(result); |
---|
3033 | return -1; |
---|
3034 | } |
---|
3035 | |
---|
3036 | json_object_set_new(room, "name", json_string(room_name)); |
---|
3037 | json_object_set_new(room, "type", json_string("room")); |
---|
3038 | json_object_set_new(room, "id", json_integer(room_id)); |
---|
3039 | json_object_set_new(room, "scope", json_array()); |
---|
3040 | json_array_append(array, room); |
---|
3041 | json_decref(room); |
---|
3042 | |
---|
3043 | room_array = json_object_get(room, "scope"); |
---|
3044 | if (!room_array) { |
---|
3045 | dbi_result_free(result); |
---|
3046 | return -1; |
---|
3047 | } |
---|
3048 | |
---|
3049 | if (og_dbi_scope_get_computer(dbi, room_array, room_id)) { |
---|
3050 | dbi_result_free(result); |
---|
3051 | return -1; |
---|
3052 | } |
---|
3053 | } |
---|
3054 | dbi_result_free(result); |
---|
3055 | |
---|
3056 | return 0; |
---|
3057 | } |
---|
3058 | |
---|
3059 | static int og_dbi_scope_get(struct og_dbi *dbi, json_t *array) |
---|
3060 | { |
---|
3061 | char center_name[OG_DB_CENTER_NAME_MAXLEN + 1] = {}; |
---|
3062 | json_t *center, *array_room; |
---|
3063 | const char *msglog; |
---|
3064 | uint32_t center_id; |
---|
3065 | dbi_result result; |
---|
3066 | |
---|
3067 | result = dbi_conn_queryf(dbi->conn, |
---|
3068 | "SELECT nombrecentro, idcentro FROM centros"); |
---|
3069 | if (!result) { |
---|
3070 | dbi_conn_error(dbi->conn, &msglog); |
---|
3071 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3072 | __func__, __LINE__, msglog); |
---|
3073 | return -1; |
---|
3074 | } |
---|
3075 | |
---|
3076 | while (dbi_result_next_row(result)) { |
---|
3077 | center_id = dbi_result_get_uint(result, "idcentro"); |
---|
3078 | strncpy(center_name, |
---|
3079 | dbi_result_get_string(result, "nombrecentro"), |
---|
3080 | OG_DB_CENTER_NAME_MAXLEN); |
---|
3081 | |
---|
3082 | center = json_object(); |
---|
3083 | if (!center) { |
---|
3084 | dbi_result_free(result); |
---|
3085 | return -1; |
---|
3086 | } |
---|
3087 | |
---|
3088 | array_room = json_array(); |
---|
3089 | if (!array_room) { |
---|
3090 | dbi_result_free(result); |
---|
3091 | json_decref(center); |
---|
3092 | return -1; |
---|
3093 | } |
---|
3094 | |
---|
3095 | json_object_set_new(center, "name", json_string(center_name)); |
---|
3096 | json_object_set_new(center, "type", json_string("center")); |
---|
3097 | json_object_set_new(center, "id", json_integer(center_id)); |
---|
3098 | json_object_set_new(center, "scope", array_room); |
---|
3099 | json_array_append(array, center); |
---|
3100 | json_decref(center); |
---|
3101 | |
---|
3102 | if (og_dbi_scope_get_room(dbi, array_room, center_id)) { |
---|
3103 | dbi_result_free(result); |
---|
3104 | return -1; |
---|
3105 | } |
---|
3106 | } |
---|
3107 | |
---|
3108 | dbi_result_free(result); |
---|
3109 | |
---|
3110 | return 0; |
---|
3111 | } |
---|
3112 | |
---|
3113 | static int og_cmd_scope_get(json_t *element, struct og_msg_params *params, |
---|
3114 | char *buffer_reply) |
---|
3115 | { |
---|
3116 | struct og_buffer og_buffer = { |
---|
3117 | .data = buffer_reply |
---|
3118 | }; |
---|
3119 | json_t *root, *array; |
---|
3120 | struct og_dbi *dbi; |
---|
3121 | |
---|
3122 | root = json_object(); |
---|
3123 | if (!root) |
---|
3124 | return -1; |
---|
3125 | |
---|
3126 | array = json_array(); |
---|
3127 | if (!array) { |
---|
3128 | json_decref(root); |
---|
3129 | return -1; |
---|
3130 | } |
---|
3131 | json_object_set_new(root, "scope", array); |
---|
3132 | |
---|
3133 | dbi = og_dbi_open(&ogconfig.db); |
---|
3134 | if (!dbi) { |
---|
3135 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3136 | __func__, __LINE__); |
---|
3137 | json_decref(root); |
---|
3138 | return -1; |
---|
3139 | } |
---|
3140 | |
---|
3141 | if (og_dbi_scope_get(dbi, array)) { |
---|
3142 | og_dbi_close(dbi); |
---|
3143 | json_decref(root); |
---|
3144 | return -1; |
---|
3145 | } |
---|
3146 | |
---|
3147 | og_dbi_close(dbi); |
---|
3148 | |
---|
3149 | json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); |
---|
3150 | json_decref(root); |
---|
3151 | |
---|
3152 | return 0; |
---|
3153 | } |
---|
3154 | |
---|
3155 | int og_dbi_schedule_get(void) |
---|
3156 | { |
---|
3157 | uint32_t schedule_id, task_id; |
---|
3158 | struct og_schedule_time time; |
---|
3159 | struct og_dbi *dbi; |
---|
3160 | const char *msglog; |
---|
3161 | dbi_result result; |
---|
3162 | |
---|
3163 | dbi = og_dbi_open(&ogconfig.db); |
---|
3164 | if (!dbi) { |
---|
3165 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3166 | __func__, __LINE__); |
---|
3167 | return -1; |
---|
3168 | } |
---|
3169 | |
---|
3170 | result = dbi_conn_queryf(dbi->conn, |
---|
3171 | "SELECT idprogramacion, tipoaccion, identificador, " |
---|
3172 | "sesion, annos, meses, diario, dias, semanas, horas, " |
---|
3173 | "ampm, minutos FROM programaciones " |
---|
3174 | "WHERE suspendida = 0"); |
---|
3175 | if (!result) { |
---|
3176 | dbi_conn_error(dbi->conn, &msglog); |
---|
3177 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3178 | __func__, __LINE__, msglog); |
---|
3179 | og_dbi_close(dbi); |
---|
3180 | return -1; |
---|
3181 | } |
---|
3182 | |
---|
3183 | while (dbi_result_next_row(result)) { |
---|
3184 | memset(&time, 0, sizeof(time)); |
---|
3185 | schedule_id = dbi_result_get_uint(result, "idprogramacion"); |
---|
3186 | task_id = dbi_result_get_uint(result, "identificador"); |
---|
3187 | time.years = dbi_result_get_uint(result, "annos"); |
---|
3188 | time.months = dbi_result_get_uint(result, "meses"); |
---|
3189 | time.weeks = dbi_result_get_uint(result, "semanas"); |
---|
3190 | time.week_days = dbi_result_get_uint(result, "dias"); |
---|
3191 | time.days = dbi_result_get_uint(result, "diario"); |
---|
3192 | time.hours = dbi_result_get_uint(result, "horas"); |
---|
3193 | time.am_pm = dbi_result_get_uint(result, "ampm"); |
---|
3194 | time.minutes = dbi_result_get_uint(result, "minutos"); |
---|
3195 | time.on_start = true; |
---|
3196 | |
---|
3197 | og_schedule_create(schedule_id, task_id, OG_SCHEDULE_TASK, |
---|
3198 | &time); |
---|
3199 | } |
---|
3200 | |
---|
3201 | dbi_result_free(result); |
---|
3202 | og_dbi_close(dbi); |
---|
3203 | |
---|
3204 | return 0; |
---|
3205 | } |
---|
3206 | |
---|
3207 | static int og_dbi_schedule_create(struct og_dbi *dbi, |
---|
3208 | struct og_msg_params *params, |
---|
3209 | uint32_t *schedule_id, |
---|
3210 | enum og_schedule_type schedule_type) |
---|
3211 | { |
---|
3212 | uint8_t suspended = 0; |
---|
3213 | uint32_t session = 0; |
---|
3214 | const char *msglog; |
---|
3215 | dbi_result result; |
---|
3216 | uint8_t type; |
---|
3217 | |
---|
3218 | switch (schedule_type) { |
---|
3219 | case OG_SCHEDULE_TASK: |
---|
3220 | type = 3; |
---|
3221 | break; |
---|
3222 | case OG_SCHEDULE_PROCEDURE: |
---|
3223 | type = 2; |
---|
3224 | break; |
---|
3225 | case OG_SCHEDULE_COMMAND: |
---|
3226 | session = atoi(params->task_id); |
---|
3227 | type = 1; |
---|
3228 | break; |
---|
3229 | } |
---|
3230 | |
---|
3231 | result = dbi_conn_queryf(dbi->conn, |
---|
3232 | "INSERT INTO programaciones (tipoaccion," |
---|
3233 | " identificador, nombrebloque, annos, meses," |
---|
3234 | " semanas, dias, diario, horas, ampm, minutos," |
---|
3235 | " suspendida, sesion) VALUES (%d, %s, '%s'," |
---|
3236 | " %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)", |
---|
3237 | type, params->task_id, params->name, |
---|
3238 | params->time.years, params->time.months, |
---|
3239 | params->time.weeks, params->time.week_days, |
---|
3240 | params->time.days, params->time.hours, |
---|
3241 | params->time.am_pm, params->time.minutes, |
---|
3242 | suspended, session); |
---|
3243 | if (!result) { |
---|
3244 | dbi_conn_error(dbi->conn, &msglog); |
---|
3245 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3246 | __func__, __LINE__, msglog); |
---|
3247 | return -1; |
---|
3248 | } |
---|
3249 | dbi_result_free(result); |
---|
3250 | |
---|
3251 | *schedule_id = dbi_conn_sequence_last(dbi->conn, NULL); |
---|
3252 | |
---|
3253 | return 0; |
---|
3254 | } |
---|
3255 | |
---|
3256 | static int og_dbi_schedule_update(struct og_dbi *dbi, |
---|
3257 | struct og_msg_params *params) |
---|
3258 | { |
---|
3259 | const char *msglog; |
---|
3260 | dbi_result result; |
---|
3261 | uint8_t type = 3; |
---|
3262 | |
---|
3263 | result = dbi_conn_queryf(dbi->conn, |
---|
3264 | "UPDATE programaciones SET tipoaccion=%d, " |
---|
3265 | "identificador='%s', nombrebloque='%s', " |
---|
3266 | "annos=%d, meses=%d, " |
---|
3267 | "diario=%d, horas=%d, ampm=%d, minutos=%d " |
---|
3268 | "WHERE idprogramacion='%s'", |
---|
3269 | type, params->task_id, params->name, |
---|
3270 | params->time.years, params->time.months, |
---|
3271 | params->time.days, params->time.hours, |
---|
3272 | params->time.am_pm, params->time.minutes, |
---|
3273 | params->id); |
---|
3274 | |
---|
3275 | if (!result) { |
---|
3276 | dbi_conn_error(dbi->conn, &msglog); |
---|
3277 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3278 | __func__, __LINE__, msglog); |
---|
3279 | return -1; |
---|
3280 | } |
---|
3281 | dbi_result_free(result); |
---|
3282 | |
---|
3283 | return 0; |
---|
3284 | } |
---|
3285 | |
---|
3286 | static int og_dbi_schedule_delete(struct og_dbi *dbi, uint32_t id) |
---|
3287 | { |
---|
3288 | const char *msglog; |
---|
3289 | dbi_result result; |
---|
3290 | |
---|
3291 | result = dbi_conn_queryf(dbi->conn, |
---|
3292 | "DELETE FROM programaciones WHERE idprogramacion=%d", |
---|
3293 | id); |
---|
3294 | if (!result) { |
---|
3295 | dbi_conn_error(dbi->conn, &msglog); |
---|
3296 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3297 | __func__, __LINE__, msglog); |
---|
3298 | return -1; |
---|
3299 | } |
---|
3300 | dbi_result_free(result); |
---|
3301 | |
---|
3302 | return 0; |
---|
3303 | } |
---|
3304 | |
---|
3305 | struct og_db_schedule { |
---|
3306 | uint32_t id; |
---|
3307 | uint32_t task_id; |
---|
3308 | const char *name; |
---|
3309 | struct og_schedule_time time; |
---|
3310 | uint32_t week_days; |
---|
3311 | uint32_t weeks; |
---|
3312 | uint32_t suspended; |
---|
3313 | uint32_t session; |
---|
3314 | }; |
---|
3315 | |
---|
3316 | static int og_dbi_schedule_get_json(struct og_dbi *dbi, json_t *root, |
---|
3317 | const char *task_id, const char *schedule_id) |
---|
3318 | { |
---|
3319 | struct og_db_schedule schedule; |
---|
3320 | json_t *obj, *array; |
---|
3321 | const char *msglog; |
---|
3322 | dbi_result result; |
---|
3323 | int err = 0; |
---|
3324 | |
---|
3325 | if (task_id) { |
---|
3326 | result = dbi_conn_queryf(dbi->conn, |
---|
3327 | "SELECT idprogramacion," |
---|
3328 | " identificador, nombrebloque," |
---|
3329 | " annos, meses, diario, dias," |
---|
3330 | " semanas, horas, ampm," |
---|
3331 | " minutos,suspendida, sesion " |
---|
3332 | "FROM programaciones " |
---|
3333 | "WHERE identificador=%d", |
---|
3334 | atoi(task_id)); |
---|
3335 | } else if (schedule_id) { |
---|
3336 | result = dbi_conn_queryf(dbi->conn, |
---|
3337 | "SELECT idprogramacion," |
---|
3338 | " identificador, nombrebloque," |
---|
3339 | " annos, meses, diario, dias," |
---|
3340 | " semanas, horas, ampm," |
---|
3341 | " minutos,suspendida, sesion " |
---|
3342 | "FROM programaciones " |
---|
3343 | "WHERE idprogramacion=%d", |
---|
3344 | atoi(schedule_id)); |
---|
3345 | } else { |
---|
3346 | result = dbi_conn_queryf(dbi->conn, |
---|
3347 | "SELECT idprogramacion," |
---|
3348 | " identificador, nombrebloque," |
---|
3349 | " annos, meses, diario, dias," |
---|
3350 | " semanas, horas, ampm," |
---|
3351 | " minutos,suspendida, sesion " |
---|
3352 | "FROM programaciones"); |
---|
3353 | } |
---|
3354 | |
---|
3355 | if (!result) { |
---|
3356 | dbi_conn_error(dbi->conn, &msglog); |
---|
3357 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
3358 | __func__, __LINE__, msglog); |
---|
3359 | return -1; |
---|
3360 | } |
---|
3361 | |
---|
3362 | array = json_array(); |
---|
3363 | if (!array) |
---|
3364 | return -1; |
---|
3365 | |
---|
3366 | while (dbi_result_next_row(result)) { |
---|
3367 | schedule.id = dbi_result_get_uint(result, "idprogramacion"); |
---|
3368 | schedule.task_id = dbi_result_get_uint(result, "identificador"); |
---|
3369 | schedule.name = dbi_result_get_string(result, "nombrebloque"); |
---|
3370 | schedule.time.years = dbi_result_get_uint(result, "annos"); |
---|
3371 | schedule.time.months = dbi_result_get_uint(result, "meses"); |
---|
3372 | schedule.time.days = dbi_result_get_uint(result, "diario"); |
---|
3373 | schedule.time.hours = dbi_result_get_uint(result, "horas"); |
---|
3374 | schedule.time.am_pm = dbi_result_get_uint(result, "ampm"); |
---|
3375 | schedule.time.minutes = dbi_result_get_uint(result, "minutos"); |
---|
3376 | schedule.week_days = dbi_result_get_uint(result, "dias"); |
---|
3377 | schedule.weeks = dbi_result_get_uint(result, "semanas"); |
---|
3378 | schedule.suspended = dbi_result_get_uint(result, "suspendida"); |
---|
3379 | schedule.session = dbi_result_get_uint(result, "sesion"); |
---|
3380 | |
---|
3381 | obj = json_object(); |
---|
3382 | if (!obj) { |
---|
3383 | err = -1; |
---|
3384 | break; |
---|
3385 | } |
---|
3386 | json_object_set_new(obj, "id", json_integer(schedule.id)); |
---|
3387 | json_object_set_new(obj, "task", json_integer(schedule.task_id)); |
---|
3388 | json_object_set_new(obj, "name", json_string(schedule.name)); |
---|
3389 | json_object_set_new(obj, "years", json_integer(schedule.time.years)); |
---|
3390 | json_object_set_new(obj, "months", json_integer(schedule.time.months)); |
---|
3391 | json_object_set_new(obj, "days", json_integer(schedule.time.days)); |
---|
3392 | json_object_set_new(obj, "hours", json_integer(schedule.time.hours)); |
---|
3393 | json_object_set_new(obj, "am_pm", json_integer(schedule.time.am_pm)); |
---|
3394 | json_object_set_new(obj, "minutes", json_integer(schedule.time.minutes)); |
---|
3395 | json_object_set_new(obj, "week_days", json_integer(schedule.week_days)); |
---|
3396 | json_object_set_new(obj, "weeks", json_integer(schedule.weeks)); |
---|
3397 | json_object_set_new(obj, "suspended", json_integer(schedule.suspended)); |
---|
3398 | json_object_set_new(obj, "session", json_integer(schedule.session)); |
---|
3399 | |
---|
3400 | json_array_append_new(array, obj); |
---|
3401 | } |
---|
3402 | |
---|
3403 | json_object_set_new(root, "schedule", array); |
---|
3404 | |
---|
3405 | dbi_result_free(result); |
---|
3406 | |
---|
3407 | return err; |
---|
3408 | } |
---|
3409 | |
---|
3410 | static int og_task_schedule_create(struct og_msg_params *params) |
---|
3411 | { |
---|
3412 | enum og_schedule_type type; |
---|
3413 | uint32_t schedule_id; |
---|
3414 | struct og_dbi *dbi; |
---|
3415 | int err; |
---|
3416 | |
---|
3417 | if (!strcmp(params->type, "task")) |
---|
3418 | type = OG_SCHEDULE_TASK; |
---|
3419 | else if (!strcmp(params->type, "procedure")) |
---|
3420 | type = OG_SCHEDULE_PROCEDURE; |
---|
3421 | else if (!strcmp(params->type, "command")) |
---|
3422 | type = OG_SCHEDULE_COMMAND; |
---|
3423 | else |
---|
3424 | return -1; |
---|
3425 | |
---|
3426 | dbi = og_dbi_open(&ogconfig.db); |
---|
3427 | if (!dbi) { |
---|
3428 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3429 | __func__, __LINE__); |
---|
3430 | return -1; |
---|
3431 | } |
---|
3432 | |
---|
3433 | err = og_dbi_schedule_create(dbi, params, &schedule_id, type); |
---|
3434 | if (err < 0) { |
---|
3435 | og_dbi_close(dbi); |
---|
3436 | return -1; |
---|
3437 | } |
---|
3438 | og_schedule_create(schedule_id, atoi(params->task_id), type, |
---|
3439 | ¶ms->time); |
---|
3440 | og_schedule_refresh(og_loop); |
---|
3441 | og_dbi_close(dbi); |
---|
3442 | |
---|
3443 | return 0; |
---|
3444 | } |
---|
3445 | |
---|
3446 | static int og_cmd_schedule_create(json_t *element, struct og_msg_params *params) |
---|
3447 | { |
---|
3448 | const char *key; |
---|
3449 | json_t *value; |
---|
3450 | int err; |
---|
3451 | |
---|
3452 | if (json_typeof(element) != JSON_OBJECT) |
---|
3453 | return -1; |
---|
3454 | |
---|
3455 | json_object_foreach(element, key, value) { |
---|
3456 | if (!strcmp(key, "task")) { |
---|
3457 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
3458 | params->flags |= OG_REST_PARAM_TASK; |
---|
3459 | } else if (!strcmp(key, "name")) { |
---|
3460 | err = og_json_parse_string(value, ¶ms->name); |
---|
3461 | params->flags |= OG_REST_PARAM_NAME; |
---|
3462 | } else if (!strcmp(key, "when")) { |
---|
3463 | err = og_json_parse_time_params(value, params); |
---|
3464 | } else if (!strcmp(key, "type")) { |
---|
3465 | err = og_json_parse_string(value, ¶ms->type); |
---|
3466 | params->flags |= OG_REST_PARAM_TYPE; |
---|
3467 | } |
---|
3468 | |
---|
3469 | if (err < 0) |
---|
3470 | break; |
---|
3471 | } |
---|
3472 | |
---|
3473 | if (!og_msg_params_validate(params, OG_REST_PARAM_TASK | |
---|
3474 | OG_REST_PARAM_NAME | |
---|
3475 | OG_REST_PARAM_TIME_YEARS | |
---|
3476 | OG_REST_PARAM_TIME_MONTHS | |
---|
3477 | OG_REST_PARAM_TIME_WEEKS | |
---|
3478 | OG_REST_PARAM_TIME_WEEK_DAYS | |
---|
3479 | OG_REST_PARAM_TIME_DAYS | |
---|
3480 | OG_REST_PARAM_TIME_HOURS | |
---|
3481 | OG_REST_PARAM_TIME_MINUTES | |
---|
3482 | OG_REST_PARAM_TIME_AM_PM | |
---|
3483 | OG_REST_PARAM_TYPE)) |
---|
3484 | return -1; |
---|
3485 | |
---|
3486 | return og_task_schedule_create(params); |
---|
3487 | } |
---|
3488 | |
---|
3489 | static int og_cmd_schedule_update(json_t *element, struct og_msg_params *params) |
---|
3490 | { |
---|
3491 | struct og_dbi *dbi; |
---|
3492 | const char *key; |
---|
3493 | json_t *value; |
---|
3494 | int err; |
---|
3495 | |
---|
3496 | if (json_typeof(element) != JSON_OBJECT) |
---|
3497 | return -1; |
---|
3498 | |
---|
3499 | json_object_foreach(element, key, value) { |
---|
3500 | if (!strcmp(key, "id")) { |
---|
3501 | err = og_json_parse_string(value, ¶ms->id); |
---|
3502 | params->flags |= OG_REST_PARAM_ID; |
---|
3503 | } else if (!strcmp(key, "task")) { |
---|
3504 | err = og_json_parse_string(value, ¶ms->task_id); |
---|
3505 | params->flags |= OG_REST_PARAM_TASK; |
---|
3506 | } else if (!strcmp(key, "name")) { |
---|
3507 | err = og_json_parse_string(value, ¶ms->name); |
---|
3508 | params->flags |= OG_REST_PARAM_NAME; |
---|
3509 | } else if (!strcmp(key, "when")) |
---|
3510 | err = og_json_parse_time_params(value, params); |
---|
3511 | |
---|
3512 | if (err < 0) |
---|
3513 | break; |
---|
3514 | } |
---|
3515 | |
---|
3516 | if (!og_msg_params_validate(params, OG_REST_PARAM_ID | |
---|
3517 | OG_REST_PARAM_TASK | |
---|
3518 | OG_REST_PARAM_NAME | |
---|
3519 | OG_REST_PARAM_TIME_YEARS | |
---|
3520 | OG_REST_PARAM_TIME_MONTHS | |
---|
3521 | OG_REST_PARAM_TIME_DAYS | |
---|
3522 | OG_REST_PARAM_TIME_HOURS | |
---|
3523 | OG_REST_PARAM_TIME_MINUTES | |
---|
3524 | OG_REST_PARAM_TIME_AM_PM)) |
---|
3525 | return -1; |
---|
3526 | |
---|
3527 | dbi = og_dbi_open(&ogconfig.db); |
---|
3528 | if (!dbi) { |
---|
3529 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3530 | __func__, __LINE__); |
---|
3531 | return -1; |
---|
3532 | } |
---|
3533 | |
---|
3534 | err = og_dbi_schedule_update(dbi, params); |
---|
3535 | og_dbi_close(dbi); |
---|
3536 | |
---|
3537 | if (err < 0) |
---|
3538 | return err; |
---|
3539 | |
---|
3540 | og_schedule_update(og_loop, atoi(params->id), atoi(params->task_id), |
---|
3541 | ¶ms->time); |
---|
3542 | og_schedule_refresh(og_loop); |
---|
3543 | |
---|
3544 | return err; |
---|
3545 | } |
---|
3546 | |
---|
3547 | static int og_cmd_schedule_delete(json_t *element, struct og_msg_params *params) |
---|
3548 | { |
---|
3549 | struct og_dbi *dbi; |
---|
3550 | const char *key; |
---|
3551 | json_t *value; |
---|
3552 | int err; |
---|
3553 | |
---|
3554 | if (json_typeof(element) != JSON_OBJECT) |
---|
3555 | return -1; |
---|
3556 | |
---|
3557 | json_object_foreach(element, key, value) { |
---|
3558 | if (!strcmp(key, "id")) { |
---|
3559 | err = og_json_parse_string(value, ¶ms->id); |
---|
3560 | params->flags |= OG_REST_PARAM_ID; |
---|
3561 | } else { |
---|
3562 | return -1; |
---|
3563 | } |
---|
3564 | |
---|
3565 | if (err < 0) |
---|
3566 | break; |
---|
3567 | } |
---|
3568 | |
---|
3569 | if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) |
---|
3570 | return -1; |
---|
3571 | |
---|
3572 | dbi = og_dbi_open(&ogconfig.db); |
---|
3573 | if (!dbi) { |
---|
3574 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3575 | __func__, __LINE__); |
---|
3576 | return -1; |
---|
3577 | } |
---|
3578 | |
---|
3579 | err = og_dbi_schedule_delete(dbi, atoi(params->id)); |
---|
3580 | og_dbi_close(dbi); |
---|
3581 | |
---|
3582 | og_schedule_delete(og_loop, atoi(params->id)); |
---|
3583 | |
---|
3584 | return err; |
---|
3585 | } |
---|
3586 | |
---|
3587 | static int og_cmd_schedule_get(json_t *element, struct og_msg_params *params, |
---|
3588 | char *buffer_reply) |
---|
3589 | { |
---|
3590 | struct og_buffer og_buffer = { |
---|
3591 | .data = buffer_reply, |
---|
3592 | }; |
---|
3593 | json_t *schedule_root; |
---|
3594 | struct og_dbi *dbi; |
---|
3595 | const char *key; |
---|
3596 | json_t *value; |
---|
3597 | int err; |
---|
3598 | |
---|
3599 | if (element) { |
---|
3600 | if (json_typeof(element) != JSON_OBJECT) |
---|
3601 | return -1; |
---|
3602 | |
---|
3603 | json_object_foreach(element, key, value) { |
---|
3604 | if (!strcmp(key, "task")) { |
---|
3605 | err = og_json_parse_string(value, |
---|
3606 | ¶ms->task_id); |
---|
3607 | } else if (!strcmp(key, "id")) { |
---|
3608 | err = og_json_parse_string(value, ¶ms->id); |
---|
3609 | } else { |
---|
3610 | return -1; |
---|
3611 | } |
---|
3612 | |
---|
3613 | if (err < 0) |
---|
3614 | break; |
---|
3615 | } |
---|
3616 | } |
---|
3617 | |
---|
3618 | dbi = og_dbi_open(&ogconfig.db); |
---|
3619 | if (!dbi) { |
---|
3620 | syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", |
---|
3621 | __func__, __LINE__); |
---|
3622 | return -1; |
---|
3623 | } |
---|
3624 | |
---|
3625 | schedule_root = json_object(); |
---|
3626 | if (!schedule_root) { |
---|
3627 | og_dbi_close(dbi); |
---|
3628 | return -1; |
---|
3629 | } |
---|
3630 | |
---|
3631 | err = og_dbi_schedule_get_json(dbi, schedule_root, |
---|
3632 | params->task_id, params->id); |
---|
3633 | og_dbi_close(dbi); |
---|
3634 | |
---|
3635 | if (err >= 0) |
---|
3636 | json_dump_callback(schedule_root, og_json_dump_clients, &og_buffer, 0); |
---|
3637 | |
---|
3638 | json_decref(schedule_root); |
---|
3639 | |
---|
3640 | return err; |
---|
3641 | } |
---|
3642 | |
---|
3643 | static int og_client_method_not_found(struct og_client *cli) |
---|
3644 | { |
---|
3645 | /* To meet RFC 7231, this function MUST generate an Allow header field |
---|
3646 | * containing the correct methods. For example: "Allow: POST\r\n" |
---|
3647 | */ |
---|
3648 | char buf[] = "HTTP/1.1 405 Method Not Allowed\r\n" |
---|
3649 | "Content-Length: 0\r\n\r\n"; |
---|
3650 | |
---|
3651 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3652 | |
---|
3653 | return -1; |
---|
3654 | } |
---|
3655 | |
---|
3656 | static int og_client_bad_request(struct og_client *cli) |
---|
3657 | { |
---|
3658 | char buf[] = "HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n"; |
---|
3659 | |
---|
3660 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3661 | |
---|
3662 | return -1; |
---|
3663 | } |
---|
3664 | |
---|
3665 | static int og_client_not_found(struct og_client *cli) |
---|
3666 | { |
---|
3667 | char buf[] = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"; |
---|
3668 | |
---|
3669 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3670 | |
---|
3671 | return -1; |
---|
3672 | } |
---|
3673 | |
---|
3674 | static int og_client_not_authorized(struct og_client *cli) |
---|
3675 | { |
---|
3676 | char buf[] = "HTTP/1.1 401 Unauthorized\r\n" |
---|
3677 | "WWW-Authenticate: Basic\r\n" |
---|
3678 | "Content-Length: 0\r\n\r\n"; |
---|
3679 | |
---|
3680 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3681 | |
---|
3682 | return -1; |
---|
3683 | } |
---|
3684 | |
---|
3685 | static int og_server_internal_error(struct og_client *cli) |
---|
3686 | { |
---|
3687 | char buf[] = "HTTP/1.1 500 Internal Server Error\r\n" |
---|
3688 | "Content-Length: 0\r\n\r\n"; |
---|
3689 | |
---|
3690 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3691 | |
---|
3692 | return -1; |
---|
3693 | } |
---|
3694 | |
---|
3695 | #define OG_MSG_RESPONSE_MAXLEN 65536 |
---|
3696 | |
---|
3697 | static int og_client_ok(struct og_client *cli, char *buf_reply) |
---|
3698 | { |
---|
3699 | char buf[OG_MSG_RESPONSE_MAXLEN] = {}; |
---|
3700 | int err = 0, len; |
---|
3701 | |
---|
3702 | len = snprintf(buf, sizeof(buf), |
---|
3703 | "HTTP/1.1 200 OK\r\nContent-Length: %ld\r\n\r\n%s", |
---|
3704 | strlen(buf_reply), buf_reply); |
---|
3705 | if (len >= (int)sizeof(buf)) |
---|
3706 | err = og_server_internal_error(cli); |
---|
3707 | |
---|
3708 | send(og_client_socket(cli), buf, strlen(buf), 0); |
---|
3709 | |
---|
3710 | return err; |
---|
3711 | } |
---|
3712 | |
---|
3713 | int og_client_state_process_payload_rest(struct og_client *cli) |
---|
3714 | { |
---|
3715 | char buf_reply[OG_MSG_RESPONSE_MAXLEN] = {}; |
---|
3716 | struct og_msg_params params = {}; |
---|
3717 | enum og_rest_method method; |
---|
3718 | const char *cmd, *body; |
---|
3719 | json_error_t json_err; |
---|
3720 | json_t *root = NULL; |
---|
3721 | int err = 0; |
---|
3722 | |
---|
3723 | syslog(LOG_DEBUG, "%s:%hu %.32s ...\n", |
---|
3724 | inet_ntoa(cli->addr.sin_addr), |
---|
3725 | ntohs(cli->addr.sin_port), cli->buf); |
---|
3726 | |
---|
3727 | if (!strncmp(cli->buf, "GET", strlen("GET"))) { |
---|
3728 | method = OG_METHOD_GET; |
---|
3729 | cmd = cli->buf + strlen("GET") + 2; |
---|
3730 | } else if (!strncmp(cli->buf, "POST", strlen("POST"))) { |
---|
3731 | method = OG_METHOD_POST; |
---|
3732 | cmd = cli->buf + strlen("POST") + 2; |
---|
3733 | } else |
---|
3734 | return og_client_method_not_found(cli); |
---|
3735 | |
---|
3736 | body = strstr(cli->buf, "\r\n\r\n") + 4; |
---|
3737 | |
---|
3738 | if (strcmp(cli->auth_token, ogconfig.rest.api_token)) { |
---|
3739 | syslog(LOG_ERR, "wrong Authentication key\n"); |
---|
3740 | return og_client_not_authorized(cli); |
---|
3741 | } |
---|
3742 | |
---|
3743 | if (cli->content_length) { |
---|
3744 | root = json_loads(body, 0, &json_err); |
---|
3745 | if (!root) { |
---|
3746 | syslog(LOG_ERR, "malformed json line %d: %s\n", |
---|
3747 | json_err.line, json_err.text); |
---|
3748 | return og_client_not_found(cli); |
---|
3749 | } |
---|
3750 | } |
---|
3751 | |
---|
3752 | if (!strncmp(cmd, "clients", strlen("clients"))) { |
---|
3753 | if (method != OG_METHOD_POST && |
---|
3754 | method != OG_METHOD_GET) |
---|
3755 | return og_client_method_not_found(cli); |
---|
3756 | |
---|
3757 | if (method == OG_METHOD_POST && !root) { |
---|
3758 | syslog(LOG_ERR, "command clients with no payload\n"); |
---|
3759 | return og_client_bad_request(cli); |
---|
3760 | } |
---|
3761 | switch (method) { |
---|
3762 | case OG_METHOD_POST: |
---|
3763 | err = og_cmd_post_clients(root, ¶ms); |
---|
3764 | break; |
---|
3765 | case OG_METHOD_GET: |
---|
3766 | err = og_cmd_get_clients(root, ¶ms, buf_reply); |
---|
3767 | break; |
---|
3768 | default: |
---|
3769 | return og_client_bad_request(cli); |
---|
3770 | } |
---|
3771 | } else if (!strncmp(cmd, "client/setup", |
---|
3772 | strlen("client/setup"))) { |
---|
3773 | if (method != OG_METHOD_GET) |
---|
3774 | return og_client_method_not_found(cli); |
---|
3775 | |
---|
3776 | if (!root) { |
---|
3777 | syslog(LOG_ERR, |
---|
3778 | "command client partitions with no payload\n"); |
---|
3779 | return og_client_bad_request(cli); |
---|
3780 | } |
---|
3781 | |
---|
3782 | err = og_cmd_get_client_setup(root, ¶ms, buf_reply); |
---|
3783 | } else if (!strncmp(cmd, "client/info", |
---|
3784 | strlen("client/info"))) { |
---|
3785 | if (method != OG_METHOD_GET) |
---|
3786 | return og_client_method_not_found(cli); |
---|
3787 | |
---|
3788 | if (!root) { |
---|
3789 | syslog(LOG_ERR, |
---|
3790 | "command client info with no payload\n"); |
---|
3791 | return og_client_bad_request(cli); |
---|
3792 | } |
---|
3793 | |
---|
3794 | err = og_cmd_get_client_info(root, ¶ms, buf_reply); |
---|
3795 | } else if (!strncmp(cmd, "client/add", strlen("client/add"))) { |
---|
3796 | if (method != OG_METHOD_POST) |
---|
3797 | return og_client_method_not_found(cli); |
---|
3798 | |
---|
3799 | if (!root) { |
---|
3800 | syslog(LOG_ERR, |
---|
3801 | "command client info with no payload\n"); |
---|
3802 | return og_client_bad_request(cli); |
---|
3803 | } |
---|
3804 | |
---|
3805 | err = og_cmd_post_client_add(root, ¶ms, buf_reply); |
---|
3806 | } else if (!strncmp(cmd, "client/delete", strlen("client/delete"))) { |
---|
3807 | if (method != OG_METHOD_POST) |
---|
3808 | return og_client_method_not_found(cli); |
---|
3809 | |
---|
3810 | if (!root) { |
---|
3811 | syslog(LOG_ERR, |
---|
3812 | "command client delete with no payload\n"); |
---|
3813 | return og_client_bad_request(cli); |
---|
3814 | } |
---|
3815 | |
---|
3816 | err = og_cmd_post_client_delete(root, ¶ms); |
---|
3817 | } else if (!strncmp(cmd, "wol", strlen("wol"))) { |
---|
3818 | if (method != OG_METHOD_POST) |
---|
3819 | return og_client_method_not_found(cli); |
---|
3820 | |
---|
3821 | if (!root) { |
---|
3822 | syslog(LOG_ERR, "command wol with no payload\n"); |
---|
3823 | return og_client_bad_request(cli); |
---|
3824 | } |
---|
3825 | err = og_cmd_wol(root, ¶ms); |
---|
3826 | } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { |
---|
3827 | if (method != OG_METHOD_POST) |
---|
3828 | return og_client_method_not_found(cli); |
---|
3829 | |
---|
3830 | if (!root) { |
---|
3831 | syslog(LOG_ERR, "command run with no payload\n"); |
---|
3832 | return og_client_bad_request(cli); |
---|
3833 | } |
---|
3834 | err = og_cmd_run_post(root, ¶ms); |
---|
3835 | } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { |
---|
3836 | if (method != OG_METHOD_POST) |
---|
3837 | return og_client_method_not_found(cli); |
---|
3838 | |
---|
3839 | if (!root) { |
---|
3840 | syslog(LOG_ERR, "command output with no payload\n"); |
---|
3841 | return og_client_bad_request(cli); |
---|
3842 | } |
---|
3843 | |
---|
3844 | err = og_cmd_run_get(root, ¶ms, buf_reply); |
---|
3845 | } else if (!strncmp(cmd, "session", strlen("session"))) { |
---|
3846 | if (method != OG_METHOD_POST && method != OG_METHOD_GET) |
---|
3847 | return og_client_method_not_found(cli); |
---|
3848 | |
---|
3849 | if (!root) { |
---|
3850 | syslog(LOG_ERR, "command session with no payload\n"); |
---|
3851 | return og_client_bad_request(cli); |
---|
3852 | } |
---|
3853 | |
---|
3854 | if (method == OG_METHOD_POST) |
---|
3855 | err = og_cmd_session(root, ¶ms); |
---|
3856 | else |
---|
3857 | err = og_cmd_get_session(root, ¶ms, buf_reply); |
---|
3858 | } else if (!strncmp(cmd, "scopes", strlen("scopes"))) { |
---|
3859 | if (method != OG_METHOD_GET) |
---|
3860 | return og_client_method_not_found(cli); |
---|
3861 | |
---|
3862 | err = og_cmd_scope_get(root, ¶ms, buf_reply); |
---|
3863 | } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { |
---|
3864 | if (method != OG_METHOD_POST) |
---|
3865 | return og_client_method_not_found(cli); |
---|
3866 | |
---|
3867 | if (!root) { |
---|
3868 | syslog(LOG_ERR, "command poweroff with no payload\n"); |
---|
3869 | return og_client_bad_request(cli); |
---|
3870 | } |
---|
3871 | err = og_cmd_poweroff(root, ¶ms); |
---|
3872 | } else if (!strncmp(cmd, "reboot", strlen("reboot"))) { |
---|
3873 | if (method != OG_METHOD_POST) |
---|
3874 | return og_client_method_not_found(cli); |
---|
3875 | |
---|
3876 | if (!root) { |
---|
3877 | syslog(LOG_ERR, "command reboot with no payload\n"); |
---|
3878 | return og_client_bad_request(cli); |
---|
3879 | } |
---|
3880 | err = og_cmd_reboot(root, ¶ms); |
---|
3881 | } else if (!strncmp(cmd, "mode", strlen("mode"))) { |
---|
3882 | if (method != OG_METHOD_GET && method != OG_METHOD_POST) |
---|
3883 | return og_client_method_not_found(cli); |
---|
3884 | |
---|
3885 | if (method == OG_METHOD_POST && !root) { |
---|
3886 | syslog(LOG_ERR, "command mode with no payload\n"); |
---|
3887 | return og_client_bad_request(cli); |
---|
3888 | } |
---|
3889 | |
---|
3890 | if (method == OG_METHOD_GET) |
---|
3891 | err = og_cmd_get_modes(root, ¶ms, buf_reply); |
---|
3892 | else if (method == OG_METHOD_POST) |
---|
3893 | err = og_cmd_post_modes(root, ¶ms); |
---|
3894 | } else if (!strncmp(cmd, "stop", strlen("stop"))) { |
---|
3895 | if (method != OG_METHOD_POST) |
---|
3896 | return og_client_method_not_found(cli); |
---|
3897 | |
---|
3898 | if (!root) { |
---|
3899 | syslog(LOG_ERR, "command stop with no payload\n"); |
---|
3900 | return og_client_bad_request(cli); |
---|
3901 | } |
---|
3902 | err = og_cmd_stop(root, ¶ms); |
---|
3903 | } else if (!strncmp(cmd, "refresh", strlen("refresh"))) { |
---|
3904 | if (method != OG_METHOD_POST) |
---|
3905 | return og_client_method_not_found(cli); |
---|
3906 | |
---|
3907 | if (!root) { |
---|
3908 | syslog(LOG_ERR, "command refresh with no payload\n"); |
---|
3909 | return og_client_bad_request(cli); |
---|
3910 | } |
---|
3911 | err = og_cmd_refresh(root, ¶ms); |
---|
3912 | } else if (!strncmp(cmd, "hardware", strlen("hardware"))) { |
---|
3913 | if (method != OG_METHOD_GET && method != OG_METHOD_POST) |
---|
3914 | return og_client_method_not_found(cli); |
---|
3915 | |
---|
3916 | if (!root) { |
---|
3917 | syslog(LOG_ERR, "command hardware with no payload\n"); |
---|
3918 | return og_client_bad_request(cli); |
---|
3919 | } |
---|
3920 | |
---|
3921 | if (method == OG_METHOD_GET) |
---|
3922 | err = og_cmd_get_hardware(root, ¶ms, buf_reply); |
---|
3923 | else if (method == OG_METHOD_POST) |
---|
3924 | err = og_cmd_hardware(root, ¶ms); |
---|
3925 | } else if (!strncmp(cmd, "software", strlen("software"))) { |
---|
3926 | if (method != OG_METHOD_POST && method != OG_METHOD_GET) |
---|
3927 | return og_client_method_not_found(cli); |
---|
3928 | |
---|
3929 | if (!root) { |
---|
3930 | syslog(LOG_ERR, "command software with no payload\n"); |
---|
3931 | return og_client_bad_request(cli); |
---|
3932 | } |
---|
3933 | |
---|
3934 | if (method == OG_METHOD_POST) |
---|
3935 | err = og_cmd_software(root, ¶ms); |
---|
3936 | else |
---|
3937 | err = og_cmd_get_software(root, ¶ms, buf_reply); |
---|
3938 | } else if (!strncmp(cmd, "images", strlen("images"))) { |
---|
3939 | if (method != OG_METHOD_GET) |
---|
3940 | return og_client_method_not_found(cli); |
---|
3941 | |
---|
3942 | if (!root) |
---|
3943 | err = og_cmd_images(buf_reply); |
---|
3944 | else |
---|
3945 | return og_client_method_not_found(cli); |
---|
3946 | } else if (!strncmp(cmd, "image/create", strlen("image/create"))) { |
---|
3947 | if (method != OG_METHOD_POST) |
---|
3948 | return og_client_method_not_found(cli); |
---|
3949 | |
---|
3950 | if (!root) { |
---|
3951 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
3952 | return og_client_bad_request(cli); |
---|
3953 | } |
---|
3954 | err = og_cmd_create_image(root, ¶ms); |
---|
3955 | } else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) { |
---|
3956 | if (method != OG_METHOD_POST) |
---|
3957 | return og_client_method_not_found(cli); |
---|
3958 | |
---|
3959 | if (!root) { |
---|
3960 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
3961 | return og_client_bad_request(cli); |
---|
3962 | } |
---|
3963 | err = og_cmd_restore_image(root, ¶ms); |
---|
3964 | } else if (!strncmp(cmd, "setup", strlen("setup"))) { |
---|
3965 | if (method != OG_METHOD_POST) |
---|
3966 | return og_client_method_not_found(cli); |
---|
3967 | |
---|
3968 | if (!root) { |
---|
3969 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
3970 | return og_client_bad_request(cli); |
---|
3971 | } |
---|
3972 | err = og_cmd_setup(root, ¶ms); |
---|
3973 | } else if (!strncmp(cmd, "run/schedule", strlen("run/schedule"))) { |
---|
3974 | if (method != OG_METHOD_POST) |
---|
3975 | return og_client_method_not_found(cli); |
---|
3976 | |
---|
3977 | if (!root) { |
---|
3978 | syslog(LOG_ERR, "command create with no payload\n"); |
---|
3979 | return og_client_bad_request(cli); |
---|
3980 | } |
---|
3981 | |
---|
3982 | err = og_cmd_run_schedule(root, ¶ms); |
---|
3983 | } else if (!strncmp(cmd, "task/run", strlen("task/run"))) { |
---|
3984 | if (method != OG_METHOD_POST) |
---|
3985 | return og_client_method_not_found(cli); |
---|
3986 | |
---|
3987 | if (!root) { |
---|
3988 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
3989 | return og_client_bad_request(cli); |
---|
3990 | } |
---|
3991 | err = og_cmd_task_post(root, ¶ms); |
---|
3992 | } else if (!strncmp(cmd, "schedule/create", |
---|
3993 | strlen("schedule/create"))) { |
---|
3994 | if (method != OG_METHOD_POST) |
---|
3995 | return og_client_method_not_found(cli); |
---|
3996 | |
---|
3997 | if (!root) { |
---|
3998 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
3999 | return og_client_bad_request(cli); |
---|
4000 | } |
---|
4001 | err = og_cmd_schedule_create(root, ¶ms); |
---|
4002 | } else if (!strncmp(cmd, "schedule/delete", |
---|
4003 | strlen("schedule/delete"))) { |
---|
4004 | if (method != OG_METHOD_POST) |
---|
4005 | return og_client_method_not_found(cli); |
---|
4006 | |
---|
4007 | if (!root) { |
---|
4008 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
4009 | return og_client_bad_request(cli); |
---|
4010 | } |
---|
4011 | err = og_cmd_schedule_delete(root, ¶ms); |
---|
4012 | } else if (!strncmp(cmd, "schedule/update", |
---|
4013 | strlen("schedule/update"))) { |
---|
4014 | if (method != OG_METHOD_POST) |
---|
4015 | return og_client_method_not_found(cli); |
---|
4016 | |
---|
4017 | if (!root) { |
---|
4018 | syslog(LOG_ERR, "command task with no payload\n"); |
---|
4019 | return og_client_bad_request(cli); |
---|
4020 | } |
---|
4021 | err = og_cmd_schedule_update(root, ¶ms); |
---|
4022 | } else if (!strncmp(cmd, "schedule/get", |
---|
4023 | strlen("schedule/get"))) { |
---|
4024 | if (method != OG_METHOD_POST) |
---|
4025 | return og_client_method_not_found(cli); |
---|
4026 | |
---|
4027 | err = og_cmd_schedule_get(root, ¶ms, buf_reply); |
---|
4028 | } else { |
---|
4029 | syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); |
---|
4030 | err = og_client_not_found(cli); |
---|
4031 | } |
---|
4032 | |
---|
4033 | if (root) |
---|
4034 | json_decref(root); |
---|
4035 | |
---|
4036 | if (err < 0) |
---|
4037 | return og_client_bad_request(cli); |
---|
4038 | |
---|
4039 | err = og_client_ok(cli, buf_reply); |
---|
4040 | if (err < 0) { |
---|
4041 | syslog(LOG_ERR, "HTTP response to %s:%hu is too large\n", |
---|
4042 | inet_ntoa(cli->addr.sin_addr), |
---|
4043 | ntohs(cli->addr.sin_port)); |
---|
4044 | } |
---|
4045 | |
---|
4046 | return err; |
---|
4047 | } |
---|