1/*
2 * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
3 *
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.
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
30#define NO_BINARY_OPEN	/* Avoid redefining open to binary_open for this file */
31#include "includes.h"
32
33#ifdef HAVE_CYGWIN
34
35#include <sys/types.h>
36#include <fcntl.h>
37#include <string.h>
38#include <unistd.h>
39#include <stdarg.h>
40#include <stdlib.h>
41#include <wchar.h>
42#include <wctype.h>
43
44#include "xmalloc.h"
45
46int
47binary_open(const char *filename, int flags, ...)
48{
49	va_list ap;
50	mode_t mode;
51
52	va_start(ap, flags);
53	mode = va_arg(ap, mode_t);
54	va_end(ap);
55	return (open(filename, flags | O_BINARY, mode));
56}
57
58int
59check_ntsec(const char *filename)
60{
61	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
62}
63
64const char *
65cygwin_ssh_privsep_user()
66{
67  static char cyg_privsep_user[DNLEN + UNLEN + 2];
68
69  if (!cyg_privsep_user[0])
70    {
71#ifdef CW_CYGNAME_FROM_WINNAME
72      if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user,
73			   sizeof cyg_privsep_user) != 0)
74#endif
75	strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user));
76    }
77  return cyg_privsep_user;
78}
79
80#define NL(x) x, (sizeof (x) - 1)
81#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
82
83static struct wenv {
84	const char *name;
85	size_t namelen;
86} wenv_arr[] = {
87	{ NL("ALLUSERSPROFILE=") },
88	{ NL("COMPUTERNAME=") },
89	{ NL("COMSPEC=") },
90	{ NL("CYGWIN=") },
91	{ NL("OS=") },
92	{ NL("PATH=") },
93	{ NL("PATHEXT=") },
94	{ NL("PROGRAMFILES=") },
95	{ NL("SYSTEMDRIVE=") },
96	{ NL("SYSTEMROOT=") },
97	{ NL("WINDIR=") }
98};
99
100char **
101fetch_windows_environment(void)
102{
103	char **e, **p;
104	unsigned int i, idx = 0;
105
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))
110				p[idx++] = *e;
111		}
112	}
113	p[idx] = NULL;
114	return p;
115}
116
117void
118free_windows_environment(char **p)
119{
120	free(p);
121}
122
123/*
124 * Returns true if the given string matches the pattern (which may contain ?
125 * and * as wildcards), and zero if it does not match.
126 *
127 * The Cygwin version of this function must be case-insensitive and take
128 * Unicode characters into account.
129 */
130
131static int
132__match_pattern (const wchar_t *s, const wchar_t *pattern)
133{
134	for (;;) {
135		/* If at end of pattern, accept if also at end of string. */
136		if (!*pattern)
137			return !*s;
138
139		if (*pattern == '*') {
140			/* Skip the asterisk. */
141			pattern++;
142
143			/* If at end of pattern, accept immediately. */
144			if (!*pattern)
145				return 1;
146
147			/* If next character in pattern is known, optimize. */
148			if (*pattern != '?' && *pattern != '*') {
149				/*
150				 * Look instances of the next character in
151				 * pattern, and try to match starting from
152				 * those.
153				 */
154				for (; *s; s++)
155					if (*s == *pattern &&
156					    __match_pattern(s + 1, pattern + 1))
157						return 1;
158				/* Failed. */
159				return 0;
160			}
161			/*
162			 * Move ahead one character at a time and try to
163			 * match at each position.
164			 */
165			for (; *s; s++)
166				if (__match_pattern(s, pattern))
167					return 1;
168			/* Failed. */
169			return 0;
170		}
171		/*
172		 * There must be at least one more character in the string.
173		 * If we are at the end, fail.
174		 */
175		if (!*s)
176			return 0;
177
178		/* Check if the next character of the string is acceptable. */
179		if (*pattern != '?' && towlower(*pattern) != towlower(*s))
180			return 0;
181
182		/* Move to the next character, both in string and in pattern. */
183		s++;
184		pattern++;
185	}
186	/* NOTREACHED */
187}
188
189static int
190_match_pattern(const char *s, const char *pattern)
191{
192	wchar_t *ws;
193	wchar_t *wpattern;
194	size_t len;
195	int ret;
196
197	if ((len = mbstowcs(NULL, s, 0)) == (size_t) -1)
198		return 0;
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)
202		return 0;
203	wpattern = (wchar_t *) xcalloc(len + 1, sizeof (wchar_t));
204	mbstowcs(wpattern, pattern, len + 1);
205	ret = __match_pattern (ws, wpattern);
206	free(ws);
207	free(wpattern);
208	return ret;
209}
210
211/*
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.
216 */
217int
218cygwin_ug_match_pattern_list(const char *string, const char *pattern)
219{
220	char sub[1024];
221	int negated;
222	int got_positive;
223	u_int i, subi, len = strlen(pattern);
224
225	got_positive = 0;
226	for (i = 0; i < len;) {
227		/* Check if the subpattern is negated. */
228		if (pattern[i] == '!') {
229			negated = 1;
230			i++;
231		} else
232			negated = 0;
233
234		/*
235		 * Extract the subpattern up to a comma or end.  Convert the
236		 * subpattern to lowercase.
237		 */
238		for (subi = 0;
239		    i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
240		    subi++, i++)
241			sub[subi] = pattern[i];
242		/* If subpattern too long, return failure (no match). */
243		if (subi >= sizeof(sub) - 1)
244			return 0;
245
246		/* If the subpattern was terminated by a comma, then skip it. */
247		if (i < len && pattern[i] == ',')
248			i++;
249
250		/* Null-terminate the subpattern. */
251		sub[subi] = '\0';
252
253		/* Try to match the subpattern against the string. */
254		if (_match_pattern(string, sub)) {
255			if (negated)
256				return -1;		/* Negative */
257			else
258				got_positive = 1;	/* Positive */
259		}
260	}
261
262	/*
263	 * Return success if got a positive match.  If there was a negative
264	 * match, we have already returned -1 and never get here.
265	 */
266	return got_positive;
267}
268
269#endif /* HAVE_CYGWIN */
270