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