source: ogServer-Git/src/dbi.c @ 927d42b

Last change on this file since 927d42b was 11d6e84, checked in by OpenGnSys Support Team <soporte-og@…>, 3 years ago

#915 Avoid duplicate db entries in /create/image

/create/image adds an entry to the database for the given partition
image created when payload contains a "description" attribute. This
insertion into the database is lacking a check for duplicates, which are
not supported for the images table.

Add a prior duplicate check before inserting. Exit with -1 code if an
image with the same name is found.

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