From ad2538d4732ca78575c6038d9d905382cf11e9b1 Mon Sep 17 00:00:00 2001 From: dartraiden Date: Thu, 13 Feb 2025 23:08:12 +0300 Subject: libcurl: update to 8.12.1 --- libs/libcurl/src/netrc.c | 123 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 40 deletions(-) (limited to 'libs/libcurl/src/netrc.c') diff --git a/libs/libcurl/src/netrc.c b/libs/libcurl/src/netrc.c index fed826a12d..1586f76fd1 100644 --- a/libs/libcurl/src/netrc.c +++ b/libs/libcurl/src/netrc.c @@ -26,7 +26,9 @@ #ifndef CURL_DISABLE_NETRC #ifdef HAVE_PWD_H +#undef __NO_NET_API /* required for AmigaOS to declare getpwuid() */ #include +#define __NO_NET_API #endif #include @@ -57,23 +59,26 @@ enum found_state { #define FOUND_LOGIN 1 #define FOUND_PASSWORD 2 -#define NETRC_FILE_MISSING 1 -#define NETRC_FAILED -1 -#define NETRC_SUCCESS 0 - #define MAX_NETRC_LINE 16384 #define MAX_NETRC_FILE (128*1024) #define MAX_NETRC_TOKEN 4096 -static CURLcode file2memory(const char *filename, struct dynbuf *filebuf) +/* convert a dynbuf call CURLcode error to a NETRCcode error */ +#define curl2netrc(result) \ + (((result) == CURLE_OUT_OF_MEMORY) ? \ + NETRC_OUT_OF_MEMORY : NETRC_SYNTAX_ERROR) + +static NETRCcode file2memory(const char *filename, struct dynbuf *filebuf) { - CURLcode result = CURLE_OK; + NETRCcode ret = NETRC_FILE_MISSING; /* if it cannot open the file */ FILE *file = fopen(filename, FOPEN_READTEXT); struct dynbuf linebuf; Curl_dyn_init(&linebuf, MAX_NETRC_LINE); if(file) { + ret = NETRC_OK; while(Curl_get_line(&linebuf, file)) { + CURLcode result; const char *line = Curl_dyn_ptr(&linebuf); /* skip comments on load */ while(ISBLANK(*line)) @@ -81,27 +86,29 @@ static CURLcode file2memory(const char *filename, struct dynbuf *filebuf) if(*line == '#') continue; result = Curl_dyn_add(filebuf, line); - if(result) + if(result) { + ret = curl2netrc(result); goto done; + } } } done: Curl_dyn_free(&linebuf); if(file) fclose(file); - return result; + return ret; } /* * Returns zero on success. */ -static int parsenetrc(struct store_netrc *store, - const char *host, - char **loginp, /* might point to a username */ - char **passwordp, - const char *netrcfile) +static NETRCcode parsenetrc(struct store_netrc *store, + const char *host, + char **loginp, /* might point to a username */ + char **passwordp, + const char *netrcfile) { - int retcode = NETRC_FILE_MISSING; + NETRCcode retcode = NETRC_NO_MATCH; char *login = *loginp; char *password = NULL; bool specific_login = !!login; /* points to something */ @@ -118,8 +125,9 @@ static int parsenetrc(struct store_netrc *store, Curl_dyn_init(&token, MAX_NETRC_TOKEN); if(!store->loaded) { - if(file2memory(netrcfile, filebuf)) - return NETRC_FAILED; + NETRCcode ret = file2memory(netrcfile, filebuf); + if(ret) + return ret; store->loaded = TRUE; } @@ -149,12 +157,18 @@ static int parsenetrc(struct store_netrc *store, tok_end = tok; if(!quoted) { size_t len = 0; + CURLcode result; while(!ISSPACE(*tok_end)) { tok_end++; len++; } - if(!len || Curl_dyn_addn(&token, tok, len)) { - retcode = NETRC_FAILED; + if(!len) { + retcode = NETRC_SYNTAX_ERROR; + goto out; + } + result = Curl_dyn_addn(&token, tok, len); + if(result) { + retcode = curl2netrc(result); goto out; } } @@ -163,6 +177,7 @@ static int parsenetrc(struct store_netrc *store, bool endquote = FALSE; tok_end++; /* pass the leading quote */ while(*tok_end) { + CURLcode result; char s = *tok_end; if(escape) { escape = FALSE; @@ -188,15 +203,16 @@ static int parsenetrc(struct store_netrc *store, endquote = TRUE; break; } - if(Curl_dyn_addn(&token, &s, 1)) { - retcode = NETRC_FAILED; + result = Curl_dyn_addn(&token, &s, 1); + if(result) { + retcode = curl2netrc(result); goto out; } tok_end++; } if(escape || !endquote) { /* bad syntax, get out */ - retcode = NETRC_FAILED; + retcode = NETRC_SYNTAX_ERROR; goto out; } } @@ -224,7 +240,7 @@ static int parsenetrc(struct store_netrc *store, } else if(strcasecompare("default", tok)) { state = HOSTVALID; - retcode = NETRC_SUCCESS; /* we did find our host */ + retcode = NETRC_OK; /* we did find our host */ } break; case MACDEF: @@ -235,7 +251,7 @@ static int parsenetrc(struct store_netrc *store, if(strcasecompare(host, tok)) { /* and yes, this is our host! */ state = HOSTVALID; - retcode = NETRC_SUCCESS; /* we did find our host */ + retcode = NETRC_OK; /* we did find our host */ } else /* not our host */ @@ -251,7 +267,7 @@ static int parsenetrc(struct store_netrc *store, free(login); login = strdup(tok); if(!login) { - retcode = NETRC_FAILED; /* allocation failed */ + retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */ goto out; } } @@ -262,10 +278,11 @@ static int parsenetrc(struct store_netrc *store, free(password); password = strdup(tok); if(!password) { - retcode = NETRC_FAILED; /* allocation failed */ + retcode = NETRC_OUT_OF_MEMORY; /* allocation failed */ goto out; } - found |= FOUND_PASSWORD; + if(!specific_login || our_login) + found |= FOUND_PASSWORD; keyword = NONE; } else if(strcasecompare("login", tok)) @@ -274,6 +291,10 @@ static int parsenetrc(struct store_netrc *store, keyword = PASSWORD; else if(strcasecompare("machine", tok)) { /* a new machine here */ + if(found & FOUND_PASSWORD) { + done = TRUE; + break; + } state = HOSTFOUND; keyword = NONE; found = 0; @@ -283,7 +304,7 @@ static int parsenetrc(struct store_netrc *store, } else if(strcasecompare("default", tok)) { state = HOSTVALID; - retcode = NETRC_SUCCESS; /* we did find our host */ + retcode = NETRC_OK; /* we did find our host */ Curl_safefree(password); if(!specific_login) Curl_safefree(login); @@ -309,11 +330,16 @@ static int parsenetrc(struct store_netrc *store, out: Curl_dyn_free(&token); - if(!retcode && !password && our_login) { - /* success without a password, set a blank one */ - password = strdup(""); - if(!password) - retcode = 1; /* out of memory */ + if(!retcode) { + if(!password && our_login) { + /* success without a password, set a blank one */ + password = strdup(""); + if(!password) + retcode = NETRC_OUT_OF_MEMORY; /* out of memory */ + } + else if(!login && !password) + /* a default with no credentials */ + retcode = NETRC_NO_MATCH; } if(!retcode) { /* success */ @@ -331,17 +357,34 @@ out: return retcode; } +const char *Curl_netrc_strerror(NETRCcode ret) +{ + switch(ret) { + default: + return ""; /* not a legit error */ + case NETRC_FILE_MISSING: + return "no such file"; + case NETRC_NO_MATCH: + return "no matching entry"; + case NETRC_OUT_OF_MEMORY: + return "out of memory"; + case NETRC_SYNTAX_ERROR: + return "syntax error"; + } + /* never reached */ +} + /* * @unittest: 1304 * * *loginp and *passwordp MUST be allocated if they are not NULL when passed * in. */ -int Curl_parsenetrc(struct store_netrc *store, const char *host, - char **loginp, char **passwordp, - char *netrcfile) +NETRCcode Curl_parsenetrc(struct store_netrc *store, const char *host, + char **loginp, char **passwordp, + char *netrcfile) { - int retcode = 1; + NETRCcode retcode = NETRC_OK; char *filealloc = NULL; if(!netrcfile) { @@ -379,13 +422,13 @@ int Curl_parsenetrc(struct store_netrc *store, const char *host, } if(!home) - return retcode; /* no home directory found (or possibly out of - memory) */ + return NETRC_FILE_MISSING; /* no home directory found (or possibly out + of memory) */ filealloc = aprintf("%s%s.netrc", home, DIR_CHAR); if(!filealloc) { free(homea); - return -1; + return NETRC_OUT_OF_MEMORY; } retcode = parsenetrc(store, host, loginp, passwordp, filealloc); free(filealloc); @@ -395,7 +438,7 @@ int Curl_parsenetrc(struct store_netrc *store, const char *host, filealloc = aprintf("%s%s_netrc", home, DIR_CHAR); if(!filealloc) { free(homea); - return -1; + return NETRC_OUT_OF_MEMORY; } retcode = parsenetrc(store, host, loginp, passwordp, filealloc); free(filealloc); -- cgit v1.2.3