HOSTVALID /* this is "our" machine! */
};
+#define NETRC_FILE_MISSING 1
+#define NETRC_FAILED -1
+#define NETRC_SUCCESS 0
+
/*
- * @unittest: 1304
- *
- * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
- * in.
+ * Returns zero on success.
*/
-int Curl_parsenetrc(const char *host,
- char **loginp,
- char **passwordp,
- bool *login_changed,
- bool *password_changed,
- char *netrcfile)
+static int parsenetrc(const char *host,
+ char **loginp,
+ char **passwordp,
+ bool *login_changed,
+ bool *password_changed,
+ char *netrcfile)
{
FILE *file;
- int retcode = 1;
+ int retcode = NETRC_FILE_MISSING;
char *login = *loginp;
char *password = *passwordp;
bool specific_login = (login && *login != 0);
bool login_alloc = FALSE;
bool password_alloc = FALSE;
- bool netrc_alloc = FALSE;
enum host_lookup_state state = NOTHING;
char state_login = 0; /* Found a login keyword */
int state_our_login = FALSE; /* With specific_login, found *our* login
name */
-#define NETRC DOT_CHAR "netrc"
-
- if(!netrcfile) {
- bool home_alloc = FALSE;
- char *home = curl_getenv("HOME"); /* portable environment reader */
- if(home) {
- home_alloc = TRUE;
-#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
- }
- else {
- struct passwd pw, *pw_res;
- char pwbuf[1024];
- if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
- && pw_res) {
- home = strdup(pw.pw_dir);
- if(!home)
- return -1;
- home_alloc = TRUE;
- }
-#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
- }
- else {
- struct passwd *pw;
- pw = getpwuid(geteuid());
- if(pw) {
- home = pw->pw_dir;
- }
-#endif
- }
-
- if(!home)
- return retcode; /* no home directory found (or possibly out of memory) */
-
- netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
- if(home_alloc)
- free(home);
- if(!netrcfile) {
- return -1;
- }
- netrc_alloc = TRUE;
- }
+ DEBUGASSERT(netrcfile);
file = fopen(netrcfile, FOPEN_READTEXT);
- if(netrc_alloc)
- free(netrcfile);
if(file) {
char *tok;
char *tok_buf;
}
else if(strcasecompare("default", tok)) {
state = HOSTVALID;
- retcode = 0; /* we did find our host */
+ retcode = NETRC_SUCCESS; /* we did find our host */
}
break;
case HOSTFOUND:
if(strcasecompare(host, tok)) {
/* and yes, this is our host! */
state = HOSTVALID;
- retcode = 0; /* we did find our host */
+ retcode = NETRC_SUCCESS; /* we did find our host */
}
else
/* not our host */
}
login = strdup(tok);
if(!login) {
- retcode = -1; /* allocation failed */
+ retcode = NETRC_FAILED; /* allocation failed */
goto out;
}
login_alloc = TRUE;
}
password = strdup(tok);
if(!password) {
- retcode = -1; /* allocation failed */
+ retcode = NETRC_FAILED; /* allocation failed */
goto out;
}
password_alloc = TRUE;
out:
if(!retcode) {
+ /* success */
*login_changed = FALSE;
*password_changed = FALSE;
if(login_alloc) {
return retcode;
}
+/*
+ * @unittest: 1304
+ *
+ * *loginp and *passwordp MUST be allocated if they aren't NULL when passed
+ * in.
+ */
+int Curl_parsenetrc(const char *host,
+ char **loginp,
+ char **passwordp,
+ bool *login_changed,
+ bool *password_changed,
+ char *netrcfile)
+{
+ int retcode = 1;
+ char *filealloc = NULL;
+
+ if(!netrcfile) {
+ char *home = curl_getenv("HOME"); /* portable environment reader */
+ if(home) {
+#if defined(HAVE_GETPWUID_R) && defined(HAVE_GETEUID)
+ }
+ else {
+ struct passwd pw, *pw_res;
+ char pwbuf[1024];
+ if(!getpwuid_r(geteuid(), &pw, pwbuf, sizeof(pwbuf), &pw_res)
+ && pw_res) {
+ home = strdup(pw.pw_dir);
+ if(!home)
+ return -1;
+ }
+#elif defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
+ }
+ else {
+ struct passwd *pw;
+ pw = getpwuid(geteuid());
+ if(pw) {
+ home = pw->pw_dir;
+ }
+#endif
+ }
+
+ if(!home)
+ return retcode; /* no home directory found (or possibly out of
+ memory) */
+
+ filealloc = curl_maprintf("%s%s.netrc", home, DIR_CHAR);
+ if(!filealloc)
+ return -1;
+ retcode = parsenetrc(host, loginp, passwordp, login_changed,
+ password_changed, filealloc);
+ free(filealloc);
+#ifdef WIN32
+ if(retcode == NETRC_FILE_MISSING) {
+ /* fallback to the old-style "_netrc" file */
+ filealloc = curl_maprintf("%s%s_netrc", home, DIR_CHAR);
+ if(!filealloc)
+ return -1;
+ retcode = parsenetrc(host, loginp, passwordp, login_changed,
+ password_changed, filealloc);
+ free(filealloc);
+ }
+#endif
+ }
+ else
+ retcode = parsenetrc(host, loginp, passwordp, login_changed,
+ password_changed, netrcfile);
+ return retcode;
+}
+
#endif