Skip to content

Commit

Permalink
improved ca path finding
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptorioo committed Jan 31, 2025
1 parent f598fe0 commit f643c57
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 3,635 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ build-aux
config.log
config.status
configure
cookie.txt
cookie.txt
ntuget
8 changes: 1 addition & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PROJECT(ntuget)
SET(CMAKE_C_STANDARD 17)
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake)

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/_dep/include)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)

FIND_PACKAGE(Curl REQUIRED)
INCLUDE_DIRECTORIES(${CURL_INCLUDE_DIR})
Expand All @@ -23,9 +23,3 @@ ELSEIF(CMAKE_BUILD_TYPE STREQUAL "Release")
TARGET_COMPILE_OPTIONS(ntuget PRIVATE -O2 -DNDEBUG)
TARGET_LINK_OPTIONS(ntuget PRIVATE -s)
ENDIF()

ADD_CUSTOM_COMMAND(TARGET ntuget POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/curl-ca-bundle.crt
$<TARGET_FILE_DIR:ntuget>
)
5 changes: 2 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CMAKE_DIST = \

EXTRA_DIST = \
$(CMAKE_DIST) \
CMakeLists.txt \
Dockerfile \
.dockerignore

Expand All @@ -13,6 +14,4 @@ SUBDIRS = \

DIST_SUBDIRS = \
$(SUBDIRS) \
include \
test \
docs
include
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AC_PREREQ([2.71])

AC_INIT([ntuget], [-])
AC_INIT([ntuget], [0.1.0])

AC_CONFIG_SRCDIR([src/ntuget.c])
AC_CONFIG_FILES([Makefile include/Makefile src/Makefile])
Expand Down
3,611 changes: 0 additions & 3,611 deletions curl-ca-bundle.crt

This file was deleted.

6 changes: 6 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
#define SAML_URL "https://cool.ntu.edu.tw/login/saml"
#define API_BASE_URL "https://cool.ntu.edu.tw/api/v1"

#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
#define SYSTEM_CA_PATH "/etc/ssl/certs/ca-certificates.crt"
#else
#define CA_FILE_NAME "ca-bundle.crt"
#endif

#endif
7 changes: 5 additions & 2 deletions include/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
#include <sys/stat.h>
#endif

#include <curl/curl.h>
#include "config.h"
#include "utils.h"

#define CA_FILE_NAME "curl-ca-bundle.crt"

typedef struct
{
long status_code;
Expand Down
33 changes: 23 additions & 10 deletions src/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,33 @@ static size_t header_callback(char *buffer, size_t size, size_t nitems, void *us
return total_size;
}

inline static char *get_certificate_path() {
#if defined(__unix__) || defined(__linux__) || defined(__APPLE__)
struct stat buffer;
if (stat(SYSTEM_CA_PATH, &buffer) == 0) {
return strdup(SYSTEM_CA_PATH);
}
return NULL;
#else
const char *exePath = executable_directory();
size_t len = strlen(exePath) + strlen(CA_FILE_NAME) + 1;
char *crt_dir = malloc(len * sizeof(char));
if (!crt_dir) {
free((void *)exePath);
return NULL;
}
snprintf(crt_dir, len, "%s%s", exePath, CA_FILE_NAME);
free((void *)exePath);
return crt_dir;
#endif
}

RequestResponse *request_get(RequestOptions opt)
{
CURL *curl = curl_easy_init();
RequestResponse *response = request_response_init();

const char *exePath = executable_directory();
size_t len = strlen(exePath) + strlen(CA_FILE_NAME) + 1;
char *crt_dir = malloc(len * sizeof(char));
snprintf(crt_dir, len, "%s%s", exePath, CA_FILE_NAME);
const char *crt_dir = get_certificate_path();

if (curl)
{
Expand Down Expand Up @@ -106,7 +124,6 @@ RequestResponse *request_get(RequestOptions opt)
if (opt.verbose)
printf("****************************************\n");

free((void *)exePath);
free((void *)crt_dir);
curl_easy_cleanup(curl);

Expand All @@ -118,10 +135,7 @@ RequestResponse *request_post(RequestOptions opt)
CURL *curl = curl_easy_init();
RequestResponse *response = request_response_init();

const char *exePath = executable_directory();
size_t len = strlen(exePath) + strlen(CA_FILE_NAME) + 1;
char *crt_dir = malloc(len * sizeof(char));
snprintf(crt_dir, len, "%s%s", exePath, CA_FILE_NAME);
const char *crt_dir = get_certificate_path();

if (curl)
{
Expand Down Expand Up @@ -186,7 +200,6 @@ RequestResponse *request_post(RequestOptions opt)
if (opt.verbose)
printf("****************************************\n");

free((void *)exePath);
free((void *)crt_dir);
curl_easy_cleanup(curl);

Expand Down

0 comments on commit f643c57

Please sign in to comment.