]> andersk Git - openssh.git/blame - openbsd-compat/bsd-cygwin_util.c
- (dtucker) [openbsd-compat/port-aix.c] Use correct include for xmalloc.h,
[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
32RCSID("$Id$");
3c62e7eb 33
34#ifdef HAVE_CYGWIN
2f125ca1 35
3c62e7eb 36#include <fcntl.h>
9cd45ea4 37#include <stdlib.h>
31fb6aaf 38#include <sys/utsname.h>
9cd45ea4 39#include <sys/vfs.h>
40#include <windows.h>
41#define is_winnt (GetVersion() < 0x80000000)
3c62e7eb 42
31fb6aaf 43#define ntsec_on(c) ((c) && strstr((c),"ntsec") && !strstr((c),"nontsec"))
f00addc9 44#define ntsec_off(c) ((c) && strstr((c),"nontsec"))
31fb6aaf 45#define ntea_on(c) ((c) && strstr((c),"ntea") && !strstr((c),"nontea"))
46
58e7f038 47#if defined(open) && open == binary_open
48# undef open
49#endif
50#if defined(pipe) && open == binary_pipe
51# undef pipe
52#endif
53
9901cb37 54int
55binary_open(const char *filename, int flags, ...)
3c62e7eb 56{
58e7f038 57 va_list ap;
58 mode_t mode;
59
60 va_start(ap, flags);
61 mode = va_arg(ap, mode_t);
62 va_end(ap);
9901cb37 63 return (open(filename, flags | O_BINARY, mode));
3c62e7eb 64}
65
9901cb37 66int
67binary_pipe(int fd[2])
3c62e7eb 68{
2f125ca1 69 int ret = pipe(fd);
70
71 if (!ret) {
9901cb37 72 setmode(fd[0], O_BINARY);
73 setmode(fd[1], O_BINARY);
2f125ca1 74 }
9901cb37 75 return (ret);
9cd45ea4 76}
77
f00addc9 78#define HAS_CREATE_TOKEN 1
79#define HAS_NTSEC_BY_DEFAULT 2
80
9901cb37 81static int
82has_capability(int what)
f00addc9 83{
f00addc9 84 static int inited;
85 static int has_create_token;
86 static int has_ntsec_by_default;
87
9901cb37 88 /*
89 * has_capability() basically calls uname() and checks if
90 * specific capabilities of Cygwin can be evaluated from that.
91 * This simplifies the calling functions which only have to ask
92 * for a capability using has_capability() instead of having
93 * to figure that out by themselves.
94 */
f00addc9 95 if (!inited) {
96 struct utsname uts;
97 char *c;
98
99 if (!uname(&uts)) {
9901cb37 100 int major_high = 0, major_low = 0, minor = 0;
101 int api_major_version = 0, api_minor_version = 0;
f00addc9 102 char *c;
103
104 sscanf(uts.release, "%d.%d.%d", &major_high,
9901cb37 105 &major_low, &minor);
106 if ((c = strchr(uts.release, '(')) != NULL) {
f00addc9 107 sscanf(c + 1, "%d.%d", &api_major_version,
9901cb37 108 &api_minor_version);
109 }
f00addc9 110 if (major_high > 1 ||
111 (major_high == 1 && (major_low > 3 ||
9901cb37 112 (major_low == 3 && minor >= 2))))
f00addc9 113 has_create_token = 1;
114 if (api_major_version > 0 || api_minor_version >= 56)
115 has_ntsec_by_default = 1;
116 inited = 1;
117 }
118 }
119 switch (what) {
120 case HAS_CREATE_TOKEN:
9901cb37 121 return (has_create_token);
f00addc9 122 case HAS_NTSEC_BY_DEFAULT:
9901cb37 123 return (has_ntsec_by_default);
f00addc9 124 }
9901cb37 125 return (0);
f00addc9 126}
127
9901cb37 128int
129check_nt_auth(int pwd_authenticated, struct passwd *pw)
9cd45ea4 130{
131 /*
2f125ca1 132 * The only authentication which is able to change the user
133 * context on NT systems is the password authentication. So
134 * we deny all requsts for changing the user context if another
135 * authentication method is used.
31fb6aaf 136 *
137 * This doesn't apply to Cygwin versions >= 1.3.2 anymore which
138 * uses the undocumented NtCreateToken() call to create a user
139 * token if the process has the appropriate privileges and if
140 * CYGWIN ntsec setting is on.
2f125ca1 141 */
31fb6aaf 142 static int has_create_token = -1;
143
e9571a2c 144 if (pw == NULL)
145 return 0;
31fb6aaf 146 if (is_winnt) {
147 if (has_create_token < 0) {
31fb6aaf 148 char *cygwin = getenv("CYGWIN");
149
150 has_create_token = 0;
f00addc9 151 if (has_capability(HAS_CREATE_TOKEN) &&
152 (ntsec_on(cygwin) ||
9901cb37 153 (has_capability(HAS_NTSEC_BY_DEFAULT) &&
154 !ntsec_off(cygwin))))
f00addc9 155 has_create_token = 1;
31fb6aaf 156 }
157 if (has_create_token < 1 &&
e9571a2c 158 !pwd_authenticated && geteuid() != pw->pw_uid)
9901cb37 159 return (0);
31fb6aaf 160 }
9901cb37 161 return (1);
9cd45ea4 162}
163
9901cb37 164int
165check_ntsec(const char *filename)
9cd45ea4 166{
167 char *cygwin;
9901cb37 168 int allow_ntea = 0, allow_ntsec = 0;
9cd45ea4 169 struct statfs fsstat;
170
171 /* Windows 95/98/ME don't support file system security at all. */
172 if (!is_winnt)
9901cb37 173 return (0);
9cd45ea4 174
175 /* Evaluate current CYGWIN settings. */
31fb6aaf 176 cygwin = getenv("CYGWIN");
177 allow_ntea = ntea_on(cygwin);
f00addc9 178 allow_ntsec = ntsec_on(cygwin) ||
9901cb37 179 (has_capability(HAS_NTSEC_BY_DEFAULT) && !ntsec_off(cygwin));
9cd45ea4 180
181 /*
182 * `ntea' is an emulation of POSIX attributes. It doesn't support
183 * real file level security as ntsec on NTFS file systems does
184 * but it supports FAT filesystems. `ntea' is minimum requirement
185 * for security checks.
186 */
187 if (allow_ntea)
9901cb37 188 return (1);
9cd45ea4 189
190 /*
191 * Retrieve file system flags. In Cygwin, file system flags are
192 * copied to f_type which has no meaning in Win32 itself.
193 */
194 if (statfs(filename, &fsstat))
9901cb37 195 return (1);
9cd45ea4 196
197 /*
198 * Only file systems supporting ACLs are able to set permissions.
199 * `ntsec' is the setting in Cygwin which switches using of NTFS
200 * ACLs to support POSIX permissions on files.
201 */
202 if (fsstat.f_type & FS_PERSISTENT_ACLS)
9901cb37 203 return (allow_ntsec);
9cd45ea4 204
9901cb37 205 return (0);
3c62e7eb 206}
2f125ca1 207
9901cb37 208void
209register_9x_service(void)
ffb8d130 210{
211 HINSTANCE kerneldll;
212 DWORD (*RegisterServiceProcess)(DWORD, DWORD);
213
214 /* The service register mechanism in 9x/Me is pretty different from
215 * NT/2K/XP. In NT/2K/XP we're using a special service starter
216 * application to register and control sshd as service. This method
217 * doesn't play nicely with 9x/Me. For that reason we register here
218 * as service when running under 9x/Me. This function is only called
219 * by the child sshd when it's going to daemonize.
220 */
221 if (is_winnt)
222 return;
9901cb37 223 if (!(kerneldll = LoadLibrary("KERNEL32.DLL")))
ffb8d130 224 return;
9901cb37 225 if (!(RegisterServiceProcess = (DWORD (*)(DWORD, DWORD))
226 GetProcAddress(kerneldll, "RegisterServiceProcess")))
ffb8d130 227 return;
228 RegisterServiceProcess(0, 1);
229}
230
2f125ca1 231#endif /* HAVE_CYGWIN */
This page took 0.185062 seconds and 5 git commands to generate.