2021-04-29 12:47:31 +02:00
|
|
|
/*
|
|
|
|
* Unix interface for ntlm_auth
|
|
|
|
*
|
|
|
|
* Copyright 2005, 2006 Kai Blin
|
|
|
|
* Copyright 2021 Hans Leidekker for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2021-08-11 18:11:58 +02:00
|
|
|
#include <stdlib.h>
|
2021-04-29 12:47:31 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winternl.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "sspi.h"
|
|
|
|
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "unixlib.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ntlm);
|
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(winediag);
|
|
|
|
|
2021-04-29 12:47:33 +02:00
|
|
|
#define INITIAL_BUFFER_SIZE 200
|
|
|
|
|
2021-11-09 11:40:39 +01:00
|
|
|
struct com_buf
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
unsigned int size;
|
|
|
|
unsigned int offset;
|
|
|
|
};
|
|
|
|
|
2021-04-29 12:47:33 +02:00
|
|
|
static SECURITY_STATUS read_line( struct ntlm_ctx *ctx, unsigned int *offset )
|
|
|
|
{
|
|
|
|
char *newline;
|
2021-11-09 11:41:29 +01:00
|
|
|
struct com_buf *com_buf = (struct com_buf *)(ULONG_PTR)ctx->com_buf;
|
2021-04-29 12:47:33 +02:00
|
|
|
|
2021-11-09 11:40:39 +01:00
|
|
|
if (!com_buf)
|
2021-04-29 12:47:33 +02:00
|
|
|
{
|
2021-11-09 11:40:39 +01:00
|
|
|
if (!(com_buf = malloc( sizeof(*com_buf) ))) return SEC_E_INSUFFICIENT_MEMORY;
|
|
|
|
if (!(com_buf->buffer = malloc( INITIAL_BUFFER_SIZE )))
|
|
|
|
{
|
|
|
|
free( com_buf );
|
2021-04-29 12:47:33 +02:00
|
|
|
return SEC_E_INSUFFICIENT_MEMORY;
|
2021-11-09 11:40:39 +01:00
|
|
|
}
|
|
|
|
com_buf->size = INITIAL_BUFFER_SIZE;
|
|
|
|
com_buf->offset = 0;
|
2021-11-09 11:41:29 +01:00
|
|
|
ctx->com_buf = (ULONG_PTR)com_buf;
|
2021-04-29 12:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ssize_t size;
|
2021-11-09 11:40:39 +01:00
|
|
|
if (com_buf->offset + INITIAL_BUFFER_SIZE > com_buf->size)
|
2021-04-29 12:47:33 +02:00
|
|
|
{
|
2021-11-09 11:40:39 +01:00
|
|
|
char *buf = realloc( com_buf->buffer, com_buf->size + INITIAL_BUFFER_SIZE );
|
2021-04-29 12:47:33 +02:00
|
|
|
if (!buf) return SEC_E_INSUFFICIENT_MEMORY;
|
2021-11-09 11:40:39 +01:00
|
|
|
com_buf->size += INITIAL_BUFFER_SIZE;
|
|
|
|
com_buf->buffer = buf;
|
2021-04-29 12:47:33 +02:00
|
|
|
}
|
2021-11-09 11:40:39 +01:00
|
|
|
size = read( ctx->pipe_in, com_buf->buffer + com_buf->offset, com_buf->size - com_buf->offset );
|
2021-04-29 12:47:33 +02:00
|
|
|
if (size <= 0) return SEC_E_INTERNAL_ERROR;
|
|
|
|
|
2021-11-09 11:40:39 +01:00
|
|
|
com_buf->offset += size;
|
|
|
|
newline = memchr( com_buf->buffer, '\n', com_buf->offset );
|
2021-04-29 12:47:33 +02:00
|
|
|
} while (!newline);
|
|
|
|
|
|
|
|
/* if there's a newline character, and we read more than that newline, we have to store the offset so we can
|
|
|
|
preserve the additional data */
|
2021-11-09 11:40:39 +01:00
|
|
|
if (newline != com_buf->buffer + com_buf->offset) *offset = (com_buf->buffer + com_buf->offset) - (newline + 1);
|
2021-04-29 12:47:33 +02:00
|
|
|
else *offset = 0;
|
|
|
|
|
|
|
|
*newline = 0;
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
static NTSTATUS ntlm_chat( void *args )
|
2021-04-29 12:47:33 +02:00
|
|
|
{
|
2021-11-09 11:41:29 +01:00
|
|
|
const struct chat_params *params = args;
|
2021-08-11 19:49:05 +02:00
|
|
|
struct ntlm_ctx *ctx = params->ctx;
|
2021-11-09 11:40:39 +01:00
|
|
|
struct com_buf *com_buf;
|
2021-04-29 12:47:33 +02:00
|
|
|
SECURITY_STATUS status = SEC_E_OK;
|
|
|
|
unsigned int offset;
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
write( ctx->pipe_out, params->buf, strlen(params->buf) );
|
2021-04-29 12:47:33 +02:00
|
|
|
write( ctx->pipe_out, "\n", 1 );
|
|
|
|
|
|
|
|
if ((status = read_line( ctx, &offset )) != SEC_E_OK) return status;
|
2021-11-09 11:41:29 +01:00
|
|
|
com_buf = (struct com_buf *)(ULONG_PTR)ctx->com_buf;
|
2021-11-09 11:40:39 +01:00
|
|
|
*params->retlen = strlen( com_buf->buffer );
|
2021-04-29 12:47:33 +02:00
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
if (*params->retlen > params->buflen) return SEC_E_BUFFER_TOO_SMALL;
|
|
|
|
if (*params->retlen < 2) return SEC_E_ILLEGAL_MESSAGE;
|
2021-11-09 11:40:39 +01:00
|
|
|
if (!strncmp( com_buf->buffer, "ERR", 3 )) return SEC_E_INVALID_TOKEN;
|
2021-04-29 12:47:33 +02:00
|
|
|
|
2021-11-09 11:40:39 +01:00
|
|
|
memcpy( params->buf, com_buf->buffer, *params->retlen + 1 );
|
2021-04-29 12:47:33 +02:00
|
|
|
|
2021-11-09 11:40:39 +01:00
|
|
|
if (!offset) com_buf->offset = 0;
|
2021-04-29 12:47:33 +02:00
|
|
|
else
|
|
|
|
{
|
2021-11-09 11:40:39 +01:00
|
|
|
memmove( com_buf->buffer, com_buf->buffer + com_buf->offset, offset );
|
|
|
|
com_buf->offset = offset;
|
2021-04-29 12:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
static NTSTATUS ntlm_cleanup( void *args )
|
2021-04-29 12:47:31 +02:00
|
|
|
{
|
2021-11-09 11:41:29 +01:00
|
|
|
struct ntlm_ctx *ctx = args;
|
|
|
|
struct com_buf *com_buf = (struct com_buf *)(ULONG_PTR)ctx->com_buf;
|
2021-08-11 19:49:05 +02:00
|
|
|
|
|
|
|
if (!ctx || (ctx->mode != MODE_CLIENT && ctx->mode != MODE_SERVER)) return STATUS_INVALID_HANDLE;
|
2021-04-29 12:47:31 +02:00
|
|
|
ctx->mode = MODE_INVALID;
|
|
|
|
|
|
|
|
/* closing stdin will terminate ntlm_auth */
|
|
|
|
close( ctx->pipe_out );
|
|
|
|
close( ctx->pipe_in );
|
|
|
|
|
|
|
|
if (ctx->pid > 0) /* reap child */
|
|
|
|
{
|
|
|
|
pid_t ret;
|
|
|
|
do {
|
|
|
|
ret = waitpid( ctx->pid, NULL, 0 );
|
|
|
|
} while (ret < 0 && errno == EINTR);
|
|
|
|
}
|
|
|
|
|
2021-11-09 11:40:39 +01:00
|
|
|
if (com_buf) free( com_buf->buffer );
|
|
|
|
free( com_buf );
|
2021-08-11 19:49:05 +02:00
|
|
|
return STATUS_SUCCESS;
|
2021-04-29 12:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
static NTSTATUS ntlm_fork( void *args )
|
2021-04-29 12:47:31 +02:00
|
|
|
{
|
2021-11-09 11:41:29 +01:00
|
|
|
const struct fork_params *params = args;
|
2021-08-11 19:49:05 +02:00
|
|
|
struct ntlm_ctx *ctx = params->ctx;
|
2021-04-29 12:47:31 +02:00
|
|
|
int pipe_in[2], pipe_out[2];
|
|
|
|
|
|
|
|
#ifdef HAVE_PIPE2
|
|
|
|
if (pipe2( pipe_in, O_CLOEXEC ) < 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (pipe( pipe_in ) < 0 ) return SEC_E_INTERNAL_ERROR;
|
|
|
|
fcntl( pipe_in[0], F_SETFD, FD_CLOEXEC );
|
|
|
|
fcntl( pipe_in[1], F_SETFD, FD_CLOEXEC );
|
|
|
|
}
|
|
|
|
#ifdef HAVE_PIPE2
|
|
|
|
if (pipe2( pipe_out, O_CLOEXEC ) < 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (pipe( pipe_out ) < 0)
|
|
|
|
{
|
|
|
|
close( pipe_in[0] );
|
|
|
|
close( pipe_in[1] );
|
|
|
|
return SEC_E_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
fcntl( pipe_out[0], F_SETFD, FD_CLOEXEC );
|
|
|
|
fcntl( pipe_out[1], F_SETFD, FD_CLOEXEC );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ctx->pid = fork())) /* child */
|
|
|
|
{
|
|
|
|
dup2( pipe_out[0], 0 );
|
|
|
|
close( pipe_out[0] );
|
|
|
|
close( pipe_out[1] );
|
|
|
|
|
|
|
|
dup2( pipe_in[1], 1 );
|
|
|
|
close( pipe_in[0] );
|
|
|
|
close( pipe_in[1] );
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
execvp( params->argv[0], params->argv );
|
2021-04-29 12:47:31 +02:00
|
|
|
|
|
|
|
write( 1, "BH\n", 3 );
|
|
|
|
_exit( 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ctx->pipe_in = pipe_in[0];
|
|
|
|
close( pipe_in[1] );
|
|
|
|
ctx->pipe_out = pipe_out[1];
|
|
|
|
close( pipe_out[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NTLM_AUTH_MAJOR_VERSION 3
|
|
|
|
#define NTLM_AUTH_MINOR_VERSION 0
|
|
|
|
#define NTLM_AUTH_MICRO_VERSION 25
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
static NTSTATUS ntlm_check_version( void *args )
|
2021-04-29 12:47:31 +02:00
|
|
|
{
|
2021-08-11 18:11:58 +02:00
|
|
|
struct ntlm_ctx ctx = { 0 };
|
2021-04-29 12:47:31 +02:00
|
|
|
char *argv[3], buf[80];
|
2021-08-11 19:49:05 +02:00
|
|
|
NTSTATUS status = STATUS_DLL_NOT_FOUND;
|
|
|
|
struct fork_params params = { &ctx, argv };
|
2021-04-29 12:47:31 +02:00
|
|
|
int len;
|
|
|
|
|
|
|
|
argv[0] = (char *)"ntlm_auth";
|
|
|
|
argv[1] = (char *)"--version";
|
|
|
|
argv[2] = NULL;
|
2021-08-11 19:49:05 +02:00
|
|
|
if (ntlm_fork( ¶ms ) != SEC_E_OK) return status;
|
2021-04-29 12:47:31 +02:00
|
|
|
|
2021-08-11 18:11:58 +02:00
|
|
|
if ((len = read( ctx.pipe_in, buf, sizeof(buf) - 1 )) > 8)
|
2021-04-29 12:47:31 +02:00
|
|
|
{
|
|
|
|
char *newline;
|
|
|
|
int major = 0, minor = 0, micro = 0;
|
|
|
|
|
|
|
|
if ((newline = memchr( buf, '\n', len ))) *newline = 0;
|
|
|
|
else buf[len] = 0;
|
|
|
|
|
|
|
|
if (sscanf( buf, "Version %d.%d.%d", &major, &minor, µ ) == 3)
|
|
|
|
{
|
|
|
|
if (((major > NTLM_AUTH_MAJOR_VERSION) ||
|
|
|
|
(major == NTLM_AUTH_MAJOR_VERSION && minor > NTLM_AUTH_MINOR_VERSION) ||
|
|
|
|
(major == NTLM_AUTH_MAJOR_VERSION && minor == NTLM_AUTH_MINOR_VERSION &&
|
|
|
|
micro >= NTLM_AUTH_MICRO_VERSION)))
|
|
|
|
{
|
|
|
|
TRACE( "detected ntlm_auth version %d.%d.%d\n", major, minor, micro );
|
2021-08-11 19:49:05 +02:00
|
|
|
status = STATUS_SUCCESS;
|
2021-04-29 12:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
if (status) ERR_(winediag)( "ntlm_auth was not found or is outdated. "
|
2021-04-29 12:47:31 +02:00
|
|
|
"Make sure that ntlm_auth >= %d.%d.%d is in your path. "
|
|
|
|
"Usually, you can find it in the winbind package of your distribution.\n",
|
|
|
|
NTLM_AUTH_MAJOR_VERSION, NTLM_AUTH_MINOR_VERSION, NTLM_AUTH_MICRO_VERSION );
|
2021-11-09 11:41:29 +01:00
|
|
|
ntlm_cleanup( &ctx );
|
2021-08-11 19:49:05 +02:00
|
|
|
return status;
|
2021-04-29 12:47:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 19:49:05 +02:00
|
|
|
const unixlib_entry_t __wine_unix_call_funcs[] =
|
2021-04-29 12:47:31 +02:00
|
|
|
{
|
2021-04-29 12:47:33 +02:00
|
|
|
ntlm_chat,
|
2021-04-29 12:47:31 +02:00
|
|
|
ntlm_cleanup,
|
|
|
|
ntlm_fork,
|
2021-08-11 19:49:05 +02:00
|
|
|
ntlm_check_version,
|
2021-04-29 12:47:31 +02:00
|
|
|
};
|
2021-11-09 11:41:29 +01:00
|
|
|
|
|
|
|
#ifdef _WIN64
|
|
|
|
|
|
|
|
typedef ULONG PTR32;
|
|
|
|
|
|
|
|
static NTSTATUS wow64_ntlm_chat( void *args )
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
PTR32 ctx;
|
|
|
|
PTR32 buf;
|
|
|
|
UINT buflen;
|
|
|
|
PTR32 retlen;
|
|
|
|
} const *params32 = args;
|
|
|
|
|
|
|
|
struct chat_params params =
|
|
|
|
{
|
|
|
|
ULongToPtr(params32->ctx),
|
|
|
|
ULongToPtr(params32->buf),
|
|
|
|
params32->buflen,
|
|
|
|
ULongToPtr(params32->retlen)
|
|
|
|
};
|
|
|
|
|
|
|
|
return ntlm_chat( ¶ms );
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS wow64_ntlm_fork( void *args )
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
PTR32 ctx;
|
|
|
|
PTR32 argv;
|
|
|
|
} const *params32 = args;
|
|
|
|
|
|
|
|
struct fork_params params;
|
|
|
|
PTR32 *argv32 = ULongToPtr(params32->argv);
|
|
|
|
char **argv;
|
|
|
|
NTSTATUS ret;
|
|
|
|
int i, argc = 0;
|
|
|
|
|
|
|
|
while (argv32[argc]) argc++;
|
|
|
|
argv = malloc( (argc + 1) * sizeof(*argv) );
|
|
|
|
for (i = 0; i <= argc; i++) argv[i] = ULongToPtr( argv32[i] );
|
|
|
|
|
|
|
|
params.ctx = ULongToPtr(params32->ctx);
|
|
|
|
params.argv = argv;
|
|
|
|
ret = ntlm_fork( ¶ms );
|
|
|
|
free( argv );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unixlib_entry_t __wine_unix_call_wow64_funcs[] =
|
|
|
|
{
|
|
|
|
wow64_ntlm_chat,
|
|
|
|
ntlm_cleanup,
|
|
|
|
wow64_ntlm_fork,
|
|
|
|
ntlm_check_version,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _WIN64 */
|