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

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

ogServer is AGPLv3+

Update license header in files.

  • Property mode set to 100644
File size: 5.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                free(dbi);
43                return NULL;
44        }
45
46        return dbi;
47}
48
49void og_dbi_close(struct og_dbi *dbi)
50{
51        dbi_conn_close(dbi->conn);
52        dbi_shutdown_r(dbi->inst);
53        free(dbi);
54}
55
56int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
57                             struct in_addr addr)
58{
59        const char *msglog;
60        dbi_result result;
61
62        result = dbi_conn_queryf(dbi->conn,
63                                 "SELECT ordenadores.idordenador,"
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 "
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");
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"));
106        computer->room = dbi_result_get_uint(result, "idaula");
107        computer->center = dbi_result_get_uint(result, "idcentro");
108        computer->hardware_id = dbi_result_get_uint(result, "idperfilhard");
109        computer->repo_id = dbi_result_get_uint(result, "idrepositorio");
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"));
118        computer->procedure_id = dbi_result_get_uint(result, "idproautoexec");
119        snprintf(computer->livedir, sizeof(computer->livedir), "%s",
120                 dbi_result_get_string(result, "oglivedir"));
121        computer->remote = dbi_result_get_uint(result, "inremotepc") != 0;
122        computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0;
123
124        dbi_result_free(result);
125
126        return 0;
127}
128
129#define OG_UNASSIGNED_SW_ID 0
130#define OG_DEFAULT_REPO_ID 1
131#define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */
132
133int 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,
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,
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}
Note: See TracBrowser for help on using the repository browser.