Converged Win Unix code in auth.c, move utf routine declarations to separate header

This commit is contained in:
Manoj Ampalam 2016-10-16 01:00:27 -07:00
parent 8566745e2d
commit c25f6a0d5e
12 changed files with 45 additions and 151 deletions

67
auth.c
View File

@ -379,72 +379,6 @@ auth_root_allowed(const char *method)
* This returns a buffer allocated by xmalloc.
*/
/*
* Win32 implementation uses UTF16 names.
*/
#ifdef WIN32_FIXME
char *expand_authorized_keys(const char *filename, struct passwd *pw)
{
wchar_t *file_w, ret[MAXPATHLEN], pw_name_w[MAXPATHLEN], filename_w[MAXPATHLEN], pw_dir_w[MAXPATHLEN];
char* expanded_utf8[MAXPATHLEN];
int i;
wchar_t *slash;
if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filename_w, MAXPATHLEN) == 0 ||
MultiByteToWideChar(CP_UTF8, 0, pw->pw_name, -1, pw_name_w, MAXPATHLEN) == 0 ||
MultiByteToWideChar(CP_UTF8, 0, pw->pw_dir, -1, pw_dir_w, MAXPATHLEN) == 0)
fatal("expand_authorized_keys -MultiByteToWideChar failed" );
file_w = percent_expand_w(filename_w, L"h", pw_dir_w,
L"u", pw_name_w, (char *) NULL);
/*
* Replace '/' with '\'
*/
slash = file_w;
while ((slash = wcschr(slash, L'/')))
{
*slash = L'\\';
}
/*
* Absolute path given.
*/
if (wcschr(file_w, ':'))
{
i = _snwprintf(ret, sizeof(ret), L"%ls", file_w);
}
/*
* Relative path given. Expand to user homedir.
*/
else
{
i = _snwprintf(ret, sizeof(ret), L"%ls\\%ls", pw->pw_dir, file_w);
}
if (i < 0 || (size_t) i >= sizeof(ret))
{
fatal("expand_authorized_keys: path too long");
}
if (WideCharToMultiByte(CP_UTF8, 0, ret, -1, expanded_utf8, MAXPATHLEN, NULL, NULL) == 0)
fatal("expand_authorized_keys: WideCharToMultiByte failed");
free(file_w);
return (xstrdup(expanded_utf8));
}
#else /* WIN32_FIXME */
char *
expand_authorized_keys(const char *filename, struct passwd *pw)
{
@ -467,7 +401,6 @@ expand_authorized_keys(const char *filename, struct passwd *pw)
free(file);
return (xstrdup(ret));
}
#endif /* WIN32_FIXME */
char *
authorized_principals_file(struct passwd *pw)

View File

@ -172,6 +172,7 @@
<ClInclude Include="$(OpenSSH-Src-Path)\contrib\win32\win32compat\signal_internal.h" />
<ClInclude Include="..\win32compat\inc\pwd.h" />
<ClInclude Include="..\win32compat\inc\sys\param.h" />
<ClInclude Include="..\win32compat\inc\utf.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -60,6 +60,9 @@
<ClInclude Include="..\win32compat\inc\sys\param.h">
<Filter>inc\sys</Filter>
</ClInclude>
<ClInclude Include="..\win32compat\inc\utf.h">
<Filter>inc</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="inc">

View File

@ -0,0 +1,12 @@
/*
* Author: Manoj Ampalam <manoj.ampalam@microsoft.com>
*
* UTF-16 <--> UTF-8 definitions
*/
#ifndef UTF_H
#define UTF_H 1
wchar_t* utf8_to_utf16(const char *);
char* utf16_to_utf8(const wchar_t*);
#endif

View File

@ -8,6 +8,7 @@
#include <WS2tcpip.h>
#include <stdio.h>
#include "defs.h"
#include "utf.h"
typedef struct w32_fd_set_ {

View File

@ -36,17 +36,17 @@
#define SECURITY_WIN32
#include <security.h>
#include "inc\pwd.h"
#include "inc\utf.h"
static struct passwd pw;
static char* pw_shellpath = "ssh-shellhost.exe";
char* utf16_to_utf8(const wchar_t*);
wchar_t* utf8_to_utf16(const char *);
int
initialize_pw() {
if (pw.pw_shell != pw_shellpath) {
memset(&pw, 0, sizeof(pw));
pw.pw_shell = pw_shellpath;
pw.pw_passwd = "\0";
}
return 0;
}

View File

@ -46,6 +46,8 @@
#include "digest.h"
#include "agent.h"
#include <utf.h>
static int use_privsep = -1;
Buffer cfg;
ServerOptions options;
@ -85,8 +87,6 @@ int GetCurrentModulePath(wchar_t *path, int pathSize)
return -1;
}
char* utf16_to_utf8(const wchar_t*);
int load_config() {
wchar_t basePath[MAX_PATH] = { 0 };
wchar_t path[MAX_PATH] = { 0 };

View File

@ -399,7 +399,6 @@ w32_lseek(int fd, long offset, int origin) {
return fileio_lseek(fd_table.w32_ios[fd], offset, origin);
}
wchar_t* utf8_to_utf16(const char *);
int
w32_mkdir(const char *path_utf8, unsigned short mode) {
wchar_t *path_utf16 = utf8_to_utf16(path_utf8);

View File

@ -31,9 +31,9 @@
*/
#include <Windows.h>
#include "inc\utf.h"
int main(int, char **);
char* utf16_to_utf8(const wchar_t*);
void w32posix_initialize();
int

58
misc.c
View File

@ -646,64 +646,6 @@ percent_expand(const char *string, ...)
#undef EXPAND_MAX_KEYS
}
#ifdef WIN32_FIXME
wchar_t *percent_expand_w(const wchar_t *string, ...)
{
#define EXPAND_MAX_KEYS 16
u_int num_keys, i, j;
struct {
const wchar_t *key;
const wchar_t *repl;
} keys[EXPAND_MAX_KEYS];
wchar_t buf[4096];
wchar_t *aptr = NULL;
va_list ap;
/* Gather keys */
va_start(ap, string);
for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
keys[num_keys].key = va_arg(ap, wchar_t *);
if (keys[num_keys].key == NULL)
break;
keys[num_keys].repl = va_arg(ap, wchar_t *);
if (keys[num_keys].repl == NULL)
fatal("%s: NULL replacement", __func__);
}
if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, wchar_t *) != NULL)
fatal("%s: too many keys", __func__);
va_end(ap);
/* Expand string */
*buf = L'\0';
for (i = 0; *string != L'\0'; string++) {
if (*string != L'%') {
append:
buf[i++] = *string;
if (i >= sizeof(buf))
fatal("%s: string too long", __func__);
buf[i] = L'\0';
continue;
}
string++;
/* %% case */
if (*string == L'%')
goto append;
for (j = 0; j < num_keys; j++) {
if (wcschr(keys[j].key, *string) != NULL) {
aptr = wcsncat(buf, keys[j].repl, sizeof(buf));
buf[sizeof(buf)-1] = 0;
if (aptr == NULL)
fatal("%s: string too long", __func__);
break;
}
}
if (j >= num_keys)
fatal("%s: unknown key %%%c", __func__, *string);
}
return (_wcsdup(buf));
#undef EXPAND_MAX_KEYS
}
#endif
/*
* Read an entire line from a public key file into a static buffer, discarding
* lines that exceed the buffer size. Returns 0 on success, -1 on failure.

3
misc.h
View File

@ -50,9 +50,6 @@ char *colon(char *);
long convtime(const char *);
char *tilde_expand_filename(const char *, uid_t);
char *percent_expand(const char *, ...) __attribute__((__sentinel__));
#ifdef WIN32_FIXME
wchar_t *percent_expand_w(const wchar_t *, ...) __attribute__((__sentinel__));
#endif
char *tohex(const void *, size_t);
void sanitise_stdfd(void);
void ms_subtract_diff(struct timeval *, int *);

View File

@ -509,6 +509,7 @@ int
do_exec_no_pty(Session *s, const char *command)
{
#ifdef WIN32_FIXME
wchar_t* pw_dir_utf16 = utf8_to_utf16(s->pw->pw_dir);
/*
* Win32 code.
@ -707,20 +708,22 @@ do_exec_no_pty(Session *s, const char *command)
* also change subsequent calls to SetEnvironmentVariable
*/
_chdir(s->pw->pw_dir);
_wchdir(pw_dir_utf16);
SetEnvironmentVariableW(L"HOME", pw_dir_utf16);
SetEnvironmentVariableW(L"USERPROFILE", pw_dir_utf16);
SetEnvironmentVariableW(L"HOME", s -> pw -> pw_dir);
wchar_t *wstr, wchr;
wstr = wcschr(s->pw->pw_dir, ':');
wstr = wcschr(pw_dir_utf16, L':');
if (wstr) {
wchr = *(wstr + 1);
*(wstr + 1) = '\0';
SetEnvironmentVariableW(L"HOMEDRIVE", s->pw->pw_dir);
SetEnvironmentVariableW(L"HOMEDRIVE", pw_dir_utf16);
*(wstr + 1) = wchr;
SetEnvironmentVariableW(L"HOMEPATH", (wstr+1));
}
SetEnvironmentVariableW(L"USERPROFILE", s -> pw -> pw_dir);
// find the server name of the domain controller which created this token
GetDomainFromToken ( &hToken, buf, sizeof(buf));
@ -734,7 +737,7 @@ do_exec_no_pty(Session *s, const char *command)
snprintf(buf, sizeof buf, "%.50s %d %d",
get_remote_ipaddr(), get_remote_port(), get_local_port());
SetEnvironmentVariable("SSH_CLIENT", buf);
SetEnvironmentVariableA("SSH_CLIENT", buf);
/*
* Set SSH_CONNECTION variable.
@ -747,16 +750,16 @@ do_exec_no_pty(Session *s, const char *command)
free(laddr);
SetEnvironmentVariable("SSH_CONNECTION", buf);
SetEnvironmentVariableA("SSH_CONNECTION", buf);
if (original_command)
SetEnvironmentVariable("SSH_ORIGINAL_COMMAND", original_command);
SetEnvironmentVariableA("SSH_ORIGINAL_COMMAND", original_command);
// set better prompt for Windows cmd shell
if (!s -> is_subsystem) {
snprintf(buf,sizeof buf, "%s@%s $P$G", s->pw->pw_name, getenv("COMPUTERNAME"));
SetEnvironmentVariable("PROMPT", buf);
SetEnvironmentVariableA("PROMPT", buf);
}
/*
@ -789,14 +792,17 @@ do_exec_no_pty(Session *s, const char *command)
DWORD dwStartupFlags = DETACHED_PROCESS;// CREATE_SUSPENDED; // 0
SetConsoleCtrlHandler(NULL, FALSE);
if (debug_flag)
b = CreateProcessW(NULL, exec_command_w, NULL, NULL, TRUE,
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, s->pw->pw_dir,
&si, &pi);
else
b = CreateProcessAsUserW(hToken, NULL, exec_command_w, NULL, NULL, TRUE,
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, s -> pw -> pw_dir,
&si, &pi);
wchar_t* p_dir = utf8_to_utf16(s->pw->pw_dir);
if (debug_flag)
b = CreateProcessW(NULL, exec_command_w, NULL, NULL, TRUE,
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, pw_dir_utf16,
&si, &pi);
else
b = CreateProcessAsUserW(hToken, NULL, exec_command_w, NULL, NULL, TRUE,
/*CREATE_NEW_PROCESS_GROUP*/ dwStartupFlags, NULL, pw_dir_utf16,
&si, &pi);
if (!b)
{
debug("ERROR. Cannot create process (%u).\n", GetLastError());