1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * cygwin_util.c
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * Copyright (c) 2000, 2001, Corinna Vinschen <vinschen@cygnus.com>
5*7c478bd9Sstevel@tonic-gate *
6*7c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
7*7c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
8*7c478bd9Sstevel@tonic-gate * are met:
9*7c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
10*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
11*7c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
12*7c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
13*7c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
14*7c478bd9Sstevel@tonic-gate *
15*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16*7c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17*7c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18*7c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19*7c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20*7c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21*7c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22*7c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23*7c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24*7c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*7c478bd9Sstevel@tonic-gate *
26*7c478bd9Sstevel@tonic-gate * Created: Sat Sep 02 12:17:00 2000 cv
27*7c478bd9Sstevel@tonic-gate *
28*7c478bd9Sstevel@tonic-gate * This file contains functions for forcing opened file descriptors to
29*7c478bd9Sstevel@tonic-gate * binary mode on Windows systems.
30*7c478bd9Sstevel@tonic-gate */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include "includes.h"
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate RCSID("$Id: bsd-cygwin_util.c,v 1.8 2002/04/15 22:00:52 stevesk Exp $");
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
37*7c478bd9Sstevel@tonic-gate
38*7c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
44*7c478bd9Sstevel@tonic-gate #include <windows.h>
45*7c478bd9Sstevel@tonic-gate #define is_winnt (GetVersion() < 0x80000000)
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #define ntsec_on(c) ((c) && strstr((c),"ntsec") && !strstr((c),"nontsec"))
48*7c478bd9Sstevel@tonic-gate #define ntea_on(c) ((c) && strstr((c),"ntea") && !strstr((c),"nontea"))
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate #if defined(open) && open == binary_open
51*7c478bd9Sstevel@tonic-gate # undef open
52*7c478bd9Sstevel@tonic-gate #endif
53*7c478bd9Sstevel@tonic-gate #if defined(pipe) && open == binary_pipe
54*7c478bd9Sstevel@tonic-gate # undef pipe
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate
binary_open(const char * filename,int flags,...)57*7c478bd9Sstevel@tonic-gate int binary_open(const char *filename, int flags, ...)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate va_list ap;
60*7c478bd9Sstevel@tonic-gate mode_t mode;
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate va_start(ap, flags);
63*7c478bd9Sstevel@tonic-gate mode = va_arg(ap, mode_t);
64*7c478bd9Sstevel@tonic-gate va_end(ap);
65*7c478bd9Sstevel@tonic-gate return open(filename, flags | O_BINARY, mode);
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate
binary_pipe(int fd[2])68*7c478bd9Sstevel@tonic-gate int binary_pipe(int fd[2])
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate int ret = pipe(fd);
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate if (!ret) {
73*7c478bd9Sstevel@tonic-gate setmode (fd[0], O_BINARY);
74*7c478bd9Sstevel@tonic-gate setmode (fd[1], O_BINARY);
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate return ret;
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate
check_nt_auth(int pwd_authenticated,struct passwd * pw)79*7c478bd9Sstevel@tonic-gate int check_nt_auth(int pwd_authenticated, struct passwd *pw)
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate * The only authentication which is able to change the user
83*7c478bd9Sstevel@tonic-gate * context on NT systems is the password authentication. So
84*7c478bd9Sstevel@tonic-gate * we deny all requsts for changing the user context if another
85*7c478bd9Sstevel@tonic-gate * authentication method is used.
86*7c478bd9Sstevel@tonic-gate *
87*7c478bd9Sstevel@tonic-gate * This doesn't apply to Cygwin versions >= 1.3.2 anymore which
88*7c478bd9Sstevel@tonic-gate * uses the undocumented NtCreateToken() call to create a user
89*7c478bd9Sstevel@tonic-gate * token if the process has the appropriate privileges and if
90*7c478bd9Sstevel@tonic-gate * CYGWIN ntsec setting is on.
91*7c478bd9Sstevel@tonic-gate */
92*7c478bd9Sstevel@tonic-gate static int has_create_token = -1;
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate if (pw == NULL)
95*7c478bd9Sstevel@tonic-gate return 0;
96*7c478bd9Sstevel@tonic-gate if (is_winnt) {
97*7c478bd9Sstevel@tonic-gate if (has_create_token < 0) {
98*7c478bd9Sstevel@tonic-gate struct utsname uts;
99*7c478bd9Sstevel@tonic-gate int major_high = 0, major_low = 0, minor = 0;
100*7c478bd9Sstevel@tonic-gate char *cygwin = getenv("CYGWIN");
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate has_create_token = 0;
103*7c478bd9Sstevel@tonic-gate if (ntsec_on(cygwin) && !uname(&uts)) {
104*7c478bd9Sstevel@tonic-gate sscanf(uts.release, "%d.%d.%d",
105*7c478bd9Sstevel@tonic-gate &major_high, &major_low, &minor);
106*7c478bd9Sstevel@tonic-gate if (major_high > 1 ||
107*7c478bd9Sstevel@tonic-gate (major_high == 1 && (major_low > 3 ||
108*7c478bd9Sstevel@tonic-gate (major_low == 3 && minor >= 2))))
109*7c478bd9Sstevel@tonic-gate has_create_token = 1;
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate if (has_create_token < 1 &&
113*7c478bd9Sstevel@tonic-gate !pwd_authenticated && geteuid() != pw->pw_uid)
114*7c478bd9Sstevel@tonic-gate return 0;
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate return 1;
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate
check_ntsec(const char * filename)119*7c478bd9Sstevel@tonic-gate int check_ntsec(const char *filename)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate char *cygwin;
122*7c478bd9Sstevel@tonic-gate int allow_ntea = 0;
123*7c478bd9Sstevel@tonic-gate int allow_ntsec = 0;
124*7c478bd9Sstevel@tonic-gate struct statfs fsstat;
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate /* Windows 95/98/ME don't support file system security at all. */
127*7c478bd9Sstevel@tonic-gate if (!is_winnt)
128*7c478bd9Sstevel@tonic-gate return 0;
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate /* Evaluate current CYGWIN settings. */
131*7c478bd9Sstevel@tonic-gate cygwin = getenv("CYGWIN");
132*7c478bd9Sstevel@tonic-gate allow_ntea = ntea_on(cygwin);
133*7c478bd9Sstevel@tonic-gate allow_ntsec = ntsec_on(cygwin);
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate * `ntea' is an emulation of POSIX attributes. It doesn't support
137*7c478bd9Sstevel@tonic-gate * real file level security as ntsec on NTFS file systems does
138*7c478bd9Sstevel@tonic-gate * but it supports FAT filesystems. `ntea' is minimum requirement
139*7c478bd9Sstevel@tonic-gate * for security checks.
140*7c478bd9Sstevel@tonic-gate */
141*7c478bd9Sstevel@tonic-gate if (allow_ntea)
142*7c478bd9Sstevel@tonic-gate return 1;
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate /*
145*7c478bd9Sstevel@tonic-gate * Retrieve file system flags. In Cygwin, file system flags are
146*7c478bd9Sstevel@tonic-gate * copied to f_type which has no meaning in Win32 itself.
147*7c478bd9Sstevel@tonic-gate */
148*7c478bd9Sstevel@tonic-gate if (statfs(filename, &fsstat))
149*7c478bd9Sstevel@tonic-gate return 1;
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate /*
152*7c478bd9Sstevel@tonic-gate * Only file systems supporting ACLs are able to set permissions.
153*7c478bd9Sstevel@tonic-gate * `ntsec' is the setting in Cygwin which switches using of NTFS
154*7c478bd9Sstevel@tonic-gate * ACLs to support POSIX permissions on files.
155*7c478bd9Sstevel@tonic-gate */
156*7c478bd9Sstevel@tonic-gate if (fsstat.f_type & FS_PERSISTENT_ACLS)
157*7c478bd9Sstevel@tonic-gate return allow_ntsec;
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate return 0;
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate
register_9x_service(void)162*7c478bd9Sstevel@tonic-gate void register_9x_service(void)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate HINSTANCE kerneldll;
165*7c478bd9Sstevel@tonic-gate DWORD (*RegisterServiceProcess)(DWORD, DWORD);
166*7c478bd9Sstevel@tonic-gate
167*7c478bd9Sstevel@tonic-gate /* The service register mechanism in 9x/Me is pretty different from
168*7c478bd9Sstevel@tonic-gate * NT/2K/XP. In NT/2K/XP we're using a special service starter
169*7c478bd9Sstevel@tonic-gate * application to register and control sshd as service. This method
170*7c478bd9Sstevel@tonic-gate * doesn't play nicely with 9x/Me. For that reason we register here
171*7c478bd9Sstevel@tonic-gate * as service when running under 9x/Me. This function is only called
172*7c478bd9Sstevel@tonic-gate * by the child sshd when it's going to daemonize.
173*7c478bd9Sstevel@tonic-gate */
174*7c478bd9Sstevel@tonic-gate if (is_winnt)
175*7c478bd9Sstevel@tonic-gate return;
176*7c478bd9Sstevel@tonic-gate if (! (kerneldll = LoadLibrary("KERNEL32.DLL")))
177*7c478bd9Sstevel@tonic-gate return;
178*7c478bd9Sstevel@tonic-gate if (! (RegisterServiceProcess = (DWORD (*)(DWORD, DWORD))
179*7c478bd9Sstevel@tonic-gate GetProcAddress(kerneldll, "RegisterServiceProcess")))
180*7c478bd9Sstevel@tonic-gate return;
181*7c478bd9Sstevel@tonic-gate RegisterServiceProcess(0, 1);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate
184*7c478bd9Sstevel@tonic-gate #endif /* HAVE_CYGWIN */
185