[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 | |
---|
| 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->host); |
---|
[0631b0e] | 34 | dbi_conn_set_option(dbi->conn, "port", config->port); |
---|
[2629906] | 35 | dbi_conn_set_option(dbi->conn, "username", config->user); |
---|
| 36 | dbi_conn_set_option(dbi->conn, "password", config->passwd); |
---|
| 37 | dbi_conn_set_option(dbi->conn, "dbname", config->database); |
---|
| 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 | } |
---|
[3cb98c7] | 54 | |
---|
[d7e2022] | 55 | int 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"); |
---|
[cbd9421] | 106 | computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); |
---|
| 107 | computer->repo_id = dbi_result_get_uint(result, "idrepositorio"); |
---|
[f537daf] | 108 | snprintf(computer->netmask, sizeof(computer->netmask), "%s", |
---|
| 109 | dbi_result_get_string(result, "mascara")); |
---|
| 110 | snprintf(computer->boot, sizeof(computer->boot), "%s", |
---|
| 111 | dbi_result_get_string(result, "arranque")); |
---|
| 112 | snprintf(computer->netiface, sizeof(computer->netiface), "%s", |
---|
| 113 | dbi_result_get_string(result, "netiface")); |
---|
| 114 | snprintf(computer->netdriver, sizeof(computer->netdriver), "%s", |
---|
| 115 | dbi_result_get_string(result, "netdriver")); |
---|
[3cb98c7] | 116 | computer->procedure_id = dbi_result_get_uint(result, "idproautoexec"); |
---|
[f537daf] | 117 | snprintf(computer->livedir, sizeof(computer->livedir), "%s", |
---|
| 118 | dbi_result_get_string(result, "oglivedir")); |
---|
[cbd9421] | 119 | computer->remote = dbi_result_get_uint(result, "inremotepc") != 0; |
---|
| 120 | computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0; |
---|
[3cb98c7] | 121 | |
---|
| 122 | dbi_result_free(result); |
---|
| 123 | |
---|
| 124 | return 0; |
---|
| 125 | } |
---|