diff --git a/.gitignore b/.gitignore index a257dec6..c66b2694 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,8 @@ pihole-FTL # Versioning files (generated by Makefile) -version* +version.h +version~ # CMake files /cmake-build-debug/ diff --git a/Makefile b/Makefile index 28fceac1..0b05e33f 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ DNSMASQ_OPTS = -DHAVE_DNSSEC -DHAVE_DNSSEC_STATIC -DHAVE_IDN FTL_DEPS = *.h database/*.h api/*.h version.h FTL_DB_OBJ = database/common.o database/query-table.o database/network-table.o database/gravity-db.o database/database-thread.o \ database/sqlite3-ext.o database/message-table.o -FTL_API_OBJ = api/http.o api/ftl.o api/stats.o api/dns.o +FTL_API_OBJ = api/http.o api/ftl.o api/stats.o api/dns.o api/version.o FTL_OBJ = $(FTL_DB_OBJ) $(FTL_API_OBJ) main.o memory.o log.o daemon.o datastructure.o signals.o files.o setupVars.o args.o gc.o config.o dnsmasq_interface.o resolve.o regex.o shmem.o capabilities.o overTime.o timers.o vector.o DNSMASQ_DEPS = config.h dhcp-protocol.h dns-protocol.h radv-protocol.h dhcp6-protocol.h dnsmasq.h ip6addr.h metrics.h ../dnsmasq_interface.h diff --git a/src/FTL.h b/src/FTL.h index 0b4e6501..491eb8b1 100644 --- a/src/FTL.h +++ b/src/FTL.h @@ -128,4 +128,7 @@ extern pthread_t DBthread; extern pthread_t GCthread; extern pthread_t DNSclientthread; +// Intentionally ignore result of function declared warn_unused_result +#define igr(x) {__typeof__(x) __attribute__((unused)) d=(x);} + #endif // FTL_H diff --git a/src/api/api.h b/src/api/api.h index 4c57cbe8..3c43e3cc 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -41,4 +41,7 @@ int api_dns_blacklist(struct mg_connection *conn); int api_dns_blacklist_exact(struct mg_connection *conn); int api_dns_blacklist_regex(struct mg_connection *conn); +// Version method +int api_version(struct mg_connection *conn); + #endif // API_H diff --git a/src/api/http.c b/src/api/http.c index 22e415cc..54c3f262 100644 --- a/src/api/http.c +++ b/src/api/http.c @@ -156,6 +156,11 @@ static int api_handler(struct mg_connection *conn, void *ignored) { ret = api_stats_recentblocked(conn); } + /******************************** api/version ****************************/ + else if(strcasecmp("/api/version", request->local_uri) == 0) + { + ret = api_version(conn); + } /******************************** not found ******************************/ /* else { diff --git a/src/api/version.c b/src/api/version.c new file mode 100644 index 00000000..24db3963 --- /dev/null +++ b/src/api/version.c @@ -0,0 +1,64 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2019 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* API Implementation /api/version +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "FTL.h" +#include "api.h" +// get_FTL_version() +#include "log.h" +#include "version.h" + +int api_version(struct mg_connection *conn) +{ + cJSON *json = JSON_NEW_OBJ(); + + cJSON *api = JSON_NEW_OBJ(); + JSON_OBJ_REF_STR(api, "branch", "none"); + JSON_OBJ_REF_STR(api, "hash", "none"); + JSON_OBJ_REF_STR(api, "tag", "none"); + JSON_OBJ_ADD_ITEM(json, "api", api); + + FILE* file; + char coreversion[256] = "N/A"; + char webversion[256] = "N/A"; + if((file = fopen("/etc/pihole/localversions", "r")) != NULL) + { + igr(fscanf(file, "%255s %255s", coreversion, webversion)); + fclose(file); + } + char corebranch[256] = "N/A"; + char webbranch[256] = "N/A"; + if((file = fopen("/etc/pihole/localbranches", "r")) != NULL) + { + igr(fscanf(file, "%255s %255s", corebranch, webbranch)); + fclose(file); + } + + cJSON *web = JSON_NEW_OBJ(); + JSON_OBJ_REF_STR(web, "branch", webbranch); + JSON_OBJ_REF_STR(web, "hash", "none"); + JSON_OBJ_REF_STR(web, "tag", webversion); + JSON_OBJ_ADD_ITEM(json, "web", web); + + cJSON *core = JSON_NEW_OBJ(); + JSON_OBJ_REF_STR(core, "branch", corebranch); + JSON_OBJ_REF_STR(core, "hash", "none"); + JSON_OBJ_REF_STR(core, "tag", coreversion); + JSON_OBJ_ADD_ITEM(json, "core", core); + + cJSON *ftl = JSON_NEW_OBJ(); + JSON_OBJ_REF_STR(ftl, "branch", GIT_BRANCH); + JSON_OBJ_REF_STR(ftl, "hash", GIT_HASH); + char *version = get_FTL_version(); + JSON_OBJ_COPY_STR(ftl, "tag", version); + JSON_OBJ_ADD_ITEM(json, "ftl", ftl); + free(version); + + JSON_SENT_OBJECT(json); +} \ No newline at end of file diff --git a/src/log.c b/src/log.c index 93a8771d..e7a6d1a6 100644 --- a/src/log.c +++ b/src/log.c @@ -161,7 +161,7 @@ void log_FTL_version(const bool crashreport) } static char *FTLversion = NULL; -const char __attribute__ ((malloc)) *get_FTL_version(void) +char __attribute__ ((malloc)) *get_FTL_version(void) { // Obtain FTL version if not already determined if(FTLversion == NULL) diff --git a/src/log.h b/src/log.h index fa31574a..ad4f8e9c 100644 --- a/src/log.h +++ b/src/log.h @@ -17,7 +17,7 @@ void open_FTL_log(const bool test); void logg(const char* format, ...) __attribute__ ((format (gnu_printf, 1, 2))); void log_counter_info(void); void format_memory_size(char *prefix, unsigned long long int bytes, double *formated); -const char *get_FTL_version(void) __attribute__ ((malloc)); +char *get_FTL_version(void) __attribute__ ((malloc)); void log_FTL_version(bool crashreport); void get_timestr(char *timestring, const time_t timein);