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