aboutsummaryrefslogtreecommitdiff
path: root/lib/netrc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/netrc.c')
-rw-r--r--lib/netrc.c93
1 files changed, 45 insertions, 48 deletions
diff --git a/lib/netrc.c b/lib/netrc.c
index ed5d70252..c6443608e 100644
--- a/lib/netrc.c
+++ b/lib/netrc.c
@@ -45,6 +45,9 @@
#include "strequal.h"
#include "strtok.h"
+#define _MPRINTF_REPLACE /* use our functions only */
+#include <curl/mprintf.h>
+
/* The last #include file should be: */
#ifdef CURLDEBUG
#include "memdebug.h"
@@ -71,54 +74,24 @@ enum {
#define LOGINSIZE 64
#define PASSWORDSIZE 64
+/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */
int Curl_parsenetrc(char *host,
char *login,
- char *password)
+ char *password,
+ char *netrcfile)
{
FILE *file;
- char netrcbuffer[256];
int retcode=1;
-
int specific_login = (login[0] != 0);
-
char *home = NULL;
+ bool home_alloc = FALSE;
+ bool netrc_alloc = FALSE;
int state=NOTHING;
char state_login=0; /* Found a login keyword */
char state_password=0; /* Found a password keyword */
char state_our_login=0; /* With specific_login, found *our* login name */
-#define NETRC DOT_CHAR "netrc"
-
-#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
- struct passwd *pw;
- pw= getpwuid(geteuid());
- if (pw) {
-#ifdef VMS
- home = decc$translate_vms(pw->pw_dir);
-#else
- home = pw->pw_dir;
-#endif
- }
-#else
- void *pw=NULL;
-#endif
-
- if(NULL == pw) {
- home = curl_getenv("HOME"); /* portable environment reader */
- if(!home) {
- return -1;
- }
- }
-
- if(strlen(home)>(sizeof(netrcbuffer)-strlen(NETRC))) {
- if(NULL==pw)
- free(home);
- return -1;
- }
-
- sprintf(netrcbuffer, "%s%s%s", home, DIR_CHAR, NETRC);
-
#ifdef CURLDEBUG
{
/* This is a hack to allow testing.
@@ -127,27 +100,49 @@ int Curl_parsenetrc(char *host,
char *override = curl_getenv("CURL_DEBUG_NETRC");
- if (override != NULL) {
+ if (override) {
printf("NETRC: overridden .netrc file: %s\n", home);
-
- if (strlen(override)+1 > sizeof(netrcbuffer)) {
- free(override);
- if(NULL==pw)
- free(home);
-
- return -1;
- }
- strcpy(netrcbuffer, override);
- free(override);
+ netrcfile = override;
+ netrc_alloc = TRUE;
}
}
#endif /* CURLDEBUG */
+ if(!netrcfile) {
+#define NETRC DOT_CHAR "netrc"
+#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
+ struct passwd *pw;
+ pw= getpwuid(geteuid());
+ if (pw) {
+#ifdef VMS
+ home = decc$translate_vms(pw->pw_dir);
+#else
+ home = pw->pw_dir;
+#endif
+ }
+#endif
+
+ if(NULL == pw) {
+ home = curl_getenv("HOME"); /* portable environment reader */
+ if(!home)
+ return -1;
+ home_alloc = TRUE;
+ }
+
+ netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
+ if(!netrcfile) {
+ if(home_alloc)
+ free(home);
+ return -1;
+ }
+ netrc_alloc = TRUE;
+ }
- file = fopen(netrcbuffer, "r");
+ file = fopen(netrcfile, "r");
if(file) {
char *tok;
char *tok_buf;
bool done=FALSE;
+ char netrcbuffer[256];
while(!done && fgets(netrcbuffer, sizeof(netrcbuffer), file)) {
tok=strtok_r(netrcbuffer, " \t\n", &tok_buf);
@@ -223,8 +218,10 @@ int Curl_parsenetrc(char *host,
fclose(file);
}
- if(NULL==pw)
+ if(home_alloc)
free(home);
+ if(netrc_alloc)
+ free(netrcfile);
return retcode;
}