source: ogServer-Git/src/dbi.c @ 41ac15d

Last change on this file since 41ac15d was cb7fa56, checked in by OpenGnSys Support Team <soporte-og@…>, 3 years ago

#941 Free dbi_inst when og_dbi_open fails to connect

When trying to open a connection to a database, an instance of
libdbi is created before any connection attempt. If connection is
unsuccessful then the og_dbi struct is freed but not the
libdbi instance member, thus leaking its memory.

Use libdbi dbi_shutdown_r to shutdown libdbi instance member
before freeing og_dbi struct inside og_dbi_open.

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