#include "strequal.h"
#include "base64.h"
#include "http_ntlm.h"
+#include "url.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
(((x) >>16)&0xff), ((x)>>24)
/* this is for creating ntlm header output */
-CURLcode Curl_output_ntlm(struct connectdata *conn)
+CURLcode Curl_output_ntlm(struct connectdata *conn,
+ bool proxy)
{
const char *domain=""; /* empty */
const char *host=""; /* empty */
int domoff; /* domain name offset */
int size;
char *base64=NULL;
-
unsigned char ntlm[256]; /* enough, unless the host/domain is very long */
+
+ /* point to the address of the pointer that holds the string to sent to the
+ server, which is for a plain host or for a HTTP proxy */
+ char **allocuserpwd;
+
+ /* point to the name and password for this */
+ char *userp;
+ char *passwdp;
+
+ if(proxy) {
+ allocuserpwd = &conn->allocptr.proxyuserpwd;
+ userp = conn->proxyuser;
+ passwdp = conn->proxypasswd;
+ }
+ else {
+ allocuserpwd = &conn->allocptr.userpwd;
+ userp = conn->user;
+ passwdp = conn->passwd;
+ }
+
switch(conn->ntlm.state) {
case NTLMSTATE_TYPE1:
default: /* for the weird cases we (re)start here */
size = Curl_base64_encode(ntlm, size, &base64);
if(size >0 ) {
- if(conn->allocptr.userpwd)
- free(conn->allocptr.userpwd);
- conn->allocptr.userpwd = aprintf("Authorization: NTLM %s\r\n",
- base64);
+ Curl_safefree(*allocuserpwd);
+ *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
+ proxy?"Proxy-":"",
+ base64);
free(base64);
}
else
const char *user;
int userlen;
- user = strchr(conn->user, '\\');
+ user = strchr(userp, '\\');
if(!user)
- user = strchr(conn->user, '/');
+ user = strchr(userp, '/');
if (user) {
- domain = conn->user;
+ domain = userp;
domlen = user - domain;
user++;
}
else
- user = conn->user;
+ user = userp;
userlen = strlen(user);
- mkhash(conn->passwd, &conn->ntlm.nonce[0], lmresp
+ mkhash(passwdp, &conn->ntlm.nonce[0], lmresp
#ifdef USE_NTRESPONSES
, ntresp
#endif
size = Curl_base64_encode(ntlm, size, &base64);
if(size >0 ) {
- if(conn->allocptr.userpwd)
- free(conn->allocptr.userpwd);
- conn->allocptr.userpwd = aprintf("Authorization: NTLM %s\r\n",
- base64);
+ Curl_safefree(*allocuserpwd);
+ *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
+ proxy?"Proxy-":"",
+ base64);
free(base64);
}
else
case NTLMSTATE_TYPE3:
/* connection is already authenticated,
* don't send a header in future requests */
- if(conn->allocptr.userpwd) {
- free(conn->allocptr.userpwd);
- conn->allocptr.userpwd=NULL;
+ if(*allocuserpwd) {
+ free(*allocuserpwd);
+ *allocuserpwd=NULL;
}
break;
}