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
41#include "xmalloc.h"
42
43int
44binary_open(const char *filename, int flags, ...)
45{
46	va_list ap;
47	mode_t mode;
48
49	va_start(ap, flags);
50	mode = va_arg(ap, mode_t);
51	va_end(ap);
52	return (open(filename, flags | O_BINARY, mode));
53}
54
55int
56check_ntsec(const char *filename)
57{
58	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
59}
60
61const char *
62cygwin_ssh_privsep_user()
63{
64  static char cyg_privsep_user[DNLEN + UNLEN + 2];
65
66  if (!cyg_privsep_user[0])
67    {
68#ifdef CW_CYGNAME_FROM_WINNAME
69      if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user,
70			   sizeof cyg_privsep_user) != 0)
71#endif
72	strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user));
73    }
74  return cyg_privsep_user;
75}
76
77#define NL(x) x, (sizeof (x) - 1)
78#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
79
80static struct wenv {
81	const char *name;
82	size_t namelen;
83} wenv_arr[] = {
84	{ NL("ALLUSERSPROFILE=") },
85	{ NL("COMPUTERNAME=") },
86	{ NL("COMSPEC=") },
87	{ NL("CYGWIN=") },
88	{ NL("OS=") },
89	{ NL("PATH=") },
90	{ NL("PATHEXT=") },
91	{ NL("PROGRAMFILES=") },
92	{ NL("SYSTEMDRIVE=") },
93	{ NL("SYSTEMROOT=") },
94	{ NL("WINDIR=") }
95};
96
97char **
98fetch_windows_environment(void)
99{
100	char **e, **p;
101	unsigned int i, idx = 0;
102
103	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
104	for (e = environ; *e != NULL; ++e) {
105		for (i = 0; i < WENV_SIZ; ++i) {
106			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
107				p[idx++] = *e;
108		}
109	}
110	p[idx] = NULL;
111	return p;
112}
113
114void
115free_windows_environment(char **p)
116{
117	free(p);
118}
119
120#endif /* HAVE_CYGWIN */
121