Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1 | /* |
Darren Tucker | c9627cd | 2013-04-01 12:40:48 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com> |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 3 | * |
Kevin Steves | eb36302 | 2002-04-15 22:00:51 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 23 | * |
| 24 | * Created: Sat Sep 02 12:17:00 2000 cv |
| 25 | * |
| 26 | * This file contains functions for forcing opened file descriptors to |
| 27 | * binary mode on Windows systems. |
| 28 | */ |
| 29 | |
Darren Tucker | c9627cd | 2013-04-01 12:40:48 +1100 | [diff] [blame] | 30 | #define NO_BINARY_OPEN /* Avoid redefining open to binary_open for this file */ |
Damien Miller | e9cf357 | 2001-02-09 12:55:35 +1100 | [diff] [blame] | 31 | #include "includes.h" |
| 32 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CYGWIN |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 34 | |
Darren Tucker | 9235010 | 2006-08-05 19:08:16 +1000 | [diff] [blame] | 35 | #include <sys/types.h> |
Darren Tucker | 9235010 | 2006-08-05 19:08:16 +1000 | [diff] [blame] | 36 | #include <fcntl.h> |
Darren Tucker | c9627cd | 2013-04-01 12:40:48 +1100 | [diff] [blame] | 37 | #include <string.h> |
Darren Tucker | 9235010 | 2006-08-05 19:08:16 +1000 | [diff] [blame] | 38 | #include <unistd.h> |
Darren Tucker | 9235010 | 2006-08-05 19:08:16 +1000 | [diff] [blame] | 39 | |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 40 | #include "xmalloc.h" |
Ben Lindstrom | cff94be | 2001-07-18 16:19:48 +0000 | [diff] [blame] | 41 | |
Darren Tucker | c20dccb | 2016-08-02 09:44:25 +1000 | [diff] [blame] | 42 | int |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 43 | binary_open(const char *filename, int flags, ...) |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 44 | { |
Damien Miller | 2deb3f6 | 2001-02-18 12:30:55 +1100 | [diff] [blame] | 45 | va_list ap; |
| 46 | mode_t mode; |
Darren Tucker | c20dccb | 2016-08-02 09:44:25 +1000 | [diff] [blame] | 47 | |
Damien Miller | 2deb3f6 | 2001-02-18 12:30:55 +1100 | [diff] [blame] | 48 | va_start(ap, flags); |
| 49 | mode = va_arg(ap, mode_t); |
| 50 | va_end(ap); |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 51 | return (open(filename, flags | O_BINARY, mode)); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 52 | } |
| 53 | |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 54 | int |
| 55 | check_ntsec(const char *filename) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 56 | { |
Darren Tucker | 9a3f2b4 | 2008-07-17 19:03:49 +1000 | [diff] [blame] | 57 | return (pathconf(filename, _PC_POSIX_PERMISSIONS)); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 58 | } |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 59 | |
Damien Miller | 6482d90 | 2014-05-27 14:34:42 +1000 | [diff] [blame] | 60 | const char * |
| 61 | cygwin_ssh_privsep_user() |
| 62 | { |
| 63 | static char cyg_privsep_user[DNLEN + UNLEN + 2]; |
| 64 | |
| 65 | if (!cyg_privsep_user[0]) |
| 66 | { |
| 67 | #ifdef CW_CYGNAME_FROM_WINNAME |
| 68 | if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user, |
| 69 | sizeof cyg_privsep_user) != 0) |
| 70 | #endif |
Darren Tucker | d7a58bb | 2015-06-02 20:15:26 +1000 | [diff] [blame] | 71 | strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user)); |
Damien Miller | 6482d90 | 2014-05-27 14:34:42 +1000 | [diff] [blame] | 72 | } |
| 73 | return cyg_privsep_user; |
| 74 | } |
| 75 | |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 76 | #define NL(x) x, (sizeof (x) - 1) |
| 77 | #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0])) |
| 78 | |
| 79 | static struct wenv { |
| 80 | const char *name; |
| 81 | size_t namelen; |
| 82 | } wenv_arr[] = { |
| 83 | { NL("ALLUSERSPROFILE=") }, |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 84 | { NL("COMPUTERNAME=") }, |
| 85 | { NL("COMSPEC=") }, |
Darren Tucker | ae8c91e | 2005-05-25 19:42:10 +1000 | [diff] [blame] | 86 | { NL("CYGWIN=") }, |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 87 | { NL("OS=") }, |
| 88 | { NL("PATH=") }, |
| 89 | { NL("PATHEXT=") }, |
Damien Miller | 7b7901c | 2012-02-14 06:38:36 +1100 | [diff] [blame] | 90 | { NL("PROGRAMFILES=") }, |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 91 | { NL("SYSTEMDRIVE=") }, |
| 92 | { NL("SYSTEMROOT=") }, |
Darren Tucker | ae8c91e | 2005-05-25 19:42:10 +1000 | [diff] [blame] | 93 | { NL("WINDIR=") } |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | char ** |
| 97 | fetch_windows_environment(void) |
| 98 | { |
| 99 | char **e, **p; |
Darren Tucker | 84af615 | 2006-02-12 11:59:08 +1100 | [diff] [blame] | 100 | unsigned int i, idx = 0; |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 101 | |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 102 | p = xcalloc(WENV_SIZ + 1, sizeof(char *)); |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 103 | for (e = environ; *e != NULL; ++e) { |
| 104 | for (i = 0; i < WENV_SIZ; ++i) { |
| 105 | if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen)) |
| 106 | p[idx++] = *e; |
| 107 | } |
| 108 | } |
| 109 | p[idx] = NULL; |
| 110 | return p; |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | free_windows_environment(char **p) |
| 115 | { |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 116 | free(p); |
Darren Tucker | 14c372d | 2004-08-30 20:42:08 +1000 | [diff] [blame] | 117 | } |
| 118 | |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 119 | #endif /* HAVE_CYGWIN */ |