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 | |
---|
9 | #include <syslog.h> |
---|
10 | #include <netinet/in.h> |
---|
11 | #include <arpa/inet.h> |
---|
12 | #include <string.h> |
---|
13 | #include "dbi.h" |
---|
14 | #include <syslog.h> |
---|
15 | #include <string.h> |
---|
16 | #include <stdio.h> |
---|
17 | |
---|
18 | struct 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 | |
---|
33 | dbi_conn_set_option(dbi->conn, "host", config->ip); |
---|
34 | dbi_conn_set_option(dbi->conn, "port", config->port); |
---|
35 | dbi_conn_set_option(dbi->conn, "username", config->user); |
---|
36 | dbi_conn_set_option(dbi->conn, "password", config->pass); |
---|
37 | dbi_conn_set_option(dbi->conn, "dbname", config->name); |
---|
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 | |
---|
48 | void og_dbi_close(struct og_dbi *dbi) |
---|
49 | { |
---|
50 | dbi_conn_close(dbi->conn); |
---|
51 | dbi_shutdown_r(dbi->inst); |
---|
52 | free(dbi); |
---|
53 | } |
---|
54 | |
---|
55 | int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, |
---|
56 | struct in_addr addr) |
---|
57 | { |
---|
58 | const char *msglog; |
---|
59 | dbi_result result; |
---|
60 | |
---|
61 | result = dbi_conn_queryf(dbi->conn, |
---|
62 | "SELECT ordenadores.idordenador," |
---|
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 " |
---|
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"); |
---|
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")); |
---|
105 | computer->room = dbi_result_get_uint(result, "idaula"); |
---|
106 | computer->center = dbi_result_get_uint(result, "idcentro"); |
---|
107 | computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); |
---|
108 | computer->repo_id = dbi_result_get_uint(result, "idrepositorio"); |
---|
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")); |
---|
117 | computer->procedure_id = dbi_result_get_uint(result, "idproautoexec"); |
---|
118 | snprintf(computer->livedir, sizeof(computer->livedir), "%s", |
---|
119 | dbi_result_get_string(result, "oglivedir")); |
---|
120 | computer->remote = dbi_result_get_uint(result, "inremotepc") != 0; |
---|
121 | computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0; |
---|
122 | |
---|
123 | dbi_result_free(result); |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | #define OG_UNASSIGNED_SW_ID 0 |
---|
129 | #define OG_DEFAULT_REPO_ID 1 |
---|
130 | #define OG_IMAGE_DEFAULT_TYPE 1 /* monolithic */ |
---|
131 | |
---|
132 | int 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, |
---|
138 | "INSERT INTO imagenes (nombreca, " |
---|
139 | "descripcion, " |
---|
140 | "idperfilsoft, " |
---|
141 | "idcentro, " |
---|
142 | "comentarios, " |
---|
143 | "grupoid, " |
---|
144 | "idrepositorio, " |
---|
145 | "tipo, " |
---|
146 | "ruta) " |
---|
147 | "VALUES ('%s', '%s', %u, %lu, '', %u, %lu, %u, '')", |
---|
148 | image->name, image->description, |
---|
149 | OG_UNASSIGNED_SW_ID, image->center_id, |
---|
150 | image->group_id, OG_DEFAULT_REPO_ID, |
---|
151 | OG_IMAGE_DEFAULT_TYPE); |
---|
152 | |
---|
153 | if (!result) { |
---|
154 | dbi_conn_error(dbi->conn, &msglog); |
---|
155 | syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n", |
---|
156 | __func__, __LINE__, msglog); |
---|
157 | return -1; |
---|
158 | } |
---|
159 | |
---|
160 | dbi_result_free(result); |
---|
161 | return dbi_conn_sequence_last(dbi->conn, NULL); |
---|
162 | } |
---|