Commit Graph

318 Commits (41c8dec51c4717bdabd60486bd4df6cd999132cb)

Author SHA1 Message Date
OpenGnSys Support Team 4567bce90b src: fallback to use first repository/server found
use first repository/server in the list in case no specific repository/server
is found in the same network, this is a fallback that works if repository/server
is reachable through gateway.
2024-09-04 12:19:58 +02:00
OpenGnSys Support Team 7977711ee9 rest: use integer for server id
id is not a string, use integer instead
2024-09-03 14:24:09 +02:00
OpenGnSys Support Team 38076677c7 rest: memleak in error path of image/create
if obtaining repository IP fails, json object for client is leaked, move it
after query to infer repository IP.
2024-08-30 13:04:34 +02:00
OpenGnSys Support Team 8c37aa3688 rest: adapt cache/fetch to support repository with multiple IP adddress
take repository_id as json attributo, then infer repository IP from client.
2024-08-30 13:03:22 +02:00
OpenGnSys Support Team d8af32cbcb rest: adapt restore/image to support repository with multiple IP adddress
take repository_id as json attributo, then infer repository IP from client.
2024-08-30 13:01:54 +02:00
OpenGnSys Support Team cd9650108a src: infer server IP when it has more than one IP address
infer server IP for a given client. User does not have to attach a client to
a given server IP anymore through ordenadores.identorno.

this simplifies previous work to allow a server to have more than one IP address

a0a3470682 ('#1074 rest: set_mode: add support for different ogserver addresses')
44745a3f22 ('rest: add POST client/server method')

POST /client/server is removed, it is only used by ogcli and explicit association
between server and client is not required.

server_id json attribute is also removed in GET client/info.
2024-08-28 15:28:47 +02:00
OpenGnSys Support Team 9ee9d66b7c schema: incorrect error log
fix incorrect error report when updating table.
2024-08-28 13:01:11 +02:00
OpenGnSys Support Team 5eba9f4e1b rest: allow repository to have more than one IP address
Repository can have more than one single IP address.

* Add alias field to database to represent the extra IPs that are attached to
  the repository, update schema and add version 9.
* Use og_dbi_get_repository_ip() to infer the repository IP address.
* Add helper functions (src/repo.c) to build a list of repositories and update
  rest API to use it.
2024-08-21 21:58:48 +02:00
Alejandro Sirgo Rica 4e0f9aac9e rest: simplify SQL in set_client_mode
Build boot file parameters in C and simplify SQL statement that
obtains the client configuration from the database.
2024-08-21 21:33:51 +02:00
OpenGnSys Support Team 1d64b21241 rest: use json integer for repository id instead of string
use integer not string, it breaks backwards compatibility.
2024-08-21 21:33:46 +02:00
Alejandro Sirgo Rica d383ff1c1a rest: add cache/fetch
Add POST cache/fetch request to request download of images in
the client's cache.

Resquest payload structure:
{
    'clients': ['10.141.10.21', '10.141.10.22']
    'image': 'windows.img'
    'type': 'TIPTORRENT'
    'repository': '12.141.10.2'
}

The clients listed in the 'clients' field will receive a
cache/fetch POST request with the payload received by the server
without the 'clients' field.
The clients respond with the contents of their cache so the server
can update the database.
2024-08-09 10:47:18 +02:00
OpenGnSys Support Team 6bc71b201d rest: use JSON_ENSURE_ASCII in json dumps
escape non-ascii characters in json otherwise ogclient len() and Content-Length:
report a mismatch when utf-8 representation is used in any of the string fields.
For consistency, enable this flag too for json dumps that are consumed by ogcp
and ogcli.
2024-08-06 16:50:55 +02:00
OpenGnSys Support Team a675dbb083 client: split revision query when updating image information
a simple cleanup to help diagnose problems, not behaviour change expected.
2024-07-19 14:23:30 +02:00
OpenGnSys Support Team a67c0d3d57 dbi: use repository_id field if front-end offers it
If repository_id json field is provided, use it to narrow down the image lookup.

After this, search use this same repository ID to fetch the IP address.

Old web console may not provide repository_id, infer it from the image name.

Always set image id field for creation and update.
2024-07-19 14:20:16 +02:00
Alejandro Sirgo Rica 075725b1fe rest: delete pxe files on client delete and update
call og_delete_tftpboot_file for both pxe configuration files
in client/delete and client/update.
Delete files for client/update only if the MAC changes.
2024-07-18 09:26:11 +02:00
Alejandro Sirgo Rica 8a23579cab rest: update client ip in /client/update
Use idordenador as discriminator value instead of the client's ip.
Update client ip if the new ip is not used by other clients.
2024-07-18 09:26:11 +02:00
OpenGnSys Support Team e9a8f467f1 rest: add GET,POST /image/restrict
Allow to restrict image to scope:

 POST /image/restrict
 { "image" : 49, "scopes" : [ 1,3 ] }

 response: 200 OK

This restricts image with ID 49 to scopes 1 and 3.

You can also fetch the current list of restrictions:

 GET /image/restrict
 { "image" : 49 }

 response: 200 OK

 { "image" : 49, "scopes" : [ 1,3 ] }

Existing limitations in this interface:

 - Only restriction of image to center is possible at this moment.
 - This is only used by ogCP to validate if this is possible, no existing code
   in the ogserver uses this to restrict POST image/restore.

This is a usability feature.
2024-07-17 17:15:27 +02:00
Alejandro Sirgo Rica e62a55ae37 src: port setclientmode code to C
Implement most of the code from the script setclientmode into
ogServer to improve error handling and logs.

Use a reduced version legacy script to replace the contents of
the base PXE template. This file is now called updategrubprofile.
Add updategrubprofile to the new extras/ folder.

Create BIOS and UEFI files for PXE boot configuration. Each file
is located in a different directory and requires a different name
pattern.

-BIOS-
File format:
/opt/opengnsys/tftpboot/menu.lst/01-XX-XX-XX-XX-XX-XX

Template dir:
/opt/opengnsys/tftpboot/menu.lst/templates/

-UEFI-
File format:
/opt/opengnsys/tftpboot/grub/01-xx:xx:xx:xx:xx:xx

Template dir:
/opt/opengnsys/tftpboot/grub/templates/
2024-07-16 13:44:40 +02:00
Alejandro Sirgo Rica df52acba04 src: add str_toupper and str_tolower functions
Add auxiliar string case change functions.
2024-07-15 16:36:45 +02:00
Alejandro Sirgo Rica 59ccaaebf6 rest: replace strcat with strncat in og_set_client_mode
Improve string handling security by replacing strcat with the
more secure strncat.
2024-07-15 14:25:22 +02:00
Alejandro Sirgo Rica d6284b6138 rest: add dbi query checks in og_set_client_mode
Check if the the execution of the main SQL query is not
successfull.
2024-07-15 14:25:22 +02:00
Alejandro Sirgo Rica 3bfdf6b477 rest: add default value to og_set_client_mode vga field
Define a default value of 788 for the vga field to assign the
value from ogServer instead of modifying it through the legacy
script setclientmode.
2024-07-15 14:23:06 +02:00
OpenGnSys Support Team 88330288e6 client: fortify check for mandatory cmd json field in shell/output
Revisit 6cbe69e89e ("rest: add cmd to shell/output") to reject response with
no 'cmd' field, otherwise strdup() crashes when dealing with NULL string.

Set retcode to zero, otherwise this value remains uninitialized if no retcode
json field is provided by the client.
2024-07-15 11:56:26 +02:00
OpenGnSys Support Team 3f02d64104 rest: add checksum to GET /images
Add a new checksum attribute to GET /images, extend database to add a new
checksum field to images table.
2024-07-05 13:32:31 +02:00
OpenGnSys Support Team 23bd5f1a07 rest: create new shell folder automagically
If folder does not exist, create it.

This makes it easier to transition from existing deployments.
2024-06-28 15:29:24 +02:00
Alejandro Sirgo Rica 814daceeee rest: enable shell/output as a GET request
shell/output does not modify state, add check to consider GET
shell/output a valid request.

Keep POST shell/output compatibility.
2024-06-25 13:50:15 +02:00
Alejandro Sirgo Rica c0ca4b8cbc rest: add shell/list
Add GET shell/list request to obtain the list of scripts available
in /opt/opengnsys/client/shell

Resquest payload structure: no paylaod

Response's structure:
{
    'scripts': ['script', 'script2']
}
2024-06-25 13:50:10 +02:00
OpenGnSys Support Team 51dd1c4b1a rest: shell/output does not fail if command was never run
skip including client in the listing if command never run or it did not finished
yet.
2024-06-21 15:23:27 +02:00
OpenGnSys Support Team 6a65b72114 rest: add timestamp to shell/output
Provide a timestamp that tells when the command output from client was received.
2024-06-21 15:12:39 +02:00
OpenGnSys Support Team 6cbe69e89e rest: add cmd to shell/output
Add "cmd" field to json that provides the original command string, so it is
provided with the output and the return code.
2024-06-21 15:05:26 +02:00
OpenGnSys Support Team 12d5caf6a6 rest: add retcode to shell/output
Add retcode field to specify return code of shell invocation.
2024-06-21 14:12:02 +02:00
Alejandro Sirgo Rica 98fab52b74 rest: fix memory leak in folder/delete
Free dbi result after use in og_delete_computer_folder().
2024-06-18 11:22:21 +02:00
OpenGnSys Support Team 86beb111d4 rest: Add /client/move
Add URI to allow a POST request to move clients to a new room/folder.

Request POST /client/move:

    {
      "clients": [
        "192.168.56.11"
      ],
      "room": 10,
      "folder_id": 0,
    }

If folder_id is zero then that means computer is not stored in a folder.
2024-06-17 13:35:19 +02:00
Alejandro Sirgo Rica 5103a2eacd rest: allow the same center name in center update
Exclude centerid of the center we update from the search of
centers with the same name.
Report when other center have the same name as the one in the
payload of the update request.
2024-06-12 12:44:11 +02:00
Alejandro Sirgo Rica cdc339659f rest: validate mac in client/update
Check if the requested new MAC exists in another client and refuse
update request if that's the case.
2024-06-12 11:42:33 +02:00
Alejandro Sirgo Rica c4d71193a5 rest: fix memory leak in client/update
Free dbi result after use in og_cmd_post_client_update().
2024-06-12 11:42:33 +02:00
OpenGnSys Support Team 9891276246 rest: Add /center/info
Add URI to allow a GET request to obtain info about a center (name, id
and comment of the center as of now).

To use this uri, simply send a GET request with a json containing the id
of the center whose info needs to be consulted:

curl -X GET -H "Authorization: $API_KEY" http://127.0.0.1:8888/center/info -d '{"id":1}'

based on work from Javier Hernandez.
2024-06-12 11:38:08 +02:00
Alejandro Sirgo Rica 51f275a867 client: update cache after image/restore
Update the database with the new cache contents sent by the
client after an image/restore operation.

Resquest response structure:
{
    ...
    'cache': [
        {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
        {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'}
    ]
    ...
}

Parse the contents of the 'cache' field to update the data of
the client's cache in the database.
This is one of the required changes the make cache/list coherent
and prevent cache desync between the server and clients.
2024-05-30 11:31:42 +02:00
Alejandro Sirgo Rica 111f077d17 rest: add cache/delete
Add POST cache/delete request to request deletion of images in
the client's cache.

Resquest payload structure:
{
    'clients': ['10.141.10.21', '10.141.10.22']
    'images': ['windows.img', 'linux.img']
}

The clients listed in the 'clients' field will receive a
cache/delete POST request with the 'clients' field removed and
only containing 'images' from the payload received by the server.
Each client will try to delete as many images as available in
their cache from the list of files in 'images'.
The clients will give response with the contents of the cache so
the server can update the database.
2024-05-30 11:16:25 +02:00
Alejandro Sirgo Rica 6666aba8b7 rest: add cache/list
Add GET cache/list request to obtain information about the client's
cache.

Resquest payload structure:
{
    'clients': ['10.141.10.21', '10.141.10.22']
}

Response's structure:
{
    'clients': [
        {
            'ip': '10.141.10.21',
            'cache_size': 2894572304857,
            'images': [
                {name:'img1', size: 87283902343, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
                {name:'img2', size: 894572304857, checksum: '3eb22f888f88a55ad954f55644e1192e'}
            ]
        },
        {
            'ip': '10.141.10.22',
            'cache_size': 49872839023434,
            'images': [
                {name:'img2', size: 894572304857, checksum: '3eb22f888f88a55ad954f55644e1192e'}
            ]
        }
    ]
}

Both 'cache_size' and the values in 'image_sizes' are provided
as bytes.

If a client has no cache partition the payload will include it as:
...
	{
            'ip': '10.141.10.22',
            'cache_size': 0,
            'images': []
        }
...
2024-05-30 11:16:20 +02:00
Alejandro Sirgo Rica efb5f27585 client: store image cache data in database
Parse the 'cache' field of the refresh payload sent by the clients.

Cache field structure in the payload:
{
    ...
    'cache': [
        {'name': 'windows.img', 'size': 2432370213, checksum: '5d4dcc677bc19f40a647d0002f4ade90'},
        {'name': 'linux.img', 'size': 243234534213, checksum: '3eb22f888f88a55ad954f55644e1192e'},
    ]
    ...
}

Store that data in the 'cache' table of the  database so it can
be obtained later on.
The table contains the following fields:
- clientid: the numeric identifier of the client.
- imagename: name of the image in cache.
- size: size in bytes of the image in cache.
- checksum: checksum of the image in cache.
2024-05-30 11:16:13 +02:00
OpenGnSys Support Team a3af24d94a src: replace old function to update image information
replace old legacy code, no functional changes are intended.
2024-05-29 17:19:20 +02:00
OpenGnSys Support Team 7f8d874338 schema: add cache table to store image files in client
Add a new table to store the image file that are stores in the client cache.
Use bigint to store file size in bytes.
2024-05-29 17:19:18 +02:00
OpenGnSys Support Team b63bbfd832 rest: extend GET /session to support for more than one client
GET /session only supports for one single client IP address, extend
it to remove this artificial limit.

Add "clients" json attribute which is an alias of the existing "client"
for consistency with other existing endpoints.
2024-05-14 18:02:01 +02:00
Alejandro Sirgo Rica 5ea25a11b2 rest: delete images that belong to deleted centers
When og_cmd_post_center_delete is invoked by the REST API the
images that contain the same id as the center being deleted are
also deleted.

Move the image deletion functionality into its own function called
og_delete_image to prevent code duplication as it is now required
in og_cmd_post_center_delete and og_cmd_delete_image.
2024-02-19 12:46:51 +01:00
OpenGnSys Support Team abfe81c1fa rest: remove .full.sum file when image is removed
Remove .full.sum file too when user requests to remove an image.
2024-02-15 13:12:25 +01:00
Alejandro Sirgo Rica 892f049135 rest: allow room update keeping the same name
Fix the code tha checked the existence of other rooms with the same
name in the center but only check the rooms other than the one being
updated.
2024-02-13 12:14:24 +01:00
Javier Hernandez e3a9814167 rest: room in folder can't be updated
change code to make possible to update rooms that are inside folders
2024-02-08 10:03:39 +01:00
Javier Hernandez 16ca353576 rest: Add uri to update folder name
Add support to update computer folder name

Add support to update room folder name
2024-02-05 11:34:44 +01:00
Javier Hernandez 5b75df049e rest: Add uri to update room
Add uri to allow the change of a room's properties, such as 'name',
'gateway' or 'netmask'

Reject update if new name is already being used by any room of the
center where the room is located
2024-02-01 11:25:34 +01:00
Javier Hernandez 5e2b562330 rest: Add uri to update center
Add uri to allow the change of a center's properties such as 'name' or
'comment'

Refuse to update center's name if that name is already in use by another
center. This is because a center should not share name with another one.
2024-02-01 11:23:54 +01:00
Javier Hernandez 128bba364a rest: Check no client owns provided mac address
Reply with an error if user tries to create a client with a mac that is
already owned by another client

Write to syslog info about the client that owns the mac: client's name,
ip, mac address and room
2024-01-16 13:30:27 +01:00
Javier Hernandez 149d1af21a rest: Change room field 'group' to 'folder_id'
Change the name of variable 'group', in og_group struct, to 'folder_id'.

Change payload field to 'folder_id'

These changes are to make code easier to understand.
2024-01-11 13:35:41 +01:00
OpenGnSys Support Team 41ce640033 rest: update og_uri_handle with new folder commands
Add folder/add and folder/delete, otherwise logging shows "unknown"
2024-01-11 12:14:43 +01:00
Javier Hernandez 529d83b19e rest: Support adding clients inside folder
Add support for adding clients that are inside a folder.

Add new folder_id field, if zero it means computer is not in a folder.
2024-01-11 12:10:04 +01:00
Javier Hernandez bc787a9a68 rest: Add support for deleting folders
Add support for deleting folders containing rooms. Delete rooms that are
in folder.
Computers inside rooms are automatically deleted in cascade when room is
deleted (implemented in sql)

Add support for deleting folders containing clients. Delete clients that are
in folder.
2024-01-10 13:06:47 +01:00
Javier Hernandez 1fb425694c rest: Add support for adding folders
Add support for adding computer folders and room folders.
2024-01-09 12:57:28 +01:00
Javier Hernandez 28ab2aa9b0 rest: Use mask to differentiate folder types
marker tells us if this folder to group rooms or computers.

this is a woraround because the database has two tables to store room and
computer folders, there is no unique id.

the tables cannot be merged yet because of the legacy web console, to overcome
this limitation, add a marker that provides a unique id to differentiate room
and computer folder.

this assumes only 65535 room folders are possible because the marker is
0x000010000 (65536)
2024-01-08 13:08:55 +01:00
OpenGnSys Support Team a7727e40ea rest: allow to set boot mode in client/update
Allow to specify .boot field and call set_client_mode() to update
the boot mode.
2023-12-21 12:18:40 +01:00
OpenGnSys Support Team 3b3fc32999 rest: missing POST /image/update in syslog
Update it so it shows in syslog, instead of unknown:

Dec 19 18:31:18 ogserver ogserver[1133]: 127.0.0.1:36538 POST /unknown clients=150.128.34.25
2023-12-21 11:58:48 +01:00
OpenGnSys Support Team cdff83d091 rest: cannot fail with image/create if it exists
legacy web console uses this REST API to refresh an existing image.

Relax check not to break things.
2023-12-19 13:34:32 +01:00
OpenGnSys Support Team f64b1c2767 rest: add POST image/update
add explicit API to update an image
2023-12-19 12:42:32 +01:00
OpenGnSys Support Team 7292c30659 rest: no assumption on description field in create/image
do not use description field to decide if this is a new image or an update.

add og_dbi_get_image() to check if the image exists. If it is not found, then
add new image entry to database.

update og_dbi_add_image() to update the image.id field.
2023-12-19 12:20:34 +01:00
OpenGnSys Support Team bad351b1d7 rest: log failed to delete entry from table
add logging to report failure to remove entry.
2023-12-18 12:20:52 +01:00
OpenGnSys Support Team b100c570a0 rest: do not exit() if execlp() fails
execlp() should not ever fail, but if it ever happens, return -1 instead of
exitting this daemon.
2023-12-15 11:12:49 +01:00
OpenGnSys Support Team 5e8e1c6467 rest: do not report error when removing image file
this file might be in a different repository, this is best effort.
2023-12-15 11:12:46 +01:00
OpenGnSys Support Team ee09056908 client: parse permissions and lastupdate in /image/create response from client
Use permissions and last update from client.

update src/schema.c to add a new database version.
2023-12-12 17:02:44 +01:00
OpenGnSys Support Team e3b8f3d2f9 client: stub to parse size field in /image/create response from client
Still incomplete, src/schema.c needs to be update to add a new field to the
'imagenes' table in the database.
2023-12-12 11:30:31 +01:00
OpenGnSys Support Team 721ab28fdf rest: expose description in GET /images
add .description field to json.
2023-12-12 10:23:46 +01:00
OpenGnSys Support Team 16d7a18441 dbi: og_computer_get_info() uses netmask field in 'room' table
netmask field in 'computers' table is never used, but ogcli still
displays it through:

 # ogcli list client --client-ip 1.2.3.4

and it shows '0' or incorrect netmask (ogCP hardcodes this field).

Update SQL query to fetch the room.netmask instead.
2023-11-30 20:58:04 +01:00
OpenGnSys Support Team edac32bb98 rest: add GET /room/info
Add GET /room/info to obtain room information, this includes the name,
gateway and netmask.

curl -X GET -H "Authorization: $API_KEY" http://127.0.0.1:8888/room/info -d '{ "id" : 1 }
2023-11-30 20:20:07 +01:00
OpenGnSys Support Team 1ce5624032 rest: fix client/info maintenance field
Use computer.maintenance instead of computer.name.

Fixes: af47a082ad ("#1004 Add GET /client/info HTTP REST method")
2023-11-30 10:04:19 +01:00
Javier Hernandez 5ff92a826e rest: add POST repository/update
Add POST repository/update request to update repository. Repository is
identified by its id, that is provided in the payload.

This can be tested with curl like:
curl -X POST -H "Authorization: $API_KEY"
http://127.0.0.1:8888/repository/update -d '{ "repo_id" : 16, "center" :
3, "name": "newName", "ip":"127.0.0.1" }'
2023-11-24 11:02:52 +01:00
OpenGnSys Support Team 0ab4721714 core: remove useless log when ogClient connects to server
The following log message is display for each ogClient that is connected to
the ogServer:

 Sent refresh to: 192.168.2.200

this tells the ogClient to send his current partition table when the connection
between ogServer <-> ogClient starts. This is not very useful, remove it.
2023-11-23 22:08:49 +01:00
OpenGnSys Support Team 56e4a01af0 rest: revisit logging
Skip logging for the following REST API invocations:

- GET /scopes
- GET /clients
- POST /clients (for legacy web console only)

The web front-end generated very frequent requests for this.

Update logging format to:

 127.0.0.1:54637 POST /wol clients=192.168.2.130

to include client IP address.
2023-11-23 22:08:49 +01:00
OpenGnSys Support Team f8eca04841 rest: fix WoL handling
Valgrind reports a memleak because og_cmd_wol() overrides params->ips_array,
then it releases params->ips_array but og_cmd_free() already does this.

Use a new_params structure to store the IPs that has been filtered through the
database query and check that buffer overrun does not happen.

Fixes: e4cb91b5f6 ("#990 wol: migrate mac and netmask query to ogServer")
2023-11-23 22:08:41 +01:00
OpenGnSys Support Team 2140012535 rest: set center in /repository/add
Set it to 1, ogCP and ogCLI never use this, and this REST API is never
used by the old web console.
2023-11-23 12:48:08 +01:00
OpenGnSys Support Team 9c69f112de rest: remove dead code in /repository/add
Remove unused code in this request and do not assume center id.
2023-11-23 10:19:36 +01:00
Javier Hernandez fc0f19fe30 rest: add /client/update
Add POST client/update request to update client, this is identified by its ip
address, with the data provided in the payload.
2023-11-23 10:16:09 +01:00
OpenGnSys Support Team e7c4e16602 main: use etc folder instead of cfg to store .json configuration file
debian packages use etc, so update source code to use this folder too.
2023-11-14 11:26:19 +01:00
OpenGnSys Support Team 944fef7b04 core: restrict rest API to 127.0.0.1
Until there is TLS support for this, frontend and ogserver needs to be in the
same box by now.
2023-11-14 11:25:42 +01:00
Javier Hernandez 485bf92216 rest: Check valid bootmode
ogserver will check that the user provided a valid bootmode. It will go
through the list of all available boot modes and check that the bootmode
provided by the user is in said list; otherwise, it will not set it.
2023-11-06 11:42:50 +01:00
Javier Hernandez fa57af3c13 rest: extract og_get_boot_modes from og_cmd_get_modes()
og_cmd_get_modes() now uses a helper function that returns a list of all boot
modes, this is for a new API REST validate that boot mode is set to something
that exists in a follow up patch.
2023-11-06 10:11:44 +01:00
Javier Hernandez 4c1995d28f rest: Add error check to delete commands
Every time a delete operation is performed it needs to be checked that a row
has been deleted. If no rows are deleted, then ogserver reports an error.

Previous to this change, it only checks that the connection to db
was succesful. For example, previously, if a user deletes a client that did
not exist, ogserver responds with no errors.

After this change, it responds with an error.

This error check has been implemented when deleting:
- A client
- An image
- A center
- A repo
- A room
- A server
2023-11-06 10:11:44 +01:00
OpenGnSys Support Team d3ad9e13e0 rest: use method not found instead of bad request for /clients requests
/clients says bad request for unsupported method, use correct error
report.
2023-10-19 10:46:23 +02:00
Jose M. Guisado 4fca62a8ee rest: extend /repository/add
POST /repository/add checks validity of the repository ip address
specified in the request payload.

/repository/add can optionally receive a center id parameter inside its
request payload. For backward compatibility, the default center id (1)
is used if no center is received inside the request payload.

POST /repository/add returns a JSON response payload containing relevant
fields from the inserted repository.

$ curl 	-D-
	\ -X POST
	\ -H "Authorization: a0e9ab768cbe93dab5b1998e952bcdb7"
  	\ --json '{"name": "helloworld", "ip": "192.168.21.21a", "center": 2}'
	\ localhost:8888/repository/add
HTTP/1.1 400 Bad Request
Content-Length: 0

$ curl 	-D-
	\ -X POST
	\ -H "Authorization: a0e9ab768cbe93dab5b1998e952bcdb7"
  	\ --json '{"name": "helloworld", "ip": "192.168.21.21a", "center": 2}'
	\ localhost:8888/repository/add
HTTP/1.1 200 OK
Content-Length: 54

{"id": 7, "ip": "192.168.21.21", "name": "helloworld"}
2023-10-11 10:28:22 +02:00
Jose M. Guisado 6f6372d0e0 src: move og_repository to dbi.h
Move struct og_repository to dbi.h and use field max length for name and
ip.

Use og_json_parse_string_copy instead of og_json_parse_string to check
maximum length against the request payload.

Fixes: 86ccc3c2e8 ("#915 Add POST /repository/add")
2023-10-10 18:09:19 +02:00
Jose M. Guisado dc68d0f135 rest: add POST /client/repo
Set a client repository in the OpenGnsys database using the endpoint
POST /client/repo.

Expects "clients" and "id" parameters in the request payload.

    POST /client/repo
    {
      "clients": [...],
      "id": "2"
    }

    Response:
    200 OK
2023-10-10 18:09:19 +02:00
Jose M. Guisado 51b70966fc rest: fix sql typo in og_set_client_mode
Replace invalid variable @ipserveradm by @serverip in the SQL query of
og_set_client_mode.

@ipserveradm is a typo as this variable does not exists. This ends up
feeding a string to the legacy setclientmode code and crashes.

This bug also caused problems when adding clients via ogCP or any other
command that required running the set client mode logic.

Fixes: e080fd5526
(rest: add "server=" boot param in set client mode)
2023-09-29 14:53:12 +02:00
Jose M. Guisado 35a3b0a1a3 schema: fix bug when og_dbi_open cannot open a connection
When og_dbi_open cannot open a connection during schema update, then
ogServer crashes.

og_dbi_open returns NULL when it cannot open a DB connection.

Do not use dbi variable if og_dbi_open is unable to open a DB
connection.

Add syslog message when database schema couldn't be updated.
2023-09-28 10:07:07 +02:00
Jose M. Guisado e080fd5526 rest: add "server=" boot param in set client mode
Store a client associated ogserver ip address in "server=" boot param.

Use this boot param to lookup for network-specific ogClient
configuration when launching ogClient during ogLive booting process.
2023-08-28 12:29:33 +02:00
Jose M. Guisado 18eb19ab42 rest: return server_id in GET /client/info
struct og_computer holds the value of "identorno" column inside the
server_id field.

Extend GET /client/info payload with server_id. This way clients can
get the associated server id when requesting client information.

{
...
"server_id": 1,
}
2023-08-23 15:35:34 +02:00
Jose M. Guisado 4cc2ca61fd dbi: add server_id to og_computer struct
Extend og_dbi_get_computer_info to fetch "identorno" column from
"ordenadores". "identorno" holds the row id value from the "entornos"
table (valid ogserver addresses).

Extend og_computer struct with a new field: "server_id" that will store
the value of "identorno" column fetched from the database.
2023-08-23 15:34:44 +02:00
Jose M. Guisado 44745a3f22 rest: add POST client/server method
Enable modification of the associated ogserver of a given client.

This API is exposed via the POST /client/server endpoint and expects a
JSON payload with an array of clients ("client":[]) and the "id" of the
ogserver ("identorno" column value inside the "entornos" table)

For example:

>>>
POST /client/server
{
  "client": [
    "10.141.10.100",
    "10.141.10.101",
    "10.141.10.104",
    "10.141.10.102"
  ],
  "id": "6"
}
<<<
HTTP/1.1 200 OK

If the ogserver id does not exist the foreign key constraint (ON UPDATE
RESTRICT) inside the "ordenadores" table will cancel the operation and
the server will reply with 400 Bad Request.

>>>
POST /client/server
{
  "client": [
    "10.141.10.100",
    "10.141.10.101",
    "10.141.10.104",
    "10.141.10.102"
  ],
  "id": "666"
}
<<<
HTTP/1.1 400 Bad Request

The OpenGnsys database stores different ip addresses for the ogServer
inside the "entornos" table. This table is related to the "ordenadores"
table using a foreign key on the "identorno" column.

i.e: Clients in the "ordenadores" table associate to an specific server
in the database using the "identorno" column (from "entornos" table).
2023-08-23 13:17:03 +02:00
Jose M. Guisado a67c1088ef rest: add DELETE operation to /server endpoint
Expose deletion of rows from "entornos" table via the /server
endpoint using the DELETE http request method.

The expected payload is the server id as a string. For example:

>>>
DELETE /server
{ "id": "4" }

<<<
200 OK

If the specified server is currently associated with any computer
("ordenadores" table) the foreign key contraint (ON DELETE RESTRICT)
will avoid the deletion and the server responds with
400 Bad Request.

>>>
DELETE /server
{ "id": "1" }

<<<
400 Bad Request
2023-08-23 11:39:19 +02:00
Jose M. Guisado b2c8c9d9ab rest: support DELETE HTTP request method
Reuse endpoints in order to add deletion operation such as "/server".
This way there is no need to declare a different if/else block in order
to parse a new URI for the new "*/delete" endpoint.

Current deletion operations are implemented using a different endpoint
name such as "/client/delete", "/center/delete" with POST request method.

Endpoints using "/delete" suffix may not be removed for backwards
compatibility. Adding HTTP method DELETE support for endpoints such as
"/center" or "/client" can use the already existing *_post_delete*
functions.
2023-08-23 11:39:19 +02:00
Jose M. Guisado 351069b591 rest: add /server GET and POST methods
DB stores different ogserver addresses in the "entornos" table.

Expose addition and deletion operations for the "entornos" table using
the /server endpoint.

GET /server returns a list of rows from the "entornos" table including
the "identorno" and "ipserveradm" columns. For example:

>>>
GET /server

<<<
200 OK
...
{
  "servers": [
    {
      "id": 1,
      "address": "10.141.10.1"
    }
  ]
}

POST /server inserts into the "entornos" table and returns the
id ("identorno") of the new row.

>>>
POST /server
{
  "address": "192.168.2.240"
}

<<<
200 OK
...
{
  "id": 2
}

If the server address already exists the response is 400 Bad Request
with no payload.

>>>
POST /server
{
  "address": "192.168.2.240"
}

<<<
400 Bad Request
2023-08-23 11:39:10 +02:00
Jose M. Guisado 74b6e3ec72 rest: add optional param "backup" for create_image
This parameter is used by ogServer to instruct target client if image
backup should be performed before image creation.

This parameter is optional to preserve backward compatibility with
webconsole (legacy web client) and avoid the need of updating any legacy
client.

Default is true if the REST request is missing this parameter.
2023-07-07 10:04:36 +02:00
Jose M. Guisado 3de8c25e4e core: log payload if sequences do not match
We need to inspect the received payload if any error is raised related
to the X-Sequence header. Not just when a malformed X-Sequence header is
detected.

Fixes: d2c19ef13d ("core: add X-Sequence header support")
2023-07-03 09:17:53 +02:00
Jose M. Guisado d2c19ef13d core: add X-Sequence header support
Add non-standard HTTP header "X-Sequence" to the header section of
requests (og_send_request) sent to a connected client.

Define a starting sequence number when creating a new instance of struct
og_client inside og_server_accept_cb. This sequence number is
incremented by one for each outgoing request from ogServer.

This sequence number is checked when receiving a response from a
connected client, if they do not match the connection is dropped.

Use sequence 0 for out-of-band commands (reboot, poweroff, stop). Any
client response with header "X-Sequence: 0" bypasses sequence check.
2023-06-13 14:04:54 +02:00