[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> |
---|
[2629906] | 16 | |
---|
| 17 | struct og_dbi *og_dbi_open(struct og_dbi_config *config) |
---|
| 18 | { |
---|
| 19 | struct og_dbi *dbi; |
---|
| 20 | |
---|
| 21 | dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi)); |
---|
| 22 | if (!dbi) |
---|
| 23 | return NULL; |
---|
| 24 | |
---|
| 25 | dbi_initialize_r(NULL, &dbi->inst); |
---|
| 26 | dbi->conn = dbi_conn_new_r("mysql", dbi->inst); |
---|
| 27 | if (!dbi->conn) { |
---|
| 28 | free(dbi); |
---|
| 29 | return NULL; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | dbi_conn_set_option(dbi->conn, "host", config->host); |
---|
[0631b0e] | 33 | dbi_conn_set_option(dbi->conn, "port", config->port); |
---|
[2629906] | 34 | dbi_conn_set_option(dbi->conn, "username", config->user); |
---|
| 35 | dbi_conn_set_option(dbi->conn, "password", config->passwd); |
---|
| 36 | dbi_conn_set_option(dbi->conn, "dbname", config->database); |
---|
| 37 | dbi_conn_set_option(dbi->conn, "encoding", "UTF-8"); |
---|
| 38 | |
---|
| 39 | if (dbi_conn_connect(dbi->conn) < 0) { |
---|
| 40 | free(dbi); |
---|
| 41 | return NULL; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | return dbi; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | void og_dbi_close(struct og_dbi *dbi) |
---|
| 48 | { |
---|
| 49 | dbi_conn_close(dbi->conn); |
---|
| 50 | dbi_shutdown_r(dbi->inst); |
---|
| 51 | free(dbi); |
---|
| 52 | } |
---|
[3cb98c7] | 53 | |
---|
[d7e2022] | 54 | int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, |
---|
| 55 | struct in_addr addr) |
---|
[3cb98c7] | 56 | { |
---|
| 57 | const char *msglog; |
---|
| 58 | dbi_result result; |
---|
| 59 | |
---|
| 60 | result = dbi_conn_queryf(dbi->conn, |
---|
| 61 | "SELECT ordenadores.idordenador," |
---|
[cbd9421] | 62 | " ordenadores.nombreordenador," |
---|
| 63 | " ordenadores.numserie," |
---|
| 64 | " ordenadores.ip," |
---|
| 65 | " ordenadores.mac," |
---|
| 66 | " ordenadores.idaula," |
---|
| 67 | " ordenadores.idperfilhard," |
---|
| 68 | " ordenadores.idrepositorio," |
---|
| 69 | " ordenadores.mascara," |
---|
| 70 | " ordenadores.arranque," |
---|
| 71 | " ordenadores.netiface," |
---|
| 72 | " ordenadores.netdriver," |
---|
| 73 | " ordenadores.idproautoexec," |
---|
| 74 | " ordenadores.oglivedir," |
---|
| 75 | " ordenadores.inremotepc," |
---|
| 76 | " ordenadores.maintenance," |
---|
| 77 | " centros.idcentro " |
---|
| 78 | "FROM ordenadores " |
---|
[3cb98c7] | 79 | "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula " |
---|
| 80 | "INNER JOIN centros ON centros.idcentro=aulas.idcentro " |
---|
| 81 | "WHERE ordenadores.ip='%s'", inet_ntoa(addr)); |
---|
| 82 | if (!result) { |
---|
| 83 | dbi_conn_error(dbi->conn, &msglog); |
---|
| 84 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
| 85 | __func__, __LINE__, msglog); |
---|
| 86 | return -1; |
---|
| 87 | } |
---|
| 88 | if (!dbi_result_next_row(result)) { |
---|
| 89 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
| 90 | __func__, __LINE__); |
---|
| 91 | dbi_result_free(result); |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | computer->id = dbi_result_get_uint(result, "idordenador"); |
---|
[cbd9421] | 96 | computer->name = strdup(dbi_result_get_string(result, "nombreordenador")); |
---|
| 97 | computer->serial_number = strdup(dbi_result_get_string(result, "numserie")); |
---|
| 98 | computer->ip = strdup(dbi_result_get_string(result, "ip")); |
---|
| 99 | computer->mac = strdup(dbi_result_get_string(result, "mac")); |
---|
[3cb98c7] | 100 | computer->room = dbi_result_get_uint(result, "idaula"); |
---|
[cbd9421] | 101 | computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); |
---|
| 102 | computer->repo_id = dbi_result_get_uint(result, "idrepositorio"); |
---|
| 103 | computer->netmask = strdup(dbi_result_get_string(result, "mascara")); |
---|
| 104 | computer->boot = strdup(dbi_result_get_string(result, "arranque")); |
---|
| 105 | computer->netiface = strdup(dbi_result_get_string(result, "netiface")); |
---|
| 106 | computer->netdriver = strdup(dbi_result_get_string(result, "netdriver")); |
---|
[3cb98c7] | 107 | computer->procedure_id = dbi_result_get_uint(result, "idproautoexec"); |
---|
[cbd9421] | 108 | computer->livedir = strdup(dbi_result_get_string(result, "oglivedir")); |
---|
| 109 | computer->remote = dbi_result_get_uint(result, "inremotepc") != 0; |
---|
| 110 | computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0; |
---|
[3cb98c7] | 111 | |
---|
| 112 | dbi_result_free(result); |
---|
| 113 | |
---|
| 114 | return 0; |
---|
| 115 | } |
---|
[cbd9421] | 116 | |
---|
| 117 | void og_dbi_free_computer_info(struct og_computer *computer) |
---|
| 118 | { |
---|
| 119 | free(computer->serial_number); |
---|
| 120 | free(computer->netdriver); |
---|
| 121 | free(computer->netiface); |
---|
| 122 | free(computer->netmask); |
---|
| 123 | free(computer->livedir); |
---|
| 124 | free(computer->name); |
---|
| 125 | free(computer->boot); |
---|
| 126 | free(computer->mac); |
---|
| 127 | free(computer->ip); |
---|
| 128 | } |
---|