Added CURLOPT_NETRC_FILE.

This commit is contained in:
Daniel Stenberg 2003-11-11 14:30:43 +00:00
parent a50d2a45d7
commit ad77f760cf
7 changed files with 87 additions and 51 deletions

View file

@ -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;
}
file = fopen(netrcbuffer, "r");
netrcfile = curl_maprintf("%s%s%s", home, DIR_CHAR, NETRC);
if(!netrcfile) {
if(home_alloc)
free(home);
return -1;
}
netrc_alloc = TRUE;
}
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;
}

View file

@ -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.

View file

@ -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);
}

View file

@ -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 */