From 8aad4d71cf91a3c835dccc99e4b7fc0b4cb156b9 Mon Sep 17 00:00:00 2001 From: oupson Date: Mon, 25 Oct 2021 11:22:53 +0200 Subject: [PATCH] error handling buf fix --- Makefile | 13 +- include/auth_univ_orleans.h | 9 -- include/auth_univ_orleans/auth_univ_orleans.h | 10 ++ .../{ => auth_univ_orleans}/cookie_iterator.h | 0 include/auth_univ_orleans/result.h | 28 ++++ src/auth_univ_orleans.c | 141 ++++++++++-------- src/cookie_iterator.c | 9 +- src/result.c | 24 +++ src/test.c | 42 ++++-- 9 files changed, 177 insertions(+), 99 deletions(-) delete mode 100644 include/auth_univ_orleans.h create mode 100644 include/auth_univ_orleans/auth_univ_orleans.h rename include/{ => auth_univ_orleans}/cookie_iterator.h (100%) create mode 100644 include/auth_univ_orleans/result.h create mode 100644 src/result.c diff --git a/Makefile b/Makefile index 57f74ad..d8696e6 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,7 @@ all: lib/libauthunivorleans.so lib/libauthunivorleans.a bin/test -OPTIMISATION ?= 0 -CFLAGS = -Iinclude $(DEFINES) `pkg-config --cflags libcurl` -fPIC -Wall -Werror +CFLAGS += -Iinclude $(DEFINES) `pkg-config --cflags libcurl` -fPIC -Wall -Werror FLAGS = -g @@ -24,16 +23,16 @@ CC ?= $(CC) obj/%.o: src/%.c @mkdir -p $(@D) - $(CC) -c -O$(OPTIMISATION) -MMD -g -o $@ $< $(CFLAGS) $(FLAGS) + $(CC) -c -MMD -o $@ $< $(CFLAGS) $(FLAGS) -lib/libauthunivorleans.so: obj/auth_univ_orleans.o obj/cookie_iterator.o +lib/libauthunivorleans.so: obj/auth_univ_orleans.o obj/cookie_iterator.o obj/result.o @mkdir -p $(@D) - $(CC) -fpic -shared -O$(OPTIMISATION) $(FLAGS) -flto -o $@ $^ `pkg-config --libs libcurl` `pkg-config --libs tidy` + $(CC) -fpic -shared $(CFLAGS) $(FLAGS) -o $@ $^ `pkg-config --libs libcurl` `pkg-config --libs tidy` -lib/libauthunivorleans.a: obj/auth_univ_orleans.o obj/cookie_iterator.o +lib/libauthunivorleans.a: obj/auth_univ_orleans.o obj/cookie_iterator.o obj/result.o @mkdir -p $(@D) $(AR) -cr $@ $^ bin/test: obj/test.o lib/libauthunivorleans.a @mkdir -p $(@D) - $(CC) -g -O$(OPTIMISATION) -flto -o $@ $^ `pkg-config --libs libcurl` `pkg-config --libs tidy` \ No newline at end of file + $(CC) -o $@ $^ $(CFLAGS) `pkg-config --libs libcurl` `pkg-config --libs tidy` \ No newline at end of file diff --git a/include/auth_univ_orleans.h b/include/auth_univ_orleans.h deleted file mode 100644 index b62657e..0000000 --- a/include/auth_univ_orleans.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef AUTH_UNIV_ORLEANS -#define AUTH_UNIV_ORLEANS - -#include -#include - -cookie_iterator_t *auth_univ_get_session_token(char *username, char *password); -int auth_univ_orleans_login(CURL *handle, char *username, char *password); -#endif \ No newline at end of file diff --git a/include/auth_univ_orleans/auth_univ_orleans.h b/include/auth_univ_orleans/auth_univ_orleans.h new file mode 100644 index 0000000..12aea38 --- /dev/null +++ b/include/auth_univ_orleans/auth_univ_orleans.h @@ -0,0 +1,10 @@ +#ifndef AUTH_UNIV_ORLEANS +#define AUTH_UNIV_ORLEANS + +#include +#include +#include + +result_t* auth_univ_get_session_token(char *username, char *password); +int auth_univ_orleans_login(CURL *handle, char *username, char *password); +#endif \ No newline at end of file diff --git a/include/cookie_iterator.h b/include/auth_univ_orleans/cookie_iterator.h similarity index 100% rename from include/cookie_iterator.h rename to include/auth_univ_orleans/cookie_iterator.h diff --git a/include/auth_univ_orleans/result.h b/include/auth_univ_orleans/result.h new file mode 100644 index 0000000..75b0a1d --- /dev/null +++ b/include/auth_univ_orleans/result.h @@ -0,0 +1,28 @@ +#ifndef ERROR_H +#define ERROR_H + +typedef struct result result_t; + +#include + +enum result_type { + OK = 0, + ERR = 1, +}; + +union inner_result { + cookie_iterator_t *ok; + char *error; +}; + +struct result { + enum result_type type; + union inner_result inner; +}; + + +enum result_type get_result_type(result_t *result); +cookie_iterator_t *get_result_ok(result_t *result); +const char *get_result_err(result_t *result); +void result_free(result_t *result); +#endif \ No newline at end of file diff --git a/src/auth_univ_orleans.c b/src/auth_univ_orleans.c index b5a7767..5c46161 100644 --- a/src/auth_univ_orleans.c +++ b/src/auth_univ_orleans.c @@ -1,93 +1,109 @@ -#include -#include +#include +#include +#include #include #include #include #include #include -int private_auth_univ_orleans_post_login(CURL *handle, char *lt, char *action, - char *username, char *password); +int __auth_univ_orleans_post_login(CURL *handle, char *lt, char *action, + char *username, char *password); -int private_auth_univ_orleans_get_action_and_lt(CURL *handle, char **lt, - char **action); +int __get_action_and_lt(CURL *handle, char **lt, char **action); -size_t private_auth_univ_orleans_dumb_write_callback(void *data, size_t size, - size_t nmemb, - void *userp) { +size_t __dumb_write_callback(void *data, size_t size, size_t nmemb, + void *userp) { return size * nmemb; } -uint private_auth_univ_orleans_tidy_buf_write_cb(char *in, uint size, - uint nmemb, TidyBuffer *out) { - uint r; +size_t __tidy_buf_write_cb(void *in, size_t size, size_t nmemb, void *out) { + size_t r; r = size * nmemb; - tidyBufAppend(out, in, r); + tidyBufAppend((TidyBuffer *)out, in, r); return r; } -int private_auth_univ_orleans_get_action_and_lt_from_node(TidyDoc doc, - TidyNode tnod, - char **lt, - char **action); +int __get_action_and_lt_from_node(TidyDoc doc, TidyNode tnod, char **lt, + char **action); -cookie_iterator_t *auth_univ_get_session_token(char *username, char *password) { +result_t *auth_univ_get_session_token(char *username, char *password) { CURL *handle = curl_easy_init(); char errbuf[CURL_ERROR_SIZE]; curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, errbuf); + curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L); /* set the error buffer as empty before performing a request */ errbuf[0] = 0; // curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1L); - auth_univ_orleans_login(handle, username, password); + int res = auth_univ_orleans_login(handle, username, password); - cookie_iterator_t *list = new_cookie_iterator_from_curl(handle); + if (res == CURLE_OK) { - curl_easy_cleanup(handle); + cookie_iterator_t *list = new_cookie_iterator_from_curl(handle); - return list; + curl_easy_cleanup(handle); + + struct result *res = (struct result *)calloc(1, sizeof(struct result)); + res->type = OK; + res->inner.ok = list; + + return res; + } else { + struct result *res = (struct result *)calloc(1, sizeof(struct result)); + res->type = ERR; + res->inner.error = strdup(errbuf); + curl_easy_cleanup(handle); + return res; + } } int auth_univ_orleans_login(CURL *handle, char *username, char *password) { + int res = 0; curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(handle, CURLOPT_COOKIEFILE, ""); curl_easy_setopt(handle, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:93.0) " "Gecko/20100101 Firefox/93.0"); - curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, - private_auth_univ_orleans_dumb_write_callback); - - curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L); + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, __dumb_write_callback); curl_easy_setopt(handle, CURLOPT_URL, "https://ent.univ-orleans.fr/"); - curl_easy_perform(handle); + res = curl_easy_perform(handle); - char *lt = NULL; - char *action = NULL; - private_auth_univ_orleans_get_action_and_lt(handle, <, &action); + if (res == CURLE_OK) { + char *lt = NULL; + char *action = NULL; + res = __get_action_and_lt(handle, <, &action); - if (lt == NULL || action == NULL) { - return 0; + if (res == CURLE_OK) { + if (lt == NULL || action == NULL) { + return 0; + } + + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, + __dumb_write_callback); + + res = __auth_univ_orleans_post_login(handle, lt, action, username, + password); + + free(lt); + free(action); + return res; + } else { + return res; + } + } else { + return res; } - curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, - private_auth_univ_orleans_dumb_write_callback); - - private_auth_univ_orleans_post_login(handle, lt, action, username, - password); - - free(lt); - free(action); - return 0; } -int private_auth_univ_orleans_get_action_and_lt(CURL *handle, char **lt, - char **action) { +int __get_action_and_lt(CURL *handle, char **lt, char **action) { TidyDoc tdoc; TidyBuffer docbuf = {0}; // TidyBuffer tidy_errbuf = {0}; @@ -98,8 +114,7 @@ int private_auth_univ_orleans_get_action_and_lt(CURL *handle, char **lt, "https://auth.univ-orleans.fr/cas/login?service=https://" "ent.univ-orleans.fr/uPortal/" "Login%3FrefUrl%3D%2FuPortal%2Ff%2Faccueil%2Fnormal%2Frender.uPn"); - curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, - private_auth_univ_orleans_tidy_buf_write_cb); + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, __tidy_buf_write_cb); tdoc = tidyCreate(); tidyOptSetBool(tdoc, TidyShowWarnings, no); @@ -110,22 +125,23 @@ int private_auth_univ_orleans_get_action_and_lt(CURL *handle, char **lt, curl_easy_setopt(handle, CURLOPT_WRITEDATA, &docbuf); err = curl_easy_perform(handle); - if (!err) { - err = tidyParseBuffer(tdoc, &docbuf); /* parse the input */ + if (err == CURLE_OK) { + err = tidyParseBuffer(tdoc, &docbuf); + /* parse the input */ // TODO ERROR if (err >= 0) { - private_auth_univ_orleans_get_action_and_lt_from_node( - tdoc, tidyGetRoot(tdoc), lt, action); /* walk the tree */ + err = __get_action_and_lt_from_node(tdoc, tidyGetRoot(tdoc), lt, + action); /* walk the tree */ } } tidyBufFree(&docbuf); // tidyBufFree(&tidy_errbuf); tidyRelease(tdoc); - return 0; + return err; } -int private_auth_univ_orleans_post_login(CURL *handle, char *lt, char *action, - char *username, char *password) { +int __auth_univ_orleans_post_login(CURL *handle, char *lt, char *action, + char *username, char *password) { char form_data[256]; char *encoded_lt = curl_easy_escape(handle, lt, strlen(lt)); char *encoded_username = @@ -145,12 +161,12 @@ int private_auth_univ_orleans_post_login(CURL *handle, char *lt, char *action, int action_size = strlen(action); char *url = (char *)calloc(28 + action_size + 1, sizeof(char)); - strncpy(url, "https://auth.univ-orleans.fr", 29); - strncpy(url + 28, action, action_size); + memcpy(url, "https://auth.univ-orleans.fr", 29); + memcpy(url + 28, action, action_size); curl_easy_setopt(handle, CURLOPT_URL, url); - curl_easy_perform(handle); + int res = curl_easy_perform(handle); free(url); @@ -158,13 +174,11 @@ int private_auth_univ_orleans_post_login(CURL *handle, char *lt, char *action, curl_free(encoded_username); curl_free(encoded_password); - return 0; + return res; } -int private_auth_univ_orleans_get_action_and_lt_from_node(TidyDoc doc, - TidyNode tnod, - char **lt, - char **action) { +int __get_action_and_lt_from_node(TidyDoc doc, TidyNode tnod, char **lt, + char **action) { TidyNode child; for (child = tidyGetChild(tnod); child; child = tidyGetNext(child)) { ctmbstr name = tidyNodeGetName(child); @@ -202,11 +216,10 @@ int private_auth_univ_orleans_get_action_and_lt_from_node(TidyDoc doc, } if (*lt == NULL || *action == NULL) { - if (private_auth_univ_orleans_get_action_and_lt_from_node( - doc, child, lt, action)) { - return 1; + if (__get_action_and_lt_from_node(doc, child, lt, action) == 0) { + return 0; } } } - return *lt != NULL && *action != NULL; + return (*lt != NULL && *action != NULL) ? 0 : -1; } diff --git a/src/cookie_iterator.c b/src/cookie_iterator.c index 8873c48..eee50da 100644 --- a/src/cookie_iterator.c +++ b/src/cookie_iterator.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -27,7 +27,10 @@ cookie_t *cookie_iterator_next(cookie_iterator_t *iterator) { return NULL; } + bool have_value = false; + if (iterator->current != NULL) { + have_value = true; char *data = iterator->current->data; char buf[STR_BUF_SIZE]; @@ -112,11 +115,11 @@ cookie_t *cookie_iterator_next(cookie_iterator_t *iterator) { i++; } iterator->cookie->value[i] = '\0'; - + iterator->current = iterator->current->next; } - return (iterator->current != NULL) ? iterator->cookie : NULL; + return (have_value) ? iterator->cookie : NULL; } void cookie_iterator_free(cookie_iterator_t *iterator) { diff --git a/src/result.c b/src/result.c new file mode 100644 index 0000000..2772640 --- /dev/null +++ b/src/result.c @@ -0,0 +1,24 @@ +#include +#include +#include + +enum result_type get_result_type(result_t *result) { return result->type; } + +cookie_iterator_t *get_result_ok(result_t *result) { return result->inner.ok; } + +const char *get_result_err(result_t *result) { + return result->inner.error; +} + +void result_free(result_t *result) { + if (result->type == OK) { + if (result->inner.ok != NULL) { + cookie_iterator_free(result->inner.ok); + result->inner.ok = NULL; + } + } else { + free(result->inner.error); + result->inner.error = NULL; + } + free(result); +} \ No newline at end of file diff --git a/src/test.c b/src/test.c index d74e2e0..55f46b3 100644 --- a/src/test.c +++ b/src/test.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include int main(void) { @@ -11,21 +11,31 @@ int main(void) { return -1; } - cookie_iterator_t *iter = auth_univ_get_session_token(username, password); - cookie_t *cookie; + result_t *res = auth_univ_get_session_token(username, password); - while ((cookie = cookie_iterator_next(iter))) { - printf("%s :\n" - "\thostname: %s\n" - "\tinclude subdomains : %d\n" - "\tpath : %s\n" - "\tsecure : %d\n" - "\texpire : %d\n" - "\tvalue: %s\n", - cookie_name(cookie), cookie_hostname(cookie), - cookie_include_subdomains(cookie), cookie_path(cookie), - cookie_secure(cookie), cookie_expire(cookie), cookie_value(cookie)); + if (get_result_type(res) == OK) { + + cookie_iterator_t *iter = get_result_ok(res); + cookie_t *cookie; + + while ((cookie = cookie_iterator_next(iter))) { + printf("%s :\n" + "\thostname: %s\n" + "\tinclude subdomains : %d\n" + "\tpath : %s\n" + "\tsecure : %d\n" + "\texpire : %d\n" + "\tvalue: %s\n", + cookie_name(cookie), cookie_hostname(cookie), + cookie_include_subdomains(cookie), cookie_path(cookie), + cookie_secure(cookie), cookie_expire(cookie), + cookie_value(cookie)); + } + + + } else { + fprintf(stderr, "error : %s\n", get_result_err(res)); } - cookie_iterator_free(iter); + result_free(res); } \ No newline at end of file