Compare commits

...

35 Commits

Author SHA1 Message Date
OpenGnSys Support Team 5cecb4f294 client: incorrect OS update after restoring image
refresh handler already updates the OS accordingly, remove broken code to
update OS that results in reporting no bootable OS after image restoration.
2025-02-13 19:19:31 +01:00
OpenGnSys Support Team 730d42a872 src: handle refresh response after image restore
use context to store disk, partition and image id that is requested to client
and handle refresh response from ogclient to update database, otherwise, an
explicit refresh from web frontend is required.
2025-02-13 13:24:42 +01:00
OpenGnSys Support Team 91c042ed61 client: incorrect partition type conversion to hexadecimal
partition code is already in decimal as integer, appending 0x to this decimal
results in an incorrect hexadecimal conversion when storing it to the database.

The web frontend bogusly displays "Empty" as partition type until a refresh
happens.
2025-02-12 13:42:48 +01:00
Alejandro Sirgo Rica acf9c07236 rest: send image/update to ogClient for update operations
Send image/update instead of only image/create for both update
and create operations. A failed update operation using the
error handler of OG_CMD_IMAGE_CREATE results in the deletion
of the image from database.

Fail if the image already exists for an image create operation.
2025-02-04 09:42:36 +01:00
OpenGnSys Support Team 4db69ad054 boot: set hostname in grub configuration
add missing hostname= to grub configuration.
2025-01-30 14:09:05 +01:00
OpenGnSys Support Team d8b271f503 cfg: rename user in ogserver.json
use default user in samba, otherwise it breaks ogrelive
2025-01-21 21:21:56 +01:00
Alejandro Sirgo Rica 2daa9cdb36 src: prevent unlinking image to a partition
Fix partition code parse from strng to integer in
og_update_client_disk_info(), forcing a positive partition format
detection every refresh. This caused the unlink of images in every
partition during a refresh command.

Remove image idimagen=0 SQL instruction from software inventory.
Prevent image unlink during a sofware inventory command for a specific
partition.
2025-01-14 14:59:11 +01:00
OpenGnSys Support Team 41c8dec51c rest: use http error code to report mac is in use
report 409 conflict if mac is in use
2025-01-13 12:30:45 +01:00
Alejandro Sirgo Rica 31d426b018 rest: prevent repo deletion if it is in use
Check if the repo is used by any client before the deletion in
/repository/delete
2024-12-16 11:59:15 +01:00
OpenGnSys Support Team 5e21716afb cfg: add samba configuration and use it in ogrelive template
add samba credentials to ogserver.json

       "samba" : {
               "user" : "og",
               "pass" : "test"
       }

and use it in ogrelive template.
2024-12-12 16:05:55 +01:00
Alejandro Sirgo Rica 6a63218f85 rest: add POST oglive/default
Remove 'is_default' column from 'lives' database table and add
column 'priority' of type INT. This new value can store a priority
value but the actual design only stores 1 or 0.

Update GET oglive/list to use the new 'priority' database value.

Add POST oglive/default.
Set the database column 'priority' to 1 if the new default
exists in the database. Set the others to priority 0.
Modify legacy ogliveinfo.json if the new default coresponds to a
live not found in the database. Edit the field 'default' of the
file's json.
2024-12-10 16:33:49 +01:00
OpenGnSys Support Team e3188191d9 rest: add and use http error code to improve error reporting
Use http error code to improve better error reporting:

- 404 Not Found
  + object is not found

- 409 Conflict
  + object already exists

- 423 Locked
  + object is in use

- 500 Internal Server Error:
  + database query fails
  + out of memory

- 501 Service Unavailable
  + Cannot connect to database

- 507 Insufficient Storage
  + Disk is full
2024-12-10 12:12:43 +01:00
OpenGnSys Support Team 8f52b8911e rest: extend og_rest_ctx to provide http error
this is to improve error reporting for ogcp and ogcli
2024-12-05 17:12:30 +01:00
OpenGnSys Support Team ef3bad6e14 rest: consolidate rest API handlers
add struct og_rest_ctx and use it to consolidate rest API handlers and to
ease future extensibility.
2024-12-03 10:08:24 +01:00
OpenGnSys Support Team da31efb4e0 rest: statify image restore and create function
only used in rest.c, remove the forward declaration in json.h
2024-12-02 23:55:35 +01:00
Alejandro Sirgo Rica 00d54325d3 rest: update POST /shell/run for cmd execution
Remove legacy "echo" parameter.
Add boolean "inline" parameter to determine if the execution is
an inline command or a predefined script in /opt/opengnsys/shell/
2024-11-27 14:06:29 +01:00
OpenGnSys Support Team 113472d5c0 src: add built-in ogrelive grub2 boot mode template
Add built-in ogrelive grub2 boot mode template and use it to generate the grub2
boot file for each computer for the new live system.

Use grub2 http to download vmlinuz, initrd.img.

Use live support to download filesystem.squashfs through http.

check for vmlinuz in cache, as usual, otherwise fallback to network boot.
2024-11-25 12:18:34 +01:00
OpenGnSys Support Team 232e6eb308 rest: add og_boot_params_find() and og_boot_params_free() and use them
Prepare to reuse this code for the new live system.

Add two new fields to represent server and repository IP.
2024-11-25 10:33:47 +01:00
OpenGnSys Support Team c110bd7d8d rest: do not release dbi in og_get_client_mode_params()
caller function releases this, this results in double free.
2024-11-25 10:33:47 +01:00
Alejandro Sirgo Rica aebf2281bf rest: allow creation of folder with same name in different nodes
Check the parent node along with the folder name to identify if
the folder already exists.

Fix error when using a folder name defined in any other folder.
2024-11-21 17:07:28 +01:00
Alejandro Sirgo Rica 0ea0394709 rest: rename "name" to "directory" in GET /oglive/list
Rename name field for the contents of the new live format.
2024-11-21 13:34:27 +01:00
Alejandro Sirgo Rica 16baa88541 refresh: update client login status from "status" field value
Update the session status based on the value of the "status" field
in the refresh response, the possible values are:
LINUXS, LINUX, WINS, WIN.
2024-11-18 10:09:08 +01:00
Alejandro Sirgo Rica 4103945785 rest: add POST /oglive/add and POST /oglive/delete
Add API to register and delete oglives from the database.

POST /oglive/add
Register an installed oglive in the database, if the entry already
exists update its creation date.

Example request payload:
{"name": "ogrelive-6.1.0-26"}

POST /oglive/delete
Remove an installed oglive from the database

Example request payload:
{"name": "ogrelive-6.1.0-26"}
2024-11-11 12:29:11 +01:00
Alejandro Sirgo Rica 892a8fa2e5 rest: update GET /oglive/list to include oglive data from database
Append oglive info at the end of the array of the "oglive" entry.
Each entry defines the name of the oglive and the creation date.
2024-11-11 12:29:11 +01:00
OpenGnSys Support Team 55179decb7 client: improve error reporting for image creation reponse
provide a bit more information in the logs to identify the error
2024-11-07 19:01:38 +01:00
OpenGnSys Support Team 1fd62a4850 client: use image id in last command context
instead of using image id that client reports
2024-11-07 18:57:32 +01:00
OpenGnSys Support Team e67cdafb71 client: no need to update repository id after creating image
this repository id is already added to the database when the image creation is
requested, remove this code to update it again when the client replies
2024-11-07 18:52:07 +01:00
OpenGnSys Support Team 24e70ac3ff rest: switch to use grub2 templates as base directory
use grub2 template for GET /mode to list boot modes
2024-10-31 16:45:30 +01:00
OpenGnSys Support Team bff3d1dc9d client: remove image stub entry image creation registration fails
if client succeeds to create the image but server fails to register this image,
then remove the image stub in the database.
2024-10-31 01:19:46 +01:00
OpenGnSys Support Team 479998d76f src: add last client command context and use it for image creation
Add last client command context that is used in the request to client.

This allows to remove the stub image entry in the database in case that the
image creation fails.
2024-10-31 01:18:57 +01:00
OpenGnSys Support Team 83b1be6589 client: add helper function to reset last client command information
just a preparation, no functional changes are intended.
2024-10-31 00:39:43 +01:00
OpenGnSys Support Team 16cc22df3e src: place last client command information in struct
just a preparation, no functional changes are intended.
2024-10-31 00:37:03 +01:00
OpenGnSys Support Team 2febb50a92 rest: do not remove image on center delete
if center is removed, leave images in place, image belongs to
repository, not to center.

This comes from b4870733 ('#942 Add REST POST /center/delete method')
2024-10-30 12:20:42 +01:00
Alejandro Sirgo Rica e679925bd0 src: add safe_strtoull for safe string to number conversion
Add safe_strtoull to validate the execution of strtoull.
Definining the base of the number is required becase partition
codes are base 16 but they lack the 0x prefix.

Replace uses of atoi and strtoull/strtoul and log the conversion
errors.
2024-10-25 15:09:31 +02:00
Alejandro Sirgo Rica e960063a13 rest: add conditional boot mode configuration for ogReLive
Add a different boot mode configuration for the legacy live and the
new live.

Remove outdated params for ogReLive.
2024-10-24 13:45:49 +02:00
15 changed files with 1724 additions and 646 deletions

View File

@ -3,6 +3,7 @@ sbin_PROGRAMS = ogserver
AM_CFLAGS = ${LIBDBI_CFLAGS} ${LIBJANSSON_CFLAGS} ${LIBEVENT_CFLAGS} -g -Wall
ogserver_SOURCES= src/ogAdmServer.c \
src/boot.c \
src/cfg.c \
src/core.c \
src/dbi.c \

View File

@ -11,6 +11,10 @@
"user" : "mysql",
"pass" : "mysql"
},
"samba" : {
"user" : "opengnsys",
"pass" : "test"
},
"wol" : {
"interface" : "lo"
},

120
src/boot.c 100644
View File

@ -0,0 +1,120 @@
/*
* Copyright (C) 2020-2024 Soleta Networks <info@soleta.eu>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <syslog.h>
#include <sys/stat.h>
#include <errno.h>
#include "boot.h"
#include "utils.h"
#define OGRELIVE_TEMPLATE \
"# Autogenerated by ogserver on %s - do not edit this file!\n" \
"set oghostname=%s\n" \
"set ogserver=%s\n" \
"set ogrepo=%s\n" \
"set ogreliveversion=%s\n" \
"set ogrelivedir=ogrelive/$ogreliveversion\n" \
"set username=%s\n" \
"set passwd=%s\n" \
"\n" \
"set timeout=0\n" \
"set timeout_style=hidden\n" \
"\n" \
"echo \"checking for vmlinuz in cache...\"\n" \
"set root=''\n" \
"search --file --set root /boot/$ogreliveversion/vmlinuz\n" \
"if [ \"$root\" == \"\" ]; then\n" \
" echo \"no vmlinuz found in cache, booting from network\"\n" \
" set default=1;\n" \
"else\n" \
" echo \"vmlinuz found, booting from cache\"\n" \
" set default=0;\n" \
"fi\n" \
"\n" \
"menuentry \"ogReLive HTTP boot\" {\n" \
" insmod http\n" \
" insmod net\n" \
" net_bootp\n" \
" linux (http,$ogrepo)/$ogrelivedir/vmlinuz ro boot=live quiet splash oglivedir=$ogrelivedir hostname=$oghostname ip=$ogserver ogrepo=$ogrepo username=$username passwd=$passwd fetch=http://$ogrepo/$ogrelivedir/filesystem.squashfs\n" \
" initrd (http,$ogrepo)/$ogrelivedir/initrd.img\n" \
" boot\n" \
"}\n"
/* Same as OG_TFTP_BOOT_UEFI. */
#define OG_BOOT_GRUB_DIR "/opt/opengnsys/tftpboot/grub"
static void ogrelive_grub2_file_path(char *grub2_filename,
size_t grub2_filename_size,
const char *mac)
{
snprintf(grub2_filename, grub2_filename_size,
OG_BOOT_GRUB_DIR"/01-%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
mac[6], mac[7], mac[8], mac[9], mac[10], mac[11]);
str_tolower(grub2_filename);
}
int ogrelive_generate_grub2_file(const struct og_boot_cfg *cfg, const char *mac)
{
char grub2_tmp_filename[] = "/tmp/grub_template_XXXXXX";
char grub2_filename[FILENAME_MAX] = {};
time_t now = time(NULL);
char *date;
FILE *fp;
int fd;
date = asctime(localtime(&now));
date[strlen(date) - 1] = '\0';
fd = mkstemp(grub2_tmp_filename);
if (fd < 0) {
syslog(LOG_ERR, "Cannot open %s to %s (%s:%u)\n",
grub2_tmp_filename, grub2_filename, __FILE__, __LINE__);
return -1;
}
fp = fdopen(fd, "w+");
if (!fp) {
close(fd);
syslog(LOG_ERR, "Cannot open %s to %s (%s:%u)\n",
grub2_tmp_filename, grub2_filename, __FILE__, __LINE__);
return -1;
}
fprintf(fp, OGRELIVE_TEMPLATE, date,
cfg->hostname, cfg->ogserver, cfg->ogrepo, cfg->ogrelivedir, cfg->username, cfg->passwd);
fclose(fp);
if (unlink(grub2_filename) < 0) {
if (errno != ENOENT) {
syslog(LOG_ERR, "Failed to delete %s (%s:%u)\n",
grub2_filename, __FILE__, __LINE__);
return -1;
}
}
ogrelive_grub2_file_path(grub2_filename, sizeof(grub2_filename), mac);
if (rename(grub2_tmp_filename, grub2_filename) < 0) {
unlink(grub2_tmp_filename);
syslog(LOG_ERR, "Failed to rename %s to %s (%s:%u)\n",
grub2_tmp_filename, grub2_filename, __FILE__, __LINE__);
return -1;
}
chmod(grub2_filename, 0644);
return 0;
}

15
src/boot.h 100644
View File

@ -0,0 +1,15 @@
#ifndef _OG_BOOT_H_
#define _OG_BOOT_H_
struct og_boot_cfg {
const char *hostname;
const char *ogserver;
const char *ogrepo;
const char *ogrelivedir;
const char *username;
const char *passwd;
};
int ogrelive_generate_grub2_file(const struct og_boot_cfg *cfg, const char *mac);
#endif /* _OG_BOOT_H_ */

View File

@ -70,6 +70,27 @@ static int parse_json_db(struct og_server_cfg *cfg, json_t *element)
return 0;
}
static int parse_json_samba(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
json_t *value;
json_object_foreach(element, key, value) {
if (!strcmp(key, "user")) {
if (og_json_parse_string(value, &cfg->smb.user) < 0)
return -1;
} else if (!strcmp(key, "pass")) {
if (og_json_parse_string(value, &cfg->smb.pass) < 0)
return -1;
} else {
syslog(LOG_ERR, "unknown key `%s' in samba\n", key);
return -1;
}
}
return 0;
}
static int parse_json_wol(struct og_server_cfg *cfg, json_t *element)
{
const char *key;
@ -110,6 +131,7 @@ static int parse_json_repo(struct og_server_cfg *cfg, json_t *element)
#define OG_SERVER_CFG_DB (1 << 1)
#define OG_SERVER_CFG_WOL (1 << 2)
#define OG_SERVER_CFG_REPO (1 << 3)
#define OG_SERVER_CFG_SAMBA (1 << 4)
int parse_json_config(const char *filename, struct og_server_cfg *cfg)
{
@ -157,6 +179,12 @@ int parse_json_config(const char *filename, struct og_server_cfg *cfg)
break;
}
flags |= OG_SERVER_CFG_DB;
} else if (!strcmp(key, "samba")) {
if (parse_json_samba(cfg, value) < 0) {
ret = -1;
break;
}
flags |= OG_SERVER_CFG_SAMBA;
} else if (!strcmp(key, "repository")) {
if (parse_json_repo(cfg, value) < 0)
return -1;
@ -181,6 +209,9 @@ int parse_json_config(const char *filename, struct og_server_cfg *cfg)
ret = -1;
}
if (!(flags & OG_SERVER_CFG_SAMBA))
syslog(LOG_WARNING, "Missing samba configuration in ogserver.json file");
if (ret < 0)
json_decref(root);
else

View File

@ -11,6 +11,10 @@ struct og_server_cfg {
const char *port;
const char *api_token;
} rest;
struct {
const char *user;
const char *pass;
} smb;
struct {
const char *interface;
} wol;

View File

@ -14,6 +14,7 @@
#include "list.h"
#include "rest.h"
#include "json.h"
#include <stdint.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
@ -557,6 +558,7 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
int disk_part_len = 0;
char disk_part[1024];
const char *msglog;
uint64_t part_code;
int i;
if (serial_number && strlen(serial_number) > 0) {
@ -617,7 +619,12 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
continue;
}
reported_disk.size = strtoull(disks[i].size, NULL, 0);
if (safe_strtoull(disks[i].size, &reported_disk.size, 10, UINT64_MAX) < 0) {
syslog(LOG_ERR, "failed to parse disk size for disk %d (%s:%d)\n",
i + 1, __func__, __LINE__);
return false;
}
cur_disk.size = dbi_result_get_longlong(result, "tamano");
dbi_result_free(result);
@ -661,8 +668,17 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
return false;
}
reported_part.size = strtoull(partitions[i].size, NULL, 0);
reported_part.code = strtoul(partitions[i].code, NULL, 16);
if (safe_strtoull(partitions[i].size, &reported_part.size, 10, UINT64_MAX) < 0) {
syslog(LOG_ERR, "failed to parse partition size %s for partition %d (%s:%d)\n",
partitions[i].size, i + 1, __func__, __LINE__);
return false;
}
if (safe_strtoull(partitions[i].code, &part_code, 16, UINT32_MAX) < 0) {
syslog(LOG_ERR, "failed to parse partition code %s for partition %d (%s:%d)\n",
partitions[i].code, i + 1, __func__, __LINE__);
return false;
}
reported_part.code = part_code;
reported_part.filesystem = get_filesystem_id(partitions[i].filesystem);
reported_part.used_size = partitions[i].used_size;
reported_part.free_size = partitions[i].free_size;
@ -677,7 +693,7 @@ static bool og_update_client_disk_info(struct og_dbi *dbi,
"idordenador, numdisk, numpar, codpar, tamano, uso, "
"idsistemafichero, idnombreso, idimagen, "
"used_size, free_size)"
" VALUES(%d,%s,%s,0x%d,%s,0,%d,%d,0,%lu,%lu)",
" VALUES(%d,%s,%s,%d,%s,0,%d,%d,0,%lu,%lu)",
computer_id,
partitions[i].disk,
partitions[i].number,
@ -833,8 +849,12 @@ static int og_resp_refresh(json_t *data, struct og_client *cli)
* client using linux/windows mode.
*/
if (status) {
if (!strncmp(status, "LINUX", strlen("LINUX"))) {
if (!strncmp(status, "LINUXS", strlen("LINUXS"))) {
cli->status = OG_CLIENT_STATUS_LINUX_SESSION;
} else if (!strncmp(status, "LINUX", strlen("LINUX"))) {
cli->status = OG_CLIENT_STATUS_LINUX;
} else if (!strncmp(status, "WINS", strlen("WINS"))) {
cli->status = OG_CLIENT_STATUS_WIN_SESSION;
} else if (!strncmp(status, "WIN", strlen("WIN"))) {
cli->status = OG_CLIENT_STATUS_WIN;
}
@ -890,38 +910,10 @@ static bool og_dbi_update_image(struct og_dbi *dbi,
const struct og_image_legacy *img_info,
const char *computer_id)
{
int repo_id, sw_id, repo_alias;
const char *msglog;
dbi_result result;
uint32_t revision;
/* find repository identifier by repository ip and computer ID. */
result = dbi_conn_queryf(dbi->conn,
"SELECT repositorios.idrepositorio, repositorios.alias"
" FROM repositorios"
" LEFT JOIN ordenadores USING (idrepositorio)"
" WHERE repositorios.ip='%s' AND ordenadores.idordenador=%s",
img_info->repo, computer_id);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
__func__, __LINE__, msglog);
return false;
}
if (!dbi_result_next_row(result)) {
syslog(LOG_ERR,
"repository does not exist in database (%s:%d)\n",
__func__, __LINE__);
dbi_result_free(result);
return false;
}
repo_alias = dbi_result_get_uint(result, "alias");
if (repo_alias)
repo_id = repo_alias;
else
repo_id = dbi_result_get_uint(result, "idrepositorio");
dbi_result_free(result);
int sw_id;
/* find software id by computer ID, disk number and partition. */
result = dbi_conn_queryf(dbi->conn,
@ -949,11 +941,11 @@ static bool og_dbi_update_image(struct og_dbi *dbi,
result = dbi_conn_queryf(dbi->conn,
"UPDATE imagenes"
" SET idordenador=%s, numdisk=%s, numpar=%s, codpar=%s,"
" idperfilsoft=%d, idrepositorio=%d,"
" idperfilsoft=%d, "
" fechacreacion=NOW(), revision=revision+1"
" WHERE idimagen=%s",
computer_id, img_info->disk, img_info->part, img_info->code,
sw_id, repo_id, img_info->image_id);
sw_id, img_info->image_id);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
@ -1115,8 +1107,8 @@ static int og_resp_image_create(json_t *data, struct og_client *cli)
computer.center);
snprintf(soft_legacy.software, sizeof(soft_legacy.software), "%s",
software);
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s",
image_id);
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%u",
cli->last_cmd.ctx.image.id);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk);
@ -1132,24 +1124,27 @@ static int og_resp_image_create(json_t *data, struct og_client *cli)
soft_legacy.center);
if (!res) {
og_dbi_close(dbi);
syslog(LOG_ERR, "Problem updating client configuration\n");
syslog(LOG_ERR, "Problem updating software inventory (%s:%u)\n",
__FILE__, __LINE__);
return -1;
}
res = og_dbi_update_image(dbi, &img_legacy, soft_legacy.id);
if (!res) {
og_dbi_close(dbi);
syslog(LOG_ERR, "Problem updating client configuration\n");
syslog(LOG_ERR, "Problem updating image (%s:%u)\n",
__FILE__, __LINE__);
return -1;
}
res = update_image_info(dbi, image_id, clonator, compressor,
res = update_image_info(dbi, img_legacy.image_id, clonator, compressor,
filesystem, datasize, size, lastupdate, perms,
checksum);
og_dbi_close(dbi);
if (res) {
syslog(LOG_ERR, "Problem updating image info\n");
syslog(LOG_ERR, "Problem updating image (%s:%u)\n",
__FILE__, __LINE__);
return -1;
}
@ -1159,37 +1154,17 @@ static int og_resp_image_create(json_t *data, struct og_client *cli)
static int og_resp_image_restore(json_t *data, struct og_client *cli)
{
struct og_software_legacy soft_legacy;
uint32_t disk, partition, image_id;
struct og_image_legacy img_legacy;
struct og_computer computer = {};
const char *partition = NULL;
const char *image_id = NULL;
const char *disk = NULL;
const char *msglog;
struct og_dbi *dbi;
dbi_result result;
const char *key;
json_t *value;
int err = 0;
if (json_typeof(data) != JSON_OBJECT)
return -1;
json_object_foreach(data, key, value) {
if (!strcmp(key, "partition"))
err = og_json_parse_string(value, &partition);
else if (!strcmp(key, "disk"))
err = og_json_parse_string(value, &disk);
else if (!strcmp(key, "image_id"))
err = og_json_parse_string(value, &image_id);
if (err < 0)
return err;
}
if (!partition || !disk || !image_id) {
syslog(LOG_ERR, "malformed response json\n");
return -1;
}
err = og_resp_refresh(data, cli);
if (err < 0)
return err;
dbi = og_dbi_open(&ogconfig.db);
if (!dbi) {
@ -1198,9 +1173,13 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
return -1;
}
image_id = cli->last_cmd.ctx.image_restore.id;
partition = cli->last_cmd.ctx.image_restore.part;
disk = cli->last_cmd.ctx.image_restore.disk;
result = dbi_conn_queryf(dbi->conn,
"SELECT idperfilsoft FROM imagenes "
" WHERE idimagen='%s'", image_id);
" WHERE idimagen='%u'", image_id);
if (!result) {
og_dbi_close(dbi);
syslog(LOG_ERR, "failed to query database\n");
@ -1209,7 +1188,8 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
if (!dbi_result_next_row(result)) {
dbi_result_free(result);
og_dbi_close(dbi);
syslog(LOG_ERR, "software profile does not exist in database\n");
syslog(LOG_ERR, "software profile for image id %u does not exist in database\n",
image_id);
return -1;
}
snprintf(img_legacy.software_id, sizeof(img_legacy.software_id),
@ -1222,20 +1202,19 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli)
return -1;
}
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%s",
snprintf(img_legacy.image_id, sizeof(img_legacy.image_id), "%u",
image_id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%s", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id);
snprintf(img_legacy.part, sizeof(img_legacy.part), "%u", partition);
snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%u", disk);
snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%u", computer.id);
result = dbi_conn_queryf(dbi->conn,
"UPDATE ordenadores_particiones"
" SET idimagen=%s, idperfilsoft=%s, fechadespliegue=NOW(),"
" revision=(SELECT revision FROM imagenes WHERE idimagen=%s),"
" idnombreso=IFNULL((SELECT idnombreso FROM perfilessoft WHERE idperfilsoft=%s),0)"
" revision=(SELECT revision FROM imagenes WHERE idimagen=%s)"
" WHERE idordenador=%s AND numdisk=%s AND numpar=%s",
img_legacy.image_id, img_legacy.software_id,
img_legacy.image_id, img_legacy.software_id,
img_legacy.image_id,
soft_legacy.id, img_legacy.disk, img_legacy.part);
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
@ -1274,11 +1253,9 @@ static int og_agent_http_response_code(const char *buf)
return -1;
}
static int og_clear_image_placeholder()
static int og_resp_image_create_error(struct og_client *cli)
{
uint32_t image_id = 0;
struct og_dbi *dbi;
dbi_result result;
dbi = og_dbi_open(&ogconfig.db);
if (!dbi) {
@ -1287,26 +1264,9 @@ static int og_clear_image_placeholder()
return -1;
}
result = dbi_conn_queryf(dbi->conn,
"SELECT idimagen FROM imagenes WHERE fechacreacion IS NULL");
if (!result) {
syslog(LOG_ERR, "failed to query database\n");
return -1;
}
if (!dbi_result_next_row(result)) {
dbi_result_free(result);
og_dbi_close(dbi);
return 0;
}
image_id = dbi_result_get_uint(result, "idimagen");
dbi_result_free(result);
syslog(LOG_INFO, "Trying to delete uninitialized image with id %d\n", image_id);
if (og_dbi_delete_image(dbi, image_id) < 0) {
og_dbi_close(dbi);
if (og_dbi_delete_image(dbi, cli->last_cmd.ctx.image.id) < 0) {
syslog(LOG_WARNING, "Cannot delete image stub with id %d\n",
cli->last_cmd.ctx.image.id);
return -1;
}
@ -1314,9 +1274,16 @@ static int og_clear_image_placeholder()
return 0;
}
static void og_client_reset_cmd(struct og_client *cli)
{
cli->last_cmd.id = 0;
cli->last_cmd.type = OG_CMD_UNSPEC;
memset(&cli->last_cmd.ctx, 0, sizeof(cli->last_cmd.ctx));
}
int og_agent_state_process_response(struct og_client *cli)
{
enum og_cmd_type cmd_type = cli->last_cmd;
enum og_cmd_type cmd_type = cli->last_cmd.type;
int ret, err = -1, code;
json_error_t json_err;
bool success;
@ -1343,7 +1310,7 @@ int og_agent_state_process_response(struct og_client *cli)
case 500:
ret = 0;
success = false;
cli->last_cmd = OG_CMD_UNSPEC;
cli->last_cmd.type = OG_CMD_UNSPEC;
syslog(LOG_ERR, "Client %s:%hu reports failure to process command\n",
inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port));
/* ... cancel pending actions related to this task for this client here */
@ -1363,21 +1330,25 @@ int og_agent_state_process_response(struct og_client *cli)
}
if (success)
cli->last_cmd_result = OG_SUCCESS;
cli->last_cmd.result = OG_SUCCESS;
else
cli->last_cmd_result = OG_FAILURE;
cli->last_cmd.result = OG_FAILURE;
if (!success && cmd_type == OG_CMD_IMAGE_CREATE)
og_clear_image_placeholder();
if (!success && cmd_type == OG_CMD_IMAGE_CREATE) {
syslog(LOG_ERR, "Client %s:%hu reports failure when creating image with id %d\n",
inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port),
cli->last_cmd.ctx.image.id);
og_resp_image_create_error(cli);
}
if (code != 200 && code != 103) {
cli->last_cmd_id = 0;
cli->last_cmd.id = 0;
memset(&cli->last_cmd.ctx, 0, sizeof(cli->last_cmd.ctx));
return ret;
}
if (!cli->content_length) {
cli->last_cmd_id = 0;
cli->last_cmd = OG_CMD_UNSPEC;
og_client_reset_cmd(cli);
return 0;
}
@ -1396,7 +1367,7 @@ int og_agent_state_process_response(struct og_client *cli)
return err;
}
switch (cli->last_cmd) {
switch (cli->last_cmd.type) {
case OG_CMD_SHELL_RUN:
err = og_resp_shell_run(cli, root);
break;
@ -1413,6 +1384,11 @@ int og_agent_state_process_response(struct og_client *cli)
err = og_resp_refresh(root, cli);
break;
case OG_CMD_IMAGE_CREATE:
err = og_resp_image_create(root, cli);
if (err < 0)
og_resp_image_create_error(cli);
break;
case OG_CMD_IMAGE_UPDATE:
err = og_resp_image_create(root, cli);
break;
case OG_CMD_IMAGE_RESTORE:
@ -1436,12 +1412,11 @@ int og_agent_state_process_response(struct og_client *cli)
if (err < 0) {
err = 0;
success = false;
cli->last_cmd_result = OG_FAILURE;
cli->last_cmd.result = OG_FAILURE;
/* ... cancel pending actions related to this task for this client here */
}
cli->last_cmd_id = 0;
cli->last_cmd = OG_CMD_UNSPEC;
og_client_reset_cmd(cli);
return err;
}

View File

@ -281,7 +281,7 @@ static void og_agent_send_refresh(struct og_client *cli)
params.ips_array[0] = inet_ntoa(cli->addr.sin_addr);
params.ips_array_len = 1;
og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, &params, NULL);
og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, &params, NULL, NULL);
}
/* Shut down connection if there is no complete message after 10 seconds. */
@ -367,6 +367,7 @@ void og_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events)
int og_socket_server_init(const char *addr, const char *port)
{
struct sockaddr_in local;
uint64_t port_num;
uint32_t s_addr;
int sd, on = 1;
@ -384,7 +385,13 @@ int og_socket_server_init(const char *addr, const char *port)
local.sin_addr.s_addr = s_addr;
local.sin_family = AF_INET;
local.sin_port = htons(atoi(port));
if (safe_strtoull(port, &port_num, 10, UINT16_MAX) < 0) {
syslog(LOG_ERR, "failed to parse port %s (%s:%d)\n",
port, __func__, __LINE__);
return -1;
}
local.sin_port = htons(port_num);
if (bind(sd, (struct sockaddr *) &local, sizeof(local)) < 0) {
close(sd);

View File

@ -75,7 +75,6 @@ struct og_msg_params {
const char *cache;
const char *cache_size;
const char *comment;
bool echo;
struct og_partition partition_setup[OG_PARTITION_MAX];
struct og_image image;
uint64_t flags;
@ -83,8 +82,6 @@ struct og_msg_params {
};
int og_json_parse_partition_setup(json_t *element, struct og_msg_params *params);
int og_json_parse_create_image(json_t *element, struct og_msg_params *params);
int og_json_parse_restore_image(json_t *element, struct og_msg_params *params);
struct og_cmd_json {
const char *type;

View File

@ -575,7 +575,7 @@ bool cuestionPerfilSoftware(struct og_dbi *dbi, char *idc, char *ido,
if (idperfilsoftware != nwidperfilsoft) { // No coinciden los perfiles
// Actualiza el identificador del perfil software del ordenador
result = dbi_conn_queryf(dbi->conn,
"UPDATE ordenadores_particiones SET idperfilsoft=%d,idimagen=0"
"UPDATE ordenadores_particiones SET idperfilsoft=%d"
" WHERE idordenador=%s AND numpar=%s", nwidperfilsoft, ido, par);
if (!result) { // Error al insertar
dbi_conn_error(dbi->conn, &msglog);

1853
src/rest.c

File diff suppressed because it is too large Load Diff

View File

@ -51,6 +51,19 @@ enum og_cmd_result {
OG_SUCCESS = 2,
};
struct og_cmd_ctx {
union {
struct {
uint32_t id;
} image;
struct {
uint32_t id;
uint32_t disk;
uint32_t part;
} image_restore;
};
};
#define OG_MSG_REQUEST_MAXLEN 131072
struct og_client {
@ -66,9 +79,12 @@ struct og_client {
int content_length;
char auth_token[64];
enum og_client_status status;
enum og_cmd_type last_cmd;
unsigned int last_cmd_id;
enum og_cmd_result last_cmd_result;
struct {
enum og_cmd_type type;
unsigned int id;
enum og_cmd_result result;
struct og_cmd_ctx ctx;
} last_cmd;
uint32_t speed;
uint32_t seq;
struct {
@ -131,7 +147,10 @@ enum og_rest_uri {
OG_URI_CACHE_FETCH,
OG_URI_PART_SETUP,
OG_URI_OGLIVE_LIST,
OG_URI_OGLIVE_ADD,
OG_URI_OGLIVE_DELETE,
OG_URI_OGLIVE_SET,
OG_URI_OGLIVE_DEFAULT,
OG_URI_CENTER_ADD,
OG_URI_CENTER_UPDATE,
OG_URI_CENTER_DELETE,
@ -157,7 +176,7 @@ enum og_rest_method {
int og_send_request(enum og_rest_method method, enum og_cmd_type type,
const struct og_msg_params *params,
const json_t *data);
const json_t *data, const struct og_cmd_ctx *ctx);
int og_dbi_scope_get(struct og_dbi *dbi, json_t *array);

View File

@ -464,6 +464,75 @@ static int og_dbi_schema_v11(struct og_dbi *dbi)
return 0;
}
static int og_dbi_schema_v12(struct og_dbi *dbi)
{
const char *msglog;
dbi_result result;
syslog(LOG_DEBUG, "Creating table oglive\n");
result = dbi_conn_query(dbi->conn, "CREATE TABLE `oglive` ("
"`id` BIGINT NOT NULL AUTO_INCREMENT,"
"`name` VARCHAR(100),"
"`creation_date` DATETIME NOT NULL,"
"`is_default` BOOLEAN NOT NULL,"
"PRIMARY KEY (`id`)"
")");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Error when creating oglive (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
result = dbi_conn_query(dbi->conn, "UPDATE version SET version = 12");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Could not update version row (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
return 0;
}
static int og_dbi_schema_v13(struct og_dbi *dbi)
{
const char *msglog;
dbi_result result;
syslog(LOG_DEBUG, "Updating table oglive\n");
result = dbi_conn_query(dbi->conn, "ALTER TABLE `oglive` DROP COLUMN `is_default`");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Error deleting column is_default in oglive table (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
result = dbi_conn_query(dbi->conn, "ALTER TABLE `oglive` ADD COLUMN `priority` INT NOT NULL DEFAULT 0");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Error adding column priority to oglive table (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
result = dbi_conn_query(dbi->conn, "UPDATE version SET version = 13");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Could not update version row (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
return 0;
}
static struct og_schema_version {
int version;
int (*update)(struct og_dbi *dbi);
@ -479,6 +548,8 @@ static struct og_schema_version {
{ .version = 9, .update = og_dbi_schema_v9, },
{ .version = 10, .update = og_dbi_schema_v10,},
{ .version = 11, .update = og_dbi_schema_v11,},
{ .version = 12, .update = og_dbi_schema_v12,},
{ .version = 13, .update = og_dbi_schema_v13,},
{ 0, NULL },
};

View File

@ -7,7 +7,12 @@
* (at your option) any later version.
*/
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include "utils.h"
void str_toupper(char *str)
@ -29,3 +34,26 @@ void str_tolower(char *str)
c++;
}
}
int safe_strtoull(const char *str, uint64_t *out_value, int base, uint64_t max)
{
char *endptr = NULL;
uint64_t result;
errno = 0;
assert(str != NULL && out_value != NULL);
if (str[0] == '-')
return -1;
result = strtoull(str, &endptr, base);
if (endptr == str ||
*endptr != '\0' ||
(errno == ERANGE && result == ULLONG_MAX) ||
result > max)
return -1;
*out_value = result;
return 0;
}

View File

@ -1,7 +1,10 @@
#ifndef _OG_UTILS_H
#define _OG_UTILS_H
#include <stdint.h>
void str_toupper(char *str);
void str_tolower(char *str);
int safe_strtoull(const char *str, uint64_t *out_value, int base, uint64_t max);
#endif