From 08cf6780ba8ea242cb451f07e14bb572079e22cf Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 8 Oct 2008 10:39:43 +0000 Subject: - Igor Novoseltsev brought a patch that introduced two new options to curl_easy_setopt: CURLOPT_USERNAME and CURLOPT_PASSWORD that sort of deprecates the good old CURLOPT_USERPWD since they allow applications to set the user name and password independently and perhaps more importantly allow both to contain colon(s) which CURLOPT_USERPWD doesn't fully support. --- lib/url.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------- lib/urldata.h | 3 ++- 2 files changed, 49 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/url.c b/lib/url.c index 17b34c68e..a1174a63b 100644 --- a/lib/url.c +++ b/lib/url.c @@ -1500,7 +1500,45 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, /* * user:password to use in the operation */ - result = setstropt(&data->set.str[STRING_USERPWD], + { + char* userpwd = va_arg(param, char *); + char* separator = strchr(userpwd, ':'); + if (separator != NULL) { + + /* store username part of option */ + char * p; + size_t username_len = (size_t)(separator-userpwd); + p = malloc(username_len+1); + if(!p) + result = CURLE_OUT_OF_MEMORY; + else { + memcpy(p, userpwd, username_len); + p[username_len] = '\0'; + data->set.str[STRING_USERNAME] = p; + } + + /* store password part of option */ + if (result == CURLE_OK) { + result = setstropt(&data->set.str[STRING_PASSWORD], separator+1); + } + } + else { + result = setstropt(&data->set.str[STRING_USERNAME], userpwd); + } + } + break; + case CURLOPT_USERNAME: + /* + * user:password to use in the operation + */ + result = setstropt(&data->set.str[STRING_USERNAME], + va_arg(param, char *)); + break; + case CURLOPT_PASSWORD: + /* + * user:password to use in the operation + */ + result = setstropt(&data->set.str[STRING_PASSWORD], va_arg(param, char *)); break; case CURLOPT_POSTQUOTE: @@ -3701,12 +3739,13 @@ static void override_userpass(struct SessionHandle *data, struct connectdata *conn, char *user, char *passwd) { - if(data->set.str[STRING_USERPWD] != NULL) { - /* the name is given, get user+password */ - sscanf(data->set.str[STRING_USERPWD], - "%" MAX_CURL_USER_LENGTH_TXT "[^:]:" - "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^\n]", - user, passwd); + if(data->set.str[STRING_USERNAME] != NULL) { + strncpy(user, data->set.str[STRING_USERNAME], MAX_CURL_USER_LENGTH); + user[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/ + } + if(data->set.str[STRING_PASSWORD] != NULL) { + strncpy(passwd, data->set.str[STRING_PASSWORD], MAX_CURL_PASSWORD_LENGTH); + passwd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/ } conn->bits.netrc = FALSE; @@ -3985,7 +4024,7 @@ static CURLcode create_conn(struct SessionHandle *data, && (conn->proxytype == CURLPROXY_HTTP)); - conn->bits.user_passwd = (bool)(NULL != data->set.str[STRING_USERPWD]); + conn->bits.user_passwd = (bool)(NULL != data->set.str[STRING_USERNAME]); conn->bits.proxy_user_passwd = (bool)(NULL != data->set.str[STRING_PROXYUSERPWD]); conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy; conn->bits.ftp_use_epsv = data->set.ftp_use_epsv; diff --git a/lib/urldata.h b/lib/urldata.h index ee0df8c12..a1a95ef44 100644 --- a/lib/urldata.h +++ b/lib/urldata.h @@ -1334,10 +1334,11 @@ enum dupstring { STRING_SSL_EGDSOCKET, /* path to file containing the EGD daemon socket */ STRING_SSL_RANDOM_FILE, /* path to file containing "random" data */ STRING_USERAGENT, /* User-Agent string */ - STRING_USERPWD, /* , if used */ STRING_SSH_HOST_PUBLIC_KEY_MD5, /* md5 of host public key in ascii hex */ STRING_SSL_CRLFILE, /* crl file to check certificate */ STRING_SSL_ISSUERCERT, /* issuer cert file to check certificate */ + STRING_USERNAME, /* , if used */ + STRING_PASSWORD, /* , if used */ /* -- end of strings -- */ STRING_LAST /* not used, just an end-of-list marker */ -- cgit v1.2.3