From 4e0f9aac9e2883a992469f5b2b231ecb14b30183 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Wed, 7 Aug 2024 11:16:09 +0200 Subject: 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. --- src/rest.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 121 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 20b241e..ad7573b 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1502,41 +1502,54 @@ static int og_create_boot_file(unsigned int boot_type, const char *mac, return 0; } -static int og_set_client_mode(struct og_dbi *dbi, const char *mac, - const char *mode) +struct og_boot_mode_params { + const char *ntp; + const char *dns; + const char *proxy; + const char *ip; + const char *ipserveradm; + const char *nombreaula; + const char *repoip; + const char *oglivedir; + const char *hardprofile; + const char *router; + const char *netmask; + const char *nombreordenador; + const char *netiface; + const char *directorio; + const char *resolucion; + int is_prof; + int has_unit; +}; + +static int og_get_client_mode_params(struct og_dbi *dbi, const char *mac, char *params, size_t params_size) { - char params[4096] = "\0"; + struct og_boot_mode_params boot_params = {}; + const char *res_prefix = " vga="; + const char *res_value = "788"; + char *lang = getenv("LANG"); const char *msglog; dbi_result result; - unsigned int i; - - if (!og_boot_mode_is_valid(mode)) { - syslog(LOG_ERR, "invalid boot mode in client (%s:%d)\n", - __FILE__, __LINE__); - return -1; - } result = dbi_conn_queryf(dbi->conn, - "SELECT ' LANG=%s', " - "' ip=', CONCAT_WS(':', ordenadores.ip, (@serverip:=entornos.ipserveradm), 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'), " - "' server=', @serverip, " - "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 ' vga=788' " - "WHEN menus.resolucion <= '999' THEN CONCAT(' vga=', menus.resolucion) " - "WHEN menus.resolucion LIKE '%:%' THEN CONCAT(' video=', menus.resolucion) " - "ELSE ' vga=788' END " - + "SELECT " + "ordenadores.ip AS ip, " + "entornos.ipserveradm AS ipserveradm, " + "aulas.router AS router, " + "aulas.netmask AS netmask, " + "ordenadores.nombreordenador AS nombreordenador, " + "ordenadores.netiface AS netiface, " + "REPLACE(TRIM(aulas.nombreaula), ' ', '_') AS nombreaula, " + "IFNULL(repositorios.ip, '') AS repoip, " + "ordenadores.oglivedir AS oglivedir, " + "IF(ordenadores.idordenador = aulas.idordprofesor, 1, 0) AS is_prof, " + "REPLACE(TRIM(IFNULL(perfileshard.descripcion, '')), ' ', '_') AS hardprofile, " + "IFNULL(aulas.ntp, '') AS ntp, " + "IFNULL(aulas.dns, '') AS dns, " + "IFNULL(aulas.proxy, '') AS proxy, " + "IF(entidades.ogunit = 1 AND NOT centros.directorio = '', 1, 0) AS has_unit, " + "centros.directorio AS directorio, " + "IFNULL(menus.resolucion, '') AS resolucion " "FROM ordenadores " "JOIN aulas USING(idaula) " "JOIN centros USING(idcentro) " @@ -1545,8 +1558,7 @@ static int og_set_client_mode(struct og_dbi *dbi, const char *mac, "LEFT JOIN repositorios USING(idrepositorio) " "LEFT JOIN perfileshard USING(idperfilhard) " "LEFT JOIN menus USING(idmenu) " - - "WHERE ordenadores.mac='%s'", getenv("LANG"), mac); + "WHERE ordenadores.mac = '%s'", mac); if (!result) { dbi_conn_error(dbi->conn, &msglog); @@ -1556,21 +1568,91 @@ static int og_set_client_mode(struct og_dbi *dbi, const char *mac, return -1; } - if (dbi_result_get_numrows(result) != 1) { + if (!dbi_result_next_row(result)) { dbi_conn_error(dbi->conn, &msglog); - syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", - __FILE__, __LINE__, msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", // TODO + __func__, __LINE__, msglog); dbi_result_free(result); + og_dbi_close(dbi); return -1; } - dbi_result_next_row(result); - for (i = 1; i <= dbi_result_get_numfields(result); ++i) - strncat(params, dbi_result_get_string_idx(result, i), - sizeof(params) - strlen(params) - 1); + boot_params.ip = dbi_result_get_string_copy(result, "ip"); + boot_params.ipserveradm = dbi_result_get_string_copy(result, "ipserveradm"); + boot_params.router = dbi_result_get_string_copy(result, "router"); + boot_params.netmask = dbi_result_get_string_copy(result, "netmask"); + boot_params.nombreordenador = dbi_result_get_string_copy(result, "nombreordenador"); + boot_params.netiface = dbi_result_get_string_copy(result, "netiface"); + boot_params.nombreaula = dbi_result_get_string_copy(result, "nombreaula"); + boot_params.repoip = dbi_result_get_string_copy(result, "repoip"); + boot_params.oglivedir = dbi_result_get_string_copy(result, "oglivedir"); + boot_params.hardprofile = dbi_result_get_string_copy(result, "hardprofile"); + boot_params.ntp = dbi_result_get_string_copy(result, "ntp"); + boot_params.dns = dbi_result_get_string_copy(result, "dns"); + boot_params.proxy = dbi_result_get_string_copy(result, "proxy"); + boot_params.directorio = dbi_result_get_string_copy(result, "directorio"); + boot_params.resolucion = dbi_result_get_string_copy(result, "resolucion"); + boot_params.has_unit = dbi_result_get_uint(result, "has_unit"); + boot_params.is_prof = dbi_result_get_uint(result, "is_prof"); + + if (boot_params.resolucion[0]) { + res_value = boot_params.resolucion; + if (strchr(boot_params.resolucion, ':')) + res_prefix = " video="; + } + + snprintf(params, params_size, + " LANG=%s ip=%s:%s:%s:%s:%s:%s:none group=%s ogrepo=%s oglive=%s oglog=%s ogshare=%s oglivedir=%s ogprof=%s server=%s" + "%s%s%s%s%s%s%s%s%s%s%s%s", + lang, + boot_params.ip, boot_params.ipserveradm, boot_params.router, boot_params.netmask, boot_params.nombreordenador, boot_params.netiface, + boot_params.nombreaula, + boot_params.repoip, + boot_params.ipserveradm, boot_params.ipserveradm, boot_params.ipserveradm, + boot_params.oglivedir, + boot_params.is_prof ? "true" : "false", + boot_params.ipserveradm, + boot_params.hardprofile[0] ? " hardprofile=" : "", boot_params.hardprofile[0] ? boot_params.hardprofile: "", + boot_params.ntp[0] ? " ogntp=" : "", boot_params.ntp[0] ? boot_params.ntp : "", + boot_params.dns[0] ? " ogdns=" : "", boot_params.dns[0] ? boot_params.dns : "", + boot_params.proxy[0] ? " ogproxy=" : "", boot_params.proxy[0] ? boot_params.proxy : "", + boot_params.has_unit ? " ogunit=" : "", boot_params.has_unit ? boot_params.directorio : "", + res_prefix, res_value); dbi_result_free(result); + free((void *)boot_params.ip); + free((void *)boot_params.ipserveradm); + free((void *)boot_params.router); + free((void *)boot_params.netmask); + free((void *)boot_params.nombreordenador); + free((void *)boot_params.netiface); + free((void *)boot_params.nombreaula); + free((void *)boot_params.repoip); + free((void *)boot_params.oglivedir); + free((void *)boot_params.hardprofile); + free((void *)boot_params.ntp); + free((void *)boot_params.dns); + free((void *)boot_params.proxy); + free((void *)boot_params.directorio); + free((void *)boot_params.resolucion); + + return 0; +} + +static int og_set_client_mode(struct og_dbi *dbi, const char *mac, + const char *mode) +{ + char params[4096]; + + if (!og_boot_mode_is_valid(mode)) { + syslog(LOG_ERR, "invalid boot mode in client (%s:%d)\n", + __FILE__, __LINE__); + return -1; + } + + og_get_client_mode_params(dbi, mac, params, sizeof(params)); + if (og_create_boot_file(OG_TFTP_BOOT_BIOS, mac, mode, params) < 0) { syslog(LOG_ERR, "failed to create BIOS boot file (%s:%d)\n", __FILE__, __LINE__); return -1; -- cgit v1.2.3-18-g5258