source: ogServer-Git/src/dbi.c @ a0a3470

Last change on this file since a0a3470 was c0573b9, checked in by Javier Sánchez Parra <jsanchez@…>, 3 years ago

#915 Set repository on image creation

Assign to the image the repository indicated in the JSON body instead of
a default one.

  • Property mode set to 100644
File size: 6.5 KB
Line 
1/*
2 * Copyright (C) 2020-2021 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; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
10#include <syslog.h>
11#include <netinet/in.h>
12#include <arpa/inet.h>
13#include <string.h>
14#include "dbi.h"
15#include <syslog.h>
16#include <string.h>
17#include <stdio.h>
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
34        dbi_conn_set_option(dbi->conn, "host", config->ip);
35        dbi_conn_set_option(dbi->conn, "port", config->port);
36        dbi_conn_set_option(dbi->conn, "username", config->user);
37        dbi_conn_set_option(dbi->conn, "password", config->pass);
38        dbi_conn_set_option(dbi->conn, "dbname", config->name);
39        dbi_conn_set_option(dbi->conn, "encoding", "UTF-8");
40
41        if (dbi_conn_connect(dbi->conn) < 0) {
42                dbi_shutdown_r(dbi->inst);
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}
56
57int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
58                             struct in_addr addr)
59{
60        const char *msglog;
61        dbi_result result;
62
63        result = dbi_conn_queryf(dbi->conn,
64                                 "SELECT ordenadores.idordenador,"
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 "
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");
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"));
107        computer->room = dbi_result_get_uint(result, "idaula");
108        computer->center = dbi_result_get_uint(result, "idcentro");
109        computer->hardware_id = dbi_result_get_uint(result, "idperfilhard");
110        computer->repo_id = dbi_result_get_uint(result, "idrepositorio");
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"));
119        computer->procedure_id = dbi_result_get_uint(result, "idproautoexec");
120        snprintf(computer->livedir, sizeof(computer->livedir), "%s",
121                 dbi_result_get_string(result, "oglivedir"));
122        computer->remote = dbi_result_get_uint(result, "inremotepc") != 0;
123        computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0;
124
125        dbi_result_free(result);
126
127        return 0;
128}
129
130const int og_dbi_get_repository(const struct og_dbi *dbi, const char *repo_ip)
131{
132        const char *msglog;
133        dbi_result result;
134        int repo_id;
135
136        /* database can store duplicated repositories, limit query to return
137         * only one */
138        result = dbi_conn_queryf(dbi->conn,
139                                 "SELECT idrepositorio FROM repositorios "
140                                 "WHERE ip = '%s' LIMIT 1",
141                                 repo_ip);
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                dbi_conn_error(dbi->conn, &msglog);
151                syslog(LOG_ERR,
152                       "software profile does not exist in database (%s:%d) %s\n",
153                       __func__, __LINE__, msglog);
154                dbi_result_free(result);
155                return -1;
156        }
157
158        repo_id = dbi_result_get_int(result, "idrepositorio");
159        dbi_result_free(result);
160
161        return repo_id;
162}
163
164#define OG_UNASSIGNED_SW_ID 0
165#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */
166
167int og_dbi_add_image(struct og_dbi *dbi, const struct og_image *image)
168{
169        const char *msglog;
170        dbi_result result;
171        int repo_id;
172
173        repo_id = og_dbi_get_repository(dbi, image->repo_ip);
174        if (repo_id < 0) {
175                syslog(LOG_ERR, "failed to get repository (%s:%d)\n",
176                       __func__, __LINE__);
177                return -1;
178        }
179
180        result = dbi_conn_queryf(dbi->conn,
181                                 "SELECT nombreca FROM imagenes WHERE nombreca = '%s'",
182                                 image->name);
183        if (!result) {
184                dbi_conn_error(dbi->conn, &msglog);
185                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
186                       __func__, __LINE__, msglog);
187                return -1;
188        }
189
190        if (dbi_result_next_row(result)) {
191                syslog(LOG_ERR, "image creation attempt with already used image name (%s:%d)\n",
192                       __func__, __LINE__);
193                dbi_result_free(result);
194                return -1;
195        }
196        dbi_result_free(result);
197
198        result = dbi_conn_queryf(dbi->conn,
199                                 "INSERT INTO imagenes (nombreca, "
200                                 "descripcion, "
201                                 "idperfilsoft, "
202                                 "idcentro, "
203                                 "comentarios, "
204                                 "grupoid, "
205                                 "idrepositorio, "
206                                 "tipo, "
207                                 "ruta) "
208                                 "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')",
209                                 image->name, image->description,
210                                 OG_UNASSIGNED_SW_ID, image->center_id,
211                                 image->group_id, repo_id,
212                                 OG_IMAGE_DEFAULT_TYPE);
213
214        if (!result) {
215                dbi_conn_error(dbi->conn, &msglog);
216                syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n",
217                       __func__, __LINE__, msglog);
218                return -1;
219        }
220
221        dbi_result_free(result);
222        return dbi_conn_sequence_last(dbi->conn, NULL);
223}
Note: See TracBrowser for help on using the repository browser.