blob: a87cf3c97bc21b840518b3bccedb92cfbb4a5708 [file] [log] [blame]
Damien Millerbac2d8a2000-09-05 16:13:06 +11001/*
Kevin Steveseb363022002-04-15 22:00:51 +00002 * Copyright (c) 2000, 2001, Corinna Vinschen <vinschen@cygnus.com>
Damien Millerbac2d8a2000-09-05 16:13:06 +11003 *
Kevin Steveseb363022002-04-15 22:00:51 +00004 * 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.
Damien Millerbac2d8a2000-09-05 16:13:06 +110023 *
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
Damien Millere9cf3572001-02-09 12:55:35 +110030#include "includes.h"
31
Darren Tucker854d7fd2003-08-07 16:23:43 +100032RCSID("$Id: bsd-cygwin_util.c,v 1.11 2003/08/07 06:23:43 dtucker Exp $");
Damien Millerbac2d8a2000-09-05 16:13:06 +110033
34#ifdef HAVE_CYGWIN
Damien Miller72c9a7e2000-09-24 11:10:13 +110035
Damien Millerbac2d8a2000-09-05 16:13:06 +110036#include <fcntl.h>
Damien Millerb70b61f2000-09-16 16:25:12 +110037#include <stdlib.h>
Ben Lindstromcff94be2001-07-18 16:19:48 +000038#include <sys/utsname.h>
Damien Millerb70b61f2000-09-16 16:25:12 +110039#include <sys/vfs.h>
40#include <windows.h>
41#define is_winnt (GetVersion() < 0x80000000)
Damien Millerbac2d8a2000-09-05 16:13:06 +110042
Ben Lindstromcff94be2001-07-18 16:19:48 +000043#define ntsec_on(c) ((c) && strstr((c),"ntsec") && !strstr((c),"nontsec"))
Ben Lindstrom224313c2002-11-09 15:59:27 +000044#define ntsec_off(c) ((c) && strstr((c),"nontsec"))
Ben Lindstromcff94be2001-07-18 16:19:48 +000045#define ntea_on(c) ((c) && strstr((c),"ntea") && !strstr((c),"nontea"))
46
Damien Miller2deb3f62001-02-18 12:30:55 +110047#if defined(open) && open == binary_open
48# undef open
49#endif
50#if defined(pipe) && open == binary_pipe
51# undef pipe
52#endif
53
Damien Miller31741252003-05-19 00:13:38 +100054int
55binary_open(const char *filename, int flags, ...)
Damien Millerbac2d8a2000-09-05 16:13:06 +110056{
Damien Miller2deb3f62001-02-18 12:30:55 +110057 va_list ap;
58 mode_t mode;
59
60 va_start(ap, flags);
61 mode = va_arg(ap, mode_t);
62 va_end(ap);
Damien Miller31741252003-05-19 00:13:38 +100063 return (open(filename, flags | O_BINARY, mode));
Damien Millerbac2d8a2000-09-05 16:13:06 +110064}
65
Damien Miller31741252003-05-19 00:13:38 +100066int
67binary_pipe(int fd[2])
Damien Millerbac2d8a2000-09-05 16:13:06 +110068{
Damien Miller72c9a7e2000-09-24 11:10:13 +110069 int ret = pipe(fd);
70
71 if (!ret) {
Damien Miller31741252003-05-19 00:13:38 +100072 setmode(fd[0], O_BINARY);
73 setmode(fd[1], O_BINARY);
Damien Miller72c9a7e2000-09-24 11:10:13 +110074 }
Damien Miller31741252003-05-19 00:13:38 +100075 return (ret);
Damien Millerb70b61f2000-09-16 16:25:12 +110076}
77
Ben Lindstrom224313c2002-11-09 15:59:27 +000078#define HAS_CREATE_TOKEN 1
79#define HAS_NTSEC_BY_DEFAULT 2
80
Damien Miller31741252003-05-19 00:13:38 +100081static int
82has_capability(int what)
Ben Lindstrom224313c2002-11-09 15:59:27 +000083{
Ben Lindstrom224313c2002-11-09 15:59:27 +000084 static int inited;
85 static int has_create_token;
86 static int has_ntsec_by_default;
87
Damien Miller31741252003-05-19 00:13:38 +100088 /*
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 */
Ben Lindstrom224313c2002-11-09 15:59:27 +000095 if (!inited) {
96 struct utsname uts;
97 char *c;
98
99 if (!uname(&uts)) {
Damien Miller31741252003-05-19 00:13:38 +1000100 int major_high = 0, major_low = 0, minor = 0;
101 int api_major_version = 0, api_minor_version = 0;
Ben Lindstrom224313c2002-11-09 15:59:27 +0000102 char *c;
103
104 sscanf(uts.release, "%d.%d.%d", &major_high,
Damien Miller31741252003-05-19 00:13:38 +1000105 &major_low, &minor);
106 if ((c = strchr(uts.release, '(')) != NULL) {
Ben Lindstrom224313c2002-11-09 15:59:27 +0000107 sscanf(c + 1, "%d.%d", &api_major_version,
Damien Miller31741252003-05-19 00:13:38 +1000108 &api_minor_version);
109 }
Ben Lindstrom224313c2002-11-09 15:59:27 +0000110 if (major_high > 1 ||
111 (major_high == 1 && (major_low > 3 ||
Damien Miller31741252003-05-19 00:13:38 +1000112 (major_low == 3 && minor >= 2))))
Ben Lindstrom224313c2002-11-09 15:59:27 +0000113 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:
Damien Miller31741252003-05-19 00:13:38 +1000121 return (has_create_token);
Ben Lindstrom224313c2002-11-09 15:59:27 +0000122 case HAS_NTSEC_BY_DEFAULT:
Damien Miller31741252003-05-19 00:13:38 +1000123 return (has_ntsec_by_default);
Ben Lindstrom224313c2002-11-09 15:59:27 +0000124 }
Damien Miller31741252003-05-19 00:13:38 +1000125 return (0);
Ben Lindstrom224313c2002-11-09 15:59:27 +0000126}
127
Damien Miller31741252003-05-19 00:13:38 +1000128int
129check_nt_auth(int pwd_authenticated, struct passwd *pw)
Damien Millerb70b61f2000-09-16 16:25:12 +1100130{
131 /*
Damien Miller72c9a7e2000-09-24 11:10:13 +1100132 * 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.
Ben Lindstromcff94be2001-07-18 16:19:48 +0000136 *
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.
Damien Miller72c9a7e2000-09-24 11:10:13 +1100141 */
Ben Lindstromcff94be2001-07-18 16:19:48 +0000142 static int has_create_token = -1;
143
Damien Miller0dea79d2001-12-29 14:08:28 +1100144 if (pw == NULL)
145 return 0;
Ben Lindstromcff94be2001-07-18 16:19:48 +0000146 if (is_winnt) {
147 if (has_create_token < 0) {
Ben Lindstromcff94be2001-07-18 16:19:48 +0000148 char *cygwin = getenv("CYGWIN");
149
150 has_create_token = 0;
Ben Lindstrom224313c2002-11-09 15:59:27 +0000151 if (has_capability(HAS_CREATE_TOKEN) &&
152 (ntsec_on(cygwin) ||
Damien Miller31741252003-05-19 00:13:38 +1000153 (has_capability(HAS_NTSEC_BY_DEFAULT) &&
154 !ntsec_off(cygwin))))
Ben Lindstrom224313c2002-11-09 15:59:27 +0000155 has_create_token = 1;
Ben Lindstromcff94be2001-07-18 16:19:48 +0000156 }
157 if (has_create_token < 1 &&
Damien Miller0dea79d2001-12-29 14:08:28 +1100158 !pwd_authenticated && geteuid() != pw->pw_uid)
Damien Miller31741252003-05-19 00:13:38 +1000159 return (0);
Ben Lindstromcff94be2001-07-18 16:19:48 +0000160 }
Damien Miller31741252003-05-19 00:13:38 +1000161 return (1);
Damien Millerb70b61f2000-09-16 16:25:12 +1100162}
163
Damien Miller31741252003-05-19 00:13:38 +1000164int
165check_ntsec(const char *filename)
Damien Millerb70b61f2000-09-16 16:25:12 +1100166{
167 char *cygwin;
Damien Miller31741252003-05-19 00:13:38 +1000168 int allow_ntea = 0, allow_ntsec = 0;
Damien Millerb70b61f2000-09-16 16:25:12 +1100169 struct statfs fsstat;
170
171 /* Windows 95/98/ME don't support file system security at all. */
172 if (!is_winnt)
Damien Miller31741252003-05-19 00:13:38 +1000173 return (0);
Damien Millerb70b61f2000-09-16 16:25:12 +1100174
175 /* Evaluate current CYGWIN settings. */
Ben Lindstromcff94be2001-07-18 16:19:48 +0000176 cygwin = getenv("CYGWIN");
177 allow_ntea = ntea_on(cygwin);
Ben Lindstrom224313c2002-11-09 15:59:27 +0000178 allow_ntsec = ntsec_on(cygwin) ||
Damien Miller31741252003-05-19 00:13:38 +1000179 (has_capability(HAS_NTSEC_BY_DEFAULT) && !ntsec_off(cygwin));
Damien Millerb70b61f2000-09-16 16:25:12 +1100180
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)
Damien Miller31741252003-05-19 00:13:38 +1000188 return (1);
Damien Millerb70b61f2000-09-16 16:25:12 +1100189
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))
Damien Miller31741252003-05-19 00:13:38 +1000195 return (1);
Damien Millerb70b61f2000-09-16 16:25:12 +1100196
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)
Damien Miller31741252003-05-19 00:13:38 +1000203 return (allow_ntsec);
Damien Millerb70b61f2000-09-16 16:25:12 +1100204
Damien Miller31741252003-05-19 00:13:38 +1000205 return (0);
Damien Millerbac2d8a2000-09-05 16:13:06 +1100206}
Damien Miller72c9a7e2000-09-24 11:10:13 +1100207
Damien Miller31741252003-05-19 00:13:38 +1000208void
209register_9x_service(void)
Tim Ricefe1d1002001-11-26 17:19:43 -0800210{
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;
Damien Miller31741252003-05-19 00:13:38 +1000223 if (!(kerneldll = LoadLibrary("KERNEL32.DLL")))
Tim Ricefe1d1002001-11-26 17:19:43 -0800224 return;
Damien Miller31741252003-05-19 00:13:38 +1000225 if (!(RegisterServiceProcess = (DWORD (*)(DWORD, DWORD))
226 GetProcAddress(kerneldll, "RegisterServiceProcess")))
Tim Ricefe1d1002001-11-26 17:19:43 -0800227 return;
228 RegisterServiceProcess(0, 1);
229}
230
Damien Miller72c9a7e2000-09-24 11:10:13 +1100231#endif /* HAVE_CYGWIN */