]>
Commit | Line | Data |
---|---|---|
3c0ef626 | 1 | /* |
700318f3 | 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. | |
3c0ef626 | 12 | * |
700318f3 | 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. | |
3c0ef626 | 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 | ||
30 | #include "includes.h" | |
31 | ||
3c0ef626 | 32 | #ifdef HAVE_CYGWIN |
33 | ||
9108f8d9 | 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 | ||
41 | #include <sys/types.h> | |
42 | #include <sys/stat.h> | |
3c0ef626 | 43 | #include <sys/utsname.h> |
44 | #include <sys/vfs.h> | |
9108f8d9 | 45 | |
46 | #include <fcntl.h> | |
47 | #include <stdlib.h> | |
48 | #include <unistd.h> | |
3c0ef626 | 49 | #include <windows.h> |
9108f8d9 | 50 | |
996d5e62 | 51 | #include "xmalloc.h" |
3c0ef626 | 52 | #define is_winnt (GetVersion() < 0x80000000) |
53 | ||
54 | #define ntsec_on(c) ((c) && strstr((c),"ntsec") && !strstr((c),"nontsec")) | |
6a9b3198 | 55 | #define ntsec_off(c) ((c) && strstr((c),"nontsec")) |
3c0ef626 | 56 | #define ntea_on(c) ((c) && strstr((c),"ntea") && !strstr((c),"nontea")) |
57 | ||
0fff78ff | 58 | int |
59 | binary_open(const char *filename, int flags, ...) | |
3c0ef626 | 60 | { |
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); | |
0fff78ff | 67 | return (open(filename, flags | O_BINARY, mode)); |
3c0ef626 | 68 | } |
69 | ||
0fff78ff | 70 | int |
71 | binary_pipe(int fd[2]) | |
3c0ef626 | 72 | { |
73 | int ret = pipe(fd); | |
74 | ||
75 | if (!ret) { | |
0fff78ff | 76 | setmode(fd[0], O_BINARY); |
77 | setmode(fd[1], O_BINARY); | |
3c0ef626 | 78 | } |
0fff78ff | 79 | return (ret); |
3c0ef626 | 80 | } |
81 | ||
6a9b3198 | 82 | #define HAS_CREATE_TOKEN 1 |
83 | #define HAS_NTSEC_BY_DEFAULT 2 | |
99be0775 | 84 | #define HAS_CREATE_TOKEN_WO_NTSEC 3 |
6a9b3198 | 85 | |
0fff78ff | 86 | static int |
87 | has_capability(int what) | |
6a9b3198 | 88 | { |
6a9b3198 | 89 | static int inited; |
90 | static int has_create_token; | |
91 | static int has_ntsec_by_default; | |
99be0775 | 92 | static int has_create_token_wo_ntsec; |
6a9b3198 | 93 | |
0fff78ff | 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 | */ | |
6a9b3198 | 101 | if (!inited) { |
102 | struct utsname uts; | |
6a9b3198 | 103 | |
104 | if (!uname(&uts)) { | |
0fff78ff | 105 | int major_high = 0, major_low = 0, minor = 0; |
106 | int api_major_version = 0, api_minor_version = 0; | |
6a9b3198 | 107 | char *c; |
108 | ||
109 | sscanf(uts.release, "%d.%d.%d", &major_high, | |
0fff78ff | 110 | &major_low, &minor); |
111 | if ((c = strchr(uts.release, '(')) != NULL) { | |
6a9b3198 | 112 | sscanf(c + 1, "%d.%d", &api_major_version, |
0fff78ff | 113 | &api_minor_version); |
114 | } | |
6a9b3198 | 115 | if (major_high > 1 || |
116 | (major_high == 1 && (major_low > 3 || | |
0fff78ff | 117 | (major_low == 3 && minor >= 2)))) |
6a9b3198 | 118 | has_create_token = 1; |
119 | if (api_major_version > 0 || api_minor_version >= 56) | |
120 | has_ntsec_by_default = 1; | |
99be0775 | 121 | if (major_high > 1 || |
122 | (major_high == 1 && major_low >= 5)) | |
123 | has_create_token_wo_ntsec = 1; | |
6a9b3198 | 124 | inited = 1; |
125 | } | |
126 | } | |
127 | switch (what) { | |
128 | case HAS_CREATE_TOKEN: | |
0fff78ff | 129 | return (has_create_token); |
6a9b3198 | 130 | case HAS_NTSEC_BY_DEFAULT: |
0fff78ff | 131 | return (has_ntsec_by_default); |
99be0775 | 132 | case HAS_CREATE_TOKEN_WO_NTSEC: |
133 | return (has_create_token_wo_ntsec); | |
6a9b3198 | 134 | } |
0fff78ff | 135 | return (0); |
6a9b3198 | 136 | } |
137 | ||
0fff78ff | 138 | int |
139 | check_nt_auth(int pwd_authenticated, struct passwd *pw) | |
3c0ef626 | 140 | { |
141 | /* | |
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. | |
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. | |
151 | */ | |
152 | static int has_create_token = -1; | |
153 | ||
e9a17296 | 154 | if (pw == NULL) |
155 | return 0; | |
3c0ef626 | 156 | if (is_winnt) { |
157 | if (has_create_token < 0) { | |
3c0ef626 | 158 | char *cygwin = getenv("CYGWIN"); |
159 | ||
160 | has_create_token = 0; | |
6a9b3198 | 161 | if (has_capability(HAS_CREATE_TOKEN) && |
162 | (ntsec_on(cygwin) || | |
0fff78ff | 163 | (has_capability(HAS_NTSEC_BY_DEFAULT) && |
99be0775 | 164 | !ntsec_off(cygwin)) || |
165 | has_capability(HAS_CREATE_TOKEN_WO_NTSEC))) | |
6a9b3198 | 166 | has_create_token = 1; |
3c0ef626 | 167 | } |
168 | if (has_create_token < 1 && | |
e9a17296 | 169 | !pwd_authenticated && geteuid() != pw->pw_uid) |
0fff78ff | 170 | return (0); |
3c0ef626 | 171 | } |
0fff78ff | 172 | return (1); |
3c0ef626 | 173 | } |
174 | ||
0fff78ff | 175 | int |
176 | check_ntsec(const char *filename) | |
3c0ef626 | 177 | { |
22616013 | 178 | return (pathconf(filename, _PC_POSIX_PERMISSIONS)); |
3c0ef626 | 179 | } |
180 | ||
0fff78ff | 181 | void |
182 | register_9x_service(void) | |
e9a17296 | 183 | { |
184 | HINSTANCE kerneldll; | |
185 | DWORD (*RegisterServiceProcess)(DWORD, DWORD); | |
186 | ||
187 | /* The service register mechanism in 9x/Me is pretty different from | |
188 | * NT/2K/XP. In NT/2K/XP we're using a special service starter | |
189 | * application to register and control sshd as service. This method | |
190 | * doesn't play nicely with 9x/Me. For that reason we register here | |
191 | * as service when running under 9x/Me. This function is only called | |
192 | * by the child sshd when it's going to daemonize. | |
193 | */ | |
194 | if (is_winnt) | |
195 | return; | |
0fff78ff | 196 | if (!(kerneldll = LoadLibrary("KERNEL32.DLL"))) |
e9a17296 | 197 | return; |
0fff78ff | 198 | if (!(RegisterServiceProcess = (DWORD (*)(DWORD, DWORD)) |
199 | GetProcAddress(kerneldll, "RegisterServiceProcess"))) | |
e9a17296 | 200 | return; |
201 | RegisterServiceProcess(0, 1); | |
202 | } | |
203 | ||
996d5e62 | 204 | #define NL(x) x, (sizeof (x) - 1) |
205 | #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0])) | |
206 | ||
207 | static struct wenv { | |
208 | const char *name; | |
209 | size_t namelen; | |
210 | } wenv_arr[] = { | |
211 | { NL("ALLUSERSPROFILE=") }, | |
212 | { NL("COMMONPROGRAMFILES=") }, | |
213 | { NL("COMPUTERNAME=") }, | |
214 | { NL("COMSPEC=") }, | |
dec6d9fe | 215 | { NL("CYGWIN=") }, |
996d5e62 | 216 | { NL("NUMBER_OF_PROCESSORS=") }, |
217 | { NL("OS=") }, | |
218 | { NL("PATH=") }, | |
219 | { NL("PATHEXT=") }, | |
220 | { NL("PROCESSOR_ARCHITECTURE=") }, | |
221 | { NL("PROCESSOR_IDENTIFIER=") }, | |
222 | { NL("PROCESSOR_LEVEL=") }, | |
223 | { NL("PROCESSOR_REVISION=") }, | |
224 | { NL("PROGRAMFILES=") }, | |
225 | { NL("SYSTEMDRIVE=") }, | |
226 | { NL("SYSTEMROOT=") }, | |
227 | { NL("TMP=") }, | |
228 | { NL("TEMP=") }, | |
dec6d9fe | 229 | { NL("WINDIR=") } |
996d5e62 | 230 | }; |
231 | ||
232 | char ** | |
233 | fetch_windows_environment(void) | |
234 | { | |
235 | char **e, **p; | |
9108f8d9 | 236 | unsigned int i, idx = 0; |
996d5e62 | 237 | |
9108f8d9 | 238 | p = xcalloc(WENV_SIZ + 1, sizeof(char *)); |
996d5e62 | 239 | for (e = environ; *e != NULL; ++e) { |
240 | for (i = 0; i < WENV_SIZ; ++i) { | |
241 | if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen)) | |
242 | p[idx++] = *e; | |
243 | } | |
244 | } | |
245 | p[idx] = NULL; | |
246 | return p; | |
247 | } | |
248 | ||
249 | void | |
250 | free_windows_environment(char **p) | |
251 | { | |
252 | xfree(p); | |
253 | } | |
254 | ||
3c0ef626 | 255 | #endif /* HAVE_CYGWIN */ |