[48de515] | 1 | /* |
---|
[a7cce8d] | 2 | * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu> |
---|
[48de515] | 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 |
---|
[a7cce8d] | 6 | * Free Software Foundation; either version 3 of the License, or |
---|
| 7 | * (at your option) any later version. |
---|
[48de515] | 8 | */ |
---|
| 9 | |
---|
[3cb98c7] | 10 | #include <syslog.h> |
---|
| 11 | #include <netinet/in.h> |
---|
| 12 | #include <arpa/inet.h> |
---|
| 13 | #include <string.h> |
---|
[2629906] | 14 | #include "dbi.h" |
---|
[cbd9421] | 15 | #include <syslog.h> |
---|
| 16 | #include <string.h> |
---|
[f537daf] | 17 | #include <stdio.h> |
---|
[2629906] | 18 | |
---|
| 19 | struct og_dbi *og_dbi_open(struct og_dbi_config *config) |
---|
| 20 | { |
---|
| 21 | struct og_dbi *dbi; |
---|
| 22 | |
---|
| 23 | dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi)); |
---|
| 24 | if (!dbi) |
---|
| 25 | return NULL; |
---|
| 26 | |
---|
| 27 | dbi_initialize_r(NULL, &dbi->inst); |
---|
| 28 | dbi->conn = dbi_conn_new_r("mysql", dbi->inst); |
---|
| 29 | if (!dbi->conn) { |
---|
| 30 | free(dbi); |
---|
| 31 | return NULL; |
---|
| 32 | } |
---|
| 33 | |
---|
[fe1ce97] | 34 | dbi_conn_set_option(dbi->conn, "host", config->ip); |
---|
[0631b0e] | 35 | dbi_conn_set_option(dbi->conn, "port", config->port); |
---|
[2629906] | 36 | dbi_conn_set_option(dbi->conn, "username", config->user); |
---|
[fe1ce97] | 37 | dbi_conn_set_option(dbi->conn, "password", config->pass); |
---|
| 38 | dbi_conn_set_option(dbi->conn, "dbname", config->name); |
---|
[2629906] | 39 | dbi_conn_set_option(dbi->conn, "encoding", "UTF-8"); |
---|
| 40 | |
---|
| 41 | if (dbi_conn_connect(dbi->conn) < 0) { |
---|
| 42 | free(dbi); |
---|
| 43 | return NULL; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | return dbi; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void og_dbi_close(struct og_dbi *dbi) |
---|
| 50 | { |
---|
| 51 | dbi_conn_close(dbi->conn); |
---|
| 52 | dbi_shutdown_r(dbi->inst); |
---|
| 53 | free(dbi); |
---|
| 54 | } |
---|
[3cb98c7] | 55 | |
---|
[d7e2022] | 56 | int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, |
---|
| 57 | struct in_addr addr) |
---|
[3cb98c7] | 58 | { |
---|
| 59 | const char *msglog; |
---|
| 60 | dbi_result result; |
---|
| 61 | |
---|
| 62 | result = dbi_conn_queryf(dbi->conn, |
---|
| 63 | "SELECT ordenadores.idordenador," |
---|
[cbd9421] | 64 | " ordenadores.nombreordenador," |
---|
| 65 | " ordenadores.numserie," |
---|
| 66 | " ordenadores.ip," |
---|
| 67 | " ordenadores.mac," |
---|
| 68 | " ordenadores.idaula," |
---|
| 69 | " ordenadores.idperfilhard," |
---|
| 70 | " ordenadores.idrepositorio," |
---|
| 71 | " ordenadores.mascara," |
---|
| 72 | " ordenadores.arranque," |
---|
| 73 | " ordenadores.netiface," |
---|
| 74 | " ordenadores.netdriver," |
---|
| 75 | " ordenadores.idproautoexec," |
---|
| 76 | " ordenadores.oglivedir," |
---|
| 77 | " ordenadores.inremotepc," |
---|
| 78 | " ordenadores.maintenance," |
---|
| 79 | " centros.idcentro " |
---|
| 80 | "FROM ordenadores " |
---|
[3cb98c7] | 81 | "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula " |
---|
| 82 | "INNER JOIN centros ON centros.idcentro=aulas.idcentro " |
---|
| 83 | "WHERE ordenadores.ip='%s'", inet_ntoa(addr)); |
---|
| 84 | if (!result) { |
---|
| 85 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 86 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 87 | __func__, __LINE__, msglog); |
---|
| 88 | return -1; |
---|
| 89 | } |
---|
| 90 | if (!dbi_result_next_row(result)) { |
---|
| 91 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
| 92 | __func__, __LINE__); |
---|
| 93 | dbi_result_free(result); |
---|
| 94 | return -1; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | computer->id = dbi_result_get_uint(result, "idordenador"); |
---|
[f537daf] | 98 | snprintf(computer->name, sizeof(computer->name), "%s", |
---|
| 99 | dbi_result_get_string(result, "nombreordenador")); |
---|
| 100 | snprintf(computer->serial_number, sizeof(computer->serial_number), "%s", |
---|
| 101 | dbi_result_get_string(result, "numserie")); |
---|
| 102 | snprintf(computer->ip, sizeof(computer->ip), "%s", |
---|
| 103 | dbi_result_get_string(result, "ip")); |
---|
| 104 | snprintf(computer->mac, sizeof(computer->mac), "%s", |
---|
| 105 | dbi_result_get_string(result, "mac")); |
---|
[3cb98c7] | 106 | computer->room = dbi_result_get_uint(result, "idaula"); |
---|
[4f5c357] | 107 | computer->center = dbi_result_get_uint(result, "idcentro"); |
---|
[cbd9421] | 108 | computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); |
---|
| 109 | computer->repo_id = dbi_result_get_uint(result, "idrepositorio"); |
---|
[f537daf] | 110 | snprintf(computer->netmask, sizeof(computer->netmask), "%s", |
---|
| 111 | dbi_result_get_string(result, "mascara")); |
---|
| 112 | snprintf(computer->boot, sizeof(computer->boot), "%s", |
---|
| 113 | dbi_result_get_string(result, "arranque")); |
---|
| 114 | snprintf(computer->netiface, sizeof(computer->netiface), "%s", |
---|
| 115 | dbi_result_get_string(result, "netiface")); |
---|
| 116 | snprintf(computer->netdriver, sizeof(computer->netdriver), "%s", |
---|
| 117 | dbi_result_get_string(result, "netdriver")); |
---|
[3cb98c7] | 118 | computer->procedure_id = dbi_result_get_uint(result, "idproautoexec"); |
---|
[f537daf] | 119 | snprintf(computer->livedir, sizeof(computer->livedir), "%s", |
---|
| 120 | dbi_result_get_string(result, "oglivedir")); |
---|
[cbd9421] | 121 | computer->remote = dbi_result_get_uint(result, "inremotepc") != 0; |
---|
| 122 | computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0; |
---|
[3cb98c7] | 123 | |
---|
| 124 | dbi_result_free(result); |
---|
| 125 | |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
[d2f20d0] | 128 | |
---|
| 129 | #define OG_UNASSIGNED_SW_ID 0 |
---|
| 130 | #define OG_DEFAULT_REPO_ID 1 |
---|
| 131 | #define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */ |
---|
| 132 | |
---|
| 133 | int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image) |
---|
| 134 | { |
---|
| 135 | const char *msglog; |
---|
| 136 | dbi_result result; |
---|
| 137 | |
---|
| 138 | result = dbi_conn_queryf(dbi->conn, |
---|
[11d6e84] | 139 | "SELECT nombreca FROM imagenes WHERE nombreca = '%s'", |
---|
| 140 | image->name); |
---|
| 141 | if (!result) { |
---|
| 142 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 143 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 144 | __func__, __LINE__, msglog); |
---|
| 145 | return -1; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | if (dbi_result_next_row(result)) { |
---|
| 149 | syslog(LOG_ERR, "image creation attempt with already used image name (%s:%d)\n", |
---|
| 150 | __func__, __LINE__); |
---|
| 151 | dbi_result_free(result); |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | dbi_result_free(result); |
---|
| 155 | |
---|
| 156 | result = dbi_conn_queryf(dbi->conn, |
---|
[d2f20d0] | 157 | "INSERT INTO imagenes (nombreca, " |
---|
| 158 | "descripcion, " |
---|
| 159 | "idperfilsoft, " |
---|
| 160 | "idcentro, " |
---|
| 161 | "comentarios, " |
---|
| 162 | "grupoid, " |
---|
| 163 | "idrepositorio, " |
---|
| 164 | "tipo, " |
---|
| 165 | "ruta) " |
---|
| 166 | "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')", |
---|
| 167 | image->name, image->description, |
---|
| 168 | OG_UNASSIGNED_SW_ID, image->center_id, |
---|
| 169 | image->group_id, OG_DEFAULT_REPO_ID, |
---|
| 170 | OG_IMAGE_DEFAULT_TYPE); |
---|
| 171 | |
---|
| 172 | if (!result) { |
---|
| 173 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 174 | syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n", |
---|
| 175 | __func__, __LINE__, msglog); |
---|
| 176 | return -1; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | dbi_result_free(result); |
---|
| 180 | return dbi_conn_sequence_last(dbi->conn, NULL); |
---|
| 181 | } |
---|