2 * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
24 * Created: Sat Sep 02 12:17:00 2000 cv
26 * This file contains functions for forcing opened file descriptors to
27 * binary mode on Windows systems.
30 #define NO_BINARY_OPEN /* Avoid redefining open to binary_open for this file */
35 #include <sys/types.h>
47 binary_open(const char *filename, int flags, ...)
53 mode = va_arg(ap, mode_t);
55 return (open(filename, flags | O_BINARY, mode));
59 check_ntsec(const char *filename)
61 return (pathconf(filename, _PC_POSIX_PERMISSIONS));
65 cygwin_ssh_privsep_user()
67 static char cyg_privsep_user[DNLEN + UNLEN + 2];
69 if (!cyg_privsep_user[0])
71 #ifdef CW_CYGNAME_FROM_WINNAME
72 if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user,
73 sizeof cyg_privsep_user) != 0)
75 strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user));
77 return cyg_privsep_user;
80 #define NL(x) x, (sizeof (x) - 1)
81 #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
87 { NL("ALLUSERSPROFILE=") },
88 { NL("COMPUTERNAME=") },
94 { NL("PROGRAMFILES=") },
95 { NL("SYSTEMDRIVE=") },
96 { NL("SYSTEMROOT=") },
101 fetch_windows_environment(void)
104 unsigned int i, idx = 0;
106 p = xcalloc(WENV_SIZ + 1, sizeof(char *));
107 for (e = environ; *e != NULL; ++e) {
108 for (i = 0; i < WENV_SIZ; ++i) {
109 if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
118 free_windows_environment(char **p)
124 * Returns true if the given string matches the pattern (which may contain ?
125 * and * as wildcards), and zero if it does not match.
127 * The Cygwin version of this function must be case-insensitive and take
128 * Unicode characters into account.
132 __match_pattern (const wchar_t *s, const wchar_t *pattern)
135 /* If at end of pattern, accept if also at end of string. */
139 if (*pattern == '*') {
140 /* Skip the asterisk. */
143 /* If at end of pattern, accept immediately. */
147 /* If next character in pattern is known, optimize. */
148 if (*pattern != '?' && *pattern != '*') {
150 * Look instances of the next character in
151 * pattern, and try to match starting from
155 if (*s == *pattern &&
156 __match_pattern(s + 1, pattern + 1))
162 * Move ahead one character at a time and try to
163 * match at each position.
166 if (__match_pattern(s, pattern))
172 * There must be at least one more character in the string.
173 * If we are at the end, fail.
178 /* Check if the next character of the string is acceptable. */
179 if (*pattern != '?' && towlower(*pattern) != towlower(*s))
182 /* Move to the next character, both in string and in pattern. */
190 _match_pattern(const char *s, const char *pattern)
197 if ((len = mbstowcs(NULL, s, 0)) == (size_t) -1)
199 ws = (wchar_t *) xcalloc(len + 1, sizeof (wchar_t));
200 mbstowcs(ws, s, len + 1);
201 if ((len = mbstowcs(NULL, pattern, 0)) == (size_t) -1)
203 wpattern = (wchar_t *) xcalloc(len + 1, sizeof (wchar_t));
204 mbstowcs(wpattern, pattern, len + 1);
205 ret = __match_pattern (ws, wpattern);
212 * Tries to match the string against the
213 * comma-separated sequence of subpatterns (each possibly preceded by ! to
214 * indicate negation). Returns -1 if negation matches, 1 if there is
215 * a positive match, 0 if there is no match at all.
218 cygwin_ug_match_pattern_list(const char *string, const char *pattern)
223 u_int i, subi, len = strlen(pattern);
226 for (i = 0; i < len;) {
227 /* Check if the subpattern is negated. */
228 if (pattern[i] == '!') {
235 * Extract the subpattern up to a comma or end. Convert the
236 * subpattern to lowercase.
239 i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
241 sub[subi] = pattern[i];
242 /* If subpattern too long, return failure (no match). */
243 if (subi >= sizeof(sub) - 1)
246 /* If the subpattern was terminated by a comma, then skip it. */
247 if (i < len && pattern[i] == ',')
250 /* Null-terminate the subpattern. */
253 /* Try to match the subpattern against the string. */
254 if (_match_pattern(string, sub)) {
256 return -1; /* Negative */
258 got_positive = 1; /* Positive */
263 * Return success if got a positive match. If there was a negative
264 * match, we have already returned -1 and never get here.
269 #endif /* HAVE_CYGWIN */