]> andersk Git - openssh.git/blame - openbsd-compat/bsd-cygwin_util.c
spacing
[openssh.git] / openbsd-compat / bsd-cygwin_util.c
CommitLineData
3c62e7eb 1/*
4e51cc76 2 * Copyright (c) 2000, 2001, Corinna Vinschen <vinschen@cygnus.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.
3c62e7eb 12 *
4e51cc76 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.
3c62e7eb 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
0b202697 30#include "includes.h"
31
3c62e7eb 32#ifdef HAVE_CYGWIN
2f125ca1 33
dd41ba6f 34#if defined(open) && open == binary_open
35# undef open
36#endif
37#if defined(pipe) && open == binary_pipe
38# undef pipe
39#endif
40
97ea266c 41#include <sys/types.h>
42#include <sys/stat.h>
31fb6aaf 43#include <sys/utsname.h>
9cd45ea4 44#include <sys/vfs.h>
97ea266c 45
46#include <fcntl.h>
47#include <stdlib.h>
48#include <unistd.h>
9cd45ea4 49#include <windows.h>
97ea266c 50
3c502155 51#include "xmalloc.h"
9cd45ea4 52#define is_winnt (GetVersion() < 0x80000000)
3c62e7eb 53
31fb6aaf 54#define ntsec_on(c) ((c) && strstr((c),"ntsec") && !strstr((c),"nontsec"))
f00addc9 55#define ntsec_off(c) ((c) && strstr((c),"nontsec"))
31fb6aaf 56#define ntea_on(c) ((c) && strstr((c),"ntea") && !strstr((c),"nontea"))
57
9901cb37 58int
59binary_open(const char *filename, int flags, ...)
3c62e7eb 60{
58e7f038 61 va_list ap;
62 mode_t mode;
63
64 va_start(ap, flags);
65 mode = va_arg(ap, mode_t);
66 va_end(ap);
9901cb37 67 return (open(filename, flags | O_BINARY, mode));
3c62e7eb 68}
69
9901cb37 70int
71binary_pipe(int fd[2])
3c62e7eb 72{
2f125ca1 73 int ret = pipe(fd);
74
75 if (!ret) {
9901cb37 76 setmode(fd[0], O_BINARY);
77 setmode(fd[1], O_BINARY);
2f125ca1 78 }
9901cb37 79 return (ret);
9cd45ea4 80}
81
f00addc9 82#define HAS_CREATE_TOKEN 1
83#define HAS_NTSEC_BY_DEFAULT 2
917ee1d2 84#define HAS_CREATE_TOKEN_WO_NTSEC 3
f00addc9 85
9901cb37 86static int
87has_capability(int what)
f00addc9 88{
f00addc9 89 static int inited;
90 static int has_create_token;
91 static int has_ntsec_by_default;
917ee1d2 92 static int has_create_token_wo_ntsec;
f00addc9 93
9901cb37 94 /*
95 * has_capability() basically calls uname() and checks if
96 * specific capabilities of Cygwin can be evaluated from that.
97 * This simplifies the calling functions which only have to ask
98 * for a capability using has_capability() instead of having
99 * to figure that out by themselves.
100 */
f00addc9 101 if (!inited) {
102 struct utsname uts;
f00addc9 103
104 if (!uname(&uts)) {
9901cb37 105 int major_high = 0, major_low = 0, minor = 0;
106 int api_major_version = 0, api_minor_version = 0;
f00addc9 107 char *c;
108
109 sscanf(uts.release, "%d.%d.%d", &major_high,
9901cb37 110 &major_low, &minor);
111 if ((c = strchr(uts.release, '(')) != NULL) {
f00addc9 112 sscanf(c + 1, "%d.%d", &api_major_version,
9901cb37 113 &api_minor_version);
114 }
f00addc9 115 if (major_high > 1 ||
116 (major_high == 1 && (major_low > 3 ||
9901cb37 117 (major_low == 3 && minor >= 2))))
f00addc9 118 has_create_token = 1;
119 if (api_major_version > 0 || api_minor_version >= 56)
120 has_ntsec_by_default = 1;
917ee1d2 121 if (major_high > 1 ||
122 (major_high == 1 && major_low >= 5))
123 has_create_token_wo_ntsec = 1;
f00addc9 124 inited = 1;
125 }
126 }
127 switch (what) {
128 case HAS_CREATE_TOKEN:
9901cb37 129 return (has_create_token);
f00addc9 130 case HAS_NTSEC_BY_DEFAULT:
9901cb37 131 return (has_ntsec_by_default);
917ee1d2 132 case HAS_CREATE_TOKEN_WO_NTSEC:
133 return (has_create_token_wo_ntsec);
f00addc9 134 }
9901cb37 135 return (0);
f00addc9 136}
137
9901cb37 138int
139check_nt_auth(int pwd_authenticated, struct passwd *pw)
9cd45ea4 140{
141 /*
2f125ca1 142 * The only authentication which is able to change the user
143 * context on NT systems is the password authentication. So
144 * we deny all requsts for changing the user context if another
145 * authentication method is used.
31fb6aaf 146 *
147 * This doesn't apply to Cygwin versions >= 1.3.2 anymore which
148 * uses the undocumented NtCreateToken() call to create a user
149 * token if the process has the appropriate privileges and if
150 * CYGWIN ntsec setting is on.
2f125ca1 151 */
31fb6aaf 152 static int has_create_token = -1;
153
e9571a2c 154 if (pw == NULL)
155 return 0;
31fb6aaf 156 if (is_winnt) {
157 if (has_create_token < 0) {
31fb6aaf 158 char *cygwin = getenv("CYGWIN");
159
160 has_create_token = 0;
f00addc9 161 if (has_capability(HAS_CREATE_TOKEN) &&
162 (ntsec_on(cygwin) ||
9901cb37 163 (has_capability(HAS_NTSEC_BY_DEFAULT) &&
917ee1d2 164 !ntsec_off(cygwin)) ||
165 has_capability(HAS_CREATE_TOKEN_WO_NTSEC)))
f00addc9 166 has_create_token = 1;
31fb6aaf 167 }
168 if (has_create_token < 1 &&
e9571a2c 169 !pwd_authenticated && geteuid() != pw->pw_uid)
9901cb37 170 return (0);
31fb6aaf 171 }
9901cb37 172 return (1);
9cd45ea4 173}
174
9901cb37 175int
176check_ntsec(const char *filename)
9cd45ea4 177{
178 char *cygwin;
9901cb37 179 int allow_ntea = 0, allow_ntsec = 0;
9cd45ea4 180 struct statfs fsstat;
181
182 /* Windows 95/98/ME don't support file system security at all. */
183 if (!is_winnt)
9901cb37 184 return (0);
9cd45ea4 185
186 /* Evaluate current CYGWIN settings. */
31fb6aaf 187 cygwin = getenv("CYGWIN");
188 allow_ntea = ntea_on(cygwin);
f00addc9 189 allow_ntsec = ntsec_on(cygwin) ||
9901cb37 190 (has_capability(HAS_NTSEC_BY_DEFAULT) && !ntsec_off(cygwin));
9cd45ea4 191
192 /*
193 * `ntea' is an emulation of POSIX attributes. It doesn't support
194 * real file level security as ntsec on NTFS file systems does
195 * but it supports FAT filesystems. `ntea' is minimum requirement
196 * for security checks.
197 */
198 if (allow_ntea)
9901cb37 199 return (1);
9cd45ea4 200
201 /*
202 * Retrieve file system flags. In Cygwin, file system flags are
203 * copied to f_type which has no meaning in Win32 itself.
204 */
205 if (statfs(filename, &fsstat))
9901cb37 206 return (1);
9cd45ea4 207
208 /*
209 * Only file systems supporting ACLs are able to set permissions.
210 * `ntsec' is the setting in Cygwin which switches using of NTFS
211 * ACLs to support POSIX permissions on files.
212 */
213 if (fsstat.f_type & FS_PERSISTENT_ACLS)
9901cb37 214 return (allow_ntsec);
9cd45ea4 215
9901cb37 216 return (0);
3c62e7eb 217}
2f125ca1 218
9901cb37 219void
220register_9x_service(void)
ffb8d130 221{
222 HINSTANCE kerneldll;
223 DWORD (*RegisterServiceProcess)(DWORD, DWORD);
224
225 /* The service register mechanism in 9x/Me is pretty different from
226 * NT/2K/XP. In NT/2K/XP we're using a special service starter
227 * application to register and control sshd as service. This method
228 * doesn't play nicely with 9x/Me. For that reason we register here
229 * as service when running under 9x/Me. This function is only called
230 * by the child sshd when it's going to daemonize.
231 */
232 if (is_winnt)
233 return;
9901cb37 234 if (!(kerneldll = LoadLibrary("KERNEL32.DLL")))
ffb8d130 235 return;
9901cb37 236 if (!(RegisterServiceProcess = (DWORD (*)(DWORD, DWORD))
237 GetProcAddress(kerneldll, "RegisterServiceProcess")))
ffb8d130 238 return;
239 RegisterServiceProcess(0, 1);
240}
241
3c502155 242#define NL(x) x, (sizeof (x) - 1)
243#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
244
245static struct wenv {
246 const char *name;
247 size_t namelen;
248} wenv_arr[] = {
249 { NL("ALLUSERSPROFILE=") },
250 { NL("COMMONPROGRAMFILES=") },
251 { NL("COMPUTERNAME=") },
252 { NL("COMSPEC=") },
b54ffe05 253 { NL("CYGWIN=") },
3c502155 254 { NL("NUMBER_OF_PROCESSORS=") },
255 { NL("OS=") },
256 { NL("PATH=") },
257 { NL("PATHEXT=") },
258 { NL("PROCESSOR_ARCHITECTURE=") },
259 { NL("PROCESSOR_IDENTIFIER=") },
260 { NL("PROCESSOR_LEVEL=") },
261 { NL("PROCESSOR_REVISION=") },
262 { NL("PROGRAMFILES=") },
263 { NL("SYSTEMDRIVE=") },
264 { NL("SYSTEMROOT=") },
265 { NL("TMP=") },
266 { NL("TEMP=") },
b54ffe05 267 { NL("WINDIR=") }
3c502155 268};
269
270char **
271fetch_windows_environment(void)
272{
273 char **e, **p;
06a517d4 274 unsigned int i, idx = 0;
3c502155 275
01d35895 276 p = xcalloc(WENV_SIZ + 1, sizeof(char *));
3c502155 277 for (e = environ; *e != NULL; ++e) {
278 for (i = 0; i < WENV_SIZ; ++i) {
279 if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
280 p[idx++] = *e;
281 }
282 }
283 p[idx] = NULL;
284 return p;
285}
286
287void
288free_windows_environment(char **p)
289{
290 xfree(p);
291}
292
2f125ca1 293#endif /* HAVE_CYGWIN */
This page took 1.094252 seconds and 5 git commands to generate.