183d2307dSDag-Erling Smørgrav /*
2420bce64SDag-Erling Smørgrav * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
383d2307dSDag-Erling Smørgrav *
483d2307dSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
583d2307dSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
683d2307dSDag-Erling Smørgrav * are met:
783d2307dSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
883d2307dSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
983d2307dSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
1083d2307dSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
1183d2307dSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
1283d2307dSDag-Erling Smørgrav *
1383d2307dSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1483d2307dSDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1583d2307dSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1683d2307dSDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1783d2307dSDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1883d2307dSDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1983d2307dSDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2083d2307dSDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2183d2307dSDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2283d2307dSDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2383d2307dSDag-Erling Smørgrav *
2483d2307dSDag-Erling Smørgrav * Created: Sat Sep 02 12:17:00 2000 cv
2583d2307dSDag-Erling Smørgrav *
2683d2307dSDag-Erling Smørgrav * This file contains functions for forcing opened file descriptors to
2783d2307dSDag-Erling Smørgrav * binary mode on Windows systems.
2883d2307dSDag-Erling Smørgrav */
2983d2307dSDag-Erling Smørgrav
30420bce64SDag-Erling Smørgrav #define NO_BINARY_OPEN /* Avoid redefining open to binary_open for this file */
3183d2307dSDag-Erling Smørgrav #include "includes.h"
3283d2307dSDag-Erling Smørgrav
3383d2307dSDag-Erling Smørgrav #ifdef HAVE_CYGWIN
3483d2307dSDag-Erling Smørgrav
35761efaa7SDag-Erling Smørgrav #include <sys/types.h>
36761efaa7SDag-Erling Smørgrav #include <fcntl.h>
37420bce64SDag-Erling Smørgrav #include <string.h>
38761efaa7SDag-Erling Smørgrav #include <unistd.h>
39190cef3dSDag-Erling Smørgrav #include <stdarg.h>
4019261079SEd Maste #include <stdlib.h>
4119261079SEd Maste #include <wchar.h>
4219261079SEd Maste #include <wctype.h>
43761efaa7SDag-Erling Smørgrav
44761efaa7SDag-Erling Smørgrav #include "xmalloc.h"
45761efaa7SDag-Erling Smørgrav
46d95e11bfSDag-Erling Smørgrav int
binary_open(const char * filename,int flags,...)47d95e11bfSDag-Erling Smørgrav binary_open(const char *filename, int flags, ...)
4883d2307dSDag-Erling Smørgrav {
4983d2307dSDag-Erling Smørgrav va_list ap;
5083d2307dSDag-Erling Smørgrav mode_t mode;
5183d2307dSDag-Erling Smørgrav
5283d2307dSDag-Erling Smørgrav va_start(ap, flags);
5383d2307dSDag-Erling Smørgrav mode = va_arg(ap, mode_t);
5483d2307dSDag-Erling Smørgrav va_end(ap);
55d95e11bfSDag-Erling Smørgrav return (open(filename, flags | O_BINARY, mode));
5683d2307dSDag-Erling Smørgrav }
5783d2307dSDag-Erling Smørgrav
58d95e11bfSDag-Erling Smørgrav int
check_ntsec(const char * filename)59d95e11bfSDag-Erling Smørgrav check_ntsec(const char *filename)
6083d2307dSDag-Erling Smørgrav {
61d4af9e69SDag-Erling Smørgrav return (pathconf(filename, _PC_POSIX_PERMISSIONS));
6283d2307dSDag-Erling Smørgrav }
6383d2307dSDag-Erling Smørgrav
64a0ee8cc6SDag-Erling Smørgrav const char *
cygwin_ssh_privsep_user()65a0ee8cc6SDag-Erling Smørgrav cygwin_ssh_privsep_user()
66a0ee8cc6SDag-Erling Smørgrav {
67a0ee8cc6SDag-Erling Smørgrav static char cyg_privsep_user[DNLEN + UNLEN + 2];
68a0ee8cc6SDag-Erling Smørgrav
69a0ee8cc6SDag-Erling Smørgrav if (!cyg_privsep_user[0])
70a0ee8cc6SDag-Erling Smørgrav {
71a0ee8cc6SDag-Erling Smørgrav #ifdef CW_CYGNAME_FROM_WINNAME
72a0ee8cc6SDag-Erling Smørgrav if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user,
73a0ee8cc6SDag-Erling Smørgrav sizeof cyg_privsep_user) != 0)
74a0ee8cc6SDag-Erling Smørgrav #endif
75557f75e5SDag-Erling Smørgrav strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user));
76a0ee8cc6SDag-Erling Smørgrav }
77a0ee8cc6SDag-Erling Smørgrav return cyg_privsep_user;
78a0ee8cc6SDag-Erling Smørgrav }
79a0ee8cc6SDag-Erling Smørgrav
805e8dbd04SDag-Erling Smørgrav #define NL(x) x, (sizeof (x) - 1)
815e8dbd04SDag-Erling Smørgrav #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
825e8dbd04SDag-Erling Smørgrav
835e8dbd04SDag-Erling Smørgrav static struct wenv {
845e8dbd04SDag-Erling Smørgrav const char *name;
855e8dbd04SDag-Erling Smørgrav size_t namelen;
865e8dbd04SDag-Erling Smørgrav } wenv_arr[] = {
875e8dbd04SDag-Erling Smørgrav { NL("ALLUSERSPROFILE=") },
885e8dbd04SDag-Erling Smørgrav { NL("COMPUTERNAME=") },
895e8dbd04SDag-Erling Smørgrav { NL("COMSPEC=") },
904518870cSDag-Erling Smørgrav { NL("CYGWIN=") },
915e8dbd04SDag-Erling Smørgrav { NL("OS=") },
925e8dbd04SDag-Erling Smørgrav { NL("PATH=") },
935e8dbd04SDag-Erling Smørgrav { NL("PATHEXT=") },
94462c32cbSDag-Erling Smørgrav { NL("PROGRAMFILES=") },
955e8dbd04SDag-Erling Smørgrav { NL("SYSTEMDRIVE=") },
965e8dbd04SDag-Erling Smørgrav { NL("SYSTEMROOT=") },
974518870cSDag-Erling Smørgrav { NL("WINDIR=") }
985e8dbd04SDag-Erling Smørgrav };
995e8dbd04SDag-Erling Smørgrav
1005e8dbd04SDag-Erling Smørgrav char **
fetch_windows_environment(void)1015e8dbd04SDag-Erling Smørgrav fetch_windows_environment(void)
1025e8dbd04SDag-Erling Smørgrav {
1035e8dbd04SDag-Erling Smørgrav char **e, **p;
104761efaa7SDag-Erling Smørgrav unsigned int i, idx = 0;
1055e8dbd04SDag-Erling Smørgrav
106761efaa7SDag-Erling Smørgrav p = xcalloc(WENV_SIZ + 1, sizeof(char *));
1075e8dbd04SDag-Erling Smørgrav for (e = environ; *e != NULL; ++e) {
1085e8dbd04SDag-Erling Smørgrav for (i = 0; i < WENV_SIZ; ++i) {
1095e8dbd04SDag-Erling Smørgrav if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
1105e8dbd04SDag-Erling Smørgrav p[idx++] = *e;
1115e8dbd04SDag-Erling Smørgrav }
1125e8dbd04SDag-Erling Smørgrav }
1135e8dbd04SDag-Erling Smørgrav p[idx] = NULL;
1145e8dbd04SDag-Erling Smørgrav return p;
1155e8dbd04SDag-Erling Smørgrav }
1165e8dbd04SDag-Erling Smørgrav
1175e8dbd04SDag-Erling Smørgrav void
free_windows_environment(char ** p)1185e8dbd04SDag-Erling Smørgrav free_windows_environment(char **p)
1195e8dbd04SDag-Erling Smørgrav {
120e4a9863fSDag-Erling Smørgrav free(p);
1215e8dbd04SDag-Erling Smørgrav }
1225e8dbd04SDag-Erling Smørgrav
12319261079SEd Maste /*
12419261079SEd Maste * Returns true if the given string matches the pattern (which may contain ?
12519261079SEd Maste * and * as wildcards), and zero if it does not match.
12619261079SEd Maste *
12719261079SEd Maste * The Cygwin version of this function must be case-insensitive and take
12819261079SEd Maste * Unicode characters into account.
12919261079SEd Maste */
13019261079SEd Maste
13119261079SEd Maste static int
__match_pattern(const wchar_t * s,const wchar_t * pattern)13219261079SEd Maste __match_pattern (const wchar_t *s, const wchar_t *pattern)
13319261079SEd Maste {
13419261079SEd Maste for (;;) {
13519261079SEd Maste /* If at end of pattern, accept if also at end of string. */
13619261079SEd Maste if (!*pattern)
13719261079SEd Maste return !*s;
13819261079SEd Maste
13919261079SEd Maste if (*pattern == '*') {
14019261079SEd Maste /* Skip the asterisk. */
14119261079SEd Maste pattern++;
14219261079SEd Maste
14319261079SEd Maste /* If at end of pattern, accept immediately. */
14419261079SEd Maste if (!*pattern)
14519261079SEd Maste return 1;
14619261079SEd Maste
14719261079SEd Maste /* If next character in pattern is known, optimize. */
14819261079SEd Maste if (*pattern != '?' && *pattern != '*') {
14919261079SEd Maste /*
15019261079SEd Maste * Look instances of the next character in
15119261079SEd Maste * pattern, and try to match starting from
15219261079SEd Maste * those.
15319261079SEd Maste */
15419261079SEd Maste for (; *s; s++)
15519261079SEd Maste if (*s == *pattern &&
15619261079SEd Maste __match_pattern(s + 1, pattern + 1))
15719261079SEd Maste return 1;
15819261079SEd Maste /* Failed. */
15919261079SEd Maste return 0;
16019261079SEd Maste }
16119261079SEd Maste /*
16219261079SEd Maste * Move ahead one character at a time and try to
16319261079SEd Maste * match at each position.
16419261079SEd Maste */
16519261079SEd Maste for (; *s; s++)
16619261079SEd Maste if (__match_pattern(s, pattern))
16719261079SEd Maste return 1;
16819261079SEd Maste /* Failed. */
16919261079SEd Maste return 0;
17019261079SEd Maste }
17119261079SEd Maste /*
17219261079SEd Maste * There must be at least one more character in the string.
17319261079SEd Maste * If we are at the end, fail.
17419261079SEd Maste */
17519261079SEd Maste if (!*s)
17619261079SEd Maste return 0;
17719261079SEd Maste
17819261079SEd Maste /* Check if the next character of the string is acceptable. */
17919261079SEd Maste if (*pattern != '?' && towlower(*pattern) != towlower(*s))
18019261079SEd Maste return 0;
18119261079SEd Maste
18219261079SEd Maste /* Move to the next character, both in string and in pattern. */
18319261079SEd Maste s++;
18419261079SEd Maste pattern++;
18519261079SEd Maste }
18619261079SEd Maste /* NOTREACHED */
18719261079SEd Maste }
18819261079SEd Maste
18919261079SEd Maste static int
_match_pattern(const char * s,const char * pattern)19019261079SEd Maste _match_pattern(const char *s, const char *pattern)
19119261079SEd Maste {
19219261079SEd Maste wchar_t *ws;
19319261079SEd Maste wchar_t *wpattern;
19419261079SEd Maste size_t len;
19519261079SEd Maste int ret;
19619261079SEd Maste
197*1323ec57SEd Maste if ((len = mbstowcs(NULL, s, 0)) == (size_t) -1)
19819261079SEd Maste return 0;
19919261079SEd Maste ws = (wchar_t *) xcalloc(len + 1, sizeof (wchar_t));
20019261079SEd Maste mbstowcs(ws, s, len + 1);
201*1323ec57SEd Maste if ((len = mbstowcs(NULL, pattern, 0)) == (size_t) -1)
20219261079SEd Maste return 0;
20319261079SEd Maste wpattern = (wchar_t *) xcalloc(len + 1, sizeof (wchar_t));
20419261079SEd Maste mbstowcs(wpattern, pattern, len + 1);
20519261079SEd Maste ret = __match_pattern (ws, wpattern);
20619261079SEd Maste free(ws);
20719261079SEd Maste free(wpattern);
20819261079SEd Maste return ret;
20919261079SEd Maste }
21019261079SEd Maste
21119261079SEd Maste /*
21219261079SEd Maste * Tries to match the string against the
21319261079SEd Maste * comma-separated sequence of subpatterns (each possibly preceded by ! to
21419261079SEd Maste * indicate negation). Returns -1 if negation matches, 1 if there is
21519261079SEd Maste * a positive match, 0 if there is no match at all.
21619261079SEd Maste */
21719261079SEd Maste int
cygwin_ug_match_pattern_list(const char * string,const char * pattern)21819261079SEd Maste cygwin_ug_match_pattern_list(const char *string, const char *pattern)
21919261079SEd Maste {
22019261079SEd Maste char sub[1024];
22119261079SEd Maste int negated;
22219261079SEd Maste int got_positive;
22319261079SEd Maste u_int i, subi, len = strlen(pattern);
22419261079SEd Maste
22519261079SEd Maste got_positive = 0;
22619261079SEd Maste for (i = 0; i < len;) {
22719261079SEd Maste /* Check if the subpattern is negated. */
22819261079SEd Maste if (pattern[i] == '!') {
22919261079SEd Maste negated = 1;
23019261079SEd Maste i++;
23119261079SEd Maste } else
23219261079SEd Maste negated = 0;
23319261079SEd Maste
23419261079SEd Maste /*
23519261079SEd Maste * Extract the subpattern up to a comma or end. Convert the
23619261079SEd Maste * subpattern to lowercase.
23719261079SEd Maste */
23819261079SEd Maste for (subi = 0;
23919261079SEd Maste i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
24019261079SEd Maste subi++, i++)
24119261079SEd Maste sub[subi] = pattern[i];
24219261079SEd Maste /* If subpattern too long, return failure (no match). */
24319261079SEd Maste if (subi >= sizeof(sub) - 1)
24419261079SEd Maste return 0;
24519261079SEd Maste
24619261079SEd Maste /* If the subpattern was terminated by a comma, then skip it. */
24719261079SEd Maste if (i < len && pattern[i] == ',')
24819261079SEd Maste i++;
24919261079SEd Maste
25019261079SEd Maste /* Null-terminate the subpattern. */
25119261079SEd Maste sub[subi] = '\0';
25219261079SEd Maste
25319261079SEd Maste /* Try to match the subpattern against the string. */
25419261079SEd Maste if (_match_pattern(string, sub)) {
25519261079SEd Maste if (negated)
25619261079SEd Maste return -1; /* Negative */
25719261079SEd Maste else
25819261079SEd Maste got_positive = 1; /* Positive */
25919261079SEd Maste }
26019261079SEd Maste }
26119261079SEd Maste
26219261079SEd Maste /*
26319261079SEd Maste * Return success if got a positive match. If there was a negative
26419261079SEd Maste * match, we have already returned -1 and never get here.
26519261079SEd Maste */
26619261079SEd Maste return got_positive;
26719261079SEd Maste }
26819261079SEd Maste
26983d2307dSDag-Erling Smørgrav #endif /* HAVE_CYGWIN */
270