2011-07-19 05:39:52 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2012-04-10 23:32:06 +08:00
|
|
|
* Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
|
2013-01-09 22:10:23 +08:00
|
|
|
* Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-07-19 05:39:52 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-07-19 05:39:52 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-10 23:32:06 +08:00
|
|
|
#include "server_setup.h"
|
2011-07-19 05:39:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a fake ntlm_auth, which is used for testing NTLM single-sign-on.
|
|
|
|
* When DEBUGBUILD is defined, libcurl invoke this tool instead of real winbind
|
|
|
|
* daemon helper /usr/bin/ntlm_auth. This tool will accept commands and
|
|
|
|
* responses with a pre-written string saved in test case test2005.
|
|
|
|
*/
|
|
|
|
|
2011-07-27 01:42:16 +08:00
|
|
|
#define ENABLE_CURLX_PRINTF
|
2011-07-19 05:39:52 +08:00
|
|
|
#include "curlx.h" /* from the private lib dir */
|
|
|
|
#include "getpart.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
/* include memdebug.h last */
|
|
|
|
#include "memdebug.h"
|
2011-07-27 01:42:16 +08:00
|
|
|
|
2011-07-19 05:39:52 +08:00
|
|
|
#ifndef DEFAULT_LOGFILE
|
|
|
|
#define DEFAULT_LOGFILE "log/fake_ntlm.log"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *serverlogfile = DEFAULT_LOGFILE;
|
|
|
|
|
2011-07-29 03:12:41 +08:00
|
|
|
/*
|
|
|
|
* Returns an allocated buffer with printable representation of input
|
|
|
|
* buffer contents or returns NULL on out of memory condition.
|
|
|
|
*/
|
|
|
|
static char *printable(char *inbuf, size_t inlength)
|
|
|
|
{
|
|
|
|
char *outbuf;
|
|
|
|
char *newbuf;
|
|
|
|
size_t newsize;
|
|
|
|
size_t outsize;
|
|
|
|
size_t outincr = 0;
|
|
|
|
size_t i, o = 0;
|
|
|
|
|
|
|
|
#define HEX_FMT_STR "[0x%02X]"
|
|
|
|
#define HEX_STR_LEN 6
|
|
|
|
#define NOTHING_STR "[NOTHING]"
|
|
|
|
#define NOTHING_LEN 9
|
|
|
|
|
|
|
|
if(!inlength)
|
|
|
|
inlength = strlen(inbuf);
|
|
|
|
|
|
|
|
if(inlength) {
|
|
|
|
outincr = ((inlength/2) < (HEX_STR_LEN+1)) ? HEX_STR_LEN+1 : inlength/2;
|
|
|
|
outsize = inlength + outincr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
outsize = NOTHING_LEN + 1;
|
|
|
|
|
|
|
|
outbuf = malloc(outsize);
|
|
|
|
if(!outbuf)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if(!inlength) {
|
2016-04-04 04:35:43 +08:00
|
|
|
snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
|
2011-07-29 03:12:41 +08:00
|
|
|
return outbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i<inlength; i++) {
|
|
|
|
|
|
|
|
if(o > outsize - (HEX_STR_LEN + 1)) {
|
|
|
|
newsize = outsize + outincr;
|
|
|
|
newbuf = realloc(outbuf, newsize);
|
|
|
|
if(!newbuf) {
|
|
|
|
free(outbuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
outbuf = newbuf;
|
|
|
|
outsize = newsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
|
|
|
|
outbuf[o] = inbuf[i];
|
|
|
|
o++;
|
|
|
|
}
|
|
|
|
else {
|
2016-04-04 04:35:43 +08:00
|
|
|
snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
|
2011-07-29 03:12:41 +08:00
|
|
|
o += HEX_STR_LEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
outbuf[o] = '\0';
|
|
|
|
|
|
|
|
return outbuf;
|
|
|
|
}
|
|
|
|
|
2011-07-28 01:04:55 +08:00
|
|
|
int main(int argc, char *argv[])
|
2011-07-19 05:39:52 +08:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
FILE *stream;
|
|
|
|
char *filename;
|
|
|
|
int error;
|
|
|
|
char *type1_input = NULL, *type3_input = NULL;
|
|
|
|
char *type1_output = NULL, *type3_output = NULL;
|
|
|
|
size_t size = 0;
|
2011-07-28 02:10:02 +08:00
|
|
|
long testnum;
|
2011-07-27 03:47:04 +08:00
|
|
|
const char *env;
|
2011-07-28 01:04:55 +08:00
|
|
|
int arg = 1;
|
2017-05-05 02:19:10 +08:00
|
|
|
const char *helper_user = "unknown";
|
|
|
|
const char *helper_proto = "unknown";
|
|
|
|
const char *helper_domain = "unknown";
|
2011-07-28 01:04:55 +08:00
|
|
|
bool use_cached_creds = FALSE;
|
2011-07-29 03:12:41 +08:00
|
|
|
char *msgbuf;
|
|
|
|
|
|
|
|
buf[0] = '\0';
|
2011-07-28 01:04:55 +08:00
|
|
|
|
|
|
|
while(argc > arg) {
|
|
|
|
if(!strcmp("--use-cached-creds", argv[arg])) {
|
|
|
|
use_cached_creds = TRUE;
|
|
|
|
arg++;
|
|
|
|
}
|
|
|
|
else if(!strcmp("--helper-protocol", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc > arg)
|
|
|
|
helper_proto = argv[arg++];
|
|
|
|
}
|
|
|
|
else if(!strcmp("--username", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc > arg)
|
|
|
|
helper_user = argv[arg++];
|
|
|
|
}
|
|
|
|
else if(!strcmp("--domain", argv[arg])) {
|
|
|
|
arg++;
|
|
|
|
if(argc > arg)
|
|
|
|
helper_domain = argv[arg++];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
puts("Usage: fake_ntlm [option]\n"
|
|
|
|
" --use-cached-creds\n"
|
|
|
|
" --helper-protocol [protocol]\n"
|
|
|
|
" --username [username]\n"
|
|
|
|
" --domain [domain]");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2011-07-19 05:39:52 +08:00
|
|
|
|
2011-07-29 04:47:42 +08:00
|
|
|
logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
|
|
|
|
helper_user, helper_proto, helper_domain,
|
2011-09-06 02:46:09 +08:00
|
|
|
(use_cached_creds) ? "yes" : "no");
|
2011-07-29 04:47:42 +08:00
|
|
|
|
2011-08-01 02:44:41 +08:00
|
|
|
env = getenv("CURL_NTLM_AUTH_TESTNUM");
|
2016-04-04 04:35:43 +08:00
|
|
|
if(env) {
|
2011-07-28 02:10:02 +08:00
|
|
|
char *endptr;
|
|
|
|
long lnum = strtol(env, &endptr, 10);
|
|
|
|
if((endptr != env + strlen(env)) || (lnum < 1L)) {
|
2011-08-01 02:44:41 +08:00
|
|
|
logmsg("Test number not valid in CURL_NTLM_AUTH_TESTNUM");
|
2011-07-28 02:10:02 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
testnum = lnum;
|
2016-04-04 04:35:43 +08:00
|
|
|
}
|
|
|
|
else {
|
2011-08-01 02:44:41 +08:00
|
|
|
logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
|
2011-07-27 03:47:04 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-08-01 02:44:41 +08:00
|
|
|
env = getenv("CURL_NTLM_AUTH_SRCDIR");
|
2016-04-04 04:35:43 +08:00
|
|
|
if(env) {
|
2011-07-27 03:47:04 +08:00
|
|
|
path = env;
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = test2file(testnum);
|
2011-07-19 05:39:52 +08:00
|
|
|
stream=fopen(filename, "rb");
|
|
|
|
if(!stream) {
|
2013-01-09 22:10:23 +08:00
|
|
|
error = errno;
|
2011-07-19 05:39:52 +08:00
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", filename);
|
2011-07-27 03:47:04 +08:00
|
|
|
logmsg("Couldn't open test file %ld", testnum);
|
2011-07-19 05:39:52 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* get the ntlm_auth input/output */
|
|
|
|
error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
|
|
|
|
fclose(stream);
|
|
|
|
if(error || size == 0) {
|
|
|
|
logmsg("getpart() type 1 input failed with error: %d", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stream=fopen(filename, "rb");
|
|
|
|
if(!stream) {
|
2013-01-09 22:10:23 +08:00
|
|
|
error = errno;
|
2011-07-19 05:39:52 +08:00
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", filename);
|
2011-07-27 03:47:04 +08:00
|
|
|
logmsg("Couldn't open test file %ld", testnum);
|
2011-07-19 05:39:52 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = 0;
|
|
|
|
error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
|
|
|
|
fclose(stream);
|
|
|
|
if(error || size == 0) {
|
|
|
|
logmsg("getpart() type 3 input failed with error: %d", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 03:41:44 +08:00
|
|
|
while(fgets(buf, sizeof(buf), stdin)) {
|
2011-07-19 05:39:52 +08:00
|
|
|
if(strcmp(buf, type1_input) == 0) {
|
|
|
|
stream=fopen(filename, "rb");
|
|
|
|
if(!stream) {
|
2013-01-09 22:10:23 +08:00
|
|
|
error = errno;
|
2011-07-19 05:39:52 +08:00
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", filename);
|
2011-07-27 03:47:04 +08:00
|
|
|
logmsg("Couldn't open test file %ld", testnum);
|
2011-07-19 05:39:52 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = 0;
|
2016-04-04 04:35:43 +08:00
|
|
|
error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
|
|
|
|
stream);
|
2011-07-19 05:39:52 +08:00
|
|
|
fclose(stream);
|
|
|
|
if(error || size == 0) {
|
|
|
|
logmsg("getpart() type 1 output failed with error: %d", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%s", type1_output);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
|
|
|
|
stream=fopen(filename, "rb");
|
|
|
|
if(!stream) {
|
2013-01-09 22:10:23 +08:00
|
|
|
error = errno;
|
2011-07-19 05:39:52 +08:00
|
|
|
logmsg("fopen() failed with error: %d %s", error, strerror(error));
|
|
|
|
logmsg("Error opening file: %s", filename);
|
2011-07-27 03:47:04 +08:00
|
|
|
logmsg("Couldn't open test file %ld", testnum);
|
2011-07-19 05:39:52 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = 0;
|
2016-04-04 04:35:43 +08:00
|
|
|
error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
|
|
|
|
stream);
|
2011-07-19 05:39:52 +08:00
|
|
|
fclose(stream);
|
|
|
|
if(error || size == 0) {
|
|
|
|
logmsg("getpart() type 3 output failed with error: %d", error);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%s", type3_output);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Unknown request\n");
|
2011-07-29 03:12:41 +08:00
|
|
|
msgbuf = printable(buf, 0);
|
|
|
|
if(msgbuf) {
|
|
|
|
logmsg("invalid input: '%s'\n", msgbuf);
|
|
|
|
free(msgbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logmsg("OOM formatting invalid input: '%s'\n", buf);
|
2011-07-19 05:39:52 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|