|
| 1 | +/* |
| 2 | +* Author: Manoj Ampalam <[email protected]> |
| 3 | +* |
| 4 | +* wmain entry for sshd-session. |
| 5 | +* |
| 6 | +* Copyright (c) 2015 Microsoft Corp. |
| 7 | +* All rights reserved |
| 8 | +* |
| 9 | +* Microsoft openssh win32 port |
| 10 | +* |
| 11 | +* Redistribution and use in source and binary forms, with or without |
| 12 | +* modification, are permitted provided that the following conditions |
| 13 | +* are met: |
| 14 | +* |
| 15 | +* 1. Redistributions of source code must retain the above copyright |
| 16 | +* notice, this list of conditions and the following disclaimer. |
| 17 | +* 2. Redistributions in binary form must reproduce the above copyright |
| 18 | +* notice, this list of conditions and the following disclaimer in the |
| 19 | +* documentation and/or other materials provided with the distribution. |
| 20 | +* |
| 21 | +* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 22 | +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 23 | +* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 24 | +* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 26 | +* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 30 | +* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +*/ |
| 32 | + |
| 33 | +/* disable inclusion of compatability defitnitions in CRT headers */ |
| 34 | +#define __STDC__ 1 |
| 35 | +#include <Windows.h> |
| 36 | +#include <wchar.h> |
| 37 | +#include <Lm.h> |
| 38 | +#include <sddl.h> |
| 39 | +#include <stdio.h> |
| 40 | + |
| 41 | +#include "inc\utf.h" |
| 42 | +#include "misc_internal.h" |
| 43 | +#include "Debug.h" |
| 44 | + |
| 45 | +int main(int, char **); |
| 46 | +extern HANDLE main_thread; |
| 47 | + |
| 48 | +int sshd_session_main(int argc, wchar_t **wargv) { |
| 49 | + char** argv = NULL; |
| 50 | + int i, r; |
| 51 | + _set_invalid_parameter_handler(invalid_parameter_handler); |
| 52 | + |
| 53 | + if ((argv = malloc((argc + 1) * sizeof(char*))) == NULL) |
| 54 | + fatal("out of memory"); |
| 55 | + |
| 56 | + for (i = 0; i < argc; i++) |
| 57 | + if ((argv[i] = utf16_to_utf8(wargv[i])) == NULL) |
| 58 | + fatal("out of memory"); |
| 59 | + argv[argc] = NULL; |
| 60 | + |
| 61 | + w32posix_initialize(); |
| 62 | + |
| 63 | + r = main(argc, argv); |
| 64 | + w32posix_done(); |
| 65 | + return r; |
| 66 | +} |
| 67 | + |
| 68 | +int argc_original = 0; |
| 69 | +wchar_t **wargv_original = NULL; |
| 70 | + |
| 71 | +int wmain(int argc, wchar_t **wargv) { |
| 72 | + wchar_t *path_value = NULL, *path_new_value; |
| 73 | + errno_t result = 0; |
| 74 | + size_t path_new_len = 0, len; |
| 75 | + argc_original = argc; |
| 76 | + wargv_original = wargv; |
| 77 | + |
| 78 | + init_prog_paths(); |
| 79 | + /* change current directory to sshd-session.exe root */ |
| 80 | + _wchdir(__wprogdir); |
| 81 | + |
| 82 | + /* |
| 83 | + * we want to launch scp and sftp executables from the binary directory |
| 84 | + * that sshd is hosted in. This will facilitate hosting and evaluating |
| 85 | + * multiple versions of OpenSSH at the same time. |
| 86 | + * it does not work well for powershell, cygwin, etc if program path is |
| 87 | + * prepended to executable directory. |
| 88 | + * To achive above, PATH is set to process environment |
| 89 | + */ |
| 90 | + _wdupenv_s(&path_value, &len, L"PATH"); |
| 91 | + if (!path_value || (wcsstr(path_value, __wprogdir)) == NULL) { |
| 92 | + path_new_len = wcslen(__wprogdir) + wcslen(path_value) + 2; |
| 93 | + if ((path_new_value = (wchar_t *) malloc(path_new_len * sizeof(wchar_t))) == NULL) { |
| 94 | + errno = ENOMEM; |
| 95 | + error("failed to allocation memory"); |
| 96 | + return -1; |
| 97 | + } |
| 98 | + swprintf_s(path_new_value, path_new_len, L"%s%s%s", __wprogdir, path_value ? L";" : L"", path_value); |
| 99 | + if (result = _wputenv_s(L"PATH", path_new_value)) { |
| 100 | + error("failed to set PATH environment variable: to value:%s, error:%d", path_new_value, result); |
| 101 | + errno = result; |
| 102 | + if (path_new_value) |
| 103 | + free(path_new_value); |
| 104 | + if(path_value) |
| 105 | + free(path_value); |
| 106 | + return -1; |
| 107 | + } |
| 108 | + if (path_new_value) |
| 109 | + free(path_new_value); |
| 110 | + if(path_value) |
| 111 | + free(path_value); |
| 112 | + } |
| 113 | + |
| 114 | + return sshd_session_main(argc, wargv); |
| 115 | +} |
0 commit comments