From ad77f760cf8223548cd214b361591dfde5443dea Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 11 Nov 2003 14:30:43 +0000 Subject: Added CURLOPT_NETRC_FILE. --- lib/netrc.c | 93 +++++++++++++++++++++++++++++------------------------------ lib/netrc.h | 3 +- lib/url.c | 9 +++++- lib/urldata.h | 2 ++ 4 files changed, 57 insertions(+), 50 deletions(-) (limited to 'lib') 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 + /* 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; } diff --git a/lib/netrc.h b/lib/netrc.h index 8b5bdc1a5..ef066b4c3 100644 --- a/lib/netrc.h +++ b/lib/netrc.h @@ -24,7 +24,8 @@ ***************************************************************************/ int Curl_parsenetrc(char *host, char *login, - char *password); + char *password, + char *filename); /* Assume: password[0]=0, host[0] != 0. * If login[0] = 0, search for login and password within a machine section * in the netrc. diff --git a/lib/url.c b/lib/url.c index 5da898c59..6b0d19170 100644 --- a/lib/url.c +++ b/lib/url.c @@ -507,6 +507,12 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, ...) */ data->set.use_netrc = va_arg(param, long); break; + case CURLOPT_NETRC_FILE: + /* + * Use this file instead of the $HOME/.netrc file + */ + data->set.netrc_file = va_arg(param, char *); + break; case CURLOPT_FOLLOWLOCATION: /* * Follow Location: header hints on a HTTP-server. @@ -2758,7 +2764,8 @@ static CURLcode CreateConnection(struct SessionHandle *data, if (data->set.use_netrc != CURL_NETRC_IGNORED) { if(Curl_parsenetrc(conn->hostname, - user, passwd)) { + user, passwd, + data->set.netrc_file)) { infof(data, "Couldn't find host %s in the .netrc file, using defaults", conn->hostname); } diff --git a/lib/urldata.h b/lib/urldata.h index d26cab6d9..aab332e80 100644 --- a/lib/urldata.h +++ b/lib/urldata.h @@ -854,6 +854,8 @@ struct UserDefined { bool upload; enum CURL_NETRC_OPTION use_netrc; /* defined in include/curl.h */ + char *netrc_file; /* if not NULL, use this instead of trying to find + $HOME/.netrc */ bool verbose; bool krb4; /* kerberos4 connection requested */ bool reuse_forbid; /* forbidden to be reused, close after use */ -- cgit v1.2.3